-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdingle_dots.cc
407 lines (371 loc) · 12.2 KB
/
dingle_dots.cc
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
#include <jack/jack.h>
#include "dingle_dots.h"
#include "v4l2_wayland.h"
#include "v4l2.h"
#include "midi.h"
#include "dingle_dots.h"
DingleDots::DingleDots() { }
int DingleDots::init(int width, int height) {
int ret;
this->s_pressed = 0;
this->smdown = 0;
this->drawing_rect.width = width;
this->drawing_rect.height = height;
this->use_rand_color_for_text = 0;
this->animating = 0;
this->nports = 2;
this->make_new_tld = 0;
this->analysis_rect.width = 260;
this->analysis_rect.height = 148;
this->ascale_factor_x = this->drawing_rect.width / (double)this->analysis_rect.width;
this->ascale_factor_y = ((double)this->drawing_rect.height) / this->analysis_rect.height;
this->analysis_frame = av_frame_alloc();
this->analysis_frame->format = AV_PIX_FMT_ARGB;
this->analysis_frame->width = this->analysis_rect.width;
this->analysis_frame->height = this->analysis_rect.height;
ret = av_image_alloc(this->analysis_frame->data, this->analysis_frame->linesize,
this->analysis_frame->width, this->analysis_frame->height, (AVPixelFormat)this->analysis_frame->format, 1);
if (ret < 0) {
fprintf(stderr, "Could not allocate raw picture buffer\n");
exit(1);
}
this->analysis_resize = sws_getContext(this->drawing_rect.width, this->drawing_rect.height, AV_PIX_FMT_ARGB, this->analysis_rect.width,
this->analysis_rect.height, AV_PIX_FMT_ARGB, SWS_BICUBIC, NULL, NULL, NULL);
this->doing_tld = 0;
this->doing_motion = 0;
this->do_snapshot = 0;
this->show_shapshot_shape = 0;
this->use_window_x11 = 0;
this->mdown = 0;
this->dragging = 0;
this->selection_in_progress = 0;
this->motion_threshold = 0.001;
for (int i = 0; i < MAX_NUM_SOUND_SHAPES; ++i) {
this->sound_shapes[i].clear_state();
}
this->setup_jack();
int sr = jack_get_sample_rate(this->client);
int bufsize = jack_get_buffer_size(this->client);
float w5 = width / 5;
ca_context_create(&this->event_sound_ctx);
color c1;
color c2;
color_init(&c1, 0.0, 0.2, 0.4, 1.0);
color_init(&c2, 0.0, 0.2, 0.1, 1.0);
this->meters[0].init(this, sr, bufsize, 0.1f, 20.0f, width/2 - 2*w5, height/2, w5, c1);
this->meters[1].init(this, sr, bufsize, 0.1f, 20.0f, width/2 + 2*w5, height/2, w5, c2);
snapshot_shape.init("SNAPSHOT", this->drawing_rect.width / 2., this->drawing_rect.width / 16.,
this->drawing_rect.width / 32., random_color(), this);
pthread_mutex_init(&this->snapshot_thread_info.lock, NULL);
pthread_cond_init(&this->snapshot_thread_info.data_ready, NULL);
uint32_t rb_size = 200 * 4 * 640 * 360;
this->snapshot_thread_info.ring_buf = jack_ringbuffer_create(rb_size);
memset(this->snapshot_thread_info.ring_buf->buf, 0,
this->snapshot_thread_info.ring_buf->size);
pthread_create(&this->snapshot_thread_info.thread_id, NULL, snapshot_disk_thread,
this);
return 0;
}
int DingleDots::deactivate_sound_shapes() {
for (int i = 0; i < MAX_NUM_SOUND_SHAPES; i++) {
if (this->sound_shapes[i].active) {
this->sound_shapes[i].deactivate();
}
}
return 0;
}
void jack_shutdown (void *) {
printf("JACK shutdown\n");
abort();
}
double hanning_window(int i, int N) {
return 0.5 * (1 - cos(2 * M_PI * i / (N - 1)));
}
int process(jack_nframes_t nframes, void *arg) {
static int first_call = 1;
DingleDots *dd = (DingleDots *)arg;
if (first_call) {
first_call = 0;
int rc = pthread_setname_np(pthread_self(), "vw_audio");
if (rc != 0) {
errno = rc;
perror("pthread_setname_np");
}
}
if (!dd->can_process) return 0;
midi_process_output(nframes, dd);
for (int chn = 0; chn < dd->nports; chn++) {
dd->in[chn] = (jack_default_audio_sample_t *)jack_port_get_buffer(dd->in_ports[chn], nframes);
dd->out[chn] = (jack_default_audio_sample_t *)jack_port_get_buffer(dd->out_ports[chn], nframes);
}
if (nframes >= FFT_SIZE) {
for (int i = 0; i < FFT_SIZE; i++) {
dd->fftw_in[i][0] = dd->in[0][i] * hanning_window(i, FFT_SIZE);
dd->fftw_in[i][1] = 0.0;
}
fftw_execute(dd->p);
}
for (uint i = 0; i < nframes; i++) {
for (int chn = 0; chn < dd->nports; chn++) {
dd->out[chn][i] = dd->in[chn][i];
}
}
for (int i = 0; i < MAX_NUM_VIDEO_FILES; ++i) {
VideoFile *vf = &dd->vf[i];
if (vf->active && vf->audio_playing && !vf->paused) {
jack_default_audio_sample_t sample;
if (vf->audio_decoding_started) {
if (vf->audio_decoding_finished &&
jack_ringbuffer_read_space(vf->abuf) == 0) {
vf->audio_playing = 0;
if (pthread_mutex_trylock(&vf->video_lock) == 0) {
pthread_cond_signal(&vf->video_data_ready);
pthread_mutex_unlock(&vf->video_lock);
}
} else{
for (uint frm = 0; frm < nframes; ++frm) {
for (int chn = 0; chn < 2; ++chn) {
if (jack_ringbuffer_read_space(vf->abuf) >= sizeof(sample)) {
jack_ringbuffer_read(vf->abuf, (char *)&sample, sizeof(sample));
dd->out[chn][frm] += sample;
}
}
}
vf->nb_frames_played += nframes;
if (pthread_mutex_trylock(&vf->audio_lock) == 0) {
pthread_cond_signal(&vf->audio_data_ready);
pthread_mutex_unlock(&vf->audio_lock);
}
}
}
}
}
for (int chn = 0; chn < 2; chn++) {
if (dd->meters[chn].active) dd->meters[chn].process(dd->out[chn], nframes);
}
uint32_t sample_size = sizeof(jack_default_audio_sample_t);
if (dd->vfo[0].get_recording_started() && !dd->vfo[0].get_audio_done() &&
dd->vfo[0].get_recording_audio()) {
if (dd->vfo[0].get_audio_first_call()) {
struct timespec *ats = dd->vfo[0].get_audio_first_time();
clock_gettime(CLOCK_MONOTONIC, ats);
dd->vfo[0].set_audio_samples_count(0);
dd->vfo[0].set_audio_first_call(0);
}
jack_ringbuffer_t *rb = dd->vfo[0].get_audio_ringbuffer();
for (uint i = 0; i < nframes; i++) {
for (int chn = 0; chn < dd->nports; chn++) {
if (jack_ringbuffer_write(rb, (const char *) (dd->out[chn]+i),
sample_size) < sample_size) {
printf("jack overrun: %ld\n", ++dd->jack_overruns);
}
}
}
dd->vfo[0].wake_up_audio_write_thread();
}
dd->vfo[0].wake_up_audio_write_thread();
return 0;
}
int DingleDots::setup_jack()
{
size_t in_size;
this->can_process = 0;
this->jack_overruns = 0;
if ((this->client = jack_client_open("v4l2_wayland",
JackNoStartServer, NULL)) == 0) {
printf("jack server not running?\n");
exit(1);
}
jack_set_process_callback(this->client, process, this);
jack_on_shutdown(this->client, jack_shutdown, NULL);
if (jack_activate(this->client)) {
printf("cannot activate jack client\n");
}
this->nports = 2;
this->in_ports = (jack_port_t **) malloc(sizeof(jack_port_t *) * this->nports);
this->out_ports = (jack_port_t **) malloc(sizeof(jack_port_t *) * this->nports);
in_size = this->nports * sizeof (jack_default_audio_sample_t *);
this->in = (jack_default_audio_sample_t **) malloc (in_size);
this->out = (jack_default_audio_sample_t **) malloc (in_size);
memset(this->in, 0, in_size);
memset(this->out, 0, in_size);
this->midi_ring_buf = jack_ringbuffer_create(MIDI_RB_SIZE);
fftw_in = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * FFT_SIZE);
fftw_out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * FFT_SIZE);
p = fftw_plan_dft_1d(FFT_SIZE, fftw_in, fftw_out, FFTW_FORWARD, FFTW_ESTIMATE);
for (int i = 0; i < this->nports; i++) {
char name[64];
sprintf(name, "input%d", i + 1);
if ((this->in_ports[i] = jack_port_register (this->client, name, JACK_DEFAULT_AUDIO_TYPE,
JackPortIsInput, 0)) == 0) {
printf("cannot register input port \"%s\"!\n", name);
jack_client_close(this->client);
exit(1);
}
sprintf(name, "output%d", i + 1);
if ((this->out_ports[i] = jack_port_register (this->client, name, JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput, 0)) == 0) {
printf("cannot register output port \"%s\"!\n", name);
jack_client_close(this->client);
exit(1);
}
}
this->midi_port = jack_port_register(this->client, "output_midi",
JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, 0);
this->can_process = 1;
return 0;
}
int DingleDots::free() {
if (this->analysis_resize) {
sws_freeContext(this->analysis_resize);
}
if (this->analysis_frame) {
av_freep(&this->analysis_frame->data[0]);
av_frame_free(&this->analysis_frame);
}
if (this->screen_frame) {
av_freep(&this->screen_frame->data[0]);
av_frame_free(&this->screen_frame);
}
return 0;
}
color DingleDots::random_color()
{
struct hsva h;
h.h = (double) rand() / RAND_MAX;
h.v = 0.45;
h.s = 1.0;
h.a = 0.5;
return hsv2rgb(&h);
}
vwColor DingleDots::random_vw_color() {
vwColor c;;
double h = (double) rand() / RAND_MAX;
c.set_hsva(h, 1.0, 0.45, 0.5);
return c;
}
void DingleDots::add_scale(midi_key_t *key, int midi_channel,
color *c) {
int i;
double x_delta;
double r =this->drawing_rect.width/32.;
x_delta = 1. / (key->num_steps + 1);
double y = r + (1.0 * rand()) / RAND_MAX * (this->drawing_rect.height - 2*r);
for (i = 0; i < key->num_steps; i++) {
char key_name[NCHAR];
char base_name[NCHAR];
const char *scale;
midi_note_to_octave_name(key->base_note, base_name);
scale = midi_scale_id_to_text(key->scaleid);
sprintf(key_name, "%s %s", base_name, scale);
this->add_note(key_name, i + 1,
key->base_note + key->steps[i], midi_channel,
x_delta * (i + 1) * this->drawing_rect.width, y,
this->drawing_rect.width/32, c);
}
}
void DingleDots::render_selection_box(cairo_t *cr) {
cairo_save(cr);
cairo_rectangle(cr, floor(this->selection_rect.x)+0.5, floor(this->selection_rect.y)+0.5,
floor(this->selection_rect.width), floor(this->selection_rect.height));
cairo_set_source_rgba(cr, 0.1, 0.5, 0.5, 0.2 * this->selection_box_alpha);
cairo_fill_preserve(cr);
cairo_set_source_rgba(cr, 0.1, 0.5, 0.5, 1 * this->selection_box_alpha);
cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
cairo_set_line_width(cr, 11);
cairo_stroke(cr);
cairo_restore(cr);
}
void DingleDots::set_selecting_on() {
this->selection_in_progress = 1;
this->selection_box_alpha = 1.0;
this->easers.clear();
gtk_widget_queue_draw(this->drawing_area);
}
void DingleDots::set_selecting_off() {
this->selection_in_progress = 0;
gtk_widget_queue_draw(this->drawing_area);
}
void DingleDots::queue_draw()
{
gtk_widget_queue_draw(this->drawing_area);
}
double DingleDots::get_selection_box_alpha() const
{
return selection_box_alpha;
}
void DingleDots::set_selection_box_alpha(double value)
{
selection_box_alpha = value;
gtk_widget_queue_draw(this->drawing_area);
}
uint8_t DingleDots::get_animating() const {
return animating;
}
void DingleDots::set_animating(const uint8_t &value) {
animating = value;
}
int DingleDots::add_note(char *scale_name,
int scale_num, int midi_note, int midi_channel, double x, double y, double r, color *c) {
char label[NCHAR], snum[NCHAR], octave_name[NCHAR];
int i;
double freq;
memset(label, '\0', NCHAR * sizeof(char));
memset(snum, '\0', NCHAR * sizeof(char));
memset(octave_name, '\0', NCHAR * sizeof(char));
freq = midi_to_freq(midi_note);
if (scale_num > 0) {
sprintf(snum, "%s", scale_name);
}
midi_note_to_octave_name(midi_note, octave_name);
sprintf(label, "%.2f\n%s\n%d\n%s", freq, snum, scale_num, octave_name);
for (i = 0; i < MAX_NUM_SOUND_SHAPES; i++) {
SoundShape *s = &this->sound_shapes[i];
if (s->active) continue;
s->init(label, midi_note, midi_channel,
x, y, r, c, this);
s->activate();
return 0;
}
return -1;
}
void DingleDots::get_sound_shapes(std::vector<vwDrawable *> &sound_shapes)
{
for (int i = 0; i < MAX_NUM_SOUND_SHAPES; ++i) {
SoundShape *ss = &this->sound_shapes[i];
if (ss->active) sound_shapes.push_back(ss);
}
for (int i = 0; i < 2; ++i) {
Meter *m = &this->meters[i];
if (m->active) sound_shapes.push_back(m);
}
sound_shapes.push_back(&this->snapshot_shape);
}
void DingleDots::get_sources(std::vector<vwDrawable *> &list)
{
for (int i = 0; i < MAX_NUM_X11; ++i) {
if (this->x11[i].active) {
list.push_back(&this->x11[i]);
}
}
for (int i = 0; i < MAX_NUM_TEXTS; ++i) {
if (this->text[i].active) {
list.push_back(&this->text[i]);
}
}
for (int i = 0; i < MAX_NUM_V4L2; i++) {
if (this->v4l2[i].active) {
list.push_back(&this->v4l2[i]);
}
}
for (int j = 0; j < MAX_NUM_VIDEO_FILES; j++) {
if (this->vf[j].active) {
list.push_back(&this->vf[j]);
}
}
for (int i = 0; i < MAX_NUM_SPRITES; i++) {
if (this->sprites[i].active) {
list.push_back(&this->sprites[i]);
}
}
}