Skip to content

Commit 57b2821

Browse files
committed
Updated generate_sample and start_tone to 3.0
Added in checks for backwards compatibility with 2.x.
1 parent c198e84 commit 57b2821

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

adafruit_circuitplayground/express.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,14 @@ def _generate_sample(self):
594594
if self._sample is not None:
595595
return
596596
length = 100
597-
self._sine_wave = array.array("H", Express._sine_sample(length))
598-
self._sample = audioio.AudioOut(board.SPEAKER, self._sine_wave)
597+
if sys.implementation.version[0] >= 3:
598+
self._sine_wave = array.array("H", Express._sine_sample(length))
599+
self._sample = audioio.AudioOut(board.SPEAKER)
600+
self._sine_wave_sample = audioio.RawSample(self._sine_wave)
601+
else:
602+
self._sine_wave = array.array("H", Express._sine_sample(length))
603+
self._sample = audioio.AudioOut(board.SPEAKER, self._sine_wave)
604+
599605

600606
def play_tone(self, frequency, duration):
601607
""" Produce a tone using the speaker. Try changing frequency to change
@@ -642,9 +648,14 @@ def start_tone(self, frequency):
642648
self._speaker_enable.value = True
643649
self._generate_sample()
644650
# Start playing a tone of the specified frequency (hz).
645-
self._sample.frequency = int(len(self._sine_wave) * frequency)
646-
if not self._sample.playing:
647-
self._sample.play(loop=True)
651+
if sys.implementation.version[0] >= 3:
652+
self._sine_wave_sample.sample_rate = int(len(self._sine_wave) * frequency)
653+
if not self._sample.playing:
654+
self._sample.play(self._sine_wave_sample, loop=True)
655+
else:
656+
self._sample.frequency = int(len(self._sine_wave) * frequency)
657+
if not self._sample.playing:
658+
self._sample.play(loop=True)
648659

649660
def stop_tone(self):
650661
""" Use with start_tone to stop the tone produced.

0 commit comments

Comments
 (0)