Learn EV3 Python‎ > ‎

Sound

To play sounds you must first import the Sound class and create an instance of that class so every script below includes
from ev3dev2.sound import Sound
sound = Sound()

All the sound functions described here have a play_type parameter which is 0 by default. Here are the meanings of the three possible play_type values:
  • 0: Play the sound and block the program until it completes. This is the default.
  • 1: Play the sound without blocking the program.
  • 2: Play the sound as a loop, blocking the program. The loop can only be ended by killing the program, so this option is not very useful.
Play a standard beep
Use  beep(play_type=0)

Example: 

#!/usr/bin/env python3
from ev3dev2.sound import Sound

sound = Sound()

sound.beep()

Note that by default the beep() command will block (pause) program execution until the sound has finished playing. For more information see the official documentation.

Play a SINGLE tone
To play a SINGLE tone of given frequency (Hz) and duration (seconds), use

play_tone(frequency, duration, play_type=0)

Example: To play a single tone of frequency 1500 Hz for 2 seconds (2000 milliseconds):

#!/usr/bin/env python3
from ev3dev2.sound import Sound

sound = Sound()

sound.play_tone(1500, 2)


Play a SEQUENCE of tones
Currently (October 2018) the new play_tone() function cannot play a tone sequence but the old tone() function can. Therefore, to play a SEQUENCE of tones, use   tone(tone_sequence)

The tone_sequence parameter is a list of tuples, where each tuple contains up to three numbers. You should know by now that a tuple is like a list except that a list can be modified and a tuple cannot. The first number is frequency in Hz, the second is duration in milliseconds, and the third is delay in milliseconds between this and the next tone in the sequence.

#!/usr/bin/env python3

from ev3dev2.sound import Sound

sound = Sound()

# Play a 200Hz tone for 2 seconds and then wait 0.4 seconds
# before playing the next tone, 800Hz for 1 second
# followed by a 3 second delay
sound.tone([(200, 2000, 400),(800, 1000, 3000)])

Official documentation.

Play a WAV sound file
To play a WAV sound file, use     play(wav_file, play_type=0 You cannot use mp3 files with the play command, nor can you use Lego sound files in the RSF or RSO format. I have made available at the bottom of this page a ZIP file containing all the Lego sounds in WAV format. All the sounds are in the same folder, not separated into different folders as in EV3-G. All the scripts on this site that make use of these sounds assume that you have put them all in a 'sounds' folder inside the 'robot' folder. Therefore the address of the 'sounds' folder will be /home/robot/sounds/ . The easy way to do that is to open the sounds folder in VS Code then click the 'send project to device' icon in the header of the EV3 Device Browser. It's as simple as that! My scripts assume that you have downloaded the EV3 sounds to the EV3 as just described. This is similar to the way my scripts assume that you have placed all the Lego images in BMP format in a folder called 'pics' in the 'robot' folder, as described on this page. If you flash a new Stretch image to your SD card then the 'sounds' and 'pics' folders (and all your scripts) will be deleted so you will need to download them to the EV3 again.

Here is a list of the EV3 sounds, without the .wav file extensions:


A good source for additional free WAV sound files is Wavsource.com. I did have a problem with a WAV file that I downloaded from there though - I got an error message 'underrun' when I ran my script, which suggests that the sound file was damaged. If you get the same message then delete that sound file and try another. If you want to add additional WAV sound files into the 'sounds' folder you have already created as suggested above then just add the WAV files to the existing files in the 'sounds' folder on your PC then download the whole folder to the EV3 again as described above.

Once you have a 'sounds' folder containing, amongst others, the 'Dog bark 1.wav' file inside your 'robot' folder on the EV3 you should be able to run the following script. A beep sound is included to prove to you that the program will pause (block) until the dog has finished barking before playing the beep:

#!/usr/bin/env python3
from ev3dev2.sound import Sound
from time import sleep

sound = Sound()

sound.play('/home/robot/sounds/Dog bark 1.wav')
sound.beep()



Text-to-Speech
Use  speak(text, play_type=0)
See the example at the bottom of the combined example below. There seems to be (as of October 2018) a small bug: if your string ends with a period then the spoken words will end with the word 'dot'. Solution: either omit the final period or include a space after it.

Combined Example
An example of a program to demonstrate playing beeps, tones and text-to-speech:
#!/usr/bin/env python3
from ev3dev2.sound import Sound
from time import sleep

sound = Sound()

#play a standard beep
sound.beep()

sleep(2) # pause for 2 seconds

# Play a SINGLE 2000 Hz tone for 1.5 seconds
sound.play_tone(2000, 1.5)

sleep(2)

# Play a SEQUENCE of tones
sound.tone([(200, 2000, 400),(800, 1800, 2000)])

sleep(2)

# Play a 500 Hz tone for 1 second and then wait 0.4 seconds
# before playing the next tone
# Play the tone three times
sound.tone([(500, 1000, 400)] * 3)

sleep(2)

#text to speech
sound.speak('Hello, my name is E V 3!')
ċ
sounds.zip
(1736k)
Nigel Ward,
19 Sep 2017, 11:04
Comments