diff options
| author | Gary Scavone <gary@music.mcgill.ca> | 2007-08-07 14:52:05 +0000 |
|---|---|---|
| committer | Stephen Sinclair <sinclair@music.mcgill.ca> | 2013-10-11 01:19:40 +0200 |
| commit | b0080e69d64ce69e21c8ce1b22b1bb7f888f1e58 (patch) | |
| tree | b51ebcd31a5280b4f4e9e45484f07d9b35723743 /tests/playsaw.cpp | |
| parent | 0fbcd74a04713a4725d2f346a493121b623d60ab (diff) | |
Check in of new version 4.0.0 distribution (GS).
Diffstat (limited to 'tests/playsaw.cpp')
| -rw-r--r-- | tests/playsaw.cpp | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/tests/playsaw.cpp b/tests/playsaw.cpp new file mode 100644 index 0000000..cbe4203 --- /dev/null +++ b/tests/playsaw.cpp @@ -0,0 +1,177 @@ +/******************************************/ +/* + playsaw.cpp + by Gary P. Scavone, 2006 + + This program will output sawtooth waveforms + of different frequencies on each channel. +*/ +/******************************************/ + +#include "RtAudio.h" +#include <iostream> + +/* +typedef signed long MY_TYPE; +#define FORMAT RTAUDIO_SINT24 +#define SCALE 2147483647.0 + +typedef char MY_TYPE; +#define FORMAT RTAUDIO_SINT8 +#define SCALE 127.0 + +typedef signed short MY_TYPE; +#define FORMAT RTAUDIO_SINT16 +#define SCALE 32767.0 + +typedef signed long MY_TYPE; +#define FORMAT RTAUDIO_SINT32 +#define SCALE 2147483647.0 +*/ + +typedef float MY_TYPE; +#define FORMAT RTAUDIO_FLOAT32 +#define SCALE 1.0 + +/* +typedef double MY_TYPE; +#define FORMAT RTAUDIO_FLOAT64 +#define SCALE 1.0 +*/ + +#define BASE_RATE 0.005 +#define TIME 1.0 + +void usage( void ) { + // Error function in case of incorrect command-line + // argument specifications + std::cout << "\nuseage: playsaw N fs <device> <channelOffset>\n"; + std::cout << " where N = number of channels,\n"; + std::cout << " fs = the sample rate,\n"; + std::cout << " device = optional device to use (default = 0),\n"; + std::cout << " and channelOffset = an optional channel offset on the device (default = 0).\n\n"; + exit( 0 ); +} + +unsigned int channels; +RtAudio::StreamOptions options; + +//#define USE_INTERLEAVED +#if defined( USE_INTERLEAVED ) + +// Interleaved buffers +int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, + double streamTime, RtAudioStreamStatus status, void *data ) +{ + unsigned int i, j; + extern unsigned int channels; + MY_TYPE *buffer = (MY_TYPE *) outputBuffer; + double *lastValues = (double *) data; + + if ( status ) + std::cout << "Stream underflow detected!" << std::endl; + + for ( i=0; i<nBufferFrames; i++ ) { + for ( j=0; j<channels; j++ ) { + *buffer++ = (MY_TYPE) (lastValues[j] * SCALE); + lastValues[j] += BASE_RATE * (j+1+(j*0.1)); + if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0; + } + } + + return 0; +} + +#else // Use non-interleaved buffers + +int saw( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames, + double streamTime, RtAudioStreamStatus status, void *data ) +{ + unsigned int i, j; + extern unsigned int channels; + MY_TYPE *buffer = (MY_TYPE *) outputBuffer; + double *lastValues = (double *) data; + + if ( status ) + std::cout << "Stream underflow detected!" << std::endl; + + double increment; + for ( j=0; j<channels; j++ ) { + increment = BASE_RATE * (j+1+(j*0.1)); + for ( i=0; i<nBufferFrames; i++ ) { + *buffer++ = (MY_TYPE) (lastValues[j] * SCALE); + lastValues[j] += increment; + if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0; + } + } + + return 0; +} +#endif + +int main( int argc, char *argv[] ) +{ + unsigned int bufferFrames, fs, device = 0, offset = 0; + + // minimal command-line checking + if (argc < 3 || argc > 5 ) usage(); + + RtAudio dac; + if ( dac.getDeviceCount() < 1 ) { + std::cout << "\nNo audio devices found!\n"; + exit( 0 ); + } + + channels = (unsigned int) atoi(argv[1]); + fs = (unsigned int) atoi(argv[2]); + if ( argc > 3 ) + device = (unsigned int) atoi(argv[3]); + if ( argc > 4 ) + offset = (unsigned int) atoi(argv[4]); + + double *data; + data = (double *) calloc( channels, sizeof( double ) ); + + // Let RtAudio print messages to stderr. + dac.showWarnings( true ); + + // Set our stream parameters for output only. + bufferFrames = 256; + RtAudio::StreamParameters oParams; + oParams.deviceId = device; + oParams.nChannels = channels; + oParams.firstChannel = offset; + + + options.flags |= RTAUDIO_HOG_DEVICE; +#if !defined( USE_INTERLEAVED ) + options.flags |= RTAUDIO_NONINTERLEAVED; +#endif + try { + dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &saw, (void *)data, &options ); + dac.startStream(); + } + catch ( RtError& e ) { + e.printMessage(); + goto cleanup; + } + + char input; + std::cout << "\nPlaying ... press <enter> to quit (buffer size = " << bufferFrames << ").\n"; + std::cin.get( input ); + + try { + // Stop the stream + std::cout << "Stream latency = " << dac.getStreamLatency() << "\n" << std::endl; + dac.stopStream(); + } + catch ( RtError& e ) { + e.printMessage(); + } + + cleanup: + if ( dac.isStreamOpen() ) dac.closeStream(); + free( data ); + + return 0; +} |
