diff --git a/adafruit_circuitplayground/express.py b/adafruit_circuitplayground/express.py index f9eeaaf..ccd8e2c 100755 --- a/adafruit_circuitplayground/express.py +++ b/adafruit_circuitplayground/express.py @@ -563,24 +563,25 @@ def red_led(self, value): self._led.value = value @staticmethod - def _sine_sample(length): - tone_volume = (2 ** 15) - 1 + def _sine_sample(length, volume=None): + if volume is None: + volume = 0 + tone_volume = (2 ** 15) + volume shift = 2 ** 15 for i in range(length): yield int(tone_volume * math.sin(2*math.pi*(i / length)) + shift) - def _generate_sample(self, length=100): + def _generate_sample(self, length=100, volume=None): if self._sample is not None: return - self._sine_wave = array.array("H", Express._sine_sample(length)) + self._sine_wave = array.array("H", Express._sine_sample(length, volume)) if sys.implementation.version[0] >= 3: self._sample = audioio.AudioOut(board.SPEAKER) self._sine_wave_sample = audioio.RawSample(self._sine_wave) else: - self._sample = audioio.AudioOut(board.SPEAKER, self._sine_wave) + raise NotImplementedError("Please use CircuitPython 3.0 or higher.") - - def play_tone(self, frequency, duration): + def play_tone(self, frequency, duration, volume=None): """ Produce a tone using the speaker. Try changing frequency to change the pitch of the tone. @@ -597,11 +598,11 @@ def play_tone(self, frequency, duration): cpx.play_tone(440, 1) """ # Play a tone of the specified frequency (hz). - self.start_tone(frequency) + self.start_tone(frequency, volume) time.sleep(duration) self.stop_tone() - def start_tone(self, frequency): + def start_tone(self, frequency, volume=None): """ Produce a tone using the speaker. Try changing frequency to change the pitch of the tone. @@ -626,16 +627,14 @@ def start_tone(self, frequency): length = 100 if length * frequency > 350000: length = 350000 // frequency - self._generate_sample(length) + self._generate_sample(length, volume) # 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) 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) + raise NotImplementedError("Please use CircuitPython 3.0 or higher.") def stop_tone(self): """ Use with start_tone to stop the tone produced. @@ -690,13 +689,9 @@ def play_file(self, file_name): while audio.playing: pass else: - with audioio.AudioOut(board.SPEAKER, open(file_name, "rb")) as audio: - audio.play() - while audio.playing: - pass + raise NotImplementedError("Please use CircuitPython 3.0 or higher.") self._speaker_enable.value = False - cpx = Express() # pylint: disable=invalid-name """Object that is automatically created on import.