using namespace ARDOUR;
PortAudioIO::PortAudioIO ()
- : _state (-1)
- , _initialized (false)
- , _capture_channels (0)
+ : _capture_channels (0)
, _playback_channels (0)
, _stream (0)
, _input_buffer (0)
PortAudioIO::~PortAudioIO ()
{
- if (_state == 0) {
- pcm_stop();
- }
- if (_initialized) {
- Pa_Terminate();
- }
+ close_stream();
+ pa_deinitialize ();
clear_device_lists ();
free (_input_buffer); _input_buffer = NULL;
int
PortAudioIO::available_sample_rates(int device_id, std::vector<float>& sampleRates)
{
- if (!initialize_pa()) return -1;
+ if (!pa_initialize()) return -1;
#ifdef WITH_ASIO
if (get_current_host_api_type() == paASIO) {
#ifdef WITH_ASIO
bool
PortAudioIO::get_asio_buffer_properties (int device_id,
- long& min_size_frames,
- long& max_size_frames,
- long& preferred_size_frames,
+ long& min_size_samples,
+ long& max_size_samples,
+ long& preferred_size_samples,
long& granularity)
{
// we shouldn't really need all these checks but it shouldn't hurt
}
PaError err = PaAsio_GetAvailableBufferSizes (device_id,
- &min_size_frames,
- &max_size_frames,
- &preferred_size_frames,
+ &min_size_samples,
+ &max_size_samples,
+ &preferred_size_samples,
&granularity);
if (err != paNoError) {
return true;
}
+static
bool
-PortAudioIO::get_asio_buffer_sizes (int device_id, std::vector<uint32_t>& buffer_sizes)
+is_power_of_two (uint32_t v)
{
- long min_size_frames = 0;
- long max_size_frames = 0;
- long preferred_size_frames = 0;
+ return ((v != 0) && !(v & (v - 1)));
+}
+
+bool
+PortAudioIO::get_asio_buffer_sizes(int device_id,
+ std::vector<uint32_t>& buffer_sizes,
+ bool preferred_only)
+{
+ long min_size_samples = 0;
+ long max_size_samples = 0;
+ long preferred_size_samples = 0;
long granularity = 0;
if (!get_asio_buffer_properties (device_id,
- min_size_frames,
- max_size_frames,
- preferred_size_frames,
+ min_size_samples,
+ max_size_samples,
+ preferred_size_samples,
granularity)) {
DEBUG_AUDIO (string_compose (
"Unable to get device buffer properties from device index %1\n", device_id));
}
DEBUG_AUDIO (string_compose ("ASIO buffer properties for device %1, "
- "min_size_frames: %2, max_size_frames: %3, "
- "preferred_size_frames: %4, granularity: %5\n",
+ "min_size_samples: %2, max_size_samples: %3, "
+ "preferred_size_samples: %4, granularity: %5\n",
device_id,
- min_size_frames,
- max_size_frames,
- preferred_size_frames,
+ min_size_samples,
+ max_size_samples,
+ preferred_size_samples,
granularity));
-#ifdef USE_ASIO_MIN_MAX_BUFFER_SIZES
- if (min_size_frames >= max_size_frames) {
- buffer_sizes.push_back (preferred_size_frames);
+ bool driver_returns_one_size = (min_size_samples == max_size_samples) &&
+ (min_size_samples == preferred_size_samples);
+
+ if (preferred_only || driver_returns_one_size) {
+ buffer_sizes.push_back(preferred_size_samples);
return true;
}
- long buffer_size = min_size_frames;
- while (buffer_size <= max_size_frames) {
- buffer_sizes.push_back (buffer_size);
+ long buffer_size = min_size_samples;
+
+ // If min size and granularity are power of two then just use values that
+ // are power of 2 even if the granularity allows for more values
+ bool use_power_of_two =
+ is_power_of_two(min_size_samples) && is_power_of_two(granularity);
+
+ if (granularity <= 0 || use_power_of_two) {
+ // driver uses buffer sizes that are power of 2
+ while (buffer_size <= max_size_samples) {
+ buffer_sizes.push_back(buffer_size);
+ buffer_size *= 2;
+ }
+ } else {
+ if (min_size_samples == max_size_samples) {
+ // The devices I have tested either return the same values for
+ // min/max/preferred and changing buffer size is intended to only be
+ // done via the control dialog or they return a range where min != max
+ // but I guess min == max could happen if a driver only supports a single
+ // buffer size
+ buffer_sizes.push_back(min_size_samples);
+ return true;
+ }
- if (granularity <= 0) {
- // buffer sizes are power of 2
- buffer_size = buffer_size * 2;
- } else {
+ // If min_size_samples is not power of 2 use at most 8 of the possible
+ // buffer sizes spread evenly between min and max
+ long max_values = 8;
+ while (((max_size_samples - min_size_samples) / granularity) > max_values) {
+ granularity *= 2;
+ }
+
+ while (buffer_size < max_size_samples) {
+ buffer_sizes.push_back(buffer_size);
buffer_size += granularity;
}
+ buffer_sizes.push_back(max_size_samples);
}
-#else
- buffer_sizes.push_back (preferred_size_frames);
-#endif
return true;
}
#endif
{
#ifdef WITH_ASIO
if (get_current_host_api_type() == paASIO) {
- if (get_asio_buffer_sizes (device_id, buffer_sizes)) {
+ if (get_asio_buffer_sizes (device_id, buffer_sizes, false)) {
return 0;
}
}
}
}
+bool&
+PortAudioIO::pa_initialized()
+{
+ static bool s_initialized = false;
+ return s_initialized;
+}
+
bool
-PortAudioIO::initialize_pa ()
+PortAudioIO::pa_initialize()
{
- PaError err = paNoError;
+ if (pa_initialized()) return true;
- if (!_initialized) {
- err = Pa_Initialize();
- if (err != paNoError) {
- return false;
- }
- _initialized = true;
- _host_api_index = Pa_GetDefaultHostApi ();
- _host_api_name = get_host_api_name_from_index (_host_api_index);
+ PaError err = Pa_Initialize();
+ if (err != paNoError) {
+ return false;
}
+ pa_initialized() = true;
+
+ return true;
+}
+
+bool
+PortAudioIO::pa_deinitialize()
+{
+ if (!pa_initialized()) return true;
+ PaError err = Pa_Terminate();
+ if (err != paNoError) {
+ return false;
+ }
+ pa_initialized() = false;
return true;
}
void
PortAudioIO::host_api_list (std::vector<std::string>& api_list)
{
- if (!initialize_pa()) return;
+ if (!pa_initialize()) return;
PaHostApiIndex count = Pa_GetHostApiCount();
PaHostApiIndex
PortAudioIO::get_host_api_index_from_name (const std::string& name)
{
- if (!initialize_pa()) return -1;
+ if (!pa_initialize()) return -1;
PaHostApiIndex count = Pa_GetHostApiCount();
}
}
-void
-PortAudioIO::discover()
+bool
+PortAudioIO::update_devices()
{
- DEBUG_AUDIO ("PortAudio: discover\n");
- if (!initialize_pa()) return;
+ DEBUG_AUDIO ("Update devices\n");
+ if (_stream != NULL) return false;
+ pa_deinitialize();
+ if (!pa_initialize()) return false;
clear_device_lists ();
- add_none_devices ();
+
+ // ASIO doesn't support separate input/output devices so adding None
+ // doesn't make sense
+ if (get_current_host_api_type() != paASIO) {
+ add_none_devices ();
+ }
add_devices ();
+ return true;
}
void
-PortAudioIO::pcm_stop ()
+PortAudioIO::reset_stream_dependents ()
{
- if (_stream) {
- Pa_CloseStream (_stream);
- }
- _stream = NULL;
-
_capture_channels = 0;
_playback_channels = 0;
_cur_sample_rate = 0;
_cur_input_latency = 0;
_cur_output_latency = 0;
+}
+
+PaErrorCode
+PortAudioIO::close_stream()
+{
+ if (!_stream) return paNoError;
+
+ PaError err = Pa_CloseStream (_stream);
+
+ if (err != paNoError) {
+ return (PaErrorCode)err;
+ }
+ _stream = NULL;
+
+ reset_stream_dependents();
free (_input_buffer); _input_buffer = NULL;
free (_output_buffer); _output_buffer = NULL;
- _state = -1;
+ return paNoError;
}
-int
-PortAudioIO::pcm_start()
+PaErrorCode
+PortAudioIO::start_stream()
{
PaError err = Pa_StartStream (_stream);
if (err != paNoError) {
- _state = -1;
- return -1;
+ DEBUG_AUDIO(string_compose("PortAudio failed to start stream %1\n",
+ Pa_GetErrorText(err)));
+ return (PaErrorCode)err;
}
- return 0;
+ return paNoError;
}
bool
return true;
}
-int
-PortAudioIO::pcm_setup (
- int device_input, int device_output,
- double sample_rate, uint32_t samples_per_period)
+bool
+PortAudioIO::get_input_stream_params(int device_input,
+ PaStreamParameters& inputParam) const
{
- _state = -2;
-
- PaError err = paNoError;
const PaDeviceInfo *nfo_in = NULL;
- const PaDeviceInfo *nfo_out = NULL;
-
- if (!initialize_pa()) {
- DEBUG_AUDIO ("PortAudio Initialization Failed\n");
- return -1;
- }
if (device_input == DeviceDefault) {
device_input = get_default_input_device ();
}
+
+ if (device_input == DeviceNone) {
+ return false;
+ }
+
+ nfo_in = Pa_GetDeviceInfo(device_input);
+
+ if (nfo_in == NULL) {
+ DEBUG_AUDIO ("PortAudio Cannot Query Input Device Info\n");
+ return false;
+ }
+
+ inputParam.device = device_input;
+ inputParam.channelCount = nfo_in->maxInputChannels;
+#ifdef INTERLEAVED_INPUT
+ inputParam.sampleFormat = paFloat32;
+#else
+ inputParam.sampleFormat = paFloat32 | paNonInterleaved;
+#endif
+ inputParam.suggestedLatency = nfo_in->defaultLowInputLatency;
+ inputParam.hostApiSpecificStreamInfo = NULL;
+
+ return true;
+}
+
+bool
+PortAudioIO::get_output_stream_params(int device_output,
+ PaStreamParameters& outputParam) const
+{
+ const PaDeviceInfo *nfo_out = NULL;
+
if (device_output == DeviceDefault) {
device_output = get_default_output_device ();
}
- _capture_channels = 0;
- _playback_channels = 0;
- _cur_sample_rate = 0;
- _cur_input_latency = 0;
- _cur_output_latency = 0;
+ if (device_output == DeviceNone) {
+ return false;
+ }
- DEBUG_AUDIO (string_compose (
- "PortAudio Device IDs: i:%1 o:%2\n", device_input, device_output));
+ nfo_out = Pa_GetDeviceInfo(device_output);
- if (device_input == DeviceNone && device_output == DeviceNone) {
- // just send the error msg for now rather than return it
- error << AudioBackend::get_error_string(AudioBackend::DeviceConfigurationNotSupportedError)
- << endmsg;
- return -1;
+ if (nfo_out == NULL) {
+ DEBUG_AUDIO ("PortAudio Cannot Query Output Device Info\n");
+ return false;
}
- if (device_input != DeviceNone) {
- nfo_in = Pa_GetDeviceInfo(device_input);
- }
+ outputParam.device = device_output;
+ outputParam.channelCount = nfo_out->maxOutputChannels;
+#ifdef INTERLEAVED_OUTPUT
+ outputParam.sampleFormat = paFloat32;
+#else
+ outputParam.sampleFormat = paFloat32 | paNonInterleaved;
+#endif
+ outputParam.suggestedLatency = nfo_out->defaultLowOutputLatency;
+ outputParam.hostApiSpecificStreamInfo = NULL;
+
+ return true;
+}
- if (device_output != DeviceNone) {
- nfo_out = Pa_GetDeviceInfo(device_output);
+PaErrorCode
+PortAudioIO::pre_stream_open(int device_input,
+ PaStreamParameters& inputParam,
+ int device_output,
+ PaStreamParameters& outputParam)
+{
+ if (!pa_initialize()) {
+ DEBUG_AUDIO ("PortAudio Initialization Failed\n");
+ return paNotInitialized;
}
- if (!nfo_in && !nfo_out) {
- DEBUG_AUDIO ("PortAudio Cannot Query Device Info\n");
- return -1;
+ reset_stream_dependents ();
+
+ DEBUG_AUDIO (string_compose (
+ "PortAudio Device IDs: i:%1 o:%2\n", device_input, device_output));
+
+ if (device_input == DeviceNone && device_output == DeviceNone) {
+ return paBadIODeviceCombination;
}
- if (nfo_in) {
- _capture_channels = nfo_in->maxInputChannels;
+ if (get_input_stream_params(device_input, inputParam)) {
+ _capture_channels = inputParam.channelCount;
}
- if (nfo_out) {
- _playback_channels = nfo_out->maxOutputChannels;
+
+ if (get_output_stream_params(device_output, outputParam)) {
+ _playback_channels = outputParam.channelCount;
}
- if(_capture_channels == 0 && _playback_channels == 0) {
- DEBUG_AUDIO ("PortAudio no input or output channels.\n");
- return -1;
+ if (_capture_channels == 0 && _playback_channels == 0) {
+ DEBUG_AUDIO("PortAudio no input or output channels.\n");
+ return paBadIODeviceCombination;
}
DEBUG_AUDIO (string_compose ("PortAudio Channels: in:%1 out:%2\n",
_capture_channels,
_playback_channels));
+ return paNoError;
+}
+
+PaErrorCode
+PortAudioIO::open_callback_stream(int device_input,
+ int device_output,
+ double sample_rate,
+ uint32_t samples_per_period,
+ PaStreamCallback* callback,
+ void* data)
+{
PaStreamParameters inputParam;
PaStreamParameters outputParam;
- if (nfo_in) {
- inputParam.device = device_input;
- inputParam.channelCount = _capture_channels;
-#ifdef INTERLEAVED_INPUT
- inputParam.sampleFormat = paFloat32;
-#else
- inputParam.sampleFormat = paFloat32 | paNonInterleaved;
-#endif
- inputParam.suggestedLatency = nfo_in->defaultLowInputLatency;
- inputParam.hostApiSpecificStreamInfo = NULL;
+ PaErrorCode error_code =
+ pre_stream_open(device_input, inputParam, device_output, outputParam);
+
+ if (error_code != paNoError) return error_code;
+
+ PaError err = paNoError;
+
+ DEBUG_AUDIO ("Open Callback Stream\n");
+
+ err = Pa_OpenStream(&_stream,
+ _capture_channels > 0 ? &inputParam : NULL,
+ _playback_channels > 0 ? &outputParam : NULL,
+ sample_rate,
+ samples_per_period,
+ paDitherOff,
+ callback,
+ data);
+
+ if (err != paNoError) {
+ DEBUG_AUDIO(string_compose("PortAudio failed to open stream %1\n",
+ Pa_GetErrorText(err)));
+ return paInternalError;
}
- if (nfo_out) {
- outputParam.device = device_output;
- outputParam.channelCount = _playback_channels;
-#ifdef INTERLEAVED_OUTPUT
- outputParam.sampleFormat = paFloat32;
-#else
- outputParam.sampleFormat = paFloat32 | paNonInterleaved;
-#endif
- outputParam.suggestedLatency = nfo_out->defaultLowOutputLatency;
- outputParam.hostApiSpecificStreamInfo = NULL;
+ if (!set_sample_rate_and_latency_from_stream()) {
+ DEBUG_AUDIO ("PortAudio failed to query stream information.\n");
+ close_stream();
+ return paInternalError;
}
- // XXX re-consider using callback API, testing needed.
+ return paNoError;
+}
+
+PaErrorCode
+PortAudioIO::open_blocking_stream(int device_input,
+ int device_output,
+ double sample_rate,
+ uint32_t samples_per_period)
+{
+ PaStreamParameters inputParam;
+ PaStreamParameters outputParam;
+
+ PaErrorCode error_code =
+ pre_stream_open(device_input, inputParam, device_output, outputParam);
+
+ if (error_code != paNoError) return error_code;
+
+ PaError err = paNoError;
+
err = Pa_OpenStream (
&_stream,
_capture_channels > 0 ? &inputParam: NULL,
NULL, NULL);
if (err != paNoError) {
- DEBUG_AUDIO ("PortAudio failed to start stream.\n");
- return -1;
+ DEBUG_AUDIO(string_compose("PortAudio failed to open stream %1\n",
+ Pa_GetErrorText(err)));
+ return (PaErrorCode)err;
}
if (!set_sample_rate_and_latency_from_stream()) {
DEBUG_AUDIO ("PortAudio failed to query stream information.\n");
- pcm_stop();
- return -1;
+ close_stream();
+ return paInternalError;
}
- _state = 0;
-
if (!allocate_buffers_for_blocking_api(samples_per_period)) {
- pcm_stop();
- return -1;
+ close_stream();
+ return paInternalError;
}
- return 0;
+ return paNoError;
}
int