You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
6.6KB

  1. /******************************************/
  2. /*
  3. playsaw.cpp
  4. by Gary P. Scavone, 2006-2019.
  5. This program will output sawtooth waveforms
  6. of different frequencies on each channel.
  7. */
  8. /******************************************/
  9. #include "RtAudio.h"
  10. #include <iostream>
  11. #include <cstdlib>
  12. #include <signal.h>
  13. /*
  14. typedef char MY_TYPE;
  15. #define FORMAT RTAUDIO_SINT8
  16. #define SCALE 127.0
  17. */
  18. typedef signed short MY_TYPE;
  19. #define FORMAT RTAUDIO_SINT16
  20. #define SCALE 32767.0
  21. /*
  22. typedef S24 MY_TYPE;
  23. #define FORMAT RTAUDIO_SINT24
  24. #define SCALE 8388607.0
  25. typedef signed long MY_TYPE;
  26. #define FORMAT RTAUDIO_SINT32
  27. #define SCALE 2147483647.0
  28. typedef float MY_TYPE;
  29. #define FORMAT RTAUDIO_FLOAT32
  30. #define SCALE 1.0
  31. typedef double MY_TYPE;
  32. #define FORMAT RTAUDIO_FLOAT64
  33. #define SCALE 1.0
  34. */
  35. // Platform-dependent sleep routines.
  36. #if defined( WIN32 )
  37. #include <windows.h>
  38. #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
  39. #else // Unix variants
  40. #include <unistd.h>
  41. #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
  42. #endif
  43. // Interrupt handler function
  44. bool done;
  45. static void finish( int /*ignore*/ ){ done = true; }
  46. #define BASE_RATE 0.005
  47. #define TIME 1.0
  48. void usage( void ) {
  49. // Error function in case of incorrect command-line
  50. // argument specifications
  51. std::cout << "\nuseage: playsaw N fs <device> <channelOffset> <time>\n";
  52. std::cout << " where N = number of channels,\n";
  53. std::cout << " fs = the sample rate,\n";
  54. std::cout << " device = optional device to use (default = 0),\n";
  55. std::cout << " channelOffset = an optional channel offset on the device (default = 0),\n";
  56. std::cout << " and time = an optional time duration in seconds (default = no limit).\n\n";
  57. exit( 0 );
  58. }
  59. void errorCallback( RtAudioErrorType /*type*/, const std::string &errorText )
  60. {
  61. // This example error handling function simply outputs the error message to stderr.
  62. std::cerr << "\nerrorCallback: " << errorText << "\n\n";
  63. }
  64. unsigned int channels;
  65. RtAudio::StreamOptions options;
  66. unsigned int frameCounter = 0;
  67. bool checkCount = false;
  68. unsigned int nFrames = 0;
  69. const unsigned int callbackReturnValue = 1; // 1 = stop and drain, 2 = abort
  70. double streamTimePrintIncrement = 1.0; // seconds
  71. double streamTimePrintTime = 1.0; // seconds
  72. #define USE_INTERLEAVED
  73. #if defined( USE_INTERLEAVED )
  74. // Interleaved buffers
  75. int saw( void *outputBuffer, void * /*inputBuffer*/, unsigned int nBufferFrames,
  76. double streamTime, RtAudioStreamStatus status, void *data )
  77. {
  78. unsigned int i, j;
  79. extern unsigned int channels;
  80. MY_TYPE *buffer = (MY_TYPE *) outputBuffer;
  81. double *lastValues = (double *) data;
  82. if ( status )
  83. std::cout << "Stream underflow detected!" << std::endl;
  84. if ( streamTime >= streamTimePrintTime ) {
  85. std::cout << "streamTime = " << streamTime << std::endl;
  86. streamTimePrintTime += streamTimePrintIncrement;
  87. }
  88. for ( i=0; i<nBufferFrames; i++ ) {
  89. for ( j=0; j<channels; j++ ) {
  90. *buffer++ = (MY_TYPE) (lastValues[j] * SCALE * 0.5);
  91. lastValues[j] += BASE_RATE * (j+1+(j*0.1));
  92. if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
  93. }
  94. }
  95. frameCounter += nBufferFrames;
  96. if ( checkCount && ( frameCounter >= nFrames ) ) return callbackReturnValue;
  97. return 0;
  98. }
  99. #else // Use non-interleaved buffers
  100. int saw( void *outputBuffer, void * /*inputBuffer*/, unsigned int nBufferFrames,
  101. double streamTime, RtAudioStreamStatus status, void *data )
  102. {
  103. unsigned int i, j;
  104. extern unsigned int channels;
  105. MY_TYPE *buffer = (MY_TYPE *) outputBuffer;
  106. double *lastValues = (double *) data;
  107. if ( status )
  108. std::cout << "Stream underflow detected!" << std::endl;
  109. if ( streamTime >= streamTimePrintTime ) {
  110. std::cout << "streamTime = " << streamTime << std::endl;
  111. streamTimePrintTime += streamTimePrintIncrement;
  112. }
  113. double increment;
  114. for ( j=0; j<channels; j++ ) {
  115. increment = BASE_RATE * (j+1+(j*0.1));
  116. for ( i=0; i<nBufferFrames; i++ ) {
  117. *buffer++ = (MY_TYPE) (lastValues[j] * SCALE * 0.5);
  118. lastValues[j] += increment;
  119. if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
  120. }
  121. }
  122. frameCounter += nBufferFrames;
  123. if ( checkCount && ( frameCounter >= nFrames ) ) return callbackReturnValue;
  124. return 0;
  125. }
  126. #endif
  127. int main( int argc, char *argv[] )
  128. {
  129. unsigned int bufferFrames, fs, device = 0, offset = 0;
  130. // minimal command-line checking
  131. if (argc < 3 || argc > 6 ) usage();
  132. // Specify our own error callback function.
  133. RtAudio dac( RtAudio::UNSPECIFIED, &errorCallback );
  134. if ( dac.getDeviceCount() < 1 ) {
  135. std::cout << "\nNo audio devices found!\n";
  136. exit( 1 );
  137. }
  138. channels = (unsigned int) atoi( argv[1] );
  139. fs = (unsigned int) atoi( argv[2] );
  140. if ( argc > 3 )
  141. device = (unsigned int) atoi( argv[3] );
  142. if ( argc > 4 )
  143. offset = (unsigned int) atoi( argv[4] );
  144. if ( argc > 5 )
  145. nFrames = (unsigned int) (fs * atof( argv[5] ));
  146. if ( nFrames > 0 ) checkCount = true;
  147. double *data = (double *) calloc( channels, sizeof( double ) );
  148. //dac.setErrorCallback( &errorCallback ); // could use if not set via constructor
  149. // Tell RtAudio to output all messages, even warnings.
  150. dac.showWarnings( true );
  151. // Set our stream parameters for output only.
  152. bufferFrames = 512;
  153. RtAudio::StreamParameters oParams;
  154. oParams.deviceId = device;
  155. oParams.nChannels = channels;
  156. oParams.firstChannel = offset;
  157. if ( device == 0 )
  158. oParams.deviceId = dac.getDefaultOutputDevice();
  159. options.flags = RTAUDIO_HOG_DEVICE;
  160. options.flags |= RTAUDIO_SCHEDULE_REALTIME;
  161. #if !defined( USE_INTERLEAVED )
  162. options.flags |= RTAUDIO_NONINTERLEAVED;
  163. #endif
  164. // An error in the openStream() function can be detected either by
  165. // checking for a non-zero return value OR by a subsequent call to
  166. // isStreamOpen().
  167. if ( dac.openStream( &oParams, NULL, FORMAT, fs, &bufferFrames, &saw, (void *)data, &options ) )
  168. goto cleanup;
  169. if ( dac.isStreamOpen() == false ) goto cleanup;
  170. //std::cout << "Stream latency = " << dac.getStreamLatency() << "\n" << std::endl;
  171. // Stream is open ... now start it.
  172. if ( dac.startStream() ) goto cleanup;
  173. if ( checkCount ) {
  174. while ( dac.isStreamRunning() == true ) SLEEP( 100 );
  175. }
  176. else {
  177. std::cout << "\nPlaying ... quit with Ctrl-C (buffer size = " << bufferFrames << ").\n";
  178. // Install an interrupt handler function.
  179. done = false;
  180. (void) signal(SIGINT, finish);
  181. while ( !done && dac.isStreamRunning() ) SLEEP( 100 );
  182. // Block released ... stop the stream
  183. if ( dac.isStreamRunning() )
  184. dac.stopStream(); // or could call dac.abortStream();
  185. }
  186. cleanup:
  187. if ( dac.isStreamOpen() ) dac.closeStream();
  188. free( data );
  189. return 0;
  190. }