Basic sound¶
Preparing For This Tutorial:¶
The LEGO Mindstorm EV3 – only brick.
Time constraints:
starting from 45 min - single lesson
Exercise¶
Prepare wav format files. Take sound.zip file from page https://sites.google.com/site/ev3python/learn_ev3_python/loudspeaker_speech where you can find all LEGO MINDSTORMS EDUCATION files converted to wav format.
Create a special directory called ‘sounds’ in your robot’s directory and copy into it wav files (full path /home/robot/sound).
Create a programme to make the robot
will play a wav file, any sound signal and will read the sentence “Welcome to the E V 3 development project”,
will play a song
Short help on programming¶
Commands/functions needed for the exercise
Library: ev3dev
- Play a standard beep
beep(<frequency in Hz>)
- Play a single tone
tone(frequency in Hz,duration in milliseconds)
- Play a sequence of tones
tone(tone_sequence)
tone_sequence – list of tuples.
Each tuple has the form (frequency in Hz, duration in milliseconds, wait in milliseconds)
- Play a WAV sound file
play(wav_file)
- Play a song
play_song(song, tempo=120, delay=0.05) song – list of tuples.
Each tuple has the form(note name, values)
The note name and its value using music conventional notation (see https://newt.phys.unsw.edu.au/jw/notes.html) for frequency and duration. Symbolic notes are acceptable (e.g. A4, D#3, Gb5). For denote durations it should be used w- whole note, h- half note, q-quarter note, e- eighth note, s – sixteenth note. Triplet should be written as for example (‘D4’,’e3’), (‘D4’,’e3’), (‘D4’,’e3’). Speak a text speak(text)
Example:
#!/usr/bin/env python3
Sound.speak(’Hello’)
or
sound=Sound()
sound.speak(’Hello’)
Library: ev3dev2
- Play a standard beep
beep(<frequency in Hz>,play_type=0)
- Play a single tone
tone(frequency in Hz,duration in milliseconds,play_type=0)
- Play a WAV sound file
play_file(wav_file, volume=100, play_type=0)
- Play a song
play_song(song, tempo=120, delay=0.05)
Analogically as above
#!/usr/bin/env python3
Speak a text
speak(text, speak_opts='-a 200 -s 130', volume=100, play_type=0)
a = amplitude (200 max, 100 default), s = speed 80-500, default = 175
play_type
has two options
| Sound.PLAY_WAIT_FOR_COMPLEE=0 (| default)
Sound.PLAY_NO_WAIT_FOR_COMPLETE=1
The behavior once playback has been initiated. We don’t use wait() after this command.
Example
#!/usr/bin/env python3
Sound().speak(’Hello’,volume=50)
or
sound=Sound()
sound.speak(’Hello’,volume=50)
https://ev3dev-lang.readthedocs.io/projects/python-ev3dev/en/2.0.0beta1/other.html