-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goniometer.c
366 lines (304 loc) · 11.3 KB
/
goniometer.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <stdbool.h>
#include <ctype.h>
#include <dirent.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "config.h"
#include "debug.h"
#include "util.h"
#include "alsa.h"
#include "common.h"
#ifndef __WIN32__
#include "fifo.h"
#endif
#include "portaudio.h"
#include "pulse.h"
#include "sndio.h"
#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif
#ifdef __GNUC__
#undef GCC_UNUSED
#define GCC_UNUSED __attribute__((unused))
#else
#define GCC_UNUSED /* nothing */
#endif
int screen_width = 256;
int screen_height = 256;
SDL_Window * window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Rect cur_bounds;
#ifdef HAVE_ALSA
static bool is_loop_device_for_sure(const char *text) {
const char *const LOOPBACK_DEVICE_PREFIX = "hw:Loopback,";
return strncmp(text, LOOPBACK_DEVICE_PREFIX, strlen(LOOPBACK_DEVICE_PREFIX)) == 0;
}
static bool directory_exists(const char *path) {
DIR *const dir = opendir(path);
if (dir == NULL)
return false;
closedir(dir);
return true;
}
#endif
#include "icons/goniometer.cdata"
void SDL_SetWindowIconMemory(SDL_Window *window) {
SDL_RWops * z = SDL_RWFromMem(goniometer_png, sizeof(goniometer_png));
SDL_Surface *window_icon_surface = IMG_Load_RW(z, 1);
if (window_icon_surface == NULL) {
fprintf(stderr, "Error: unable to load application icon: '%s'.\n", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_SetWindowIcon(window, window_icon_surface);
SDL_FreeSurface(window_icon_surface);
}
int SDL_RenderFillCircle(SDL_Renderer * renderer, int x, int y, int radius) {
int offsetx, offsety, d;
int status;
offsetx = 0;
offsety = radius;
d = radius -1;
status = 0;
while (offsety >= offsetx) {
status += SDL_RenderDrawLine(renderer, x - offsety, y + offsetx,
x + offsety, y + offsetx);
status += SDL_RenderDrawLine(renderer, x - offsetx, y + offsety,
x + offsetx, y + offsety);
status += SDL_RenderDrawLine(renderer, x - offsetx, y - offsety,
x + offsetx, y - offsety);
status += SDL_RenderDrawLine(renderer, x - offsety, y - offsetx,
x + offsety, y - offsetx);
if (status < 0) {
status = -1;
break;
}
if (d >= 2*offsetx) {
d -= 2*offsetx + 1;
offsetx +=1;
}
else if (d < 2 * (radius - offsety)) {
d += 2 * offsety - 1;
offsety -= 1;
}
else {
d += 2 * (offsety - offsetx - 1);
offsety -= 1;
offsetx += 1;
}
}
return status;
}
void execute(double *in, unsigned int new_samples, struct audio_data audio, int frame_time) {
debug("new_samples = % 8d\n", new_samples);
// do not overflow
if (new_samples > (unsigned int)audio.input_buffer_size) {
new_samples = (unsigned int)audio.input_buffer_size;
}
SDL_Rect cur_bounds;
SDL_GetWindowPosition(window, &cur_bounds.x, &cur_bounds.y);
SDL_GetWindowSize(window, &cur_bounds.w, &cur_bounds.h);
screen_width = cur_bounds.w;
screen_height = cur_bounds.h;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 64+32);
SDL_Rect rect = { 0, 0, screen_width, screen_height };
SDL_RenderFillRect(renderer, &rect);
if (new_samples > 0) {
for (unsigned int i = 0; i < new_samples - 1; i++) {
#define DX (screen_width / 2)
#define DY (screen_height / 2)
float scale = 0.004; //XXX
float x = in[i * 2 ] * scale;
float y = in[i * 2 + 1] * scale;
double radius = sqrt((x * x) + (y * y));
double angle = atan(y/x);
if ((x < 0 && y > 0) || (x < 0 && y < 0)) {
angle += 3.14159265; // Pi radians = 180 degrees
} else if (x > 0 && y < 0) {
angle += 6.28318530; // 2Pi radians = 360 degrees
}
// atan() will return zero if either of our coordinates is zero.
// Correct for this by manually setting the angle.
if (x == 0) {
angle = y > 0 ? 1.57079633 : 4.71238898; // 90 or 270 degrees
} else if (y == 0) {
angle = x > 0 ? 0 : 3.14159265; // 0 or 180 degrees
}
// Rotate coordinate by 45 degrees counter clockwise
angle += 0.78539816;
// Convert polar coordinate back to cartesian coordinate.
double xRotated = DX + radius * cos(angle);
double yRotated = DY + radius * sin(angle);
SDL_SetRenderDrawColor(renderer, 255, 255, 0, SDL_ALPHA_OPAQUE);
//SDL_RenderDrawLine(renderer, pnx1, pny1, pnx2, pny2);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 16);
SDL_RenderFillCircle(renderer, xRotated, yRotated, 2);
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 32);
SDL_Rect rect3 = { xRotated, yRotated, 1, 1 };
SDL_RenderFillRect(renderer, &rect3);
}
}
SDL_RenderPresent(renderer);
SDL_Delay(frame_time);
}
SDL_Rect toggle_fake_fullscreen(SDL_Rect old_bounds) {
if (SDL_GetWindowFlags(window) & SDL_WINDOW_BORDERLESS) {
SDL_SetWindowBordered(window, SDL_TRUE);
SDL_SetWindowSize(window, old_bounds.w, old_bounds.h);
SDL_SetWindowPosition(window, old_bounds.x, old_bounds.y);
return old_bounds;
} else {
SDL_Rect cur_bounds;
SDL_GetWindowPosition(window, &cur_bounds.x, &cur_bounds.y);
SDL_GetWindowSize(window, &cur_bounds.w, &cur_bounds.h);
int idx = SDL_GetWindowDisplayIndex(window);
SDL_Rect bounds;
SDL_GetDisplayBounds(idx, &bounds);
SDL_SetWindowBordered(window, SDL_FALSE);
//SDL_SetWindowPosition(window, bounds.x, bounds.y);
SDL_SetWindowSize(window, bounds.w, bounds.h);
return cur_bounds;
}
}
int main(int argc, char** argv) {
(void)argc;
(void)argv;
debug("1\n");
struct audio_data audio;
memset(&audio, 0, sizeof(audio));
// TODO make this a parameter
//int input = INPUT_PULSE;
//int input = INPUT_ALSA;
int input = INPUT_PORTAUDIO;
char* audio_source = "list"; // pulse
//char* audio_source = "hw:Loopback,1"; // alsa
audio.source = malloc(1 + strlen(audio_source));
strcpy(audio.source, audio_source);
audio.format = -1;
audio.rate = 0;
audio.samples_counter = 0;
audio.channels = 2;
audio.input_buffer_size = BUFFER_SIZE * audio.channels;
audio.buffer_size = audio.input_buffer_size * 8;
audio.in = (double *)malloc(audio.buffer_size * sizeof(double));
memset(audio.in, 0, sizeof(int) * audio.buffer_size);
audio.terminate = 0;
debug("starting audio thread\n");
pthread_t p_thread;
int timeout_counter = 0;
struct timespec timeout_timer = {.tv_sec = 0, .tv_nsec = 1000000};
int thr_id GCC_UNUSED;
switch (input) {
#ifdef HAVE_ALSA
case INPUT_ALSA:
// input_alsa: wait for the input to be ready
if (is_loop_device_for_sure(audio.source)) {
if (directory_exists("/sys/")) {
if (!directory_exists("/sys/module/snd_aloop/")) {
//XXXcleanup();
fprintf(stderr,
"Linux kernel module \"snd_aloop\" does not seem to be loaded.\n"
"Maybe run \"sudo modprobe snd_aloop\".\n");
exit(EXIT_FAILURE);
}
}
}
thr_id = pthread_create(&p_thread, NULL, input_alsa,
(void *)&audio); // starting alsamusic listener
timeout_counter = 0;
while (audio.format == -1 || audio.rate == 0) {
nanosleep(&timeout_timer, NULL);
timeout_counter++;
if (timeout_counter > 2000) {
//XXXcleanup();
fprintf(stderr, "could not get rate and/or format, problems with audio thread? "
"quiting...\n");
exit(EXIT_FAILURE);
}
}
debug("got format: %d and rate %d\n", audio.format, audio.rate);
break;
#endif
#ifndef __WIN32__
case INPUT_FIFO:
// starting fifomusic listener
thr_id = pthread_create(&p_thread, NULL, input_fifo, (void *)&audio);
audio.rate = 44100;
audio.format = 16;
break;
#endif
#ifdef HAVE_PULSE
case INPUT_PULSE:
if (strcmp(audio.source, "auto") == 0) {
getPulseDefaultSink((void *)&audio);
}
// starting pulsemusic listener
thr_id = pthread_create(&p_thread, NULL, input_pulse, (void *)&audio);
audio.rate = 44100;
break;
#endif
#ifdef HAVE_SNDIO
case INPUT_SNDIO:
thr_id = pthread_create(&p_thread, NULL, input_sndio, (void *)&audio);
audio.rate = 44100;
break;
#endif
#ifdef HAVE_PORTAUDIO
case INPUT_PORTAUDIO:
thr_id = pthread_create(&p_thread, NULL, input_portaudio, (void *)&audio);
audio.rate = 44100;
break;
#endif
default:
exit(EXIT_FAILURE); // Can't happen.
}
debug("audio format: %d rate: %d channels: %d\n", audio.format, audio.rate, audio.channels);
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
debug("SDL_Init failed: %s\n", SDL_GetError());
exit(1);
}
window = SDL_CreateWindow("goniometer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height, SDL_WINDOW_SHOWN);
//SDL_SetWindowResizable(window, true);
SDL_SetWindowAlwaysOnTop(window, true);
SDL_SetWindowIconMemory(window);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
float framerate = 60.0;
int frame_time_msec = (1 / (float)framerate) * 1000;
bool quit = false;
while (!quit) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT || (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)) {
quit = true;
}
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_F11) {
cur_bounds = toggle_fake_fullscreen(cur_bounds);
}
}
// process: execute
pthread_mutex_lock(&audio.lock);
execute(audio.in, audio.samples_counter, audio, frame_time_msec);
if (audio.samples_counter > 0) {
audio.samples_counter = 0;
}
pthread_mutex_unlock(&audio.lock);
// checking if audio thread has exited unexpectedly
if (audio.terminate == 1) {
//cleanup();
fprintf(stderr, "Audio thread exited unexpectedly. %s\n", audio.error_message);
exit(EXIT_FAILURE);
}
}
audio.terminate = 1;
pthread_join(p_thread, NULL);
free(audio.source);
free(audio.in);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}