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();
}
}