Merge branch 'waveview_hacks' of https://github.com/nmains/ardour into cairocanvas
[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 #include <stdlib.h>
23
24 #include <glibmm.h>
25
26 #include "dummy_audiobackend.h"
27
28 #include "pbd/error.h"
29 #include "ardour/port_manager.h"
30 #include "i18n.h"
31
32 using namespace ARDOUR;
33
34 static std::string s_instance_name;
35 size_t DummyAudioBackend::_max_buffer_size = 8192;
36 std::vector<std::string> DummyAudioBackend::_midi_options;
37 std::vector<AudioBackend::DeviceStatus> DummyAudioBackend::_device_status;
38
39 DummyAudioBackend::DummyAudioBackend (AudioEngine& e, AudioBackendInfo& info)
40         : AudioBackend (e, info)
41         , _running (false)
42         , _freewheeling (false)
43         , _device ("")
44         , _samplerate (48000)
45         , _samples_per_period (1024)
46         , _dsp_load (0)
47         , _n_inputs (0)
48         , _n_outputs (0)
49         , _n_midi_inputs (0)
50         , _n_midi_outputs (0)
51         , _systemic_input_latency (0)
52         , _systemic_output_latency (0)
53         , _processed_samples (0)
54         , _port_change_flag (false)
55 {
56         _instance_name = s_instance_name;
57         pthread_mutex_init (&_port_callback_mutex, 0);
58 }
59
60 DummyAudioBackend::~DummyAudioBackend ()
61 {
62         pthread_mutex_destroy (&_port_callback_mutex);
63 }
64
65 /* AUDIOBACKEND API */
66
67 std::string
68 DummyAudioBackend::name () const
69 {
70         return X_("Dummy");
71 }
72
73 bool
74 DummyAudioBackend::is_realtime () const
75 {
76         return false;
77 }
78
79 std::vector<AudioBackend::DeviceStatus>
80 DummyAudioBackend::enumerate_devices () const
81 {
82         if (_device_status.empty()) {
83                 _device_status.push_back (DeviceStatus (_("Silence"), true));
84                 _device_status.push_back (DeviceStatus (_("Sine Wave"), true));
85                 _device_status.push_back (DeviceStatus (_("Uniform White Noise"), true));
86                 _device_status.push_back (DeviceStatus (_("Gaussian White Noise"), true));
87                 _device_status.push_back (DeviceStatus (_("Pink Noise"), true));
88                 _device_status.push_back (DeviceStatus (_("Pink Noise (low CPU)"), true));
89         }
90         return _device_status;
91 }
92
93 std::vector<float>
94 DummyAudioBackend::available_sample_rates (const std::string&) const
95 {
96         std::vector<float> sr;
97         sr.push_back (8000.0);
98         sr.push_back (22050.0);
99         sr.push_back (24000.0);
100         sr.push_back (44100.0);
101         sr.push_back (48000.0);
102         sr.push_back (88200.0);
103         sr.push_back (96000.0);
104         sr.push_back (176400.0);
105         sr.push_back (192000.0);
106         return sr;
107 }
108
109 std::vector<uint32_t>
110 DummyAudioBackend::available_buffer_sizes (const std::string&) const
111 {
112         std::vector<uint32_t> bs;
113         bs.push_back (4);
114         bs.push_back (8);
115         bs.push_back (16);
116         bs.push_back (32);
117         bs.push_back (64);
118         bs.push_back (128);
119         bs.push_back (256);
120         bs.push_back (512);
121         bs.push_back (1024);
122         bs.push_back (2048);
123         bs.push_back (4096);
124         bs.push_back (8192);
125         return bs;
126 }
127
128 uint32_t
129 DummyAudioBackend::available_input_channel_count (const std::string&) const
130 {
131         return 128;
132 }
133
134 uint32_t
135 DummyAudioBackend::available_output_channel_count (const std::string&) const
136 {
137         return 128;
138 }
139
140 bool
141 DummyAudioBackend::can_change_sample_rate_when_running () const
142 {
143         return true;
144 }
145
146 bool
147 DummyAudioBackend::can_change_buffer_size_when_running () const
148 {
149         return true;
150 }
151
152 int
153 DummyAudioBackend::set_device_name (const std::string& d)
154 {
155         _device = d;
156         return 0;
157 }
158
159 int
160 DummyAudioBackend::set_sample_rate (float sr)
161 {
162         if (sr <= 0) { return -1; }
163         _samplerate = sr;
164         engine.sample_rate_change (sr);
165         return 0;
166 }
167
168 int
169 DummyAudioBackend::set_buffer_size (uint32_t bs)
170 {
171         if (bs <= 0 || bs >= _max_buffer_size) {
172                 return -1;
173         }
174         _samples_per_period = bs;
175         engine.buffer_size_change (bs);
176         return 0;
177 }
178
179 int
180 DummyAudioBackend::set_interleaved (bool yn)
181 {
182         if (!yn) { return 0; }
183         return -1;
184 }
185
186 int
187 DummyAudioBackend::set_input_channels (uint32_t cc)
188 {
189         _n_inputs = cc;
190         return 0;
191 }
192
193 int
194 DummyAudioBackend::set_output_channels (uint32_t cc)
195 {
196         _n_outputs = cc;
197         return 0;
198 }
199
200 int
201 DummyAudioBackend::set_systemic_input_latency (uint32_t sl)
202 {
203         _systemic_input_latency = sl;
204         return 0;
205 }
206
207 int
208 DummyAudioBackend::set_systemic_output_latency (uint32_t sl)
209 {
210         _systemic_output_latency = sl;
211         return 0;
212 }
213
214 /* Retrieving parameters */
215 std::string
216 DummyAudioBackend::device_name () const
217 {
218         return _device;
219 }
220
221 float
222 DummyAudioBackend::sample_rate () const
223 {
224         return _samplerate;
225 }
226
227 uint32_t
228 DummyAudioBackend::buffer_size () const
229 {
230         return _samples_per_period;
231 }
232
233 bool
234 DummyAudioBackend::interleaved () const
235 {
236         return false;
237 }
238
239 uint32_t
240 DummyAudioBackend::input_channels () const
241 {
242         return _n_inputs;
243 }
244
245 uint32_t
246 DummyAudioBackend::output_channels () const
247 {
248         return _n_outputs;
249 }
250
251 uint32_t
252 DummyAudioBackend::systemic_input_latency () const
253 {
254         return _systemic_input_latency;
255 }
256
257 uint32_t
258 DummyAudioBackend::systemic_output_latency () const
259 {
260         return _systemic_output_latency;
261 }
262
263
264 /* MIDI */
265 std::vector<std::string>
266 DummyAudioBackend::enumerate_midi_options () const
267 {
268         if (_midi_options.empty()) {
269                 _midi_options.push_back (_("1 in, 1 out"));
270                 _midi_options.push_back (_("2 in, 2 out"));
271                 _midi_options.push_back (_("8 in, 8 out"));
272         }
273         return _midi_options;
274 }
275
276 int
277 DummyAudioBackend::set_midi_option (const std::string& opt)
278 {
279         if (opt == _("1 in, 1 out")) {
280                 _n_midi_inputs = _n_midi_outputs = 1;
281         }
282         else if (opt == _("2 in, 2 out")) {
283                 _n_midi_inputs = _n_midi_outputs = 2;
284         }
285         else if (opt == _("8 in, 8 out")) {
286                 _n_midi_inputs = _n_midi_outputs = 8;
287         }
288         else {
289                 _n_midi_inputs = _n_midi_outputs = 0;
290         }
291         return 0;
292 }
293
294 std::string
295 DummyAudioBackend::midi_option () const
296 {
297         return ""; // TODO
298 }
299
300 /* State Control */
301
302 static void * pthread_process (void *arg)
303 {
304         DummyAudioBackend *d = static_cast<DummyAudioBackend *>(arg);
305         d->main_process_thread ();
306         pthread_exit (0);
307         return 0;
308 }
309
310 int
311 DummyAudioBackend::_start (bool /*for_latency_measurement*/)
312 {
313         if (_running) {
314                 PBD::error << _("DummyAudioBackend: already active.") << endmsg;
315                 return -1;
316         }
317
318         if (_ports.size()) {
319                 PBD::warning << _("DummyAudioBackend: recovering from unclean shutdown, port registry is not empty.") << endmsg;
320                 _system_inputs.clear();
321                 _ports.clear();
322         }
323
324         if (register_system_ports()) {
325                 PBD::error << _("DummyAudioBackend: failed to register system ports.") << endmsg;
326                 return -1;
327         }
328
329         engine.sample_rate_change (_samplerate);
330         engine.buffer_size_change (_samples_per_period);
331
332         if (engine.reestablish_ports ()) {
333                 PBD::error << _("DummyAudioBackend: Could not re-establish ports.") << endmsg;
334                 stop ();
335                 return -1;
336         }
337
338         engine.reconnect_ports ();
339         _port_change_flag = false;
340
341         if (pthread_create (&_main_thread, NULL, pthread_process, this)) {
342                 PBD::error << _("DummyAudioBackend: cannot start.") << endmsg;
343         }
344
345         int timeout = 5000;
346         while (!_running && --timeout > 0) { Glib::usleep (1000); }
347
348         if (timeout == 0 || !_running) {
349                 PBD::error << _("DummyAudioBackend: failed to start process thread.") << endmsg;
350                 return -1;
351         }
352
353         return 0;
354 }
355
356 int
357 DummyAudioBackend::stop ()
358 {
359         void *status;
360         if (!_running) {
361                 return 0;
362         }
363
364         _running = false;
365         if (pthread_join (_main_thread, &status)) {
366                 PBD::error << _("DummyAudioBackend: failed to terminate.") << endmsg;
367                 return -1;
368         }
369         unregister_system_ports();
370         return 0;
371 }
372
373 int
374 DummyAudioBackend::freewheel (bool onoff)
375 {
376         if (onoff == _freewheeling) {
377                 return 0;
378         }
379         _freewheeling = onoff;
380         engine.freewheel_callback (onoff);
381         return 0;
382 }
383
384 float
385 DummyAudioBackend::dsp_load () const
386 {
387         return 100.f * _dsp_load;
388 }
389
390 size_t
391 DummyAudioBackend::raw_buffer_size (DataType t)
392 {
393         switch (t) {
394                 case DataType::AUDIO:
395                         return _samples_per_period * sizeof(Sample);
396                 case DataType::MIDI:
397                         return _max_buffer_size; // XXX not really limited
398         }
399         return 0;
400 }
401
402 /* Process time */
403 pframes_t
404 DummyAudioBackend::sample_time ()
405 {
406         return _processed_samples;
407 }
408
409 pframes_t
410 DummyAudioBackend::sample_time_at_cycle_start ()
411 {
412         return _processed_samples;
413 }
414
415 pframes_t
416 DummyAudioBackend::samples_since_cycle_start ()
417 {
418         return 0;
419 }
420
421
422 void *
423 DummyAudioBackend::dummy_process_thread (void *arg)
424 {
425         ThreadData* td = reinterpret_cast<ThreadData*> (arg);
426         boost::function<void ()> f = td->f;
427         delete td;
428         f ();
429         return 0;
430 }
431
432 int
433 DummyAudioBackend::create_process_thread (boost::function<void()> func)
434 {
435         pthread_t thread_id;
436         pthread_attr_t attr;
437         size_t stacksize = 100000;
438
439         pthread_attr_init (&attr);
440         pthread_attr_setstacksize (&attr, stacksize);
441         ThreadData* td = new ThreadData (this, func, stacksize);
442
443         if (pthread_create (&thread_id, &attr, dummy_process_thread, td)) {
444                 PBD::error << _("AudioEngine: cannot create process thread.") << endmsg;
445                 pthread_attr_destroy (&attr);
446                 return -1;
447         }
448         pthread_attr_destroy (&attr);
449
450         _threads.push_back (thread_id);
451         return 0;
452 }
453
454 int
455 DummyAudioBackend::join_process_threads ()
456 {
457         int rv = 0;
458
459         for (std::vector<pthread_t>::const_iterator i = _threads.begin (); i != _threads.end (); ++i)
460         {
461                 void *status;
462                 if (pthread_join (*i, &status)) {
463                         PBD::error << _("AudioEngine: cannot terminate process thread.") << endmsg;
464                         rv -= 1;
465                 }
466         }
467         _threads.clear ();
468         return rv;
469 }
470
471 bool
472 DummyAudioBackend::in_process_thread ()
473 {
474         for (std::vector<pthread_t>::const_iterator i = _threads.begin (); i != _threads.end (); ++i)
475         {
476                 if (pthread_equal (*i, pthread_self ()) != 0) {
477                         return true;
478                 }
479         }
480         return false;
481 }
482
483 uint32_t
484 DummyAudioBackend::process_thread_count ()
485 {
486         return _threads.size ();
487 }
488
489 void
490 DummyAudioBackend::update_latencies ()
491 {
492         // trigger latency callback in RT thread (locked graph)
493         port_connect_add_remove_callback();
494 }
495
496 /* PORTENGINE API */
497
498 void*
499 DummyAudioBackend::private_handle () const
500 {
501         return NULL;
502 }
503
504 const std::string&
505 DummyAudioBackend::my_name () const
506 {
507         return _instance_name;
508 }
509
510 bool
511 DummyAudioBackend::available () const
512 {
513         return true;
514 }
515
516 uint32_t
517 DummyAudioBackend::port_name_size () const
518 {
519         return 256;
520 }
521
522 int
523 DummyAudioBackend::set_port_name (PortEngine::PortHandle port, const std::string& name)
524 {
525         if (!valid_port (port)) {
526                 PBD::error << _("DummyBackend::set_port_name: Invalid Port(s)") << endmsg;
527                 return -1;
528         }
529         return static_cast<DummyPort*>(port)->set_name (_instance_name + ":" + name);
530 }
531
532 std::string
533 DummyAudioBackend::get_port_name (PortEngine::PortHandle port) const
534 {
535         if (!valid_port (port)) {
536                 PBD::error << _("DummyBackend::get_port_name: Invalid Port(s)") << endmsg;
537                 return std::string ();
538         }
539         return static_cast<DummyPort*>(port)->name ();
540 }
541
542 PortEngine::PortHandle
543 DummyAudioBackend::get_port_by_name (const std::string& name) const
544 {
545         PortHandle port = (PortHandle) find_port (name);
546         return port;
547 }
548
549 int
550 DummyAudioBackend::get_ports (
551                 const std::string& port_name_pattern,
552                 DataType type, PortFlags flags,
553                 std::vector<std::string>& port_names) const
554 {
555         int rv = 0;
556         regex_t port_regex;
557         bool use_regexp = false;
558         if (port_name_pattern.size () > 0) {
559                 if (!regcomp (&port_regex, port_name_pattern.c_str (), REG_EXTENDED|REG_NOSUB)) {
560                         use_regexp = true;
561                 }
562         }
563         for (size_t i = 0; i < _ports.size (); ++i) {
564                 DummyPort* port = _ports[i];
565                 if ((port->type () == type) && (port->flags () & flags)) {
566                         if (!use_regexp || !regexec (&port_regex, port->name ().c_str (), 0, NULL, 0)) {
567                                 port_names.push_back (port->name ());
568                                 ++rv;
569                         }
570                 }
571         }
572         if (use_regexp) {
573                 regfree (&port_regex);
574         }
575         return rv;
576 }
577
578 DataType
579 DummyAudioBackend::port_data_type (PortEngine::PortHandle port) const
580 {
581         if (!valid_port (port)) {
582                 return DataType::NIL;
583         }
584         return static_cast<DummyPort*>(port)->type ();
585 }
586
587 PortEngine::PortHandle
588 DummyAudioBackend::register_port (
589                 const std::string& name,
590                 ARDOUR::DataType type,
591                 ARDOUR::PortFlags flags)
592 {
593         if (name.size () == 0) { return 0; }
594         if (flags & IsPhysical) { return 0; }
595         return add_port (_instance_name + ":" + name, type, flags);
596 }
597
598 PortEngine::PortHandle
599 DummyAudioBackend::add_port (
600                 const std::string& name,
601                 ARDOUR::DataType type,
602                 ARDOUR::PortFlags flags)
603 {
604         assert(name.size ());
605         if (find_port (name)) {
606                 PBD::error << _("DummyBackend::register_port: Port already exists:")
607                                 << " (" << name << ")" << endmsg;
608                 return 0;
609         }
610         DummyPort* port = NULL;
611         switch (type) {
612                 case DataType::AUDIO:
613                         port = new DummyAudioPort (*this, name, flags);
614                         break;
615                 case DataType::MIDI:
616                         port = new DummyMidiPort (*this, name, flags);
617                         break;
618                 default:
619                         PBD::error << _("DummyBackend::register_port: Invalid Data Type.") << endmsg;
620                         return 0;
621         }
622
623         _ports.push_back (port);
624
625         return port;
626 }
627
628 void
629 DummyAudioBackend::unregister_port (PortEngine::PortHandle port_handle)
630 {
631         if (!valid_port (port_handle)) {
632                 PBD::error << _("DummyBackend::unregister_port: Invalid Port.") << endmsg;
633         }
634         DummyPort* port = static_cast<DummyPort*>(port_handle);
635         std::vector<DummyPort*>::iterator i = std::find (_ports.begin (), _ports.end (), static_cast<DummyPort*>(port_handle));
636         if (i == _ports.end ()) {
637                 PBD::error << _("DummyBackend::unregister_port: Failed to find port") << endmsg;
638                 return;
639         }
640         disconnect_all(port_handle);
641         _ports.erase (i);
642         delete port;
643 }
644
645 int
646 DummyAudioBackend::register_system_ports()
647 {
648         LatencyRange lr;
649         enum DummyAudioPort::GeneratorType gt;
650         if (_device == _("Uniform White Noise")) {
651                 gt = DummyAudioPort::UniformWhiteNoise;
652         } else if (_device == _("Gaussian White Noise")) {
653                 gt = DummyAudioPort::GaussianWhiteNoise;
654         } else if (_device == _("Pink Noise")) {
655                 gt = DummyAudioPort::PinkNoise;
656         } else if (_device == _("Pink Noise (low CPU)")) {
657                 gt = DummyAudioPort::PonyNoise;
658         } else if (_device == _("Sine Wave")) {
659                 gt = DummyAudioPort::SineWave;
660         } else {
661                 gt = DummyAudioPort::Silence;
662         }
663
664         const int a_ins = _n_inputs > 0 ? _n_inputs : 8;
665         const int a_out = _n_outputs > 0 ? _n_outputs : 8;
666         const int m_ins = _n_midi_inputs > 0 ? _n_midi_inputs : 2;
667         const int m_out = _n_midi_outputs > 0 ? _n_midi_outputs : 2;
668
669         /* audio ports */
670         lr.min = lr.max = _samples_per_period + _systemic_input_latency;
671         for (int i = 1; i <= a_ins; ++i) {
672                 char tmp[64];
673                 snprintf(tmp, sizeof(tmp), "system:capture_%d", i);
674                 PortHandle p = add_port(std::string(tmp), DataType::AUDIO, static_cast<PortFlags>(IsOutput | IsPhysical | IsTerminal));
675                 if (!p) return -1;
676                 set_latency_range (p, false, lr);
677                 _system_inputs.push_back (static_cast<DummyAudioPort*>(p));
678                 static_cast<DummyAudioPort*>(p)->setup_generator (gt, _samplerate);
679         }
680
681         lr.min = lr.max = _samples_per_period + _systemic_output_latency;
682         for (int i = 1; i <= a_out; ++i) {
683                 char tmp[64];
684                 snprintf(tmp, sizeof(tmp), "system:playback_%d", i);
685                 PortHandle p = add_port(std::string(tmp), DataType::AUDIO, static_cast<PortFlags>(IsInput | IsPhysical | IsTerminal));
686                 if (!p) return -1;
687                 set_latency_range (p, true, lr);
688         }
689
690         /* midi ports */
691         lr.min = lr.max = _samples_per_period + _systemic_input_latency;
692         for (int i = 1; i <= m_ins; ++i) {
693                 char tmp[64];
694                 snprintf(tmp, sizeof(tmp), "system:midi_capture_%d", i);
695                 PortHandle p = add_port(std::string(tmp), DataType::MIDI, static_cast<PortFlags>(IsOutput | IsPhysical | IsTerminal));
696                 if (!p) return -1;
697                 set_latency_range (p, false, lr);
698         }
699
700         lr.min = lr.max = _samples_per_period + _systemic_output_latency;
701         for (int i = 1; i <= m_out; ++i) {
702                 char tmp[64];
703                 snprintf(tmp, sizeof(tmp), "system:midi_playback_%d", i);
704                 PortHandle p = add_port(std::string(tmp), DataType::MIDI, static_cast<PortFlags>(IsInput | IsPhysical | IsTerminal));
705                 if (!p) return -1;
706                 set_latency_range (p, true, lr);
707         }
708         return 0;
709 }
710
711 void
712 DummyAudioBackend::unregister_system_ports()
713 {
714         size_t i = 0;
715         _system_inputs.clear();
716         while (i <  _ports.size ()) {
717                 DummyPort* port = _ports[i];
718                 if (port->is_physical () && port->is_terminal ()) {
719                         port->disconnect_all ();
720                         _ports.erase (_ports.begin() + i);
721                 } else {
722                         ++i;
723                 }
724         }
725 }
726
727 int
728 DummyAudioBackend::connect (const std::string& src, const std::string& dst)
729 {
730         DummyPort* src_port = find_port (src);
731         DummyPort* dst_port = find_port (dst);
732
733         if (!src_port) {
734                 PBD::error << _("DummyBackend::connect: Invalid Source port:")
735                                 << " (" << src <<")" << endmsg;
736                 return -1;
737         }
738         if (!dst_port) {
739                 PBD::error << _("DummyBackend::connect: Invalid Destination port:")
740                         << " (" << dst <<")" << endmsg;
741                 return -1;
742         }
743         return src_port->connect (dst_port);
744 }
745
746 int
747 DummyAudioBackend::disconnect (const std::string& src, const std::string& dst)
748 {
749         DummyPort* src_port = find_port (src);
750         DummyPort* dst_port = find_port (dst);
751
752         if (!src_port || !dst_port) {
753                 PBD::error << _("DummyBackend::disconnect: Invalid Port(s)") << endmsg;
754                 return -1;
755         }
756         return src_port->disconnect (dst_port);
757 }
758
759 int
760 DummyAudioBackend::connect (PortEngine::PortHandle src, const std::string& dst)
761 {
762         DummyPort* dst_port = find_port (dst);
763         if (!valid_port (src)) {
764                 PBD::error << _("DummyBackend::connect: Invalid Source Port Handle") << endmsg;
765                 return -1;
766         }
767         if (!dst_port) {
768                 PBD::error << _("DummyBackend::connect: Invalid Destination Port")
769                         << " (" << dst << ")" << endmsg;
770                 return -1;
771         }
772         return static_cast<DummyPort*>(src)->connect (dst_port);
773 }
774
775 int
776 DummyAudioBackend::disconnect (PortEngine::PortHandle src, const std::string& dst)
777 {
778         DummyPort* dst_port = find_port (dst);
779         if (!valid_port (src) || !dst_port) {
780                 PBD::error << _("DummyBackend::disconnect: Invalid Port(s)") << endmsg;
781                 return -1;
782         }
783         return static_cast<DummyPort*>(src)->disconnect (dst_port);
784 }
785
786 int
787 DummyAudioBackend::disconnect_all (PortEngine::PortHandle port)
788 {
789         if (!valid_port (port)) {
790                 PBD::error << _("DummyBackend::disconnect_all: Invalid Port") << endmsg;
791                 return -1;
792         }
793         static_cast<DummyPort*>(port)->disconnect_all ();
794         return 0;
795 }
796
797 bool
798 DummyAudioBackend::connected (PortEngine::PortHandle port, bool /* process_callback_safe*/)
799 {
800         if (!valid_port (port)) {
801                 PBD::error << _("DummyBackend::disconnect_all: Invalid Port") << endmsg;
802                 return false;
803         }
804         return static_cast<DummyPort*>(port)->is_connected ();
805 }
806
807 bool
808 DummyAudioBackend::connected_to (PortEngine::PortHandle src, const std::string& dst, bool /*process_callback_safe*/)
809 {
810         DummyPort* dst_port = find_port (dst);
811         if (!valid_port (src) || !dst_port) {
812                 PBD::error << _("DummyBackend::connected_to: Invalid Port") << endmsg;
813                 return false;
814         }
815         return static_cast<DummyPort*>(src)->is_connected (dst_port);
816 }
817
818 bool
819 DummyAudioBackend::physically_connected (PortEngine::PortHandle port, bool /*process_callback_safe*/)
820 {
821         if (!valid_port (port)) {
822                 PBD::error << _("DummyBackend::physically_connected: Invalid Port") << endmsg;
823                 return false;
824         }
825         return static_cast<DummyPort*>(port)->is_physically_connected ();
826 }
827
828 int
829 DummyAudioBackend::get_connections (PortEngine::PortHandle port, std::vector<std::string>& names, bool /*process_callback_safe*/)
830 {
831         if (!valid_port (port)) {
832                 PBD::error << _("DummyBackend::get_connections: Invalid Port") << endmsg;
833                 return -1;
834         }
835
836         assert (0 == names.size ());
837
838         const std::vector<DummyPort*>& connected_ports = static_cast<DummyPort*>(port)->get_connections ();
839
840         for (std::vector<DummyPort*>::const_iterator i = connected_ports.begin (); i != connected_ports.end (); ++i) {
841                 names.push_back ((*i)->name ());
842         }
843
844         return (int)names.size ();
845 }
846
847 /* MIDI */
848 int
849 DummyAudioBackend::midi_event_get (
850                 pframes_t& timestamp,
851                 size_t& size, uint8_t** buf, void* port_buffer,
852                 uint32_t event_index)
853 {
854         assert (buf && port_buffer);
855         DummyMidiBuffer& source = * static_cast<DummyMidiBuffer*>(port_buffer);
856         if (event_index >= source.size ()) {
857                 return -1;
858         }
859         DummyMidiEvent * const event = source[event_index].get ();
860
861         timestamp = event->timestamp ();
862         size = event->size ();
863         *buf = event->data ();
864         return 0;
865 }
866
867 int
868 DummyAudioBackend::midi_event_put (
869                 void* port_buffer,
870                 pframes_t timestamp,
871                 const uint8_t* buffer, size_t size)
872 {
873         assert (buffer && port_buffer);
874         DummyMidiBuffer& dst = * static_cast<DummyMidiBuffer*>(port_buffer);
875         if (dst.size () && (pframes_t)dst.back ()->timestamp () > timestamp) {
876                 fprintf (stderr, "DummyMidiBuffer: it's too late for this event.\n");
877                 return -1;
878         }
879         dst.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (timestamp, buffer, size)));
880         return 0;
881 }
882
883 uint32_t
884 DummyAudioBackend::get_midi_event_count (void* port_buffer)
885 {
886         assert (port_buffer);
887         return static_cast<DummyMidiBuffer*>(port_buffer)->size ();
888 }
889
890 void
891 DummyAudioBackend::midi_clear (void* port_buffer)
892 {
893         assert (port_buffer);
894         DummyMidiBuffer * buf = static_cast<DummyMidiBuffer*>(port_buffer);
895         assert (buf);
896         buf->clear ();
897 }
898
899 /* Monitoring */
900
901 bool
902 DummyAudioBackend::can_monitor_input () const
903 {
904         return false;
905 }
906
907 int
908 DummyAudioBackend::request_input_monitoring (PortEngine::PortHandle, bool)
909 {
910         return -1;
911 }
912
913 int
914 DummyAudioBackend::ensure_input_monitoring (PortEngine::PortHandle, bool)
915 {
916         return -1;
917 }
918
919 bool
920 DummyAudioBackend::monitoring_input (PortEngine::PortHandle)
921 {
922         return false;
923 }
924
925 /* Latency management */
926
927 void
928 DummyAudioBackend::set_latency_range (PortEngine::PortHandle port, bool for_playback, LatencyRange latency_range)
929 {
930         if (!valid_port (port)) {
931                 PBD::error << _("DummyPort::set_latency_range (): invalid port.") << endmsg;
932         }
933         static_cast<DummyPort*>(port)->set_latency_range (latency_range, for_playback);
934 }
935
936 LatencyRange
937 DummyAudioBackend::get_latency_range (PortEngine::PortHandle port, bool for_playback)
938 {
939         if (!valid_port (port)) {
940                 PBD::error << _("DummyPort::get_latency_range (): invalid port.") << endmsg;
941                 LatencyRange r;
942                 r.min = 0;
943                 r.max = 0;
944                 return r;
945         }
946         return static_cast<DummyPort*>(port)->latency_range (for_playback);
947 }
948
949 /* Discovering physical ports */
950
951 bool
952 DummyAudioBackend::port_is_physical (PortEngine::PortHandle port) const
953 {
954         if (!valid_port (port)) {
955                 PBD::error << _("DummyPort::port_is_physical (): invalid port.") << endmsg;
956                 return false;
957         }
958         return static_cast<DummyPort*>(port)->is_physical ();
959 }
960
961 void
962 DummyAudioBackend::get_physical_outputs (DataType type, std::vector<std::string>& port_names)
963 {
964         for (size_t i = 0; i < _ports.size (); ++i) {
965                 DummyPort* port = _ports[i];
966                 if ((port->type () == type) && port->is_input () && port->is_physical ()) {
967                         port_names.push_back (port->name ());
968                 }
969         }
970 }
971
972 void
973 DummyAudioBackend::get_physical_inputs (DataType type, std::vector<std::string>& port_names)
974 {
975         for (size_t i = 0; i < _ports.size (); ++i) {
976                 DummyPort* port = _ports[i];
977                 if ((port->type () == type) && port->is_output () && port->is_physical ()) {
978                         port_names.push_back (port->name ());
979                 }
980         }
981 }
982
983 ChanCount
984 DummyAudioBackend::n_physical_outputs () const
985 {
986         int n_midi = 0;
987         int n_audio = 0;
988         for (size_t i = 0; i < _ports.size (); ++i) {
989                 DummyPort* port = _ports[i];
990                 if (port->is_output () && port->is_physical ()) {
991                         switch (port->type ()) {
992                                 case DataType::AUDIO: ++n_audio; break;
993                                 case DataType::MIDI: ++n_midi; break;
994                                 default: break;
995                         }
996                 }
997         }
998         ChanCount cc;
999         cc.set (DataType::AUDIO, n_audio);
1000         cc.set (DataType::MIDI, n_midi);
1001         return cc;
1002 }
1003
1004 ChanCount
1005 DummyAudioBackend::n_physical_inputs () const
1006 {
1007         int n_midi = 0;
1008         int n_audio = 0;
1009         for (size_t i = 0; i < _ports.size (); ++i) {
1010                 DummyPort* port = _ports[i];
1011                 if (port->is_input () && port->is_physical ()) {
1012                         switch (port->type ()) {
1013                                 case DataType::AUDIO: ++n_audio; break;
1014                                 case DataType::MIDI: ++n_midi; break;
1015                                 default: break;
1016                         }
1017                 }
1018         }
1019         ChanCount cc;
1020         cc.set (DataType::AUDIO, n_audio);
1021         cc.set (DataType::MIDI, n_midi);
1022         return cc;
1023 }
1024
1025 /* Getting access to the data buffer for a port */
1026
1027 void*
1028 DummyAudioBackend::get_buffer (PortEngine::PortHandle port, pframes_t nframes)
1029 {
1030         assert (port);
1031         assert (valid_port (port));
1032         return static_cast<DummyPort*>(port)->get_buffer (nframes);
1033 }
1034
1035 /* Engine Process */
1036 void *
1037 DummyAudioBackend::main_process_thread ()
1038 {
1039         AudioEngine::thread_init_callback (this);
1040         _running = true;
1041         _processed_samples = 0;
1042
1043         manager.registration_callback();
1044         manager.graph_order_callback();
1045
1046         uint64_t clock1, clock2;
1047         clock1 = g_get_monotonic_time();
1048         while (_running) {
1049
1050                 // re-set input buffers, generate on demand.
1051                 for (std::vector<DummyAudioPort*>::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) {
1052                         (*it)->next_period();
1053                 }
1054
1055                 if (engine.process_callback (_samples_per_period)) {
1056                         return 0;
1057                 }
1058                 _processed_samples += _samples_per_period;
1059                 if (!_freewheeling) {
1060                         clock2 = g_get_monotonic_time();
1061                         const int64_t elapsed_time = clock2 - clock1;
1062                         const int64_t nomial_time = 1e6 * _samples_per_period / _samplerate;
1063                         _dsp_load = elapsed_time / (float) nomial_time;
1064                         if (elapsed_time < nomial_time) {
1065                                 Glib::usleep (nomial_time - elapsed_time);
1066                         } else {
1067                                 Glib::usleep (100); // don't hog cpu
1068                         }
1069                 } else {
1070                         _dsp_load = 1.0;
1071                         Glib::usleep (100); // don't hog cpu
1072                 }
1073                 clock1 = g_get_monotonic_time();
1074
1075                 bool connections_changed = false;
1076                 bool ports_changed = false;
1077                 if (!pthread_mutex_trylock (&_port_callback_mutex)) {
1078                         if (_port_change_flag) {
1079                                 ports_changed = true;
1080                                 _port_change_flag = false;
1081                         }
1082                         if (!_port_connection_queue.empty ()) {
1083                                 connections_changed = true;
1084                         }
1085                         while (!_port_connection_queue.empty ()) {
1086                                 PortConnectData *c = _port_connection_queue.back ();
1087                                 manager.connect_callback (c->a, c->b, c->c);
1088                                 _port_connection_queue.pop_back ();
1089                                 delete c;
1090                         }
1091                         pthread_mutex_unlock (&_port_callback_mutex);
1092                 }
1093                 if (ports_changed) {
1094                         manager.registration_callback();
1095                 }
1096                 if (connections_changed) {
1097                         manager.graph_order_callback();
1098                 }
1099                 if (connections_changed || ports_changed) {
1100                         engine.latency_callback(false);
1101                         engine.latency_callback(true);
1102                 }
1103
1104         }
1105         _running = false;
1106         return 0;
1107 }
1108
1109
1110 /******************************************************************************/
1111
1112 static boost::shared_ptr<DummyAudioBackend> _instance;
1113
1114 static boost::shared_ptr<AudioBackend> backend_factory (AudioEngine& e);
1115 static int instantiate (const std::string& arg1, const std::string& /* arg2 */);
1116 static int deinstantiate ();
1117 static bool already_configured ();
1118
1119 static ARDOUR::AudioBackendInfo _descriptor = {
1120         "Dummy",
1121         instantiate,
1122         deinstantiate,
1123         backend_factory,
1124         already_configured,
1125 };
1126
1127 static boost::shared_ptr<AudioBackend>
1128 backend_factory (AudioEngine& e)
1129 {
1130         if (!_instance) {
1131                 _instance.reset (new DummyAudioBackend (e, _descriptor));
1132         }
1133         return _instance;
1134 }
1135
1136 static int
1137 instantiate (const std::string& arg1, const std::string& /* arg2 */)
1138 {
1139         s_instance_name = arg1;
1140         return 0;
1141 }
1142
1143 static int
1144 deinstantiate ()
1145 {
1146         _instance.reset ();
1147         return 0;
1148 }
1149
1150 static bool
1151 already_configured ()
1152 {
1153         return false;
1154 }
1155
1156 extern "C" ARDOURBACKEND_API ARDOUR::AudioBackendInfo* descriptor ()
1157 {
1158         return &_descriptor;
1159 }
1160
1161
1162 /******************************************************************************/
1163 DummyPort::DummyPort (DummyAudioBackend &b, const std::string& name, PortFlags flags)
1164         : _dummy_backend (b)
1165         , _name  (name)
1166         , _flags (flags)
1167 {
1168         _capture_latency_range.min = 0;
1169         _capture_latency_range.max = 0;
1170         _playback_latency_range.min = 0;
1171         _playback_latency_range.max = 0;
1172         _dummy_backend.port_connect_add_remove_callback();
1173 }
1174
1175 DummyPort::~DummyPort () {
1176         disconnect_all ();
1177         _dummy_backend.port_connect_add_remove_callback();
1178 }
1179
1180
1181 int DummyPort::connect (DummyPort *port)
1182 {
1183         if (!port) {
1184                 PBD::error << _("DummyPort::connect (): invalid (null) port") << endmsg;
1185                 return -1;
1186         }
1187
1188         if (type () != port->type ()) {
1189                 PBD::error << _("DummyPort::connect (): wrong port-type") << endmsg;
1190                 return -1;
1191         }
1192
1193         if (is_output () && port->is_output ()) {
1194                 PBD::error << _("DummyPort::connect (): cannot inter-connect output ports.") << endmsg;
1195                 return -1;
1196         }
1197
1198         if (is_input () && port->is_input ()) {
1199                 PBD::error << _("DummyPort::connect (): cannot inter-connect input ports.") << endmsg;
1200                 return -1;
1201         }
1202
1203         if (this == port) {
1204                 PBD::error << _("DummyPort::connect (): cannot self-connect ports.") << endmsg;
1205                 return -1;
1206         }
1207
1208         if (is_connected (port)) {
1209 #if 0 // don't bother to warn about this for now. just ignore it
1210                 PBD::error << _("DummyPort::connect (): ports are already connected:")
1211                         << " (" << name () << ") -> (" << port->name () << ")"
1212                         << endmsg;
1213 #endif
1214                 return -1;
1215         }
1216
1217         _connect (port, true);
1218         return 0;
1219 }
1220
1221
1222 void DummyPort::_connect (DummyPort *port, bool callback)
1223 {
1224         _connections.push_back (port);
1225         if (callback) {
1226                 port->_connect (this, false);
1227                 _dummy_backend.port_connect_callback (name(),  port->name(), true);
1228         }
1229 }
1230
1231 int DummyPort::disconnect (DummyPort *port)
1232 {
1233         if (!port) {
1234                 PBD::error << _("DummyPort::disconnect (): invalid (null) port") << endmsg;
1235                 return -1;
1236         }
1237
1238         if (!is_connected (port)) {
1239                 PBD::error << _("DummyPort::disconnect (): ports are not connected:")
1240                         << " (" << name () << ") -> (" << port->name () << ")"
1241                         << endmsg;
1242                 return -1;
1243         }
1244         _disconnect (port, true);
1245         return 0;
1246 }
1247
1248 void DummyPort::_disconnect (DummyPort *port, bool callback)
1249 {
1250         std::vector<DummyPort*>::iterator it = std::find (_connections.begin (), _connections.end (), port);
1251
1252         assert (it != _connections.end ());
1253
1254         _connections.erase (it);
1255
1256         if (callback) {
1257                 port->_disconnect (this, false);
1258                 _dummy_backend.port_connect_callback (name(),  port->name(), false);
1259         }
1260 }
1261
1262
1263 void DummyPort::disconnect_all ()
1264 {
1265         while (!_connections.empty ()) {
1266                 _connections.back ()->_disconnect (this, false);
1267                 _dummy_backend.port_connect_callback (name(),  _connections.back ()->name(), false);
1268                 _connections.pop_back ();
1269         }
1270 }
1271
1272 bool
1273 DummyPort::is_connected (const DummyPort *port) const
1274 {
1275         return std::find (_connections.begin (), _connections.end (), port) != _connections.end ();
1276 }
1277
1278 bool DummyPort::is_physically_connected () const
1279 {
1280         for (std::vector<DummyPort*>::const_iterator it = _connections.begin (); it != _connections.end (); ++it) {
1281                 if ((*it)->is_physical ()) {
1282                         return true;
1283                 }
1284         }
1285         return false;
1286 }
1287
1288 /******************************************************************************/
1289
1290 DummyAudioPort::DummyAudioPort (DummyAudioBackend &b, const std::string& name, PortFlags flags)
1291         : DummyPort (b, name, flags)
1292         , _gen_type (Silence)
1293         , _gen_cycle (false)
1294         , _b0 (0)
1295         , _b1 (0)
1296         , _b2 (0)
1297         , _b3 (0)
1298         , _b4 (0)
1299         , _b5 (0)
1300         , _b6 (0)
1301         , _wavetable (0)
1302         , _tbl_length (0)
1303         , _tbl_offset (0)
1304         , _pass (false)
1305         , _rn1 (0)
1306 {
1307         memset (_buffer, 0, sizeof (_buffer));
1308 }
1309
1310 DummyAudioPort::~DummyAudioPort () {
1311         free(_wavetable);
1312         _wavetable = 0;
1313 }
1314
1315 void DummyAudioPort::setup_generator (GeneratorType const g, float const samplerate)
1316 {
1317         _gen_type = g;
1318         _rseed = g_get_monotonic_time() % UINT_MAX;
1319
1320         switch (_gen_type) {
1321                 case PinkNoise:
1322                 case PonyNoise:
1323                 case UniformWhiteNoise:
1324                 case GaussianWhiteNoise:
1325                 case Silence:
1326                         break;
1327                 case SineWave:
1328                         {
1329                                 _tbl_length = 5 + randi() % (int)(samplerate / 20.f);
1330                                 _wavetable = (Sample*) malloc( _tbl_length * sizeof(Sample));
1331                                 for (uint32_t i = 0 ; i < _tbl_length; ++i) {
1332                                         _wavetable[i] = .12589f * sinf(2.0 * M_PI * (float)i / (float)_tbl_length);
1333                                 }
1334                         }
1335                         break;
1336         }
1337 }
1338
1339 inline uint32_t
1340 DummyAudioPort::randi ()
1341 {
1342         // 31bit Park-Miller-Carta Pseudo-Random Number Generator
1343         // http://www.firstpr.com.au/dsp/rand31/
1344         uint32_t hi, lo;
1345         lo = 16807 * (_rseed & 0xffff);
1346         hi = 16807 * (_rseed >> 16);
1347
1348         lo += (hi & 0x7fff) << 16;
1349         lo += hi >> 15;
1350 #if 1
1351         lo = (lo & 0x7fffffff) + (lo >> 31);
1352 #else
1353         if (lo > 0x7fffffff) { lo -= 0x7fffffff; }
1354 #endif
1355         return (_rseed = lo);
1356 }
1357
1358 inline float
1359 DummyAudioPort::randf ()
1360 {
1361         return (randi() / 1073741824.f) - 1.f;
1362 }
1363
1364 float DummyAudioPort::grandf ()
1365 {
1366         // Gaussian White Noise
1367         // http://www.musicdsp.org/archive.php?classid=0#109
1368         float x1, x2, r;
1369
1370         if (_pass) {
1371                 _pass = false;
1372                 return _rn1;
1373         }
1374
1375         do {
1376                 x1 = randf ();
1377                 x2 = randf ();
1378                 r = x1 * x1 + x2 * x2;
1379         } while ((r >= 1.0f) || (r < 1e-22f));
1380
1381         r = sqrtf (-2.f * logf (r) / r);
1382
1383         _pass = true;
1384         _rn1 = r * x2;
1385         return r * x1;
1386 }
1387
1388 void DummyAudioPort::generate (const pframes_t n_samples)
1389 {
1390         Glib::Threads::Mutex::Lock lm (generator_lock);
1391         if (_gen_cycle) {
1392                 return;
1393         }
1394
1395         switch (_gen_type) {
1396                 case Silence:
1397                         memset (_buffer, 0, n_samples * sizeof (Sample));
1398                         break;
1399                 case SineWave:
1400                         assert(_wavetable && _tbl_length > 0);
1401                         {
1402                                 pframes_t written = 0;
1403                                 while (written < n_samples) {
1404                                         const uint32_t remain = n_samples - written;
1405                                         const uint32_t to_copy = std::min(remain, _tbl_length - _tbl_offset);
1406                                         memcpy((void*)&_buffer[written],
1407                                                         (void*)&_wavetable[_tbl_offset],
1408                                                         to_copy * sizeof(Sample));
1409                                         written += to_copy;
1410                                         _tbl_offset = (_tbl_offset + to_copy) % _tbl_length;
1411                                 }
1412                         }
1413                         break;
1414                 case UniformWhiteNoise:
1415                         for (pframes_t i = 0 ; i < n_samples; ++i) {
1416                                 _buffer[i] = .158489f * randf();
1417                         }
1418                         break;
1419                 case GaussianWhiteNoise:
1420                         for (pframes_t i = 0 ; i < n_samples; ++i) {
1421                                 _buffer[i] = .089125f * grandf();
1422                         }
1423                         break;
1424                 case PinkNoise:
1425                         for (pframes_t i = 0 ; i < n_samples; ++i) {
1426                                 // Paul Kellet's refined method
1427                                 // http://www.musicdsp.org/files/pink.txt
1428                                 // NB. If 'white' consists of uniform random numbers,
1429                                 // the pink noise will have an almost gaussian distribution.
1430                                 const float white = .0498f * randf ();
1431                                 _b0 = .99886f * _b0 + white * .0555179f;
1432                                 _b1 = .99332f * _b1 + white * .0750759f;
1433                                 _b2 = .96900f * _b2 + white * .1538520f;
1434                                 _b3 = .86650f * _b3 + white * .3104856f;
1435                                 _b4 = .55000f * _b4 + white * .5329522f;
1436                                 _b5 = -.7616f * _b5 - white * .0168980f;
1437                                 _buffer[i] = _b0 + _b1 + _b2 + _b3 + _b4 + _b5 + _b6 + white * 0.5362;
1438                                 _b6 = white * 0.115926;
1439                         }
1440                         break;
1441                 case PonyNoise:
1442                         for (pframes_t i = 0 ; i < n_samples; ++i) {
1443                                 const float white = 0.0498f * randf ();
1444                                 // Paul Kellet's economy method
1445                                 // http://www.musicdsp.org/files/pink.txt
1446                                 _b0 = 0.99765 * _b0 + white * 0.0990460;
1447                                 _b1 = 0.96300 * _b1 + white * 0.2965164;
1448                                 _b2 = 0.57000 * _b2 + white * 1.0526913;
1449                                 _buffer[i] = _b0 + _b1 + _b2 + white * 0.1848;
1450                         }
1451                         break;
1452         }
1453         _gen_cycle = true;
1454 }
1455
1456 void* DummyAudioPort::get_buffer (pframes_t n_samples)
1457 {
1458         if (is_input ()) {
1459                 std::vector<DummyPort*>::const_iterator it = get_connections ().begin ();
1460                 if (it == get_connections ().end ()) {
1461                         memset (_buffer, 0, n_samples * sizeof (Sample));
1462                 } else {
1463                         DummyAudioPort * source = static_cast<DummyAudioPort*>(*it);
1464                         assert (source && source->is_output ());
1465                         if (source->is_physical() && source->is_terminal()) {
1466                                 source->get_buffer(n_samples); // generate signal.
1467                         }
1468                         memcpy (_buffer, source->const_buffer (), n_samples * sizeof (Sample));
1469                         while (++it != get_connections ().end ()) {
1470                                 source = static_cast<DummyAudioPort*>(*it);
1471                                 assert (source && source->is_output ());
1472                                 Sample* dst = buffer ();
1473                                 if (source->is_physical() && source->is_terminal()) {
1474                                         source->get_buffer(n_samples); // generate signal.
1475                                 }
1476                                 const Sample* src = source->const_buffer ();
1477                                 for (uint32_t s = 0; s < n_samples; ++s, ++dst, ++src) {
1478                                         *dst += *src;
1479                                 }
1480                         }
1481                 }
1482         } else if (is_output () && is_physical () && is_terminal()) {
1483                 if (!_gen_cycle) {
1484                         generate(n_samples);
1485                 }
1486         }
1487         return _buffer;
1488 }
1489
1490
1491 DummyMidiPort::DummyMidiPort (DummyAudioBackend &b, const std::string& name, PortFlags flags)
1492         : DummyPort (b, name, flags)
1493 {
1494         _buffer.clear ();
1495 }
1496
1497 DummyMidiPort::~DummyMidiPort () { }
1498
1499 struct MidiEventSorter {
1500         bool operator() (const boost::shared_ptr<DummyMidiEvent>& a, const boost::shared_ptr<DummyMidiEvent>& b) {
1501                 return *a < *b;
1502         }
1503 };
1504
1505 void* DummyMidiPort::get_buffer (pframes_t /* nframes */)
1506 {
1507         if (is_input ()) {
1508                 _buffer.clear ();
1509                 for (std::vector<DummyPort*>::const_iterator i = get_connections ().begin ();
1510                                 i != get_connections ().end ();
1511                                 ++i) {
1512                         const DummyMidiBuffer src = static_cast<const DummyMidiPort*>(*i)->const_buffer ();
1513                         for (DummyMidiBuffer::const_iterator it = src.begin (); it != src.end (); ++it) {
1514                                 _buffer.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (**it)));
1515                         }
1516                 }
1517                 std::sort (_buffer.begin (), _buffer.end (), MidiEventSorter());
1518         } else if (is_output () && is_physical () && is_terminal()) {
1519                 _buffer.clear ();
1520         }
1521         return &_buffer;
1522 }
1523
1524 DummyMidiEvent::DummyMidiEvent (const pframes_t timestamp, const uint8_t* data, size_t size)
1525         : _size (size)
1526         , _timestamp (timestamp)
1527         , _data (0)
1528 {
1529         if (size > 0) {
1530                 _data = (uint8_t*) malloc (size);
1531          memcpy (_data, data, size);
1532         }
1533 }
1534
1535 DummyMidiEvent::DummyMidiEvent (const DummyMidiEvent& other)
1536         : _size (other.size ())
1537         , _timestamp (other.timestamp ())
1538         , _data (0)
1539 {
1540         if (other.size () && other.const_data ()) {
1541                 _data = (uint8_t*) malloc (other.size ());
1542                 memcpy (_data, other.const_data (), other.size ());
1543         }
1544 };
1545
1546 DummyMidiEvent::~DummyMidiEvent () {
1547         free (_data);
1548 };