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"
|
|
|
|
|
2009-03-10 00:55:29 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-09-14 02:14:29 -07:00
|
|
|
|
2009-03-10 00:55:29 -07:00
|
|
|
#include "alMain.h"
|
2012-09-14 02:14:29 -07:00
|
|
|
#include "alu.h"
|
2018-01-11 07:56:54 -08:00
|
|
|
#include "alconfig.h"
|
2018-01-11 09:16:28 -08:00
|
|
|
#include "ringbuffer.h"
|
2013-10-28 17:48:03 -07:00
|
|
|
#include "compat.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
|
|
|
|
2018-11-15 23:50:15 -08:00
|
|
|
bool pa_load(void)
|
2009-09-27 00:21:40 -07:00
|
|
|
{
|
2012-03-01 03:37:06 -08:00
|
|
|
PaError err;
|
2010-03-21 20:57:15 -07:00
|
|
|
|
2011-06-26 15:40:15 -07:00
|
|
|
#ifdef HAVE_DYNLOAD
|
2012-03-01 03:37:06 -08:00
|
|
|
if(!pa_handle)
|
|
|
|
{
|
2010-03-21 20:57:15 -07:00
|
|
|
#ifdef _WIN32
|
2011-06-12 04:37:32 -07:00
|
|
|
# define PALIB "portaudio.dll"
|
|
|
|
#elif defined(__APPLE__) && defined(__MACH__)
|
2009-09-27 00:21:40 -07:00
|
|
|
# define PALIB "libportaudio.2.dylib"
|
2011-06-22 16:43:37 -07:00
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
# define PALIB "libportaudio.so"
|
2009-09-27 00:21:40 -07:00
|
|
|
#else
|
|
|
|
# define PALIB "libportaudio.so.2"
|
|
|
|
#endif
|
|
|
|
|
2011-06-12 04:37:32 -07:00
|
|
|
pa_handle = LoadLib(PALIB);
|
2010-03-23 17:45:37 -07:00
|
|
|
if(!pa_handle)
|
2018-11-15 23:50:15 -08:00
|
|
|
return false;
|
2010-03-23 17:45:37 -07:00
|
|
|
|
2011-06-12 04:37:32 -07:00
|
|
|
#define LOAD_FUNC(f) do { \
|
2018-11-12 23:32:11 -08:00
|
|
|
p##f = reinterpret_cast<decltype(p##f)>(GetSymbol(pa_handle, #f)); \
|
|
|
|
if(p##f == nullptr) \
|
2011-06-12 04:37:32 -07:00
|
|
|
{ \
|
|
|
|
CloseLib(pa_handle); \
|
2018-11-12 23:32:11 -08:00
|
|
|
pa_handle = nullptr; \
|
2018-11-15 23:50:15 -08:00
|
|
|
return false; \
|
2011-06-12 04:37:32 -07:00
|
|
|
} \
|
|
|
|
} 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);
|
2014-08-08 19:48:45 -07:00
|
|
|
LOAD_FUNC(Pa_GetDefaultInputDevice);
|
2011-06-12 04:37:32 -07:00
|
|
|
LOAD_FUNC(Pa_GetStreamInfo);
|
2009-09-27 00:21:40 -07:00
|
|
|
#undef LOAD_FUNC
|
|
|
|
|
2011-06-12 04:37:32 -07:00
|
|
|
if((err=Pa_Initialize()) != paNoError)
|
2009-09-27 00:21:40 -07:00
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err));
|
2011-06-12 04:37:32 -07:00
|
|
|
CloseLib(pa_handle);
|
2018-11-12 23:32:11 -08:00
|
|
|
pa_handle = nullptr;
|
2018-11-15 23:50:15 -08:00
|
|
|
return false;
|
2009-09-27 00:21:40 -07:00
|
|
|
}
|
|
|
|
}
|
2012-03-01 03:37:06 -08:00
|
|
|
#else
|
|
|
|
if((err=Pa_Initialize()) != paNoError)
|
|
|
|
{
|
|
|
|
ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err));
|
2018-11-15 23:50:15 -08:00
|
|
|
return false;
|
2012-03-01 03:37:06 -08:00
|
|
|
}
|
|
|
|
#endif
|
2018-11-15 23:50:15 -08:00
|
|
|
return true;
|
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
|
|
|
|
|
|
|
static int writeCallbackC(const void *inputBuffer, void *outputBuffer,
|
|
|
|
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
|
|
|
|
const PaStreamCallbackFlags statusFlags, void *userData);
|
|
|
|
int writeCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer,
|
|
|
|
const PaStreamCallbackTimeInfo *timeInfo, const PaStreamCallbackFlags statusFlags);
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
ALCenum open(const ALCchar *name) override;
|
|
|
|
ALCboolean reset() override;
|
|
|
|
ALCboolean start() override;
|
|
|
|
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
|
|
|
static constexpr inline const char *CurrentPrefix() noexcept { return "PortPlayback::"; }
|
|
|
|
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
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
int PortPlayback::writeCallbackC(const void *inputBuffer, void *outputBuffer,
|
2018-12-27 23:37:24 -08:00
|
|
|
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
|
|
|
|
const PaStreamCallbackFlags statusFlags, void *userData)
|
2010-03-18 01:58:25 -07:00
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
return static_cast<PortPlayback*>(userData)->writeCallback(inputBuffer, outputBuffer,
|
2018-12-27 23:37:24 -08:00
|
|
|
framesPerBuffer, timeInfo, statusFlags);
|
|
|
|
}
|
2010-03-18 01:58:25 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
int PortPlayback::writeCallback(const void* UNUSED(inputBuffer), void *outputBuffer,
|
2018-12-27 23:37:24 -08:00
|
|
|
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* UNUSED(timeInfo),
|
|
|
|
const PaStreamCallbackFlags UNUSED(statusFlags))
|
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
lock();
|
2018-12-27 23:37:24 -08:00
|
|
|
aluMixData(mDevice, outputBuffer, framesPerBuffer);
|
2018-12-28 22:56:20 -08:00
|
|
|
unlock();
|
2010-03-18 01:58:25 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-10 00:55:29 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
ALCenum 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)
|
2011-08-24 14:24:48 -07:00
|
|
|
return ALC_INVALID_VALUE;
|
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
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.device = -1;
|
|
|
|
if(!ConfigValueInt(nullptr, "port", "device", &mParams.device) || mParams.device < 0)
|
|
|
|
mParams.device = Pa_GetDefaultOutputDevice();
|
|
|
|
mParams.suggestedLatency = (mDevice->UpdateSize*mDevice->NumUpdates) /
|
|
|
|
(float)mDevice->Frequency;
|
|
|
|
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
|
|
|
{
|
2010-12-04 19:50:00 -08:00
|
|
|
case DevFmtByte:
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.sampleFormat = paInt8;
|
2010-12-04 19:50:00 -08:00
|
|
|
break;
|
|
|
|
case DevFmtUByte:
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.sampleFormat = paUInt8;
|
2009-03-10 00:55:29 -07:00
|
|
|
break;
|
2010-12-04 19:50:00 -08:00
|
|
|
case DevFmtUShort:
|
|
|
|
/* fall-through */
|
|
|
|
case DevFmtShort:
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.sampleFormat = paInt16;
|
2009-03-10 00:55:29 -07:00
|
|
|
break;
|
2012-02-14 11:44:57 -08:00
|
|
|
case DevFmtUInt:
|
|
|
|
/* fall-through */
|
|
|
|
case DevFmtInt:
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.sampleFormat = paInt32;
|
2012-02-14 11:44:57 -08:00
|
|
|
break;
|
2010-12-04 19:50:00 -08:00
|
|
|
case DevFmtFloat:
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.sampleFormat = paFloat32;
|
2009-08-15 13:20:35 -07:00
|
|
|
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;
|
|
|
|
}
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err));
|
2011-08-24 14:24:48 -07:00
|
|
|
return ALC_INVALID_VALUE;
|
2009-03-10 00:55:29 -07:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
mDevice->DeviceName = name;
|
2011-08-24 14:24:48 -07:00
|
|
|
return ALC_NO_ERROR;
|
2015-10-22 10:46:36 -07:00
|
|
|
|
2009-03-10 00:55:29 -07:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
ALCboolean PortPlayback::reset()
|
2009-08-13 12:28:46 -07:00
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
const PaStreamInfo *streamInfo{Pa_GetStreamInfo(mStream)};
|
|
|
|
mDevice->Frequency = streamInfo->sampleRate;
|
|
|
|
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);
|
2012-03-13 22:18:51 -07:00
|
|
|
return ALC_FALSE;
|
|
|
|
}
|
|
|
|
|
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);
|
2012-03-13 22:18:51 -07:00
|
|
|
return ALC_FALSE;
|
|
|
|
}
|
2018-12-28 22:56:20 -08:00
|
|
|
SetDefaultChannelOrder(mDevice);
|
2012-03-13 22:18:51 -07:00
|
|
|
|
2012-03-05 07:11:09 -08:00
|
|
|
return ALC_TRUE;
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
ALCboolean PortPlayback::start()
|
2012-03-05 07:11:09 -08:00
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
PaError err{Pa_StartStream(mStream)};
|
2009-12-26 08:14:28 -08:00
|
|
|
if(err != paNoError)
|
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("Pa_StartStream() returned an error: %s\n", Pa_GetErrorText(err));
|
2009-12-26 08:14:28 -08:00
|
|
|
return ALC_FALSE;
|
|
|
|
}
|
2009-08-13 12:28:46 -07:00
|
|
|
return ALC_TRUE;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
static int readCallbackC(const void *inputBuffer, void *outputBuffer,
|
|
|
|
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
|
|
|
|
const PaStreamCallbackFlags statusFlags, void *userData);
|
|
|
|
int readCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer,
|
|
|
|
const PaStreamCallbackTimeInfo *timeInfo, const PaStreamCallbackFlags statusFlags);
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
ALCenum open(const ALCchar *name) override;
|
|
|
|
ALCboolean start() override;
|
|
|
|
void stop() override;
|
|
|
|
ALCenum captureSamples(ALCvoid *buffer, ALCuint samples) override;
|
|
|
|
ALCuint availableSamples() override;
|
|
|
|
|
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
|
|
|
static constexpr inline const char *CurrentPrefix() noexcept { return "PortCapture::"; }
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
int PortCapture::readCallbackC(const void *inputBuffer, void *outputBuffer,
|
2018-12-27 23:37:24 -08:00
|
|
|
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
|
|
|
|
const PaStreamCallbackFlags statusFlags, void* userData)
|
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
return static_cast<PortCapture*>(userData)->readCallback(inputBuffer, outputBuffer,
|
2018-12-27 23:37:24 -08:00
|
|
|
framesPerBuffer, timeInfo, statusFlags);
|
|
|
|
}
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
int PortCapture::readCallback(const void *inputBuffer, void *UNUSED(outputBuffer),
|
2015-10-22 10:46:36 -07:00
|
|
|
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *UNUSED(timeInfo),
|
2018-12-27 23:37:24 -08:00
|
|
|
const PaStreamCallbackFlags UNUSED(statusFlags))
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
ALCenum 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)
|
2011-08-24 14:44:15 -07:00
|
|
|
return ALC_INVALID_VALUE;
|
2010-03-18 01:58:25 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
ALuint samples{mDevice->UpdateSize * mDevice->NumUpdates};
|
|
|
|
samples = maxu(samples, 100 * mDevice->Frequency / 1000);
|
|
|
|
ALsizei frame_size{mDevice->frameSizeFromFmt()};
|
2010-03-18 01:58:25 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
mRing = CreateRingBuffer(samples, frame_size, false);
|
|
|
|
if(!mRing) return ALC_INVALID_VALUE;
|
2015-10-22 10:46:36 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.device = -1;
|
|
|
|
if(!ConfigValueInt(nullptr, "port", "capture", &mParams.device) || mParams.device < 0)
|
|
|
|
mParams.device = Pa_GetDefaultInputDevice();
|
|
|
|
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
|
|
|
{
|
2010-12-04 19:50:00 -08:00
|
|
|
case DevFmtByte:
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.sampleFormat = paInt8;
|
2010-12-04 19:50:00 -08:00
|
|
|
break;
|
|
|
|
case DevFmtUByte:
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.sampleFormat = paUInt8;
|
2010-03-18 01:58:25 -07:00
|
|
|
break;
|
2010-12-04 19:50:00 -08:00
|
|
|
case DevFmtShort:
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.sampleFormat = paInt16;
|
2010-03-18 01:58:25 -07:00
|
|
|
break;
|
2012-02-14 11:44:57 -08:00
|
|
|
case DevFmtInt:
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.sampleFormat = paInt32;
|
2012-02-14 11:44:57 -08:00
|
|
|
break;
|
2010-12-04 19:50:00 -08:00
|
|
|
case DevFmtFloat:
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.sampleFormat = paFloat32;
|
2010-03-18 01:58:25 -07:00
|
|
|
break;
|
2012-02-14 11:44:57 -08:00
|
|
|
case DevFmtUInt:
|
2010-12-04 19:50:00 -08:00
|
|
|
case DevFmtUShort:
|
2018-12-28 22:56:20 -08:00
|
|
|
ERR("%s samples not supported\n", DevFmtTypeString(mDevice->FmtType));
|
2015-10-22 10:46:36 -07:00
|
|
|
return ALC_INVALID_VALUE;
|
2010-03-18 01:58:25 -07:00
|
|
|
}
|
2018-12-28 22:56:20 -08:00
|
|
|
mParams.channelCount = 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)
|
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err));
|
2015-10-22 10:46:36 -07:00
|
|
|
return ALC_INVALID_VALUE;
|
2010-03-18 01:58:25 -07:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
mDevice->DeviceName = name;
|
2011-08-24 14:44:15 -07:00
|
|
|
return ALC_NO_ERROR;
|
2009-03-10 00:55:29 -07:00
|
|
|
}
|
|
|
|
|
2010-03-18 01:58:25 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
ALCboolean PortCapture::start()
|
2015-10-22 10:46:36 -07:00
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
PaError err{Pa_StartStream(mStream)};
|
2010-03-18 01:58:25 -07:00
|
|
|
if(err != paNoError)
|
2015-10-22 10:46:36 -07:00
|
|
|
{
|
2011-07-13 01:43:00 -07:00
|
|
|
ERR("Error starting stream: %s\n", Pa_GetErrorText(err));
|
2015-10-22 10:46:36 -07:00
|
|
|
return ALC_FALSE;
|
|
|
|
}
|
|
|
|
return ALC_TRUE;
|
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
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
ALCuint PortCapture::availableSamples()
|
|
|
|
{ return mRing->readSpace(); }
|
2010-03-18 01:58:25 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
ALCenum PortCapture::captureSamples(ALCvoid *buffer, ALCuint samples)
|
2010-03-18 01:58:25 -07:00
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
mRing->read(buffer, samples);
|
2015-10-22 10:46:36 -07:00
|
|
|
return ALC_NO_ERROR;
|
2010-03-18 01:58:25 -07:00
|
|
|
}
|
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()
|
|
|
|
{ return pa_load(); }
|
2018-11-12 23:32:11 -08:00
|
|
|
|
2018-11-15 23:50:15 -08:00
|
|
|
void PortBackendFactory::deinit()
|
2009-08-26 23:45:00 -07:00
|
|
|
{
|
2012-03-01 03:37:06 -08:00
|
|
|
#ifdef HAVE_DYNLOAD
|
2010-05-28 02:22:17 -07:00
|
|
|
if(pa_handle)
|
|
|
|
{
|
2011-06-12 04:37:32 -07:00
|
|
|
Pa_Terminate();
|
|
|
|
CloseLib(pa_handle);
|
2018-11-12 23:32:11 -08:00
|
|
|
pa_handle = nullptr;
|
2010-05-28 02:22:17 -07:00
|
|
|
}
|
2012-03-01 03:37:06 -08:00
|
|
|
#else
|
|
|
|
Pa_Terminate();
|
|
|
|
#endif
|
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
|
|
|
|
2018-12-24 19:29:01 -08:00
|
|
|
void PortBackendFactory::probe(DevProbe type, std::string *outnames)
|
2009-08-27 06:09:33 -07:00
|
|
|
{
|
2011-06-14 04:02:58 -07:00
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case ALL_DEVICE_PROBE:
|
|
|
|
case CAPTURE_DEVICE_PROBE:
|
2018-11-15 04:24:33 -08:00
|
|
|
/* Includes null char. */
|
|
|
|
outnames->append(pa_device, sizeof(pa_device));
|
2011-06-14 04:02:58 -07:00
|
|
|
break;
|
|
|
|
}
|
2009-08-27 06:09:33 -07:00
|
|
|
}
|
2015-10-22 10:46:36 -07:00
|
|
|
|
2018-12-29 01:38:26 -08:00
|
|
|
BackendBase *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-28 22:56:20 -08:00
|
|
|
return new PortPlayback{device};
|
2018-12-29 01:38:26 -08:00
|
|
|
if(type == BackendType::Capture)
|
2018-12-28 22:56:20 -08:00
|
|
|
return 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
|
|
|
}
|