#include "../includes/audio.h"
#include "SDL_mixer.h"
#include <stdio.h>
// 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);
void initMusic(Mix_Chunk *music, const char *filename) {
Mix_Init(MIX_INIT_MID);
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);
Mix_SetPostMix(callback, NULL);
if ((_channel = Mix_PlayChannel(-1, music, -1)) == -1) {
fprintf(stderr, "Erreur Mix_PlayChannel\n");
void freeMusic(Mix_Chunk *music) {
if (music) {
if (Mix_Playing(_channel)) {
Mix_HaltChannel(_channel);
Mix_FreeChunk(music);
music = NULL;
Mix_CloseAudio();
Mix_Quit();