keep portmap & portindex in sync when renaming ports
[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 #ifdef PLATFORM_WINDOWS
27 #include <windows.h>
28 #include <pbd/windows_timer_utils.h>
29 #endif
30
31 #include "dummy_audiobackend.h"
32 #include "dummy_midi_seq.h"
33
34 #include "pbd/error.h"
35 #include "ardour/port_manager.h"
36 #include "i18n.h"
37
38 using namespace ARDOUR;
39
40 static std::string s_instance_name;
41 size_t DummyAudioBackend::_max_buffer_size = 8192;
42 std::vector<std::string> DummyAudioBackend::_midi_options;
43 std::vector<AudioBackend::DeviceStatus> DummyAudioBackend::_device_status;
44
45 std::vector<DummyAudioBackend::DriverSpeed> DummyAudioBackend::_driver_speed;
46
47 static int64_t _x_get_monotonic_usec() {
48 #ifdef PLATFORM_WINDOWS
49         return PBD::get_microseconds();
50 #endif
51         return g_get_monotonic_time();
52 }
53
54 DummyAudioBackend::DummyAudioBackend (AudioEngine& e, AudioBackendInfo& info)
55         : AudioBackend (e, info)
56         , _running (false)
57         , _freewheel (false)
58         , _freewheeling (false)
59         , _speedup (1.0)
60         , _device ("")
61         , _samplerate (48000)
62         , _samples_per_period (1024)
63         , _dsp_load (0)
64         , _n_inputs (0)
65         , _n_outputs (0)
66         , _n_midi_inputs (0)
67         , _n_midi_outputs (0)
68         , _midi_mode (MidiNoEvents)
69         , _systemic_input_latency (0)
70         , _systemic_output_latency (0)
71         , _processed_samples (0)
72         , _port_change_flag (false)
73 {
74         _instance_name = s_instance_name;
75         _device = _("Silence");
76         pthread_mutex_init (&_port_callback_mutex, 0);
77
78         if (_driver_speed.empty()) {
79                 _driver_speed.push_back (DriverSpeed (_("Half Speed"),   2.0f));
80                 _driver_speed.push_back (DriverSpeed (_("Normal Speed"), 1.0f));
81                 _driver_speed.push_back (DriverSpeed (_("Double Speed"), 0.5f));
82                 _driver_speed.push_back (DriverSpeed (_("5x Speed"),     0.2f));
83                 _driver_speed.push_back (DriverSpeed (_("10x Speed"),    0.1f));
84                 _driver_speed.push_back (DriverSpeed (_("15x Speed"),    0.06666f));
85                 _driver_speed.push_back (DriverSpeed (_("20x Speed"),    0.05f));
86                 _driver_speed.push_back (DriverSpeed (_("50x Speed"),    0.02f));
87         }
88
89 }
90
91 DummyAudioBackend::~DummyAudioBackend ()
92 {
93         pthread_mutex_destroy (&_port_callback_mutex);
94 }
95
96 /* AUDIOBACKEND API */
97
98 std::string
99 DummyAudioBackend::name () const
100 {
101         return X_("Dummy"); // internal name
102 }
103
104 bool
105 DummyAudioBackend::is_realtime () const
106 {
107         return false;
108 }
109
110 std::vector<AudioBackend::DeviceStatus>
111 DummyAudioBackend::enumerate_devices () const
112 {
113         if (_device_status.empty()) {
114                 _device_status.push_back (DeviceStatus (_("Silence"), true));
115                 _device_status.push_back (DeviceStatus (_("Sine Wave"), true));
116                 _device_status.push_back (DeviceStatus (_("Square Wave"), true));
117                 _device_status.push_back (DeviceStatus (_("Impulses"), true));
118                 _device_status.push_back (DeviceStatus (_("Uniform White Noise"), true));
119                 _device_status.push_back (DeviceStatus (_("Gaussian White Noise"), true));
120                 _device_status.push_back (DeviceStatus (_("Pink Noise"), true));
121                 _device_status.push_back (DeviceStatus (_("Pink Noise (low CPU)"), true));
122                 _device_status.push_back (DeviceStatus (_("Sine Sweep"), true));
123                 _device_status.push_back (DeviceStatus (_("Sine Sweep Swell"), true));
124                 _device_status.push_back (DeviceStatus (_("Square Sweep"), true));
125                 _device_status.push_back (DeviceStatus (_("Square Sweep Swell"), true));
126                 _device_status.push_back (DeviceStatus (_("Loopback"), true));
127         }
128         return _device_status;
129 }
130
131 std::vector<float>
132 DummyAudioBackend::available_sample_rates (const std::string&) const
133 {
134         std::vector<float> sr;
135         sr.push_back (8000.0);
136         sr.push_back (22050.0);
137         sr.push_back (24000.0);
138         sr.push_back (44100.0);
139         sr.push_back (48000.0);
140         sr.push_back (88200.0);
141         sr.push_back (96000.0);
142         sr.push_back (176400.0);
143         sr.push_back (192000.0);
144         return sr;
145 }
146
147 std::vector<uint32_t>
148 DummyAudioBackend::available_buffer_sizes (const std::string&) const
149 {
150         std::vector<uint32_t> bs;
151         bs.push_back (4);
152         bs.push_back (8);
153         bs.push_back (16);
154         bs.push_back (32);
155         bs.push_back (64);
156         bs.push_back (128);
157         bs.push_back (256);
158         bs.push_back (512);
159         bs.push_back (1024);
160         bs.push_back (2048);
161         bs.push_back (4096);
162         bs.push_back (8192);
163         return bs;
164 }
165
166 uint32_t
167 DummyAudioBackend::available_input_channel_count (const std::string&) const
168 {
169         return 128;
170 }
171
172 uint32_t
173 DummyAudioBackend::available_output_channel_count (const std::string&) const
174 {
175         return 128;
176 }
177
178 bool
179 DummyAudioBackend::can_change_sample_rate_when_running () const
180 {
181         return true;
182 }
183
184 bool
185 DummyAudioBackend::can_change_buffer_size_when_running () const
186 {
187         return true;
188 }
189
190 std::vector<std::string>
191 DummyAudioBackend::enumerate_drivers () const
192 {
193         std::vector<std::string> speed_drivers;
194         for (std::vector<DriverSpeed>::const_iterator it = _driver_speed.begin () ; it != _driver_speed.end (); ++it) {
195                 speed_drivers.push_back (it->name);
196         }
197         return speed_drivers;
198 }
199
200 std::string
201 DummyAudioBackend::driver_name () const
202 {
203         for (std::vector<DriverSpeed>::const_iterator it = _driver_speed.begin () ; it != _driver_speed.end (); ++it) {
204                 if (rintf (1e6f * _speedup) == rintf (1e6f * it->speedup)) {
205                         return it->name;
206                 }
207         }
208         assert (0);
209         return _("Normal Speed");
210 }
211
212 int
213 DummyAudioBackend::set_driver (const std::string& d)
214 {
215         for (std::vector<DriverSpeed>::const_iterator it = _driver_speed.begin () ; it != _driver_speed.end (); ++it) {
216                 if (d == it->name) {
217                         _speedup = it->speedup;
218                         return 0;
219                 }
220         }
221         assert (0);
222         return -1;
223 }
224
225 int
226 DummyAudioBackend::set_device_name (const std::string& d)
227 {
228         _device = d;
229         return 0;
230 }
231
232 int
233 DummyAudioBackend::set_sample_rate (float sr)
234 {
235         if (sr <= 0) { return -1; }
236         _samplerate = sr;
237         engine.sample_rate_change (sr);
238         return 0;
239 }
240
241 int
242 DummyAudioBackend::set_buffer_size (uint32_t bs)
243 {
244         if (bs <= 0 || bs > _max_buffer_size) {
245                 return -1;
246         }
247         _samples_per_period = bs;
248
249         /* update port latencies
250          * with 'Loopback' there is exactly once cycle latency,
251          * divide it between In + Out;
252          */
253         LatencyRange lr;
254         lr.min = lr.max = _systemic_input_latency;
255         for (std::vector<DummyAudioPort*>::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) {
256                 set_latency_range (*it, false, lr);
257         }
258         for (std::vector<DummyMidiPort*>::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it) {
259                 set_latency_range (*it, false, lr);
260         }
261
262         lr.min = lr.max = _systemic_output_latency;
263         for (std::vector<DummyAudioPort*>::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it) {
264                 set_latency_range (*it, true, lr);
265         }
266         for (std::vector<DummyMidiPort*>::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it) {
267                 set_latency_range (*it, true, lr);
268         }
269
270         engine.buffer_size_change (bs);
271         return 0;
272 }
273
274 int
275 DummyAudioBackend::set_interleaved (bool yn)
276 {
277         if (!yn) { return 0; }
278         return -1;
279 }
280
281 int
282 DummyAudioBackend::set_input_channels (uint32_t cc)
283 {
284         _n_inputs = cc;
285         return 0;
286 }
287
288 int
289 DummyAudioBackend::set_output_channels (uint32_t cc)
290 {
291         _n_outputs = cc;
292         return 0;
293 }
294
295 int
296 DummyAudioBackend::set_systemic_input_latency (uint32_t sl)
297 {
298         _systemic_input_latency = sl;
299         return 0;
300 }
301
302 int
303 DummyAudioBackend::set_systemic_output_latency (uint32_t sl)
304 {
305         _systemic_output_latency = sl;
306         return 0;
307 }
308
309 /* Retrieving parameters */
310 std::string
311 DummyAudioBackend::device_name () const
312 {
313         return _device;
314 }
315
316 float
317 DummyAudioBackend::sample_rate () const
318 {
319         return _samplerate;
320 }
321
322 uint32_t
323 DummyAudioBackend::buffer_size () const
324 {
325         return _samples_per_period;
326 }
327
328 bool
329 DummyAudioBackend::interleaved () const
330 {
331         return false;
332 }
333
334 uint32_t
335 DummyAudioBackend::input_channels () const
336 {
337         return _n_inputs;
338 }
339
340 uint32_t
341 DummyAudioBackend::output_channels () const
342 {
343         return _n_outputs;
344 }
345
346 uint32_t
347 DummyAudioBackend::systemic_input_latency () const
348 {
349         return _systemic_input_latency;
350 }
351
352 uint32_t
353 DummyAudioBackend::systemic_output_latency () const
354 {
355         return _systemic_output_latency;
356 }
357
358
359 /* MIDI */
360 std::vector<std::string>
361 DummyAudioBackend::enumerate_midi_options () const
362 {
363         if (_midi_options.empty()) {
364                 _midi_options.push_back (_("1 in, 1 out, Silence"));
365                 _midi_options.push_back (_("2 in, 2 out, Silence"));
366                 _midi_options.push_back (_("8 in, 8 out, Silence"));
367                 _midi_options.push_back (_("Midi Event Generators"));
368                 _midi_options.push_back (_("8 in, 8 out, Loopback"));
369                 _midi_options.push_back (_("MIDI to Audio, Loopback"));
370                 _midi_options.push_back (_("No MIDI I/O"));
371         }
372         return _midi_options;
373 }
374
375 int
376 DummyAudioBackend::set_midi_option (const std::string& opt)
377 {
378         _midi_mode = MidiNoEvents;
379         if (opt == _("1 in, 1 out, Silence")) {
380                 _n_midi_inputs = _n_midi_outputs = 1;
381         }
382         else if (opt == _("2 in, 2 out, Silence")) {
383                 _n_midi_inputs = _n_midi_outputs = 2;
384         }
385         else if (opt == _("8 in, 8 out, Silence")) {
386                 _n_midi_inputs = _n_midi_outputs = 8;
387         }
388         else if (opt == _("Midi Event Generators")) {
389                 _n_midi_inputs = _n_midi_outputs = NUM_MIDI_EVENT_GENERATORS;
390                 _midi_mode = MidiGenerator;
391         }
392         else if (opt == _("8 in, 8 out, Loopback")) {
393                 _n_midi_inputs = _n_midi_outputs = 8;
394                 _midi_mode = MidiLoopback;
395         }
396         else if (opt == _("MIDI to Audio, Loopback")) {
397                 _n_midi_inputs = _n_midi_outputs = UINT32_MAX;
398                 _midi_mode = MidiToAudio;
399         }
400         else {
401                 _n_midi_inputs = _n_midi_outputs = 0;
402         }
403         return 0;
404 }
405
406 std::string
407 DummyAudioBackend::midi_option () const
408 {
409         return ""; // TODO
410 }
411
412 /* State Control */
413
414 static void * pthread_process (void *arg)
415 {
416         DummyAudioBackend *d = static_cast<DummyAudioBackend *>(arg);
417         d->main_process_thread ();
418         pthread_exit (0);
419         return 0;
420 }
421
422 int
423 DummyAudioBackend::_start (bool /*for_latency_measurement*/)
424 {
425         if (_running) {
426                 PBD::error << _("DummyAudioBackend: already active.") << endmsg;
427                 return BackendReinitializationError;
428         }
429
430         if (_ports.size () || _portmap.size ()) {
431                 PBD::warning << _("DummyAudioBackend: recovering from unclean shutdown, port registry is not empty.") << endmsg;
432                 for (PortIndex::const_iterator it = _ports.begin (); it != _ports.end (); ++it) {
433                         PBD::info << _("DummyAudioBackend: port '") << (*it)->name () << "' exists." << endmsg;
434                 }
435                 for (PortMap::const_iterator it = _portmap.begin (); it != _portmap.end (); ++it) {
436                         PBD::info << _("DummyAudioBackend: portmap '") << (*it).first << "' exists." << endmsg;
437                 }
438                 _system_inputs.clear();
439                 _system_outputs.clear();
440                 _system_midi_in.clear();
441                 _system_midi_out.clear();
442                 _ports.clear();
443                 _portmap.clear();
444         }
445
446         if (register_system_ports()) {
447                 PBD::error << _("DummyAudioBackend: failed to register system ports.") << endmsg;
448                 return PortRegistrationError;
449         }
450
451         engine.sample_rate_change (_samplerate);
452         engine.buffer_size_change (_samples_per_period);
453
454         _dsp_load_calc.set_max_time (_samplerate, _samples_per_period);
455
456         if (engine.reestablish_ports ()) {
457                 PBD::error << _("DummyAudioBackend: Could not re-establish ports.") << endmsg;
458                 stop ();
459                 return PortReconnectError;
460         }
461
462         engine.reconnect_ports ();
463         _port_change_flag = false;
464
465         if (pthread_create (&_main_thread, NULL, pthread_process, this)) {
466                 PBD::error << _("DummyAudioBackend: cannot start.") << endmsg;
467         }
468
469         int timeout = 5000;
470         while (!_running && --timeout > 0) { Glib::usleep (1000); }
471
472         if (timeout == 0 || !_running) {
473                 PBD::error << _("DummyAudioBackend: failed to start process thread.") << endmsg;
474                 return ProcessThreadStartError;
475         }
476
477         return NoError;
478 }
479
480 int
481 DummyAudioBackend::stop ()
482 {
483         void *status;
484         if (!_running) {
485                 return 0;
486         }
487
488         _running = false;
489         if (pthread_join (_main_thread, &status)) {
490                 PBD::error << _("DummyAudioBackend: failed to terminate.") << endmsg;
491                 return -1;
492         }
493         unregister_ports();
494         return 0;
495 }
496
497 int
498 DummyAudioBackend::freewheel (bool onoff)
499 {
500         _freewheeling = onoff;
501         return 0;
502 }
503
504 float
505 DummyAudioBackend::dsp_load () const
506 {
507         return 100.f * _dsp_load;
508 }
509
510 size_t
511 DummyAudioBackend::raw_buffer_size (DataType t)
512 {
513         switch (t) {
514                 case DataType::AUDIO:
515                         return _samples_per_period * sizeof(Sample);
516                 case DataType::MIDI:
517                         return _max_buffer_size; // XXX not really limited
518         }
519         return 0;
520 }
521
522 /* Process time */
523 framepos_t
524 DummyAudioBackend::sample_time ()
525 {
526         return _processed_samples;
527 }
528
529 framepos_t
530 DummyAudioBackend::sample_time_at_cycle_start ()
531 {
532         return _processed_samples;
533 }
534
535 pframes_t
536 DummyAudioBackend::samples_since_cycle_start ()
537 {
538         return 0;
539 }
540
541
542 void *
543 DummyAudioBackend::dummy_process_thread (void *arg)
544 {
545         ThreadData* td = reinterpret_cast<ThreadData*> (arg);
546         boost::function<void ()> f = td->f;
547         delete td;
548         f ();
549         return 0;
550 }
551
552 int
553 DummyAudioBackend::create_process_thread (boost::function<void()> func)
554 {
555         pthread_t thread_id;
556         pthread_attr_t attr;
557         size_t stacksize = 100000;
558
559         pthread_attr_init (&attr);
560         pthread_attr_setstacksize (&attr, stacksize);
561         ThreadData* td = new ThreadData (this, func, stacksize);
562
563         if (pthread_create (&thread_id, &attr, dummy_process_thread, td)) {
564                 PBD::error << _("AudioEngine: cannot create process thread.") << endmsg;
565                 pthread_attr_destroy (&attr);
566                 return -1;
567         }
568         pthread_attr_destroy (&attr);
569
570         _threads.push_back (thread_id);
571         return 0;
572 }
573
574 int
575 DummyAudioBackend::join_process_threads ()
576 {
577         int rv = 0;
578
579         for (std::vector<pthread_t>::const_iterator i = _threads.begin (); i != _threads.end (); ++i)
580         {
581                 void *status;
582                 if (pthread_join (*i, &status)) {
583                         PBD::error << _("AudioEngine: cannot terminate process thread.") << endmsg;
584                         rv -= 1;
585                 }
586         }
587         _threads.clear ();
588         return rv;
589 }
590
591 bool
592 DummyAudioBackend::in_process_thread ()
593 {
594         if (pthread_equal (_main_thread, pthread_self()) != 0) {
595                 return true;
596         }
597
598         for (std::vector<pthread_t>::const_iterator i = _threads.begin (); i != _threads.end (); ++i)
599         {
600                 if (pthread_equal (*i, pthread_self ()) != 0) {
601                         return true;
602                 }
603         }
604         return false;
605 }
606
607 uint32_t
608 DummyAudioBackend::process_thread_count ()
609 {
610         return _threads.size ();
611 }
612
613 void
614 DummyAudioBackend::update_latencies ()
615 {
616         // trigger latency callback in RT thread (locked graph)
617         port_connect_add_remove_callback();
618 }
619
620 /* PORTENGINE API */
621
622 void*
623 DummyAudioBackend::private_handle () const
624 {
625         return NULL;
626 }
627
628 const std::string&
629 DummyAudioBackend::my_name () const
630 {
631         return _instance_name;
632 }
633
634 bool
635 DummyAudioBackend::available () const
636 {
637         return true;
638 }
639
640 uint32_t
641 DummyAudioBackend::port_name_size () const
642 {
643         return 256;
644 }
645
646 int
647 DummyAudioBackend::set_port_name (PortEngine::PortHandle port, const std::string& name)
648 {
649         std::string newname (_instance_name + ":" + name);
650
651         if (!valid_port (port)) {
652                 PBD::error << _("DummyBackend::set_port_name: Invalid Port(s)") << endmsg;
653                 return -1;
654         }
655
656         if (find_port (newname)) {
657                 PBD::error << _("DummyBackend::set_port_name: Port with given name already exists") << endmsg;
658                 return -1;
659         }
660
661         DummyPort* p = static_cast<DummyPort*>(port);
662         _portmap.erase (p->name());
663         _portmap.insert (make_pair (newname, p));
664         return p->set_name (newname);
665 }
666
667 std::string
668 DummyAudioBackend::get_port_name (PortEngine::PortHandle port) const
669 {
670         if (!valid_port (port)) {
671                 PBD::error << _("DummyBackend::get_port_name: Invalid Port(s)") << endmsg;
672                 return std::string ();
673         }
674         return static_cast<DummyPort*>(port)->name ();
675 }
676
677 int
678 DummyAudioBackend::get_port_property (PortHandle port, const std::string& key, std::string& value, std::string& type) const
679 {
680         if (!valid_port (port)) {
681                 PBD::warning << _("DummyBackend::get_port_property: Invalid Port(s)") << endmsg;
682                 return -1;
683         }
684         if (key == "http://jackaudio.org/metadata/pretty-name") {
685                 type = "";
686                 value = static_cast<DummyPort*>(port)->pretty_name ();
687                 if (!value.empty()) {
688                         return 0;
689                 }
690         }
691         return -1;
692 }
693
694 int
695 DummyAudioBackend::set_port_property (PortHandle port, const std::string& key, const std::string& value, const std::string& type)
696 {
697         if (!valid_port (port)) {
698                 PBD::warning << _("DummyBackend::set_port_property: Invalid Port(s)") << endmsg;
699                 return -1;
700         }
701         if (key == "http://jackaudio.org/metadata/pretty-name" && type.empty ()) {
702                 static_cast<DummyPort*>(port)->set_pretty_name (value);
703                 return 0;
704         }
705         return -1;
706 }
707
708 PortEngine::PortHandle
709 DummyAudioBackend::get_port_by_name (const std::string& name) const
710 {
711         PortHandle port = (PortHandle) find_port (name);
712         return port;
713 }
714
715 int
716 DummyAudioBackend::get_ports (
717                 const std::string& port_name_pattern,
718                 DataType type, PortFlags flags,
719                 std::vector<std::string>& port_names) const
720 {
721         int rv = 0;
722         regex_t port_regex;
723         bool use_regexp = false;
724         if (port_name_pattern.size () > 0) {
725                 if (!regcomp (&port_regex, port_name_pattern.c_str (), REG_EXTENDED|REG_NOSUB)) {
726                         use_regexp = true;
727                 }
728         }
729
730         for (PortIndex::iterator i = _ports.begin (); i != _ports.end (); ++i) {
731                 DummyPort* port = *i;
732                 if ((port->type () == type) && flags == (port->flags () & flags)) {
733                         if (!use_regexp || !regexec (&port_regex, port->name ().c_str (), 0, NULL, 0)) {
734                                 port_names.push_back (port->name ());
735                                 ++rv;
736                         }
737                 }
738         }
739         if (use_regexp) {
740                 regfree (&port_regex);
741         }
742         return rv;
743 }
744
745 DataType
746 DummyAudioBackend::port_data_type (PortEngine::PortHandle port) const
747 {
748         if (!valid_port (port)) {
749                 return DataType::NIL;
750         }
751         return static_cast<DummyPort*>(port)->type ();
752 }
753
754 PortEngine::PortHandle
755 DummyAudioBackend::register_port (
756                 const std::string& name,
757                 ARDOUR::DataType type,
758                 ARDOUR::PortFlags flags)
759 {
760         if (name.size () == 0) { return 0; }
761         if (flags & IsPhysical) { return 0; }
762         if (!_running) {
763                 PBD::info << _("DummyBackend::register_port: Engine is not running.") << endmsg;
764         }
765         return add_port (_instance_name + ":" + name, type, flags);
766 }
767
768 PortEngine::PortHandle
769 DummyAudioBackend::add_port (
770                 const std::string& name,
771                 ARDOUR::DataType type,
772                 ARDOUR::PortFlags flags)
773 {
774         assert(name.size ());
775         if (find_port (name)) {
776                 PBD::error << _("DummyBackend::register_port: Port already exists:")
777                                 << " (" << name << ")" << endmsg;
778                 return 0;
779         }
780         DummyPort* port = NULL;
781         switch (type) {
782                 case DataType::AUDIO:
783                         port = new DummyAudioPort (*this, name, flags);
784                         break;
785                 case DataType::MIDI:
786                         port = new DummyMidiPort (*this, name, flags);
787                         break;
788                 default:
789                         PBD::error << _("DummyBackend::register_port: Invalid Data Type.") << endmsg;
790                         return 0;
791         }
792
793         _ports.insert (port);
794         _portmap.insert (make_pair (name, port));
795
796         return port;
797 }
798
799 void
800 DummyAudioBackend::unregister_port (PortEngine::PortHandle port_handle)
801 {
802         if (!_running) {
803                 PBD::info << _("DummyBackend::unregister_port: Engine is not running.") << endmsg;
804                 assert (!valid_port (port_handle));
805                 return;
806         }
807         DummyPort* port = static_cast<DummyPort*>(port_handle);
808         PortIndex::iterator i = _ports.find (static_cast<DummyPort*>(port_handle));
809         if (i == _ports.end ()) {
810                 PBD::error << _("DummyBackend::unregister_port: Failed to find port") << endmsg;
811                 return;
812         }
813         disconnect_all(port_handle);
814         _portmap.erase (port->name());
815         _ports.erase (i);
816         delete port;
817 }
818
819 int
820 DummyAudioBackend::register_system_ports()
821 {
822         LatencyRange lr;
823         enum DummyAudioPort::GeneratorType gt;
824         if (_device == _("Uniform White Noise")) {
825                 gt = DummyAudioPort::UniformWhiteNoise;
826         } else if (_device == _("Gaussian White Noise")) {
827                 gt = DummyAudioPort::GaussianWhiteNoise;
828         } else if (_device == _("Pink Noise")) {
829                 gt = DummyAudioPort::PinkNoise;
830         } else if (_device == _("Pink Noise (low CPU)")) {
831                 gt = DummyAudioPort::PonyNoise;
832         } else if (_device == _("Sine Wave")) {
833                 gt = DummyAudioPort::SineWave;
834         } else if (_device == _("Square Wave")) {
835                 gt = DummyAudioPort::SquareWave;
836         } else if (_device == _("Impulses")) {
837                 gt = DummyAudioPort::KronekerDelta;
838         } else if (_device == _("Sine Sweep")) {
839                 gt = DummyAudioPort::SineSweep;
840         } else if (_device == _("Sine Sweep Swell")) {
841                 gt = DummyAudioPort::SineSweepSwell;
842         } else if (_device == _("Square Sweep")) {
843                 gt = DummyAudioPort::SquareSweep;
844         } else if (_device == _("Square Sweep Swell")) {
845                 gt = DummyAudioPort::SquareSweepSwell;
846         } else if (_device == _("Loopback")) {
847                 gt = DummyAudioPort::Loopback;
848         } else {
849                 gt = DummyAudioPort::Silence;
850         }
851
852         if (_midi_mode == MidiToAudio) {
853                 gt = DummyAudioPort::Loopback;
854         }
855
856         const int a_ins = _n_inputs > 0 ? _n_inputs : 8;
857         const int a_out = _n_outputs > 0 ? _n_outputs : 8;
858         const int m_ins = _n_midi_inputs == UINT_MAX ? 0 : _n_midi_inputs;
859         const int m_out = _n_midi_outputs == UINT_MAX ? a_ins : _n_midi_outputs;
860
861
862         /* audio ports */
863         lr.min = lr.max = _systemic_input_latency;
864         for (int i = 1; i <= a_ins; ++i) {
865                 char tmp[64];
866                 snprintf(tmp, sizeof(tmp), "system:capture_%d", i);
867                 PortHandle p = add_port(std::string(tmp), DataType::AUDIO, static_cast<PortFlags>(IsOutput | IsPhysical | IsTerminal));
868                 if (!p) return -1;
869                 set_latency_range (p, false, lr);
870                 _system_inputs.push_back (static_cast<DummyAudioPort*>(p));
871                 static_cast<DummyAudioPort*>(p)->setup_generator (gt, _samplerate);
872         }
873
874         lr.min = lr.max = _systemic_output_latency;
875         for (int i = 1; i <= a_out; ++i) {
876                 char tmp[64];
877                 snprintf(tmp, sizeof(tmp), "system:playback_%d", i);
878                 PortHandle p = add_port(std::string(tmp), DataType::AUDIO, static_cast<PortFlags>(IsInput | IsPhysical | IsTerminal));
879                 if (!p) return -1;
880                 set_latency_range (p, true, lr);
881                 _system_outputs.push_back (static_cast<DummyAudioPort*>(p));
882         }
883
884         /* midi ports */
885         lr.min = lr.max = _systemic_input_latency;
886         for (int i = 0; i < m_ins; ++i) {
887                 char tmp[64];
888                 snprintf(tmp, sizeof(tmp), "system:midi_capture_%d", i+1);
889                 PortHandle p = add_port(std::string(tmp), DataType::MIDI, static_cast<PortFlags>(IsOutput | IsPhysical | IsTerminal));
890                 if (!p) return -1;
891                 set_latency_range (p, false, lr);
892                 _system_midi_in.push_back (static_cast<DummyMidiPort*>(p));
893                 if (_midi_mode == MidiGenerator) {
894                         static_cast<DummyMidiPort*>(p)->setup_generator (i % NUM_MIDI_EVENT_GENERATORS, _samplerate);
895                         static_cast<DummyMidiPort*>(p)->set_pretty_name (DummyMidiData::sequence_names[i % NUM_MIDI_EVENT_GENERATORS]);
896                 }
897         }
898
899         lr.min = lr.max = _systemic_output_latency;
900         for (int i = 1; i <= m_out; ++i) {
901                 char tmp[64];
902                 snprintf(tmp, sizeof(tmp), "system:midi_playback_%d", i);
903                 PortHandle p = add_port(std::string(tmp), DataType::MIDI, static_cast<PortFlags>(IsInput | IsPhysical | IsTerminal));
904                 if (!p) return -1;
905                 set_latency_range (p, true, lr);
906                 _system_midi_out.push_back (static_cast<DummyMidiPort*>(p));
907
908                 if (_device == _("Loopback") && _midi_mode == MidiToAudio) {
909                         std::stringstream ss;
910                         ss << "Midi2Audio";
911                         for (int apc = 0; apc < (int)_system_inputs.size(); ++apc) {
912                                 if ((apc % m_out) + 1 == i) {
913                                         ss << " >" << (apc + 1);
914                                 }
915                         }
916                         static_cast<DummyMidiPort*>(p)->set_pretty_name (ss.str());
917                 }
918         }
919         return 0;
920 }
921
922 void
923 DummyAudioBackend::unregister_ports (bool system_only)
924 {
925         _system_inputs.clear();
926         _system_outputs.clear();
927         _system_midi_in.clear();
928         _system_midi_out.clear();
929
930         for (PortIndex::iterator i = _ports.begin (); i != _ports.end ();) {
931                 PortIndex::iterator cur = i++;
932                 DummyPort* port = *cur;
933                 if (! system_only || (port->is_physical () && port->is_terminal ())) {
934                         port->disconnect_all ();
935                         _portmap.erase (port->name());
936                         delete port;
937                         _ports.erase (cur);
938                 }
939         }
940 }
941
942 int
943 DummyAudioBackend::connect (const std::string& src, const std::string& dst)
944 {
945         DummyPort* src_port = find_port (src);
946         DummyPort* dst_port = find_port (dst);
947
948         if (!src_port) {
949                 PBD::error << _("DummyBackend::connect: Invalid Source port:")
950                                 << " (" << src <<")" << endmsg;
951                 return -1;
952         }
953         if (!dst_port) {
954                 PBD::error << _("DummyBackend::connect: Invalid Destination port:")
955                         << " (" << dst <<")" << endmsg;
956                 return -1;
957         }
958         return src_port->connect (dst_port);
959 }
960
961 int
962 DummyAudioBackend::disconnect (const std::string& src, const std::string& dst)
963 {
964         DummyPort* src_port = find_port (src);
965         DummyPort* dst_port = find_port (dst);
966
967         if (!src_port || !dst_port) {
968                 PBD::error << _("DummyBackend::disconnect: Invalid Port(s)") << endmsg;
969                 return -1;
970         }
971         return src_port->disconnect (dst_port);
972 }
973
974 int
975 DummyAudioBackend::connect (PortEngine::PortHandle src, const std::string& dst)
976 {
977         DummyPort* dst_port = find_port (dst);
978         if (!valid_port (src)) {
979                 PBD::error << _("DummyBackend::connect: Invalid Source Port Handle") << endmsg;
980                 return -1;
981         }
982         if (!dst_port) {
983                 PBD::error << _("DummyBackend::connect: Invalid Destination Port")
984                         << " (" << dst << ")" << endmsg;
985                 return -1;
986         }
987         return static_cast<DummyPort*>(src)->connect (dst_port);
988 }
989
990 int
991 DummyAudioBackend::disconnect (PortEngine::PortHandle src, const std::string& dst)
992 {
993         DummyPort* dst_port = find_port (dst);
994         if (!valid_port (src) || !dst_port) {
995                 PBD::error << _("DummyBackend::disconnect: Invalid Port(s)") << endmsg;
996                 return -1;
997         }
998         return static_cast<DummyPort*>(src)->disconnect (dst_port);
999 }
1000
1001 int
1002 DummyAudioBackend::disconnect_all (PortEngine::PortHandle port)
1003 {
1004         if (!valid_port (port)) {
1005                 PBD::error << _("DummyBackend::disconnect_all: Invalid Port") << endmsg;
1006                 return -1;
1007         }
1008         static_cast<DummyPort*>(port)->disconnect_all ();
1009         return 0;
1010 }
1011
1012 bool
1013 DummyAudioBackend::connected (PortEngine::PortHandle port, bool /* process_callback_safe*/)
1014 {
1015         if (!valid_port (port)) {
1016                 PBD::error << _("DummyBackend::disconnect_all: Invalid Port") << endmsg;
1017                 return false;
1018         }
1019         return static_cast<DummyPort*>(port)->is_connected ();
1020 }
1021
1022 bool
1023 DummyAudioBackend::connected_to (PortEngine::PortHandle src, const std::string& dst, bool /*process_callback_safe*/)
1024 {
1025         DummyPort* dst_port = find_port (dst);
1026 #ifndef NDEBUG
1027         if (!valid_port (src) || !dst_port) {
1028                 PBD::error << _("DummyBackend::connected_to: Invalid Port") << endmsg;
1029                 return false;
1030         }
1031 #endif
1032         return static_cast<DummyPort*>(src)->is_connected (dst_port);
1033 }
1034
1035 bool
1036 DummyAudioBackend::physically_connected (PortEngine::PortHandle port, bool /*process_callback_safe*/)
1037 {
1038         if (!valid_port (port)) {
1039                 PBD::error << _("DummyBackend::physically_connected: Invalid Port") << endmsg;
1040                 return false;
1041         }
1042         return static_cast<DummyPort*>(port)->is_physically_connected ();
1043 }
1044
1045 int
1046 DummyAudioBackend::get_connections (PortEngine::PortHandle port, std::vector<std::string>& names, bool /*process_callback_safe*/)
1047 {
1048         if (!valid_port (port)) {
1049                 PBD::error << _("DummyBackend::get_connections: Invalid Port") << endmsg;
1050                 return -1;
1051         }
1052
1053         assert (0 == names.size ());
1054
1055         const std::set<DummyPort*>& connected_ports = static_cast<DummyPort*>(port)->get_connections ();
1056
1057         for (std::set<DummyPort*>::const_iterator i = connected_ports.begin (); i != connected_ports.end (); ++i) {
1058                 names.push_back ((*i)->name ());
1059         }
1060
1061         return (int)names.size ();
1062 }
1063
1064 /* MIDI */
1065 int
1066 DummyAudioBackend::midi_event_get (
1067                 pframes_t& timestamp,
1068                 size_t& size, uint8_t** buf, void* port_buffer,
1069                 uint32_t event_index)
1070 {
1071         assert (buf && port_buffer);
1072         DummyMidiBuffer& source = * static_cast<DummyMidiBuffer*>(port_buffer);
1073         if (event_index >= source.size ()) {
1074                 return -1;
1075         }
1076         DummyMidiEvent * const event = source[event_index].get ();
1077
1078         timestamp = event->timestamp ();
1079         size = event->size ();
1080         *buf = event->data ();
1081         return 0;
1082 }
1083
1084 int
1085 DummyAudioBackend::midi_event_put (
1086                 void* port_buffer,
1087                 pframes_t timestamp,
1088                 const uint8_t* buffer, size_t size)
1089 {
1090         assert (buffer && port_buffer);
1091         DummyMidiBuffer& dst = * static_cast<DummyMidiBuffer*>(port_buffer);
1092         if (dst.size () && (pframes_t)dst.back ()->timestamp () > timestamp) {
1093                 // nevermind, ::get_buffer() sorts events, but always print warning
1094                 fprintf (stderr, "DummyMidiBuffer: it's too late for this event %d > %d.\n", (pframes_t)dst.back ()->timestamp (), timestamp);
1095         }
1096         dst.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (timestamp, buffer, size)));
1097 #if 0 // DEBUG MIDI EVENTS
1098         printf("DummyAudioBackend::midi_event_put %d, %zu: ", timestamp, size);
1099         for (size_t xx = 0; xx < size; ++xx) {
1100                 printf(" %02x", buffer[xx]);
1101         }
1102         printf("\n");
1103 #endif
1104         return 0;
1105 }
1106
1107 uint32_t
1108 DummyAudioBackend::get_midi_event_count (void* port_buffer)
1109 {
1110         assert (port_buffer);
1111         return static_cast<DummyMidiBuffer*>(port_buffer)->size ();
1112 }
1113
1114 void
1115 DummyAudioBackend::midi_clear (void* port_buffer)
1116 {
1117         assert (port_buffer);
1118         DummyMidiBuffer * buf = static_cast<DummyMidiBuffer*>(port_buffer);
1119         assert (buf);
1120         buf->clear ();
1121 }
1122
1123 /* Monitoring */
1124
1125 bool
1126 DummyAudioBackend::can_monitor_input () const
1127 {
1128         return false;
1129 }
1130
1131 int
1132 DummyAudioBackend::request_input_monitoring (PortEngine::PortHandle, bool)
1133 {
1134         return -1;
1135 }
1136
1137 int
1138 DummyAudioBackend::ensure_input_monitoring (PortEngine::PortHandle, bool)
1139 {
1140         return -1;
1141 }
1142
1143 bool
1144 DummyAudioBackend::monitoring_input (PortEngine::PortHandle)
1145 {
1146         return false;
1147 }
1148
1149 /* Latency management */
1150
1151 void
1152 DummyAudioBackend::set_latency_range (PortEngine::PortHandle port, bool for_playback, LatencyRange latency_range)
1153 {
1154         if (!valid_port (port)) {
1155                 PBD::error << _("DummyPort::set_latency_range (): invalid port.") << endmsg;
1156         }
1157         static_cast<DummyPort*>(port)->set_latency_range (latency_range, for_playback);
1158 }
1159
1160 LatencyRange
1161 DummyAudioBackend::get_latency_range (PortEngine::PortHandle port, bool for_playback)
1162 {
1163         LatencyRange r;
1164         if (!valid_port (port)) {
1165                 PBD::error << _("DummyPort::get_latency_range (): invalid port.") << endmsg;
1166                 r.min = 0;
1167                 r.max = 0;
1168                 return r;
1169         }
1170         DummyPort *p =  static_cast<DummyPort*>(port);
1171         assert(p);
1172
1173         r = p->latency_range (for_playback);
1174         if (p->is_physical() && p->is_terminal()) {
1175                 if (p->is_input() && for_playback) {
1176                         const size_t l_in = _samples_per_period * .25;
1177                         r.min += l_in;
1178                         r.max += l_in;
1179                 }
1180                 if (p->is_output() && !for_playback) {
1181                         /* with 'Loopback' there is exactly once cycle latency, divide it between In + Out; */
1182                         const size_t l_in = _samples_per_period * .25;
1183                         const size_t l_out = _samples_per_period - l_in;
1184                         r.min += l_out;
1185                         r.max += l_out;
1186                 }
1187         }
1188         return r;
1189 }
1190
1191 /* Discovering physical ports */
1192
1193 bool
1194 DummyAudioBackend::port_is_physical (PortEngine::PortHandle port) const
1195 {
1196         if (!valid_port (port)) {
1197                 PBD::error << _("DummyPort::port_is_physical (): invalid port.") << endmsg;
1198                 return false;
1199         }
1200         return static_cast<DummyPort*>(port)->is_physical ();
1201 }
1202
1203 void
1204 DummyAudioBackend::get_physical_outputs (DataType type, std::vector<std::string>& port_names)
1205 {
1206         for (PortIndex::iterator i = _ports.begin (); i != _ports.end (); ++i) {
1207                 DummyPort* port = *i;
1208                 if ((port->type () == type) && port->is_input () && port->is_physical ()) {
1209                         port_names.push_back (port->name ());
1210                 }
1211         }
1212 }
1213
1214 void
1215 DummyAudioBackend::get_physical_inputs (DataType type, std::vector<std::string>& port_names)
1216 {
1217         for (PortIndex::iterator i = _ports.begin (); i != _ports.end (); ++i) {
1218                 DummyPort* port = *i;
1219                 if ((port->type () == type) && port->is_output () && port->is_physical ()) {
1220                         port_names.push_back (port->name ());
1221                 }
1222         }
1223 }
1224
1225 ChanCount
1226 DummyAudioBackend::n_physical_outputs () const
1227 {
1228         int n_midi = 0;
1229         int n_audio = 0;
1230         for (PortIndex::iterator i = _ports.begin (); i != _ports.end (); ++i) {
1231                 DummyPort* port = *i;
1232                 if (port->is_output () && port->is_physical ()) {
1233                         switch (port->type ()) {
1234                                 case DataType::AUDIO: ++n_audio; break;
1235                                 case DataType::MIDI: ++n_midi; break;
1236                                 default: break;
1237                         }
1238                 }
1239         }
1240         ChanCount cc;
1241         cc.set (DataType::AUDIO, n_audio);
1242         cc.set (DataType::MIDI, n_midi);
1243         return cc;
1244 }
1245
1246 ChanCount
1247 DummyAudioBackend::n_physical_inputs () const
1248 {
1249         int n_midi = 0;
1250         int n_audio = 0;
1251         for (PortIndex::iterator i = _ports.begin (); i != _ports.end (); ++i) {
1252                 DummyPort* port = *i;
1253                 if (port->is_input () && port->is_physical ()) {
1254                         switch (port->type ()) {
1255                                 case DataType::AUDIO: ++n_audio; break;
1256                                 case DataType::MIDI: ++n_midi; break;
1257                                 default: break;
1258                         }
1259                 }
1260         }
1261         ChanCount cc;
1262         cc.set (DataType::AUDIO, n_audio);
1263         cc.set (DataType::MIDI, n_midi);
1264         return cc;
1265 }
1266
1267 /* Getting access to the data buffer for a port */
1268
1269 void*
1270 DummyAudioBackend::get_buffer (PortEngine::PortHandle port, pframes_t nframes)
1271 {
1272         assert (port);
1273         assert (valid_port (port));
1274         return static_cast<DummyPort*>(port)->get_buffer (nframes);
1275 }
1276
1277 /* Engine Process */
1278 void *
1279 DummyAudioBackend::main_process_thread ()
1280 {
1281         AudioEngine::thread_init_callback (this);
1282         _running = true;
1283         _processed_samples = 0;
1284
1285         manager.registration_callback();
1286         manager.graph_order_callback();
1287
1288         int64_t clock1;
1289         clock1 = -1;
1290         while (_running) {
1291
1292                 if (_freewheeling != _freewheel) {
1293                         _freewheel = _freewheeling;
1294                         engine.freewheel_callback (_freewheel);
1295                 }
1296
1297                 // re-set input buffers, generate on demand.
1298                 for (std::vector<DummyAudioPort*>::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) {
1299                         (*it)->next_period();
1300                 }
1301                 for (std::vector<DummyMidiPort*>::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it) {
1302                         (*it)->next_period();
1303                 }
1304
1305                 if (engine.process_callback (_samples_per_period)) {
1306                         return 0;
1307                 }
1308                 _processed_samples += _samples_per_period;
1309
1310                 if (_device == _("Loopback") && _midi_mode != MidiToAudio) {
1311                         int opn = 0;
1312                         int opc = _system_outputs.size();
1313                         for (std::vector<DummyAudioPort*>::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it, ++opn) {
1314                                 DummyAudioPort* op = _system_outputs[(opn % opc)];
1315                                 (*it)->fill_wavetable ((const float*)op->get_buffer (_samples_per_period), _samples_per_period);
1316                         }
1317                 }
1318
1319                 if (_midi_mode == MidiLoopback) {
1320                         int opn = 0;
1321                         int opc = _system_midi_out.size();
1322                         for (std::vector<DummyMidiPort*>::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it, ++opn) {
1323                                 DummyMidiPort* op = _system_midi_out[(opn % opc)];
1324                                 op->get_buffer(0); // mix-down
1325                                 (*it)->set_loopback (op->const_buffer());
1326                         }
1327                 }
1328                 else if (_midi_mode == MidiToAudio) {
1329                         int opn = 0;
1330                         int opc = _system_midi_out.size();
1331                         for (std::vector<DummyAudioPort*>::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it, ++opn) {
1332                                 DummyMidiPort* op = _system_midi_out[(opn % opc)];
1333                                 op->get_buffer(0); // mix-down
1334                                 (*it)->midi_to_wavetable (op->const_buffer(), _samples_per_period);
1335                         }
1336                 }
1337
1338                 if (!_freewheel) {
1339                         _dsp_load_calc.set_start_timestamp_us (clock1);
1340                         _dsp_load_calc.set_stop_timestamp_us (_x_get_monotonic_usec());
1341                         _dsp_load = _dsp_load_calc.get_dsp_load_unbound ();
1342
1343                         const int64_t elapsed_time = _dsp_load_calc.elapsed_time_us ();
1344                         const int64_t nominal_time = _dsp_load_calc.get_max_time_us ();
1345                         if (elapsed_time < nominal_time) {
1346                                 const int64_t sleepy = _speedup * (nominal_time - elapsed_time);
1347                                 Glib::usleep (std::max ((int64_t) 100, sleepy));
1348                         } else {
1349                                 Glib::usleep (100); // don't hog cpu
1350                         }
1351                 } else {
1352                         _dsp_load = 1.0f;
1353                         Glib::usleep (100); // don't hog cpu
1354                 }
1355
1356                 /* beginning of next cycle */
1357                 clock1 = _x_get_monotonic_usec();
1358
1359                 bool connections_changed = false;
1360                 bool ports_changed = false;
1361                 if (!pthread_mutex_trylock (&_port_callback_mutex)) {
1362                         if (_port_change_flag) {
1363                                 ports_changed = true;
1364                                 _port_change_flag = false;
1365                         }
1366                         if (!_port_connection_queue.empty ()) {
1367                                 connections_changed = true;
1368                         }
1369                         while (!_port_connection_queue.empty ()) {
1370                                 PortConnectData *c = _port_connection_queue.back ();
1371                                 manager.connect_callback (c->a, c->b, c->c);
1372                                 _port_connection_queue.pop_back ();
1373                                 delete c;
1374                         }
1375                         pthread_mutex_unlock (&_port_callback_mutex);
1376                 }
1377                 if (ports_changed) {
1378                         manager.registration_callback();
1379                 }
1380                 if (connections_changed) {
1381                         manager.graph_order_callback();
1382                 }
1383                 if (connections_changed || ports_changed) {
1384                         engine.latency_callback(false);
1385                         engine.latency_callback(true);
1386                 }
1387
1388         }
1389         _running = false;
1390         return 0;
1391 }
1392
1393
1394 /******************************************************************************/
1395
1396 static boost::shared_ptr<DummyAudioBackend> _instance;
1397
1398 static boost::shared_ptr<AudioBackend> backend_factory (AudioEngine& e);
1399 static int instantiate (const std::string& arg1, const std::string& /* arg2 */);
1400 static int deinstantiate ();
1401 static bool already_configured ();
1402 static bool available ();
1403
1404 static ARDOUR::AudioBackendInfo _descriptor = {
1405         _("None (Dummy)"),
1406         instantiate,
1407         deinstantiate,
1408         backend_factory,
1409         already_configured,
1410         available
1411 };
1412
1413 static boost::shared_ptr<AudioBackend>
1414 backend_factory (AudioEngine& e)
1415 {
1416         if (!_instance) {
1417                 _instance.reset (new DummyAudioBackend (e, _descriptor));
1418         }
1419         return _instance;
1420 }
1421
1422 static int
1423 instantiate (const std::string& arg1, const std::string& /* arg2 */)
1424 {
1425         s_instance_name = arg1;
1426         return 0;
1427 }
1428
1429 static int
1430 deinstantiate ()
1431 {
1432         _instance.reset ();
1433         return 0;
1434 }
1435
1436 static bool
1437 already_configured ()
1438 {
1439         // special-case: unit-tests require backend to be pre-configured.
1440         if (s_instance_name == "Unit-Test") {
1441                 return true;
1442         }
1443         return false;
1444 }
1445
1446 static bool
1447 available ()
1448 {
1449         return true;
1450 }
1451
1452 extern "C" ARDOURBACKEND_API ARDOUR::AudioBackendInfo* descriptor ()
1453 {
1454         return &_descriptor;
1455 }
1456
1457
1458 /******************************************************************************/
1459 DummyPort::DummyPort (DummyAudioBackend &b, const std::string& name, PortFlags flags)
1460         : _dummy_backend (b)
1461         , _name  (name)
1462         , _flags (flags)
1463         , _rseed (0)
1464         , _gen_cycle (false)
1465 {
1466         _capture_latency_range.min = 0;
1467         _capture_latency_range.max = 0;
1468         _playback_latency_range.min = 0;
1469         _playback_latency_range.max = 0;
1470         _dummy_backend.port_connect_add_remove_callback();
1471 }
1472
1473 DummyPort::~DummyPort () {
1474         disconnect_all ();
1475         _dummy_backend.port_connect_add_remove_callback();
1476 }
1477
1478
1479 int DummyPort::connect (DummyPort *port)
1480 {
1481         if (!port) {
1482                 PBD::error << _("DummyPort::connect (): invalid (null) port") << endmsg;
1483                 return -1;
1484         }
1485
1486         if (type () != port->type ()) {
1487                 PBD::error << _("DummyPort::connect (): wrong port-type") << endmsg;
1488                 return -1;
1489         }
1490
1491         if (is_output () && port->is_output ()) {
1492                 PBD::error << _("DummyPort::connect (): cannot inter-connect output ports.") << endmsg;
1493                 return -1;
1494         }
1495
1496         if (is_input () && port->is_input ()) {
1497                 PBD::error << _("DummyPort::connect (): cannot inter-connect input ports.") << endmsg;
1498                 return -1;
1499         }
1500
1501         if (this == port) {
1502                 PBD::error << _("DummyPort::connect (): cannot self-connect ports.") << endmsg;
1503                 return -1;
1504         }
1505
1506         if (is_connected (port)) {
1507 #if 0 // don't bother to warn about this for now. just ignore it
1508                 PBD::error << _("DummyPort::connect (): ports are already connected:")
1509                         << " (" << name () << ") -> (" << port->name () << ")"
1510                         << endmsg;
1511 #endif
1512                 return -1;
1513         }
1514
1515         _connect (port, true);
1516         return 0;
1517 }
1518
1519
1520 void DummyPort::_connect (DummyPort *port, bool callback)
1521 {
1522         _connections.insert (port);
1523         if (callback) {
1524                 port->_connect (this, false);
1525                 _dummy_backend.port_connect_callback (name(),  port->name(), true);
1526         }
1527 }
1528
1529 int DummyPort::disconnect (DummyPort *port)
1530 {
1531         if (!port) {
1532                 PBD::error << _("DummyPort::disconnect (): invalid (null) port") << endmsg;
1533                 return -1;
1534         }
1535
1536         if (!is_connected (port)) {
1537                 PBD::error << _("DummyPort::disconnect (): ports are not connected:")
1538                         << " (" << name () << ") -> (" << port->name () << ")"
1539                         << endmsg;
1540                 return -1;
1541         }
1542         _disconnect (port, true);
1543         return 0;
1544 }
1545
1546 void DummyPort::_disconnect (DummyPort *port, bool callback)
1547 {
1548         std::set<DummyPort*>::iterator it = _connections.find (port);
1549         assert (it != _connections.end ());
1550         _connections.erase (it);
1551         if (callback) {
1552                 port->_disconnect (this, false);
1553                 _dummy_backend.port_connect_callback (name(),  port->name(), false);
1554         }
1555 }
1556
1557
1558 void DummyPort::disconnect_all ()
1559 {
1560         while (!_connections.empty ()) {
1561                 std::set<DummyPort*>::iterator it = _connections.begin ();
1562                 (*it)->_disconnect (this, false);
1563                 _dummy_backend.port_connect_callback (name(), (*it)->name(), false);
1564                 _connections.erase (it);
1565         }
1566 }
1567
1568 bool
1569 DummyPort::is_connected (const DummyPort *port) const
1570 {
1571         return _connections.find (const_cast<DummyPort *>(port)) != _connections.end ();
1572 }
1573
1574 bool DummyPort::is_physically_connected () const
1575 {
1576         for (std::set<DummyPort*>::const_iterator it = _connections.begin (); it != _connections.end (); ++it) {
1577                 if ((*it)->is_physical ()) {
1578                         return true;
1579                 }
1580         }
1581         return false;
1582 }
1583
1584 void DummyPort::setup_random_number_generator ()
1585 {
1586 #ifdef PLATFORM_WINDOWS
1587         LARGE_INTEGER Count;
1588         if (QueryPerformanceCounter (&Count)) {
1589                 _rseed = Count.QuadPart % UINT_MAX;
1590         } else
1591 #endif
1592         {
1593         _rseed = g_get_monotonic_time() % UINT_MAX;
1594         }
1595         _rseed = (_rseed + (uint64_t)this) % UINT_MAX;
1596         if (_rseed == 0) _rseed = 1;
1597 }
1598
1599 inline uint32_t
1600 DummyPort::randi ()
1601 {
1602         // 31bit Park-Miller-Carta Pseudo-Random Number Generator
1603         // http://www.firstpr.com.au/dsp/rand31/
1604         uint32_t hi, lo;
1605         lo = 16807 * (_rseed & 0xffff);
1606         hi = 16807 * (_rseed >> 16);
1607
1608         lo += (hi & 0x7fff) << 16;
1609         lo += hi >> 15;
1610 #if 1
1611         lo = (lo & 0x7fffffff) + (lo >> 31);
1612 #else
1613         if (lo > 0x7fffffff) { lo -= 0x7fffffff; }
1614 #endif
1615         return (_rseed = lo);
1616 }
1617
1618 inline float
1619 DummyPort::randf ()
1620 {
1621         return (randi() / 1073741824.f) - 1.f;
1622 }
1623
1624 /******************************************************************************/
1625
1626 DummyAudioPort::DummyAudioPort (DummyAudioBackend &b, const std::string& name, PortFlags flags)
1627         : DummyPort (b, name, flags)
1628         , _gen_type (Silence)
1629         , _b0 (0)
1630         , _b1 (0)
1631         , _b2 (0)
1632         , _b3 (0)
1633         , _b4 (0)
1634         , _b5 (0)
1635         , _b6 (0)
1636         , _wavetable (0)
1637         , _gen_period (0)
1638         , _gen_offset (0)
1639         , _gen_perio2 (0)
1640         , _gen_count2 (0)
1641         , _pass (false)
1642         , _rn1 (0)
1643 {
1644         memset (_buffer, 0, sizeof (_buffer));
1645 }
1646
1647 DummyAudioPort::~DummyAudioPort () {
1648         free(_wavetable);
1649         _wavetable = 0;
1650 }
1651
1652 void DummyAudioPort::setup_generator (GeneratorType const g, float const samplerate)
1653 {
1654         DummyPort::setup_random_number_generator();
1655         _gen_type = g;
1656
1657         switch (_gen_type) {
1658                 case PinkNoise:
1659                 case PonyNoise:
1660                 case UniformWhiteNoise:
1661                 case GaussianWhiteNoise:
1662                 case Silence:
1663                         break;
1664                 case KronekerDelta:
1665                         _gen_period = (5 + randi() % (int)(samplerate / 20.f));
1666                         break;
1667                 case SquareWave:
1668                         _gen_period = (5 + randi() % (int)(samplerate / 20.f)) & ~1;
1669                         break;
1670                 case SineWave:
1671                         _gen_period = 5 + randi() % (int)(samplerate / 20.f);
1672                         _wavetable = (Sample*) malloc (_gen_period * sizeof(Sample));
1673                         for (uint32_t i = 0 ; i < _gen_period; ++i) {
1674                                 _wavetable[i] = .12589f * sinf(2.0f * M_PI * (float)i / (float)_gen_period); // -18dBFS
1675                         }
1676                         break;
1677                 case SquareSweep:
1678                 case SquareSweepSwell:
1679                 case SineSweep:
1680                 case SineSweepSwell:
1681                         {
1682                                 _gen_period = 5 * samplerate + randi() % (int)(samplerate * 10.f);
1683                                 _gen_period &= ~1;
1684                                 _gen_perio2 = 1 | (int)ceilf (_gen_period * .89f); // Volume Swell period
1685                                 const double f_min = 20.;
1686                                 const double f_max = samplerate * .5;
1687                                 const double g_p2 = _gen_period * .5;
1688 #ifdef LINEAR_SWEEP
1689                                 const double b = (f_max - f_min) / (2. * samplerate * g_p2);
1690                                 const double a = f_min / samplerate;
1691 #else
1692                                 const double b = log (f_max / f_min) / g_p2;
1693                                 const double a = f_min / (b * samplerate);
1694 #endif
1695                                 const uint32_t g_p2i = rint(g_p2);
1696                                 _wavetable = (Sample*) malloc (_gen_period * sizeof(Sample));
1697                                 for (uint32_t i = 0 ; i < g_p2i; ++i) {
1698 #ifdef LINEAR_SWEEP
1699                                         const double phase = i * (a + b * i);
1700 #else
1701                                         const double phase = a * exp (b * i) - a;
1702 #endif
1703                                         _wavetable[i] = (float)sin (2. * M_PI * (phase - floor (phase)));
1704                                 }
1705                                 for (uint32_t i = g_p2i; i < _gen_period; ++i) {
1706                                         const uint32_t j = _gen_period - i;
1707 #ifdef LINEAR_SWEEP
1708                                         const double phase = j * (a + b * j);
1709 #else
1710                                         const double phase = a * exp (b * j) - a;
1711 #endif
1712                                         _wavetable[i] = -(float)sin (2. * M_PI * (phase - floor (phase)));
1713                                 }
1714                                 if (_gen_type == SquareSweep) {
1715                                         for (uint32_t i = 0 ; i < _gen_period; ++i) {
1716                                                 _wavetable[i] = _wavetable[i] < 0 ? -.40709f : .40709f;
1717                                         }
1718                                 }
1719                                 else if (_gen_type == SquareSweepSwell) {
1720                                         for (uint32_t i = 0 ; i < _gen_period; ++i) {
1721                                                 _wavetable[i] = _wavetable[i] < 0 ? -1 : 1;
1722                                         }
1723                                 }
1724                         }
1725                         break;
1726                 case Loopback:
1727                         _wavetable = (Sample*) malloc (DummyAudioBackend::max_buffer_size() * sizeof(Sample));
1728                         break;
1729         }
1730 }
1731
1732 void DummyAudioPort::midi_to_wavetable (DummyMidiBuffer const * const src, size_t n_samples)
1733 {
1734         memset(_wavetable, 0, n_samples * sizeof(float));
1735         /* generate an audio spike for every midi message
1736          * to verify layency-compensation alignment
1737          * (here: midi-out playback-latency + audio-in capture-latency)
1738          */
1739         for (DummyMidiBuffer::const_iterator it = src->begin (); it != src->end (); ++it) {
1740                 const pframes_t t = (*it)->timestamp();
1741                 assert(t < n_samples);
1742                 // somewhat arbitrary mapping for quick visual feedback
1743                 float v = -.5f;
1744                 if ((*it)->size() == 3) {
1745                         const unsigned char *d = (*it)->const_data();
1746                         if ((d[0] & 0xf0) == 0x90) { // note on
1747                                 v = .25f + d[2] / 512.f;
1748                         }
1749                         else if ((d[0] & 0xf0) == 0x80) { // note off
1750                                 v = .3f - d[2] / 640.f;
1751                         }
1752                         else if ((d[0] & 0xf0) == 0xb0) { // CC
1753                                 v = -.1f - d[2] / 256.f;
1754                         }
1755                 }
1756                 _wavetable[t] += v;
1757         }
1758 }
1759
1760 float DummyAudioPort::grandf ()
1761 {
1762         // Gaussian White Noise
1763         // http://www.musicdsp.org/archive.php?classid=0#109
1764         float x1, x2, r;
1765
1766         if (_pass) {
1767                 _pass = false;
1768                 return _rn1;
1769         }
1770
1771         do {
1772                 x1 = randf ();
1773                 x2 = randf ();
1774                 r = x1 * x1 + x2 * x2;
1775         } while ((r >= 1.0f) || (r < 1e-22f));
1776
1777         r = sqrtf (-2.f * logf (r) / r);
1778
1779         _pass = true;
1780         _rn1 = r * x2;
1781         return r * x1;
1782 }
1783
1784 void DummyAudioPort::generate (const pframes_t n_samples)
1785 {
1786         Glib::Threads::Mutex::Lock lm (generator_lock);
1787         if (_gen_cycle) {
1788                 return;
1789         }
1790
1791         switch (_gen_type) {
1792                 case Silence:
1793                         memset (_buffer, 0, n_samples * sizeof (Sample));
1794                         break;
1795                 case SquareWave:
1796                         assert(_gen_period > 0);
1797                         for (pframes_t i = 0 ; i < n_samples; ++i) {
1798                                 if (_gen_offset < _gen_period * .5f) {
1799                                         _buffer[i] =  .40709f; // -6dBFS
1800                                 } else {
1801                                         _buffer[i] = -.40709f;
1802                                 }
1803                                 _gen_offset = (_gen_offset + 1) % _gen_period;
1804                         }
1805                         break;
1806                 case KronekerDelta:
1807                         assert(_gen_period > 0);
1808                         memset (_buffer, 0, n_samples * sizeof (Sample));
1809                         for (pframes_t i = 0; i < n_samples; ++i) {
1810                                 if (_gen_offset == 0) {
1811                                         _buffer[i] = 1.0f;
1812                                 }
1813                                 _gen_offset = (_gen_offset + 1) % _gen_period;
1814                         }
1815                         break;
1816                 case SineSweepSwell:
1817                 case SquareSweepSwell:
1818                         assert(_wavetable && _gen_period > 0);
1819                         {
1820                                 const float vols = 2.f / (float)_gen_perio2;
1821                                 for (pframes_t i = 0; i < n_samples; ++i) {
1822                                         const float g = fabsf (_gen_count2 * vols - 1.f);
1823                                         _buffer[i] = g * _wavetable[_gen_offset];
1824                                         _gen_offset = (_gen_offset + 1) % _gen_period;
1825                                         _gen_count2 = (_gen_count2 + 1) % _gen_perio2;
1826                                 }
1827                         }
1828                         break;
1829                 case Loopback:
1830                         _gen_period = n_samples; // XXX DummyBackend::_samples_per_period;
1831                 case SineWave:
1832                 case SineSweep:
1833                 case SquareSweep:
1834                         assert(_wavetable && _gen_period > 0);
1835                         {
1836                                 pframes_t written = 0;
1837                                 while (written < n_samples) {
1838                                         const uint32_t remain = n_samples - written;
1839                                         const uint32_t to_copy = std::min(remain, _gen_period - _gen_offset);
1840                                         memcpy((void*)&_buffer[written],
1841                                                         (void*)&_wavetable[_gen_offset],
1842                                                         to_copy * sizeof(Sample));
1843                                         written += to_copy;
1844                                         _gen_offset = (_gen_offset + to_copy) % _gen_period;
1845                                 }
1846                         }
1847                         break;
1848                 case UniformWhiteNoise:
1849                         for (pframes_t i = 0 ; i < n_samples; ++i) {
1850                                 _buffer[i] = .158489f * randf();
1851                         }
1852                         break;
1853                 case GaussianWhiteNoise:
1854                         for (pframes_t i = 0 ; i < n_samples; ++i) {
1855                                 _buffer[i] = .089125f * grandf();
1856                         }
1857                         break;
1858                 case PinkNoise:
1859                         for (pframes_t i = 0 ; i < n_samples; ++i) {
1860                                 // Paul Kellet's refined method
1861                                 // http://www.musicdsp.org/files/pink.txt
1862                                 // NB. If 'white' consists of uniform random numbers,
1863                                 // the pink noise will have an almost gaussian distribution.
1864                                 const float white = .0498f * randf ();
1865                                 _b0 = .99886f * _b0 + white * .0555179f;
1866                                 _b1 = .99332f * _b1 + white * .0750759f;
1867                                 _b2 = .96900f * _b2 + white * .1538520f;
1868                                 _b3 = .86650f * _b3 + white * .3104856f;
1869                                 _b4 = .55000f * _b4 + white * .5329522f;
1870                                 _b5 = -.7616f * _b5 - white * .0168980f;
1871                                 _buffer[i] = _b0 + _b1 + _b2 + _b3 + _b4 + _b5 + _b6 + white * 0.5362f;
1872                                 _b6 = white * 0.115926f;
1873                         }
1874                         break;
1875                 case PonyNoise:
1876                         for (pframes_t i = 0 ; i < n_samples; ++i) {
1877                                 const float white = 0.0498f * randf ();
1878                                 // Paul Kellet's economy method
1879                                 // http://www.musicdsp.org/files/pink.txt
1880                                 _b0 = 0.99765f * _b0 + white * 0.0990460f;
1881                                 _b1 = 0.96300f * _b1 + white * 0.2965164f;
1882                                 _b2 = 0.57000f * _b2 + white * 1.0526913f;
1883                                 _buffer[i] = _b0 + _b1 + _b2 + white * 0.1848f;
1884                         }
1885                         break;
1886         }
1887         _gen_cycle = true;
1888 }
1889
1890 void* DummyAudioPort::get_buffer (pframes_t n_samples)
1891 {
1892         if (is_input ()) {
1893                 const std::set<DummyPort *>& connections = get_connections ();
1894                 std::set<DummyPort*>::const_iterator it = connections.begin ();
1895                 if (it == connections.end ()) {
1896                         memset (_buffer, 0, n_samples * sizeof (Sample));
1897                 } else {
1898                         DummyAudioPort * source = static_cast<DummyAudioPort*>(*it);
1899                         assert (source && source->is_output ());
1900                         if (source->is_physical() && source->is_terminal()) {
1901                                 source->get_buffer(n_samples); // generate signal.
1902                         }
1903                         memcpy (_buffer, source->const_buffer (), n_samples * sizeof (Sample));
1904                         while (++it != connections.end ()) {
1905                                 source = static_cast<DummyAudioPort*>(*it);
1906                                 assert (source && source->is_output ());
1907                                 Sample* dst = buffer ();
1908                                 if (source->is_physical() && source->is_terminal()) {
1909                                         source->get_buffer(n_samples); // generate signal.
1910                                 }
1911                                 const Sample* src = source->const_buffer ();
1912                                 for (uint32_t s = 0; s < n_samples; ++s, ++dst, ++src) {
1913                                         *dst += *src;
1914                                 }
1915                         }
1916                 }
1917         } else if (is_output () && is_physical () && is_terminal()) {
1918                 if (!_gen_cycle) {
1919                         generate(n_samples);
1920                 }
1921         }
1922         return _buffer;
1923 }
1924
1925
1926 DummyMidiPort::DummyMidiPort (DummyAudioBackend &b, const std::string& name, PortFlags flags)
1927         : DummyPort (b, name, flags)
1928         , _midi_seq_spb (0)
1929         , _midi_seq_time (0)
1930         , _midi_seq_pos (0)
1931 {
1932         _buffer.clear ();
1933         _loopback.clear ();
1934 }
1935
1936 DummyMidiPort::~DummyMidiPort () {
1937         _buffer.clear ();
1938         _loopback.clear ();
1939 }
1940
1941 struct MidiEventSorter {
1942         bool operator() (const boost::shared_ptr<DummyMidiEvent>& a, const boost::shared_ptr<DummyMidiEvent>& b) {
1943                 return *a < *b;
1944         }
1945 };
1946
1947 void DummyMidiPort::set_loopback (DummyMidiBuffer const * const src)
1948 {
1949         _loopback.clear ();
1950         for (DummyMidiBuffer::const_iterator it = src->begin (); it != src->end (); ++it) {
1951                 _loopback.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (**it)));
1952         }
1953 }
1954
1955 void DummyMidiPort::setup_generator (int seq_id, const float sr)
1956 {
1957         DummyPort::setup_random_number_generator();
1958         _midi_seq_dat = DummyMidiData::sequences[seq_id % NUM_MIDI_EVENT_GENERATORS];
1959         _midi_seq_spb = sr * .5f; // 120 BPM, beat_time 1.0 per beat.
1960         _midi_seq_pos = 0;
1961         _midi_seq_time = 0;
1962 }
1963
1964 void DummyMidiPort::midi_generate (const pframes_t n_samples)
1965 {
1966         Glib::Threads::Mutex::Lock lm (generator_lock);
1967         if (_gen_cycle) {
1968                 return;
1969         }
1970
1971         _buffer.clear ();
1972         _gen_cycle = true;
1973
1974         if (_midi_seq_spb == 0 || !_midi_seq_dat) {
1975                 for (DummyMidiBuffer::const_iterator it = _loopback.begin (); it != _loopback.end (); ++it) {
1976                         _buffer.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (**it)));
1977                 }
1978                 return;
1979         }
1980
1981         while (1) {
1982                 const int32_t ev_beat_time = _midi_seq_dat[_midi_seq_pos].beat_time * _midi_seq_spb - _midi_seq_time;
1983                 if (ev_beat_time < 0) {
1984                         break;
1985                 }
1986                 if ((pframes_t) ev_beat_time >= n_samples) {
1987                         break;
1988                 }
1989                 _buffer.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (
1990                                                 ev_beat_time,
1991                                                 _midi_seq_dat[_midi_seq_pos].event,
1992                                                 _midi_seq_dat[_midi_seq_pos].size
1993                                                 )));
1994                 ++_midi_seq_pos;
1995
1996                 if (_midi_seq_dat[_midi_seq_pos].event[0] == 0xff && _midi_seq_dat[_midi_seq_pos].event[1] == 0xff) {
1997                         _midi_seq_time -= _midi_seq_dat[_midi_seq_pos].beat_time * _midi_seq_spb;
1998                         _midi_seq_pos = 0;
1999                 }
2000         }
2001         _midi_seq_time += n_samples;
2002 }
2003
2004
2005 void* DummyMidiPort::get_buffer (pframes_t n_samples)
2006 {
2007         if (is_input ()) {
2008                 _buffer.clear ();
2009                 const std::set<DummyPort*>& connections = get_connections ();
2010                 for (std::set<DummyPort*>::const_iterator i = connections.begin ();
2011                                 i != connections.end ();
2012                                 ++i) {
2013                         DummyMidiPort * source = static_cast<DummyMidiPort*>(*i);
2014                         if (source->is_physical() && source->is_terminal()) {
2015                                 source->get_buffer(n_samples); // generate signal.
2016                         }
2017                         const DummyMidiBuffer *src = source->const_buffer ();
2018                         for (DummyMidiBuffer::const_iterator it = src->begin (); it != src->end (); ++it) {
2019                                 _buffer.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (**it)));
2020                         }
2021                 }
2022                 std::sort (_buffer.begin (), _buffer.end (), MidiEventSorter());
2023         } else if (is_output () && is_physical () && is_terminal()) {
2024                 if (!_gen_cycle) {
2025                         midi_generate(n_samples);
2026                 }
2027         }
2028         return &_buffer;
2029 }
2030
2031 DummyMidiEvent::DummyMidiEvent (const pframes_t timestamp, const uint8_t* data, size_t size)
2032         : _size (size)
2033         , _timestamp (timestamp)
2034         , _data (0)
2035 {
2036         if (size > 0) {
2037                 _data = (uint8_t*) malloc (size);
2038                 memcpy (_data, data, size);
2039         }
2040 }
2041
2042 DummyMidiEvent::DummyMidiEvent (const DummyMidiEvent& other)
2043         : _size (other.size ())
2044         , _timestamp (other.timestamp ())
2045         , _data (0)
2046 {
2047         if (other.size () && other.const_data ()) {
2048                 _data = (uint8_t*) malloc (other.size ());
2049                 memcpy (_data, other.const_data (), other.size ());
2050         }
2051 };
2052
2053 DummyMidiEvent::~DummyMidiEvent () {
2054         free (_data);
2055 };