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

50 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:21:12 +02:00
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);
2023-05-03 18:21:12 +02:00
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();
}