0

Problem with sound sensor in CircuitPython 6.1.0 on Circuit
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Problem with sound sensor in CircuitPython 6.1.0 on Circuit

by artgandalf on Thu Mar 11, 2021 1:03 am

Upgrading Adafruit CircuitPython from 5.3.1 to 6.1.0 causes the standard sound sensor example to not work correctly anymore on my Circuit Playground Express.

Code: Select all | TOGGLE FULL SIZE
import array
import math
import time
import audiobusio
import board

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

def normalized_rms(values):
    minbuf = int(mean(values))
    sum_of_samples = sum(
        float(sample - minbuf) * (sample - minbuf)
        for sample in values
    )
    return math.sqrt(sum_of_samples / len(values))

mic = audiobusio.PDMIn(
    board.MICROPHONE_CLOCK,
    board.MICROPHONE_DATA,
    sample_rate=16000,
    bit_depth=16
)
samples = array.array('H', [0] * 160)

while True:
    mic.record(samples, len(samples))
    print(samples)
    magnitude = normalized_rms(samples)
    print(((magnitude),))
    time.sleep(5)


The problem is that "mic.record(...)" will fill only the first 32 entries of the array with values, but leaves the remaining entries at 0.
However, including "from adafruit_circuitplayground import cp" in the list of imports fixes this problem.
Unfortunately, this is not intuitive and, therefore, not something one would do if none of the API calls in that library are needed.

Has anyone else encountered this issue?
Is it a problem with the latest version of Adafruit CircuitPython? (When I downgrade to the previous version, it works again.)

artgandalf
 
Posts: 1
Joined: Thu Mar 11, 2021 12:17 am

Please be positive and constructive with your questions and comments.