DummyAudioBackend: port-engine, midi-buffers and the rest of it
[ardour.git] / libs / backends / dummy / dummy_audiobackend.cc
1 /*
2  * Copyright (C) 2014 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2013 Paul Davis
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include <sys/time.h>
21 #include <regex.h>
22
23 #include "dummy_audiobackend.h"
24 #include "pbd/error.h"
25 #include "i18n.h"
26
27 using namespace ARDOUR;
28
29 static std::string s_instance_name;
30 size_t DummyAudioBackend::_max_buffer_size = 8192;
31
32 DummyAudioBackend::DummyAudioBackend (AudioEngine& e)
33         : AudioBackend (e)
34         , _running (false)
35         , _freewheeling (false)
36         , _samplerate (48000)
37         , _audio_buffersize (1024)
38         , _dsp_load (0)
39         , _n_inputs (0)
40         , _n_outputs (0)
41         , _systemic_input_latency (0)
42         , _systemic_output_latency (0)
43         , _processed_samples (0)
44 {
45         _instance_name = s_instance_name;
46 }
47
48 DummyAudioBackend::~DummyAudioBackend ()
49 {
50 }
51
52 /* AUDIOBACKEND API */
53
54 std::string
55 DummyAudioBackend::name () const
56 {
57         return X_("Dummy");
58 }
59
60 bool
61 DummyAudioBackend::is_realtime () const
62 {
63         return false;
64 }
65
66 std::vector<AudioBackend::DeviceStatus>
67 DummyAudioBackend::enumerate_devices () const
68 {
69         std::vector<AudioBackend::DeviceStatus> s;
70         s.push_back (DeviceStatus (_("Dummy"), true));
71         return s;
72 }
73
74 std::vector<float>
75 DummyAudioBackend::available_sample_rates (const std::string&) const
76 {
77         std::vector<float> sr;
78         sr.push_back (8000.0);
79         sr.push_back (22050.0);
80         sr.push_back (24000.0);
81         sr.push_back (44100.0);
82         sr.push_back (48000.0);
83         sr.push_back (88200.0);
84         sr.push_back (96000.0);
85         sr.push_back (176400.0);
86         sr.push_back (192000.0);
87         return sr;
88 }
89
90 std::vector<uint32_t>
91 DummyAudioBackend::available_buffer_sizes (const std::string&) const
92 {
93         std::vector<uint32_t> bs;
94         bs.push_back (4);
95         bs.push_back (8);
96         bs.push_back (16);
97         bs.push_back (32);
98         bs.push_back (64);
99         bs.push_back (128);
100         bs.push_back (256);
101         bs.push_back (512);
102         bs.push_back (1024);
103         bs.push_back (2048);
104         bs.push_back (4096);
105         bs.push_back (8192);
106         return bs;
107 }
108
109 uint32_t
110 DummyAudioBackend::available_input_channel_count (const std::string&) const
111 {
112         return 128;
113 }
114
115 uint32_t
116 DummyAudioBackend::available_output_channel_count (const std::string&) const
117 {
118         return 128;
119 }
120
121 bool
122 DummyAudioBackend::can_change_sample_rate_when_running () const
123 {
124         return true;
125 }
126
127 bool
128 DummyAudioBackend::can_change_buffer_size_when_running () const
129 {
130         return true;
131 }
132
133 int
134 DummyAudioBackend::set_device_name (const std::string&)
135 {
136         return 0;
137 }
138
139 int
140 DummyAudioBackend::set_sample_rate (float sr)
141 {
142         if (sr <= 0) { return -1; }
143         _samplerate = sr;
144         engine.sample_rate_change (sr);
145         return 0;
146 }
147
148 int
149 DummyAudioBackend::set_buffer_size (uint32_t bs)
150 {
151         if (bs <= 0 || bs >= _max_buffer_size) {
152                 return -1;
153         }
154         _audio_buffersize = bs;
155         engine.buffer_size_change (bs);
156         return 0;
157 }
158
159 int
160 DummyAudioBackend::set_interleaved (bool yn)
161 {
162         if (!yn) { return 0; }
163         return -1;
164 }
165
166 int
167 DummyAudioBackend::set_input_channels (uint32_t cc)
168 {
169         _n_inputs = cc;
170         return 0;
171 }
172
173 int
174 DummyAudioBackend::set_output_channels (uint32_t cc)
175 {
176         _n_outputs = cc;
177         return 0;
178 }
179
180 int
181 DummyAudioBackend::set_systemic_input_latency (uint32_t sl)
182 {
183         _systemic_input_latency = sl;
184         return 0;
185 }
186
187 int
188 DummyAudioBackend::set_systemic_output_latency (uint32_t sl)
189 {
190         _systemic_output_latency = sl;
191         return 0;
192 }
193
194 /* Retrieving parameters */
195 std::string
196 DummyAudioBackend::device_name () const
197 {
198         return _("Dummy Device");
199 }
200
201 float
202 DummyAudioBackend::sample_rate () const
203 {
204         return _samplerate;
205 }
206
207 uint32_t
208 DummyAudioBackend::buffer_size () const
209 {
210         return _audio_buffersize;
211 }
212
213 bool
214 DummyAudioBackend::interleaved () const
215 {
216         return false;
217 }
218
219 uint32_t
220 DummyAudioBackend::input_channels () const
221 {
222         return _n_inputs;
223 }
224
225 uint32_t
226 DummyAudioBackend::output_channels () const
227 {
228         return _n_outputs;
229 }
230
231 uint32_t
232 DummyAudioBackend::systemic_input_latency () const
233 {
234         return _systemic_input_latency;
235 }
236
237 uint32_t
238 DummyAudioBackend::systemic_output_latency () const
239 {
240         return _systemic_output_latency;
241 }
242
243 /* MIDI */
244 std::vector<std::string>
245 DummyAudioBackend::enumerate_midi_options () const
246 {
247         std::vector<std::string> m;
248         m.push_back (_("None"));
249         return m;
250 }
251
252 int
253 DummyAudioBackend::set_midi_option (const std::string&)
254 {
255         return -1;
256 }
257
258 std::string
259 DummyAudioBackend::midi_option () const
260 {
261         return "";
262 }
263
264 /* State Control */
265
266 static void * pthread_process (void *arg)
267 {
268         DummyAudioBackend *d = static_cast<DummyAudioBackend *>(arg);
269         d->main_process_thread ();
270         pthread_exit (0);
271         return 0;
272 }
273
274 int
275 DummyAudioBackend::_start (bool /*for_latency_measurement*/)
276 {
277         if (_running) {
278                 PBD::error << _("DummyAudioBackend: already active.") << endmsg;
279                 return -1;
280         }
281         if (pthread_create (&_main_thread, NULL, pthread_process, this)) {
282                 PBD::error << _("DummyAudioBackend: cannot start.") << endmsg;
283         }
284
285         int timeout = 5000;
286         while (!_running && --timeout > 0) { usleep (1000); }
287
288         if (timeout == 0 || !_running) {
289                 PBD::error << _("DummyAudioBackend: failed to start process thread.") << endmsg;
290                 return -1;
291         }
292
293         if (engine.reestablish_ports ()) {
294                 PBD::error << _("DummyAudioBackend: Could not re-establish ports.") << endmsg;
295                 stop ();
296                 return -1;
297         }
298
299         engine.reconnect_ports ();
300         return 0;
301 }
302
303 int
304 DummyAudioBackend::stop ()
305 {
306         void *status;
307         if (!_running) {
308                 return -1;
309         }
310
311         _running = false;
312         if (pthread_join (_main_thread, &status)) {
313                 PBD::error << _("DummyAudioBackend: failed to terminate.") << endmsg;
314                 return -1;
315         }
316         return 0;
317 }
318
319 int
320 DummyAudioBackend::freewheel (bool onoff)
321 {
322         if (onoff != _freewheeling) {
323                 return 0;
324         }
325         _freewheeling = onoff;
326         engine.freewheel_callback (onoff);
327         return 0;
328 }
329
330 float
331 DummyAudioBackend::dsp_load () const
332 {
333         return 100.f * _dsp_load;
334 }
335
336 size_t
337 DummyAudioBackend::raw_buffer_size (DataType t)
338 {
339         switch (t) {
340                 case DataType::AUDIO:
341                         return _max_buffer_size * sizeof(Sample);
342                 case DataType::MIDI:
343                         return _max_buffer_size; // XXX not really limited
344         }
345         return 0;
346 }
347
348 /* Process time */
349 pframes_t
350 DummyAudioBackend::sample_time ()
351 {
352         return _processed_samples;
353 }
354
355 pframes_t
356 DummyAudioBackend::sample_time_at_cycle_start ()
357 {
358         return _processed_samples;
359 }
360
361 pframes_t
362 DummyAudioBackend::samples_since_cycle_start ()
363 {
364         return 0;
365 }
366
367
368 void *
369 DummyAudioBackend::dummy_process_thread (void *arg)
370 {
371         ThreadData* td = reinterpret_cast<ThreadData*> (arg);
372         boost::function<void ()> f = td->f;
373         delete td;
374         f ();
375         return 0;
376 }
377
378 int
379 DummyAudioBackend::create_process_thread (boost::function<void()> func)
380 {
381         pthread_t thread_id;
382         pthread_attr_t attr;
383         size_t stacksize = 100000;
384
385         pthread_attr_init (&attr);
386         pthread_attr_setstacksize (&attr, stacksize);
387         ThreadData* td = new ThreadData (this, func, stacksize);
388
389         if (pthread_create (&thread_id, &attr, dummy_process_thread, td)) {
390                 PBD::error << _("AudioEngine: cannot create process thread.") << endmsg;
391                 return -1;
392         }
393
394         _threads.push_back (thread_id);
395         return 0;
396 }
397
398 int
399 DummyAudioBackend::join_process_threads ()
400 {
401         int rv = 0;
402
403         for (std::vector<pthread_t>::const_iterator i = _threads.begin (); i != _threads.end (); ++i)
404         {
405                 void *status;
406                 if (pthread_join (*i, &status)) {
407                         PBD::error << _("AudioEngine: cannot terminate process thread.") << endmsg;
408                         rv -= 1;
409                 }
410         }
411         _threads.clear ();
412         return rv;
413 }
414
415 bool
416 DummyAudioBackend::in_process_thread ()
417 {
418         for (std::vector<pthread_t>::const_iterator i = _threads.begin (); i != _threads.end (); ++i)
419         {
420 #ifdef COMPILER_MINGW
421                 if (*i == GetCurrentThread ()) {
422                         return true;
423                 }
424 #else // pthreads
425                 if (pthread_equal (*i, pthread_self ()) != 0) {
426                         return true;
427                 }
428 #endif
429         }
430         return false;
431 }
432
433 uint32_t
434 DummyAudioBackend::process_thread_count ()
435 {
436         return _threads.size ();
437 }
438
439 void
440 DummyAudioBackend::update_latencies ()
441 {
442 }
443
444 /* PORTENGINE API */
445
446 void*
447 DummyAudioBackend::private_handle () const
448 {
449         return NULL;
450 }
451
452 const std::string&
453 DummyAudioBackend::my_name () const
454 {
455         return _instance_name;
456 }
457
458 bool
459 DummyAudioBackend::available () const
460 {
461         return true;
462 }
463
464 uint32_t
465 DummyAudioBackend::port_name_size () const
466 {
467         return 256;
468 }
469
470 int
471 DummyAudioBackend::set_port_name (PortEngine::PortHandle port, const std::string& name)
472 {
473         if (!valid_port (port)) {
474                 PBD::error << _("DummyBackend::set_port_name: Invalid Port(s)") << endmsg;
475                 return -1;
476         }
477         return static_cast<DummyPort*>(port)->set_name (_instance_name + ":" + name);
478 }
479
480 std::string
481 DummyAudioBackend::get_port_name (PortEngine::PortHandle port) const
482 {
483         if (!valid_port (port)) {
484                 PBD::error << _("DummyBackend::get_port_name: Invalid Port(s)") << endmsg;
485                 return std::string ();
486         }
487         return static_cast<DummyPort*>(port)->name ();
488 }
489
490 PortEngine::PortHandle
491 DummyAudioBackend::get_port_by_name (const std::string& name) const
492 {
493         PortHandle port = (PortHandle) find_port (name);
494         return port;
495 }
496
497 int
498 DummyAudioBackend::get_ports (
499                 const std::string& port_name_pattern,
500                 DataType type, PortFlags flags,
501                 std::vector<std::string>& port_names) const
502 {
503         int rv = 0;
504         regex_t port_regex;
505         bool use_regexp = false;
506         if (port_name_pattern.size () > 0) {
507                 if (!regcomp (&port_regex, port_name_pattern.c_str (), REG_EXTENDED|REG_NOSUB)) {
508                         use_regexp = true;
509                 }
510         }
511         for (size_t i = 0; i < _ports.size (); ++i) {
512                 DummyPort* port = _ports[i];
513                 if ((port->type () == type) && (port->flags () & flags)) {
514                         if (!use_regexp || !regexec (&port_regex, port->name ().c_str (), 0, NULL, 0)) {
515                                 port_names.push_back (port->name ());
516                                 ++rv;
517                         }
518                 }
519         }
520         if (use_regexp) {
521                 regfree (&port_regex);
522         }
523         return rv;
524 }
525
526 DataType
527 DummyAudioBackend::port_data_type (PortEngine::PortHandle port) const
528 {
529         if (!valid_port (port)) {
530                 return DataType::NIL;
531         }
532         return static_cast<DummyPort*>(port)->type ();
533 }
534
535 PortEngine::PortHandle
536 DummyAudioBackend::register_port (
537                 const std::string& name,
538                 ARDOUR::DataType type,
539                 ARDOUR::PortFlags flags)
540 {
541         if (name.size () == 0) { return 0; }
542         if (flags & IsPhysical) { return 0; }
543         if (find_port (_instance_name + ":" + name) != NULL) {
544                 PBD::error << _("DummyBackend::register_port: Port already exists.") << endmsg;
545                 return 0;
546         }
547
548         DummyPort* port = NULL;
549         switch (type) {
550                 case DataType::AUDIO:
551                         port = new DummyAudioPort (_instance_name + ":" + name, flags);
552                         break;
553                 case DataType::MIDI:
554                         port = new DummyMidiPort (_instance_name + ":" + name, flags);
555                         break;
556                 default:
557                         PBD::error << _("DummyBackend::register_port: Invalid Data Type.") << endmsg;
558                         return 0;
559         }
560
561         _ports.push_back (port);
562
563         return port;
564 }
565
566 void
567 DummyAudioBackend::unregister_port (PortEngine::PortHandle port_handle)
568 {
569         if (!valid_port (port_handle)) {
570                 PBD::error << _("DummyBackend::unregister_port: Invalid Port.") << endmsg;
571         }
572         DummyPort* port = (DummyPort*)port_handle;
573         std::vector<DummyPort*>::iterator i = std::find (_ports.begin (), _ports.end (), (DummyPort*)port_handle);
574         if (i == _ports.end ()) {
575                 PBD::error << _("DummyBackend::unregister_port: Failed to find port") << endmsg;
576                 return;
577         }
578         _ports.erase (i);
579         delete port;
580 }
581
582 int
583 DummyAudioBackend::connect (const std::string& src, const std::string& dst)
584 {
585         DummyPort* src_port = find_port (src);
586         DummyPort* dst_port = find_port (dst);
587
588         if (!src_port) {
589                 PBD::error << _("DummyBackend::connect: Invalid Source port:")
590                                 << " (" << src <<")" << endmsg;
591                 return -1;
592         }
593         if (!dst_port) {
594                 PBD::error << _("DummyBackend::connect: Invalid Destination port:")
595                         << " (" << dst <<")" << endmsg;
596                 return -1;
597         }
598         return src_port->connect (dst_port);
599 }
600
601 int
602 DummyAudioBackend::disconnect (const std::string& src, const std::string& dst)
603 {
604         DummyPort* src_port = find_port (src);
605         DummyPort* dst_port = find_port (dst);
606
607         if (!src_port || !dst_port) {
608                 PBD::error << _("DummyBackend::disconnect: Invalid Port(s)") << endmsg;
609                 return -1;
610         }
611         return src_port->disconnect (dst_port);
612 }
613
614 int
615 DummyAudioBackend::connect (PortEngine::PortHandle src, const std::string& dst)
616 {
617         DummyPort* dst_port = find_port (dst);
618         if (!valid_port (src)) {
619                 PBD::error << _("DummyBackend::connect: Invalid Source Port Handle") << endmsg;
620                 return -1;
621         }
622         if (!dst_port) {
623                 PBD::error << _("DummyBackend::connect: Invalid Destination Port")
624                         << " (" << dst << ")" << endmsg;
625                 return -1;
626         }
627         return static_cast<DummyPort*>(src)->connect (dst_port);
628 }
629
630 int
631 DummyAudioBackend::disconnect (PortEngine::PortHandle src, const std::string& dst)
632 {
633         DummyPort* dst_port = find_port (dst);
634         if (!valid_port (src) || !dst_port) {
635                 PBD::error << _("DummyBackend::disconnect: Invalid Port(s)") << endmsg;
636                 return -1;
637         }
638         return static_cast<DummyPort*>(src)->disconnect (dst_port);
639 }
640
641 int
642 DummyAudioBackend::disconnect_all (PortEngine::PortHandle port)
643 {
644         if (!valid_port (port)) {
645                 PBD::error << _("DummyBackend::disconnect_all: Invalid Port") << endmsg;
646                 return -1;
647         }
648         static_cast<DummyPort*>(port)->disconnect_all ();
649         return 0;
650 }
651
652 bool
653 DummyAudioBackend::connected (PortEngine::PortHandle port, bool /* process_callback_safe*/)
654 {
655         if (!valid_port (port)) {
656                 PBD::error << _("DummyBackend::disconnect_all: Invalid Port") << endmsg;
657                 return false;
658         }
659         return static_cast<DummyPort*>(port)->is_connected ();
660 }
661
662 bool
663 DummyAudioBackend::connected_to (PortEngine::PortHandle src, const std::string& dst, bool /*process_callback_safe*/)
664 {
665         DummyPort* dst_port = find_port (dst);
666         if (!valid_port (src) || !dst_port) {
667                 PBD::error << _("DummyBackend::connected_to: Invalid Port") << endmsg;
668                 return false;
669         }
670         return static_cast<DummyPort*>(src)->is_connected (dst_port);
671 }
672
673 bool
674 DummyAudioBackend::physically_connected (PortEngine::PortHandle port, bool /*process_callback_safe*/)
675 {
676         if (!valid_port (port)) {
677                 PBD::error << _("DummyBackend::physically_connected: Invalid Port") << endmsg;
678                 return false;
679         }
680         return static_cast<DummyPort*>(port)->is_physically_connected ();
681 }
682
683 int
684 DummyAudioBackend::get_connections (PortEngine::PortHandle port, std::vector<std::string>& names, bool /*process_callback_safe*/)
685 {
686         if (!valid_port (port)) {
687                 PBD::error << _("DummyBackend::get_connections: Invalid Port") << endmsg;
688                 return -1;
689         }
690
691         assert (0 == names.size ());
692
693         const std::vector<DummyPort*>& connected_ports = static_cast<DummyPort*>(port)->get_connections ();
694
695         for (std::vector<DummyPort*>::const_iterator i = connected_ports.begin (); i != connected_ports.end (); ++i) {
696                 names.push_back ((*i)->name ());
697         }
698
699         return (int)names.size ();
700 }
701
702 /* MIDI */
703 int
704 DummyAudioBackend::midi_event_get (
705                 pframes_t& timestamp,
706                 size_t& size, uint8_t** buf, void* port_buffer,
707                 uint32_t event_index)
708 {
709         assert (buf && port_buffer);
710         DummyMidiBuffer& source = * (DummyMidiBuffer*)port_buffer;
711         if (event_index >= source.size ()) {
712                 return -1;
713         }
714         DummyMidiEvent * const event = source[event_index].get ();
715
716         timestamp = event->timestamp ();
717         size = event->size ();
718         *buf = event->data ();
719         return 0;
720 }
721
722 int
723 DummyAudioBackend::midi_event_put (
724                 void* port_buffer,
725                 pframes_t timestamp,
726                 const uint8_t* buffer, size_t size)
727 {
728         assert (buffer && port_buffer);
729         DummyMidiBuffer& dst = * (DummyMidiBuffer*)port_buffer;
730         if (dst.size () && (pframes_t)dst.back ()->timestamp () > timestamp) {
731                 fprintf (stderr, "DummyMidiBuffer: it's too late for this event.\n");
732                 return -1;
733         }
734         dst.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (timestamp, buffer, size)));
735         return 0;
736 }
737
738 uint32_t
739 DummyAudioBackend::get_midi_event_count (void* port_buffer)
740 {
741         assert (port_buffer);
742         return static_cast<DummyMidiBuffer*>(port_buffer)->size ();
743 }
744
745 void
746 DummyAudioBackend::midi_clear (void* port_buffer)
747 {
748         assert (port_buffer);
749         DummyMidiBuffer * buf = static_cast<DummyMidiBuffer*>(port_buffer);
750         assert (buf);
751         buf->clear ();
752 }
753
754 /* Monitoring */
755
756 bool
757 DummyAudioBackend::can_monitor_input () const
758 {
759         return false;
760 }
761
762 int
763 DummyAudioBackend::request_input_monitoring (PortEngine::PortHandle, bool)
764 {
765         return -1;
766 }
767
768 int
769 DummyAudioBackend::ensure_input_monitoring (PortEngine::PortHandle, bool)
770 {
771         return -1;
772 }
773
774 bool
775 DummyAudioBackend::monitoring_input (PortEngine::PortHandle)
776 {
777         return false;
778 }
779
780 /* Latency management */
781
782 void
783 DummyAudioBackend::set_latency_range (PortEngine::PortHandle port, bool for_playback, LatencyRange latency_range)
784 {
785         if (!valid_port (port)) {
786                 PBD::error << _("DummyPort::set_latency_range (): invalid port.") << endmsg;
787         }
788         static_cast<DummyPort*>(port)->set_latency_range (latency_range, for_playback);
789 }
790
791 LatencyRange
792 DummyAudioBackend::get_latency_range (PortEngine::PortHandle port, bool for_playback)
793 {
794         if (!valid_port (port)) {
795                 PBD::error << _("DummyPort::get_latency_range (): invalid port.") << endmsg;
796                 LatencyRange r;
797                 r.min = 0;
798                 r.max = 0;
799                 return r;
800         }
801         return static_cast<DummyPort*>(port)->latency_range (for_playback);
802 }
803
804 /* Discovering physical ports */
805
806 bool
807 DummyAudioBackend::port_is_physical (PortEngine::PortHandle port) const
808 {
809         if (!valid_port (port)) {
810                 PBD::error << _("DummyPort::port_is_physical (): invalid port.") << endmsg;
811                 return false;
812         }
813         return static_cast<DummyPort*>(port)->is_physical ();
814 }
815
816 void
817 DummyAudioBackend::get_physical_outputs (DataType type, std::vector<std::string>& port_names)
818 {
819         for (size_t i = 0; i < _ports.size (); ++i) {
820                 DummyPort* port = _ports[i];
821                 if ((port->type () == type) && port->is_output () && port->is_physical ()) {
822                         port_names.push_back (port->name ());
823                 }
824         }
825 }
826
827 void
828 DummyAudioBackend::get_physical_inputs (DataType type, std::vector<std::string>& port_names)
829 {
830         for (size_t i = 0; i < _ports.size (); ++i) {
831                 DummyPort* port = _ports[i];
832                 if ((port->type () == type) && port->is_input () && port->is_physical ()) {
833                         port_names.push_back (port->name ());
834                 }
835         }
836 }
837
838 ChanCount
839 DummyAudioBackend::n_physical_outputs () const
840 {
841         int n_midi = 0;
842         int n_audio = 0;
843         for (size_t i = 0; i < _ports.size (); ++i) {
844                 DummyPort* port = _ports[i];
845                 if (port->is_output () && port->is_physical ()) {
846                         switch (port->type ()) {
847                                 case DataType::AUDIO: ++n_audio; break;
848                                 case DataType::MIDI: ++n_midi; break;
849                                 default: break;
850                         }
851                 }
852         }
853         ChanCount cc;
854         cc.set (DataType::AUDIO, n_audio);
855         cc.set (DataType::MIDI, n_midi);
856         return cc;
857 }
858
859 ChanCount
860 DummyAudioBackend::n_physical_inputs () const
861 {
862         int n_midi = 0;
863         int n_audio = 0;
864         for (size_t i = 0; i < _ports.size (); ++i) {
865                 DummyPort* port = _ports[i];
866                 if (port->is_input () && port->is_physical ()) {
867                         switch (port->type ()) {
868                                 case DataType::AUDIO: ++n_audio; break;
869                                 case DataType::MIDI: ++n_midi; break;
870                                 default: break;
871                         }
872                 }
873         }
874         ChanCount cc;
875         cc.set (DataType::AUDIO, n_audio);
876         cc.set (DataType::MIDI, n_midi);
877         return cc;
878 }
879
880 /* Getting access to the data buffer for a port */
881
882 void*
883 DummyAudioBackend::get_buffer (PortEngine::PortHandle port, pframes_t nframes)
884 {
885         assert (port);
886         assert (valid_port (port));
887         return static_cast<DummyPort*>(port)->get_buffer (nframes);
888 }
889
890 /* Engine Process */
891 void *
892 DummyAudioBackend::main_process_thread ()
893 {
894         AudioEngine::thread_init_callback (this);
895         _running = true;
896         _processed_samples = 0;
897
898         struct timeval clock1, clock2;
899         ::gettimeofday (&clock1, NULL);
900         while (_running) {
901                 if (engine.process_callback (_audio_buffersize)) {
902                         return 0;
903                 }
904                 _processed_samples += _audio_buffersize;
905                 if (!_freewheeling) {
906                         ::gettimeofday (&clock2, NULL);
907                         const int elapsed_time = (clock2.tv_sec - clock1.tv_sec) * 1000000 + (clock2.tv_usec - clock1.tv_usec);
908                         const int nomial_time = 1000000 * _audio_buffersize / _samplerate;
909                         _dsp_load = elapsed_time / (float) nomial_time;
910                         if (elapsed_time < nomial_time) {
911                                 ::usleep (nomial_time - elapsed_time);
912                         } else {
913                                 ::usleep (100); // don't hog cpu
914                         }
915                 } else {
916                         _dsp_load = 1.0;
917                         ::usleep (100); // don't hog cpu
918                 }
919                 ::gettimeofday (&clock1, NULL);
920         }
921         _running = false;
922         return 0;
923 }
924
925
926 /******************************************************************************/
927
928 static boost::shared_ptr<DummyAudioBackend> _instance;
929
930 static boost::shared_ptr<AudioBackend>
931 backend_factory (AudioEngine& e)
932 {
933         if (!_instance) {
934                 _instance.reset (new DummyAudioBackend (e));
935         }
936         return _instance;
937 }
938
939 static int
940 instantiate (const std::string& arg1, const std::string& /* arg2 */)
941 {
942         s_instance_name = arg1;
943         return 0;
944 }
945
946 static int
947 deinstantiate ()
948 {
949         _instance.reset ();
950         return 0;
951 }
952
953 static bool
954 already_configured ()
955 {
956         return false;
957 }
958
959 static ARDOUR::AudioBackendInfo _descriptor = {
960         "Dummy",
961         instantiate,
962         deinstantiate,
963         backend_factory,
964         already_configured,
965 };
966
967 extern "C" ARDOURBACKEND_API ARDOUR::AudioBackendInfo* descriptor ()
968 {
969         return &_descriptor;
970 }
971
972
973 /******************************************************************************/
974 DummyPort::DummyPort (const std::string& name, PortFlags flags)
975         : _name  (name)
976         , _flags (flags)
977 {
978         _capture_latency_range.min = 0;
979         _capture_latency_range.max = 0;
980         _playback_latency_range.min = 0;
981         _playback_latency_range.max = 0;
982 }
983
984 DummyPort::~DummyPort () {
985         disconnect_all ();
986 }
987
988
989 int DummyPort::connect (DummyPort *port)
990 {
991         if (!port) {
992                 PBD::error << _("DummyPort::connect (): invalid (null) port") << endmsg;
993                 return -1;
994         }
995
996         if (type () != port->type ()) {
997                 PBD::error << _("DummyPort::connect (): wrong port-type") << endmsg;
998                 return -1;
999         }
1000
1001         if (is_output () && port->is_output ()) {
1002                 PBD::error << _("DummyPort::connect (): cannot inter-connect output ports.") << endmsg;
1003                 return -1;
1004         }
1005
1006         if (is_input () && port->is_input ()) {
1007                 PBD::error << _("DummyPort::connect (): cannot inter-connect input ports.") << endmsg;
1008                 return -1;
1009         }
1010
1011         if (this == port) {
1012                 PBD::error << _("DummyPort::connect (): cannot self-connect ports.") << endmsg;
1013                 return -1;
1014         }
1015
1016         if (is_connected (port)) {
1017                 PBD::error << _("DummyPort::connect (): ports are already connected:")
1018                         << " (" << name () << ") -> (" << port->name () << ")"
1019                         << endmsg;
1020                 return -1;
1021         }
1022
1023         _connect (port, true);
1024         return 0;
1025 }
1026
1027
1028 void DummyPort::_connect (DummyPort *port, bool callback)
1029 {
1030         _connections.push_back (port);
1031         if (callback) {
1032                 port->_connect (this, false);
1033         }
1034 }
1035
1036 int DummyPort::disconnect (DummyPort *port)
1037 {
1038         if (!port) {
1039                 PBD::error << _("DummyPort::disconnect (): invalid (null) port") << endmsg;
1040                 return -1;
1041         }
1042
1043         if (!is_connected (port)) {
1044                 PBD::error << _("DummyPort::disconnect (): ports are not connected:")
1045                         << " (" << name () << ") -> (" << port->name () << ")"
1046                         << endmsg;
1047                 return -1;
1048         }
1049         _disconnect (port, true);
1050         return 0;
1051 }
1052
1053 void DummyPort::_disconnect (DummyPort *port, bool callback)
1054 {
1055         std::vector<DummyPort*>::iterator it = std::find (_connections.begin (), _connections.end (), port);
1056
1057         assert (it != _connections.end ());
1058
1059         _connections.erase (it);
1060
1061         if (callback) {
1062                 port->_disconnect (this, false);
1063         }
1064 }
1065
1066
1067 void DummyPort::disconnect_all ()
1068 {
1069         while (!_connections.empty ()) {
1070                 _connections.back ()->_disconnect (this, false);
1071                 _connections.pop_back ();
1072         }
1073 }
1074
1075 bool
1076 DummyPort::is_connected (const DummyPort *port) const
1077 {
1078         return std::find (_connections.begin (), _connections.end (), port) != _connections.end ();
1079 }
1080
1081 bool DummyPort::is_physically_connected () const
1082 {
1083         for (std::vector<DummyPort*>::const_iterator it = _connections.begin (); it != _connections.end (); ++it) {
1084                 if ((*it)->is_physical ()) {
1085                         return true;
1086                 }
1087         }
1088         return false;
1089 }
1090
1091 /******************************************************************************/
1092
1093 DummyAudioPort::DummyAudioPort (const std::string& name, PortFlags flags)
1094         : DummyPort (name, flags)
1095 {
1096         memset (_buffer, 0, sizeof (_buffer));
1097 }
1098
1099 DummyAudioPort::~DummyAudioPort () { }
1100
1101 void* DummyAudioPort::get_buffer (pframes_t n_samples)
1102 {
1103         if (is_input ()) {
1104                 std::vector<DummyPort*>::const_iterator it = get_connections ().begin ();
1105                 if (it == get_connections ().end ()) {
1106                         memset (_buffer, 0, n_samples * sizeof (Sample));
1107                 } else {
1108                         DummyAudioPort const * source = static_cast<const DummyAudioPort*>(*it);
1109                         assert (source && source->is_output ());
1110                         memcpy (_buffer, source->const_buffer (), n_samples * sizeof (Sample));
1111                         while (++it != get_connections ().end ()) {
1112                                 source = static_cast<const DummyAudioPort*>(*it);
1113                                 assert (source && source->is_output ());
1114                                 Sample* dst = buffer ();
1115                                 const Sample* src = source->const_buffer ();
1116                                 for (uint32_t s = 0; s < n_samples; ++s, ++dst, ++src) {
1117                                         *dst += *src;
1118                                 }
1119                         }
1120                 }
1121         }
1122         return _buffer;
1123 }
1124
1125
1126 DummyMidiPort::DummyMidiPort (const std::string& name, PortFlags flags)
1127         : DummyPort (name, flags)
1128 {
1129         _buffer.clear ();
1130 }
1131
1132 DummyMidiPort::~DummyMidiPort () { }
1133
1134 void* DummyMidiPort::get_buffer (pframes_t /* nframes */)
1135 {
1136         if (is_input ()) {
1137                 _buffer.clear ();
1138                 for (std::vector<DummyPort*>::const_iterator i = get_connections ().begin ();
1139                                 i != get_connections ().end ();
1140                                 ++i) {
1141                         const DummyMidiBuffer src = static_cast<const DummyMidiPort*>(*i)->const_buffer ();
1142                         for (DummyMidiBuffer::const_iterator it = src.begin (); it != src.end (); ++it) {
1143                                 _buffer.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (**it)));
1144                         }
1145                 }
1146                 std::sort (_buffer.begin (), _buffer.end ());
1147         }
1148         return &_buffer;
1149 }
1150
1151 DummyMidiEvent::DummyMidiEvent (const pframes_t timestamp, const uint8_t* data, size_t size)
1152         : _size (size)
1153         , _timestamp (timestamp)
1154         , _data (0)
1155 {
1156         if (size > 0) {
1157                 _data = (uint8_t*) malloc (size);
1158          memcpy (_data, data, size);
1159         }
1160 }
1161
1162 DummyMidiEvent::DummyMidiEvent (const DummyMidiEvent& other)
1163         : _size (other.size ())
1164         , _timestamp (other.timestamp ())
1165         , _data (0)
1166 {
1167         if (other.size () && other.const_data ()) {
1168                 _data = (uint8_t*) malloc (other.size ());
1169                 memcpy (_data, other.const_data (), other.size ());
1170         }
1171 };
1172
1173 DummyMidiEvent::~DummyMidiEvent () {
1174         free (_data);
1175 };