[DEV] rework continue (better integration of pulseaudio and low level devices

This commit is contained in:
Edouard DUPIN 2015-06-14 18:32:08 +02:00
parent 4b5bbd9626
commit 7b0316a8aa
12 changed files with 666 additions and 656 deletions

View File

@ -19,26 +19,22 @@ void audio::orchestra::DeviceInfo::display(int32_t _tabNumber) const {
for (int32_t iii=0; iii<_tabNumber; ++iii) {
space += " ";
}
ATA_PRINT(space + "probe=" << probed);
ATA_PRINT(space + "mode=" << (input==true?"input":"output"));
ATA_PRINT(space + "name=" << name);
ATA_PRINT(space + "outputChannels=" << outputChannels);
ATA_PRINT(space + "inputChannels=" << inputChannels);
ATA_PRINT(space + "duplexChannels=" << duplexChannels);
ATA_PRINT(space + "isDefaultOutput=" << (isDefaultOutput==true?"true":"false"));
ATA_PRINT(space + "isDefaultInput=" << (isDefaultInput==true?"true":"false"));
ATA_PRINT(space + "rates=" << sampleRates);
ATA_PRINT(space + "native Format: " << nativeFormats);
ATA_PRINT(space + "desc=" << desc);
ATA_PRINT(space + "channel" << (channels.size()>1?"s":"") << "=" << channels.size() << " : " << channels);
ATA_PRINT(space + "rate" << (sampleRates.size()>1?"s":"") << "=" << sampleRates);
ATA_PRINT(space + "native Format" << (nativeFormats.size()>1?"s":"") << ": " << nativeFormats);
ATA_PRINT(space + "default=" << (isDefault==true?"true":"false"));
}
std::ostream& audio::orchestra::operator <<(std::ostream& _os, const audio::orchestra::DeviceInfo& _obj) {
_os << "{";
_os << "probe=" << _obj.probed << ", ";
_os << "name=" << _obj.name << ", ";
_os << "outputChannels=" << _obj.outputChannels << ", ";
_os << "inputChannels=" << _obj.inputChannels << ", ";
_os << "duplexChannels=" << _obj.duplexChannels << ", ";
_os << "isDefaultOutput=" << _obj.isDefaultOutput << ", ";
_os << "isDefaultInput=" << _obj.isDefaultInput << ", ";
_os << "description=" << _obj.desc << ", ";
_os << "channels=" << _obj.channels << ", ";
_os << "default=" << _obj.isDefault << ", ";
_os << "rates=" << _obj.sampleRates << ", ";
_os << "native Format: " << _obj.nativeFormats;
_os << "}";

View File

@ -9,6 +9,7 @@
#define __AUDIO_ORCHESTRA_DEVICE_INFO_H__
#include <audio/format.h>
#include <audio/channel.h>
namespace audio {
@ -18,24 +19,22 @@ namespace audio {
*/
class DeviceInfo {
public:
bool probed; //!< true if the device capabilities were successfully probed.
bool input; //!< true if the device in an input; false: output.
std::string name; //!< Character string device identifier.
uint32_t outputChannels; //!< Maximum output channels supported by device.
uint32_t inputChannels; //!< Maximum input channels supported by device.
uint32_t duplexChannels; //!< Maximum simultaneous input/output channels supported by device.
bool isDefaultOutput; //!< true if this is the default output device.
bool isDefaultInput; //!< true if this is the default input device.
std::string desc; //!< description of the device
std::vector<audio::channel> channels; //!< Channels interfaces.
std::vector<uint32_t> sampleRates; //!< Supported sample rates (queried from list of standard rates).
std::vector<audio::format> nativeFormats; //!< Bit mask of supported data formats.
bool isDefault; //! is default input/output
// Default constructor.
DeviceInfo() :
probed(false),
outputChannels(0),
inputChannels(0),
duplexChannels(0),
isDefaultOutput(false),
isDefaultInput(false),
nativeFormats() {}
input(false),
name(),
desc(),
channels(),
sampleRates(),
nativeFormats(),
isDefault(false) {}
void display(int32_t _tabNumber = 1) const;
};
std::ostream& operator <<(std::ostream& _os, const audio::orchestra::DeviceInfo& _obj);

View File

@ -26,14 +26,14 @@ std::vector<std::string> audio::orchestra::Interface::getListApi() {
void audio::orchestra::Interface::openApi(const std::string& _api) {
delete m_rtapi;
m_rtapi = nullptr;
delete m_api;
m_api = nullptr;
for (size_t iii=0; iii<m_apiAvaillable.size(); ++iii) {
ATA_INFO("try open " << m_apiAvaillable[iii].first);
if (_api == m_apiAvaillable[iii].first) {
ATA_INFO(" ==> call it");
m_rtapi = m_apiAvaillable[iii].second();
if (m_rtapi != nullptr) {
m_api = m_apiAvaillable[iii].second();
if (m_api != nullptr) {
return;
}
}
@ -44,7 +44,7 @@ void audio::orchestra::Interface::openApi(const std::string& _api) {
audio::orchestra::Interface::Interface() :
m_rtapi(nullptr) {
m_api(nullptr) {
ATA_DEBUG("Add interface:");
#if defined(ORCHESTRA_BUILD_JACK)
addInterface(audio::orchestra::type_jack, audio::orchestra::api::Jack::create);
@ -84,18 +84,18 @@ void audio::orchestra::Interface::addInterface(const std::string& _api, Api* (*_
enum audio::orchestra::error audio::orchestra::Interface::clear() {
ATA_INFO("Clear API ...");
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
ATA_WARNING("Interface NOT started!");
return audio::orchestra::error_none;
}
delete m_rtapi;
m_rtapi = nullptr;
delete m_api;
m_api = nullptr;
return audio::orchestra::error_none;
}
enum audio::orchestra::error audio::orchestra::Interface::instanciate(const std::string& _api) {
ATA_INFO("Instanciate API ...");
if (m_rtapi != nullptr) {
if (m_api != nullptr) {
ATA_WARNING("Interface already started!");
return audio::orchestra::error_none;
}
@ -103,15 +103,15 @@ enum audio::orchestra::error audio::orchestra::Interface::instanciate(const std:
ATA_INFO("API specified : " << _api);
// Attempt to open the specified API.
openApi(_api);
if (m_rtapi != nullptr) {
if (m_rtapi->getDeviceCount() != 0) {
if (m_api != nullptr) {
if (m_api->getDeviceCount() != 0) {
ATA_INFO(" ==> api open");
}
return audio::orchestra::error_none;
}
// No compiled support for specified API value. Issue a debug
// warning and continue as if no API was specified.
ATA_ERROR("RtAudio: no compiled support for specified API argument!");
ATA_ERROR("API NOT Supported '" << _api << "' not in " << getListApi());
return audio::orchestra::error_fail;
}
ATA_INFO("Auto choice API :");
@ -122,26 +122,26 @@ enum audio::orchestra::error audio::orchestra::Interface::instanciate(const std:
for (size_t iii=0; iii<apis.size(); ++iii) {
ATA_INFO("try open ...");
openApi(apis[iii]);
if(m_rtapi == nullptr) {
if(m_api == nullptr) {
ATA_ERROR(" ==> can not create ...");
continue;
}
if (m_rtapi->getDeviceCount() != 0) {
if (m_api->getDeviceCount() != 0) {
ATA_INFO(" ==> api open");
break;
}
}
if (m_rtapi != nullptr) {
if (m_api != nullptr) {
return audio::orchestra::error_none;
}
ATA_ERROR("RtAudio: no compiled API support found ... critical error!!");
ATA_ERROR("API NOT Supported '" << _api << "' not in " << getListApi());
return audio::orchestra::error_fail;
}
audio::orchestra::Interface::~Interface() {
ATA_INFO("Remove interface");
delete m_rtapi;
m_rtapi = nullptr;
delete m_api;
m_api = nullptr;
}
enum audio::orchestra::error audio::orchestra::Interface::openStream(audio::orchestra::StreamParameters* _outputParameters,
@ -151,10 +151,10 @@ enum audio::orchestra::error audio::orchestra::Interface::openStream(audio::orch
uint32_t* _bufferFrames,
audio::orchestra::AirTAudioCallback _callback,
const audio::orchestra::StreamOptions& _options) {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return audio::orchestra::error_inputNull;
}
return m_rtapi->openStream(_outputParameters,
return m_api->openStream(_outputParameters,
_inputParameters,
_format,
_sampleRate,
@ -164,22 +164,22 @@ enum audio::orchestra::error audio::orchestra::Interface::openStream(audio::orch
}
bool audio::orchestra::Interface::isMasterOf(audio::orchestra::Interface& _interface) {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
ATA_ERROR("Current Master API is nullptr ...");
return false;
}
if (_interface.m_rtapi == nullptr) {
if (_interface.m_api == nullptr) {
ATA_ERROR("Current Slave API is nullptr ...");
return false;
}
if (m_rtapi->getCurrentApi() != _interface.m_rtapi->getCurrentApi()) {
if (m_api->getCurrentApi() != _interface.m_api->getCurrentApi()) {
ATA_ERROR("Can not link 2 Interface with not the same Low level type (?)");//" << _interface.m_adac->getCurrentApi() << " != " << m_adac->getCurrentApi() << ")");
return false;
}
if (m_rtapi->getCurrentApi() != audio::orchestra::type_alsa) {
ATA_ERROR("Link 2 device together work only if the interafec is ?");// << audio::orchestra::type_alsa << " not for " << m_rtapi->getCurrentApi());
if (m_api->getCurrentApi() != audio::orchestra::type_alsa) {
ATA_ERROR("Link 2 device together work only if the interafec is ?");// << audio::orchestra::type_alsa << " not for " << m_api->getCurrentApi());
return false;
}
return m_rtapi->isMasterOf(_interface.m_rtapi);
return m_api->isMasterOf(_interface.m_api);
}

View File

@ -40,14 +40,14 @@ namespace audio {
protected:
std::vector<std::pair<std::string, Api* (*)()> > m_apiAvaillable;
protected:
audio::orchestra::Api *m_rtapi;
audio::orchestra::Api *m_api;
public:
void setName(const std::string& _name) {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return;
}
m_rtapi->setName(_name);
m_api->setName(_name);
}
/**
* @brief Get the list of all availlable API in the system.
@ -84,10 +84,10 @@ namespace audio {
* @return the audio API specifier for the current instance of airtaudio.
*/
const std::string& getCurrentApi() {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return audio::orchestra::type_undefined;
}
return m_rtapi->getCurrentApi();
return m_api->getCurrentApi();
}
/**
* @brief A public function that queries for the number of audio devices available.
@ -97,10 +97,10 @@ namespace audio {
* a system error occurs during processing, a warning will be issued.
*/
uint32_t getDeviceCount() {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return 0;
}
return m_rtapi->getDeviceCount();
return m_api->getDeviceCount();
}
/**
* @brief Any device integer between 0 and getDeviceCount() - 1 is valid.
@ -114,17 +114,17 @@ namespace audio {
* @return An audio::orchestra::DeviceInfo structure for a specified device number.
*/
audio::orchestra::DeviceInfo getDeviceInfo(uint32_t _device) {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return audio::orchestra::DeviceInfo();
}
return m_rtapi->getDeviceInfo(_device);
return m_api->getDeviceInfo(_device);
}
audio::orchestra::DeviceInfo getDeviceInfo(const std::string& _deviceName) {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return audio::orchestra::DeviceInfo();
}
audio::orchestra::DeviceInfo info;
m_rtapi->getNamedDeviceInfo(_deviceName, info);
m_api->getNamedDeviceInfo(_deviceName, info);
return info;
}
/**
@ -137,10 +137,10 @@ namespace audio {
* before attempting to open a stream.
*/
uint32_t getDefaultOutputDevice() {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return 0;
}
return m_rtapi->getDefaultOutputDevice();
return m_api->getDefaultOutputDevice();
}
/**
* @brief A function that returns the index of the default input device.
@ -152,10 +152,10 @@ namespace audio {
* before attempting to open a stream.
*/
uint32_t getDefaultInputDevice() {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return 0;
}
return m_rtapi->getDefaultInputDevice();
return m_api->getDefaultInputDevice();
}
/**
* @brief A public function for opening a stream with the specified parameters.
@ -210,10 +210,10 @@ namespace audio {
* returns (no exception is thrown).
*/
enum audio::orchestra::error closeStream() {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return audio::orchestra::error_inputNull;
}
return m_rtapi->closeStream();
return m_api->closeStream();
}
/**
* @brief A function that starts a stream.
@ -224,10 +224,10 @@ namespace audio {
* running.
*/
enum audio::orchestra::error startStream() {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return audio::orchestra::error_inputNull;
}
return m_rtapi->startStream();
return m_api->startStream();
}
/**
* @brief Stop a stream, allowing any samples remaining in the output queue to be played.
@ -238,10 +238,10 @@ namespace audio {
* stopped.
*/
enum audio::orchestra::error stopStream() {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return audio::orchestra::error_inputNull;
}
return m_rtapi->stopStream();
return m_api->stopStream();
}
/**
* @brief Stop a stream, discarding any samples remaining in the input/output queue.
@ -251,38 +251,38 @@ namespace audio {
* stopped.
*/
enum audio::orchestra::error abortStream() {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return audio::orchestra::error_inputNull;
}
return m_rtapi->abortStream();
return m_api->abortStream();
}
/**
* @return true if a stream is open and false if not.
*/
bool isStreamOpen() const {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return false;
}
return m_rtapi->isStreamOpen();
return m_api->isStreamOpen();
}
/**
* @return true if the stream is running and false if it is stopped or not open.
*/
bool isStreamRunning() const {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return false;
}
return m_rtapi->isStreamRunning();
return m_api->isStreamRunning();
}
/**
* @brief If a stream is not open, an RtError (type = INVALID_USE) will be thrown.
* @return the number of elapsed seconds since the stream was started.
*/
audio::Time getStreamTime() {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return audio::Time();
}
return m_rtapi->getStreamTime();
return m_api->getStreamTime();
}
/**
* @brief The stream latency refers to delay in audio input and/or output
@ -294,10 +294,10 @@ namespace audio {
* @return The internal stream latency in sample frames.
*/
long getStreamLatency() {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return 0;
}
return m_rtapi->getStreamLatency();
return m_api->getStreamLatency();
}
/**
* @brief On some systems, the sample rate used may be slightly different
@ -306,10 +306,10 @@ namespace audio {
* @return Returns actual sample rate in use by the stream.
*/
uint32_t getStreamSampleRate() {
if (m_rtapi == nullptr) {
if (m_api == nullptr) {
return 0;
}
return m_rtapi->getStreamSampleRate();
return m_api->getStreamSampleRate();
}
bool isMasterOf(audio::orchestra::Interface& _interface);
protected:

View File

@ -104,7 +104,8 @@ uint32_t audio::orchestra::api::Alsa::getDeviceCount() {
if (subdevice < 0) {
break;
}
nDevices++;
// input/output
nDevices+=2;
}
nextcard:
snd_ctl_close(handle);
@ -113,7 +114,7 @@ nextcard:
return nDevices;
}
bool audio::orchestra::api::Alsa::getNamedDeviceInfoLocal(const std::string& _deviceName, audio::orchestra::DeviceInfo& _info, int32_t _cardId, int32_t _subdevice, int32_t _localDeviceId) {
bool audio::orchestra::api::Alsa::getNamedDeviceInfoLocal(const std::string& _deviceName, audio::orchestra::DeviceInfo& _info, int32_t _cardId, int32_t _subdevice, int32_t _localDeviceId, bool _input) {
int32_t result;
snd_ctl_t *chandle;
int32_t openMode = SND_PCM_ASYNC;
@ -124,7 +125,12 @@ bool audio::orchestra::api::Alsa::getNamedDeviceInfoLocal(const std::string& _de
snd_pcm_hw_params_t *params;
snd_pcm_hw_params_alloca(&params);
// First try for playback unless default _device (which has subdev -1)
_info.input = _input;
if (_input == true) {
stream = SND_PCM_STREAM_CAPTURE;
} else {
stream = SND_PCM_STREAM_PLAYBACK;
}
snd_pcm_info_set_stream(pcminfo, stream);
std::vector<std::string> listElement = etk::split(_deviceName, ',');
if (listElement.size() == 0) {
@ -146,39 +152,14 @@ bool audio::orchestra::api::Alsa::getNamedDeviceInfoLocal(const std::string& _de
snd_pcm_info_set_subdevice(pcminfo, 0);
result = snd_ctl_pcm_info(chandle, pcminfo);
if (result < 0) {
// Device probably doesn't support playback.
goto captureProbe;
// Device probably doesn't support IO.
return false;
}
}
result = snd_pcm_open(&phandle, _deviceName.c_str(), stream, openMode | SND_PCM_NONBLOCK);
if (result < 0) {
ATA_ERROR("snd_pcm_open error for device (" << _deviceName << "), " << snd_strerror(result) << ". (seems to be already open)");
// TODO : Return audio::orchestra::error_warning;
goto captureProbe;
// ALSA doesn't provide default devices so we'll use the first available one.
if (_localDeviceId == 0) {
_info.isDefault = true;
}
// The device is open ... fill the parameter structure.
result = snd_pcm_hw_params_any(phandle, params);
if (result < 0) {
snd_pcm_close(phandle);
ATA_ERROR("snd_pcm_hw_params error for device (" << _deviceName << "), " << snd_strerror(result) << ".");
// TODO : Return audio::orchestra::error_warning;
goto captureProbe;
}
// Get output channel information.
uint32_t value;
result = snd_pcm_hw_params_get_channels_max(params, &value);
if (result < 0) {
snd_pcm_close(phandle);
ATA_ERROR("error getting device (" << _deviceName << ") output channels, " << snd_strerror(result) << ".");
// TODO : Return audio::orchestra::error_warning;
goto captureProbe;
}
ATA_ERROR("Output channel = " << value);
_info.outputChannels = value;
snd_pcm_close(phandle);
captureProbe:
stream = SND_PCM_STREAM_CAPTURE;
snd_pcm_info_set_stream(pcminfo, stream);
// Now try for capture unless default device (with subdev = -1)
if (_subdevice != -1) {
@ -186,82 +167,32 @@ captureProbe:
snd_ctl_close(chandle);
if (result < 0) {
// Device probably doesn't support capture.
if (_info.outputChannels == 0) {
return true;
}
goto probeParameters;
return false;
}
}
// open device:
result = snd_pcm_open(&phandle, _deviceName.c_str(), stream, openMode | SND_PCM_NONBLOCK);
if (result < 0) {
ATA_ERROR("snd_pcm_open error for device (" << _deviceName << "), " << snd_strerror(result) << ". (seems to be already open)");
// TODO : Return audio::orchestra::error_warning;
if (_info.outputChannels == 0) {
return true;
}
goto probeParameters;
}
// The device is open ... fill the parameter structure.
result = snd_pcm_hw_params_any(phandle, params);
if (result < 0) {
snd_pcm_close(phandle);
ATA_ERROR("snd_pcm_hw_params error for device (" << _deviceName << "), " << snd_strerror(result) << ".");
// TODO : Return audio::orchestra::error_warning;
if (_info.outputChannels == 0) {
return true;
}
goto probeParameters;
return false;
}
unsigned int value;
result = snd_pcm_hw_params_get_channels_max(params, &value);
if (result < 0) {
snd_pcm_close(phandle);
ATA_ERROR("error getting device (" << _deviceName << ") input channels, " << snd_strerror(result) << ".");
// TODO : Return audio::orchestra::error_warning;
if (_info.outputChannels == 0) {
return true;
}
goto probeParameters;
return false;
}
ATA_ERROR("Input channel = " << value);
_info.inputChannels = value;
snd_pcm_close(phandle);
// ALSA does not support duplex, but synchronization between I/Os
_info.duplexChannels = 0;
// ALSA doesn't provide default devices so we'll use the first available one.
if ( _localDeviceId == 0
&& _info.outputChannels > 0) {
_info.isDefaultOutput = true;
}
if ( _localDeviceId == 0
&& _info.inputChannels > 0) {
_info.isDefaultInput = true;
}
probeParameters:
// At this point, we just need to figure out the supported data
// formats and sample rates. We'll proceed by opening the device in
// the direction with the maximum number of channels, or playback if
// they are equal. This might limit our sample rate options, but so
// be it.
if (_info.outputChannels >= _info.inputChannels) {
stream = SND_PCM_STREAM_PLAYBACK;
} else {
stream = SND_PCM_STREAM_CAPTURE;
}
snd_pcm_info_set_stream(pcminfo, stream);
result = snd_pcm_open(&phandle, _deviceName.c_str(), stream, openMode | SND_PCM_NONBLOCK);
if (result < 0) {
ATA_ERROR("snd_pcm_open error for device (" << _deviceName << "), " << snd_strerror(result) << ".");
// TODO : Return audio::orchestra::error_warning;
return false;
}
// The device is open ... fill the parameter structure.
result = snd_pcm_hw_params_any(phandle, params);
if (result < 0) {
snd_pcm_close(phandle);
ATA_ERROR("snd_pcm_hw_params error for device (" << _deviceName << "), " << snd_strerror(result) << ".");
// TODO : Return audio::orchestra::error_warning;
return false;
for (int32_t iii=0; iii<value; ++iii) {
_info.channels.push_back(audio::channel_unknow);
}
// Test our discrete set of sample rate values.
_info.sampleRates.clear();
@ -275,7 +206,6 @@ probeParameters:
if (_info.sampleRates.size() == 0) {
snd_pcm_close(phandle);
ATA_ERROR("no supported sample rates found for device (" << _deviceName << ").");
// TODO : Return audio::orchestra::error_warning;
return false;
}
// Probe the supported data formats ... we don't care about endian-ness just yet
@ -325,24 +255,16 @@ probeParameters:
}
// That's all ... close the device and return
snd_pcm_close(phandle);
_info.probed = true;
return true;
}
audio::orchestra::DeviceInfo audio::orchestra::api::Alsa::getDeviceInfo(uint32_t _device) {
audio::orchestra::DeviceInfo info;
/*
ATA_WARNING("plop");
getDeviceInfo("hw:0,0,0", info);
info.display();
getDeviceInfo("hw:0,0,1", info);
info.display();
*/
info.probed = false;
unsigned nDevices = 0;
int32_t result = -1;
int32_t subdevice = -1;
int32_t card = -1;
bool isInput = false;
char name[64];
snd_ctl_t *chandle;
// Count cards and devices
@ -366,10 +288,18 @@ audio::orchestra::DeviceInfo audio::orchestra::api::Alsa::getDeviceInfo(uint32_t
break;
}
if (nDevices == _device) {
// for input
sprintf(name, "hw:%d,%d", card, subdevice);
isInput = true;
goto foundDevice;
}
nDevices++;
if (nDevices+1 == _device) {
// for output
sprintf(name, "hw:%d,%d", card, subdevice);
isInput = false;
goto foundDevice;
}
nDevices+=2;
}
nextcard:
snd_ctl_close(chandle);
@ -400,7 +330,7 @@ foundDevice:
}
return m_devices[_device];
}
bool ret = audio::orchestra::api::Alsa::getNamedDeviceInfoLocal(name, info, card, subdevice, _device);
bool ret = audio::orchestra::api::Alsa::getNamedDeviceInfoLocal(name, info, card, subdevice, _device, isInput);
if (ret == false) {
// TODO : ...
return info;

View File

@ -27,7 +27,8 @@ namespace audio {
audio::orchestra::DeviceInfo& _info,
int32_t _cardId=-1, // Alsa card ID
int32_t _subdevice=-1, // alsa subdevice ID
int32_t _localDeviceId=-1); // local ID of device fined
int32_t _localDeviceId=-1,// local ID of device find
bool _input=false);
public:
bool getNamedDeviceInfo(const std::string& _deviceName, audio::orchestra::DeviceInfo& _info) {
return getNamedDeviceInfoLocal(_deviceName, _info);
@ -48,7 +49,6 @@ namespace audio {
void callbackEventOneCycleMMAPWrite();
private:
static void alsaCallbackEvent(void* _userData);
static void alsaCallbackEventMMap(void* _userData);
private:
std11::shared_ptr<AlsaPrivate> m_private;
std::vector<audio::orchestra::DeviceInfo> m_devices;

View File

@ -9,6 +9,7 @@
#if defined(ORCHESTRA_BUILD_DS)
#include <audio/orchestra/Interface.h>
#include <audio/orchestra/debug.h>
#include <etk/thread/tools.h>
#undef __class__
#define __class__ "api::Ds"
@ -58,15 +59,13 @@ static inline DWORD dsPointerBetween(DWORD _pointer, DWORD _laterPointer, DWORD
class DsDevice {
public:
LPGUID id[2];
bool validId[2];
bool found;
LPGUID id;
bool input;
std::string name;
DsDevice() :
found(false) {
validId[0] = false;
validId[1] = false;
id(0),
input(false) {
}
};
@ -105,12 +104,6 @@ namespace audio {
}
}
// Declarations for utility functions, callbacks, and structures
// specific to the DirectSound implementation.
static BOOL CALLBACK deviceQueryCallback(LPGUID _lpguid,
LPCTSTR _description,
LPCTSTR _module,
LPVOID _lpContext);
static const char* getErrorString(int32_t _code);
@ -139,90 +132,126 @@ audio::orchestra::api::Ds::~Ds() {
}
}
// The DirectSound default output is always the first device.
uint32_t audio::orchestra::api::Ds::getDefaultOutputDevice() {
return 0;
#include "tchar.h"
static std::string convertTChar(LPCTSTR _name) {
#if defined(UNICODE) || defined(_UNICODE)
int32_t length = WideCharToMultiByte(CP_UTF8, 0, _name, -1, nullptr, 0, nullptr, nullptr);
std::string s(length-1, '\0');
WideCharToMultiByte(CP_UTF8, 0, _name, -1, &s[0], length, nullptr, nullptr);
#else
std::string s(_name);
#endif
return s;
}
// The DirectSound default input is always the first input device,
// which is the first capture device enumerated.
uint32_t audio::orchestra::api::Ds::getDefaultInputDevice() {
return 0;
static BOOL CALLBACK deviceQueryCallback(LPGUID _lpguid,
LPCTSTR _description,
LPCTSTR _module,
LPVOID _lpContext) {
struct DsProbeData& probeInfo = *(struct DsProbeData*) _lpContext;
std::vector<DsDevice>& dsDevices = *probeInfo.dsDevices;
HRESULT hr;
bool validDevice = false;
if (probeInfo.isInput == true) {
DSCCAPS caps;
LPDIRECTSOUNDCAPTURE object;
hr = DirectSoundCaptureCreate(_lpguid, &object, nullptr);
if (hr != DS_OK) {
return TRUE;
}
caps.dwSize = sizeof(caps);
hr = object->GetCaps(&caps);
if (hr == DS_OK) {
if (caps.dwChannels > 0 && caps.dwFormats > 0) {
validDevice = true;
}
}
object->Release();
} else {
DSCAPS caps;
LPDIRECTSOUND object;
hr = DirectSoundCreate(_lpguid, &object, nullptr);
if (hr != DS_OK) {
return TRUE;
}
caps.dwSize = sizeof(caps);
hr = object->GetCaps(&caps);
if (hr == DS_OK) {
if ( caps.dwFlags & DSCAPS_PRIMARYMONO
|| caps.dwFlags & DSCAPS_PRIMARYSTEREO) {
validDevice = true;
}
}
object->Release();
}
if (validDevice == false) {
return TRUE;
}
// If good device, then save its name and guid.
std::string name = convertTChar(_description);
//if (name == "Primary Sound Driver" || name == "Primary Sound Capture Driver")
if (_lpguid == nullptr) {
name = "Default Device";
}
DsDevice device;
device.name = name;
device.input = probeInfo.isInput;
device.id = _lpguid;
dsDevices.push_back(device);
return TRUE;
}
uint32_t audio::orchestra::api::Ds::getDeviceCount() {
// Set query flag for previously found devices to false, so that we
// can check for any devices that have disappeared.
for (size_t iii=0; iii<m_private->dsDevices.size(); ++iii) {
m_private->dsDevices[iii].found = false;
}
// Query DirectSound devices.
struct DsProbeData probeInfo;
probeInfo.isInput = false;
probeInfo.dsDevices = &m_private->dsDevices;
HRESULT result = DirectSoundEnumerate((LPDSENUMCALLBACK) deviceQueryCallback, &probeInfo);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") enumerating output devices!");
ATA_ERROR(getErrorString(result) << ": enumerating output devices!");
return 0;
}
// Query DirectSoundCapture devices.
probeInfo.isInput = true;
result = DirectSoundCaptureEnumerate((LPDSENUMCALLBACK) deviceQueryCallback, &probeInfo);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") enumerating input devices!");
ATA_ERROR(getErrorString(result) << ": enumerating input devices!");
return 0;
}
// Clean out any devices that may have disappeared.
std::vector< int32_t > indices;
for (uint32_t i=0; i<m_private->dsDevices.size(); i++) {
if (m_private->dsDevices[i].found == false) {
indices.push_back(i);
}
}
uint32_t nErased = 0;
for (uint32_t i=0; i<indices.size(); i++) {
m_private->dsDevices.erase(m_private->dsDevices.begin()-nErased++);
}
return m_private->dsDevices.size();
}
audio::orchestra::DeviceInfo audio::orchestra::api::Ds::getDeviceInfo(uint32_t _device) {
audio::orchestra::DeviceInfo info;
info.probed = false;
if (m_private->dsDevices.size() == 0) {
// Force a query of all devices
getDeviceCount();
if (m_private->dsDevices.size() == 0) {
ATA_ERROR("no devices found!");
return info;
}
}
if (_device >= m_private->dsDevices.size()) {
ATA_ERROR("device ID is invalid!");
return info;
}
HRESULT result;
if (m_private->dsDevices[ _device ].validId[0] == false) {
goto probeInput;
}
if (m_private->dsDevices[_device].input == false) {
LPDIRECTSOUND output;
DSCAPS outCaps;
result = DirectSoundCreate(m_private->dsDevices[ _device ].id[0], &output, nullptr);
result = DirectSoundCreate(m_private->dsDevices[_device].id, &output, nullptr);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") opening output device (" << m_private->dsDevices[ _device ].name << ")!");
goto probeInput;
ATA_ERROR(getErrorString(result) << ": opening output device (" << m_private->dsDevices[_device].name << ")!");
return info;
}
outCaps.dwSize = sizeof(outCaps);
result = output->GetCaps(&outCaps);
if (FAILED(result)) {
output->Release();
ATA_ERROR("error (" << getErrorString(result) << ") getting capabilities!");
goto probeInput;
ATA_ERROR(getErrorString(result) << ": getting capabilities!");
return info;
}
// Get output channel information.
info.outputChannels = (outCaps.dwFlags & DSCAPS_PRIMARYSTEREO) ? 2 : 1;
if (outCaps.dwFlags & DSCAPS_PRIMARYSTEREO) {
info.channels.push_back(audio::channel_unknow);
info.channels.push_back(audio::channel_unknow);
} else {
info.channels.push_back(audio::channel_unknow);
}
// Get sample rate information.
info.sampleRates.clear();
for (auto &it : audio::orchestra::genericSampleRate()) {
if ( it >= outCaps.dwMinSecondarySampleRate
&& it <= outCaps.dwMaxSecondarySampleRate) {
@ -237,19 +266,13 @@ audio::orchestra::DeviceInfo audio::orchestra::api::Ds::getDeviceInfo(uint32_t _
info.nativeFormats.push_back(audio::format_int8);
}
output->Release();
if (getDefaultOutputDevice() == _device) {
info.isDefaultOutput = true;
}
if (m_private->dsDevices[ _device ].validId[1] == false) {
info.name = m_private->dsDevices[_device].name;
info.probed = true;
return info;
}
probeInput:
} else {
LPDIRECTSOUNDCAPTURE input;
result = DirectSoundCaptureCreate(m_private->dsDevices[ _device ].id[1], &input, nullptr);
result = DirectSoundCaptureCreate(m_private->dsDevices[_device].id, &input, nullptr);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") opening input device (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": opening input device (" << m_private->dsDevices[_device].name << ")!");
return info;
}
DSCCAPS inCaps;
@ -257,11 +280,13 @@ probeInput:
result = input->GetCaps(&inCaps);
if (FAILED(result)) {
input->Release();
ATA_ERROR("error (" << getErrorString(result) << ") getting object capabilities (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": getting object capabilities (" << m_private->dsDevices[_device].name << ")!");
return info;
}
// Get input channel information.
info.inputChannels = inCaps.dwChannels;
for (int32_t iii=0; iii<inCaps.dwChannels; ++iii) {
info.channels.push_back(audio::channel_unknow);
}
// Get sample rate and format information.
std::vector<uint32_t> rates;
if (inCaps.dwChannels >= 2) {
@ -324,10 +349,10 @@ probeInput:
}
} else {
// technically, this would be an error
info.inputChannels = 0;
info.channels.clear();
}
input->Release();
if (info.inputChannels == 0) {
if (info.channels.size() == 0) {
return info;
}
// Copy the supported rates to the info structure but avoid duplication.
@ -340,19 +365,15 @@ probeInput:
break;
}
}
if (found == false) info.sampleRates.push_back(rates[i]);
if (found == false) {
info.sampleRates.push_back(rates[i]);
}
}
std::sort(info.sampleRates.begin(), info.sampleRates.end());
// If device opens for both playback and capture, we determine the channels.
if (info.outputChannels > 0 && info.inputChannels > 0) {
info.duplexChannels = (info.outputChannels > info.inputChannels) ? info.inputChannels : info.outputChannels;
}
if (_device == 0) {
info.isDefaultInput = true;
}
// Copy name and return.
info.name = m_private->dsDevices[_device].name;
info.probed = true;
return info;
}
return info;
}
@ -379,17 +400,6 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
ATA_ERROR("device ID is invalid!");
return false;
}
if (_mode == audio::orchestra::mode_output) {
if (m_private->dsDevices[ _device ].validId[0] == false) {
ATA_ERROR("device (" << _device << ") does not support output!");
return false;
}
} else { // _mode == audio::orchestra::mode_input
if (m_private->dsDevices[ _device ].validId[1] == false) {
ATA_ERROR("device (" << _device << ") does not support input!");
return false;
}
}
// According to a note in PortAudio, using GetDesktopWindow()
// instead of GetForegroundWindow() is supposed to avoid problems
// that occur when the application's window is not the foreground
@ -433,9 +443,9 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
HRESULT result;
if (_mode == audio::orchestra::mode_output) {
LPDIRECTSOUND output;
result = DirectSoundCreate(m_private->dsDevices[ _device ].id[0], &output, nullptr);
result = DirectSoundCreate(m_private->dsDevices[_device].id, &output, nullptr);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") opening output device (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": opening output device (" << m_private->dsDevices[_device].name << ")!");
return false;
}
DSCAPS outCaps;
@ -443,7 +453,7 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
result = output->GetCaps(&outCaps);
if (FAILED(result)) {
output->Release();
ATA_ERROR("error (" << getErrorString(result) << ") getting capabilities (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": getting capabilities (" << m_private->dsDevices[_device].name << ")!");
return false;
}
// Check channel information.
@ -477,7 +487,7 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
result = output->SetCooperativeLevel(hWnd, DSSCL_PRIORITY);
if (FAILED(result)) {
output->Release();
ATA_ERROR("error (" << getErrorString(result) << ") setting cooperative level (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": setting cooperative level (" << m_private->dsDevices[_device].name << ")!");
return false;
}
// Even though we will write to the secondary buffer, we need to
@ -493,14 +503,14 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
result = output->CreateSoundBuffer(&bufferDescription, &buffer, nullptr);
if (FAILED(result)) {
output->Release();
ATA_ERROR("error (" << getErrorString(result) << ") accessing primary buffer (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": accessing primary buffer (" << m_private->dsDevices[_device].name << ")!");
return false;
}
// Set the primary DS buffer sound format.
result = buffer->SetFormat(&waveFormat);
if (FAILED(result)) {
output->Release();
ATA_ERROR("error (" << getErrorString(result) << ") setting primary buffer format (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": setting primary buffer format (" << m_private->dsDevices[_device].name << ")!");
return false;
}
// Setup the secondary DS buffer description.
@ -523,7 +533,7 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
result = output->CreateSoundBuffer(&bufferDescription, &buffer, nullptr);
if (FAILED(result)) {
output->Release();
ATA_ERROR("error (" << getErrorString(result) << ") creating secondary buffer (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": creating secondary buffer (" << m_private->dsDevices[_device].name << ")!");
return false;
}
}
@ -534,7 +544,7 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
if (FAILED(result)) {
output->Release();
buffer->Release();
ATA_ERROR("error (" << getErrorString(result) << ") getting buffer settings (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": getting buffer settings (" << m_private->dsDevices[_device].name << ")!");
return false;
}
dsBufferSize = dsbcaps.dwBufferBytes;
@ -545,7 +555,7 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
if (FAILED(result)) {
output->Release();
buffer->Release();
ATA_ERROR("error (" << getErrorString(result) << ") locking buffer (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": locking buffer (" << m_private->dsDevices[_device].name << ")!");
return false;
}
// Zero the DS buffer
@ -555,7 +565,7 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
if (FAILED(result)) {
output->Release();
buffer->Release();
ATA_ERROR("error (" << getErrorString(result) << ") unlocking buffer (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": unlocking buffer (" << m_private->dsDevices[_device].name << ")!");
return false;
}
ohandle = (void *) output;
@ -563,9 +573,9 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
}
if (_mode == audio::orchestra::mode_input) {
LPDIRECTSOUNDCAPTURE input;
result = DirectSoundCaptureCreate(m_private->dsDevices[ _device ].id[1], &input, nullptr);
result = DirectSoundCaptureCreate(m_private->dsDevices[_device].id, &input, nullptr);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") opening input device (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": opening input device (" << m_private->dsDevices[_device].name << ")!");
return false;
}
DSCCAPS inCaps;
@ -573,7 +583,7 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
result = input->GetCaps(&inCaps);
if (FAILED(result)) {
input->Release();
ATA_ERROR("error (" << getErrorString(result) << ") getting input capabilities (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": getting input capabilities (" << m_private->dsDevices[_device].name << ")!");
return false;
}
// Check channel information.
@ -626,7 +636,7 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
result = input->CreateCaptureBuffer(&bufferDescription, &buffer, nullptr);
if (FAILED(result)) {
input->Release();
ATA_ERROR("error (" << getErrorString(result) << ") creating input buffer (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": creating input buffer (" << m_private->dsDevices[_device].name << ")!");
return false;
}
// Get the buffer size ... might be different from what we specified.
@ -636,7 +646,7 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
if (FAILED(result)) {
input->Release();
buffer->Release();
ATA_ERROR("error (" << getErrorString(result) << ") getting buffer settings (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": getting buffer settings (" << m_private->dsDevices[_device].name << ")!");
return false;
}
dsBufferSize = dscbcaps.dwBufferBytes;
@ -651,7 +661,7 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
if (FAILED(result)) {
input->Release();
buffer->Release();
ATA_ERROR("error (" << getErrorString(result) << ") locking input buffer (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": locking input buffer (" << m_private->dsDevices[_device].name << ")!");
return false;
}
// Zero the buffer
@ -661,7 +671,7 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
if (FAILED(result)) {
input->Release();
buffer->Release();
ATA_ERROR("error (" << getErrorString(result) << ") unlocking input buffer (" << m_private->dsDevices[ _device ].name << ")!");
ATA_ERROR(getErrorString(result) << ": unlocking input buffer (" << m_private->dsDevices[_device].name << ")!");
return false;
}
ohandle = (void *) input;
@ -749,7 +759,7 @@ bool audio::orchestra::api::Ds::probeDeviceOpen(uint32_t _device,
goto error;
}
// Boost DS thread priority
SetThreadPriority((HANDLE)m_private->thread, THREAD_PRIORITY_HIGHEST);
etk::thread::setPriority(*m_private->thread, -6);
}
return true;
error:
@ -788,8 +798,10 @@ enum audio::orchestra::error audio::orchestra::api::Ds::closeStream() {
}
// Stop the callback thread.
m_private->threadRunning = false;
WaitForSingleObject((HANDLE) m_private->thread, INFINITE);
CloseHandle((HANDLE) m_private->thread);
if (m_private->thread != nullptr) {
m_private->thread->join();
m_private->thread = nullptr;
}
if (m_private->buffer[0]) { // the object pointer can be nullptr and valid
LPDIRECTSOUND object = (LPDIRECTSOUND) m_private->id[0];
LPDIRECTSOUNDBUFFER buffer = (LPDIRECTSOUNDBUFFER) m_private->buffer[0];
@ -846,7 +858,7 @@ enum audio::orchestra::error audio::orchestra::api::Ds::startStream() {
LPDIRECTSOUNDBUFFER buffer = (LPDIRECTSOUNDBUFFER) m_private->buffer[0];
result = buffer->Play(0, 0, DSBPLAY_LOOPING);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") starting output buffer!");
ATA_ERROR(getErrorString(result) << ": starting output buffer!");
goto unlock;
}
}
@ -855,7 +867,7 @@ enum audio::orchestra::error audio::orchestra::api::Ds::startStream() {
LPDIRECTSOUNDCAPTUREBUFFER buffer = (LPDIRECTSOUNDCAPTUREBUFFER) m_private->buffer[1];
result = buffer->Start(DSCBSTART_LOOPING);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") starting input buffer!");
ATA_ERROR(getErrorString(result) << ": starting input buffer!");
goto unlock;
}
}
@ -892,14 +904,14 @@ enum audio::orchestra::error audio::orchestra::api::Ds::stopStream() {
LPDIRECTSOUNDBUFFER buffer = (LPDIRECTSOUNDBUFFER) m_private->buffer[0];
result = buffer->Stop();
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") stopping output buffer!");
ATA_ERROR(getErrorString(result) << ": stopping output buffer!");
goto unlock;
}
// Lock the buffer and clear it so that if we start to play again,
// we won't have old data playing.
result = buffer->Lock(0, m_private->dsBufferSize[0], &audioPtr, &dataLen, nullptr, nullptr, 0);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") locking output buffer!");
ATA_ERROR(getErrorString(result) << ": locking output buffer!");
goto unlock;
}
// Zero the DS buffer
@ -907,7 +919,7 @@ enum audio::orchestra::error audio::orchestra::api::Ds::stopStream() {
// Unlock the DS buffer
result = buffer->Unlock(audioPtr, dataLen, nullptr, 0);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") unlocking output buffer!");
ATA_ERROR(getErrorString(result) << ": unlocking output buffer!");
goto unlock;
}
// If we start playing again, we must begin at beginning of buffer.
@ -921,14 +933,14 @@ enum audio::orchestra::error audio::orchestra::api::Ds::stopStream() {
m_state = audio::orchestra::state_stopped;
result = buffer->Stop();
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") stopping input buffer!");
ATA_ERROR(getErrorString(result) << ": stopping input buffer!");
goto unlock;
}
// Lock the buffer and clear it so that if we start to play again,
// we won't have old data playing.
result = buffer->Lock(0, m_private->dsBufferSize[1], &audioPtr, &dataLen, nullptr, nullptr, 0);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") locking input buffer!");
ATA_ERROR(getErrorString(result) << ": locking input buffer!");
goto unlock;
}
// Zero the DS buffer
@ -936,7 +948,7 @@ enum audio::orchestra::error audio::orchestra::api::Ds::stopStream() {
// Unlock the DS buffer
result = buffer->Unlock(audioPtr, dataLen, nullptr, 0);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") unlocking input buffer!");
ATA_ERROR(getErrorString(result) << ": unlocking input buffer!");
goto unlock;
}
// If we start recording again, we must begin at beginning of buffer.
@ -963,6 +975,7 @@ enum audio::orchestra::error audio::orchestra::api::Ds::abortStream() {
}
void audio::orchestra::api::Ds::callbackEvent() {
etk::thread::setName("DS IO-" + m_name);
if (m_state == audio::orchestra::state_stopped || m_state == audio::orchestra::state_stopping) {
Sleep(50); // sleep 50 milliseconds
return;
@ -985,18 +998,18 @@ void audio::orchestra::api::Ds::callbackEvent() {
// draining stream.
if (m_private->drainCounter == 0) {
audio::Time streamTime = getStreamTime();
audio::orchestra::status status = audio::orchestra::status_ok;
std::vector<audio::orchestra::status> status;
if ( m_mode != audio::orchestra::mode_input
&& m_private->xrun[0] == true) {
status = audio::orchestra::status_underflow;
status.push_back(audio::orchestra::status_underflow);
m_private->xrun[0] = false;
}
if ( m_mode != audio::orchestra::mode_output
&& m_private->xrun[1] == true) {
status = audio::orchestra::status_overflow;
status.push_back(audio::orchestra::status_overflow);
m_private->xrun[1] = false;
}
int32_t cbReturnValue = info->callback(&m_userBuffer[1][0],
int32_t cbReturnValue = m_callback(&m_userBuffer[1][0],
streamTime,
&m_userBuffer[0][0],
streamTime,
@ -1042,23 +1055,23 @@ void audio::orchestra::api::Ds::callbackEvent() {
DWORD startSafeWritePointer, startSafeReadPointer;
result = dsWriteBuffer->GetCurrentPosition(nullptr, &startSafeWritePointer);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") getting current write position!");
ATA_ERROR(getErrorString(result) << ": getting current write position!");
return;
}
result = dsCaptureBuffer->GetCurrentPosition(nullptr, &startSafeReadPointer);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") getting current read position!");
ATA_ERROR(getErrorString(result) << ": getting current read position!");
return;
}
while (true) {
result = dsWriteBuffer->GetCurrentPosition(nullptr, &safeWritePointer);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") getting current write position!");
ATA_ERROR(getErrorString(result) << ": getting current write position!");
return;
}
result = dsCaptureBuffer->GetCurrentPosition(nullptr, &safeReadPointer);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") getting current read position!");
ATA_ERROR(getErrorString(result) << ": getting current read position!");
return;
}
if ( safeWritePointer != startSafeWritePointer
@ -1078,7 +1091,7 @@ void audio::orchestra::api::Ds::callbackEvent() {
LPDIRECTSOUNDBUFFER dsWriteBuffer = (LPDIRECTSOUNDBUFFER) m_private->buffer[0];
result = dsWriteBuffer->GetCurrentPosition(&currentWritePointer, &safeWritePointer);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") getting current write position!");
ATA_ERROR(getErrorString(result) << ": getting current write position!");
return;
}
m_private->bufferPointer[0] = safeWritePointer + m_private->dsPointerLeadTime[0];
@ -1123,7 +1136,7 @@ void audio::orchestra::api::Ds::callbackEvent() {
// Find out where the read and "safe write" pointers are.
result = dsBuffer->GetCurrentPosition(&currentWritePointer, &safeWritePointer);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") getting current write position!");
ATA_ERROR(getErrorString(result) << ": getting current write position!");
return;
}
// We will copy our output buffer into the region between
@ -1172,7 +1185,7 @@ void audio::orchestra::api::Ds::callbackEvent() {
&bufferSize2,
0);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") locking buffer during playback!");
ATA_ERROR(getErrorString(result) << ": locking buffer during playback!");
return;
}
// Copy our buffer into the DS buffer
@ -1183,7 +1196,7 @@ void audio::orchestra::api::Ds::callbackEvent() {
// Update our buffer offset and unlock sound buffer
dsBuffer->Unlock(buffer1, bufferSize1, buffer2, bufferSize2);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") unlocking buffer during playback!");
ATA_ERROR(getErrorString(result) << ": unlocking buffer during playback!");
return;
}
nextWritePointer = (nextWritePointer + bufferSize1 + bufferSize2) % dsBufferSize;
@ -1211,7 +1224,7 @@ void audio::orchestra::api::Ds::callbackEvent() {
// Find out where the write and "safe read" pointers are.
result = dsBuffer->GetCurrentPosition(&currentReadPointer, &safeReadPointer);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") getting current read position!");
ATA_ERROR(getErrorString(result) << ": getting current read position!");
return;
}
if (safeReadPointer < (DWORD)nextReadPointer) {
@ -1271,7 +1284,7 @@ void audio::orchestra::api::Ds::callbackEvent() {
// Wake up and find out where we are now.
result = dsBuffer->GetCurrentPosition(&currentReadPointer, &safeReadPointer);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") getting current read position!");
ATA_ERROR(getErrorString(result) << ": getting current read position!");
return;
}
if (safeReadPointer < (DWORD)nextReadPointer) {
@ -1289,7 +1302,7 @@ void audio::orchestra::api::Ds::callbackEvent() {
&bufferSize2,
0);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") locking capture buffer!");
ATA_ERROR(getErrorString(result) << ": locking capture buffer!");
return;
}
if (m_duplexPrerollBytes <= 0) {
@ -1309,7 +1322,7 @@ void audio::orchestra::api::Ds::callbackEvent() {
nextReadPointer = (nextReadPointer + bufferSize1 + bufferSize2) % dsBufferSize;
dsBuffer->Unlock(buffer1, bufferSize1, buffer2, bufferSize2);
if (FAILED(result)) {
ATA_ERROR("error (" << getErrorString(result) << ") unlocking capture buffer!");
ATA_ERROR(getErrorString(result) << ": unlocking capture buffer!");
return;
}
m_private->bufferPointer[1] = nextReadPointer;
@ -1330,100 +1343,12 @@ unlock:
}
void audio::orchestra::api::Ds::dsCallbackEvent(void *_userData) {
etk::thread::setName("DS IO-" + m_name);
audio::orchestra::api::Ds* myClass = reinterpret_cast<audio::orchestra::api::Ds*>(_userData);
while (myClass->m_private->threadRunning == true) {
myClass->callbackEvent();
}
}
#include "tchar.h"
static std::string convertTChar(LPCTSTR _name) {
#if defined(UNICODE) || defined(_UNICODE)
int32_t length = WideCharToMultiByte(CP_UTF8, 0, _name, -1, nullptr, 0, nullptr, nullptr);
std::string s(length-1, '\0');
WideCharToMultiByte(CP_UTF8, 0, _name, -1, &s[0], length, nullptr, nullptr);
#else
std::string s(_name);
#endif
return s;
}
static BOOL CALLBACK deviceQueryCallback(LPGUID _lpguid,
LPCTSTR _description,
LPCTSTR _module,
LPVOID _lpContext) {
struct DsProbeData& probeInfo = *(struct DsProbeData*) _lpContext;
std::vector<DsDevice>& dsDevices = *probeInfo.dsDevices;
HRESULT hr;
bool validDevice = false;
if (probeInfo.isInput == true) {
DSCCAPS caps;
LPDIRECTSOUNDCAPTURE object;
hr = DirectSoundCaptureCreate(_lpguid, &object, nullptr);
if (hr != DS_OK) {
return TRUE;
}
caps.dwSize = sizeof(caps);
hr = object->GetCaps(&caps);
if (hr == DS_OK) {
if (caps.dwChannels > 0 && caps.dwFormats > 0) {
validDevice = true;
}
}
object->Release();
} else {
DSCAPS caps;
LPDIRECTSOUND object;
hr = DirectSoundCreate(_lpguid, &object, nullptr);
if (hr != DS_OK) {
return TRUE;
}
caps.dwSize = sizeof(caps);
hr = object->GetCaps(&caps);
if (hr == DS_OK) {
if ( caps.dwFlags & DSCAPS_PRIMARYMONO
|| caps.dwFlags & DSCAPS_PRIMARYSTEREO) {
validDevice = true;
}
}
object->Release();
}
// If good device, then save its name and guid.
std::string name = convertTChar(_description);
//if (name == "Primary Sound Driver" || name == "Primary Sound Capture Driver")
if (_lpguid == nullptr) {
name = "Default Device";
}
if (validDevice) {
for (size_t i=0; i<dsDevices.size(); i++) {
if (dsDevices[i].name == name) {
dsDevices[i].found = true;
if (probeInfo.isInput) {
dsDevices[i].id[1] = _lpguid;
dsDevices[i].validId[1] = true;
} else {
dsDevices[i].id[0] = _lpguid;
dsDevices[i].validId[0] = true;
}
return TRUE;
}
}
DsDevice device;
device.name = name;
device.found = true;
if (probeInfo.isInput) {
device.id[1] = _lpguid;
device.validId[1] = true;
} else {
device.id[0] = _lpguid;
device.validId[0] = true;
}
dsDevices.push_back(device);
}
return TRUE;
}
static const char* getErrorString(int32_t code) {
switch (code) {
case DSERR_ALLOCATED:

View File

@ -23,8 +23,6 @@ namespace audio {
return audio::orchestra::type_ds;
}
uint32_t getDeviceCount();
uint32_t getDefaultOutputDevice();
uint32_t getDefaultInputDevice();
audio::orchestra::DeviceInfo getDeviceInfo(uint32_t _device);
enum audio::orchestra::error closeStream();
enum audio::orchestra::error startStream();

View File

@ -125,12 +125,11 @@ uint32_t audio::orchestra::api::Jack::getDeviceCount() {
free(ports);
}
jack_client_close(client);
return nDevices;
return nDevices*2;
}
audio::orchestra::DeviceInfo audio::orchestra::api::Jack::getDeviceInfo(uint32_t _device) {
audio::orchestra::DeviceInfo info;
info.probed = false;
jack_options_t options = (jack_options_t) (JackNoStartServer); //JackNullOption
jack_status_t *status = nullptr;
jack_client_t *client = jack_client_open("orchestraJackInfo", options, status);
@ -143,6 +142,8 @@ audio::orchestra::DeviceInfo audio::orchestra::api::Jack::getDeviceInfo(uint32_t
std::string port, previousPort;
uint32_t nPorts = 0, nDevices = 0;
ports = jack_get_ports(client, nullptr, nullptr, 0);
int32_t deviceID = _device/2;
info.input = _device%2==0?true:false; // note that jack sens are inverted
if (ports) {
// Parse the port names up to the first colon (:).
size_t iColon = 0;
@ -152,7 +153,7 @@ audio::orchestra::DeviceInfo audio::orchestra::api::Jack::getDeviceInfo(uint32_t
if (iColon != std::string::npos) {
port = port.substr(0, iColon);
if (port != previousPort) {
if (nDevices == _device) {
if (nDevices == deviceID) {
info.name = port;
}
nDevices++;
@ -162,7 +163,7 @@ audio::orchestra::DeviceInfo audio::orchestra::api::Jack::getDeviceInfo(uint32_t
} while (ports[++nPorts]);
free(ports);
}
if (_device >= nDevices) {
if (deviceID >= nDevices) {
jack_client_close(client);
ATA_ERROR("device ID is invalid!");
// TODO : audio::orchestra::error_invalidUse;
@ -171,50 +172,42 @@ audio::orchestra::DeviceInfo audio::orchestra::api::Jack::getDeviceInfo(uint32_t
// Get the current jack server sample rate.
info.sampleRates.clear();
info.sampleRates.push_back(jack_get_sample_rate(client));
// Count the available ports containing the client name as device
// channels. Jack "input ports" equal RtAudio output channels.
uint32_t nChannels = 0;
ports = jack_get_ports(client, info.name.c_str(), nullptr, JackPortIsInput);
if (ports) {
while (ports[ nChannels ]) {
nChannels++;
}
free(ports);
info.outputChannels = nChannels;
}
// Jack "output ports" equal RtAudio input channels.
nChannels = 0;
if (info.input == true) {
ports = jack_get_ports(client, info.name.c_str(), nullptr, JackPortIsOutput);
if (ports) {
while (ports[ nChannels ]) {
nChannels++;
int32_t iii=0;
while (ports[iii]) {
ATA_ERROR(" ploppp='" << ports[iii] << "'");
info.channels.push_back(audio::channel_unknow);
iii++;
}
free(ports);
info.inputChannels = nChannels;
}
if (info.outputChannels == 0 && info.inputChannels == 0) {
} else {
ports = jack_get_ports(client, info.name.c_str(), nullptr, JackPortIsInput);
if (ports) {
int32_t iii=0;
while (ports[iii]) {
ATA_ERROR(" ploppp='" << ports[iii] << "'");
info.channels.push_back(audio::channel_unknow);
iii++;
}
free(ports);
}
}
if (info.channels.size() == 0) {
jack_client_close(client);
ATA_ERROR("error determining Jack input/output channels!");
// TODO : audio::orchestra::error_warning;
return info;
}
// If device opens for both playback and capture, we determine the channels.
if (info.outputChannels > 0 && info.inputChannels > 0) {
info.duplexChannels = (info.outputChannels > info.inputChannels) ? info.inputChannels : info.outputChannels;
}
// Jack always uses 32-bit floats.
info.nativeFormats.push_back(audio::format_float);
// Jack doesn't provide default devices so we'll use the first available one.
if ( _device == 0
&& info.outputChannels > 0) {
info.isDefaultOutput = true;
}
if ( _device == 0
&& info.inputChannels > 0) {
info.isDefaultInput = true;
if (deviceID == 0) {
info.isDefault = true;
}
jack_client_close(client);
info.probed = true;
return info;
}
@ -295,6 +288,8 @@ bool audio::orchestra::api::Jack::probeDeviceOpen(uint32_t _device,
const char **ports;
std::string port, previousPort, deviceName;
uint32_t nPorts = 0, nDevices = 0;
int32_t deviceID = _device/2;
bool isInput = _device%2==0?true:false;
ports = jack_get_ports(client, nullptr, nullptr, 0);
if (ports) {
// Parse the port names up to the first colon (:).
@ -305,7 +300,7 @@ bool audio::orchestra::api::Jack::probeDeviceOpen(uint32_t _device,
if (iColon != std::string::npos) {
port = port.substr(0, iColon);
if (port != previousPort) {
if (nDevices == _device) {
if (nDevices == deviceID) {
deviceName = port;
}
nDevices++;
@ -323,7 +318,9 @@ bool audio::orchestra::api::Jack::probeDeviceOpen(uint32_t _device,
// channels. Jack "input ports" equal RtAudio output channels.
uint32_t nChannels = 0;
uint64_t flag = JackPortIsInput;
if (_mode == audio::orchestra::mode_input) flag = JackPortIsOutput;
if (_mode == audio::orchestra::mode_input) {
flag = JackPortIsOutput;
}
ports = jack_get_ports(client, deviceName.c_str(), nullptr, flag);
if (ports) {
while (ports[ nChannels ]) {

View File

@ -81,8 +81,8 @@ audio::orchestra::api::Pulse::~Pulse() {
}
uint32_t audio::orchestra::api::Pulse::getDeviceCount() {
#if 0
std::vector<audio::orchestra::api::pulse::Element> list = audio::orchestra::api::pulse::getDeviceList();
#if 1
std::vector<audio::orchestra::DeviceInfo> list = audio::orchestra::api::pulse::getDeviceList();
return list.size();
#else
return 1;
@ -90,23 +90,12 @@ uint32_t audio::orchestra::api::Pulse::getDeviceCount() {
}
audio::orchestra::DeviceInfo audio::orchestra::api::Pulse::getDeviceInfo(uint32_t _device) {
audio::orchestra::DeviceInfo info;
//std::vector<audio::orchestra::api::pulse::Element> list = audio::orchestra::api::pulse::getDeviceList();
// TODO : Do it better... it is a little poor ...
info.probed = true;
info.name = "PulseAudio";
info.outputChannels = 2;
info.inputChannels = 2;
info.duplexChannels = 0;
info.isDefaultOutput = true;
info.isDefaultInput = true;
for (const uint32_t *sr = SUPPORTED_SAMPLERATES; *sr; ++sr) {
info.sampleRates.push_back(*sr);
std::vector<audio::orchestra::DeviceInfo> list = audio::orchestra::api::pulse::getDeviceList();
if (_device >= list.size()) {
ATA_ERROR("Request device out of IDs:" << _device << " >= " << list.size());
return audio::orchestra::DeviceInfo();
}
info.nativeFormats.push_back(audio::format_int16);
info.nativeFormats.push_back(audio::format_int32);
info.nativeFormats.push_back(audio::format_float);
return info;
return list[_device];
}
static void pulseaudio_callback(void* _userData) {

View File

@ -9,6 +9,10 @@
#include <pulse/pulseaudio.h>
#include <audio/orchestra/api/PulseDeviceList.h>
#include <audio/orchestra/debug.h>
#include <audio/Time.h>
#include <audio/Duration.h>
#include <audio/format.h>
#include <etk/stdTools.h>
// This callback gets called when our context changes state. We really only
// care about when it's ready or if it has failed
@ -19,87 +23,259 @@ static void callbackStateMachine(pa_context* _contex, void *_userdata) {
switch (state) {
// There are just here for reference
case PA_CONTEXT_UNCONNECTED:
ATA_INFO("pulse state: PA_CONTEXT_UNCONNECTED");
ATA_VERBOSE("pulse state: PA_CONTEXT_UNCONNECTED");
break;
case PA_CONTEXT_CONNECTING:
ATA_INFO("pulse state: PA_CONTEXT_CONNECTING");
ATA_VERBOSE("pulse state: PA_CONTEXT_CONNECTING");
break;
case PA_CONTEXT_AUTHORIZING:
ATA_INFO("pulse state: PA_CONTEXT_AUTHORIZING");
ATA_VERBOSE("pulse state: PA_CONTEXT_AUTHORIZING");
break;
case PA_CONTEXT_SETTING_NAME:
ATA_INFO("pulse state: PA_CONTEXT_SETTING_NAME");
ATA_VERBOSE("pulse state: PA_CONTEXT_SETTING_NAME");
break;
default:
ATA_INFO("pulse state: default");
ATA_VERBOSE("pulse state: default");
break;
case PA_CONTEXT_FAILED:
*pulseAudioReady = 2;
ATA_INFO("pulse state: PA_CONTEXT_FAILED");
ATA_VERBOSE("pulse state: PA_CONTEXT_FAILED");
break;
case PA_CONTEXT_TERMINATED:
*pulseAudioReady = 2;
ATA_INFO("pulse state: PA_CONTEXT_TERMINATED");
ATA_VERBOSE("pulse state: PA_CONTEXT_TERMINATED");
break;
case PA_CONTEXT_READY:
*pulseAudioReady = 1;
ATA_INFO("pulse state: PA_CONTEXT_READY");
ATA_VERBOSE("pulse state: PA_CONTEXT_READY");
break;
}
}
static audio::format getFormatFromPulseFormat(enum pa_sample_format _format) {
switch (_format) {
case PA_SAMPLE_U8:
return audio::format_int8;
break;
case PA_SAMPLE_ALAW:
ATA_ERROR("Not supported: uint8_t a-law");
return audio::format_unknow;
case PA_SAMPLE_ULAW:
ATA_ERROR("Not supported: uint8_t mu-law");
return audio::format_unknow;
case PA_SAMPLE_S16LE:
return audio::format_int16;
break;
case PA_SAMPLE_S16BE:
return audio::format_int16;
break;
case PA_SAMPLE_FLOAT32LE:
return audio::format_float;
break;
case PA_SAMPLE_FLOAT32BE:
return audio::format_float;
break;
case PA_SAMPLE_S32LE:
return audio::format_int32;
break;
case PA_SAMPLE_S32BE:
return audio::format_int32;
break;
case PA_SAMPLE_S24LE:
return audio::format_int24;
break;
case PA_SAMPLE_S24BE:
return audio::format_int24;
break;
case PA_SAMPLE_S24_32LE:
return audio::format_int24_on_int32;
break;
case PA_SAMPLE_S24_32BE:
return audio::format_int24_on_int32;
break;
case PA_SAMPLE_INVALID:
case PA_SAMPLE_MAX:
ATA_ERROR("Not supported: invalid");
return audio::format_unknow;
}
ATA_ERROR("Not supported: UNKNOW flag...");
return audio::format_unknow;
}
static std::vector<audio::channel> getChannelOrderFromPulseChannel(const struct pa_channel_map& _map) {
std::vector<audio::channel> out;
for (int32_t iii=0; iii<_map.channels; ++iii) {
switch(_map.map[iii]) {
default:
case PA_CHANNEL_POSITION_MAX:
case PA_CHANNEL_POSITION_INVALID:
out.push_back(audio::channel_unknow);
break;
case PA_CHANNEL_POSITION_MONO:
case PA_CHANNEL_POSITION_FRONT_CENTER:
out.push_back(audio::channel_frontCenter);
break;
case PA_CHANNEL_POSITION_FRONT_LEFT:
out.push_back(audio::channel_frontLeft);
break;
case PA_CHANNEL_POSITION_FRONT_RIGHT:
out.push_back(audio::channel_frontRight);
break;
case PA_CHANNEL_POSITION_REAR_CENTER:
out.push_back(audio::channel_rearCenter);
break;
case PA_CHANNEL_POSITION_REAR_LEFT:
out.push_back(audio::channel_rearLeft);
break;
case PA_CHANNEL_POSITION_REAR_RIGHT:
out.push_back(audio::channel_rearRight);
break;
case PA_CHANNEL_POSITION_LFE:
out.push_back(audio::channel_lfe);
break;
case PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER:
out.push_back(audio::channel_centerLeft);
break;
case PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER:
out.push_back(audio::channel_centerRight);
break;
case PA_CHANNEL_POSITION_SIDE_LEFT:
out.push_back(audio::channel_topCenterLeft);
break;
case PA_CHANNEL_POSITION_SIDE_RIGHT:
out.push_back(audio::channel_topCenterRight);
break;
case PA_CHANNEL_POSITION_TOP_CENTER:
case PA_CHANNEL_POSITION_TOP_FRONT_CENTER:
out.push_back(audio::channel_topFrontCenter);
break;
case PA_CHANNEL_POSITION_TOP_FRONT_LEFT:
out.push_back(audio::channel_topFrontLeft);
break;
case PA_CHANNEL_POSITION_TOP_FRONT_RIGHT:
out.push_back(audio::channel_topFrontRight);
break;
case PA_CHANNEL_POSITION_TOP_REAR_LEFT:
out.push_back(audio::channel_topRearLeft);
break;
case PA_CHANNEL_POSITION_TOP_REAR_RIGHT:
out.push_back(audio::channel_topRearRight);
break;
case PA_CHANNEL_POSITION_TOP_REAR_CENTER:
out.push_back(audio::channel_topRearCenter);
break;
case PA_CHANNEL_POSITION_AUX0: out.push_back(audio::channel_aux0); break;
case PA_CHANNEL_POSITION_AUX1: out.push_back(audio::channel_aux1); break;
case PA_CHANNEL_POSITION_AUX2: out.push_back(audio::channel_aux2); break;
case PA_CHANNEL_POSITION_AUX3: out.push_back(audio::channel_aux3); break;
case PA_CHANNEL_POSITION_AUX4: out.push_back(audio::channel_aux4); break;
case PA_CHANNEL_POSITION_AUX5: out.push_back(audio::channel_aux5); break;
case PA_CHANNEL_POSITION_AUX6: out.push_back(audio::channel_aux6); break;
case PA_CHANNEL_POSITION_AUX7: out.push_back(audio::channel_aux7); break;
case PA_CHANNEL_POSITION_AUX8: out.push_back(audio::channel_aux8); break;
case PA_CHANNEL_POSITION_AUX9: out.push_back(audio::channel_aux9); break;
case PA_CHANNEL_POSITION_AUX10: out.push_back(audio::channel_aux10); break;
case PA_CHANNEL_POSITION_AUX11: out.push_back(audio::channel_aux11); break;
case PA_CHANNEL_POSITION_AUX12: out.push_back(audio::channel_aux12); break;
case PA_CHANNEL_POSITION_AUX13: out.push_back(audio::channel_aux13); break;
case PA_CHANNEL_POSITION_AUX14: out.push_back(audio::channel_aux14); break;
case PA_CHANNEL_POSITION_AUX15: out.push_back(audio::channel_aux15); break;
case PA_CHANNEL_POSITION_AUX16: out.push_back(audio::channel_aux16); break;
case PA_CHANNEL_POSITION_AUX17: out.push_back(audio::channel_aux17); break;
case PA_CHANNEL_POSITION_AUX18: out.push_back(audio::channel_aux18); break;
case PA_CHANNEL_POSITION_AUX19: out.push_back(audio::channel_aux19); break;
case PA_CHANNEL_POSITION_AUX20: out.push_back(audio::channel_aux20); break;
case PA_CHANNEL_POSITION_AUX21: out.push_back(audio::channel_aux21); break;
case PA_CHANNEL_POSITION_AUX22: out.push_back(audio::channel_aux22); break;
case PA_CHANNEL_POSITION_AUX23: out.push_back(audio::channel_aux23); break;
case PA_CHANNEL_POSITION_AUX24: out.push_back(audio::channel_aux24); break;
case PA_CHANNEL_POSITION_AUX25: out.push_back(audio::channel_aux25); break;
case PA_CHANNEL_POSITION_AUX26: out.push_back(audio::channel_aux26); break;
case PA_CHANNEL_POSITION_AUX27: out.push_back(audio::channel_aux27); break;
case PA_CHANNEL_POSITION_AUX28: out.push_back(audio::channel_aux28); break;
case PA_CHANNEL_POSITION_AUX29: out.push_back(audio::channel_aux29); break;
case PA_CHANNEL_POSITION_AUX30: out.push_back(audio::channel_aux30); break;
case PA_CHANNEL_POSITION_AUX31: out.push_back(audio::channel_aux31); break;
}
}
return out;
}
// Callback on getting data from pulseaudio:
static void callbackGetSinkList(pa_context* _contex, const pa_sink_info* _info, int _eol, void* _userdata) {
std::vector<audio::orchestra::api::pulse::Element>* list = static_cast<std::vector<audio::orchestra::api::pulse::Element>*>(_userdata);
std::vector<audio::orchestra::DeviceInfo>* list = static_cast<std::vector<audio::orchestra::DeviceInfo>*>(_userdata);
// If eol is set to a positive number, you're at the end of the list
if (_eol > 0) {
return;
}
ATA_INFO("find output : " << _info->name);
list->push_back(audio::orchestra::api::pulse::Element(_info->index, false, _info->name, _info->description));
audio::orchestra::DeviceInfo info;
info.input = false;
info.name = _info->name;
info.desc = _info->description;
info.sampleRates.push_back(_info->sample_spec.rate);
info.nativeFormats.push_back(getFormatFromPulseFormat(_info->sample_spec.format));
info.channels = getChannelOrderFromPulseChannel(_info->channel_map);
ATA_VERBOSE("plop=" << _info->index << " " << _info->name);
//ATA_DEBUG(" ports=" << _info->n_ports);
list->push_back(info);
}
// allback to get data from pulseaudio:
static void callbackGetSourceList(pa_context* _contex, const pa_source_info* _info, int _eol, void* _userdata) {
std::vector<audio::orchestra::api::pulse::Element>* list = static_cast<std::vector<audio::orchestra::api::pulse::Element>*>(_userdata);
std::vector<audio::orchestra::DeviceInfo>* list = static_cast<std::vector<audio::orchestra::DeviceInfo>*>(_userdata);
if (_eol > 0) {
return;
}
ATA_INFO("find input : " << _info->name);
list->push_back(audio::orchestra::api::pulse::Element(_info->index, true, _info->name, _info->description));
audio::orchestra::DeviceInfo info;
info.input = true;
info.name = _info->name;
info.desc = _info->description;
info.sampleRates.push_back(_info->sample_spec.rate);
info.nativeFormats.push_back(getFormatFromPulseFormat(_info->sample_spec.format));
info.channels = getChannelOrderFromPulseChannel(_info->channel_map);
ATA_VERBOSE("plop=" << _info->index << " " << _info->name);
list->push_back(info);
}
std::vector<audio::orchestra::api::pulse::Element> audio::orchestra::api::pulse::getDeviceList() {
// to not update all the time ...
static std::vector<audio::orchestra::DeviceInfo> pulseAudioListOfDevice;
static audio::Time pulseAudioListOfDeviceTime;
std::vector<audio::orchestra::DeviceInfo> audio::orchestra::api::pulse::getDeviceList() {
audio::Duration delta = audio::Time::now() - pulseAudioListOfDeviceTime;
if (delta < audio::Duration(30,0)) {
return pulseAudioListOfDevice;
}
// Define our pulse audio loop and connection variables
pa_mainloop* pulseAudioMainLoop;
pa_mainloop_api* pulseAudioMainLoopAPI;
pa_operation* pulseAudioOperation;
pa_context* pulseAudioContex;
pa_context_flags_t pulseAudioFlags;
std::vector<audio::orchestra::api::pulse::Element> out;
pa_context_flags_t pulseAudioFlags = PA_CONTEXT_NOAUTOSPAWN;
std::vector<audio::orchestra::DeviceInfo>& out = pulseAudioListOfDevice;
out.clear();
// We'll need these state variables to keep track of our requests
int state = 0;
int pulseAudioReady = 0;
// Create a mainloop API and connection to the default server
pulseAudioMainLoop = pa_mainloop_new();
pulseAudioMainLoopAPI = pa_mainloop_get_api(pulseAudioMainLoop);
pulseAudioContex = pa_context_new(pulseAudioMainLoopAPI, "test");
// This function connects to the pulse server
pa_context_connect(pulseAudioContex, NULL, pulseAudioFlags, NULL);
pulseAudioContex = pa_context_new(pulseAudioMainLoopAPI, "orchestraPulseCount");
// If there's an error, the callback will set pulseAudioReady
pa_context_set_state_callback(pulseAudioContex, callbackStateMachine, &pulseAudioReady);
ATA_INFO("start main loop...");
while (true) {
ATA_INFO("loop");
// This function connects to the pulse server
pa_context_connect(pulseAudioContex, NULL, pulseAudioFlags, NULL);
bool playLoop = true;
while (playLoop == true) {
// We can't do anything until PA is ready, so just iterate the mainloop
// and continue
if (pulseAudioReady == 0) {
ATA_INFO("Pulse not ready");
pa_mainloop_iterate(pulseAudioMainLoop, 1, nullptr);
continue;
}
// We couldn't get a connection to the server, so exit out
if (pulseAudioReady == 2) {
ATA_INFO("pulse not ready");
pa_context_disconnect(pulseAudioContex);
pa_context_unref(pulseAudioContex);
pa_mainloop_free(pulseAudioMainLoop);
@ -111,7 +287,7 @@ std::vector<audio::orchestra::api::pulse::Element> audio::orchestra::api::pulse:
switch (state) {
// State 0: we haven't done anything yet
case 0:
ATA_INFO("Request sink list");
ATA_DEBUG("Request sink list");
pulseAudioOperation = pa_context_get_sink_info_list(pulseAudioContex,
callbackGetSinkList,
&out);
@ -123,7 +299,7 @@ std::vector<audio::orchestra::api::pulse::Element> audio::orchestra::api::pulse:
// along to the next state
if (pa_operation_get_state(pulseAudioOperation) == PA_OPERATION_DONE) {
pa_operation_unref(pulseAudioOperation);
ATA_INFO("Request sources list");
ATA_DEBUG("Request sources list");
pulseAudioOperation = pa_context_get_source_info_list(pulseAudioContex,
callbackGetSourceList,
&out);
@ -132,13 +308,14 @@ std::vector<audio::orchestra::api::pulse::Element> audio::orchestra::api::pulse:
break;
case 2:
if (pa_operation_get_state(pulseAudioOperation) == PA_OPERATION_DONE) {
ATA_INFO("All is done");
ATA_DEBUG("All is done");
// Now we're done, clean up and disconnect and return
pa_operation_unref(pulseAudioOperation);
pa_context_disconnect(pulseAudioContex);
pa_context_unref(pulseAudioContex);
pa_mainloop_free(pulseAudioMainLoop);
return out;
playLoop = false;
break;
}
break;
default:
@ -147,7 +324,32 @@ std::vector<audio::orchestra::api::pulse::Element> audio::orchestra::api::pulse:
return out;
}
// Iterate the main loop ..
if (playLoop == true) {
pa_mainloop_iterate(pulseAudioMainLoop, 1, nullptr);
}
}
// TODO: need to do it better ...
// set default device:
int32_t idInput = -1;
int32_t idOutput = -1;
for (int32_t iii=0; iii<out.size(); ++iii) {
if (out[iii].input == true) {
if (idInput != -1) {
continue;
}
if (etk::end_with(out[iii].name, ".monitor", false) == false) {
idInput = iii;
out[iii].isDefault = true;
}
} else {
if (idOutput != -1) {
continue;
}
if (etk::end_with(out[iii].name, ".monitor", false) == false) {
idOutput = iii;
out[iii].isDefault = true;
}
}
}
return out;
}

View File

@ -9,39 +9,13 @@
#define __AUDIO_ORCHESTRA_API_PULSE_DEVICE_H__
#include <etk/types.h>
#include <audio/orchestra/DeviceInfo.h>
namespace audio {
namespace orchestra {
namespace api {
namespace pulse {
class Element {
private:
size_t m_index;
bool m_input;
std::string m_name;
std::string m_description;
public:
Element(size_t _index, bool _input, const std::string& _name, const std::string& _desc) :
m_index(_index),
m_input(_input),
m_name(_name),
m_description(_desc) {
// nothing to do...
}
size_t getIndex() const {
return m_index;
}
bool isInput() const {
return m_input;
}
const std::string& getName() const {
return m_name;
}
const std::string& getDescription() const {
return m_description;
}
};
std::vector<audio::orchestra::api::pulse::Element> getDeviceList();
std::vector<audio::orchestra::DeviceInfo> getDeviceList();
}
}
}