Examples:
from ev3dev2.sound import Sound
spkr = Sound()
# Play 'bark.wav':
spkr.play_file('bark.wav')
# Introduce yourself:
spkr.speak('Hello, I am Robot')
# Play a small song
spkr.play_song((
('D4', 'e3'),
('D4', 'e3'),
('D4', 'e3'),
('G4', 'h'),
('D5', 'h')
))In order to mimic EV3-G API parameters, durations used in methods exposed as EV3-G blocks for sound related operations are expressed as a float number of seconds.
Any additional arguments to be passed to beep (see the `beep man page`_ for details)
The behavior of beep once playback has been initiated
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the spawn subprocess from subprocess.Popen; None otherwise
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the spawn subprocess from subprocess.Popen; None otherwise
The sound file path
The play volume, in percent of maximum volume
The behavior of play_file once playback has been initiated
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the spawn subprocess from subprocess.Popen; None otherwise
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the spawn subprocess from subprocess.Popen; None otherwise
The note symbol with its octave number
Tone duration, in seconds
The play volume, in percent of maximum volume
The behavior of play_note once playback has been initiated
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the PID of the underlying beep command; None otherwise
is invalid parameter (note, duration,...)
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the PID of the underlying beep command; None otherwise
It supports symbolic notes (e.g. A4, D#3, Gb5) and durations (e.g. q, h). You can also specify rests by using R instead of note pitch.
For an exhaustive list of accepted note symbols and values, have a look at the _NOTE_FREQUENCIES and _NOTE_VALUES private dictionaries in the source code.
The value can be suffixed by modifiers:
a divider introduced by a / to obtain triplets for instance (e.g. q/3 for a triplet of eight note)
a multiplier introduced by * (e.g. *1.5 is a dotted note).
Shortcuts exist for common modifiers:
3 produces a triplet member note. For instance e3 gives a triplet of eight notes, i.e. 3 eight notes in the duration of a single quarter. You must ensure that 3 triplets notes are defined in sequence to match the count, otherwise the result will not be the expected one.
. produces a dotted note, i.e. which duration is one and a half the base one. Double dots are not currently supported.
Example:
>>> # A long time ago in a galaxy far,
>>> # far away...
>>> from ev3dev2.sound import Sound
>>> spkr = Sound()
>>> spkr.play_song((
>>> ('D4', 'e3'), # intro anacrouse
>>> ('D4', 'e3'),
>>> ('D4', 'e3'),
>>> ('G4', 'h'), # meas 1
>>> ('D5', 'h'),
>>> ('C5', 'e3'), # meas 2
>>> ('B4', 'e3'),
>>> ('A4', 'e3'),
>>> ('G5', 'h'),
>>> ('D5', 'q'),
>>> ('C5', 'e3'), # meas 3
>>> ('B4', 'e3'),
>>> ('A4', 'e3'),
>>> ('G5', 'h'),
>>> ('D5', 'q'),
>>> ('C5', 'e3'), # meas 4
>>> ('B4', 'e3'),
>>> ('C5', 'e3'),
>>> ('A4', 'h.'),
>>> ))Important
Only 4/4 signature songs are supported with respect to note durations.
the song
the song tempo, given in quarters per minute
delay between notes (in seconds)
When python3 is used the spawn subprocess from subprocess.Popen is returned; None otherwise
if invalid note in song or invalid play parameters
When python3 is used the spawn subprocess from subprocess.Popen is returned; None otherwise
the tone frequency, in Hertz
Tone duration, in seconds
Delay after tone, in seconds (can be useful when chaining calls to play_tone)
The play volume, in percent of maximum volume
The behavior of play_tone once playback has been initiated
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the PID of the underlying beep command; None otherwise
if invalid parameter
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the PID of the underlying beep command; None otherwise
Uses the espeak external command.
The text to speak
espeak command options (advanced usage)
The play volume, in percent of maximum volume
The behavior of speak once playback has been initiated
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the spawn subprocess from subprocess.Popen; None otherwise
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the spawn subprocess from subprocess.Popen; None otherwise
Play tone sequence.
Here is a cheerful example:
my_sound = Sound()
my_sound.tone([
(392, 350, 100), (392, 350, 100), (392, 350, 100), (311.1, 250, 100),
(466.2, 25, 100), (392, 350, 100), (311.1, 250, 100), (466.2, 25, 100),
(392, 700, 100), (587.32, 350, 100), (587.32, 350, 100),
(587.32, 350, 100), (622.26, 250, 100), (466.2, 25, 100),
(369.99, 350, 100), (311.1, 250, 100), (466.2, 25, 100), (392, 700, 100),
(784, 350, 100), (392, 250, 100), (392, 25, 100), (784, 350, 100),
(739.98, 250, 100), (698.46, 25, 100), (659.26, 25, 100),
(622.26, 25, 100), (659.26, 50, 400), (415.3, 25, 200), (554.36, 350, 100),
(523.25, 250, 100), (493.88, 25, 100), (466.16, 25, 100), (440, 25, 100),
(466.16, 50, 400), (311.13, 25, 200), (369.99, 350, 100),
(311.13, 250, 100), (392, 25, 100), (466.16, 350, 100), (392, 250, 100),
(466.16, 25, 100), (587.32, 700, 100), (784, 350, 100), (392, 250, 100),
(392, 25, 100), (784, 350, 100), (739.98, 250, 100), (698.46, 25, 100),
(659.26, 25, 100), (622.26, 25, 100), (659.26, 50, 400), (415.3, 25, 200),
(554.36, 350, 100), (523.25, 250, 100), (493.88, 25, 100),
(466.16, 25, 100), (440, 25, 100), (466.16, 50, 400), (311.13, 25, 200),
(392, 350, 100), (311.13, 250, 100), (466.16, 25, 100),
(392.00, 300, 150), (311.13, 250, 100), (466.16, 25, 100), (392, 700)
])Have also a look at :py:meth:`play_song` for a more musician-friendly way of doing, which uses the conventional notation for notes and durations.
The sequence of tones to play. The first number of each tuple 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.
The behavior of tone once playback has been initiated
The frequency of the tone in Hz
The duration of the tone in milliseconds
The behavior of tone once playback has been initiated
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the spawn subprocess from subprocess.Popen; None otherwise
tone(frequency, duration)
Play single tone of given frequency and duration.
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the spawn subprocess from subprocess.Popen; None otherwise
tone(frequency, duration)
Play single tone of given frequency and duration.
When python3 is used and Sound.PLAY_NO_WAIT_FOR_COMPLETE is specified, returns the spawn subprocess from subprocess.Popen; None otherwise