use chunk instead of music

This commit is contained in:
Mylloon 2023-05-03 19:53:05 +02:00
parent 1089501ad9
commit a93521e673
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 16 additions and 13 deletions

View file

@ -4,12 +4,12 @@
#include <SDL_mixer.h> #include <SDL_mixer.h>
// Charge et lance la musique // Charge et lance la musique
void initMusic(Mix_Music *, const char *); void initMusic(Mix_Chunk *, const char *);
// Renvoie une moyenne de l'intensité du son // Renvoie une moyenne de l'intensité du son
double averageMusic(void); double averageMusic(void);
// Libère la musique en mémoire // Libère la musique en mémoire
void freeMusic(Mix_Music *); void freeMusic(Mix_Chunk *);
#endif #endif

2
main.c
View file

@ -4,7 +4,7 @@
#include "includes/animations.h" #include "includes/animations.h"
// Son de fond // Son de fond
static Mix_Music *_ambiance = NULL; static Mix_Chunk *_ambiance = NULL;
// Dimensions initiales de la fenêtre // Dimensions initiales de la fenêtre
GLuint _dims[] = {1280, 720}; GLuint _dims[] = {1280, 720};

View file

@ -1,9 +1,12 @@
#include "../includes/audio.h" #include "../includes/audio.h"
#include "SDL_mixer.h"
#include <stdio.h> #include <stdio.h>
// Stocke les données à propos de la musique // Stocke les données à propos de la musique
static double average; static double average;
static int _channel;
double averageMusic(void) { return average; } double averageMusic(void) { return average; }
void callback(void *_, Uint8 *stream, int size) { void callback(void *_, Uint8 *stream, int size) {
@ -15,33 +18,33 @@ void callback(void *_, Uint8 *stream, int size) {
average = fabs(tmp / i); average = fabs(tmp / i);
} }
void initMusic(Mix_Music *music, const char *filename) { void initMusic(Mix_Chunk *music, const char *filename) {
Mix_Init(MIX_INIT_MID); Mix_Init(MIX_INIT_MID);
if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 1, 2048)) { if (Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, 1, 2048)) {
fprintf(stderr, "Erreur Mix_OpenAudio\n"); fprintf(stderr, "Erreur Mix_OpenAudio\n");
exit(2); exit(2);
}; };
if (!(music = Mix_LoadMUS(filename))) { if (!(music = Mix_LoadWAV(filename))) {
fprintf(stderr, "Erreur Mix_LoadMUS for '%s'\n", filename); fprintf(stderr, "Erreur Mix_LoadWAV for '%s'\n", filename);
exit(2); exit(2);
} }
Mix_VolumeMusic(MIX_MAX_VOLUME / 10); Mix_VolumeChunk(music, MIX_MAX_VOLUME / 10);
Mix_SetPostMix(callback, NULL); Mix_SetPostMix(callback, NULL);
if (Mix_PlayMusic(music, -1)) { if ((_channel = Mix_PlayChannel(-1, music, -1)) == -1) {
fprintf(stderr, "Erreur Mix_PlayMusic\n"); fprintf(stderr, "Erreur Mix_PlayChannel\n");
exit(2); exit(2);
}; };
} }
void freeMusic(Mix_Music *music) { void freeMusic(Mix_Chunk *music) {
if (music) { if (music) {
if (Mix_PlayingMusic()) { if (Mix_Playing(_channel)) {
Mix_HaltMusic(); Mix_HaltChannel(_channel);
} }
Mix_FreeMusic(music); Mix_FreeChunk(music);
music = NULL; music = NULL;
} }