Skip to content

Commit 599f44b

Browse files
authored
Update picozero.py
More flexibility in specifying lists of notes without duration. #21
1 parent 1eedd1f commit 599f44b

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

picozero/picozero.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -524,25 +524,31 @@ def _to_freq(self, freq):
524524
else:
525525
return None
526526

527-
def play(self, tune=440, duration=1, volume=1, n=1, wait=True):
527+
def play(self, tune=440, duration=1, volume=1, n=1, wait=True):
528528
if type(tune) is not list: # use note and duration, no generator
529529
self._pitch(tune, duration, volume, wait)
530-
elif type(tune[0]) is not list: # single note don't use a generator
530+
elif type(tune[0]) is not list and len(tune) <= 2: # single note don't use a generator
531531
self._pitch(tune[0], tune[1], volume, wait)
532532
else: # tune with multiple notes
533533
def tune_generator():
534+
next_duration = duration
534535
for next in tune:
535-
note = next[0]
536-
if len(next) == 2:
537-
duration = float(next[1])
536+
if type(next) is not list:
537+
note = next
538+
else:
539+
note = next[0]
540+
if type(next) is list and len(next) == 2:
541+
if next[1] is not None:
542+
next_duration = float(next[1])
538543
if note == '' or note is None:
539-
yield ((None, 0), duration)
544+
yield ((None, 0), next_duration)
540545
else: # leave a gap between notes
541-
yield ((note, volume), duration * 0.9)
542-
yield ((None, 0), duration * 0.1)
546+
yield ((note, volume), next_duration * 0.9)
547+
yield ((None, 0), next_duration* 0.1)
543548

544549
self.off()
545550
self._start_change(tune_generator, n, wait)
551+
546552

547553
def _stop(self, timer_obj=None):
548554
self.off()

0 commit comments

Comments
 (0)