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.

240 lines
6.6KB

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