Skip to content

Commit b9d77f1

Browse files
committed
Adding sound_level, loud_sound to bluefruit
1 parent e18b7ac commit b9d77f1

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

adafruit_circuitplayground/bluefruit.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import digitalio
4545
import board
4646
import audiopwmio
47+
import audiobusio
4748
from adafruit_circuitplayground.circuit_playground_base import CircuitPlaygroundBase
4849

4950

@@ -66,6 +67,11 @@ def __init__(self):
6667
self._sine_wave = None
6768
self._sine_wave_sample = None
6869

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+
6975
super().__init__()
7076

7177
@staticmethod
@@ -187,6 +193,76 @@ def play_file(self, file_name):
187193
pass
188194
self._speaker_enable.value = False
189195

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+
190266

191267
cpb = Bluefruit() # pylint: disable=invalid-name
192268
"""Object that is automatically created on import.

0 commit comments

Comments
 (0)