2015-03-12 23:05:53 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Robin Gareus <robin@gareus.org>
|
2015-05-17 06:55:04 -04:00
|
|
|
* Copyright (C) 2015 Tim Mayberry <mojofunk@gmail.com>
|
2015-03-12 23:05:53 -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.
|
|
|
|
*
|
|
|
|
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2015-07-08 05:33:37 -04:00
|
|
|
#include <glibmm.h>
|
2015-08-22 07:04:53 -04:00
|
|
|
|
2015-03-12 23:05:53 -04:00
|
|
|
#include "portaudio_io.h"
|
|
|
|
|
2015-07-20 07:44:46 -04:00
|
|
|
#ifdef WITH_ASIO
|
|
|
|
#include "pa_asio.h"
|
|
|
|
#endif
|
|
|
|
|
2015-05-17 06:55:04 -04:00
|
|
|
#include "pbd/compose.h"
|
|
|
|
|
2015-08-22 06:22:01 -04:00
|
|
|
#include "ardour/audio_backend.h"
|
|
|
|
|
2015-05-17 06:55:04 -04:00
|
|
|
#include "debug.h"
|
|
|
|
|
2015-03-20 06:45:56 -04:00
|
|
|
#define INTERLEAVED_INPUT
|
|
|
|
#define INTERLEAVED_OUTPUT
|
|
|
|
|
2015-08-22 07:43:12 -04:00
|
|
|
using namespace PBD;
|
2015-03-12 23:05:53 -04:00
|
|
|
using namespace ARDOUR;
|
|
|
|
|
|
|
|
PortAudioIO::PortAudioIO ()
|
2015-08-23 22:57:34 -04:00
|
|
|
: _capture_channels (0)
|
2015-03-12 23:05:53 -04:00
|
|
|
, _playback_channels (0)
|
|
|
|
, _stream (0)
|
|
|
|
, _input_buffer (0)
|
|
|
|
, _output_buffer (0)
|
|
|
|
, _cur_sample_rate (0)
|
|
|
|
, _cur_input_latency (0)
|
|
|
|
, _cur_output_latency (0)
|
2015-07-28 23:28:17 -04:00
|
|
|
, _host_api_index(-1)
|
2015-03-12 23:05:53 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PortAudioIO::~PortAudioIO ()
|
|
|
|
{
|
2015-08-24 06:31:47 -04:00
|
|
|
close_stream();
|
2015-08-23 22:15:06 -04:00
|
|
|
|
2015-08-23 22:57:34 -04:00
|
|
|
pa_deinitialize ();
|
2015-05-08 21:42:46 -04:00
|
|
|
clear_device_lists ();
|
2015-03-12 23:05:53 -04:00
|
|
|
|
|
|
|
free (_input_buffer); _input_buffer = NULL;
|
|
|
|
free (_output_buffer); _output_buffer = NULL;
|
|
|
|
}
|
|
|
|
|
2015-07-20 07:44:46 -04:00
|
|
|
std::string
|
|
|
|
PortAudioIO::control_app_name (int device_id) const
|
|
|
|
{
|
|
|
|
#ifdef WITH_ASIO
|
2015-08-22 07:21:41 -04:00
|
|
|
if (get_current_host_api_type() == paASIO) {
|
2015-07-20 07:44:46 -04:00
|
|
|
// is this used for anything, or just acts as a boolean?
|
|
|
|
return "PortaudioASIO";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-08-22 07:21:41 -04:00
|
|
|
return std::string();
|
2015-07-20 07:44:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PortAudioIO::launch_control_app (int device_id)
|
|
|
|
{
|
|
|
|
#ifdef WITH_ASIO
|
|
|
|
PaError err = PaAsio_ShowControlPanel (device_id, NULL);
|
|
|
|
|
|
|
|
if (err != paNoError) {
|
|
|
|
// error << ?
|
|
|
|
DEBUG_AUDIO (string_compose (
|
|
|
|
"Unable to show control panel for device with index %1\n", device_id));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2015-03-12 23:05:53 -04:00
|
|
|
|
2015-08-22 23:12:26 -04:00
|
|
|
void
|
|
|
|
PortAudioIO::get_default_sample_rates (std::vector<float>& rates)
|
|
|
|
{
|
|
|
|
rates.push_back(8000.0);
|
|
|
|
rates.push_back(22050.0);
|
|
|
|
rates.push_back(24000.0);
|
|
|
|
rates.push_back(44100.0);
|
|
|
|
rates.push_back(48000.0);
|
|
|
|
rates.push_back(88200.0);
|
|
|
|
rates.push_back(96000.0);
|
|
|
|
rates.push_back(176400.0);
|
|
|
|
rates.push_back(192000.0);
|
|
|
|
}
|
|
|
|
|
2015-03-12 23:05:53 -04:00
|
|
|
int
|
|
|
|
PortAudioIO::available_sample_rates(int device_id, std::vector<float>& sampleRates)
|
|
|
|
{
|
2015-08-23 22:57:34 -04:00
|
|
|
if (!pa_initialize()) return -1;
|
2015-03-12 23:05:53 -04:00
|
|
|
|
2015-08-22 23:53:07 -04:00
|
|
|
#ifdef WITH_ASIO
|
|
|
|
if (get_current_host_api_type() == paASIO) {
|
|
|
|
get_default_sample_rates(sampleRates);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-12 23:05:53 -04:00
|
|
|
// TODO use separate int device_input, int device_output ?!
|
2015-08-21 08:12:07 -04:00
|
|
|
if (device_id == DeviceDefault) {
|
2015-04-01 08:56:45 -04:00
|
|
|
device_id = get_default_input_device ();
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
2015-05-17 06:55:04 -04:00
|
|
|
|
|
|
|
DEBUG_AUDIO (
|
|
|
|
string_compose ("Querying Samplerates for device %1\n", device_id));
|
2015-03-12 23:05:53 -04:00
|
|
|
|
|
|
|
sampleRates.clear();
|
|
|
|
const PaDeviceInfo* nfo = Pa_GetDeviceInfo(device_id);
|
|
|
|
|
2015-04-14 21:04:19 -04:00
|
|
|
if (nfo) {
|
|
|
|
PaStreamParameters inputParam;
|
|
|
|
PaStreamParameters outputParam;
|
|
|
|
|
|
|
|
inputParam.device = device_id;
|
|
|
|
inputParam.channelCount = nfo->maxInputChannels;
|
|
|
|
inputParam.sampleFormat = paFloat32;
|
|
|
|
inputParam.suggestedLatency = 0;
|
|
|
|
inputParam.hostApiSpecificStreamInfo = 0;
|
|
|
|
|
|
|
|
outputParam.device = device_id;
|
|
|
|
outputParam.channelCount = nfo->maxOutputChannels;
|
|
|
|
outputParam.sampleFormat = paFloat32;
|
|
|
|
outputParam.suggestedLatency = 0;
|
|
|
|
outputParam.hostApiSpecificStreamInfo = 0;
|
|
|
|
|
2015-08-22 23:12:26 -04:00
|
|
|
std::vector<float> rates;
|
|
|
|
get_default_sample_rates(rates);
|
|
|
|
|
|
|
|
for (std::vector<float>::const_iterator i = rates.begin(); i != rates.end();
|
|
|
|
++i) {
|
|
|
|
if (paFormatIsSupported ==
|
|
|
|
Pa_IsFormatSupported(nfo->maxInputChannels > 0 ? &inputParam : NULL,
|
|
|
|
nfo->maxOutputChannels > 0 ? &outputParam : NULL,
|
|
|
|
*i)) {
|
|
|
|
sampleRates.push_back(*i);
|
2015-04-14 21:04:19 -04:00
|
|
|
}
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sampleRates.empty()) {
|
2015-04-14 21:04:19 -04:00
|
|
|
// fill in something..
|
2015-08-22 23:12:26 -04:00
|
|
|
get_default_sample_rates(sampleRates);
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-03 22:02:06 -04:00
|
|
|
#ifdef WITH_ASIO
|
|
|
|
bool
|
2015-08-03 22:25:46 -04:00
|
|
|
PortAudioIO::get_asio_buffer_properties (int device_id,
|
|
|
|
long& min_size_frames,
|
|
|
|
long& max_size_frames,
|
|
|
|
long& preferred_size_frames,
|
|
|
|
long& granularity)
|
2015-08-03 22:02:06 -04:00
|
|
|
{
|
|
|
|
// we shouldn't really need all these checks but it shouldn't hurt
|
|
|
|
const PaDeviceInfo* device_info = Pa_GetDeviceInfo(device_id);
|
|
|
|
|
|
|
|
if (!device_info) {
|
|
|
|
DEBUG_AUDIO (string_compose (
|
|
|
|
"Unable to get device info from device index %1\n", device_id));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-22 07:21:41 -04:00
|
|
|
if (get_current_host_api_type() != paASIO) {
|
2015-08-03 22:02:06 -04:00
|
|
|
DEBUG_AUDIO (string_compose (
|
|
|
|
"ERROR device_id %1 is not an ASIO device\n", device_id));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-03 22:25:46 -04:00
|
|
|
PaError err = PaAsio_GetAvailableBufferSizes (device_id,
|
|
|
|
&min_size_frames,
|
|
|
|
&max_size_frames,
|
|
|
|
&preferred_size_frames,
|
|
|
|
&granularity);
|
2015-08-03 22:02:06 -04:00
|
|
|
|
|
|
|
if (err != paNoError) {
|
|
|
|
DEBUG_AUDIO (string_compose (
|
|
|
|
"Unable to determine available buffer sizes for device %1\n", device_id));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-03 22:25:46 -04:00
|
|
|
|
2015-11-02 00:22:43 -05:00
|
|
|
static
|
2015-08-03 22:25:46 -04:00
|
|
|
bool
|
2015-11-02 00:22:43 -05:00
|
|
|
is_power_of_two (uint32_t v)
|
|
|
|
{
|
|
|
|
return ((v != 0) && !(v & (v - 1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PortAudioIO::get_asio_buffer_sizes(int device_id,
|
|
|
|
std::vector<uint32_t>& buffer_sizes,
|
|
|
|
bool preferred_only)
|
2015-08-03 22:25:46 -04:00
|
|
|
{
|
2015-08-03 23:36:29 -04:00
|
|
|
long min_size_frames = 0;
|
|
|
|
long max_size_frames = 0;
|
|
|
|
long preferred_size_frames = 0;
|
|
|
|
long granularity = 0;
|
2015-08-03 22:25:46 -04:00
|
|
|
|
|
|
|
if (!get_asio_buffer_properties (device_id,
|
|
|
|
min_size_frames,
|
|
|
|
max_size_frames,
|
|
|
|
preferred_size_frames,
|
|
|
|
granularity)) {
|
|
|
|
DEBUG_AUDIO (string_compose (
|
|
|
|
"Unable to get device buffer properties from device index %1\n", device_id));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-03 23:36:29 -04:00
|
|
|
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",
|
|
|
|
device_id,
|
|
|
|
min_size_frames,
|
|
|
|
max_size_frames,
|
|
|
|
preferred_size_frames,
|
|
|
|
granularity));
|
|
|
|
|
2015-11-02 00:22:43 -05:00
|
|
|
bool driver_returns_one_size = (min_size_frames == max_size_frames) &&
|
|
|
|
(min_size_frames == preferred_size_frames);
|
|
|
|
|
|
|
|
if (preferred_only || driver_returns_one_size) {
|
|
|
|
buffer_sizes.push_back(preferred_size_frames);
|
2015-08-03 23:36:29 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
long buffer_size = min_size_frames;
|
|
|
|
|
2015-11-02 00:22:43 -05:00
|
|
|
// 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_frames) && 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_frames) {
|
|
|
|
buffer_sizes.push_back(buffer_size);
|
|
|
|
buffer_size *= 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (min_size_frames == max_size_frames) {
|
|
|
|
// 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_frames);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If min_size_frames 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_frames - min_size_frames) / granularity) > max_values) {
|
|
|
|
granularity *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (buffer_size < max_size_frames) {
|
|
|
|
buffer_sizes.push_back(buffer_size);
|
2015-08-03 23:36:29 -04:00
|
|
|
buffer_size += granularity;
|
|
|
|
}
|
2015-11-02 00:22:43 -05:00
|
|
|
buffer_sizes.push_back(max_size_frames);
|
2015-08-03 23:36:29 -04:00
|
|
|
}
|
|
|
|
return true;
|
2015-08-03 22:25:46 -04:00
|
|
|
}
|
2015-08-03 22:02:06 -04:00
|
|
|
#endif
|
|
|
|
|
2015-08-22 23:12:26 -04:00
|
|
|
void
|
|
|
|
PortAudioIO::get_default_buffer_sizes(std::vector<uint32_t>& buffer_sizes)
|
2015-03-12 23:05:53 -04:00
|
|
|
{
|
2015-08-22 23:12:26 -04:00
|
|
|
buffer_sizes.push_back(64);
|
|
|
|
buffer_sizes.push_back(128);
|
|
|
|
buffer_sizes.push_back(256);
|
|
|
|
buffer_sizes.push_back(512);
|
|
|
|
buffer_sizes.push_back(1024);
|
|
|
|
buffer_sizes.push_back(2048);
|
|
|
|
buffer_sizes.push_back(4096);
|
2015-08-03 22:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PortAudioIO::available_buffer_sizes(int device_id, std::vector<uint32_t>& buffer_sizes)
|
|
|
|
{
|
|
|
|
#ifdef WITH_ASIO
|
2015-08-22 07:21:41 -04:00
|
|
|
if (get_current_host_api_type() == paASIO) {
|
2015-11-02 00:22:43 -05:00
|
|
|
if (get_asio_buffer_sizes (device_id, buffer_sizes, false)) {
|
2015-08-03 22:02:06 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
2015-08-03 22:02:06 -04:00
|
|
|
#endif
|
|
|
|
|
2015-08-22 23:12:26 -04:00
|
|
|
get_default_buffer_sizes (buffer_sizes);
|
2015-08-03 22:02:06 -04:00
|
|
|
|
2015-03-12 23:05:53 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-05-08 21:42:46 -04:00
|
|
|
PortAudioIO::input_device_list(std::map<int, std::string> &devices) const
|
|
|
|
{
|
|
|
|
for (std::map<int, paDevice*>::const_iterator i = _input_devices.begin ();
|
|
|
|
i != _input_devices.end ();
|
|
|
|
++i) {
|
|
|
|
devices.insert (std::pair<int, std::string>(i->first, Glib::locale_to_utf8(i->second->name)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PortAudioIO::output_device_list(std::map<int, std::string> &devices) const
|
|
|
|
{
|
|
|
|
for (std::map<int, paDevice*>::const_iterator i = _output_devices.begin ();
|
|
|
|
i != _output_devices.end ();
|
|
|
|
++i) {
|
|
|
|
devices.insert (std::pair<int, std::string>(i->first, Glib::locale_to_utf8(i->second->name)));
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 22:57:34 -04:00
|
|
|
bool&
|
|
|
|
PortAudioIO::pa_initialized()
|
|
|
|
{
|
|
|
|
static bool s_initialized = false;
|
|
|
|
return s_initialized;
|
|
|
|
}
|
|
|
|
|
2015-04-01 02:55:34 -04:00
|
|
|
bool
|
2015-08-23 22:57:34 -04:00
|
|
|
PortAudioIO::pa_initialize()
|
2015-03-12 23:05:53 -04:00
|
|
|
{
|
2015-08-23 22:57:34 -04:00
|
|
|
if (pa_initialized()) return true;
|
2015-03-12 23:05:53 -04:00
|
|
|
|
2015-08-23 22:57:34 -04:00
|
|
|
PaError err = Pa_Initialize();
|
|
|
|
if (err != paNoError) {
|
|
|
|
return false;
|
2015-04-01 02:55:34 -04:00
|
|
|
}
|
2015-08-23 22:57:34 -04:00
|
|
|
pa_initialized() = true;
|
2015-04-01 02:55:34 -04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-23 22:57:34 -04:00
|
|
|
bool
|
|
|
|
PortAudioIO::pa_deinitialize()
|
|
|
|
{
|
|
|
|
if (!pa_initialized()) return true;
|
|
|
|
|
|
|
|
PaError err = Pa_Terminate();
|
|
|
|
if (err != paNoError) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pa_initialized() = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-01 02:55:34 -04:00
|
|
|
void
|
|
|
|
PortAudioIO::host_api_list (std::vector<std::string>& api_list)
|
|
|
|
{
|
2015-08-23 22:57:34 -04:00
|
|
|
if (!pa_initialize()) return;
|
2015-04-01 02:55:34 -04:00
|
|
|
|
|
|
|
PaHostApiIndex count = Pa_GetHostApiCount();
|
|
|
|
|
|
|
|
if (count < 0) return;
|
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
const PaHostApiInfo* info = Pa_GetHostApiInfo (i);
|
|
|
|
if (info->name != NULL) { // possible?
|
|
|
|
api_list.push_back (info->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-22 07:16:17 -04:00
|
|
|
|
|
|
|
PaHostApiTypeId
|
|
|
|
PortAudioIO::get_current_host_api_type () const
|
|
|
|
{
|
|
|
|
const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
|
|
|
|
|
|
|
|
if (info == NULL) {
|
|
|
|
DEBUG_AUDIO(string_compose(
|
|
|
|
"Unable to determine Host API type from index %1\n", _host_api_index));
|
|
|
|
return (PaHostApiTypeId)0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return info->type;
|
|
|
|
}
|
|
|
|
|
2015-08-06 08:22:18 -04:00
|
|
|
std::string
|
|
|
|
PortAudioIO::get_host_api_name_from_index (PaHostApiIndex index)
|
|
|
|
{
|
|
|
|
std::vector<std::string> api_list;
|
|
|
|
host_api_list(api_list);
|
|
|
|
return api_list[index];
|
|
|
|
}
|
|
|
|
|
2015-07-28 23:28:17 -04:00
|
|
|
bool
|
2015-04-01 08:56:45 -04:00
|
|
|
PortAudioIO::set_host_api (const std::string& host_api_name)
|
|
|
|
{
|
2015-07-28 23:28:17 -04:00
|
|
|
PaHostApiIndex new_index = get_host_api_index_from_name (host_api_name);
|
2015-04-01 08:56:45 -04:00
|
|
|
|
2015-07-28 23:28:17 -04:00
|
|
|
if (new_index < 0) {
|
|
|
|
DEBUG_AUDIO ("Portaudio: Error setting host API\n");
|
|
|
|
return false;
|
2015-04-01 08:56:45 -04:00
|
|
|
}
|
2015-07-28 23:28:17 -04:00
|
|
|
_host_api_index = new_index;
|
|
|
|
_host_api_name = host_api_name;
|
|
|
|
return true;
|
2015-04-01 08:56:45 -04:00
|
|
|
}
|
|
|
|
|
2015-04-01 02:55:34 -04:00
|
|
|
PaHostApiIndex
|
|
|
|
PortAudioIO::get_host_api_index_from_name (const std::string& name)
|
|
|
|
{
|
2015-08-23 22:57:34 -04:00
|
|
|
if (!pa_initialize()) return -1;
|
2015-04-01 02:55:34 -04:00
|
|
|
|
|
|
|
PaHostApiIndex count = Pa_GetHostApiCount();
|
|
|
|
|
2015-05-17 06:55:04 -04:00
|
|
|
if (count < 0) {
|
|
|
|
DEBUG_AUDIO ("Host API count < 0\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2015-04-01 02:55:34 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
const PaHostApiInfo* info = Pa_GetHostApiInfo (i);
|
2015-07-20 07:44:46 -04:00
|
|
|
if (info != NULL && info->name != NULL) { // possible?
|
|
|
|
if (name == info->name) {
|
|
|
|
return i;
|
|
|
|
}
|
2015-04-01 02:55:34 -04:00
|
|
|
}
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
2015-05-17 06:55:04 -04:00
|
|
|
DEBUG_AUDIO (string_compose ("Unable to get host API from name: %1\n", name));
|
|
|
|
|
2015-04-01 02:55:34 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-01 08:56:45 -04:00
|
|
|
PaDeviceIndex
|
2015-08-23 18:59:58 -04:00
|
|
|
PortAudioIO::get_default_input_device () const
|
2015-04-01 08:56:45 -04:00
|
|
|
{
|
|
|
|
const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
|
|
|
|
if (info == NULL) return -1;
|
|
|
|
return info->defaultInputDevice;
|
|
|
|
}
|
|
|
|
|
|
|
|
PaDeviceIndex
|
2015-08-23 18:59:58 -04:00
|
|
|
PortAudioIO::get_default_output_device () const
|
2015-04-01 08:56:45 -04:00
|
|
|
{
|
|
|
|
const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
|
|
|
|
if (info == NULL) return -1;
|
|
|
|
return info->defaultOutputDevice;
|
|
|
|
}
|
|
|
|
|
2015-04-01 02:55:34 -04:00
|
|
|
void
|
2015-05-08 21:42:46 -04:00
|
|
|
PortAudioIO::clear_device_lists ()
|
2015-04-01 02:55:34 -04:00
|
|
|
{
|
2015-05-08 21:42:46 -04:00
|
|
|
for (std::map<int, paDevice*>::const_iterator i = _input_devices.begin (); i != _input_devices.end(); ++i) {
|
|
|
|
delete i->second;
|
|
|
|
}
|
|
|
|
_input_devices.clear();
|
|
|
|
|
|
|
|
for (std::map<int, paDevice*>::const_iterator i = _output_devices.begin (); i != _output_devices.end(); ++i) {
|
2015-04-01 02:55:34 -04:00
|
|
|
delete i->second;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
2015-05-08 21:42:46 -04:00
|
|
|
_output_devices.clear();
|
2015-05-03 23:28:41 -04:00
|
|
|
}
|
2015-03-12 23:05:53 -04:00
|
|
|
|
2015-08-22 07:04:53 -04:00
|
|
|
void
|
|
|
|
PortAudioIO::add_none_devices ()
|
|
|
|
{
|
|
|
|
_input_devices.insert(std::pair<int, paDevice*>(
|
|
|
|
DeviceNone, new paDevice(AudioBackend::get_standard_device_name(AudioBackend::DeviceNone), 0, 0)));
|
|
|
|
_output_devices.insert(std::pair<int, paDevice*>(
|
|
|
|
DeviceNone, new paDevice(AudioBackend::get_standard_device_name(AudioBackend::DeviceNone), 0, 0)));
|
|
|
|
}
|
|
|
|
|
2015-05-03 23:28:41 -04:00
|
|
|
void
|
2015-05-08 21:42:46 -04:00
|
|
|
PortAudioIO::add_default_devices ()
|
2015-05-03 23:28:41 -04:00
|
|
|
{
|
2015-04-01 08:56:45 -04:00
|
|
|
const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
|
2015-04-01 02:55:34 -04:00
|
|
|
if (info == NULL) return;
|
2015-03-12 23:05:53 -04:00
|
|
|
|
2015-05-03 23:28:41 -04:00
|
|
|
const PaDeviceInfo* nfo_i = Pa_GetDeviceInfo(get_default_input_device());
|
|
|
|
const PaDeviceInfo* nfo_o = Pa_GetDeviceInfo(get_default_output_device());
|
|
|
|
if (nfo_i && nfo_o) {
|
2015-08-21 08:12:07 -04:00
|
|
|
_input_devices.insert (std::pair<int, paDevice*> (DeviceDefault,
|
2015-08-22 06:22:01 -04:00
|
|
|
new paDevice(AudioBackend::get_standard_device_name(AudioBackend::DeviceDefault),
|
2015-05-08 21:42:46 -04:00
|
|
|
nfo_i->maxInputChannels,
|
|
|
|
nfo_o->maxOutputChannels
|
|
|
|
)));
|
2015-08-21 08:12:07 -04:00
|
|
|
_output_devices.insert (std::pair<int, paDevice*> (DeviceDefault,
|
2015-08-22 06:22:01 -04:00
|
|
|
new paDevice(AudioBackend::get_standard_device_name(AudioBackend::DeviceDefault),
|
2015-05-03 23:28:41 -04:00
|
|
|
nfo_i->maxInputChannels,
|
|
|
|
nfo_o->maxOutputChannels
|
|
|
|
)));
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
2015-05-03 23:28:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
PortAudioIO::add_devices ()
|
|
|
|
{
|
|
|
|
const PaHostApiInfo* info = Pa_GetHostApiInfo (_host_api_index);
|
|
|
|
if (info == NULL) return;
|
2015-03-12 23:05:53 -04:00
|
|
|
|
|
|
|
int n_devices = Pa_GetDeviceCount();
|
2015-05-17 06:55:04 -04:00
|
|
|
|
|
|
|
DEBUG_AUDIO (string_compose ("PortAudio found %1 devices\n", n_devices));
|
2015-03-12 23:05:53 -04:00
|
|
|
|
|
|
|
for (int i = 0 ; i < n_devices; ++i) {
|
|
|
|
const PaDeviceInfo* nfo = Pa_GetDeviceInfo(i);
|
2015-04-01 02:55:34 -04:00
|
|
|
|
2015-03-12 23:05:53 -04:00
|
|
|
if (!nfo) continue;
|
2015-04-01 08:56:45 -04:00
|
|
|
if (nfo->hostApi != _host_api_index) continue;
|
2015-05-17 06:55:04 -04:00
|
|
|
|
|
|
|
DEBUG_AUDIO (string_compose (" (%1) '%2' '%3' in: %4 (lat: %5 .. %6) out: %7 "
|
|
|
|
"(lat: %8 .. %9) sr:%10\n",
|
|
|
|
i,
|
|
|
|
info->name,
|
|
|
|
nfo->name,
|
|
|
|
nfo->maxInputChannels,
|
|
|
|
nfo->defaultLowInputLatency * 1e3,
|
|
|
|
nfo->defaultHighInputLatency * 1e3,
|
|
|
|
nfo->maxOutputChannels,
|
|
|
|
nfo->defaultLowOutputLatency * 1e3,
|
|
|
|
nfo->defaultHighOutputLatency * 1e3,
|
|
|
|
nfo->defaultSampleRate));
|
|
|
|
|
2015-03-12 23:05:53 -04:00
|
|
|
if ( nfo->maxInputChannels == 0 && nfo->maxOutputChannels == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-05-03 23:28:41 -04:00
|
|
|
|
2015-05-08 21:42:46 -04:00
|
|
|
if (nfo->maxInputChannels > 0) {
|
|
|
|
_input_devices.insert (std::pair<int, paDevice*> (i, new paDevice(
|
|
|
|
nfo->name,
|
|
|
|
nfo->maxInputChannels,
|
|
|
|
nfo->maxOutputChannels
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
if (nfo->maxOutputChannels > 0) {
|
|
|
|
_output_devices.insert (std::pair<int, paDevice*> (i, new paDevice(
|
|
|
|
nfo->name,
|
|
|
|
nfo->maxInputChannels,
|
|
|
|
nfo->maxOutputChannels
|
|
|
|
)));
|
|
|
|
}
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-25 03:23:07 -04:00
|
|
|
bool
|
2015-08-24 19:10:02 -04:00
|
|
|
PortAudioIO::update_devices()
|
2015-05-03 23:28:41 -04:00
|
|
|
{
|
2015-08-24 19:10:02 -04:00
|
|
|
DEBUG_AUDIO ("Update devices\n");
|
2015-08-25 03:23:07 -04:00
|
|
|
if (_stream != NULL) return false;
|
2015-08-24 19:10:02 -04:00
|
|
|
pa_deinitialize();
|
2015-08-25 03:23:07 -04:00
|
|
|
if (!pa_initialize()) return false;
|
2015-05-03 23:28:41 -04:00
|
|
|
|
2015-05-08 21:42:46 -04:00
|
|
|
clear_device_lists ();
|
2015-08-25 22:36:31 -04:00
|
|
|
|
|
|
|
// 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 ();
|
|
|
|
}
|
2015-05-03 23:28:41 -04:00
|
|
|
add_devices ();
|
2015-08-25 03:23:07 -04:00
|
|
|
return true;
|
2015-05-03 23:28:41 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 06:36:23 -04:00
|
|
|
void
|
|
|
|
PortAudioIO::reset_stream_dependents ()
|
|
|
|
{
|
|
|
|
_capture_channels = 0;
|
|
|
|
_playback_channels = 0;
|
|
|
|
_cur_sample_rate = 0;
|
|
|
|
_cur_input_latency = 0;
|
|
|
|
_cur_output_latency = 0;
|
|
|
|
}
|
|
|
|
|
2015-09-26 06:49:41 -04:00
|
|
|
PaErrorCode
|
2015-08-24 06:31:47 -04:00
|
|
|
PortAudioIO::close_stream()
|
2015-03-12 23:05:53 -04:00
|
|
|
{
|
2015-09-26 06:49:41 -04:00
|
|
|
if (!_stream) return paNoError;
|
2015-08-24 06:31:47 -04:00
|
|
|
|
|
|
|
PaError err = Pa_CloseStream (_stream);
|
|
|
|
|
|
|
|
if (err != paNoError) {
|
2015-09-26 06:49:41 -04:00
|
|
|
return (PaErrorCode)err;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
_stream = NULL;
|
|
|
|
|
2015-08-23 06:36:23 -04:00
|
|
|
reset_stream_dependents();
|
2015-03-12 23:05:53 -04:00
|
|
|
|
|
|
|
free (_input_buffer); _input_buffer = NULL;
|
|
|
|
free (_output_buffer); _output_buffer = NULL;
|
2015-09-26 06:49:41 -04:00
|
|
|
return paNoError;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
|
2015-09-26 06:49:41 -04:00
|
|
|
PaErrorCode
|
2015-08-24 06:31:47 -04:00
|
|
|
PortAudioIO::start_stream()
|
2015-03-12 23:05:53 -04:00
|
|
|
{
|
|
|
|
PaError err = Pa_StartStream (_stream);
|
|
|
|
|
|
|
|
if (err != paNoError) {
|
2015-09-26 06:49:41 -04:00
|
|
|
DEBUG_AUDIO(string_compose("PortAudio failed to start stream %1\n",
|
|
|
|
Pa_GetErrorText(err)));
|
|
|
|
return (PaErrorCode)err;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
2015-09-26 06:49:41 -04:00
|
|
|
return paNoError;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 03:37:14 -04:00
|
|
|
bool
|
|
|
|
PortAudioIO::set_sample_rate_and_latency_from_stream ()
|
|
|
|
{
|
|
|
|
const PaStreamInfo* nfo_s = Pa_GetStreamInfo(_stream);
|
|
|
|
|
|
|
|
if (nfo_s == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_cur_sample_rate = nfo_s->sampleRate;
|
|
|
|
_cur_input_latency = nfo_s->inputLatency * _cur_sample_rate;
|
|
|
|
_cur_output_latency = nfo_s->outputLatency * _cur_sample_rate;
|
|
|
|
|
|
|
|
DEBUG_AUDIO (string_compose ("PA Sample Rate %1 SPS\n", _cur_sample_rate));
|
|
|
|
|
|
|
|
DEBUG_AUDIO (string_compose ("PA Input Latency %1ms, %2 spl\n",
|
|
|
|
1e3 * nfo_s->inputLatency,
|
|
|
|
_cur_input_latency));
|
|
|
|
|
|
|
|
DEBUG_AUDIO (string_compose ("PA Output Latency %1ms, %2 spl\n",
|
|
|
|
1e3 * nfo_s->outputLatency,
|
|
|
|
_cur_output_latency));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-23 03:08:50 -04:00
|
|
|
bool
|
|
|
|
PortAudioIO::allocate_buffers_for_blocking_api (uint32_t samples_per_period)
|
|
|
|
{
|
|
|
|
if (_capture_channels > 0) {
|
|
|
|
_input_buffer =
|
|
|
|
(float*)malloc(samples_per_period * _capture_channels * sizeof(float));
|
|
|
|
if (!_input_buffer) {
|
|
|
|
DEBUG_AUDIO("PortAudio failed to allocate input buffer.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_playback_channels > 0) {
|
|
|
|
_output_buffer =
|
|
|
|
(float*)calloc(samples_per_period * _playback_channels, sizeof(float));
|
|
|
|
if (!_output_buffer) {
|
|
|
|
DEBUG_AUDIO("PortAudio failed to allocate output buffer.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-23 04:38:01 -04:00
|
|
|
bool
|
|
|
|
PortAudioIO::get_input_stream_params(int device_input,
|
|
|
|
PaStreamParameters& inputParam) const
|
|
|
|
{
|
|
|
|
const PaDeviceInfo *nfo_in = NULL;
|
|
|
|
|
|
|
|
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 ();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (device_output == DeviceNone) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nfo_out = Pa_GetDeviceInfo(device_output);
|
|
|
|
|
|
|
|
if (nfo_out == NULL) {
|
|
|
|
DEBUG_AUDIO ("PortAudio Cannot Query Output Device Info\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-09-26 06:49:41 -04:00
|
|
|
PaErrorCode
|
2015-08-24 04:35:43 -04:00
|
|
|
PortAudioIO::pre_stream_open(int device_input,
|
|
|
|
PaStreamParameters& inputParam,
|
|
|
|
int device_output,
|
|
|
|
PaStreamParameters& outputParam)
|
2015-03-12 23:05:53 -04:00
|
|
|
{
|
2015-08-23 22:57:34 -04:00
|
|
|
if (!pa_initialize()) {
|
2015-05-17 06:55:04 -04:00
|
|
|
DEBUG_AUDIO ("PortAudio Initialization Failed\n");
|
2015-09-26 06:49:41 -04:00
|
|
|
return paNotInitialized;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 06:36:23 -04:00
|
|
|
reset_stream_dependents ();
|
2015-03-12 23:05:53 -04:00
|
|
|
|
2015-05-17 06:55:04 -04:00
|
|
|
DEBUG_AUDIO (string_compose (
|
|
|
|
"PortAudio Device IDs: i:%1 o:%2\n", device_input, device_output));
|
2015-03-12 23:05:53 -04:00
|
|
|
|
2015-08-22 07:04:53 -04:00
|
|
|
if (device_input == DeviceNone && device_output == DeviceNone) {
|
2015-09-26 06:49:41 -04:00
|
|
|
return paBadIODeviceCombination;
|
2015-08-22 07:04:53 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 04:38:01 -04:00
|
|
|
if (get_input_stream_params(device_input, inputParam)) {
|
|
|
|
_capture_channels = inputParam.channelCount;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 04:38:01 -04:00
|
|
|
if (get_output_stream_params(device_output, outputParam)) {
|
|
|
|
_playback_channels = outputParam.channelCount;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 04:38:01 -04:00
|
|
|
if (_capture_channels == 0 && _playback_channels == 0) {
|
|
|
|
DEBUG_AUDIO("PortAudio no input or output channels.\n");
|
2015-09-26 06:49:41 -04:00
|
|
|
return paBadIODeviceCombination;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
|
2015-05-17 06:55:04 -04:00
|
|
|
DEBUG_AUDIO (string_compose ("PortAudio Channels: in:%1 out:%2\n",
|
|
|
|
_capture_channels,
|
|
|
|
_playback_channels));
|
2015-03-12 23:05:53 -04:00
|
|
|
|
2015-08-27 18:53:10 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
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) {
|
2016-11-09 20:41:19 -05:00
|
|
|
DEBUG_AUDIO(string_compose("PortAudio failed to open stream %1\n",
|
|
|
|
Pa_GetErrorText(err)));
|
2015-08-27 18:53:10 -04:00
|
|
|
return paInternalError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!set_sample_rate_and_latency_from_stream()) {
|
|
|
|
DEBUG_AUDIO ("PortAudio failed to query stream information.\n");
|
|
|
|
close_stream();
|
|
|
|
return paInternalError;
|
|
|
|
}
|
|
|
|
|
2015-09-26 06:49:41 -04:00
|
|
|
return paNoError;
|
2015-08-24 04:35:43 -04:00
|
|
|
}
|
|
|
|
|
2015-09-26 06:49:41 -04:00
|
|
|
PaErrorCode
|
2015-08-24 06:31:47 -04:00
|
|
|
PortAudioIO::open_blocking_stream(int device_input,
|
|
|
|
int device_output,
|
|
|
|
double sample_rate,
|
|
|
|
uint32_t samples_per_period)
|
2015-08-24 04:35:43 -04:00
|
|
|
{
|
|
|
|
PaStreamParameters inputParam;
|
|
|
|
PaStreamParameters outputParam;
|
|
|
|
|
2015-09-26 06:49:41 -04:00
|
|
|
PaErrorCode error_code =
|
2015-08-24 04:35:43 -04:00
|
|
|
pre_stream_open(device_input, inputParam, device_output, outputParam);
|
|
|
|
|
2015-09-26 06:49:41 -04:00
|
|
|
if (error_code != paNoError) return error_code;
|
2015-08-24 04:35:43 -04:00
|
|
|
|
2015-08-23 20:50:00 -04:00
|
|
|
PaError err = paNoError;
|
2015-03-12 23:05:53 -04:00
|
|
|
|
|
|
|
err = Pa_OpenStream (
|
|
|
|
&_stream,
|
|
|
|
_capture_channels > 0 ? &inputParam: NULL,
|
|
|
|
_playback_channels > 0 ? &outputParam: NULL,
|
|
|
|
sample_rate,
|
|
|
|
samples_per_period,
|
2015-08-03 16:57:10 -04:00
|
|
|
paDitherOff,
|
2015-03-12 23:05:53 -04:00
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
if (err != paNoError) {
|
2015-09-26 06:49:41 -04:00
|
|
|
DEBUG_AUDIO(string_compose("PortAudio failed to open stream %1\n",
|
|
|
|
Pa_GetErrorText(err)));
|
|
|
|
return (PaErrorCode)err;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 03:37:14 -04:00
|
|
|
if (!set_sample_rate_and_latency_from_stream()) {
|
2015-05-17 06:55:04 -04:00
|
|
|
DEBUG_AUDIO ("PortAudio failed to query stream information.\n");
|
2015-08-24 06:31:47 -04:00
|
|
|
close_stream();
|
2015-09-26 06:49:41 -04:00
|
|
|
return paInternalError;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
|
2015-08-23 03:08:50 -04:00
|
|
|
if (!allocate_buffers_for_blocking_api(samples_per_period)) {
|
2015-08-24 06:31:47 -04:00
|
|
|
close_stream();
|
2015-09-26 06:49:41 -04:00
|
|
|
return paInternalError;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
2015-09-26 06:49:41 -04:00
|
|
|
return paNoError;
|
2015-03-12 23:05:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
PortAudioIO::next_cycle (uint32_t n_samples)
|
|
|
|
{
|
|
|
|
bool xrun = false;
|
|
|
|
PaError err;
|
|
|
|
err = Pa_IsStreamActive (_stream);
|
|
|
|
if (err != 1) {
|
|
|
|
// 0: inactive / aborted
|
|
|
|
// < 0: error
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO, check drift.. process part with larger capacity first.
|
|
|
|
// Pa_GetStreamReadAvailable(_stream) < Pa_GetStreamWriteAvailable(_stream)
|
|
|
|
|
|
|
|
if (_playback_channels > 0) {
|
|
|
|
err = Pa_WriteStream (_stream, _output_buffer, n_samples);
|
|
|
|
if (err) xrun = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_capture_channels > 0) {
|
|
|
|
err = Pa_ReadStream (_stream, _input_buffer, n_samples);
|
|
|
|
if (err) {
|
|
|
|
memset (_input_buffer, 0, sizeof(float) * n_samples * _capture_channels);
|
|
|
|
xrun = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return xrun ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
2015-08-04 01:29:03 -04:00
|
|
|
std::string
|
|
|
|
PortAudioIO::get_input_channel_name (int device_id, uint32_t channel) const
|
|
|
|
{
|
|
|
|
#ifdef WITH_ASIO
|
|
|
|
const char* channel_name;
|
|
|
|
|
|
|
|
// This will return an error for non-ASIO devices so no need to check if
|
|
|
|
// the device_id corresponds to an ASIO device.
|
|
|
|
PaError err = PaAsio_GetInputChannelName (device_id, channel, &channel_name);
|
|
|
|
|
|
|
|
if (err == paNoError) {
|
|
|
|
DEBUG_AUDIO (
|
|
|
|
string_compose ("Input channel name for device %1, channel %2 is %3\n",
|
|
|
|
device_id,
|
|
|
|
channel,
|
|
|
|
channel_name));
|
|
|
|
return channel_name;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
PortAudioIO::get_output_channel_name (int device_id, uint32_t channel) const
|
|
|
|
{
|
|
|
|
#ifdef WITH_ASIO
|
|
|
|
const char* channel_name;
|
|
|
|
|
|
|
|
PaError err = PaAsio_GetOutputChannelName (device_id, channel, &channel_name);
|
|
|
|
|
|
|
|
if (err == paNoError) {
|
|
|
|
DEBUG_AUDIO (
|
|
|
|
string_compose ("Output channel name for device %1, channel %2 is %3\n",
|
|
|
|
device_id,
|
|
|
|
channel,
|
|
|
|
channel_name));
|
|
|
|
return channel_name;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return std::string();
|
|
|
|
}
|
2015-03-20 06:45:56 -04:00
|
|
|
|
|
|
|
#ifdef INTERLEAVED_INPUT
|
|
|
|
|
2015-03-12 23:05:53 -04:00
|
|
|
int
|
|
|
|
PortAudioIO::get_capture_channel (uint32_t chn, float *input, uint32_t n_samples)
|
|
|
|
{
|
|
|
|
assert(chn < _capture_channels);
|
|
|
|
const uint32_t stride = _capture_channels;
|
|
|
|
float *ptr = _input_buffer + chn;
|
|
|
|
while (n_samples-- > 0) {
|
|
|
|
*input++ = *ptr;
|
|
|
|
ptr += stride;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-20 06:45:56 -04:00
|
|
|
#else
|
|
|
|
|
|
|
|
int
|
|
|
|
PortAudioIO::get_capture_channel (uint32_t chn, float *input, uint32_t n_samples)
|
|
|
|
{
|
|
|
|
assert(chn < _capture_channels);
|
|
|
|
memcpy((void*)input, &(_input_buffer[chn * n_samples]), n_samples * sizeof(float));
|
2015-03-20 11:16:55 -04:00
|
|
|
return 0;
|
2015-03-20 06:45:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef INTERLEAVED_OUTPUT
|
|
|
|
|
2015-03-12 23:05:53 -04:00
|
|
|
int
|
|
|
|
PortAudioIO::set_playback_channel (uint32_t chn, const float *output, uint32_t n_samples)
|
|
|
|
{
|
|
|
|
assert(chn < _playback_channels);
|
|
|
|
const uint32_t stride = _playback_channels;
|
|
|
|
float *ptr = _output_buffer + chn;
|
|
|
|
while (n_samples-- > 0) {
|
|
|
|
*ptr = *output++;
|
|
|
|
ptr += stride;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2015-03-20 06:45:56 -04:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
int
|
|
|
|
PortAudioIO::set_playback_channel (uint32_t chn, const float *output, uint32_t n_samples)
|
|
|
|
{
|
|
|
|
assert(chn < _playback_channels);
|
|
|
|
memcpy((void*)&(_output_buffer[chn * n_samples]), (void*)output, n_samples * sizeof(float));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|