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.

221 lines
5.9KB

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