X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fbackends%2Fportaudio%2Fportaudio_backend.cc;h=b25a54f7a9e965e375fa92a2b509e04791bf3c7e;hb=cf52d6e4b40111eb04b244ec054055a4ec15dbe0;hp=dccca3b4c817f1fcc216e9e4822f6a69ca6e59c4;hpb=ee77a7a88300b4f59bd84881a025599a1c668879;p=ardour.git diff --git a/libs/backends/portaudio/portaudio_backend.cc b/libs/backends/portaudio/portaudio_backend.cc index dccca3b4c8..b25a54f7a9 100644 --- a/libs/backends/portaudio/portaudio_backend.cc +++ b/libs/backends/portaudio/portaudio_backend.cc @@ -24,6 +24,10 @@ #include #endif +#ifdef COMPILER_MINGW +#include +#endif + #include #include "portaudio_backend.h" @@ -32,13 +36,14 @@ #include "pbd/compose.h" #include "pbd/error.h" #include "pbd/file_utils.h" +#include "pbd/windows_timer_utils.h" +#include "pbd/windows_mmcss.h" #include "ardour/filesystem_paths.h" #include "ardour/port_manager.h" -#include "i18n.h" +#include "pbd/i18n.h" -#include "win_utils.h" -#include "mmcss.h" +#include "audio_utils.h" #include "debug.h" @@ -61,11 +66,15 @@ PortAudioBackend::PortAudioBackend (AudioEngine& e, AudioBackendInfo& info) , _pcmio (0) , _run (false) , _active (false) + , _use_blocking_api(false) , _freewheel (false) + , _freewheeling (false) + , _freewheel_ack (false) + , _reinit_thread_callback (false) , _measure_latency (false) - , m_cycle_count(0) - , m_total_deviation_us(0) - , m_max_deviation_us(0) + , _cycle_count(0) + , _total_deviation_us(0) + , _max_deviation_us(0) , _input_audio_device("") , _output_audio_device("") , _midi_driver_option(get_standard_device_name(DeviceNone)) @@ -81,8 +90,8 @@ PortAudioBackend::PortAudioBackend (AudioEngine& e, AudioBackendInfo& info) { _instance_name = s_instance_name; pthread_mutex_init (&_port_callback_mutex, 0); - - mmcss::initialize (); + pthread_mutex_init (&_freewheel_mutex, 0); + pthread_cond_init (&_freewheel_signal, 0); _pcmio = new PortAudioIO (); _midiio = new WinMMEMidiIO (); @@ -93,9 +102,9 @@ PortAudioBackend::~PortAudioBackend () delete _pcmio; _pcmio = 0; delete _midiio; _midiio = 0; - mmcss::deinitialize (); - pthread_mutex_destroy (&_port_callback_mutex); + pthread_mutex_destroy (&_freewheel_mutex); + pthread_cond_destroy (&_freewheel_signal); } /* AUDIOBACKEND API */ @@ -141,9 +150,29 @@ PortAudioBackend::set_driver (const std::string& name) DEBUG_AUDIO (string_compose ("Portaudio: Unable to set_driver %1 \n", name)); return -1; } + _pcmio->update_devices(); return 0; } +bool +PortAudioBackend::update_devices () +{ + // update midi device info? + return _pcmio->update_devices(); +} + +void +PortAudioBackend::set_use_buffered_io (bool use_buffered_io) +{ + DEBUG_AUDIO (string_compose ("Portaudio: use_buffered_io %1 \n", use_buffered_io)); + + if (running()) { + return; + } + + _use_blocking_api = use_buffered_io; +} + std::string PortAudioBackend::driver_name () const { @@ -168,7 +197,6 @@ PortAudioBackend::enumerate_devices () const std::vector PortAudioBackend::enumerate_input_devices () const { - _pcmio->discover(); _input_audio_device_status.clear(); std::map input_devices; _pcmio->input_device_list(input_devices); @@ -183,7 +211,6 @@ PortAudioBackend::enumerate_input_devices () const std::vector PortAudioBackend::enumerate_output_devices () const { - _pcmio->discover(); _output_audio_device_status.clear(); std::map output_devices; _pcmio->output_device_list(output_devices); @@ -316,6 +343,24 @@ PortAudioBackend::set_systemic_output_latency (uint32_t sl) return 0; } +int +PortAudioBackend::set_systemic_midi_input_latency (std::string const device, uint32_t sl) +{ + MidiDeviceInfo* nfo = midi_device_info (device); + if (!nfo) return -1; + nfo->systemic_input_latency = sl; + return 0; +} + +int +PortAudioBackend::set_systemic_midi_output_latency (std::string const device, uint32_t sl) +{ + MidiDeviceInfo* nfo = midi_device_info (device); + if (!nfo) return -1; + nfo->systemic_output_latency = sl; + return 0; +} + /* Retrieving parameters */ std::string PortAudioBackend::device_name () const @@ -377,6 +422,22 @@ PortAudioBackend::systemic_output_latency () const return _systemic_audio_output_latency; } +uint32_t +PortAudioBackend::systemic_midi_input_latency (std::string const device) const +{ + MidiDeviceInfo* nfo = midi_device_info (device); + if (!nfo) return 0; + return nfo->systemic_input_latency; +} + +uint32_t +PortAudioBackend::systemic_midi_output_latency (std::string const device) const +{ + MidiDeviceInfo* nfo = midi_device_info (device); + if (!nfo) return 0; + return nfo->systemic_output_latency; +} + std::string PortAudioBackend::control_app_name () const { @@ -418,31 +479,98 @@ PortAudioBackend::midi_option () const return _midi_driver_option; } +std::vector +PortAudioBackend::enumerate_midi_devices () const +{ + std::vector midi_device_status; + std::vector device_info; + + if (_midi_driver_option == winmme_driver_name) { + _midiio->update_device_info (); + device_info = _midiio->get_device_info (); + } + + for (std::vector::const_iterator i = device_info.begin(); + i != device_info.end(); + ++i) { + midi_device_status.push_back(DeviceStatus((*i)->device_name, true)); + } + return midi_device_status; +} + +MidiDeviceInfo* +PortAudioBackend::midi_device_info (const std::string& device_name) const +{ + std::vector dev_info; + + if (_midi_driver_option == winmme_driver_name) { + dev_info = _midiio->get_device_info(); + + for (std::vector::const_iterator i = dev_info.begin(); + i != dev_info.end(); + ++i) { + if ((*i)->device_name == device_name) { + return *i; + } + } + } + return 0; +} + +int +PortAudioBackend::set_midi_device_enabled (std::string const device, bool enable) +{ + MidiDeviceInfo* nfo = midi_device_info(device); + if (!nfo) return -1; + nfo->enable = enable; + return 0; +} + +bool +PortAudioBackend::midi_device_enabled (std::string const device) const +{ + MidiDeviceInfo* nfo = midi_device_info(device); + if (!nfo) return false; + return nfo->enable; +} + /* State Control */ -static void * pthread_process (void *arg) +static void * blocking_thread_func (void *arg) { PortAudioBackend *d = static_cast(arg); - d->main_process_thread (); + d->blocking_process_thread (); pthread_exit (0); return 0; } +bool +PortAudioBackend::engine_halted () +{ + return !_active && _run; +} + +bool +PortAudioBackend::running () +{ + return _active || _run; +} + int PortAudioBackend::_start (bool for_latency_measurement) { - if (!_active && _run) { - // recover from 'halted', reap threads + if (engine_halted()) { stop(); } - if (_active || _run) { - PBD::error << _("PortAudioBackend: already active.") << endmsg; - return -1; + if (running()) { + DEBUG_AUDIO("Already started.\n"); + return BackendReinitializationError; } if (_ports.size()) { - PBD::warning << _("PortAudioBackend: recovering from unclean shutdown, port registry is not empty.") << endmsg; + DEBUG_AUDIO( + "Recovering from unclean shutdown, port registry is not empty.\n"); _system_inputs.clear(); _system_outputs.clear(); _system_midi_in.clear(); @@ -455,25 +583,46 @@ PortAudioBackend::_start (bool for_latency_measurement) _freewheeling = false; _freewheel = false; - _pcmio->pcm_setup (name_to_id(_input_audio_device), name_to_id(_output_audio_device), _samplerate, _samples_per_period); - - switch (_pcmio->state ()) { - case 0: /* OK */ break; - case -1: PBD::error << _("PortAudioBackend: failed to open device.") << endmsg; break; - default: PBD::error << _("PortAudioBackend: initialization failed.") << endmsg; break; - } - if (_pcmio->state ()) { - return -1; + PaErrorCode err = paNoError; + + if (_use_blocking_api) { + DEBUG_AUDIO("Opening blocking audio stream\n"); + err = _pcmio->open_blocking_stream(name_to_id(_input_audio_device), + name_to_id(_output_audio_device), + _samplerate, + _samples_per_period); + } else { + DEBUG_AUDIO("Opening callback audio stream\n"); + err = _pcmio->open_callback_stream(name_to_id(_input_audio_device), + name_to_id(_output_audio_device), + _samplerate, + _samples_per_period, + portaudio_callback, + this); + } + + // reintepret Portaudio error messages + switch (err) { + case paNoError: + break; + case paBadIODeviceCombination: + return DeviceConfigurationNotSupportedError; + case paInvalidChannelCount: + return ChannelCountNotSupportedError; + case paInvalidSampleRate: + return SampleRateNotSupportedError; + default: + return AudioDeviceOpenError; } if (_n_outputs != _pcmio->n_playback_channels ()) { _n_outputs = _pcmio->n_playback_channels (); - PBD::info << _("PortAudioBackend: adjusted output channel count to match device.") << endmsg; + PBD::info << get_error_string(OutputChannelCountNotSupportedError) << endmsg; } if (_n_inputs != _pcmio->n_capture_channels ()) { _n_inputs = _pcmio->n_capture_channels (); - PBD::info << _("PortAudioBackend: adjusted input channel count to match device.") << endmsg; + PBD::info << get_error_string(InputChannelCountNotSupportedError) << endmsg; } #if 0 if (_pcmio->samples_per_period() != _samples_per_period) { @@ -485,7 +634,7 @@ PortAudioBackend::_start (bool for_latency_measurement) if (_pcmio->sample_rate() != _samplerate) { _samplerate = _pcmio->sample_rate(); engine.sample_rate_change (_samplerate); - PBD::warning << _("PortAudioBackend: sample rate does not match.") << endmsg; + PBD::warning << get_error_string(SampleRateNotSupportedError) << endmsg; } _measure_latency = for_latency_measurement; @@ -499,49 +648,144 @@ PortAudioBackend::_start (bool for_latency_measurement) _midiio->start(); // triggers port discovery, callback coremidi_rediscover() } - m_cycle_timer.set_samplerate(_samplerate); - m_cycle_timer.set_samples_per_cycle(_samples_per_period); + _cycle_timer.set_samplerate(_samplerate); + _cycle_timer.set_samples_per_cycle(_samples_per_period); + + _dsp_calc.set_max_time_us (_cycle_timer.get_length_us()); DEBUG_MIDI ("Registering MIDI ports\n"); if (register_system_midi_ports () != 0) { - PBD::error << _ ("PortAudioBackend: failed to register system midi ports.") - << endmsg; + DEBUG_PORTS("Failed to register system midi ports.\n") _run = false; - return -1; + return PortRegistrationError; } DEBUG_AUDIO ("Registering Audio ports\n"); if (register_system_audio_ports()) { - PBD::error << _("PortAudioBackend: failed to register system audio ports.") << endmsg; + DEBUG_PORTS("Failed to register system audio ports.\n"); _run = false; - return -1; + return PortRegistrationError; } engine.sample_rate_change (_samplerate); engine.buffer_size_change (_samples_per_period); if (engine.reestablish_ports ()) { - PBD::error << _("PortAudioBackend: Could not re-establish ports.") << endmsg; + DEBUG_PORTS("Could not re-establish ports.\n"); _run = false; - return -1; + return PortReconnectError; } engine.reconnect_ports (); _run = true; _port_change_flag = false; + if (_use_blocking_api) { + if (!start_blocking_process_thread()) { + return ProcessThreadStartError; + } + } else { + if (_pcmio->start_stream() != paNoError) { + DEBUG_AUDIO("Unable to start stream\n"); + return AudioDeviceOpenError; + } + + if (!start_freewheel_process_thread()) { + DEBUG_AUDIO("Unable to start freewheel thread\n"); + stop(); + return ProcessThreadStartError; + } + } + + return NoError; +} + +int +PortAudioBackend::portaudio_callback(const void* input, + void* output, + unsigned long frame_count, + const PaStreamCallbackTimeInfo* time_info, + PaStreamCallbackFlags status_flags, + void* user_data) +{ + PortAudioBackend* pa_backend = static_cast(user_data); + + if (!pa_backend->process_callback((const float*)input, + (float*)output, + frame_count, + time_info, + status_flags)) { + return paAbort; + } + + return paContinue; +} + +bool +PortAudioBackend::process_callback(const float* input, + float* output, + uint32_t frame_count, + const PaStreamCallbackTimeInfo* timeInfo, + PaStreamCallbackFlags statusFlags) +{ + _active = true; + + _dsp_calc.set_start_timestamp_us (PBD::get_microseconds()); + + if (_run && _freewheel && !_freewheel_ack) { + // acknowledge freewheeling; hand-over thread ID + pthread_mutex_lock (&_freewheel_mutex); + if (_freewheel) { + DEBUG_AUDIO("Setting _freewheel_ack = true;\n"); + _freewheel_ack = true; + } + DEBUG_AUDIO("Signalling freewheel thread\n"); + pthread_cond_signal (&_freewheel_signal); + pthread_mutex_unlock (&_freewheel_mutex); + } + + if (statusFlags & paInputUnderflow || + statusFlags & paInputOverflow || + statusFlags & paOutputUnderflow || + statusFlags & paOutputOverflow ) { + DEBUG_AUDIO("PortAudio: Xrun\n"); + engine.Xrun(); + return true; + } + + if (!_run || _freewheel) { + memset(output, 0, frame_count * sizeof(float) * _system_outputs.size()); + return true; + } + + bool in_main_thread = pthread_equal(_main_thread, pthread_self()); + + if (_reinit_thread_callback || !in_main_thread) { + _reinit_thread_callback = false; + _main_thread = pthread_self(); + AudioEngine::thread_init_callback (this); + } + + process_port_connection_changes(); + + return blocking_process_main (input, output); +} + +bool +PortAudioBackend::start_blocking_process_thread () +{ if (_realtime_pthread_create (SCHED_FIFO, -20, 100000, - &_main_thread, pthread_process, this)) + &_main_blocking_thread, blocking_thread_func, this)) { - if (pthread_create (&_main_thread, NULL, pthread_process, this)) + if (pthread_create (&_main_blocking_thread, NULL, blocking_thread_func, this)) { - PBD::error << _("PortAudioBackend: failed to create process thread.") << endmsg; + DEBUG_AUDIO("Failed to create main audio thread\n"); _run = false; - return -1; + return false; } else { - PBD::warning << _("PortAudioBackend: cannot acquire realtime permissions.") << endmsg; + PBD::warning << get_error_string(AquireRealtimePermissionError) << endmsg; } } @@ -549,37 +793,172 @@ PortAudioBackend::_start (bool for_latency_measurement) while (!_active && --timeout > 0) { Glib::usleep (1000); } if (timeout == 0 || !_active) { - PBD::error << _("PortAudioBackend: failed to start.") << endmsg; - _pcmio->pcm_stop(); + DEBUG_AUDIO("Failed to start main audio thread\n"); + _pcmio->close_stream(); _run = false; unregister_ports(); _active = false; - return -1; + return false; } + return true; +} - return 0; +bool +PortAudioBackend::stop_blocking_process_thread () +{ + void *status; + + if (pthread_join (_main_blocking_thread, &status)) { + DEBUG_AUDIO("Failed to stop main audio thread\n"); + return false; + } + + return true; } int PortAudioBackend::stop () { - void *status; if (!_run) { return 0; } + _midiio->stop(); + _run = false; - if (pthread_join (_main_thread, &status)) { - PBD::error << _("PortAudioBackend: failed to terminate.") << endmsg; - return -1; - } + if (_use_blocking_api) { + if (!stop_blocking_process_thread()) { + return -1; + } + } else { + _pcmio->close_stream(); + _active = false; + + if (!stop_freewheel_process_thread()) { + return -1; + } + } unregister_ports(); return (_active == false) ? 0 : -1; } +static void* freewheel_thread(void* arg) +{ + PortAudioBackend* d = static_cast(arg); + d->freewheel_process_thread (); + pthread_exit (0); + return 0; +} + +bool +PortAudioBackend::start_freewheel_process_thread () +{ + if (pthread_create(&_pthread_freewheel, NULL, freewheel_thread, this)) { + DEBUG_AUDIO("Failed to create main audio thread\n"); + return false; + } + + int timeout = 5000; + while (!_freewheel_thread_active && --timeout > 0) { Glib::usleep (1000); } + + if (timeout == 0 || !_freewheel_thread_active) { + DEBUG_AUDIO("Failed to start freewheel thread\n"); + return false; + } + return true; +} + +bool +PortAudioBackend::stop_freewheel_process_thread () +{ + void *status; + + if (!_freewheel_thread_active) { + return true; + } + + DEBUG_AUDIO("Signaling freewheel thread to stop\n"); + + pthread_mutex_lock (&_freewheel_mutex); + pthread_cond_signal (&_freewheel_signal); + pthread_mutex_unlock (&_freewheel_mutex); + + if (pthread_join (_pthread_freewheel, &status) != 0) { + DEBUG_AUDIO("Failed to stop freewheel thread\n"); + return false; + } + + return true; +} + +void* +PortAudioBackend::freewheel_process_thread() +{ + _freewheel_thread_active = true; + + bool first_run = false; + + pthread_mutex_lock (&_freewheel_mutex); + + while(_run) { + // check if we should run, + if (_freewheeling != _freewheel) { + if (!_freewheeling) { + DEBUG_AUDIO("Leaving freewheel\n"); + _freewheel = false; // first mark as disabled + _reinit_thread_callback = true; // hand over _main_thread + _freewheel_ack = false; // prepare next handshake + _midiio->set_enabled(true); + engine.freewheel_callback (_freewheeling); + } else { + first_run = true; + _freewheel = true; + } + } + + if (!_freewheel || !_freewheel_ack) { + // wait for a change, we use a timed wait to + // terminate early in case some error sets _run = 0 + struct timeval tv; + struct timespec ts; + gettimeofday (&tv, NULL); + ts.tv_sec = tv.tv_sec + 3; + ts.tv_nsec = 0; + DEBUG_AUDIO("Waiting for freewheel change\n"); + pthread_cond_timedwait (&_freewheel_signal, &_freewheel_mutex, &ts); + continue; + } + + if (first_run) { + // tell the engine we're ready to GO. + engine.freewheel_callback (_freewheeling); + first_run = false; + _main_thread = pthread_self(); + AudioEngine::thread_init_callback (this); + _midiio->set_enabled(false); + } + + if (!blocking_process_freewheel()) { + break; + } + + process_port_connection_changes(); + } + + pthread_mutex_unlock (&_freewheel_mutex); + + _freewheel_thread_active = false; + + if (_run) { + // engine.process_callback() returner error + engine.halted_callback("CoreAudio Freehweeling aborted."); + } + return 0; +} + int PortAudioBackend::freewheel (bool onoff) { @@ -587,6 +966,11 @@ PortAudioBackend::freewheel (bool onoff) return 0; } _freewheeling = onoff; + + if (0 == pthread_mutex_trylock (&_freewheel_mutex)) { + pthread_cond_signal (&_freewheel_signal); + pthread_mutex_unlock (&_freewheel_mutex); + } return 0; } @@ -600,10 +984,10 @@ size_t PortAudioBackend::raw_buffer_size (DataType t) { switch (t) { - case DataType::AUDIO: - return _samples_per_period * sizeof(Sample); - case DataType::MIDI: - return _max_buffer_size; // XXX not really limited + case DataType::AUDIO: + return _samples_per_period * sizeof(Sample); + case DataType::MIDI: + return _max_buffer_size; // XXX not really limited } return 0; } @@ -627,11 +1011,11 @@ PortAudioBackend::samples_since_cycle_start () if (!_active || !_run || _freewheeling || _freewheel) { return 0; } - if (!m_cycle_timer.valid()) { + if (!_cycle_timer.valid()) { return 0; } - return m_cycle_timer.samples_since_cycle_start (utils::get_microseconds()); + return _cycle_timer.samples_since_cycle_start (PBD::get_microseconds()); } int @@ -650,6 +1034,41 @@ PortAudioBackend::name_to_id(std::string device_name) const { return device_id; } +bool +PortAudioBackend::set_mmcss_pro_audio (HANDLE* task_handle) +{ + bool mmcss_success = PBD::MMCSS::set_thread_characteristics ("Pro Audio", task_handle); + + if (!mmcss_success) { + PBD::warning << get_error_string(SettingAudioThreadPriorityError) << endmsg; + return false; + } else { + DEBUG_THREADS("Thread characteristics set to Pro Audio\n"); + } + + bool mmcss_priority = + PBD::MMCSS::set_thread_priority(*task_handle, PBD::MMCSS::AVRT_PRIORITY_NORMAL); + + if (!mmcss_priority) { + PBD::warning << get_error_string(SettingAudioThreadPriorityError) << endmsg; + return false; + } else { + DEBUG_THREADS("Thread priority set to AVRT_PRIORITY_NORMAL\n"); + } + + return true; +} + +bool +PortAudioBackend::reset_mmcss (HANDLE task_handle) +{ + if (!PBD::MMCSS::revert_thread_characteristics(task_handle)) { + DEBUG_THREADS("Unable to reset process thread characteristics\n"); + return false; + } + return true; +} + void * PortAudioBackend::portaudio_process_thread (void *arg) { @@ -659,9 +1078,7 @@ PortAudioBackend::portaudio_process_thread (void *arg) #ifdef USE_MMCSS_THREAD_PRIORITIES HANDLE task_handle; - - mmcss::set_thread_characteristics ("Pro Audio", &task_handle); - mmcss::set_thread_priority (task_handle, mmcss::AVRT_PRIORITY_NORMAL); + bool mmcss_success = set_mmcss_pro_audio (&task_handle); #endif DWORD tid = GetCurrentThreadId (); @@ -670,7 +1087,9 @@ PortAudioBackend::portaudio_process_thread (void *arg) f (); #ifdef USE_MMCSS_THREAD_PRIORITIES - mmcss::revert_thread_characteristics (task_handle); + if (mmcss_success) { + reset_mmcss (task_handle); + } #endif return 0; @@ -690,7 +1109,7 @@ PortAudioBackend::create_process_thread (boost::function func) pthread_attr_init (&attr); pthread_attr_setstacksize (&attr, stacksize); if (pthread_create (&thread_id, &attr, portaudio_process_thread, td)) { - PBD::error << _("AudioEngine: cannot create process thread.") << endmsg; + DEBUG_AUDIO("Cannot create process thread."); pthread_attr_destroy (&attr); return -1; } @@ -710,7 +1129,7 @@ PortAudioBackend::join_process_threads () { void *status; if (pthread_join (*i, &status)) { - PBD::error << _("AudioEngine: cannot terminate process thread.") << endmsg; + DEBUG_AUDIO("Cannot terminate process thread."); rv -= 1; } } @@ -721,10 +1140,15 @@ PortAudioBackend::join_process_threads () bool PortAudioBackend::in_process_thread () { - if (pthread_equal (_main_thread, pthread_self()) != 0) { - return true; + if (_use_blocking_api) { + if (pthread_equal(_main_blocking_thread, pthread_self()) != 0) { + return true; + } + } else { + if (pthread_equal(_main_thread, pthread_self()) != 0) { + return true; + } } - for (std::vector::const_iterator i = _threads.begin (); i != _threads.end (); ++i) { if (pthread_equal (*i, pthread_self ()) != 0) { @@ -777,7 +1201,7 @@ int PortAudioBackend::set_port_name (PortEngine::PortHandle port, const std::string& name) { if (!valid_port (port)) { - PBD::error << _("PortAudioBackend::set_port_name: Invalid Port(s)") << endmsg; + DEBUG_PORTS("set_port_name: Invalid Port(s)\n"); return -1; } return static_cast(port)->set_name (_instance_name + ":" + name); @@ -787,7 +1211,7 @@ std::string PortAudioBackend::get_port_name (PortEngine::PortHandle port) const { if (!valid_port (port)) { - PBD::error << _("PortAudioBackend::get_port_name: Invalid Port(s)") << endmsg; + DEBUG_PORTS("get_port_name: Invalid Port(s)\n"); return std::string (); } return static_cast(port)->name (); @@ -800,8 +1224,7 @@ PortAudioBackend::get_port_property (PortHandle port, std::string& type) const { if (!valid_port (port)) { - PBD::error << _ ("PortAudioBackend::get_port_name: Invalid Port(s)") - << endmsg; + DEBUG_PORTS("get_port_name: Invalid Port(s)\n"); return -1; } @@ -815,6 +1238,24 @@ PortAudioBackend::get_port_property (PortHandle port, return -1; } +int +PortAudioBackend::set_port_property (PortHandle port, + const std::string& key, + const std::string& value, + const std::string& type) +{ + if (!valid_port (port)) { + DEBUG_PORTS("get_port_name: Invalid Port(s)\n"); + return -1; + } + + if (key == "http://jackaudio.org/metadata/pretty-name" && type.empty ()) { + static_cast(port)->set_pretty_name (value); + return 0; + } + return -1; +} + PortEngine::PortHandle PortAudioBackend::get_port_by_name (const std::string& name) const { @@ -879,21 +1320,21 @@ PortAudioBackend::add_port ( { assert(name.size ()); if (find_port (name)) { - PBD::error << _("PortAudioBackend::register_port: Port already exists:") - << " (" << name << ")" << endmsg; + DEBUG_PORTS( + string_compose("register_port: Port already exists: (%1)\n", name)); return 0; } PamPort* port = NULL; switch (type) { - case DataType::AUDIO: - port = new PortAudioPort (*this, name, flags); - break; - case DataType::MIDI: - port = new PortMidiPort (*this, name, flags); - break; - default: - PBD::error << _("PortAudioBackend::register_port: Invalid Data Type.") << endmsg; - return 0; + case DataType::AUDIO: + port = new PortAudioPort(*this, name, flags); + break; + case DataType::MIDI: + port = new PortMidiPort(*this, name, flags); + break; + default: + DEBUG_PORTS("register_port: Invalid Data Type.\n"); + return 0; } _ports.push_back (port); @@ -910,7 +1351,7 @@ PortAudioBackend::unregister_port (PortEngine::PortHandle port_handle) PamPort* port = static_cast(port_handle); std::vector::iterator i = std::find (_ports.begin (), _ports.end (), static_cast(port_handle)); if (i == _ports.end ()) { - PBD::error << _("PortAudioBackend::unregister_port: Failed to find port") << endmsg; + DEBUG_PORTS("unregister_port: Failed to find port\n"); return; } disconnect_all(port_handle); @@ -926,12 +1367,19 @@ PortAudioBackend::register_system_audio_ports() const uint32_t a_ins = _n_inputs; const uint32_t a_out = _n_outputs; - // XXX PA reported stream latencies don't match measurements - const uint32_t portaudio_reported_input_latency = _samples_per_period ; // _pcmio->capture_latency(); - const uint32_t portaudio_reported_output_latency = /* _samples_per_period + */ _pcmio->playback_latency(); + uint32_t capture_latency = 0; + uint32_t playback_latency = 0; + + // guard against erroneous latency values + if (_pcmio->capture_latency() > _samples_per_period) { + capture_latency = _pcmio->capture_latency() - _samples_per_period; + } + if (_pcmio->playback_latency() > _samples_per_period) { + playback_latency = _pcmio->playback_latency() - _samples_per_period; + } /* audio ports */ - lr.min = lr.max = portaudio_reported_input_latency + (_measure_latency ? 0 : _systemic_audio_input_latency); + lr.min = lr.max = capture_latency + (_measure_latency ? 0 : _systemic_audio_input_latency); for (uint32_t i = 0; i < a_ins; ++i) { char tmp[64]; snprintf(tmp, sizeof(tmp), "system:capture_%d", i+1); @@ -944,7 +1392,7 @@ PortAudioBackend::register_system_audio_ports() _system_inputs.push_back (audio_port); } - lr.min = lr.max = portaudio_reported_output_latency + (_measure_latency ? 0 : _systemic_audio_output_latency); + lr.min = lr.max = playback_latency + (_measure_latency ? 0 : _systemic_audio_output_latency); for (uint32_t i = 0; i < a_out; ++i) { char tmp[64]; snprintf(tmp, sizeof(tmp), "system:playback_%d", i+1); @@ -975,13 +1423,19 @@ PortAudioBackend::register_system_midi_ports() for (std::vector::const_iterator i = inputs.begin (); i != inputs.end (); ++i) { - std::string port_name = "system_midi:" + (*i)->name() + " capture"; + std::string port_name = "system:midi_capture_" + (*i)->name(); PortHandle p = add_port (port_name, DataType::MIDI, static_cast(IsOutput | IsPhysical | IsTerminal)); if (!p) return -1; + + MidiDeviceInfo* info = _midiio->get_device_info((*i)->name()); + if (info) { // assert? + lr.min = lr.max = _samples_per_period + info->systemic_input_latency; + } set_latency_range (p, false, lr); + PortMidiPort* midi_port = static_cast(p); midi_port->set_pretty_name ((*i)->name()); _system_midi_in.push_back (midi_port); @@ -993,13 +1447,19 @@ PortAudioBackend::register_system_midi_ports() for (std::vector::const_iterator i = outputs.begin (); i != outputs.end (); ++i) { - std::string port_name = "system_midi:" + (*i)->name() + " playback"; + std::string port_name = "system:midi_playback_" + (*i)->name(); PortHandle p = add_port (port_name, DataType::MIDI, static_cast(IsInput | IsPhysical | IsTerminal)); if (!p) return -1; + + MidiDeviceInfo* info = _midiio->get_device_info((*i)->name()); + if (info) { // assert? + lr.min = lr.max = _samples_per_period + info->systemic_output_latency; + } set_latency_range (p, false, lr); + PortMidiPort* midi_port = static_cast(p); midi_port->set_n_periods(2); midi_port->set_pretty_name ((*i)->name()); @@ -1036,13 +1496,11 @@ PortAudioBackend::connect (const std::string& src, const std::string& dst) PamPort* dst_port = find_port (dst); if (!src_port) { - PBD::error << _("PortAudioBackend::connect: Invalid Source port:") - << " (" << src <<")" << endmsg; + DEBUG_PORTS(string_compose("connect: Invalid Source port: (%1)\n", src)); return -1; } if (!dst_port) { - PBD::error << _("PortAudioBackend::connect: Invalid Destination port:") - << " (" << dst <<")" << endmsg; + DEBUG_PORTS(string_compose("connect: Invalid Destination port: (%1)\n", dst)); return -1; } return src_port->connect (dst_port); @@ -1055,7 +1513,7 @@ PortAudioBackend::disconnect (const std::string& src, const std::string& dst) PamPort* dst_port = find_port (dst); if (!src_port || !dst_port) { - PBD::error << _("PortAudioBackend::disconnect: Invalid Port(s)") << endmsg; + DEBUG_PORTS("disconnect: Invalid Port(s)\n"); return -1; } return src_port->disconnect (dst_port); @@ -1066,12 +1524,11 @@ PortAudioBackend::connect (PortEngine::PortHandle src, const std::string& dst) { PamPort* dst_port = find_port (dst); if (!valid_port (src)) { - PBD::error << _("PortAudioBackend::connect: Invalid Source Port Handle") << endmsg; + DEBUG_PORTS("connect: Invalid Source Port Handle\n"); return -1; } if (!dst_port) { - PBD::error << _("PortAudioBackend::connect: Invalid Destination Port") - << " (" << dst << ")" << endmsg; + DEBUG_PORTS(string_compose("connect: Invalid Destination Port (%1)\n", dst)); return -1; } return static_cast(src)->connect (dst_port); @@ -1082,7 +1539,7 @@ PortAudioBackend::disconnect (PortEngine::PortHandle src, const std::string& dst { PamPort* dst_port = find_port (dst); if (!valid_port (src) || !dst_port) { - PBD::error << _("PortAudioBackend::disconnect: Invalid Port(s)") << endmsg; + DEBUG_PORTS("disconnect: Invalid Port(s)\n"); return -1; } return static_cast(src)->disconnect (dst_port); @@ -1092,7 +1549,7 @@ int PortAudioBackend::disconnect_all (PortEngine::PortHandle port) { if (!valid_port (port)) { - PBD::error << _("PortAudioBackend::disconnect_all: Invalid Port") << endmsg; + DEBUG_PORTS("disconnect_all: Invalid Port\n"); return -1; } static_cast(port)->disconnect_all (); @@ -1103,7 +1560,7 @@ bool PortAudioBackend::connected (PortEngine::PortHandle port, bool /* process_callback_safe*/) { if (!valid_port (port)) { - PBD::error << _("PortAudioBackend::disconnect_all: Invalid Port") << endmsg; + DEBUG_PORTS("disconnect_all: Invalid Port\n"); return false; } return static_cast(port)->is_connected (); @@ -1114,7 +1571,7 @@ PortAudioBackend::connected_to (PortEngine::PortHandle src, const std::string& d { PamPort* dst_port = find_port (dst); if (!valid_port (src) || !dst_port) { - PBD::error << _("PortAudioBackend::connected_to: Invalid Port") << endmsg; + DEBUG_PORTS("connected_to: Invalid Port\n"); return false; } return static_cast(src)->is_connected (dst_port); @@ -1124,7 +1581,7 @@ bool PortAudioBackend::physically_connected (PortEngine::PortHandle port, bool /*process_callback_safe*/) { if (!valid_port (port)) { - PBD::error << _("PortAudioBackend::physically_connected: Invalid Port") << endmsg; + DEBUG_PORTS("physically_connected: Invalid Port\n"); return false; } return static_cast(port)->is_physically_connected (); @@ -1134,7 +1591,7 @@ int PortAudioBackend::get_connections (PortEngine::PortHandle port, std::vector& names, bool /*process_callback_safe*/) { if (!valid_port (port)) { - PBD::error << _("PortAudioBackend::get_connections: Invalid Port") << endmsg; + DEBUG_PORTS("get_connections: Invalid Port\n"); return -1; } @@ -1235,7 +1692,7 @@ void PortAudioBackend::set_latency_range (PortEngine::PortHandle port, bool for_playback, LatencyRange latency_range) { if (!valid_port (port)) { - PBD::error << _("PamPort::set_latency_range (): invalid port.") << endmsg; + DEBUG_PORTS("PamPort::set_latency_range (): invalid port.\n"); } static_cast(port)->set_latency_range (latency_range, for_playback); } @@ -1245,7 +1702,7 @@ PortAudioBackend::get_latency_range (PortEngine::PortHandle port, bool for_playb { LatencyRange r; if (!valid_port (port)) { - PBD::error << _("PamPort::get_latency_range (): invalid port.") << endmsg; + DEBUG_PORTS("PamPort::get_latency_range (): invalid port.\n"); r.min = 0; r.max = 0; return r; @@ -1274,7 +1731,7 @@ bool PortAudioBackend::port_is_physical (PortEngine::PortHandle port) const { if (!valid_port (port)) { - PBD::error << _("PamPort::port_is_physical (): invalid port.") << endmsg; + DEBUG_PORTS("PamPort::port_is_physical (): invalid port.\n"); return false; } return static_cast(port)->is_physical (); @@ -1311,9 +1768,14 @@ PortAudioBackend::n_physical_outputs () const PamPort* port = _ports[i]; if (port->is_output () && port->is_physical ()) { switch (port->type ()) { - case DataType::AUDIO: ++n_audio; break; - case DataType::MIDI: ++n_midi; break; - default: break; + case DataType::AUDIO: + ++n_audio; + break; + case DataType::MIDI: + ++n_midi; + break; + default: + break; } } } @@ -1332,9 +1794,14 @@ PortAudioBackend::n_physical_inputs () const PamPort* port = _ports[i]; if (port->is_input () && port->is_physical ()) { switch (port->type ()) { - case DataType::AUDIO: ++n_audio; break; - case DataType::MIDI: ++n_midi; break; - default: break; + case DataType::AUDIO: + ++n_audio; + break; + case DataType::MIDI: + ++n_midi; + break; + default: + break; } } } @@ -1355,32 +1822,24 @@ PortAudioBackend::get_buffer (PortEngine::PortHandle port, pframes_t nframes) void * -PortAudioBackend::main_process_thread () +PortAudioBackend::blocking_process_thread () { AudioEngine::thread_init_callback (this); _active = true; _processed_samples = 0; - uint64_t clock1, clock2; - int64_t min_elapsed_us = 1000000; - int64_t max_elapsed_us = 0; - const int64_t nomial_time = 1e6 * _samples_per_period / _samplerate; - // const int64_t nomial_time = m_cycle_timer.get_length_us(); - manager.registration_callback(); manager.graph_order_callback(); - if (_pcmio->pcm_start()) { - _pcmio->pcm_stop (); + if (_pcmio->start_stream() != paNoError) { + _pcmio->close_stream (); _active = false; - engine.halted_callback("PortAudio I/O error."); + engine.halted_callback(get_error_string(AudioDeviceIOError).c_str()); } #ifdef USE_MMCSS_THREAD_PRIORITIES HANDLE task_handle; - - mmcss::set_thread_characteristics ("Pro Audio", &task_handle); - mmcss::set_thread_priority (task_handle, mmcss::AVRT_PRIORITY_NORMAL); + bool mmcss_success = set_mmcss_pro_audio (&task_handle); #endif DWORD tid = GetCurrentThreadId (); @@ -1396,208 +1855,273 @@ PortAudioBackend::main_process_thread () if (!_freewheel) { switch (_pcmio->next_cycle (_samples_per_period)) { - case 0: // OK - break; - case 1: - DEBUG_AUDIO ("PortAudio: Xrun\n"); - engine.Xrun (); - break; - default: - PBD::error << _("PortAudioBackend: I/O error. Audio Process Terminated.") << endmsg; - break; + case 0: // OK + break; + case 1: + DEBUG_AUDIO("PortAudio: Xrun\n"); + engine.Xrun(); + break; + default: + PBD::error << get_error_string(AudioDeviceIOError) << endmsg; + break; } - uint32_t i = 0; - clock1 = utils::get_microseconds (); - - /* get audio */ - i = 0; - for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it, ++i) { - _pcmio->get_capture_channel (i, (float*)((*it)->get_buffer(_samples_per_period)), _samples_per_period); + if (!blocking_process_main(_pcmio->get_capture_buffer(), + _pcmio->get_playback_buffer())) { + return 0; } + } else { - /* de-queue incoming midi*/ - i=0; - for (std::vector::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it, ++i) { - PortMidiBuffer* mbuf = static_cast((*it)->get_buffer(0)); - mbuf->clear(); - uint64_t timestamp; - pframes_t sample_offset; - uint8_t data[256]; - size_t size = sizeof(data); - while (_midiio->dequeue_input_event (i, - m_cycle_timer.get_start (), - m_cycle_timer.get_next_start (), - timestamp, - data, - size)) { - sample_offset = m_cycle_timer.samples_since_cycle_start (timestamp); - midi_event_put (mbuf, sample_offset, data, size); - DEBUG_MIDI (string_compose ("Dequeuing incoming MIDI data for device: %1 " - "sample_offset: %2 timestamp: %3, size: %4\n", - _midiio->get_inputs ()[i]->name (), - sample_offset, - timestamp, - size)); - size = sizeof(data); - } + if (!blocking_process_freewheel()) { + return 0; } + } - /* clear output buffers */ - for (std::vector::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it) { - memset ((*it)->get_buffer (_samples_per_period), 0, _samples_per_period * sizeof (Sample)); - } + process_port_connection_changes(); + } + _pcmio->close_stream(); + _active = false; + if (_run) { + engine.halted_callback(get_error_string(AudioDeviceIOError).c_str()); + } - m_last_cycle_start = m_cycle_timer.get_start (); - m_cycle_timer.reset_start(utils::get_microseconds()); - m_cycle_count++; - - uint64_t cycle_diff_us = (m_cycle_timer.get_start () - m_last_cycle_start); - int64_t deviation_us = (cycle_diff_us - m_cycle_timer.get_length_us()); - m_total_deviation_us += ::llabs(deviation_us); - m_max_deviation_us = - std::max (m_max_deviation_us, (uint64_t)::llabs (deviation_us)); - - if ((m_cycle_count % 1000) == 0) { - uint64_t mean_deviation_us = m_total_deviation_us / m_cycle_count; - DEBUG_TIMING ( - string_compose ("Mean avg cycle deviation: %1(ms), max %2(ms)\n", - mean_deviation_us * 1e-3, - m_max_deviation_us * 1e-3)); - } +#ifdef USE_MMCSS_THREAD_PRIORITIES + if (mmcss_success) { + reset_mmcss(task_handle); + } +#endif - if (::llabs(deviation_us) > m_cycle_timer.get_length_us()) { - DEBUG_TIMING (string_compose ( - "time between process(ms): %1, Est(ms): %2, Dev(ms): %3\n", - cycle_diff_us * 1e-3, - m_cycle_timer.get_length_us () * 1e-3, - deviation_us * 1e-3)); - } + return 0; +} - /* call engine process callback */ - if (engine.process_callback (_samples_per_period)) { - _pcmio->pcm_stop (); - _active = false; - return 0; - } - /* mixdown midi */ - for (std::vector::iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it) { - static_cast(*it)->next_period(); - } - /* queue outgoing midi */ - i = 0; - for (std::vector::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it, ++i) { - const PortMidiBuffer* src = static_cast(*it)->const_buffer(); - - for (PortMidiBuffer::const_iterator mit = src->begin (); mit != src->end (); ++mit) { - uint64_t timestamp = - m_cycle_timer.timestamp_from_sample_offset ((*mit)->timestamp ()); - DEBUG_MIDI ( - string_compose ("Queuing outgoing MIDI data for device: " - "%1 sample_offset: %2 timestamp: %3, size: %4\n", - _midiio->get_outputs ()[i]->name (), - (*mit)->timestamp (), - timestamp, - (*mit)->size ())); - _midiio->enqueue_output_event ( - i, timestamp, (*mit)->data (), (*mit)->size ()); - } - } +bool +PortAudioBackend::blocking_process_main(const float* interleaved_input_data, + float* interleaved_output_data) +{ + uint32_t i = 0; + int64_t min_elapsed_us = 1000000; + int64_t max_elapsed_us = 0; - /* write back audio */ - i = 0; - for (std::vector::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it, ++i) { - _pcmio->set_playback_channel (i, (float const*)(*it)->get_buffer (_samples_per_period), _samples_per_period); - } + _dsp_calc.set_start_timestamp_us (PBD::get_microseconds()); - _processed_samples += _samples_per_period; + i = 0; + /* Copy input audio data into input port buffers */ + for (std::vector::const_iterator it = _system_inputs.begin(); + it != _system_inputs.end(); + ++it, ++i) { + assert(_system_inputs.size() == _pcmio->n_capture_channels()); + uint32_t channels = _system_inputs.size(); + float* input_port_buffer = (float*)(*it)->get_buffer(_samples_per_period); + deinterleave_audio_data( + interleaved_input_data, input_port_buffer, _samples_per_period, i, channels); + } - /* calculate DSP load */ - clock2 = utils::get_microseconds (); - const int64_t elapsed_time = clock2 - clock1; - _dsp_load = elapsed_time / (float) nomial_time; + process_incoming_midi (); - max_elapsed_us = std::max (elapsed_time, max_elapsed_us); - min_elapsed_us = std::min (elapsed_time, min_elapsed_us); - if ((m_cycle_count % 1000) == 0) { - DEBUG_TIMING ( - string_compose ("Elapsed process time(usecs) max: %1, min: %2\n", - max_elapsed_us, - min_elapsed_us)); - } + /* clear output buffers */ + for (std::vector::const_iterator it = _system_outputs.begin(); + it != _system_outputs.end(); + ++it) { + memset((*it)->get_buffer(_samples_per_period), + 0, + _samples_per_period * sizeof(Sample)); + } - } else { - // Freewheelin' + _last_cycle_start = _cycle_timer.get_start(); + _cycle_timer.reset_start(PBD::get_microseconds()); + _cycle_count++; - // zero audio input buffers - for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) { - memset ((*it)->get_buffer (_samples_per_period), 0, _samples_per_period * sizeof (Sample)); - } + uint64_t cycle_diff_us = (_cycle_timer.get_start() - _last_cycle_start); + int64_t deviation_us = (cycle_diff_us - _cycle_timer.get_length_us()); + _total_deviation_us += ::llabs(deviation_us); + _max_deviation_us = + std::max(_max_deviation_us, (uint64_t)::llabs(deviation_us)); - clock1 = utils::get_microseconds (); + if ((_cycle_count % 1000) == 0) { + uint64_t mean_deviation_us = _total_deviation_us / _cycle_count; + DEBUG_TIMING(string_compose("Mean avg cycle deviation: %1(ms), max %2(ms)\n", + mean_deviation_us * 1e-3, + _max_deviation_us * 1e-3)); + } - // TODO clear midi or stop midi recv when entering fwheelin' + if (::llabs(deviation_us) > _cycle_timer.get_length_us()) { + DEBUG_TIMING( + string_compose("time between process(ms): %1, Est(ms): %2, Dev(ms): %3\n", + cycle_diff_us * 1e-3, + _cycle_timer.get_length_us() * 1e-3, + deviation_us * 1e-3)); + } - if (engine.process_callback (_samples_per_period)) { - _pcmio->pcm_stop (); - _active = false; - return 0; - } + /* call engine process callback */ + if (engine.process_callback(_samples_per_period)) { + _pcmio->close_stream(); + _active = false; + return false; + } - // drop all outgoing MIDI messages - for (std::vector::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it) { - void *bptr = (*it)->get_buffer(0); - midi_clear(bptr); - } + process_outgoing_midi (); + + /* write back audio */ + i = 0; + for (std::vector::const_iterator it = _system_outputs.begin(); + it != _system_outputs.end(); + ++it, ++i) { + assert(_system_outputs.size() == _pcmio->n_playback_channels()); + const uint32_t channels = _system_outputs.size(); + float* output_port_buffer = (float*)(*it)->get_buffer(_samples_per_period); + interleave_audio_data( + output_port_buffer, interleaved_output_data, _samples_per_period, i, channels); + } + + _processed_samples += _samples_per_period; + + /* calculate DSP load */ + _dsp_calc.set_stop_timestamp_us (PBD::get_microseconds()); + _dsp_load = _dsp_calc.get_dsp_load(); + + DEBUG_TIMING(string_compose("DSP Load: %1\n", _dsp_load)); - _dsp_load = 1.0; - Glib::usleep (100); // don't hog cpu + max_elapsed_us = std::max(_dsp_calc.elapsed_time_us(), max_elapsed_us); + min_elapsed_us = std::min(_dsp_calc.elapsed_time_us(), min_elapsed_us); + if ((_cycle_count % 1000) == 0) { + DEBUG_TIMING(string_compose("Elapsed process time(usecs) max: %1, min: %2\n", + max_elapsed_us, + min_elapsed_us)); + } + + return true; +} + +bool +PortAudioBackend::blocking_process_freewheel() +{ + // zero audio input buffers + for (std::vector::const_iterator it = _system_inputs.begin(); + it != _system_inputs.end(); + ++it) { + memset((*it)->get_buffer(_samples_per_period), + 0, + _samples_per_period * sizeof(Sample)); + } + + // TODO clear midi or stop midi recv when entering fwheelin' + + if (engine.process_callback(_samples_per_period)) { + _pcmio->close_stream(); + _active = false; + return false; + } + + // drop all outgoing MIDI messages + for (std::vector::const_iterator it = _system_midi_out.begin(); + it != _system_midi_out.end(); + ++it) { + void* bptr = (*it)->get_buffer(0); + midi_clear(bptr); + } + + _dsp_load = 1.0; + Glib::usleep(100); // don't hog cpu + return true; +} + +void +PortAudioBackend::process_incoming_midi () +{ + uint32_t i = 0; + for (std::vector::const_iterator it = _system_midi_in.begin(); + it != _system_midi_in.end(); + ++it, ++i) { + PortMidiBuffer* mbuf = static_cast((*it)->get_buffer(0)); + mbuf->clear(); + uint64_t timestamp; + pframes_t sample_offset; + uint8_t data[256]; + size_t size = sizeof(data); + while (_midiio->dequeue_input_event(i, + _cycle_timer.get_start(), + _cycle_timer.get_next_start(), + timestamp, + data, + size)) { + sample_offset = _cycle_timer.samples_since_cycle_start(timestamp); + midi_event_put(mbuf, sample_offset, data, size); + DEBUG_MIDI(string_compose("Dequeuing incoming MIDI data for device: %1 " + "sample_offset: %2 timestamp: %3, size: %4\n", + _midiio->get_inputs()[i]->name(), + sample_offset, + timestamp, + size)); + size = sizeof(data); } + } +} - 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); - _port_connection_queue.pop_back (); - delete c; - } - pthread_mutex_unlock (&_port_callback_mutex); +void +PortAudioBackend::process_outgoing_midi () +{ + /* mixdown midi */ + for (std::vector::iterator it = _system_midi_out.begin(); + it != _system_midi_out.end(); + ++it) { + static_cast(*it)->next_period(); + } + /* queue outgoing midi */ + uint32_t i = 0; + for (std::vector::const_iterator it = _system_midi_out.begin(); + it != _system_midi_out.end(); + ++it, ++i) { + const PortMidiBuffer* src = + static_cast(*it)->const_buffer(); + + for (PortMidiBuffer::const_iterator mit = src->begin(); mit != src->end(); + ++mit) { + uint64_t timestamp = + _cycle_timer.timestamp_from_sample_offset((*mit)->timestamp()); + DEBUG_MIDI(string_compose("Queuing outgoing MIDI data for device: " + "%1 sample_offset: %2 timestamp: %3, size: %4\n", + _midiio->get_outputs()[i]->name(), + (*mit)->timestamp(), + timestamp, + (*mit)->size())); + _midiio->enqueue_output_event(i, timestamp, (*mit)->data(), (*mit)->size()); } - if (ports_changed) { - manager.registration_callback(); + } +} + +void +PortAudioBackend::process_port_connection_changes () +{ + 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 (connections_changed) { - manager.graph_order_callback(); + if (!_port_connection_queue.empty ()) { + connections_changed = true; } - if (connections_changed || ports_changed) { - engine.latency_callback(false); - engine.latency_callback(true); + while (!_port_connection_queue.empty ()) { + PortConnectData *c = _port_connection_queue.back (); + manager.connect_callback (c->a, c->b, c->c); + _port_connection_queue.pop_back (); + delete c; } - + pthread_mutex_unlock (&_port_callback_mutex); } - _pcmio->pcm_stop (); - _active = false; - if (_run) { - engine.halted_callback("PortAudio I/O error."); + 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); } - -#ifdef USE_MMCSS_THREAD_PRIORITIES - mmcss::revert_thread_characteristics (task_handle); -#endif - - return 0; } - /******************************************************************************/ static boost::shared_ptr _instance; @@ -1609,7 +2133,7 @@ static bool already_configured (); static bool available (); static ARDOUR::AudioBackendInfo _descriptor = { - "PortAudio", + BACKEND_NAME, instantiate, deinstantiate, backend_factory, @@ -1678,27 +2202,27 @@ PamPort::~PamPort () { int PamPort::connect (PamPort *port) { if (!port) { - PBD::error << _("PamPort::connect (): invalid (null) port") << endmsg; + DEBUG_PORTS("PamPort::connect (): invalid (null) port\n"); return -1; } if (type () != port->type ()) { - PBD::error << _("PamPort::connect (): wrong port-type") << endmsg; + DEBUG_PORTS("PamPort::connect (): wrong port-type\n"); return -1; } if (is_output () && port->is_output ()) { - PBD::error << _("PamPort::connect (): cannot inter-connect output ports.") << endmsg; + DEBUG_PORTS("PamPort::connect (): cannot inter-connect output ports.\n"); return -1; } if (is_input () && port->is_input ()) { - PBD::error << _("PamPort::connect (): cannot inter-connect input ports.") << endmsg; + DEBUG_PORTS("PamPort::connect (): cannot inter-connect input ports.\n"); return -1; } if (this == port) { - PBD::error << _("PamPort::connect (): cannot self-connect ports.") << endmsg; + DEBUG_PORTS("PamPort::connect (): cannot self-connect ports.\n"); return -1; } @@ -1728,14 +2252,15 @@ void PamPort::_connect (PamPort *port, bool callback) int PamPort::disconnect (PamPort *port) { if (!port) { - PBD::error << _("PamPort::disconnect (): invalid (null) port") << endmsg; + DEBUG_PORTS("PamPort::disconnect (): invalid (null) port\n"); return -1; } if (!is_connected (port)) { - PBD::error << _("PamPort::disconnect (): ports are not connected:") - << " (" << name () << ") -> (" << port->name () << ")" - << endmsg; + DEBUG_PORTS(string_compose( + "PamPort::disconnect (): ports are not connected: (%1) -> (%2)\n", + name(), + port->name())); return -1; } _disconnect (port, true);