-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcpx_sound_meter.py
51 lines (39 loc) · 1.54 KB
/
cpx_sound_meter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import array
import math
import audiobusio
import board
from adafruit_circuitplayground.express import cpx
def constrain(value, floor, ceiling):
return max(floor, min(value, ceiling))
def log_scale(input_value, input_min, input_max, output_min, output_max):
normalized_input_value = (input_value - input_min) / (input_max - input_min)
return output_min + math.pow(normalized_input_value, 0.630957) * (output_max - output_min)
def normalized_rms(values):
minbuf = int(sum(values) / len(values))
return math.sqrt(sum(float(sample - minbuf) *
(sample - minbuf) for sample in values) / len(values))
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, sample_rate=16000, bit_depth=16)
samples = array.array('H', [0] * 160)
mic.record(samples, len(samples))
input_floor = normalized_rms(samples) + 10
# Lower number means more sensitive - more LEDs will light up with less sound.
sensitivity = 500
input_ceiling = input_floor + sensitivity
peak = 0
while True:
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
print((magnitude,))
c = log_scale(constrain(magnitude, input_floor, input_ceiling),
input_floor, input_ceiling, 0, 10)
cpx.pixels.fill((0, 0, 0))
for i in range(10):
if i < c:
cpx.pixels[i] = (i * (255 // 10), 50, 0)
if c >= peak:
peak = min(c, 10 - 1)
elif peak > 0:
peak = peak - 1
if peak > 0:
cpx.pixels[int(peak)] = (80, 0, 255)
cpx.pixels.show()