Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
python-ev3dev2 (2.0.0~beta3) stable; urgency=medium

* Sound: removed play() in favor of play_file()
* suppress unittest ResourceWarning messages
* Added DistanceValue classes
* wait_until_not_moving should consider "running holding" as "not moving"
Expand Down
36 changes: 16 additions & 20 deletions ev3dev2/sound.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Sound(object):
Examples::
# Play 'bark.wav':
Sound.play('bark.wav')
Sound.play_file('bark.wav')
# Introduce yourself:
Sound.speak('Hello, I am Robot')
Expand Down Expand Up @@ -98,7 +98,7 @@ def beep(self, args='', play_type=PLAY_WAIT_FOR_COMPLETE):
See `beep man page`_ and google `linux beep music`_ for inspiration.
:param string args: Any additional arguments to be passed to ``beep`` (see the `beep man page`_ for details)
:param play_type: The behavior of ``beep`` once playback has been initiated
:type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_NO_WAIT_FOR_COMPLETE``
Expand Down Expand Up @@ -247,16 +247,27 @@ def play_note(self, note, duration, volume=100, play_type=PLAY_WAIT_FOR_COMPLETE

return self.play_tone(freq, duration=duration, volume=volume, play_type=play_type)

def play(self, wav_file, play_type=PLAY_WAIT_FOR_COMPLETE):
""" Play a sound file (wav format).
def play_file(self, wav_file, volume=100, play_type=PLAY_WAIT_FOR_COMPLETE):
""" Play a sound file (wav format) at a given volume.
:param string wav_file: The sound file path
:param int volume: The play volume, in percent of maximum volume
:param play_type: The behavior of ``play`` once playback has been initiated
:param play_type: The behavior of ``play_file`` once playback has been initiated
:type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE``, ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_LOOP``
:returns: When ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` is specified, returns the spawn subprocess from ``subprocess.Popen``; ``None`` otherwise
"""
if not 0 < volume <= 100:
raise ValueError('invalid volume (%s)' % volume)

if not wav_file.endswith(".wav"):
raise ValueError('invalid sound file (%s), only .wav files are supported' % wav_file)

if not os.path.isfile(wav_file):
raise ValueError("%s does not exist" % wav_file)

self.set_volume(volume)
self._validate_play_type(play_type)

with open(os.devnull, 'w') as n:
Expand All @@ -274,21 +285,6 @@ def play(self, wav_file, play_type=PLAY_WAIT_FOR_COMPLETE):
pid = Popen(shlex.split('/usr/bin/aplay -q "%s"' % wav_file), stdout=n)
pid.wait()

def play_file(self, wav_file, volume=100, play_type=PLAY_WAIT_FOR_COMPLETE):
""" Play a sound file (wav format) at a given volume.
:param string wav_file: The sound file path
:param int volume: The play volume, in percent of maximum volume
:param play_type: The behavior of ``play_file`` once playback has been initiated
:type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE``, ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_LOOP``
:returns: When ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` is specified, returns the spawn subprocess from ``subprocess.Popen``; ``None`` otherwise
"""
self.set_volume(volume)
self.play(wav_file, play_type)

def speak(self, text, espeak_opts='-a 200 -s 130', volume=100, play_type=PLAY_WAIT_FOR_COMPLETE):
""" Speak the given text aloud.
Expand Down