Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
7905 lines (6858 sloc) 270 KB
/************************************************************************/
/*! \class RtAudio
\brief Realtime audio i/o C++ classes.
RtAudio provides a common API (Application Programming Interface)
for realtime audio input/output across Linux (native ALSA, Jack,
and OSS), Macintosh OS X (CoreAudio and Jack), and Windows
(DirectSound and ASIO) operating systems.
RtAudio WWW site: http://www.music.mcgill.ca/~gary/rtaudio/
RtAudio: realtime audio i/o C++ classes
Copyright (c) 2001-2010 Gary P. Scavone
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
Any person wishing to distribute modifications to the Software is
asked to send the modifications to the original developer so that
they can be incorporated into the canonical version. This is,
however, not a binding provision of this license.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/************************************************************************/
// RtAudio: Version 4.0.7
#include "RtAudio.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <climits>
// Static variable definitions.
const unsigned int RtApi::MAX_SAMPLE_RATES = 14;
const unsigned int RtApi::SAMPLE_RATES[] = {
4000, 5512, 8000, 9600, 11025, 16000, 22050,
32000, 44100, 48000, 88200, 96000, 176400, 192000
};
#if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__)
#define MUTEX_INITIALIZE(A) InitializeCriticalSection(A)
#define MUTEX_DESTROY(A) DeleteCriticalSection(A)
#define MUTEX_LOCK(A) EnterCriticalSection(A)
#define MUTEX_UNLOCK(A) LeaveCriticalSection(A)
#elif defined(__LINUX_ALSA__) || defined(__UNIX_JACK__) || defined(__LINUX_OSS__) || defined(__MACOSX_CORE__)
// pthread API
#define MUTEX_INITIALIZE(A) pthread_mutex_init(A, NULL)
#define MUTEX_DESTROY(A) pthread_mutex_destroy(A)
#define MUTEX_LOCK(A) pthread_mutex_lock(A)
#define MUTEX_UNLOCK(A) pthread_mutex_unlock(A)
#else
#define MUTEX_INITIALIZE(A) abs(*A) // dummy definitions
#define MUTEX_DESTROY(A) abs(*A) // dummy definitions
#endif
// *************************************************** //
//
// RtAudio definitions.
//
// *************************************************** //
void RtAudio :: getCompiledApi( std::vector<RtAudio::Api> &apis ) throw()
{
apis.clear();
// The order here will control the order of RtAudio's API search in
// the constructor.
#if defined(__UNIX_JACK__)
apis.push_back( UNIX_JACK );
#endif
#if defined(__LINUX_ALSA__)
apis.push_back( LINUX_ALSA );
#endif
#if defined(__LINUX_OSS__)
apis.push_back( LINUX_OSS );
#endif
#if defined(__WINDOWS_ASIO__)
apis.push_back( WINDOWS_ASIO );
#endif
#if defined(__WINDOWS_DS__)
apis.push_back( WINDOWS_DS );
#endif
#if defined(__MACOSX_CORE__)
apis.push_back( MACOSX_CORE );
#endif
#if defined(__RTAUDIO_DUMMY__)
apis.push_back( RTAUDIO_DUMMY );
#endif
}
void RtAudio :: openRtApi( RtAudio::Api api )
{
#if defined(__UNIX_JACK__)
if ( api == UNIX_JACK )
rtapi_ = new RtApiJack();
#endif
#if defined(__LINUX_ALSA__)
if ( api == LINUX_ALSA )
rtapi_ = new RtApiAlsa();
#endif
#if defined(__LINUX_OSS__)
if ( api == LINUX_OSS )
rtapi_ = new RtApiOss();
#endif
#if defined(__WINDOWS_ASIO__)
if ( api == WINDOWS_ASIO )
rtapi_ = new RtApiAsio();
#endif
#if defined(__WINDOWS_DS__)
if ( api == WINDOWS_DS )
rtapi_ = new RtApiDs();
#endif
#if defined(__MACOSX_CORE__)
if ( api == MACOSX_CORE )
rtapi_ = new RtApiCore();
#endif
#if defined(__RTAUDIO_DUMMY__)
if ( api == RTAUDIO_DUMMY )
rtapi_ = new RtApiDummy();
#endif
}
RtAudio :: RtAudio( RtAudio::Api api ) throw()
{
rtapi_ = 0;
if ( api != UNSPECIFIED ) {
// Attempt to open the specified API.
openRtApi( api );
if ( rtapi_ ) return;
// 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;
}
// 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< RtAudio::Api > apis;
getCompiledApi( apis );
for ( unsigned int i=0; i<apis.size(); i++ ) {
openRtApi( apis[i] );
if ( rtapi_->getDeviceCount() ) break;
}
if ( rtapi_ ) return;
// It should not be possible to get here because the preprocessor
// definition __RTAUDIO_DUMMY__ is automatically defined if no
// API-specific definitions are passed to the compiler. But just in
// case something weird happens, we'll print out an error message.
std::cerr << "\nRtAudio: no compiled API support found ... critical error!!\n\n";
}
RtAudio :: ~RtAudio() throw()
{
delete rtapi_;
}
void RtAudio :: openStream( RtAudio::StreamParameters *outputParameters,
RtAudio::StreamParameters *inputParameters,
RtAudioFormat format, unsigned int sampleRate,
unsigned int *bufferFrames,
RtAudioCallback callback, void *userData,
RtAudio::StreamOptions *options )
{
return rtapi_->openStream( outputParameters, inputParameters, format,
sampleRate, bufferFrames, callback,
userData, options );
}
// *************************************************** //
//
// Public RtApi definitions (see end of file for
// private or protected utility functions).
//
// *************************************************** //
RtApi :: RtApi()
{
stream_.state = STREAM_CLOSED;
stream_.mode = UNINITIALIZED;
stream_.apiHandle = 0;
stream_.userBuffer[0] = 0;
stream_.userBuffer[1] = 0;
MUTEX_INITIALIZE( &stream_.mutex );
showWarnings_ = true;
}
RtApi :: ~RtApi()
{
MUTEX_DESTROY( &stream_.mutex );
}
void RtApi :: openStream( RtAudio::StreamParameters *oParams,
RtAudio::StreamParameters *iParams,
RtAudioFormat format, unsigned int sampleRate,
unsigned int *bufferFrames,
RtAudioCallback callback, void *userData,
RtAudio::StreamOptions *options )
{
if ( stream_.state != STREAM_CLOSED ) {
errorText_ = "RtApi::openStream: a stream is already open!";
error( RtError::INVALID_USE );
}
if ( oParams && oParams->nChannels < 1 ) {
errorText_ = "RtApi::openStream: a non-NULL output StreamParameters structure cannot have an nChannels value less than one.";
error( RtError::INVALID_USE );
}
if ( iParams && iParams->nChannels < 1 ) {
errorText_ = "RtApi::openStream: a non-NULL input StreamParameters structure cannot have an nChannels value less than one.";
error( RtError::INVALID_USE );
}
if ( oParams == NULL && iParams == NULL ) {
errorText_ = "RtApi::openStream: input and output StreamParameters structures are both NULL!";
error( RtError::INVALID_USE );
}
if ( formatBytes(format) == 0 ) {
errorText_ = "RtApi::openStream: 'format' parameter value is undefined.";
error( RtError::INVALID_USE );
}
unsigned int nDevices = getDeviceCount();
unsigned int oChannels = 0;
if ( oParams ) {
oChannels = oParams->nChannels;
if ( oParams->deviceId >= nDevices ) {
errorText_ = "RtApi::openStream: output device parameter value is invalid.";
error( RtError::INVALID_USE );
}
}
unsigned int iChannels = 0;
if ( iParams ) {
iChannels = iParams->nChannels;
if ( iParams->deviceId >= nDevices ) {
errorText_ = "RtApi::openStream: input device parameter value is invalid.";
error( RtError::INVALID_USE );
}
}
clearStreamInfo();
bool result;
if ( oChannels > 0 ) {
result = probeDeviceOpen( oParams->deviceId, OUTPUT, oChannels, oParams->firstChannel,
sampleRate, format, bufferFrames, options );
if ( result == false ) error( RtError::SYSTEM_ERROR );
}
if ( iChannels > 0 ) {
result = probeDeviceOpen( iParams->deviceId, INPUT, iChannels, iParams->firstChannel,
sampleRate, format, bufferFrames, options );
if ( result == false ) {
if ( oChannels > 0 ) closeStream();
error( RtError::SYSTEM_ERROR );
}
}
stream_.callbackInfo.callback = (void *) callback;
stream_.callbackInfo.userData = userData;
if ( options ) options->numberOfBuffers = stream_.nBuffers;
stream_.state = STREAM_STOPPED;
}
unsigned int RtApi :: getDefaultInputDevice( void )
{
// Should be implemented in subclasses if possible.
return 0;
}
unsigned int RtApi :: getDefaultOutputDevice( void )
{
// Should be implemented in subclasses if possible.
return 0;
}
void RtApi :: closeStream( void )
{
// MUST be implemented in subclasses!
return;
}
bool RtApi :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,
unsigned int firstChannel, unsigned int sampleRate,
RtAudioFormat format, unsigned int *bufferSize,
RtAudio::StreamOptions *options )
{
// MUST be implemented in subclasses!
return FAILURE;
}
void RtApi :: tickStreamTime( void )
{
// Subclasses that do not provide their own implementation of
// getStreamTime should call this function once per buffer I/O to
// provide basic stream time support.
stream_.streamTime += ( stream_.bufferSize * 1.0 / stream_.sampleRate );
#if defined( HAVE_GETTIMEOFDAY )
gettimeofday( &stream_.lastTickTimestamp, NULL );
#endif
}
long RtApi :: getStreamLatency( void )
{
verifyStream();
long totalLatency = 0;
if ( stream_.mode == OUTPUT || stream_.mode == DUPLEX )
totalLatency = stream_.latency[0];
if ( stream_.mode == INPUT || stream_.mode == DUPLEX )
totalLatency += stream_.latency[1];
return totalLatency;
}
double RtApi :: getStreamTime( void )
{
verifyStream();
#if defined( HAVE_GETTIMEOFDAY )
// Return a very accurate estimate of the stream time by
// adding in the elapsed time since the last tick.
struct timeval then;
struct timeval now;
if ( stream_.state != STREAM_RUNNING || stream_.streamTime == 0.0 )
return stream_.streamTime;
gettimeofday( &now, NULL );
then = stream_.lastTickTimestamp;
return stream_.streamTime +
((now.tv_sec + 0.000001 * now.tv_usec) -
(then.tv_sec + 0.000001 * then.tv_usec));
#else
return stream_.streamTime;
#endif
}
unsigned int RtApi :: getStreamSampleRate( void )
{
verifyStream();
return stream_.sampleRate;
}
// *************************************************** //
//
// OS/API-specific methods.
//
// *************************************************** //
#if defined(__MACOSX_CORE__)
// The OS X CoreAudio API is designed to use a separate callback
// procedure for each of its audio devices. A single RtAudio duplex
// stream using two different devices is supported here, though it
// cannot be guaranteed to always behave correctly because we cannot
// synchronize these two callbacks.
//
// A property listener is installed for over/underrun information.
// However, no functionality is currently provided to allow property
// listeners to trigger user handlers because it is unclear what could
// be done if a critical stream parameter (buffer size, sample rate,
// device disconnect) notification arrived. The listeners entail
// quite a bit of extra code and most likely, a user program wouldn't
// be prepared for the result anyway. However, we do provide a flag
// to the client callback function to inform of an over/underrun.
//
// The mechanism for querying and setting system parameters was
// updated (and perhaps simplified) in OS-X version 10.4. However,
// since 10.4 support is not necessarily available to all users, I've
// decided not to update the respective code at this time. Perhaps
// this will happen when Apple makes 10.4 free for everyone. :-)
// A structure to hold various information related to the CoreAudio API
// implementation.
struct CoreHandle {
AudioDeviceID id[2]; // device ids
#if defined( MAC_OS_X_VERSION_10_5 ) && ( MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 )
AudioDeviceIOProcID procId[2];
#endif
UInt32 iStream[2]; // device stream index (or first if using multiple)
UInt32 nStreams[2]; // number of streams to use
bool xrun[2];
char *deviceBuffer;
pthread_cond_t condition;
int drainCounter; // Tracks callback counts when draining
bool internalDrain; // Indicates if stop is initiated from callback or not.
CoreHandle()
:deviceBuffer(0), drainCounter(0), internalDrain(false) { nStreams[0] = 1; nStreams[1] = 1; id[0] = 0; id[1] = 0; xrun[0] = false; xrun[1] = false; }
};
RtApiCore :: RtApiCore()
{
// Nothing to do here.
}
RtApiCore :: ~RtApiCore()
{
// The subclass destructor gets called before the base class
// destructor, so close an existing stream before deallocating
// apiDeviceId memory.
if ( stream_.state != STREAM_CLOSED ) closeStream();
}
unsigned int RtApiCore :: getDeviceCount( void )
{
// Find out how many audio devices there are, if any.
UInt32 dataSize;
AudioObjectPropertyAddress propertyAddress = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
OSStatus result = AudioObjectGetPropertyDataSize( kAudioObjectSystemObject, &propertyAddress, 0, NULL, &dataSize );
if ( result != noErr ) {
errorText_ = "RtApiCore::getDeviceCount: OS-X error getting device info!";
error( RtError::WARNING );
return 0;
}
return dataSize / sizeof( AudioDeviceID );
}
unsigned int RtApiCore :: getDefaultInputDevice( void )
{
unsigned int nDevices = getDeviceCount();
if ( nDevices <= 1 ) return 0;
AudioDeviceID id;
UInt32 dataSize = sizeof( AudioDeviceID );
AudioObjectPropertyAddress property = { kAudioHardwarePropertyDefaultInputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
OSStatus result = AudioObjectGetPropertyData( kAudioObjectSystemObject, &property, 0, NULL, &dataSize, &id );
if ( result != noErr ) {
errorText_ = "RtApiCore::getDefaultInputDevice: OS-X system error getting device.";
error( RtError::WARNING );
return 0;
}
dataSize *= nDevices;
AudioDeviceID deviceList[ nDevices ];
property.mSelector = kAudioHardwarePropertyDevices;
result = AudioObjectGetPropertyData( kAudioObjectSystemObject, &property, 0, NULL, &dataSize, (void *) &deviceList );
if ( result != noErr ) {
errorText_ = "RtApiCore::getDefaultInputDevice: OS-X system error getting device IDs.";
error( RtError::WARNING );
return 0;
}
for ( unsigned int i=0; i<nDevices; i++ )
if ( id == deviceList[i] ) return i;
errorText_ = "RtApiCore::getDefaultInputDevice: No default device found!";
error( RtError::WARNING );
return 0;
}
unsigned int RtApiCore :: getDefaultOutputDevice( void )
{
unsigned int nDevices = getDeviceCount();
if ( nDevices <= 1 ) return 0;
AudioDeviceID id;
UInt32 dataSize = sizeof( AudioDeviceID );
AudioObjectPropertyAddress property = { kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
OSStatus result = AudioObjectGetPropertyData( kAudioObjectSystemObject, &property, 0, NULL, &dataSize, &id );
if ( result != noErr ) {
errorText_ = "RtApiCore::getDefaultOutputDevice: OS-X system error getting device.";
error( RtError::WARNING );
return 0;
}
dataSize = sizeof( AudioDeviceID ) * nDevices;
AudioDeviceID deviceList[ nDevices ];
property.mSelector = kAudioHardwarePropertyDevices;
result = AudioObjectGetPropertyData( kAudioObjectSystemObject, &property, 0, NULL, &dataSize, (void *) &deviceList );
if ( result != noErr ) {
errorText_ = "RtApiCore::getDefaultOutputDevice: OS-X system error getting device IDs.";
error( RtError::WARNING );
return 0;
}
for ( unsigned int i=0; i<nDevices; i++ )
if ( id == deviceList[i] ) return i;
errorText_ = "RtApiCore::getDefaultOutputDevice: No default device found!";
error( RtError::WARNING );
return 0;
}
RtAudio::DeviceInfo RtApiCore :: getDeviceInfo( unsigned int device )
{
RtAudio::DeviceInfo info;
info.probed = false;
// Get device ID
unsigned int nDevices = getDeviceCount();
if ( nDevices == 0 ) {
errorText_ = "RtApiCore::getDeviceInfo: no devices found!";
error( RtError::INVALID_USE );
}
if ( device >= nDevices ) {
errorText_ = "RtApiCore::getDeviceInfo: device ID is invalid!";
error( RtError::INVALID_USE );
}
AudioDeviceID deviceList[ nDevices ];
UInt32 dataSize = sizeof( AudioDeviceID ) * nDevices;
AudioObjectPropertyAddress property = { kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster };
OSStatus result = AudioObjectGetPropertyData( kAudioObjectSystemObject, &property,
0, NULL, &dataSize, (void *) &deviceList );
if ( result != noErr ) {
errorText_ = "RtApiCore::getDeviceInfo: OS-X system error getting device IDs.";
error( RtError::WARNING );
return info;
}
AudioDeviceID id = deviceList[ device ];
// Get the device name.
info.name.erase();
CFStringRef cfname;
dataSize = sizeof( CFStringRef );
property.mSelector = kAudioObjectPropertyManufacturer;
result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, &cfname );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceInfo: system error (" << getErrorCode( result ) << ") getting device manufacturer.";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
const char *mname = CFStringGetCStringPtr( cfname, CFStringGetSystemEncoding() );
info.name.append( (const char *)mname, strlen(mname) );
info.name.append( ": " );
CFRelease( cfname );
property.mSelector = kAudioObjectPropertyName;
result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, &cfname );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceInfo: system error (" << getErrorCode( result ) << ") getting device name.";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
const char *name = CFStringGetCStringPtr( cfname, CFStringGetSystemEncoding() );
info.name.append( (const char *)name, strlen(name) );
CFRelease( cfname );
// Get the output stream "configuration".
AudioBufferList *bufferList = nil;
property.mSelector = kAudioDevicePropertyStreamConfiguration;
property.mScope = kAudioDevicePropertyScopeOutput;
// property.mElement = kAudioObjectPropertyElementWildcard;
dataSize = 0;
result = AudioObjectGetPropertyDataSize( id, &property, 0, NULL, &dataSize );
if ( result != noErr || dataSize == 0 ) {
errorStream_ << "RtApiCore::getDeviceInfo: system error (" << getErrorCode( result ) << ") getting output stream configuration info for device (" << device << ").";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
// Allocate the AudioBufferList.
bufferList = (AudioBufferList *) malloc( dataSize );
if ( bufferList == NULL ) {
errorText_ = "RtApiCore::getDeviceInfo: memory error allocating output AudioBufferList.";
error( RtError::WARNING );
return info;
}
result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, bufferList );
if ( result != noErr || dataSize == 0 ) {
free( bufferList );
errorStream_ << "RtApiCore::getDeviceInfo: system error (" << getErrorCode( result ) << ") getting output stream configuration for device (" << device << ").";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
// Get output channel information.
unsigned int i, nStreams = bufferList->mNumberBuffers;
for ( i=0; i<nStreams; i++ )
info.outputChannels += bufferList->mBuffers[i].mNumberChannels;
free( bufferList );
// Get the input stream "configuration".
property.mScope = kAudioDevicePropertyScopeInput;
result = AudioObjectGetPropertyDataSize( id, &property, 0, NULL, &dataSize );
if ( result != noErr || dataSize == 0 ) {
errorStream_ << "RtApiCore::getDeviceInfo: system error (" << getErrorCode( result ) << ") getting input stream configuration info for device (" << device << ").";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
// Allocate the AudioBufferList.
bufferList = (AudioBufferList *) malloc( dataSize );
if ( bufferList == NULL ) {
errorText_ = "RtApiCore::getDeviceInfo: memory error allocating input AudioBufferList.";
error( RtError::WARNING );
return info;
}
result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, bufferList );
if (result != noErr || dataSize == 0) {
free( bufferList );
errorStream_ << "RtApiCore::getDeviceInfo: system error (" << getErrorCode( result ) << ") getting input stream configuration for device (" << device << ").";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
// Get input channel information.
nStreams = bufferList->mNumberBuffers;
for ( i=0; i<nStreams; i++ )
info.inputChannels += bufferList->mBuffers[i].mNumberChannels;
free( bufferList );
// 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;
// Probe the device sample rates.
bool isInput = false;
if ( info.outputChannels == 0 ) isInput = true;
// Determine the supported sample rates.
property.mSelector = kAudioDevicePropertyAvailableNominalSampleRates;
if ( isInput == false ) property.mScope = kAudioDevicePropertyScopeOutput;
result = AudioObjectGetPropertyDataSize( id, &property, 0, NULL, &dataSize );
if ( result != kAudioHardwareNoError || dataSize == 0 ) {
errorStream_ << "RtApiCore::getDeviceInfo: system error (" << getErrorCode( result ) << ") getting sample rate info.";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
UInt32 nRanges = dataSize / sizeof( AudioValueRange );
AudioValueRange rangeList[ nRanges ];
result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, &rangeList );
if ( result != kAudioHardwareNoError ) {
errorStream_ << "RtApiCore::getDeviceInfo: system error (" << getErrorCode( result ) << ") getting sample rates.";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
Float64 minimumRate = 100000000.0, maximumRate = 0.0;
for ( UInt32 i=0; i<nRanges; i++ ) {
if ( rangeList[i].mMinimum < minimumRate ) minimumRate = rangeList[i].mMinimum;
if ( rangeList[i].mMaximum > maximumRate ) maximumRate = rangeList[i].mMaximum;
}
info.sampleRates.clear();
for ( unsigned int k=0; k<MAX_SAMPLE_RATES; k++ ) {
if ( SAMPLE_RATES[k] >= (unsigned int) minimumRate && SAMPLE_RATES[k] <= (unsigned int) maximumRate )
info.sampleRates.push_back( SAMPLE_RATES[k] );
}
if ( info.sampleRates.size() == 0 ) {
errorStream_ << "RtApiCore::probeDeviceInfo: No supported sample rates found for device (" << device << ").";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
// CoreAudio always uses 32-bit floating point data for PCM streams.
// Thus, any other "physical" formats supported by the device are of
// no interest to the client.
info.nativeFormats = RTAUDIO_FLOAT32;
if ( info.outputChannels > 0 )
if ( getDefaultOutputDevice() == device ) info.isDefaultOutput = true;
if ( info.inputChannels > 0 )
if ( getDefaultInputDevice() == device ) info.isDefaultInput = true;
info.probed = true;
return info;
}
OSStatus callbackHandler( AudioDeviceID inDevice,
const AudioTimeStamp* inNow,
const AudioBufferList* inInputData,
const AudioTimeStamp* inInputTime,
AudioBufferList* outOutputData,
const AudioTimeStamp* inOutputTime,
void* infoPointer )
{
CallbackInfo *info = (CallbackInfo *) infoPointer;
RtApiCore *object = (RtApiCore *) info->object;
if ( object->callbackEvent( inDevice, inInputData, outOutputData ) == false )
return kAudioHardwareUnspecifiedError;
else
return kAudioHardwareNoError;
}
OSStatus deviceListener( AudioObjectID inDevice,
UInt32 nAddresses,
const AudioObjectPropertyAddress properties[],
void* handlePointer )
{
CoreHandle *handle = (CoreHandle *) handlePointer;
for ( UInt32 i=0; i<nAddresses; i++ ) {
if ( properties[i].mSelector == kAudioDeviceProcessorOverload ) {
if ( properties[i].mScope == kAudioDevicePropertyScopeInput )
handle->xrun[1] = true;
else
handle->xrun[0] = true;
}
}
return kAudioHardwareNoError;
}
bool RtApiCore :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,
unsigned int firstChannel, unsigned int sampleRate,
RtAudioFormat format, unsigned int *bufferSize,
RtAudio::StreamOptions *options )
{
// Get device ID
unsigned int nDevices = getDeviceCount();
if ( nDevices == 0 ) {
// This should not happen because a check is made before this function is called.
errorText_ = "RtApiCore::probeDeviceOpen: no devices found!";
return FAILURE;
}
if ( device >= nDevices ) {
// This should not happen because a check is made before this function is called.
errorText_ = "RtApiCore::probeDeviceOpen: device ID is invalid!";
return FAILURE;
}
AudioDeviceID deviceList[ nDevices ];
UInt32 dataSize = sizeof( AudioDeviceID ) * nDevices;
AudioObjectPropertyAddress property = { kAudioHardwarePropertyDevices,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster };
OSStatus result = AudioObjectGetPropertyData( kAudioObjectSystemObject, &property,
0, NULL, &dataSize, (void *) &deviceList );
if ( result != noErr ) {
errorText_ = "RtApiCore::probeDeviceOpen: OS-X system error getting device IDs.";
return FAILURE;
}
AudioDeviceID id = deviceList[ device ];
// Setup for stream mode.
bool isInput = false;
if ( mode == INPUT ) {
isInput = true;
property.mScope = kAudioDevicePropertyScopeInput;
}
else
property.mScope = kAudioDevicePropertyScopeOutput;
// Get the stream "configuration".
AudioBufferList *bufferList = nil;
dataSize = 0;
property.mSelector = kAudioDevicePropertyStreamConfiguration;
result = AudioObjectGetPropertyDataSize( id, &property, 0, NULL, &dataSize );
if ( result != noErr || dataSize == 0 ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") getting stream configuration info for device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
// Allocate the AudioBufferList.
bufferList = (AudioBufferList *) malloc( dataSize );
if ( bufferList == NULL ) {
errorText_ = "RtApiCore::probeDeviceOpen: memory error allocating AudioBufferList.";
return FAILURE;
}
result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, bufferList );
if (result != noErr || dataSize == 0) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") getting stream configuration for device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
// Search for one or more streams that contain the desired number of
// channels. CoreAudio devices can have an arbitrary number of
// streams and each stream can have an arbitrary number of channels.
// For each stream, a single buffer of interleaved samples is
// provided. RtAudio prefers the use of one stream of interleaved
// data or multiple consecutive single-channel streams. However, we
// now support multiple consecutive multi-channel streams of
// interleaved data as well.
UInt32 iStream, offsetCounter = firstChannel;
UInt32 nStreams = bufferList->mNumberBuffers;
bool monoMode = false;
bool foundStream = false;
// First check that the device supports the requested number of
// channels.
UInt32 deviceChannels = 0;
for ( iStream=0; iStream<nStreams; iStream++ )
deviceChannels += bufferList->mBuffers[iStream].mNumberChannels;
if ( deviceChannels < ( channels + firstChannel ) ) {
free( bufferList );
errorStream_ << "RtApiCore::probeDeviceOpen: the device (" << device << ") does not support the requested channel count.";
errorText_ = errorStream_.str();
return FAILURE;
}
// Look for a single stream meeting our needs.
UInt32 firstStream, streamCount = 1, streamChannels = 0, channelOffset = 0;
for ( iStream=0; iStream<nStreams; iStream++ ) {
streamChannels = bufferList->mBuffers[iStream].mNumberChannels;
if ( streamChannels >= channels + offsetCounter ) {
firstStream = iStream;
channelOffset = offsetCounter;
foundStream = true;
break;
}
if ( streamChannels > offsetCounter ) break;
offsetCounter -= streamChannels;
}
// If we didn't find a single stream above, then we should be able
// to meet the channel specification with multiple streams.
if ( foundStream == false ) {
monoMode = true;
offsetCounter = firstChannel;
for ( iStream=0; iStream<nStreams; iStream++ ) {
streamChannels = bufferList->mBuffers[iStream].mNumberChannels;
if ( streamChannels > offsetCounter ) break;
offsetCounter -= streamChannels;
}
firstStream = iStream;
channelOffset = offsetCounter;
Int32 channelCounter = channels + offsetCounter - streamChannels;
if ( streamChannels > 1 ) monoMode = false;
while ( channelCounter > 0 ) {
streamChannels = bufferList->mBuffers[++iStream].mNumberChannels;
if ( streamChannels > 1 ) monoMode = false;
channelCounter -= streamChannels;
streamCount++;
}
}
free( bufferList );
// Determine the buffer size.
AudioValueRange bufferRange;
dataSize = sizeof( AudioValueRange );
property.mSelector = kAudioDevicePropertyBufferFrameSizeRange;
result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, &bufferRange );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") getting buffer size range for device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
if ( bufferRange.mMinimum > *bufferSize ) *bufferSize = (unsigned long) bufferRange.mMinimum;
else if ( bufferRange.mMaximum < *bufferSize ) *bufferSize = (unsigned long) bufferRange.mMaximum;
if ( options && options->flags & RTAUDIO_MINIMIZE_LATENCY ) *bufferSize = (unsigned long) bufferRange.mMinimum;
// Set the buffer size. For multiple streams, I'm assuming we only
// need to make this setting for the master channel.
UInt32 theSize = (UInt32) *bufferSize;
dataSize = sizeof( UInt32 );
property.mSelector = kAudioDevicePropertyBufferFrameSize;
result = AudioObjectSetPropertyData( id, &property, 0, NULL, dataSize, &theSize );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") setting the buffer size for device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
// If attempting to setup a duplex stream, the bufferSize parameter
// MUST be the same in both directions!
*bufferSize = theSize;
if ( stream_.mode == OUTPUT && mode == INPUT && *bufferSize != stream_.bufferSize ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error setting buffer size for duplex stream on device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
stream_.bufferSize = *bufferSize;
stream_.nBuffers = 1;
// Check and if necessary, change the sample rate for the device.
Float64 nominalRate;
dataSize = sizeof( Float64 );
property.mSelector = kAudioDevicePropertyNominalSampleRate;
result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, &nominalRate );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") getting current sample rate.";
errorText_ = errorStream_.str();
return FAILURE;
}
// Only change the sample rate if off by more than 1 Hz.
if ( fabs( nominalRate - (double)sampleRate ) > 1.0 ) {
nominalRate = (Float64) sampleRate;
result = AudioObjectSetPropertyData( id, &property, 0, NULL, dataSize, &nominalRate );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") setting sample rate for device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
}
// Try to set "hog" mode ... it's not clear to me this is working.
if ( options && options->flags & RTAUDIO_HOG_DEVICE ) {
pid_t hog_pid;
dataSize = sizeof( hog_pid );
property.mSelector = kAudioDevicePropertyHogMode;
result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, &hog_pid );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") getting 'hog' state!";
errorText_ = errorStream_.str();
return FAILURE;
}
if ( hog_pid != getpid() ) {
hog_pid = getpid();
result = AudioObjectSetPropertyData( id, &property, 0, NULL, dataSize, &hog_pid );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") setting 'hog' state!";
errorText_ = errorStream_.str();
return FAILURE;
}
}
}
// Get the stream ID(s) so we can set the stream format.
AudioStreamID streamIDs[ nStreams ];
dataSize = nStreams * sizeof( AudioStreamID );
property.mSelector = kAudioDevicePropertyStreams;
result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, &streamIDs );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") getting stream ID(s) for device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
// Now set the stream format for each stream. Also, check the
// physical format of the device and change that if necessary.
AudioStreamBasicDescription description;
dataSize = sizeof( AudioStreamBasicDescription );
bool updateFormat;
for ( UInt32 i=0; i<streamCount; i++ ) {
property.mSelector = kAudioStreamPropertyVirtualFormat;
result = AudioObjectGetPropertyData( streamIDs[firstStream+i], &property, 0, NULL, &dataSize, &description );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") getting stream format for device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
// Set the sample rate and data format id. However, only make the
// change if the sample rate is not within 1.0 of the desired
// rate and the format is not linear pcm.
updateFormat = false;
if ( fabs( description.mSampleRate - (double)sampleRate ) > 1.0 ) {
description.mSampleRate = (double) sampleRate;
updateFormat = true;
}
if ( description.mFormatID != kAudioFormatLinearPCM ) {
description.mFormatID = kAudioFormatLinearPCM;
updateFormat = true;
}
if ( updateFormat ) {
result = AudioObjectSetPropertyData( streamIDs[firstStream+i], &property, 0, NULL, dataSize, &description );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") setting sample rate or data format for device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
}
// Now check the physical format.
property.mSelector = kAudioStreamPropertyPhysicalFormat;
result = AudioObjectGetPropertyData( streamIDs[firstStream+i], &property, 0, NULL, &dataSize, &description );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") getting stream physical format for device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
if ( description.mFormatID != kAudioFormatLinearPCM || description.mBitsPerChannel < 24 ) {
description.mFormatID = kAudioFormatLinearPCM;
AudioStreamBasicDescription testDescription = description;
unsigned long formatFlags;
// We'll try higher bit rates first and then work our way down.
testDescription.mBitsPerChannel = 32;
testDescription.mBytesPerFrame = testDescription.mBitsPerChannel/8 * testDescription.mChannelsPerFrame;
testDescription.mBytesPerPacket = testDescription.mBytesPerFrame * testDescription.mFramesPerPacket;
formatFlags = description.mFormatFlags | kLinearPCMFormatFlagIsFloat & ~kLinearPCMFormatFlagIsSignedInteger;
testDescription.mFormatFlags = formatFlags;
result = AudioObjectSetPropertyData( streamIDs[firstStream+i], &property, 0, NULL, dataSize, &testDescription );
if ( result == noErr ) continue;
testDescription = description;
testDescription.mBitsPerChannel = 32;
testDescription.mBytesPerFrame = testDescription.mBitsPerChannel/8 * testDescription.mChannelsPerFrame;
testDescription.mBytesPerPacket = testDescription.mBytesPerFrame * testDescription.mFramesPerPacket;
formatFlags = (description.mFormatFlags | kLinearPCMFormatFlagIsSignedInteger) & ~kLinearPCMFormatFlagIsFloat;
testDescription.mFormatFlags = formatFlags;
result = AudioObjectSetPropertyData( streamIDs[firstStream+i], &property, 0, NULL, dataSize, &testDescription );
if ( result == noErr ) continue;
testDescription = description;
testDescription.mBitsPerChannel = 24;
testDescription.mBytesPerFrame = testDescription.mBitsPerChannel/8 * testDescription.mChannelsPerFrame;
testDescription.mBytesPerPacket = testDescription.mBytesPerFrame * testDescription.mFramesPerPacket;
testDescription.mFormatFlags = formatFlags;
result = AudioObjectSetPropertyData( streamIDs[firstStream+i], &property, 0, NULL, dataSize, &testDescription );
if ( result == noErr ) continue;
testDescription = description;
testDescription.mBitsPerChannel = 16;
testDescription.mBytesPerFrame = testDescription.mBitsPerChannel/8 * testDescription.mChannelsPerFrame;
testDescription.mBytesPerPacket = testDescription.mBytesPerFrame * testDescription.mFramesPerPacket;
testDescription.mFormatFlags = formatFlags;
result = AudioObjectSetPropertyData( streamIDs[firstStream+i], &property, 0, NULL, dataSize, &testDescription );
if ( result == noErr ) continue;
testDescription = description;
testDescription.mBitsPerChannel = 8;
testDescription.mBytesPerFrame = testDescription.mBitsPerChannel/8 * testDescription.mChannelsPerFrame;
testDescription.mBytesPerPacket = testDescription.mBytesPerFrame * testDescription.mFramesPerPacket;
testDescription.mFormatFlags = formatFlags;
result = AudioObjectSetPropertyData( streamIDs[firstStream+i], &property, 0, NULL, dataSize, &testDescription );
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") setting physical data format for device (" << device << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
}
}
// Get the stream latency. There can be latency in both the device
// and the stream. First, attempt to get the device latency on the
// master channel or the first open channel. Errors that might
// occur here are not deemed critical.
UInt32 latency;
dataSize = sizeof( UInt32 );
property.mSelector = kAudioDevicePropertyLatency;
if ( AudioObjectHasProperty( id, &property ) == true ) {
result = AudioObjectGetPropertyData( id, &property, 0, NULL, &dataSize, &latency );
if ( result == kAudioHardwareNoError ) stream_.latency[ mode ] = latency;
else {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") getting device latency for device (" << device << ").";
errorText_ = errorStream_.str();
error( RtError::WARNING );
}
}
// Now try to get the stream latency. For multiple streams, I assume the
// latency is equal for each.
result = AudioObjectGetPropertyData( streamIDs[firstStream], &property, 0, NULL, &dataSize, &latency );
if ( result == kAudioHardwareNoError ) stream_.latency[ mode ] += latency;
else {
errorStream_ << "RtApiCore::probeDeviceOpen: system error (" << getErrorCode( result ) << ") getting stream latency for device (" << device << ").";
errorText_ = errorStream_.str();
error( RtError::WARNING );
}
// Byte-swapping: According to AudioHardware.h, the stream data will
// always be presented in native-endian format, so we should never
// need to byte swap.
stream_.doByteSwap[mode] = false;
// From the CoreAudio documentation, PCM data must be supplied as
// 32-bit floats.
stream_.userFormat = format;
stream_.deviceFormat[mode] = RTAUDIO_FLOAT32;
if ( streamCount == 1 )
stream_.nDeviceChannels[mode] = description.mChannelsPerFrame;
else // multiple streams
stream_.nDeviceChannels[mode] = channels;
stream_.nUserChannels[mode] = channels;
stream_.channelOffset[mode] = channelOffset; // offset within a CoreAudio stream
if ( options && options->flags & RTAUDIO_NONINTERLEAVED ) stream_.userInterleaved = false;
else stream_.userInterleaved = true;
stream_.deviceInterleaved[mode] = true;
if ( monoMode == true ) stream_.deviceInterleaved[mode] = false;
// Set flags for buffer conversion.
stream_.doConvertBuffer[mode] = false;
if ( stream_.userFormat != stream_.deviceFormat[mode] )
stream_.doConvertBuffer[mode] = true;
if ( stream_.nUserChannels[mode] < stream_.nDeviceChannels[mode] )
stream_.doConvertBuffer[mode] = true;
if ( streamCount == 1 ) {
if ( stream_.nUserChannels[mode] > 1 &&
stream_.userInterleaved != stream_.deviceInterleaved[mode] )
stream_.doConvertBuffer[mode] = true;
}
else if ( monoMode && stream_.userInterleaved )
stream_.doConvertBuffer[mode] = true;
// Allocate our CoreHandle structure for the stream.
CoreHandle *handle = 0;
if ( stream_.apiHandle == 0 ) {
try {
handle = new CoreHandle;
}
catch ( std::bad_alloc& ) {
errorText_ = "RtApiCore::probeDeviceOpen: error allocating CoreHandle memory.";
goto error;
}
if ( pthread_cond_init( &handle->condition, NULL ) ) {
errorText_ = "RtApiCore::probeDeviceOpen: error initializing pthread condition variable.";
goto error;
}
stream_.apiHandle = (void *) handle;
}
else
handle = (CoreHandle *) stream_.apiHandle;
handle->iStream[mode] = firstStream;
handle->nStreams[mode] = streamCount;
handle->id[mode] = id;
// Allocate necessary internal buffers.
unsigned long bufferBytes;
bufferBytes = stream_.nUserChannels[mode] * *bufferSize * formatBytes( stream_.userFormat );
stream_.userBuffer[mode] = (char *) calloc( bufferBytes, 1 );
if ( stream_.userBuffer[mode] == NULL ) {
errorText_ = "RtApiCore::probeDeviceOpen: error allocating user buffer memory.";
goto error;
}
// If possible, we will make use of the CoreAudio stream buffers as
// "device buffers". However, we can't do this if using multiple
// streams.
if ( stream_.doConvertBuffer[mode] && handle->nStreams[mode] > 1 ) {
bool makeBuffer = true;
bufferBytes = stream_.nDeviceChannels[mode] * formatBytes( stream_.deviceFormat[mode] );
if ( mode == INPUT ) {
if ( stream_.mode == OUTPUT && stream_.deviceBuffer ) {
unsigned long bytesOut = stream_.nDeviceChannels[0] * formatBytes( stream_.deviceFormat[0] );
if ( bufferBytes <= bytesOut ) makeBuffer = false;
}
}
if ( makeBuffer ) {
bufferBytes *= *bufferSize;
if ( stream_.deviceBuffer ) free( stream_.deviceBuffer );
stream_.deviceBuffer = (char *) calloc( bufferBytes, 1 );
if ( stream_.deviceBuffer == NULL ) {
errorText_ = "RtApiCore::probeDeviceOpen: error allocating device buffer memory.";
goto error;
}
}
}
stream_.sampleRate = sampleRate;
stream_.device[mode] = device;
stream_.state = STREAM_STOPPED;
stream_.callbackInfo.object = (void *) this;
// Setup the buffer conversion information structure.
if ( stream_.doConvertBuffer[mode] ) {
if ( streamCount > 1 ) setConvertInfo( mode, 0 );
else setConvertInfo( mode, channelOffset );
}
if ( mode == INPUT && stream_.mode == OUTPUT && stream_.device[0] == device )
// Only one callback procedure per device.
stream_.mode = DUPLEX;
else {
#if defined( MAC_OS_X_VERSION_10_5 ) && ( MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 )
result = AudioDeviceCreateIOProcID( id, callbackHandler, (void *) &stream_.callbackInfo, &handle->procId[mode] );
#else
// deprecated in favor of AudioDeviceCreateIOProcID()
result = AudioDeviceAddIOProc( id, callbackHandler, (void *) &stream_.callbackInfo );
#endif
if ( result != noErr ) {
errorStream_ << "RtApiCore::probeDeviceOpen: system error setting callback for device (" << device << ").";
errorText_ = errorStream_.str();
goto error;
}
if ( stream_.mode == OUTPUT && mode == INPUT )
stream_.mode = DUPLEX;
else
stream_.mode = mode;
}
// Setup the device property listener for over/underload.
property.mSelector = kAudioDeviceProcessorOverload;
result = AudioObjectAddPropertyListener( id, &property, deviceListener, (void *) handle );
return SUCCESS;
error:
if ( handle ) {
pthread_cond_destroy( &handle->condition );
delete handle;
stream_.apiHandle = 0;
}
for ( int i=0; i<2; i++ ) {
if ( stream_.userBuffer[i] ) {
free( stream_.userBuffer[i] );
stream_.userBuffer[i] = 0;
}
}
if ( stream_.deviceBuffer ) {
free( stream_.deviceBuffer );
stream_.deviceBuffer = 0;
}
return FAILURE;
}
void RtApiCore :: closeStream( void )
{
if ( stream_.state == STREAM_CLOSED ) {
errorText_ = "RtApiCore::closeStream(): no open stream to close!";
error( RtError::WARNING );
return;
}
CoreHandle *handle = (CoreHandle *) stream_.apiHandle;
if ( stream_.mode == OUTPUT || stream_.mode == DUPLEX ) {
if ( stream_.state == STREAM_RUNNING )
AudioDeviceStop( handle->id[0], callbackHandler );
#if defined( MAC_OS_X_VERSION_10_5 ) && ( MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 )
AudioDeviceDestroyIOProcID( handle->id[0], handle->procId[0] );
#else
// deprecated in favor of AudioDeviceDestroyIOProcID()
AudioDeviceRemoveIOProc( handle->id[0], callbackHandler );
#endif
}
if ( stream_.mode == INPUT || ( stream_.mode == DUPLEX && stream_.device[0] != stream_.device[1] ) ) {
if ( stream_.state == STREAM_RUNNING )
AudioDeviceStop( handle->id[1], callbackHandler );
#if defined( MAC_OS_X_VERSION_10_5 ) && ( MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5 )
AudioDeviceDestroyIOProcID( handle->id[1], handle->procId[1] );
#else
// deprecated in favor of AudioDeviceDestroyIOProcID()
AudioDeviceRemoveIOProc( handle->id[1], callbackHandler );
#endif
}
for ( int i=0; i<2; i++ ) {
if ( stream_.userBuffer[i] ) {
free( stream_.userBuffer[i] );
stream_.userBuffer[i] = 0;
}
}
if ( stream_.deviceBuffer ) {
free( stream_.deviceBuffer );
stream_.deviceBuffer = 0;
}
// Destroy pthread condition variable.
pthread_cond_destroy( &handle->condition );
delete handle;
stream_.apiHandle = 0;
stream_.mode = UNINITIALIZED;
stream_.state = STREAM_CLOSED;
}
void RtApiCore :: startStream( void )
{
verifyStream();
if ( stream_.state == STREAM_RUNNING ) {
errorText_ = "RtApiCore::startStream(): the stream is already running!";
error( RtError::WARNING );
return;
}
MUTEX_LOCK( &stream_.mutex );
OSStatus result = noErr;
CoreHandle *handle = (CoreHandle *) stream_.apiHandle;
if ( stream_.mode == OUTPUT || stream_.mode == DUPLEX ) {
result = AudioDeviceStart( handle->id[0], callbackHandler );
if ( result != noErr ) {
errorStream_ << "RtApiCore::startStream: system error (" << getErrorCode( result ) << ") starting callback procedure on device (" << stream_.device[0] << ").";
errorText_ = errorStream_.str();
goto unlock;
}
}
if ( stream_.mode == INPUT ||
( stream_.mode == DUPLEX && stream_.device[0] != stream_.device[1] ) ) {
result = AudioDeviceStart( handle->id[1], callbackHandler );
if ( result != noErr ) {
errorStream_ << "RtApiCore::startStream: system error starting input callback procedure on device (" << stream_.device[1] << ").";
errorText_ = errorStream_.str();
goto unlock;
}
}
handle->drainCounter = 0;
handle->internalDrain = false;
stream_.state = STREAM_RUNNING;
unlock:
MUTEX_UNLOCK( &stream_.mutex );
if ( result == noErr ) return;
error( RtError::SYSTEM_ERROR );
}
void RtApiCore :: stopStream( void )
{
verifyStream();
if ( stream_.state == STREAM_STOPPED ) {
errorText_ = "RtApiCore::stopStream(): the stream is already stopped!";
error( RtError::WARNING );
return;
}
MUTEX_LOCK( &stream_.mutex );
if ( stream_.state == STREAM_STOPPED ) {
MUTEX_UNLOCK( &stream_.mutex );
return;
}
OSStatus result = noErr;
CoreHandle *handle = (CoreHandle *) stream_.apiHandle;
if ( stream_.mode == OUTPUT || stream_.mode == DUPLEX ) {
if ( handle->drainCounter == 0 ) {
handle->drainCounter = 1;
pthread_cond_wait( &handle->condition, &stream_.mutex ); // block until signaled
}
MUTEX_UNLOCK( &stream_.mutex );
result = AudioDeviceStop( handle->id[0], callbackHandler );
MUTEX_LOCK( &stream_.mutex );
if ( result != noErr ) {
errorStream_ << "RtApiCore::stopStream: system error (" << getErrorCode( result ) << ") stopping callback procedure on device (" << stream_.device[0] << ").";
errorText_ = errorStream_.str();
goto unlock;
}
}
if ( stream_.mode == INPUT || ( stream_.mode == DUPLEX && stream_.device[0] != stream_.device[1] ) ) {
result = AudioDeviceStop( handle->id[1], callbackHandler );
if ( result != noErr ) {
errorStream_ << "RtApiCore::stopStream: system error (" << getErrorCode( result ) << ") stopping input callback procedure on device (" << stream_.device[1] << ").";
errorText_ = errorStream_.str();
goto unlock;
}
}
stream_.state = STREAM_STOPPED;
unlock:
MUTEX_UNLOCK( &stream_.mutex );
if ( result == noErr ) return;
error( RtError::SYSTEM_ERROR );
}
void RtApiCore :: abortStream( void )
{
verifyStream();
if ( stream_.state == STREAM_STOPPED ) {
errorText_ = "RtApiCore::abortStream(): the stream is already stopped!";
error( RtError::WARNING );
return;
}
CoreHandle *handle = (CoreHandle *) stream_.apiHandle;
handle->drainCounter = 1;
stopStream();
}
bool RtApiCore :: callbackEvent( AudioDeviceID deviceId,
const AudioBufferList *inBufferList,
const AudioBufferList *outBufferList )
{
if ( stream_.state == STREAM_STOPPED ) return SUCCESS;
if ( stream_.state == STREAM_CLOSED ) {
errorText_ = "RtApiCore::callbackEvent(): the stream is closed ... this shouldn't happen!";
error( RtError::WARNING );
return FAILURE;
}
CallbackInfo *info = (CallbackInfo *) &stream_.callbackInfo;
CoreHandle *handle = (CoreHandle *) stream_.apiHandle;
// Check if we were draining the stream and signal is finished.
if ( handle->drainCounter > 3 ) {
if ( handle->internalDrain == false )
pthread_cond_signal( &handle->condition );
else
stopStream();
return SUCCESS;
}
MUTEX_LOCK( &stream_.mutex );
// The state might change while waiting on a mutex.
if ( stream_.state == STREAM_STOPPED ) {
MUTEX_UNLOCK( &stream_.mutex );
return SUCCESS;
}
AudioDeviceID outputDevice = handle->id[0];
// Invoke user callback to get fresh output data UNLESS we are
// draining stream or duplex mode AND the input/output devices are
// different AND this function is called for the input device.
if ( handle->drainCounter == 0 && ( stream_.mode != DUPLEX || deviceId == outputDevice ) ) {
RtAudioCallback callback = (RtAudioCallback) info->callback;
double streamTime = getStreamTime();
RtAudioStreamStatus status = 0;
if ( stream_.mode != INPUT && handle->xrun[0] == true ) {
status |= RTAUDIO_OUTPUT_UNDERFLOW;
handle->xrun[0] = false;
}
if ( stream_.mode != OUTPUT && handle->xrun[1] == true ) {
status |= RTAUDIO_INPUT_OVERFLOW;
handle->xrun[1] = false;
}
handle->drainCounter = callback( stream_.userBuffer[0], stream_.userBuffer[1],
stream_.bufferSize, streamTime, status, info->userData );
if ( handle->drainCounter == 2 ) {
MUTEX_UNLOCK( &stream_.mutex );
abortStream();
return SUCCESS;
}
else if ( handle->drainCounter == 1 )
handle->internalDrain = true;
}
if ( stream_.mode == OUTPUT || ( stream_.mode == DUPLEX && deviceId == outputDevice ) ) {
if ( handle->drainCounter > 1 ) { // write zeros to the output stream
if ( handle->nStreams[0] == 1 ) {
memset( outBufferList->mBuffers[handle->iStream[0]].mData,
0,
outBufferList->mBuffers[handle->iStream[0]].mDataByteSize );
}
else { // fill multiple streams with zeros
for ( unsigned int i=0; i<handle->nStreams[0]; i++ ) {
memset( outBufferList->mBuffers[handle->iStream[0]+i].mData,
0,
outBufferList->mBuffers[handle->iStream[0]+i].mDataByteSize );
}
}
}
else if ( handle->nStreams[0] == 1 ) {
if ( stream_.doConvertBuffer[0] ) { // convert directly to CoreAudio stream buffer
convertBuffer( (char *) outBufferList->mBuffers[handle->iStream[0]].mData,
stream_.userBuffer[0], stream_.convertInfo[0] );
}
else { // copy from user buffer
memcpy( outBufferList->mBuffers[handle->iStream[0]].mData,
stream_.userBuffer[0],
outBufferList->mBuffers[handle->iStream[0]].mDataByteSize );
}
}
else { // fill multiple streams
Float32 *inBuffer = (Float32 *) stream_.userBuffer[0];
if ( stream_.doConvertBuffer[0] ) {
convertBuffer( stream_.deviceBuffer, stream_.userBuffer[0], stream_.convertInfo[0] );
inBuffer = (Float32 *) stream_.deviceBuffer;
}
if ( stream_.deviceInterleaved[0] == false ) { // mono mode
UInt32 bufferBytes = outBufferList->mBuffers[handle->iStream[0]].mDataByteSize;
for ( unsigned int i=0; i<stream_.nUserChannels[0]; i++ ) {
memcpy( outBufferList->mBuffers[handle->iStream[0]+i].mData,
(void *)&inBuffer[i*stream_.bufferSize], bufferBytes );
}
}
else { // fill multiple multi-channel streams with interleaved data
UInt32 streamChannels, channelsLeft, inJump, outJump, inOffset;
Float32 *out, *in;
bool inInterleaved = ( stream_.userInterleaved ) ? true : false;
UInt32 inChannels = stream_.nUserChannels[0];
if ( stream_.doConvertBuffer[0] ) {
inInterleaved = true; // device buffer will always be interleaved for nStreams > 1 and not mono mode
inChannels = stream_.nDeviceChannels[0];
}
if ( inInterleaved ) inOffset = 1;
else inOffset = stream_.bufferSize;
channelsLeft = inChannels;
for ( unsigned int i=0; i<handle->nStreams[0]; i++ ) {
in = inBuffer;
out = (Float32 *) outBufferList->mBuffers[handle->iStream[0]+i].mData;
streamChannels = outBufferList->mBuffers[handle->iStream[0]+i].mNumberChannels;
outJump = 0;
// Account for possible channel offset in first stream
if ( i == 0 && stream_.channelOffset[0] > 0 ) {
streamChannels -= stream_.channelOffset[0];
outJump = stream_.channelOffset[0];
out += outJump;
}
// Account for possible unfilled channels at end of the last stream
if ( streamChannels > channelsLeft ) {
outJump = streamChannels - channelsLeft;
streamChannels = channelsLeft;
}
// Determine input buffer offsets and skips
if ( inInterleaved ) {
inJump = inChannels;
in += inChannels - channelsLeft;
}
else {
inJump = 1;
in += (inChannels - channelsLeft) * inOffset;
}
for ( unsigned int i=0; i<stream_.bufferSize; i++ ) {
for ( unsigned int j=0; j<streamChannels; j++ ) {
*out++ = in[j*inOffset];
}
out += outJump;
in += inJump;
}
channelsLeft -= streamChannels;
}
}
}
if ( handle->drainCounter ) {
handle->drainCounter++;
goto unlock;
}
}
AudioDeviceID inputDevice;
inputDevice = handle->id[1];
if ( stream_.mode == INPUT || ( stream_.mode == DUPLEX && deviceId == inputDevice ) ) {
if ( handle->nStreams[1] == 1 ) {
if ( stream_.doConvertBuffer[1] ) { // convert directly from CoreAudio stream buffer
convertBuffer( stream_.userBuffer[1],
(char *) inBufferList->mBuffers[handle->iStream[1]].mData,
stream_.convertInfo[1] );
}
else { // copy to user buffer
memcpy( stream_.userBuffer[1],
inBufferList->mBuffers[handle->iStream[1]].mData,
inBufferList->mBuffers[handle->iStream[1]].mDataByteSize );
}
}
else { // read from multiple streams
Float32 *outBuffer = (Float32 *) stream_.userBuffer[1];
if ( stream_.doConvertBuffer[1] ) outBuffer = (Float32 *) stream_.deviceBuffer;
if ( stream_.deviceInterleaved[1] == false ) { // mono mode
UInt32 bufferBytes = inBufferList->mBuffers[handle->iStream[1]].mDataByteSize;
for ( unsigned int i=0; i<stream_.nUserChannels[1]; i++ ) {
memcpy( (void *)&outBuffer[i*stream_.bufferSize],
inBufferList->mBuffers[handle->iStream[1]+i].mData, bufferBytes );
}
}
else { // read from multiple multi-channel streams
UInt32 streamChannels, channelsLeft, inJump, outJump, outOffset;
Float32 *out, *in;
bool outInterleaved = ( stream_.userInterleaved ) ? true : false;
UInt32 outChannels = stream_.nUserChannels[1];
if ( stream_.doConvertBuffer[1] ) {
outInterleaved = true; // device buffer will always be interleaved for nStreams > 1 and not mono mode
outChannels = stream_.nDeviceChannels[1];
}
if ( outInterleaved ) outOffset = 1;
else outOffset = stream_.bufferSize;
channelsLeft = outChannels;
for ( unsigned int i=0; i<handle->nStreams[1]; i++ ) {
out = outBuffer;
in = (Float32 *) inBufferList->mBuffers[handle->iStream[1]+i].mData;
streamChannels = inBufferList->mBuffers[handle->iStream[1]+i].mNumberChannels;
inJump = 0;
// Account for possible channel offset in first stream
if ( i == 0 && stream_.channelOffset[1] > 0 ) {
streamChannels -= stream_.channelOffset[1];
inJump = stream_.channelOffset[1];
in += inJump;
}
// Account for possible unread channels at end of the last stream
if ( streamChannels > channelsLeft ) {
inJump = streamChannels - channelsLeft;
streamChannels = channelsLeft;
}
// Determine output buffer offsets and skips
if ( outInterleaved ) {
outJump = outChannels;
out += outChannels - channelsLeft;
}
else {
outJump = 1;
out += (outChannels - channelsLeft) * outOffset;
}
for ( unsigned int i=0; i<stream_.bufferSize; i++ ) {
for ( unsigned int j=0; j<streamChannels; j++ ) {
out[j*outOffset] = *in++;
}
out += outJump;
in += inJump;
}
channelsLeft -= streamChannels;
}
}
if ( stream_.doConvertBuffer[1] ) { // convert from our internal "device" buffer
convertBuffer( stream_.userBuffer[1],
stream_.deviceBuffer,
stream_.convertInfo[1] );
}
}
}
unlock:
MUTEX_UNLOCK( &stream_.mutex );
RtApi::tickStreamTime();
return SUCCESS;
}
const char* RtApiCore :: getErrorCode( OSStatus code )
{
switch( code ) {
case kAudioHardwareNotRunningError:
return "kAudioHardwareNotRunningError";
case kAudioHardwareUnspecifiedError:
return "kAudioHardwareUnspecifiedError";
case kAudioHardwareUnknownPropertyError:
return "kAudioHardwareUnknownPropertyError";
case kAudioHardwareBadPropertySizeError:
return "kAudioHardwareBadPropertySizeError";
case kAudioHardwareIllegalOperationError:
return "kAudioHardwareIllegalOperationError";
case kAudioHardwareBadObjectError:
return "kAudioHardwareBadObjectError";
case kAudioHardwareBadDeviceError:
return "kAudioHardwareBadDeviceError";
case kAudioHardwareBadStreamError:
return "kAudioHardwareBadStreamError";
case kAudioHardwareUnsupportedOperationError:
return "kAudioHardwareUnsupportedOperationError";
case kAudioDeviceUnsupportedFormatError:
return "kAudioDeviceUnsupportedFormatError";
case kAudioDevicePermissionsError:
return "kAudioDevicePermissionsError";
default:
return "CoreAudio unknown error";
}
}
//******************** End of __MACOSX_CORE__ *********************//
#endif
#if defined(__UNIX_JACK__)
// JACK is a low-latency audio server, originally written for the
// GNU/Linux operating system and now also ported to OS-X. It can
// connect a number of different applications to an audio device, as
// well as allowing them to share audio between themselves.
//
// When using JACK with RtAudio, "devices" refer to JACK clients that
// have ports connected to the server. The JACK server is typically
// started in a terminal as follows:
//
// .jackd -d alsa -d hw:0
//
// or through an interface program such as qjackctl. Many of the
// parameters normally set for a stream are fixed by the JACK server
// and can be specified when the JACK server is started. In
// particular,
//
// .jackd -d alsa -d hw:0 -r 44100 -p 512 -n 4
//
// specifies a sample rate of 44100 Hz, a buffer size of 512 sample
// frames, and number of buffers = 4. Once the server is running, it
// is not possible to override these values. If the values are not
// specified in the command-line, the JACK server uses default values.
//
// The JACK server does not have to be running when an instance of
// RtApiJack is created, though the function getDeviceCount() will
// report 0 devices found until JACK has been started. When no
// devices are available (i.e., the JACK server is not running), a
// stream cannot be opened.
#include <jack/jack.h>
#include <unistd.h>
#include <cstdio>
// A structure to hold various information related to the Jack API
// implementation.
struct JackHandle {
jack_client_t *client;
jack_port_t **ports[2];
std::string deviceName[2];
bool xrun[2];
pthread_cond_t condition;
int drainCounter; // Tracks callback counts when draining
bool internalDrain; // Indicates if stop is initiated from callback or not.
JackHandle()
:client(0), drainCounter(0), internalDrain(false) { ports[0] = 0; ports[1] = 0; xrun[0] = false; xrun[1] = false; }
};
ThreadHandle threadId;
void jackSilentError( const char * ) {};
RtApiJack :: RtApiJack()
{
// Nothing to do here.
#if !defined(__RTAUDIO_DEBUG__)
// Turn off Jack's internal error reporting.
jack_set_error_function( &jackSilentError );
#endif
}
RtApiJack :: ~RtApiJack()
{
if ( stream_.state != STREAM_CLOSED ) closeStream();
}
unsigned int RtApiJack :: getDeviceCount( void )
{
// See if we can become a jack client.
jack_options_t options = (jack_options_t) ( JackNoStartServer | JackUseExactName ); //JackNullOption;
jack_status_t *status = NULL;
jack_client_t *client = jack_client_open( "RtApiJackCount", options, status );
if ( client == 0 ) return 0;
const char **ports;
std::string port, previousPort;
unsigned int nChannels = 0, nDevices = 0;
ports = jack_get_ports( client, NULL, NULL, 0 );
if ( ports ) {
// Parse the port names up to the first colon (:).
size_t iColon = 0;
do {
port = (char *) ports[ nChannels ];
iColon = port.find(":");
if ( iColon != std::string::npos ) {
port = port.substr( 0, iColon + 1 );
if ( port != previousPort ) {
nDevices++;
previousPort = port;
}
}
} while ( ports[++nChannels] );
free( ports );
}
jack_client_close( client );
return nDevices;
}
RtAudio::DeviceInfo RtApiJack :: getDeviceInfo( unsigned int device )
{
RtAudio::DeviceInfo info;
info.probed = false;
jack_options_t options = (jack_options_t) ( JackNoStartServer | JackUseExactName ); //JackNullOption
jack_status_t *status = NULL;
jack_client_t *client = jack_client_open( "RtApiJackInfo", options, status );
if ( client == 0 ) {
errorText_ = "RtApiJack::getDeviceInfo: Jack server not found or connection error!";
error( RtError::WARNING );
return info;
}
const char **ports;
std::string port, previousPort;
unsigned int nPorts = 0, nDevices = 0;
ports = jack_get_ports( client, NULL, NULL, 0 );
if ( ports ) {
// Parse the port names up to the first colon (:).
size_t iColon = 0;
do {
port = (char *) ports[ nPorts ];
iColon = port.find(":");
if ( iColon != std::string::npos ) {
port = port.substr( 0, iColon );
if ( port != previousPort ) {
if ( nDevices == device ) info.name = port;
nDevices++;
previousPort = port;
}
}
} while ( ports[++nPorts] );
free( ports );
}
if ( device >= nDevices ) {
errorText_ = "RtApiJack::getDeviceInfo: device ID is invalid!";
error( RtError::INVALID_USE );
}
// 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.
unsigned int nChannels = 0;
ports = jack_get_ports( client, info.name.c_str(), NULL, JackPortIsInput );
if ( ports ) {
while ( ports[ nChannels ] ) nChannels++;
free( ports );
info.outputChannels = nChannels;
}
// Jack "output ports" equal RtAudio input channels.
nChannels = 0;
ports = jack_get_ports( client, info.name.c_str(), NULL, JackPortIsOutput );
if ( ports ) {
while ( ports[ nChannels ] ) nChannels++;
free( ports );
info.inputChannels = nChannels;
}
if ( info.outputChannels == 0 && info.inputChannels == 0 ) {
jack_client_close(client);
errorText_ = "RtApiJack::getDeviceInfo: error determining Jack input/output channels!";
error( RtError::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 = RTAUDIO_FLOAT32;
// 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;
jack_client_close(client);
info.probed = true;
return info;
}
int jackCallbackHandler( jack_nframes_t nframes, void *infoPointer )
{
CallbackInfo *info = (CallbackInfo *) infoPointer;
RtApiJack *object = (RtApiJack *) info->object;
if ( object->callbackEvent( (unsigned long) nframes ) == false ) return 1;
return 0;
}
// This function will be called by a spawned thread when the Jack
// server signals that it is shutting down. It is necessary to handle
// it this way because the jackShutdown() function must return before
// the jack_deactivate() function (in closeStream()) will return.
extern "C" void *jackCloseStream( void *ptr )
{
CallbackInfo *info = (CallbackInfo *) ptr;
RtApiJack *object = (RtApiJack *) info->object;
object->closeStream();
pthread_exit( NULL );
}
void jackShutdown( void *infoPointer )
{
CallbackInfo *info = (CallbackInfo *) infoPointer;
RtApiJack *object = (RtApiJack *) info->object;
// Check current stream state. If stopped, then we'll assume this
// was called as a result of a call to RtApiJack::stopStream (the
// deactivation of a client handle causes this function to be called).
// If not, we'll assume the Jack server is shutting down or some
// other problem occurred and we should close the stream.
if ( object->isStreamRunning() == false ) return;
pthread_create( &threadId, NULL, jackCloseStream, info );
std::cerr << "\nRtApiJack: the Jack server is shutting down this client ... stream stopped and closed!!\n" << std::endl;
}
int jackXrun( void *infoPointer )
{
JackHandle *handle = (JackHandle *) infoPointer;
if ( handle->ports[0] ) handle->xrun[0] = true;
if ( handle->ports[1] ) handle->xrun[1] = true;
return 0;
}
bool RtApiJack :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,
unsigned int firstChannel, unsigned int sampleRate,
RtAudioFormat format, unsigned int *bufferSize,
RtAudio::StreamOptions *options )
{
JackHandle *handle = (JackHandle *) stream_.apiHandle;
// Look for jack server and try to become a client (only do once per stream).
jack_client_t *client = 0;
if ( mode == OUTPUT || ( mode == INPUT && stream_.mode != OUTPUT ) ) {
jack_options_t jackoptions = (jack_options_t) ( JackNoStartServer | JackUseExactName ); //JackNullOption;
jack_status_t *status = NULL;
if ( options && !options->streamName.empty() )
client = jack_client_open( options->streamName.c_str(), jackoptions, status );
else
client = jack_client_open( "RtApiJack", jackoptions, status );
if ( client == 0 ) {
errorText_ = "RtApiJack::probeDeviceOpen: Jack server not found or connection error!";
error( RtError::WARNING );
return FAILURE;
}
}
else {
// The handle must have been created on an earlier pass.
client = handle->client;
}
const char **ports;
std::string port, previousPort, deviceName;
unsigned int nPorts = 0, nDevices = 0;
ports = jack_get_ports( client, NULL, NULL, 0 );
if ( ports ) {
// Parse the port names up to the first colon (:).
size_t iColon = 0;
do {
port = (char *) ports[ nPorts ];
iColon = port.find(":");
if ( iColon != std::string::npos ) {
port = port.substr( 0, iColon );
if ( port != previousPort ) {
if ( nDevices == device ) deviceName = port;
nDevices++;
previousPort = port;
}
}
} while ( ports[++nPorts] );
free( ports );
}
if ( device >= nDevices ) {
errorText_ = "RtApiJack::probeDeviceOpen: device ID is invalid!";
return FAILURE;
}
// Count the available ports containing the client name as device
// channels. Jack "input ports" equal RtAudio output channels.
unsigned int nChannels = 0;
unsigned long flag = JackPortIsInput;
if ( mode == INPUT ) flag = JackPortIsOutput;
ports = jack_get_ports( client, deviceName.c_str(), NULL, flag );
if ( ports ) {
while ( ports[ nChannels ] ) nChannels++;
free( ports );
}
// Compare the jack ports for specified client to the requested number of channels.
if ( nChannels < (channels + firstChannel) ) {
errorStream_ << "RtApiJack::probeDeviceOpen: requested number of channels (" << channels << ") + offset (" << firstChannel << ") not found for specified device (" << device << ":" << deviceName << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
// Check the jack server sample rate.
unsigned int jackRate = jack_get_sample_rate( client );
if ( sampleRate != jackRate ) {
jack_client_close( client );
errorStream_ << "RtApiJack::probeDeviceOpen: the requested sample rate (" << sampleRate << ") is different than the JACK server rate (" << jackRate << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
stream_.sampleRate = jackRate;
// Get the latency of the JACK port.
ports = jack_get_ports( client, deviceName.c_str(), NULL, flag );
if ( ports[ firstChannel ] )
stream_.latency[mode] = jack_port_get_latency( jack_port_by_name( client, ports[ firstChannel ] ) );
free( ports );
// The jack server always uses 32-bit floating-point data.
stream_.deviceFormat[mode] = RTAUDIO_FLOAT32;
stream_.userFormat = format;
if ( options && options->flags & RTAUDIO_NONINTERLEAVED ) stream_.userInterleaved = false;
else stream_.userInterleaved = true;
// Jack always uses non-interleaved buffers.
stream_.deviceInterleaved[mode] = false;
// Jack always provides host byte-ordered data.
stream_.doByteSwap[mode] = false;
// Get the buffer size. The buffer size and number of buffers
// (periods) is set when the jack server is started.
stream_.bufferSize = (int) jack_get_buffer_size( client );
*bufferSize = stream_.bufferSize;
stream_.nDeviceChannels[mode] = channels;
stream_.nUserChannels[mode] = channels;
// Set flags for buffer conversion.
stream_.doConvertBuffer[mode] = false;
if ( stream_.userFormat != stream_.deviceFormat[mode] )
stream_.doConvertBuffer[mode] = true;
if ( stream_.userInterleaved != stream_.deviceInterleaved[mode] &&
stream_.nUserChannels[mode] > 1 )
stream_.doConvertBuffer[mode] = true;
// Allocate our JackHandle structure for the stream.
if ( handle == 0 ) {
try {
handle = new JackHandle;
}
catch ( std::bad_alloc& ) {
errorText_ = "RtApiJack::probeDeviceOpen: error allocating JackHandle memory.";
goto error;
}
if ( pthread_cond_init(&handle->condition, NULL) ) {
errorText_ = "RtApiJack::probeDeviceOpen: error initializing pthread condition variable.";
goto error;
}
stream_.apiHandle = (void *) handle;
handle->client = client;
}
handle->deviceName[mode] = deviceName;
// Allocate necessary internal buffers.
unsigned long bufferBytes;
bufferBytes = stream_.nUserChannels[mode] * *bufferSize * formatBytes( stream_.userFormat );
stream_.userBuffer[mode] = (char *) calloc( bufferBytes, 1 );
if ( stream_.userBuffer[mode] == NULL ) {
errorText_ = "RtApiJack::probeDeviceOpen: error allocating user buffer memory.";
goto error;
}
if ( stream_.doConvertBuffer[mode] ) {
bool makeBuffer = true;
if ( mode == OUTPUT )
bufferBytes = stream_.nDeviceChannels[0] * formatBytes( stream_.deviceFormat[0] );
else { // mode == INPUT
bufferBytes = stream_.nDeviceChannels[1] * formatBytes( stream_.deviceFormat[1] );
if ( stream_.mode == OUTPUT && stream_.deviceBuffer ) {
unsigned long bytesOut = stream_.nDeviceChannels[0] * formatBytes(stream_.deviceFormat[0]);
if ( bufferBytes < bytesOut ) makeBuffer = false;
}
}
if ( makeBuffer ) {
bufferBytes *= *bufferSize;
if ( stream_.deviceBuffer ) free( stream_.deviceBuffer );
stream_.deviceBuffer = (char *) calloc( bufferBytes, 1 );
if ( stream_.deviceBuffer == NULL ) {
errorText_ = "RtApiJack::probeDeviceOpen: error allocating device buffer memory.";
goto error;
}
}
}
// Allocate memory for the Jack ports (channels) identifiers.
handle->ports[mode] = (jack_port_t **) malloc ( sizeof (jack_port_t *) * channels );
if ( handle->ports[mode] == NULL ) {
errorText_ = "RtApiJack::probeDeviceOpen: error allocating port memory.";
goto error;
}
stream_.device[mode] = device;
stream_.channelOffset[mode] = firstChannel;
stream_.state = STREAM_STOPPED;
stream_.callbackInfo.object = (void *) this;
if ( stream_.mode == OUTPUT && mode == INPUT )
// We had already set up the stream for output.
stream_.mode = DUPLEX;
else {
stream_.mode = mode;
jack_set_process_callback( handle->client, jackCallbackHandler, (void *) &stream_.callbackInfo );
jack_set_xrun_callback( handle->client, jackXrun, (void *) &handle );
jack_on_shutdown( handle->client, jackShutdown, (void *) &stream_.callbackInfo );
}
// Register our ports.
char label[64];
if ( mode == OUTPUT ) {
for ( unsigned int i=0; i<stream_.nUserChannels[0]; i++ ) {
snprintf( label, 64, "outport %d", i );
handle->ports[0][i] = jack_port_register( handle->client, (const char *)label,
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
}
}
else {
for ( unsigned int i=0; i<stream_.nUserChannels[1]; i++ ) {
snprintf( label, 64, "inport %d", i );
handle->ports[1][i] = jack_port_register( handle->client, (const char *)label,
JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0 );
}
}
// Setup the buffer conversion information structure. We don't use
// buffers to do channel offsets, so we override that parameter
// here.
if ( stream_.doConvertBuffer[mode] ) setConvertInfo( mode, 0 );
return SUCCESS;
error:
if ( handle ) {
pthread_cond_destroy( &handle->condition );
jack_client_close( handle->client );
if ( handle->ports[0] ) free( handle->ports[0] );
if ( handle->ports[1] ) free( handle->ports[1] );
delete handle;
stream_.apiHandle = 0;
}
for ( int i=0; i<2; i++ ) {
if ( stream_.userBuffer[i] ) {
free( stream_.userBuffer[i] );
stream_.userBuffer[i] = 0;
}
}
if ( stream_.deviceBuffer ) {
free( stream_.deviceBuffer );
stream_.deviceBuffer = 0;
}
return FAILURE;
}
void RtApiJack :: closeStream( void )
{
if ( stream_.state == STREAM_CLOSED ) {
errorText_ = "RtApiJack::closeStream(): no open stream to close!";
error( RtError::WARNING );
return;
}
JackHandle *handle = (JackHandle *) stream_.apiHandle;
if ( handle ) {
if ( stream_.state == STREAM_RUNNING )
jack_deactivate( handle->client );
jack_client_close( handle->client );
}
if ( handle ) {
if ( handle->ports[0] ) free( handle->ports[0] );
if ( handle->ports[1] ) free( handle->ports[1] );
pthread_cond_destroy( &handle->condition );
delete handle;
stream_.apiHandle = 0;
}
for ( int i=0; i<2; i++ ) {
if ( stream_.userBuffer[i] ) {
free( stream_.userBuffer[i] );
stream_.userBuffer[i] = 0;
}
}
if ( stream_.deviceBuffer ) {
free( stream_.deviceBuffer );
stream_.deviceBuffer = 0;
}
stream_.mode = UNINITIALIZED;
stream_.state = STREAM_CLOSED;
}
void RtApiJack :: startStream( void )
{
verifyStream();
if ( stream_.state == STREAM_RUNNING ) {
errorText_ = "RtApiJack::startStream(): the stream is already running!";
error( RtError::WARNING );
return;
}
MUTEX_LOCK(&stream_.mutex);
JackHandle *handle = (JackHandle *) stream_.apiHandle;
int result = jack_activate( handle->client );
if ( result ) {
errorText_ = "RtApiJack::startStream(): unable to activate JACK client!";
goto unlock;
}
const char **ports;
// Get the list of available ports.
if ( stream_.mode == OUTPUT || stream_.mode == DUPLEX ) {
result = 1;
ports = jack_get_ports( handle->client, handle->deviceName[0].c_str(), NULL, JackPortIsInput);
if ( ports == NULL) {
errorText_ = "RtApiJack::startStream(): error determining available JACK input ports!";
goto unlock;
}
// Now make the port connections. Since RtAudio wasn't designed to
// allow the user to select particular channels of a device, we'll
// just open the first "nChannels" ports with offset.
for ( unsigned int i=0; i<stream_.nUserChannels[0]; i++ ) {
result = 1;
if ( ports[ stream_.channelOffset[0] + i ] )
result = jack_connect( handle->client, jack_port_name( handle->ports[0][i] ), ports[ stream_.channelOffset[0] + i ] );
if ( result ) {
free( ports );
errorText_ = "RtApiJack::startStream(): error connecting output ports!";
goto unlock;
}
}
free(ports);
}
if ( stream_.mode == INPUT || stream_.mode == DUPLEX ) {
result = 1;
ports = jack_get_ports( handle->client, handle->deviceName[1].c_str(), NULL, JackPortIsOutput );
if ( ports == NULL) {
errorText_ = "RtApiJack::startStream(): error determining available JACK output ports!";
goto unlock;
}
// Now make the port connections. See note above.
for ( unsigned int i=0; i<stream_.nUserChannels[1]; i++ ) {
result = 1;
if ( ports[ stream_.channelOffset[1] + i ] )
result = jack_connect( handle->client, ports[ stream_.channelOffset[1] + i ], jack_port_name( handle->ports[1][i] ) );
if ( result ) {
free( ports );
errorText_ = "RtApiJack::startStream(): error connecting input ports!";
goto unlock;
}
}
free(ports);
}
handle->drainCounter = 0;
handle->internalDrain = false;
stream_.state = STREAM_RUNNING;
unlock:
MUTEX_UNLOCK(&stream_.mutex);
if ( result == 0 ) return;
error( RtError::SYSTEM_ERROR );
}
void RtApiJack :: stopStream( void )
{
verifyStream();
if ( stream_.state == STREAM_STOPPED ) {
errorText_ = "RtApiJack::stopStream(): the stream is already stopped!";
error( RtError::WARNING );
return;
}
MUTEX_LOCK( &stream_.mutex );
if ( stream_.state == STREAM_STOPPED ) {
MUTEX_UNLOCK( &stream_.mutex );
return;
}
JackHandle *handle = (JackHandle *) stream_.apiHandle;
if ( stream_.mode == OUTPUT || stream_.mode == DUPLEX ) {
if ( handle->drainCounter == 0 ) {
handle->drainCounter = 1;
pthread_cond_wait( &handle->condition, &stream_.mutex ); // block until signaled
}
}
jack_deactivate( handle->client );
stream_.state = STREAM_STOPPED;
MUTEX_UNLOCK( &stream_.mutex );
}
void RtApiJack :: abortStream( void )
{
verifyStream();
if ( stream_.state == STREAM_STOPPED ) {
errorText_ = "RtApiJack::abortStream(): the stream is already stopped!";
error( RtError::WARNING );
return;
}
JackHandle *handle = (JackHandle *) stream_.apiHandle;
handle->drainCounter = 1;
stopStream();
}
// This function will be called by a spawned thread when the user
// callback function signals that the stream should be stopped or
// aborted. It is necessary to handle it this way because the
// callbackEvent() function must return before the jack_deactivate()
// function will return.
extern "C" void *jackStopStream( void *ptr )
{
CallbackInfo *info = (CallbackInfo *) ptr;
RtApiJack *object = (RtApiJack *) info->object;
object->stopStream();
pthread_exit( NULL );
}
bool RtApiJack :: callbackEvent( unsigned long nframes )
{
if ( stream_.state == STREAM_STOPPED ) return SUCCESS;
if ( stream_.state == STREAM_CLOSED ) {
errorText_ = "RtApiCore::callbackEvent(): the stream is closed ... this shouldn't happen!";
error( RtError::WARNING );
return FAILURE;
}
if ( stream_.bufferSize != nframes ) {
errorText_ = "RtApiCore::callbackEvent(): the JACK buffer size has changed ... cannot process!";
error( RtError::WARNING );
return FAILURE;
}
CallbackInfo *info = (CallbackInfo *) &stream_.callbackInfo;
JackHandle *handle = (JackHandle *) stream_.apiHandle;
// Check if we were draining the stream and signal is finished.
if ( handle->drainCounter > 3 ) {
if ( handle->internalDrain == true ) {
pthread_create( &threadId, NULL, jackStopStream, info );
}
else
pthread_cond_signal( &handle->condition );
return SUCCESS;
}
MUTEX_LOCK( &stream_.mutex );
// The state might change while waiting on a mutex.
if ( stream_.state == STREAM_STOPPED ) {
MUTEX_UNLOCK( &stream_.mutex );
return SUCCESS;
}
// Invoke user callback first, to get fresh output data.
if ( handle->drainCounter == 0 ) {
RtAudioCallback callback = (RtAudioCallback) info->callback;
double streamTime = getStreamTime();
RtAudioStreamStatus status = 0;
if ( stream_.mode != INPUT && handle->xrun[0] == true ) {
status |= RTAUDIO_OUTPUT_UNDERFLOW;
handle->xrun[0] = false;
}
if ( stream_.mode != OUTPUT && handle->xrun[1] == true ) {
status |= RTAUDIO_INPUT_OVERFLOW;
handle->xrun[1] = false;
}
handle->drainCounter = callback( stream_.userBuffer[0], stream_.userBuffer[1],
stream_.bufferSize, streamTime, status, info->userData );
if ( handle->drainCounter == 2 ) {
MUTEX_UNLOCK( &stream_.mutex );
ThreadHandle id;
pthread_create( &id, NULL, jackStopStream, info );
return SUCCESS;
}
else if ( handle->drainCounter == 1 )
handle->internalDrain = true;
}
jack_default_audio_sample_t *jackbuffer;
unsigned long bufferBytes = nframes * sizeof( jack_default_audio_sample_t );
if ( stream_.mode == OUTPUT || stream_.mode == DUPLEX ) {
if ( handle->drainCounter > 0 ) { // write zeros to the output stream
for ( unsigned int i=0; i<stream_.nDeviceChannels[0]; i++ ) {
jackbuffer = (jack_default_audio_sample_t *) jack_port_get_buffer( handle->ports[0][i], (jack_nframes_t) nframes );
memset( jackbuffer, 0, bufferBytes );
}
}
else if ( stream_.doConvertBuffer[0] ) {
convertBuffer( stream_.deviceBuffer, stream_.userBuffer[0], stream_.convertInfo[0] );
for ( unsigned int i=0; i<stream_.nDeviceChannels[0]; i++ ) {
jackbuffer = (jack_default_audio_sample_t *) jack_port_get_buffer( handle->ports[0][i], (jack_nframes_t) nframes );
memcpy( jackbuffer, &stream_.deviceBuffer[i*bufferBytes], bufferBytes );
}
}
else { // no buffer conversion
for ( unsigned int i=0; i<stream_.nUserChannels[0]; i++ ) {
jackbuffer = (jack_default_audio_sample_t *) jack_port_get_buffer( handle->ports[0][i], (jack_nframes_t) nframes );
memcpy( jackbuffer, &stream_.userBuffer[0][i*bufferBytes], bufferBytes );
}
}
if ( handle->drainCounter ) {
handle->drainCounter++;
goto unlock;
}
}
if ( stream_.mode == INPUT || stream_.mode == DUPLEX ) {
if ( stream_.doConvertBuffer[1] ) {
for ( unsigned int i=0; i<stream_.nDeviceChannels[1]; i++ ) {
jackbuffer = (jack_default_audio_sample_t *) jack_port_get_buffer( handle->ports[1][i], (jack_nframes_t) nframes );
memcpy( &stream_.deviceBuffer[i*bufferBytes], jackbuffer, bufferBytes );
}
convertBuffer( stream_.userBuffer[1], stream_.deviceBuffer, stream_.convertInfo[1] );
}
else { // no buffer conversion
for ( unsigned int i=0; i<stream_.nUserChannels[1]; i++ ) {
jackbuffer = (jack_default_audio_sample_t *) jack_port_get_buffer( handle->ports[1][i], (jack_nframes_t) nframes );
memcpy( &stream_.userBuffer[1][i*bufferBytes], jackbuffer, bufferBytes );
}
}
}
unlock:
MUTEX_UNLOCK(&stream_.mutex);
RtApi::tickStreamTime();
return SUCCESS;
}
//******************** End of __UNIX_JACK__ *********************//
#endif
#if defined(__WINDOWS_ASIO__) // ASIO API on Windows
// The ASIO API is designed around a callback scheme, so this
// implementation is similar to that used for OS-X CoreAudio and Linux
// Jack. The primary constraint with ASIO is that it only allows
// access to a single driver at a time. Thus, it is not possible to
// have more than one simultaneous RtAudio stream.
//
// This implementation also requires a number of external ASIO files
// and a few global variables. The ASIO callback scheme does not
// allow for the passing of user data, so we must create a global
// pointer to our callbackInfo structure.
//
// On unix systems, we make use of a pthread condition variable.
// Since there is no equivalent in Windows, I hacked something based
// on information found in
// http://www.cs.wustl.edu/~schmidt/win32-cv-1.html.
#include "asiosys.h"
#include "asio.h"
#include "iasiothiscallresolver.h"
#include "asiodrivers.h"
#include <cmath>
AsioDrivers drivers;
ASIOCallbacks asioCallbacks;
ASIODriverInfo driverInfo;
CallbackInfo *asioCallbackInfo;
bool asioXRun;
struct AsioHandle {
int drainCounter; // Tracks callback counts when draining
bool internalDrain; // Indicates if stop is initiated from callback or not.
ASIOBufferInfo *bufferInfos;
HANDLE condition;
AsioHandle()
:drainCounter(0), internalDrain(false), bufferInfos(0) {}
};
// Function declarations (definitions at end of section)
static const char* getAsioErrorString( ASIOError result );
void sampleRateChanged( ASIOSampleRate sRate );
long asioMessages( long selector, long value, void* message, double* opt );
RtApiAsio :: RtApiAsio()
{
// ASIO cannot run on a multi-threaded appartment. You can call
// CoInitialize beforehand, but it must be for appartment threading
// (in which case, CoInitilialize will return S_FALSE here).
coInitialized_ = false;
HRESULT hr = CoInitialize( NULL );
if ( FAILED(hr) ) {
errorText_ = "RtApiAsio::ASIO requires a single-threaded appartment. Call CoInitializeEx(0,COINIT_APARTMENTTHREADED)";
error( RtError::WARNING );
}
coInitialized_ = true;
drivers.removeCurrentDriver();
driverInfo.asioVersion = 2;
// See note in DirectSound implementation about GetDesktopWindow().
driverInfo.sysRef = GetForegroundWindow();
}
RtApiAsio :: ~RtApiAsio()
{
if ( stream_.state != STREAM_CLOSED ) closeStream();
if ( coInitialized_ ) CoUninitialize();
}
unsigned int RtApiAsio :: getDeviceCount( void )
{
return (unsigned int) drivers.asioGetNumDev();
}
RtAudio::DeviceInfo RtApiAsio :: getDeviceInfo( unsigned int device )
{
RtAudio::DeviceInfo info;
info.probed = false;
// Get device ID
unsigned int nDevices = getDeviceCount();
if ( nDevices == 0 ) {
errorText_ = "RtApiAsio::getDeviceInfo: no devices found!";
error( RtError::INVALID_USE );
}
if ( device >= nDevices ) {
errorText_ = "RtApiAsio::getDeviceInfo: device ID is invalid!";
error( RtError::INVALID_USE );
}
// If a stream is already open, we cannot probe other devices. Thus, use the saved results.
if ( stream_.state != STREAM_CLOSED ) {
if ( device >= devices_.size() ) {
errorText_ = "RtApiAsio::getDeviceInfo: device ID was not present before stream was opened.";
error( RtError::WARNING );
return info;
}
return devices_[ device ];
}
char driverName[32];
ASIOError result = drivers.asioGetDriverName( (int) device, driverName, 32 );
if ( result != ASE_OK ) {
errorStream_ << "RtApiAsio::getDeviceInfo: unable to get driver name (" << getAsioErrorString( result ) << ").";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
info.name = driverName;
if ( !drivers.loadDriver( driverName ) ) {
errorStream_ << "RtApiAsio::getDeviceInfo: unable to load driver (" << driverName << ").";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
result = ASIOInit( &driverInfo );
if ( result != ASE_OK ) {
errorStream_ << "RtApiAsio::getDeviceInfo: error (" << getAsioErrorString( result ) << ") initializing driver (" << driverName << ").";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
// Determine the device channel information.
long inputChannels, outputChannels;
result = ASIOGetChannels( &inputChannels, &outputChannels );
if ( result != ASE_OK ) {
drivers.removeCurrentDriver();
errorStream_ << "RtApiAsio::getDeviceInfo: error (" << getAsioErrorString( result ) << ") getting channel count (" << driverName << ").";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
info.outputChannels = outputChannels;
info.inputChannels = inputChannels;
if ( info.outputChannels > 0 && info.inputChannels > 0 )
info.duplexChannels = (info.outputChannels > info.inputChannels) ? info.inputChannels : info.outputChannels;
// Determine the supported sample rates.
info.sampleRates.clear();
for ( unsigned int i=0; i<MAX_SAMPLE_RATES; i++ ) {
result = ASIOCanSampleRate( (ASIOSampleRate) SAMPLE_RATES[i] );
if ( result == ASE_OK )
info.sampleRates.push_back( SAMPLE_RATES[i] );
}
// Determine supported data types ... just check first channel and assume rest are the same.
ASIOChannelInfo channelInfo;
channelInfo.channel = 0;
channelInfo.isInput = true;
if ( info.inputChannels <= 0 ) channelInfo.isInput = false;
result = ASIOGetChannelInfo( &channelInfo );
if ( result != ASE_OK ) {
drivers.removeCurrentDriver();
errorStream_ << "RtApiAsio::getDeviceInfo: error (" << getAsioErrorString( result ) << ") getting driver channel info (" << driverName << ").";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
info.nativeFormats = 0;
if ( channelInfo.type == ASIOSTInt16MSB || channelInfo.type == ASIOSTInt16LSB )
info.nativeFormats |= RTAUDIO_SINT16;
else if ( channelInfo.type == ASIOSTInt32MSB || channelInfo.type == ASIOSTInt32LSB )
info.nativeFormats |= RTAUDIO_SINT32;
else if ( channelInfo.type == ASIOSTFloat32MSB || channelInfo.type == ASIOSTFloat32LSB )
info.nativeFormats |= RTAUDIO_FLOAT32;
else if ( channelInfo.type == ASIOSTFloat64MSB || channelInfo.type == ASIOSTFloat64LSB )
info.nativeFormats |= RTAUDIO_FLOAT64;
if ( info.outputChannels > 0 )
if ( getDefaultOutputDevice() == device ) info.isDefaultOutput = true;
if ( info.inputChannels > 0 )
if ( getDefaultInputDevice() == device ) info.isDefaultInput = true;
info.probed = true;
drivers.removeCurrentDriver();
return info;
}
void bufferSwitch( long index, ASIOBool processNow )
{
RtApiAsio *object = (RtApiAsio *) asioCallbackInfo->object;
object->callbackEvent( index );
}
void RtApiAsio :: saveDeviceInfo( void )
{
devices_.clear();
unsigned int nDevices = getDeviceCount();
devices_.resize( nDevices );
for ( unsigned int i=0; i<nDevices; i++ )
devices_[i] = getDeviceInfo( i );
}
bool RtApiAsio :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,
unsigned int firstChannel, unsigned int sampleRate,
RtAudioFormat format, unsigned int *bufferSize,
RtAudio::StreamOptions *options )
{
// For ASIO, a duplex stream MUST use the same driver.
if ( mode == INPUT && stream_.mode == OUTPUT && stream_.device[0] != device ) {
errorText_ = "RtApiAsio::probeDeviceOpen: an ASIO duplex stream must use the same device for input and output!";
return FAILURE;
}
char driverName[32];
ASIOError result = drivers.asioGetDriverName( (int) device, driverName, 32 );
if ( result != ASE_OK ) {
errorStream_ << "RtApiAsio::probeDeviceOpen: unable to get driver name (" << getAsioErrorString( result ) << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
// The getDeviceInfo() function will not work when a stream is open
// because ASIO does not allow multiple devices to run at the same
// time. Thus, we'll probe the system before opening a stream and
// save the results for use by getDeviceInfo().
this->saveDeviceInfo();
// Only load the driver once for duplex stream.
if ( mode != INPUT || stream_.mode != OUTPUT ) {
if ( !drivers.loadDriver( driverName ) ) {
errorStream_ << "RtApiAsio::probeDeviceOpen: unable to load driver (" << driverName << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
result = ASIOInit( &driverInfo );
if ( result != ASE_OK ) {
errorStream_ << "RtApiAsio::probeDeviceOpen: error (" << getAsioErrorString( result ) << ") initializing driver (" << driverName << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
}
// Check the device channel count.
long inputChannels, outputChannels;
result = ASIOGetChannels( &inputChannels, &outputChannels );
if ( result != ASE_OK ) {
drivers.removeCurrentDriver();
errorStream_ << "RtApiAsio::probeDeviceOpen: error (" << getAsioErrorString( result ) << ") getting channel count (" << driverName << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
if ( ( mode == OUTPUT && (channels+firstChannel) > (unsigned int) outputChannels) ||
( mode == INPUT && (channels+firstChannel) > (unsigned int) inputChannels) ) {
drivers.removeCurrentDriver();
errorStream_ << "RtApiAsio::probeDeviceOpen: driver (" << driverName << ") does not support requested channel count (" << channels << ") + offset (" << firstChannel << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
stream_.nDeviceChannels[mode] = channels;
stream_.nUserChannels[mode] = channels;
stream_.channelOffset[mode] = firstChannel;
// Verify the sample rate is supported.
result = ASIOCanSampleRate( (ASIOSampleRate) sampleRate );
if ( result != ASE_OK ) {
drivers.removeCurrentDriver();
errorStream_ << "RtApiAsio::probeDeviceOpen: driver (" << driverName << ") does not support requested sample rate (" << sampleRate << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
// Get the current sample rate
ASIOSampleRate currentRate;
result = ASIOGetSampleRate( &currentRate );
if ( result != ASE_OK ) {
drivers.removeCurrentDriver();
errorStream_ << "RtApiAsio::probeDeviceOpen: driver (" << driverName << ") error getting sample rate.";
errorText_ = errorStream_.str();
return FAILURE;
}
// Set the sample rate only if necessary
if ( currentRate != sampleRate ) {
result = ASIOSetSampleRate( (ASIOSampleRate) sampleRate );
if ( result != ASE_OK ) {
drivers.removeCurrentDriver();
errorStream_ << "RtApiAsio::probeDeviceOpen: driver (" << driverName << ") error setting sample rate (" << sampleRate << ").";
errorText_ = errorStream_.str();
return FAILURE;
}
}
// Determine the driver data type.
ASIOChannelInfo channelInfo;
channelInfo.channel = 0;
if ( mode == OUTPUT ) channelInfo.isInput = false;
else channelInfo.isInput = true;
result = ASIOGetChannelInfo( &channelInfo );
if ( result != ASE_OK ) {
drivers.removeCurrentDriver();
errorStream_ << "RtApiAsio::probeDeviceOpen: driver (" << driverName << ") error (" << getAsioErrorString( result ) << ") getting data format.";
errorText_ = errorStream_.str();
return FAILURE;
}
// Assuming WINDOWS host is always little-endian.
stream_.doByteSwap[mode] = false;
stream_.userFormat = format;
stream_.deviceFormat[mode] = 0;
if ( channelInfo.type == ASIOSTInt16MSB || channelInfo.type == ASIOSTInt16LSB ) {
stream_.deviceFormat[mode] = RTAUDIO_SINT16;
if ( channelInfo.type == ASIOSTInt16MSB ) stream_.doByteSwap[mode] = true;
}
else if ( channelInfo.type == ASIOSTInt32MSB || channelInfo.type == ASIOSTInt32LSB ) {
stream_.deviceFormat[mode] = RTAUDIO_SINT32;
if ( channelInfo.type == ASIOSTInt32MSB ) stream_.doByteSwap[mode] = true;
}
else if ( channelInfo.type == ASIOSTFloat32MSB || channelInfo.type == ASIOSTFloat32LSB ) {
stream_.deviceFormat[mode] = RTAUDIO_FLOAT32;
if ( channelInfo.type == ASIOSTFloat32MSB ) stream_.doByteSwap[mode] = true;
}
else if ( channelInfo.type == ASIOSTFloat64MSB || channelInfo.type == ASIOSTFloat64LSB ) {
stream_.deviceFormat[mode] = RTAUDIO_FLOAT64;
if ( channelInfo.type == ASIOSTFloat64MSB ) stream_.doByteSwap[mode] = true;
}
if ( stream_.deviceFormat[mode] == 0 ) {
drivers.removeCurrentDriver();
errorStream_ << "RtApiAsio::probeDeviceOpen: driver (" << driverName << ") data format not supported by RtAudio.";
errorText_ = errorStream_.str();
return FAILURE;
}
// Set the buffer size. For a duplex stream, this will end up
// setting the buffer size based on the input constraints, which
// should be ok.
long minSize, maxSize, preferSize, granularity;
result = ASIOGetBufferSize( &minSize, &maxSize, &preferSize, &granularity );
if ( result != ASE_OK ) {
drivers.removeCurrentDriver();
errorStream_ << "RtApiAsio::probeDeviceOpen: driver (" << driverName << ") error (" << getAsioErrorString( result ) << ") getting buffer size.";
errorText_ = errorStream_.str();
return FAILURE;
}
if ( *bufferSize < (unsigned int) minSize ) *bufferSize = (unsigned int) minSize;
else if ( *bufferSize > (unsigned int) maxSize ) *bufferSize = (unsigned int) maxSize;
else if ( granularity == -1 ) {
// Make sure bufferSize is a power of two.
int log2_of_min_size = 0;
int log2_of_max_size = 0;
for ( unsigned int i = 0; i < sizeof(long) * 8; i++ ) {
if ( minSize & ((long)1 << i) ) log2_of_min_size = i;
if ( maxSize & ((long)1 << i) ) log2_of_max_size = i;
}
long min_delta = std::abs( (long)*bufferSize - ((long)1 << log2_of_min_size) );
int min_delta_num = log2_of_min_size;
for (int i = log2_of_min_size + 1; i <= log2_of_max_size; i++) {
long current_delta = std::abs( (long)*bufferSize - ((long)1 << i) );
if (current_delta < min_delta) {
min_delta = current_delta;
min_delta_num = i;
}
}
*bufferSize = ( (unsigned int)1 << min_delta_num );
if ( *bufferSize < (unsigned int) minSize ) *bufferSize = (unsigned int) minSize;
else if ( *bufferSize > (unsigned int) maxSize ) *bufferSize = (unsigned int) maxSize;
}
else if ( granularity != 0 ) {
// Set to an even multiple of granularity, rounding up.
*bufferSize = (*bufferSize + granularity-1) / granularity * granularity;
}
if ( mode == INPUT && stream_.mode == OUTPUT && stream_.bufferSize != *bufferSize ) {
drivers.removeCurrentDriver();
errorText_ = "RtApiAsio::probeDeviceOpen: input/output buffersize discrepancy!";
return FAILURE;
}
stream_.bufferSize = *bufferSize;
stream_.nBuffers = 2;
if ( options && options->flags & RTAUDIO_NONINTERLEAVED ) stream_.userInterleaved = false;
else stream_.userInterleaved = true;
// ASIO always uses non-interleaved buffers.
stream_.deviceInterleaved[mode] = false;
// Allocate, if necessary, our AsioHandle structure for the stream.
AsioHandle *handle = (AsioHandle *) stream_.apiHandle;
if ( handle == 0 ) {
try {
handle = new AsioHandle;
}
catch ( std::bad_alloc& ) {
//if ( handle == NULL ) {
drivers.removeCurrentDriver();
errorText_ = "RtApiAsio::probeDeviceOpen: error allocating AsioHandle memory.";
return FAILURE;
}
handle->bufferInfos = 0;
// Create a manual-reset event.
handle->condition = CreateEvent( NULL, // no security
TRUE, // manual-reset
FALSE, // non-signaled initially
NULL ); // unnamed
stream_.apiHandle = (void *) handle;
}
// Create the ASIO internal buffers. Since RtAudio sets up input
// and output separately, we'll have to dispose of previously
// created output buffers for a duplex stream.
long inputLatency, outputLatency;
if ( mode == INPUT && stream_.mode == OUTPUT ) {
ASIODisposeBuffers();
if ( handle->bufferInfos ) free( handle->bufferInfos );
}
// Allocate, initialize, and save the bufferInfos in our stream callbackInfo structure.
bool buffersAllocated = false;
unsigned int i, nChannels = stream_.nDeviceChannels[0] + stream_.nDeviceChannels[1];
handle->bufferInfos = (ASIOBufferInfo *) malloc( nChannels * sizeof(ASIOBufferInfo) );
if ( handle->bufferInfos == NULL ) {
errorStream_ << "RtApiAsio::probeDeviceOpen: error allocating bufferInfo memory for driver (" << driverName << ").";
errorText_ = errorStream_.str();
goto error;
}
ASIOBufferInfo *infos;
infos = handle->bufferInfos;
for ( i=0; i<stream_.nDeviceChannels[0]; i++, infos++ ) {
infos->isInput = ASIOFalse;
infos->channelNum = i + stream_.channelOffset[0];
infos->buffers[0] = infos->buffers[1] = 0;
}
for ( i=0; i<stream_.nDeviceChannels[1]; i++, infos++ ) {
infos->isInput = ASIOTrue;
infos->channelNum = i + stream_.channelOffset[1];
infos->buffers[0] = infos->buffers[1] = 0;
}
// Set up the ASIO callback structure and create the ASIO data buffers.
asioCallbacks.bufferSwitch = &bufferSwitch;
asioCallbacks.sampleRateDidChange = &sampleRateChanged;
asioCallbacks.asioMessage = &asioMessages;
asioCallbacks.bufferSwitchTimeInfo = NULL;
result = ASIOCreateBuffers( handle->bufferInfos, nChannels, stream_.bufferSize, &asioCallbacks );
if ( result != ASE_OK ) {
errorStream_ << "RtApiAsio::probeDeviceOpen: driver (" << driverName << ") error (" << getAsioErrorString( result ) << ") creating buffers.";
errorText_ = errorStream_.str();
goto error;
}
buffersAllocated = true;
// Set flags for buffer conversion.
stream_.doConvertBuffer[mode] = false;
if ( stream_.userFormat != stream_.deviceFormat[mode] )
stream_.doConvertBuffer[mode] = true;
if ( stream_.userInterleaved != stream_.deviceInterleaved[mode] &&
stream_.nUserChannels[mode] > 1 )
stream_.doConvertBuffer[mode] = true;
// Allocate necessary internal buffers
unsigned long bufferBytes;
bufferBytes = stream_.nUserChannels[mode] * *bufferSize * formatBytes( stream_.userFormat );
stream_.userBuffer[mode] = (char *) calloc( bufferBytes, 1 );
if ( stream_.userBuffer[mode] == NULL ) {
errorText_ = "RtApiAsio::probeDeviceOpen: error allocating user buffer memory.";
goto error;
}
if ( stream_.doConvertBuffer[mode] ) {
bool makeBuffer = true;
bufferBytes = stream_.nDeviceChannels[mode] * formatBytes( stream_.deviceFormat[mode] );
if ( mode == INPUT ) {
if ( stream_.mode == OUTPUT && stream_.deviceBuffer ) {
unsigned long bytesOut = stream_.nDeviceChannels[0] * formatBytes( stream_.deviceFormat[0] );
if ( bufferBytes <= bytesOut ) makeBuffer = false;
}
}
if ( makeBuffer ) {
bufferBytes *= *bufferSize;
if ( stream_.deviceBuffer ) free( stream_.deviceBuffer );
stream_.deviceBuffer = (char *) calloc( bufferBytes, 1 );
if ( stream_.deviceBuffer == NULL ) {
errorText_ = "RtApiAsio::probeDeviceOpen: error allocating device buffer memory.";
goto error;
}
}
}
stream_.sampleRate = sampleRate;
stream_.device[mode] = device;
stream_.state = STREAM_STOPPED;
asioCallbackInfo = &stream_.callbackInfo;
stream_.callbackInfo.object = (void *) this;
if ( stream_.mode == OUTPUT && mode == INPUT )
// We had already set up an output stream.
stream_.mode = DUPLEX;
else
stream_.mode = mode;
// Determine device latencies
result = ASIOGetLatencies( &inputLatency, &outputLatency );
if ( result != ASE_OK ) {
errorStream_ << "RtApiAsio::probeDeviceOpen: driver (" << driverName << ") error (" << getAsioErrorString( result ) << ") getting latency.";
errorText_ = errorStream_.str();
error( RtError::WARNING); // warn but don't fail
}
else {
stream_.latency[0] = outputLatency;
stream_.latency[1] = inputLatency;
}
// Setup the buffer conversion information structure. We don't use
// buffers to do channel offsets, so we override that parameter
// here.
if ( stream_.doConvertBuffer[mode] ) setConvertInfo( mode, 0 );
return SUCCESS;
error:
if ( buffersAllocated )
ASIODisposeBuffers();
drivers.removeCurrentDriver();
if ( handle ) {
CloseHandle( handle->condition );
if ( handle->bufferInfos )
free( handle->bufferInfos );
delete handle;
stream_.apiHandle = 0;
}
for ( int i=0; i<2; i++ ) {
if ( stream_.userBuffer[i] ) {
free( stream_.userBuffer[i] );
stream_.userBuffer[i] = 0;
}
}
if ( stream_.deviceBuffer ) {
free( stream_.deviceBuffer );
stream_.deviceBuffer = 0;
}
return FAILURE;
}
void RtApiAsio :: closeStream()
{
if ( stream_.state == STREAM_CLOSED ) {
errorText_ = "RtApiAsio::closeStream(): no open stream to close!";
error( RtError::WARNING );
return;
}
if ( stream_.state == STREAM_RUNNING ) {
stream_.state = STREAM_STOPPED;
ASIOStop();
}
ASIODisposeBuffers();
drivers.removeCurrentDriver();
AsioHandle *handle = (AsioHandle *) stream_.apiHandle;
if ( handle ) {
CloseHandle( handle->condition );
if ( handle->bufferInfos )
free( handle->bufferInfos );
delete handle;
stream_.apiHandle = 0;
}
for ( int i=0; i<2; i++ ) {
if ( stream_.userBuffer[i] ) {
free( stream_.userBuffer[i] );
stream_.userBuffer[i] = 0;
}
}
if ( stream_.deviceBuffer ) {
free( stream_.deviceBuffer );
stream_.deviceBuffer = 0;
}
stream_.mode = UNINITIALIZED;
stream_.state = STREAM_CLOSED;
}
void RtApiAsio :: startStream()
{
verifyStream();
if ( stream_.state == STREAM_RUNNING ) {
errorText_ = "RtApiAsio::startStream(): the stream is already running!";
error( RtError::WARNING );
return;
}
MUTEX_LOCK( &stream_.mutex );
AsioHandle *handle = (AsioHandle *) stream_.apiHandle;
ASIOError result = ASIOStart();
if ( result != ASE_OK ) {
errorStream_ << "RtApiAsio::startStream: error (" << getAsioErrorString( result ) << ") starting device.";
errorText_ = errorStream_.str();
goto unlock;
}
handle->drainCounter = 0;
handle->internalDrain = false;
stream_.state = STREAM_RUNNING;
asioXRun = false;
unlock:
MUTEX_UNLOCK( &stream_.mutex );
if ( result == ASE_OK ) return;
error( RtError::SYSTEM_ERROR );
}
void RtApiAsio :: stopStream()
{
verifyStream();
if ( stream_.state == STREAM_STOPPED ) {
errorText_ = "RtApiAsio::stopStream(): the stream is already stopped!";
error( RtError::WARNING );
return;
}
MUTEX_LOCK( &stream_.mutex );
if ( stream_.state == STREAM_STOPPED ) {
MUTEX_UNLOCK( &stream_.mutex );
return;
}
AsioHandle *handle = (AsioHandle *) stream_.apiHandle;
if ( stream_.mode == OUTPUT || stream_.mode == DUPLEX ) {
if ( handle->drainCounter == 0 ) {
handle->drainCounter = 1;
MUTEX_UNLOCK( &stream_.mutex );
WaitForMultipleObjects( 1, &handle->condition, FALSE, INFINITE ); // block until signaled
ResetEvent( handle->condition );
MUTEX_LOCK( &stream_.mutex );
}
}
ASIOError result = ASIOStop();
if ( result != ASE_OK ) {
errorStream_ << "RtApiAsio::stopStream: error (" << getAsioErrorString( result ) << ") stopping device.";
errorText_ = errorStream_.str();
}
stream_.state = STREAM_STOPPED;
MUTEX_UNLOCK( &stream_.mutex );
if ( result == ASE_OK ) return;
error( RtError::SYSTEM_ERROR );
}
void RtApiAsio :: abortStream()
{
verifyStream();
if ( stream_.state == STREAM_STOPPED ) {
errorText_ = "RtApiAsio::abortStream(): the stream is already stopped!";
error( RtError::WARNING );
return;
}
// The following lines were commented-out because some behavior was
// noted where the device buffers need to be zeroed to avoid
// continuing sound, even when the device buffers are completely
// disposed. So now, calling abort is the same as calling stop.
// AsioHandle *handle = (AsioHandle *) stream_.apiHandle;
// handle->drainCounter = 1;
stopStream();
}
bool RtApiAsio :: callbackEvent( long bufferIndex )
{
if ( stream_.state == STREAM_STOPPED ) return SUCCESS;
if ( stream_.state == STREAM_CLOSED ) {
errorText_ = "RtApiAsio::callbackEvent(): the stream is closed ... this shouldn't happen!";
error( RtError::WARNING );
return FAILURE;
}
CallbackInfo *info = (CallbackInfo *) &stream_.callbackInfo;
AsioHandle *handle = (AsioHandle *) stream_.apiHandle;
// Check if we were draining the stream and signal is finished.
if ( handle->drainCounter > 3 ) {
if ( handle->internalDrain == false )
SetEvent( handle->condition );
else
stopStream();
return SUCCESS;
}
MUTEX_LOCK( &stream_.mutex );
// The state might change while waiting on a mutex.
if ( stream_.state == STREAM_STOPPED ) goto unlock;
// Invoke user callback to get fresh output data UNLESS we are
// draining stream.
if ( handle->drainCounter == 0 ) {
RtAudioCallback callback = (RtAudioCallback) info->callback;
double streamTime = getStreamTime();
RtAudioStreamStatus status = 0;
if ( stream_.mode != INPUT && asioXRun == true ) {
status |= RTAUDIO_OUTPUT_UNDERFLOW;
asioXRun = false;
}
if ( stream_.mode != OUTPUT && asioXRun == true ) {
status |= RTAUDIO_INPUT_OVERFLOW;
asioXRun = false;
}
handle->drainCounter = callback( stream_.userBuffer[0], stream_.userBuffer[1],
stream_.bufferSize, streamTime, status, info->userData );
if ( handle->drainCounter == 2 ) {
MUTEX_UNLOCK( &stream_.mutex );
abortStream();
return SUCCESS;
}
else if ( handle->drainCounter == 1 )
handle->internalDrain = true;
}
unsigned int nChannels, bufferBytes, i, j;
nChannels = stream_.nDeviceChannels[0] + stream_.nDeviceChannels[1];
if ( stream_.mode == OUTPUT || stream_.mode == DUPLEX ) {
bufferBytes = stream_.bufferSize * formatBytes( stream_.deviceFormat[0] );
if ( handle->drainCounter > 1 ) { // write zeros to the output stream
for ( i=0, j=0; i<nChannels; i++ ) {
if ( handle->bufferInfos[i].isInput != ASIOTrue )
memset( handle->bufferInfos[i].buffers[bufferIndex], 0, bufferBytes );
}
}
else if ( stream_.doConvertBuffer[0] ) {
convertBuffer( stream_.deviceBuffer, stream_.userBuffer[0], stream_.convertInfo[0] );
if ( stream_.doByteSwap[0] )
byteSwapBuffer( stream_.deviceBuffer,
stream_.bufferSize * stream_.nDeviceChannels[0],
stream_.deviceFormat[0] );
for ( i=0, j=0; i<nChannels; i++ ) {
if ( handle->bufferInfos[i].isInput != ASIOTrue )
memcpy( handle->bufferInfos[i].buffers[bufferIndex],
&stream_.deviceBuffer[j++*bufferBytes], bufferBytes );
}
}
else {
if ( stream_.doByteSwap[0] )
byteSwapBuffer( stream_.userBuffer[0],
stream_.bufferSize * stream_.nUserChannels[0],
stream_.userFormat );
for ( i=0, j=0; i<nChannels; i++ ) {
if ( handle->bufferInfos[i].isInput != ASIOTrue )
memcpy( handle->bufferInfos[i].buffers[bufferIndex],
&stream_.userBuffer[0][bufferBytes*j++], bufferBytes );
}
}
if ( handle->drainCounter ) {
handle->drainCounter++;
goto unlock;
}
}
if ( stream_.mode == INPUT || stream_.mode == DUPLEX ) {
bufferBytes = stream_.bufferSize * formatBytes(stream_.deviceFormat[1]);
if (stream_.doConvertBuffer[1]) {
// Always interleave ASIO input data.
for ( i=0, j=0; i<nChannels; i++ ) {
if ( handle->bufferInfos[i].isInput == ASIOTrue )
memcpy( &stream_.deviceBuffer[j++*bufferBytes],
handle->bufferInfos[i].buffers[bufferIndex],
bufferBytes );
}
if ( stream_.doByteSwap[1] )
byteSwapBuffer( stream_.deviceBuffer,
stream_.bufferSize * stream_.nDeviceChannels[1],
stream_.deviceFormat[1] );
convertBuffer( stream_.userBuffer[1], stream_.deviceBuffer, stream_.convertInfo[1] );
}
else {
for ( i=0, j=0; i<nChannels; i++ ) {
if ( handle->bufferInfos[i].isInput == ASIOTrue ) {
memcpy( &stream_.userBuffer[1][bufferBytes*j++],
handle->bufferInfos[i].buffers[bufferIndex],
bufferBytes );
}
}
if ( stream_.doByteSwap[1] )
byteSwapBuffer( stream_.userBuffer[1],
stream_.bufferSize * stream_.nUserChannels[1],
stream_.userFormat );
}
}
unlock:
// The following call was suggested by Malte Clasen. While the API
// documentation indicates it should not be required, some device
// drivers apparently do not function correctly without it.
ASIOOutputReady();
MUTEX_UNLOCK( &stream_.mutex );
RtApi::tickStreamTime();
return SUCCESS;
}
void sampleRateChanged( ASIOSampleRate sRate )
{
// The ASIO documentation says that this usually only happens during
// external sync. Audio processing is not stopped by the driver,
// actual sample rate might not have even changed, maybe only the
// sample rate status of an AES/EBU or S/PDIF digital input at the
// audio device.
RtApi *object = (RtApi *) asioCallbackInfo->object;
try {
object->stopStream();
}
catch ( RtError &exception ) {
std::cerr << "\nRtApiAsio: sampleRateChanged() error (" << exception.getMessage() << ")!\n" << std::endl;
return;
}
std::cerr << "\nRtApiAsio: driver reports sample rate changed to " << sRate << " ... stream stopped!!!\n" << std::endl;
}
long asioMessages( long selector, long value, void* message, double* opt )
{
long ret = 0;
switch( selector ) {
case kAsioSelectorSupported:
if ( value == kAsioResetRequest
|| value == kAsioEngineVersion
|| value == kAsioResyncRequest
|| value == kAsioLatenciesChanged
// The following three were added for ASIO 2.0, you don't
// necessarily have to support them.
|| value == kAsioSupportsTimeInfo
|| value == kAsioSupportsTimeCode
|| value == kAsioSupportsInputMonitor)
ret = 1L;
break;
case kAsioResetRequest:
// Defer the task and perform the reset of the driver during the
// next "safe" situation. You cannot reset the driver right now,
// as this code is called from the driver. Reset the driver is
// done by completely destruct is. I.e. ASIOStop(),
// ASIODisposeBuffers(), Destruction Afterwards you initialize the
// driver again.
std::cerr << "\nRtApiAsio: driver reset requested!!!" << std::endl;
ret = 1L;
break;
case kAsioResyncRequest:
// This informs the application that the driver encountered some
// non-fatal data loss. It is used for synchronization purposes
// of different media. Added mainly to work around the Win16Mutex
// problems in Windows 95/98 with the Windows Multimedia system,
// which could lose data because the Mutex was held too long by
// another thread. However a driver can issue it in other
// situations, too.
// std::cerr << "\nRtApiAsio: driver resync requested!!!" << std::endl;
asioXRun = true;
ret = 1L;
break;
case kAsioLatenciesChanged:
// This will inform the host application that the drivers were
// latencies changed. Beware, it this does not mean that the
// buffer sizes have changed! You might need to update internal
// delay data.
std::cerr << "\nRtApiAsio: driver latency may have changed!!!" << std::endl;
ret = 1L;
break;
case kAsioEngineVersion:
// Return the supported ASIO version of the host application. If
// a host application does not implement this selector, ASIO 1.0
// is assumed by the driver.
ret = 2L;
break;
case kAsioSupportsTimeInfo:
// Informs the driver whether the
// asioCallbacks.bufferSwitchTimeInfo() callback is supported.
// For compatibility with ASIO 1.0 drivers the host application
// should always support the "old" bufferSwitch method, too.
ret = 0;
break;
case kAsioSupportsTimeCode:
// Informs the driver whether application is interested in time
// code info. If an application does not need to know about time
// code, the driver has less work to do.
ret = 0;
break;
}
return ret;
}
static const char* getAsioErrorString( ASIOError result )
{
struct Messages
{
ASIOError value;
const char*message;
};
static Messages m[] =
{
{ ASE_NotPresent, "Hardware input or output is not present or available." },
{ ASE_HWMalfunction, "Hardware is malfunctioning." },
{ ASE_InvalidParameter, "Invalid input parameter." },
{ ASE_InvalidMode, "Invalid mode." },
{ ASE_SPNotAdvancing, "Sample position not advancing." },
{ ASE_NoClock, "Sample clock or rate cannot be determined or is not present." },
{ ASE_NoMemory, "Not enough memory to complete the request." }
};
for ( unsigned int i = 0; i < sizeof(m)/sizeof(m[0]); ++i )
if ( m[i].value == result ) return m[i].message;
return "Unknown error.";
}
//******************** End of __WINDOWS_ASIO__ *********************//
#endif
#if defined(__WINDOWS_DS__) // Windows DirectSound API
// Modified by Robin Davies, October 2005
// - Improvements to DirectX pointer chasing.
// - Bug fix for non-power-of-two Asio granularity used by Edirol PCR-A30.
// - Auto-call CoInitialize for DSOUND and ASIO platforms.
// Various revisions for RtAudio 4.0 by Gary Scavone, April 2007
// Changed device query structure for RtAudio 4.0.7, January 2010
#include <dsound.h>
#include <assert.h>
#include <algorithm>
#if defined(__MINGW32__)
// missing from latest mingw winapi
#define WAVE_FORMAT_96M08 0x00010000 /* 96 kHz, Mono, 8-bit */
#define WAVE_FORMAT_96S08 0x00020000 /* 96 kHz, Stereo, 8-bit */
#define WAVE_FORMAT_96M16 0x00040000 /* 96 kHz, Mono, 16-bit */
#define WAVE_FORMAT_96S16 0x00080000 /* 96 kHz, Stereo, 16-bit */
#endif
#define MINIMUM_DEVICE_BUFFER_SIZE 32768
#ifdef _MSC_VER // if Microsoft Visual C++
#pragma comment( lib, "winmm.lib" ) // then, auto-link winmm.lib. Otherwise, it has to be added manually.
#endif
static inline DWORD dsPointerBetween( DWORD pointer, DWORD laterPointer, DWORD earlierPointer, DWORD bufferSize )
{
if ( pointer > bufferSize ) pointer -= bufferSize;
if ( laterPointer < earlierPointer ) laterPointer += bufferSize;
if ( pointer < earlierPointer ) pointer += bufferSize;
return pointer >= earlierPointer && pointer < laterPointer;
}
// A structure to hold various information related to the DirectSound
// API implementation.
struct DsHandle {
unsigned int drainCounter; // Tracks callback counts when draining
bool internalDrain; // Indicates if stop is initiated from callback or not.
void *id[2];
void *buffer[2];
bool xrun[2];
UINT bufferPointer[2];
DWORD dsBufferSize[2];
DWORD dsPointerLeadTime[2]; // the number of bytes ahead of the safe pointer to lead by.
HANDLE condition;
DsHandle()
:drainCounter(0), internalDrain(false) { id[0] = 0; id[1] = 0; buffer[0] = 0; buffer[1] = 0; xrun[0] = false; xrun[1] = false; bufferPointer[0] = 0; bufferPointer[1] = 0; }
};
// 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( int code );
extern "C" unsigned __stdcall callbackHandler( void *ptr );
struct DsDevice {
LPGUID id[2];
bool validId[2];
bool found;
std::string name;
DsDevice()
: found(false) { validId[0] = false; validId[1] = false; }
};
std::vector< DsDevice > dsDevices;
RtApiDs :: RtApiDs()
{
// Dsound will run both-threaded. If CoInitialize fails, then just
// accept whatever the mainline chose for a threading model.
coInitialized_ = false;
HRESULT hr = CoInitialize( NULL );
if ( !FAILED( hr ) ) coInitialized_ = true;
}
RtApiDs :: ~RtApiDs()
{
if ( coInitialized_ ) CoUninitialize(); // balanced call.
if ( stream_.state != STREAM_CLOSED ) closeStream();
}
// The DirectSound default output is always the first device.
unsigned int RtApiDs :: getDefaultOutputDevice( void )
{
return 0;
}
// The DirectSound default input is always the first input device,
// which is the first capture device enumerated.
unsigned int RtApiDs :: getDefaultInputDevice( void )
{
return 0;
}
unsigned int RtApiDs :: getDeviceCount( void )
{
// Set query flag for previously found devices to false, so that we
// can check for any devices that have disappeared.
for ( unsigned int i=0; i<dsDevices.size(); i++ )
dsDevices[i].found = false;
// Query DirectSound devices.
bool isInput = false;
HRESULT result = DirectSoundEnumerate( (LPDSENUMCALLBACK) deviceQueryCallback, &isInput );
if ( FAILED( result ) ) {
errorStream_ << "RtApiDs::getDeviceCount: error (" << getErrorString( result ) << ") enumerating output devices!";
errorText_ = errorStream_.str();
error( RtError::WARNING );
}
// Query DirectSoundCapture devices.
isInput = true;
result = DirectSoundCaptureEnumerate( (LPDSENUMCALLBACK) deviceQueryCallback, &isInput );
if ( FAILED( result ) ) {
errorStream_ << "RtApiDs::getDeviceCount: error (" << getErrorString( result ) << ") enumerating input devices!";
errorText_ = errorStream_.str();
error( RtError::WARNING );
}
// Clean out any devices that may have disappeared.
std::vector< DsDevice > :: iterator it;
for ( it=dsDevices.begin(); it < dsDevices.end(); it++ )
if ( it->found == false ) dsDevices.erase( it );
return dsDevices.size();
}
RtAudio::DeviceInfo RtApiDs :: getDeviceInfo( unsigned int device )
{
RtAudio::DeviceInfo info;
info.probed = false;
if ( dsDevices.size() == 0 ) {
// Force a query of all devices
getDeviceCount();
if ( dsDevices.size() == 0 ) {
errorText_ = "RtApiDs::getDeviceInfo: no devices found!";
error( RtError::INVALID_USE );
}
}
if ( device >= dsDevices.size() ) {
errorText_ = "RtApiDs::getDeviceInfo: device ID is invalid!";
error( RtError::INVALID_USE );
}
HRESULT result;
if ( dsDevices[ device ].validId[0] == false ) goto probeInput;
LPDIRECTSOUND output;
DSCAPS outCaps;
result = DirectSoundCreate( dsDevices[ device ].id[0], &output, NULL );
if ( FAILED( result ) ) {
errorStream_ << "RtApiDs::getDeviceInfo: error (" << getErrorString( result ) << ") opening output device (" << dsDevices[ device ].name << ")!";
errorText_ = errorStream_.str();
error( RtError::WARNING );
goto probeInput;
}
outCaps.dwSize = sizeof( outCaps );
result = output->GetCaps( &outCaps );
if ( FAILED( result ) ) {
output->Release();
errorStream_ << "RtApiDs::getDeviceInfo: error (" << getErrorString( result ) << ") getting capabilities!";
errorText_ = errorStream_.str();
error( RtError::WARNING );
goto probeInput;
}
// Get output channel information.
info.outputChannels = ( outCaps.dwFlags & DSCAPS_PRIMARYSTEREO ) ? 2 : 1;
// Get sample rate information.
info.sampleRates.clear();
for ( unsigned int k=0; k<MAX_SAMPLE_RATES; k++ ) {
if ( SAMPLE_RATES[k] >= (unsigned int) outCaps.dwMinSecondarySampleRate &&
SAMPLE_RATES[k] <= (unsigned int) outCaps.dwMaxSecondarySampleRate )
info.sampleRates.push_back( SAMPLE_RATES[k] );
}
// Get format information.
if ( outCaps.dwFlags & DSCAPS_PRIMARY16BIT ) info.nativeFormats |= RTAUDIO_SINT16;
if ( outCaps.dwFlags & DSCAPS_PRIMARY8BIT ) info.nativeFormats |= RTAUDIO_SINT8;
output->Release();
if ( getDefaultOutputDevice() == device )
info.isDefaultOutput = true;
if ( dsDevices[ device ].validId[1] == false ) {
info.name = dsDevices[ device ].name;
info.probed = true;
return info;
}
probeInput:
LPDIRECTSOUNDCAPTURE input;
result = DirectSoundCaptureCreate( dsDevices[ device ].id[1], &input, NULL );
if ( FAILED( result ) ) {
errorStream_ << "RtApiDs::getDeviceInfo: error (" << getErrorString( result ) << ") opening input device (" << dsDevices[ device ].name << ")!";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
DSCCAPS inCaps;
inCaps.dwSize = sizeof( inCaps );
result = input->GetCaps( &inCaps );
if ( FAILED( result ) ) {
input->Release();
errorStream_ << "RtApiDs::getDeviceInfo: error (" << getErrorString( result ) << ") getting object capabilities (" << dsDevices[ device ].name << ")!";
errorText_ = errorStream_.str();
error( RtError::WARNING );
return info;
}
// Get input channel information.
info.inputChannels = inCaps.dwChannels;
// Get sample rate and format information.
std::vector<unsigned int> rates;
if ( inCaps.dwChannels == 2 ) {
if ( inCaps.dwFormats & WAVE_FORMAT_1S16 ) info.nativeFormats |= RTAUDIO_SINT16;
if ( inCaps.dwFormats & WAVE_FORMAT_2S16 ) info.nativeFormats |= RTAUDIO_SINT16;
if ( inCaps.dwFormats & WAVE_FORMAT_4S16 ) info.nativeFormats |= RTAUDIO_SINT16;
if ( inCaps.dwFormats & WAVE_FORMAT_96S16 ) info.nativeFormats |= RTAUDIO_SINT16;
if ( inCaps.dwFormats & WAVE_FORMAT_1S08 ) info.nativeFormats |= RTAUDIO_SINT8;
if ( inCaps.dwFormats & WAVE_FORMAT_2S08 ) info.nativeFormats |= RTAUDIO_SINT8;
if ( inCaps.dwFormats & WAVE_FORMAT_4S08 ) info.nativeFormats |= RTAUDIO_SINT8;
if ( inCaps.dwFormats & WAVE_FORMAT_96S08 ) info.nativeFormats |= RTAUDIO_SINT8;
if ( info.nativeFormats & RTAUDIO_SINT16 ) {
if ( inCaps.dwFormats & WAVE_FORMAT_1S16 ) rates.push_back( 11025 );
if ( inCaps.dwFormats & WAVE_FORMAT_2S16 ) rates.push_back( 22050 );
if ( inCaps.dwFormats & WAVE_FORMAT_4S16 ) rates.push_back( 44100 );
if ( inCaps.dwFormats & WAVE_FORMAT_96S16 ) rates.push_back( 96000 );
}
else if ( info.nativeFormats & RTAUDIO_SINT8 ) {
if ( inCaps.dwFormats & WAVE_FORMAT_1S08 ) rates.push_back( 11025 );
if ( inCaps.dwFormats & WAVE_FORMAT_2S08 ) rates.push_back( 22050 );
if ( inCaps.dwFormats & WAVE_FORMAT_4S08 ) rates.push_back( 44100 );
if ( inCaps.dwFormats & WAVE_FORMAT_96S08 ) rates.push_back( 96000 );
}
}
else if ( inCaps.dwChannels == 1 ) {
if ( inCaps.dwFormats & WAVE_FORMAT_1M16 ) info.nativeFormats |= RTAUDIO_SINT16;
if ( inCaps.dwFormats & WAVE_FORMAT_2M16 ) info.nativeFormats |= RTAUDIO_SINT16;
if ( inCaps.dwFormats & WAVE_FORMAT_4M16 ) info.nativeFormats |= RTAUDIO_SINT16;
if ( inCaps.dwFormats & WAVE_FORMAT_96M16 ) info.nativeFormats |= RTAUDIO_SINT16;
if ( inCaps.dwFormats & WAVE_FORMAT_1M08 ) info.nativeFormats |= RTAUDIO_SINT8;
if ( inCaps.dwFormats & WAVE_FORMAT_2M08 ) info.nativeFormats |= RTAUDIO_SINT8;
if ( inCaps.dwFormats & WAVE_FORMAT_4M08 ) info.nativeFormats |= RTAUDIO_SINT8;
if ( inCaps.dwFormats & WAVE_FORMAT_96M08 ) info.nativeFormats |= RTAUDIO_SINT8;
if ( info.nativeFormats & RTAUDIO_SINT16 ) {
if ( inCaps.dwFormats & WAVE_FORMAT_1M16 ) rates.push_back( 11025 );
if ( inCaps.dwFormats & WAVE_FORMAT_2M16 ) rates.push_back( 22050 );
if ( inCaps.dwFormats & WAVE_FORMAT_4M16 ) rates.push_back( 44100 );
if ( inCaps.dwFormats & WAVE_FORMAT_96M16 ) rates.push_back( 96000 );
}
else if ( info.nativeFormats & RTAUDIO_SINT8 ) {
if ( inCaps.dwFormats & WAVE_FORMAT_1M08 ) rates.push_back( 11025 );
if ( inCaps.dwFormats & WAVE_FORMAT_2M08 ) rates.push_back( 22050 );
if ( inCaps.dwFormats & WAVE_FORMAT_4M08 ) rates.push_back( 44100 );
if ( inCaps.dwFormats & WAVE_FORMAT_96M08 ) rates.push_back( 96000 );
}
}
else info.inputChannels = 0; // technically, this would be an error
input->Release();
if ( info.inputChannels == 0 ) return info;
// Copy the supported rates to the info structure but avoid duplication.
bool found;
for ( unsigned int i=0; i<rates.size(); i++ ) {
found = false;
for ( unsigned int j=0; j<info.sampleRates.size(); j++ ) {
if ( rates[i] == info.sampleRates[j] ) {
found = true;
break;
}
}
if ( found == false ) info.sampleRates.push_back( rates[i] );
}
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 = dsDevices[ device ].name;
info.probed = true;
return info;
}
bool RtApiDs :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,
unsigned int firstChannel, unsigned int sampleRate,
RtAudioFormat format, unsigned int *bufferSize,
RtAudio::StreamOptions *options )
{
if ( channels + firstChannel > 2 ) {
errorText_ = "RtApiDs::probeDeviceOpen: DirectSound does not support more than 2 channels per device.";
return FAILURE;
}
unsigned int nDevices = dsDevices.size();
if ( nDevices == 0 ) {
// This should not happen because a check is made before this function is called.
errorText_ = "RtApiDs::probeDeviceOpen: no devices found!";
return FAILURE;
}
if ( device >= nDevices ) {
// This should not happen because a check is made before this function is called.
errorText_ = "RtApiDs::probeDeviceOpen: device ID is invalid!";
return FAILURE;
}
if ( mode == OUTPUT ) {
if ( dsDevices[ device ].validId[0] == false ) {
errorStream_ << "RtApiDs::probeDeviceOpen: device (" << device << ") does not support output!";
errorText_ = errorStream_.str();
return FAILURE;
}
}
else { // mode == INPUT
if ( dsDevices[ device ].validId[1] == false ) {
errorStream_ << "RtApiDs::probeDeviceOpen: device (" << device << ") does not support input!";
errorText_ = errorStream_.str();
return FAILURE;
}
}
// 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
// window. Also, if the application window closes before the
// DirectSound buffer, DirectSound can crash. In the past, I had
// problems when using GetDesktopWindow() but it seems fine now
// (January 2010). I'll leave it commented here.
// HWND hWnd = GetForegroundWindow();
HWND hWnd = GetDesktopWindow();
// Check the numberOfBuffers parameter and limit the lowest value to
// two. This is a judgement call and a value of two is probably too
// low for capture, but it should work for playback.
int nBuffers = 0;
if ( options ) nBuffers = options->numberOfBuffers;
if ( options && options->flags & RTAUDIO_MINIMIZE_LATENCY ) nBuffers = 2;
if ( nBuffers < 2 ) nBuffers = 3;
// Check the lower range of the user-specified buffer size and set
// (arbitrarily) to a lower bound of 32.
if ( *bufferSize < 32 ) *bufferSize = 32;
// Create the wave format structure. The data format setting will
// be determined later.
WAVEFORMATEX waveFormat;
ZeroMemory( &waveFormat, sizeof(WAVEFORMATEX) );
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.nChannels = channels + firstChannel;
waveFormat.nSamplesPerSec = (unsigned long) sampleRate;
// Determine the device buffer size. By default, we'll use the value
// defined above (32K), but we will grow it to make allowances for
// very large software buffer sizes.
DWORD dsBufferSize = MINIMUM_DEVICE_BUFFER_SIZE;;
DWORD dsPointerLeadTime = 0;
void *ohandle = 0, *bhandle = 0;
HRESULT result;
if ( mode == OUTPUT ) {
LPDIRECTSOUND output;
result = DirectSoundCreate( dsDevices[ device ].id[0], &output, NULL );
if ( FAILED( result ) ) {
errorStream_ << "RtApiDs::probeDeviceOpen: error (" << getErrorString( result ) << ") opening output device (" << dsDevices[ device ].name << ")!";
errorText_ = errorStream_.str();
return FAILURE;
}
DSCAPS outCaps;
outCaps.dwSize = sizeof( outCaps );
result = output->GetCaps( &outCaps );
if ( FAILED( result ) ) {
output->Release();
errorStream_ << "RtApiDs::probeDeviceOpen: error (" << getErrorString( result ) << ") getting capabilities (" << dsDevices[ device ].name << ")!";
errorText_ = errorStream_.str();
return FAILURE;
}
// Check channel information.
if ( channels + firstChannel == 2 && !( outCaps.dwFlags & DSCAPS_PRIMARYSTEREO ) ) {
errorStream_ << "RtApiDs::getDeviceInfo: the output device (" << dsDevices[ device ].name << ") does not support stereo playback.";
errorText_ = errorStream_.str();
return FAILURE;
}
// Check format information. Use 16-bit format unless not
// supported or user requests 8-bit.
if ( outCaps.dwFlags & DSCAPS_PRIMARY16BIT &&
!( format == RTAUDIO_SINT8 && outCaps.dwFlags & DSCAPS_PRIMARY8BIT ) ) {
waveFormat.wBitsPerSample = 16;
stream_.deviceFormat[mode] = RTAUDIO_SINT16;
}
else {
waveFormat.wBitsPerSample = 8;
stream_.deviceFormat[mode] = RTAUDIO_SINT8;
}
stream_.userFormat = format;
// Update wave format structure and buffer information.
waveFormat.nBlockAlign = waveFormat.nChannels * waveFormat.wBitsPerSample / 8;
waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
dsPointerLeadTime = nBuffers * (*bufferSize) * (waveFormat.wBitsPerSample / 8) * channels;
// If the user wants an even bigger buffer, increase the device buffer size accordingly.
while ( dsPointerLeadTime * 2U > dsBufferSize )
dsBufferSize *= 2;
// Set cooperative level to DSSCL_EXCLUSIVE ... sound stops when window focus changes.
// result = output->SetCooperativeLevel( hWnd, DSSCL_EXCLUSIVE );
// Set cooperative level to DSSCL_PRIORITY ... sound remains when window focus changes.
result = output->SetCooperativeLevel( hWnd, DSSCL_PRIORITY );
if ( FAILED( result ) ) {
output->Release();
errorStream_ << "RtApiDs::probeDeviceOpen: error (" << getErrorString( result ) << ") setting cooperative level (" << dsDevices[ device ].name << ")!";
errorText_ = errorStream_.str();
return FAILURE;
}
// Even though we will write to the secondary buffer, we need to
// access the primary buffer to set the correct output format
// (since the default is 8-bit, 22 kHz!). Setup the DS primary
// buffer description.
DSBUFFERDESC bufferDescription;
ZeroMemory( &bufferDescription, sizeof( DSBUFFERDESC ) );
bufferDescription.dwSize = sizeof( DSBUFFERDESC );
bufferDescription.dwFlags = DSBCAPS_PRIMARYBUFFER;
// Obtain the primary buffer
LPDIRECTSOUNDBUFFER buffer;
result = output->CreateSoundBuffer( &bufferDescription, &buffer, NULL );
if ( FAILED( result ) ) {
output->Release();
errorStream_ << "RtApiDs::probeDeviceOpen: error (" << getErrorString( result ) << ") accessing primary buffer (" << dsDevices[ device ].name << ")!";
errorText_ = errorStream_.str();
return FAILURE;
}
// Set the primary DS buffer sound format.
result = buffer->SetFormat( &waveFormat );