File: ui_ports.h

package info (click to toggle)
lsp-plugins 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 91,856 kB
  • sloc: cpp: 427,831; xml: 57,779; makefile: 9,961; php: 1,005; sh: 18
file content (847 lines) | stat: -rw-r--r-- 38,030 bytes parent folder | download
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/*
 * Copyright (C) 2021 Linux Studio Plugins Project <https://lsp-plug.in/>
 *           (C) 2021 Vladimir Sadovnikov <sadko4u@gmail.com>
 *
 * This file is part of lsp-plugin-fw
 * Created on: 20 нояб. 2021 г.
 *
 * lsp-plugin-fw is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * lsp-plugin-fw 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with lsp-plugin-fw. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef LSP_PLUG_IN_PLUG_FW_WRAP_LV2_UI_PORTS_H_
#define LSP_PLUG_IN_PLUG_FW_WRAP_LV2_UI_PORTS_H_

#include <lsp-plug.in/plug-fw/version.h>

#include <lsp-plug.in/plug-fw/meta/func.h>
#include <lsp-plug.in/plug-fw/meta/types.h>
#include <lsp-plug.in/plug-fw/ui.h>
#include <lsp-plug.in/plug-fw/wrap/lv2/extensions.h>
#include <lsp-plug.in/plug-fw/wrap/lv2/ports.h>

namespace lsp
{
    namespace lv2
    {
        // Specify port classes
        class UIPort: public ui::IPort, public lv2::Serializable
        {
            protected:
                ssize_t                 nID;

            public:
                explicit UIPort(const meta::port_t *meta, lv2::Extensions *ext) : ui::IPort(meta), lv2::Serializable(ext)
                {
                    nID         = -1;
                    urid        = (meta != NULL) ? pExt->map_port(meta->id) : -1;
                }

                virtual ~UIPort()
                {
                }

            public:
                virtual void notify(const void *buffer, size_t protocol, size_t size)
                {
                }

                inline const char      *get_uri() const         { return (pExt->unmap_urid(urid)); };
                virtual LV2_URID        get_type_urid() const   { return 0; };
                inline void             set_id(ssize_t id)      { nID = id; };
                inline ssize_t          get_id() const          { return nID; };
                virtual bool            sync()                  { return false; };
        };

        class UIPortGroup: public UIPort
        {
            protected:
                size_t          nRows;
                size_t          nCols;
                size_t          nCurrRow;
                lv2::Port      *pPort;

            public:
                UIPortGroup(const meta::port_t *meta, lv2::Extensions *ext, lv2::Port *port) : UIPort(meta, ext)
                {
                    nCurrRow            = meta->start;
                    nRows               = list_size(meta->items);
                    nCols               = port_list_size(meta->members);
                    pPort               = port;

                    if (port != NULL)
                    {
                        lsp_trace("Connected direct group port id=%s", port->metadata()->id);
                        nCurrRow            = port->value();
                    }
                }

                virtual ~UIPortGroup()
                {
                }

            public:
                virtual float value()
                {
                    return nCurrRow;
                }

                virtual void set_value(float value)
                {
                    size_t new_value = meta::limit_value(pMetadata, value);
                    if ((new_value >= 0) && (new_value < nRows) && (new_value != nCurrRow))
                    {
                        nCurrRow        = new_value;
                        lsp_trace("writing patch event id=%s, value=%d", pMetadata->id, int(new_value));
                        pExt->ui_write_patch(this);
                    }
                }

                virtual void serialize()
                {
                    // Serialize and reset pending flag
                    pExt->forge_int(nCurrRow);
                }

                virtual void deserialize(const void *data)
                {
                    const LV2_Atom_Int *atom = reinterpret_cast<const LV2_Atom_Int *>(data);
                    if ((atom->body >= 0) && (atom->body < int32_t(nRows)))
                        nCurrRow        = atom->body;
                }

                virtual LV2_URID get_type_urid() const
                {
                    return pExt->forge.Int;
                }

            public:
                inline size_t rows() const  { return nRows; }
                inline size_t cols() const  { return nCols; }
        };

        class UIFloatPort: public UIPort
        {
            protected:
                float           fValue;
                bool            bForce;
                lv2::Port      *pPort;

            public:
                explicit UIFloatPort(const meta::port_t *meta, lv2::Extensions *ext, lv2::Port *port) :
                    UIPort(meta, ext)
                {
                    fValue      = meta->start;
                    pPort       = port;
                    if (port != NULL)
                    {
                        lsp_trace("Connected direct float port id=%s", port->metadata()->id);
                        fValue      = port->value();
                    }
                    bForce      = port != NULL;
                }
                virtual ~UIFloatPort() { fValue  =   pMetadata->start; };

            public:
                virtual float value() { return fValue; }

                virtual void set_value(float value)
                {
                    fValue      = meta::limit_value(pMetadata, value);
                    if (nID >= 0)
                    {
                        // Use standard mechanism to access port
                        //lsp_trace("write(%d, %d, %d, %f)", int(nID), int(sizeof(float)), int(0), fValue);
                        pExt->write_data(nID, sizeof(float), 0, &fValue);
                    }
                    else
                    {
                        lsp_trace("writing patch event id=%s, value=%f", pMetadata->id, fValue);
                        pExt->ui_write_patch(this);
                    }
                }

                virtual LV2_URID        get_type_urid() const   { return pExt->forge.Float; };

                virtual void serialize()
                {
                    pExt->forge_float(fValue);
                };

                virtual void deserialize(const void *data)
                {
                    const LV2_Atom_Float *atom = reinterpret_cast<const LV2_Atom_Float *>(data);
                    fValue      = meta::limit_value(pMetadata, atom->body);
                }

                virtual void notify(const void *buffer, size_t protocol, size_t size)
                {
                    if (size == sizeof(float))
                        fValue = meta::limit_value(pMetadata, *(reinterpret_cast<const float *>(buffer)));
                }

                virtual bool sync()
                {
                    if ((pPort == NULL) || (nID >= 0))
                        return false;

                    float old   = fValue;
                    fValue      = meta::limit_value(pMetadata, pPort->value());
                    bool synced = (fValue != old) || bForce;
                    bForce      = false;

                #ifdef LSP_TRACE
                    if (synced)
                        lsp_trace("Directly received float port id=%s, value=%f",
                            pPort->metadata()->id, fValue);
                #endif
                    return synced;
                }
        };

        class UIBypassPort: public UIFloatPort
        {
            public:
                explicit UIBypassPort(const meta::port_t *meta, lv2::Extensions *ext, lv2::Port *port) :
                    UIFloatPort(meta, ext, port)
                {
                }

                virtual ~UIBypassPort() { };

            public:
                virtual void set_value(float value)
                {
                    fValue      = meta::limit_value(pMetadata, value);
                    if (nID >= 0)
                    {
                        // Use standard mechanism to access port
                        float value = pMetadata->max - fValue;
                        lsp_trace("write(%d, %d, %d, %f)", int(nID), int(sizeof(float)), int(0), value);
                        pExt->write_data(nID, sizeof(float), 0, &value);
                    }
                    else
                    {
                        lsp_trace("writing patch event id=%s, value=%f", pMetadata->id, fValue);
                        pExt->ui_write_patch(this);
                    }
                }

                virtual void serialize()
                {
                    pExt->forge_float(pMetadata->max - fValue);
                };

                virtual void deserialize(const void *data)
                {
                    const LV2_Atom_Float *atom = reinterpret_cast<const LV2_Atom_Float *>(data);
                    fValue      = meta::limit_value(pMetadata, pMetadata->max - atom->body);
                }

                virtual void notify(const void *buffer, size_t protocol, size_t size)
                {
                    if (size == sizeof(float))
                        fValue = meta::limit_value(pMetadata, pMetadata->max - *(reinterpret_cast<const float *>(buffer)));
                    lsp_trace("set value of port %s = %f", pMetadata->id, fValue);
                }
        };

        class UIPeakPort: public UIFloatPort
        {
            public:
                explicit UIPeakPort(const meta::port_t *meta, lv2::Extensions *ext, lv2::Port *port) :
                    UIFloatPort(meta, ext, port) {}
                virtual ~UIPeakPort() {};

            public:
                virtual void notify(const void *buffer, size_t protocol, size_t size)
                {
                    if (size == sizeof(LV2UI_Peak_Data))
                    {
                        fValue = meta::limit_value(pMetadata, (reinterpret_cast<const LV2UI_Peak_Data *>(buffer))->peak);
                        return;
                    }
                    UIFloatPort::notify(buffer, protocol, size);
    //                lsp_trace("id=%s, value=%f", pMetadata->id, fValue);
                }
        };

        class UIMeshPort: public UIPort
        {
            protected:
                lv2_mesh_t              sMesh;
                bool                    bParsed;
                lv2::MeshPort          *pPort;

            public:
                explicit UIMeshPort(const meta::port_t *meta, lv2::Extensions *ext, lv2::Port *xport) : UIPort(meta, ext)
                {
                    sMesh.init(meta);
                    bParsed     = false;
                    pPort       = NULL;

                    lsp_trace("id=%s, ext=%p, xport=%p", meta->id, ext, xport);

                    // Try to perform direct access to the port using LV2:Instance interface
                    const meta::port_t *xmeta = (xport != NULL) ? xport->metadata() : NULL;
                    if (meta::is_mesh_port(xmeta))
                    {
                        pPort                   = static_cast<lv2::MeshPort *>(xport);
                        plug::mesh_t *mesh      = static_cast<plug::mesh_t *>(pPort->buffer());
                        mesh->cleanup();  // Mark mesh as empty to force the DSP to write data to mesh
                        lsp_trace("Connected direct mesh port id=%s", xmeta->id);
                    }
                }

                virtual ~UIMeshPort()
                {
                }

            public:
                virtual LV2_URID        get_type_urid() const   { return pExt->uridMeshType; };

                virtual void deserialize(const void *data)
                {
                    const LV2_Atom_Object* obj = reinterpret_cast<const LV2_Atom_Object *>(data);

                    // Parse atom body
                    bParsed     = false;
                    LV2_Atom_Property_Body *body    = lv2_atom_object_begin(&obj->body);

                    // Get number of vectors (dimensions)
                    if (lv2_atom_object_is_end(&obj->body, obj->atom.size, body))
                        return;
    //                lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));

                    if ((body->key != pExt->uridMeshDimensions) || (body->value.type != pExt->forge.Int))
                        return;
                    ssize_t dimensions = (reinterpret_cast<const LV2_Atom_Int *>(& body->value))->body;
    //                lsp_trace("dimensions=%d", int(dimensions));
                    if (dimensions > ssize_t(sMesh.nBuffers))
                        return;
                    sMesh.pMesh->nBuffers   = dimensions;

                    // Get size of each vector
                    body = lv2_atom_object_next(body);
                    if (lv2_atom_object_is_end(&obj->body, obj->atom.size, body))
                        return;

    //                lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));

                    if ((body->key != pExt->uridMeshItems) || (body->value.type != pExt->forge.Int))
                        return;
                    ssize_t vector_size = (reinterpret_cast<const LV2_Atom_Int *>(& body->value))->body;
    //                lsp_trace("vector size=%d", int(vector_size));
                    if ((vector_size < 0) || (vector_size > ssize_t(sMesh.nMaxItems)))
                        return;
                    sMesh.pMesh->nItems     = vector_size;

                    // Now parse each vector
                    for (ssize_t i=0; i < dimensions; ++i)
                    {
                        // Read vector as array of floats
                        body = lv2_atom_object_next(body);
                        if (lv2_atom_object_is_end(&obj->body, obj->atom.size, body))
                            return;

    //                    lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                    lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));
    //                    lsp_trace("sMesh.pUrids[%d] (%d) = %s", int(i), int(sMesh.pUrids[i]), pExt->unmap_urid(sMesh.pUrids[i]));

                        if ((body->key != pExt->uridMeshData) || (body->value.type != pExt->forge.Vector))
                            return;
                        const LV2_Atom_Vector *v = reinterpret_cast<const LV2_Atom_Vector *>(&body->value);

    //                    lsp_trace("v->body.child_type (%d) = %s", int(v->body.child_type), pExt->unmap_urid(v->body.child_type));
    //                    lsp_trace("v->body.child_size = %d", int(v->body.child_size));
                        if ((v->body.child_size != sizeof(float)) || (v->body.child_type != pExt->forge.Float))
                            return;
                        ssize_t v_items     = (v->atom.size - sizeof(LV2_Atom_Vector_Body)) / sizeof(float);
    //                    lsp_trace("vector items=%d", int(v_items));
                        if (v_items != vector_size)
                            return;

                        // Now we can surely copy data
                        dsp::copy_saturated(sMesh.pMesh->pvData[i], reinterpret_cast<const float *>(v + 1), v_items);
    //                    memcpy(sMesh.pMesh->pvData[i], reinterpret_cast<const float *>(v + 1), v_items * sizeof(float));
                    }

                    // Update mesh parameters
    //                lsp_trace("Mesh was successful parsed dimensions=%d, items=%d", int(sMesh.pMesh->nBuffers), int(sMesh.pMesh->nItems));
                    bParsed                 = true;
                }

                virtual void *buffer()
                {
                    if (!bParsed)
                        return NULL;

                    return sMesh.pMesh;
                }

                virtual bool sync()
                {
                    if (pPort == NULL)
                        return false;

                    plug::mesh_t *mesh = reinterpret_cast<plug::mesh_t *>(pPort->buffer());
                    if ((mesh == NULL) || (!mesh->containsData()))
                        return false;

                    // Copy mesh data
                    for (size_t i=0; i < mesh->nBuffers; ++i)
                        dsp::copy_saturated(sMesh.pMesh->pvData[i], mesh->pvData[i], mesh->nItems);
                    sMesh.pMesh->data(mesh->nBuffers, mesh->nItems);
    //                lsp_trace("Directly received mesh port id=%s, buffers=%d, items=%d",
    //                        pPort->metadata()->id, int(sMesh.pMesh->nBuffers), int(sMesh.pMesh->nItems));

                    // Clean source mesh
                    mesh->cleanup();
                    bParsed = true;
                    return sMesh.pMesh->containsData();
                }
        };

        class UIStreamPort: public UIPort
        {
            protected:
                plug::stream_t         *pStream;
                lv2::StreamPort        *pPort;

            public:
                explicit UIStreamPort(const meta::port_t *meta, lv2::Extensions *ext, lv2::Port *xport) : UIPort(meta, ext)
                {
                    pStream         = plug::stream_t::create(pMetadata->min, pMetadata->max, pMetadata->start);
                    pPort           = NULL;

                    // Try to perform direct access to the port using LV2:Instance interface
                    const meta::port_t *xmeta = (xport != NULL) ? xport->metadata() : NULL;
                    if (meta::is_stream_port(xmeta))
                    {
                        pPort               = static_cast<lv2::StreamPort *>(xport);
                        lsp_trace("Connected direct stream port id=%s", xmeta->id);
                    }
                }

                virtual ~UIStreamPort()
                {
                    plug::stream_t::destroy(pStream);
                    pStream         = NULL;
                };

            protected:
                void deserialize_frame(LV2_Atom_Object *frame)
                {
                    // Parse atom body
                    LV2_Atom_Property_Body *body    = lv2_atom_object_begin(&frame->body);

                    // Get frame identifier
                    if (lv2_atom_object_is_end(&frame->body, frame->atom.size, body))
                        return;
    //                lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));
                    if ((body->key != pExt->uridStreamFrameId) || (body->value.type != pExt->forge.Int))
                        return;
                    uint32_t frame_id = (reinterpret_cast<const LV2_Atom_Int *>(&body->value))->body;
    //                lsp_trace("frame_id = %d", int(frame_id));

                    // Get frame size
                    body = lv2_atom_object_next(body);
                    if (lv2_atom_object_is_end(&frame->body, frame->atom.size, body))
                        return;
    //                lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));
                    if ((body->key != pExt->uridStreamFrameSize) || (body->value.type != pExt->forge.Int))
                        return;
                    ssize_t frame_size  = (reinterpret_cast<const LV2_Atom_Int *>(&body->value))->body;
    //                lsp_trace("frame_size = %d", int(frame_size));

                    frame_size          = lsp_min(frame_size, STREAM_MAX_FRAME_SIZE);
    //                lsp_trace("act_frame_size = %d", int(frame_size));

                    // If frame differs to the current one - clear the stream
                    uint32_t prev_id    = frame_id - 1;
    //                lsp_trace("prev_id = %d, frame_id=%d", int(prev_id), int(pStream->frame_id()));
                    if (pStream->frame_id() != prev_id)
                    {
                        pStream->clear(prev_id);
    //                    lsp_trace("cleared stream to frame %d", int(pStream->frame_id()));
                    }

                    // Now we are able to commit the frame
                    frame_size          = pStream->add_frame(frame_size);
                    for (size_t i=0, n=pStream->channels(); i<n; ++i)
                    {
                        // Read vector as array of floats
                        body = lv2_atom_object_next(body);
                        if (lv2_atom_object_is_end(&frame->body, frame->atom.size, body))
                            break;
    //                    lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                    lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));

                        if ((body->key != pExt->uridStreamFrameData) || (body->value.type != pExt->forge.Vector))
                            return;
                        const LV2_Atom_Vector *v = reinterpret_cast<const LV2_Atom_Vector *>(&body->value);
    //                    lsp_trace("body->child_size = %d, body->child_type (%d) = %s", int(v->body.child_size), int(v->body.child_type), pExt->unmap_urid(v->body.child_type));
                        if ((v->body.child_size != sizeof(float)) || (v->body.child_type != pExt->forge.Float))
                            return;

                        ssize_t v_items     = lsp_min(frame_size, ssize_t((v->atom.size - sizeof(LV2_Atom_Vector_Body)) / sizeof(float)));
    //                    lsp_trace("channel = %d, floats = %d", int(i), int(v_items));
                        pStream->write_frame(i, reinterpret_cast<const float *>(v + 1), 0, v_items);
                    }

                    // Commit the frame
                    pStream->commit_frame();
                }

            public:
                virtual LV2_URID        get_type_urid() const   { return pExt->uridStreamType; };

                virtual void deserialize(const void *data)
                {
                    const LV2_Atom_Object* obj = reinterpret_cast<const LV2_Atom_Object *>(data);
    //                lsp_trace("id = %s", pMetadata->id);

                    // Parse atom body
                    LV2_Atom_Property_Body *body    = lv2_atom_object_begin(&obj->body);

                    // Get number of vectors (dimensions)
                    if (lv2_atom_object_is_end(&obj->body, obj->atom.size, body))
                        return;
    //                lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));

                    if ((body->key != pExt->uridStreamDimensions) || (body->value.type != pExt->forge.Int))
                        return;
                    uint32_t dimensions = (reinterpret_cast<const LV2_Atom_Int *>(&body->value))->body;
                    if (dimensions != pStream->channels())
                        return;

                    while (true)
                    {
                        // Get size of each vector
                        body = lv2_atom_object_next(body);
                        if (lv2_atom_object_is_end(&obj->body, obj->atom.size, body))
                            break;

    //                    lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                    lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));

                        if ((body->key != pExt->uridStreamFrame) || (body->value.type != pExt->forge.Object))
                            continue;

                        LV2_Atom_Object *fobj       = (reinterpret_cast<LV2_Atom_Object *>(& body->value));
                        if ((fobj->body.id != pExt->uridBlank) || (fobj->body.otype != pExt->uridStreamFrameType))
                            continue;

                        // Read frame data
                        deserialize_frame(fobj);
                    }
                }

                virtual void *buffer()
                {
                    return pStream;
                }

                virtual bool sync()
                {
                    // Check if there is data for viewing
                    plug::stream_t *s = static_cast<plug::stream_t *>(pPort->buffer());
                    return (s != NULL) ? pStream->sync(s) : false;
                }
        };

        class UIFrameBufferPort: public UIPort
        {
            protected:
                plug::frame_buffer_t    sFB;
                lv2::FrameBufferPort   *pPort;

            public:
                explicit UIFrameBufferPort(const meta::port_t *meta, lv2::Extensions *ext, lv2::Port *xport):
                    UIPort(meta, ext)
                {
                    sFB.init(meta->start, meta->step);
                    pPort       = NULL;

                    lsp_trace("id=%s, ext=%p, xport=%p", meta->id, ext, xport);

                    // Try to perform direct access to the port using LV2:Instance interface
                    const meta::port_t *xmeta = (xport != NULL) ? xport->metadata() : NULL;
                    if (meta::is_framebuffer_port(xmeta))
                    {
                        pPort               = static_cast<lv2::FrameBufferPort *>(xport);
                        lsp_trace("Connected direct framebuffer port id=%s", xmeta->id);
                    }
                }

                virtual ~UIFrameBufferPort()
                {
                }

            public:
                virtual LV2_URID        get_type_urid() const   { return pExt->uridMeshType; };

                virtual void deserialize(const void *data)
                {
                    const LV2_Atom_Object* obj = reinterpret_cast<const LV2_Atom_Object *>(data);

    //                lsp_trace("id = %s", pMetadata->id);

                    // Parse atom body
                    LV2_Atom_Property_Body *body    = lv2_atom_object_begin(&obj->body);

                    // Get number of vectors (dimensions)
                    if (lv2_atom_object_is_end(&obj->body, obj->atom.size, body))
                        return;
    //                lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));

                    if ((body->key != pExt->uridFrameBufferRows) || (body->value.type != pExt->forge.Int))
                        return;
                    uint32_t rows = (reinterpret_cast<const LV2_Atom_Int *>(& body->value))->body;
                    if (rows != sFB.rows())
                    {
    //                    lsp_trace("Row count does not match: %d vs %d", int(rows), int(sFB.rows()));
                        return;
                    }

                    // Get size of each vector
                    body = lv2_atom_object_next(body);
                    if (lv2_atom_object_is_end(&obj->body, obj->atom.size, body))
                    {
    //                    lsp_trace("unexpected end of object");
                        return;
                    }
    //                lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));

                    if ((body->key != pExt->uridFrameBufferCols) || (body->value.type != pExt->forge.Int))
                        return;
                    uint32_t cols = (reinterpret_cast<const LV2_Atom_Int *>(& body->value))->body;
                    if (cols != sFB.cols())
                    {
    //                    lsp_trace("Column count does not match: %d vs %d", int(cols), int(sFB.cols()));
                        return;
                    }

                    // Get first row
                    body = lv2_atom_object_next(body);
                    if (lv2_atom_object_is_end(&obj->body, obj->atom.size, body))
                    {
    //                    lsp_trace("unexpected end of object");
                        return;
                    }

    //                lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));
                    if ((body->key != pExt->uridFrameBufferFirstRowID) || (body->value.type != pExt->forge.Int))
                        return;
                    uint32_t first_row = (reinterpret_cast<const LV2_Atom_Int *>(& body->value))->body;
    //                lsp_trace("first_row = %d", int(first_row));

                    // Get last row
                    body = lv2_atom_object_next(body);
                    if (lv2_atom_object_is_end(&obj->body, obj->atom.size, body))
                    {
    //                    lsp_trace("unexpected end of object");
                        return;
                    }

    //                lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));

                    if ((body->key != pExt->uridFrameBufferLastRowID) || (body->value.type != pExt->forge.Int))
                        return;
                    uint32_t last_row = (reinterpret_cast<const LV2_Atom_Int *>(& body->value))->body;
    //                lsp_trace("last_row = %d", int(last_row));

                    lsp_trace("first_row = %d, last_row = %d", int(first_row), int(last_row));

                    // Validate
                    uint32_t delta = last_row - first_row;
                    if (delta > FRAMEBUFFER_BULK_MAX)
                    {
    //                    lsp_trace("delta too large: %d", int(delta));
                        return;
                    }

                    // Now parse each vector
                    while (first_row != last_row)
                    {
                        // Read vector as array of floats
                        body = lv2_atom_object_next(body);
                        if (lv2_atom_object_is_end(&obj->body, obj->atom.size, body))
                        {
    //                        lsp_trace("unexpected end of object");
                            return;
                        }

    //                    lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
    //                    lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));

                        if ((body->key != pExt->uridFrameBufferData) || (body->value.type != pExt->forge.Vector))
                            return;
                        const LV2_Atom_Vector *v = reinterpret_cast<const LV2_Atom_Vector *>(&body->value);

    //                    lsp_trace("body->child_size = %d, body->child_type (%d) = %s", int(v->body.child_size), int(v->body.child_type), pExt->unmap_urid(v->body.child_type));

                        if ((v->body.child_size != sizeof(float)) || (v->body.child_type != pExt->forge.Float))
                            return;
                        ssize_t v_items     = (v->atom.size - sizeof(LV2_Atom_Vector_Body)) / sizeof(float);
                        if (v_items != ssize_t(cols))
                        {
    //                        lsp_trace("vector items does not match columns count: %d vs %d", int(v_items), int(cols));
                            return;
                        }

                        sFB.write_row(first_row++, reinterpret_cast<const float *>(v + 1));
                    }
                    sFB.seek(first_row);
                }

                virtual void *buffer()
                {
                    return &sFB;
                }

                virtual bool sync()
                {
                    // Check if there is data for viewing
                    plug::frame_buffer_t *fb = reinterpret_cast<plug::frame_buffer_t *>(pPort->buffer());
                    return (fb != NULL) ? sFB.sync(fb) : false;
                }
        };

        class UIPathPort: public UIPort
        {
            protected:
                lv2::PathPort      *pPort;
                char                sPath[PATH_MAX];

            public:
                explicit UIPathPort(const meta::port_t *meta, lv2::Extensions *ext, lv2::Port *xport):
                    UIPort(meta, ext)
                {
                    sPath[0]    = '\0';
                    pPort       = NULL;

                    lsp_trace("id=%s, ext=%p, xport=%p", meta->id, ext, xport);

                    // Try to perform direct access to the port using LV2:Instance interface
                    const meta::port_t *xmeta = (xport != NULL) ? xport->metadata() : NULL;
                    if (meta::is_path_port(xmeta))
                    {
                        pPort               = static_cast<lv2::PathPort *>(xport);
                        pPort->tx_request();
                        lsp_trace("Connected direct path port id=%s", xmeta->id);
                    }
                }

                virtual ~UIPathPort()
                {
                };

            public:
                virtual void deserialize(const void *data)
                {
                    // Read path value
                    const LV2_Atom *atom = reinterpret_cast<const LV2_Atom *>(data);
                    lv2_set_string(sPath, PATH_MAX, reinterpret_cast<const char *>(atom + 1), atom->size);

                    // The code below works only when restoring plugin state
                    lsp_trace("mapPath = %p, path = %s", pExt->mapPath, sPath);
                    if ((pExt->mapPath != NULL) && (::strstr(sPath, LSP_BUILTIN_PREFIX) != sPath))
                    {
                        char *unmapped_path = pExt->mapPath->absolute_path(pExt->mapPath->handle, sPath);
                        if (unmapped_path != NULL)
                        {
                            lsp_trace("unmapped path: %s -> %s", sPath, unmapped_path);
                            lv2_set_string(sPath, PATH_MAX, unmapped_path, ::strlen(unmapped_path));
                            ::free(unmapped_path);
                        }
                    }
                }

                virtual LV2_URID        get_type_urid() const   { return pExt->uridPathType; };

                virtual void serialize()
                {
                    lsp_trace("mapPath = %p, path = %s", pExt->mapPath, sPath);
                    if ((pExt->mapPath != NULL) && (::strstr(sPath, LSP_BUILTIN_PREFIX) != sPath))
                    {
                        char* mapped_path = pExt->mapPath->abstract_path(pExt->mapPath->handle, sPath);
                        if (mapped_path != NULL)
                        {
                            lsp_trace("mapped path: %s -> %s", sPath, mapped_path);
                            pExt->forge_path(mapped_path);
                            ::free(mapped_path);
                        }
                        else
                            pExt->forge_path(sPath);
                    }
                    else
                        pExt->forge_path(sPath);
                }

                virtual void write(const void* buffer, size_t size, size_t flags)
                {
                    lv2_set_string(sPath, PATH_MAX, reinterpret_cast<const char *>(buffer), size);

                    // Try to perform direct access to the port using LV2:Instance interface
                    lsp_trace(
                        "writing patch event id=%s, path=%s (%d)",
                        pMetadata->id, static_cast<const char *>(buffer), int(size));
                    pExt->ui_write_patch(this);
                }

                virtual void write(const void* buffer, size_t size)
                {
                    write(buffer, size, 0);
                }

                virtual bool sync()
                {
                    if (!pPort->tx_pending())
                        return false;
                    pPort->reset_tx_pending();

                    plug::path_t *path  = static_cast<plug::path_t *>(pPort->buffer());
                    ::strncpy(sPath, path->path(), PATH_MAX-1); // Copy current contents
                    sPath[PATH_MAX-1]   = '\0';

                    lsp_trace("Directly received path port id=%s, path=%s",
                            pPort->metadata()->id, sPath);

                    return true;
                }

                virtual void *buffer()
                {
                    return sPath;
                }

                virtual void set_default()
                {
                    write("", 0, plug::PF_PRESET_IMPORT);
                }
        };
    } /* namespace lv2 */
} /* namespace lsp */


#endif /* LSP_PLUG_IN_PLUG_FW_WRAP_LV2_UI_PORTS_H_ */