diff --git a/RtAudio.cpp b/RtAudio.cpp index 767908d..1f6a597 100644 --- a/RtAudio.cpp +++ b/RtAudio.cpp @@ -9,8 +9,8 @@ RtAudio WWW site: http://music.mcgill.ca/~gary/rtaudio/ - RtAudio: a realtime audio i/o C++ class - Copyright (c) 2001-2004 Gary P. Scavone + RtAudio: realtime audio i/o C++ classes + Copyright (c) 2001-2005 Gary P. Scavone Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files @@ -37,10 +37,17 @@ */ /************************************************************************/ -// RtAudio: Version 3.0.1, 22 March 2004 +// RtAudio: Version 3.0.2 (14 October 2005) + +// Modified by Robin Davies, 1 October 2005 +// - Improvements to DirectX pointer chasing. +// - Backdoor RtDsStatistics hook provides DirectX performance information. +// - Bug fix for non-power-of-two Asio granularity used by Edirol PCR-A30. +// - Auto-call CoInitialize for DSOUND and ASIO platforms. #include "RtAudio.h" #include +#include // Static variable definitions. const unsigned int RtApi::MAX_SAMPLE_RATES = 14; @@ -92,6 +99,26 @@ RtAudio :: RtAudio( int outputDevice, int outputChannels, } } +RtAudio :: RtAudio( int outputDevice, int outputChannels, + int inputDevice, int inputChannels, + RtAudioFormat format, int sampleRate, + int *bufferSize, int *numberOfBuffers, RtAudioApi api ) +{ + initialize( api ); + + try { + rtapi_->openStream( outputDevice, outputChannels, + inputDevice, inputChannels, + format, sampleRate, + bufferSize, numberOfBuffers ); + } + catch (RtError &exception) { + // Deallocate the RtApi instance. + delete rtapi_; + throw exception; + } +} + RtAudio :: ~RtAudio() { delete rtapi_; @@ -107,6 +134,16 @@ void RtAudio :: openStream( int outputDevice, int outputChannels, bufferSize, numberOfBuffers ); } +void RtAudio :: openStream( int outputDevice, int outputChannels, + int inputDevice, int inputChannels, + RtAudioFormat format, int sampleRate, + int *bufferSize, int *numberOfBuffers ) +{ + rtapi_->openStream( outputDevice, outputChannels, inputDevice, + inputChannels, format, sampleRate, + bufferSize, *numberOfBuffers ); +} + void RtAudio::initialize( RtAudioApi api ) { rtapi_ = 0; @@ -216,6 +253,7 @@ void RtAudio::initialize( RtAudioApi api ) RtApi :: RtApi() { stream_.mode = UNINITIALIZED; + stream_.state = STREAM_STOPPED; stream_.apiHandle = 0; MUTEX_INITIALIZE(&stream_.mutex); } @@ -225,6 +263,17 @@ RtApi :: ~RtApi() MUTEX_DESTROY(&stream_.mutex); } +void RtApi :: openStream( int outputDevice, int outputChannels, + int inputDevice, int inputChannels, + RtAudioFormat format, int sampleRate, + int *bufferSize, int *numberOfBuffers ) +{ + this->openStream( outputDevice, outputChannels, inputDevice, + inputChannels, format, sampleRate, + bufferSize, *numberOfBuffers ); + *numberOfBuffers = stream_.nBuffers; +} + void RtApi :: openStream( int outputDevice, int outputChannels, int inputDevice, int inputChannels, RtAudioFormat format, int sampleRate, @@ -259,6 +308,7 @@ void RtApi :: openStream( int outputDevice, int outputChannels, } } + std::string errorMessages; clearStreamInfo(); bool result = FAILURE; int device, defaultDevice = 0; @@ -281,7 +331,7 @@ void RtApi :: openStream( int outputDevice, int outputChannels, if ( i == defaultDevice ) continue; device = i; } - if (devices_[device].probed == false) { + if ( devices_[device].probed == false ) { // If the device wasn't successfully probed before, try it // (again) now. clearDeviceInfo(&devices_[device]); @@ -291,6 +341,9 @@ void RtApi :: openStream( int outputDevice, int outputChannels, result = probeDeviceOpen(device, mode, channels, sampleRate, format, bufferSize, numberOfBuffers); if ( result == SUCCESS ) break; + errorMessages.append( " " ); + errorMessages.append( message_ ); + errorMessages.append( "\n" ); if ( outputDevice > 0 ) break; clearStreamInfo(); } @@ -308,22 +361,25 @@ void RtApi :: openStream( int outputDevice, int outputChannels, else device = inputDevice - 1; - for (int i=-1; i= 0 ) { if ( i == defaultDevice ) continue; device = i; } - if (devices_[device].probed == false) { + if ( devices_[device].probed == false ) { // If the device wasn't successfully probed before, try it // (again) now. clearDeviceInfo(&devices_[device]); probeDeviceInfo(&devices_[device]); } if ( devices_[device].probed ) - result = probeDeviceOpen(device, mode, channels, sampleRate, - format, bufferSize, numberOfBuffers); - if (result == SUCCESS) break; - if ( outputDevice > 0 ) break; + result = probeDeviceOpen( device, mode, channels, sampleRate, + format, bufferSize, numberOfBuffers ); + if ( result == SUCCESS ) break; + errorMessages.append( " " ); + errorMessages.append( message_ ); + errorMessages.append( "\n" ); + if ( inputDevice > 0 ) break; } } @@ -336,9 +392,11 @@ void RtApi :: openStream( int outputDevice, int outputChannels, clearStreamInfo(); if ( ( outputDevice == 0 && outputChannels > 0 ) || ( inputDevice == 0 && inputChannels > 0 ) ) - sprintf(message_,"RtApi: no devices found for given stream parameters."); + sprintf(message_,"RtApi: no devices found for given stream parameters: \n%s", + errorMessages.c_str()); else - sprintf(message_,"RtApi: unable to open specified device(s) with given stream parameters."); + sprintf(message_,"RtApi: unable to open specified device(s) with given stream parameters: \n%s", + errorMessages.c_str()); error(RtError::INVALID_PARAMETER); return; @@ -349,6 +407,11 @@ int RtApi :: getDeviceCount(void) return devices_.size(); } +RtApi::StreamState RtApi :: getStreamState( void ) const +{ + return stream_.state; +} + RtAudioDeviceInfo RtApi :: getDeviceInfo( int device ) { if (device > (int) devices_.size() || device < 1) { @@ -1130,6 +1193,49 @@ bool RtApiOss :: probeDeviceOpen(int device, StreamMode mode, int channels, else stream_.mode = mode; + // Setup the buffer conversion information structure. + if ( stream_.doConvertBuffer[mode] ) { + if (mode == INPUT) { // convert device to user buffer + stream_.convertInfo[mode].inJump = stream_.nDeviceChannels[1]; + stream_.convertInfo[mode].outJump = stream_.nUserChannels[1]; + stream_.convertInfo[mode].inFormat = stream_.deviceFormat[1]; + stream_.convertInfo[mode].outFormat = stream_.userFormat; + } + else { // convert user to device buffer + stream_.convertInfo[mode].inJump = stream_.nUserChannels[0]; + stream_.convertInfo[mode].outJump = stream_.nDeviceChannels[0]; + stream_.convertInfo[mode].inFormat = stream_.userFormat; + stream_.convertInfo[mode].outFormat = stream_.deviceFormat[0]; + } + + if ( stream_.convertInfo[mode].inJump < stream_.convertInfo[mode].outJump ) + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].inJump; + else + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].outJump; + + // Set up the interleave/deinterleave offsets. + if ( mode == INPUT && stream_.deInterleave[1] ) { + for (int k=0; kprobed = true; } -OSStatus callbackHandler(AudioDeviceID inDevice, - const AudioTimeStamp* inNow, - const AudioBufferList* inInputData, - const AudioTimeStamp* inInputTime, - AudioBufferList* outOutputData, - const AudioTimeStamp* inOutputTime, - void* infoPointer) +OSStatus callbackHandler( AudioDeviceID inDevice, + const AudioTimeStamp* inNow, + const AudioBufferList* inInputData, + const AudioTimeStamp* inInputTime, + AudioBufferList* outOutputData, + const AudioTimeStamp* inOutputTime, + void* infoPointer ) { CallbackInfo *info = (CallbackInfo *) infoPointer; @@ -1882,11 +1988,11 @@ OSStatus callbackHandler(AudioDeviceID inDevice, return kAudioHardwareNoError; } -OSStatus deviceListener(AudioDeviceID inDevice, - UInt32 channel, - Boolean isInput, - AudioDevicePropertyID propertyID, - void* handlePointer) +OSStatus deviceListener( AudioDeviceID inDevice, + UInt32 channel, + Boolean isInput, + AudioDevicePropertyID propertyID, + void* handlePointer ) { CoreHandle *handle = (CoreHandle *) handlePointer; if ( propertyID == kAudioDeviceProcessorOverload ) { @@ -2110,7 +2216,7 @@ bool RtApiCore :: probeDeviceOpen( int device, StreamMode mode, int channels, stream_.apiHandle = (void *) handle; } else - handle = (CoreHandle *) stream_.apiHandle; + handle = (CoreHandle *) stream_.apiHandle; handle->index[mode] = iStream; // Allocate necessary internal buffers. @@ -2171,6 +2277,49 @@ bool RtApiCore :: probeDeviceOpen( int device, StreamMode mode, int channels, stream_.state = STREAM_STOPPED; stream_.callbackInfo.object = (void *) this; + // Setup the buffer conversion information structure. + if ( stream_.doConvertBuffer[mode] ) { + if (mode == INPUT) { // convert device to user buffer + stream_.convertInfo[mode].inJump = stream_.nDeviceChannels[1]; + stream_.convertInfo[mode].outJump = stream_.nUserChannels[1]; + stream_.convertInfo[mode].inFormat = stream_.deviceFormat[1]; + stream_.convertInfo[mode].outFormat = stream_.userFormat; + } + else { // convert user to device buffer + stream_.convertInfo[mode].inJump = stream_.nUserChannels[0]; + stream_.convertInfo[mode].outJump = stream_.nDeviceChannels[0]; + stream_.convertInfo[mode].inFormat = stream_.userFormat; + stream_.convertInfo[mode].outFormat = stream_.deviceFormat[0]; + } + + if ( stream_.convertInfo[mode].inJump < stream_.convertInfo[mode].outJump ) + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].inJump; + else + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].outJump; + + // Set up the interleave/deinterleave offsets. + if ( mode == INPUT && stream_.deInterleave[1] ) { + for (int k=0; kdeviceBuffer; - convertStreamBuffer(OUTPUT); + convertBuffer( stream_.deviceBuffer, stream_.userBuffer, stream_.convertInfo[0] ); if ( stream_.doByteSwap[0] ) byteSwapBuffer(stream_.deviceBuffer, stream_.bufferSize * stream_.nDeviceChannels[0], @@ -2434,6 +2583,7 @@ void RtApiCore :: callbackEvent( AudioDeviceID deviceId, void *inData, void *out } } + id = *( (AudioDeviceID *) devices_[stream_.device[1]].apiDeviceId ); if ( stream_.mode == INPUT || ( stream_.mode == DUPLEX && deviceId == id ) ) { if (stream_.doConvertBuffer[1]) { @@ -2453,7 +2603,7 @@ void RtApiCore :: callbackEvent( AudioDeviceID deviceId, void *inData, void *out byteSwapBuffer(stream_.deviceBuffer, stream_.bufferSize * stream_.nDeviceChannels[1], stream_.deviceFormat[1]); - convertStreamBuffer(INPUT); + convertBuffer( stream_.userBuffer, stream_.deviceBuffer, stream_.convertInfo[1] ); } else { @@ -2526,9 +2676,10 @@ void RtApiCore :: cancelStreamCallback() // // .jackd -d alsa -d hw:0 // -// 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, +// 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 // @@ -2645,7 +2796,7 @@ void RtApiJack :: probeDeviceInfo(RtApiDevice *info) if (info->maxOutputChannels == 0 && info->maxInputChannels == 0) { jack_client_close(client); sprintf(message_, "RtApiJack: error determining jack input/output channels!"); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return; } @@ -2671,7 +2822,7 @@ void RtApiJack :: probeDeviceInfo(RtApiDevice *info) if (info->nativeFormats == 0) { jack_client_close(client); sprintf(message_, "RtApiJack: error determining jack server data format!"); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return; } @@ -2700,6 +2851,14 @@ void jackShutdown(void *infoPointer) JackHandle *handle = (JackHandle *) info->apiInfo; handle->clientOpen = false; 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->getStreamState() == RtApi::STREAM_STOPPED ) return; + try { object->closeStream(); } @@ -2708,7 +2867,7 @@ void jackShutdown(void *infoPointer) return; } - fprintf(stderr, "\nRtApiJack: the Jack server is shutting down ... stream stopped and closed!!!\n\n"); + fprintf(stderr, "\nRtApiJack: the Jack server is shutting down this client ... stream stopped and closed!!!\n\n"); } int jackXrun( void * ) @@ -2877,6 +3036,49 @@ bool RtApiJack :: probeDeviceOpen(int device, StreamMode mode, int channels, jack_on_shutdown( handle->client, jackShutdown, (void *) &stream_.callbackInfo ); } + // Setup the buffer conversion information structure. + if ( stream_.doConvertBuffer[mode] ) { + if (mode == INPUT) { // convert device to user buffer + stream_.convertInfo[mode].inJump = stream_.nDeviceChannels[1]; + stream_.convertInfo[mode].outJump = stream_.nUserChannels[1]; + stream_.convertInfo[mode].inFormat = stream_.deviceFormat[1]; + stream_.convertInfo[mode].outFormat = stream_.userFormat; + } + else { // convert user to device buffer + stream_.convertInfo[mode].inJump = stream_.nUserChannels[0]; + stream_.convertInfo[mode].outJump = stream_.nDeviceChannels[0]; + stream_.convertInfo[mode].inFormat = stream_.userFormat; + stream_.convertInfo[mode].outFormat = stream_.deviceFormat[0]; + } + + if ( stream_.convertInfo[mode].inJump < stream_.convertInfo[mode].outJump ) + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].inJump; + else + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].outJump; + + // Set up the interleave/deinterleave offsets. + if ( mode == INPUT && stream_.deInterleave[1] ) { + for (int k=0; kports[0][i], @@ -3120,7 +3322,7 @@ void RtApiJack :: callbackEvent( unsigned long nframes ) (jack_nframes_t) nframes); memcpy(&stream_.deviceBuffer[i*bufferBytes], jackbuffer, bufferBytes ); } - convertStreamBuffer(INPUT); + convertBuffer( stream_.userBuffer, stream_.deviceBuffer, stream_.convertInfo[1] ); } else { // single channel only jackbuffer = (jack_default_audio_sample_t *) jack_port_get_buffer(handle->ports[1][0], @@ -3178,6 +3380,17 @@ void RtApiJack :: cancelStreamCallback() #include #include +// A structure to hold various information related to the ALSA API +// implementation. +struct AlsaHandle { + snd_pcm_t *handles[2]; + bool synchronized; + char *tempBuffer; + + AlsaHandle() + :synchronized(false), tempBuffer(0) {} +}; + extern "C" void *alsaCallbackHandler(void * ptr); RtApiAlsa :: RtApiAlsa() @@ -3319,7 +3532,7 @@ void RtApiAlsa :: probeDeviceInfo(RtApiDevice *info) snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: hardware probe error (%s): %s.", info->name.c_str(), snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); goto capture_probe; } @@ -3330,7 +3543,7 @@ void RtApiAlsa :: probeDeviceInfo(RtApiDevice *info) snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: hardware minimum channel probe error (%s): %s.", info->name.c_str(), snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); goto capture_probe; } info->minOutputChannels = value; @@ -3340,7 +3553,7 @@ void RtApiAlsa :: probeDeviceInfo(RtApiDevice *info) snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: hardware maximum channel probe error (%s): %s.", info->name.c_str(), snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); goto capture_probe; } info->maxOutputChannels = value; @@ -3391,7 +3604,7 @@ void RtApiAlsa :: probeDeviceInfo(RtApiDevice *info) snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: hardware probe error (%s): %s.", info->name.c_str(), snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); if (info->maxOutputChannels > 0) goto probe_parameters; else @@ -3404,7 +3617,7 @@ void RtApiAlsa :: probeDeviceInfo(RtApiDevice *info) snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: hardware minimum in channel probe error (%s): %s.", info->name.c_str(), snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); if (info->maxOutputChannels > 0) goto probe_parameters; else @@ -3417,7 +3630,7 @@ void RtApiAlsa :: probeDeviceInfo(RtApiDevice *info) snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: hardware maximum in channel probe error (%s): %s.", info->name.c_str(), snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); if (info->maxOutputChannels > 0) goto probe_parameters; else @@ -3453,7 +3666,7 @@ void RtApiAlsa :: probeDeviceInfo(RtApiDevice *info) if (err < 0) { sprintf(message_, "RtApiAlsa: pcm (%s) won't reopen during probe: %s.", info->name.c_str(), snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return; } @@ -3463,7 +3676,7 @@ void RtApiAlsa :: probeDeviceInfo(RtApiDevice *info) snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: hardware reopen probe error (%s): %s.", info->name.c_str(), snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return; } @@ -3509,7 +3722,7 @@ void RtApiAlsa :: probeDeviceInfo(RtApiDevice *info) snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: pcm device (%s) data format not supported by RtAudio.", info->name.c_str()); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return; } @@ -3544,7 +3757,7 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, if (err < 0) { sprintf(message_,"RtApiAlsa: pcm device (%s) won't open: %s.", name, snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -3556,7 +3769,7 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: error getting parameter handle (%s): %s.", name, snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -3576,14 +3789,14 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, else { snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: device (%s) access not supported by RtAudio.", name); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } if (err < 0) { snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: error setting access ( (%s): %s.", name, snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -3649,7 +3862,7 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, // If we get here, no supported format was found. sprintf(message_,"RtApiAlsa: pcm device (%s) data format not supported by RtAudio.", name); snd_pcm_close(handle); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; set_format: @@ -3658,7 +3871,7 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: error setting format (%s): %s.", name, snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -3672,7 +3885,7 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: error getting format endian-ness (%s): %s.", name, snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } } @@ -3683,7 +3896,7 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: error setting sample rate (%d) on device (%s): %s.", sampleRate, name, snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -3697,7 +3910,7 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: channels (%d) not supported by device (%s).", channels, name); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -3705,7 +3918,7 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, if (err < 0 ) { snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: error getting min channels count on device (%s).", name); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } device_channels = value; @@ -3718,7 +3931,7 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: error setting channels (%d) on device (%s): %s.", device_channels, name, snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -3727,54 +3940,26 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, unsigned int periods = numberOfBuffers; // Even though the hardware might allow 1 buffer, it won't work reliably. if (periods < 2) periods = 2; - err = snd_pcm_hw_params_get_periods_min(hw_params, &value, &dir); - if (err < 0) { - snd_pcm_close(handle); - sprintf(message_, "RtApiAlsa: error getting min periods on device (%s): %s.", - name, snd_strerror(err)); - error(RtError::WARNING); - return FAILURE; - } - if (value > periods) periods = value; - err = snd_pcm_hw_params_get_periods_max(hw_params, &value, &dir); - if (err < 0) { - snd_pcm_close(handle); - sprintf(message_, "RtApiAlsa: error getting max periods on device (%s): %s.", - name, snd_strerror(err)); - error(RtError::WARNING); - return FAILURE; - } - if (value < periods) periods = value; - - err = snd_pcm_hw_params_set_periods(handle, hw_params, periods, 0); + err = snd_pcm_hw_params_set_periods_near(handle, hw_params, &periods, &dir); if (err < 0) { snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: error setting periods (%s): %s.", name, snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } // Set the buffer (or period) size. - snd_pcm_uframes_t period_size; - err = snd_pcm_hw_params_get_period_size_min(hw_params, &period_size, &dir); - if (err < 0) { - snd_pcm_close(handle); - sprintf(message_, "RtApiAlsa: error getting period size (%s): %s.", - name, snd_strerror(err)); - error(RtError::WARNING); - return FAILURE; - } - if (*bufferSize < (int) period_size) *bufferSize = (int) period_size; - - err = snd_pcm_hw_params_set_period_size(handle, hw_params, *bufferSize, 0); + snd_pcm_uframes_t period_size = *bufferSize; + err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &period_size, &dir); if (err < 0) { snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: error setting period size (%s): %s.", name, snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } + *bufferSize = period_size; // If attempting to setup a duplex stream, the bufferSize parameter // MUST be the same in both directions! @@ -3793,7 +3978,7 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, snd_pcm_close(handle); sprintf(message_, "RtApiAlsa: error installing hardware configuration (%s): %s.", name, snd_strerror(err)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -3802,23 +3987,40 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, snd_pcm_hw_params_dump(hw_params, out); #endif - // Allocate the stream handle if necessary and then save. - snd_pcm_t **handles; + // Set the software configuration to fill buffers with zeros and prevent device stopping on xruns. + snd_pcm_sw_params_t *sw_params = NULL; + snd_pcm_sw_params_alloca( &sw_params ); + snd_pcm_sw_params_current( handle, sw_params ); + snd_pcm_sw_params_set_start_threshold( handle, sw_params, *bufferSize ); + snd_pcm_sw_params_set_stop_threshold( handle, sw_params, 0x7fffffff ); + snd_pcm_sw_params_set_silence_threshold( handle, sw_params, 0 ); + snd_pcm_sw_params_set_silence_size( handle, sw_params, INT_MAX ); + err = snd_pcm_sw_params( handle, sw_params ); + if (err < 0) { + snd_pcm_close(handle); + sprintf(message_, "RtAudio: ALSA error installing software configuration (%s): %s.", + name, snd_strerror(err)); + error(RtError::DEBUG_WARNING); + return FAILURE; + } + +#if defined(__RTAUDIO_DEBUG__) + fprintf(stderr, "\nRtApiAlsa: dump software params after installation:\n\n"); + snd_pcm_sw_params_dump(sw_params, out); +#endif + + // Allocate the ApiHandle if necessary and then save. + AlsaHandle *apiInfo = 0; if ( stream_.apiHandle == 0 ) { - handles = (snd_pcm_t **) calloc(2, sizeof(snd_pcm_t *)); - if ( handle == NULL ) { - sprintf(message_, "RtApiAlsa: error allocating handle memory (%s).", - devices_[device].name.c_str()); - goto error; - } - stream_.apiHandle = (void *) handles; - handles[0] = 0; - handles[1] = 0; + apiInfo = (AlsaHandle *) new AlsaHandle; + stream_.apiHandle = (void *) apiInfo; + apiInfo->handles[0] = 0; + apiInfo->handles[1] = 0; } else { - handles = (snd_pcm_t **) stream_.apiHandle; + apiInfo = (AlsaHandle *) stream_.apiHandle; } - handles[mode] = handle; + apiInfo->handles[mode] = handle; // Set flags for buffer conversion stream_.doConvertBuffer[mode] = false; @@ -3840,8 +4042,10 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, buffer_bytes *= *bufferSize * formatBytes(stream_.userFormat); if (stream_.userBuffer) free(stream_.userBuffer); + if (apiInfo->tempBuffer) free(apiInfo->tempBuffer); stream_.userBuffer = (char *) calloc(buffer_bytes, 1); - if (stream_.userBuffer == NULL) { + apiInfo->tempBuffer = (char *) calloc(buffer_bytes, 1); + if ( stream_.userBuffer == NULL || apiInfo->tempBuffer == NULL ) { sprintf(message_, "RtApiAlsa: error allocating user buffer memory (%s).", devices_[device].name.c_str()); goto error; @@ -3876,23 +4080,77 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, stream_.device[mode] = device; stream_.state = STREAM_STOPPED; - if ( stream_.mode == OUTPUT && mode == INPUT ) + if ( stream_.mode == OUTPUT && mode == INPUT ) { // We had already set up an output stream. stream_.mode = DUPLEX; + // Link the streams if possible. + apiInfo->synchronized = false; + if (snd_pcm_link( apiInfo->handles[0], apiInfo->handles[1] ) == 0) + apiInfo->synchronized = true; + else { + sprintf(message_, "RtApiAlsa: unable to synchronize input and output streams (%s).", + devices_[device].name.c_str()); + error(RtError::DEBUG_WARNING); + } + } else stream_.mode = mode; stream_.nBuffers = periods; stream_.sampleRate = sampleRate; + // Setup the buffer conversion information structure. + if ( stream_.doConvertBuffer[mode] ) { + if (mode == INPUT) { // convert device to user buffer + stream_.convertInfo[mode].inJump = stream_.nDeviceChannels[1]; + stream_.convertInfo[mode].outJump = stream_.nUserChannels[1]; + stream_.convertInfo[mode].inFormat = stream_.deviceFormat[1]; + stream_.convertInfo[mode].outFormat = stream_.userFormat; + } + else { // convert user to device buffer + stream_.convertInfo[mode].inJump = stream_.nUserChannels[0]; + stream_.convertInfo[mode].outJump = stream_.nDeviceChannels[0]; + stream_.convertInfo[mode].inFormat = stream_.userFormat; + stream_.convertInfo[mode].outFormat = stream_.deviceFormat[0]; + } + + if ( stream_.convertInfo[mode].inJump < stream_.convertInfo[mode].outJump ) + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].inJump; + else + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].outJump; + + // Set up the interleave/deinterleave offsets. + if ( mode == INPUT && stream_.deInterleave[1] ) { + for (int k=0; khandles[0]) + snd_pcm_close(apiInfo->handles[0]); + if (apiInfo->handles[1]) + snd_pcm_close(apiInfo->handles[1]); + if ( apiInfo->tempBuffer ) free(apiInfo->tempBuffer); + delete apiInfo; stream_.apiHandle = 0; } @@ -3901,7 +4159,7 @@ bool RtApiAlsa :: probeDeviceOpen( int device, StreamMode mode, int channels, stream_.userBuffer = 0; } - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -3916,12 +4174,12 @@ void RtApiAlsa :: closeStream() return; } - snd_pcm_t **handle = (snd_pcm_t **) stream_.apiHandle; + AlsaHandle *apiInfo = (AlsaHandle *) stream_.apiHandle; if (stream_.state == STREAM_RUNNING) { if (stream_.mode == OUTPUT || stream_.mode == DUPLEX) - snd_pcm_drop(handle[0]); + snd_pcm_drop(apiInfo->handles[0]); if (stream_.mode == INPUT || stream_.mode == DUPLEX) - snd_pcm_drop(handle[1]); + snd_pcm_drop(apiInfo->handles[1]); stream_.state = STREAM_STOPPED; } @@ -3930,11 +4188,12 @@ void RtApiAlsa :: closeStream() pthread_join(stream_.callbackInfo.thread, NULL); } - if (handle) { - if (handle[0]) snd_pcm_close(handle[0]); - if (handle[1]) snd_pcm_close(handle[1]); - free(handle); - handle = 0; + if (apiInfo) { + if (apiInfo->handles[0]) snd_pcm_close(apiInfo->handles[0]); + if (apiInfo->handles[1]) snd_pcm_close(apiInfo->handles[1]); + free(apiInfo->tempBuffer); + delete apiInfo; + stream_.apiHandle = 0; } if (stream_.userBuffer) { @@ -3961,7 +4220,8 @@ void RtApiAlsa :: startStream() int err; snd_pcm_state_t state; - snd_pcm_t **handle = (snd_pcm_t **) stream_.apiHandle; + AlsaHandle *apiInfo = (AlsaHandle *) stream_.apiHandle; + snd_pcm_t **handle = (snd_pcm_t **) apiInfo->handles; if (stream_.mode == OUTPUT || stream_.mode == DUPLEX) { state = snd_pcm_state(handle[0]); if (state != SND_PCM_STATE_PREPARED) { @@ -3975,7 +4235,7 @@ void RtApiAlsa :: startStream() } } - if (stream_.mode == INPUT || stream_.mode == DUPLEX) { + if ( (stream_.mode == INPUT || stream_.mode == DUPLEX) && !apiInfo->synchronized ) { state = snd_pcm_state(handle[1]); if (state != SND_PCM_STATE_PREPARED) { err = snd_pcm_prepare(handle[1]); @@ -4003,7 +4263,8 @@ void RtApiAlsa :: stopStream() MUTEX_LOCK(&stream_.mutex); int err; - snd_pcm_t **handle = (snd_pcm_t **) stream_.apiHandle; + AlsaHandle *apiInfo = (AlsaHandle *) stream_.apiHandle; + snd_pcm_t **handle = (snd_pcm_t **) apiInfo->handles; if (stream_.mode == OUTPUT || stream_.mode == DUPLEX) { err = snd_pcm_drain(handle[0]); if (err < 0) { @@ -4014,7 +4275,7 @@ void RtApiAlsa :: stopStream() } } - if (stream_.mode == INPUT || stream_.mode == DUPLEX) { + if ( (stream_.mode == INPUT || stream_.mode == DUPLEX) && !apiInfo->synchronized ) { err = snd_pcm_drain(handle[1]); if (err < 0) { sprintf(message_, "RtApiAlsa: error draining pcm device (%s): %s.", @@ -4038,7 +4299,8 @@ void RtApiAlsa :: abortStream() MUTEX_LOCK(&stream_.mutex); int err; - snd_pcm_t **handle = (snd_pcm_t **) stream_.apiHandle; + AlsaHandle *apiInfo = (AlsaHandle *) stream_.apiHandle; + snd_pcm_t **handle = (snd_pcm_t **) apiInfo->handles; if (stream_.mode == OUTPUT || stream_.mode == DUPLEX) { err = snd_pcm_drop(handle[0]); if (err < 0) { @@ -4049,7 +4311,7 @@ void RtApiAlsa :: abortStream() } } - if (stream_.mode == INPUT || stream_.mode == DUPLEX) { + if ( (stream_.mode == INPUT || stream_.mode == DUPLEX) && !apiInfo->synchronized ) { err = snd_pcm_drop(handle[1]); if (err < 0) { sprintf(message_, "RtApiAlsa: error draining pcm device (%s): %s.", @@ -4070,7 +4332,8 @@ int RtApiAlsa :: streamWillBlock() MUTEX_LOCK(&stream_.mutex); int err = 0, frames = 0; - snd_pcm_t **handle = (snd_pcm_t **) stream_.apiHandle; + AlsaHandle *apiInfo = (AlsaHandle *) stream_.apiHandle; + snd_pcm_t **handle = (snd_pcm_t **) apiInfo->handles; if (stream_.mode == OUTPUT || stream_.mode == DUPLEX) { err = snd_pcm_avail_update(handle[0]); if (err < 0) { @@ -4124,49 +4387,56 @@ void RtApiAlsa :: tickStream() int err; char *buffer; int channels; + AlsaHandle *apiInfo; snd_pcm_t **handle; RtAudioFormat format; - handle = (snd_pcm_t **) stream_.apiHandle; - if (stream_.mode == OUTPUT || stream_.mode == DUPLEX) { + apiInfo = (AlsaHandle *) stream_.apiHandle; + handle = (snd_pcm_t **) apiInfo->handles; - // Setup parameters and do buffer conversion if necessary. - if (stream_.doConvertBuffer[0]) { - convertStreamBuffer(OUTPUT); + if ( stream_.mode == DUPLEX ) { + // In duplex mode, we need to make the snd_pcm_read call before + // the snd_pcm_write call in order to avoid under/over runs. So, + // copy the userData to our temporary buffer. + int bufferBytes; + bufferBytes = stream_.bufferSize * stream_.nUserChannels[0] * formatBytes(stream_.userFormat); + memcpy( apiInfo->tempBuffer, stream_.userBuffer, bufferBytes ); + } + + if (stream_.mode == INPUT || stream_.mode == DUPLEX) { + + // Setup parameters. + if (stream_.doConvertBuffer[1]) { buffer = stream_.deviceBuffer; - channels = stream_.nDeviceChannels[0]; - format = stream_.deviceFormat[0]; + channels = stream_.nDeviceChannels[1]; + format = stream_.deviceFormat[1]; } else { buffer = stream_.userBuffer; - channels = stream_.nUserChannels[0]; + channels = stream_.nUserChannels[1]; format = stream_.userFormat; } - // Do byte swapping if necessary. - if (stream_.doByteSwap[0]) - byteSwapBuffer(buffer, stream_.bufferSize * channels, format); - - // Write samples to device in interleaved/non-interleaved format. - if (stream_.deInterleave[0]) { + // Read samples from device in interleaved/non-interleaved format. + if (stream_.deInterleave[1]) { void *bufs[channels]; size_t offset = stream_.bufferSize * formatBytes(format); for (int i=0; itempBuffer, stream_.convertInfo[0] ); + else + convertBuffer( buffer, stream_.userBuffer, stream_.convertInfo[0] ); + channels = stream_.nDeviceChannels[0]; + format = stream_.deviceFormat[0]; } else { - buffer = stream_.userBuffer; - channels = stream_.nUserChannels[1]; + if ( stream_.mode == DUPLEX ) + buffer = apiInfo->tempBuffer; + else + buffer = stream_.userBuffer; + channels = stream_.nUserChannels[0]; format = stream_.userFormat; } - // Read samples from device in interleaved/non-interleaved format. - if (stream_.deInterleave[1]) { + // Do byte swapping if necessary. + if (stream_.doByteSwap[0]) + byteSwapBuffer(buffer, stream_.bufferSize * channels, format); + + // Write samples to device in interleaved/non-interleaved format. + if (stream_.deInterleave[0]) { void *bufs[channels]; size_t offset = stream_.bufferSize * formatBytes(format); for (int i=0; icoInitialized = false; this->initialize(); if (nDevices_ <= 0) { @@ -4388,10 +4695,26 @@ RtApiAsio :: RtApiAsio() RtApiAsio :: ~RtApiAsio() { if ( stream_.mode != UNINITIALIZED ) closeStream(); + if ( coInitialized ) + { + CoUninitialize(); + } + } void RtApiAsio :: initialize(void) { + + // 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)) + { + sprintf(message_,"RtApiAsio: ASIO requires a single-threaded appartment. Call CoInitializeEx(0,COINIT_APARTMENTTHREADED)"); + } + coInitialized = true; + nDevices_ = drivers.asioGetNumDev(); if (nDevices_ <= 0) return; @@ -4435,16 +4758,8 @@ void RtApiAsio :: probeDeviceInfo(RtApiDevice *info) ASIOError result = ASIOInit( &driverInfo ); if ( result != ASE_OK ) { - char details[32]; - if ( result == ASE_HWMalfunction ) - sprintf(details, "hardware malfunction"); - else if ( result == ASE_NoMemory ) - sprintf(details, "no memory"); - else if ( result == ASE_NotPresent ) - sprintf(details, "driver/hardware not present"); - else - sprintf(details, "unspecified"); - sprintf(message_, "RtApiAsio: error (%s) initializing driver (%s).", details, info->name.c_str()); + sprintf(message_, "RtApiAsio: error (%s) initializing driver (%s).", + GetAsioErrorString(result), info->name.c_str()); error(RtError::DEBUG_WARNING); return; } @@ -4454,7 +4769,9 @@ void RtApiAsio :: probeDeviceInfo(RtApiDevice *info) result = ASIOGetChannels( &inputChannels, &outputChannels ); if ( result != ASE_OK ) { drivers.removeCurrentDriver(); - sprintf(message_, "RtApiAsio: error getting input/output channel count (%s).", info->name.c_str()); + sprintf(message_, "RtApiAsio: error (%s) getting input/output channel count (%s).", + GetAsioErrorString(result), + info->name.c_str()); error(RtError::DEBUG_WARNING); return; } @@ -4497,7 +4814,9 @@ void RtApiAsio :: probeDeviceInfo(RtApiDevice *info) result = ASIOGetChannelInfo( &channelInfo ); if ( result != ASE_OK ) { drivers.removeCurrentDriver(); - sprintf(message_, "RtApiAsio: error getting driver (%s) channel information.", info->name.c_str()); + sprintf(message_, "RtApiAsio: error (%s) getting driver (%s) channel information.", + GetAsioErrorString(result), + info->name.c_str()); error(RtError::DEBUG_WARNING); return; } @@ -4641,23 +4960,16 @@ bool RtApiAsio :: probeDeviceOpen(int device, StreamMode mode, int channels, ASIOError result; if ( mode != INPUT || stream_.mode != OUTPUT ) { if ( !drivers.loadDriver( (char *)devices_[device].name.c_str() ) ) { - sprintf(message_, "RtApiAsio: error loading driver (%s).", devices_[device].name.c_str()); + sprintf(message_, "RtApiAsio: error loading driver (%s).", + devices_[device].name.c_str()); error(RtError::DEBUG_WARNING); return FAILURE; } result = ASIOInit( &driverInfo ); if ( result != ASE_OK ) { - char details[32]; - if ( result == ASE_HWMalfunction ) - sprintf(details, "hardware malfunction"); - else if ( result == ASE_NoMemory ) - sprintf(details, "no memory"); - else if ( result == ASE_NotPresent ) - sprintf(details, "driver/hardware not present"); - else - sprintf(details, "unspecified"); - sprintf(message_, "RtApiAsio: error (%s) initializing driver (%s).", details, devices_[device].name.c_str()); + sprintf(message_, "RtApiAsio: error (%s) initializing driver (%s).", + GetAsioErrorString(result), devices_[device].name.c_str()); error(RtError::DEBUG_WARNING); return FAILURE; } @@ -4668,8 +4980,9 @@ bool RtApiAsio :: probeDeviceOpen(int device, StreamMode mode, int channels, result = ASIOGetChannels( &inputChannels, &outputChannels ); if ( result != ASE_OK ) { drivers.removeCurrentDriver(); - sprintf(message_, "RtApiAsio: error getting input/output channel count (%s).", - devices_[device].name.c_str()); + sprintf(message_, "RtApiAsio: error (%s) getting input/output channel count (%s).", + GetAsioErrorString(result), + devices_[device].name.c_str()); error(RtError::DEBUG_WARNING); return FAILURE; } @@ -4755,8 +5068,9 @@ bool RtApiAsio :: probeDeviceOpen(int device, StreamMode mode, int channels, result = ASIOGetBufferSize( &minSize, &maxSize, &preferSize, &granularity ); if ( result != ASE_OK ) { drivers.removeCurrentDriver(); - sprintf(message_, "RtApiAsio: driver (%s) error getting buffer size.", - devices_[device].name.c_str()); + sprintf(message_, "RtApiAsio: error (%s) on driver (%s) error getting buffer size.", + GetAsioErrorString(result), + devices_[device].name.c_str()); error(RtError::DEBUG_WARNING); return FAILURE; } @@ -4770,8 +5084,14 @@ bool RtApiAsio :: probeDeviceOpen(int device, StreamMode mode, int channels, if ( *bufferSize < minSize ) *bufferSize = minSize; else if ( *bufferSize > maxSize ) *bufferSize = maxSize; else *bufferSize = preferSize; + } else if (granularity != 0) + { + // to an even multiple of granularity, rounding up. + *bufferSize = (*bufferSize + granularity-1)/granularity*granularity; } + + if ( mode == INPUT && stream_.mode == OUTPUT && stream_.bufferSize != *bufferSize ) std::cerr << "Possible input/output buffersize discrepancy!" << std::endl; @@ -4794,10 +5114,10 @@ bool RtApiAsio :: probeDeviceOpen(int device, StreamMode mode, int channels, } handle->bufferInfos = 0; // Create a manual-reset event. - handle->condition = CreateEvent(NULL, // no security - TRUE, // manual-reset - FALSE, // non-signaled initially - NULL); // unnamed + handle->condition = CreateEvent( NULL, // no security + TRUE, // manual-reset + FALSE, // non-signaled initially + NULL ); // unnamed stream_.apiHandle = (void *) handle; } @@ -4837,8 +5157,9 @@ bool RtApiAsio :: probeDeviceOpen(int device, StreamMode mode, int channels, asioCallbacks.bufferSwitchTimeInfo = NULL; result = ASIOCreateBuffers( handle->bufferInfos, nChannels, stream_.bufferSize, &asioCallbacks); if ( result != ASE_OK ) { - sprintf(message_, "RtApiAsio: driver (%s) error creating buffers.", - devices_[device].name.c_str()); + sprintf(message_, "RtApiAsio: eror (%s) on driver (%s) error creating buffers.", + GetAsioErrorString(result), + devices_[device].name.c_str()); goto error; } @@ -4864,8 +5185,9 @@ bool RtApiAsio :: probeDeviceOpen(int device, StreamMode mode, int channels, if (stream_.userBuffer) free(stream_.userBuffer); stream_.userBuffer = (char *) calloc(buffer_bytes, 1); if (stream_.userBuffer == NULL) { - sprintf(message_, "RtApiAsio: error allocating user buffer memory (%s).", - devices_[device].name.c_str()); + sprintf(message_, "RtApiAsio: error (%s) allocating user buffer memory (%s).", + GetAsioErrorString(result), + devices_[device].name.c_str()); goto error; } } @@ -4889,7 +5211,8 @@ bool RtApiAsio :: probeDeviceOpen(int device, StreamMode mode, int channels, if (stream_.deviceBuffer) free(stream_.deviceBuffer); stream_.deviceBuffer = (char *) calloc(buffer_bytes, 1); if (stream_.deviceBuffer == NULL) { - sprintf(message_, "RtApiAsio: error allocating device buffer memory (%s).", + sprintf(message_, "RtApiAsio: error (%s) allocating device buffer memory (%s).", + GetAsioErrorString(result), devices_[device].name.c_str()); goto error; } @@ -4907,6 +5230,49 @@ bool RtApiAsio :: probeDeviceOpen(int device, StreamMode mode, int channels, asioCallbackInfo = &stream_.callbackInfo; stream_.callbackInfo.object = (void *) this; + // Setup the buffer conversion information structure. + if ( stream_.doConvertBuffer[mode] ) { + if (mode == INPUT) { // convert device to user buffer + stream_.convertInfo[mode].inJump = stream_.nDeviceChannels[1]; + stream_.convertInfo[mode].outJump = stream_.nUserChannels[1]; + stream_.convertInfo[mode].inFormat = stream_.deviceFormat[1]; + stream_.convertInfo[mode].outFormat = stream_.userFormat; + } + else { // convert user to device buffer + stream_.convertInfo[mode].inJump = stream_.nUserChannels[0]; + stream_.convertInfo[mode].outJump = stream_.nDeviceChannels[0]; + stream_.convertInfo[mode].inFormat = stream_.userFormat; + stream_.convertInfo[mode].outFormat = stream_.deviceFormat[0]; + } + + if ( stream_.convertInfo[mode].inJump < stream_.convertInfo[mode].outJump ) + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].inJump; + else + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].outJump; + + // Set up the interleave/deinterleave offsets. + if ( mode == INPUT && stream_.deInterleave[1] ) { + for (int k=0; kusingCallback ) SetEvent( handle->condition ); + // 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); } @@ -5187,7 +5558,63 @@ void RtApiAsio :: callbackEvent(long bufferIndex) #if defined(__WINDOWS_DS__) // Windows DirectSound API + #include +#include + +#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 dsPointerDifference(DWORD laterPointer,DWORD earlierPointer,DWORD bufferSize) +{ + if (laterPointer > earlierPointer) + { + return laterPointer-earlierPointer; + } else + { + return laterPointer-earlierPointer+bufferSize; + } +} + +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; +} + + +#undef GENERATE_DEBUG_LOG // Define this to generate a debug timing log file in c:/rtaudiolog.txt" +#ifdef GENERATE_DEBUG_LOG + +#include "mmsystem.h" +#include "fstream" + +struct TTickRecord +{ + DWORD currentReadPointer, safeReadPointer; + DWORD currentWritePointer, safeWritePointer; + DWORD readTime, writeTime; + DWORD nextWritePointer, nextReadPointer; +}; + +int currentDebugLogEntry = 0; +std::vector debugLog(2000); + + +#endif // A structure to hold various information related to the DirectSound // API implementation. @@ -5195,8 +5622,33 @@ struct DsHandle { void *object; void *buffer; UINT bufferPointer; + DWORD dsBufferSize; + DWORD dsPointerLeadTime; // the number of bytes ahead of the safe pointer to lead by. }; + +RtApiDs::RtDsStatistics RtApiDs::statistics; + +// Provides a backdoor hook to monitor for DirectSound read overruns and write underruns. +RtApiDs::RtDsStatistics RtApiDs::getDsStatistics() +{ + RtDsStatistics s = statistics; + // update the calculated fields. + + + if (s.inputFrameSize != 0) + { + s.latency += s.readDeviceSafeLeadBytes*1.0/s.inputFrameSize / s.sampleRate; + } + if (s.outputFrameSize != 0) + { + s.latency += + (s.writeDeviceSafeLeadBytes+ s.writeDeviceBufferLeadBytes)*1.0/s.outputFrameSize / s.sampleRate; + } + return s; +} + + // Declarations for utility functions, callbacks, and structures // specific to the DirectSound implementation. static bool CALLBACK deviceCountCallback(LPGUID lpguid, @@ -5232,6 +5684,14 @@ struct enum_info { 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; + } + this->initialize(); if (nDevices_ <= 0) { @@ -5242,6 +5702,10 @@ RtApiDs :: RtApiDs() RtApiDs :: ~RtApiDs() { + if (coInitialized) + { + CoUninitialize(); // balanced call. + } if ( stream_.mode != UNINITIALIZED ) closeStream(); } @@ -5364,7 +5828,7 @@ void RtApiDs :: probeDeviceInfo(RtApiDevice *info) if ( FAILED(result) ) { sprintf(message_, "RtApiDs: Error performing input device id enumeration: %s.", getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return; } @@ -5377,7 +5841,7 @@ void RtApiDs :: probeDeviceInfo(RtApiDevice *info) if ( FAILED(result) ) { sprintf(message_, "RtApiDs: Could not create capture object (%s): %s.", info->name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); goto playback_probe; } @@ -5388,7 +5852,7 @@ void RtApiDs :: probeDeviceInfo(RtApiDevice *info) input->Release(); sprintf(message_, "RtApiDs: Could not get capture capabilities (%s): %s.", info->name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); goto playback_probe; } @@ -5449,7 +5913,7 @@ void RtApiDs :: probeDeviceInfo(RtApiDevice *info) if ( FAILED(result) ) { sprintf(message_, "RtApiDs: Error performing output device id enumeration: %s.", getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return; } @@ -5463,7 +5927,7 @@ void RtApiDs :: probeDeviceInfo(RtApiDevice *info) if ( FAILED(result) ) { sprintf(message_, "RtApiDs: Could not create playback object (%s): %s.", info->name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); goto check_parameters; } @@ -5473,7 +5937,7 @@ void RtApiDs :: probeDeviceInfo(RtApiDevice *info) output->Release(); sprintf(message_, "RtApiDs: Could not get playback capabilities (%s): %s.", info->name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); goto check_parameters; } @@ -5485,17 +5949,31 @@ void RtApiDs :: probeDeviceInfo(RtApiDevice *info) // if it exists. if ( info->sampleRates.size() == 0 ) { info->sampleRates.push_back( (int) out_caps.dwMinSecondarySampleRate ); - info->sampleRates.push_back( (int) out_caps.dwMaxSecondarySampleRate ); + if ( out_caps.dwMaxSecondarySampleRate > out_caps.dwMinSecondarySampleRate ) + info->sampleRates.push_back( (int) out_caps.dwMaxSecondarySampleRate ); } else { - // Check input rates against output rate range. - for ( int i=info->sampleRates.size()-1; i>=0; i-- ) { - if ( (unsigned int) info->sampleRates[i] > out_caps.dwMaxSecondarySampleRate ) - info->sampleRates.erase( info->sampleRates.begin() + i ); + // Check input rates against output rate range. If there's an + // inconsistency (such as a duplex-capable device which reports a + // single output rate of 48000 Hz), we'll go with the output + // rate(s) since the DirectSoundCapture API is stupid and broken. + // Note that the probed sample rate values are NOT used when + // opening the device. Thanks to Tue Andersen for reporting this. + if ( info->sampleRates.back() < (int) out_caps.dwMinSecondarySampleRate ) { + info->sampleRates.clear(); + info->sampleRates.push_back( (int) out_caps.dwMinSecondarySampleRate ); + if ( out_caps.dwMaxSecondarySampleRate > out_caps.dwMinSecondarySampleRate ) + info->sampleRates.push_back( (int) out_caps.dwMaxSecondarySampleRate ); } - while ( info->sampleRates.size() > 0 && - ((unsigned int) info->sampleRates[0] < out_caps.dwMinSecondarySampleRate) ) { - info->sampleRates.erase( info->sampleRates.begin() ); + else { + for ( int i=info->sampleRates.size()-1; i>=0; i-- ) { + if ( (unsigned int) info->sampleRates[i] > out_caps.dwMaxSecondarySampleRate ) + info->sampleRates.erase( info->sampleRates.begin() + i ); + } + while ( info->sampleRates.size() > 0 && + ((unsigned int) info->sampleRates[0] < out_caps.dwMinSecondarySampleRate) ) { + info->sampleRates.erase( info->sampleRates.begin() ); + } } } @@ -5595,11 +6073,32 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, waveFormat.nBlockAlign = waveFormat.nChannels * waveFormat.wBitsPerSample / 8; waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign; + // Determine the device buffer size. By default, 32k, + // but we will grow it to make allowances for very large softare buffer sizes. + DWORD dsBufferSize = 0; + DWORD dsPointerLeadTime = 0; + + buffer_size = MINIMUM_DEVICE_BUFFER_SIZE; // sound cards will always *knock wood* support this + + + // poisonously large buffer lead time? Then increase the device buffer size accordingly. + while (dsPointerLeadTime *2U > (DWORD)buffer_size) + { + buffer_size *= 2; + } + + + enum_info dsinfo; void *ohandle = 0, *bhandle = 0; strncpy( dsinfo.name, devices_[device].name.c_str(), 64 ); dsinfo.isValid = false; if ( mode == OUTPUT ) { + dsPointerLeadTime = (numberOfBuffers) * + (*bufferSize) * + (waveFormat.wBitsPerSample / 8) + *channels; + if ( devices_[device].maxOutputChannels < channels ) { sprintf(message_, "RtApiDs: requested channels (%d) > than supported (%d) by device (%s).", @@ -5642,7 +6141,7 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, object->Release(); sprintf(message_, "RtApiDs: Unable to set cooperative level (%s): %s.", devices_[device].name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -5659,7 +6158,7 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, object->Release(); sprintf(message_, "RtApiDs: Unable to access primary buffer (%s): %s.", devices_[device].name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -5669,12 +6168,12 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, object->Release(); sprintf(message_, "RtApiDs: Unable to set primary buffer format (%s): %s.", devices_[device].name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } // Setup the secondary DS buffer description. - buffer_size = channels * *bufferSize * nBuffers * waveFormat.wBitsPerSample / 8; + dsBufferSize = (DWORD)buffer_size; ZeroMemory(&bufferDescription, sizeof(DSBUFFERDESC)); bufferDescription.dwSize = sizeof(DSBUFFERDESC); bufferDescription.dwFlags = ( DSBCAPS_STICKYFOCUS | @@ -5695,7 +6194,7 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, object->Release(); sprintf(message_, "RtApiDs: Unable to create secondary DS buffer (%s): %s.", devices_[device].name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } } @@ -5713,7 +6212,7 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, buffer->Release(); sprintf(message_, "RtApiDs: Unable to lock buffer (%s): %s.", devices_[device].name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -5727,7 +6226,7 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, buffer->Release(); sprintf(message_, "RtApiDs: Unable to unlock buffer(%s): %s.", devices_[device].name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -5738,8 +6237,11 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, if ( mode == INPUT ) { - if ( devices_[device].maxInputChannels < channels ) + if ( devices_[device].maxInputChannels < channels ) { + sprintf(message_, "RtAudioDS: device (%s) does not support %d channels.", devices_[device].name.c_str(), channels); + error(RtError::DEBUG_WARNING); return FAILURE; + } // Enumerate through input devices to find the id (if it exists). result = DirectSoundCaptureEnumerate((LPDSENUMCALLBACK)deviceIdCallback, &dsinfo); @@ -5765,12 +6267,12 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, if ( FAILED(result) ) { sprintf(message_, "RtApiDs: Could not create capture object (%s): %s.", devices_[device].name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } // Setup the secondary DS buffer description. - buffer_size = channels * *bufferSize * nBuffers * waveFormat.wBitsPerSample / 8; + dsBufferSize = buffer_size; ZeroMemory(&bufferDescription, sizeof(DSCBUFFERDESC)); bufferDescription.dwSize = sizeof(DSCBUFFERDESC); bufferDescription.dwFlags = 0; @@ -5784,7 +6286,7 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, object->Release(); sprintf(message_, "RtApiDs: Unable to create capture buffer (%s): %s.", devices_[device].name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -5795,7 +6297,7 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, buffer->Release(); sprintf(message_, "RtApiDs: Unable to lock capture buffer (%s): %s.", devices_[device].name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -5809,7 +6311,7 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, buffer->Release(); sprintf(message_, "RtApiDs: Unable to unlock capture buffer (%s): %s.", devices_[device].name.c_str(), getErrorString(result)); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -5824,7 +6326,7 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, else stream_.deviceFormat[mode] = RTAUDIO_SINT16; stream_.nUserChannels[mode] = channels; - *bufferSize = buffer_size / (channels * nBuffers * waveFormat.wBitsPerSample / 8); + stream_.bufferSize = *bufferSize; // Set flags for buffer conversion @@ -5896,6 +6398,8 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, handles = (DsHandle *) stream_.apiHandle; handles[mode].object = ohandle; handles[mode].buffer = bhandle; + handles[mode].dsBufferSize = dsBufferSize; + handles[mode].dsPointerLeadTime = dsPointerLeadTime; stream_.device[mode] = device; stream_.state = STREAM_STOPPED; @@ -5907,6 +6411,49 @@ bool RtApiDs :: probeDeviceOpen( int device, StreamMode mode, int channels, stream_.nBuffers = nBuffers; stream_.sampleRate = sampleRate; + // Setup the buffer conversion information structure. + if ( stream_.doConvertBuffer[mode] ) { + if (mode == INPUT) { // convert device to user buffer + stream_.convertInfo[mode].inJump = stream_.nDeviceChannels[1]; + stream_.convertInfo[mode].outJump = stream_.nUserChannels[1]; + stream_.convertInfo[mode].inFormat = stream_.deviceFormat[1]; + stream_.convertInfo[mode].outFormat = stream_.userFormat; + } + else { // convert user to device buffer + stream_.convertInfo[mode].inJump = stream_.nUserChannels[0]; + stream_.convertInfo[mode].outJump = stream_.nDeviceChannels[0]; + stream_.convertInfo[mode].inFormat = stream_.userFormat; + stream_.convertInfo[mode].outFormat = stream_.deviceFormat[0]; + } + + if ( stream_.convertInfo[mode].inJump < stream_.convertInfo[mode].outJump ) + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].inJump; + else + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].outJump; + + // Set up the interleave/deinterleave offsets. + if ( mode == INPUT && stream_.deInterleave[1] ) { + for (int k=0; kPlay(0, 0, DSBPLAY_LOOPING ); if ( FAILED(result) ) { @@ -6063,6 +6640,9 @@ void RtApiDs :: startStream() } if (stream_.mode == INPUT || stream_.mode == DUPLEX) { + statistics.inputFrameSize = formatBytes( stream_.deviceFormat[1]) + *stream_.nDeviceChannels[1]; + LPDIRECTSOUNDCAPTUREBUFFER buffer = (LPDIRECTSOUNDCAPTUREBUFFER) handles[1].buffer; result = buffer->Start(DSCBSTART_LOOPING ); if ( FAILED(result) ) { @@ -6081,11 +6661,31 @@ void RtApiDs :: stopStream() verifyStream(); if (stream_.state == STREAM_STOPPED) return; + // Change the state before the lock to improve shutdown response // when using a callback. stream_.state = STREAM_STOPPED; MUTEX_LOCK(&stream_.mutex); + + timeEndPeriod(1); // revert to normal scheduler frequency on lesser windows. + +#ifdef GENERATE_DEBUG_LOG + // write the timing log to a .TSV file for analysis in Excel. + unlink("c:/rtaudiolog.txt"); + std::ofstream os("c:/rtaudiolog.txt"); + os << "writeTime\treadDelay\tnextWritePointer\tnextReadPointer\tcurrentWritePointer\tsafeWritePointer\tcurrentReadPointer\tsafeReadPointer" << std::endl; + for (int i = 0; i < currentDebugLogEntry ; ++i) + { + TTickRecord &r = debugLog[i]; + os + << r.writeTime-debugLog[0].writeTime << "\t" << (r.readTime-r.writeTime) << "\t" + << r.nextWritePointer % BUFFER_SIZE << "\t" << r.nextReadPointer % BUFFER_SIZE + << "\t" << r.currentWritePointer % BUFFER_SIZE << "\t" << r.safeWritePointer % BUFFER_SIZE + << "\t" << r.currentReadPointer % BUFFER_SIZE << "\t" << r.safeReadPointer % BUFFER_SIZE << std::endl; + } +#endif + // There is no specific DirectSound API call to "drain" a buffer // before stopping. We can hack this for playback by writing zeroes // for another bufferSize * nBuffers frames. For capture, the @@ -6101,12 +6701,13 @@ void RtApiDs :: stopStream() if (stream_.mode == OUTPUT || stream_.mode == DUPLEX) { DWORD currentPos, safePos; - long buffer_bytes = stream_.bufferSize * stream_.nDeviceChannels[0]; - buffer_bytes *= formatBytes(stream_.deviceFormat[0]); + long buffer_bytes = stream_.bufferSize * stream_.nDeviceChannels[0] + * formatBytes(stream_.deviceFormat[0]); + LPDIRECTSOUNDBUFFER dsBuffer = (LPDIRECTSOUNDBUFFER) handles[0].buffer; - UINT nextWritePos = handles[0].bufferPointer; - dsBufferSize = buffer_bytes * stream_.nBuffers; + long nextWritePos = handles[0].bufferPointer; + dsBufferSize = handles[0].dsBufferSize; // Write zeroes for nBuffer counts. for (int i=0; iStop(); if ( FAILED(result) ) { @@ -6233,8 +6836,7 @@ void RtApiDs :: abortStream() error(RtError::DRIVER_ERROR); } - dsBufferSize = stream_.bufferSize * stream_.nDeviceChannels[0]; - dsBufferSize *= formatBytes(stream_.deviceFormat[0]) * stream_.nBuffers; + dsBufferSize = handles[0].dsBufferSize; // Lock the buffer and clear it so that if we start to play again, // we won't have old data playing. @@ -6272,8 +6874,7 @@ void RtApiDs :: abortStream() error(RtError::DRIVER_ERROR); } - dsBufferSize = stream_.bufferSize * stream_.nDeviceChannels[1]; - dsBufferSize *= formatBytes(stream_.deviceFormat[1]) * stream_.nBuffers; + dsBufferSize = handles[1].dsBufferSize; // Lock the buffer and clear it so that if we start to play again, // we won't have old data playing. @@ -6320,8 +6921,7 @@ int RtApiDs :: streamWillBlock() LPDIRECTSOUNDBUFFER dsBuffer = (LPDIRECTSOUNDBUFFER) handles[0].buffer; UINT nextWritePos = handles[0].bufferPointer; channels = stream_.nDeviceChannels[0]; - DWORD dsBufferSize = stream_.bufferSize * channels; - dsBufferSize *= formatBytes(stream_.deviceFormat[0]) * stream_.nBuffers; + DWORD dsBufferSize = handles[0].dsBufferSize; // Find out where the read and "safe write" pointers are. result = dsBuffer->GetCurrentPosition(¤tPos, &safePos); @@ -6331,18 +6931,23 @@ int RtApiDs :: streamWillBlock() error(RtError::DRIVER_ERROR); } - if ( currentPos < nextWritePos ) currentPos += dsBufferSize; // unwrap offset - frames = currentPos - nextWritePos; + DWORD leadPos = safePos + handles[0].dsPointerLeadTime; + if (leadPos > dsBufferSize) { + leadPos -= dsBufferSize; + } + if ( leadPos < nextWritePos ) leadPos += dsBufferSize; // unwrap offset + + frames = (leadPos - nextWritePos); frames /= channels * formatBytes(stream_.deviceFormat[0]); } - if (stream_.mode == INPUT || stream_.mode == DUPLEX) { + if (stream_.mode == INPUT ) { + // note that we don't block on DUPLEX input anymore. We run lockstep with the write pointer instead. LPDIRECTSOUNDCAPTUREBUFFER dsBuffer = (LPDIRECTSOUNDCAPTUREBUFFER) handles[1].buffer; UINT nextReadPos = handles[1].bufferPointer; channels = stream_.nDeviceChannels[1]; - DWORD dsBufferSize = stream_.bufferSize * channels; - dsBufferSize *= formatBytes(stream_.deviceFormat[1]) * stream_.nBuffers; + DWORD dsBufferSize = handles[1].dsBufferSize; // Find out where the write and "safe read" pointers are. result = dsBuffer->GetCurrentPosition(¤tPos, &safePos); @@ -6352,18 +6957,10 @@ int RtApiDs :: streamWillBlock() error(RtError::DRIVER_ERROR); } - if ( safePos < nextReadPos ) safePos += dsBufferSize; // unwrap offset + if ( safePos < (DWORD)nextReadPos ) safePos += dsBufferSize; // unwrap offset - if (stream_.mode == DUPLEX ) { - // Take largest value of the two. - int temp = safePos - nextReadPos; - temp /= channels * formatBytes(stream_.deviceFormat[1]); - frames = ( temp > frames ) ? temp : frames; - } - else { - frames = safePos - nextReadPos; - frames /= channels * formatBytes(stream_.deviceFormat[1]); - } + frames = (int)(safePos - nextReadPos); + frames /= channels * formatBytes(stream_.deviceFormat[1]); } frames = stream_.bufferSize - frames; @@ -6396,20 +6993,96 @@ void RtApiDs :: tickStream() } HRESULT result; - DWORD currentPos, safePos; + DWORD currentWritePos, safeWritePos; + DWORD currentReadPos, safeReadPos; + DWORD leadPos; + UINT nextWritePos; + +#ifdef GENERATE_DEBUG_LOG + DWORD writeTime, readTime; +#endif LPVOID buffer1 = NULL; LPVOID buffer2 = NULL; DWORD bufferSize1 = 0; DWORD bufferSize2 = 0; + char *buffer; long buffer_bytes; DsHandle *handles = (DsHandle *) stream_.apiHandle; + + if (stream_.mode == DUPLEX && !buffersRolling) + { + assert(handles[0].dsBufferSize == handles[1].dsBufferSize); + + // it takes a while for the devices to get rolling. As a result, there's + // no guarantee that the capture and write device pointers will move in lockstep. + // Wait here for both devices to start rolling, and then set our buffer pointers accordingly. + // e.g. Crystal Drivers: the capture buffer starts up 5700 to 9600 bytes later than the write + // buffer. + + // Stub: a serious risk of having a pre-emptive scheduling round take place between + // the two GetCurrentPosition calls... but I'm really not sure how to solve the problem. + // Temporarily boost to Realtime priority, maybe; but I'm not sure what priority the + // directsound service threads run at. We *should* be roughly within a ms or so of correct. + + LPDIRECTSOUNDBUFFER dsWriteBuffer = (LPDIRECTSOUNDBUFFER) handles[0].buffer; + LPDIRECTSOUNDCAPTUREBUFFER dsCaptureBuffer = (LPDIRECTSOUNDCAPTUREBUFFER) handles[1].buffer; + + + DWORD initialWritePos, initialSafeWritePos; + DWORD initialReadPos, initialSafeReadPos;; + + + result = dsWriteBuffer->GetCurrentPosition(&initialWritePos, &initialSafeWritePos); + if ( FAILED(result) ) { + sprintf(message_, "RtApiDs: Unable to get current position (%s): %s.", + devices_[stream_.device[0]].name.c_str(), getErrorString(result)); + error(RtError::DRIVER_ERROR); + } + result = dsCaptureBuffer->GetCurrentPosition(&initialReadPos, &initialSafeReadPos); + if ( FAILED(result) ) { + sprintf(message_, "RtApiDs: Unable to get current capture position (%s): %s.", + devices_[stream_.device[1]].name.c_str(), getErrorString(result)); + error(RtError::DRIVER_ERROR); + } + while (true) + { + result = dsWriteBuffer->GetCurrentPosition(¤tWritePos, &safeWritePos); + if ( FAILED(result) ) { + sprintf(message_, "RtApiDs: Unable to get current position (%s): %s.", + devices_[stream_.device[0]].name.c_str(), getErrorString(result)); + error(RtError::DRIVER_ERROR); + } + result = dsCaptureBuffer->GetCurrentPosition(¤tReadPos, &safeReadPos); + if ( FAILED(result) ) { + sprintf(message_, "RtApiDs: Unable to get current capture position (%s): %s.", + devices_[stream_.device[1]].name.c_str(), getErrorString(result)); + error(RtError::DRIVER_ERROR); + } + if (safeWritePos != initialSafeWritePos && safeReadPos != initialSafeReadPos) + { + break; + } + Sleep(1); + } + + assert(handles[0].dsBufferSize == handles[1].dsBufferSize); + + UINT writeBufferLead = (safeWritePos-safeReadPos + handles[0].dsBufferSize) % handles[0].dsBufferSize; + buffersRolling = true; + handles[0].bufferPointer = (safeWritePos + handles[0].dsPointerLeadTime); + handles[1].bufferPointer = safeReadPos; + + } + if (stream_.mode == OUTPUT || stream_.mode == DUPLEX) { + + LPDIRECTSOUNDBUFFER dsBuffer = (LPDIRECTSOUNDBUFFER) handles[0].buffer; // Setup parameters and do buffer conversion if necessary. if (stream_.doConvertBuffer[0]) { - convertStreamBuffer(OUTPUT); buffer = stream_.deviceBuffer; + convertBuffer( buffer, stream_.userBuffer, stream_.convertInfo[0] ); buffer_bytes = stream_.bufferSize * stream_.nDeviceChannels[0]; buffer_bytes *= formatBytes(stream_.deviceFormat[0]); } @@ -6421,23 +7094,39 @@ void RtApiDs :: tickStream() // No byte swapping necessary in DirectSound implementation. - LPDIRECTSOUNDBUFFER dsBuffer = (LPDIRECTSOUNDBUFFER) handles[0].buffer; - UINT nextWritePos = handles[0].bufferPointer; - DWORD dsBufferSize = buffer_bytes * stream_.nBuffers; + // Ahhh ... windoze. 16-bit data is signed but 8-bit data is + // unsigned. So, we need to convert our signed 8-bit data here to + // unsigned. + if ( stream_.deviceFormat[0] == RTAUDIO_SINT8 ) + for ( int i=0; iGetCurrentPosition(¤tPos, &safePos); - if ( FAILED(result) ) { - sprintf(message_, "RtApiDs: Unable to get current position (%s): %s.", - devices_[stream_.device[0]].name.c_str(), getErrorString(result)); - error(RtError::DRIVER_ERROR); - } + DWORD dsBufferSize = handles[0].dsBufferSize; + nextWritePos = handles[0].bufferPointer; - if ( currentPos < nextWritePos ) currentPos += dsBufferSize; // unwrap offset - DWORD endWrite = nextWritePos + buffer_bytes; + DWORD endWrite; + while (true) + { + // Find out where the read and "safe write" pointers are. + result = dsBuffer->GetCurrentPosition(¤tWritePos, &safeWritePos); + if ( FAILED(result) ) { + sprintf(message_, "RtApiDs: Unable to get current position (%s): %s.", + devices_[stream_.device[0]].name.c_str(), getErrorString(result)); + error(RtError::DRIVER_ERROR); + } + + leadPos = safeWritePos + handles[0].dsPointerLeadTime; + if (leadPos > dsBufferSize) { + leadPos -= dsBufferSize; + } + if ( leadPos < nextWritePos ) leadPos += dsBufferSize; // unwrap offset + + + endWrite = nextWritePos + buffer_bytes; // Check whether the entire write region is behind the play pointer. - while ( currentPos < endWrite ) { + + if ( leadPos >= endWrite ) break; + // If we are here, then we must wait until the play pointer gets // beyond the write region. The approach here is to use the // Sleep() function to suspend operation until safePos catches @@ -6447,21 +7136,38 @@ void RtApiDs :: tickStream() // A "fudgefactor" less than 1 is used because it was found // that sleeping too long was MUCH worse than sleeping for // several shorter periods. - double millis = (endWrite - currentPos) * 900.0; - millis /= ( formatBytes(stream_.deviceFormat[0]) * stream_.sampleRate); + double millis = (endWrite - leadPos) * 900.0; + millis /= ( formatBytes(stream_.deviceFormat[0]) *stream_.nDeviceChannels[0]* stream_.sampleRate); if ( millis < 1.0 ) millis = 1.0; - Sleep( (DWORD) millis ); - - // Wake up, find out where we are now - result = dsBuffer->GetCurrentPosition( ¤tPos, &safePos ); - if ( FAILED(result) ) { - sprintf(message_, "RtApiDs: Unable to get current position (%s): %s.", - devices_[stream_.device[0]].name.c_str(), getErrorString(result)); - error(RtError::DRIVER_ERROR); + if (millis > 50.0) { + static int nOverruns = 0; + ++nOverruns; } - if ( currentPos < nextWritePos ) currentPos += dsBufferSize; // unwrap offset + Sleep( (DWORD) millis ); + // Sleep( (DWORD) 2); } - +#ifdef GENERATE_DEBUG_LOG + writeTime = timeGetTime(); +#endif + if (statistics.writeDeviceSafeLeadBytes < dsPointerDifference(safeWritePos,currentWritePos,handles[0].dsBufferSize)) + { + statistics.writeDeviceSafeLeadBytes = dsPointerDifference(safeWritePos,currentWritePos,handles[0].dsBufferSize); + } + + if ( + dsPointerBetween(nextWritePos,safeWritePos,currentWritePos,dsBufferSize) + || dsPointerBetween(endWrite,safeWritePos,currentWritePos,dsBufferSize) + ) + { + // we've strayed into the forbidden zone. + // resync the read pointer. + ++statistics.numberOfWriteUnderruns; + nextWritePos = safeWritePos + handles[0].dsPointerLeadTime-buffer_bytes+dsBufferSize; + while (nextWritePos >= dsBufferSize) nextWritePos-= dsBufferSize; + handles[0].bufferPointer = nextWritePos; + endWrite = nextWritePos + buffer_bytes; + } + // Lock free space in the buffer result = dsBuffer->Lock (nextWritePos, buffer_bytes, &buffer1, &bufferSize1, &buffer2, &bufferSize2, 0); @@ -6499,39 +7205,93 @@ void RtApiDs :: tickStream() buffer_bytes = stream_.bufferSize * stream_.nUserChannels[1]; buffer_bytes *= formatBytes(stream_.userFormat); } - LPDIRECTSOUNDCAPTUREBUFFER dsBuffer = (LPDIRECTSOUNDCAPTUREBUFFER) handles[1].buffer; - UINT nextReadPos = handles[1].bufferPointer; - DWORD dsBufferSize = buffer_bytes * stream_.nBuffers; + long nextReadPos = handles[1].bufferPointer; + DWORD dsBufferSize = handles[1].dsBufferSize; // Find out where the write and "safe read" pointers are. - result = dsBuffer->GetCurrentPosition(¤tPos, &safePos); + result = dsBuffer->GetCurrentPosition(¤tReadPos, &safeReadPos); if ( FAILED(result) ) { sprintf(message_, "RtApiDs: Unable to get current capture position (%s): %s.", devices_[stream_.device[1]].name.c_str(), getErrorString(result)); error(RtError::DRIVER_ERROR); } - if ( safePos < nextReadPos ) safePos += dsBufferSize; // unwrap offset + if ( safeReadPos < (DWORD)nextReadPos ) safeReadPos += dsBufferSize; // unwrap offset DWORD endRead = nextReadPos + buffer_bytes; - // Check whether the entire write region is behind the play pointer. - while ( safePos < endRead ) { - // See comments for playback. - double millis = (endRead - safePos) * 900.0; - millis /= ( formatBytes(stream_.deviceFormat[1]) * stream_.sampleRate); - if ( millis < 1.0 ) millis = 1.0; - Sleep( (DWORD) millis ); + // Handling depends on whether we are INPUT or DUPLEX. + // If we're in INPUT mode then waiting is a good thing. If we're in DUPLEX mode, + // then a wait here will drag the write pointers into the forbidden zone. + // + // In DUPLEX mode, rather than wait, we will back off the read pointer until + // it's in a safe position. This causes dropouts, but it seems to be the only + // practical way to sync up the read and write pointers reliably, given the + // the very complex relationship between phase and increment of the read and write + // pointers. + // + // In order to minimize audible dropouts in DUPLEX mode, we will provide a pre-roll + // period of 0.5 seconds + // in which we return zeros from the read buffer while the pointers sync up. + + if (stream_.mode == DUPLEX) + { + if (safeReadPos < endRead) + { + if (duplexPrerollBytes <= 0) + { + // pre-roll time over. Be more agressive. + int adjustment = endRead-safeReadPos; + + ++statistics.numberOfReadOverruns; + // Two cases: + // large adjustments: we've probably run out of CPU cycles, so just resync exactly, + // and perform fine adjustments later. + // small adjustments: back off by twice as much. + if (adjustment >= 2*buffer_bytes) + { + nextReadPos = safeReadPos-2*buffer_bytes; + } else + { + nextReadPos = safeReadPos-buffer_bytes-adjustment; + } + statistics.readDeviceSafeLeadBytes = currentReadPos-nextReadPos; + if (statistics.readDeviceSafeLeadBytes < 0) statistics.readDeviceSafeLeadBytes += dsBufferSize; - // Wake up, find out where we are now - result = dsBuffer->GetCurrentPosition( ¤tPos, &safePos ); - if ( FAILED(result) ) { - sprintf(message_, "RtApiDs: Unable to get current capture position (%s): %s.", - devices_[stream_.device[1]].name.c_str(), getErrorString(result)); - error(RtError::DRIVER_ERROR); + if (nextReadPos < 0) nextReadPos += dsBufferSize; + + } else { + // in pre=roll time. Just do it. + nextReadPos = safeReadPos-buffer_bytes; + while (nextReadPos < 0) nextReadPos += dsBufferSize; + } + endRead = nextReadPos + buffer_bytes; } + } else { + while ( safeReadPos < endRead ) { + // See comments for playback. + double millis = (endRead - safeReadPos) * 900.0; + millis /= ( formatBytes(stream_.deviceFormat[1]) * stream_.nDeviceChannels[1] * stream_.sampleRate); + if ( millis < 1.0 ) millis = 1.0; + Sleep( (DWORD) millis ); + + // Wake up, find out where we are now + result = dsBuffer->GetCurrentPosition( ¤tReadPos, &safeReadPos ); + if ( FAILED(result) ) { + sprintf(message_, "RtApiDs: Unable to get current capture position (%s): %s.", + devices_[stream_.device[1]].name.c_str(), getErrorString(result)); + error(RtError::DRIVER_ERROR); + } - if ( safePos < nextReadPos ) safePos += dsBufferSize; // unwrap offset + if ( safeReadPos < (DWORD)nextReadPos ) safeReadPos += dsBufferSize; // unwrap offset + } + } +#ifdef GENERATE_DEBUG_LOG + readTime = timeGetTime(); +#endif + if (statistics.readDeviceSafeLeadBytes < dsPointerDifference(currentReadPos,nextReadPos ,dsBufferSize)) + { + statistics.readDeviceSafeLeadBytes = dsPointerDifference(currentReadPos,nextReadPos ,dsBufferSize); } // Lock free space in the buffer @@ -6543,9 +7303,16 @@ void RtApiDs :: tickStream() error(RtError::DRIVER_ERROR); } - // Copy our buffer into the DS buffer - CopyMemory(buffer, buffer1, bufferSize1); - if (buffer2 != NULL) CopyMemory(buffer+bufferSize1, buffer2, bufferSize2); + if (duplexPrerollBytes <= 0) + { + // Copy our buffer into the DS buffer + CopyMemory(buffer, buffer1, bufferSize1); + if (buffer2 != NULL) CopyMemory(buffer+bufferSize1, buffer2, bufferSize2); + } else { + memset(buffer,0,bufferSize1); + if (buffer2 != NULL) memset(buffer+bufferSize1,0,bufferSize2); + duplexPrerollBytes -= bufferSize1 + bufferSize2; + } // Update our buffer offset and unlock sound buffer nextReadPos = (nextReadPos + bufferSize1 + bufferSize2) % dsBufferSize; @@ -6557,19 +7324,38 @@ void RtApiDs :: tickStream() } handles[1].bufferPointer = nextReadPos; + // No byte swapping necessary in DirectSound implementation. + // If necessary, convert 8-bit data from unsigned to signed. + if ( stream_.deviceFormat[1] == RTAUDIO_SINT8 ) + for ( int j=0; jstopStream(); } - // Definitions for utility functions and callbacks // specific to the DirectSound implementation. @@ -6687,52 +7473,52 @@ static char* getErrorString(int code) switch (code) { case DSERR_ALLOCATED: - return "Direct Sound already allocated"; + return "Already allocated."; case DSERR_CONTROLUNAVAIL: - return "Direct Sound control unavailable"; + return "Control unavailable."; case DSERR_INVALIDPARAM: - return "Direct Sound invalid parameter"; + return "Invalid parameter."; case DSERR_INVALIDCALL: - return "Direct Sound invalid call"; + return "Invalid call."; case DSERR_GENERIC: - return "Direct Sound generic error"; + return "Generic error."; case DSERR_PRIOLEVELNEEDED: - return "Direct Sound Priority level needed"; + return "Priority level needed"; case DSERR_OUTOFMEMORY: - return "Direct Sound out of memory"; + return "Out of memory"; case DSERR_BADFORMAT: - return "Direct Sound bad format"; + return "The sample rate or the channel format is not supported."; case DSERR_UNSUPPORTED: - return "Direct Sound unsupported error"; + return "Not supported."; case DSERR_NODRIVER: - return "Direct Sound no driver error"; + return "No driver."; case DSERR_ALREADYINITIALIZED: - return "Direct Sound already initialized"; + return "Already initialized."; case DSERR_NOAGGREGATION: - return "Direct Sound no aggregation"; + return "No aggregation."; case DSERR_BUFFERLOST: - return "Direct Sound buffer lost"; + return "Buffer lost."; case DSERR_OTHERAPPHASPRIO: - return "Direct Sound other app has priority"; + return "Another application already has priority."; case DSERR_UNINITIALIZED: - return "Direct Sound uninitialized"; + return "Uninitialized."; default: - return "Direct Sound unknown error"; + return "DirectSound unknown error"; } } @@ -6905,7 +7691,7 @@ void RtApiAl :: probeDeviceInfo(RtApiDevice *info) if (result < 0) { sprintf(message_, "RtApiAl: error getting device (%s) channels: %s.", info->name.c_str(), alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); } else { info->maxOutputChannels = value.i; @@ -6916,7 +7702,7 @@ void RtApiAl :: probeDeviceInfo(RtApiDevice *info) if (result < 0) { sprintf(message_, "RtApiAl: error getting device (%s) rates: %s.", info->name.c_str(), alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); } else { info->sampleRates.clear(); @@ -6939,7 +7725,7 @@ void RtApiAl :: probeDeviceInfo(RtApiDevice *info) if (result < 0) { sprintf(message_, "RtApiAl: error getting device (%s) channels: %s.", info->name.c_str(), alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); } else { info->maxInputChannels = value.i; @@ -6950,7 +7736,7 @@ void RtApiAl :: probeDeviceInfo(RtApiDevice *info) if (result < 0) { sprintf(message_, "RtApiAl: error getting device (%s) rates: %s.", info->name.c_str(), alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); } else { // In the case of the default device, these values will @@ -7007,7 +7793,7 @@ bool RtApiAl :: probeDeviceOpen(int device, StreamMode mode, int channels, if ( !al_config ) { sprintf(message_,"RtApiAl: can't get AL config: %s.", alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -7017,7 +7803,7 @@ bool RtApiAl :: probeDeviceOpen(int device, StreamMode mode, int channels, alFreeConfig(al_config); sprintf(message_,"RtApiAl: can't set %d channels in AL config: %s.", channels, alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -7039,7 +7825,7 @@ bool RtApiAl :: probeDeviceOpen(int device, StreamMode mode, int channels, alFreeConfig(al_config); sprintf(message_,"RtApiAl: can't set buffer size (%ld) in AL config: %s.", buffer_size, alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } *bufferSize = buffer_size / nBuffers; @@ -7078,7 +7864,7 @@ bool RtApiAl :: probeDeviceOpen(int device, StreamMode mode, int channels, alFreeConfig(al_config); sprintf(message_,"RtApiAl: error setting sample format in AL config: %s.", alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -7094,7 +7880,7 @@ bool RtApiAl :: probeDeviceOpen(int device, StreamMode mode, int channels, alFreeConfig(al_config); sprintf(message_,"RtApiAl: error setting device (%s) in AL config: %s.", devices_[device].name.c_str(), alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -7104,7 +7890,7 @@ bool RtApiAl :: probeDeviceOpen(int device, StreamMode mode, int channels, alFreeConfig(al_config); sprintf(message_,"RtApiAl: error opening output port: %s.", alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -7119,7 +7905,7 @@ bool RtApiAl :: probeDeviceOpen(int device, StreamMode mode, int channels, alFreeConfig(al_config); sprintf(message_,"RtApiAl: error setting sample rate (%d) for device (%s): %s.", sampleRate, devices_[device].name.c_str(), alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } } @@ -7135,7 +7921,7 @@ bool RtApiAl :: probeDeviceOpen(int device, StreamMode mode, int channels, alFreeConfig(al_config); sprintf(message_,"RtApiAl: error setting device (%s) in AL config: %s.", devices_[device].name.c_str(), alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -7145,7 +7931,7 @@ bool RtApiAl :: probeDeviceOpen(int device, StreamMode mode, int channels, alFreeConfig(al_config); sprintf(message_,"RtApiAl: error opening input port: %s.", alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } @@ -7160,7 +7946,7 @@ bool RtApiAl :: probeDeviceOpen(int device, StreamMode mode, int channels, alFreeConfig(al_config); sprintf(message_,"RtApiAl: error setting sample rate (%d) for device (%s): %s.", sampleRate, devices_[device].name.c_str(), alGetErrorString(oserror())); - error(RtError::WARNING); + error(RtError::DEBUG_WARNING); return FAILURE; } } @@ -7246,6 +8032,49 @@ bool RtApiAl :: probeDeviceOpen(int device, StreamMode mode, int channels, stream_.bufferSize = *bufferSize; stream_.sampleRate = sampleRate; + // Setup the buffer conversion information structure. + if ( stream_.doConvertBuffer[mode] ) { + if (mode == INPUT) { // convert device to user buffer + stream_.convertInfo[mode].inJump = stream_.nDeviceChannels[1]; + stream_.convertInfo[mode].outJump = stream_.nUserChannels[1]; + stream_.convertInfo[mode].inFormat = stream_.deviceFormat[1]; + stream_.convertInfo[mode].outFormat = stream_.userFormat; + } + else { // convert user to device buffer + stream_.convertInfo[mode].inJump = stream_.nUserChannels[0]; + stream_.convertInfo[mode].outJump = stream_.nDeviceChannels[0]; + stream_.convertInfo[mode].inFormat = stream_.userFormat; + stream_.convertInfo[mode].outFormat = stream_.deviceFormat[0]; + } + + if ( stream_.convertInfo[mode].inJump < stream_.convertInfo[mode].outJump ) + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].inJump; + else + stream_.convertInfo[mode].channels = stream_.convertInfo[mode].outJump; + + // Set up the interleave/deinterleave offsets. + if ( mode == INPUT && stream_.deInterleave[1] ) { + for (int k=0; k offset_in(channels); - std::vector offset_out(channels); - if (mode == INPUT && stream_.deInterleave[1]) { - for (int k=0; k> 16) & 0x0000ffff); + for (j=0; j> 16) & 0x0000ffff); } - in += jump_in; - out += jump_out; + in += info.inJump; + out += info.outJump; } } - else if (format_in == RTAUDIO_SINT32) { - Int32 *in = (Int32 *)input; + else if (info.inFormat == RTAUDIO_SINT32) { + Int32 *in = (Int32 *)inBuffer; for (int i=0; i> 16) & 0x0000ffff); + for (j=0; j> 16) & 0x0000ffff); } - in += jump_in; - out += jump_out; + in += info.inJump; + out += info.outJump; } } - else if (format_in == RTAUDIO_FLOAT32) { - Float32 *in = (Float32 *)input; + else if (info.inFormat == RTAUDIO_FLOAT32) { + Float32 *in = (Float32 *)inBuffer; for (int i=0; i> 8) & 0x00ff); + for (j=0; j> 8) & 0x00ff); } - in += jump_in; - out += jump_out; + in += info.inJump; + out += info.outJump; } } - else if (format_in == RTAUDIO_SINT24) { - Int32 *in = (Int32 *)input; + else if (info.inFormat == RTAUDIO_SINT24) { + Int32 *in = (Int32 *)inBuffer; for (int i=0; i> 24) & 0x000000ff); + for (j=0; j> 24) & 0x000000ff); } - in += jump_in; - out += jump_out; + in += info.inJump; + out += info.outJump; } } - else if (format_in == RTAUDIO_SINT32) { - Int32 *in = (Int32 *)input; + else if (info.inFormat == RTAUDIO_SINT32) { + Int32 *in = (Int32 *)inBuffer; for (int i=0; i> 24) & 0x000000ff); + for (j=0; j> 24) & 0x000000ff); } - in += jump_in; - out += jump_out; + in += info.inJump; + out += info.outJump; } } - else if (format_in == RTAUDIO_FLOAT32) { - Float32 *in = (Float32 *)input; + else if (info.inFormat == RTAUDIO_FLOAT32) { + Float32 *in = (Float32 *)inBuffer; for (int i=0; i inOffset; + std::vector outOffset; }; // A protected structure for audio streams. @@ -183,10 +197,10 @@ protected: RtAudioFormat deviceFormat[2]; // Playback and record, respectively. StreamMutex mutex; CallbackInfo callbackInfo; + ConvertInfo convertInfo[2]; RtApiStream() :apiHandle(0), userBuffer(0), deviceBuffer(0) {} - // mode(UNINITIALIZED), state(STREAM_STOPPED), }; // A protected device structure for audio devices. @@ -217,7 +231,7 @@ protected: typedef float Float32; typedef double Float64; - char message_[256]; + char message_[1024]; int nDevices_; std::vector devices_; RtApiStream stream_; @@ -281,7 +295,7 @@ protected: Protected method used to perform format, channel number, and/or interleaving conversions between the user and device buffers. */ - void convertStreamBuffer( StreamMode mode ); + void convertBuffer( char *outBuffer, char *inBuffer, ConvertInfo &info ); //! Protected common method used to perform byte-swapping on buffers. void byteSwapBuffer( char *buffer, int samples, RtAudioFormat format ); @@ -350,6 +364,20 @@ public: RtAudioFormat format, int sampleRate, int *bufferSize, int numberOfBuffers, RtAudioApi api=UNSPECIFIED ); + //! An overloaded constructor which opens a stream and also returns \c numberOfBuffers parameter via pointer argument. + /*! + See the previous constructor call for details. This overloaded + version differs only in that it takes a pointer argument for the + \c numberOfBuffers parameter and returns the value used by the + audio device (which may be different from that requested). Note + that the \c numberofBuffers parameter is not used with the Linux + Jack, Macintosh CoreAudio, and Windows ASIO APIs. + */ + RtAudio( int outputDevice, int outputChannels, + int inputDevice, int inputChannels, + RtAudioFormat format, int sampleRate, + int *bufferSize, int *numberOfBuffers, RtAudioApi api=UNSPECIFIED ); + //! The destructor. /*! Stops and closes an open stream and devices and deallocates @@ -389,6 +417,20 @@ public: RtAudioFormat format, int sampleRate, int *bufferSize, int numberOfBuffers ); + //! A public method for opening a stream and also returning \c numberOfBuffers parameter via pointer argument. + /*! + See the previous function call for details. This overloaded + version differs only in that it takes a pointer argument for the + \c numberOfBuffers parameter and returns the value used by the + audio device (which may be different from that requested). Note + that the \c numberofBuffers parameter is not used with the Linux + Jack, Macintosh CoreAudio, and Windows ASIO APIs. + */ + void openStream( int outputDevice, int outputChannels, + int inputDevice, int inputChannels, + RtAudioFormat format, int sampleRate, + int *bufferSize, int *numberOfBuffers ); + //! A public method which sets a user-defined callback function for a given stream. /*! This method assigns a callback function to a previously opened @@ -634,6 +676,53 @@ public: void setStreamCallback( RtAudioCallback callback, void *userData ); void cancelStreamCallback(); + public: + // \brief Internal structure that provide debug information on the state of a running DSound device. + struct RtDsStatistics { + // \brief Sample Rate. + long sampleRate; + // \brief The size of one sample * number of channels on the input device. + int inputFrameSize; + // \brief The size of one sample * number of channels on the output device. + int outputFrameSize; + /* \brief The number of times the read pointer had to be adjusted to avoid reading from an unsafe buffer position. + * + * This field is only used when running in DUPLEX mode. INPUT mode devices just wait until the data is + * available. + */ + int numberOfReadOverruns; + // \brief The number of times the write pointer had to be adjusted to avoid writing in an unsafe buffer position. + int numberOfWriteUnderruns; + // \brief Number of bytes by attribute to buffer configuration by which writing must lead the current write pointer. + int writeDeviceBufferLeadBytes; + // \brief Number of bytes by attributable to the device driver by which writing must lead the current write pointer on this output device. + unsigned long writeDeviceSafeLeadBytes; + // \brief Number of bytes by which reading must trail the current read pointer on this input device. + unsigned long readDeviceSafeLeadBytes; + /* \brief Estimated latency in seconds. + * + * For INPUT mode devices, based the latency of the device's safe read pointer, plus one buffer's + * worth of additional latency. + * + * For OUTPUT mode devices, the latency of the device's safe write pointer, plus N buffers of + * additional buffer latency. + * + * For DUPLEX devices, the sum of latencies for both input and output devices. DUPLEX devices + * also back off the read pointers an additional amount in order to maintain synchronization + * between out-of-phase read and write pointers. This time is also included. + * + * Note that most software packages report latency between the safe write pointer + * and the software lead pointer, excluding the hardware device's safe write pointer + * latency. Figures of 1 or 2ms of latency on Windows audio devices are invariably of this type. + * The reality is that hardware devices often have latencies of 30ms or more (often much + * higher for duplex operation). + */ + + double latency; + }; + // \brief Report on the current state of a running DSound device. + static RtDsStatistics getDsStatistics(); + private: void initialize(void); @@ -641,6 +730,12 @@ public: bool probeDeviceOpen( int device, StreamMode mode, int channels, int sampleRate, RtAudioFormat format, int *bufferSize, int numberOfBuffers ); + + bool coInitialized; + bool buffersRolling; + long duplexPrerollBytes; + static RtDsStatistics statistics; + }; #endif @@ -674,6 +769,9 @@ public: bool probeDeviceOpen( int device, StreamMode mode, int channels, int sampleRate, RtAudioFormat format, int *bufferSize, int numberOfBuffers ); + + bool coInitialized; + }; #endif diff --git a/RtError.h b/RtError.h index 465cd75..cb1283d 100644 --- a/RtError.h +++ b/RtError.h @@ -39,7 +39,7 @@ protected: public: //! The constructor. - RtError(const std::string& message, Type type = RtError::UNSPECIFIED) : message_(message), type_(type){} + RtError(const std::string& message, Type type = RtError::UNSPECIFIED) : message_(message), type_(type) {} //! The destructor. virtual ~RtError(void) {}; diff --git a/configure.ac b/configure.ac index 0ad7b37..83723fd 100644 --- a/configure.ac +++ b/configure.ac @@ -80,7 +80,6 @@ case $host in [AC_SUBST( audio_apis, [-D__MACOSX_CORE__] )], [AC_MSG_ERROR(CoreAudio header files not found!)] ) AC_SUBST( frameworks, ["-framework CoreAudio"] ) - AC_CHECK_LIB(stdc++, printf, , AC_MSG_ERROR(RtAudio requires the C++ library!) ) ;; *) diff --git a/doc/doxygen/footer.html b/doc/doxygen/footer.html index 888ebe6..d05cf27 100644 --- a/doc/doxygen/footer.html +++ b/doc/doxygen/footer.html @@ -1,7 +1,7 @@
-
©2001-2004 Gary P. Scavone, McGill University. All Rights Reserved.
+
©2001-2005 Gary P. Scavone, McGill University. All Rights Reserved.
Maintained by Gary P. Scavone, gary@music.mcgill.ca
diff --git a/doc/doxygen/tutorial.txt b/doc/doxygen/tutorial.txt index 9b45bbc..2d79952 100644 --- a/doc/doxygen/tutorial.txt +++ b/doc/doxygen/tutorial.txt @@ -1,8 +1,6 @@ /*! \mainpage The RtAudio Tutorial - - -
\ref intro    \ref changes   \ref download    \ref start    \ref error    \ref probing    \ref settings    \ref playbackb    \ref playbackc    \ref recording    \ref duplex    \ref multi    \ref methods    \ref compiling    \ref debug    \ref apinotes    \ref acknowledge    \ref license
+
\ref intro    \ref changes   \ref download    \ref start    \ref error    \ref probing    \ref settings    \ref playbackb    \ref playbackc    \ref recording    \ref duplex    \ref multi    \ref methods    \ref compiling    \ref debug    \ref apinotes    \ref wishlist    \ref acknowledge    \ref license
\section intro Introduction @@ -38,7 +36,7 @@ The RtError class declaration and definition have been extracted to a separate f \section download Download -Latest Release (22 March 2004): Version 3.0.1 (200 kB tar/gzipped) +Latest Release (14 October 2005): Version 3.0.2 \section start Getting Started @@ -52,7 +50,7 @@ The first thing that must be done when using RtAudio is to create an instance of int main() { - RtAudio *audio; + RtAudio *audio = 0; // Default RtAudio constructor try { @@ -89,7 +87,7 @@ A programmer may wish to query the available audio device capabilities before de int main() { - RtAudio *audio; + RtAudio *audio = 0; // Default RtAudio constructor try { @@ -176,7 +174,7 @@ int main() int bufferSize = 256; // 256 sample frames int nBuffers = 4; // number of internal buffers used by device int device = 0; // 0 indicates the default or first available device - RtAudio *audio; + RtAudio *audio = 0; // Instantiate RtAudio and open a stream within a try/catch block try { @@ -230,7 +228,7 @@ int main() int nBuffers = 4; // number of internal buffers used by device float *buffer; int device = 0; // 0 indicates the default or first available device - RtAudio *audio; + RtAudio *audio = 0; // Open a stream during RtAudio instantiation try { @@ -333,7 +331,7 @@ int main() int device = 0; // 0 indicates the default or first available device double data[2]; char input; - RtAudio *audio; + RtAudio *audio = 0; // Open a stream during RtAudio instantiation try { @@ -401,7 +399,7 @@ int main() int nBuffers = 4; // number of internal buffers used by device float *buffer; int device = 0; // 0 indicates the default or first available device - RtAudio *audio; + RtAudio *audio = 0; // Instantiate RtAudio and open a stream. try { @@ -496,7 +494,7 @@ int main() int nBuffers = 4; // number of internal buffers used by device int device = 0; // 0 indicates the default or first available device char input; - RtAudio *audio; + RtAudio *audio = 0; // Open a stream during RtAudio instantiation try { @@ -683,17 +681,51 @@ The Steinberg ASIO audio API is based on a callback scheme. In addition, the AP A number of ASIO source and header files are required for use with RtAudio. Specifically, an RtAudio project must include the following files: asio.h,cpp; asiodrivers.h,cpp; asiolist.h,cpp; asiodrvr.h; asiosys.h; ginclude.h; iasiodrv.h. The Visual C++ projects found in /tests/Windows/ compile both ASIO and DirectSound support. +\section wishlist Possible Future Changes -\section acknowledge Acknowledgements +There are a few issues that still need to be addressed in future versions of RtAudio, including: -The RtAudio API incorporates many of the concepts developed in the PortAudio project by Phil Burk and Ross Bencina. Early development also incorporated ideas from Bill Schottstaedt's sndlib. The CCRMA SoundWire group provided valuable feedback during the API proposal stages. +
    +
  • Provide a mechanism so the user can "pre-fill" audio output buffers to allow precise measurement of an acoustic response;
  • +
  • Allow the user to read / write non-interleaved data to / from the audio buffer;
  • +
  • Further support in Windows OS for multi-channel (>2) input / output. This is currently only possible with ASIO interface (in large part due to limitations with the DirectSound API). But perhaps a port to the WinMM API should be investigated?
  • +
  • Investigate the possibility of allowing the user to select specific channels of a soundcard. For example, if an audio device supports 8 channels and the user wishes to send data out channels 7-8 only, it is currently necessary to open all 8 channels and write the two channels of output data to the correct positions in each audio frame of an interleaved data buffer.
  • +
+ +\section acknowledge Acknowledgements -The early 2.0 version of RtAudio was slowly developed over the course of many months while in residence at the Institut Universitari de L'Audiovisual (IUA) in Barcelona, Spain and the Laboratory of Acoustics and Audio Signal Processing at the Helsinki University of Technology, Finland. Much subsequent development happened while working at the Center for Computer Research in Music and Acoustics (CCRMA) at Stanford University. The most recent version of RtAudio was finished while working as an assistant professor of Music Technology at McGill University. This work was supported in part by the United States Air Force Office of Scientific Research (grant \#F49620-99-1-0293). +Thanks to Robin Davies for a number of bug fixes and improvements to +the DirectSound and ASIO implementations in the 3.0.2 release! + +The RtAudio API incorporates many of the concepts developed in the PortAudio project by Phil Burk +and Ross Bencina. Early development also incorporated ideas from Bill +Schottstaedt's sndlib. +The CCRMA SoundWire +group provided valuable feedback during the API proposal stages. + +The early 2.0 version of RtAudio was slowly developed over the course +of many months while in residence at the Institut Universitari de L'Audiovisual +(IUA) in Barcelona, Spain and the Laboratory of Acoustics and Audio +Signal Processing at the Helsinki University of Technology, +Finland. Much subsequent development happened while working at the Center for Computer Research in +Music and Acoustics (CCRMA) at Stanford University. The most +recent version of RtAudio was finished while working as an assistant +professor of Music +Technology at McGill +University. This work was supported in part by the United States +Air Force Office of Scientific Research (grant \#F49620-99-1-0293). \section license License RtAudio: a realtime audio i/o C++ classes
- Copyright (c) 2001-2004 Gary P. Scavone + Copyright (c) 2001-2005 Gary P. Scavone Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files diff --git a/doc/release.txt b/doc/release.txt index ad9c8e9..ba72cc3 100644 --- a/doc/release.txt +++ b/doc/release.txt @@ -1,6 +1,20 @@ RtAudio - a set of C++ classes which provide a common API for realtime audio input/output across Linux (native ALSA, JACK, and OSS), SGI, Macintosh OS X (CoreAudio), and Windows (DirectSound and ASIO) operating systems. -By Gary P. Scavone, 2001-2004. +By Gary P. Scavone, 2001-2005. + +v3.0.2: (14 October 2005) +- modification of ALSA read/write order to fix duplex under/overruns +- added synchronization of input/output devices for ALSA duplex operation +- cleaned up and improved error reporting throughout +- bug fix in Windows DirectSound support for 8-bit audio +- bug fix in Windows DirectSound support during device capture query +- added ASIOOutputReady() call near end of callbackEvent to fix some driver behavior +- added #include to RtAudio.cpp +- fixed bug in RtApiCore for duplex operation with different I/O devices +- improvements to DirectX pointer chasing (by Robin Davies) +- backdoor RtDsStatistics hook provides DirectX performance information (by Robin Davies) +- bug fix for non-power-of-two Asio granularity used by Edirol PCR-A30 (by Robin Davies) +- auto-call CoInitialize for DSOUND and ASIO platforms (by Robin Davies) v3.0.1: (22 March 2004) - bug fix in Windows DirectSound support for cards with output only diff --git a/install b/install index 7aab632..b2f086b 100644 --- a/install +++ b/install @@ -1,6 +1,6 @@ RtAudio - a set of C++ classes which provide a common API for realtime audio input/output across Linux (native ALSA, JACK, and OSS), SGI, Macintosh OS X (CoreAudio), and Windows (DirectSound and ASIO) operating systems. -By Gary P. Scavone, 2001-2004. +By Gary P. Scavone, 2001-2005. To configure and compile (on Unix systems): @@ -18,7 +18,7 @@ A few options can be passed to configure, including: --with-jack = choose JACK server support (linux only) --with-oss = choose OSS API support (linux only) -Typing "./configure --help" will display all the available options. +Typing "./configure --help" will display all the available options. Note that you can provide more than one "--with-" flag to the configure script to enable multiple API support. If you wish to use a different compiler than that selected by configure, specify that compiler in the command line (ex. to use CC): diff --git a/readme b/readme index ae40510..1202532 100644 --- a/readme +++ b/readme @@ -1,6 +1,6 @@ RtAudio - a set of C++ classes which provide a common API for realtime audio input/output across Linux (native ALSA, JACK, and OSS), SGI, Macintosh OS X (CoreAudio), and Windows (DirectSound and ASIO) operating systems. -By Gary P. Scavone, 2001-2004. +By Gary P. Scavone, 2001-2005. This distribution of RtAudio contains the following: @@ -38,7 +38,7 @@ LEGAL AND ETHICAL: The RtAudio license is similar to the the MIT License, with the added "feature" that modifications be sent to the developer. RtAudio: a set of realtime audio i/o C++ classes - Copyright (c) 2001-2004 Gary P. Scavone + Copyright (c) 2001-2005 Gary P. Scavone Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files diff --git a/tests/Windows/rtaudiotest/Release/.placeholder b/tests/Windows/rtaudiotest/Release/.placeholder new file mode 100644 index 0000000..e69de29 diff --git a/tests/Windows/rtaudiotest/StdOpt.cpp b/tests/Windows/rtaudiotest/StdOpt.cpp new file mode 100644 index 0000000..02c8eea --- /dev/null +++ b/tests/Windows/rtaudiotest/StdOpt.cpp @@ -0,0 +1,91 @@ +/************************************************************************/ +/*! \class CommandLine + \brief Command-line option parser. + + Copyright (c) 2005 Robin Davies. + + 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. + + 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. +*/ +/************************************************************************/ + +#include ".\stdopt.h" + +using namespace stdopt; + +CommandLine::CommandLine() +{ +} + +void CommandLine::ProcessCommandLine(int argc, char**argv) +{ + std::vector cmdline; + for (int i = 0; i < argc; ++i) { + cmdline.push_back(argv[i]); + } + ProcessCommandLine(cmdline); +} + +const CommandLine::COptionHandlerBase*CommandLine::GetOptionHandler(const std::string &name) const +{ + // Return excact matches only. + for (size_t i = 0; i < optionHandlers.size(); ++i) + { + if (optionHandlers[i]->getName() == name) { + return (optionHandlers[i]); + } + } + return NULL; +} + +void CommandLine::ProcessCommandLine(const std::vector& cmdline) +{ + for (size_t i = 1; i < cmdline.size(); ++i) + { + if (cmdline[i].length() != 0 && cmdline[i][0] == L'/' || (cmdline[i][0] == '-')) { + std::string arg = cmdline[i].substr(1); + const COptionHandlerBase *pHandler = GetOptionHandler(arg); + if (pHandler == NULL) { + throw CommandLineException(std::string("Unknown option: ") + arg); + } + if (pHandler->HasArgument()) + { + std::string strArg; + if (i+1 < cmdline.size()) { + ++i; + strArg = cmdline[i]; + } + pHandler->Process(strArg.c_str()); + } else { + pHandler->Process(NULL); + } + } else { + args.push_back(cmdline[i]); + } + } +} + +CommandLine::~CommandLine(void) +{ + for (size_t i = 0; i < optionHandlers.size(); ++i) + { + delete optionHandlers[i]; + } + optionHandlers.resize(0); +} diff --git a/tests/Windows/rtaudiotest/StdOpt.h b/tests/Windows/rtaudiotest/StdOpt.h new file mode 100644 index 0000000..4de660a --- /dev/null +++ b/tests/Windows/rtaudiotest/StdOpt.h @@ -0,0 +1,186 @@ +/************************************************************************/ +/*! \class CommandLine + \brief Command-line opition parser. + + Copyright (c) 2005 Robin Davies. + + 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. + + 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. +*/ +/************************************************************************/ + +#ifndef STDOPT_H +#define STDOPT_H + +#include +#include +#include +#include + +namespace stdopt +{ + + class CommandLineException: public std::exception { + public: + CommandLineException(const std::string &error) + { + s = error; + } + const char*what() { return s.c_str(); } + private: + std::string s; + }; + + class CommandLine + { + public: + CommandLine(); + virtual ~CommandLine(); + + void ProcessCommandLine(int argc, char**argv); + void ProcessCommandLine(const std::vector& cmdline); + + template void AddOption(const char*name,TVAL*pResult, TVAL defaultValue) + { + this->optionHandlers.push_back(new COptionHandler(name,pResult)); + *pResult = defaultValue; + + } + template void AddOption(const char*name,TVAL*pResult) + { + this->optionHandlers.push_back(new COptionHandler(name,pResult)); + } + const std::vector &GetArguments() { return args; } + template void GetArgument(size_t arg, T*pVal) + { + if (arg >= args.size()) { + std::stringstream os; + os << "Argument " << (arg+1) << " not provided."; + throw CommandLineException(os.str()); + } + std::stringstream is(args[arg]); + T value; + is >> value; + if (!is.fail() && is.eof()) + { + *pVal = value; + } else { + std::stringstream os; + os << "Argument " << (arg+1) << " was not in the correct format."; + throw CommandLineException(os.str()); + } + } + void GetArgument(size_t arg, std::string*pVal) + { + if (arg >= args.size()) { + std::stringstream os; + os << "Argument " << (arg+1) << " not provided."; + throw CommandLineException(os.str()); + } + *pVal = args[arg]; + } + + + private: + + class COptionHandlerBase { + public: + COptionHandlerBase(const std::string & name) { this->name = name;} + virtual ~COptionHandlerBase() { }; + const std::string &getName() const { return name; } + virtual bool HasArgument()const = 0; + virtual void Process(const char* value) const = 0; + private: + std::string name; + }; + template class COptionHandler: public COptionHandlerBase { + public: + COptionHandler(const std::string &name,T *result, T defaultValue = T()) + : COptionHandlerBase(name) + { + _pResult = result; + *_pResult = defaultValue; + + } + virtual bool HasArgument() const; + + virtual void Process(const char *strValue) const { + std::stringstream is(strValue); + T value; + is >> value; + if (!is.fail() && is.eof()) + { + *_pResult = value; + } else { + std::stringstream os; + os << "Invalid value provided for option '" << getName() << "'."; + throw CommandLineException(os.str().c_str()); + } + } + + private: + std::string strName; + T*_pResult; + }; + const CommandLine::COptionHandlerBase*GetOptionHandler(const std::string &name) const; + + std::vector args; + std::vector optionHandlers; + }; + + // Argument specializations for bool. + template bool CommandLine::COptionHandler::HasArgument()const { + return true; + }; + inline bool CommandLine::COptionHandler::HasArgument() const { + return false; + } + inline void CommandLine::COptionHandler::Process(const char*strValue) const { + if (strValue == NULL) + { + *_pResult = true; + return; + } + switch (*strValue) + { + case '\0': + case '+': + *_pResult = true; + break; + case '-': + *_pResult = false; + break; + default: + throw CommandLineException("Please specify '+' or '-' for boolean options."); + } + } + // specializations for std::string. + inline void CommandLine::COptionHandler::Process(const char*strValue)const { + *_pResult = strValue; + } + + // specializations for std::vector + inline void CommandLine::COptionHandler >::Process(const char*strValue)const { + _pResult->push_back(strValue); + } +}; + + + +#endif diff --git a/tests/Windows/rtaudiotest/readme.txt b/tests/Windows/rtaudiotest/readme.txt new file mode 100644 index 0000000..d883384 --- /dev/null +++ b/tests/Windows/rtaudiotest/readme.txt @@ -0,0 +1,8 @@ +rtaudiotest - Test rtaudio devices. + +rtaudiotest is a small tool that allows easy testing of rtaudio devices +and configurations. + +Run rtaudiotest.exe for a description of how to run it. + +rtaudiotest is currently only supported for the Windows platform. diff --git a/tests/Windows/rtaudiotest/rtaudiotest.cpp b/tests/Windows/rtaudiotest/rtaudiotest.cpp new file mode 100644 index 0000000..90d8c2c --- /dev/null +++ b/tests/Windows/rtaudiotest/rtaudiotest.cpp @@ -0,0 +1,386 @@ +/************************************************************************/ +/*! \brief Interactively Test RtAudio parameters. + + RtAudio is a command-line utility that allows users to enumerate + installed devices, and to test input, output and duplex operation + of RtAudio devices with various buffer and buffer-size + configurations. + + Copyright (c) 2005 Robin Davies. + + 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. + + 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. +*/ +/************************************************************************/ + +#include "RtAudio.h" +#include "FileWvOut.h" + +#include +#include +#include + +using namespace std; +using namespace stdopt; + +#ifdef _WIN32 +// Correct windows.h standards violation. +#undef min +#undef max +#endif + +#define DSOUND 1 + +RtAudio::RtAudioApi rtApi = RtAudio::WINDOWS_DS; + +void DisplayHelp(std::ostream &os) +{ + os + << "rtaudiotest - Test rtaudio devices." << endl + << endl + << "Syntax:" << endl + << " rtaudiotest [options]* enum" << endl + << " - Display installed devices." << endl + << " rtaudiotest [options]* inputtest []" << endl + << " - Capture audio to a .wav file." << endl + << " rtaudiotest [options]* outputtest " << endl + << " - Generate a test signal on the device.." << endl + << " rtaudiotest [options]* duplextest " << endl + << " - Echo input to output." << endl + + << "Options:" << endl + << " -h -? Display this message." << endl + << " -dsound Use DirectX drivers." << endl + << " -asio Use ASIO drivers." << endl + << " -buffers N Use N buffers." << endl + << " -size N Use buffers of size N." << endl + << " -srate N Use a sample-rate of N (defaults to 44100)." << endl + << " -channels N Use N channels (defaults to 2)." << endl + << " -seconds N Run the test for N seconds (default 5)." << endl + << "Description: " << endl + << " RtAudio is a command-line utility that allows users to enumerate " << endl + << " installed devices, and to test input, output and duplex operation " << endl + << " of RtAudio devices with various buffer and buffer-size " << endl + << " configurations." << endl + << "Examples:" << endl + << " rtaudio -asio enum" << endl + << " rtaudio -dsound -buffers 4 -size 128 -seconds 3 inputtest 0 test.wav" << endl + ; + +} + +void EnumerateDevices(RtAudio::RtAudioApi api) +{ + RtAudio rt(api); + for (int i = 1; i <= rt.getDeviceCount(); ++i) + { + RtAudioDeviceInfo info = rt.getDeviceInfo(i); + cout << "Device " << i << ": " << info.name << endl; + } +} + +struct TestConfiguration +{ + long srate; + int channels; + int bufferSize; + int buffers; + int seconds; +}; + + +bool DisplayStats(RtAudio::RtAudioApi api) +{ +#ifdef __WINDOWS_DS__ + // Display latency results for Windows DSound drivers. + if (api == RtAudio::WINDOWS_DS) + { + RtApiDs::RtDsStatistics s = RtApiDs::getDsStatistics(); + + cout << " Latency: " << s.latency*1000.0 << "ms" << endl; + if (s.inputFrameSize) + { + cout << " Read overruns: " << s.numberOfReadOverruns << endl; + } + if (s.outputFrameSize) + { + cout << " Write underruns: " << s.numberOfWriteUnderruns << endl; + } + + if (s.inputFrameSize) + { + cout << " Read lead time in sample frames (device): " << s.readDeviceSafeLeadBytes/ s.inputFrameSize << endl; + } + if (s.outputFrameSize) + { + cout << " Write lead time in sample frames (device): " << s.writeDeviceSafeLeadBytes / s.outputFrameSize << endl; + cout << " Write lead time in sample frames (buffer): " << s.writeDeviceBufferLeadBytes / s.outputFrameSize << endl; + + } + + } +#endif + return true; +} + +void InputTest( RtAudio::RtAudioApi api, + int inputDevice, + const std::string &fileName, + TestConfiguration &configuration ) +{ + RtAudio rt(api); + + int bufferSize = configuration.bufferSize; + + RtAudioDeviceInfo info = rt.getDeviceInfo(inputDevice); + cout << "Reading from device " << inputDevice << " (" << info.name << ")\n"; + + rt.openStream(0,0,inputDevice,configuration.channels, RTAUDIO_SINT16, configuration.srate,&bufferSize,configuration.buffers); + if (bufferSize != configuration.bufferSize) + { + cout << "The buffer size was changed to " << bufferSize << " by the device." << endl; + configuration.bufferSize = bufferSize; + + } + + int nTicks = (int)ceil((configuration.srate* configuration.seconds)*1.0/configuration.bufferSize); + + if (fileName.length() == 0) + { + // just run the stream. + rt.startStream(); + for (int i = 0; i < nTicks; ++i) + { + rt.tickStream(); + } + rt.stopStream(); + } else + { + if (configuration.seconds > 10) { + throw CommandLineException("Capture of more than 10 seconds of data is not supported."); + } + std::vector data; + // we could be smarter, but the point here is to capture data without interfering with the stream. + // File writes while ticking the stream is not cool. + data.resize(nTicks*configuration.bufferSize*configuration.channels); // potentially very big. That's why we restrict capture to 10 seconds. + short *pData = &data[0]; + + rt.startStream(); + int i; + for (i = 0; i < nTicks; ++i) + { + rt.tickStream(); + short *streamBuffer = (short*)rt.getStreamBuffer(); + for (int samples = 0; samples < configuration.bufferSize; ++samples) + { + for (int channel = 0; channel < configuration.channels; ++channel) + { + *pData ++ = *streamBuffer++; + } + } + } + rt.stopStream(); + remove(fileName.c_str()); + FileWvOut wvOut; + wvOut.openFile( fileName.c_str(), configuration.channels, FileWrite::FILE_WAV ); + + StkFrames frame(1,configuration.channels,false); + pData = &data[0]; + + for (i = 0; i < nTicks; ++i) { + for (int samples = 0; samples < configuration.bufferSize; ++samples) { + for (int channel = 0; channel < configuration.channels; ++channel) { + frame[channel] = (float)( *pData++*( 1.0 / 32768.0 ) ); + } + wvOut.tickFrame(frame); + } + } + wvOut.closeFile(); + } + rt.closeStream(); + + if (DisplayStats(api)) { + cout << "Test succeeded." << endl; + } +} + +void OutputTest( RtAudio::RtAudioApi api, + int outputDevice, + TestConfiguration &configuration ) +{ + RtAudio rt(api); + int bufferSize = configuration.bufferSize; + + RtAudioDeviceInfo info = rt.getDeviceInfo(outputDevice); + cout << "Writing to " << info.name << "...\n"; + + rt.openStream(outputDevice,configuration.channels, 0,0, RTAUDIO_SINT16, configuration.srate,&bufferSize,configuration.buffers); + if (bufferSize != configuration.bufferSize) { + cout << "The buffer size was changed to " << bufferSize << " by the device." << endl; + configuration.bufferSize = bufferSize; + } + + rt.startStream(); + short *pBuffer = (short*)rt.getStreamBuffer(); + int nTicks = (int)ceil((configuration.srate* configuration.seconds)*1.0/configuration.bufferSize); + + double phase = 0; + double deltaPhase = 880.0/configuration.srate; + for (int i = 0; i < nTicks; ++i) { + short *p = pBuffer; + for (int samp = 0; samp < configuration.bufferSize; ++samp) { + short val = (short)(sin(phase)*(32768/4)); // sin()*0.25 magnitude. Audible, but not damaging to ears or speakers. + phase += deltaPhase; + + for (int chan = 0; chan < configuration.channels; ++chan) { + *p++ = val; + } + } + rt.tickStream(); + } + rt.stopStream(); + rt.closeStream(); + + if ( DisplayStats(api) ) { + cout << "Test succeeded." << endl; + } +} + +void DuplexTest( RtAudio::RtAudioApi api, + int inputDevice, + int outputDevice, + TestConfiguration &configuration ) +{ + RtAudio rt(api); + int bufferSize = configuration.bufferSize; + + RtAudioDeviceInfo info = rt.getDeviceInfo(inputDevice); + cout << "Reading from " << info.name << ", " << endl; + info = rt.getDeviceInfo(outputDevice); + cout << "Writing to " << info.name << "..." << endl; + + rt.openStream(outputDevice,configuration.channels, inputDevice,configuration.channels, RTAUDIO_SINT16, configuration.srate,&bufferSize,configuration.buffers); + if (bufferSize != configuration.bufferSize) + { + cout << "The buffer size was changed to " << bufferSize << " by the device." << endl; + configuration.bufferSize = bufferSize; + } + + rt.startStream(); + short *pBuffer = (short*)rt.getStreamBuffer(); + int nTicks = (int)ceil((configuration.srate* configuration.seconds)*1.0/configuration.bufferSize); + + for (int i = 0; i < nTicks; ++i) { + rt.tickStream(); + } + rt.stopStream(); + rt.closeStream(); + + if ( DisplayStats(api) ) { + cout << "Test succeeded." << endl; + } +} + +int main(int argc, char **argv) +{ + try + { + CommandLine commandLine; + + TestConfiguration configuration; + bool displayHelp; + bool useDsound; + bool useAsio; + + commandLine.AddOption("h",&displayHelp); + commandLine.AddOption("?",&displayHelp); + commandLine.AddOption("dsound",&useDsound); + commandLine.AddOption("asio",&useAsio); + commandLine.AddOption("srate",&configuration.srate,44100L); + commandLine.AddOption("channels",&configuration.channels,2); + commandLine.AddOption("seconds",&configuration.seconds,5); + commandLine.AddOption("buffers",&configuration.buffers,2); + commandLine.AddOption("size",&configuration.bufferSize,128); + + commandLine.ProcessCommandLine(argc,argv); + + if (displayHelp || commandLine.GetArguments().size() == 0) + { + DisplayHelp(cout); + return 0; + } + if (useDsound) + { + rtApi = RtAudio::WINDOWS_DS; + } else if (useAsio) + { + rtApi = RtAudio::WINDOWS_ASIO; + } else { + throw CommandLineException("Please specify an API to use: '-dsound', or '-asio'"); + } + + std::string testName; + commandLine.GetArgument(0,&testName); + if (testName == "enum") + { + EnumerateDevices(rtApi); + } else if (testName == "inputtest") + { + int inputDevice; + std::string fileName; + commandLine.GetArgument(1,&inputDevice); + if (commandLine.GetArguments().size() >= 2) + { + commandLine.GetArgument(2,&fileName); + } + InputTest(rtApi,inputDevice,fileName,configuration); + } else if (testName == "outputtest") + { + int inputDevice; + commandLine.GetArgument(1,&inputDevice); + OutputTest(rtApi,inputDevice,configuration); + } else if (testName == "duplextest") + { + int inputDevice; + int outputDevice; + commandLine.GetArgument(1,&inputDevice); + commandLine.GetArgument(2,&outputDevice); + DuplexTest(rtApi,inputDevice,outputDevice,configuration); + } else { + throw CommandLineException("Not a valid test name."); + } + + } catch (CommandLineException &e) + { + cerr << e.what() << endl << endl; + cerr << "Run 'rtaudiotest -h' to see the commandline syntax." << endl; + return 3; + } catch (RtError &e) + { + cerr << e.getMessage() << endl; + return 3; + + } catch (std::exception &e) + { + cerr << "Error: " << e.what() << endl; + return 3; + + } + return 0; +} diff --git a/tests/Windows/rtaudiotest/rtaudiotest.dsp b/tests/Windows/rtaudiotest/rtaudiotest.dsp new file mode 100644 index 0000000..519756b --- /dev/null +++ b/tests/Windows/rtaudiotest/rtaudiotest.dsp @@ -0,0 +1,146 @@ +# Microsoft Developer Studio Project File - Name="rtaudiotest" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Console Application" 0x0103 + +CFG=rtaudiotest - Win32 Debug +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "rtaudiotest.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "rtaudiotest.mak" CFG="rtaudiotest - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "rtaudiotest - Win32 Release" (based on "Win32 (x86) Console Application") +!MESSAGE "rtaudiotest - Win32 Debug" (based on "Win32 (x86) Console Application") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +RSC=rc.exe + +!IF "$(CFG)" == "rtaudiotest - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c +# ADD CPP /nologo /MT /W3 /GX /Zi /O2 /I "..\..\include" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /D "__WINDOWS_DS__" /D "__WINDOWS_ASIO__" /D "__LITTLE_ENDIAN__" /YX /FD /c +# ADD BASE RSC /l 0x409 /d "NDEBUG" +# ADD RSC /l 0x409 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib dsound.lib winmm.lib /nologo /subsystem:console /debug /machine:I386 + +!ELSEIF "$(CFG)" == "rtaudiotest - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c +# ADD CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /I "..\..\include" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "__WINDOWS_DS__" /D "__WINDOWS_ASIO__" /D "__LITTLE_ENDIAN__" /FR /YX /FD /GZ /c +# ADD BASE RSC /l 0x409 /d "_DEBUG" +# ADD RSC /l 0x409 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib dsound.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept + +!ENDIF + +# Begin Target + +# Name "rtaudiotest - Win32 Release" +# Name "rtaudiotest - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=..\..\src\asio\asio.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\src\asio\asiodrivers.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\src\asio\asiolist.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\src\RtAudio.cpp +# End Source File +# Begin Source File + +SOURCE=.\rtaudiotest.cpp +# End Source File +# Begin Source File + +SOURCE=.\StdOpt.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\src\Stk.cpp +# End Source File +# Begin Source File + +SOURCE=..\..\src\WvOut.cpp +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=..\..\include\RtAudio.h +# End Source File +# Begin Source File + +SOURCE=.\StdOpt.h +# End Source File +# Begin Source File + +SOURCE=..\..\include\Stk.h +# End Source File +# Begin Source File + +SOURCE=..\..\include\WvOut.h +# End Source File +# End Group +# Begin Group "Resource Files" + +# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" +# End Group +# End Target +# End Project diff --git a/tests/Windows/rtaudiotest/rtaudiotest.dsw b/tests/Windows/rtaudiotest/rtaudiotest.dsw new file mode 100644 index 0000000..eabbccd --- /dev/null +++ b/tests/Windows/rtaudiotest/rtaudiotest.dsw @@ -0,0 +1,29 @@ +Microsoft Developer Studio Workspace File, Format Version 6.00 +# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! + +############################################################################### + +Project: "rtaudiotest"=".\rtaudiotest.dsp" - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Global: + +Package=<5> +{{{ +}}} + +Package=<3> +{{{ +}}} + +############################################################################### + diff --git a/tests/call_inout.cpp b/tests/call_inout.cpp index e941e1e..f51ad6f 100644 --- a/tests/call_inout.cpp +++ b/tests/call_inout.cpp @@ -53,7 +53,7 @@ int inout(char *buffer, int buffer_size, void *) int main(int argc, char *argv[]) { int chans, fs, device = 0; - RtAudio *audio; + RtAudio *audio = 0; char input; // minimal command-line checking @@ -67,8 +67,8 @@ int main(int argc, char *argv[]) // Open the realtime output device int buffer_size = 512; try { - audio = new RtAudio(device, chans, device, chans, - FORMAT, fs, &buffer_size, 8); + audio = new RtAudio( device, chans, device, chans, + FORMAT, fs, &buffer_size, 8 ); } catch (RtError &error) { error.printMessage(); diff --git a/tests/call_saw.cpp b/tests/call_saw.cpp index 57f225c..b37841b 100644 --- a/tests/call_saw.cpp +++ b/tests/call_saw.cpp @@ -76,8 +76,8 @@ int saw(char *buffer, int buffer_size, void *data) int main(int argc, char *argv[]) { int buffer_size, fs, device = 0; - RtAudio *audio; - double *data; + RtAudio *audio = 0; + double *data = 0; char input; // minimal command-line checking diff --git a/tests/in_out.cpp b/tests/in_out.cpp index bba4d73..18fa8c1 100644 --- a/tests/in_out.cpp +++ b/tests/in_out.cpp @@ -51,7 +51,7 @@ int main(int argc, char *argv[]) int chans, fs, buffer_size, device = 0; long frames, counter = 0; MY_TYPE *buffer; - RtAudio *audio; + RtAudio *audio = 0; // minimal command-line checking if (argc != 3 && argc != 4 ) usage(); diff --git a/tests/info.cpp b/tests/info.cpp index 81a4234..113efb0 100644 --- a/tests/info.cpp +++ b/tests/info.cpp @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) { - RtAudio *audio; + RtAudio *audio = 0; RtAudioDeviceInfo info; try { audio = new RtAudio(); diff --git a/tests/play_raw.cpp b/tests/play_raw.cpp index fcc921f..6dd477d 100644 --- a/tests/play_raw.cpp +++ b/tests/play_raw.cpp @@ -60,7 +60,7 @@ int main(int argc, char *argv[]) MY_TYPE *buffer; char *file; FILE *fd; - RtAudio *audio; + RtAudio *audio = 0; // minimal command-line checking if (argc != 4 && argc != 5 ) usage(); diff --git a/tests/play_saw.cpp b/tests/play_saw.cpp index 4b2cbdd..b99248f 100644 --- a/tests/play_saw.cpp +++ b/tests/play_saw.cpp @@ -58,7 +58,7 @@ int main(int argc, char *argv[]) int chans, fs, buffer_size, device = 0; long frames, counter = 0, i, j; MY_TYPE *buffer; - RtAudio *audio; + RtAudio *audio = 0; double *data = 0; // minimal command-line checking diff --git a/tests/record_raw.cpp b/tests/record_raw.cpp index 717ce2c..3a207f9 100644 --- a/tests/record_raw.cpp +++ b/tests/record_raw.cpp @@ -54,7 +54,7 @@ int main(int argc, char *argv[]) long frames, counter = 0; MY_TYPE *buffer; FILE *fd; - RtAudio *audio; + RtAudio *audio = 0; // minimal command-line checking if (argc != 3 && argc != 4 ) usage(); diff --git a/tests/twostreams.cpp b/tests/twostreams.cpp index 508cf4d..17badd0 100644 --- a/tests/twostreams.cpp +++ b/tests/twostreams.cpp @@ -62,7 +62,7 @@ int main(int argc, char *argv[]) int chans, fs, buffer_size, device = 0; long frames, counter = 0, i, j; MY_TYPE *buffer1, *buffer2; - RtAudio *stream1, *stream2; + RtAudio *stream1 = 0, *stream2 = 0; FILE *fd; double *data = 0;