Inconsistent performance with audio files on CPX with Crickit

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
enauman
 
Posts: 17
Joined: Sat Dec 26, 2020 12:16 am

Inconsistent performance with audio files on CPX with Crickit

Post by enauman »

My students are having a great time with CPX boards connected to Crickit boards with speakers, motors, and neopixel strips attached. Playing WAV files is very inconsistent, however. I followed the recommended format for the sound files students chose and kept them between 500-1000KB in size.
The version and config is Adafruit CircuitPython 7.3.1 on 2022-06-22; Adafruit CircuitPlayground Express with Crickit libraries with samd21g18
Board ID:circuitplayground_express_crickit
We're using Mu Editor IDE.
I've tried many iterations of debugging and can't find a pattern, sometimes it just works and other times I get a memory allocation error:

Code: Select all

Traceback (most recent call last):
  File "main.py", line 26, in <module>
MemoryError: memory allocation failed, allocating 512 bytes
Here's a sample program that seems to fail consistently on the call to play() (Drivetrain is a class students wrote themselves and is in a separate file):

Code: Select all

import time
from drivetrain import Drivetrain
from adafruit_circuitplayground import cp
import audioio
import audiocore
import board
import neopixel

drivetrain = Drivetrain()
num_pixels = 15
pixels = neopixel.NeoPixel(board.A1, num_pixels, brightness=1, auto_write=False)
cp.detect_taps = 1

wavfile = "SCREEEEEAM.wav"
f = open(wavfile, "rb")
wav = audiocore.WaveFile(f)
a = audioio.AudioOut(board.A0)

while True:
    drivetrain.forward()
    pixels.fill((0,255,0))
    pixels.show()
    if cp.tapped:
        pixels.fill((255,0,0))
        pixels.show()
        a.play(wav)
        drivetrain.stop()
        drivetrain.back()
        time.sleep(1)
        drivetrain.right()
        time.sleep(1)

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Inconsistent performance with audio files on CPX with Crickit

Post by dastels »

The SAMD21 used on the CPX only has 32K of ram which stores your code, anything from CIRCUITPY/lib that gets imported, and all data allocated and used by the code. That's running pretty tight, which is why the Crickit build has the crickit code frozen into the runtime (and not taking up ram).

You could try importing gc and calling gc.collect() after the imports. That will clean up any memory used during importing and also at the top of the loop to clean up each time through. It might help.

Another thing are to make the wav files as small as possible. Memory bugs are hard to find since the bahavior of the heap isn't always consistent.

If you haven't yet, check the advice here: https://learn.adafruit.com/welcome-to-c ... es-3129414

Dave

User avatar
enauman
 
Posts: 17
Joined: Sat Dec 26, 2020 12:16 am

Re: Inconsistent performance with audio files on CPX with Crickit

Post by enauman »

Thank you! Using gc didn't help but I will try converting the drivetrain module to an mpy format, and maybe the main.py too.

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Inconsistent performance with audio files on CPX with Crickit

Post by dastels »

main.py (or code.py) can't be precompiled the way the run time startup works.

Dave

User avatar
enauman
 
Posts: 17
Joined: Sat Dec 26, 2020 12:16 am

Re: Inconsistent performance with audio files on CPX with Crickit

Post by enauman »

Thanks, I was hoping to convert the drivetrain class into an mpy file, it's already in a separate file as a .py file. But I can't find enough information about how to install mpy-cross so that idea seems a dead end.

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Inconsistent performance with audio files on CPX with Crickit

Post by dastels »

Yes, you can move code from code.py and import it. Only code.py/main.py can't be precompiled.

Have you looked at https://learn.adafruit.com/building-cir ... ss-2986721?

Dave

User avatar
enauman
 
Posts: 17
Joined: Sat Dec 26, 2020 12:16 am

Re: Inconsistent performance with audio files on CPX with Crickit

Post by enauman »

Oh wow it worked! I've created the .mpy so I'll try it on the CPX tomorrow and see if there's enough space to play the audio successfully.

User avatar
enauman
 
Posts: 17
Joined: Sat Dec 26, 2020 12:16 am

Re: Inconsistent performance with audio files on CPX with Crickit

Post by enauman »

I replaced the drivetrain.py class file with a drivetrain.mpy file and the audio works! Thank you!

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

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