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.

227 lines
6.3KB

  1. /******************************************/
  2. /*
  3. testall.cpp
  4. by Gary P. Scavone, 2007-2008
  5. This program will make a variety of calls
  6. to extensively test RtAudio functionality.
  7. */
  8. /******************************************/
  9. #include "RtAudio.h"
  10. #include <iostream>
  11. #define BASE_RATE 0.005
  12. #define TIME 1.0
  13. void usage( void ) {
  14. // Error function in case of incorrect command-line
  15. // argument specifications
  16. std::cout << "\nuseage: testall N fs <iDevice> <oDevice> <iChannelOffset> <oChannelOffset>\n";
  17. std::cout << " where N = number of channels,\n";
  18. std::cout << " fs = the sample rate,\n";
  19. std::cout << " iDevice = optional input device to use (default = 0),\n";
  20. std::cout << " oDevice = optional output device to use (default = 0),\n";
  21. std::cout << " iChannelOffset = an optional input channel offset (default = 0),\n";
  22. std::cout << " and oChannelOffset = optional output channel offset (default = 0).\n\n";
  23. exit( 0 );
  24. }
  25. unsigned int channels;
  26. // Interleaved buffers
  27. int sawi( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
  28. double streamTime, RtAudioStreamStatus status, void *data )
  29. {
  30. unsigned int i, j;
  31. extern unsigned int channels;
  32. double *buffer = (double *) outputBuffer;
  33. double *lastValues = (double *) data;
  34. if ( status )
  35. std::cout << "Stream underflow detected!" << std::endl;
  36. for ( i=0; i<nBufferFrames; i++ ) {
  37. for ( j=0; j<channels; j++ ) {
  38. *buffer++ = (double) lastValues[j];
  39. lastValues[j] += BASE_RATE * (j+1+(j*0.1));
  40. if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
  41. }
  42. }
  43. return 0;
  44. }
  45. // Non-interleaved buffers
  46. int sawni( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
  47. double streamTime, RtAudioStreamStatus status, void *data )
  48. {
  49. unsigned int i, j;
  50. extern unsigned int channels;
  51. double *buffer = (double *) outputBuffer;
  52. double *lastValues = (double *) data;
  53. if ( status )
  54. std::cout << "Stream underflow detected!" << std::endl;
  55. float increment;
  56. for ( j=0; j<channels; j++ ) {
  57. increment = BASE_RATE * (j+1+(j*0.1));
  58. for ( i=0; i<nBufferFrames; i++ ) {
  59. *buffer++ = (double) lastValues[j];
  60. lastValues[j] += increment;
  61. if ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;
  62. }
  63. }
  64. return 0;
  65. }
  66. int inout( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
  67. double streamTime, RtAudioStreamStatus status, void *data )
  68. {
  69. // Since the number of input and output channels is equal, we can do
  70. // a simple buffer copy operation here.
  71. if ( status ) std::cout << "Stream over/underflow detected." << std::endl;
  72. unsigned long *bytes = (unsigned long *) data;
  73. memcpy( outputBuffer, inputBuffer, *bytes );
  74. return 0;
  75. }
  76. int main( int argc, char *argv[] )
  77. {
  78. unsigned int bufferFrames, fs, oDevice = 0, iDevice = 0, iOffset = 0, oOffset = 0;
  79. char input;
  80. // minimal command-line checking
  81. if (argc < 3 || argc > 7 ) usage();
  82. RtAudio dac;
  83. if ( dac.getDeviceCount() < 1 ) {
  84. std::cout << "\nNo audio devices found!\n";
  85. exit( 1 );
  86. }
  87. channels = (unsigned int) atoi( argv[1] );
  88. fs = (unsigned int) atoi( argv[2] );
  89. if ( argc > 3 )
  90. iDevice = (unsigned int) atoi( argv[3] );
  91. if ( argc > 4 )
  92. oDevice = (unsigned int) atoi(argv[4]);
  93. if ( argc > 5 )
  94. iOffset = (unsigned int) atoi(argv[5]);
  95. if ( argc > 6 )
  96. oOffset = (unsigned int) atoi(argv[6]);
  97. double *data = (double *) calloc( channels, sizeof( double ) );
  98. // Let RtAudio print messages to stderr.
  99. dac.showWarnings( true );
  100. // Set our stream parameters for output only.
  101. bufferFrames = 256;
  102. RtAudio::StreamParameters oParams, iParams;
  103. oParams.deviceId = oDevice;
  104. oParams.nChannels = channels;
  105. oParams.firstChannel = oOffset;
  106. RtAudio::StreamOptions options;
  107. options.flags = RTAUDIO_HOG_DEVICE;
  108. try {
  109. dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawi, (void *)data, &options );
  110. std::cout << "\nStream latency = " << dac.getStreamLatency() << std::endl;
  111. // Start the stream
  112. dac.startStream();
  113. std::cout << "\nPlaying ... press <enter> to stop.\n";
  114. std::cin.get( input );
  115. // Stop the stream
  116. dac.stopStream();
  117. // Restart again
  118. std::cout << "Press <enter> to restart.\n";
  119. std::cin.get( input );
  120. dac.startStream();
  121. // Test abort function
  122. std::cout << "Playing again ... press <enter> to abort.\n";
  123. std::cin.get( input );
  124. dac.abortStream();
  125. // Restart another time
  126. std::cout << "Press <enter> to restart again.\n";
  127. std::cin.get( input );
  128. dac.startStream();
  129. std::cout << "Playing again ... press <enter> to close the stream.\n";
  130. std::cin.get( input );
  131. }
  132. catch ( RtError& e ) {
  133. e.printMessage();
  134. goto cleanup;
  135. }
  136. if ( dac.isStreamOpen() ) dac.closeStream();
  137. // Test non-interleaved functionality
  138. options.flags = RTAUDIO_NONINTERLEAVED;
  139. try {
  140. dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawni, (void *)data, &options );
  141. std::cout << "Press <enter> to start non-interleaved playback.\n";
  142. std::cin.get( input );
  143. // Start the stream
  144. dac.startStream();
  145. std::cout << "\nPlaying ... press <enter> to stop.\n";
  146. std::cin.get( input );
  147. }
  148. catch ( RtError& e ) {
  149. e.printMessage();
  150. goto cleanup;
  151. }
  152. if ( dac.isStreamOpen() ) dac.closeStream();
  153. // Now open a duplex stream.
  154. unsigned int bufferBytes;
  155. iParams.deviceId = iDevice;
  156. iParams.nChannels = channels;
  157. iParams.firstChannel = iOffset;
  158. options.flags = RTAUDIO_NONINTERLEAVED;
  159. try {
  160. dac.openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
  161. bufferBytes = bufferFrames * channels * 4;
  162. std::cout << "Press <enter> to start duplex operation.\n";
  163. std::cin.get( input );
  164. // Start the stream
  165. dac.startStream();
  166. std::cout << "\nRunning ... press <enter> to stop.\n";
  167. std::cin.get( input );
  168. // Stop the stream
  169. dac.stopStream();
  170. std::cout << "\nStopped ... press <enter> to restart.\n";
  171. std::cin.get( input );
  172. // Restart the stream
  173. dac.startStream();
  174. std::cout << "\nRunning ... press <enter> to stop.\n";
  175. std::cin.get( input );
  176. }
  177. catch ( RtError& e ) {
  178. e.printMessage();
  179. }
  180. cleanup:
  181. if ( dac.isStreamOpen() ) dac.closeStream();
  182. free( data );
  183. return 0;
  184. }