Created
November 5, 2019 02:37
-
-
Save deckerego/2597e78e5810b1b940427a82f8f989b1 to your computer and use it in GitHub Desktop.
Audio level meter with Circuit Playground Express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import array | |
import math | |
import time | |
import neopixel | |
import audiobusio | |
import board | |
def avg(values): | |
return sum(values) / len(values) | |
def normalized_rms(values): | |
avg_value = int(avg(values)) | |
sum_samples = sum(float(sample - avg_value) * (sample - avg_value) for sample in values) | |
return math.sqrt(sum_samples / len(values)) | |
pixels = neopixel.NeoPixel(board.NEOPIXEL, 10, brightness=0.1, auto_write=False) | |
mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA, bit_depth=16) | |
samples = array.array('H', [0] * 192) | |
mic.record(samples, len(samples)) | |
while True: | |
mic.record(samples, len(samples)) | |
magnitude = normalized_rms(samples) | |
pixels.fill((0, 0, 0)) | |
pixels[0] = pixels[9] = (0, 128, 0) | |
if magnitude > 50: | |
pixels[1] = pixels[8] = (64, 128, 0) | |
if magnitude > 100: | |
pixels[2] = pixels[7] = (128, 64, 0) | |
if magnitude > 200: | |
pixels[3] = pixels[6] = (128, 32, 0) | |
if magnitude > 400: | |
pixels[4] = pixels[5] = (128, 0, 0) | |
pixels.show() | |
time.sleep(0.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment