This repository has been archived on 2023-05-27. You can view files and clone it, but cannot push or open issues or pull requests.
api8/src/audio.c

51 lines
1.1 KiB
C
Raw Normal View History

#include "../includes/audio.h"
2023-05-03 18:21:12 +02:00
#include <stdio.h>
// 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);
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 18:21:12 +02:00
if (!(music = Mix_LoadMUS(filename))) {
2023-05-03 18:38:41 +02:00
fprintf(stderr, "Erreur Mix_LoadMUS for '%s'\n", filename);
2023-05-03 18:21:12 +02:00
exit(2);
}
Mix_VolumeMusic(MIX_MAX_VOLUME / 10);
Mix_SetPostMix(callback, NULL);
2023-05-03 19:36:13 +02:00
if (Mix_PlayMusic(music, -1)) {
2023-05-03 18:21:12 +02:00
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();
}