2323
2424import array
2525import math
26-
2726import audiobusio
2827import board
2928import 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-
3630PEAK_COLOR = (100 , 0 , 255 )
3731NUM_PIXELS = 10
3832
3933# Number of samples to read at once.
4034NUM_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.
4643def 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-
5348def 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-
6457def 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 )
8879pixels .fill (0 )
8980pixels .show ()
9081
91- # For CircuitPython 2.x:
9282mic = 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.
10086samples = array .array ('H' , [0 ] * NUM_SAMPLES )
0 commit comments