#include "pbd/compose.h"
#include "pbd/error.h"
+#include "pbd/file_utils.h"
+#include "ardour/filesystem_paths.h"
#include "ardour/port_manager.h"
+#include "ardouralsautil/devicelist.h"
#include "i18n.h"
using namespace ARDOUR;
, _run (false)
, _active (false)
, _freewheeling (false)
- , _capture_device("")
- , _playback_device("")
+ , _measure_latency (false)
+ , _audio_device("")
+ , _midi_driver_option(_("None"))
+ , _device_reservation(0)
, _samplerate (48000)
, _samples_per_period (1024)
, _periods_per_cycle (2)
- , _dsp_load (0)
, _n_inputs (0)
, _n_outputs (0)
- , _systemic_input_latency (0)
- , _systemic_output_latency (0)
+ , _systemic_audio_input_latency (0)
+ , _systemic_audio_output_latency (0)
+ , _dsp_load (0)
, _processed_samples (0)
+ , _port_change_flag (false)
{
_instance_name = s_instance_name;
pthread_mutex_init (&_port_callback_mutex, 0);
AlsaAudioBackend::enumerate_devices () const
{
std::vector<AudioBackend::DeviceStatus> s;
- int cardnum = -1;
- int device = -1;
- snd_ctl_card_info_t *info;
- snd_ctl_card_info_alloca (&info);
- snd_pcm_info_t *pcminfo;
- snd_pcm_info_alloca (&pcminfo);
-
- while (snd_card_next (&cardnum) >= 0 && cardnum >= 0) {
- snd_ctl_t *handle;
-
- std::string devname = "hw:";
- devname += PBD::to_string (cardnum, std::dec);
-
- if (snd_ctl_open (&handle, devname.c_str(), 0) >= 0 && snd_ctl_card_info (handle, info) >= 0) {
- if (snd_ctl_card_info (handle, info) < 0) {
- continue;
- }
+ std::map<std::string, std::string> devices;
+ get_alsa_audio_device_names(devices);
+ for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end(); ++i) {
+ s.push_back (DeviceStatus (i->first, true));
+ }
+ return s;
+}
- std::string card_name = snd_ctl_card_info_get_name (info);
- devname = "hw:";
- devname += snd_ctl_card_info_get_id (info);
+void
+AlsaAudioBackend::reservation_stdout (std::string d, size_t /* s */)
+{
+ if (d.substr(0, 19) == "Acquired audio-card") {
+ _reservation_succeeded = true;
+ }
+}
- while (snd_ctl_pcm_next_device (handle, &device) >= 0 && device >= 0) {
- snd_pcm_info_set_device (pcminfo, device);
- snd_pcm_info_set_subdevice (pcminfo, 0);
- snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_CAPTURE);
- if (snd_ctl_pcm_info (handle, pcminfo) < 0) {
- continue;
- }
- snd_pcm_info_set_device (pcminfo, device);
- snd_pcm_info_set_subdevice (pcminfo, 0);
- snd_pcm_info_set_stream (pcminfo, SND_PCM_STREAM_PLAYBACK);
- if (snd_ctl_pcm_info (handle, pcminfo) < 0) {
- continue;
- }
- devname += ',';
- devname += PBD::to_string (device, std::dec);
- s.push_back (DeviceStatus (devname + " " + card_name, true));
- }
- snd_ctl_close (handle);
+void
+AlsaAudioBackend::release_device()
+{
+ _reservation_connection.drop_connections();
+ ARDOUR::SystemExec * tmp = _device_reservation;
+ _device_reservation = 0;
+ delete tmp;
+}
+
+bool
+AlsaAudioBackend::acquire_device(const char* device_name)
+{
+ /* This is quick hack, ideally we'll link against libdbus and implement a dbus-listener
+ * that owns the device. here we try to get away by just requesting it and then block it...
+ * (pulseaudio periodically checks anyway)
+ *
+ * dbus-send --session --print-reply --type=method_call --dest=org.freedesktop.ReserveDevice1.Audio2 /org/freedesktop/ReserveDevice1/Audio2 org.freedesktop.ReserveDevice1.RequestRelease int32:4
+ * -> should not return 'boolean false'
+ */
+ int device_number = card_to_num(device_name);
+ if (device_number < 0) return false;
+
+ assert(_device_reservation == 0);
+ _reservation_succeeded = false;
+
+ std::string request_device_exe;
+ if (!PBD::find_file_in_search_path (
+ PBD::Searchpath(Glib::build_filename(ARDOUR::ardour_dll_directory(), "ardouralsautil")
+ + G_SEARCHPATH_SEPARATOR_S + ARDOUR::ardour_dll_directory()),
+ "ardour-request-device", request_device_exe))
+ {
+ PBD::warning << "ardour-request-device binary was not found..'" << endmsg;
+ return false;
+ }
+ else
+ {
+ char **argp;
+ char tmp[128];
+ argp=(char**) calloc(5,sizeof(char*));
+ argp[0] = strdup(request_device_exe.c_str());
+ argp[1] = strdup("-P");
+ snprintf(tmp, sizeof(tmp), "%d", getpid());
+ argp[2] = strdup(tmp);
+ snprintf(tmp, sizeof(tmp), "Audio%d", device_number);
+ argp[3] = strdup(tmp);
+ argp[4] = 0;
+
+ _device_reservation = new ARDOUR::SystemExec(request_device_exe, argp);
+ _device_reservation->ReadStdout.connect_same_thread (_reservation_connection, boost::bind (&AlsaAudioBackend::reservation_stdout, this, _1 ,_2));
+ _device_reservation->Terminated.connect_same_thread (_reservation_connection, boost::bind (&AlsaAudioBackend::release_device, this));
+ if (_device_reservation->start(0)) {
+ PBD::warning << _("AlsaAudioBackend: Device Request failed.") << endmsg;
+ release_device();
+ return false;
}
}
- return s;
+ // wait to check if reservation suceeded.
+ int timeout = 500; // 5 sec
+ while (_device_reservation && !_reservation_succeeded && --timeout > 0) {
+ Glib::usleep(10000);
+ }
+ if (timeout == 0 || !_reservation_succeeded) {
+ PBD::warning << _("AlsaAudioBackend: Device Reservation failed.") << endmsg;
+ release_device();
+ return false;
+ }
+ return true;
}
std::vector<float>
int
AlsaAudioBackend::set_device_name (const std::string& d)
{
- _capture_device = d;
- _playback_device = d;
+ _audio_device = d;
return 0;
}
int
AlsaAudioBackend::set_systemic_input_latency (uint32_t sl)
{
- _systemic_input_latency = sl;
+ _systemic_audio_input_latency = sl;
return 0;
}
int
AlsaAudioBackend::set_systemic_output_latency (uint32_t sl)
{
- _systemic_output_latency = sl;
+ _systemic_audio_output_latency = sl;
+ return 0;
+}
+
+int
+AlsaAudioBackend::set_systemic_midi_input_latency (std::string const device, uint32_t sl)
+{
+ struct AlsaMidiDeviceInfo * nfo = midi_device_info(device);
+ if (!nfo) return -1;
+ nfo->systemic_input_latency = sl;
+ return 0;
+}
+
+int
+AlsaAudioBackend::set_systemic_midi_output_latency (std::string const device, uint32_t sl)
+{
+ struct AlsaMidiDeviceInfo * nfo = midi_device_info(device);
+ if (!nfo) return -1;
+ nfo->systemic_output_latency = sl;
return 0;
}
std::string
AlsaAudioBackend::device_name () const
{
- return _capture_device;
+ return _audio_device;
}
float
uint32_t
AlsaAudioBackend::systemic_input_latency () const
{
- return _systemic_input_latency;
+ return _systemic_audio_input_latency;
}
uint32_t
AlsaAudioBackend::systemic_output_latency () const
{
- return _systemic_output_latency;
+ return _systemic_audio_output_latency;
}
-/* MIDI */
-void
-AlsaAudioBackend::enumerate_midi_devices (std::vector<std::string> &m) const
-{
- int cardnum = -1;
- snd_ctl_card_info_t *cinfo;
- snd_ctl_card_info_alloca (&cinfo);
- while (snd_card_next (&cardnum) >= 0 && cardnum >= 0) {
- snd_ctl_t *handle;
- std::string devname = "hw:";
- devname += PBD::to_string (cardnum, std::dec);
- if (snd_ctl_open (&handle, devname.c_str (), 0) >= 0 && snd_ctl_card_info (handle, cinfo) >= 0) {
- int device = -1;
- while (snd_ctl_rawmidi_next_device (handle, &device) >= 0 && device >= 0) {
- snd_rawmidi_info_t *info;
- snd_rawmidi_info_alloca (&info);
- snd_rawmidi_info_set_device (info, device);
-
- int subs_in, subs_out;
-
- snd_rawmidi_info_set_stream (info, SND_RAWMIDI_STREAM_INPUT);
- if (snd_ctl_rawmidi_info (handle, info) >= 0) {
- subs_in = snd_rawmidi_info_get_subdevices_count (info);
- } else {
- subs_in = 0;
- }
-
- snd_rawmidi_info_set_stream (info, SND_RAWMIDI_STREAM_OUTPUT);
- if (snd_ctl_rawmidi_info (handle, info) >= 0) {
- subs_out = snd_rawmidi_info_get_subdevices_count (info);
- } else {
- subs_out = 0;
- }
-
- const int subs = subs_in > subs_out ? subs_in : subs_out;
- if (!subs) {
- continue;
- }
+uint32_t
+AlsaAudioBackend::systemic_midi_input_latency (std::string const device) const
+{
+ struct AlsaMidiDeviceInfo * nfo = midi_device_info(device);
+ if (!nfo) return 0;
+ return nfo->systemic_input_latency;
+}
- for (int sub = 0; sub < subs; ++sub) {
- snd_rawmidi_info_set_stream (info, sub < subs_in ?
- SND_RAWMIDI_STREAM_INPUT :
- SND_RAWMIDI_STREAM_OUTPUT);
+uint32_t
+AlsaAudioBackend::systemic_midi_output_latency (std::string const device) const
+{
+ struct AlsaMidiDeviceInfo * nfo = midi_device_info(device);
+ if (!nfo) return 0;
+ return nfo->systemic_output_latency;
+}
- snd_rawmidi_info_set_subdevice (info, sub);
- if (snd_ctl_rawmidi_info (handle, info) < 0) {
- continue;
- }
+/* MIDI */
+struct AlsaAudioBackend::AlsaMidiDeviceInfo *
+AlsaAudioBackend::midi_device_info(std::string const name) const {
+ for (std::map<std::string, struct AlsaMidiDeviceInfo*>::const_iterator i = _midi_devices.begin (); i != _midi_devices.end(); ++i) {
+ if (i->first == name) {
+ return (i->second);
+ }
+ }
- const char *sub_name = snd_rawmidi_info_get_subdevice_name (info);
- if (sub == 0 && sub_name[0] == '\0') {
- devname = "hw:";
- devname += snd_ctl_card_info_get_id (cinfo);
- devname += ",";
- devname += PBD::to_string (device, std::dec);
- devname += " ";
- devname += snd_rawmidi_info_get_name (info);
- devname += " (";
- if (sub < subs_in) devname += "I";
- if (sub < subs_out) devname += "O";
- devname += ")";
- m.push_back (devname);
- break;
- } else {
- devname = "hw:";
- devname += snd_ctl_card_info_get_id (cinfo);
- devname += ",";
- devname += PBD::to_string (device, std::dec);
- devname += ",";
- devname += PBD::to_string (sub, std::dec);
- devname += " ";
- devname += sub_name;
- devname += " (";
- if (sub < subs_in) devname += "I";
- if (sub < subs_out) devname += "O";
- devname += ")";
- m.push_back (devname);
- }
- }
- }
- snd_ctl_close (handle);
+ std::map<std::string, std::string> devices;
+ get_alsa_rawmidi_device_names(devices);
+ for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end(); ++i) {
+ if (i->first == name) {
+ _midi_devices[name] = new AlsaMidiDeviceInfo();
+ return _midi_devices[name];
}
}
+ return 0;
}
std::vector<std::string>
AlsaAudioBackend::enumerate_midi_options () const
{
std::vector<std::string> m;
- m.push_back (_("-None-"));
- enumerate_midi_devices(m);
- if (m.size() > 2) {
- m.push_back (_("-All-"));
- }
+ m.push_back (_("None"));
+ m.push_back (_("ALSA raw devices"));
return m;
}
+std::vector<AudioBackend::DeviceStatus>
+AlsaAudioBackend::enumerate_midi_devices () const
+{
+ std::vector<AudioBackend::DeviceStatus> s;
+ if (_midi_driver_option == _("None")) {
+ return s;
+ }
+
+ std::map<std::string, std::string> devices;
+ get_alsa_rawmidi_device_names(devices);
+
+ for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end(); ++i) {
+ s.push_back (DeviceStatus (i->first, true));
+ }
+ return s;
+}
+
int
AlsaAudioBackend::set_midi_option (const std::string& opt)
{
- _midi_device = opt;
+ if (opt != _("None") && opt != _("ALSA raw devices")) {
+ return -1;
+ }
+ _midi_driver_option = opt;
return 0;
}
std::string
AlsaAudioBackend::midi_option () const
{
- return _midi_device;
+ return _midi_driver_option;
+}
+
+int
+AlsaAudioBackend::set_midi_device_enabled (std::string const device, bool enable)
+{
+ struct AlsaMidiDeviceInfo * nfo = midi_device_info(device);
+ if (!nfo) return -1;
+ nfo->enabled = enable;
+ return 0;
+}
+
+bool
+AlsaAudioBackend::midi_device_enabled (std::string const device) const
+{
+ struct AlsaMidiDeviceInfo * nfo = midi_device_info(device);
+ if (!nfo) return false;
+ return nfo->enabled;
}
/* State Control */
int
AlsaAudioBackend::_start (bool for_latency_measurement)
{
+ if (!_active && _run) {
+ // recover from 'halted', reap threads
+ stop();
+ }
+
if (_active || _run) {
PBD::error << _("AlsaAudioBackend: already active.") << endmsg;
return -1;
_ports.clear();
}
+ release_device();
+
assert(_rmidi_in.size() == 0);
assert(_rmidi_out.size() == 0);
assert(_pcmi == 0);
- unsigned int pos = _capture_device.find(" ");
- _pcmi = new Alsa_pcmi (_capture_device.substr(0, pos).c_str(), _playback_device.substr(0, pos).c_str(), 0, _samplerate, _samples_per_period, _periods_per_cycle, 0);
+ std::string alsa_device;
+ std::map<std::string, std::string> devices;
+ get_alsa_audio_device_names(devices);
+ for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end(); ++i) {
+ if (i->first == _audio_device) {
+ alsa_device = i->second;
+ break;
+ }
+ }
+
+ acquire_device(alsa_device.c_str());
+ _pcmi = new Alsa_pcmi (alsa_device.c_str(), alsa_device.c_str(), 0, _samplerate, _samples_per_period, _periods_per_cycle, 0);
switch (_pcmi->state ()) {
case 0: /* OK */ break;
case -1: PBD::error << _("AlsaAudioBackend: failed to open device.") << endmsg; break;
}
if (_pcmi->state ()) {
delete _pcmi; _pcmi = 0;
+ release_device();
return -1;
}
PBD::warning << _("AlsaAudioBackend: sample rate does not match.") << endmsg;
}
- if (for_latency_measurement) {
- _systemic_input_latency = 0;
- _systemic_output_latency = 0;
- }
+ _measure_latency = for_latency_measurement;
register_system_midi_ports();
if (register_system_audio_ports()) {
PBD::error << _("AlsaAudioBackend: failed to register system ports.") << endmsg;
delete _pcmi; _pcmi = 0;
+ release_device();
return -1;
}
+ engine.sample_rate_change (_samplerate);
+ engine.buffer_size_change (_samples_per_period);
+
if (engine.reestablish_ports ()) {
PBD::error << _("AlsaAudioBackend: Could not re-establish ports.") << endmsg;
delete _pcmi; _pcmi = 0;
+ release_device();
return -1;
}
- engine.buffer_size_change (_samples_per_period);
engine.reconnect_ports ();
_run = true;
+ _port_change_flag = false;
- if (_realtime_pthread_create (SCHED_FIFO, -20,
+ if (_realtime_pthread_create (SCHED_FIFO, -20, 100000,
&_main_thread, pthread_process, this))
{
if (pthread_create (&_main_thread, NULL, pthread_process, this))
{
PBD::error << _("AlsaAudioBackend: failed to create process thread.") << endmsg;
delete _pcmi; _pcmi = 0;
+ release_device();
_run = false;
return -1;
} else {
if (timeout == 0 || !_active) {
PBD::error << _("AlsaAudioBackend: failed to start process thread.") << endmsg;
delete _pcmi; _pcmi = 0;
+ release_device();
_run = false;
return -1;
}
AlsaAudioBackend::stop ()
{
void *status;
- if (!_active) {
+ if (!_run) {
return 0;
}
unregister_system_ports();
delete _pcmi; _pcmi = 0;
+ release_device();
+
return (_active == false) ? 0 : -1;
}
pthread_attr_t attr;
size_t stacksize = 100000;
- pthread_attr_init (&attr);
- pthread_attr_setstacksize (&attr, stacksize);
ThreadData* td = new ThreadData (this, func, stacksize);
- if (pthread_create (&thread_id, &attr, alsa_process_thread, td)) {
- PBD::error << _("AudioEngine: cannot create process thread.") << endmsg;
+ if (_realtime_pthread_create (SCHED_FIFO, -21, stacksize,
+ &thread_id, alsa_process_thread, td)) {
+ pthread_attr_init (&attr);
+ pthread_attr_setstacksize (&attr, stacksize);
+ if (pthread_create (&thread_id, &attr, alsa_process_thread, td)) {
+ PBD::error << _("AudioEngine: cannot create process thread.") << endmsg;
+ pthread_attr_destroy (&attr);
+ return -1;
+ }
pthread_attr_destroy (&attr);
- return -1;
}
- pthread_attr_destroy (&attr);
_threads.push_back (thread_id);
return 0;
void
AlsaAudioBackend::update_latencies ()
{
+ // trigger latency callback in RT thread (locked graph)
+ port_connect_add_remove_callback();
}
/* PORTENGINE API */
const int a_out = _n_outputs > 0 ? _n_outputs : 2;
/* audio ports */
- lr.min = lr.max = _samples_per_period * _periods_per_cycle + _systemic_input_latency;
+ lr.min = lr.max = _samples_per_period + _measure_latency ? 0 : _systemic_audio_input_latency;
for (int i = 1; i <= a_ins; ++i) {
char tmp[64];
snprintf(tmp, sizeof(tmp), "system:capture_%d", i);
_system_inputs.push_back(static_cast<AlsaPort*>(p));
}
- lr.min = lr.max = _samples_per_period * _periods_per_cycle + _systemic_output_latency;
+ lr.min = lr.max = _samples_per_period + _measure_latency ? 0 : _systemic_audio_output_latency;
for (int i = 1; i <= a_out; ++i) {
char tmp[64];
snprintf(tmp, sizeof(tmp), "system:playback_%d", i);
PortHandle p = add_port(std::string(tmp), DataType::AUDIO, static_cast<PortFlags>(IsInput | IsPhysical | IsTerminal));
if (!p) return -1;
- set_latency_range (p, false, lr);
+ set_latency_range (p, true, lr);
_system_outputs.push_back(static_cast<AlsaPort*>(p));
}
return 0;
int
AlsaAudioBackend::register_system_midi_ports()
{
- LatencyRange lr;
- std::vector<std::string> devices;
+ std::map<std::string, std::string> devices;
+ int midi_ins = 0;
+ int midi_outs = 0;
- if (_midi_device == _("-None-")) {
+ if (_midi_driver_option == _("None")) {
return 0;
}
- else if (_midi_device == _("-All-")) {
- enumerate_midi_devices(devices);
- } else {
- unsigned int pos = _midi_device.find(" ");
- devices.push_back(_midi_device.substr(0, pos));
- }
+ get_alsa_rawmidi_device_names(devices);
- for (std::vector<std::string>::const_iterator i = devices.begin (); i != devices.end (); ++i) {
+ for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end(); ++i) {
+ struct AlsaMidiDeviceInfo * nfo = midi_device_info(i->first);
+ if (!nfo) continue;
+ if (!nfo->enabled) continue;
- AlsaRawMidiOut *mout = new AlsaRawMidiOut (i->c_str());
+ AlsaRawMidiOut *mout = new AlsaRawMidiOut (i->second.c_str());
if (mout->state ()) {
PBD::warning << string_compose (
- _("AlsaRawMidiOut: failed to open midi device '%1'."), *i)
+ _("AlsaRawMidiOut: failed to open midi device '%1'."), i->second)
<< endmsg;
delete mout;
} else {
mout->sync_time (g_get_monotonic_time());
if (mout->start ()) {
PBD::warning << string_compose (
- _("AlsaRawMidiOut: failed to start midi device '%1'."), *i)
+ _("AlsaRawMidiOut: failed to start midi device '%1'."), i->second)
<< endmsg;
delete mout;
} else {
+ char tmp[64];
+ snprintf(tmp, sizeof(tmp), "system:midi_playback_%d", ++midi_ins);
+ PortHandle p = add_port(std::string(tmp), DataType::MIDI, static_cast<PortFlags>(IsInput | IsPhysical | IsTerminal));
+ if (!p) {
+ mout->stop();
+ delete mout;
+ }
+ LatencyRange lr;
+ lr.min = lr.max = _samples_per_period + nfo->systemic_output_latency;
+ set_latency_range (p, false, lr);
+ _system_midi_out.push_back(static_cast<AlsaPort*>(p));
_rmidi_out.push_back (mout);
}
}
- AlsaRawMidiIn *midin = new AlsaRawMidiIn (i->c_str());
+ AlsaRawMidiIn *midin = new AlsaRawMidiIn (i->second.c_str());
if (midin->state ()) {
PBD::warning << string_compose (
- _("AlsaRawMidiIn: failed to open midi device '%1'."), *i)
+ _("AlsaRawMidiIn: failed to open midi device '%1'."), i->second)
<< endmsg;
delete midin;
} else {
midin->sync_time (g_get_monotonic_time());
if (midin->start ()) {
PBD::warning << string_compose (
- _("AlsaRawMidiIn: failed to start midi device '%1'."), *i)
+ _("AlsaRawMidiIn: failed to start midi device '%1'."), i->second)
<< endmsg;
delete midin;
} else {
+ char tmp[64];
+ snprintf(tmp, sizeof(tmp), "system:midi_capture_%d", ++midi_outs);
+ PortHandle p = add_port(std::string(tmp), DataType::MIDI, static_cast<PortFlags>(IsOutput | IsPhysical | IsTerminal));
+ if (!p) {
+ midin->stop();
+ delete midin;
+ continue;
+ }
+ LatencyRange lr;
+ lr.min = lr.max = _samples_per_period + nfo->systemic_input_latency;
+ set_latency_range (p, false, lr);
+ _system_midi_in.push_back(static_cast<AlsaPort*>(p));
_rmidi_in.push_back (midin);
}
}
}
-
- const int m_ins = _rmidi_in.size();
- const int m_out = _rmidi_out.size();
-
- lr.min = lr.max = _samples_per_period + _systemic_input_latency;
- for (int i = 1; i <= m_ins; ++i) {
- char tmp[64];
- snprintf(tmp, sizeof(tmp), "system:midi_capture_%d", i);
- PortHandle p = add_port(std::string(tmp), DataType::MIDI, static_cast<PortFlags>(IsOutput | IsPhysical | IsTerminal));
- if (!p) return -1;
- set_latency_range (p, false, lr);
- _system_midi_in.push_back(static_cast<AlsaPort*>(p));
- }
-
- lr.min = lr.max = _samples_per_period + _systemic_output_latency;
- for (int i = 1; i <= m_out; ++i) {
- char tmp[64];
- snprintf(tmp, sizeof(tmp), "system:midi_playback_%d", i);
- PortHandle p = add_port(std::string(tmp), DataType::MIDI, static_cast<PortFlags>(IsInput | IsPhysical | IsTerminal));
- if (!p) return -1;
- set_latency_range (p, false, lr);
- _system_midi_out.push_back(static_cast<AlsaPort*>(p));
- }
-
return 0;
}
clock1 = g_get_monotonic_time();
_pcmi->pcm_start ();
int no_proc_errors = 0;
+ const int bailout = 2 * _samplerate / _samples_per_period;
+ const int64_t nomial_time = 1e6 * _samples_per_period / _samplerate;
+
+ manager.registration_callback();
+ manager.graph_order_callback();
while (_run) {
long nr;
++no_proc_errors;
xrun = true;
}
- if (_pcmi->state () < 0 || no_proc_errors > 50) {
+ if (_pcmi->state () < 0 || no_proc_errors > bailout) {
PBD::error << _("AlsaAudioBackend: I/O error. Audio Process Terminated.") << endmsg;
break;
}
return 0;
}
+ i = 0;
+ for (std::vector<AlsaPort*>::iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it, ++i) {
+ static_cast<AlsaMidiPort*>(*it)->next_period();
+ }
+
/* queue midi*/
i = 0;
for (std::vector<AlsaPort*>::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it, ++i) {
assert (_rmidi_out.size() > i);
+ const AlsaMidiBuffer src = static_cast<const AlsaMidiPort*>(*it)->const_buffer();
AlsaRawMidiOut *rm = static_cast<AlsaRawMidiOut*>(_rmidi_out.at(i));
- const AlsaMidiBuffer *src = static_cast<const AlsaMidiBuffer*>((*it)->get_buffer(0));
- rm->sync_time (clock1); // ?? use clock pre DSP load?
- for (AlsaMidiBuffer::const_iterator mit = src->begin (); mit != src->end (); ++mit) {
+ rm->sync_time (clock1);
+ for (AlsaMidiBuffer::const_iterator mit = src.begin (); mit != src.end (); ++mit) {
rm->send_event ((*mit)->timestamp(), (*mit)->data(), (*mit)->size());
}
}
/* calculate DSP load */
clock2 = g_get_monotonic_time();
const int64_t elapsed_time = clock2 - clock1;
- const int64_t nomial_time = 1e6 * _samples_per_period / _samplerate;
_dsp_load = elapsed_time / (float) nomial_time;
}
Glib::usleep (100); // don't hog cpu
}
+ bool connections_changed = false;
+ bool ports_changed = false;
if (!pthread_mutex_trylock (&_port_callback_mutex)) {
+ if (_port_change_flag) {
+ ports_changed = true;
+ _port_change_flag = false;
+ }
+ if (!_port_connection_queue.empty ()) {
+ connections_changed = true;
+ }
while (!_port_connection_queue.empty ()) {
PortConnectData *c = _port_connection_queue.back ();
manager.connect_callback (c->a, c->b, c->c);
}
pthread_mutex_unlock (&_port_callback_mutex);
}
+ if (ports_changed) {
+ manager.registration_callback();
+ }
+ if (connections_changed) {
+ manager.graph_order_callback();
+ }
+ if (connections_changed || ports_changed) {
+ engine.latency_callback(false);
+ engine.latency_callback(true);
+ }
}
_pcmi->pcm_stop ();
static bool already_configured ();
static ARDOUR::AudioBackendInfo _descriptor = {
- "Alsa",
+ "ALSA",
instantiate,
deinstantiate,
backend_factory,
AlsaMidiPort::AlsaMidiPort (AlsaAudioBackend &b, const std::string& name, PortFlags flags)
: AlsaPort (b, name, flags)
+ , _bufperiod (0)
{
- _buffer.clear ();
+ _buffer[0].clear ();
+ _buffer[1].clear ();
}
AlsaMidiPort::~AlsaMidiPort () { }
void* AlsaMidiPort::get_buffer (pframes_t /* nframes */)
{
if (is_input ()) {
- _buffer.clear ();
+ (_buffer[_bufperiod]).clear ();
for (std::vector<AlsaPort*>::const_iterator i = get_connections ().begin ();
i != get_connections ().end ();
++i) {
const AlsaMidiBuffer src = static_cast<const AlsaMidiPort*>(*i)->const_buffer ();
for (AlsaMidiBuffer::const_iterator it = src.begin (); it != src.end (); ++it) {
- _buffer.push_back (boost::shared_ptr<AlsaMidiEvent>(new AlsaMidiEvent (**it)));
+ (_buffer[_bufperiod]).push_back (boost::shared_ptr<AlsaMidiEvent>(new AlsaMidiEvent (**it)));
}
}
- std::sort (_buffer.begin (), _buffer.end (), MidiEventSorter());
+ std::sort ((_buffer[_bufperiod]).begin (), (_buffer[_bufperiod]).end (), MidiEventSorter());
}
- return &_buffer;
+ return &(_buffer[_bufperiod]);
}
AlsaMidiEvent::AlsaMidiEvent (const pframes_t timestamp, const uint8_t* data, size_t size)