diff --git a/projects/demo/demo.cpp b/projects/demo/demo.cpp index e0612097..623b3e75 100644 --- a/projects/demo/demo.cpp +++ b/projects/demo/demo.cpp @@ -203,7 +203,6 @@ int main( int argc, char *argv[] ) int i; #if defined(__STK_REALTIME__) - //RtAudio dac( RtAudio::UNSPECIFIED ); RtAudio *dac = 0; #endif diff --git a/projects/eguitar/eguitar.cpp b/projects/eguitar/eguitar.cpp index d1a09c1d..997f28a5 100644 --- a/projects/eguitar/eguitar.cpp +++ b/projects/eguitar/eguitar.cpp @@ -265,7 +265,7 @@ int main( int argc, char *argv[] ) int i; #if defined(__STK_REALTIME__) - RtAudio dac; + RtAudio *dac = 0; #endif // If you want to change the default sample rate (set in Stk.h), do @@ -294,16 +294,14 @@ int main( int argc, char *argv[] ) // If realtime output, allocate the dac here. #if defined(__STK_REALTIME__) if ( data.realtime ) { + dac = (RtAudio *) new RtAudio( RtAudio::UNSPECIFIED ); RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32; RtAudio::StreamParameters parameters; - parameters.deviceId = dac.getDefaultOutputDevice(); + parameters.deviceId = dac->getDefaultOutputDevice(); parameters.nChannels = data.channels; unsigned int bufferFrames = RT_BUFFER_SIZE; - try { - dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data ); - } - catch ( RtAudioError& error ) { - error.printMessage(); + if ( dac->openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data ) ) { + std::cout << dac->getErrorText() << std::endl; goto cleanup; } } @@ -335,11 +333,8 @@ int main( int argc, char *argv[] ) // If realtime output, set our callback function and start the dac. #if defined(__STK_REALTIME__) if ( data.realtime ) { - try { - dac.startStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac->startStream() ) { + std::cout << dac->getErrorText() << std::endl; goto cleanup; } } @@ -359,14 +354,8 @@ int main( int argc, char *argv[] ) // Shut down the output stream. #if defined(__STK_REALTIME__) - if ( data.realtime ) { - try { - dac.closeStream(); - } - catch ( RtAudioError& error ) { - error.printMessage(); - } - } + if ( data.realtime ) + dac->closeStream(); #endif cleanup: @@ -374,6 +363,7 @@ int main( int argc, char *argv[] ) for ( i=0; i<(int)data.nWvOuts; i++ ) delete data.wvout[i]; free( data.wvout ); delete data.guitar; + delete dac; std::cout << "\nStk eguitar finished ... goodbye.\n\n"; return 0; diff --git a/projects/examples/audioprobe.cpp b/projects/examples/audioprobe.cpp index ece84ac8..bfa7a47f 100644 --- a/projects/examples/audioprobe.cpp +++ b/projects/examples/audioprobe.cpp @@ -11,76 +11,107 @@ #include #include -int main() -{ - // Create an api map. - std::map apiMap; - apiMap[RtAudio::MACOSX_CORE] = "OS-X CoreAudio"; - apiMap[RtAudio::WINDOWS_ASIO] = "Windows ASIO"; - apiMap[RtAudio::WINDOWS_DS] = "Windows DirectSound"; - apiMap[RtAudio::UNIX_JACK] = "Jack Client"; - apiMap[RtAudio::LINUX_ALSA] = "Linux ALSA"; - apiMap[RtAudio::LINUX_OSS] = "Linux OSS"; - apiMap[RtAudio::RTAUDIO_DUMMY] = "RtAudio Dummy"; +void usage( void ) { + // Error function in case of incorrect command-line + // argument specifications + std::cout << "\nuseage: audioprobe \n"; + std::cout << " where apiname = an optional api (ex., 'core', default = all compiled),\n"; + std::cout << " and nRepeats = an optional number of times to repeat the device query (default = 0),\n"; + std::cout << " which can be used to test device (dis)connections.\n\n"; + exit( 0 ); +} +std::vector< RtAudio::Api > listApis() +{ std::vector< RtAudio::Api > apis; RtAudio :: getCompiledApi( apis ); std::cout << "\nCompiled APIs:\n"; - for ( unsigned int i=0; i devices = audio.getDeviceIds(); + std::cout << "\nFound " << devices.size() << " device(s) ...\n"; - for (unsigned int i=0; i apis = listApis(); + + // minimal command-line checking + if (argc > 3 ) usage(); + unsigned int nRepeats = 0; + if ( argc > 2 ) nRepeats = (unsigned int) atoi( argv[2] ); + + char input; + for ( size_t api=0; api < apis.size(); api++ ) { + if (argc < 2 || apis[api] == RtAudio::getCompiledApiByName(argv[1]) ) { + RtAudio audio(apis[api]); + for ( size_t n=0; n <= nRepeats; n++ ) { + listDevices(audio); + if ( n < nRepeats ) { + std::cout << std::endl; + std::cout << "\nWaiting ... press to repeat.\n"; + std::cin.get(input); + } } - std::cout << std::endl; } } - std::cout << std::endl; return 0; } diff --git a/projects/examples/bethree.cpp b/projects/examples/bethree.cpp index a448aed8..a4e21c18 100644 --- a/projects/examples/bethree.cpp +++ b/projects/examples/bethree.cpp @@ -56,11 +56,8 @@ int main() parameters.nChannels = 1; RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32; unsigned int bufferFrames = RT_BUFFER_SIZE; - try { - dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data ); - } - catch ( RtAudioError& error ) { - error.printMessage(); + if ( dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data ) ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } @@ -75,11 +72,8 @@ int main() data.frequency = 220.0; data.instrument->noteOn( data.frequency, 0.5 ); - try { - dac.startStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac.startStream() ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } @@ -88,12 +82,7 @@ int main() Stk::sleep( 100 ); // Shut down the callback and output stream. - try { - dac.closeStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); - } + dac.closeStream(); cleanup: delete data.instrument; diff --git a/projects/examples/controlbee.cpp b/projects/examples/controlbee.cpp index 20d80e1b..fafe416e 100644 --- a/projects/examples/controlbee.cpp +++ b/projects/examples/controlbee.cpp @@ -130,11 +130,8 @@ int main( int argc, char *argv[] ) parameters.nChannels = 1; RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32; unsigned int bufferFrames = RT_BUFFER_SIZE; - try { - dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data ); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data ) ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } @@ -149,11 +146,8 @@ int main( int argc, char *argv[] ) if ( data.messager.setScoreFile( argv[1] ) == false ) goto cleanup; - try { - dac.startStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac.startStream() ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } @@ -162,12 +156,7 @@ int main( int argc, char *argv[] ) Stk::sleep( 100 ); // Shut down the output stream. - try { - dac.closeStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); - } + dac.closeStream(); cleanup: delete data.instrument; diff --git a/projects/examples/crtsine.cpp b/projects/examples/crtsine.cpp index f8a26dd0..935e16cf 100644 --- a/projects/examples/crtsine.cpp +++ b/projects/examples/crtsine.cpp @@ -33,21 +33,15 @@ int main() parameters.nChannels = 1; RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32; unsigned int bufferFrames = RT_BUFFER_SIZE; - try { - dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&sine ); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&sine ) ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } sine.setFrequency(440.0); - try { - dac.startStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac.startStream() ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } @@ -57,12 +51,7 @@ int main() std::cin.get( keyhit ); // Shut down the output stream. - try { - dac.closeStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); - } + dac.closeStream(); cleanup: diff --git a/projects/examples/duplex.cpp b/projects/examples/duplex.cpp index d0a02007..1257eea5 100644 --- a/projects/examples/duplex.cpp +++ b/projects/examples/duplex.cpp @@ -1,7 +1,7 @@ /******************************************/ /* duplex.cpp - by Gary P. Scavone, 2006-2007. + by Gary P. Scavone, 2006-2019. This program opens a duplex stream and passes input directly through to the output. @@ -14,24 +14,26 @@ #include /* -typedef signed long MY_TYPE; -#define FORMAT RTAUDIO_SINT24 - -typedef char MY_TYPE; +typedef char MY_TYPE; #define FORMAT RTAUDIO_SINT8 +*/ -typedef signed short MY_TYPE; +typedef signed short MY_TYPE; #define FORMAT RTAUDIO_SINT16 -typedef signed long MY_TYPE; +/* +typedef S24 MY_TYPE; +#define FORMAT RTAUDIO_SINT24 + +typedef signed long MY_TYPE; #define FORMAT RTAUDIO_SINT32 -typedef float MY_TYPE; +typedef float MY_TYPE; #define FORMAT RTAUDIO_FLOAT32 -*/ -typedef double MY_TYPE; +typedef double MY_TYPE; #define FORMAT RTAUDIO_FLOAT64 +*/ void usage( void ) { // Error function in case of incorrect command-line @@ -39,26 +41,52 @@ void usage( void ) { std::cout << "\nuseage: duplex N fs \n"; std::cout << " where N = number of channels,\n"; std::cout << " fs = the sample rate,\n"; - std::cout << " iDevice = optional input device to use (default = 0),\n"; - std::cout << " oDevice = optional output device to use (default = 0),\n"; + std::cout << " iDevice = optional input device index to use (default = 0),\n"; + std::cout << " oDevice = optional output device index to use (default = 0),\n"; std::cout << " iChannelOffset = an optional input channel offset (default = 0),\n"; std::cout << " and oChannelOffset = optional output channel offset (default = 0).\n\n"; exit( 0 ); } -int inout( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, +unsigned int getDeviceIndex( std::vector deviceNames, bool isInput = false ) +{ + unsigned int i; + std::string keyHit; + std::cout << '\n'; + for ( i=0; i> i; + } while ( i >= deviceNames.size() ); + std::getline( std::cin, keyHit ); // used to clear out stdin + return i; +} + +double streamTimePrintIncrement = 1.0; // seconds +double streamTimePrintTime = 1.0; // seconds + +int inout( void *outputBuffer, void *inputBuffer, unsigned int /*nBufferFrames*/, double streamTime, RtAudioStreamStatus status, void *data ) { // Since the number of input and output channels is equal, we can do // a simple buffer copy operation here. if ( status ) std::cout << "Stream over/underflow detected." << std::endl; - unsigned long *bytes = (unsigned long *) data; + if ( streamTime >= streamTimePrintTime ) { + std::cout << "streamTime = " << streamTime << std::endl; + streamTimePrintTime += streamTimePrintIncrement; + } + + unsigned int *bytes = (unsigned int *) data; memcpy( outputBuffer, inputBuffer, *bytes ); return 0; } -int main(int argc, char *argv[]) +int main( int argc, char *argv[] ) { unsigned int channels, fs, bufferBytes, oDevice = 0, iDevice = 0, iOffset = 0, oOffset = 0; @@ -66,9 +94,10 @@ int main(int argc, char *argv[]) if (argc < 3 || argc > 7 ) usage(); RtAudio adac; - if ( adac.getDeviceCount() < 1 ) { + std::vector deviceIds = adac.getDeviceIds(); + if ( deviceIds.size() < 1 ) { std::cout << "\nNo audio devices found!\n"; - exit( 0 ); + exit( 1 ); } channels = (unsigned int) atoi(argv[1]); @@ -88,48 +117,48 @@ int main(int argc, char *argv[]) // Set the same number of channels for both input and output. unsigned int bufferFrames = 512; RtAudio::StreamParameters iParams, oParams; - if ( iDevice == 0 ) - iParams.deviceId = adac.getDefaultInputDevice(); - else - iParams.deviceId = iDevice - 1; iParams.nChannels = channels; iParams.firstChannel = iOffset; - if ( oDevice == 0 ) - oParams.deviceId = adac.getDefaultOutputDevice(); - else - oParams.deviceId = oDevice - 1; oParams.nChannels = channels; oParams.firstChannel = oOffset; + if ( iDevice == 0 ) + iParams.deviceId = adac.getDefaultInputDevice(); + else { + if ( iDevice >= deviceIds.size() ) + iDevice = getDeviceIndex( adac.getDeviceNames(), true ); + iParams.deviceId = deviceIds[iDevice]; + } + if ( oDevice == 0 ) + oParams.deviceId = adac.getDefaultOutputDevice(); + else { + if ( oDevice >= deviceIds.size() ) + oDevice = getDeviceIndex( adac.getDeviceNames() ); + oParams.deviceId = deviceIds[oDevice]; + } + RtAudio::StreamOptions options; //options.flags |= RTAUDIO_NONINTERLEAVED; bufferBytes = bufferFrames * channels * sizeof( MY_TYPE ); - try { - adac.openStream( &oParams, &iParams, FORMAT, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options ); - } - catch ( RtAudioError& e ) { - std::cout << '\n' << e.getMessage() << '\n' << std::endl; - exit( 1 ); + if ( adac.openStream( &oParams, &iParams, FORMAT, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options ) ) { + goto cleanup; } + if ( adac.isStreamOpen() == false ) goto cleanup; + // Test RtAudio functionality for reporting latency. std::cout << "\nStream latency = " << adac.getStreamLatency() << " frames" << std::endl; - try { - adac.startStream(); + if ( adac.startStream() ) goto cleanup; - char input; - std::cout << "\nRunning ... press to quit (buffer frames = " << bufferFrames << ").\n"; - std::cin.get(input); + char input; + std::cout << "\nRunning ... press to quit (buffer frames = " << bufferFrames << ").\n"; + std::cin.get(input); - // Stop the stream. + // Stop the stream. + if ( adac.isStreamRunning() ) adac.stopStream(); - } - catch ( RtAudioError& e ) { - std::cout << '\n' << e.getMessage() << '\n' << std::endl; - goto cleanup; - } cleanup: if ( adac.isStreamOpen() ) adac.closeStream(); diff --git a/projects/examples/grains.cpp b/projects/examples/grains.cpp index 998508d9..4048ab14 100644 --- a/projects/examples/grains.cpp +++ b/projects/examples/grains.cpp @@ -79,35 +79,23 @@ int main( int argc, char *argv[] ) parameters.nChannels = grani.channelsOut(); RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32; unsigned int bufferFrames = RT_BUFFER_SIZE; - try { - dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&grani ); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&grani ) ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } - try { - dac.startStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac.startStream() ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } - // Block waiting here. char keyhit; std::cout << "\nPlaying ... press to quit.\n"; std::cin.get( keyhit ); // Shut down the callback and output stream. - try { - dac.closeStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); - } + dac.closeStream(); cleanup: diff --git a/projects/examples/play.cpp b/projects/examples/play.cpp index 5863823b..3457c87b 100644 --- a/projects/examples/play.cpp +++ b/projects/examples/play.cpp @@ -99,11 +99,8 @@ int main(int argc, char *argv[]) parameters.nChannels = ( channels == 1 ) ? 2 : channels; // Play mono files as stereo. RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32; unsigned int bufferFrames = RT_BUFFER_SIZE; - try { - dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&input ); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&input ) ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } @@ -113,11 +110,8 @@ int main(int argc, char *argv[]) // Resize the StkFrames object appropriately. frames.resize( bufferFrames, channels ); - try { - dac.startStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac.startStream() ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } @@ -127,12 +121,7 @@ int main(int argc, char *argv[]) // By returning a non-zero value in the callback above, the stream // is automatically stopped. But we should still close it. - try { - dac.closeStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); - } + dac.closeStream(); cleanup: return 0; diff --git a/projects/examples/threebees.cpp b/projects/examples/threebees.cpp index fa68e0ee..03966ef9 100644 --- a/projects/examples/threebees.cpp +++ b/projects/examples/threebees.cpp @@ -130,11 +130,8 @@ int main() parameters.nChannels = 1; RtAudioFormat format = ( sizeof(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32; unsigned int bufferFrames = RT_BUFFER_SIZE; - try { - dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data ); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data ) ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } @@ -154,11 +151,8 @@ int main() if ( data.messager.startStdInput() == false ) goto cleanup; - try { - dac.startStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac.startStream() ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } @@ -167,12 +161,7 @@ int main() Stk::sleep( 100 ); // Shut down the callback and output stream. - try { - dac.closeStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); - } + dac.closeStream(); cleanup: for ( i=0; i<3; i++ ) delete instrument[i]; diff --git a/projects/ragamatic/ragamat.cpp b/projects/ragamatic/ragamat.cpp index 8fe665aa..cbab7f24 100644 --- a/projects/ragamatic/ragamat.cpp +++ b/projects/ragamatic/ragamat.cpp @@ -291,11 +291,8 @@ int main( int argc, char *argv[] ) parameters.deviceId = dac.getDefaultOutputDevice(); parameters.nChannels = 2; unsigned int bufferFrames = RT_BUFFER_SIZE; - try { - dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data ); - } - catch ( RtAudioError& error ) { - error.printMessage(); + if ( dac.openStream( ¶meters, NULL, format, (unsigned int)Stk::sampleRate(), &bufferFrames, &tick, (void *)&data ) ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } @@ -314,11 +311,8 @@ int main( int argc, char *argv[] ) (void) signal( SIGINT, finish ); // If realtime output, set our callback function and start the dac. - try { - dac.startStream(); - } - catch ( RtAudioError &error ) { - error.printMessage(); + if ( dac.startStream() ) { + std::cout << dac.getErrorText() << std::endl; goto cleanup; } @@ -329,15 +323,9 @@ int main( int argc, char *argv[] ) } // Shut down the output stream. - try { - dac.closeStream(); - } - catch ( RtAudioError& error ) { - error.printMessage(); - } + dac.closeStream(); cleanup: return 0; - }