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

70 lines
1.8 KiB
C
Raw Normal View History

#include "../includes/audio.h"
// Stocke les données à propos de la musique
static double average;
2023-05-03 21:05:06 +02:00
// Channel son utilisé
2023-05-03 19:53:05 +02:00
static int _channel;
2023-05-03 21:05:06 +02:00
// Code utilisé en tant qu'erreur de SDL_Mixer
static int _errStatus = 2;
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 20:03:03 +02:00
average = fabs(tmp / (i / 100.));
}
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)) {
2023-05-03 21:05:06 +02:00
fprintf(stderr, "Erreur Mix_Init - Chargement des librairies dynamiques\n");
exit(_errStatus);
2023-05-03 20:11:18 +02:00
}
if (!Mix_GetSoundFonts()) {
fprintf(stderr,
"Aucun soundfonts, mettez les dans /usr/share/sounds/sf2\n");
fprintf(stderr,
"S'ils sont dans /usr/share/soundfonts, "
"vous pouvez faire :\n"
"sudo mkdir -p /usr/share/sounds/sf2 && "
"sudo ln -s /usr/share/soundfonts/*.sf2 /usr/share/sounds/sf2/\n");
exit(_errStatus);
}
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");
2023-05-03 21:05:06 +02:00
exit(_errStatus);
2023-05-03 18:21:12 +02:00
};
2023-05-03 18:38:41 +02:00
2023-05-03 19:53:05 +02:00
if (!(music = Mix_LoadWAV(filename))) {
2023-05-03 21:05:06 +02:00
fprintf(stderr, "Erreur Mix_LoadWAV en chargant '%s'\n", filename);
exit(_errStatus);
2023-05-03 18:21:12 +02:00
}
Mix_SetPostMix(callback, NULL);
2023-05-03 19:53:05 +02:00
if ((_channel = Mix_PlayChannel(-1, music, -1)) == -1) {
2023-05-03 21:05:06 +02:00
fprintf(stderr, "Erreur Mix_PlayChannel - Le son n'as pas pu être joué\n");
exit(_errStatus);
2023-05-03 18:21:12 +02:00
};
}
2023-05-03 19:53:05 +02:00
void freeMusic(Mix_Chunk *music) {
if (music) {
2023-05-03 19:53:05 +02:00
if (Mix_Playing(_channel)) {
Mix_HaltChannel(_channel);
}
2023-05-03 19:53:05 +02:00
Mix_FreeChunk(music);
music = NULL;
}
Mix_CloseAudio();
Mix_Quit();
}