add some midi-debug printf() to the dummy backend
[ardour.git] / libs / backends / dummy / dummy_audiobackend.cc
index a42f3dbade654dbe716d990a800aa986d79be561..130a92dc5f37a411a716873bcc6a0e30ada9005b 100644 (file)
@@ -25,6 +25,7 @@
 
 #ifdef PLATFORM_WINDOWS
 #include <windows.h>
+#include <pbd/windows_timer_utils.h>
 #endif
 
 #include "dummy_audiobackend.h"
@@ -41,20 +42,11 @@ size_t DummyAudioBackend::_max_buffer_size = 8192;
 std::vector<std::string> DummyAudioBackend::_midi_options;
 std::vector<AudioBackend::DeviceStatus> DummyAudioBackend::_device_status;
 
-#ifdef PLATFORM_WINDOWS
-static double _win_pc_rate = 0; // usec per tick
-#endif
+std::vector<DummyAudioBackend::DriverSpeed> DummyAudioBackend::_driver_speed;
 
 static int64_t _x_get_monotonic_usec() {
 #ifdef PLATFORM_WINDOWS
-       if (_win_pc_rate > 0) {
-               LARGE_INTEGER Count;
-               // not very reliable, but the only realistic way for sub milli-seconds
-               if (QueryPerformanceCounter (&Count)) {
-                       return (int64_t) (Count.QuadPart * _win_pc_rate);
-               }
-               return -1;
-       }
+       return PBD::get_microseconds();
 #endif
        return g_get_monotonic_time();
 }
@@ -64,6 +56,7 @@ DummyAudioBackend::DummyAudioBackend (AudioEngine& e, AudioBackendInfo& info)
        , _running (false)
        , _freewheel (false)
        , _freewheeling (false)
+       , _speedup (1.0)
        , _device ("")
        , _samplerate (48000)
        , _samples_per_period (1024)
@@ -81,6 +74,18 @@ DummyAudioBackend::DummyAudioBackend (AudioEngine& e, AudioBackendInfo& info)
        _instance_name = s_instance_name;
        _device = _("Silence");
        pthread_mutex_init (&_port_callback_mutex, 0);
+
+       if (_driver_speed.empty()) {
+               _driver_speed.push_back (DriverSpeed (_("Half Speed"),   2.0f));
+               _driver_speed.push_back (DriverSpeed (_("Normal Speed"), 1.0f));
+               _driver_speed.push_back (DriverSpeed (_("Double Speed"), 0.5f));
+               _driver_speed.push_back (DriverSpeed (_("5x Speed"),     0.2f));
+               _driver_speed.push_back (DriverSpeed (_("10x Speed"),    0.1f));
+               _driver_speed.push_back (DriverSpeed (_("15x Speed"),    0.06666f));
+               _driver_speed.push_back (DriverSpeed (_("20x Speed"),    0.05f));
+               _driver_speed.push_back (DriverSpeed (_("50x Speed"),    0.02f));
+       }
+
 }
 
 DummyAudioBackend::~DummyAudioBackend ()
@@ -93,7 +98,7 @@ DummyAudioBackend::~DummyAudioBackend ()
 std::string
 DummyAudioBackend::name () const
 {
-       return X_("Dummy");
+       return X_("Dummy"); // internal name
 }
 
 bool
@@ -182,6 +187,41 @@ DummyAudioBackend::can_change_buffer_size_when_running () const
        return true;
 }
 
+std::vector<std::string>
+DummyAudioBackend::enumerate_drivers () const
+{
+       std::vector<std::string> speed_drivers;
+       for (std::vector<DriverSpeed>::const_iterator it = _driver_speed.begin () ; it != _driver_speed.end (); ++it) {
+               speed_drivers.push_back (it->name);
+       }
+       return speed_drivers;
+}
+
+std::string
+DummyAudioBackend::driver_name () const
+{
+       for (std::vector<DriverSpeed>::const_iterator it = _driver_speed.begin () ; it != _driver_speed.end (); ++it) {
+               if (rintf (1e6f * _speedup) == rintf (1e6f * it->speedup)) {
+                       return it->name;
+               }
+       }
+       assert (0);
+       return _("Normal Speed");
+}
+
+int
+DummyAudioBackend::set_driver (const std::string& d)
+{
+       for (std::vector<DriverSpeed>::const_iterator it = _driver_speed.begin () ; it != _driver_speed.end (); ++it) {
+               if (d == it->name) {
+                       _speedup = it->speedup;
+                       return 0;
+               }
+       }
+       assert (0);
+       return -1;
+}
+
 int
 DummyAudioBackend::set_device_name (const std::string& d)
 {
@@ -201,7 +241,7 @@ DummyAudioBackend::set_sample_rate (float sr)
 int
 DummyAudioBackend::set_buffer_size (uint32_t bs)
 {
-       if (bs <= 0 || bs >= _max_buffer_size) {
+       if (bs <= 0 || bs > _max_buffer_size) {
                return -1;
        }
        _samples_per_period = bs;
@@ -384,7 +424,7 @@ DummyAudioBackend::_start (bool /*for_latency_measurement*/)
 {
        if (_running) {
                PBD::error << _("DummyAudioBackend: already active.") << endmsg;
-               return -1;
+               return BackendReinitializationError;
        }
 
        if (_ports.size()) {
@@ -401,16 +441,18 @@ DummyAudioBackend::_start (bool /*for_latency_measurement*/)
 
        if (register_system_ports()) {
                PBD::error << _("DummyAudioBackend: failed to register system ports.") << endmsg;
-               return -1;
+               return PortRegistrationError;
        }
 
        engine.sample_rate_change (_samplerate);
        engine.buffer_size_change (_samples_per_period);
 
+       _dsp_load_calc.set_max_time (_samplerate, _samples_per_period);
+
        if (engine.reestablish_ports ()) {
                PBD::error << _("DummyAudioBackend: Could not re-establish ports.") << endmsg;
                stop ();
-               return -1;
+               return PortReconnectError;
        }
 
        engine.reconnect_ports ();
@@ -425,10 +467,10 @@ DummyAudioBackend::_start (bool /*for_latency_measurement*/)
 
        if (timeout == 0 || !_running) {
                PBD::error << _("DummyAudioBackend: failed to start process thread.") << endmsg;
-               return -1;
+               return ProcessThreadStartError;
        }
 
-       return 0;
+       return NoError;
 }
 
 int
@@ -640,7 +682,7 @@ DummyAudioBackend::get_ports (
        }
        for (size_t i = 0; i < _ports.size (); ++i) {
                DummyPort* port = _ports[i];
-               if ((port->type () == type) && (port->flags () & flags)) {
+               if ((port->type () == type) && flags == (port->flags () & flags)) {
                        if (!use_regexp || !regexec (&port_regex, port->name ().c_str (), 0, NULL, 0)) {
                                port_names.push_back (port->name ());
                                ++rv;
@@ -986,9 +1028,16 @@ DummyAudioBackend::midi_event_put (
        DummyMidiBuffer& dst = * static_cast<DummyMidiBuffer*>(port_buffer);
        if (dst.size () && (pframes_t)dst.back ()->timestamp () > timestamp) {
                // nevermind, ::get_buffer() sorts events, but always print warning
-               fprintf (stderr, "DummyMidiBuffer: it's too late for this event.\n");
+               fprintf (stderr, "DummyMidiBuffer: it's too late for this event %d > %d.\n", (pframes_t)dst.back ()->timestamp (), timestamp);
        }
        dst.push_back (boost::shared_ptr<DummyMidiEvent>(new DummyMidiEvent (timestamp, buffer, size)));
+#if 0 // DEBUG MIDI EVENTS
+       printf("DummyAudioBackend::midi_event_put %d, %zu: ", timestamp, size);
+       for (size_t xx = 0; xx < size; ++xx) {
+               printf(" %02x", buffer[xx]);
+       }
+       printf("\n");
+#endif
        return 0;
 }
 
@@ -1173,7 +1222,7 @@ DummyAudioBackend::main_process_thread ()
        manager.registration_callback();
        manager.graph_order_callback();
 
-       int64_t clock1, clock2;
+       int64_t clock1;
        clock1 = -1;
        while (_running) {
 
@@ -1224,37 +1273,15 @@ DummyAudioBackend::main_process_thread ()
                }
 
                if (!_freewheel) {
-                       const int64_t nominal_time = 1e6 * _samples_per_period / _samplerate;
-                       clock2 = _x_get_monotonic_usec();
-                       bool timers_ok = true;
-
-                       /* querying the performance counter can fail occasionally (-1).
-                        * Also on some multi-core systems, timers are CPU specific and not
-                        * synchronized. We assume they differ more than a few milliseconds
-                        * (4 * nominal cycle time) and simply ignore cases where the
-                        * execution switches cores.
-                        */
-                       if (clock1 < 0 || clock2 < 0 || (clock1 > clock2) || (clock2 - clock1) > 4 * nominal_time) {
-                               clock1 = 0;
-                               clock2 = nominal_time;
-                               timers_ok = false;
-                       }
-
-                       const int64_t elapsed_time = clock2 - clock1;
-
-                       if (timers_ok)
-                       { // low pass filter
-                               const float load = elapsed_time / (float) nominal_time;
-                               if (load > _dsp_load) {
-                                       _dsp_load = load;
-                               } else {
-                                       const float a = .2 * _samples_per_period / _samplerate;
-                                       _dsp_load = _dsp_load + a * (load - _dsp_load) + 1e-12;
-                               }
-                       }
+                       _dsp_load_calc.set_start_timestamp_us (clock1);
+                       _dsp_load_calc.set_stop_timestamp_us (_x_get_monotonic_usec());
+                       _dsp_load = _dsp_load_calc.get_dsp_load_unbound ();
 
+                       const int64_t elapsed_time = _dsp_load_calc.elapsed_time_us ();
+                       const int64_t nominal_time = _dsp_load_calc.get_max_time_us ();
                        if (elapsed_time < nominal_time) {
-                               Glib::usleep (nominal_time - elapsed_time);
+                               const int64_t sleepy = _speedup * (nominal_time - elapsed_time);
+                               Glib::usleep (std::max ((int64_t) 100, sleepy));
                        } else {
                                Glib::usleep (100); // don't hog cpu
                        }
@@ -1312,7 +1339,7 @@ static bool already_configured ();
 static bool available ();
 
 static ARDOUR::AudioBackendInfo _descriptor = {
-       "Dummy",
+       _("None (Dummy)"),
        instantiate,
        deinstantiate,
        backend_factory,
@@ -1333,14 +1360,6 @@ static int
 instantiate (const std::string& arg1, const std::string& /* arg2 */)
 {
        s_instance_name = arg1;
-#ifdef PLATFORM_WINDOWS
-       LARGE_INTEGER Frequency;
-       if (!QueryPerformanceFrequency(&Frequency) || Frequency.QuadPart < 1) {
-               _win_pc_rate = 0;
-       } else {
-               _win_pc_rate = 1000000.0 / Frequency.QuadPart;
-       }
-#endif
        return 0;
 }
 
@@ -1354,8 +1373,9 @@ deinstantiate ()
 static bool
 already_configured ()
 {
-       if (_instance) {
-               return _instance->is_running();
+       // special-case: unit-tests require backend to be pre-configured.
+       if (s_instance_name == "Unit-Test") {
+               return true;
        }
        return false;
 }
@@ -1512,6 +1532,7 @@ void DummyPort::setup_random_number_generator ()
        _rseed = g_get_monotonic_time() % UINT_MAX;
        }
        _rseed = (_rseed + (uint64_t)this) % UINT_MAX;
+       if (_rseed == 0) _rseed = 1;
 }
 
 inline uint32_t
@@ -1737,7 +1758,7 @@ void DummyAudioPort::generate (const pframes_t n_samples)
                        {
                                const float vols = 2.f / (float)_gen_perio2;
                                for (pframes_t i = 0; i < n_samples; ++i) {
-                                       const float g = fabsf (_gen_count2 * vols - 1.0);
+                                       const float g = fabsf (_gen_count2 * vols - 1.f);
                                        _buffer[i] = g * _wavetable[_gen_offset];
                                        _gen_offset = (_gen_offset + 1) % _gen_period;
                                        _gen_count2 = (_gen_count2 + 1) % _gen_perio2;