0

I'm having trouble with SDL_Mixer (my lack of experience). Chunks and Music play just fine (using Mix_PlayChannel and Mix_PlayMusic), and playing two different chunks simultaneously isn't an issue.

My problem is that I would like to play some chunk1, and then play second iteration of chunk1 overlapping the first. I am trying to play a single chunk in rapid succession, but it instead plays the sound repeatedly at a much longer interval (not as quickly as I want). I've tested console output and my method of playing/looping is not at fault, since I can see console messages printing, looped at the right speed.

I have an array of Chunks that I periodically load during initialization, using Mix_LoadWAV();

Mix_Chunk *sounds[32];

I also have a function reserved for playing these chunks:

void PlaySound(int snd_id)
{
    if(snd_id >= 0 && snd_id < 32)
    {
        if(Mix_PlayChannel(-1, sounds[snd_id], 0) == -1)
        {
            printf("Mix_PlayChannel: %s\n",Mix_GetError());
        }
    }
}

Attempting to play a single sound several times in rapid succession(say, 100ms delay/10bps), I am given the sound playing at a set, slower interval(some 500ms or so/2bps) despite the function being called at 10bps.

I already used "Mix_AllocateChannels(16);" to ensure I have allocated channels (let me know if I'm using that incorrectly) and still, a single chunk from the array refuses to play at a certain rate.

Any ideas/help is appreciated, as well as critique on how I posted this question.

Luft
  • 185
  • 4
  • 17

1 Answers1

1

As said in the documentation of SDL_Mixer (https://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer_28.html) : "... -1 for the first free unreserved channel."

So if your chunk is longer than 1.6 seconds (16 channels*100ms) you'll run out of channels after 1.6 seconds, and so you wont be enabled to play new chunks until one of the channels end playing.

So there are basically 2 solutions :

  1. Allocate more channels (more than : ChunkDuration (in sec) / Delay (in sec))
  2. Stop a channel, so that you can use it. (and to do it properly, you should not use -1 as channel but a variable that you increment each time you play a chunk (don't forget to set it back to 0 when it's equal to your number of channels) )
dido22
  • 41
  • 5