Skip to content

Commit f3fe81e

Browse files
authored
Sound: removed play() in favor of play_file() (ev3dev#552)
Resolves issue ev3dev#532
1 parent 3a5054e commit f3fe81e

2 files changed

Lines changed: 17 additions & 20 deletions

File tree

‎debian/changelog‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
python-ev3dev2 (2.0.0~beta3) stable; urgency=medium
22

3+
* Sound: removed play() in favor of play_file()
34
* suppress unittest ResourceWarning messages
45
* Added DistanceValue classes
56
* wait_until_not_moving should consider "running holding" as "not moving"

‎ev3dev2/sound.py‎

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Sound(object):
5656
Examples::
5757
5858
# Play 'bark.wav':
59-
Sound.play('bark.wav')
59+
Sound.play_file('bark.wav')
6060
6161
# Introduce yourself:
6262
Sound.speak('Hello, I am Robot')
@@ -98,7 +98,7 @@ def beep(self, args='', play_type=PLAY_WAIT_FOR_COMPLETE):
9898
See `beep man page`_ and google `linux beep music`_ for inspiration.
9999
100100
:param string args: Any additional arguments to be passed to ``beep`` (see the `beep man page`_ for details)
101-
101+
102102
:param play_type: The behavior of ``beep`` once playback has been initiated
103103
:type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_NO_WAIT_FOR_COMPLETE``
104104
@@ -247,16 +247,27 @@ def play_note(self, note, duration, volume=100, play_type=PLAY_WAIT_FOR_COMPLETE
247247

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

250-
def play(self, wav_file, play_type=PLAY_WAIT_FOR_COMPLETE):
251-
""" Play a sound file (wav format).
250+
def play_file(self, wav_file, volume=100, play_type=PLAY_WAIT_FOR_COMPLETE):
251+
""" Play a sound file (wav format) at a given volume.
252252
253253
:param string wav_file: The sound file path
254+
:param int volume: The play volume, in percent of maximum volume
254255
255-
:param play_type: The behavior of ``play`` once playback has been initiated
256+
:param play_type: The behavior of ``play_file`` once playback has been initiated
256257
:type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE``, ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_LOOP``
257258
258259
:returns: When ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` is specified, returns the spawn subprocess from ``subprocess.Popen``; ``None`` otherwise
259260
"""
261+
if not 0 < volume <= 100:
262+
raise ValueError('invalid volume (%s)' % volume)
263+
264+
if not wav_file.endswith(".wav"):
265+
raise ValueError('invalid sound file (%s), only .wav files are supported' % wav_file)
266+
267+
if not os.path.isfile(wav_file):
268+
raise ValueError("%s does not exist" % wav_file)
269+
270+
self.set_volume(volume)
260271
self._validate_play_type(play_type)
261272

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

277-
def play_file(self, wav_file, volume=100, play_type=PLAY_WAIT_FOR_COMPLETE):
278-
""" Play a sound file (wav format) at a given volume.
279-
280-
281-
:param string wav_file: The sound file path
282-
:param int volume: The play volume, in percent of maximum volume
283-
284-
:param play_type: The behavior of ``play_file`` once playback has been initiated
285-
:type play_type: ``Sound.PLAY_WAIT_FOR_COMPLETE``, ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` or ``Sound.PLAY_LOOP``
286-
287-
:returns: When ``Sound.PLAY_NO_WAIT_FOR_COMPLETE`` is specified, returns the spawn subprocess from ``subprocess.Popen``; ``None`` otherwise
288-
"""
289-
self.set_volume(volume)
290-
self.play(wav_file, play_type)
291-
292288
def speak(self, text, espeak_opts='-a 200 -s 130', volume=100, play_type=PLAY_WAIT_FOR_COMPLETE):
293289
""" Speak the given text aloud.
294290

0 commit comments

Comments
 (0)