2023-06-15 07:48:45 +00:00
|
|
|
/* #define DEBUGTRACE_ENABLED */
|
2023-06-14 19:23:53 +00:00
|
|
|
#include "debugtrace.hpp"
|
|
|
|
#include "lasp_config.h"
|
|
|
|
|
|
|
|
#if LASP_HAS_PORTAUDIO == 1
|
|
|
|
#include <gsl-lite/gsl-lite.hpp>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
#include "lasp_portaudiodaq.h"
|
|
|
|
#include "portaudio.h"
|
|
|
|
|
2023-06-14 19:23:53 +00:00
|
|
|
using rte = std::runtime_error;
|
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
|
|
|
using std::string;
|
|
|
|
using std::to_string;
|
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
inline void throwIfError(PaError e) {
|
2023-06-14 19:23:53 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2024-02-06 13:59:51 +00:00
|
|
|
if (e != paNoError) {
|
2023-06-14 19:23:53 +00:00
|
|
|
throw rte(string("PortAudio backend error: ") + Pa_GetErrorText(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Device info, plus PortAudio stuff
|
|
|
|
*/
|
2024-02-06 13:59:51 +00:00
|
|
|
class OurPaDeviceInfo : public DeviceInfo {
|
|
|
|
public:
|
2023-06-14 19:23:53 +00:00
|
|
|
/**
|
|
|
|
* @brief Store instance to PaDeviceInfo.
|
|
|
|
*/
|
|
|
|
PaDeviceInfo _paDevInfo;
|
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
virtual std::unique_ptr<DeviceInfo> clone() const override final {
|
2023-06-14 19:23:53 +00:00
|
|
|
return std::make_unique<OurPaDeviceInfo>(*this);
|
|
|
|
}
|
2024-01-25 14:31:53 +00:00
|
|
|
OurPaDeviceInfo &operator=(const OurPaDeviceInfo &) = delete;
|
|
|
|
OurPaDeviceInfo(const OurPaDeviceInfo &) = default;
|
|
|
|
OurPaDeviceInfo(const PaDeviceInfo &o) : DeviceInfo(), _paDevInfo(o) {}
|
2023-06-14 19:23:53 +00:00
|
|
|
};
|
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
void fillPortAudioDeviceInfo(DeviceInfoList &devinfolist) {
|
2023-06-14 19:23:53 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2024-02-06 13:59:51 +00:00
|
|
|
bool shouldPaTerminate = false;
|
|
|
|
try {
|
2023-06-14 19:23:53 +00:00
|
|
|
PaError err = Pa_Initialize();
|
|
|
|
/// PortAudio says that Pa_Terminate() should not be called whenever there
|
|
|
|
/// is an error in Pa_Initialize(). This is opposite to what most examples
|
|
|
|
/// of PortAudio show.
|
|
|
|
throwIfError(err);
|
2024-02-06 13:59:51 +00:00
|
|
|
shouldPaTerminate = true;
|
2023-06-14 19:23:53 +00:00
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
auto fin = gsl::finally([&err] {
|
2023-06-14 19:23:53 +00:00
|
|
|
DEBUGTRACE_PRINT("Terminating PortAudio instance");
|
|
|
|
err = Pa_Terminate();
|
|
|
|
if (err != paNoError) {
|
|
|
|
cerr << "Error terminating PortAudio. Do not know what to do." << endl;
|
2024-02-06 13:59:51 +00:00
|
|
|
}
|
|
|
|
});
|
2023-06-14 19:23:53 +00:00
|
|
|
|
2024-01-25 14:31:53 +00:00
|
|
|
const PaHostApiIndex apicount = Pa_GetHostApiCount();
|
2024-02-06 13:59:51 +00:00
|
|
|
if (apicount < 0) {
|
2024-01-25 14:31:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2023-06-14 19:23:53 +00:00
|
|
|
/* const PaDeviceInfo *deviceInfo; */
|
|
|
|
const int numDevices = Pa_GetDeviceCount();
|
2024-02-06 13:59:51 +00:00
|
|
|
if (numDevices < 0) {
|
2023-06-14 19:23:53 +00:00
|
|
|
throw rte("PortAudio could not find any devices");
|
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
for (us i = 0; i < (us)numDevices; i++) {
|
2023-06-14 19:23:53 +00:00
|
|
|
/* DEBUGTRACE_PRINT(i); */
|
2024-02-06 13:59:51 +00:00
|
|
|
bool hasDuplexMode = false;
|
2023-06-14 19:23:53 +00:00
|
|
|
|
|
|
|
const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(i);
|
2024-02-06 13:59:51 +00:00
|
|
|
if (!deviceInfo) {
|
2023-06-14 19:23:53 +00:00
|
|
|
throw rte("No device info struct returned");
|
|
|
|
}
|
2024-01-25 14:31:53 +00:00
|
|
|
OurPaDeviceInfo d(*deviceInfo);
|
2024-01-19 11:35:56 +00:00
|
|
|
// We store the name in d.device_name
|
|
|
|
d._paDevInfo.name = nullptr;
|
2023-06-14 19:23:53 +00:00
|
|
|
d.device_name = deviceInfo->name;
|
|
|
|
|
2024-01-25 14:31:53 +00:00
|
|
|
const PaHostApiInfo *hostapiinfo = Pa_GetHostApiInfo(deviceInfo->hostApi);
|
2024-02-06 13:59:51 +00:00
|
|
|
if (hostapiinfo == nullptr) {
|
2024-01-25 14:31:53 +00:00
|
|
|
throw std::runtime_error("Hostapi nullptr!");
|
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
switch (hostapiinfo->type) {
|
|
|
|
case paALSA:
|
|
|
|
// Duplex mode for alsa
|
|
|
|
hasDuplexMode = true;
|
|
|
|
d.api = portaudioALSAApi;
|
|
|
|
break;
|
|
|
|
case paASIO:
|
|
|
|
d.api = portaudioASIOApi;
|
|
|
|
break;
|
|
|
|
case paDirectSound:
|
|
|
|
d.api = portaudioDirectSoundApi;
|
|
|
|
break;
|
|
|
|
case paMME:
|
|
|
|
d.api = portaudioWMMEApi;
|
|
|
|
break;
|
|
|
|
case paWDMKS:
|
|
|
|
d.api = portaudioWDMKS;
|
|
|
|
break;
|
|
|
|
case paWASAPI:
|
|
|
|
d.api = portaudioWASAPIApi;
|
|
|
|
break;
|
|
|
|
case paPulseAudio:
|
|
|
|
d.api = portaudioPulseApi;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw rte("Unimplemented portaudio API!");
|
|
|
|
break;
|
2024-01-25 14:31:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 19:23:53 +00:00
|
|
|
d.availableDataTypes = {DataTypeDescriptor::DataType::dtype_int16,
|
|
|
|
DataTypeDescriptor::DataType::dtype_int32,
|
|
|
|
DataTypeDescriptor::DataType::dtype_fl32};
|
|
|
|
|
|
|
|
d.prefDataTypeIndex = 2;
|
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
d.availableSampleRates = {8000.0, 9600.0, 11025.0, 12000.0, 16000.0,
|
2023-06-14 19:23:53 +00:00
|
|
|
22050.0, 24000.0, 32000.0, 44100.0, 48000.0,
|
|
|
|
88200.0, 96000.0, 192000.0};
|
|
|
|
d.prefSampleRateIndex = 9;
|
|
|
|
|
|
|
|
d.availableFramesPerBlock = {512, 1024, 2048, 4096, 8192};
|
|
|
|
d.prefFramesPerBlockIndex = 2;
|
|
|
|
|
|
|
|
d.availableInputRanges = {1.0};
|
2024-01-19 11:35:56 +00:00
|
|
|
// d.prefInputRangeIndex = 0; // Constructor-defined
|
|
|
|
d.availableOutputRanges = {1.0};
|
|
|
|
// d.prefOutputRangeIndex = 0; // Constructor-defined
|
2023-06-14 19:23:53 +00:00
|
|
|
|
|
|
|
d.ninchannels = deviceInfo->maxInputChannels;
|
|
|
|
d.noutchannels = deviceInfo->maxOutputChannels;
|
|
|
|
|
2024-02-06 14:02:25 +00:00
|
|
|
// Duplex mode, only for ALSA devices
|
|
|
|
d.hasDuplexMode = hasDuplexMode;
|
|
|
|
|
2023-06-14 19:23:53 +00:00
|
|
|
devinfolist.push_back(std::make_unique<OurPaDeviceInfo>(d));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
catch (rte &e) {
|
|
|
|
if (shouldPaTerminate) {
|
|
|
|
PaError err = Pa_Terminate();
|
|
|
|
if (err != paNoError) {
|
|
|
|
cerr << "Error terminating PortAudio. Do not know what to do." << endl;
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 19:23:53 +00:00
|
|
|
cerr << "PortAudio backend error: " << e.what() << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Forward declaration of raw callback. Calls into
|
|
|
|
* PortAudioDaq->memberPaCallback. Undocumented parameters are specified
|
|
|
|
* in memberPaCallback
|
|
|
|
*
|
|
|
|
* @param inputBuffer
|
|
|
|
* @param outputBuffer
|
|
|
|
* @param framesPerBuffer
|
|
|
|
* @param timeInfo
|
|
|
|
* @param statusFlags
|
|
|
|
* @param userData Pointer to PortAudioDaq* instance.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
static int rawPaCallback(const void *inputBuffer, void *outputBuffer,
|
|
|
|
unsigned long framesPerBuffer,
|
|
|
|
const PaStreamCallbackTimeInfo *timeInfo,
|
|
|
|
PaStreamCallbackFlags statusFlags, void *userData);
|
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
class PortAudioDaq : public Daq {
|
2023-06-14 19:23:53 +00:00
|
|
|
PaStream *_stream = nullptr;
|
2023-06-15 07:48:45 +00:00
|
|
|
std::atomic<StreamStatus::StreamError> _streamError =
|
|
|
|
StreamStatus::StreamError::noError;
|
|
|
|
InDaqCallback _incallback;
|
|
|
|
OutDaqCallback _outcallback;
|
2023-06-14 19:23:53 +00:00
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
public:
|
2023-06-14 19:23:53 +00:00
|
|
|
PortAudioDaq(const OurPaDeviceInfo &devinfo_gen,
|
|
|
|
const DaqConfiguration &config);
|
|
|
|
|
|
|
|
void start(InDaqCallback inCallback,
|
|
|
|
OutDaqCallback outCallback) override final;
|
|
|
|
void stop() override final;
|
|
|
|
|
|
|
|
StreamStatus getStreamStatus() const override final;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Member va
|
|
|
|
*
|
|
|
|
* @param inputBuffer
|
|
|
|
* @param outputBuffer
|
|
|
|
* @param framesPerBuffer
|
|
|
|
* @param timeInfo
|
|
|
|
* @param statusFlags
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
int memberPaCallback(const void *inputBuffer, void *outputBuffer,
|
|
|
|
unsigned long framesPerBuffer,
|
|
|
|
const PaStreamCallbackTimeInfo *timeInfo,
|
|
|
|
PaStreamCallbackFlags statusFlags);
|
|
|
|
~PortAudioDaq();
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<Daq> createPortAudioDevice(const DeviceInfo &devinfo,
|
2024-02-06 13:59:51 +00:00
|
|
|
const DaqConfiguration &config) {
|
2024-01-25 14:31:53 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2023-06-14 19:23:53 +00:00
|
|
|
const OurPaDeviceInfo *_info =
|
|
|
|
dynamic_cast<const OurPaDeviceInfo *>(&devinfo);
|
2024-02-06 13:59:51 +00:00
|
|
|
if (_info == nullptr) {
|
2023-06-14 19:23:53 +00:00
|
|
|
throw rte("BUG: Could not cast DeviceInfo to OurPaDeviceInfo");
|
|
|
|
}
|
|
|
|
return std::make_unique<PortAudioDaq>(*_info, config);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rawPaCallback(const void *inputBuffer, void *outputBuffer,
|
|
|
|
unsigned long framesPerBuffer,
|
|
|
|
const PaStreamCallbackTimeInfo *timeInfo,
|
2024-02-06 13:59:51 +00:00
|
|
|
PaStreamCallbackFlags statusFlags, void *userData) {
|
2023-06-14 19:23:53 +00:00
|
|
|
return static_cast<PortAudioDaq *>(userData)->memberPaCallback(
|
|
|
|
inputBuffer, outputBuffer, framesPerBuffer, timeInfo, statusFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
PortAudioDaq::PortAudioDaq(const OurPaDeviceInfo &devinfo_gen,
|
|
|
|
const DaqConfiguration &config)
|
2024-02-06 13:59:51 +00:00
|
|
|
: Daq(devinfo_gen, config) {
|
2023-06-14 19:23:53 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
2024-02-06 13:59:51 +00:00
|
|
|
bool shouldPaTerminate = false;
|
|
|
|
try {
|
|
|
|
PaError err = Pa_Initialize();
|
|
|
|
/// PortAudio says that Pa_Terminate() should not be called whenever there
|
|
|
|
/// is an error in Pa_Initialize(). This is opposite to what most examples
|
|
|
|
/// of PortAudio show.
|
|
|
|
throwIfError(err);
|
2023-06-14 19:23:53 +00:00
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
// OK, Pa_Initialize successfully finished, it means we have to clean up
|
|
|
|
// with Pa_Terminate in the destructor.
|
|
|
|
shouldPaTerminate = true;
|
|
|
|
|
|
|
|
// Going to find the device in the list. If its there, we have to retrieve
|
|
|
|
// the index, as this is required in the PaStreamParameters struct
|
|
|
|
int devindex = -1;
|
|
|
|
for (int i = 0; i < Pa_GetDeviceCount(); i++) {
|
|
|
|
// DEBUGTRACE_PRINT(i);
|
|
|
|
bool ok = true;
|
|
|
|
const PaDeviceInfo *info = Pa_GetDeviceInfo(i);
|
|
|
|
if (!info) {
|
|
|
|
throw rte("No device structure returned from PortAudio");
|
|
|
|
}
|
|
|
|
ok &= string(info->name) == devinfo_gen.device_name;
|
|
|
|
ok &= info->hostApi == devinfo_gen._paDevInfo.hostApi;
|
|
|
|
ok &= info->maxInputChannels == devinfo_gen._paDevInfo.maxInputChannels;
|
|
|
|
ok &= info->maxOutputChannels == devinfo_gen._paDevInfo.maxOutputChannels;
|
|
|
|
ok &= info->defaultSampleRate == devinfo_gen._paDevInfo.defaultSampleRate;
|
|
|
|
|
|
|
|
if (ok) {
|
|
|
|
devindex = i;
|
|
|
|
}
|
2023-06-14 19:23:53 +00:00
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
if (devindex < 0) {
|
|
|
|
throw rte(string("Device not found: ") + string(devinfo_gen.device_name));
|
2023-06-14 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
using Dtype = DataTypeDescriptor::DataType;
|
|
|
|
const Dtype dtype = dataType();
|
|
|
|
// Sample format is bit flag
|
|
|
|
PaSampleFormat format = paNonInterleaved;
|
|
|
|
switch (dtype) {
|
|
|
|
case Dtype::dtype_fl32:
|
|
|
|
DEBUGTRACE_PRINT("Datatype float32");
|
|
|
|
format |= paFloat32;
|
|
|
|
break;
|
|
|
|
case Dtype::dtype_fl64:
|
|
|
|
DEBUGTRACE_PRINT("Datatype float64");
|
|
|
|
throw rte("Invalid data type specified for DAQ stream.");
|
|
|
|
break;
|
|
|
|
case Dtype::dtype_int8:
|
|
|
|
DEBUGTRACE_PRINT("Datatype int8");
|
|
|
|
format |= paInt8;
|
|
|
|
break;
|
|
|
|
case Dtype::dtype_int16:
|
|
|
|
DEBUGTRACE_PRINT("Datatype int16");
|
|
|
|
format |= paInt16;
|
|
|
|
break;
|
|
|
|
case Dtype::dtype_int32:
|
|
|
|
DEBUGTRACE_PRINT("Datatype int32");
|
|
|
|
format |= paInt32;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw rte("Invalid data type specified for DAQ stream.");
|
|
|
|
break;
|
|
|
|
}
|
2023-06-14 19:23:53 +00:00
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
std::unique_ptr<PaStreamParameters> instreamParams;
|
|
|
|
std::unique_ptr<PaStreamParameters> outstreamParams;
|
2023-06-14 19:23:53 +00:00
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
if (neninchannels() > 0) {
|
|
|
|
instreamParams = std::make_unique<PaStreamParameters>(PaStreamParameters(
|
|
|
|
{.device = devindex,
|
|
|
|
.channelCount = (int)getHighestEnabledInChannel() + 1,
|
|
|
|
.sampleFormat = format,
|
|
|
|
.suggestedLatency = framesPerBlock() / samplerate(),
|
|
|
|
.hostApiSpecificStreamInfo = nullptr}));
|
|
|
|
}
|
|
|
|
if (nenoutchannels() > 0) {
|
|
|
|
outstreamParams = std::make_unique<PaStreamParameters>(PaStreamParameters(
|
|
|
|
{.device = devindex,
|
|
|
|
.channelCount = (int)getHighestEnabledOutChannel() + 1,
|
|
|
|
.sampleFormat = format,
|
|
|
|
.suggestedLatency = framesPerBlock() / samplerate(),
|
|
|
|
.hostApiSpecificStreamInfo = nullptr}));
|
|
|
|
}
|
2023-06-14 19:23:53 +00:00
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
// Next step: check whether we are OK
|
|
|
|
err = Pa_IsFormatSupported(instreamParams.get(), outstreamParams.get(),
|
|
|
|
samplerate());
|
|
|
|
throwIfError(err);
|
|
|
|
|
|
|
|
err = Pa_OpenStream(&_stream, // stream
|
|
|
|
instreamParams.get(), // inputParameters
|
|
|
|
outstreamParams.get(), // outputParameters
|
|
|
|
samplerate(), // yeah,
|
|
|
|
framesPerBlock(), // framesPerBuffer
|
|
|
|
paNoFlag, // streamFlags
|
|
|
|
rawPaCallback, this);
|
|
|
|
throwIfError(err);
|
|
|
|
assert(_stream);
|
|
|
|
} catch (rte &e) {
|
|
|
|
if (shouldPaTerminate) {
|
|
|
|
PaError err = Pa_Terminate();
|
|
|
|
if (err != paNoError) {
|
|
|
|
cerr << "Error terminating PortAudio. Do not know what to do." << endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw;
|
|
|
|
}
|
2023-06-14 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
void PortAudioDaq::start(InDaqCallback inCallback, OutDaqCallback outCallback) {
|
2023-06-14 19:23:53 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
assert(_stream);
|
2024-02-06 13:59:51 +00:00
|
|
|
|
|
|
|
if (Pa_IsStreamActive(_stream)) {
|
2023-06-14 19:23:53 +00:00
|
|
|
throw rte("Stream is already running");
|
|
|
|
}
|
2023-06-15 07:48:45 +00:00
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
if (neninchannels() > 0) {
|
|
|
|
if (!inCallback) {
|
2023-06-15 07:48:45 +00:00
|
|
|
throw rte(
|
|
|
|
|
|
|
|
"Input callback given, but stream does not provide input data");
|
|
|
|
}
|
|
|
|
|
|
|
|
_incallback = inCallback;
|
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
if (nenoutchannels() > 0) {
|
|
|
|
if (!outCallback) {
|
2023-06-15 07:48:45 +00:00
|
|
|
throw rte(
|
|
|
|
"Output callback given, but stream does not provide output data");
|
|
|
|
}
|
|
|
|
_outcallback = outCallback;
|
|
|
|
}
|
|
|
|
|
2023-06-14 19:23:53 +00:00
|
|
|
PaError err = Pa_StartStream(_stream);
|
|
|
|
throwIfError(err);
|
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
void PortAudioDaq::stop() {
|
2023-06-14 19:23:53 +00:00
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
assert(_stream);
|
2024-02-06 13:59:51 +00:00
|
|
|
if (Pa_IsStreamStopped(_stream) > 1) {
|
2023-06-14 19:23:53 +00:00
|
|
|
throw rte("Stream is already stopped");
|
|
|
|
}
|
|
|
|
PaError err = Pa_StopStream(_stream);
|
|
|
|
throwIfError(err);
|
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
Daq::StreamStatus PortAudioDaq::getStreamStatus() const {
|
|
|
|
DEBUGTRACE_ENTER;
|
|
|
|
// Stores an error type and whether the
|
2023-06-14 19:23:53 +00:00
|
|
|
Daq::StreamStatus status;
|
2024-02-06 13:59:51 +00:00
|
|
|
using StreamError = Daq::StreamStatus::StreamError;
|
|
|
|
Daq::StreamStatus::StreamError errortype = _streamError.load();
|
|
|
|
|
|
|
|
PaError err = Pa_IsStreamStopped(_stream);
|
|
|
|
if (err > 1) {
|
|
|
|
// Stream is stopped due to an error in the callback. The exact error type
|
|
|
|
// is filled in in the if-statement above
|
|
|
|
return status;
|
|
|
|
} else if (err == 0) {
|
|
|
|
// Still running
|
|
|
|
status.isRunning = true;
|
|
|
|
} else if (err < 0) {
|
|
|
|
// Stream encountered an error.
|
|
|
|
switch (err) {
|
|
|
|
case paInternalError:
|
|
|
|
errortype = StreamError::driverError;
|
|
|
|
break;
|
|
|
|
case paDeviceUnavailable:
|
|
|
|
errortype = StreamError::driverError;
|
|
|
|
break;
|
|
|
|
case paInputOverflowed:
|
|
|
|
errortype = StreamError::inputXRun;
|
|
|
|
break;
|
|
|
|
case paOutputUnderflowed:
|
|
|
|
errortype = StreamError::outputXRun;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errortype = StreamError::driverError;
|
|
|
|
cerr << "Portaudio backend error:" << Pa_GetErrorText(err) << endl;
|
|
|
|
break;
|
2023-06-14 19:23:53 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
status.errorType = errortype;
|
2023-06-14 19:23:53 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
PortAudioDaq::~PortAudioDaq() {
|
2023-06-14 19:23:53 +00:00
|
|
|
PaError err;
|
2024-02-06 13:59:51 +00:00
|
|
|
assert(_stream);
|
|
|
|
if (Pa_IsStreamActive(_stream)) {
|
|
|
|
// Stop the stream first
|
|
|
|
stop();
|
|
|
|
}
|
2023-06-14 19:23:53 +00:00
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
err = Pa_CloseStream(_stream);
|
|
|
|
_stream = nullptr;
|
|
|
|
if (err != paNoError) {
|
|
|
|
cerr << "Error closing PortAudio stream. Do not know what to do." << endl;
|
2023-06-14 19:23:53 +00:00
|
|
|
}
|
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
err = Pa_Terminate();
|
|
|
|
if (err != paNoError) {
|
|
|
|
cerr << "Error terminating PortAudio. Do not know what to do." << endl;
|
2023-06-14 19:23:53 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
|
2023-06-14 19:23:53 +00:00
|
|
|
int PortAudioDaq::memberPaCallback(const void *inputBuffer, void *outputBuffer,
|
|
|
|
unsigned long framesPerBuffer,
|
|
|
|
const PaStreamCallbackTimeInfo *timeInfo,
|
2024-02-06 13:59:51 +00:00
|
|
|
PaStreamCallbackFlags statusFlags) {
|
|
|
|
// DEBUGTRACE_ENTER;
|
2023-06-15 07:48:45 +00:00
|
|
|
typedef Daq::StreamStatus::StreamError se;
|
2024-02-06 13:59:51 +00:00
|
|
|
if (statusFlags & paPrimingOutput) {
|
2023-06-15 07:48:45 +00:00
|
|
|
// Initial output buffers generated. So nothing with input yet
|
|
|
|
return paContinue;
|
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
if ((statusFlags & paInputUnderflow) || (statusFlags & paInputOverflow)) {
|
2023-06-15 07:48:45 +00:00
|
|
|
_streamError = se::inputXRun;
|
|
|
|
return paAbort;
|
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
if ((statusFlags & paOutputUnderflow) || (statusFlags & paOutputOverflow)) {
|
2023-06-15 07:48:45 +00:00
|
|
|
_streamError = se::outputXRun;
|
|
|
|
return paAbort;
|
|
|
|
}
|
2024-02-06 13:59:51 +00:00
|
|
|
if (framesPerBuffer != framesPerBlock()) {
|
2023-06-15 07:48:45 +00:00
|
|
|
cerr << "Logic error: expected a block size of: " << framesPerBlock()
|
|
|
|
<< endl;
|
|
|
|
_streamError = se::logicError;
|
|
|
|
return paAbort;
|
|
|
|
}
|
|
|
|
|
|
|
|
const us neninchannels = this->neninchannels();
|
|
|
|
const us nenoutchannels = this->nenoutchannels();
|
|
|
|
const auto &dtype_descr = dtypeDescr();
|
|
|
|
const auto dtype = dataType();
|
|
|
|
const us sw = dtype_descr.sw;
|
2024-02-06 13:59:51 +00:00
|
|
|
if (inputBuffer) {
|
2023-06-15 07:48:45 +00:00
|
|
|
assert(_incallback);
|
|
|
|
std::vector<byte_t *> ptrs;
|
|
|
|
ptrs.reserve(neninchannels);
|
|
|
|
|
|
|
|
const us ch_min = getLowestEnabledInChannel();
|
|
|
|
const us ch_max = getHighestEnabledInChannel();
|
|
|
|
assert(ch_min < ninchannels);
|
|
|
|
assert(ch_max < ninchannels);
|
|
|
|
|
|
|
|
/// Only pass on the pointers of the channels we want. inputBuffer is
|
|
|
|
/// noninterleaved, as specified in PortAudioDaq constructor.
|
2024-02-06 13:59:51 +00:00
|
|
|
for (us ch = ch_min; ch <= ch_max; ch++) {
|
|
|
|
if (inchannel_config.at(ch).enabled) {
|
2023-06-15 07:48:45 +00:00
|
|
|
byte_t *ch_ptr =
|
|
|
|
reinterpret_cast<byte_t **>(const_cast<void *>(inputBuffer))[ch];
|
|
|
|
ptrs.push_back(ch_ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DaqData d{framesPerBuffer, neninchannels, dtype};
|
|
|
|
d.copyInFromRaw(ptrs);
|
|
|
|
|
|
|
|
_incallback(d);
|
|
|
|
}
|
|
|
|
|
2024-02-06 13:59:51 +00:00
|
|
|
if (outputBuffer) {
|
2023-06-15 07:48:45 +00:00
|
|
|
assert(_outcallback);
|
|
|
|
std::vector<byte_t *> ptrs;
|
|
|
|
ptrs.reserve(nenoutchannels);
|
|
|
|
|
|
|
|
/* outCallback */
|
|
|
|
|
|
|
|
const us ch_min = getLowestEnabledOutChannel();
|
|
|
|
const us ch_max = getHighestEnabledOutChannel();
|
|
|
|
assert(ch_min < noutchannels);
|
|
|
|
assert(ch_max < noutchannels);
|
|
|
|
/// Only pass on the pointers of the channels we want
|
2024-02-06 13:59:51 +00:00
|
|
|
for (us ch = ch_min; ch <= ch_max; ch++) {
|
|
|
|
if (outchannel_config.at(ch).enabled) {
|
2023-06-15 07:48:45 +00:00
|
|
|
byte_t *ch_ptr = reinterpret_cast<byte_t **>(outputBuffer)[ch];
|
|
|
|
ptrs.push_back(ch_ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DaqData d{framesPerBuffer, nenoutchannels, dtype};
|
|
|
|
|
|
|
|
_outcallback(d);
|
|
|
|
// Copy over the buffer
|
|
|
|
us j = 0;
|
2024-02-06 13:59:51 +00:00
|
|
|
for (auto ptr : ptrs) {
|
2023-06-15 07:48:45 +00:00
|
|
|
d.copyToRaw(j, ptr);
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
2023-06-14 19:23:53 +00:00
|
|
|
|
|
|
|
return paContinue;
|
|
|
|
}
|
|
|
|
#endif
|