(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:
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?
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?
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.
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
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 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
)
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.
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.