This is awkward but I don’t really know how to play sounds and haven’t found satisfactory documentation on these forums. I could figure it out, but being lazy and having no comfortable HW testing set up I figured it could also be good to have this publicly answered in a stack overflow style, also for others.
What I need is a typical scenario: continuously play 8bit sound from a circular buffer at specified rate (8 kHz). Could some knowledgeable person here please post a super simple snippet that does this, along with what I should put in my_settings.h etc.? Thank you very much
Here’s the solution (based on @spinal’s code), thanks to everyone:
From the work I did on my tracker music player and more recently with FM radio hat, I can say that sound on pokitto is both simple and tricky to figure out for a non-audio person like myself to grasp.
I’m not at my computer right now, but if there’s an example of my music player on the forum somewhere it might help with mixing samples together as well as playing different sample rates at the same time. There is a little code in pokitto lib for using my player in hwsound.c or somewhere like that that I used to mix multiple samples together, that might help.
Maybe, it seems a bit rough around the edges, does it actually work? I’d like to edit it a little bit and then put it into my post as an answer. Unfortunately I don’t have Pokitto with me to test. I have a few questions and notes:
What are buffers, currentBuffer, currentPtr, BUFFER_SIZE etc.? I don’t see these declared anywhere. Are they internal Pokttolib things?
What’s endPtr for? You’re not using it anywhere, are you sure it needs to be there?
Why do you need multiple buffers? Are you switching buffers as you do with graphics? With sound I’ve mostly seen using one circular buffer.
filled_buffer = (filled_buffer < NUM_BUFFERS-1) ? filled_buffer+1 : 0; can simply be written as filled_buffer = (filled_buffer + 1) % NUM_BUFFERS;.
What is currentBuffer for? It’s always 0, so if(currentBuffer == filled_buffer) can be rewritten as if(filled_buffer == 0) which then actually means only buffer 0 is ever used.
These are all pretty hacky ways of playing sounds though, shame there isn’t a nice API for that, especially when sound is pretty important in games. It could be as simple as a single function playSound(char *sound, int length). I still don’t get why three buffers are used and why sound code is mixed with file system code.
Anyway, as soon as I get home I’ll test your solutions and will try to reduce them into a bare minimum snippet which I will post.
Amazing, that could conclude the thread, however by looking at the source the function doesn’t mix multiple SFXs, which is what I’d like it to, so I’ll be looking for other solutions.
For the context, here is my playSound function in SDL, which I’d like to mimic in Pokitto. It works with a single circular buffer that is being played constantly over and over and the playSound function simply adds the SFX to the values in the buffer. The only limitation is that the SFX can’t be longer than the buffer size.