-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
337 lines (248 loc) · 7.52 KB
/
example.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <math.h>
#include <argp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <SDL/SDL.h>
#include <sys/time.h>
#include "modplay.h"
// thx, https://gist.github.com/ictlyh/b9be0b020ae3d044dc75ef0caac01fbb
/* Program documentation. */
#ifdef TEST
static const char doc[] = "MODPlay example program ** COMPILED IN TEST MODE, MAY BE SLOWER **";
#else
static const char doc[] = "MODPlay example program";
#endif
/* A description of the arguments we accept. */
static const char args_doc[] = "MODFILE";
/* The options we understand. */
static struct argp_option options[] = {
{ "render", 'r', "filename.wav", 0, "render to a WAV file (disables playback)" },
{ "sample-rate", 'a', "HZ", 0, "sets the playback/render sample rate\n(in Hz, 44100 by default)" },
{ "buffer-size", 'b', "SAMPLES", 0, "forces a specific audio buffer size" },
{ "disable-ch-info", 'd', 0, 0, "disable live channel info during playback\n(enabled by default)" },
{ "start-order", 'o', "ORDER_ID", 0, "start playback from specified order\n(1 by default)" },
{ "speed", 's', "SPEED", 0, "force a given speed (the tune may override this setting)" },
{ "tempo", 't', "TEMPO", 0, "force a given tempo (the tune may override this setting)" },
{ 0 }
};
static bool disable_ch_info = false;
static char *render_filename = NULL;
static int start_order = 1;
static int start_speed = 0;
static int start_tempo = 0;
static int sample_rate = 44100;
static int buffer_size = 0;
static char *filename;
/* Parse a single option. */
static error_t parse_opt (int key, char *arg, struct argp_state *state) {
switch(key) {
case 'r':
render_filename = arg;
break;
case 'a':
sample_rate = atoi(arg);
break;
case 'b':
buffer_size = atoi(arg);
break;
case 'd':
disable_ch_info = true;
break;
case 'o':
start_order = atoi(arg);
break;
case 's':
start_speed = atoi(arg);
break;
case 't':
start_tempo = atoi(arg);
break;
case ARGP_KEY_ARG:
if (state->arg_num >= 1)
// Too many arguments.
argp_usage (state);
filename = arg;
break;
case ARGP_KEY_END:
if (state->arg_num < 1)
/* Not enough arguments. */
argp_usage (state);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
/* Our argp parser. */
static struct argp argp = { options, parse_opt, args_doc, doc };
static int channels;
#define BARGRAPHSTEPS 48
static double bargraphvals[CHANNELS];
static double bargraphtargets[CHANNELS];
static double bargrapholdages[CHANNELS];
static double callback_rate; // 2 for 44100 Hz, 4 for 22050 Hz, etc.
void SDL_Callback(void *data, uint8_t *stream, int len) {
short *buf = (short *) stream;
ModPlayerStatus_t *mp = RenderMOD(buf, len / (sizeof(short) * 2));
if(!disable_ch_info) {
printf("\n\nID | FREQ | VL | SM | BARGRAPH");
for(int i = 0; i < channels; i++) {
printf("\n\r\e[2KCH %02d |% 5d |% 3d |% 3d | ",
i + 1,
mp->ch[i].samplegen.period ? (mp->paularate / mp->ch[i].samplegen.period) : 0,
mp->ch[i].samplegen.volume,
mp->ch[i].sample + 1);
bargraphtargets[i] = mp->ch[i].samplegen.volume;
if(mp->ch[i].samplegen.age < bargrapholdages[i])
bargraphtargets[i] *= 1.5;
bargrapholdages[i] = mp->ch[i].samplegen.age;
if(mp->ch[i].period == 0) bargraphtargets[i] = 0.0;
if(bargraphvals[i] < bargraphtargets[i]) bargraphvals[i] = bargraphtargets[i];
for(int x = 0; x < BARGRAPHSTEPS; x++) {
if(bargraphvals[i] > x * 96 / BARGRAPHSTEPS)
putchar('=');
else
break;
}
if(bargraphvals[i] > bargraphtargets[i]) {
bargraphvals[i] -= callback_rate;
if(bargraphvals[i] < bargraphtargets[i]) bargraphvals[i] = bargraphtargets[i];
}
}
printf("\e[%dA", 2 + channels);
}
printf("\r\e[2KRow %02d, order %02d/%02d (pattern %02d) @ speed %d", mp->row, mp->order + 1, mp->orders, mp->ordertable[mp->order], mp->speed);
fflush(stdout);
}
SDL_AudioSpec sdl_audio = {
.freq = 0,
.format = AUDIO_S16,
.channels = 2,
.samples = 1024,
.callback = SDL_Callback
};
void play(ModPlayerStatus_t *mp) {
for(int i = 0; i < CHANNELS; i++) {
bargraphvals[i] = 0.0;
bargraphtargets[i] = 0.0;
bargrapholdages[i] = INFINITY;
}
sdl_audio.freq = sample_rate;
if(buffer_size) {
sdl_audio.samples = buffer_size;
} else {
while(sample_rate / sdl_audio.samples > 60 && sdl_audio.samples < 4096) {
sdl_audio.samples *= 2;
}
while(sample_rate / sdl_audio.samples < 30 && sdl_audio.samples > 64) {
sdl_audio.samples /= 2;
}
}
callback_rate = 44100.f * 2.f / ((double) sample_rate) * ((double) sdl_audio.samples) / 1024.f;
printf("Playing %s (at %d Hz, %d samples per frame)...\n\n", filename, sdl_audio.freq, sdl_audio.samples);
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
SDL_OpenAudio(&sdl_audio, 0);
SDL_PauseAudio(0);
SDL_Event event;
while(1) {
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
printf("\r\e[2KQuitting.\r\e[%dB\n\n", 2 + channels);
SDL_Quit();
exit(0);
break;
}
}
SDL_Delay(100);
}
}
void render(ModPlayerStatus_t *mp) {
// Yes, this will only work on a little endian machine. Too bad.
printf("Rendering %s...\n", filename);
int oldorder = -1, samples = 0;
uint8_t outbuf[512] = "RIFFabcdWAVE"
"fmt " // Format header
"\x10\x00\x00\x00" // Format header size (16 bytes)
"\x01\x00" // Type = PCM
"\x02\x00" // 2 channels
"efgh" // Sample rate (will fill this in later)
"ijkl" // Byte rate (will fill this in later)
"\x04\x00" // 4 bytes per sample
"\x10\x00" // 16 bits per sample
"data" // Data header
"mnop"; // Data size (will fill this in later)
struct timeval stop, start;
gettimeofday(&start, NULL);
FILE *f = fopen(render_filename, "wb");
if(f == NULL) {
printf("Error opening file for writing!\n");
exit(1);
}
fwrite(outbuf, 1, 44, f);
while(oldorder <= mp->order) {
if(oldorder != mp->order) {
printf("-> order %d/%d\n", mp->order + 1, mp->orders);
oldorder = mp->order;
}
RenderMOD((short *) outbuf, 128); // TODO - use audiospeed for this
fwrite(outbuf, 1, sizeof(outbuf), f);
samples += 128;
}
// Fill in the sample rate & byte rate
fseek(f, 24, SEEK_SET);
uint32_t tmp = sample_rate;
fwrite(&tmp, 1, 4, f);
tmp *= 4;
fwrite(&tmp, 1, 4, f);
// Fill in the data size
tmp = samples * 4;
fseek(f, 40, SEEK_SET);
fwrite(&tmp, 1, 4, f);
// Fill in the file size
tmp += 36;
fseek(f, 4, SEEK_SET);
fwrite(&tmp, 1, 4, f);
fclose(f);
gettimeofday(&stop, NULL);
double rendered = ((double) samples) / ((double) sample_rate);
double elapsed = (double) (stop.tv_usec - start.tv_usec) / 1000000.0 + (double) (stop.tv_sec - start.tv_sec);
printf("Rendered %.3fs of audio in %.3fs (%.3fx as fast as real time).\n", rendered, elapsed, rendered / elapsed);
}
int main(int argc, char *argv[]) {
argp_parse(&argp, argc, argv, 0, 0, NULL);
FILE *f = fopen(filename, "rb");
if(f == NULL) {
printf("Error loading file!\n");
exit(1);
}
fseek(f, 0, SEEK_END);
int tune_len = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t *tune = malloc(tune_len);
fread(tune, 1, tune_len, f);
fclose(f);
ModPlayerStatus_t *mp = InitMOD(tune, sample_rate);
if(!mp) {
printf("Invalid file!\n");
exit(1);
}
channels = mp->channels;
if(start_order > 1 && start_order <= mp->orders) {
JumpMOD(start_order - 1);
}
if(start_speed > 0) {
mp->speed = start_speed;
}
if(start_tempo > 0) {
mp->audiospeed = mp->samplerate * 125 / start_tempo / 50;
}
// printf("Status type size: %d\n", sizeof(ModPlayerStatus_t));
if(render_filename == NULL) {
play(mp);
} else {
render(mp);
}
}