audio with dynamic audio files

This commit is contained in:
Frank Villaro-Dixon 2015-01-02 17:18:29 +01:00
parent 4cc5cc82d6
commit de4ed42491
16 changed files with 535 additions and 99 deletions

View file

@ -1,5 +1,5 @@
all:
clang src/*.c `sdl-config --cflags --libs` -lSDL_mixer -lm -Wall -Wextra -o hyper_rotagon
clang src/*.c -g3 `sdl-config --cflags --libs` -lSDL_mixer -lm -Wall -Wextra -o hyper_rotagon
clean:
rm hyper_rotagon

2
audio/AVAIL_MUSICS Normal file
View file

@ -0,0 +1,2 @@
DIM.M_-_DIM.M_-__16BIT.wav
OxiD_-_8bit.mp3

Binary file not shown.

View file

@ -0,0 +1,181 @@
2.799121270
3.405993639
4.079199207
4.751378455
6.743124372
7.431422720
8.095099273
9.414840182
10.103351160
10.767543516
12.143101791
16.183125427
17.103421185
17.406726148
18.038984995
19.071436150
19.350678395
19.687130854
21.703173727
21.990995832
22.302219295
24.039014978
25.047194403
25.366899738
26.399328781
26.710914574
27.727235287
29.070995928
29.390277316
30.391055681
30.703095435
31.751215442
32.062383507
33.039349241
33.359043696
34.383447312
34.710683983
37.406877321
38.087126816
39.351047424
39.679268176
40.647400313
40.982934217
41.358678321
42.655025247
44.078866066
45.358838863
46.687116904
48.086980613
49.414011712
50.654983683
51.983107122
52.671120351
55.055367422
55.423029406
55.694931732
56.038884203
57.358696739
57.991537423
58.647456713
59.351340532
60.031316440
60.687376918
61.383150048
62.062757279
62.727219683
63.414975824
64.079446326
64.686960970
65.351356475
66.015112430
66.663054951
67.375329567
68.047376300
68.703315876
69.375493754
70.039297756
70.703289852
71.383282296
72.023138801
72.663400096
73.343313065
73.967249447
74.647223519
75.318869121
75.991174041
76.695186668
77.391514888
78.030831685
78.711190034
79.367215039
80.007332761
80.703219214
81.391099116
82.022100966
82.695267301
83.383385475
84.071430738
84.727346582
85.383405891
86.079371084
86.766428284
87.447414362
88.079128834
88.735159802
89.399277711
90.087417927
90.767178782
91.407416044
92.071060784
92.711132429
93.399083456
94.071378605
96.982807593
98.111270188
99.366992533
101.215191779
102.071214147
103.430860885
104.758775261
107.447060783
108.718929417
110.038052417
111.334858009
112.790509654
114.110849646
114.830796520
115.374739228
118.086637328
119.382879438
120.327124141
121.334279659
121.655075552
121.990723767
122.309836530
123.710809046
125.069973584
126.454084476
127.710691596
128.406802762
129.055087368
129.766439234
130.423106748
131.726842365
132.415279117
133.102811027
133.735359196
134.342785694
135.710785386
136.430922250
137.062829938
140.135242878
141.606789242
142.719143686
144.038548577
145.422614598
146.821831089
148.070818480
149.365916757
150.694765973
152.046589127
153.358790675
154.638844765
155.287271577
156.479318095
157.774561435
158.735269802
160.014080477
161.366855887
162.006659170
162.671300132
163.966878504
165.382540524
166.726798035
168.022746856
169.382825048
170.662874337
172.006808181
173.358748375
174.015033166
175.078907619

BIN
audio/OxiD_-_8bit.mp3 Normal file

Binary file not shown.

View file

@ -0,0 +1,94 @@
1.516679035
1.540187396
2.002545669
2.897811185
3.896592771
4.863039320
5.845757879
1.032259708
2.007150248
2.957088029
3.914903334
4.850520340
5.824917605
6.783502254
7.766241177
8.652908641
9.603513947
10.609908071
11.616489152
12.566514683
13.516857565
14.452016950
15.371147265
16.361445539
17.312036233
18.262685873
19.253377331
20.227723727
21.146326761
22.088837796
23.071673403
23.556922809
24.003699810
24.465662596
24.984614217
25.462392789
25.908974638
26.435129881
26.905590681
27.391757503
27.822230092
28.356462098
28.826800306
29.297019493
29.767322081
30.277485371
30.747826994
31.218309805
31.704229048
33.135252728
33.613860805
35.068597544
35.514546569
37.017829192
37.944695781
39.871957740
41.807525776
43.702473356
45.637345359
47.604609595
49.531774398
51.410625446
53.298193277
53.776138175
54.230599727
54.716719778
55.251075736
55.721199955
56.183024584
56.628117700
57.156345396
57.642547041
58.120774158
58.591094663
59.125426483
59.547258416
60.033476923
60.487792449
61.014727857
61.492227109
61.963626228
62.417709293
62.904223466
63.374321370
63.890819561
64.330843745
64.849127484
65.327562406
65.813743042
66.266179974
66.778535669
67.296945222
67.727068293
68.189046523

View file

View file

@ -1,75 +0,0 @@
2.847254552
2.997123886
3.187358367
3.329112981
3.903859124
4.093101074
4.628480381
4.810299832
5.000607697
5.190262526
5.724933872
5.882498013
6.145166568
6.358887131
6.557398015
6.739249437
7.329791416
7.923106506
8.352454416
8.922743422
9.095985371
10.047007003
10.219828993
10.581944582
11.564514422
11.746278661
11.928596176
12.118479017
12.308819651
13.194737393
13.697964142
14.656913675
14.838240846
15.020543878
15.202888304
15.400701241
16.205802392
16.372948980
16.555745773
16.729538514
17.767830749
17.942386845
18.108622017
18.290055586
18.456553392
19.239673241
19.795658812
20.854475954
21.052255889
21.521443159
21.670031277
22.489120616
22.653419373
23.088754125
23.254390492
23.824465858
23.982048166
24.504870599
24.656008352
25.407961235
25.797650266
26.313551326
26.520676395
26.686349445
26.868799111
27.042173361
27.446639331
28.100783371
28.249282315
28.792045848
28.998001125
29.170862984
29.354634960
29.560513659
29.774344974

BIN
mus.mp3

Binary file not shown.

View file

@ -10,6 +10,10 @@
#include "SDL/SDL.h"
#include "SDL_prims.h"
#define MUSICS_PATH "./audio/"
#define MUSICS_PATH_SDL "/audio/"
#define MUSICS_FILE MUSICS_PATH"AVAIL_MUSICS"
#define SCREEN_WIDTH 1000
#define SCREEN_HEIGHT 700
#define SCREEN_DIAGONAL (sqrt(SCREEN_HEIGHT*SCREEN_HEIGHT + SCREEN_WIDTH*SCREEN_WIDTH)/2)
@ -55,16 +59,19 @@ struct s_obstacle {
struct s_game {
int cursor_angle; //relative to the polygon center's referential
int general_rotation;
float sexual_pulsation;
int polygon_type; //Shape
int color;
int actual_speed;
int counter;
SDL_Surface *screen;
Uint8 *keys;
int general_rotation; //rotate all the objects
float sexual_pulsation; //let's feel the beats !
int polygon_type; //Shape; it's number of vertices
int color; //the actual game color
int actual_speed; //it's actual speed
int counter; //number of frames since start
//our table containing all the obstacles
#define NUM_OBS 100
struct s_obstacle obs[NUM_OBS];
int num_obs;
@ -72,7 +79,20 @@ struct s_game {
struct timespec start_time;
int beat_already_done;
struct {
int avail_musics;
#define MAX_MUSICS_NUM 30
char *musics[MAX_MUSICS_NUM]; //the available musics
float *beats[MAX_MUSICS_NUM];
int num_beats[MAX_MUSICS_NUM];
int currently_playing;
//which beat was already done
int beat_already_done;
} audio;
SDL_Surface *screen;
Uint8 *keys;
};

View file

@ -24,6 +24,7 @@ start:
init_game(&ga);
init_music(&ga);
init_SDL(&ga);
music_loadnplay(&ga);
struct s_obstacle a = create_obstacle(SHAPE_THICK, 2, 1, -1);
add_obstacle(&ga, a);

View file

@ -10,21 +10,109 @@
#include <time.h>
#include "SDL_mixer.h"
#include "game.h"
#include "utils.h"
#include "mus_beats.h"
void
init_music(struct s_game *g) {
Mix_OpenAudio(22050,AUDIO_S16SYS,2,640);
Mix_Music *mus;
mus = Mix_LoadMUS("./mus.mp3");
Mix_PlayMusic(mus,1);
music_load_beats(struct s_game *g, int music) { // {{{
FILE *f;
int i = 0;
char buffer[2048];
float *beats = NULL;
g->beat_already_done = 0;
}
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) && i < MAX_MUSICS_NUM) {
//ugly, but it's a one time
beats = realloc(beats, (i+1) * sizeof(*beats));
beats[i] = (long)atof(strtrim(buffer));
i++;
}
g->audio.beats[music] = beats;
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);
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;
printf(">> %d\n", g->audio.currently_playing);
if(music == -1)
return 0;
struct timespec actual_time;
clock_gettime(CLOCK_REALTIME, &actual_time);
@ -33,11 +121,16 @@ did_have_beat(struct s_game *g) {
(actual_time.tv_nsec - g->start_time.tv_nsec) / 1e9;
printf(">> %d, done: %d\n", g->audio.num_beats[music], g->audio.beat_already_done);
// printf("Secs passed: %lf, actual: %lf\n", secs_passed, mus_beats[already_done]);
if(mus_beats[g->beat_already_done] < secs_passed) {
g->beat_already_done++;
return 1;
if(g->audio.num_beats[music] > g->audio.beat_already_done) {
puts("Hallo");
if(g->audio.beats[music][g->audio.beat_already_done] < secs_passed) {
g->audio.beat_already_done++;
return 1;
}
}
return 0;
}

View file

@ -10,6 +10,9 @@
void
init_music(struct s_game *g);
void
music_loadnplay(struct s_game *g);
int
did_have_beat(struct s_game *g);

80
src/utils.c Normal file
View file

@ -0,0 +1,80 @@
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "utils.h"
char *
strtrim(char *str)
{
char *end;
if(!str)
return NULL;
//le debut
while(isspace(*str)) str++;
if(*str == '\0') //fin du str
return str;
//la fin
end = str + strlen(str) - 1;
while(end > str && isspace(*end))
end--;
//on ferme la fin
*(end+1) = '\0';
return str;
}
void *
auto_malloc(size_t size)
{
void *mem;
if((mem = malloc(size)) == NULL) {
FAILE("Could not malloc; outamem");
}
return mem;
}
void *
auto_calloc(size_t nmemb, size_t size)
{
void *mem;
if((mem = calloc(nmemb, size)) == NULL) {
FAILE("Could not calloc; outamem");
}
return mem;
}
char *
strdupcat(char *str, char *str2)
{
char *ret;
size_t len;
if(str && *str)
len = strlen(str);
else
return strdup(str2);
if(str2 && *str2)
len += strlen(str2);
else
return strdup(str);
len += 1;
if((ret = calloc(len, sizeof(*ret))) == NULL)
return NULL;
strcpy(ret, str);
strcat(ret, str2);
return ret;
}

28
src/utils.h Normal file
View file

@ -0,0 +1,28 @@
/*
** utils.h - <+DESC+>
**
** 2014 - Frank Villaro-Dixon <Frank@Villaro-Dixon.eu>
*/
#ifndef UTILS_H
#define UTILS_H
#include <stdio.h>
#define FAILE(msg) do { fprintf(stderr, msg); exit(0); } while(0);
char *
strtrim(char *str);
void *
auto_malloc(size_t size);
void *
auto_calloc(size_t nmemb, size_t size);
char *
strdupcat(char *str, char *str2);
#endif /* ndef UTILS_H */

View file

@ -1,13 +1,22 @@
#!/bin/bash
mplayer mus.mp3 > /dev/null &
START=$(date +%s.%N)
if [ $# -ne 1 ]; then
>&2 echo "Usage: $0 music.ext"
exit
fi
> mus.beats
mplayer $1 > /dev/null &
START=$(date +%s.%N)
sleep 0.2 #deal with human lag
> $1.beats.bak
while true; do
read n
END=$(date +%s.%N)
DIFF=$(echo "$END - $START" | bc)
echo $DIFF >> mus.beats
echo $DIFF >> $1.beats
done
#cat $1.beats.bak | wc -l > $1.beats
cat $1.beats.bak >> $1.beats