-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.c
512 lines (420 loc) · 11 KB
/
demo.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <linux/input.h>
#include <signal.h>
#include <time.h>
#include <pthread.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <math.h>
#include <jack/jack.h>
int running = 1;
typedef struct {
int fbfd;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
long int screensize;
char *fbp;
unsigned char *buf;
} framebuffer;
#define BUFSIZE 62
typedef struct {
framebuffer fb;
float vals[3];
int please_draw;
jack_port_t *out[2];
jack_client_t *client;
float lphs;
int sr;
int counter;
int subcount;
float buf[4096];
int mute;
} main_data;
void quit(int sig)
{
printf("Closing.\n");
running = 0;
}
int open_knob(int knob)
{
char path[256];
sprintf(path,
"/dev/input/by-path/platform-soc:knob%d-event",
knob);
return open(path, O_RDONLY | O_NONBLOCK);
}
int open_buttons(void)
{
return open(
"/dev/input/by-path/platform-keys-event",
O_RDONLY | O_NONBLOCK);
}
void pixel(framebuffer *fb,
int x,
int y,
unsigned char val)
{
long int loc;
if(x >= fb->vinfo.xres || y >= fb->vinfo.yres) return;
loc = x + y * fb->vinfo.xres;
fb->buf[loc] = val;
}
void copy_to_fb(framebuffer *fb)
{
long int location;
int x, y;
unsigned char val;
int w, h;
h = fb->vinfo.yres;
w = fb->vinfo.xres;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
location = (x + fb->vinfo.xoffset) *
(fb->vinfo.bits_per_pixel/8) +
(y + fb->vinfo.yoffset) * fb->finfo.line_length;
val = fb->buf[x + y * w];
fb->fbp[location] = 0x0;
fb->fbp[location + 1] = val;
}
}
}
void clearscreen(framebuffer *fb)
{
int x, y;
for (y = 0; y < fb->vinfo.yres; y++) {
for(x = 0; x < fb->vinfo.xres; x++) {
pixel(fb, x, y, 0);
}
}
}
void vslider(framebuffer *fb, int x, int y, float val)
{
float w;
float h;
int p1, p2;
int n, k;
int nlines;
w = 8;
h = 32;
for (n = 0; n < w; n++) {
pixel(fb, x + n, y, 255);
}
p1 = y + h - 1;
for (n = 0; n < w; n++) {
pixel(fb, x + n, p1, 255);
}
for (n = 0; n < h; n++) {
pixel(fb, x, y + n, 255);
}
p1 = x + w - 1;
for (n = 0; n < h; n++) {
pixel(fb, p1, y + n, 255);
}
/* draw value */
nlines = round(val * 28);
for (k = 0; k < nlines; k++) {
for (n = 0; n < (w - 4); n++) {
pixel(fb, x + 2 + n, (y + (h - k) - 3), 255);
}
}
}
void wavedisplay(framebuffer *fb, float *buf, int size)
{
float x, y;
float w, h;
int n;
float val;
int pos;
int jmp;
x = 48;
y = 24;
w = 64;
h = 32;
for (n = 0; n < w; n++) {
pixel(fb, x+n, y, 255);
}
for (n = 0; n < w; n++) {
pixel(fb, x+n, y + h - 1, 255);
}
for (n = 0; n < h; n++) {
pixel(fb, x, y + n, 255);
}
for (n = 0; n < h; n++) {
pixel(fb, x + w - 1, y + n, 255);
}
for (n = 0; n < 62; n++) {
val = buf[n];
val = 1 - ((val + 1) * 0.5);
pos = floor(val * 30);
pixel(fb, x + 1 + n, y + pos, 255);
}
}
void* draw(void *data)
{
framebuffer *fb;
main_data *m;
int draw;
m = data;
fb = &m->fb;
clearscreen(fb);
while (running) {
draw = m->please_draw;
if(draw) {
clearscreen(fb);
vslider(fb, 16, 24, m->vals[0]);
vslider(fb, 24, 24, m->vals[1]);
vslider(fb, 32, 24, m->vals[2]);
wavedisplay(fb, m->buf, 4096);
m->please_draw = 0;
copy_to_fb(fb);
}
usleep(800);
}
fprintf(stderr, "quitting\n");
pthread_exit(NULL);
}
int fb_setup(framebuffer *fb)
{
int rc;
fb->fbfd = open("/dev/fb0", O_RDWR);
if (fb->fbfd == -1) {
perror("Error: cannot open framebuffer device");
return 1;
}
rc = ioctl(fb->fbfd, FBIOGET_FSCREENINFO, &fb->finfo);
if (rc == -1) {
perror("Error reading fixed information");
return 2;
}
rc = ioctl(fb->fbfd, FBIOGET_VSCREENINFO, &fb->vinfo);
if (rc == -1) {
perror("Error reading variable information");
return 3;
}
fb->screensize = fb->vinfo.xres *
fb->vinfo.yres *
fb->vinfo.bits_per_pixel / 8;
fb->fbp = (char *)mmap(0,
fb->screensize,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fb->fbfd,
0);
fb->buf = calloc(1, fb->vinfo.xres * fb->vinfo.xres);
if ((int)fb->fbp == -1) {
return 4;
}
return 0;
}
void fb_cleanup(framebuffer *fb)
{
munmap(fb->fbp, fb->screensize);
close(fb->fbfd);
free(fb->buf);
}
static int jack_process(jack_nframes_t nframes, void *arg);
static int jack_process(jack_nframes_t nframes, void *arg)
{
jack_default_audio_sample_t *o[2];
int n;
float smp;
float frq;
main_data *m;
float gate;
jack_port_t **out;
jack_client_t *client;
m = arg;
out = m->out;
client = m->client;
o[0] = (jack_default_audio_sample_t*)jack_port_get_buffer(out[0], nframes);
o[1] = (jack_default_audio_sample_t*)jack_port_get_buffer(out[1], nframes);
gate = m->mute == 0;
for (n = 0; n < nframes; n++) {
smp = sin(m->lphs);
smp *= m->vals[0] * gate;
o[0][n] = smp;
o[1][n] = smp;
frq = 200 + 2000 * m->vals[1];
m->lphs += (float)frq * ((2.0 * M_PI) / m->sr);
m->lphs = fmod(m->lphs, 2.0 * M_PI);
if (m->subcount == 0) {
m->buf[m->counter] = smp;
}
m->counter++;
if (m->counter >= BUFSIZE) {
m->counter = 0;
if (m->subcount == 0) m->please_draw = 1;
m->subcount = (m->subcount + 1) % 8;
}
}
return 0;
}
void audio_start(main_data *m)
{
const char **ports;
const char *client_name;
const char *server_name;
jack_options_t options;
jack_status_t status;
jack_port_t **out;
jack_client_t *client;
out = m->out;
client = m->client;
if (client != NULL) {
fprintf(stderr, "JACK audio server seems to be started already\n");
return;
}
options = JackNullOption;
server_name=NULL;
client_name="Monolith"; /* set the client name */
client = jack_client_open(client_name, options, &status, server_name);
if (client == NULL) {
fprintf(stderr, "JACK has failed you.\n");
if (status & JackServerFailed) {
fprintf (stderr, "It was unable to connect to the JACK server\n");
}
return;
}
if (status & JackServerStarted) {
fprintf(stderr, "JACK server started\n");
}
if (status & JackNameNotUnique) {
client_name = jack_get_client_name(client);
fprintf(stderr, "unique name `%s' assigned\n", client_name);
}
jack_set_process_callback (client, jack_process, m);
out[0] = jack_port_register(client, "output1",
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput, 0);
out[1] = jack_port_register (client, "output2",
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput, 0);
if ((out[0] == NULL) || (out[1] == NULL)) {
fprintf(stderr, "no more JACK ports available\n");
return;
}
if (jack_activate(client)) {
fprintf(stderr, "cannot activate client\n");
return;
}
ports = jack_get_ports (client, NULL, NULL,
JackPortIsPhysical|JackPortIsInput);
if (ports == NULL) {
fprintf(stderr, "no physical playback ports\n");
return;
}
if (jack_connect(client, jack_port_name(out[0]), ports[0])) {
fprintf (stderr, "cannot connect output ports\n");
}
if (jack_connect(client, jack_port_name(out[1]), ports[1])) {
fprintf (stderr, "cannot connect output ports\n");
}
jack_free (ports);
}
void audio_stop(main_data *m)
{
if(m->client != NULL) {
jack_client_close (m->client);
m->client = NULL;
}
}
int main(int argc, char *argv[])
{
int fid[3];
int bfid;
int rc;
struct input_event evt[8];
int nevts;
int e;
int k;
pthread_t draw_thread;
main_data m;
int pd;
float tmp;
int val;
int code;
int please_poweroff;
signal(SIGINT, quit);
fb_setup(&m.fb);
for (k = 0; k < 3; k++) {
fid[k] = open_knob(k + 1);
m.vals[k] = 0;
}
bfid = open_buttons();
running = 1;
pthread_create(&draw_thread, NULL, draw, &m);
m.client = NULL;
m.lphs = 0;
m.counter = 0;
m.subcount = 0;
m.sr = 44100; /* TODO: make this dynamic */
m.mute = 0;
please_poweroff = 0;
audio_start(&m);
while (running) {
pd = 0;
for (k = 0; k < 3; k++) {
rc = read(fid[k],
evt,
sizeof(struct input_event) * 8);
if (rc != -1) {
nevts = rc / sizeof(struct input_event);
pd = 1;
for (e = 0; e < nevts; e++) {
if (evt[e].type) {
/* fprintf(stderr, "%d: %d\n", k, evt[e].value); */
tmp = m.vals[k];
tmp += evt[e].value * 0.01;
if (tmp < 0) tmp = 0;
if (tmp > 1) tmp = 1;
m.vals[k] = tmp;
}
}
}
}
rc = read(bfid, evt, sizeof(struct input_event) * 8);
if (rc != -1) {
nevts = rc / sizeof(struct input_event);
for (e = 0; e < nevts; e++) {
if (evt[e].type) {
fprintf(stdout,
"%d %d\n",
evt[e].code,
evt[e].value);
fflush(stdout);
val = evt[e].value;
code = evt[e].code;
switch (code) {
case 1:
quit(0);
please_poweroff = 1;
break;
case 2:
m.mute = val == 1;
break;
case 3:
break;
}
}
}
}
m.please_draw = pd;
usleep(800);
}
audio_stop(&m);
pthread_join(draw_thread, NULL);
for (k = 0; k < 3; k++) close(fid[k]);
close(bfid);
fb_cleanup(&m.fb);
if (please_poweroff) {
system("poweroff");
}
return 0;
}