summaryrefslogtreecommitdiff
path: root/libs/backends/wavesaudio/waves_audiobackend.port_engine.cc
blob: 203aa1f35d9066c930483a8d70270541ffd5f13f (plain)
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
/*
    Copyright (C) 2014 Waves Audio Ltd.

    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 2 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, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "waves_audiobackend.h"
#include "waves_audioport.h"
#include "waves_midiport.h"
#include "waves_midi_event.h"

using namespace ARDOUR;

uint32_t
WavesAudioBackend::port_name_size () const
{
    return 256+64;
}

int
WavesAudioBackend::set_port_name (PortHandle port_handle, const std::string& port_name)
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::set_port_name (): [" << std::hex << port_handle << std::dec << "], [" << port_name << "]" << std::endl;

    if (!_registered (port_handle)) {
        std::cerr << "WavesAudioBackend::set_port_name (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
        return -1;
    }

    return ((WavesAudioPort*)port_handle)->set_name (__instantiated_name + ":" + port_name);
}


std::string
WavesAudioBackend::get_port_name (PortHandle port_handle) const
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::get_port_name (): [" << std::hex << port_handle << std::dec << "]" << std::endl;
    if (!_registered (port_handle)) {
        std::cerr << "WavesAudioBackend::get_port_name (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
        return std::string ();
    }
    // COMMENTED DBG LOGS */ else std::cout  << "\t[" << ((WavesAudioPort*)port_handle)->name () << "]" << std::endl;

    return ((WavesAudioPort*)port_handle)->name ();
}


PortEngine::PortHandle
WavesAudioBackend::get_port_by_name (const std::string& port_name) const
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::get_port_by_name (): [" << port_name << "]" << std::endl;

    PortHandle port_handle = (PortHandle)_find_port (port_name);
    if (!port_handle) {
        std::cerr << "WavesAudioBackend::get_port_by_name (): Failed to find port [" << port_name << "]!" << std::endl;
    }

    return port_handle;
}


WavesDataPort*
WavesAudioBackend::_find_port (const std::string& port_name) const
{
    for (std::vector<WavesDataPort*>::const_iterator it = _ports.begin (); it != _ports.end (); ++it) {
        if ((*it)->name () == port_name) {
            return *it;
        }
    }

    return NULL;
}


int
WavesAudioBackend::get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector<std::string>& port_names) const
{

    // COMMENTED DBG LOGS */ std::cout << "WavesAudioBackend::get_ports (): \n\tPattern: [" << port_name_pattern << "]\n\tType: " << type << "\n\tFlags: " << flags << endl;

    unsigned found_ports =0;

    for (size_t i = 0; i < _ports.size (); ++i) {
        WavesDataPort* port = _ports[i];

        if ((port->type () == type) && flags == (port->flags () & flags)) {
            port_names.push_back (port->name ());
            found_ports++;
        }
    }
    return found_ports;
}


DataType
WavesAudioBackend::port_data_type (PortHandle port_handle) const
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::port_data_type" << std::endl;

    if (!_registered (port_handle)) {
        std::cerr << "WavesAudioBackend::port_data_type (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
        return DataType::NIL;
    }

    // COMMENTED DBG LOGS */ std::cout << "WavesAudioBackend::port_data_type: " << endl;

    return ((WavesAudioPort*)port_handle)->type ();
}


PortEngine::PortHandle
WavesAudioBackend::register_port (const std::string& shortname, ARDOUR::DataType type, ARDOUR::PortFlags flags)
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::register_port (): " << type.to_string () << " [" << shortname << "]" << std::endl;

    if (shortname.size () == 0) {
        std::cerr << "WavesAudioBackend::register_port (): Invalid (empty) port name!" << std::endl;
        return NULL;
    }

    if (flags & IsPhysical) {
        std::cerr << "WavesAudioBackend::register_port (): Unexpected attribute for port [" << shortname << "]! The port must not be physical!";
        return NULL;
    }

    return (PortEngine::PortHandle)_register_port (__instantiated_name + ":" + shortname, type, flags);
}


WavesDataPort*
WavesAudioBackend::_register_port (const std::string& port_name, ARDOUR::DataType type, ARDOUR::PortFlags flags)
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::_register_port (): [" << port_name << "]" << std::endl;

    if (_find_port (port_name) != NULL) {
        std::cerr << "WavesAudioBackend::register_port () : Port [" << port_name << "] is already registered!" << std::endl;
        return NULL;
    }

    WavesDataPort* port = NULL;
    switch (type) {
        case ARDOUR::DataType::AUDIO: {
            WavesAudioPort* audio_port = new WavesAudioPort (port_name, flags);
            if (flags & IsPhysical)
            {
                if (flags & IsOutput)
                {
                    _physical_audio_inputs.push_back (audio_port);
                    // COMMENTED DBG LOGS */ std::cout  << "\t\t" << port_name << " added to physical AUDIO Inputs !" << std::endl;
                }
                else if (flags & IsInput)
                {
                    _physical_audio_outputs.push_back (audio_port);
                    // COMMENTED DBG LOGS */ std::cout  << "\t\t" << port_name << " added to physical AUDIO Outputs !" << std::endl;
                }
            }
            port = audio_port;
        } break;
        case ARDOUR::DataType::MIDI: {
            WavesMidiPort* midi_port = new WavesMidiPort (port_name, flags);
            if (flags & IsPhysical)
            {
                if (flags & IsOutput)
                {
                    _physical_midi_inputs.push_back (midi_port);
                    // COMMENTED DBG LOGS */ std::cout  << "\t\t" << port_name << " added to physical MIDI Inputs !" << std::endl;
                }
                else if (flags & IsInput)
                {
                    _physical_midi_outputs.push_back (midi_port);
                    // COMMENTED DBG LOGS */ std::cout  << "\t\t" << port_name << " added to physical MIDI Outputs !" << std::endl;
                }
            }
            port = midi_port;
        } break;
        default:
            std::cerr << "WavesAudioBackend::register_port () : Invalid data type (" << (uint32_t)type << ") applied to port [" << port_name << "]!" << std::endl;
        return NULL;
    }

    _ports.push_back (port);

    return port;
}


void
WavesAudioBackend::unregister_port (PortHandle port_handle)
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::unregister_port ():" << std::hex << port_handle << std::dec << std::endl;

    // so far we suppose all disconnections will be done prior to unregistering.
    WavesDataPort* port = (WavesDataPort*)port_handle;
    std::vector<WavesDataPort*>::iterator port_iterator = std::find (_ports.begin (), _ports.end (), (WavesDataPort*)port_handle);
    if (port_iterator == _ports.end ()) {
        std::cerr << "WavesAudioBackend::unregister_port (): Failed to find port [" << std::hex << port_handle << std::dec << "]!"  << std::endl;
        return;
    }
    // COMMENTED DBG LOGS */ std::cout  << "\t[" << ((WavesDataPort*)port_handle)->name () << "]" << std::endl;

    _ports.erase (port_iterator);

    if (port->is_physical ()) {
        if (port->is_output ()) {
            switch (port->type ()) {
                case ARDOUR::DataType::AUDIO: {
                    std::vector<WavesAudioPort*>::iterator audio_port_iterator = std::find (_physical_audio_inputs.begin (), _physical_audio_inputs.end (), port);
                    if (audio_port_iterator == _physical_audio_inputs.end ())    {
                        std::cerr << "WavesAudioBackend::unregister_port (): Failed to find port [" << port->name () << "] in the list of registered physical audio inputs!" << std::endl;
                        return;
                    }
                    _physical_audio_inputs.erase (audio_port_iterator);
                }
                break;
                case ARDOUR::DataType::MIDI: {
                    std::vector<WavesMidiPort*>::iterator midi_port_iterator = std::find (_physical_midi_inputs.begin (), _physical_midi_inputs.end (), port);
                    if (midi_port_iterator == _physical_midi_inputs.end ()) {
                        std::cerr << "WavesAudioBackend::unregister_port (): Failed to find port [" << port->name () << "] in the list of registered physical midi inputs!" << std::endl;
                        return;
                    }
                    _physical_midi_inputs.erase (midi_port_iterator);
                }
                break;
                default:
                    std::cerr << "WavesAudioBackend::unregister_port (): Invalid type (" << port->type () << " applied to [" << port->name () << "]!" << std::endl;
                break;
            }
        }
        else if (port->flags () & IsInput) {
            switch (port->type ()) {
                case ARDOUR::DataType::AUDIO: {
                    std::vector<WavesAudioPort*>::iterator audio_port_iterator = std::find (_physical_audio_outputs.begin (), _physical_audio_outputs.end (), port);
                    if (audio_port_iterator == _physical_audio_outputs.end ())
                    {
                        std::cerr << "WavesAudioBackend::unregister_port: Failed to find port [" << port->name () << std::dec << "] in the list of registered physical audio outputs!\n";
                        return;
                    }
                    _physical_audio_outputs.erase (audio_port_iterator);
                }
                break;
                case ARDOUR::DataType::MIDI: {

                    std::vector<WavesMidiPort*>::iterator midi_port_iterator = std::find (_physical_midi_outputs.begin (), _physical_midi_outputs.end (), port);
                    if (midi_port_iterator == _physical_midi_outputs.end ())
                    {
                        std::cerr << "WavesAudioBackend::unregister_port: Failed to find port [" << port->name () << std::dec << "] in the list of registered physical midi outputs!\n";
                        return;
                    }
                    _physical_midi_outputs.erase (midi_port_iterator);
                }
                break;
                default:
                    std::cerr << "WavesAudioBackend::unregister_port (): Invalid type (" << port->type () << " applied to [" << port->name () << "]!" << std::endl;
                break;
            }
        }
    }

    delete port;
}


int
WavesAudioBackend::connect (const std::string& src_port_name, const std::string& dst_port_name)
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::connect (" << src_port_name << ", " << dst_port_name << "):" << std::endl;

    WavesDataPort* src_port = _find_port (src_port_name);
    if (src_port == NULL) {
        std::cerr << "WavesAudioBackend::connect: Failed to find source port " << src_port_name << " !" << std::endl;
        return -1;
    }

    WavesDataPort* dst_port = _find_port (dst_port_name);
    if (dst_port == NULL) {
        std::cerr << "WavesAudioBackend::connect: Failed to find destination port " << dst_port_name << " !" << std::endl;
        return -1;
    }

    // COMMENTED DBG LOGS */ std::cout  << "\t\t (" << src_port << ", " << dst_port << "):" << std::endl;
    return src_port->connect (dst_port);
}


int
WavesAudioBackend::connect (PortHandle src_port_handle, const std::string& dst_port_name)
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::connect ():" << std::endl;
    if (!_registered (src_port_handle)) {
        std::cerr << "WavesAudioBackend::connect: Failed to find source port [" << std::hex << src_port_handle << std::dec << "]!" << std::endl;
        return -1;
    }

    // COMMENTED DBG LOGS */ std::cout  << "\t[" << std::hex << src_port_handle << std::dec << "]" << std::endl;
    // COMMENTED DBG LOGS */ std::cout  << "\t[" << dst_port_name << "]" << std::endl;

    WavesDataPort* dst_port = _find_port (dst_port_name);
    if (dst_port == NULL) {
        std::cerr << "WavesAudioBackend::connect (): Failed to find destination port [" << dst_port_name << "]!" << std::endl;
        return -1;
    }

    return ((WavesDataPort*)src_port_handle)->connect (dst_port);
}


int
WavesAudioBackend::disconnect (PortHandle src_port_handle, const std::string& dst_port_name)
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::disconnect (" << src_port_handle << ", " << dst_port_name << "):" << std::endl;
    if (!_registered (src_port_handle)) {
        std::cerr << "WavesAudioBackend::disconnect (): Failed to find source port [" << std::hex << src_port_handle << std::dec << "]!" << std::endl;
        return -1;
    }

    // COMMENTED DBG LOGS */ std::cout  << "\t[" << std::hex << src_port_handle << std::dec << "]" << std::endl;
    // COMMENTED DBG LOGS */ std::cout  << "\t[" << dst_port_name << "]" << std::endl;

    WavesDataPort* dst_port = _find_port (dst_port_name);
    if (dst_port == NULL) {
        std::cerr << "WavesAudioBackend::disconnect (): Failed to find destination port [" << dst_port_name << "]!" << std::endl;
        return -1;
    }

    return ((WavesDataPort*)src_port_handle)->disconnect (dst_port);
}


int
WavesAudioBackend::disconnect_all (PortHandle port_handle)
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::disconnect_all ():" << std::endl;
    if (!_registered (port_handle)) {
        std::cerr << "WavesAudioBackend::disconnect_all : Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
        return -1;
    }

 ((WavesDataPort*)port_handle)->disconnect_all ();

    return 0;
}


int
WavesAudioBackend::disconnect (const std::string& src_port_name, const std::string& dst_port_name)
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::disconnect (" << src_port_name << ", " << dst_port_name << "):" << std::endl;

    WavesDataPort* src_port = _find_port (src_port_name);
    if (src_port == NULL) {
        std::cerr << "WavesAudioBackend::disconnect : Failed to find source port!\n";
        return -1;
    }

    WavesDataPort* dst_port = _find_port (dst_port_name);
    if (dst_port == NULL) {
        std::cerr << "WavesAudioBackend::disconnect : Failed to find destination port!\n";
        return -1;
    }

    return dst_port->disconnect (src_port);
}


bool
WavesAudioBackend::connected (PortHandle port_handle, bool process_callback_safe)
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::connected ():" << std::endl;
    if (!_registered (port_handle)) {
        std::cerr << "WavesAudioBackend::connected (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
        return false;
    }

    return ((WavesDataPort*)port_handle)->is_connected ();
}


bool
WavesAudioBackend::connected_to (PortHandle src_port_handle, const std::string& dst_port_name, bool process_callback_safe)
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::connected_to (" << src_port_handle << ", " << dst_port_name << ")" << std::endl;

    if (!_registered (src_port_handle)) {
        std::cerr << "WavesAudioBackend::connected_to : Failed to find source port!" << std::endl;
        return false;
    }

    WavesDataPort* dst_port = _find_port (dst_port_name);
    if (dst_port == NULL) {
        std::cerr << "WavesAudioBackend::connected_to : Failed to find destination port!" << std::endl;
        return -1;
    }
    // COMMENTED DBG LOGS */ std::cout  << "\t return " << ((((WavesDataPort*)src_port_handle)->is_connected (dst_port)) ? "YES":"NO") << ", " << dst_port_name << ")" << std::endl;
    return ((WavesDataPort*)src_port_handle)->is_connected (dst_port);
}


bool
WavesAudioBackend::physically_connected (PortHandle port_handle, bool process_callback_safe)
{
    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::physically_connected ():" << std::endl;

    if (!_registered (port_handle)) {
        std::cerr << "WavesAudioBackend::physically_connected (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
        return false;
    }

    return ((WavesDataPort*)port_handle)->is_physically_connected ();
}


int
WavesAudioBackend::get_connections (PortHandle port_handle, std::vector<std::string>& names, bool process_callback_safe)
{
   // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::get_connections ()" << std::endl;

    if (!_registered (port_handle)) {
        std::cerr << "WavesAudioBackend::get_connections (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
        return -1;
    }

    if (names.size ()) {
        std::cerr << "WavesAudioBackend::get_connections () : Parameter 'names' is not empty!\n";
        return -1;
    }

    const std::vector<WavesDataPort*>& connected_ports = ((WavesDataPort*)port_handle)->get_connections ();

    for (std::vector<WavesDataPort*>::const_iterator it = connected_ports.begin (); it != connected_ports.end (); ++it) {
        names.push_back ((*it)->name ());
    }

    return (int)names.size ();
}


int
WavesAudioBackend::request_input_monitoring (PortHandle, bool)
{
    // COMMENTED DBG LOGS */ std::cout << "WavesAudioBackend::request_input_monitoring: " << std::endl;
    return 0;
}


int
WavesAudioBackend::ensure_input_monitoring (PortHandle, bool)
{
    // COMMENTED DBG LOGS */ std::cout << "WavesAudioBackend::ensure_input_monitoring: " << std::endl;
    return 0;
}


bool
WavesAudioBackend::monitoring_input (PortHandle)
{
    // COMMENTED DBG LOGS */ std::cout << "WavesAudioBackend::monitoring_input: " << std::endl;
    return false;
}


bool
WavesAudioBackend::port_is_physical (PortHandle port_handle) const
{

    if (!_registered (port_handle)) {
        std::cerr << "WavesAudioBackend::port_is_physical (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
        return -1;
    }

    return (((WavesAudioPort*)port_handle)->flags () & IsPhysical) != 0;
}


void
WavesAudioBackend::get_physical_outputs (DataType type, std::vector<std::string>& names)
{
    names.clear();

    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::get_physical_outputs ():" << std::endl << "\tdatatype = " << type << std::endl;

    switch (type) {
        case ARDOUR::DataType::AUDIO: {
            for (std::vector<WavesAudioPort*>::iterator it = _physical_audio_outputs.begin (); it != _physical_audio_outputs.end (); ++it) {
                // COMMENTED DBG LOGS */ std::cout  << "\t" << (*it)->name () << std::endl;
                names.push_back ((*it)->name ());
            }
        } break;
        case ARDOUR::DataType::MIDI: {
            for (std::vector<WavesMidiPort*>::iterator it = _physical_midi_outputs.begin (); it != _physical_midi_outputs.end (); ++it) {
                // COMMENTED DBG LOGS */ std::cout  << "\t" << (*it)->name () << std::endl;
                names.push_back ((*it)->name ());
            }
        } break;
        default:
            break;
    }
}


void
WavesAudioBackend::get_physical_inputs (DataType type, std::vector<std::string>& names)
{
     names.clear();

    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::get_physical_inputs ():" << std::endl << "\tdatatype = " << type << std::endl;
    switch (type) {
        case ARDOUR::DataType::AUDIO: {
            for (std::vector<WavesAudioPort*>::iterator it = _physical_audio_inputs.begin (); it != _physical_audio_inputs.end (); ++it) {
                // COMMENTED DBG LOGS */ std::cout  << "\t" << (*it)->name () << std::endl;
                names.push_back ((*it)->name ());
            }
        } break;
        case ARDOUR::DataType::MIDI: {
            for (std::vector<WavesMidiPort*>::iterator it = _physical_midi_inputs.begin (); it != _physical_midi_inputs.end (); ++it) {
                // COMMENTED DBG LOGS */ std::cout  << "\t" << (*it)->name () << std::endl;
                names.push_back ((*it)->name ());
            }
        } break;
        default:
        break;
    }
}


ChanCount
WavesAudioBackend::n_physical_outputs () const
{
    ChanCount chan_count;
    chan_count.set (DataType::AUDIO, _physical_audio_outputs.size ());
    chan_count.set (DataType::MIDI, _physical_midi_outputs.size ());

    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::n_physical_outputs ():" << std::endl << "\ttotal = " << chan_count.n_total () << std::endl;

    return chan_count;
}


ChanCount
WavesAudioBackend::n_physical_inputs () const
{
    ChanCount chan_count;
    chan_count.set (DataType::AUDIO, _physical_audio_inputs.size ());
    chan_count.set (DataType::MIDI, _physical_midi_inputs.size ());

    // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::n_physical_outputs ():" << std::endl << "\ttotal = " << chan_count.n_total () << std::endl;

    return chan_count;
}


void*
WavesAudioBackend::get_buffer (PortHandle port_handle, pframes_t nframes)
{
    // Here we would check if the port is registered. However, we will not do it as
    // it's relatively VERY SLOW operation. So let's count on consistency
    // of the caller as get_buffer normally is called hundreds of "kilotimes" per second.

    if (port_handle == NULL) {
        std::cerr << "WavesAudioBackend::get_buffer : Invalid port handler <NULL>!" << std::endl;
        return NULL;
    }

    return ((WavesAudioPort*)port_handle)->get_buffer (nframes);
}


int
WavesAudioBackend::_register_system_audio_ports ()
{
    if (!_device) {
        std::cerr << "WavesAudioBackend::_register_system_audio_ports (): No device is set!" << std::endl;
        return -1;
    }

    std::vector<std::string> input_channels = _device->InputChannels ();
    _max_input_channels = input_channels.size ();

    uint32_t channels = (_input_channels ? _input_channels : input_channels.size ());
    uint32_t port_number = 0;

    LatencyRange lr = {0,0};

    // Get latency for capture
    lr.min = lr.max = _device->GetLatency (false) + _device->CurrentBufferSize () + _systemic_input_latency;
    for (std::vector<std::string>::iterator it = input_channels.begin ();
         (port_number < channels) && (it != input_channels.end ());
        ++it) {

        std::ostringstream port_name(*it);
        WavesDataPort* port = _register_port ("system:capture:" + port_name.str (), DataType::AUDIO ,
                                              static_cast<PortFlags> (IsOutput | IsPhysical | IsTerminal));

        if (port == NULL) {
            std::cerr << "WavesAudioBackend::_create_system_audio_ports (): Failed registering port [" << port_name << "] for [" << _device->DeviceName () << "]" << std::endl;
            return-1;
        }
        set_latency_range (port, false, lr);
    }

    std::vector<std::string> output_channels = _device->OutputChannels ();
    _max_output_channels = output_channels.size ();
    channels = (_output_channels ? _output_channels : _max_output_channels);
    port_number = 0;

    // Get latency for playback
    lr.min = lr.max = _device->GetLatency (true) + _device->CurrentBufferSize () + _systemic_output_latency;

    for (std::vector<std::string>::iterator it = output_channels.begin ();
         (port_number < channels) && (it != output_channels.end ());
        ++it) {

        std::ostringstream port_name(*it);
        WavesDataPort* port = _register_port ("system:playback:" + port_name.str (), DataType::AUDIO ,
                                              static_cast<PortFlags> (IsInput| IsPhysical | IsTerminal));

        if (port == NULL) {
            std::cerr << "WavesAudioBackend::_create_system_audio_ports (): Failed registering port ]" << port_name << "] for [" << _device->DeviceName () << "]" << std::endl;
            return-1;
        }
        set_latency_range (port, true, lr);
    }

    return 0;
}


void
WavesAudioBackend::_unregister_system_audio_ports ()
{
    std::vector<WavesAudioPort*> physical_audio_ports = _physical_audio_inputs;
    physical_audio_ports.insert (physical_audio_ports.begin (), _physical_audio_outputs.begin (), _physical_audio_outputs.end ());

    for (std::vector<WavesAudioPort*>::const_iterator it = physical_audio_ports.begin (); it != physical_audio_ports.end (); ++it) {
        std::vector<WavesDataPort*>::iterator port_iterator = std::find (_ports.begin (), _ports.end (), *it);
        if (port_iterator == _ports.end ()) {
            std::cerr << "WavesAudioBackend::_unregister_system_audio_ports (): Failed to find port [" << (*it)->name () << "]!"  << std::endl;
        }
        else {
            _ports.erase (port_iterator);
        }
        delete *it;
    }

    _physical_audio_inputs.clear ();
    _physical_audio_outputs.clear ();
}