@@ -611,7 +611,7 @@ def volume(self):
611
611
@volume .setter
612
612
def volume (self , value ):
613
613
self ._volume = value
614
- self .value = (self .freq , self ._volume )
614
+ self .value = (self .freq , self .volume )
615
615
616
616
@property
617
617
def freq (self ):
@@ -622,14 +622,16 @@ def freq(self):
622
622
623
623
@freq .setter
624
624
def freq (self , freq ):
625
- self ._pwm_buzzer . freq (freq )
626
-
625
+ self .value = (freq , self . volume )
626
+
627
627
def _write (self , value ):
628
628
# set the frequency
629
- self ._pwm_buzzer .freq = value [0 ]
629
+ if value [0 ] is not None :
630
+ self ._pwm_buzzer .freq = value [0 ]
630
631
631
632
# write the volume value
632
- self ._pwm_buzzer .volume = value [1 ]
633
+ if value [1 ] is not None :
634
+ self ._pwm_buzzer .volume = value [1 ]
633
635
634
636
def _to_freq (self , freq ):
635
637
if freq is not None and freq != '' and freq != 0 :
@@ -688,7 +690,7 @@ def play(self, tune=440, duration=1, volume=1, n=1, wait=True):
688
690
+ a frequency in Hz e.g. `440`
689
691
+ a midi note e.g. `60`
690
692
+ a note name as a string e.g `"E4"`
691
- + a list of single notes e.g. `[440, 60, "E4"]`
693
+ + a list of note and duration e.g. `[440, 1]` or `[ "E4", 2 ]`
692
694
+ a list of 2 value tuples of (note, duration) e.g. `[(440,1), (60, 2), ("e4", 3)]`
693
695
694
696
Defaults to `440`.
@@ -712,10 +714,13 @@ def play(self, tune=440, duration=1, volume=1, n=1, wait=True):
712
714
# tune isnt a list, so it must be a single frequency or note
713
715
if not isinstance (tune , (list , tuple )):
714
716
tune = [(tune , duration )]
717
+ # if the first element isnt a list, then it must be list of a single note and duration
718
+ elif not isinstance (tune [0 ], (list , tuple )):
719
+ tune = [tune ]
715
720
716
721
def tune_generator ():
717
722
for note in tune :
718
-
723
+
719
724
# note isnt a list or tuple, it must be a single frequency or note
720
725
if not isinstance (note , (list , tuple )):
721
726
# make it into a tuple
@@ -724,12 +729,13 @@ def tune_generator():
724
729
# turn the notes in frequencies
725
730
freq = self ._to_freq (note [0 ])
726
731
freq_duration = note [1 ]
732
+ freq_volume = volume if freq is not None else 0
727
733
728
734
# if this is a tune of greater than 1 note, add gaps between notes
729
735
if len (tune ) == 1 :
730
- yield ((freq , volume ), freq_duration )
736
+ yield ((freq , freq_volume ), freq_duration )
731
737
else :
732
- yield ((freq , volume ), freq_duration * 0.9 )
738
+ yield ((freq , freq_volume ), freq_duration * 0.9 )
733
739
yield ((freq , 0 ), freq_duration * 0.1 )
734
740
735
741
self ._start_change (tune_generator , n , wait )
0 commit comments