ev3dev / ev3dev-lang-python

Pure python bindings for ev3dev
MIT License
422 stars 146 forks source link

Sound.play_file not producing any audio #760

Closed drew-linerud closed 3 years ago

drew-linerud commented 3 years ago

Hi, I'm not able to play audio from a wav file using Sound.play_file. Here's the code:

from ev3dev2.sound import Sound my_sound = Sound() my_sound.speak('Hello?') my_sound.play_file('Recording.wav', 100, Sound.PLAY_LOOP) my_sound.speak('Done')

If I don't include Sound.PLAY_LOOP in play_file, then the program completes successfully, just never playing the audio in Recording.wav. With PLAY_LOOP specified, the script speaks "Hello?" and then runs indefinitely without making another sound.

I also tried specifying the full path to Recording.wav which didn't seem to make any difference. I verified that the file is on the device in the same folder with the script.

Am I doing something wrong, or is this a bug? Attaching a zip file with my small .wav file.

audio.zip

WasabiFan commented 3 years ago

Internally, play_file uses the aplay command. Could you try running this command and see if it is able to play your file? If not, are there any error messages?

/usr/bin/aplay path/to/Recording.wav
dlech commented 3 years ago

Try normalizing the sound to make it as loud as possible. Currently, the peaks are a small fraction of the available range.

image

Also, 16-bit, mono, 22050Hz sound format works best, otherwise the CPU on the EV3 has to do extra processing to convert the format on the fly.

drew-linerud commented 3 years ago

Converting to 16-bit mono, 22050 Hz (352,800 bit rate) allowed it to work. Thanks for the quick answer! Would be awesome to add that format to the documentation.