Skip to content

Updated generate_sample and start_tone to 3.0 #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 25, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions adafruit_circuitplayground/express.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,14 @@ def _generate_sample(self):
if self._sample is not None:
return
length = 100
self._sine_wave = array.array("H", Express._sine_sample(length))
self._sample = audioio.AudioOut(board.SPEAKER, self._sine_wave)
if sys.implementation.version[0] >= 3:
self._sine_wave = array.array("H", Express._sine_sample(length))
self._sample = audioio.AudioOut(board.SPEAKER)
self._sine_wave_sample = audioio.RawSample(self._sine_wave)
else:
self._sine_wave = array.array("H", Express._sine_sample(length))
self._sample = audioio.AudioOut(board.SPEAKER, self._sine_wave)


def play_tone(self, frequency, duration):
""" Produce a tone using the speaker. Try changing frequency to change
Expand Down Expand Up @@ -642,9 +648,14 @@ def start_tone(self, frequency):
self._speaker_enable.value = True
self._generate_sample()
# Start playing a tone of the specified frequency (hz).
self._sample.frequency = int(len(self._sine_wave) * frequency)
if not self._sample.playing:
self._sample.play(loop=True)
if sys.implementation.version[0] >= 3:
self._sine_wave_sample.sample_rate = int(len(self._sine_wave) * frequency)
if not self._sample.playing:
self._sample.play(self._sine_wave_sample, loop=True)
else:
self._sample.frequency = int(len(self._sine_wave) * frequency)
if not self._sample.playing:
self._sample.play(loop=True)

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