2009-03-10 00:55:29 -07:00
|
|
|
/**
|
|
|
|
* OpenAL cross platform audio library
|
|
|
|
* Copyright (C) 1999-2007 by authors.
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2014-08-18 14:11:03 +02:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2009-03-10 00:55:29 -07:00
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2018-11-15 23:50:15 -08:00
|
|
|
#include "backends/portaudio.h"
|
|
|
|
|
2019-01-09 19:42:40 +01:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2012-09-14 02:14:29 -07:00
|
|
|
|
2019-07-28 18:33:29 -07:00
|
|
|
#include "alcmain.h"
|
2019-10-07 21:37:56 -07:00
|
|
|
#include "alexcpt.h"
|
2012-09-14 02:14:29 -07:00
|
|
|
#include "alu.h"
|
2018-01-11 07:56:54 -08:00
|
|
|
#include "alconfig.h"
|
2020-12-17 01:25:33 -08:00
|
|
|
#include "core/logging.h"
|
2019-08-10 21:54:30 -07:00
|
|
|
#include "dynload.h"
|
2018-01-11 09:16:28 -08:00
|
|
|
#include "ringbuffer.h"
|
2009-03-10 00:55:29 -07:00
|
|
|
|
|
|
|
#include <portaudio.h>
|
|
|
|
|
2011-06-12 04:37:32 -07:00
|
|
|
|
2018-11-15 23:50:15 -08:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr ALCchar pa_device[] = "PortAudio Default";
|
2011-06-12 04:37:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_DYNLOAD
|
2018-11-15 23:50:15 -08:00
|
|
|
void *pa_handle;
|
|
|
|
#define MAKE_FUNC(x) decltype(x) * p##x
|
2009-03-10 00:55:29 -07:00
|
|
|
MAKE_FUNC(Pa_Initialize);
|
2009-09-15 22:45:27 -07:00
|
|
|
MAKE_FUNC(Pa_Terminate);
|
2009-03-10 00:55:29 -07:00
|
|
|
MAKE_FUNC(Pa_GetErrorText);
|
|
|
|
MAKE_FUNC(Pa_StartStream);
|
|
|
|
MAKE_FUNC(Pa_StopStream);
|
|
|
|
MAKE_FUNC(Pa_OpenStream);
|
|
|
|
MAKE_FUNC(Pa_CloseStream);
|
|
|
|
MAKE_FUNC(Pa_GetDefaultOutputDevice);
|
2014-08-08 19:48:45 -07:00
|
|
|
MAKE_FUNC(Pa_GetDefaultInputDevice);
|
2009-09-15 23:14:14 -07:00
|
|
|
MAKE_FUNC(Pa_GetStreamInfo);
|
2009-03-10 00:55:29 -07:00
|
|
|
#undef MAKE_FUNC
|
|
|
|
|
2018-11-07 18:26:33 -08:00
|
|
|
#ifndef IN_IDE_PARSER
|
2011-06-12 04:37:32 -07:00
|
|
|
#define Pa_Initialize pPa_Initialize
|
|
|
|
#define Pa_Terminate pPa_Terminate
|
|
|
|
#define Pa_GetErrorText pPa_GetErrorText
|
|
|
|
#define Pa_StartStream pPa_StartStream
|
|
|
|
#define Pa_StopStream pPa_StopStream
|
|
|
|
#define Pa_OpenStream pPa_OpenStream
|
|
|
|
#define Pa_CloseStream pPa_CloseStream
|
|
|
|
#define Pa_GetDefaultOutputDevice pPa_GetDefaultOutputDevice
|
2014-08-08 19:48:45 -07:00
|
|
|
#define Pa_GetDefaultInputDevice pPa_GetDefaultInputDevice
|
2011-06-12 04:37:32 -07:00
|
|
|
#define Pa_GetStreamInfo pPa_GetStreamInfo
|
2011-06-26 15:40:15 -07:00
|
|
|
#endif
|
2018-11-07 18:26:33 -08:00
|
|
|
#endif
|
2009-09-27 00:21:40 -07:00
|
|
|
|
2009-03-10 00:55:29 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
struct PortPlayback final : public BackendBase {
|
|
|
|
PortPlayback(ALCdevice *device) noexcept : BackendBase{device} { }
|
|
|
|
~PortPlayback() override;
|
2018-12-27 23:37:24 -08:00
|
|
|
|
2019-10-09 03:29:25 -07:00
|
|
|
int writeCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer,
|
|
|
|
const PaStreamCallbackTimeInfo *timeInfo, const PaStreamCallbackFlags statusFlags) noexcept;
|
2018-12-27 23:37:24 -08:00
|
|
|
static int writeCallbackC(const void *inputBuffer, void *outputBuffer,
|
|
|
|
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
|
2019-10-09 03:29:25 -07:00
|
|
|
const PaStreamCallbackFlags statusFlags, void *userData) noexcept
|
2019-10-07 21:37:56 -07:00
|
|
|
{
|
|
|
|
return static_cast<PortPlayback*>(userData)->writeCallback(inputBuffer, outputBuffer,
|
|
|
|
framesPerBuffer, timeInfo, statusFlags);
|
|
|
|
}
|
2018-12-27 23:37:24 -08:00
|
|
|
|
2019-10-07 21:37:56 -07:00
|
|
|
void open(const ALCchar *name) override;
|
2019-09-15 09:50:28 -07:00
|
|
|
bool reset() override;
|
2020-04-28 19:25:58 -07:00
|
|
|
void start() override;
|
2018-12-28 22:56:20 -08:00
|
|
|
void stop() override;
|
|
|
|
|
2018-12-27 17:09:14 -08:00
|
|
|
PaStream *mStream{nullptr};
|
|
|
|
PaStreamParameters mParams{};
|
|
|
|
ALuint mUpdateSize{0u};
|
2009-03-10 00:55:29 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
DEF_NEWDEL(PortPlayback)
|
|
|
|
};
|
2018-12-27 23:37:24 -08:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
PortPlayback::~PortPlayback()
|
2009-03-10 00:55:29 -07:00
|
|
|
{
|
2018-12-27 23:37:24 -08:00
|
|
|
PaError err{mStream ? Pa_CloseStream(mStream) : paNoError};
|
2018-01-29 01:00:53 -08:00
|
|
|
if(err != paNoError)
|
|
|
|
ERR("Error closing stream: %s\n", Pa_GetErrorText(err));
|
2018-12-27 23:37:24 -08:00
|
|
|
mStream = nullptr;
|
2009-03-10 00:55:29 -07:00
|
|
|
}
|
|
|
|
|
2015-10-22 10:46:36 -07:00
|
|
|
|
2019-10-09 03:29:25 -07:00
|
|
|
int PortPlayback::writeCallback(const void*, void *outputBuffer, unsigned long framesPerBuffer,
|
|
|
|
const PaStreamCallbackTimeInfo*, const PaStreamCallbackFlags) noexcept
|
2018-12-27 23:37:24 -08:00
|
|
|
{
|
2020-08-07 06:17:12 -07:00
|
|
|
mDevice->renderSamples(outputBuffer, static_cast<ALuint>(framesPerBuffer),
|
2020-04-13 23:36:05 -07:00
|
|
|
static_cast<ALuint>(mParams.channelCount));
|
2010-03-18 01:58:25 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-10 00:55:29 -07:00
|
|
|
|
2019-10-07 21:37:56 -07:00
|
|
|
void PortPlayback::open(const ALCchar *name)
|
2009-03-10 00:55:29 -07:00
|
|
|
{
|
2015-10-22 10:46:36 -07:00
|
|
|
if(!name)
|
|
|
|
name = pa_device;
|
|
|
|
else if(strcmp(name, pa_device) != 0)
|
2020-12-17 21:07:53 -08:00
|
|
|
throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%s\" not found",
|
|
|
|
name};
|
2009-03-10 00:55:29 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
mUpdateSize = mDevice->UpdateSize;
|
2010-02-24 18:51:57 -08:00
|
|
|
|
2019-06-30 12:00:10 -07:00
|
|
|
auto devidopt = ConfigValueInt(nullptr, "port", "device");
|
|
|
|
if(devidopt && *devidopt >= 0) mParams.device = *devidopt;
|
|
|
|
else mParams.device = Pa_GetDefaultOutputDevice();
|
2019-04-26 15:58:25 -07:00
|
|
|
mParams.suggestedLatency = mDevice->BufferSize / static_cast<double>(mDevice->Frequency);
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.hostApiSpecificStreamInfo = nullptr;
|
2009-03-10 00:55:29 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.channelCount = ((mDevice->FmtChans == DevFmtMono) ? 1 : 2);
|
2012-01-17 15:26:22 -08:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
switch(mDevice->FmtType)
|
2009-03-10 00:55:29 -07:00
|
|
|
{
|
2019-10-07 21:37:56 -07:00
|
|
|
case DevFmtByte:
|
|
|
|
mParams.sampleFormat = paInt8;
|
|
|
|
break;
|
|
|
|
case DevFmtUByte:
|
|
|
|
mParams.sampleFormat = paUInt8;
|
|
|
|
break;
|
|
|
|
case DevFmtUShort:
|
|
|
|
/* fall-through */
|
|
|
|
case DevFmtShort:
|
|
|
|
mParams.sampleFormat = paInt16;
|
|
|
|
break;
|
|
|
|
case DevFmtUInt:
|
|
|
|
/* fall-through */
|
|
|
|
case DevFmtInt:
|
|
|
|
mParams.sampleFormat = paInt32;
|
|
|
|
break;
|
|
|
|
case DevFmtFloat:
|
|
|
|
mParams.sampleFormat = paFloat32;
|
|
|
|
break;
|
2009-03-10 00:55:29 -07:00
|
|
|
}
|
2009-12-02 04:03:51 -08:00
|
|
|
|
2012-03-13 22:18:51 -07:00
|
|
|
retry_open:
|
2018-12-28 22:56:20 -08:00
|
|
|
PaError err{Pa_OpenStream(&mStream, nullptr, &mParams, mDevice->Frequency, mDevice->UpdateSize,
|
|
|
|
paNoFlag, &PortPlayback::writeCallbackC, this)};
|
2009-03-10 00:55:29 -07:00
|
|
|
if(err != paNoError)
|
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
if(mParams.sampleFormat == paFloat32)
|
2012-01-17 15:26:22 -08:00
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.sampleFormat = paInt16;
|
2012-01-17 15:26:22 -08:00
|
|
|
goto retry_open;
|
|
|
|
}
|
2020-12-17 21:07:53 -08:00
|
|
|
throw al::backend_exception{al::backend_error::NoDevice, "Failed to open stream: %s",
|
2019-10-07 21:37:56 -07:00
|
|
|
Pa_GetErrorText(err)};
|
2009-03-10 00:55:29 -07:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
mDevice->DeviceName = name;
|
2009-03-10 00:55:29 -07:00
|
|
|
}
|
|
|
|
|
2019-09-15 09:50:28 -07:00
|
|
|
bool PortPlayback::reset()
|
2009-08-13 12:28:46 -07:00
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
const PaStreamInfo *streamInfo{Pa_GetStreamInfo(mStream)};
|
2019-09-13 14:29:25 -07:00
|
|
|
mDevice->Frequency = static_cast<ALuint>(streamInfo->sampleRate);
|
2018-12-28 22:56:20 -08:00
|
|
|
mDevice->UpdateSize = mUpdateSize;
|
|
|
|
|
|
|
|
if(mParams.sampleFormat == paInt8)
|
|
|
|
mDevice->FmtType = DevFmtByte;
|
|
|
|
else if(mParams.sampleFormat == paUInt8)
|
|
|
|
mDevice->FmtType = DevFmtUByte;
|
|
|
|
else if(mParams.sampleFormat == paInt16)
|
|
|
|
mDevice->FmtType = DevFmtShort;
|
|
|
|
else if(mParams.sampleFormat == paInt32)
|
|
|
|
mDevice->FmtType = DevFmtInt;
|
|
|
|
else if(mParams.sampleFormat == paFloat32)
|
|
|
|
mDevice->FmtType = DevFmtFloat;
|
2012-03-13 22:18:51 -07:00
|
|
|
else
|
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
ERR("Unexpected sample format: 0x%lx\n", mParams.sampleFormat);
|
2019-09-15 09:50:28 -07:00
|
|
|
return false;
|
2012-03-13 22:18:51 -07:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
if(mParams.channelCount == 2)
|
|
|
|
mDevice->FmtChans = DevFmtStereo;
|
|
|
|
else if(mParams.channelCount == 1)
|
|
|
|
mDevice->FmtChans = DevFmtMono;
|
2012-03-13 22:18:51 -07:00
|
|
|
else
|
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
ERR("Unexpected channel count: %u\n", mParams.channelCount);
|
2019-09-15 09:50:28 -07:00
|
|
|
return false;
|
2012-03-13 22:18:51 -07:00
|
|
|
}
|
2020-06-12 11:58:41 -07:00
|
|
|
setDefaultChannelOrder();
|
2012-03-13 22:18:51 -07:00
|
|
|
|
2019-09-15 09:50:28 -07:00
|
|
|
return true;
|
2012-03-05 07:11:09 -08:00
|
|
|
}
|
|
|
|
|
2020-04-28 19:25:58 -07:00
|
|
|
void PortPlayback::start()
|
2012-03-05 07:11:09 -08:00
|
|
|
{
|
2020-04-28 19:25:58 -07:00
|
|
|
const PaError err{Pa_StartStream(mStream)};
|
|
|
|
if(err == paNoError)
|
2020-12-17 21:07:53 -08:00
|
|
|
throw al::backend_exception{al::backend_error::DeviceError, "Failed to start playback: %s",
|
2020-04-28 19:25:58 -07:00
|
|
|
Pa_GetErrorText(err)};
|
2009-08-13 12:28:46 -07:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
void PortPlayback::stop()
|
2009-08-13 12:28:46 -07:00
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
PaError err{Pa_StopStream(mStream)};
|
2009-12-26 08:14:28 -08:00
|
|
|
if(err != paNoError)
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("Error stopping stream: %s\n", Pa_GetErrorText(err));
|
2009-08-13 12:28:46 -07:00
|
|
|
}
|
|
|
|
|
2009-03-10 00:55:29 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
struct PortCapture final : public BackendBase {
|
|
|
|
PortCapture(ALCdevice *device) noexcept : BackendBase{device} { }
|
|
|
|
~PortCapture() override;
|
2018-12-27 23:37:24 -08:00
|
|
|
|
2019-10-09 03:29:25 -07:00
|
|
|
int readCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer,
|
|
|
|
const PaStreamCallbackTimeInfo *timeInfo, const PaStreamCallbackFlags statusFlags) noexcept;
|
2018-12-27 23:37:24 -08:00
|
|
|
static int readCallbackC(const void *inputBuffer, void *outputBuffer,
|
|
|
|
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
|
2019-10-09 03:29:25 -07:00
|
|
|
const PaStreamCallbackFlags statusFlags, void *userData) noexcept
|
2019-10-07 21:37:56 -07:00
|
|
|
{
|
|
|
|
return static_cast<PortCapture*>(userData)->readCallback(inputBuffer, outputBuffer,
|
|
|
|
framesPerBuffer, timeInfo, statusFlags);
|
|
|
|
}
|
2018-12-27 23:37:24 -08:00
|
|
|
|
2019-10-07 21:37:56 -07:00
|
|
|
void open(const ALCchar *name) override;
|
2020-04-28 19:25:58 -07:00
|
|
|
void start() override;
|
2018-12-28 22:56:20 -08:00
|
|
|
void stop() override;
|
2020-12-17 03:06:52 -08:00
|
|
|
void captureSamples(al::byte *buffer, uint samples) override;
|
|
|
|
uint availableSamples() override;
|
2018-12-28 22:56:20 -08:00
|
|
|
|
2018-12-27 17:09:14 -08:00
|
|
|
PaStream *mStream{nullptr};
|
|
|
|
PaStreamParameters mParams;
|
2015-10-22 10:46:36 -07:00
|
|
|
|
2018-12-27 17:09:14 -08:00
|
|
|
RingBufferPtr mRing{nullptr};
|
2015-10-22 10:46:36 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
DEF_NEWDEL(PortCapture)
|
|
|
|
};
|
2015-10-22 10:46:36 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
PortCapture::~PortCapture()
|
2015-10-22 10:46:36 -07:00
|
|
|
{
|
2018-12-27 23:37:24 -08:00
|
|
|
PaError err{mStream ? Pa_CloseStream(mStream) : paNoError};
|
2018-01-29 01:00:53 -08:00
|
|
|
if(err != paNoError)
|
|
|
|
ERR("Error closing stream: %s\n", Pa_GetErrorText(err));
|
2018-12-27 23:37:24 -08:00
|
|
|
mStream = nullptr;
|
2015-10-22 10:46:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-09 03:29:25 -07:00
|
|
|
int PortCapture::readCallback(const void *inputBuffer, void*, unsigned long framesPerBuffer,
|
|
|
|
const PaStreamCallbackTimeInfo*, const PaStreamCallbackFlags) noexcept
|
2015-10-22 10:46:36 -07:00
|
|
|
{
|
2018-12-27 23:37:24 -08:00
|
|
|
mRing->write(inputBuffer, framesPerBuffer);
|
2015-10-22 10:46:36 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-07 21:37:56 -07:00
|
|
|
void PortCapture::open(const ALCchar *name)
|
2009-03-10 00:55:29 -07:00
|
|
|
{
|
2015-10-22 10:46:36 -07:00
|
|
|
if(!name)
|
|
|
|
name = pa_device;
|
|
|
|
else if(strcmp(name, pa_device) != 0)
|
2020-12-17 21:07:53 -08:00
|
|
|
throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%s\" not found",
|
|
|
|
name};
|
2010-03-18 01:58:25 -07:00
|
|
|
|
2019-04-26 15:58:25 -07:00
|
|
|
ALuint samples{mDevice->BufferSize};
|
2018-12-28 22:56:20 -08:00
|
|
|
samples = maxu(samples, 100 * mDevice->Frequency / 1000);
|
2019-09-13 14:29:25 -07:00
|
|
|
ALuint frame_size{mDevice->frameSizeFromFmt()};
|
2010-03-18 01:58:25 -07:00
|
|
|
|
2020-01-10 07:56:43 -08:00
|
|
|
mRing = RingBuffer::Create(samples, frame_size, false);
|
2015-10-22 10:46:36 -07:00
|
|
|
|
2019-06-30 12:00:10 -07:00
|
|
|
auto devidopt = ConfigValueInt(nullptr, "port", "capture");
|
|
|
|
if(devidopt && *devidopt >= 0) mParams.device = *devidopt;
|
|
|
|
else mParams.device = Pa_GetDefaultOutputDevice();
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.suggestedLatency = 0.0f;
|
|
|
|
mParams.hostApiSpecificStreamInfo = nullptr;
|
2010-03-18 01:58:25 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
switch(mDevice->FmtType)
|
2010-03-18 01:58:25 -07:00
|
|
|
{
|
2019-10-07 21:37:56 -07:00
|
|
|
case DevFmtByte:
|
|
|
|
mParams.sampleFormat = paInt8;
|
|
|
|
break;
|
|
|
|
case DevFmtUByte:
|
|
|
|
mParams.sampleFormat = paUInt8;
|
|
|
|
break;
|
|
|
|
case DevFmtShort:
|
|
|
|
mParams.sampleFormat = paInt16;
|
|
|
|
break;
|
|
|
|
case DevFmtInt:
|
|
|
|
mParams.sampleFormat = paInt32;
|
|
|
|
break;
|
|
|
|
case DevFmtFloat:
|
|
|
|
mParams.sampleFormat = paFloat32;
|
|
|
|
break;
|
|
|
|
case DevFmtUInt:
|
|
|
|
case DevFmtUShort:
|
2020-12-17 21:07:53 -08:00
|
|
|
throw al::backend_exception{al::backend_error::DeviceError, "%s samples not supported",
|
2019-10-07 21:37:56 -07:00
|
|
|
DevFmtTypeString(mDevice->FmtType)};
|
2010-03-18 01:58:25 -07:00
|
|
|
}
|
2019-09-13 14:29:25 -07:00
|
|
|
mParams.channelCount = static_cast<int>(mDevice->channelsFromFmt());
|
2010-03-18 01:58:25 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
PaError err{Pa_OpenStream(&mStream, &mParams, nullptr, mDevice->Frequency,
|
|
|
|
paFramesPerBufferUnspecified, paNoFlag, &PortCapture::readCallbackC, this)};
|
2010-03-18 01:58:25 -07:00
|
|
|
if(err != paNoError)
|
2020-12-17 21:07:53 -08:00
|
|
|
throw al::backend_exception{al::backend_error::NoDevice, "Failed to open stream: %s",
|
2019-10-07 21:37:56 -07:00
|
|
|
Pa_GetErrorText(err)};
|
2010-03-18 01:58:25 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
mDevice->DeviceName = name;
|
2009-03-10 00:55:29 -07:00
|
|
|
}
|
|
|
|
|
2010-03-18 01:58:25 -07:00
|
|
|
|
2020-04-28 19:25:58 -07:00
|
|
|
void PortCapture::start()
|
2015-10-22 10:46:36 -07:00
|
|
|
{
|
2020-04-28 19:25:58 -07:00
|
|
|
const PaError err{Pa_StartStream(mStream)};
|
2010-03-18 01:58:25 -07:00
|
|
|
if(err != paNoError)
|
2020-12-17 21:07:53 -08:00
|
|
|
throw al::backend_exception{al::backend_error::DeviceError,
|
|
|
|
"Failed to start recording: %s", Pa_GetErrorText(err)};
|
2010-03-18 01:58:25 -07:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
void PortCapture::stop()
|
2010-03-18 01:58:25 -07:00
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
PaError err{Pa_StopStream(mStream)};
|
2010-03-18 01:58:25 -07:00
|
|
|
if(err != paNoError)
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("Error stopping stream: %s\n", Pa_GetErrorText(err));
|
2010-03-18 01:58:25 -07:00
|
|
|
}
|
|
|
|
|
2015-10-22 10:46:36 -07:00
|
|
|
|
2020-12-17 03:06:52 -08:00
|
|
|
uint PortCapture::availableSamples()
|
|
|
|
{ return static_cast<uint>(mRing->readSpace()); }
|
2010-03-18 01:58:25 -07:00
|
|
|
|
2020-12-17 03:06:52 -08:00
|
|
|
void PortCapture::captureSamples(al::byte *buffer, uint samples)
|
|
|
|
{ mRing->read(buffer, samples); }
|
2009-03-10 00:55:29 -07:00
|
|
|
|
2018-11-15 23:50:15 -08:00
|
|
|
} // namespace
|
2015-10-22 10:46:36 -07:00
|
|
|
|
|
|
|
|
2018-11-15 23:50:15 -08:00
|
|
|
bool PortBackendFactory::init()
|
2009-08-26 23:45:00 -07:00
|
|
|
{
|
2019-04-14 02:16:42 -07:00
|
|
|
PaError err;
|
|
|
|
|
2012-03-01 03:37:06 -08:00
|
|
|
#ifdef HAVE_DYNLOAD
|
2019-04-14 02:16:42 -07:00
|
|
|
if(!pa_handle)
|
2010-05-28 02:22:17 -07:00
|
|
|
{
|
2019-04-14 02:16:42 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
# define PALIB "portaudio.dll"
|
|
|
|
#elif defined(__APPLE__) && defined(__MACH__)
|
|
|
|
# define PALIB "libportaudio.2.dylib"
|
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
# define PALIB "libportaudio.so"
|
|
|
|
#else
|
|
|
|
# define PALIB "libportaudio.so.2"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pa_handle = LoadLib(PALIB);
|
|
|
|
if(!pa_handle)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
#define LOAD_FUNC(f) do { \
|
|
|
|
p##f = reinterpret_cast<decltype(p##f)>(GetSymbol(pa_handle, #f)); \
|
|
|
|
if(p##f == nullptr) \
|
|
|
|
{ \
|
|
|
|
CloseLib(pa_handle); \
|
|
|
|
pa_handle = nullptr; \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
} while(0)
|
|
|
|
LOAD_FUNC(Pa_Initialize);
|
|
|
|
LOAD_FUNC(Pa_Terminate);
|
|
|
|
LOAD_FUNC(Pa_GetErrorText);
|
|
|
|
LOAD_FUNC(Pa_StartStream);
|
|
|
|
LOAD_FUNC(Pa_StopStream);
|
|
|
|
LOAD_FUNC(Pa_OpenStream);
|
|
|
|
LOAD_FUNC(Pa_CloseStream);
|
|
|
|
LOAD_FUNC(Pa_GetDefaultOutputDevice);
|
|
|
|
LOAD_FUNC(Pa_GetDefaultInputDevice);
|
|
|
|
LOAD_FUNC(Pa_GetStreamInfo);
|
|
|
|
#undef LOAD_FUNC
|
|
|
|
|
|
|
|
if((err=Pa_Initialize()) != paNoError)
|
|
|
|
{
|
|
|
|
ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err));
|
|
|
|
CloseLib(pa_handle);
|
|
|
|
pa_handle = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-28 02:22:17 -07:00
|
|
|
}
|
2012-03-01 03:37:06 -08:00
|
|
|
#else
|
2019-04-14 02:16:42 -07:00
|
|
|
if((err=Pa_Initialize()) != paNoError)
|
|
|
|
{
|
|
|
|
ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err));
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-01 03:37:06 -08:00
|
|
|
#endif
|
2019-04-14 02:16:42 -07:00
|
|
|
return true;
|
2009-08-26 23:45:00 -07:00
|
|
|
}
|
2009-08-27 06:09:33 -07:00
|
|
|
|
2018-12-29 01:38:26 -08:00
|
|
|
bool PortBackendFactory::querySupport(BackendType type)
|
|
|
|
{ return (type == BackendType::Playback || type == BackendType::Capture); }
|
2015-10-22 10:46:36 -07:00
|
|
|
|
2020-03-30 16:00:02 -07:00
|
|
|
std::string PortBackendFactory::probe(BackendType type)
|
2009-08-27 06:09:33 -07:00
|
|
|
{
|
2020-03-30 15:37:41 -07:00
|
|
|
std::string outnames;
|
2011-06-14 04:02:58 -07:00
|
|
|
switch(type)
|
|
|
|
{
|
2020-03-30 16:00:02 -07:00
|
|
|
case BackendType::Playback:
|
|
|
|
case BackendType::Capture:
|
2020-03-30 15:37:41 -07:00
|
|
|
/* Includes null char. */
|
|
|
|
outnames.append(pa_device, sizeof(pa_device));
|
|
|
|
break;
|
2011-06-14 04:02:58 -07:00
|
|
|
}
|
2020-03-30 15:37:41 -07:00
|
|
|
return outnames;
|
2009-08-27 06:09:33 -07:00
|
|
|
}
|
2015-10-22 10:46:36 -07:00
|
|
|
|
2018-12-29 02:16:16 -08:00
|
|
|
BackendPtr PortBackendFactory::createBackend(ALCdevice *device, BackendType type)
|
2015-10-22 10:46:36 -07:00
|
|
|
{
|
2018-12-29 01:38:26 -08:00
|
|
|
if(type == BackendType::Playback)
|
2018-12-29 02:16:16 -08:00
|
|
|
return BackendPtr{new PortPlayback{device}};
|
2018-12-29 01:38:26 -08:00
|
|
|
if(type == BackendType::Capture)
|
2018-12-29 02:16:16 -08:00
|
|
|
return BackendPtr{new PortCapture{device}};
|
2018-11-12 23:32:11 -08:00
|
|
|
return nullptr;
|
2015-10-22 10:46:36 -07:00
|
|
|
}
|
|
|
|
|
2018-11-15 23:50:15 -08:00
|
|
|
BackendFactory &PortBackendFactory::getFactory()
|
2015-10-22 10:46:36 -07:00
|
|
|
{
|
2018-11-15 23:50:15 -08:00
|
|
|
static PortBackendFactory factory{};
|
|
|
|
return factory;
|
2015-10-22 10:46:36 -07:00
|
|
|
}
|