-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignalViewUI.cpp
More file actions
718 lines (617 loc) · 18.4 KB
/
SignalViewUI.cpp
File metadata and controls
718 lines (617 loc) · 18.4 KB
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
SignalView LV2 analysis plugin
Copyright (C) 2025 Timothy William Krause
mailto:tmkrs4482@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "SignalViewUI.h"
SignalViewUI::SignalViewUI(
const LV2UI_Descriptor *descriptor,
const char *plugin_uri,
const char *bundle_path,
LV2UI_Write_Function write_function,
LV2UI_Controller controller,
LV2UI_Widget *widget,
const LV2_Feature *const *features)
:
write(write_function),
controller(controller),
bundle_path(bundle_path)
{
parentXWindow = nullptr;
map = nullptr;
logger_log = nullptr;
const char* missing = lv2_features_query(
features,
LV2_URID__map, &map, true,
LV2_UI__parent, &parentXWindow, true,
LV2_LOG__log, &logger_log, false,
NULL);
if (missing) {
printf("Missing feature <%s>\n", missing);
throw std::exception();
}
uris.reset(new SignalViewURIs(map));
lv2_atom_forge_init(&forge, map);
lv2_log_logger_init(&logger, map, logger_log);
lv2_log_note(&logger, "SignalViewUI::SignalViewUI logger initialized.\n");
state_valid = false;
quit = false;
rate = 48000.0f;
dB_min = -180.0f;
dB_max = 0.0f;
linFreq = rate/2.0f;
log = false;
mousing = false;
time_last = std::chrono::steady_clock::now();
std::function<void()> deferred_task = std::bind(ui_thread_func, this);
try {
ui_thread = std::thread(deferred_task);
}
catch(const std::system_error& e){
lv2_log_error(&logger, "SignalViewUI::SignalViewUI Couldn't start thread.\n");
throw;
}
send_ui_send_state();
}
SignalViewUI::~SignalViewUI()
{
quit = true;
ui_thread.join();
}
void ui_thread_func(SignalViewUI* ui)
{
// Initialize the pugl Window and View
ui->setupPugl();
// Enter the event loop
ui->eventLoop();
}
void SignalViewUI::eventLoop(void)
{
if(!view_ready){
return;
}
// wait for the state
state_sem.wait();
timeout = 1.0 / frame_rate;
// enter the event loop
while(!quit)
{
puglUpdate(world, timeout);
}
//printf("SignalViewUI::eventLoop puglFreeView\n");
puglFreeView(view);
puglFreeWorld(world);
}
void SignalViewUI::setupPugl(void)
{
world = puglNewWorld(PUGL_MODULE, 0);
if(!world){
lv2_log_error(&logger, "SignalViewUI::setupPugl puglNewWorld failed.\n");
view_ready = false;
view_sem.post();
return;
}
view = puglNewView(world);
if(!view){
lv2_log_error(&logger, "SignalViewUI::setupPugl puglNewView failed.\n");
puglFreeWorld(world);
view_ready = false;
view_sem.post();
return;
}
puglSetWorldString(world, PUGL_CLASS_NAME, "SignalView");
puglSetViewString(view, PUGL_WINDOW_TITLE, "SignalView");
puglSetSizeHint(view, PUGL_DEFAULT_SIZE, 400, 400);
puglSetSizeHint(view, PUGL_MIN_SIZE, 200, 200);
puglSetSizeHint(view, PUGL_MAX_SIZE, 1920, 1080);
puglSetSizeHint(view, PUGL_MIN_ASPECT, 1, 1);
puglSetSizeHint(view, PUGL_MAX_ASPECT, 16, 9);
puglSetBackend(view, puglGlBackend());
puglSetViewHint(view, PUGL_CONTEXT_API, PUGL_OPENGL_API);
puglSetViewHint(view, PUGL_CONTEXT_VERSION_MAJOR, 4);
puglSetViewHint(view, PUGL_CONTEXT_VERSION_MINOR, 6);
puglSetViewHint(view, PUGL_CONTEXT_PROFILE, PUGL_OPENGL_CORE_PROFILE);
puglSetViewHint(view, PUGL_DOUBLE_BUFFER, PUGL_TRUE);
puglSetViewHint(view, PUGL_RESIZABLE, PUGL_TRUE);
puglSetViewHint(view, PUGL_SWAP_INTERVAL, PUGL_TRUE);
puglSetHandle(view, this);
puglSetParent(view, (PuglNativeView)parentXWindow);
puglSetEventFunc(view, ::onEvent);
const PuglStatus st = puglRealize(view);
if (st) {
lv2_log_error(
&logger,
"SignalViewUI::setupPugl puglRealize failed:%s\n",
puglStrerror(st));
puglFreeView(view);
puglFreeWorld(world);
view_ready = false;
view_sem.post();
}else{
lv2_log_note(&logger, "SignalViewUI::setupPugl view realized.\n");
view_ready = true;
// Show window
puglShow(view, PUGL_SHOW_RAISE);
// Signal to the host that the view is ready
view_sem.post();
}
}
void SignalViewUI::setSpectrum(void)
{
if(spectrum){
spectrum->SetdBLimits(dB_min, dB_max);
if(log){
spectrum->SetWidth(rate/2.0);
} else {
spectrum->SetWidth(linFreq);
}
spectrum->SetFrequency(log);
}
}
PuglStatus SignalViewUI::setupGL(void)
{
// load glad
if(!gladLoadGL((GLADloadfunc)&puglGetProcAddress)){
lv2_log_error(&logger, "SignalViewUI::setupGL gladLoadGL failed.\n");
return PUGL_REALIZE_FAILED;
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
frame_rate = puglGetViewHint(view, PUGL_REFRESH_RATE);
lv2_log_note(&logger, "SignalViewUI frame_rate=%f\n", frame_rate);
// Create a new SignalViewGL
try {
spectrum.reset(
new Spectrum(
(int)(rate / 10.0f),
rate,
frame_rate,
3,
bundle_path));
}
catch(const std::bad_alloc& e){
lv2_log_error(&logger, "SignalViewUI::setupGL Spectrum memory allocation error\n");
return PUGL_REALIZE_FAILED;
}
catch(...){
lv2_log_error(&logger, "SignalViewUI::setupGL Error initializing Spectrum.");
return PUGL_REALIZE_FAILED;
}
if(spectrum) {
try {
spectrum->GLInit();
}
catch(const std::bad_alloc& e) {
lv2_log_error(&logger, "SignalViewUI::setupGL Spectrum GLInit memory allocation error.\n");
spectrum.reset(nullptr);
return PUGL_REALIZE_FAILED;
}
catch(...){
lv2_log_error(&logger, "SignalViewUI::setupGL Error initializing Spectrum GL.");
spectrum.reset(nullptr);
return PUGL_REALIZE_FAILED;
}
setSpectrum();
}
// enable data from the plugin
if(spectrum) send_ui_enable();
return PUGL_SUCCESS;
}
void SignalViewUI::teardownGL(void)
{
if(spectrum){
// disable data form the plugin
send_ui_disable();
// Destroy all of the GL elements
spectrum->GLDestroy();
}
// destroy the Spectrum
spectrum.reset(nullptr);
}
void SignalViewUI::onConfigure(int width, int height)
{
glViewport(0, 0, width, height);
SignalViewUI::width = width;
SignalViewUI::height = height;
h1 = height/3;
h2 = height*2/3;
}
void SignalViewUI::onExpose(void)
{
// std::chrono::time_point<std::chrono::steady_clock>
// time_now = std::chrono::steady_clock::now();
// std::chrono::duration<double> diff = time_now - time_last;
// std::cout << "frame interval(%):" << diff.count()*frame_rate*100.0 << std::endl;
// time_last = time_now;
glViewport(0, 0, width, height);
// draw the SignalViewGL
// printf("SignalViewUI::onExpose\n");
if(spectrum) spectrum->Render();
}
void SignalViewUI::onScroll(int y, int dy)
{
if(y>=h1 && y<=h2){
float delta = dy * 2;
float alpha = (float)(y-h1)/(float)(h2-h1);
float d_dB_min = delta*(1.0f - alpha);
float d_dB_max = -delta*alpha;
dB_min += d_dB_min;
dB_max += d_dB_max;
if(dB_min < -180.0f) dB_min = -180.0f;
if(dB_min > 0.0f) dB_min = 0.0f;
if(dB_max < -180.0f) dB_max = -180.0f;
if(dB_max > 0.0f) dB_max = 0.0f;
if(dB_max < dB_min){
float t = dB_min;
dB_min = dB_max;
dB_max = t;
}
if(spectrum) spectrum->SetdBLimits(dB_min, dB_max);
send_ui_state();
}
}
void SignalViewUI::onButtonPress(const PuglButtonEvent* e)
{
int button = e->button;
if(button==BUTTON_LOG){
log = !log;
if(spectrum) spectrum->SetFrequency(log);
if(log){
if(spectrum) spectrum->SetWidth(rate/2.0);
}else{
if(spectrum) spectrum->SetWidth(linFreq);
}
send_ui_state();
}else if(button==BUTTON_MOTION){
mousing = true;
x_last = e->x;
y_last = e->y;
}
}
void SignalViewUI::onButtonRelease(const PuglButtonEvent* e)
{
int button = e->button;
if(button==BUTTON_MOTION){
mousing = false;
}
}
void SignalViewUI::onMotion(const PuglMotionEvent* e)
{
if(mousing && !log){
float dx = e->x - x_last;
linFreq -= dx * 50.0f;
if(linFreq<1000.0f)linFreq = 1000.0f;
float nyquist = rate/2.0;
if(linFreq>nyquist)linFreq = nyquist;
if(spectrum) spectrum->SetWidth(linFreq);
send_ui_state();
x_last = e->x;
y_last = e->y;
}
}
void SignalViewUI::port_event(
uint32_t port_index,
uint32_t buffer_size,
uint32_t format,
const void *buffer)
{
if(format == uris->atom_eventTransfer){
const LV2_Atom* atom = (const LV2_Atom*)buffer;
if(lv2_atom_forge_is_object_type(&forge, atom->type)){
const LV2_Atom_Object* obj = (const LV2_Atom_Object*)atom;
if(obj->body.otype == uris->RawAudio){
recv_raw_audio(obj);
}else if(obj->body.otype == uris->ui_State){
recv_ui_state(obj);
}
}
}
}
PuglStatus onEvent(PuglView* view, const PuglEvent* event)
{
SignalViewUI* sv = (SignalViewUI*)puglGetHandle(view);
if(sv){
return sv->onEvent(view, event);
}
return PUGL_SUCCESS;
}
PuglStatus SignalViewUI::onEvent(PuglView* view, const PuglEvent* event)
{
if(view != SignalViewUI::view) return PUGL_SUCCESS;
switch (event->type)
{
case PUGL_REALIZE:
//printf("PUGL_REALIZE\n");
return setupGL();
break;
case PUGL_UNREALIZE:
//printf("PUGLE_UNREALIZE\n");
teardownGL();
break;
case PUGL_CONFIGURE:
//printf("PUGL_CONFIGURE w:%d h:%d\n",
// event->configure.width,
// event->configure.height);
onConfigure(event->configure.width, event->configure.height);
break;
case PUGL_UPDATE:
//printf("PUGL_UPDATE\n");
puglObscureView(view);
break;
case PUGL_EXPOSE:
//printf("PUGL_EXPOSE\n");
onExpose();
break;
case PUGL_CLOSE:
//printf("PUGL_CLOSE\n");
quit = true;
break;
case PUGL_KEY_PRESS:
//printf("PUGL_KEY_PRESS\n");
if (event->key.key == 'q' || event->key.key == PUGL_KEY_ESCAPE)
{
quit = true;
}
break;
case PUGL_SCROLL:
onScroll(event->scroll.y, event->scroll.dy);
break;
case PUGL_BUTTON_PRESS:
onButtonPress(&event->button);
break;
case PUGL_BUTTON_RELEASE:
onButtonRelease(&event->button);
break;
case PUGL_MOTION:
onMotion(&event->motion);
break;
default:
break;
}
return PUGL_SUCCESS;
}
PuglNativeView SignalViewUI::getNativeView(void)
{
PuglNativeView r;
// wait for the view to be initialized;
view_sem.wait();
if(view_ready){
// return the native view
r = puglGetNativeView(view);
}else{
r = (PuglNativeView)0;
}
//printf("SignalViewUI::getNativeView r = %p\n", (void*)r);
return r;
}
int SignalViewUI::ui_idle(void)
{
if(state_valid){
puglUpdate(world, 0.0);
}
if(quit)
return 1;
else
return 0;
}
void SignalViewUI::send_ui_state(void)
{
lv2_atom_forge_set_buffer(&forge, obj_buf, sizeof(obj_buf));
LV2_Atom_Forge_Frame frame;
LV2_Atom* msg =
(LV2_Atom*)lv2_atom_forge_object(&forge, &frame, 0, uris->ui_State);
assert(msg);
lv2_atom_forge_key(&forge, uris->ui_dB_min);
lv2_atom_forge_float(&forge, dB_min);
lv2_atom_forge_key(&forge, uris->ui_dB_max);
lv2_atom_forge_float(&forge, dB_max);
lv2_atom_forge_key(&forge, uris->ui_linFreq);
lv2_atom_forge_float(&forge, linFreq);
lv2_atom_forge_key(&forge, uris->ui_log);
lv2_atom_forge_bool(&forge, log);
lv2_atom_forge_pop(&forge, &frame);
write(
controller,
0,
lv2_atom_total_size(msg),
uris->atom_eventTransfer,
msg);
}
void SignalViewUI::send_ui_disable(void)
{
lv2_atom_forge_set_buffer(&forge, obj_buf, sizeof(obj_buf));
LV2_Atom_Forge_Frame frame;
LV2_Atom* msg =
(LV2_Atom*)lv2_atom_forge_object(&forge, &frame, 0, uris->ui_Off);
assert(msg);
lv2_atom_forge_pop(&forge, &frame);
write(controller,
0,
lv2_atom_total_size(msg),
uris->atom_eventTransfer,
msg);
}
void SignalViewUI::send_ui_enable(void)
{
lv2_atom_forge_set_buffer(&forge, obj_buf, sizeof(obj_buf));
LV2_Atom_Forge_Frame frame;
LV2_Atom* msg =
(LV2_Atom*)lv2_atom_forge_object(&forge, &frame, 0, uris->ui_On);
assert(msg);
lv2_atom_forge_pop(&forge, &frame);
write(controller,
0,
lv2_atom_total_size(msg),
uris->atom_eventTransfer,
msg);
}
void SignalViewUI::send_ui_send_state(void)
{
lv2_atom_forge_set_buffer(&forge, obj_buf, sizeof(obj_buf));
LV2_Atom_Forge_Frame frame;
LV2_Atom* msg =
(LV2_Atom*)lv2_atom_forge_object(&forge, &frame, 0, uris->ui_SendState);
assert(msg);
lv2_atom_forge_pop(&forge, &frame);
write(controller,
0,
lv2_atom_total_size(msg),
uris->atom_eventTransfer,
msg);
}
void SignalViewUI::recv_raw_audio(const LV2_Atom_Object* obj)
{
const LV2_Atom* nChannels_atom = NULL;
const LV2_Atom* data_atom = NULL;
const int n_props = lv2_atom_object_get(
obj,
uris->nChannels, &nChannels_atom,
uris->audioData, &data_atom,
0);
if(n_props!=2 || nChannels_atom->type!=uris->atom_Int
|| data_atom->type!=uris->atom_Vector){
return;
}
const int nChannels = ((const LV2_Atom_Int*)nChannels_atom)->body;
if(nChannels!=2){
return;
}
const LV2_Atom_Vector* vec = (const LV2_Atom_Vector*)data_atom;
if(vec->body.child_type != uris->atom_Float){
return;
}
const size_t n_elem =
(data_atom->size - sizeof(LV2_Atom_Vector_Body))/sizeof(float)/nChannels;
const float* data = (const float*)(&vec->body+1);
if(spectrum){
for(size_t i=0;i<n_elem;i++){
float l = *(data++);
float r = *(data++);
spectrum->EvaluateSample(l, r);
}
}
}
void SignalViewUI::recv_ui_state(const LV2_Atom_Object* obj)
{
const LV2_Atom* dB_min_atom = NULL;
const LV2_Atom* dB_max_atom = NULL;
const LV2_Atom* linFreq_atom = NULL;
const LV2_Atom* log_atom = NULL;
const LV2_Atom* rate_atom = NULL;
lv2_atom_object_get(
obj,
uris->ui_dB_min, &dB_min_atom,
uris->ui_dB_max, &dB_max_atom,
uris->ui_linFreq, &linFreq_atom,
uris->ui_log, &log_atom,
uris->param_sampleRate, &rate_atom,
0);
if(dB_min_atom) {
dB_min = ((const LV2_Atom_Float*)dB_min_atom)->body;
}
if(dB_max_atom) {
dB_max = ((const LV2_Atom_Float*)dB_max_atom)->body;
}
if(linFreq_atom) {
linFreq = ((const LV2_Atom_Float*)linFreq_atom)->body;
}
if(log_atom) {
log = ((const LV2_Atom_Bool*)log_atom)->body != 0;
}
if(rate_atom) {
rate = ((const LV2_Atom_Float*)rate_atom)->body;
// signal to the ui thread that the state is valid
state_sem.post();
}
setSpectrum();
}
static LV2UI_Handle instantiate(const struct LV2UI_Descriptor *descriptor, const char *plugin_uri, const char *bundle_path, LV2UI_Write_Function write_function, LV2UI_Controller controller, LV2UI_Widget *widget, const LV2_Feature *const *features)
{
//printf("instantiate\n");
if (strcmp (plugin_uri, SIGNAL_VIEW_URI) != 0) return nullptr;
SignalViewUI* ui;
try
{
ui = new SignalViewUI(
descriptor,
plugin_uri,
bundle_path,
write_function,
controller,
widget,
features);
}
catch(...)
{
LV2_Log_Logger logger;
LV2_Log_Log* log = NULL;
LV2_URID_Map* map = NULL;
const char* missing = lv2_features_query(
features,
LV2_URID__map, &map, true,
LV2_LOG__log, &log, false,
NULL);
if (missing) {
printf("SignalViewUI instantiate error allocating SignalViewUI.\n");
printf("Missing feature <%s>\n", missing);
return nullptr;
}
lv2_log_logger_init(&logger, map, log);
lv2_log_error(&logger, "SignalViewUI instanitate error allocaing SignalViewUI.\n");
return nullptr;
}
PuglNativeView nview = ui->getNativeView();
if(nview){
*widget = (LV2UI_Widget)nview;
return (LV2UI_Handle) ui;
}else{
delete ui;
return (LV2UI_Handle)nullptr;
}
}
static void cleanup (LV2UI_Handle ui)
{
//printf("cleanup\n");
SignalViewUI* sui = static_cast<SignalViewUI*>(ui);
if(sui) delete sui;
}
static void port_event(
LV2UI_Handle ui,
uint32_t port_index,
uint32_t buffer_size,
uint32_t format,
const void *buffer)
{
SignalViewUI* sui = static_cast<SignalViewUI*>(ui);
if(sui)
sui->port_event(port_index,buffer_size,format,buffer);
}
static const void * extension_data (const char *uri)
{
return nullptr;
}
static const LV2UI_Descriptor ui_descriptor =
{
SIGNAL_VIEW_UI_URI,
instantiate,
cleanup,
port_event,
extension_data
};
LV2_SYMBOL_EXPORT const LV2UI_Descriptor * lv2ui_descriptor (uint32_t index)
{
switch (index)
{
case 0: return &ui_descriptor;
default: return 0;
}
}