Playground Sound Meter - I cannot get to work on Express.

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
CPXdev
 
Posts: 2
Joined: Wed Dec 29, 2021 11:04 pm

Playground Sound Meter - I cannot get to work on Express.

Post by CPXdev »

Hi,
The code below is the Playground Sound Meter program from the adafruit site but I cannot get it to work on my new Circuit Playground Express. I included a picture that shows the output. I have no errors but no LEDs light up when I make any kind of sound, and the program does not even print out any of my Print statements so I can follow along what the code is doing.
Please help!
Thank you.
Regards,

Code: Select all

# The MIT License (MIT)
#
# Copyright (c) 2017 Dan Halbert for Adafruit Industries
# Copyright (c) 2017 Kattni Rembor, Tony DiCola for Adafruit Industries
#

# Circuit Playground Sound Meter

import array
import math
import audiobusio
import board
import neopixel
print('It starts')
# Color of the peak pixel.
PEAK_COLOR = (100, 0, 255)
# Number of total pixels - 10 build into Circuit Playground
NUM_PIXELS = 10

# Exponential scaling factor.
# Should probably be in range -10 .. 10 to be reasonable.
CURVE = 10
SCALE_EXPONENT = math.pow(10, CURVE * -0.1)

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


# Restrict value to be between floor and ceiling.
def constrain(value, floor, ceiling):
    return max(floor, min(value, ceiling))


# Scale input_value between output_min and output_max, exponentially.
def log_scale(input_value, input_min, input_max, output_min, output_max):
    normalized_input_value = (input_value - input_min) / \
                             (input_max - input_min)
    return output_min + \
        math.pow(normalized_input_value, SCALE_EXPONENT) \
        * (output_max - output_min)


# Remove DC bias before computing RMS.
def normalized_rms(values):
    minbuf = int(mean(values))
    samples_sum = sum(
        float(sample - minbuf) * (sample - minbuf)
        for sample in values
    )

    return math.sqrt(samples_sum / len(values))


def mean(values):
    return sum(values) / len(values)


def volume_color(volume):
    return 200, volume * (255 // NUM_PIXELS), 0


# Main program
print('It starts')
# Set up NeoPixels and turn them all off.
pixels = neopixel.NeoPixel(board.NEOPIXEL, NUM_PIXELS, brightness=0.1, 
                           auto_write=False)
pixels.fill(0)
pixels.show()

mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
                       sample_rate=16000, bit_depth=16)

# Record an initial sample to calibrate. Assume it's quiet when we start.
samples = array.array('H', [0] * NUM_SAMPLES)
mic.record(samples, len(samples))
# Set lowest level to expect, plus a little.
input_floor = normalized_rms(samples) + 10
# OR: used a fixed floor
# input_floor = 50

# You might want to print the input_floor to help adjust other values.
print(input_floor)

# Corresponds to sensitivity: lower means more pixels light up with lower sound
# Adjust this as you see fit.
input_ceiling = input_floor + 500

peak = 0
while True:
    mic.record(samples, len(samples))
    magnitude = normalized_rms(samples)
# You might want to print this to see the values.
print(magnitude)

# Compute scaled logarithmic reading in the range 0 to NUM_PIXELS
c = log_scale(constrain(magnitude, input_floor, input_ceiling),
              input_floor, input_ceiling, 0, NUM_PIXELS)

# Light up pixels that are below the scaled and interpolated magnitude.
pixels.fill(0)
for i in range(NUM_PIXELS):
        if i < c:
            pixels[i] = volume_color(i)
        # Light up the peak pixel and animate it slowly dropping.
        if c >= peak:
            peak = min(c, NUM_PIXELS - 1)
        elif peak > 0:
            peak = peak - 1
        if peak > 0:
            pixels[int(peak)] = PEAK_COLOR
pixels.show()
Attachments
Screenshot from 2021-12-29 20-54-48.png
Screenshot from 2021-12-29 20-54-48.png (27.78 KiB) Viewed 147 times

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: Playground Sound Meter - I cannot get to work on Express

Post by danhalbert »

The original code is here: https://learn.adafruit.com/adafruit-cir ... ound-meter

When you modified the program in various ways to add the print statements, you also changed the line indentation. The lines below starting with `# You might...` are indented to the left four spaces. So you get stuck in the `while True:` loop.

If you have a "diff" program (meld is a nice one: https://meldmerge.org/), compare your program with the original, and you'll see the differences.

Code: Select all

while True:
    mic.record(samples, len(samples))
    magnitude = normalized_rms(samples)
# You might want to print this to see the values.  <---- INDENTATION WRONG STARTING HERE
print(magnitude)

# Compute scaled logarithmic reading in the range 0 to NUM_PIXELS
c = log_scale(constrain(magnitude, input_floor, input_ceiling),
              input_floor, input_ceiling, 0, NUM_PIXELS)

# Light up pixels that are below the scaled and interpolated magnitude.
pixels.fill(0)
for i in range(NUM_PIXELS):

User avatar
CPXdev
 
Posts: 2
Joined: Wed Dec 29, 2021 11:04 pm

Re: Playground Sound Meter - I cannot get to work on Express

Post by CPXdev »

Hi Dan,
Thanks for your response. There is some sort of problem with the following line (which other people are having also):

mic = audiobusio.PDMIn(board.MICROPHONE_CLOCK, board.MICROPHONE_DATA,
sample_rate=16000, bit_depth=16)

Apparently it works on Bluefruit but not Express. Thanks for help though.

Regards,

User avatar
danhalbert
 
Posts: 4613
Joined: Tue Aug 08, 2017 12:37 pm

Re: Playground Sound Meter - I cannot get to work on Express

Post by danhalbert »

I am trying to fix this right now, yes. https://github.com/adafruit/circuitpython/issues/5797

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”