#include <glibmm.h>
+#ifdef PLATFORM_WINDOWS
+#include <windows.h>
+#include <pbd/windows_timer_utils.h>
+#endif
+
#include "dummy_audiobackend.h"
#include "dummy_midi_seq.h"
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();
}
, _running (false)
, _freewheel (false)
, _freewheeling (false)
+ , _speedup (1.0)
, _device ("")
, _samplerate (48000)
, _samples_per_period (1024)
_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 ()
std::string
DummyAudioBackend::name () const
{
- return X_("Dummy");
+ return X_("Dummy"); // internal name
}
bool
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)
{
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;
* with 'Loopback' there is exactly once cycle latency,
* divide it between In + Out;
*/
- const size_t l_in = _samples_per_period * .25;
- const size_t l_out = _samples_per_period - l_in;
LatencyRange lr;
- lr.min = lr.max = l_in + _systemic_input_latency;
+ lr.min = lr.max = _systemic_input_latency;
for (std::vector<DummyAudioPort*>::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) {
set_latency_range (*it, false, lr);
}
set_latency_range (*it, false, lr);
}
- lr.min = lr.max = l_out + _systemic_output_latency;
+ lr.min = lr.max = _systemic_output_latency;
for (std::vector<DummyAudioPort*>::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it) {
set_latency_range (*it, true, lr);
}
DummyAudioBackend::enumerate_midi_options () const
{
if (_midi_options.empty()) {
- _midi_options.push_back (_("No MIDI I/O"));
_midi_options.push_back (_("1 in, 1 out, Silence"));
_midi_options.push_back (_("2 in, 2 out, Silence"));
_midi_options.push_back (_("8 in, 8 out, Silence"));
_midi_options.push_back (_("Midi Event Generators"));
_midi_options.push_back (_("8 in, 8 out, Loopback"));
_midi_options.push_back (_("MIDI to Audio, Loopback"));
+ _midi_options.push_back (_("No MIDI I/O"));
}
return _midi_options;
}
{
if (_running) {
PBD::error << _("DummyAudioBackend: already active.") << endmsg;
- return -1;
+ return BackendReinitializationError;
}
if (_ports.size()) {
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 ();
if (timeout == 0 || !_running) {
PBD::error << _("DummyAudioBackend: failed to start process thread.") << endmsg;
- return -1;
+ return ProcessThreadStartError;
}
- return 0;
+ return NoError;
}
int
}
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;
const int m_ins = _n_midi_inputs == UINT_MAX ? 0 : _n_midi_inputs;
const int m_out = _n_midi_outputs == UINT_MAX ? a_ins : _n_midi_outputs;
- /* with 'Loopback' there is exactly once cycle latency, divide it between In + Out; */
- const size_t l_in = _samples_per_period * .25;
- const size_t l_out = _samples_per_period - l_in;
/* audio ports */
- lr.min = lr.max = l_in + _systemic_input_latency;
+ lr.min = lr.max = _systemic_input_latency;
for (int i = 1; i <= a_ins; ++i) {
char tmp[64];
snprintf(tmp, sizeof(tmp), "system:capture_%d", i);
static_cast<DummyAudioPort*>(p)->setup_generator (gt, _samplerate);
}
- lr.min = lr.max = l_out + _systemic_output_latency;
+ lr.min = lr.max = _systemic_output_latency;
for (int i = 1; i <= a_out; ++i) {
char tmp[64];
snprintf(tmp, sizeof(tmp), "system:playback_%d", i);
}
/* midi ports */
- lr.min = lr.max = l_in + _systemic_input_latency;
+ lr.min = lr.max = _systemic_input_latency;
for (int i = 0; i < m_ins; ++i) {
char tmp[64];
snprintf(tmp, sizeof(tmp), "system:midi_capture_%d", i+1);
}
}
- lr.min = lr.max = l_out + _systemic_output_latency;
+ lr.min = lr.max = _systemic_output_latency;
for (int i = 1; i <= m_out; ++i) {
char tmp[64];
snprintf(tmp, sizeof(tmp), "system:midi_playback_%d", i);
void
DummyAudioBackend::unregister_ports (bool system_only)
{
- size_t i = 0;
_system_inputs.clear();
_system_outputs.clear();
_system_midi_in.clear();
_system_midi_out.clear();
- while (i < _ports.size ()) {
- DummyPort* port = _ports[i];
+
+ for (std::vector<DummyPort*>::iterator i = _ports.begin (); i != _ports.end ();) {
+ DummyPort* port = *i;
if (! system_only || (port->is_physical () && port->is_terminal ())) {
port->disconnect_all ();
delete port;
- _ports.erase (_ports.begin() + i);
+ i = _ports.erase (i);
} else {
++i;
}
assert (buffer && port_buffer);
DummyMidiBuffer& dst = * static_cast<DummyMidiBuffer*>(port_buffer);
if (dst.size () && (pframes_t)dst.back ()->timestamp () > timestamp) {
- fprintf (stderr, "DummyMidiBuffer: it's too late for this event.\n");
- return -1;
+ // nevermind, ::get_buffer() sorts events, but always print warning
+ 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;
}
LatencyRange
DummyAudioBackend::get_latency_range (PortEngine::PortHandle port, bool for_playback)
{
+ LatencyRange r;
if (!valid_port (port)) {
PBD::error << _("DummyPort::get_latency_range (): invalid port.") << endmsg;
- LatencyRange r;
r.min = 0;
r.max = 0;
return r;
}
- return static_cast<DummyPort*>(port)->latency_range (for_playback);
+ DummyPort *p = static_cast<DummyPort*>(port);
+ assert(p);
+
+ r = p->latency_range (for_playback);
+ if (p->is_physical() && p->is_terminal()) {
+ if (p->is_input() && for_playback) {
+ const size_t l_in = _samples_per_period * .25;
+ r.min += l_in;
+ r.max += l_in;
+ }
+ if (p->is_output() && !for_playback) {
+ /* with 'Loopback' there is exactly once cycle latency, divide it between In + Out; */
+ const size_t l_in = _samples_per_period * .25;
+ const size_t l_out = _samples_per_period - l_in;
+ r.min += l_out;
+ r.max += l_out;
+ }
+ }
+ return r;
}
/* Discovering physical ports */
manager.registration_callback();
manager.graph_order_callback();
- int64_t clock1, clock2;
- clock1 = _x_get_monotonic_usec();
+ int64_t clock1;
+ clock1 = -1;
while (_running) {
if (_freewheeling != _freewheel) {
}
if (!_freewheel) {
- const int64_t nomial_time = 1e6 * _samples_per_period / _samplerate;
- clock2 = _x_get_monotonic_usec();
-#ifdef PLATFORM_WINDOWS
- bool win_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 * nomial_time) {
- clock2 = clock1 = 0;
- win_timers_ok = false;
- }
-#endif
- const int64_t elapsed_time = clock2 - clock1;
-#ifdef PLATFORM_WINDOWS
- if (win_timers_ok)
-#endif
- { // low pass filter
- _dsp_load = _dsp_load + .05 * ((elapsed_time / (float) nomial_time) - _dsp_load) + 1e-12;
- }
-
- if (elapsed_time < nomial_time) {
- Glib::usleep (nomial_time - elapsed_time);
+ _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) {
+ 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
}
Glib::usleep (100); // don't hog cpu
}
- /* beginning of netx cycle */
+ /* beginning of next cycle */
clock1 = _x_get_monotonic_usec();
bool connections_changed = false;
static bool available ();
static ARDOUR::AudioBackendInfo _descriptor = {
- "Dummy",
+ _("None (Dummy)"),
instantiate,
deinstantiate,
backend_factory,
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;
}
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;
}
_rseed = g_get_monotonic_time() % UINT_MAX;
}
_rseed = (_rseed + (uint64_t)this) % UINT_MAX;
+ if (_rseed == 0) _rseed = 1;
}
inline uint32_t
const double b = log (f_max / f_min) / g_p2;
const double a = f_min / (b * samplerate);
#endif
+ const uint32_t g_p2i = rint(g_p2);
_wavetable = (Sample*) malloc (_gen_period * sizeof(Sample));
- for (uint32_t i = 0 ; i < g_p2; ++i) {
+ for (uint32_t i = 0 ; i < g_p2i; ++i) {
#ifdef LINEAR_SWEEP
const double phase = i * (a + b * i);
#else
#endif
_wavetable[i] = (float)sin (2. * M_PI * (phase - floor (phase)));
}
- for (uint32_t i = g_p2; i < _gen_period; ++i) {
+ for (uint32_t i = g_p2i; i < _gen_period; ++i) {
const uint32_t j = _gen_period - i;
#ifdef LINEAR_SWEEP
const double phase = j * (a + b * j);
#else
const double phase = a * exp (b * j) - a;
#endif
- _wavetable[i] = (float)sin (2. * M_PI * (phase - floor (phase)));
+ _wavetable[i] = -(float)sin (2. * M_PI * (phase - floor (phase)));
}
if (_gen_type == SquareSweep) {
for (uint32_t i = 0 ; i < _gen_period; ++i) {
{
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;