2023-05-03 12:22:42 +02:00
|
|
|
#include "../includes/audio.h"
|
2023-05-03 19:53:05 +02:00
|
|
|
#include "SDL_mixer.h"
|
2023-05-03 18:21:12 +02:00
|
|
|
#include <stdio.h>
|
2023-05-03 12:22:42 +02:00
|
|
|
|
|
|
|
// Stocke les données à propos de la musique
|
|
|
|
static double average;
|
|
|
|
|
2023-05-03 19:53:05 +02:00
|
|
|
static int _channel;
|
|
|
|
|
2023-05-03 12:22:42 +02:00
|
|
|
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) {
|
2023-05-03 20:03:03 +02:00
|
|
|
tmp += stream16[i] / ((1 << 15) + 1.);
|
2023-05-03 12:22:42 +02:00
|
|
|
}
|
2023-05-03 20:03:03 +02:00
|
|
|
average = fabs(tmp / (i / 100.));
|
2023-05-03 12:22:42 +02:00
|
|
|
}
|
|
|
|
|
2023-05-03 19:53:05 +02:00
|
|
|
void initMusic(Mix_Chunk *music, const char *filename) {
|
2023-05-03 20:11:18 +02:00
|
|
|
if (!Mix_Init(MIX_INIT_MID)) {
|
|
|
|
fprintf(stderr, "Erreur Mix_Init\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2023-05-03 18:38:41 +02:00
|
|
|
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 1, 2048)) {
|
2023-05-03 18:21:12 +02:00
|
|
|
fprintf(stderr, "Erreur Mix_OpenAudio\n");
|
|
|
|
exit(2);
|
|
|
|
};
|
2023-05-03 18:38:41 +02:00
|
|
|
|
2023-05-03 19:53:05 +02:00
|
|
|
if (!(music = Mix_LoadWAV(filename))) {
|
|
|
|
fprintf(stderr, "Erreur Mix_LoadWAV for '%s'\n", filename);
|
2023-05-03 18:21:12 +02:00
|
|
|
exit(2);
|
|
|
|
}
|
2023-05-03 12:22:42 +02:00
|
|
|
|
|
|
|
Mix_SetPostMix(callback, NULL);
|
|
|
|
|
2023-05-03 19:53:05 +02:00
|
|
|
if ((_channel = Mix_PlayChannel(-1, music, -1)) == -1) {
|
|
|
|
fprintf(stderr, "Erreur Mix_PlayChannel\n");
|
2023-05-03 18:21:12 +02:00
|
|
|
exit(2);
|
|
|
|
};
|
2023-05-03 12:22:42 +02:00
|
|
|
}
|
|
|
|
|
2023-05-03 19:53:05 +02:00
|
|
|
void freeMusic(Mix_Chunk *music) {
|
2023-05-03 12:22:42 +02:00
|
|
|
if (music) {
|
2023-05-03 19:53:05 +02:00
|
|
|
if (Mix_Playing(_channel)) {
|
|
|
|
Mix_HaltChannel(_channel);
|
2023-05-03 12:22:42 +02:00
|
|
|
}
|
2023-05-03 19:53:05 +02:00
|
|
|
Mix_FreeChunk(music);
|
2023-05-03 12:22:42 +02:00
|
|
|
music = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mix_CloseAudio();
|
|
|
|
Mix_Quit();
|
|
|
|
}
|