X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fbackends%2Fportaudio%2Fportaudio_backend.cc;h=b7afb4c48d70623b403b6eb2765b94702e0fc722;hb=2991afaf0da78f860854f59a7d6c4e64fb519d23;hp=9248c2d32c9dcbcd18aca5b00921bba86c2f56e3;hpb=cf81caa7982f8cd541c8aba2b81ead216879c1b2;p=ardour.git diff --git a/libs/backends/portaudio/portaudio_backend.cc b/libs/backends/portaudio/portaudio_backend.cc index 9248c2d32c..b7afb4c48d 100644 --- a/libs/backends/portaudio/portaudio_backend.cc +++ b/libs/backends/portaudio/portaudio_backend.cc @@ -24,21 +24,26 @@ #include #endif +#ifdef COMPILER_MINGW +#include +#endif + #include #include "portaudio_backend.h" -#include "rt_thread.h" #include "pbd/compose.h" #include "pbd/error.h" #include "pbd/file_utils.h" +#include "pbd/pthread_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,10 @@ PortAudioBackend::PortAudioBackend (AudioEngine& e, AudioBackendInfo& info) { _instance_name = s_instance_name; pthread_mutex_init (&_port_callback_mutex, 0); + pthread_mutex_init (&_freewheel_mutex, 0); + pthread_cond_init (&_freewheel_signal, 0); - mmcss::initialize (); + _port_connection_queue.reserve (128); _pcmio = new PortAudioIO (); _midiio = new WinMMEMidiIO (); @@ -93,9 +104,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 */ @@ -148,9 +159,22 @@ PortAudioBackend::set_driver (const std::string& name) 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 { @@ -321,6 +345,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 @@ -382,6 +424,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 { @@ -423,27 +481,93 @@ 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_blocking_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) { - DEBUG_AUDIO("Already active.\n"); - return -1; + if (running()) { + DEBUG_AUDIO("Already started.\n"); + return BackendReinitializationError; } if (_ports.size()) { @@ -457,27 +581,42 @@ PortAudioBackend::_start (bool for_latency_measurement) } /* reset internal state */ + assert (_run == false); + _run = false; _dsp_load = 0; _freewheeling = false; _freewheel = false; - PortAudioIO::ErrorCode err; - - err = _pcmio->open_blocking_stream(name_to_id(_input_audio_device), - name_to_id(_output_audio_device), - _samplerate, - _samples_per_period); - + 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 PortAudioIO::NoError: + case paNoError: break; - case PortAudioIO::DeviceConfigNotSupportedError: - PBD::error << get_error_string(DeviceConfigurationNotSupportedError) - << endmsg; - return -1; + case paBadIODeviceCombination: + return DeviceConfigurationNotSupportedError; + case paInvalidChannelCount: + return ChannelCountNotSupportedError; + case paInvalidSampleRate: + return SampleRateNotSupportedError; default: - PBD::error << get_error_string(AudioDeviceOpenError) << endmsg; - return -1; + return AudioDeviceOpenError; } if (_n_outputs != _pcmio->n_playback_channels ()) { @@ -504,7 +643,6 @@ PortAudioBackend::_start (bool for_latency_measurement) _measure_latency = for_latency_measurement; - _run = true; _port_change_flag = false; if (_midi_driver_option == winmme_driver_name) { @@ -513,23 +651,23 @@ 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) { 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()) { DEBUG_PORTS("Failed to register system audio ports.\n"); - _run = false; - return -1; + return PortRegistrationError; } engine.sample_rate_change (_samplerate); @@ -537,28 +675,122 @@ PortAudioBackend::_start (bool for_latency_measurement) if (engine.reestablish_ports ()) { DEBUG_PORTS("Could not re-establish ports.\n"); - _run = false; - return -1; + return PortReconnectError; } - engine.reconnect_ports (); _run = true; + + engine.reconnect_ports (); _port_change_flag = false; - if (!start_blocking_process_thread()) { - return -1; + 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; + } + + /* wait for backend to become active */ + int timeout = 5000; + while (!_active && --timeout > 0) { Glib::usleep (1000); } + + if (timeout == 0 || !_active) { + PBD::error << _("PortAudio:: failed to start device.") << endmsg; + stop (); + return ProcessThreadStartError; + } } - return 0; + return NoError; +} + +int +PortAudioBackend::portaudio_callback(const void* input, + void* output, + unsigned long sample_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, + sample_count, + time_info, + status_flags)) { + return paAbort; + } + + return paContinue; +} + +bool +PortAudioBackend::process_callback(const float* input, + float* output, + uint32_t sample_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, sample_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_blocking_thread, pthread_process, this)) + if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -20, 100000, + &_main_blocking_thread, blocking_thread_func, this)) { - if (pthread_create (&_main_blocking_thread, NULL, pthread_process, this)) + if (pthread_create (&_main_blocking_thread, NULL, blocking_thread_func, this)) { DEBUG_AUDIO("Failed to create main audio thread\n"); _run = false; @@ -606,8 +838,17 @@ PortAudioBackend::stop () _run = false; - if (!stop_blocking_process_thread ()) { - 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(); @@ -615,6 +856,120 @@ PortAudioBackend::stop () 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) { @@ -622,6 +977,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; } @@ -644,13 +1004,13 @@ PortAudioBackend::raw_buffer_size (DataType t) } /* Process time */ -framepos_t +samplepos_t PortAudioBackend::sample_time () { return _processed_samples; } -framepos_t +samplepos_t PortAudioBackend::sample_time_at_cycle_start () { return _processed_samples; @@ -662,11 +1022,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 @@ -685,6 +1045,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) { @@ -694,12 +1089,7 @@ PortAudioBackend::portaudio_process_thread (void *arg) #ifdef USE_MMCSS_THREAD_PRIORITIES HANDLE task_handle; - - mmcss::set_thread_characteristics ("Pro Audio", &task_handle); - if (!mmcss::set_thread_priority(task_handle, mmcss::AVRT_PRIORITY_NORMAL)) { - PBD::warning << get_error_string(SettingAudioThreadPriorityError) - << endmsg; - } + bool mmcss_success = set_mmcss_pro_audio (&task_handle); #endif DWORD tid = GetCurrentThreadId (); @@ -708,7 +1098,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; @@ -723,7 +1115,7 @@ PortAudioBackend::create_process_thread (boost::function func) ThreadData* td = new ThreadData (this, func, stacksize); - if (_realtime_pthread_create (SCHED_FIFO, -21, stacksize, + if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, -22, stacksize, &thread_id, portaudio_process_thread, td)) { pthread_attr_init (&attr); pthread_attr_setstacksize (&attr, stacksize); @@ -759,10 +1151,15 @@ PortAudioBackend::join_process_threads () bool PortAudioBackend::in_process_thread () { - if (pthread_equal (_main_blocking_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) { @@ -831,6 +1228,16 @@ PortAudioBackend::get_port_name (PortEngine::PortHandle port) const return static_cast(port)->name (); } +PortFlags +PortAudioBackend::get_port_flags (PortEngine::PortHandle port) const +{ + if (!valid_port (port)) { + DEBUG_PORTS("get_port_flags: Invalid Port(s)\n"); + return PortFlags (0); + } + return static_cast(port)->flags (); +} + int PortAudioBackend::get_port_property (PortHandle port, const std::string& key, @@ -852,6 +1259,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 { @@ -963,12 +1388,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); @@ -981,7 +1413,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); @@ -1012,13 +1444,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); @@ -1030,13 +1468,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()); @@ -1066,6 +1510,24 @@ PortAudioBackend::unregister_ports (bool system_only) } } +void +PortAudioBackend::update_system_port_latecies () +{ + for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) { + (*it)->update_connected_latency (true); + } + for (std::vector::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it) { + (*it)->update_connected_latency (false); + } + + for (std::vector::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it) { + (*it)->update_connected_latency (true); + } + for (std::vector::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it) { + (*it)->update_connected_latency (false); + } +} + int PortAudioBackend::connect (const std::string& src, const std::string& dst) { @@ -1187,7 +1649,7 @@ PortAudioBackend::get_connections (PortEngine::PortHandle port, std::vector= source.size ()) { return -1; } - PortMidiEvent * const event = source[event_index].get (); + PortMidiEvent const& event = source[event_index]; - timestamp = event->timestamp (); - size = event->size (); - *buf = event->data (); + timestamp = event.timestamp (); + size = event.size (); + *buf = event.data (); return 0; } @@ -1211,13 +1673,15 @@ PortAudioBackend::midi_event_put ( { if (!buffer || !port_buffer) return -1; PortMidiBuffer& dst = * static_cast(port_buffer); - if (dst.size () && (pframes_t)dst.back ()->timestamp () > timestamp) { +#ifndef NDEBUG + if (dst.size () && (pframes_t)dst.back ().timestamp () > timestamp) { // nevermind, ::get_buffer() sorts events DEBUG_MIDI (string_compose ("PortMidiBuffer: unordered event: %1 > %2\n", - (pframes_t)dst.back ()->timestamp (), + (pframes_t)dst.back ().timestamp (), timestamp)); } - dst.push_back (boost::shared_ptr(new PortMidiEvent (timestamp, buffer, size))); +#endif + dst.push_back (PortMidiEvent (timestamp, buffer, size)); return 0; } @@ -1393,28 +1857,24 @@ PortAudioBackend::n_physical_inputs () const void* PortAudioBackend::get_buffer (PortEngine::PortHandle port, pframes_t nframes) { - if (!port || !valid_port (port)) return NULL; + assert (port); + assert (valid_port (port)); + if (!port || !valid_port (port)) return NULL; // XXX remove me return static_cast(port)->get_buffer (nframes); } void * -PortAudioBackend::main_blocking_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->start_stream() != PortAudioIO::NoError) { + if (_pcmio->start_stream() != paNoError) { _pcmio->close_stream (); _active = false; engine.halted_callback(get_error_string(AudioDeviceIOError).c_str()); @@ -1422,12 +1882,7 @@ PortAudioBackend::main_blocking_process_thread () #ifdef USE_MMCSS_THREAD_PRIORITIES HANDLE task_handle; - - mmcss::set_thread_characteristics ("Pro Audio", &task_handle); - if (!mmcss::set_thread_priority(task_handle, mmcss::AVRT_PRIORITY_NORMAL)) { - PBD::warning << get_error_string(SettingAudioThreadPriorityError) - << endmsg; - } + bool mmcss_success = set_mmcss_pro_audio (&task_handle); #endif DWORD tid = GetCurrentThreadId (); @@ -1454,164 +1909,227 @@ PortAudioBackend::main_blocking_process_thread () 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->close_stream (); - _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)); - // TODO clear midi or stop midi recv when entering fwheelin' + 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)); + } - if (engine.process_callback (_samples_per_period)) { - _pcmio->close_stream(); - _active = false; - return 0; - } + 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)); + } - // 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); - } + /* call engine process callback */ + if (engine.process_callback(_samples_per_period)) { + _pcmio->close_stream(); + _active = false; + return false; + } - _dsp_load = 1.0; - Glib::usleep (100); // don't hog cpu - } + process_outgoing_midi (); - process_port_connection_changes(); + /* 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); } - _pcmio->close_stream(); - _active = false; - if (_run) { - engine.halted_callback(get_error_string(AudioDeviceIOError).c_str()); + + _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)); + + 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)); } -#ifdef USE_MMCSS_THREAD_PRIORITIES - mmcss::revert_thread_characteristics (task_handle); -#endif + return true; +} - return 0; +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[MaxWinMidiEventSize]; + 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); + } + } +} + +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()); + } + } } void @@ -1642,6 +2160,7 @@ PortAudioBackend::process_port_connection_changes () manager.graph_order_callback(); } if (connections_changed || ports_changed) { + update_system_port_latecies (); engine.latency_callback(false); engine.latency_callback(true); } @@ -1658,7 +2177,7 @@ static bool already_configured (); static bool available (); static ARDOUR::AudioBackendInfo _descriptor = { - "PortAudio", + BACKEND_NAME, instantiate, deinstantiate, backend_factory, @@ -1832,6 +2351,36 @@ bool PamPort::is_physically_connected () const return false; } +void +PamPort::set_latency_range (const LatencyRange &latency_range, bool for_playback) +{ + if (for_playback) { + _playback_latency_range = latency_range; + } else { + _capture_latency_range = latency_range; + } + + for (std::vector::const_iterator it = _connections.begin (); it != _connections.end (); ++it) { + if ((*it)->is_physical ()) { + (*it)->update_connected_latency (is_input ()); + } + } +} + +void +PamPort::update_connected_latency (bool for_playback) +{ + LatencyRange lr; + lr.min = lr.max = 0; + for (std::vector::const_iterator it = _connections.begin (); it != _connections.end (); ++it) { + LatencyRange l; + l = (*it)->latency_range (for_playback); + lr.min = std::max (lr.min, l.min); + lr.max = std::max (lr.max, l.max); + } + set_latency_range (lr, for_playback); +} + /******************************************************************************/ PortAudioPort::PortAudioPort (PortAudioBackend &b, const std::string& name, PortFlags flags) @@ -1877,13 +2426,16 @@ PortMidiPort::PortMidiPort (PortAudioBackend &b, const std::string& name, PortFl { _buffer[0].clear (); _buffer[1].clear (); + + _buffer[0].reserve (256); + _buffer[1].reserve (256); } PortMidiPort::~PortMidiPort () { } struct MidiEventSorter { - bool operator() (const boost::shared_ptr& a, const boost::shared_ptr& b) { - return *a < *b; + bool operator() (PortMidiEvent const& a, PortMidiEvent const& b) { + return a < b; } }; @@ -1896,10 +2448,10 @@ void* PortMidiPort::get_buffer (pframes_t /* nframes */) ++i) { const PortMidiBuffer * src = static_cast(*i)->const_buffer (); for (PortMidiBuffer::const_iterator it = src->begin (); it != src->end (); ++it) { - (_buffer[_bufperiod]).push_back (boost::shared_ptr(new PortMidiEvent (**it))); + (_buffer[_bufperiod]).push_back (*it); } } - std::sort ((_buffer[_bufperiod]).begin (), (_buffer[_bufperiod]).end (), MidiEventSorter()); + std::stable_sort ((_buffer[_bufperiod]).begin (), (_buffer[_bufperiod]).end (), MidiEventSorter()); } return &(_buffer[_bufperiod]); } @@ -1907,10 +2459,8 @@ void* PortMidiPort::get_buffer (pframes_t /* nframes */) PortMidiEvent::PortMidiEvent (const pframes_t timestamp, const uint8_t* data, size_t size) : _size (size) , _timestamp (timestamp) - , _data (0) { - if (size > 0) { - _data = (uint8_t*) malloc (size); + if (size > 0 && size < MaxWinMidiEventSize) { memcpy (_data, data, size); } } @@ -1918,14 +2468,9 @@ PortMidiEvent::PortMidiEvent (const pframes_t timestamp, const uint8_t* data, si PortMidiEvent::PortMidiEvent (const PortMidiEvent& other) : _size (other.size ()) , _timestamp (other.timestamp ()) - , _data (0) { - if (other.size () && other.const_data ()) { - _data = (uint8_t*) malloc (other.size ()); - memcpy (_data, other.const_data (), other.size ()); + if (other._size > 0) { + assert (other._size < MaxWinMidiEventSize); + memcpy (_data, other._data, other._size); } }; - -PortMidiEvent::~PortMidiEvent () { - free (_data); -};