CPX hangs when using mic and speaker together

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
ronturcotte
 
Posts: 6
Joined: Fri Jul 27, 2018 9:55 pm

CPX hangs when using mic and speaker together

Post by ronturcotte »

(deleted and reposted with properly formatted code)

I'm building an application that requires both the microphone and the speaker on a Circuit Playground Express. The code works if I use EITHER the mic or the speaker, but everything freezes when I try to use both. The problem is easy to reproduce - I just start with the code from this page: https://learn.adafruit.com/sensor-plott ... thon/sound, add an import for circuitplayground and a call to cp.play_tone:

Code: Select all

import array
import math
import time

import audiobusio
import board
from adafruit_circuitplayground import cp


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)
mic.record(samples, len(samples))

while True:
    mic.record(samples, len(samples))
    magnitude = normalized_rms(samples)
    print(((magnitude),))
    cp.play_tone(440,.25)
    time.sleep(0.1)
I'm using CircuitPython 6.1.0. Does anyone know why this is happening?

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: CPX hangs when using mic and speaker together

Post by mikeysklar »

@ronturcotte,

Could you try running our "Interactive Scream" example which is using the mic and playback at the same time.

https://learn.adafruit.com/the-scream-m ... cuitpython

Code: Select all

import time
import math
import array
import audiobusio
import audioio
import audiocore
import board
from adafruit_crickit import crickit

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

# 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)

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))

head_servo = crickit.servo_1
head_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500)
head_servo.angle = 90  # center the head.

# Set audio out on speaker.
a = audioio.AudioOut(board.A0)

# Start playing the file (in the background).
def play_file(wavfile):
    print("Playing scream!")
    with open(wavfile, "rb") as f:
        wav = audiocore.WaveFile(f)
        a.play(wav)
        while a.playing:
            head_servo.angle = 60
            time.sleep(.01)
            head_servo.angle = 120
            time.sleep(.01)


while True:
    mic.record(samples, len(samples))
    magnitude = normalized_rms(samples)
    print(((magnitude),))  # formatting is for the Mu plotter.

    if magnitude < 1000:  # it's quiet, do nothing.
        pass
    else:
        print("LOUD")
        head_servo.angle = 60
        time.sleep(.05)
        head_servo.angle = 120
        time.sleep(.05)
        head_servo.angle = 90
        time.sleep(.02)
        play_file("scream_low.wav")
        head_servo.angle = 90
        time.sleep(2)

User avatar
MarcBaum
 
Posts: 4
Joined: Sun Feb 01, 2015 6:26 pm

Re: CPX hangs when using mic and speaker together

Post by MarcBaum »

Here the same, running "Interactive Scream" example. CPX+crickit hangs after playing the wave file with "adafruit-circuitpython-circuitplayground_express_crickit-de_DE-6.3.0". Some Ideas?

Thanks
Marc

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: CPX hangs when using mic and speaker together

Post by mikeysklar »

Interesting that both of our chunk of example code cause the hang.

Were you using the CPX + Crickit in both scenarios? Do you see thing hang if you remove the crickit?

I see you have been good about staying current with the CircuitPython release updates. Have you also removed all the older /lib libraries and replaced them with the updated 6.3 library release?

https://circuitpython.org/libraries

Since you are seeing example code hanging I'm guessing that an older library might be used which can be problematic with a newer CircuitPython release.

User avatar
MarcBaum
 
Posts: 4
Joined: Sun Feb 01, 2015 6:26 pm

Re: CPX hangs when using mic and speaker together

Post by MarcBaum »

thank you for yor helph

I using the CPX with and without the Crickit. It hang in both szenario, but the code works without playing any wave file. (#)
The error arises when I use the microphone and the sound output

The lib folder ist empty, there no olds libarys.

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: CPX hangs when using mic and speaker together

Post by mikeysklar »

@MarcBaum,

Let's make sure you bootloader is current and that the adafruit_circuitplayground library is indeed in your CIRCUITPY/lib folder as that should be necessary for the import line of your example code.

The current bootloader version for the CPX is:

https://github.com/adafruit/uf2-samdx1/ ... 3.13.0.uf2

# from the 20210616 CircuitPython 6.3 library bundle - this folder needs to be in your CIRCUITPY/lib

Code: Select all

$ find adafruit_circuitplayground/
adafruit_circuitplayground/
adafruit_circuitplayground//__init__.mpy
adafruit_circuitplayground//express.mpy
adafruit_circuitplayground//bluefruit.mpy
adafruit_circuitplayground//circuit_playground_base.mpy

User avatar
MarcBaum
 
Posts: 4
Joined: Sun Feb 01, 2015 6:26 pm

Re: CPX hangs when using mic and speaker together

Post by MarcBaum »

@mikeysklar

The bootloader ist current (3.13). I moved the library file to the LIB folder but had the same problem. After playing the WAVE file the CPX hangs. When i use the led instead of audio, all works finet with his code.

import time
import math
import array
import audiobusio
import audioio
import audiocore
import board
import digitalio
from adafruit_crickit import crickit

# Number of samples to read at once.
NUM_SAMPLES = 160
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
# 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)

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))

head_servo = crickit.servo_1
head_servo.set_pulse_width_range(min_pulse=500, max_pulse=2500)
head_servo.angle = 90 # center the head.

# Set audio out on speaker.
a = audioio.AudioOut(board.A0)

# Start playing the file (in the background).
def play_file(wavfile):
print("Playing scream!")
with open(wavfile, "rb") as f:
wav = audiocore.WaveFile(f)
a.play(wav)
while a.playing:
head_servo.angle = 60
time.sleep(.01)
head_servo.angle = 120
time.sleep(.01)


while True:
mic.record(samples, len(samples))
magnitude = normalized_rms(samples)
print(((magnitude),)) # formatting is for the Mu plotter.

if magnitude < 14000: # it's quiet, do nothing.
led.value = False
else:
print("LOUD")
led.value = True
head_servo.angle = 60
time.sleep(.05)
head_servo.angle = 120
time.sleep(.05)
head_servo.angle = 90
time.sleep(.02)
# play_file("scream_low.wav")
head_servo.angle = 90
time.sleep(2)

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: CPX hangs when using mic and speaker together

Post by mikeysklar »

This is still concerning that the CPX hangs after playing the audio file. I wonder if it could be a memory issue?

Have you tried running the CircuitPython 7.0.0-alpha3 (and updating the necessary libraries). I suspect the issue is still there.

https://circuitpython.org/board/circuit ... d_express/

There is an open issue for the CPX hanging when a WAV file is played and mic.record is being used.

https://github.com/adafruit/circuitpython/issues/4600

This was working with CircuitPython 5.x

User avatar
MarcBaum
 
Posts: 4
Joined: Sun Feb 01, 2015 6:26 pm

Re: CPX hangs when using mic and speaker together

Post by MarcBaum »

On CircuitPython 7.0.0-alpha3 and updated the libraries the same. CPX still hanging

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: CPX hangs when using mic and speaker together

Post by mikeysklar »

@MarcBaum,

Thank you for taking the time that the current CircuitPython alpha release and libs still show the hang. If you would like to add a comment on the open github issue I think it would be relevant and helpful. It is an open issue.

https://github.com/adafruit/circuitpython/issues/4600

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

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