Skip to content

adjust sample length if needed for max sample rate #41

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 1 commit into from
Oct 3, 2018
Merged
Changes from all commits
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
8 changes: 5 additions & 3 deletions adafruit_circuitplayground/express.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,10 +562,9 @@ def _sine_sample(length):
for i in range(length):
yield int(tone_volume * math.sin(2*math.pi*(i / length)) + shift)

def _generate_sample(self):
def _generate_sample(self, length=100):
if self._sample is not None:
return
length = 100
self._sine_wave = array.array("H", Express._sine_sample(length))
if sys.implementation.version[0] >= 3:
self._sample = audioio.AudioOut(board.SPEAKER)
Expand Down Expand Up @@ -617,7 +616,10 @@ def start_tone(self, frequency):
cpx.stop_tone()
"""
self._speaker_enable.value = True
self._generate_sample()
length = 100
if length * frequency > 350000:
length = 350000 // frequency
self._generate_sample(length)
# Start playing a tone of the specified frequency (hz).
if sys.implementation.version[0] >= 3:
self._sine_wave_sample.sample_rate = int(len(self._sine_wave) * frequency)
Expand Down