2014-06-02 08:29:19 -04:00
|
|
|
/*
|
2019-08-02 22:11:41 -04:00
|
|
|
* Copyright (C) 2014-2015 Tim Mayberry <mojofunk@gmail.com>
|
|
|
|
* Copyright (C) 2014-2018 Paul Davis <paul@linuxaudiosystems.com>
|
2021-05-15 18:08:03 -04:00
|
|
|
* Copyright (C) 2014-2021 Robin Gareus <robin@gareus.org>
|
2014-06-02 08:29:19 -04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
2019-08-02 22:11:41 -04:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2014-06-02 08:29:19 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <regex.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include <glibmm.h>
|
|
|
|
|
2017-09-18 10:13:47 -04:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/tokenizer.hpp>
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
#include "alsa_audiobackend.h"
|
|
|
|
|
2014-06-02 10:52:07 -04:00
|
|
|
#include "pbd/compose.h"
|
2017-09-18 10:13:47 -04:00
|
|
|
#include "pbd/convert.h"
|
2014-06-02 08:29:19 -04:00
|
|
|
#include "pbd/error.h"
|
2014-06-04 11:09:55 -04:00
|
|
|
#include "pbd/file_utils.h"
|
2017-08-29 11:23:19 -04:00
|
|
|
#include "pbd/pthread_utils.h"
|
2017-09-18 10:13:47 -04:00
|
|
|
|
2022-06-24 13:24:48 -04:00
|
|
|
#include "ardour/debug.h"
|
2014-06-04 20:08:56 -04:00
|
|
|
#include "ardour/filesystem_paths.h"
|
2014-06-02 08:29:19 -04:00
|
|
|
#include "ardour/port_manager.h"
|
2014-06-04 13:16:37 -04:00
|
|
|
#include "ardouralsautil/devicelist.h"
|
2016-07-14 14:44:52 -04:00
|
|
|
#include "pbd/i18n.h"
|
2014-06-02 08:29:19 -04:00
|
|
|
|
|
|
|
using namespace ARDOUR;
|
|
|
|
|
|
|
|
static std::string s_instance_name;
|
2021-05-15 18:08:03 -04:00
|
|
|
|
|
|
|
size_t AlsaAudioBackend::_max_buffer_size = 8192;
|
|
|
|
std::vector<std::string> AlsaAudioBackend::_midi_options;
|
2015-06-14 16:24:53 -04:00
|
|
|
std::vector<AudioBackend::DeviceStatus> AlsaAudioBackend::_input_audio_device_status;
|
|
|
|
std::vector<AudioBackend::DeviceStatus> AlsaAudioBackend::_output_audio_device_status;
|
|
|
|
std::vector<AudioBackend::DeviceStatus> AlsaAudioBackend::_duplex_audio_device_status;
|
2014-06-23 06:16:35 -04:00
|
|
|
std::vector<AudioBackend::DeviceStatus> AlsaAudioBackend::_midi_device_status;
|
2014-06-02 08:29:19 -04:00
|
|
|
|
2015-08-11 13:01:52 -04:00
|
|
|
ALSADeviceInfo AlsaAudioBackend::_input_audio_device_info;
|
|
|
|
ALSADeviceInfo AlsaAudioBackend::_output_audio_device_info;
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
AlsaAudioBackend::AlsaAudioBackend (AudioEngine& e, AudioBackendInfo& info)
|
|
|
|
: AudioBackend (e, info)
|
2020-04-02 16:26:44 -04:00
|
|
|
, PortEngineSharedImpl (e, s_instance_name)
|
2014-06-02 08:29:19 -04:00
|
|
|
, _pcmi (0)
|
2014-06-03 20:39:39 -04:00
|
|
|
, _run (false)
|
|
|
|
, _active (false)
|
2015-01-24 17:39:34 -05:00
|
|
|
, _freewheel (false)
|
2014-06-02 08:29:19 -04:00
|
|
|
, _freewheeling (false)
|
2014-06-12 15:35:25 -04:00
|
|
|
, _measure_latency (false)
|
2015-03-04 19:33:13 -05:00
|
|
|
, _last_process_start (0)
|
2021-05-15 18:08:03 -04:00
|
|
|
, _input_audio_device ("")
|
|
|
|
, _output_audio_device ("")
|
2022-07-03 16:10:31 -04:00
|
|
|
, _midi_driver_option (_("ALSA sequencer"))
|
2014-06-02 08:29:19 -04:00
|
|
|
, _samplerate (48000)
|
|
|
|
, _samples_per_period (1024)
|
|
|
|
, _periods_per_cycle (2)
|
|
|
|
, _n_inputs (0)
|
|
|
|
, _n_outputs (0)
|
2014-06-06 18:13:32 -04:00
|
|
|
, _systemic_audio_input_latency (0)
|
|
|
|
, _systemic_audio_output_latency (0)
|
2018-12-23 15:48:07 -05:00
|
|
|
, _midi_device_thread_active (false)
|
2014-06-12 15:35:25 -04:00
|
|
|
, _dsp_load (0)
|
2014-06-02 08:29:19 -04:00
|
|
|
, _processed_samples (0)
|
|
|
|
{
|
|
|
|
_instance_name = s_instance_name;
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_init (&_device_port_mutex, 0);
|
2021-05-15 18:08:03 -04:00
|
|
|
_input_audio_device_info.valid = false;
|
2015-08-11 13:01:52 -04:00
|
|
|
_output_audio_device_info.valid = false;
|
2017-08-05 09:03:06 -04:00
|
|
|
|
|
|
|
_port_connection_queue.reserve (128);
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
AlsaAudioBackend::~AlsaAudioBackend ()
|
|
|
|
{
|
2020-04-08 01:26:51 -04:00
|
|
|
clear_ports ();
|
|
|
|
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_destroy (&_device_port_mutex);
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* AUDIOBACKEND API */
|
|
|
|
|
|
|
|
std::string
|
|
|
|
AlsaAudioBackend::name () const
|
|
|
|
{
|
|
|
|
return X_("ALSA");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AlsaAudioBackend::is_realtime () const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<AudioBackend::DeviceStatus>
|
|
|
|
AlsaAudioBackend::enumerate_devices () const
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
_duplex_audio_device_status.clear ();
|
2014-06-04 13:16:37 -04:00
|
|
|
std::map<std::string, std::string> devices;
|
2021-05-15 18:08:03 -04:00
|
|
|
get_alsa_audio_device_names (devices);
|
|
|
|
for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end (); ++i) {
|
|
|
|
if (_input_audio_device == "") {
|
|
|
|
_input_audio_device = i->first;
|
|
|
|
}
|
|
|
|
if (_output_audio_device == "") {
|
|
|
|
_output_audio_device = i->first;
|
|
|
|
}
|
2015-06-14 16:24:53 -04:00
|
|
|
_duplex_audio_device_status.push_back (DeviceStatus (i->first, true));
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
2015-06-14 16:24:53 -04:00
|
|
|
return _duplex_audio_device_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<AudioBackend::DeviceStatus>
|
|
|
|
AlsaAudioBackend::enumerate_input_devices () const
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
_input_audio_device_status.clear ();
|
2015-06-14 16:24:53 -04:00
|
|
|
std::map<std::string, std::string> devices;
|
2021-05-15 18:08:03 -04:00
|
|
|
get_alsa_audio_device_names (devices, HalfDuplexIn);
|
|
|
|
_input_audio_device_status.push_back (DeviceStatus (get_standard_device_name (DeviceNone), true));
|
|
|
|
for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end (); ++i) {
|
|
|
|
if (_input_audio_device == "") {
|
|
|
|
_input_audio_device = i->first;
|
|
|
|
}
|
2015-06-14 16:24:53 -04:00
|
|
|
_input_audio_device_status.push_back (DeviceStatus (i->first, true));
|
|
|
|
}
|
|
|
|
return _input_audio_device_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<AudioBackend::DeviceStatus>
|
|
|
|
AlsaAudioBackend::enumerate_output_devices () const
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
_output_audio_device_status.clear ();
|
2015-06-14 16:24:53 -04:00
|
|
|
std::map<std::string, std::string> devices;
|
2021-05-15 18:08:03 -04:00
|
|
|
get_alsa_audio_device_names (devices, HalfDuplexOut);
|
|
|
|
_output_audio_device_status.push_back (DeviceStatus (get_standard_device_name (DeviceNone), true));
|
|
|
|
for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end (); ++i) {
|
|
|
|
if (_output_audio_device == "") {
|
|
|
|
_output_audio_device = i->first;
|
|
|
|
}
|
2015-06-14 16:24:53 -04:00
|
|
|
_output_audio_device_status.push_back (DeviceStatus (i->first, true));
|
|
|
|
}
|
|
|
|
return _output_audio_device_status;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<float>
|
2015-08-11 17:43:45 -04:00
|
|
|
AlsaAudioBackend::available_sample_rates2 (const std::string& input_device, const std::string& output_device) const
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
|
|
|
std::vector<float> sr;
|
2021-05-15 18:08:03 -04:00
|
|
|
if (input_device == get_standard_device_name (DeviceNone) && output_device == get_standard_device_name (DeviceNone)) {
|
2015-08-11 13:01:52 -04:00
|
|
|
return sr;
|
2021-05-15 18:08:03 -04:00
|
|
|
} else if (input_device == get_standard_device_name (DeviceNone)) {
|
2015-08-11 13:01:52 -04:00
|
|
|
sr = available_sample_rates (output_device);
|
2021-05-15 18:08:03 -04:00
|
|
|
} else if (output_device == get_standard_device_name (DeviceNone)) {
|
2015-08-11 13:01:52 -04:00
|
|
|
sr = available_sample_rates (input_device);
|
|
|
|
} else {
|
2021-05-15 18:08:03 -04:00
|
|
|
std::vector<float> sr_in = available_sample_rates (input_device);
|
2015-08-11 13:01:52 -04:00
|
|
|
std::vector<float> sr_out = available_sample_rates (output_device);
|
2021-05-15 18:08:03 -04:00
|
|
|
std::set_intersection (sr_in.begin (), sr_in.end (), sr_out.begin (), sr_out.end (), std::back_inserter (sr));
|
2015-08-11 13:01:52 -04:00
|
|
|
}
|
|
|
|
return sr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<float>
|
|
|
|
AlsaAudioBackend::available_sample_rates (const std::string& device) const
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
ALSADeviceInfo* nfo = NULL;
|
2015-08-11 13:01:52 -04:00
|
|
|
std::vector<float> sr;
|
2021-05-15 18:08:03 -04:00
|
|
|
if (device == get_standard_device_name (DeviceNone)) {
|
2015-08-11 13:01:52 -04:00
|
|
|
return sr;
|
|
|
|
}
|
|
|
|
if (device == _input_audio_device && _input_audio_device_info.valid) {
|
|
|
|
nfo = &_input_audio_device_info;
|
2021-05-15 18:08:03 -04:00
|
|
|
} else if (device == _output_audio_device && _output_audio_device_info.valid) {
|
2015-08-11 13:01:52 -04:00
|
|
|
nfo = &_output_audio_device_info;
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
static const float avail_rates[] = { 8000, 22050.0, 24000.0, 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0 };
|
2015-08-11 13:01:52 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
for (size_t i = 0; i < sizeof (avail_rates) / sizeof (float); ++i) {
|
2015-08-11 13:01:52 -04:00
|
|
|
if (!nfo || (avail_rates[i] >= nfo->min_rate && avail_rates[i] <= nfo->max_rate)) {
|
|
|
|
sr.push_back (avail_rates[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
return sr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint32_t>
|
2015-08-11 17:43:45 -04:00
|
|
|
AlsaAudioBackend::available_buffer_sizes2 (const std::string& input_device, const std::string& output_device) const
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
|
|
|
std::vector<uint32_t> bs;
|
2021-05-15 18:08:03 -04:00
|
|
|
if (input_device == get_standard_device_name (DeviceNone) && output_device == get_standard_device_name (DeviceNone)) {
|
2015-08-11 13:01:52 -04:00
|
|
|
return bs;
|
2021-05-15 18:08:03 -04:00
|
|
|
} else if (input_device == get_standard_device_name (DeviceNone)) {
|
2015-08-11 13:01:52 -04:00
|
|
|
bs = available_buffer_sizes (output_device);
|
2021-05-15 18:08:03 -04:00
|
|
|
} else if (output_device == get_standard_device_name (DeviceNone)) {
|
2015-08-11 13:01:52 -04:00
|
|
|
bs = available_buffer_sizes (input_device);
|
|
|
|
} else {
|
2021-05-15 18:08:03 -04:00
|
|
|
std::vector<uint32_t> bs_in = available_buffer_sizes (input_device);
|
2015-08-11 13:01:52 -04:00
|
|
|
std::vector<uint32_t> bs_out = available_buffer_sizes (output_device);
|
2021-05-15 18:08:03 -04:00
|
|
|
std::set_intersection (bs_in.begin (), bs_in.end (), bs_out.begin (), bs_out.end (), std::back_inserter (bs));
|
2015-08-11 13:01:52 -04:00
|
|
|
}
|
|
|
|
return bs;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint32_t>
|
|
|
|
AlsaAudioBackend::available_buffer_sizes (const std::string& device) const
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
ALSADeviceInfo* nfo = NULL;
|
2015-08-11 13:01:52 -04:00
|
|
|
std::vector<uint32_t> bs;
|
2021-05-15 18:08:03 -04:00
|
|
|
if (device == get_standard_device_name (DeviceNone)) {
|
2015-08-11 13:01:52 -04:00
|
|
|
return bs;
|
|
|
|
}
|
|
|
|
if (device == _input_audio_device && _input_audio_device_info.valid) {
|
|
|
|
nfo = &_input_audio_device_info;
|
2021-05-15 18:08:03 -04:00
|
|
|
} else if (device == _output_audio_device && _output_audio_device_info.valid) {
|
2015-08-11 13:01:52 -04:00
|
|
|
nfo = &_output_audio_device_info;
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
static const unsigned long avail_sizes[] = { 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192 };
|
2015-08-11 13:01:52 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
for (size_t i = 0; i < sizeof (avail_sizes) / sizeof (unsigned long); ++i) {
|
2015-08-11 13:01:52 -04:00
|
|
|
if (!nfo || (avail_sizes[i] >= nfo->min_size && avail_sizes[i] <= nfo->max_size)) {
|
|
|
|
bs.push_back (avail_sizes[i]);
|
|
|
|
}
|
|
|
|
}
|
2021-01-22 10:06:18 -05:00
|
|
|
|
|
|
|
if (!nfo) {
|
|
|
|
return bs;
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
static const unsigned long try_msec[] = { 2, 4, 5, 6, 8, 10, 15, 20, 25, 40 };
|
2021-01-22 10:06:18 -05:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
for (size_t i = 0; i < sizeof (try_msec) / sizeof (unsigned long); ++i) {
|
2021-01-22 10:06:18 -05:00
|
|
|
unsigned int msbs = _samplerate * try_msec[i] / 1000;
|
|
|
|
if (msbs >= nfo->min_size && msbs <= nfo->max_size) {
|
|
|
|
bs.push_back (msbs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::sort (bs.begin (), bs.end ());
|
2014-06-02 08:29:19 -04:00
|
|
|
return bs;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2015-08-11 13:01:52 -04:00
|
|
|
AlsaAudioBackend::available_input_channel_count (const std::string& device) const
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
if (device == get_standard_device_name (DeviceNone)) {
|
2015-08-11 13:01:52 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (device == _input_audio_device && _input_audio_device_info.valid) {
|
|
|
|
return _input_audio_device_info.max_channels;
|
|
|
|
}
|
|
|
|
return 128;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
2015-08-11 13:01:52 -04:00
|
|
|
AlsaAudioBackend::available_output_channel_count (const std::string& device) const
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
if (device == get_standard_device_name (DeviceNone)) {
|
2015-08-11 13:01:52 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (device == _output_audio_device && _output_audio_device_info.valid) {
|
|
|
|
return _output_audio_device_info.max_channels;
|
|
|
|
}
|
|
|
|
return 128;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2015-12-04 04:15:55 -05:00
|
|
|
std::vector<uint32_t>
|
2020-04-28 11:56:17 -04:00
|
|
|
AlsaAudioBackend::available_period_sizes (const std::string& driver, const std::string& device) const
|
2015-12-04 04:15:55 -05:00
|
|
|
{
|
|
|
|
std::vector<uint32_t> ps;
|
2020-04-29 17:55:51 -04:00
|
|
|
ps.push_back (2);
|
2020-04-27 21:28:34 -04:00
|
|
|
|
2020-04-28 11:56:17 -04:00
|
|
|
ALSADeviceInfo* nfo = NULL;
|
2021-05-15 18:08:03 -04:00
|
|
|
if (device == get_standard_device_name (DeviceNone)) {
|
2020-04-27 21:28:34 -04:00
|
|
|
return ps;
|
|
|
|
}
|
2020-04-29 17:55:51 -04:00
|
|
|
|
2020-04-27 21:28:34 -04:00
|
|
|
if (device == _output_audio_device && _output_audio_device_info.valid) {
|
|
|
|
nfo = &_output_audio_device_info;
|
2020-04-29 17:55:51 -04:00
|
|
|
if (nfo->max_nper > 2) {
|
2020-04-27 21:28:34 -04:00
|
|
|
ps.push_back (3);
|
|
|
|
}
|
2020-05-10 16:04:23 -04:00
|
|
|
if (nfo->min_nper > 3) {
|
2020-04-29 17:55:51 -04:00
|
|
|
ps.push_back (nfo->min_nper);
|
|
|
|
}
|
2020-04-27 21:28:34 -04:00
|
|
|
} else {
|
2020-04-29 17:55:51 -04:00
|
|
|
ps.push_back (3);
|
2020-04-27 21:28:34 -04:00
|
|
|
}
|
|
|
|
|
2015-12-04 04:15:55 -05:00
|
|
|
return ps;
|
|
|
|
}
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
bool
|
|
|
|
AlsaAudioBackend::can_change_sample_rate_when_running () const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AlsaAudioBackend::can_change_buffer_size_when_running () const
|
|
|
|
{
|
2015-03-07 03:11:15 -05:00
|
|
|
return false; // why not? :)
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-06-14 16:24:53 -04:00
|
|
|
AlsaAudioBackend::set_input_device_name (const std::string& d)
|
|
|
|
{
|
2021-06-29 22:10:25 -04:00
|
|
|
if (_input_audio_device == d && _input_audio_device_info.valid) {
|
2015-08-11 13:01:52 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2015-06-14 16:24:53 -04:00
|
|
|
_input_audio_device = d;
|
2015-08-11 13:01:52 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
if (d == get_standard_device_name (DeviceNone)) {
|
2015-08-11 13:01:52 -04:00
|
|
|
_input_audio_device_info.valid = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
std::string alsa_device;
|
|
|
|
std::map<std::string, std::string> devices;
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
get_alsa_audio_device_names (devices, HalfDuplexIn);
|
|
|
|
for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end (); ++i) {
|
2015-08-11 13:01:52 -04:00
|
|
|
if (i->first == d) {
|
|
|
|
alsa_device = i->second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (alsa_device == "") {
|
|
|
|
_input_audio_device_info.valid = false;
|
|
|
|
return 1;
|
|
|
|
}
|
2022-11-29 11:34:45 -05:00
|
|
|
AlsaDeviceReservation adr (alsa_device.c_str ());
|
2015-08-11 13:01:52 -04:00
|
|
|
/* device will be busy once used, hence cache the parameters */
|
2021-05-15 18:08:03 -04:00
|
|
|
/* return */ get_alsa_device_parameters (alsa_device.c_str (), false, &_input_audio_device_info);
|
2015-06-14 16:24:53 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_output_device_name (const std::string& d)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
2021-06-29 22:10:25 -04:00
|
|
|
if (_output_audio_device == d && _output_audio_device_info.valid) {
|
2015-08-11 13:01:52 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-14 16:24:53 -04:00
|
|
|
_output_audio_device = d;
|
2015-08-11 13:01:52 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
if (d == get_standard_device_name (DeviceNone)) {
|
2015-08-11 13:01:52 -04:00
|
|
|
_output_audio_device_info.valid = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
std::string alsa_device;
|
|
|
|
std::map<std::string, std::string> devices;
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
get_alsa_audio_device_names (devices, HalfDuplexOut);
|
|
|
|
for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end (); ++i) {
|
2015-08-11 13:01:52 -04:00
|
|
|
if (i->first == d) {
|
|
|
|
alsa_device = i->second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (alsa_device == "") {
|
|
|
|
_output_audio_device_info.valid = false;
|
|
|
|
return 1;
|
|
|
|
}
|
2022-11-29 11:34:45 -05:00
|
|
|
AlsaDeviceReservation adr (alsa_device.c_str ());
|
2021-05-15 18:08:03 -04:00
|
|
|
/* return */ get_alsa_device_parameters (alsa_device.c_str (), true, &_output_audio_device_info);
|
2014-06-02 08:29:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-14 16:24:53 -04:00
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_device_name (const std::string& d)
|
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
rv |= set_input_device_name (d);
|
|
|
|
rv |= set_output_device_name (d);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2020-06-29 21:34:26 -04:00
|
|
|
bool
|
|
|
|
AlsaAudioBackend::can_measure_systemic_latency () const
|
|
|
|
{
|
|
|
|
return _input_audio_device == _output_audio_device && _input_audio_device != get_standard_device_name (DeviceNone);
|
|
|
|
}
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_sample_rate (float sr)
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
if (sr <= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
_samplerate = sr;
|
|
|
|
engine.sample_rate_change (sr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-04 04:15:55 -05:00
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_peridod_size (uint32_t n)
|
|
|
|
{
|
2020-04-27 21:28:34 -04:00
|
|
|
if (n == 0) {
|
2015-12-04 04:15:55 -05:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (_run) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
_periods_per_cycle = n;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_buffer_size (uint32_t bs)
|
|
|
|
{
|
|
|
|
if (bs <= 0 || bs >= _max_buffer_size) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-03-07 03:11:15 -05:00
|
|
|
if (_run) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
_samples_per_period = bs;
|
|
|
|
engine.buffer_size_change (bs);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_interleaved (bool yn)
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
if (!yn) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_input_channels (uint32_t cc)
|
|
|
|
{
|
|
|
|
_n_inputs = cc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_output_channels (uint32_t cc)
|
|
|
|
{
|
|
|
|
_n_outputs = cc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_systemic_input_latency (uint32_t sl)
|
|
|
|
{
|
2020-05-08 19:21:11 -04:00
|
|
|
if (_systemic_audio_input_latency == sl) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-06 18:13:32 -04:00
|
|
|
_systemic_audio_input_latency = sl;
|
2015-12-04 11:45:49 -05:00
|
|
|
if (_run) {
|
2021-05-15 18:08:03 -04:00
|
|
|
update_systemic_audio_latencies ();
|
2015-12-04 11:45:49 -05:00
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_systemic_output_latency (uint32_t sl)
|
|
|
|
{
|
2020-05-08 19:21:11 -04:00
|
|
|
if (_systemic_audio_output_latency == sl) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-06 18:13:32 -04:00
|
|
|
_systemic_audio_output_latency = sl;
|
2015-12-04 11:45:49 -05:00
|
|
|
if (_run) {
|
2021-05-15 18:08:03 -04:00
|
|
|
update_systemic_audio_latencies ();
|
2015-12-04 11:45:49 -05:00
|
|
|
}
|
2014-06-12 15:35:25 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_systemic_midi_input_latency (std::string const device, uint32_t sl)
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
struct AlsaMidiDeviceInfo* nfo = midi_device_info (device);
|
|
|
|
if (!nfo) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-12 15:35:25 -04:00
|
|
|
nfo->systemic_input_latency = sl;
|
2015-12-04 11:45:49 -05:00
|
|
|
if (_run && nfo->enabled) {
|
|
|
|
update_systemic_midi_latencies ();
|
|
|
|
}
|
2014-06-12 15:35:25 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_systemic_midi_output_latency (std::string const device, uint32_t sl)
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
struct AlsaMidiDeviceInfo* nfo = midi_device_info (device);
|
|
|
|
if (!nfo) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-12 15:35:25 -04:00
|
|
|
nfo->systemic_output_latency = sl;
|
2015-12-04 11:45:49 -05:00
|
|
|
if (_run && nfo->enabled) {
|
|
|
|
update_systemic_midi_latencies ();
|
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-12-04 11:45:49 -05:00
|
|
|
void
|
|
|
|
AlsaAudioBackend::update_systemic_audio_latencies ()
|
|
|
|
{
|
|
|
|
const uint32_t lcpp = (_periods_per_cycle - 2) * _samples_per_period;
|
2021-05-15 18:08:03 -04:00
|
|
|
LatencyRange lr;
|
2015-12-04 11:45:49 -05:00
|
|
|
|
2021-04-11 09:07:04 -04:00
|
|
|
lr.min = lr.max = (_measure_latency ? 0 : _systemic_audio_output_latency);
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it) {
|
2015-12-04 11:45:49 -05:00
|
|
|
set_latency_range (*it, true, lr);
|
|
|
|
}
|
|
|
|
|
2021-04-11 09:07:04 -04:00
|
|
|
lr.min = lr.max = lcpp + (_measure_latency ? 0 : _systemic_audio_input_latency);
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) {
|
2015-12-04 11:45:49 -05:00
|
|
|
set_latency_range (*it, false, lr);
|
|
|
|
}
|
|
|
|
update_latencies ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AlsaAudioBackend::update_systemic_midi_latencies ()
|
|
|
|
{
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_lock (&_device_port_mutex);
|
2015-12-04 16:19:47 -05:00
|
|
|
uint32_t i = 0;
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it, ++i) {
|
2021-05-15 18:08:03 -04:00
|
|
|
assert (_rmidi_out.size () > i);
|
|
|
|
AlsaMidiOut* rm = _rmidi_out.at (i);
|
|
|
|
struct AlsaMidiDeviceInfo* nfo = midi_device_info (rm->name ());
|
2015-12-04 16:19:47 -05:00
|
|
|
assert (nfo);
|
2015-12-04 11:45:49 -05:00
|
|
|
LatencyRange lr;
|
|
|
|
lr.min = lr.max = (_measure_latency ? 0 : nfo->systemic_output_latency);
|
2020-04-09 17:40:23 -04:00
|
|
|
set_latency_range (*it, true, lr);
|
2015-12-04 11:45:49 -05:00
|
|
|
}
|
2015-12-04 16:19:47 -05:00
|
|
|
|
|
|
|
i = 0;
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it, ++i) {
|
2021-05-15 18:08:03 -04:00
|
|
|
assert (_rmidi_in.size () > i);
|
|
|
|
AlsaMidiIO* rm = _rmidi_in.at (i);
|
|
|
|
struct AlsaMidiDeviceInfo* nfo = midi_device_info (rm->name ());
|
2015-12-04 16:19:47 -05:00
|
|
|
assert (nfo);
|
2015-12-04 11:45:49 -05:00
|
|
|
LatencyRange lr;
|
|
|
|
lr.min = lr.max = (_measure_latency ? 0 : nfo->systemic_input_latency);
|
2019-05-05 18:14:27 -04:00
|
|
|
set_latency_range (*it, false, lr);
|
2015-12-04 11:45:49 -05:00
|
|
|
}
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_unlock (&_device_port_mutex);
|
2015-12-04 11:45:49 -05:00
|
|
|
update_latencies ();
|
|
|
|
}
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
/* Retrieving parameters */
|
|
|
|
std::string
|
|
|
|
AlsaAudioBackend::device_name () const
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
if (_input_audio_device != get_standard_device_name (DeviceNone)) {
|
2015-06-14 16:24:53 -04:00
|
|
|
return _input_audio_device;
|
|
|
|
}
|
2021-05-15 18:08:03 -04:00
|
|
|
if (_output_audio_device != get_standard_device_name (DeviceNone)) {
|
2015-06-14 16:24:53 -04:00
|
|
|
return _output_audio_device;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
AlsaAudioBackend::input_device_name () const
|
|
|
|
{
|
|
|
|
return _input_audio_device;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
AlsaAudioBackend::output_device_name () const
|
|
|
|
{
|
|
|
|
return _output_audio_device;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
AlsaAudioBackend::sample_rate () const
|
|
|
|
{
|
|
|
|
return _samplerate;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
AlsaAudioBackend::buffer_size () const
|
|
|
|
{
|
|
|
|
return _samples_per_period;
|
|
|
|
}
|
|
|
|
|
2015-12-04 04:15:55 -05:00
|
|
|
uint32_t
|
|
|
|
AlsaAudioBackend::period_size () const
|
|
|
|
{
|
|
|
|
return _periods_per_cycle;
|
|
|
|
}
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
bool
|
|
|
|
AlsaAudioBackend::interleaved () const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
AlsaAudioBackend::input_channels () const
|
|
|
|
{
|
|
|
|
return _n_inputs;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
AlsaAudioBackend::output_channels () const
|
|
|
|
{
|
|
|
|
return _n_outputs;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
AlsaAudioBackend::systemic_input_latency () const
|
|
|
|
{
|
2014-06-06 18:13:32 -04:00
|
|
|
return _systemic_audio_input_latency;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
AlsaAudioBackend::systemic_output_latency () const
|
|
|
|
{
|
2014-06-06 18:13:32 -04:00
|
|
|
return _systemic_audio_output_latency;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2014-06-12 15:35:25 -04:00
|
|
|
uint32_t
|
|
|
|
AlsaAudioBackend::systemic_midi_input_latency (std::string const device) const
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
struct AlsaMidiDeviceInfo* nfo = midi_device_info (device);
|
|
|
|
if (!nfo) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-12 15:35:25 -04:00
|
|
|
return nfo->systemic_input_latency;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
AlsaAudioBackend::systemic_midi_output_latency (std::string const device) const
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
struct AlsaMidiDeviceInfo* nfo = midi_device_info (device);
|
|
|
|
if (!nfo) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-12 15:35:25 -04:00
|
|
|
return nfo->systemic_output_latency;
|
|
|
|
}
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
/* MIDI */
|
2021-05-15 18:08:03 -04:00
|
|
|
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) {
|
2014-06-12 15:35:25 -04:00
|
|
|
if (i->first == name) {
|
|
|
|
return (i->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
assert (_midi_driver_option != get_standard_device_name (DeviceNone));
|
2014-06-21 07:51:46 -04:00
|
|
|
|
2014-06-12 15:35:25 -04:00
|
|
|
std::map<std::string, std::string> devices;
|
2014-06-21 07:51:46 -04:00
|
|
|
if (_midi_driver_option == _("ALSA raw devices")) {
|
2021-05-15 18:08:03 -04:00
|
|
|
get_alsa_rawmidi_device_names (devices);
|
2014-06-21 07:51:46 -04:00
|
|
|
} else {
|
|
|
|
get_alsa_sequencer_names (devices);
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end (); ++i) {
|
2014-06-12 15:35:25 -04:00
|
|
|
if (i->first == name) {
|
2021-05-15 18:08:03 -04:00
|
|
|
_midi_devices[name] = new AlsaMidiDeviceInfo ();
|
2014-06-12 15:35:25 -04:00
|
|
|
return _midi_devices[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
std::vector<std::string>
|
|
|
|
AlsaAudioBackend::enumerate_midi_options () const
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
if (_midi_options.empty ()) {
|
2014-06-23 05:56:58 -04:00
|
|
|
_midi_options.push_back (_("ALSA raw devices"));
|
|
|
|
_midi_options.push_back (_("ALSA sequencer"));
|
2021-05-15 18:08:03 -04:00
|
|
|
_midi_options.push_back (get_standard_device_name (DeviceNone));
|
2014-06-23 05:56:58 -04:00
|
|
|
}
|
|
|
|
return _midi_options;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2014-06-12 15:35:25 -04:00
|
|
|
std::vector<AudioBackend::DeviceStatus>
|
|
|
|
AlsaAudioBackend::enumerate_midi_devices () const
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
_midi_device_status.clear ();
|
2014-06-21 07:51:46 -04:00
|
|
|
std::map<std::string, std::string> devices;
|
|
|
|
|
|
|
|
if (_midi_driver_option == _("ALSA raw devices")) {
|
|
|
|
get_alsa_rawmidi_device_names (devices);
|
2021-05-15 18:08:03 -04:00
|
|
|
} else if (_midi_driver_option == _("ALSA sequencer")) {
|
2014-06-21 07:51:46 -04:00
|
|
|
get_alsa_sequencer_names (devices);
|
2014-06-13 05:46:37 -04:00
|
|
|
}
|
2014-06-12 15:35:25 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end (); ++i) {
|
2014-06-23 06:16:35 -04:00
|
|
|
_midi_device_status.push_back (DeviceStatus (i->first, true));
|
2014-06-12 15:35:25 -04:00
|
|
|
}
|
2014-06-23 06:16:35 -04:00
|
|
|
return _midi_device_status;
|
2014-06-12 15:35:25 -04:00
|
|
|
}
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
int
|
2014-06-02 10:52:07 -04:00
|
|
|
AlsaAudioBackend::set_midi_option (const std::string& opt)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
if (opt != get_standard_device_name (DeviceNone) && opt != _("ALSA raw devices") && opt != _("ALSA sequencer")) {
|
2014-06-13 05:46:37 -04:00
|
|
|
return -1;
|
|
|
|
}
|
2015-12-04 16:19:47 -05:00
|
|
|
if (_run && _midi_driver_option != opt) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-12 15:35:25 -04:00
|
|
|
_midi_driver_option = opt;
|
2014-06-02 10:52:07 -04:00
|
|
|
return 0;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
AlsaAudioBackend::midi_option () const
|
|
|
|
{
|
2014-06-12 15:35:25 -04:00
|
|
|
return _midi_driver_option;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::set_midi_device_enabled (std::string const device, bool enable)
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
struct AlsaMidiDeviceInfo* nfo = midi_device_info (device);
|
|
|
|
if (!nfo) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-12-04 16:19:47 -05:00
|
|
|
const bool prev_enabled = nfo->enabled;
|
2021-05-15 18:08:03 -04:00
|
|
|
nfo->enabled = enable;
|
2015-12-04 16:19:47 -05:00
|
|
|
|
|
|
|
if (_run && prev_enabled != enable) {
|
|
|
|
if (enable) {
|
|
|
|
// add ports for the given device
|
2021-05-15 18:08:03 -04:00
|
|
|
register_system_midi_ports (device);
|
2015-12-04 16:19:47 -05:00
|
|
|
} else {
|
|
|
|
// remove all ports provided by the given device
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_lock (&_device_port_mutex);
|
2015-12-04 16:19:47 -05:00
|
|
|
uint32_t i = 0;
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::iterator it = _system_midi_out.begin (); it != _system_midi_out.end ();) {
|
2021-05-15 18:08:03 -04:00
|
|
|
assert (_rmidi_out.size () > i);
|
|
|
|
AlsaMidiOut* rm = _rmidi_out.at (i);
|
|
|
|
if (rm->name () != device) {
|
|
|
|
++it;
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-04 16:19:47 -05:00
|
|
|
unregister_port (*it);
|
2019-05-05 12:06:37 -04:00
|
|
|
it = _system_midi_out.erase (it);
|
2021-05-15 18:08:03 -04:00
|
|
|
rm->stop ();
|
|
|
|
assert (rm == *(_rmidi_out.begin () + i));
|
|
|
|
_rmidi_out.erase (_rmidi_out.begin () + i);
|
2015-12-04 16:19:47 -05:00
|
|
|
delete rm;
|
|
|
|
}
|
|
|
|
|
|
|
|
i = 0;
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::iterator it = _system_midi_in.begin (); it != _system_midi_in.end ();) {
|
2021-05-15 18:08:03 -04:00
|
|
|
assert (_rmidi_in.size () > i);
|
|
|
|
AlsaMidiIn* rm = _rmidi_in.at (i);
|
|
|
|
if (rm->name () != device) {
|
|
|
|
++it;
|
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
2015-12-04 16:19:47 -05:00
|
|
|
unregister_port (*it);
|
2019-05-05 12:06:37 -04:00
|
|
|
it = _system_midi_in.erase (it);
|
2021-05-15 18:08:03 -04:00
|
|
|
rm->stop ();
|
|
|
|
assert (rm == *(_rmidi_in.begin () + i));
|
|
|
|
_rmidi_in.erase (_rmidi_in.begin () + i);
|
2015-12-04 16:19:47 -05:00
|
|
|
delete rm;
|
|
|
|
}
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_unlock (&_device_port_mutex);
|
2015-12-04 16:19:47 -05:00
|
|
|
}
|
|
|
|
update_systemic_midi_latencies ();
|
|
|
|
}
|
2014-06-12 15:35:25 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AlsaAudioBackend::midi_device_enabled (std::string const device) const
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
struct AlsaMidiDeviceInfo* nfo = midi_device_info (device);
|
|
|
|
if (!nfo) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-06-12 15:35:25 -04:00
|
|
|
return nfo->enabled;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* State Control */
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
static void*
|
|
|
|
pthread_process (void* arg)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaAudioBackend* d = static_cast<AlsaAudioBackend*> (arg);
|
2014-06-02 08:29:19 -04:00
|
|
|
d->main_process_thread ();
|
|
|
|
pthread_exit (0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::_start (bool for_latency_measurement)
|
|
|
|
{
|
2014-06-08 15:04:30 -04:00
|
|
|
if (!_active && _run) {
|
|
|
|
// recover from 'halted', reap threads
|
2021-05-15 18:08:03 -04:00
|
|
|
stop ();
|
2014-06-08 15:04:30 -04:00
|
|
|
}
|
|
|
|
|
2014-06-03 20:39:39 -04:00
|
|
|
if (_active || _run) {
|
2017-04-27 14:59:15 -04:00
|
|
|
if (for_latency_measurement != _measure_latency) {
|
|
|
|
_measure_latency = for_latency_measurement;
|
2021-05-15 18:08:03 -04:00
|
|
|
update_systemic_audio_latencies ();
|
2017-04-27 14:59:15 -04:00
|
|
|
update_systemic_midi_latencies ();
|
|
|
|
PBD::info << _("AlsaAudioBackend: reload latencies.") << endmsg;
|
|
|
|
return NoError;
|
|
|
|
}
|
|
|
|
PBD::info << _("AlsaAudioBackend: already active.") << endmsg;
|
2015-10-02 11:51:17 -04:00
|
|
|
return BackendReinitializationError;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2020-04-09 19:42:42 -04:00
|
|
|
_measure_latency = for_latency_measurement;
|
|
|
|
|
2020-04-02 16:26:44 -04:00
|
|
|
clear_ports ();
|
2014-06-02 08:29:19 -04:00
|
|
|
|
2015-03-04 19:33:13 -05:00
|
|
|
/* reset internal state */
|
2021-05-15 18:08:03 -04:00
|
|
|
_dsp_load = 0;
|
|
|
|
_freewheeling = false;
|
|
|
|
_freewheel = false;
|
2015-03-04 19:33:13 -05:00
|
|
|
_last_process_start = 0;
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
_device_reservation.release_device ();
|
2014-06-04 20:08:56 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
assert (_rmidi_in.size () == 0);
|
|
|
|
assert (_rmidi_out.size () == 0);
|
|
|
|
assert (_pcmi == 0);
|
2014-06-02 08:29:19 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
int duplex = 0;
|
2015-06-14 16:24:53 -04:00
|
|
|
std::string audio_device;
|
2014-06-04 13:16:37 -04:00
|
|
|
std::string alsa_device;
|
2021-05-15 18:08:03 -04:00
|
|
|
|
2014-06-04 13:16:37 -04:00
|
|
|
std::map<std::string, std::string> devices;
|
2015-06-14 16:24:53 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
if (_input_audio_device == get_standard_device_name (DeviceNone) && _output_audio_device == get_standard_device_name (DeviceNone)) {
|
2015-06-14 16:24:53 -04:00
|
|
|
PBD::error << _("AlsaAudioBackend: At least one of input or output device needs to be set.");
|
2015-10-02 11:51:17 -04:00
|
|
|
return AudioDeviceInvalidError;
|
2015-06-14 16:24:53 -04:00
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
std::string slave_device;
|
2020-04-09 22:39:58 -04:00
|
|
|
AudioSlave::DuplexMode slave_duplex = AudioSlave::FullDuplex;
|
|
|
|
|
2015-06-14 16:24:53 -04:00
|
|
|
if (_input_audio_device != _output_audio_device) {
|
2022-11-29 00:39:20 -05:00
|
|
|
std::string input_audio_device (_input_audio_device);
|
|
|
|
std::string output_audio_device (_output_audio_device);
|
2021-05-15 18:08:03 -04:00
|
|
|
if (_input_audio_device != get_standard_device_name (DeviceNone) && _output_audio_device != get_standard_device_name (DeviceNone)) {
|
2021-01-24 12:10:35 -05:00
|
|
|
/* Different devices for In + Out.
|
|
|
|
* Ideally use input as clock source, and resample output.
|
|
|
|
* But when using separate devices, input is usually one (or more)
|
|
|
|
* cheap USB mic. Also keeping output device as "main",
|
|
|
|
* retains master-out connection.
|
|
|
|
*/
|
|
|
|
if (getenv ("ARDOUR_ALSA_CLK")) {
|
2022-11-29 00:39:20 -05:00
|
|
|
slave_device = _output_audio_device;
|
|
|
|
output_audio_device = get_standard_device_name (DeviceNone); //XXX
|
|
|
|
slave_duplex = AudioSlave::HalfDuplexOut;
|
2021-01-24 12:10:35 -05:00
|
|
|
} else {
|
2022-11-29 00:39:20 -05:00
|
|
|
slave_device = _input_audio_device;
|
|
|
|
input_audio_device = get_standard_device_name (DeviceNone); //XXX
|
|
|
|
slave_duplex = AudioSlave::HalfDuplexIn;
|
2021-01-24 12:10:35 -05:00
|
|
|
}
|
2015-06-14 16:24:53 -04:00
|
|
|
}
|
2022-11-29 00:39:20 -05:00
|
|
|
if (input_audio_device != get_standard_device_name (DeviceNone)) {
|
2021-05-15 18:08:03 -04:00
|
|
|
get_alsa_audio_device_names (devices, HalfDuplexIn);
|
2022-11-29 00:39:20 -05:00
|
|
|
audio_device = input_audio_device;
|
2021-05-15 18:08:03 -04:00
|
|
|
duplex = 1;
|
2015-06-14 16:24:53 -04:00
|
|
|
} else {
|
2021-05-15 18:08:03 -04:00
|
|
|
get_alsa_audio_device_names (devices, HalfDuplexOut);
|
2022-11-29 00:39:20 -05:00
|
|
|
audio_device = output_audio_device;
|
2021-05-15 18:08:03 -04:00
|
|
|
duplex = 2;
|
2015-06-14 16:24:53 -04:00
|
|
|
}
|
|
|
|
} else {
|
2021-05-15 18:08:03 -04:00
|
|
|
get_alsa_audio_device_names (devices);
|
2015-06-14 16:24:53 -04:00
|
|
|
audio_device = _input_audio_device;
|
2021-05-15 18:08:03 -04:00
|
|
|
duplex = 3;
|
2015-06-14 16:24:53 -04:00
|
|
|
}
|
|
|
|
|
2020-04-09 22:39:58 -04:00
|
|
|
std::map<std::string, std::string>::const_iterator di = devices.find (audio_device);
|
|
|
|
|
|
|
|
if (di == devices.end ()) {
|
2015-06-14 16:24:53 -04:00
|
|
|
PBD::error << _("AlsaAudioBackend: Cannot find configured device. Is it still connected?");
|
2015-10-02 11:51:17 -04:00
|
|
|
return AudioDeviceNotAvailableError;
|
2020-04-09 22:39:58 -04:00
|
|
|
} else {
|
|
|
|
alsa_device = di->second;
|
2021-05-15 18:08:03 -04:00
|
|
|
assert (!alsa_device.empty ());
|
2015-06-14 16:24:53 -04:00
|
|
|
}
|
2014-06-04 13:16:37 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
_device_reservation.acquire_device (alsa_device.c_str ());
|
2015-06-14 16:24:53 -04:00
|
|
|
_pcmi = new Alsa_pcmi (
|
2021-05-15 18:08:03 -04:00
|
|
|
(duplex & 2) ? alsa_device.c_str () : NULL,
|
|
|
|
(duplex & 1) ? alsa_device.c_str () : NULL,
|
|
|
|
/* ctrl name */ 0,
|
|
|
|
_samplerate, _samples_per_period,
|
|
|
|
_periods_per_cycle, _periods_per_cycle,
|
|
|
|
/* debug */ 0);
|
2015-09-26 07:03:02 -04:00
|
|
|
|
|
|
|
AudioBackend::ErrorCode error_code = NoError;
|
2021-05-15 18:08:03 -04:00
|
|
|
switch (_pcmi->state ()) {
|
|
|
|
case 0: /* OK */
|
|
|
|
break;
|
|
|
|
case -1:
|
|
|
|
PBD::error << _("AlsaAudioBackend: failed to open device.") << endmsg;
|
|
|
|
error_code = AudioDeviceOpenError;
|
|
|
|
break;
|
|
|
|
case -2:
|
|
|
|
PBD::error << _("AlsaAudioBackend: failed to allocate parameters.") << endmsg;
|
|
|
|
error_code = AudioDeviceOpenError;
|
|
|
|
break;
|
|
|
|
case -3:
|
|
|
|
PBD::error << _("AlsaAudioBackend: cannot set requested sample rate.")
|
|
|
|
<< endmsg;
|
|
|
|
error_code = SampleRateNotSupportedError;
|
|
|
|
break;
|
|
|
|
case -4:
|
|
|
|
PBD::error << _("AlsaAudioBackend: cannot set requested period size.")
|
|
|
|
<< endmsg;
|
|
|
|
error_code = PeriodSizeNotSupportedError;
|
|
|
|
break;
|
|
|
|
case -5:
|
|
|
|
PBD::error << _("AlsaAudioBackend: cannot set requested number of periods.")
|
|
|
|
<< endmsg;
|
|
|
|
error_code = PeriodCountNotSupportedError;
|
|
|
|
break;
|
|
|
|
case -6:
|
|
|
|
PBD::error << _("AlsaAudioBackend: unsupported sample format.") << endmsg;
|
|
|
|
error_code = SampleFormatNotSupportedError;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PBD::error << _("AlsaAudioBackend: initialization failed.") << endmsg;
|
|
|
|
error_code = AudioDeviceOpenError;
|
|
|
|
break;
|
2014-06-02 20:16:56 -04:00
|
|
|
}
|
2015-09-26 07:03:02 -04:00
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
if (_pcmi->state ()) {
|
2021-05-15 18:08:03 -04:00
|
|
|
delete _pcmi;
|
|
|
|
_pcmi = 0;
|
|
|
|
_device_reservation.release_device ();
|
2015-09-26 07:03:02 -04:00
|
|
|
return error_code;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
2021-05-15 18:08:03 -04:00
|
|
|
fprintf (stdout, " --[[ ALSA Device %s\n", alsa_device.c_str ());
|
2014-06-02 08:29:19 -04:00
|
|
|
_pcmi->printinfo ();
|
2020-12-03 15:29:21 -05:00
|
|
|
fprintf (stdout, " --]]\n");
|
2019-07-24 10:30:42 -04:00
|
|
|
#else
|
|
|
|
/* If any debug parameter is set, print info */
|
2021-06-09 20:05:04 -04:00
|
|
|
if (getenv ("ARDOUR_ALSA_DEBUG")) {
|
2021-05-15 18:08:03 -04:00
|
|
|
fprintf (stdout, " --[[ ALSA Device %s\n", alsa_device.c_str ());
|
2019-07-24 10:30:42 -04:00
|
|
|
_pcmi->printinfo ();
|
2020-12-03 15:29:21 -05:00
|
|
|
fprintf (stdout, " --]]\n");
|
2019-07-24 10:30:42 -04:00
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (_n_outputs != _pcmi->nplay ()) {
|
|
|
|
if (_n_outputs == 0) {
|
2021-05-15 18:08:03 -04:00
|
|
|
_n_outputs = _pcmi->nplay ();
|
2014-06-02 08:29:19 -04:00
|
|
|
} else {
|
2021-05-15 18:08:03 -04:00
|
|
|
_n_outputs = std::min (_n_outputs, _pcmi->nplay ());
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
2020-01-24 22:17:53 -05:00
|
|
|
PBD::info << _("AlsaAudioBackend: adjusted output channel count to match device.") << endmsg;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_n_inputs != _pcmi->ncapt ()) {
|
|
|
|
if (_n_inputs == 0) {
|
2021-05-15 18:08:03 -04:00
|
|
|
_n_inputs = _pcmi->ncapt ();
|
2014-06-02 08:29:19 -04:00
|
|
|
} else {
|
2021-05-15 18:08:03 -04:00
|
|
|
_n_inputs = std::min (_n_inputs, _pcmi->ncapt ());
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
2020-01-24 15:16:40 -05:00
|
|
|
PBD::info << _("AlsaAudioBackend: adjusted input channel count to match device.") << endmsg;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
if (_pcmi->fsize () != _samples_per_period) {
|
|
|
|
_samples_per_period = _pcmi->fsize ();
|
2021-01-22 10:01:41 -05:00
|
|
|
PBD::warning << string_compose (_("AlsaAudioBackend: samples per period does not match, using %1."), _samples_per_period) << endmsg;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
if (_pcmi->fsamp () != _samplerate) {
|
|
|
|
_samplerate = _pcmi->fsamp ();
|
2014-06-02 08:29:19 -04:00
|
|
|
engine.sample_rate_change (_samplerate);
|
|
|
|
PBD::warning << _("AlsaAudioBackend: sample rate does not match.") << endmsg;
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
register_system_midi_ports ();
|
2014-06-02 10:52:07 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
if (register_system_audio_ports ()) {
|
2014-06-02 08:29:19 -04:00
|
|
|
PBD::error << _("AlsaAudioBackend: failed to register system ports.") << endmsg;
|
2021-05-15 18:08:03 -04:00
|
|
|
delete _pcmi;
|
|
|
|
_pcmi = 0;
|
|
|
|
_device_reservation.release_device ();
|
2015-10-02 11:51:17 -04:00
|
|
|
return PortRegistrationError;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2014-06-05 21:43:39 -04:00
|
|
|
engine.sample_rate_change (_samplerate);
|
|
|
|
engine.buffer_size_change (_samples_per_period);
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
if (engine.reestablish_ports ()) {
|
|
|
|
PBD::error << _("AlsaAudioBackend: Could not re-establish ports.") << endmsg;
|
2021-05-15 18:08:03 -04:00
|
|
|
delete _pcmi;
|
|
|
|
_pcmi = 0;
|
|
|
|
_device_reservation.release_device ();
|
2015-10-02 11:51:17 -04:00
|
|
|
return PortReconnectError;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2014-06-03 20:39:39 -04:00
|
|
|
_run = true;
|
2021-02-04 15:04:40 -05:00
|
|
|
g_atomic_int_set (&_port_change_flag, 0);
|
2014-06-02 08:29:19 -04:00
|
|
|
|
2020-04-22 19:28:58 -04:00
|
|
|
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_MAIN, PBD_RT_STACKSIZE_PROC,
|
2021-05-15 18:08:03 -04:00
|
|
|
&_main_thread, pthread_process, this)) {
|
2020-06-06 12:35:44 -04:00
|
|
|
if (pbd_pthread_create (PBD_RT_STACKSIZE_PROC, &_main_thread, pthread_process, this)) {
|
2014-06-02 08:29:19 -04:00
|
|
|
PBD::error << _("AlsaAudioBackend: failed to create process thread.") << endmsg;
|
2021-05-15 18:08:03 -04:00
|
|
|
delete _pcmi;
|
|
|
|
_pcmi = 0;
|
|
|
|
_device_reservation.release_device ();
|
2014-06-03 20:39:39 -04:00
|
|
|
_run = false;
|
2015-10-02 11:51:17 -04:00
|
|
|
return ProcessThreadStartError;
|
2014-06-02 08:29:19 -04:00
|
|
|
} else {
|
|
|
|
PBD::warning << _("AlsaAudioBackend: cannot acquire realtime permissions.") << endmsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int timeout = 5000;
|
2021-05-15 18:08:03 -04:00
|
|
|
while (!_active && --timeout > 0) {
|
|
|
|
Glib::usleep (1000);
|
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
|
2014-06-03 20:39:39 -04:00
|
|
|
if (timeout == 0 || !_active) {
|
2014-06-02 08:29:19 -04:00
|
|
|
PBD::error << _("AlsaAudioBackend: failed to start process thread.") << endmsg;
|
2021-05-15 18:08:03 -04:00
|
|
|
delete _pcmi;
|
|
|
|
_pcmi = 0;
|
|
|
|
_device_reservation.release_device ();
|
2014-06-03 20:39:39 -04:00
|
|
|
_run = false;
|
2015-10-02 11:51:17 -04:00
|
|
|
return ProcessThreadStartError;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2018-12-23 15:48:07 -05:00
|
|
|
_midi_device_thread_active = listen_for_midi_device_changes ();
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
get_alsa_audio_device_names (devices, (AlsaDuplex)slave_duplex);
|
2021-01-22 10:06:42 -05:00
|
|
|
|
2020-04-09 22:39:58 -04:00
|
|
|
if (!slave_device.empty () && (di = devices.find (slave_device)) != devices.end ()) {
|
|
|
|
std::string dev = di->second;
|
2021-05-15 18:08:03 -04:00
|
|
|
if (add_slave (dev.c_str (), _samplerate, _samples_per_period, _periods_per_cycle, slave_duplex)) {
|
2020-04-09 22:39:58 -04:00
|
|
|
PBD::info << string_compose (_("ALSA slave '%1' added"), dev) << endmsg;
|
|
|
|
} else {
|
|
|
|
PBD::error << string_compose (_("ALSA failed to add '%1' as slave"), dev) << endmsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-26 10:44:29 -04:00
|
|
|
#if 1 // TODO: we need a GUI (and API) for this
|
2020-04-09 22:39:58 -04:00
|
|
|
/* example: ARDOUR_ALSA_EXT="hw:2@48000/512*3;hw:3@44100" */
|
2019-07-26 10:44:29 -04:00
|
|
|
if (NULL != getenv ("ARDOUR_ALSA_EXT")) {
|
2017-09-18 10:13:47 -04:00
|
|
|
boost::char_separator<char> sep (";");
|
2021-05-15 18:08:03 -04:00
|
|
|
std::string ext (getenv ("ARDOUR_ALSA_EXT"));
|
|
|
|
|
2020-04-09 22:39:58 -04:00
|
|
|
boost::tokenizer<boost::char_separator<char> > devs (ext, sep);
|
2021-05-15 18:08:03 -04:00
|
|
|
|
2017-09-18 10:13:47 -04:00
|
|
|
BOOST_FOREACH (const std::string& tmp, devs) {
|
2021-05-15 18:08:03 -04:00
|
|
|
std::string dev (tmp);
|
|
|
|
unsigned int sr = _samplerate;
|
|
|
|
unsigned int spp = _samples_per_period;
|
|
|
|
unsigned int ppc = _periods_per_cycle;
|
2020-04-09 22:39:58 -04:00
|
|
|
AudioSlave::DuplexMode duplex = AudioSlave::FullDuplex;
|
2021-05-15 18:08:03 -04:00
|
|
|
std::string::size_type n = dev.find ('@');
|
|
|
|
|
2017-09-18 10:13:47 -04:00
|
|
|
if (n != std::string::npos) {
|
2020-04-09 22:39:58 -04:00
|
|
|
std::string const opt (dev.substr (n + 1));
|
2021-05-15 18:08:03 -04:00
|
|
|
sr = PBD::atoi (opt);
|
2017-09-18 10:13:47 -04:00
|
|
|
dev = dev.substr (0, n);
|
2021-05-15 18:08:03 -04:00
|
|
|
|
2017-09-18 10:13:47 -04:00
|
|
|
std::string::size_type n = opt.find ('/');
|
|
|
|
if (n != std::string::npos) {
|
2020-04-09 22:39:58 -04:00
|
|
|
std::string const opt2 (opt.substr (n + 1));
|
|
|
|
spp = PBD::atoi (opt2);
|
2021-05-15 18:08:03 -04:00
|
|
|
|
2020-04-09 22:39:58 -04:00
|
|
|
std::string::size_type n = opt2.find ('*');
|
|
|
|
if (n != std::string::npos) {
|
|
|
|
ppc = PBD::atoi (opt2.substr (n + 1));
|
|
|
|
}
|
2017-09-18 10:13:47 -04:00
|
|
|
}
|
|
|
|
}
|
2021-05-15 18:08:03 -04:00
|
|
|
if (add_slave (dev.c_str (), sr, spp, ppc, duplex)) {
|
2017-09-18 10:13:47 -04:00
|
|
|
PBD::info << string_compose (_("ALSA slave '%1' added"), dev) << endmsg;
|
|
|
|
} else {
|
|
|
|
PBD::error << string_compose (_("ALSA failed to add '%1' as slave"), dev) << endmsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-02-19 08:02:34 -05:00
|
|
|
engine.reconnect_ports ();
|
|
|
|
|
2015-10-02 11:51:17 -04:00
|
|
|
return NoError;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::stop ()
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
void* status;
|
2014-06-08 09:53:15 -04:00
|
|
|
if (!_run) {
|
2014-06-02 08:29:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-03 20:39:39 -04:00
|
|
|
_run = false;
|
2014-06-02 08:29:19 -04:00
|
|
|
if (pthread_join (_main_thread, &status)) {
|
|
|
|
PBD::error << _("AlsaAudioBackend: failed to terminate.") << endmsg;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-02 10:52:07 -04:00
|
|
|
|
2018-12-23 15:48:07 -05:00
|
|
|
stop_listen_for_midi_device_changes ();
|
|
|
|
|
2014-06-02 10:52:07 -04:00
|
|
|
while (!_rmidi_out.empty ()) {
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaMidiIO* m = _rmidi_out.back ();
|
|
|
|
m->stop ();
|
2014-06-02 10:52:07 -04:00
|
|
|
_rmidi_out.pop_back ();
|
|
|
|
delete m;
|
|
|
|
}
|
|
|
|
while (!_rmidi_in.empty ()) {
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaMidiIO* m = _rmidi_in.back ();
|
|
|
|
m->stop ();
|
2014-06-02 10:52:07 -04:00
|
|
|
_rmidi_in.pop_back ();
|
|
|
|
delete m;
|
|
|
|
}
|
|
|
|
|
2017-09-18 10:13:47 -04:00
|
|
|
while (!_slaves.empty ()) {
|
|
|
|
AudioSlave* s = _slaves.back ();
|
|
|
|
_slaves.pop_back ();
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
unregister_ports ();
|
|
|
|
delete _pcmi;
|
|
|
|
_pcmi = 0;
|
|
|
|
_device_reservation.release_device ();
|
2017-04-27 14:59:15 -04:00
|
|
|
_measure_latency = false;
|
2014-06-04 20:08:56 -04:00
|
|
|
|
2014-06-03 20:39:39 -04:00
|
|
|
return (_active == false) ? 0 : -1;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::freewheel (bool onoff)
|
|
|
|
{
|
|
|
|
_freewheeling = onoff;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
AlsaAudioBackend::dsp_load () const
|
|
|
|
{
|
2015-09-16 11:45:16 -04:00
|
|
|
return 100.f * _dsp_load;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
AlsaAudioBackend::raw_buffer_size (DataType t)
|
|
|
|
{
|
|
|
|
switch (t) {
|
|
|
|
case DataType::AUDIO:
|
2021-05-15 18:08:03 -04:00
|
|
|
return _samples_per_period * sizeof (Sample);
|
2014-06-02 08:29:19 -04:00
|
|
|
case DataType::MIDI:
|
|
|
|
return _max_buffer_size; // XXX not really limited
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Process time */
|
2017-09-18 12:39:17 -04:00
|
|
|
samplepos_t
|
2014-06-02 08:29:19 -04:00
|
|
|
AlsaAudioBackend::sample_time ()
|
|
|
|
{
|
|
|
|
return _processed_samples;
|
|
|
|
}
|
|
|
|
|
2017-09-18 12:39:17 -04:00
|
|
|
samplepos_t
|
2014-06-02 08:29:19 -04:00
|
|
|
AlsaAudioBackend::sample_time_at_cycle_start ()
|
|
|
|
{
|
|
|
|
return _processed_samples;
|
|
|
|
}
|
|
|
|
|
|
|
|
pframes_t
|
|
|
|
AlsaAudioBackend::samples_since_cycle_start ()
|
|
|
|
{
|
2015-03-04 19:33:13 -05:00
|
|
|
if (!_active || !_run || _freewheeling || _freewheel) {
|
|
|
|
return 0;
|
|
|
|
}
|
2015-03-09 12:02:05 -04:00
|
|
|
if (_last_process_start == 0) {
|
2015-03-04 19:33:13 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
const int64_t elapsed_time_us = g_get_monotonic_time () - _last_process_start;
|
|
|
|
return std::max ((pframes_t)0, (pframes_t)rint (1e-6 * elapsed_time_us * _samplerate));
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
void*
|
|
|
|
AlsaAudioBackend::alsa_process_thread (void* arg)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
|
|
|
ThreadData* td = reinterpret_cast<ThreadData*> (arg);
|
|
|
|
boost::function<void ()> f = td->f;
|
|
|
|
delete td;
|
|
|
|
f ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaAudioBackend::create_process_thread (boost::function<void ()> func)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
2020-06-06 12:35:44 -04:00
|
|
|
pthread_t thread_id;
|
2020-04-22 19:28:58 -04:00
|
|
|
ThreadData* td = new ThreadData (this, func, PBD_RT_STACKSIZE_PROC);
|
2014-06-02 08:29:19 -04:00
|
|
|
|
2020-06-06 12:35:44 -04:00
|
|
|
if (pbd_realtime_pthread_create (PBD_SCHED_FIFO, PBD_RT_PRI_PROC, PBD_RT_STACKSIZE_PROC, &thread_id, alsa_process_thread, td)) {
|
|
|
|
if (pbd_pthread_create (PBD_RT_STACKSIZE_PROC, &thread_id, alsa_process_thread, td)) {
|
2014-06-06 14:37:00 -04:00
|
|
|
PBD::error << _("AudioEngine: cannot create process thread.") << endmsg;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_threads.push_back (thread_id);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::join_process_threads ()
|
|
|
|
{
|
|
|
|
int rv = 0;
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
for (std::vector<pthread_t>::const_iterator i = _threads.begin (); i != _threads.end (); ++i) {
|
|
|
|
void* status;
|
2014-06-02 08:29:19 -04:00
|
|
|
if (pthread_join (*i, &status)) {
|
|
|
|
PBD::error << _("AudioEngine: cannot terminate process thread.") << endmsg;
|
|
|
|
rv -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_threads.clear ();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AlsaAudioBackend::in_process_thread ()
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
if (pthread_equal (_main_thread, pthread_self ()) != 0) {
|
2014-10-22 17:02:15 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
for (std::vector<pthread_t>::const_iterator i = _threads.begin (); i != _threads.end (); ++i) {
|
2014-06-02 08:29:19 -04:00
|
|
|
if (pthread_equal (*i, pthread_self ()) != 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
AlsaAudioBackend::process_thread_count ()
|
|
|
|
{
|
|
|
|
return _threads.size ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AlsaAudioBackend::update_latencies ()
|
|
|
|
{
|
2014-06-08 15:04:30 -04:00
|
|
|
// trigger latency callback in RT thread (locked graph)
|
2021-05-15 18:08:03 -04:00
|
|
|
port_connect_add_remove_callback ();
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* PORTENGINE API */
|
|
|
|
|
|
|
|
void*
|
|
|
|
AlsaAudioBackend::private_handle () const
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string&
|
|
|
|
AlsaAudioBackend::my_name () const
|
|
|
|
{
|
|
|
|
return _instance_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaAudioBackend::register_system_audio_ports ()
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
|
|
|
LatencyRange lr;
|
|
|
|
|
2015-06-14 14:43:29 -04:00
|
|
|
const int a_ins = _n_inputs;
|
|
|
|
const int a_out = _n_outputs;
|
2014-06-02 08:29:19 -04:00
|
|
|
|
2015-12-04 11:45:49 -05:00
|
|
|
const uint32_t lcpp = (_periods_per_cycle - 2) * _samples_per_period;
|
2015-12-04 04:15:55 -05:00
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
/* audio ports */
|
2020-04-09 19:42:42 -04:00
|
|
|
lr.min = lr.max = (_measure_latency ? 0 : _systemic_audio_input_latency);
|
2014-06-02 08:29:19 -04:00
|
|
|
for (int i = 1; i <= a_ins; ++i) {
|
|
|
|
char tmp[64];
|
2021-05-15 18:08:03 -04:00
|
|
|
snprintf (tmp, sizeof (tmp), "system:capture_%d", i);
|
|
|
|
PortHandle p = add_port (std::string (tmp), DataType::AUDIO, static_cast<PortFlags> (IsOutput | IsPhysical | IsTerminal));
|
|
|
|
if (!p)
|
|
|
|
return -1;
|
2014-06-02 08:29:19 -04:00
|
|
|
set_latency_range (p, false, lr);
|
2021-05-15 18:08:03 -04:00
|
|
|
BackendPortPtr ap = boost::dynamic_pointer_cast<BackendPort> (p);
|
2021-01-16 11:44:05 -05:00
|
|
|
ap->set_hw_port_name (string_compose (_("Main In %1"), i));
|
2016-01-11 05:46:10 -05:00
|
|
|
_system_inputs.push_back (ap);
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2020-04-09 19:42:42 -04:00
|
|
|
lr.min = lr.max = lcpp + (_measure_latency ? 0 : _systemic_audio_output_latency);
|
2014-06-02 08:29:19 -04:00
|
|
|
for (int i = 1; i <= a_out; ++i) {
|
|
|
|
char tmp[64];
|
2021-05-15 18:08:03 -04:00
|
|
|
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;
|
2014-06-05 18:57:30 -04:00
|
|
|
set_latency_range (p, true, lr);
|
2021-05-15 18:08:03 -04:00
|
|
|
BackendPortPtr ap = boost::dynamic_pointer_cast<BackendPort> (p);
|
2021-01-16 11:44:05 -05:00
|
|
|
if (a_out == 2) {
|
|
|
|
ap->set_hw_port_name (i == 1 ? _("Out Left") : _("Out Right"));
|
|
|
|
} else {
|
|
|
|
ap->set_hw_port_name (string_compose (_("Main Out %1"), i));
|
|
|
|
}
|
2016-01-11 05:46:10 -05:00
|
|
|
_system_outputs.push_back (ap);
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
2014-06-02 10:52:07 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-23 15:48:07 -05:00
|
|
|
void
|
|
|
|
AlsaAudioBackend::auto_update_midi_devices ()
|
|
|
|
{
|
|
|
|
std::map<std::string, std::string> devices;
|
|
|
|
if (_midi_driver_option == _("ALSA raw devices")) {
|
|
|
|
get_alsa_rawmidi_device_names (devices);
|
|
|
|
} else if (_midi_driver_option == _("ALSA sequencer")) {
|
|
|
|
get_alsa_sequencer_names (devices);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* find new devices */
|
2021-05-15 18:08:03 -04:00
|
|
|
for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end (); ++i) {
|
|
|
|
if (_midi_devices.find (i->first) != _midi_devices.end ()) {
|
2018-12-23 15:48:07 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_midi_devices[i->first] = new AlsaMidiDeviceInfo (false);
|
|
|
|
set_midi_device_enabled (i->first, true);
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
for (std::map<std::string, struct AlsaMidiDeviceInfo*>::iterator i = _midi_devices.begin (); i != _midi_devices.end ();) {
|
|
|
|
if (devices.find (i->first) != devices.end ()) {
|
2018-12-23 15:48:07 -05:00
|
|
|
++i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
set_midi_device_enabled (i->first, false);
|
2021-05-15 18:08:03 -04:00
|
|
|
std::map<std::string, struct AlsaMidiDeviceInfo*>::iterator tmp = i;
|
2018-12-27 06:45:36 -05:00
|
|
|
++tmp;
|
|
|
|
_midi_devices.erase (i);
|
|
|
|
i = tmp;
|
2018-12-23 15:48:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
|
|
|
AlsaAudioBackend::_midi_device_thread (void* arg)
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaAudioBackend* self = static_cast<AlsaAudioBackend*> (arg);
|
2021-06-30 11:14:37 -04:00
|
|
|
pthread_set_name ("ALSA-MIDI-LIST");
|
2018-12-23 15:48:07 -05:00
|
|
|
self->midi_device_thread ();
|
|
|
|
pthread_exit (0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AlsaAudioBackend::midi_device_thread ()
|
|
|
|
{
|
|
|
|
snd_seq_t* seq;
|
|
|
|
if (snd_seq_open (&seq, "hw", SND_SEQ_OPEN_INPUT, 0) < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (snd_seq_set_client_name (seq, "Ardour")) {
|
|
|
|
snd_seq_close (seq);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (snd_seq_nonblock (seq, 1) < 0) {
|
|
|
|
snd_seq_close (seq);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int npfds = snd_seq_poll_descriptors_count (seq, POLLIN);
|
|
|
|
if (npfds < 1) {
|
|
|
|
snd_seq_close (seq);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int port = snd_seq_create_simple_port (seq, "port", SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_NO_EXPORT, SND_SEQ_PORT_TYPE_APPLICATION);
|
|
|
|
snd_seq_connect_from (seq, port, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE);
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
struct pollfd* pfds = (struct pollfd*)malloc (npfds * sizeof (struct pollfd));
|
2018-12-23 15:48:07 -05:00
|
|
|
snd_seq_poll_descriptors (seq, pfds, npfds, POLLIN);
|
|
|
|
snd_seq_drop_input (seq);
|
|
|
|
|
|
|
|
bool do_poll = true;
|
|
|
|
while (_run) {
|
|
|
|
if (do_poll) {
|
|
|
|
int perr = poll (pfds, npfds, 200 /* ms */);
|
|
|
|
if (perr == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (perr < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
snd_seq_event_t* event;
|
|
|
|
ssize_t err = snd_seq_event_input (seq, &event);
|
2018-12-23 15:48:07 -05:00
|
|
|
#if EAGAIN == EWOULDBLOCK
|
|
|
|
if ((err == -EAGAIN) || (err == -ENOSPC))
|
|
|
|
#else
|
|
|
|
if ((err == -EAGAIN) || (err == -EWOULDBLOCK) || (err == -ENOSPC))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
do_poll = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (err < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (event->source.client == SND_SEQ_CLIENT_SYSTEM);
|
|
|
|
|
|
|
|
switch (event->type) {
|
|
|
|
case SND_SEQ_EVENT_PORT_START:
|
|
|
|
case SND_SEQ_EVENT_PORT_EXIT:
|
|
|
|
case SND_SEQ_EVENT_PORT_CHANGE:
|
|
|
|
auto_update_midi_devices ();
|
2021-05-15 18:08:03 -04:00
|
|
|
engine.request_device_list_update ();
|
2018-12-23 15:48:07 -05:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
do_poll = (0 == err);
|
|
|
|
}
|
|
|
|
free (pfds);
|
|
|
|
snd_seq_delete_simple_port (seq, port);
|
|
|
|
snd_seq_close (seq);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
AlsaAudioBackend::listen_for_midi_device_changes ()
|
|
|
|
{
|
|
|
|
if (pthread_create (&_midi_device_thread_id, NULL, _midi_device_thread, this)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AlsaAudioBackend::stop_listen_for_midi_device_changes ()
|
|
|
|
{
|
|
|
|
if (!_midi_device_thread_active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pthread_join (_midi_device_thread_id, NULL);
|
|
|
|
_midi_device_thread_active = false;
|
|
|
|
}
|
|
|
|
|
2017-09-22 09:28:00 -04:00
|
|
|
/* set playback-latency for _system_inputs
|
|
|
|
* and capture-latency for _system_outputs
|
|
|
|
*/
|
|
|
|
void
|
2020-04-07 12:18:16 -04:00
|
|
|
AlsaAudioBackend::update_system_port_latencies ()
|
2017-09-22 09:28:00 -04:00
|
|
|
{
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_lock (&_device_port_mutex);
|
2020-04-07 12:18:16 -04:00
|
|
|
|
|
|
|
PortEngineSharedImpl::update_system_port_latencies ();
|
|
|
|
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_unlock (&_device_port_mutex);
|
2017-09-22 09:28:00 -04:00
|
|
|
|
|
|
|
for (AudioSlaves::iterator s = _slaves.begin (); s != _slaves.end (); ++s) {
|
|
|
|
if ((*s)->dead) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-07 12:18:16 -04:00
|
|
|
|
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = (*s)->inputs.begin (); it != (*s)->inputs.end (); ++it) {
|
2017-09-22 09:28:00 -04:00
|
|
|
(*it)->update_connected_latency (true);
|
|
|
|
}
|
2020-04-07 12:18:16 -04:00
|
|
|
|
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = (*s)->outputs.begin (); it != (*s)->outputs.end (); ++it) {
|
2017-09-22 09:28:00 -04:00
|
|
|
(*it)->update_connected_latency (false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-21 04:43:22 -04:00
|
|
|
/* libs/ardouralsautil/devicelist.cc appends either of
|
|
|
|
* " (IO)", " (I)", or " (O)"
|
|
|
|
* depending of the device is full-duples or half-duplex
|
|
|
|
*/
|
2021-05-15 18:08:03 -04:00
|
|
|
static std::string
|
|
|
|
replace_name_io (std::string const& name, bool in)
|
2017-04-21 04:43:22 -04:00
|
|
|
{
|
|
|
|
if (name.empty ()) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
size_t pos = name.find_last_of ('(');
|
|
|
|
if (pos == std::string::npos) {
|
|
|
|
assert (0); // this should never happen.
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
return name.substr (0, pos) + "(" + (in ? "In" : "Out") + ")";
|
|
|
|
}
|
|
|
|
|
2018-12-23 13:40:48 -05:00
|
|
|
static uint32_t
|
|
|
|
elf_hash (std::string const& s)
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
const uint8_t* b = (const uint8_t*)s.c_str ();
|
|
|
|
uint32_t h = 0;
|
|
|
|
for (size_t i = 0; i < s.length (); ++i) {
|
|
|
|
h = (h << 4) + b[i];
|
2018-12-23 13:40:48 -05:00
|
|
|
uint32_t high = h & 0xF0000000;
|
|
|
|
if (high) {
|
|
|
|
h ^= high >> 24;
|
|
|
|
h &= ~high;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
2014-06-02 10:52:07 -04:00
|
|
|
int
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaAudioBackend::register_system_midi_ports (const std::string device)
|
2014-06-02 10:52:07 -04:00
|
|
|
{
|
2014-06-13 05:46:37 -04:00
|
|
|
std::map<std::string, std::string> devices;
|
2015-12-04 16:19:47 -05:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
if (_midi_driver_option == get_standard_device_name (DeviceNone)) {
|
2014-06-02 10:52:07 -04:00
|
|
|
return 0;
|
2014-06-21 07:51:46 -04:00
|
|
|
} else if (_midi_driver_option == _("ALSA raw devices")) {
|
2021-05-15 18:08:03 -04:00
|
|
|
get_alsa_rawmidi_device_names (devices);
|
2014-06-21 07:51:46 -04:00
|
|
|
} else {
|
|
|
|
get_alsa_sequencer_names (devices);
|
2014-06-02 10:52:07 -04:00
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
for (std::map<std::string, std::string>::const_iterator i = devices.begin (); i != devices.end (); ++i) {
|
|
|
|
if (!device.empty () && device != i->first) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
struct AlsaMidiDeviceInfo* nfo = midi_device_info (i->first);
|
|
|
|
if (!nfo) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!nfo->enabled) {
|
2015-12-04 16:19:47 -05:00
|
|
|
continue;
|
|
|
|
}
|
2014-06-02 10:52:07 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaMidiOut* mout;
|
2014-06-21 07:51:46 -04:00
|
|
|
if (_midi_driver_option == _("ALSA raw devices")) {
|
2021-05-15 18:08:03 -04:00
|
|
|
mout = new AlsaRawMidiOut (i->first, i->second.c_str ());
|
2014-06-21 07:51:46 -04:00
|
|
|
} else {
|
2021-05-15 18:08:03 -04:00
|
|
|
mout = new AlsaSeqMidiOut (i->first, i->second.c_str ());
|
2014-06-21 07:51:46 -04:00
|
|
|
}
|
|
|
|
|
2014-06-02 10:52:07 -04:00
|
|
|
if (mout->state ()) {
|
2021-05-15 18:08:03 -04:00
|
|
|
PBD::warning << string_compose (_("AlsaMidiOut: failed to open midi device '%1'."), i->second) << endmsg;
|
2014-06-02 10:52:07 -04:00
|
|
|
delete mout;
|
|
|
|
} else {
|
2021-05-15 18:08:03 -04:00
|
|
|
mout->setup_timing (_samples_per_period, _samplerate);
|
|
|
|
mout->sync_time (g_get_monotonic_time ());
|
2014-06-02 10:52:07 -04:00
|
|
|
if (mout->start ()) {
|
2021-05-15 18:08:03 -04:00
|
|
|
PBD::warning << string_compose (_("AlsaMidiOut: failed to start midi device '%1'."), i->second) << endmsg;
|
2014-06-02 10:52:07 -04:00
|
|
|
delete mout;
|
|
|
|
} else {
|
2014-06-13 05:46:37 -04:00
|
|
|
char tmp[64];
|
2019-03-06 17:41:15 -05:00
|
|
|
for (int x = 0; x < 10; ++x) {
|
2021-05-15 18:08:03 -04:00
|
|
|
snprintf (tmp, sizeof (tmp), "system:midi_playback_%x%d", elf_hash (i->first), x);
|
2019-03-06 17:41:15 -05:00
|
|
|
if (!find_port (tmp)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-15 18:08:03 -04:00
|
|
|
PortHandle p = add_port (std::string (tmp), DataType::MIDI, static_cast<PortFlags> (IsInput | IsPhysical | IsTerminal));
|
2014-06-13 05:46:37 -04:00
|
|
|
if (!p) {
|
2021-05-15 18:08:03 -04:00
|
|
|
mout->stop ();
|
2014-06-13 05:46:37 -04:00
|
|
|
delete mout;
|
2019-08-22 22:39:12 -04:00
|
|
|
} else {
|
|
|
|
LatencyRange lr;
|
2020-04-09 19:42:42 -04:00
|
|
|
lr.min = lr.max = (_measure_latency ? 0 : nfo->systemic_output_latency);
|
2019-08-22 22:39:12 -04:00
|
|
|
set_latency_range (p, true, lr);
|
2021-05-15 18:08:03 -04:00
|
|
|
boost::dynamic_pointer_cast<AlsaMidiPort> (p)->set_n_periods (_periods_per_cycle); // TODO check MIDI alignment
|
|
|
|
BackendPortPtr ap = boost::dynamic_pointer_cast<BackendPort> (p);
|
2021-01-13 16:07:11 -05:00
|
|
|
ap->set_hw_port_name (replace_name_io (i->first, false));
|
2019-08-22 22:39:12 -04:00
|
|
|
pthread_mutex_lock (&_device_port_mutex);
|
|
|
|
_system_midi_out.push_back (ap);
|
|
|
|
pthread_mutex_unlock (&_device_port_mutex);
|
|
|
|
_rmidi_out.push_back (mout);
|
2014-06-13 05:46:37 -04:00
|
|
|
}
|
2014-06-02 10:52:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaMidiIn* midin;
|
2014-06-21 07:51:46 -04:00
|
|
|
if (_midi_driver_option == _("ALSA raw devices")) {
|
2021-05-15 18:08:03 -04:00
|
|
|
midin = new AlsaRawMidiIn (i->first, i->second.c_str ());
|
2014-06-21 07:51:46 -04:00
|
|
|
} else {
|
2021-05-15 18:08:03 -04:00
|
|
|
midin = new AlsaSeqMidiIn (i->first, i->second.c_str ());
|
2014-06-21 07:51:46 -04:00
|
|
|
}
|
|
|
|
|
2014-06-02 10:52:07 -04:00
|
|
|
if (midin->state ()) {
|
2021-05-15 18:08:03 -04:00
|
|
|
PBD::warning << string_compose (_("AlsaMidiIn: failed to open midi device '%1'."), i->second) << endmsg;
|
2014-06-02 10:52:07 -04:00
|
|
|
delete midin;
|
|
|
|
} else {
|
2021-05-15 18:08:03 -04:00
|
|
|
midin->setup_timing (_samples_per_period, _samplerate);
|
|
|
|
midin->sync_time (g_get_monotonic_time ());
|
2014-06-02 10:52:07 -04:00
|
|
|
if (midin->start ()) {
|
2021-05-15 18:08:03 -04:00
|
|
|
PBD::warning << string_compose (_("AlsaMidiIn: failed to start midi device '%1'."), i->second) << endmsg;
|
2014-06-02 10:52:07 -04:00
|
|
|
delete midin;
|
|
|
|
} else {
|
2014-06-13 05:46:37 -04:00
|
|
|
char tmp[64];
|
2019-03-06 17:41:15 -05:00
|
|
|
for (int x = 0; x < 10; ++x) {
|
2021-05-15 18:08:03 -04:00
|
|
|
snprintf (tmp, sizeof (tmp), "system:midi_capture_%x%d", elf_hash (i->first), x);
|
2019-03-06 17:41:15 -05:00
|
|
|
if (!find_port (tmp)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-15 18:08:03 -04:00
|
|
|
PortHandle p = add_port (std::string (tmp), DataType::MIDI, static_cast<PortFlags> (IsOutput | IsPhysical | IsTerminal));
|
2014-06-13 05:46:37 -04:00
|
|
|
if (!p) {
|
2021-05-15 18:08:03 -04:00
|
|
|
midin->stop ();
|
2014-06-13 05:46:37 -04:00
|
|
|
delete midin;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
LatencyRange lr;
|
2020-04-09 19:42:42 -04:00
|
|
|
lr.min = lr.max = (_measure_latency ? 0 : nfo->systemic_input_latency);
|
2014-06-13 05:46:37 -04:00
|
|
|
set_latency_range (p, false, lr);
|
2021-05-15 18:08:03 -04:00
|
|
|
BackendPortPtr ap = boost::dynamic_pointer_cast<BackendPort> (p);
|
2021-01-13 16:07:11 -05:00
|
|
|
ap->set_hw_port_name (replace_name_io (i->first, true));
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_lock (&_device_port_mutex);
|
2016-01-11 05:46:10 -05:00
|
|
|
_system_midi_in.push_back (ap);
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_unlock (&_device_port_mutex);
|
2014-06-02 10:52:07 -04:00
|
|
|
_rmidi_in.push_back (midin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* MIDI */
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::midi_event_get (
|
2021-05-15 18:08:03 -04:00
|
|
|
pframes_t& timestamp,
|
|
|
|
size_t& size, uint8_t const** buf, void* port_buffer,
|
|
|
|
uint32_t event_index)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
|
|
|
assert (buf && port_buffer);
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaMidiBuffer& source = *static_cast<AlsaMidiBuffer*> (port_buffer);
|
2014-06-02 08:29:19 -04:00
|
|
|
if (event_index >= source.size ()) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-08-05 09:03:06 -04:00
|
|
|
AlsaMidiEvent const& event = source[event_index];
|
2014-06-02 08:29:19 -04:00
|
|
|
|
2017-08-05 09:03:06 -04:00
|
|
|
timestamp = event.timestamp ();
|
2021-05-15 18:08:03 -04:00
|
|
|
size = event.size ();
|
|
|
|
*buf = event.data ();
|
2014-06-02 08:29:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::midi_event_put (
|
2021-05-15 18:08:03 -04:00
|
|
|
void* port_buffer,
|
|
|
|
pframes_t timestamp,
|
|
|
|
const uint8_t* buffer, size_t size)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
|
|
|
assert (buffer && port_buffer);
|
2017-08-05 09:03:06 -04:00
|
|
|
if (size >= MaxAlsaMidiEventSize) {
|
|
|
|
return -1;
|
|
|
|
}
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaMidiBuffer& dst = *static_cast<AlsaMidiBuffer*> (port_buffer);
|
2015-03-09 00:41:36 -04:00
|
|
|
#ifndef NDEBUG
|
2017-08-05 09:03:06 -04:00
|
|
|
if (dst.size () && (pframes_t)dst.back ().timestamp () > timestamp) {
|
2015-03-09 00:41:36 -04:00
|
|
|
// nevermind, ::get_buffer() sorts events
|
2014-06-02 08:29:19 -04:00
|
|
|
fprintf (stderr, "AlsaMidiBuffer: it's too late for this event. %d > %d\n",
|
2021-05-15 18:08:03 -04:00
|
|
|
(pframes_t)dst.back ().timestamp (), timestamp);
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
2017-08-05 09:03:06 -04:00
|
|
|
#endif
|
|
|
|
dst.push_back (AlsaMidiEvent (timestamp, buffer, size));
|
2014-06-02 08:29:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
AlsaAudioBackend::get_midi_event_count (void* port_buffer)
|
|
|
|
{
|
|
|
|
assert (port_buffer);
|
2021-05-15 18:08:03 -04:00
|
|
|
return static_cast<AlsaMidiBuffer*> (port_buffer)->size ();
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AlsaAudioBackend::midi_clear (void* port_buffer)
|
|
|
|
{
|
|
|
|
assert (port_buffer);
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaMidiBuffer* buf = static_cast<AlsaMidiBuffer*> (port_buffer);
|
2014-06-02 08:29:19 -04:00
|
|
|
assert (buf);
|
|
|
|
buf->clear ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Monitoring */
|
|
|
|
|
|
|
|
bool
|
|
|
|
AlsaAudioBackend::can_monitor_input () const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::request_input_monitoring (PortEngine::PortHandle, bool)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
AlsaAudioBackend::ensure_input_monitoring (PortEngine::PortHandle, bool)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaAudioBackend::monitoring_input (PortEngine::PortHandle)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Latency management */
|
|
|
|
|
|
|
|
void
|
2020-04-07 12:18:16 -04:00
|
|
|
AlsaAudioBackend::set_latency_range (PortEngine::PortHandle port_handle, bool for_playback, LatencyRange latency_range)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
2020-04-07 12:18:16 -04:00
|
|
|
BackendPortPtr port = boost::dynamic_pointer_cast<BackendPort> (port_handle);
|
2014-06-02 08:29:19 -04:00
|
|
|
if (!valid_port (port)) {
|
2022-06-24 13:24:48 -04:00
|
|
|
DEBUG_TRACE (PBD::DEBUG::BackendPorts, "AlsaPort::set_latency_range (): invalid port.");
|
2022-06-24 13:55:17 -04:00
|
|
|
return;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
2020-04-07 12:18:16 -04:00
|
|
|
port->set_latency_range (latency_range, for_playback);
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
LatencyRange
|
2020-04-07 12:18:16 -04:00
|
|
|
AlsaAudioBackend::get_latency_range (PortEngine::PortHandle port_handle, bool for_playback)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
2020-04-07 12:18:16 -04:00
|
|
|
BackendPortPtr port = boost::dynamic_pointer_cast<BackendPort> (port_handle);
|
2021-05-15 18:08:03 -04:00
|
|
|
LatencyRange r;
|
2020-04-07 12:18:16 -04:00
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
if (!valid_port (port)) {
|
2022-06-24 13:24:48 -04:00
|
|
|
DEBUG_TRACE (PBD::DEBUG::BackendPorts, "AlsaPort::get_latency_range (): invalid port.");
|
2014-06-02 08:29:19 -04:00
|
|
|
r.min = 0;
|
|
|
|
r.max = 0;
|
|
|
|
return r;
|
|
|
|
}
|
2015-03-07 03:11:15 -05:00
|
|
|
|
2020-04-07 12:18:16 -04:00
|
|
|
r = port->latency_range (for_playback);
|
2021-05-15 18:08:03 -04:00
|
|
|
if (port->is_physical () && port->is_terminal ()) {
|
|
|
|
if (port->is_input () && for_playback) {
|
2015-03-07 03:11:15 -05:00
|
|
|
r.min += _samples_per_period;
|
|
|
|
r.max += _samples_per_period;
|
|
|
|
}
|
2021-05-15 18:08:03 -04:00
|
|
|
if (port->is_output () && !for_playback) {
|
2015-03-07 03:11:15 -05:00
|
|
|
r.min += _samples_per_period;
|
|
|
|
r.max += _samples_per_period;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 12:18:16 -04:00
|
|
|
|
2015-03-07 03:11:15 -05:00
|
|
|
return r;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2020-04-02 16:26:44 -04:00
|
|
|
BackendPort*
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaAudioBackend::port_factory (std::string const& name, ARDOUR::DataType type, ARDOUR::PortFlags flags)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
2020-04-02 16:26:44 -04:00
|
|
|
BackendPort* port = 0;
|
2020-04-02 01:04:23 -04:00
|
|
|
|
2020-04-02 16:26:44 -04:00
|
|
|
switch (type) {
|
|
|
|
case DataType::AUDIO:
|
|
|
|
port = new AlsaAudioPort (*this, name, flags);
|
|
|
|
break;
|
|
|
|
case DataType::MIDI:
|
|
|
|
port = new AlsaMidiPort (*this, name, flags);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
PBD::error << string_compose (_("%1::register_port: Invalid Data Type."), _instance_name) << endmsg;
|
|
|
|
return 0;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
2020-04-02 01:04:23 -04:00
|
|
|
|
2020-04-02 16:26:44 -04:00
|
|
|
return port;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Getting access to the data buffer for a port */
|
|
|
|
|
|
|
|
void*
|
2020-04-07 12:18:16 -04:00
|
|
|
AlsaAudioBackend::get_buffer (PortEngine::PortHandle port_handle, pframes_t nframes)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
2020-04-07 12:18:16 -04:00
|
|
|
BackendPortPtr port = boost::dynamic_pointer_cast<BackendPort> (port_handle);
|
2014-06-02 08:29:19 -04:00
|
|
|
assert (port);
|
2020-04-07 12:18:16 -04:00
|
|
|
return port->get_buffer (nframes);
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Engine Process */
|
2021-05-15 18:08:03 -04:00
|
|
|
void*
|
2014-06-02 08:29:19 -04:00
|
|
|
AlsaAudioBackend::main_process_thread ()
|
|
|
|
{
|
|
|
|
AudioEngine::thread_init_callback (this);
|
2021-05-15 18:08:03 -04:00
|
|
|
bool reset_dll = true;
|
|
|
|
int last_n_periods = 0;
|
|
|
|
_active = true;
|
|
|
|
_processed_samples = 0;
|
2014-06-02 08:29:19 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
double dll_dt = (double)_samples_per_period / (double)_samplerate;
|
2017-09-18 10:13:47 -04:00
|
|
|
double dll_w1 = 2 * M_PI * 0.1 * dll_dt;
|
|
|
|
double dll_w2 = dll_w1 * dll_w1;
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
uint64_t clock1;
|
|
|
|
int no_proc_errors = 0;
|
|
|
|
const int bailout = 5 * _samplerate / _samples_per_period;
|
2014-06-02 08:29:19 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
manager.registration_callback ();
|
|
|
|
manager.graph_order_callback ();
|
2014-06-05 21:43:39 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
const double sr_norm = 1e-6 * (double)_samplerate / (double)_samples_per_period;
|
2017-09-18 10:13:47 -04:00
|
|
|
|
2020-04-16 10:21:12 -04:00
|
|
|
/* warm up freewheel dry-run - see also AudioEngine _init_countdown */
|
|
|
|
int cnt = std::max (4, (int)(_samplerate / _samples_per_period) / 8);
|
2020-03-30 00:14:41 -04:00
|
|
|
for (int w = 0; w < cnt; ++w) {
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) {
|
2020-03-30 00:14:41 -04:00
|
|
|
memset ((*it)->get_buffer (_samples_per_period), 0, _samples_per_period * sizeof (Sample));
|
|
|
|
}
|
|
|
|
if (engine.process_callback (_samples_per_period)) {
|
|
|
|
_active = false;
|
|
|
|
return 0;
|
|
|
|
}
|
2021-05-15 18:08:03 -04:00
|
|
|
Glib::usleep (1000000 * (_samples_per_period / _samplerate));
|
2020-03-30 00:14:41 -04:00
|
|
|
}
|
|
|
|
|
2020-03-30 10:58:55 -04:00
|
|
|
_dsp_load_calc.reset ();
|
2020-03-30 00:14:41 -04:00
|
|
|
_pcmi->pcm_start ();
|
|
|
|
|
2014-06-03 20:39:39 -04:00
|
|
|
while (_run) {
|
2014-06-02 08:29:19 -04:00
|
|
|
long nr;
|
2021-05-15 18:08:03 -04:00
|
|
|
bool xrun = false;
|
2017-09-18 10:13:47 -04:00
|
|
|
bool drain_slaves = false;
|
2015-01-24 17:39:34 -05:00
|
|
|
|
|
|
|
if (_freewheeling != _freewheel) {
|
|
|
|
_freewheel = _freewheeling;
|
|
|
|
engine.freewheel_callback (_freewheel);
|
2017-09-18 10:13:47 -04:00
|
|
|
for (AudioSlaves::iterator s = _slaves.begin (); s != _slaves.end (); ++s) {
|
|
|
|
(*s)->freewheel (_freewheel);
|
|
|
|
}
|
|
|
|
if (!_freewheel) {
|
|
|
|
_pcmi->pcm_stop ();
|
|
|
|
_pcmi->pcm_start ();
|
|
|
|
drain_slaves = true;
|
2020-03-30 10:58:55 -04:00
|
|
|
_dsp_load_calc.reset ();
|
2017-09-18 10:13:47 -04:00
|
|
|
}
|
2015-01-24 17:39:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!_freewheel) {
|
2021-06-11 17:38:58 -04:00
|
|
|
dsp_stats[DeviceWait].start();
|
2014-06-02 08:29:19 -04:00
|
|
|
nr = _pcmi->pcm_wait ();
|
2021-06-11 17:38:58 -04:00
|
|
|
dsp_stats[DeviceWait].update();
|
2021-06-13 12:28:23 -04:00
|
|
|
dsp_stats[RunLoop].start ();
|
2014-06-02 08:29:19 -04:00
|
|
|
|
2017-09-18 10:13:47 -04:00
|
|
|
/* update DLL */
|
2021-05-15 18:08:03 -04:00
|
|
|
uint64_t clock0 = g_get_monotonic_time ();
|
2017-09-18 10:13:47 -04:00
|
|
|
if (reset_dll || last_n_periods != 1) {
|
2021-05-15 18:08:03 -04:00
|
|
|
reset_dll = false;
|
2017-09-18 10:13:47 -04:00
|
|
|
drain_slaves = true;
|
2021-05-15 18:08:03 -04:00
|
|
|
dll_dt = 1e6 * (double)_samples_per_period / (double)_samplerate;
|
|
|
|
_t0 = clock0;
|
|
|
|
_t1 = clock0 + dll_dt;
|
2017-09-18 10:13:47 -04:00
|
|
|
} else {
|
|
|
|
const double er = clock0 - _t1;
|
|
|
|
_t0 = _t1;
|
|
|
|
_t1 = _t1 + dll_w1 * er + dll_dt;
|
|
|
|
dll_dt += dll_w2 * er;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (AudioSlaves::iterator s = _slaves.begin (); s != _slaves.end (); ++s) {
|
|
|
|
if ((*s)->dead) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((*s)->halt) {
|
|
|
|
/* slave died, unregister its ports (not rt-safe, but no matter) */
|
|
|
|
PBD::error << _("ALSA Slave device halted") << endmsg;
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = (*s)->inputs.begin (); it != (*s)->inputs.end (); ++it) {
|
2017-09-18 10:13:47 -04:00
|
|
|
unregister_port (*it);
|
|
|
|
}
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = (*s)->outputs.begin (); it != (*s)->outputs.end (); ++it) {
|
2017-09-18 10:13:47 -04:00
|
|
|
unregister_port (*it);
|
|
|
|
}
|
|
|
|
(*s)->inputs.clear ();
|
|
|
|
(*s)->outputs.clear ();
|
|
|
|
(*s)->active = false;
|
2021-05-15 18:08:03 -04:00
|
|
|
(*s)->dead = true;
|
2017-09-18 10:13:47 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(*s)->active = (*s)->running () && (*s)->state () >= 0;
|
|
|
|
if (!(*s)->active) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(*s)->cycle_start (_t0, (_t1 - _t0) * sr_norm, drain_slaves);
|
|
|
|
}
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
if (_pcmi->state () > 0) {
|
|
|
|
++no_proc_errors;
|
|
|
|
xrun = true;
|
|
|
|
}
|
2015-01-28 07:19:03 -05:00
|
|
|
if (_pcmi->state () < 0) {
|
2014-06-02 08:29:19 -04:00
|
|
|
PBD::error << _("AlsaAudioBackend: I/O error. Audio Process Terminated.") << endmsg;
|
|
|
|
break;
|
|
|
|
}
|
2015-01-28 07:19:03 -05:00
|
|
|
if (no_proc_errors > bailout) {
|
|
|
|
PBD::error
|
2021-05-15 18:08:03 -04:00
|
|
|
<< string_compose (_("AlsaAudioBackend: Audio Process Terminated after %1 consecutive xruns."), no_proc_errors) << endmsg;
|
2015-01-28 07:19:03 -05:00
|
|
|
break;
|
|
|
|
}
|
2015-01-24 17:39:34 -05:00
|
|
|
|
2017-09-18 10:13:47 -04:00
|
|
|
last_n_periods = 0;
|
2015-01-24 17:39:34 -05:00
|
|
|
while (nr >= (long)_samples_per_period && _freewheeling == _freewheel) {
|
2021-05-15 18:08:03 -04:00
|
|
|
uint32_t i = 0;
|
|
|
|
clock1 = g_get_monotonic_time ();
|
2014-06-02 08:29:19 -04:00
|
|
|
no_proc_errors = 0;
|
|
|
|
|
|
|
|
_pcmi->capt_init (_samples_per_period);
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it, ++i) {
|
2021-05-15 18:08:03 -04:00
|
|
|
_pcmi->capt_chan (i, (float*)(*it)->get_buffer (_samples_per_period), _samples_per_period);
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
_pcmi->capt_done (_samples_per_period);
|
|
|
|
|
2017-09-18 10:13:47 -04:00
|
|
|
for (AudioSlaves::iterator s = _slaves.begin (); s != _slaves.end (); ++s) {
|
|
|
|
if (!(*s)->active) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i = 0;
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = (*s)->inputs.begin (); it != (*s)->inputs.end (); ++it, ++i) {
|
2021-05-15 18:08:03 -04:00
|
|
|
(*s)->capt_chan (i, (float*)(boost::dynamic_pointer_cast<BackendPort> (*it)->get_buffer (_samples_per_period)), _samples_per_period);
|
2017-09-18 10:13:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-14 16:07:51 -05:00
|
|
|
/* only used when adding/removing MIDI device/system ports */
|
|
|
|
pthread_mutex_lock (&_device_port_mutex);
|
2014-12-02 11:03:29 -05:00
|
|
|
/* de-queue incoming midi*/
|
2014-06-02 10:52:07 -04:00
|
|
|
i = 0;
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it, ++i) {
|
2021-05-15 18:08:03 -04:00
|
|
|
assert (_rmidi_in.size () > i);
|
|
|
|
AlsaMidiIn* rm = _rmidi_in.at (i);
|
|
|
|
void* bptr = (*it)->get_buffer (0);
|
|
|
|
pframes_t time;
|
|
|
|
uint8_t data[MaxAlsaMidiEventSize];
|
|
|
|
size_t size = sizeof (data);
|
|
|
|
midi_clear (bptr);
|
2014-06-02 10:52:07 -04:00
|
|
|
while (rm->recv_event (time, data, size)) {
|
2021-05-15 18:08:03 -04:00
|
|
|
midi_event_put (bptr, time, data, size);
|
|
|
|
size = sizeof (data);
|
2014-06-02 10:52:07 -04:00
|
|
|
}
|
|
|
|
rm->sync_time (clock1);
|
|
|
|
}
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_unlock (&_device_port_mutex);
|
2014-06-02 10:52:07 -04:00
|
|
|
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it) {
|
2014-06-02 08:29:19 -04:00
|
|
|
memset ((*it)->get_buffer (_samples_per_period), 0, _samples_per_period * sizeof (Sample));
|
|
|
|
}
|
|
|
|
|
2015-03-04 19:33:13 -05:00
|
|
|
/* call engine process callback */
|
2021-05-15 18:08:03 -04:00
|
|
|
_last_process_start = g_get_monotonic_time ();
|
2014-06-02 08:29:19 -04:00
|
|
|
if (engine.process_callback (_samples_per_period)) {
|
|
|
|
_pcmi->pcm_stop ();
|
2014-06-04 11:04:25 -04:00
|
|
|
_active = false;
|
2014-06-02 08:29:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-14 16:07:51 -05:00
|
|
|
/* only used when adding/removing MIDI device/system ports */
|
|
|
|
pthread_mutex_lock (&_device_port_mutex);
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it) {
|
2021-05-15 18:08:03 -04:00
|
|
|
boost::dynamic_pointer_cast<AlsaMidiPort> (*it)->next_period ();
|
2014-06-08 09:53:15 -04:00
|
|
|
}
|
|
|
|
|
2014-12-02 11:03:29 -05:00
|
|
|
/* queue outgoing midi */
|
2014-06-02 10:52:07 -04:00
|
|
|
i = 0;
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it, ++i) {
|
2021-05-15 18:08:03 -04:00
|
|
|
assert (_rmidi_out.size () > i);
|
|
|
|
AlsaMidiBuffer const* src = boost::dynamic_pointer_cast<const AlsaMidiPort> (*it)->const_buffer ();
|
|
|
|
AlsaMidiOut* rm = _rmidi_out.at (i);
|
2014-06-08 09:53:15 -04:00
|
|
|
rm->sync_time (clock1);
|
2014-12-30 21:12:57 -05:00
|
|
|
for (AlsaMidiBuffer::const_iterator mit = src->begin (); mit != src->end (); ++mit) {
|
2017-08-05 09:03:06 -04:00
|
|
|
rm->send_event (mit->timestamp (), mit->data (), mit->size ());
|
2014-06-02 10:52:07 -04:00
|
|
|
}
|
|
|
|
}
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_unlock (&_device_port_mutex);
|
2014-06-02 10:52:07 -04:00
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
/* write back audio */
|
|
|
|
i = 0;
|
|
|
|
_pcmi->play_init (_samples_per_period);
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it, ++i) {
|
2014-06-02 08:29:19 -04:00
|
|
|
_pcmi->play_chan (i, (const float*)(*it)->get_buffer (_samples_per_period), _samples_per_period);
|
|
|
|
}
|
|
|
|
for (; i < _pcmi->nplay (); ++i) {
|
|
|
|
_pcmi->clear_chan (i, _samples_per_period);
|
|
|
|
}
|
|
|
|
_pcmi->play_done (_samples_per_period);
|
2017-09-18 10:13:47 -04:00
|
|
|
|
|
|
|
for (AudioSlaves::iterator s = _slaves.begin (); s != _slaves.end (); ++s) {
|
|
|
|
if (!(*s)->active) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i = 0;
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = (*s)->outputs.begin (); it != (*s)->outputs.end (); ++it, ++i) {
|
2021-05-15 18:08:03 -04:00
|
|
|
(*s)->play_chan (i, (float*)(*it)->get_buffer (_samples_per_period), _samples_per_period);
|
2017-09-18 10:13:47 -04:00
|
|
|
}
|
|
|
|
(*s)->cycle_end ();
|
|
|
|
}
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
nr -= _samples_per_period;
|
|
|
|
_processed_samples += _samples_per_period;
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
_dsp_load_calc.set_max_time (_samplerate, _samples_per_period);
|
2015-09-14 06:33:57 -04:00
|
|
|
_dsp_load_calc.set_start_timestamp_us (clock1);
|
2021-05-15 18:08:03 -04:00
|
|
|
_dsp_load_calc.set_stop_timestamp_us (g_get_monotonic_time ());
|
2015-09-14 06:33:57 -04:00
|
|
|
_dsp_load = _dsp_load_calc.get_dsp_load ();
|
2017-09-18 10:13:47 -04:00
|
|
|
++last_n_periods;
|
2021-06-11 17:38:58 -04:00
|
|
|
|
2021-06-13 12:28:23 -04:00
|
|
|
dsp_stats[RunLoop].update ();
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
if (xrun && (_pcmi->capt_xrun () > 0 || _pcmi->play_xrun () > 0)) {
|
2014-06-02 08:29:19 -04:00
|
|
|
engine.Xrun ();
|
2017-09-18 10:13:47 -04:00
|
|
|
reset_dll = true;
|
2014-06-02 08:29:19 -04:00
|
|
|
#if 0
|
2021-02-26 16:22:08 -05:00
|
|
|
fprintf(stderr, "ALSA xrun read: %.2f ms, write: %.2f ms\n",
|
2014-06-02 08:29:19 -04:00
|
|
|
_pcmi->capt_xrun() * 1000.0, _pcmi->play_xrun() * 1000.0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Freewheelin'
|
2014-12-02 11:03:29 -05:00
|
|
|
|
|
|
|
// zero audio input buffers
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) {
|
2014-06-02 08:29:19 -04:00
|
|
|
memset ((*it)->get_buffer (_samples_per_period), 0, _samples_per_period * sizeof (Sample));
|
|
|
|
}
|
2014-12-02 11:03:29 -05:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
clock1 = g_get_monotonic_time ();
|
2014-12-02 11:03:29 -05:00
|
|
|
uint32_t i = 0;
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_lock (&_device_port_mutex);
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it, ++i) {
|
2021-05-15 18:08:03 -04:00
|
|
|
static_cast<AlsaMidiBuffer*> ((*it)->get_buffer (0))->clear ();
|
|
|
|
AlsaMidiIn* rm = _rmidi_in.at (i);
|
|
|
|
void* bptr = (*it)->get_buffer (0);
|
|
|
|
midi_clear (bptr); // zero midi buffer
|
2014-12-02 11:03:29 -05:00
|
|
|
|
|
|
|
// TODO add an API call for this.
|
|
|
|
pframes_t time;
|
2021-05-15 18:08:03 -04:00
|
|
|
uint8_t data[64]; // match MaxAlsaEventSize in alsa_rawmidi.cc
|
|
|
|
size_t size = sizeof (data);
|
2014-12-02 11:03:29 -05:00
|
|
|
while (rm->recv_event (time, data, size)) {
|
|
|
|
; // discard midi-data from HW.
|
|
|
|
}
|
|
|
|
rm->sync_time (clock1);
|
2014-06-02 10:52:07 -04:00
|
|
|
}
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_unlock (&_device_port_mutex);
|
2014-06-02 10:52:07 -04:00
|
|
|
|
2015-03-04 20:10:21 -05:00
|
|
|
_last_process_start = 0;
|
2014-06-02 08:29:19 -04:00
|
|
|
if (engine.process_callback (_samples_per_period)) {
|
|
|
|
_pcmi->pcm_stop ();
|
2014-12-02 11:03:29 -05:00
|
|
|
_active = false;
|
2014-06-02 08:29:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2014-12-02 11:03:29 -05:00
|
|
|
|
|
|
|
// drop all outgoing MIDI messages
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_lock (&_device_port_mutex);
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it) {
|
2021-05-15 18:08:03 -04:00
|
|
|
void* bptr = (*it)->get_buffer (0);
|
|
|
|
midi_clear (bptr);
|
2014-12-02 11:03:29 -05:00
|
|
|
}
|
2019-01-14 16:07:51 -05:00
|
|
|
pthread_mutex_unlock (&_device_port_mutex);
|
2014-12-02 11:03:29 -05:00
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
_dsp_load = 1.0;
|
2017-09-18 10:13:47 -04:00
|
|
|
reset_dll = true;
|
2014-06-02 08:29:19 -04:00
|
|
|
Glib::usleep (100); // don't hog cpu
|
|
|
|
}
|
|
|
|
|
2014-06-06 07:53:07 -04:00
|
|
|
bool connections_changed = false;
|
2021-05-15 18:08:03 -04:00
|
|
|
bool ports_changed = false;
|
2014-06-02 08:29:19 -04:00
|
|
|
if (!pthread_mutex_trylock (&_port_callback_mutex)) {
|
2021-02-04 15:04:40 -05:00
|
|
|
if (g_atomic_int_compare_and_exchange (&_port_change_flag, 1, 0)) {
|
2014-06-06 07:53:07 -04:00
|
|
|
ports_changed = true;
|
|
|
|
}
|
|
|
|
if (!_port_connection_queue.empty ()) {
|
|
|
|
connections_changed = true;
|
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
while (!_port_connection_queue.empty ()) {
|
2021-05-15 18:08:03 -04:00
|
|
|
PortConnectData* c = _port_connection_queue.back ();
|
2014-06-02 08:29:19 -04:00
|
|
|
manager.connect_callback (c->a, c->b, c->c);
|
|
|
|
_port_connection_queue.pop_back ();
|
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
pthread_mutex_unlock (&_port_callback_mutex);
|
|
|
|
}
|
2014-06-06 07:53:07 -04:00
|
|
|
if (ports_changed) {
|
2021-05-15 18:08:03 -04:00
|
|
|
manager.registration_callback ();
|
2014-06-06 07:53:07 -04:00
|
|
|
}
|
|
|
|
if (connections_changed) {
|
2021-05-15 18:08:03 -04:00
|
|
|
manager.graph_order_callback ();
|
2014-06-06 07:53:07 -04:00
|
|
|
}
|
2014-06-08 15:04:30 -04:00
|
|
|
if (connections_changed || ports_changed) {
|
2020-04-07 12:18:16 -04:00
|
|
|
update_system_port_latencies (); // flush, clear
|
2021-05-15 18:08:03 -04:00
|
|
|
engine.latency_callback (false);
|
|
|
|
engine.latency_callback (true);
|
2014-06-08 15:04:30 -04:00
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
_pcmi->pcm_stop ();
|
2014-06-03 20:39:39 -04:00
|
|
|
_active = false;
|
2014-06-04 11:04:25 -04:00
|
|
|
if (_run) {
|
2021-05-15 18:08:03 -04:00
|
|
|
engine.halted_callback ("ALSA I/O error.");
|
2014-06-04 11:04:25 -04:00
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-18 10:13:47 -04:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
bool
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaAudioBackend::add_slave (const char* device,
|
|
|
|
unsigned int slave_rate,
|
|
|
|
unsigned int slave_spp,
|
|
|
|
unsigned int slave_ppc,
|
2020-04-09 22:39:58 -04:00
|
|
|
AudioSlave::DuplexMode duplex)
|
2017-09-18 10:13:47 -04:00
|
|
|
{
|
|
|
|
AudioSlave* s = new AudioSlave (device, duplex,
|
2021-05-15 18:08:03 -04:00
|
|
|
_samplerate, _samples_per_period,
|
|
|
|
slave_rate, slave_spp, slave_ppc);
|
2017-09-18 10:13:47 -04:00
|
|
|
|
|
|
|
if (s->state ()) {
|
|
|
|
// TODO parse error status
|
|
|
|
PBD::error << string_compose (_("Failed to create slave device '%1' error %2\n"), device, s->state ()) << endmsg;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0, n = 1; i < s->ncapt (); ++i) {
|
|
|
|
char tmp[64];
|
|
|
|
do {
|
2021-05-15 18:08:03 -04:00
|
|
|
snprintf (tmp, sizeof (tmp), "extern:capture_%d", n);
|
2017-09-18 10:13:47 -04:00
|
|
|
if (find_port (tmp)) {
|
|
|
|
++n;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
2021-05-15 18:08:03 -04:00
|
|
|
PortPtr p = add_port (std::string (tmp), DataType::AUDIO, static_cast<PortFlags> (IsOutput | IsPhysical | IsTerminal));
|
|
|
|
if (!p) {
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
BackendPortPtr ap = boost::dynamic_pointer_cast<BackendPort> (p);
|
2021-01-16 11:44:05 -05:00
|
|
|
ap->set_hw_port_name (string_compose (_("Aux In %1"), n));
|
|
|
|
s->inputs.push_back (ap);
|
2017-09-18 10:13:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0, n = 1; i < s->nplay (); ++i) {
|
|
|
|
char tmp[64];
|
|
|
|
do {
|
2021-05-15 18:08:03 -04:00
|
|
|
snprintf (tmp, sizeof (tmp), "extern:playback_%d", n);
|
2017-09-18 10:13:47 -04:00
|
|
|
if (find_port (tmp)) {
|
|
|
|
++n;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (1);
|
2021-05-15 18:08:03 -04:00
|
|
|
PortPtr p = add_port (std::string (tmp), DataType::AUDIO, static_cast<PortFlags> (IsInput | IsPhysical | IsTerminal));
|
|
|
|
if (!p) {
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
BackendPortPtr ap = boost::dynamic_pointer_cast<BackendPort> (p);
|
2021-01-16 11:44:05 -05:00
|
|
|
ap->set_hw_port_name (string_compose (_("Aux Out %1"), n));
|
|
|
|
s->outputs.push_back (ap);
|
2017-09-18 10:13:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!s->start ()) {
|
|
|
|
PBD::error << string_compose (_("Failed to start slave device '%1'\n"), device) << endmsg;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
s->UpdateLatency.connect_same_thread (s->latency_connection, boost::bind (&AlsaAudioBackend::update_latencies, this));
|
|
|
|
_slaves.push_back (s);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
errout:
|
|
|
|
delete s; // releases device
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AlsaAudioBackend::AudioSlave::AudioSlave (
|
2021-05-15 18:08:03 -04:00
|
|
|
const char* device,
|
|
|
|
DuplexMode duplex,
|
|
|
|
unsigned int master_rate,
|
|
|
|
unsigned int master_samples_per_period,
|
|
|
|
unsigned int slave_rate,
|
|
|
|
unsigned int slave_samples_per_period,
|
|
|
|
unsigned int slave_periods_per_cycle)
|
2017-09-18 10:13:47 -04:00
|
|
|
: AlsaDeviceReservation (device)
|
|
|
|
, AlsaAudioSlave (
|
2020-04-09 22:39:58 -04:00
|
|
|
(duplex & HalfDuplexOut) ? device : NULL /* playback */,
|
|
|
|
(duplex & HalfDuplexIn) ? device : NULL /* capture */,
|
2017-09-18 10:13:47 -04:00
|
|
|
master_rate, master_samples_per_period,
|
2020-04-09 22:39:58 -04:00
|
|
|
slave_rate, slave_samples_per_period, slave_periods_per_cycle)
|
2017-09-18 10:13:47 -04:00
|
|
|
, active (false)
|
|
|
|
, halt (false)
|
|
|
|
, dead (false)
|
|
|
|
{
|
|
|
|
Halted.connect_same_thread (_halted_connection, boost::bind (&AudioSlave::halted, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
AlsaAudioBackend::AudioSlave::~AudioSlave ()
|
|
|
|
{
|
|
|
|
stop ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AlsaAudioBackend::AudioSlave::halted ()
|
|
|
|
{
|
|
|
|
// Note: Halted() is emitted from the Slave's process thread.
|
|
|
|
release_device ();
|
|
|
|
halt = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AlsaAudioBackend::AudioSlave::update_latencies (uint32_t play, uint32_t capt)
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
LatencyRange lr;
|
|
|
|
lr.min = lr.max = (capt);
|
2022-05-03 23:57:52 -04:00
|
|
|
bool changed = false;
|
2021-05-15 18:08:03 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = inputs.begin (); it != inputs.end (); ++it) {
|
2022-05-03 23:57:52 -04:00
|
|
|
LatencyRange lx;
|
|
|
|
lx = (*it)->latency_range (false);
|
|
|
|
if (lr == lx) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-18 10:13:47 -04:00
|
|
|
(*it)->set_latency_range (lr, false);
|
2022-05-03 23:57:52 -04:00
|
|
|
changed = true;
|
2021-05-15 18:08:03 -04:00
|
|
|
}
|
2017-09-18 10:13:47 -04:00
|
|
|
|
|
|
|
lr.min = lr.max = play;
|
2020-04-07 12:18:16 -04:00
|
|
|
for (std::vector<BackendPortPtr>::const_iterator it = outputs.begin (); it != outputs.end (); ++it) {
|
2022-05-03 23:57:52 -04:00
|
|
|
LatencyRange lx;
|
|
|
|
lx = (*it)->latency_range (true);
|
|
|
|
if (lr == lx) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-18 10:13:47 -04:00
|
|
|
(*it)->set_latency_range (lr, true);
|
2022-05-03 23:57:52 -04:00
|
|
|
changed = true;
|
2017-09-18 10:13:47 -04:00
|
|
|
}
|
2020-09-18 09:40:44 -04:00
|
|
|
#ifndef NDEBUG
|
2022-05-03 23:57:52 -04:00
|
|
|
printf ("ALSA SLAVE-device latency play=%d capt=%d changed:%d\n", play, capt, changed); // XXX DEBUG
|
2020-09-18 09:40:44 -04:00
|
|
|
#endif
|
2022-05-03 23:57:52 -04:00
|
|
|
if (changed) {
|
|
|
|
UpdateLatency (); /* EMIT SIGNAL */
|
|
|
|
}
|
2017-09-18 10:13:47 -04:00
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
static boost::shared_ptr<AlsaAudioBackend> _instance;
|
|
|
|
|
|
|
|
static boost::shared_ptr<AudioBackend> backend_factory (AudioEngine& e);
|
2021-05-15 18:08:03 -04:00
|
|
|
static int instantiate (const std::string& arg1, const std::string& /* arg2 */);
|
|
|
|
static int deinstantiate ();
|
2014-06-02 08:29:19 -04:00
|
|
|
static bool already_configured ();
|
2014-10-23 07:02:02 -04:00
|
|
|
static bool available ();
|
2014-06-02 08:29:19 -04:00
|
|
|
|
|
|
|
static ARDOUR::AudioBackendInfo _descriptor = {
|
2014-06-13 05:46:37 -04:00
|
|
|
"ALSA",
|
2014-06-02 08:29:19 -04:00
|
|
|
instantiate,
|
|
|
|
deinstantiate,
|
|
|
|
backend_factory,
|
|
|
|
already_configured,
|
2014-10-23 07:02:02 -04:00
|
|
|
available
|
2014-06-02 08:29:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static boost::shared_ptr<AudioBackend>
|
|
|
|
backend_factory (AudioEngine& e)
|
|
|
|
{
|
|
|
|
if (!_instance) {
|
|
|
|
_instance.reset (new AlsaAudioBackend (e, _descriptor));
|
|
|
|
}
|
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
instantiate (const std::string& arg1, const std::string& /* arg2 */)
|
|
|
|
{
|
|
|
|
s_instance_name = arg1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
deinstantiate ()
|
|
|
|
{
|
|
|
|
_instance.reset ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
already_configured ()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-23 07:02:02 -04:00
|
|
|
static bool
|
|
|
|
available ()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-02 08:29:19 -04:00
|
|
|
extern "C" ARDOURBACKEND_API ARDOUR::AudioBackendInfo* descriptor ()
|
|
|
|
{
|
|
|
|
return &_descriptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaAudioPort::AlsaAudioPort (AlsaAudioBackend& b, const std::string& name, PortFlags flags)
|
2020-04-02 16:26:44 -04:00
|
|
|
: BackendPort (b, name, flags)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
|
|
|
memset (_buffer, 0, sizeof (_buffer));
|
2020-04-07 12:18:16 -04:00
|
|
|
mlock (_buffer, sizeof (_buffer));
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2020-04-10 11:53:44 -04:00
|
|
|
AlsaAudioPort::~AlsaAudioPort ()
|
|
|
|
{
|
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
|
2020-04-07 12:18:16 -04:00
|
|
|
void*
|
|
|
|
AlsaAudioPort::get_buffer (pframes_t n_samples)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
|
|
|
if (is_input ()) {
|
2020-04-07 12:18:16 -04:00
|
|
|
const std::set<BackendPortPtr>& connections = get_connections ();
|
|
|
|
std::set<BackendPortPtr>::const_iterator it = connections.begin ();
|
2016-04-24 14:45:37 -04:00
|
|
|
if (it == connections.end ()) {
|
2014-06-02 08:29:19 -04:00
|
|
|
memset (_buffer, 0, n_samples * sizeof (Sample));
|
|
|
|
} else {
|
2021-05-15 18:08:03 -04:00
|
|
|
boost::shared_ptr<const AlsaAudioPort> source = boost::dynamic_pointer_cast<const AlsaAudioPort> (*it);
|
2014-06-02 08:29:19 -04:00
|
|
|
assert (source && source->is_output ());
|
|
|
|
memcpy (_buffer, source->const_buffer (), n_samples * sizeof (Sample));
|
2016-04-24 14:45:37 -04:00
|
|
|
while (++it != connections.end ()) {
|
2021-05-15 18:08:03 -04:00
|
|
|
source = boost::dynamic_pointer_cast<const AlsaAudioPort> (*it);
|
2014-06-02 08:29:19 -04:00
|
|
|
assert (source && source->is_output ());
|
2021-05-15 18:08:03 -04:00
|
|
|
Sample* dst = buffer ();
|
2014-06-02 08:29:19 -04:00
|
|
|
const Sample* src = source->const_buffer ();
|
|
|
|
for (uint32_t s = 0; s < n_samples; ++s, ++dst, ++src) {
|
|
|
|
*dst += *src;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return _buffer;
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
AlsaMidiPort::AlsaMidiPort (AlsaAudioBackend& b, const std::string& name, PortFlags flags)
|
2020-04-02 16:26:44 -04:00
|
|
|
: BackendPort (b, name, flags)
|
2014-06-22 12:20:22 -04:00
|
|
|
, _n_periods (1)
|
2014-06-08 09:53:15 -04:00
|
|
|
, _bufperiod (0)
|
2014-06-02 08:29:19 -04:00
|
|
|
{
|
2014-06-08 09:53:15 -04:00
|
|
|
_buffer[0].clear ();
|
|
|
|
_buffer[1].clear ();
|
2017-08-05 09:03:06 -04:00
|
|
|
_buffer[2].clear ();
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
_buffer[0].reserve (256);
|
|
|
|
_buffer[1].reserve (256);
|
|
|
|
_buffer[2].reserve (256);
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
2020-04-10 11:53:44 -04:00
|
|
|
AlsaMidiPort::~AlsaMidiPort ()
|
|
|
|
{
|
|
|
|
}
|
2014-06-02 08:29:19 -04:00
|
|
|
|
|
|
|
struct MidiEventSorter {
|
2021-05-15 18:08:03 -04:00
|
|
|
bool operator() (AlsaMidiEvent const& a, AlsaMidiEvent const& b)
|
|
|
|
{
|
2017-08-05 09:03:06 -04:00
|
|
|
return a < b;
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void* AlsaMidiPort::get_buffer (pframes_t /* nframes */)
|
|
|
|
{
|
|
|
|
if (is_input ()) {
|
2014-06-08 09:53:15 -04:00
|
|
|
(_buffer[_bufperiod]).clear ();
|
2020-04-07 12:18:16 -04:00
|
|
|
const std::set<BackendPortPtr>& connections = get_connections ();
|
|
|
|
for (std::set<BackendPortPtr>::const_iterator i = connections.begin ();
|
2021-05-15 18:08:03 -04:00
|
|
|
i != connections.end ();
|
|
|
|
++i) {
|
|
|
|
const AlsaMidiBuffer* src = boost::dynamic_pointer_cast<const AlsaMidiPort> (*i)->const_buffer ();
|
2014-12-30 21:12:57 -05:00
|
|
|
for (AlsaMidiBuffer::const_iterator it = src->begin (); it != src->end (); ++it) {
|
2017-08-05 09:03:06 -04:00
|
|
|
(_buffer[_bufperiod]).push_back (*it);
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
}
|
2021-05-15 18:08:03 -04:00
|
|
|
std::stable_sort ((_buffer[_bufperiod]).begin (), (_buffer[_bufperiod]).end (), MidiEventSorter ());
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
2014-06-08 09:53:15 -04:00
|
|
|
return &(_buffer[_bufperiod]);
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
AlsaMidiEvent::AlsaMidiEvent (const pframes_t timestamp, const uint8_t* data, size_t size)
|
|
|
|
: _size (size)
|
|
|
|
, _timestamp (timestamp)
|
|
|
|
{
|
2017-08-05 09:03:06 -04:00
|
|
|
if (size > 0 && size < MaxAlsaMidiEventSize) {
|
2014-06-02 08:29:19 -04:00
|
|
|
memcpy (_data, data, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AlsaMidiEvent::AlsaMidiEvent (const AlsaMidiEvent& other)
|
|
|
|
: _size (other.size ())
|
|
|
|
, _timestamp (other.timestamp ())
|
|
|
|
{
|
2017-08-05 09:03:06 -04:00
|
|
|
if (other._size > 0) {
|
|
|
|
assert (other._size < MaxAlsaMidiEventSize);
|
|
|
|
memcpy (_data, other._data, other._size);
|
2014-06-02 08:29:19 -04:00
|
|
|
}
|
|
|
|
};
|
2017-09-18 08:50:31 -04:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
AlsaDeviceReservation::AlsaDeviceReservation ()
|
|
|
|
: _device_reservation (0)
|
2021-05-15 18:08:03 -04:00
|
|
|
{
|
|
|
|
}
|
2017-09-18 08:50:31 -04:00
|
|
|
|
|
|
|
AlsaDeviceReservation::AlsaDeviceReservation (const char* device_name)
|
|
|
|
: _device_reservation (0)
|
|
|
|
{
|
2022-11-29 11:34:45 -05:00
|
|
|
acquire_device (device_name, true);
|
2017-09-18 08:50:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
AlsaDeviceReservation::~AlsaDeviceReservation ()
|
|
|
|
{
|
|
|
|
release_device ();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2022-11-29 11:34:45 -05:00
|
|
|
AlsaDeviceReservation::acquire_device (const char* device_name, bool silent)
|
2017-09-18 08:50:31 -04:00
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
int device_number = card_to_num (device_name);
|
|
|
|
if (device_number < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-18 08:50:31 -04:00
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
assert (_device_reservation == 0);
|
2017-09-18 08:50:31 -04:00
|
|
|
_reservation_succeeded = false;
|
|
|
|
|
|
|
|
std::string request_device_exe;
|
|
|
|
if (!PBD::find_file (
|
2021-05-15 18:08:03 -04:00
|
|
|
PBD::Searchpath (Glib::build_filename (ARDOUR::ardour_dll_directory (), "ardouralsautil") + G_SEARCHPATH_SEPARATOR_S + ARDOUR::ardour_dll_directory ()),
|
|
|
|
"ardour-request-device", request_device_exe)) {
|
2017-09-18 08:50:31 -04:00
|
|
|
PBD::warning << "ardour-request-device binary was not found..'" << endmsg;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
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);
|
2017-09-18 08:50:31 -04:00
|
|
|
argp[4] = 0;
|
|
|
|
|
2021-05-15 18:08:03 -04:00
|
|
|
_device_reservation = new ARDOUR::SystemExec (request_device_exe, argp);
|
|
|
|
_device_reservation->ReadStdout.connect_same_thread (_reservation_connection, boost::bind (&AlsaDeviceReservation::reservation_stdout, this, _1, _2));
|
2017-09-18 08:50:31 -04:00
|
|
|
_device_reservation->Terminated.connect_same_thread (_reservation_connection, boost::bind (&AlsaDeviceReservation::release_device, this));
|
|
|
|
|
2019-03-05 13:06:15 -05:00
|
|
|
if (_device_reservation->start (SystemExec::ShareWithParent)) {
|
2022-11-29 11:34:45 -05:00
|
|
|
if (!silent) {
|
|
|
|
PBD::warning << _("AlsaAudioBackend: Device Request failed.") << endmsg;
|
|
|
|
}
|
2021-05-15 18:08:03 -04:00
|
|
|
release_device ();
|
2017-09-18 08:50:31 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Fix various typos
Found via `codespell -q 3 -S *.po,./share/patchfiles,./libs -L ba,buss,busses,doubleclick,hsi,ontop,ro,seh,siz,sord,sur,te,trough,ue`
Follow-up to 364f2f078
2022-04-07 09:09:04 -04:00
|
|
|
/* wait to check if reservation succeeded. */
|
2017-09-18 08:50:31 -04:00
|
|
|
int timeout = 500; // 5 sec
|
|
|
|
while (_device_reservation && !_reservation_succeeded && --timeout > 0) {
|
2021-05-15 18:08:03 -04:00
|
|
|
Glib::usleep (10000);
|
2017-09-18 08:50:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout == 0 || !_reservation_succeeded) {
|
2022-11-29 11:34:45 -05:00
|
|
|
if (!silent) {
|
|
|
|
PBD::warning << _("AlsaAudioBackend: Device Reservation failed.") << endmsg;
|
|
|
|
}
|
2021-05-15 18:08:03 -04:00
|
|
|
release_device ();
|
2017-09-18 08:50:31 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AlsaDeviceReservation::release_device ()
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
_reservation_connection.drop_connections ();
|
2017-09-18 08:50:31 -04:00
|
|
|
ARDOUR::SystemExec* tmp = _device_reservation;
|
2021-05-15 18:08:03 -04:00
|
|
|
_device_reservation = 0;
|
2017-09-18 08:50:31 -04:00
|
|
|
delete tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AlsaDeviceReservation::reservation_stdout (std::string d, size_t /* s */)
|
|
|
|
{
|
2021-05-15 18:08:03 -04:00
|
|
|
if (d.substr (0, 19) == "Acquired audio-card") {
|
2017-09-18 08:50:31 -04:00
|
|
|
_reservation_succeeded = true;
|
|
|
|
}
|
|
|
|
}
|