#include "../includes/audio.h" #include "SDL_mixer.h" #include // Stocke les données à propos de la musique static double average; static int _channel; double averageMusic(void) { return average; } void callback(void *_, Uint8 *stream, int size) { int i = 0; double tmp = 0.; for (signed short *stream16 = (signed short *)stream; i < size / 2; ++i) { tmp += stream16[i] / ((1 << 15) + 1.); } average = fabs(tmp / (i / 100.)); } void initMusic(Mix_Chunk *music, const char *filename) { if (!Mix_Init(MIX_INIT_MID)) { fprintf(stderr, "Erreur Mix_Init\n"); exit(2); } if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 1, 2048)) { fprintf(stderr, "Erreur Mix_OpenAudio\n"); exit(2); }; if (!(music = Mix_LoadWAV(filename))) { fprintf(stderr, "Erreur Mix_LoadWAV for '%s'\n", filename); exit(2); } Mix_SetPostMix(callback, NULL); if ((_channel = Mix_PlayChannel(-1, music, -1)) == -1) { fprintf(stderr, "Erreur Mix_PlayChannel\n"); exit(2); }; } void freeMusic(Mix_Chunk *music) { if (music) { if (Mix_Playing(_channel)) { Mix_HaltChannel(_channel); } Mix_FreeChunk(music); music = NULL; } Mix_CloseAudio(); Mix_Quit(); }