Audio plugin host https://kx.studio/carla
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.

229 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. #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. float 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. RtAudio::StreamOptions options;
  109. options.flags = RTAUDIO_HOG_DEVICE;
  110. try {
  111. dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawi, (void *)data, &options );
  112. std::cout << "\nStream latency = " << dac.getStreamLatency() << std::endl;
  113. // Start the stream
  114. dac.startStream();
  115. std::cout << "\nPlaying ... press <enter> to stop.\n";
  116. std::cin.get( input );
  117. // Stop the stream
  118. dac.stopStream();
  119. // Restart again
  120. std::cout << "Press <enter> to restart.\n";
  121. std::cin.get( input );
  122. dac.startStream();
  123. // Test abort function
  124. std::cout << "Playing again ... press <enter> to abort.\n";
  125. std::cin.get( input );
  126. dac.abortStream();
  127. // Restart another time
  128. std::cout << "Press <enter> to restart again.\n";
  129. std::cin.get( input );
  130. dac.startStream();
  131. std::cout << "Playing again ... press <enter> to close the stream.\n";
  132. std::cin.get( input );
  133. }
  134. catch ( RtError& e ) {
  135. e.printMessage();
  136. goto cleanup;
  137. }
  138. if ( dac.isStreamOpen() ) dac.closeStream();
  139. // Test non-interleaved functionality
  140. options.flags = RTAUDIO_NONINTERLEAVED;
  141. try {
  142. dac.openStream( &oParams, NULL, RTAUDIO_FLOAT64, fs, &bufferFrames, &sawni, (void *)data, &options );
  143. std::cout << "Press <enter> to start non-interleaved playback.\n";
  144. std::cin.get( input );
  145. // Start the stream
  146. dac.startStream();
  147. std::cout << "\nPlaying ... press <enter> to stop.\n";
  148. std::cin.get( input );
  149. }
  150. catch ( RtError& e ) {
  151. e.printMessage();
  152. goto cleanup;
  153. }
  154. if ( dac.isStreamOpen() ) dac.closeStream();
  155. // Now open a duplex stream.
  156. unsigned int bufferBytes;
  157. iParams.deviceId = iDevice;
  158. iParams.nChannels = channels;
  159. iParams.firstChannel = iOffset;
  160. options.flags = RTAUDIO_NONINTERLEAVED;
  161. try {
  162. dac.openStream( &oParams, &iParams, RTAUDIO_SINT32, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
  163. bufferBytes = bufferFrames * channels * 4;
  164. std::cout << "Press <enter> to start duplex operation.\n";
  165. std::cin.get( input );
  166. // Start the stream
  167. dac.startStream();
  168. std::cout << "\nRunning ... press <enter> to stop.\n";
  169. std::cin.get( input );
  170. // Stop the stream
  171. dac.stopStream();
  172. std::cout << "\nStopped ... press <enter> to restart.\n";
  173. std::cin.get( input );
  174. // Restart the stream
  175. dac.startStream();
  176. std::cout << "\nRunning ... press <enter> to stop.\n";
  177. std::cin.get( input );
  178. }
  179. catch ( RtError& e ) {
  180. e.printMessage();
  181. }
  182. cleanup:
  183. if ( dac.isStreamOpen() ) dac.closeStream();
  184. free( data );
  185. return 0;
  186. }