How to set playback volume for soundfile in Google AIY voice kit on Pi Zero in python?

279 Views Asked by At

I'm using Google AIY Voice Kit (v2) with Raspberry Pi Zero to build a voice-control robot. It's working great! But I have an elementary question. While the robot is processing user speech (and deciding how to respond) I want to play a short sound file to indicate the robot is "thinking." The sound file is currently playing too loud. How to set the playback volume of a soundfile in python?

Here's a snippet of code:

aiy.voice.audio.play_wav_async("think.wav")

This plays successfully, but I can't figure out how to set the volume the way I can set volume in the text to speech function aiy.voice.tts.say(sentence, lang='en-GB', volume=10, pitch=120, speed=90, device='default')

Many thanks for any suggestions!

1

There are 1 best solutions below

2
On

So this is a very hacky way to look at this problem, but after reading the AIY documentation and seeing that it is straight-up reading the bytes of the file pointer with no option to set volume [or anything else] I think hacks might be the best route.

Let's take your input file, modify it, then save it back as a tmp file to be read into AIY.

We can do something like the following:

# Throw this line somewhere higher up so you can edit it
# like you would the volume level in 
# aiy.voice.tts.say(sentence, lang='en-GB', volume=10, pitch=120, speed=90, device='default')
# or just replace the later reference with the int and modify that line directly instead
HOW_MUCH_QUIETER: 10
from pydub import AudioSegment

song = AudioSegment.from_wav("think.wav")

# reduce volume by 10 dB
song_10_db_quieter = song - HOW_MUCH_QUIETER

# save the output
song.export("quieter.wav", "wave")

aiy.voice.audio.play_wav_async("quieter.wav")