#include "../includes/audio.h" #include // Stocke les données à propos de la musique static double average; 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); } void initMusic(Mix_Music *music, const char *filename) { Mix_Init(MIX_INIT_MID); if (Mix_OpenAudio(44100, AUDIO_S16LSB, 1, 1024)) { fprintf(stderr, "Erreur Mix_OpenAudio\n"); exit(2); }; if (!(music = Mix_LoadMUS(filename))) { fprintf(stderr, "Erreur Mix_LoadMUS\n"); exit(2); } Mix_VolumeMusic(MIX_MAX_VOLUME / 10); Mix_SetPostMix(callback, NULL); if (Mix_PlayMusic(music, 1)) { fprintf(stderr, "Erreur Mix_PlayMusic\n"); exit(2); }; } void freeMusic(Mix_Music *music) { if (music) { if (Mix_PlayingMusic()) { Mix_HaltMusic(); } Mix_FreeMusic(music); music = NULL; } Mix_CloseAudio(); Mix_Quit(); }