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.

222 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( 0 );
  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;
  92. data = (double *) calloc( channels, sizeof( double ) );
  93. // Let RtAudio print messages to stderr.
  94. dac.showWarnings( true );
  95. // Set our stream parameters for output only.
  96. bufferFrames = 256;
  97. RtAudio::StreamParameters oParams, iParams;
  98. oParams.deviceId = device;
  99. oParams.nChannels = channels;
  100. oParams.firstChannel = offset;
  101. RtAudio::StreamOptions options;
  102. options.flags = RTAUDIO_HOG_DEVICE;
  103. try {
  104. dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawi, (void *)data, &options );
  105. std::cout << "\nStream latency = " << dac.getStreamLatency() << std::endl;
  106. // Start the stream
  107. dac.startStream();
  108. std::cout << "\nPlaying ... press <enter> to stop.\n";
  109. std::cin.get( input );
  110. // Stop the stream
  111. dac.stopStream();
  112. // Restart again
  113. std::cout << "Press <enter> to restart.\n";
  114. std::cin.get( input );
  115. dac.startStream();
  116. // Test abort function
  117. std::cout << "Playing again ... press <enter> to abort.\n";
  118. std::cin.get( input );
  119. dac.abortStream();
  120. // Restart another time
  121. std::cout << "Press <enter> to restart again.\n";
  122. std::cin.get( input );
  123. dac.startStream();
  124. std::cout << "Playing again ... press <enter> to close the stream.\n";
  125. std::cin.get( input );
  126. }
  127. catch ( RtError& e ) {
  128. e.printMessage();
  129. goto cleanup;
  130. }
  131. if ( dac.isStreamOpen() ) dac.closeStream();
  132. // Test non-interleaved functionality
  133. options.flags = RTAUDIO_NONINTERLEAVED;
  134. try {
  135. dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawni, (void *)data, &options );
  136. std::cout << "Press <enter> to start non-interleaved playback.\n";
  137. std::cin.get( input );
  138. // Start the stream
  139. dac.startStream();
  140. std::cout << "\nPlaying ... press <enter> to stop.\n";
  141. std::cin.get( input );
  142. }
  143. catch ( RtError& e ) {
  144. e.printMessage();
  145. goto cleanup;
  146. }
  147. if ( dac.isStreamOpen() ) dac.closeStream();
  148. // Now open a duplex stream.
  149. unsigned int bufferBytes;
  150. iParams.deviceId = device;
  151. iParams.nChannels = channels;
  152. iParams.firstChannel = offset;
  153. options.flags = RTAUDIO_NONINTERLEAVED;
  154. try {
  155. dac.openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
  156. bufferBytes = bufferFrames * channels * 4;
  157. std::cout << "Press <enter> to start duplex operation.\n";
  158. std::cin.get( input );
  159. // Start the stream
  160. dac.startStream();
  161. std::cout << "\nRunning ... press <enter> to stop.\n";
  162. std::cin.get( input );
  163. // Stop the stream
  164. dac.stopStream();
  165. std::cout << "\nStopped ... press <enter> to restart.\n";
  166. std::cin.get( input );
  167. // Restart the stream
  168. dac.startStream();
  169. std::cout << "\nRunning ... press <enter> to stop.\n";
  170. std::cin.get( input );
  171. }
  172. catch ( RtError& e ) {
  173. e.printMessage();
  174. }
  175. cleanup:
  176. if ( dac.isStreamOpen() ) dac.closeStream();
  177. free( data );
  178. return 0;
  179. }