Skip to content

Commit ba44adf

Browse files
committed
Merge pull request #53 from ddemidov/sound-api
Make Sound API backwards compatible
2 parents dbc83a1 + b91c2c7 commit ba44adf

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
@@ -2301,8 +2301,10 @@ def beep(args=''):
23012301
return Popen('/usr/bin/beep %s' % args, stdout=n, shell=True)
23022302

23032303
@staticmethod
2304-
def tone(tone_sequence):
2304+
def tone(*args):
23052305
"""
2306+
tone(tone_sequence):
2307+
23062308
Play tone sequence. The tone_sequence parameter is a list of tuples,
23072309
where each tuple contains up to three numbers. The first number is
23082310
frequency in Hz, the second is duration in milliseconds, and the third
@@ -2331,16 +2333,28 @@ def tone(tone_sequence):
23312333
(392, 350, 100), (311.13, 250, 100), (466.16, 25, 100),
23322334
(392.00, 300, 150), (311.13, 250, 100), (466.16, 25, 100), (392, 700)
23332335
]).wait()
2336+
2337+
tone(frequency, duration):
2338+
2339+
Play single tone of given frequency (Hz) and duration (milliseconds).
23342340
"""
2335-
def beep_args(frequency=None, duration=None, delay=None):
2336-
args = '-n '
2337-
if frequency is not None: args += '-f %s ' % frequency
2338-
if duration is not None: args += '-l %s ' % duration
2339-
if delay is not None: args += '-d %s ' % delay
2341+
def play_tone_sequence(tone_sequence):
2342+
def beep_args(frequency=None, duration=None, delay=None):
2343+
args = '-n '
2344+
if frequency is not None: args += '-f %s ' % frequency
2345+
if duration is not None: args += '-l %s ' % duration
2346+
if delay is not None: args += '-d %s ' % delay
23402347

2341-
return args
2348+
return args
23422349

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

23452359
@staticmethod
23462360
def play(wav_file):

0 commit comments

Comments
 (0)