Basic sound¶
Prerequisite¶
What students should know before:
No prerequisites
Time constraints:
starting from 45 min - single lesson
Preparing For This Tutorial:¶
The LEGO Mindstorm EV3 – only brick.
Effects¶
Computer science – Students learn how to work with the functions of the Sound class. Students will create a program that will allow the robot to play wav music files, play songs from sheet music and read sentences.
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
Example solution¶
Computer science
Step 1:
Check this text to create a program that will play the wav file, play any beep, and read the sentence “Welcome to the E V 3 development project”.
Program#!/usr/bin/env python3
#!/usr/bin/env python3
from ev3dev.ev3 import \*
Sound.beep(440).wait()
Sound.play('/home/robot/sounds/Activate.wav').wait()
Sound.tone('440',200).wait()
Sound.play('/home/robot/sounds/Air release.wav').wait()
Sound.speak('Welcome to the E V 3 dev project!').wait()
#the same result, but first we create instance of an object of Sound class
sound=Sound()
sound.beep(440).wait()
sound.play('/home/robot/sounds/Activate.wav').wait()
sound.tone('440',200).wait()
sound.play('/home/robot/sounds/Air release.wav').wait()
sound.speak('Welcome to the E V 3 dev project!').wait()
File here
Note. If you omit the wait function, you may get the following error “Device or resource busy”. Below we present the same program written using the ev3dev2 library. We don’t have the wait command - because in every function we have a play_type argument with 0 defaults.
(PLAY_WAIT_FOR_COMPLE). Moreover, we obtain a new possibility like the change in the volume.
#!/usr/bin/env python3
from ev3dev2.sound import Sound
sound=Sound()
sound.beep(440)
sound.tone(440,200)
sound.play('/home/robot/sounds/Air release.wav')
sound.play_file('/home/robot/sounds/Air release.wav',volume=30)
sound.speak('Welcome to the E V 3 dev project!')
sound.speak('Welcome to the E V 3 dev project!',volume=10)
#or
#I prefer this version presented above because is according to object
#oriented programming
#Sound().beep(440)
#Sound().tone(440,200)
#Sound().play('/home/robot/sounds/Air release.wav')
#Sound().play_file('/home/robot/sounds/Air release.wav',volume=30)
#Sound().speak('Welcome to the E V 3 dev project!')
#Sound().speak('Welcome to the E V 3 dev project!',volume=10)
File: here
Step 2.
Create a programme to make the robot play your favourite song.
Play a song provided as a list of tuples containing the note name and it’s 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 (‘D4’,’e3’), (‘D4’,’e3’), (‘D4’,’e3’).
Program
#!/usr/bin/env python3
from ev3dev.ev3 import \*
Sound.speak('Frere Jacques ').wait()
Sound.play_song((
('G4', 'q'), #1
('A4', 'q'),
('B4', 'q'), #2
('G4', 'q'),
('G4', 'q'), #3
('A4', 'q'),
('B4', 'q'), #4
('G4', 'q'),
('C5', 'q'), #5
('D5', 'q'),
('E5', 'h'), #7
('C5', 'q'), #8
('D5', 'q'),
('E5', 'h'), #9
('E5', 'e'), #10
('F5', 'e'),
('E5', 'e'),
('D5', 'e'),
('B4', 'q'),#11
('G4','q'),
('E5', 'e'), #12
('F5', 'e'),
('E5', 'e'),
('D5', 'e'),
('B4', 'q'),#13
('G4','q'),
('G4','q'),#14
('D4','q'),
('G4','h'),#15
('G4','q'),#16
('D4','q'),
('G4','h'),#17
))
#!/usr/bin/env python3
from ev3dev2.sound import Sound
Sound().speak('Frere Jacques ')
Sound().play_song((
('G4', 'q'),# ... as above
('G4','h'),#17
))
File here