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)])
Play a WAV sound file
To play a WAV sound file, use play_file(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_file('/home/robot/sounds/Dog bark 1.wav')
sound.beep()
Text-to-Speech
Use speak(text, play_type=0)
Try this script below, which should be self-explanatory. Note that you can control the amplitude (loudness), the speed and the language, and that you can croak and whisper if you wish! The speak() function actually controls an underlying espeak() function. The espeak() function has default values for amplitude and speed of 100 and 175 but these are overridden by the default values of speak() which are 200 and 130. For more information see espeak.sourceforge.net . Note how the English text has been deformed slightly to make it sound better. Try changing the comment/uncomment to hear the unmodified text.
#!/usr/bin/env python3
from ev3dev2.sound import Sound
sound = Sound()
# see http://espeak.sourceforge.net/
# values -a 200 -s 130 SHOULD BE INCLUDED if specifying any other options
# a = amplitude (200 max, 100 default), s = speed 80-500, default = 175)
opts = '-a 200 -s 130 -v'
# str_en = "I think you ought to know, I'm feeling very depressed"
str_en = "I think you ought to know, eyem feeling very depressed"
# Default voice = English, male
sound.speak(str_en)
# English, male1 (lowest male tone)
sound.speak(str_en, espeak_opts='-a 200 -s 130 -ven+m1') # long form
# English, male7 (highest male tone)
sound.speak(str_en, espeak_opts=opts+'en+m7') # using my variable 'opts'
# English, female1 (lowest female tone)
sound.speak(str_en, espeak_opts=opts+'en+f1')
# English, female5 (highest female tone)
sound.speak(str_en, espeak_opts=opts+'en+f5')
# croak
sound.speak(str_en, espeak_opts=opts+'en+croak')
# whisper
sound.speak(str_en, espeak_opts=opts+'en+whisper')
# en-us = US English
sound.speak(str_en, espeak_opts=opts+'en-us')
# en-rp = Received pronunciation ('BBC English')
sound.speak(str_en, espeak_opts=opts+'en-rp')
# s = 80 is slowest possible speed
sound.speak(str_en, espeak_opts='-a 200 -s 80')
# s = 300 is a high speed
sound.speak(str_en, espeak_opts='-a 200 -s 300')
# French
str_fr = 'Le chat est sous la chaise! Le singe est sur la branche!'
sound.speak(str_fr, espeak_opts=opts+'fr')
# French, high pitched male
sound.speak(str_fr, espeak_opts=opts+'fr+m7')
# French, rather low pitched female
sound.speak(str_fr, espeak_opts=opts+'fr+f2')
# See http://espeak.sourceforge.net/languages.html
# for other languages
See also 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!')