Skip to content

Commit c5e4dae

Browse files
committed
Update sound meter code.
1 parent e65921a commit c5e4dae

1 file changed

Lines changed: 7 additions & 21 deletions

File tree

Introducing_CircuitPlaygroundExpress/CircuitPlaygroundExpress_SoundMeter.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,28 @@
2323

2424
import array
2525
import math
26-
2726
import audiobusio
2827
import board
2928
import neopixel
3029

31-
# Exponential scaling factor.
32-
# Should probably be in range -10 .. 10 to be reasonable.
33-
CURVE = 2
34-
SCALE_EXPONENT = math.pow(10, CURVE * -0.1)
35-
3630
PEAK_COLOR = (100, 0, 255)
3731
NUM_PIXELS = 10
3832

3933
# Number of samples to read at once.
4034
NUM_SAMPLES = 160
4135

42-
43-
# Restrict value to be between floor and ceiling.
36+
# Exponential scaling factor.
37+
# Should probably be in range -10 .. 10 to be reasonable.
38+
CURVE = 2
39+
SCALE_EXPONENT = math.pow(10, CURVE * -0.1)
4440

4541

42+
# Restrict value to be between floor and ceiling.
4643
def constrain(value, floor, ceiling):
4744
return max(floor, min(value, ceiling))
4845

4946

5047
# Scale input_value between output_min and output_max, exponentially.
51-
52-
5348
def log_scale(input_value, input_min, input_max, output_min, output_max):
5449
normalized_input_value = (input_value - input_min) / \
5550
(input_max - input_min)
@@ -59,8 +54,6 @@ def log_scale(input_value, input_min, input_max, output_min, output_max):
5954

6055

6156
# Remove DC bias before computing RMS.
62-
63-
6457
def normalized_rms(values):
6558
minbuf = int(mean(values))
6659
samples_sum = sum(
@@ -81,20 +74,13 @@ def volume_color(volume):
8174

8275
# Main program
8376

84-
8577
# Set up NeoPixels and turn them all off.
86-
pixels = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS,
87-
brightness=0.1, auto_write=False)
78+
pixels = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1, auto_write=False)
8879
pixels.fill(0)
8980
pixels.show()
9081

91-
# For CircuitPython 2.x:
9282
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
93-
frequency=16000, bit_depth=16)
94-
# For Circuitpython 3.0 and up, "frequency" is now called "sample_rate".
95-
# Comment the lines above and uncomment the lines below.
96-
#mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
97-
# sample_rate=16000, bit_depth=16)
83+
sample_rate=16000, bit_depth=16)
9884

9985
# Record an initial sample to calibrate. Assume it's quiet when we start.
10086
samples = array.array('H', [0] * NUM_SAMPLES)

0 commit comments

Comments
 (0)