Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 25 additions & 33 deletions ev3dev2/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from ev3dev2 import is_micropython
import os
import re
from time import sleep

if not is_micropython():
import shlex
Expand All @@ -43,7 +44,7 @@ def _make_scales(notes):
for note, freq in notes:
freq = round(freq)
for n in note.split('/'):
res[n] = freq
Comment thread
WasabiFan marked this conversation as resolved.
Resolved
res[n.upper()] = freq
return res


Expand Down Expand Up @@ -406,6 +407,7 @@ def play_song(self, song, tempo=120, delay=0.05):
and duration.

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.
Expand Down Expand Up @@ -471,39 +473,29 @@ def play_song(self, song, tempo=120, delay=0.05):
delay_ms = int(delay * 1000)
meas_duration_ms = 60000 / tempo * 4 # we only support 4/4 bars, hence "* 4"

def beep_args(note, value):
""" Builds the arguments string for producing a beep matching
the requested note and value.

Args:
note (str): the note note and octave
value (str): the note value expression
Returns:
str: the arguments to be passed to the beep command
"""
freq = self._NOTE_FREQUENCIES.get(note.upper(), self._NOTE_FREQUENCIES[note])

if '/' in value:
base, factor = value.split('/')
duration_ms = meas_duration_ms * self._NOTE_VALUES[base] / float(factor)
elif '*' in value:
base, factor = value.split('*')
duration_ms = meas_duration_ms * self._NOTE_VALUES[base] * float(factor)
elif value.endswith('.'):
base = value[:-1]
duration_ms = meas_duration_ms * self._NOTE_VALUES[base] * 1.5
elif value.endswith('3'):
base = value[:-1]
duration_ms = meas_duration_ms * self._NOTE_VALUES[base] * 2 / 3
else:
duration_ms = meas_duration_ms * self._NOTE_VALUES[value]

return '-f %d -l %d -D %d' % (freq, duration_ms, delay_ms)

try:
return self.beep(' -n '.join(
[beep_args(note, value) for (note, value) in song]
))
for (note, value) in song:
value = value.lower()
if '/' in value:
base, factor = value.split('/')
duration_ms = meas_duration_ms * self._NOTE_VALUES[base] / float(factor)
elif '*' in value:
base, factor = value.split('*')
duration_ms = meas_duration_ms * self._NOTE_VALUES[base] * float(factor)
elif value.endswith('.'):
base = value[:-1]
duration_ms = meas_duration_ms * self._NOTE_VALUES[base] * 1.5
elif value.endswith('3'):
base = value[:-1]
duration_ms = meas_duration_ms * self._NOTE_VALUES[base] * 2 / 3
else:
duration_ms = meas_duration_ms * self._NOTE_VALUES[value]

if note == "R":
sleep(duration_ms / 1000 + delay)
else:
freq = self._NOTE_FREQUENCIES[note.upper()]
self.beep('-f %d -l %d -D %d' % (freq, duration_ms, delay_ms))
except KeyError as e:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this try...except around the whole thing is scary. Can we move it to the smallest relevant statement, probably the freq lookup?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you get a chance to change this I'll merge the PR.

raise ValueError('invalid note (%s)' % e)

Expand Down