[2.0] SDL_Mixer - Tempo Issues

I’m currently in the process of creating a basic framework on top of SDL 2.0 to help facilitate the development of a 2D game. I’ve been working on the audio portion lately so I created a test scene which loads around 20 .mp3 files and 50 .wav files and allows the tester to toggle through them and play them in order to ensure everything is working as intended.

Everything seemed okay until one particular song which plays 3-4x slower than it should be. I’m not sure what the issue is because every other song seems fine, but this one in particular seems troublesome…possibly due to how fast it is? I don’t really know enough about audio programming to fix this bug…or maybe I’m not using SDL_Mixer properly so here are some code snippets:

Window.cpp Snippet

Code:

int mixFlags = MIX_INIT_MP3 | MIX_INIT_OGG;
if (!(Mix_Init(mixFlags) & mixFlags)) {
	Debug::Log(EMessage::FATAL_ERROR, "Window", "Initialize", "Failed to initialize SDL Mix!  SDL Error: " + std::string(SDL_GetError()));
	Shutdown();
	return false;
}

int audioRate = MIX_DEFAULT_FREQUENCY;
Uint16 audioFormat = AUDIO_S16; /* 16-bit stereo */
int audioChannels = MIX_DEFAULT_CHANNELS;
int audioBuffers = 4096;

if (Mix_OpenAudio(audioRate, audioFormat, audioChannels, audioBuffers)) {
	Debug::Log(EMessage::FATAL_ERROR, "Window", "Initialize", "Failed to initialize SDL Mix Audio!  SDL Error: " + std::string(SDL_GetError()));
	Shutdown();
	return false;
}

if (!Mix_QuerySpec(&audioRate, &audioFormat, &audioChannels)) {
	std::stringstream ss;
	ss << "Audio Rate: " << audioRate << ", Audio Format: " << audioFormat << ", Audio Channels: " << audioChannels;
	Debug::Log(EMessage::WARNING, "Window", "Initialize", ss.str());
}

Music.h Snippet

Code:

private:
	_Mix_Music* mFile;

Music.cpp Snippet

Code:

bool Music::Load(const std::string& szFileName) {
Destroy();
mFile = Mix_LoadMUS(szFileName.c_str());

if (!mFile) {
	Debug::Log(EMessage::ERROR, "Music", "Load", "Failed to load Music from file " + szFileName + ".  MIX Error: " + Mix_GetError());
	return false;
}
return true;

}

void Music::Play(const int loops) const {
Stop();
if (Mix_PlayMusic(mFile, loops) != 0) {
Debug::Log(EMessage::ERROR, “Music”, “Play”, "Failed to play Music! MIX Error: " + std::string(Mix_GetError()));
}
}

void Music::SetVolume(const int musicVolume) const {
Mix_VolumeMusic(musicVolume);
}

void Music::Destroy() {
Mix_FreeMusic(mFile);
mFile = nullptr;
}

void Music::Stop() const {
Mix_HaltMusic();
}

void Music::Pause() const {
Mix_PauseMusic();
}

void Music::Resume() const {
Mix_ResumeMusic();
}

void Music::TogglePause() const {
if (Mix_PausedMusic()) {
Mix_ResumeMusic();
}
else {
Mix_PauseMusic();
}
}

What is the sampling rate of that file ? Is it the same as the others ?

Hmm, yeah, the other ones are 192kbps, and the one that’s being played slowly is 320kbps so that’d probably explain the slow tempo. Is there some way to accommodate this difference dynamically in code so both files play at the correct tempo?

There is no way to play multiple sounds of different sample rates at the same time using SDL_Mixer.

However it seems like using pure SDL audio functions it “might” be possible. But you’re on your own on loading formats other than WAV.

http://wiki.libsdl.org/SDL_LoadWAV?highlight=(\bCategoryAudio\b)|(CategoryEnum)|(CategoryStruct)
http://wiki.libsdl.org/SDL_OpenAudio?highlight=(\bCategoryAudio\b)|(CategoryEnum)|(CategoryStruct)

Artemiye wrote:> Hmm, yeah, the other ones are 192kbps, and the one that’s being played slowly is 320kbps so that’d probably explain the slow tempo. Is there some way to accommodate this difference dynamically in code so both files play at the correct tempo?

I guess I’m SOL then. I’ll just manually adjust the files via Audacity.

Actually I was expecting something like 44.1KHz vs 48KHz :-). I guessed it’s problem with sampling rate conversion.