Skip to content

Commit 9c7dc77

Browse files
kattnitannewt
authored andcommitted
start_tone / stop_tone implemented. (#4)
Add `start_tone` and `stop_tone` so we can check touch state while tons still play.
1 parent f8aab39 commit 9c7dc77

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ dist
55
_build/
66
*.mpy
77
build/
8-
8+
.DS_Store

adafruit_circuitplayground/express.py

100644100755
Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ def __init__(self):
7070
self.speaker_enable = digitalio.DigitalInOut(board.SPEAKER_ENABLE)
7171
self.speaker_enable.switch_to_output(value=False)
7272

73+
self.sample = None
74+
self.sine_wave = None
75+
7376
@property
7477
def button_a(self):
7578
"""``True`` when Button A is pressed. ``False`` if not.
@@ -191,6 +194,20 @@ def red_led(self):
191194
def red_led(self, value):
192195
self._led.value = value
193196

197+
@staticmethod
198+
def _sine_sample(length):
199+
TONE_VOLUME = (2 ** 15) - 1
200+
shift = 2 ** 15
201+
for i in range(length):
202+
yield int(TONE_VOLUME * math.sin(2*math.pi*(i / length)) + shift)
203+
204+
def _generate_sample(self):
205+
if self.sample is not None:
206+
return
207+
length = 100
208+
self.sine_wave = array.array("H", Express._sine_sample(length))
209+
self.sample = audioio.AudioOut(board.SPEAKER, self.sine_wave)
210+
194211
def play_tone(self, frequency, duration):
195212
""" Produce a tone using the speaker. Try changing frequency to change
196213
the pitch of the tone.
@@ -206,19 +223,58 @@ def play_tone(self, frequency, duration):
206223
207224
circuit.play_tone(440, 1)
208225
"""
209-
length = 8000 // frequency
210-
sine_wave = array.array("H", [0] * length)
211-
for i in range(length):
212-
sine_wave[i] = int(math.sin(math.pi * 2 * i / 18) * (2 ** 15) + 2 ** 15)
226+
# Play a tone of the specified frequency (hz).
227+
self.start_tone(frequency)
228+
time.sleep(duration)
229+
self.stop_tone()
230+
231+
def start_tone(self, frequency):
232+
""" Produce a tone using the speaker. Try changing frequency to change
233+
the pitch of the tone.
234+
235+
:param int frequency: The frequency of the tone in Hz
236+
237+
.. image :: /_static/speaker.jpg
213238
214-
sample = audioio.AudioOut(board.SPEAKER, sine_wave)
239+
.. code-block:: python
240+
241+
from adafruit_circuitplayground.express import circuit
215242
243+
while True:
244+
if circuit.button_a:
245+
circuit.start_tone(262)
246+
elif circuit.button_b:
247+
circuit.start_tone(294)
248+
else:
249+
circuit.stop_tone()
250+
"""
216251
self.speaker_enable.value = True
252+
self._generate_sample()
253+
# Start playing a tone of the specified frequency (hz).
254+
self.sample.frequency = int(len(self.sine_wave) * frequency)
255+
if not self.sample.playing:
256+
self.sample.play(loop=True)
217257

218-
sample.play(loop=True)
219-
time.sleep(duration)
220-
sample.stop()
258+
def stop_tone(self):
259+
""" Use with start_tone to stop the tone produced.
260+
261+
.. image :: /_static/speaker.jpg
262+
263+
.. code-block:: python
221264
265+
from adafruit_circuitplayground.express import circuit
266+
267+
while True:
268+
if circuit.button_a:
269+
circuit.start_tone(262)
270+
elif circuit.button_b:
271+
circuit.start_tone(294)
272+
else:
273+
circuit.stop_tone()
274+
"""
275+
# Stop playing any tones.
276+
if self.sample is not None and self.sample.playing:
277+
self.sample.stop()
222278
self.speaker_enable.value = False
223279

224280

0 commit comments

Comments
 (0)