Memory alocation errors with audio playback wave file

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
hal90001
 
Posts: 2
Joined: Thu Aug 26, 2021 10:51 am

Memory alocation errors with audio playback wave file

Post by hal90001 »

I am having an issue where I can't seem to randomly play 1 of 2 wave files as part of a function. Whenever I call the function I get a memory allocation error:

code.py output:
Playing random file

Code: Select all

Traceback (most recent call last):
  File "code.py", line 112, in <module>
  File "code.py", line 57, in playRandom
  File "adafruit_circuitplayground/circuit_playground_base.py", line 834, in play_file
  File "adafruit_circuitplayground/circuit_playground_base.py", line 832, in play_file
MemoryError: memory allocation failed, allocating 512 bytes
The code in question:

Code: Select all

# dictionary defining the random files that can be selected from
playFile = {1 : "meow.wav", 2 : "hiOwen.wav"}

def playRandom():
    print("Playing random file")
    cp.play_file(playFile[random.randint(1, 2)])
Then when I call playRandom from the main while loop or other functions I get the error.
Is there any way to work around this issue? I have double checked the wav files are mono 16 bit, 22 khz as specified by circuit python recommendations.
THANKS!!!

User avatar
hal90001
 
Posts: 2
Joined: Thu Aug 26, 2021 10:51 am

Re: Memory alocation errors with audio playback wave file

Post by hal90001 »

Is there maybe a better way for me to randomly select between files? Is there a better way to set the files to variables so that I can call them over and over without memory issues?

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

Re: Memory alocation errors with audio playback wave file

Post by danhalbert »

The `play_file()` is not directly causing a memory problem. Instead, the rest of your program may be using too much RAM, or it may be "fragmenting" the memory into chunks that are are smaller than the 512 byte allocation that `play_file()` needs to do. Could you show the rest of the program?

A slightly more efficient way of selecting between two files would be:

Code: Select all

playFile = [meow.wav", "hiOwen.wav"]

def playRandom():
    print("Playing random file")
    cp.play_file(playFile[random.randint(0, 1)])
`playFile` is a list, which you can index with an integer, starting at 0.

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

Return to “Adafruit CircuitPython”