Learn EV3 Python‎ > ‎

Sound

Play a standard beep
Use   Sound.beep()

Examples: 

#!/usr/bin/env python3
# so that script can be run from Brickman

from ev3dev.ev3 import *  # needed for Sound

Sound.beep()

or, to cause the program to wait until the beep has completed before continuing:

#!/usr/bin/env python3
# so that script can be run from Brickman

from ev3dev.ev3 import *

Sound.beep().wait()

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

Sound.tone(frequency, duration)

Example:

#!/usr/bin/env python3

from ev3dev.ev3 import *

# Play a SINGLE 1500Hz tone for 2 seconds 
# Wait for the tone to finish playing before continuing
Sound.tone(1500, 2000).wait()


Play a SEQUENCE of tones
To play a SEQUENCE of tones, use
Sound.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
# so that script can be run from Brickman

from ev3dev.ev3 import *  # needed for Sound
# Play a 3000Hz tone for 2 seconds and then wait 0.4 seconds
# before playing the next tone, 800Hz for 1.8 seconds
# followed by a 2 second delay
# Wait for the sequence to finish playing before continuing
Sound.tone([(3000, 2000, 400),(800, 1800, 2000)]).wait()

Play a WAV sound file
To play a WAV sound file, use     Sound.play(wav_file You cannot use mp3 files with the Sound.play command.

A good source for 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. A sample wav file 'cat_yowl.wav' from wavsource.com can be downloaded using the link at the bottom of this page. I suggest you make a directory called 'sounds' in your 'robot' directory. If you're using MobaXTerm free edition it's easy to make a new directory and upload a file to it using the appropriate buttons. Upload the 'cat_yowl.wav' file into your sounds directory and run the following script (assumed to be in your robot directory):

#!/usr/bin/env python3
from ev3dev.ev3 import *
from time import sleep

Sound.play('sounds/cat_yowl.wav').wait()
Sound.beep()

sleep(4)

Sound.play('sounds/cat_yowl.wav')
Sound.beep()

Notice that the first cat yowl plays before the beep sound but the second plays at the same time as (or even after) the beep because there is no .wait() to make program execution pause until the sound has finished playing before continuing.

Text-to-Speech
Use  Sound.speak(text)
Example at the bottom of the combined example below.

Combined Example
An example of a program to demonstrate playing beeps, tones and text-to-speech:

#!/usr/bin/env python3
# so that script can be run from Brickman

from time import sleep   # needed for sleep
from ev3dev.ev3 import *  # needed for Sound

#play a standard beep
Sound.beep()

sleep(2)  # pause for 2 seconds

# Play a SINGLE 1500 Hz tone for 2 seconds
# Wait for the tone to finish playing before continuing
Sound.tone(2000, 1500).wait()

sleep(2)  # pause for 2 seconds

# To play a SEQUENCE of tones, use a LIST of TUPLES
# The third argument in each tuple is the delay in ms
# before the next tone is played.
# Play a 3000 Hz tone for 2 seconds, then wait 0.4 seconds
# before playing the next tone, 800Hz for 1.8 seconds
# followed by a 2 second delay
# Wait for the sequence to finish playing before continuing
Sound.tone([(3000, 2000, 400),(800, 1800, 2000)]).wait()

# 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).wait()

#text to speech
Sound.speak('Hello, my name is E V 3!').wait()
Ć
cat_yowl.wav
(163k)
Nigel Ward,
1 Oct 2016, 10:56
Comments