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

40 lines
849 B
C
Raw Normal View History

#include "../includes/audio.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);
Mix_OpenAudio(44100, AUDIO_S16LSB, 2, 1024);
music = Mix_LoadMUS(filename);
Mix_VolumeMusic(MIX_MAX_VOLUME / 10);
Mix_SetPostMix(callback, NULL);
Mix_PlayMusic(music, 1);
}
void freeMusic(Mix_Music *music) {
if (music) {
if (Mix_PlayingMusic()) {
Mix_HaltMusic();
}
Mix_FreeMusic(music);
music = NULL;
}
Mix_CloseAudio();
Mix_Quit();
}