133 lines
2.6 KiB
C
133 lines
2.6 KiB
C
/*
|
|
** music.c - <+DESC+>
|
|
**
|
|
** 2014 - Frank Villaro-Dixon <Frank@Villaro-Dixon.eu>
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <sys/time.h>
|
|
#include <time.h>
|
|
#include "SDL_mixer.h"
|
|
#include "game.h"
|
|
#include "utils.h"
|
|
#include "mus_beats.h"
|
|
|
|
void
|
|
music_load_beats(struct s_game *g, int music) { // {{{
|
|
FILE *f;
|
|
int i = 0;
|
|
char buffer[2048];
|
|
float *beats = NULL;
|
|
|
|
char *beats_file_tmp = strdupcat(MUSICS_PATH, g->audio.musics[music]);
|
|
char *beats_file = strdupcat(beats_file_tmp, ".beats");
|
|
free(beats_file_tmp);
|
|
|
|
printf("Will read %s\n", beats_file);
|
|
|
|
if(!(f = fopen(beats_file, "r"))) {
|
|
perror("Could not load beats for music");
|
|
g->audio.num_beats[music] = 0;
|
|
return;
|
|
}
|
|
|
|
free(beats_file);
|
|
|
|
while(fgets(buffer, sizeof(buffer), f)) {
|
|
//ugly, but it's a one time
|
|
beats = realloc(beats, (i+1) * sizeof(*beats));
|
|
beats[i] = strtof(strtrim(buffer), NULL);
|
|
i++;
|
|
}
|
|
|
|
g->audio.beats[music] = beats;
|
|
g->audio.num_beats[music] = i;
|
|
|
|
|
|
fclose(f);
|
|
} //}}}
|
|
|
|
void
|
|
music_find_avail(struct s_game *g) { //{{{
|
|
FILE *f;
|
|
int i = 0;
|
|
char buffer[2048];
|
|
|
|
if(!(f = fopen(MUSICS_FILE, "r"))) {
|
|
perror("Could not load music list");
|
|
g->audio.avail_musics = 0;
|
|
return;
|
|
}
|
|
|
|
while(fgets(buffer, sizeof(buffer), f) && i < MAX_MUSICS_NUM) {
|
|
g->audio.musics[i] = strdup(strtrim(buffer));
|
|
music_load_beats(g, i);
|
|
i++;
|
|
puts("toto");
|
|
}
|
|
|
|
g->audio.avail_musics = i;
|
|
|
|
fclose(f);
|
|
}//}}}
|
|
|
|
void
|
|
music_loadnplay(struct s_game *g) { //{{{
|
|
Mix_Music *mus;
|
|
|
|
if(!g->audio.avail_musics) {
|
|
g->audio.currently_playing = -1;
|
|
return;
|
|
}
|
|
|
|
printf("Num musics %d\n", g->audio.avail_musics);
|
|
|
|
//choose one random
|
|
g->audio.currently_playing = rand()%g->audio.avail_musics;
|
|
|
|
char *music_path = strdupcat(MUSICS_PATH,
|
|
g->audio.musics[g->audio.currently_playing]);
|
|
|
|
printf("Music to read: %s\n", music_path);
|
|
|
|
mus = Mix_LoadMUS(music_path);
|
|
|
|
Mix_PlayMusic(mus, 1);
|
|
g->audio.beat_already_done = 0;
|
|
free(music_path);
|
|
}//}}}
|
|
|
|
void
|
|
init_music(struct s_game *g) { //{{{
|
|
Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 640);
|
|
|
|
g->audio.avail_musics = 0;
|
|
g->audio.beat_already_done = 0;
|
|
music_find_avail(g);
|
|
}//}}}
|
|
|
|
|
|
int
|
|
did_have_beat(struct s_game *g) {
|
|
int music = g->audio.currently_playing;
|
|
|
|
if(music == -1)
|
|
return 0;
|
|
|
|
struct timespec actual_time;
|
|
clock_gettime(CLOCK_REALTIME, &actual_time);
|
|
|
|
float secs_passed = actual_time.tv_sec - g->start_time.tv_sec +
|
|
(actual_time.tv_nsec - g->start_time.tv_nsec) / 1e9;
|
|
|
|
if(g->audio.num_beats[music] > g->audio.beat_already_done) {
|
|
if(g->audio.beats[music][g->audio.beat_already_done] < secs_passed) {
|
|
g->audio.beat_already_done++;
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|