Skip to content

Commit 5869d46

Browse files
committed
2 parents 3d59e52 + 4db264a commit 5869d46

File tree

10 files changed

+8568
-13
lines changed

10 files changed

+8568
-13
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import time
2+
import array
3+
import board
4+
import audiobusio
5+
6+
#---| User Configuration |---------------------------
7+
SAMPLERATE = 16000
8+
SAMPLES = 1024
9+
THRESHOLD = 100
10+
MIN_DELTAS = 5
11+
DELAY = 0.2
12+
#----------------------------------------------------
13+
14+
# Create a buffer to record into
15+
samples = array.array('H', [0] * SAMPLES)
16+
17+
# Setup the mic input
18+
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK,
19+
board.MICROPHONE_DATA,
20+
sample_rate=SAMPLERATE,
21+
bit_depth=16)
22+
23+
while True:
24+
# Get raw mic data
25+
mic.record(samples, SAMPLES)
26+
27+
# Compute DC offset (mean) and threshold level
28+
mean = int(sum(samples) / len(samples) + 0.5)
29+
threshold = mean + THRESHOLD
30+
31+
# Compute deltas between mean crossing points
32+
# (this bit by Dan Halbert)
33+
deltas = []
34+
last_xing_point = None
35+
crossed_threshold = False
36+
for i in range(SAMPLES-1):
37+
sample = samples[i]
38+
if sample > threshold:
39+
crossed_threshold = True
40+
if crossed_threshold and sample < mean:
41+
if last_xing_point:
42+
deltas.append(i - last_xing_point)
43+
last_xing_point = i
44+
crossed_threshold = False
45+
46+
# Try again if not enough deltas
47+
if len(deltas) < MIN_DELTAS:
48+
continue
49+
50+
# Average the deltas
51+
mean = sum(deltas) / len(deltas)
52+
53+
# Compute frequency
54+
freq = SAMPLERATE / mean
55+
56+
print("crossings: {} mean: {} freq: {} ".format(len(deltas), mean, freq))
57+
58+
time.sleep(DELAY)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import time
2+
import array
3+
import board
4+
import audiobusio
5+
import simpleio
6+
import neopixel
7+
8+
#---| User Configuration |---------------------------
9+
SAMPLERATE = 16000
10+
SAMPLES = 1024
11+
THRESHOLD = 100
12+
MIN_DELTAS = 5
13+
DELAY = 0.2
14+
15+
FREQ_LOW = 520
16+
FREQ_HIGH = 990
17+
COLORS = (
18+
(0xFF, 0x00, 0x00) , # pixel 0
19+
(0xFF, 0x71, 0x00) , # pixel 1
20+
(0xFF, 0xE2, 0x00) , # pixel 2
21+
(0xAA, 0xFF, 0x00) , # pixel 3
22+
(0x38, 0xFF, 0x00) , # pixel 4
23+
(0x00, 0xFF, 0x38) , # pixel 5
24+
(0x00, 0xFF, 0xA9) , # pixel 6
25+
(0x00, 0xE2, 0xFF) , # pixel 7
26+
(0x00, 0x71, 0xFF) , # pixel 8
27+
(0x00, 0x00, 0xFF) , # pixel 9
28+
)
29+
#----------------------------------------------------
30+
31+
# Create a buffer to record into
32+
samples = array.array('H', [0] * SAMPLES)
33+
34+
# Setup the mic input
35+
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK,
36+
board.MICROPHONE_DATA,
37+
sample_rate=SAMPLERATE,
38+
bit_depth=16)
39+
40+
# Setup NeoPixels
41+
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, auto_write=False)
42+
43+
while True:
44+
# Get raw mic data
45+
mic.record(samples, SAMPLES)
46+
47+
# Compute DC offset (mean) and threshold level
48+
mean = int(sum(samples) / len(samples) + 0.5)
49+
threshold = mean + THRESHOLD
50+
51+
# Compute deltas between mean crossing points
52+
# (this bit by Dan Halbert)
53+
deltas = []
54+
last_xing_point = None
55+
crossed_threshold = False
56+
for i in range(SAMPLES-1):
57+
sample = samples[i]
58+
if sample > threshold:
59+
crossed_threshold = True
60+
if crossed_threshold and sample < mean:
61+
if last_xing_point:
62+
deltas.append(i - last_xing_point)
63+
last_xing_point = i
64+
crossed_threshold = False
65+
66+
# Try again if not enough deltas
67+
if len(deltas) < MIN_DELTAS:
68+
continue
69+
70+
# Average the deltas
71+
mean = sum(deltas) / len(deltas)
72+
73+
# Compute frequency
74+
freq = SAMPLERATE / mean
75+
76+
print("crossings: {} mean: {} freq: {} ".format(len(deltas), mean, freq))
77+
78+
# Show on NeoPixels
79+
pixels.fill(0)
80+
pixel = round(simpleio.map_range(freq, FREQ_LOW, FREQ_HIGH, 0, 9))
81+
pixels[pixel] = COLORS[pixel]
82+
pixels.show()
83+
84+
time.sleep(DELAY)

0 commit comments

Comments
 (0)