Play different SFX for each button. Using LittleFS, MP3 , I2S and RP2040 #1242
-
Phil, I'm working on a game that needs different sound effects for each circumstance. I'm using six ".mp3" files, mono, with 22050 sample rate, 16 bits pcm, named "audio_01.mp3" ... "audio_06.mp3". I made some changes to player-littlefs-mp3 to suit my reality - I'm using an RP2040-based card, with 16mb of flash memory (equivalent to Waveshare Rp2040 Plus). Distributes flash memory using 1Mb for Sketch and 15Mb for the file system. I kept the CPU speed at 133Mhz. Sketch with adaptations is working, playing each of the six files once. I still don't understand how I could adapt my sketch to play the files randomly, for example. Or play a specific file with each button press. How is this done? (I am not asking you for the code to detect button press... etc... I am just need to understand how can I play especific audio file in main loop, ok?) How do I implement this in the Arduino main loop? Thank you very much in advance. Ps.: The code above is your example with some adjustments in order to play each file, once.
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
And here is my Serial Log:
|
Beta Was this translation helpful? Give feedback.
-
To detect button press events you can use a pin interupt with (attachInterrupt) or you can use my AudioActions class. I don't have any plain vanilla example but only some AudioKit ones is using this class. This example also shows how to define the file to play. If you don't want to play the next song automatically: the player has a method setAutoNext(): just set it to false However I would build the solution as follows:
There is much less complexity involved in this solution.... |
Beta Was this translation helpful? Give feedback.
To detect button press events you can use a pin interupt with (attachInterrupt) or you can use my AudioActions class. I don't have any plain vanilla example but only some AudioKit ones is using this class. This example also shows how to define the file to play.
If you don't want to play the next song automatically: the player has a method setAutoNext(): just set it to false
If you need more control on what is played you can implement your own AudioSourceCallback
However I would build the solution as follows: