audio-orchestra/airtaudio/Interface.cpp

138 lines
3.9 KiB
C++
Raw Normal View History

/**
* @author Gary P. SCAVONE
*
* @copyright 2001-2013 Gary P. Scavone, all right reserved
*
* @license like MIT (see license file)
*/
#include <airtaudio/Interface.h>
2014-03-12 23:55:49 +01:00
#include <airtaudio/debug.h>
#include <iostream>
std::vector<airtaudio::api::type> airtaudio::Interface::getCompiledApi(void) {
std::vector<airtaudio::api::type> apis;
// The order here will control the order of RtAudio's API search in
// the constructor.
2014-03-11 22:37:22 +01:00
for (auto &it : m_apiAvaillable) {
apis.push_back(it.first);
}
return apis;
}
2014-03-11 22:37:22 +01:00
void airtaudio::Interface::openRtApi(airtaudio::api::type _api) {
if (m_rtapi != NULL) {
delete m_rtapi;
m_rtapi = NULL;
}
2014-03-11 22:37:22 +01:00
for (auto &it :m_apiAvaillable) {
if (_api == it.first) {
m_rtapi = it.second();
if (m_rtapi != NULL) {
return;
}
}
}
2014-03-11 22:37:22 +01:00
// TODO : An eror occured ...
}
airtaudio::Interface::Interface(void) :
m_rtapi(NULL) {
#if defined(__UNIX_JACK__)
addInterface(airtaudio::api::UNIX_JACK, airtaudio::api::Jack::Create);
#endif
#if defined(__LINUX_ALSA__)
2014-03-11 22:37:22 +01:00
addInterface(airtaudio::api::LINUX_ALSA, airtaudio::api::Alsa::Create);
#endif
#if defined(__LINUX_PULSE__)
2014-03-11 22:37:22 +01:00
addInterface(airtaudio::api::LINUX_PULSE, airtaudio::api::Pulse::Create);
#endif
#if defined(__LINUX_OSS__)
2014-03-11 22:37:22 +01:00
addInterface(airtaudio::api::LINUX_OSS, airtaudio::api::Oss::Create);
#endif
#if defined(__WINDOWS_ASIO__)
2014-03-11 22:37:22 +01:00
addInterface(airtaudio::api::WINDOWS_ASIO, airtaudio::api::Asio::Create);
#endif
#if defined(__WINDOWS_DS__)
2014-03-11 22:37:22 +01:00
addInterface(airtaudio::api::WINDOWS_DS, airtaudio::api::Ds::Create);
#endif
#if defined(__MACOSX_CORE__)
2014-03-11 22:37:22 +01:00
addInterface(airtaudio::api::MACOSX_CORE, airtaudio::api::Core::Create);
#endif
#if defined(__AIRTAUDIO_DUMMY__)
2014-03-11 22:37:22 +01:00
addInterface(airtaudio::api::RTAUDIO_DUMMY, airtaudio::api::Dummy::Create);
#endif
}
2014-03-11 22:37:22 +01:00
void airtaudio::Interface::addInterface(airtaudio::api::type _api, Api* (*_callbackCreate)(void)) {
m_apiAvaillable.push_back(std::pair<airtaudio::api::type, Api* (*)(void)>(_api, _callbackCreate));
}
enum airtaudio::errorType airtaudio::Interface::instanciate(airtaudio::api::type _api) {
if (m_rtapi != NULL) {
std::cerr << "\nInterface already started ...!\n" << std::endl;
return airtaudio::errorNone;
}
if (_api != airtaudio::api::UNSPECIFIED) {
// Attempt to open the specified API.
openRtApi(_api);
if (m_rtapi != NULL) {
2014-03-11 22:37:22 +01:00
return airtaudio::errorNone;
}
// No compiled support for specified API value. Issue a debug
// warning and continue as if no API was specified.
std::cerr << "\nRtAudio: no compiled support for specified API argument!\n" << std::endl;
2014-03-11 22:37:22 +01:00
return airtaudio::errorFail;
}
// Iterate through the compiled APIs and return as soon as we find
// one with at least one device or we reach the end of the list.
std::vector<airtaudio::api::type> apis = getCompiledApi();
2014-03-11 22:37:22 +01:00
for (auto &it : apis) {
openRtApi(it);
if (m_rtapi->getDeviceCount() != 0) {
break;
}
}
if (m_rtapi != NULL) {
2014-03-11 22:37:22 +01:00
return airtaudio::errorNone;
}
std::cout << "\nRtAudio: no compiled API support found ... critical error!!\n\n";
2014-03-11 22:37:22 +01:00
return airtaudio::errorFail;
}
airtaudio::Interface::~Interface(void) {
if (m_rtapi != NULL) {
delete m_rtapi;
m_rtapi = NULL;
}
}
2014-03-11 22:37:22 +01:00
enum airtaudio::errorType airtaudio::Interface::openStream(
airtaudio::StreamParameters* _outputParameters,
airtaudio::StreamParameters* _inputParameters,
airtaudio::format _format,
uint32_t _sampleRate,
uint32_t* _bufferFrames,
airtaudio::AirTAudioCallback _callback,
void* _userData,
2014-03-12 23:55:49 +01:00
airtaudio::StreamOptions* _options) {
if (m_rtapi == NULL) {
2014-03-12 23:55:49 +01:00
return airtaudio::errorInputNull;
}
return m_rtapi->openStream(_outputParameters,
_inputParameters,
_format,
_sampleRate,
_bufferFrames,
_callback,
_userData,
2014-03-12 23:55:49 +01:00
_options);
}