-
Notifications
You must be signed in to change notification settings - Fork 0
/
rave.c
181 lines (146 loc) · 4.09 KB
/
rave.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <assert.h>
#include <raylib.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
uint64_t *g_frames;
uint64_t *g_frames_d;
uint64_t *render_lock = NULL;
uint64_t *callback_lock = NULL;
unsigned int cap;
int g_frames_count = 0;
/* Black */
static const Color background = CLITERAL(Color){0x00, 0x00, 0x00, 0xff};
/* White */
static const Color foreground = CLITERAL(Color){0xff, 0xff, 0xff, 0xff};
void callback(void *buffer, unsigned int frames) {
if (frames == 0) return;
while (render_lock == g_frames_d);
callback_lock = g_frames_d;
if (cap - g_frames_count >= frames) {
memcpy(g_frames_d, g_frames, g_frames_count * sizeof(uint64_t));
memcpy(g_frames_d + g_frames_count, buffer, frames * sizeof(uint64_t));
g_frames_count += frames;
} else if (cap >= frames) {
memcpy(g_frames_d, g_frames + frames, (cap - frames) * sizeof(uint64_t));
memcpy(g_frames_d + (cap - frames), buffer, frames * sizeof(uint64_t));
} else {
memcpy(g_frames_d, buffer, cap * sizeof(uint64_t));
g_frames_count = cap;
}
uint64_t *tmp = g_frames;
g_frames = g_frames_d;
g_frames_d = tmp;
callback_lock = NULL;
}
void render(uint64_t *buf, int fcount) {
int width = GetRenderWidth();
int height = GetRenderHeight();
float w = (((float)width) / (float)fcount);
while (callback_lock == buf);
render_lock = buf;
for (int i = 0; i < (fcount * 2); i++) {
float sample = ((float *)buf)[i];
int h = (int)(((float)height / 2) * sample);
if (sample > 0) {
DrawRectangle(i * w, height / 2 - h, 1, h / 5 + 1, foreground);
} else if (sample < 0) {
h = -h;
DrawRectangle(i * w, height / 2 + h - (h / 5 + 1), 1, h / 5 + 1,
foreground);
}
}
render_lock = NULL;
}
void usage(void) {
printf(
"usage: rave [music file] [-h<height in pixels>] [-w<width in pixels>] "
"[-b<buffer size in long words>] [-f<fps>]\n");
printf("If no music file is provided, waits for drag&drop.\n");
}
char *drag_and_drop(void) {
while (!WindowShouldClose()) {
if (IsFileDropped()) return LoadDroppedFiles().paths[0];
BeginDrawing();
ClearBackground(background);
DrawText("Drag&Drop a music file.", 0, 0, 45, foreground);
EndDrawing();
}
return NULL;
}
int main(int argc, char *argv[]) {
int buffer_size = 4800;
int height = 800;
int width = 1200;
int fps = 60;
char *music_file = NULL;
for (int i = 1; i < argc; i++) {
if (argv[i][0] == '-' && strlen(argv[i]) > 1) {
char d1, d2;
int *parse;
switch (argv[i][1]) {
case 'h':
parse = &height;
break;
case 'w':
parse = &width;
break;
case 'b':
parse = &buffer_size;
break;
case 'f':
parse = &fps;
break;
default:
usage();
return 1;
}
if (sscanf(argv[i], "%c%c%d", &d1, &d2, parse) != 3) {
usage();
return 1;
}
} else {
music_file = argv[i];
}
}
SetConfigFlags(FLAG_WINDOW_ALWAYS_RUN);
InitWindow(width, height, "Raylib Audio Visualizer");
SetTargetFPS(fps);
cap = buffer_size;
g_frames = malloc(buffer_size * sizeof(uint64_t));
g_frames_d = malloc(buffer_size * sizeof(uint64_t));
for (unsigned int i = 0; i < cap; i++) {
g_frames[i] = 0;
g_frames_d[i] = 0;
}
if (music_file == NULL) {
music_file = drag_and_drop();
if (music_file == NULL) {
usage();
return 1;
}
}
InitAudioDevice();
Music s = LoadMusicStream(music_file);
assert(s.stream.sampleSize == 32);
assert(s.stream.channels == 2);
PlayMusicStream(s);
AttachAudioStreamProcessor(s.stream, callback);
while (!WindowShouldClose()) {
UpdateMusicStream(s);
if (IsKeyPressed(KEY_Q) || IsKeyPressed(KEY_ESCAPE)) {
CloseWindow();
} else {
BeginDrawing();
ClearBackground(background);
if (g_frames_count != 0) render(g_frames, g_frames_count);
EndDrawing();
}
}
DetachAudioStreamProcessor(s.stream, callback);
StopMusicStream(s);
free(g_frames);
free(g_frames_d);
return 0;
}