2121# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222# THE SOFTWARE.
2323
24+ # Circuit Playground Sound Meter
25+
2426import array
2527import math
26-
2728import audiobusio
2829import board
2930import neopixel
3031
32+ # Color of the peak pixel.
33+ PEAK_COLOR = (100 , 0 , 255 )
34+ # Number of total pixels - 10 build into Circuit Playground
35+ NUM_PIXELS = 10
36+
3137# Exponential scaling factor.
3238# Should probably be in range -10 .. 10 to be reasonable.
3339CURVE = 2
3440SCALE_EXPONENT = math .pow (10 , CURVE * - 0.1 )
3541
36- PEAK_COLOR = (100 , 0 , 255 )
37- NUM_PIXELS = 10
38-
3942# Number of samples to read at once.
4043NUM_SAMPLES = 160
4144
4245
4346# Restrict value to be between floor and ceiling.
44-
45-
4647def constrain (value , floor , ceiling ):
4748 return max (floor , min (value , ceiling ))
4849
4950
5051# Scale input_value between output_min and output_max, exponentially.
51-
52-
5352def log_scale (input_value , input_min , input_max , output_min , output_max ):
5453 normalized_input_value = (input_value - input_min ) / \
5554 (input_max - input_min )
@@ -59,8 +58,6 @@ def log_scale(input_value, input_min, input_max, output_min, output_max):
5958
6059
6160# Remove DC bias before computing RMS.
62-
63-
6461def normalized_rms (values ):
6562 minbuf = int (mean (values ))
6663 samples_sum = sum (
@@ -81,20 +78,13 @@ def volume_color(volume):
8178
8279# Main program
8380
84-
8581# Set up NeoPixels and turn them all off.
86- pixels = neopixel .NeoPixel (board .NEOPIXEL , NUM_PIXELS ,
87- brightness = 0.1 , auto_write = False )
82+ pixels = neopixel .NeoPixel (board .NEOPIXEL , NUM_PIXELS , brightness = 0.1 , auto_write = False )
8883pixels .fill (0 )
8984pixels .show ()
9085
91- # For CircuitPython 2.x:
9286mic = 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)
87+ sample_rate = 16000 , bit_depth = 16 )
9888
9989# Record an initial sample to calibrate. Assume it's quiet when we start.
10090samples = array .array ('H' , [0 ] * NUM_SAMPLES )
0 commit comments