44
44
import digitalio
45
45
import board
46
46
import audiopwmio
47
+ import audiobusio
47
48
from adafruit_circuitplayground .circuit_playground_base import CircuitPlaygroundBase
48
49
49
50
@@ -66,6 +67,11 @@ def __init__(self):
66
67
self ._sine_wave = None
67
68
self ._sine_wave_sample = None
68
69
70
+ # Define mic/sound sensor:
71
+ self ._mic = audiobusio .PDMIn (board .MICROPHONE_CLOCK , board .MICROPHONE_DATA ,
72
+ sample_rate = 16000 , bit_depth = 16 )
73
+ self ._samples = None
74
+
69
75
super ().__init__ ()
70
76
71
77
@staticmethod
@@ -187,6 +193,76 @@ def play_file(self, file_name):
187
193
pass
188
194
self ._speaker_enable .value = False
189
195
196
+ @staticmethod
197
+ def _normalized_rms (values ):
198
+ mean_values = int (sum (values ) / len (values ))
199
+ return math .sqrt (sum (float (sample - mean_values ) * (sample - mean_values )
200
+ for sample in values ) / len (values ))
201
+
202
+ @property
203
+ def sound_level (self ):
204
+ """Obtain the sound level from the microphone (sound sensor).
205
+
206
+ .. image :: ../docs/_static/microphone.jpg
207
+ :alt: Microphone (sound sensor)
208
+
209
+ This example prints the sound levels. Try clapping or blowing on
210
+ the microphone to see the levels change.
211
+
212
+ .. code-block:: python
213
+
214
+ from adafruit_circuitplayground.bluefruit import cpb
215
+
216
+ while True:
217
+ print(cpb.sound_level)
218
+ """
219
+ if self ._sample is None :
220
+ self ._samples = array .array ('H' , [0 ] * 160 )
221
+ self ._mic .record (self ._samples , len (self ._samples ))
222
+ return self ._normalized_rms (self ._samples )
223
+
224
+ def loud_sound (self , sound_threshold = 200 ):
225
+ """Utilise a loud sound as an input.
226
+
227
+ :param int sound_threshold: Threshold sound level must exceed to return true (Default: 200)
228
+
229
+ .. image :: ../docs/_static/microphone.jpg
230
+ :alt: Microphone (sound sensor)
231
+
232
+ This example turns the LEDs red each time you make a loud sound.
233
+ Try clapping or blowing onto the microphone to trigger it.
234
+
235
+ .. code-block:: python
236
+
237
+ from adafruit_circuitplayground.bluefruit import cpb
238
+
239
+ while True:
240
+ if cpb.loud_sound():
241
+ cpb.pixels.fill((50, 0, 0))
242
+ else:
243
+ cpb.pixels.fill(0)
244
+
245
+ You may find that the code is not responding how you would like.
246
+ If this is the case, you can change the loud sound threshold to
247
+ make it more or less responsive. Setting it to a higher number
248
+ means it will take a louder sound to trigger. Setting it to a
249
+ lower number will take a quieter sound to trigger. The following
250
+ example shows the threshold being set to a higher number than
251
+ the default.
252
+
253
+ .. code-block:: python
254
+
255
+ from adafruit_circuitplayground.bluefruit import cpb
256
+
257
+ while True:
258
+ if cpb.loud_sound(sound_threshold=300):
259
+ cpb.pixels.fill((50, 0, 0))
260
+ else:
261
+ cpb.pixels.fill(0)
262
+ """
263
+
264
+ return self .sound_level > sound_threshold
265
+
190
266
191
267
cpb = Bluefruit () # pylint: disable=invalid-name
192
268
"""Object that is automatically created on import.
0 commit comments