@@ -70,6 +70,9 @@ def __init__(self):
70
70
self .speaker_enable = digitalio .DigitalInOut (board .SPEAKER_ENABLE )
71
71
self .speaker_enable .switch_to_output (value = False )
72
72
73
+ self .sample = None
74
+ self .sine_wave = None
75
+
73
76
@property
74
77
def button_a (self ):
75
78
"""``True`` when Button A is pressed. ``False`` if not.
@@ -191,6 +194,20 @@ def red_led(self):
191
194
def red_led (self , value ):
192
195
self ._led .value = value
193
196
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
+
194
211
def play_tone (self , frequency , duration ):
195
212
""" Produce a tone using the speaker. Try changing frequency to change
196
213
the pitch of the tone.
@@ -206,19 +223,58 @@ def play_tone(self, frequency, duration):
206
223
207
224
circuit.play_tone(440, 1)
208
225
"""
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
213
238
214
- sample = audioio .AudioOut (board .SPEAKER , sine_wave )
239
+ .. code-block:: python
240
+
241
+ from adafruit_circuitplayground.express import circuit
215
242
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
+ """
216
251
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 )
217
257
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
221
264
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 ()
222
278
self .speaker_enable .value = False
223
279
224
280
0 commit comments