update wavesaudio backend, now supports Windows (ASIO) as well as OS X (CoreAudio)
[ardour.git] / libs / backends / wavesaudio / waves_audiobackend.port_engine.cc
1 /*
2     Copyright (C) 2013 Valeriy Kamyshniy
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "waves_audiobackend.h"
21 #include "waves_audioport.h"
22 #include "waves_midiport.h"
23 #include "waves_midi_event.h"
24
25 using namespace ARDOUR;
26
27 uint32_t
28 WavesAudioBackend::port_name_size () const
29 {
30     return 256+64;
31 }
32
33 int
34 WavesAudioBackend::set_port_name (PortHandle port_handle, const std::string& port_name)
35 {
36     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::set_port_name (): [" << std::hex << port_handle << std::dec << "], [" << port_name << "]" << std::endl;
37     
38     if (!_registered (port_handle)) {
39         std::cerr << "WavesAudioBackend::set_port_name (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
40         return -1;
41     }
42
43     return ((WavesAudioPort*)port_handle)->set_name (__instantiated_name + ":" + port_name);
44 }
45
46
47 std::string
48 WavesAudioBackend::get_port_name (PortHandle port_handle) const
49 {
50     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::get_port_name (): [" << std::hex << port_handle << std::dec << "]" << std::endl;
51     if (!_registered (port_handle)) {
52         std::cerr << "WavesAudioBackend::get_port_name (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
53         return std::string ();
54     }
55     // COMMENTED DBG LOGS */ else std::cout  << "\t[" << ((WavesAudioPort*)port_handle)->name () << "]" << std::endl;
56
57     return ((WavesAudioPort*)port_handle)->name ();
58 }
59
60
61 PortEngine::PortHandle
62 WavesAudioBackend::get_port_by_name (const std::string& port_name) const
63 {
64     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::get_port_by_name (): [" << port_name << "]" << std::endl;
65
66     PortHandle port_handle = (PortHandle)_find_port (port_name);
67     if (!port_handle) {
68         std::cerr << "WavesAudioBackend::get_port_by_name (): Failed to find port [" << port_name << "]!" << std::endl;
69     }
70
71     return port_handle;
72 }
73
74
75 WavesDataPort* 
76 WavesAudioBackend::_find_port (const std::string& port_name) const
77 {
78     for (std::vector<WavesDataPort*>::const_iterator it = _ports.begin (); it != _ports.end (); ++it) {
79         if ((*it)->name () == port_name) {
80             return *it;
81         }
82     }
83
84     return NULL;
85 }
86
87
88 int
89 WavesAudioBackend::get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector<std::string>& port_names) const
90 {
91   
92     // COMMENTED DBG LOGS */ std::cout << "WavesAudioBackend::get_ports (): \n\tPattern: [" << port_name_pattern << "]\n\tType: " << type << "\n\tFlags: " << flags << endl;
93     
94     unsigned found_ports =0;
95     
96     for (size_t i = 0; i < _ports.size (); ++i) {
97         WavesDataPort* port = _ports[i];
98         
99         if ((port->type () == type) && (port->flags () & flags)) {
100             port_names.push_back (port->name ());
101             found_ports++;
102         }
103     }
104     return found_ports;
105 }
106
107
108 DataType
109 WavesAudioBackend::port_data_type (PortHandle port_handle) const
110 {
111     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::port_data_type" << std::endl;
112
113     if (!_registered (port_handle)) {
114         std::cerr << "WavesAudioBackend::port_data_type (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
115         return DataType::NIL;
116     }
117     
118     // COMMENTED DBG LOGS */ std::cout << "WavesAudioBackend::port_data_type: " << endl;
119     
120     return ((WavesAudioPort*)port_handle)->type ();
121 }
122
123
124 PortEngine::PortHandle
125 WavesAudioBackend::register_port (const std::string& shortname, ARDOUR::DataType type, ARDOUR::PortFlags flags)
126 {
127     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::register_port (): " << type.to_string () << " [" << shortname << "]" << std::endl;
128
129     if (shortname.size () == 0) {
130         std::cerr << "WavesAudioBackend::register_port (): Invalid (empty) port name!" << std::endl;
131         return NULL;
132     }
133
134     if (flags & IsPhysical) {
135         std::cerr << "WavesAudioBackend::register_port (): Unexpected attribute for port [" << shortname << "]! The port must not be physical!";
136         return NULL;
137     }
138
139     return (PortEngine::PortHandle)_register_port (__instantiated_name + ":" + shortname, type, flags);
140 }
141
142
143 WavesDataPort*
144 WavesAudioBackend::_register_port (const std::string& port_name, ARDOUR::DataType type, ARDOUR::PortFlags flags)
145 {
146     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::_register_port (): [" << port_name << "]" << std::endl;
147
148     if (_find_port (port_name) != NULL) {
149         std::cerr << "WavesAudioBackend::register_port () : Port [" << port_name << "] is already registered!" << std::endl;
150         return NULL;
151     }
152
153     WavesDataPort* port = NULL;
154     switch (type) {
155         case ARDOUR::DataType::AUDIO: {
156             WavesAudioPort* audio_port = new WavesAudioPort (port_name, flags);
157             if (flags & IsPhysical)
158             {
159                 if (flags & IsOutput)
160                 {
161                     _physical_audio_inputs.push_back (audio_port);
162                     // COMMENTED DBG LOGS */ std::cout  << "\t\t" << port_name << " added to physical AUDIO Inputs !" << std::endl;
163                 }
164                 else if (flags & IsInput)
165                 {
166                     _physical_audio_outputs.push_back (audio_port);
167                     // COMMENTED DBG LOGS */ std::cout  << "\t\t" << port_name << " added to physical AUDIO Outputs !" << std::endl;
168                 }
169             }
170             port = audio_port;
171         } break;
172         case ARDOUR::DataType::MIDI: {
173             WavesMidiPort* midi_port = new WavesMidiPort (port_name, flags);
174             if (flags & IsPhysical)
175             {
176                 if (flags & IsOutput)
177                 {
178                     _physical_midi_inputs.push_back (midi_port);
179                     // COMMENTED DBG LOGS */ std::cout  << "\t\t" << port_name << " added to physical MIDI Inputs !" << std::endl;
180                 }
181                 else if (flags & IsInput)
182                 {
183                     _physical_midi_outputs.push_back (midi_port);
184                     // COMMENTED DBG LOGS */ std::cout  << "\t\t" << port_name << " added to physical MIDI Outputs !" << std::endl;
185                 }
186             }
187             port = midi_port;
188         } break;
189         default:
190             std::cerr << "WavesAudioBackend::register_port () : Invalid data type (" << (uint32_t)type << ") applied to port [" << port_name << "]!" << std::endl;
191         return NULL;
192     }
193     
194     _ports.push_back (port);
195
196     return port;
197 }
198
199
200 void
201 WavesAudioBackend::unregister_port (PortHandle port_handle)
202 {
203     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::unregister_port ():" << std::hex << port_handle << std::dec << std::endl;
204
205     // so far we suppose all disconnections will be done prior to unregistering.
206     WavesDataPort* port = (WavesDataPort*)port_handle;
207     std::vector<WavesDataPort*>::iterator port_iterator = std::find (_ports.begin (), _ports.end (), (WavesDataPort*)port_handle);
208     if (port_iterator == _ports.end ()) {
209         std::cerr << "WavesAudioBackend::unregister_port (): Failed to find port [" << std::hex << port_handle << std::dec << "]!"  << std::endl;
210         return;
211     }
212     // COMMENTED DBG LOGS */ std::cout  << "\t[" << ((WavesDataPort*)port_handle)->name () << "]" << std::endl;
213
214     _ports.erase (port_iterator);
215
216     if (port->is_physical ()) {
217         if (port->is_output ()) {
218             switch (port->type ()) {
219                 case ARDOUR::DataType::AUDIO: {
220                     std::vector<WavesAudioPort*>::iterator audio_port_iterator = std::find (_physical_audio_inputs.begin (), _physical_audio_inputs.end (), port);
221                     if (audio_port_iterator == _physical_audio_inputs.end ())    {
222                         std::cerr << "WavesAudioBackend::unregister_port (): Failed to find port [" << port->name () << "] in the list of registered physical audio inputs!" << std::endl;
223                         return;
224                     }
225                     _physical_audio_inputs.erase (audio_port_iterator);
226                 }
227                 break;
228                 case ARDOUR::DataType::MIDI: {
229                     std::vector<WavesMidiPort*>::iterator midi_port_iterator = std::find (_physical_midi_inputs.begin (), _physical_midi_inputs.end (), port);
230                     if (midi_port_iterator == _physical_midi_inputs.end ()) {
231                         std::cerr << "WavesAudioBackend::unregister_port (): Failed to find port [" << port->name () << "] in the list of registered physical midi inputs!" << std::endl;
232                         return;
233                     }
234                     _physical_midi_inputs.erase (midi_port_iterator);
235                 }
236                 break;
237                 default:
238                     std::cerr << "WavesAudioBackend::unregister_port (): Invalid type (" << port->type () << " applied to [" << port->name () << "]!" << std::endl;
239                 break;
240             }
241         }
242         else if (port->flags () & IsInput) {
243             switch (port->type ()) {
244                 case ARDOUR::DataType::AUDIO: {
245                     std::vector<WavesAudioPort*>::iterator audio_port_iterator = std::find (_physical_audio_outputs.begin (), _physical_audio_outputs.end (), port);
246                     if (audio_port_iterator == _physical_audio_outputs.end ())
247                     {
248                         std::cerr << "WavesAudioBackend::unregister_port: Failed to find port [" << port->name () << std::dec << "] in the list of registered physical audio outputs!\n";
249                         return;
250                     }
251                     _physical_audio_outputs.erase (audio_port_iterator);
252                 }
253                 break;
254                 case ARDOUR::DataType::MIDI: {
255
256                     std::vector<WavesMidiPort*>::iterator midi_port_iterator = std::find (_physical_midi_outputs.begin (), _physical_midi_outputs.end (), port);
257                     if (midi_port_iterator == _physical_midi_outputs.end ())
258                     {
259                         std::cerr << "WavesAudioBackend::unregister_port: Failed to find port [" << port->name () << std::dec << "] in the list of registered physical midi outputs!\n";
260                         return;
261                     }
262                     _physical_midi_outputs.erase (midi_port_iterator);
263                 }
264                 break;
265                 default:
266                     std::cerr << "WavesAudioBackend::unregister_port (): Invalid type (" << port->type () << " applied to [" << port->name () << "]!" << std::endl;
267                 break;
268             }
269         }
270     }
271
272     delete port;
273 }
274
275
276 int
277 WavesAudioBackend::connect (const std::string& src_port_name, const std::string& dst_port_name)
278 {
279     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::connect (" << src_port_name << ", " << dst_port_name << "):" << std::endl;
280
281     WavesDataPort* src_port = _find_port (src_port_name);
282     if (src_port == NULL) {
283         std::cerr << "WavesAudioBackend::connect: Failed to find source port " << src_port_name << " !" << std::endl;
284         return -1;
285     }
286     
287     WavesDataPort* dst_port = _find_port (dst_port_name);
288     if (dst_port == NULL) {
289         std::cerr << "WavesAudioBackend::connect: Failed to find destination port " << dst_port_name << " !" << std::endl;
290         return -1;
291     }
292
293     // COMMENTED DBG LOGS */ std::cout  << "\t\t (" << src_port << ", " << dst_port << "):" << std::endl;
294     return src_port->connect (dst_port);
295 }
296
297
298 int
299 WavesAudioBackend::connect (PortHandle src_port_handle, const std::string& dst_port_name)
300 {
301     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::connect ():" << std::endl;
302     if (!_registered (src_port_handle)) {
303         std::cerr << "WavesAudioBackend::connect: Failed to find source port [" << std::hex << src_port_handle << std::dec << "]!" << std::endl;
304         return -1;
305     }
306
307     // COMMENTED DBG LOGS */ std::cout  << "\t[" << std::hex << src_port_handle << std::dec << "]" << std::endl;
308     // COMMENTED DBG LOGS */ std::cout  << "\t[" << dst_port_name << "]" << std::endl;
309
310     WavesDataPort* dst_port = _find_port (dst_port_name);
311     if (dst_port == NULL) {
312         std::cerr << "WavesAudioBackend::connect (): Failed to find destination port [" << dst_port_name << "]!" << std::endl;
313         return -1;
314     }
315
316     return ((WavesDataPort*)src_port_handle)->connect (dst_port);
317 }
318
319
320 int
321 WavesAudioBackend::disconnect (PortHandle src_port_handle, const std::string& dst_port_name)
322 {
323     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::disconnect (" << src_port_handle << ", " << dst_port_name << "):" << std::endl;
324     if (!_registered (src_port_handle)) {
325         std::cerr << "WavesAudioBackend::disconnect (): Failed to find source port [" << std::hex << src_port_handle << std::dec << "]!" << std::endl;
326         return -1;
327     }
328     
329     // COMMENTED DBG LOGS */ std::cout  << "\t[" << std::hex << src_port_handle << std::dec << "]" << std::endl;
330     // COMMENTED DBG LOGS */ std::cout  << "\t[" << dst_port_name << "]" << std::endl;
331
332     WavesDataPort* dst_port = _find_port (dst_port_name);
333     if (dst_port == NULL) {
334         std::cerr << "WavesAudioBackend::disconnect (): Failed to find destination port [" << dst_port_name << "]!" << std::endl;
335         return -1;
336     }
337
338     return ((WavesDataPort*)src_port_handle)->disconnect (dst_port);
339 }
340
341
342 int
343 WavesAudioBackend::disconnect_all (PortHandle port_handle)
344 {
345     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::disconnect_all ():" << std::endl;
346     if (!_registered (port_handle)) {
347         std::cerr << "WavesAudioBackend::disconnect_all : Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
348         return -1;
349     }
350
351  ((WavesDataPort*)port_handle)->disconnect_all ();
352
353     return 0;
354 }
355
356
357 int
358 WavesAudioBackend::disconnect (const std::string& src_port_name, const std::string& dst_port_name)
359 {
360     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::disconnect (" << src_port_name << ", " << dst_port_name << "):" << std::endl;
361
362     WavesDataPort* src_port = _find_port (src_port_name);
363     if (src_port == NULL) {
364         std::cerr << "WavesAudioBackend::disconnect : Failed to find source port!\n";
365         return -1;
366     }
367     
368     WavesDataPort* dst_port = _find_port (dst_port_name);
369     if (dst_port == NULL) {
370         std::cerr << "WavesAudioBackend::disconnect : Failed to find destination port!\n";
371         return -1;
372     }
373
374     return dst_port->disconnect (src_port);
375 }
376
377
378 bool
379 WavesAudioBackend::connected (PortHandle port_handle, bool process_callback_safe)
380 {
381     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::connected ():" << std::endl;
382     if (!_registered (port_handle)) {
383         std::cerr << "WavesAudioBackend::connected (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
384         return false;
385     }
386     
387     return ((WavesDataPort*)port_handle)->is_connected ();
388 }
389
390
391 bool
392 WavesAudioBackend::connected_to (PortHandle src_port_handle, const std::string& dst_port_name, bool process_callback_safe)
393 {
394     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::connected_to (" << src_port_handle << ", " << dst_port_name << ")" << std::endl;
395
396     if (!_registered (src_port_handle)) {
397         std::cerr << "WavesAudioBackend::connected_to : Failed to find source port!" << std::endl;
398         return false;
399     }
400
401     WavesDataPort* dst_port = _find_port (dst_port_name);
402     if (dst_port == NULL) {
403         std::cerr << "WavesAudioBackend::connected_to : Failed to find destination port!" << std::endl;
404         return -1;
405     }
406     // COMMENTED DBG LOGS */ std::cout  << "\t return " << ((((WavesDataPort*)src_port_handle)->is_connected (dst_port)) ? "YES":"NO") << ", " << dst_port_name << ")" << std::endl;
407     return ((WavesDataPort*)src_port_handle)->is_connected (dst_port);
408 }
409
410
411 bool
412 WavesAudioBackend::physically_connected (PortHandle port_handle, bool process_callback_safe)
413 {
414     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::physically_connected ():" << std::endl;
415
416     if (!_registered (port_handle)) {
417         std::cerr << "WavesAudioBackend::physically_connected (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
418         return false;
419     }
420
421     return ((WavesDataPort*)port_handle)->is_physically_connected ();
422 }
423
424
425 int
426 WavesAudioBackend::get_connections (PortHandle port_handle, std::vector<std::string>& names, bool process_callback_safe)
427 {
428     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::get_connections ()" << std::endl;
429     
430     if (!_registered (port_handle)) {
431         std::cerr << "WavesAudioBackend::get_connections (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
432         return -1;
433     }
434
435     if (names.size ()) {
436         std::cerr << "WavesAudioBackend::get_connections () : Parameter 'names' is not empty!\n";
437         return -1;
438     }
439  
440     const std::vector<WavesDataPort*>& connected_ports = ((WavesDataPort*)port_handle)->get_connections ();
441
442     for (std::vector<WavesDataPort*>::const_iterator it = connected_ports.begin (); it != connected_ports.end (); ++it) {
443         names.push_back ((*it)->name ());
444     }
445
446     return (int)names.size ();
447 }
448
449
450 int
451 WavesAudioBackend::request_input_monitoring (PortHandle, bool)
452 {
453     // COMMENTED DBG LOGS */ std::cout << "WavesAudioBackend::request_input_monitoring: " << std::endl;
454     return 0;
455 }
456
457
458 int
459 WavesAudioBackend::ensure_input_monitoring (PortHandle, bool)
460 {
461     // COMMENTED DBG LOGS */ std::cout << "WavesAudioBackend::ensure_input_monitoring: " << std::endl;
462     return 0;
463 }
464
465
466 bool
467 WavesAudioBackend::monitoring_input (PortHandle)
468 {
469     // COMMENTED DBG LOGS */ std::cout << "WavesAudioBackend::monitoring_input: " << std::endl;
470     return false;
471 }
472
473
474 bool
475 WavesAudioBackend::port_is_physical (PortHandle port_handle) const
476 {
477     
478     if (!_registered (port_handle)) {
479         std::cerr << "WavesAudioBackend::port_is_physical (): Failed to find port [" << std::hex << port_handle << std::dec << "]!" << std::endl;
480         return -1;
481     }
482     
483     return (((WavesAudioPort*)port_handle)->flags () & IsPhysical) != 0;
484 }
485
486
487 void
488 WavesAudioBackend::get_physical_outputs (DataType type, std::vector<std::string>& names)
489 {
490     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::get_physical_outputs ():" << std::endl << "\tdatatype = " << type << std::endl;
491
492     switch (type) {
493         case ARDOUR::DataType::AUDIO: {
494             for (std::vector<WavesAudioPort*>::iterator it = _physical_audio_outputs.begin (); it != _physical_audio_outputs.end (); ++it) {
495                 // COMMENTED DBG LOGS */ std::cout  << "\t" << (*it)->name () << std::endl;
496                 names.push_back ((*it)->name ());
497             }
498         } break;
499         case ARDOUR::DataType::MIDI: {
500             for (std::vector<WavesMidiPort*>::iterator it = _physical_midi_outputs.begin (); it != _physical_midi_outputs.end (); ++it) {
501                 // COMMENTED DBG LOGS */ std::cout  << "\t" << (*it)->name () << std::endl;
502                 names.push_back ((*it)->name ());
503             }
504         } break;
505         default:
506             break;
507     }
508 }
509
510
511 void
512 WavesAudioBackend::get_physical_inputs (DataType type, std::vector<std::string>& names)
513 {
514     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::get_physical_inputs ():" << std::endl << "\tdatatype = " << type << std::endl;
515     switch (type) {
516         case ARDOUR::DataType::AUDIO: {
517             for (std::vector<WavesAudioPort*>::iterator it = _physical_audio_inputs.begin (); it != _physical_audio_inputs.end (); ++it) {
518                 // COMMENTED DBG LOGS */ std::cout  << "\t" << (*it)->name () << std::endl;
519                 names.push_back ((*it)->name ());
520             }
521         } break;
522         case ARDOUR::DataType::MIDI: {
523             for (std::vector<WavesMidiPort*>::iterator it = _physical_midi_inputs.begin (); it != _physical_midi_inputs.end (); ++it) {
524                 // COMMENTED DBG LOGS */ std::cout  << "\t" << (*it)->name () << std::endl;
525                 names.push_back ((*it)->name ());
526             }
527         } break;
528         default:
529         break;
530     }
531 }
532
533
534 ChanCount
535 WavesAudioBackend::n_physical_outputs () const
536 {
537     ChanCount chan_count;
538     chan_count.set (DataType::AUDIO, _physical_audio_outputs.size ());
539     chan_count.set (DataType::MIDI, _physical_midi_outputs.size ());
540
541     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::n_physical_outputs ():" << std::endl << "\ttotal = " << chan_count.n_total () << std::endl;
542
543     return chan_count;
544 }
545
546
547 ChanCount
548 WavesAudioBackend::n_physical_inputs () const
549 {
550     ChanCount chan_count;
551     chan_count.set (DataType::AUDIO, _physical_audio_inputs.size ());
552     chan_count.set (DataType::MIDI, _physical_midi_inputs.size ());
553
554     // COMMENTED DBG LOGS */ std::cout  << "WavesAudioBackend::n_physical_outputs ():" << std::endl << "\ttotal = " << chan_count.n_total () << std::endl;
555
556     return chan_count;
557 }
558
559
560 void*
561 WavesAudioBackend::get_buffer (PortHandle port_handle, pframes_t nframes)
562 {
563     // Here we would check if the port is registered. However, we will not do it as
564     // it's relatively VERY SLOW operation. So let's count on consistency
565     // of the caller as get_buffer normally is called hundreds of "kilotimes" per second.
566
567     if (port_handle == NULL) {
568         std::cerr << "WavesAudioBackend::get_buffer : Invalid port handler <NULL>!" << std::endl;
569         return NULL;
570     }  
571     
572     return ((WavesAudioPort*)port_handle)->get_buffer (nframes);
573 }
574
575
576 int
577 WavesAudioBackend::_register_system_audio_ports ()
578 {
579     if (!_device) {
580         std::cerr << "WavesAudioBackend::_register_system_audio_ports (): No device is set!" << std::endl;
581         return -1;
582     }
583     
584     std::vector<std::string> input_channels = _device->InputChannels ();
585     _max_input_channels = input_channels.size ();
586     
587     uint32_t channels = (_input_channels ? _input_channels : input_channels.size ());
588     uint32_t port_number = 0;
589
590     LatencyRange lr = {0,0};
591
592     // Get latency for capture
593     lr.min = lr.max = _device->GetLatency (false) + _device->CurrentBufferSize () + _systemic_input_latency;
594     for (std::vector<std::string>::iterator it = input_channels.begin (); 
595          (port_number < channels) && (it != input_channels.end ());
596         ++it) {
597         std::ostringstream port_name;
598         port_name << "capture_" << ++port_number;
599
600         WavesDataPort* port = _register_port ("system:" + port_name.str (), DataType::AUDIO , static_cast<PortFlags> (IsOutput | IsPhysical | IsTerminal));
601         if (port == NULL) {
602             std::cerr << "WavesAudioBackend::_create_system_audio_ports (): Failed registering port [" << port_name << "] for [" << _device->DeviceName () << "]" << std::endl;
603             return-1;
604         }
605         set_latency_range (port, false, lr);
606     }
607     
608     std::vector<std::string> output_channels = _device->OutputChannels ();
609     _max_output_channels = output_channels.size ();
610     channels = (_output_channels ? _output_channels : _max_output_channels);
611     port_number = 0;
612     
613     // Get latency for playback
614     lr.min = lr.max = _device->GetLatency (true) + _device->CurrentBufferSize () + _systemic_output_latency;
615
616     for (std::vector<std::string>::iterator it = output_channels.begin ();
617          (port_number < channels) && (it != output_channels.end ());
618         ++it) {
619         std::ostringstream port_name;
620         port_name << "playback_" << ++port_number;
621         WavesDataPort* port = _register_port ("system:" + port_name.str (), DataType::AUDIO , static_cast<PortFlags> (IsInput| IsPhysical | IsTerminal));
622         if (port == NULL) {
623             std::cerr << "WavesAudioBackend::_create_system_audio_ports (): Failed registering port ]" << port_name << "] for [" << _device->DeviceName () << "]" << std::endl;
624             return-1;
625         }
626         set_latency_range (port, true, lr);
627     }
628     
629     return 0;
630 }
631
632
633 void
634 WavesAudioBackend::_unregister_system_audio_ports ()
635 {
636     std::vector<WavesAudioPort*> physical_audio_ports = _physical_audio_inputs;
637     physical_audio_ports.insert (physical_audio_ports.begin (), _physical_audio_outputs.begin (), _physical_audio_outputs.end ());
638         
639     for (std::vector<WavesAudioPort*>::const_iterator it = physical_audio_ports.begin (); it != physical_audio_ports.end (); ++it) {
640         std::vector<WavesDataPort*>::iterator port_iterator = std::find (_ports.begin (), _ports.end (), *it);
641         if (port_iterator == _ports.end ()) {
642             std::cerr << "WavesAudioBackend::_unregister_system_audio_ports (): Failed to find port [" << (*it)->name () << "]!"  << std::endl;
643         }
644         else {
645             _ports.erase (port_iterator);
646         }
647         delete *it;
648     }
649
650     _physical_audio_inputs.clear ();
651     _physical_audio_outputs.clear ();
652 }
653
654