We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 413

Summary: Mix_Chunk loop/callback error with samples of certain lengths
Product: SDL_mixer Reporter: Jesse Anders <jesseanders>
Component: miscAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: jesseanders
Version: unspecified   
Hardware: PowerPC   
OS: Mac OS X 10.4 (Intel)   
Attachments: Audio sample

Description Jesse Anders 2007-03-08 18:27:40 UTC
In the mix_channels() function, it appears that looping and playback termination behaves incorrectly when the length of the sample is an even multiple of the value of the 'len' argument. When the 'loop' argument to Mix_PlayChannel() is > 0, the sound plays back one fewer times than requested, and the callback is not called when the sound finishes playing.

Here is a short program that demonstrates this behavior:

/******************************************************************************/

#include "SDL.h"
#include "SDL_mixer.h"

#include <iostream>

void Callback(int) { std::cout << "Callback() was called." << std::endl; }

int main(int argc, char *argv[])
{
    SDL_Init(SDL_INIT_AUDIO);
    Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024);
    Mix_ChannelFinished(Callback);

    std::cout << "\nPlaying sample with loop = 1: Should play twice, but\n"
              << "will only play once and callback will not be called.\n\n"
              << "(Press any key followed by enter to quit)" << std::endl;

    Mix_Chunk* sound = Mix_LoadWAV("sdl_mixer_bug.wav");
    Mix_PlayChannel(-1, sound, 1);

    std::cin.get();

    Mix_FreeChunk(sound);
    Mix_CloseAudio();
    SDL_Quit();

    return 0;
}

/******************************************************************************/

Here is the URL of the audio file used:

http://cmldev.net/wp-content/uploads/2007/03/sdl_mixer_bug.wav

The length of this particular sample is 98304, which is an even multiple of 4096, the value I get for the mix_channels() 'len' argument when the buffer size is set to 1024.

I'm using SDL_mixer 1.2.7 in OS X 10.4.8.
Comment 1 Jesse Anders 2007-03-08 18:34:20 UTC
Created attachment 197 [details]
Audio sample

Here is the audio sample as an attachment.
Comment 2 Sam Lantinga 2007-07-14 21:42:10 UTC
This is fixed in subversion revision 3277, thanks!  This was a great test case, BTW.