This repository has been archived by the owner on Nov 27, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
420 lines (345 loc) · 8.59 KB
/
main.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <getopt.h>
#include <ccore/file.h>
#include <ccore/display.h>
#include <ccore/window.h>
#include <ccore/opengl.h>
#include <ccore/event.h>
#include <ccore/string.h>
#include <ccore/error.h>
#include <ccore/time.h>
#include <GL/glew.h>
#include <fftw3.h>
#include <pthread.h>
#include <alsa/asoundlib.h>
#include "shader.h"
#include "utils.h"
#define BUFFER_SIZE 2048
#define LISTEN_BUFFER_SIZE 128
#define WIDTH 1600
#define HEIGHT 900
typedef struct {
int format, out[BUFFER_SIZE];
unsigned int rate;
char *source;
} audiodata_t;
pthread_mutex_t mutex;
pthread_cond_t cond;
bool threaddone;
int alsaSetHwParams(snd_pcm_t *handle, snd_pcm_hw_params_t *params, int *dir, snd_pcm_uframes_t *frames)
{
snd_pcm_hw_params_any(handle, params);
if(snd_pcm_hw_params_set_access(handle, params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0){
#ifdef VISMU_DEBUG
fprintf(stderr, "Error setting access\n");
#endif
return -1;
}
if(snd_pcm_hw_params_set_format(handle, params, SND_PCM_FORMAT_S16_LE) < 0){
#ifdef VISMU_DEBUG
fprintf(stderr, "Error setting format\n");
#endif
return -1;
}
if(snd_pcm_hw_params_set_channels(handle, params, 2) < 0){
#ifdef VISMU_DEBUG
fprintf(stderr, "Error setting channels\n");
#endif
return -1;
}
unsigned int rate = 44100;
if(snd_pcm_hw_params_set_rate_near(handle, params, &rate, dir) < 0){
#ifdef VISMU_DEBUG
fprintf(stderr, "Error setting rate\n");
#endif
return -1;
}
snd_pcm_hw_params_set_period_size_near(handle, params, frames, dir);
int status = snd_pcm_hw_params(handle, params);
if(status < 0){
#ifdef VISMU_DEBUG
fprintf(stderr, "Unable to set hardware parameters: %s\n", snd_strerror(status));
#endif
return -1;
}
return 0;
}
int alsaListenDevice(int card, int dev)
{
char name[32];
sprintf(name, "hw:%d,%d", card, dev);
snd_pcm_t *handle;
int status = snd_pcm_open(&handle, name, SND_PCM_STREAM_CAPTURE, 0);
if(status < 0){
#ifdef VISMU_DEBUG
fprintf(stderr, "Error opening stream: %s\n", snd_strerror(status));
#endif
return -1;
}
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_malloc(¶ms);
int dir = 0;
snd_pcm_uframes_t frames = 50000;
if(alsaSetHwParams(handle, params, &dir, &frames) < 0){
snd_pcm_close(handle);
return -1;
}
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
snd_pcm_hw_params_free(params);
size_t size = frames << 1;
if(size <= 0 || size < frames){
snd_pcm_close(handle);
#ifdef VISMU_DEBUG
fprintf(stderr, "Invalid buffer size: %d\n", size);
#endif
return -1;
}
#ifdef VISMU_DEBUG
printf("Buffer size: %d\n", size);
#endif
short *buf = (short*)calloc(size, sizeof(short));
double peak = 0.0;
int i = 0;
while(i++ < 10){
int result;
while((result = snd_pcm_readi(handle, buf, frames))< 0){ // Buffer size / channels / 2
snd_pcm_prepare(handle);
}
if(result == -EPIPE){
#ifdef VISMU_DEBUG
fprintf(stderr, "Buffer overrun\n");
#endif
snd_pcm_prepare(handle);
continue;
}else if(result < 0){
#ifdef VISMU_DEBUG
fprintf(stderr, "Read error: %s\n", snd_strerror(result));
#endif
}
double val = rootMeanSquare(buf, size);
if(peak < val){
peak = val;
}
}
snd_pcm_drain(handle);
snd_pcm_close(handle);
free(buf);
return 20 * log10(peak);
}
void alsaSetDefaultInput(audiodata_t *audio, int best)
{
snd_ctl_card_info_t *info;
snd_ctl_card_info_alloca(&info);
snd_pcm_info_t *pcminfo;
snd_pcm_info_alloca(&pcminfo);
int card = -1;
if(snd_card_next(&card) < 0 || card < 0){
fprintf(stderr, "Error: no soundcard found\n");
exit(1);
}
#ifdef VISMU_DEBUG
printf("List of %s hardware devices:\n", snd_pcm_stream_name(SND_PCM_STREAM_PLAYBACK));
#endif
int highnum = 0, highcard = -1, highdev = -1;
while(card >= 0){
char name[32];
sprintf(name, "hw:%d", card);
snd_ctl_t *handle;
snd_ctl_open(&handle, name, 0);
snd_ctl_card_info(handle, info);
int dev = -1;
while(1){
snd_ctl_pcm_next_device(handle, &dev);
if(dev < 0){
break;
}
snd_pcm_info_set_device(pcminfo, dev);
snd_pcm_info_set_subdevice(pcminfo, 0);
snd_pcm_info_set_stream(pcminfo, SND_PCM_STREAM_PLAYBACK);
if(snd_ctl_pcm_info(handle, pcminfo) < 0){
continue;
}
#ifdef VISMU_DEBUG
printf("%i: %s [%s], device %i: %s [%s]\n", card, snd_ctl_card_info_get_id(info), snd_ctl_card_info_get_name(info), dev, snd_pcm_info_get_id(pcminfo), snd_pcm_info_get_name(pcminfo));
#endif
int peak = alsaListenDevice(card, dev);
if(peak <= 0 || peak < highnum){
continue;
}
highnum = peak;
highcard = card;
highdev = dev;
if(!best){
goto done;
}
}
snd_ctl_close(handle);
if(snd_card_next(&card) < 0){
exit(1);
}
}
done:
if(highnum < 0 || highcard < 0 || highdev < 0){
fprintf(stderr, "Could not find default input device!\n");
exit(1);
}
#ifdef VISMU_DEBUG
printf("Found device \"hw:%d,%d\" with a peak of %d\n", highcard, highdev, highnum);
#endif
audio->source = (char*)malloc(32);
sprintf(audio->source, "hw:%d,%d", highcard, highdev);
}
void* alsaParseInput(void *data)
{
audiodata_t *audio = (audiodata_t*)data;
snd_pcm_t *handle;
int status = snd_pcm_open(&handle, audio->source, SND_PCM_STREAM_CAPTURE, 0);
if(status < 0){
#ifdef VISMU_DEBUG
fprintf(stderr, "Error opening stream: %s\n", snd_strerror(status));
#endif
exit(1);
}
int dir = 0;
snd_pcm_uframes_t frames = 256;
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_malloc(¶ms);
if(alsaSetHwParams(handle, params, &dir, &frames) < 0){
exit(1);
}
snd_pcm_format_t format;
snd_pcm_hw_params_get_format(params, &format);
if(format < 6){
audio->format = 16;
}else if(format < 10){
audio->format = 24;
}else{
audio->format = 32;
}
snd_pcm_hw_params_get_rate(params, &audio->rate, &dir);
snd_pcm_hw_params_get_period_size(params, &frames, &dir);
unsigned int time;
snd_pcm_hw_params_get_period_time(params, &time, &dir);
snd_pcm_hw_params_free(params);
int size = frames * (audio->format >> 3) << 1;
#ifdef VISMU_DEBUG
printf("Buffer size: %d\n", size);
#endif
char *buf = (char*)calloc(size, 1);
int adjustr = audio->format >> 2;
int adjustl = audio->format >> 3;
int framecount = 0;
while(!threaddone){
int status = snd_pcm_readi(handle, buf, frames);
if(status == -EPIPE){
snd_pcm_prepare(handle);
}
pthread_mutex_lock(&mutex);
int i;
for(i = 0; i < size; i = i + (adjustl << 1)){
int right = buf[i + adjustr - 1] << 2;
int lo = buf[i + adjustr - 2] >> 6; // lo = ?
if(lo < 0){
lo = -lo + 1;
}
if(right >= 0){
right += lo;
}else{
right -= lo;
}
int left = buf[i + adjustl - 1] << 2;
lo = buf[i + adjustl - 2] >> 6; // lo = ?
if(lo < 0){
lo = -lo + 1;
}
if(left >= 0){
left += lo;
}else{
left -= lo;
}
audio->out[framecount++] = (right + left) >> 1;
if(framecount >= BUFFER_SIZE - 1){
framecount = 0;
}
}
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main(int argc, char **argv)
{
audiodata_t audio = {.rate = 0, .format = -1, .source = NULL};
int i;
for(i = 0; i < BUFFER_SIZE; i++){
audio.out[i] = 0;
}
int c, best = 0;
while((c = getopt(argc, argv, "bd:")) != -1){
switch(c){
case 'd':
audio.source = optarg;
break;
case 'b':
best = 1;
break;
}
}
if(audio.source == NULL){
#ifdef VISMU_DEBUG
printf("No valid ALSA source is supplied, finding default active input\n");
#endif
alsaSetDefaultInput(&audio, best);
}
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
threaddone = false;
pthread_t thread;
pthread_create(&thread, NULL, alsaParseInput, (void*)&audio);
double in[BUFFER_SIZE];
fftw_complex out[BUFFER_SIZE];
fftw_plan plan = fftw_plan_dft_r2c_1d(BUFFER_SIZE, in, out, FFTW_MEASURE);
ccDisplayInitialize();
ccWindowCreate((ccRect){.x = 0, .y = 0, .width = WIDTH, .height = HEIGHT}, "vismu", 0);
ccGLContextBind();
initVis(WIDTH, HEIGHT);
bool loop = true;
while(loop){
while(ccWindowEventPoll()){
ccEvent event = ccWindowEventGet();
switch(event.type){
case CC_EVENT_WINDOW_QUIT:
loop = false;
break;
default:
break;
}
}
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
int i;
for (i = 0; i < BUFFER_SIZE; i++) {
in[i] = audio.out[i];
}
pthread_mutex_unlock(&mutex);
fftw_execute(plan);
double peak = 0;
for(i = 0; i < BUFFER_SIZE >> 1; i++){
double real = sqrt(out[i][0] * out[i][0] + out[i][1] * out[i][1]);
if(real > peak){
peak = real;
}
}
renderVis(peak);
ccGLBuffersSwap();
ccTimeDelay(6);
}
fftw_destroy_plan(plan);
ccFree();
threaddone = true;
pthread_mutex_destroy(&mutex);
pthread_exit(NULL);
}