Skip to content

Commit d0022de

Browse files
authored
Merge pull request #33 from kattni/cp-3-update
Updated generate_sample and start_tone to 3.0
2 parents d82fb23 + 39608f0 commit d0022de

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

adafruit_circuitplayground/express.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def __init__(self):
9898
self._speaker_enable.switch_to_output(value=False)
9999
self._sample = None
100100
self._sine_wave = None
101+
self._sine_wave_sample = None
101102

102103
# Define touch:
103104
# We chose these verbose touch_A# names so that beginners could use it without understanding
@@ -594,8 +595,14 @@ def _generate_sample(self):
594595
if self._sample is not None:
595596
return
596597
length = 100
597-
self._sine_wave = array.array("H", Express._sine_sample(length))
598-
self._sample = audioio.AudioOut(board.SPEAKER, self._sine_wave)
598+
if sys.implementation.version[0] >= 3:
599+
self._sine_wave = array.array("H", Express._sine_sample(length))
600+
self._sample = audioio.AudioOut(board.SPEAKER)
601+
self._sine_wave_sample = audioio.RawSample(self._sine_wave)
602+
else:
603+
self._sine_wave = array.array("H", Express._sine_sample(length))
604+
self._sample = audioio.AudioOut(board.SPEAKER, self._sine_wave)
605+
599606

600607
def play_tone(self, frequency, duration):
601608
""" Produce a tone using the speaker. Try changing frequency to change
@@ -642,9 +649,14 @@ def start_tone(self, frequency):
642649
self._speaker_enable.value = True
643650
self._generate_sample()
644651
# 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)
652+
if sys.implementation.version[0] >= 3:
653+
self._sine_wave_sample.sample_rate = int(len(self._sine_wave) * frequency)
654+
if not self._sample.playing:
655+
self._sample.play(self._sine_wave_sample, loop=True)
656+
else:
657+
self._sample.frequency = int(len(self._sine_wave) * frequency)
658+
if not self._sample.playing:
659+
self._sample.play(loop=True)
648660

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

0 commit comments

Comments
 (0)