From this Lazy Foo tutorial (https://lazyfoo.net/tutorials/SDL/21_sound_effects_and_music/index.php) I wrote the following lines of code:
#include <SDL.h>
#include <SDL_mixer.h>
bool running = true;
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
SDL_Window* window = SDL_CreateWindow("testing musique", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, 0);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Event quit;
Mix_Music* music;
while (running) {
while(SDL_PollEvent(&quit)){
switch(quit.type) {
case SDL_QUIT:
running = false;
break;
}
}
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
music = Mix_LoadMUS("../pikachu/keypress_BMP/beat.wav");
Mix_PlayMusic(music, -1);
SDL_SetRenderDrawColor(renderer, 20, 20, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
Mix_FreeMusic(music);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
I even used the same audio file (beat.wav) as Lazy Foo did in his tutorial, but while his program runs without a hitch, mine plays the wav too fast (despite the fact that I checked every parameters to make sure mine matched with his). I tried decreasing the frequency parameter in Mix_OpenAudio, but while the wav did slow down, so did the pitch, and it should not have made sense to do this in the first place. What should I do?
The audio subsystem should only be opened once, before the main loop, meanwhile you open it in each iteration of the loop. The same goes for loading the file into memory — again on each iteration of the loop, instead of once. So the correct code structure should look like this:
Don't forget about
Mix_CloseAudio
and about error checking.