Skip to content

Commit b91c2c7

Browse files
committed
Make Sound API backwards compatible
Also make it easier to sound single tone
1 parent b242d15 commit b91c2c7

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

ev3dev/core.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,8 +2299,10 @@ def beep(args=''):
22992299
return Popen('/usr/bin/beep %s' % args, stdout=n, shell=True)
23002300

23012301
@staticmethod
2302-
def tone(tone_sequence):
2302+
def tone(*args):
23032303
"""
2304+
tone(tone_sequence):
2305+
23042306
Play tone sequence. The tone_sequence parameter is a list of tuples,
23052307
where each tuple contains up to three numbers. The first number is
23062308
frequency in Hz, the second is duration in milliseconds, and the third
@@ -2329,16 +2331,28 @@ def tone(tone_sequence):
23292331
(392, 350, 100), (311.13, 250, 100), (466.16, 25, 100),
23302332
(392.00, 300, 150), (311.13, 250, 100), (466.16, 25, 100), (392, 700)
23312333
]).wait()
2334+
2335+
tone(frequency, duration):
2336+
2337+
Play single tone of given frequency (Hz) and duration (milliseconds).
23322338
"""
2333-
def beep_args(frequency=None, duration=None, delay=None):
2334-
args = '-n '
2335-
if frequency is not None: args += '-f %s ' % frequency
2336-
if duration is not None: args += '-l %s ' % duration
2337-
if delay is not None: args += '-d %s ' % delay
2339+
def play_tone_sequence(tone_sequence):
2340+
def beep_args(frequency=None, duration=None, delay=None):
2341+
args = '-n '
2342+
if frequency is not None: args += '-f %s ' % frequency
2343+
if duration is not None: args += '-l %s ' % duration
2344+
if delay is not None: args += '-d %s ' % delay
23382345

2339-
return args
2346+
return args
23402347

2341-
return Sound.beep(' '.join([beep_args(*t) for t in tone_sequence]))
2348+
return Sound.beep(' '.join([beep_args(*t) for t in tone_sequence]))
2349+
2350+
if len(args) == 1:
2351+
return play_tone_sequence(args[0])
2352+
elif len(args) == 2:
2353+
return play_tone_sequence([(args[0], args[1])])
2354+
else:
2355+
raise Exception("Unsupported number of parameters in Sound.tone()")
23422356

23432357
@staticmethod
23442358
def play(wav_file):

0 commit comments

Comments
 (0)