From cb05dd24cfab8028b6f4cd4173566b11b5bf380c Mon Sep 17 00:00:00 2001 From: caternuson Date: Tue, 2 Oct 2018 14:39:27 -0700 Subject: [PATCH] adjust sample length if needed for max sample rate --- adafruit_circuitplayground/express.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/adafruit_circuitplayground/express.py b/adafruit_circuitplayground/express.py index 7330f41..123dab9 100755 --- a/adafruit_circuitplayground/express.py +++ b/adafruit_circuitplayground/express.py @@ -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) @@ -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)