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.

148 lines
4.1KB

  1. /******************************************/
  2. /*
  3. duplex.cpp
  4. by Gary P. Scavone, 2006-2007.
  5. This program opens a duplex stream and passes
  6. input directly through to the output.
  7. */
  8. /******************************************/
  9. #include "RtAudio.h"
  10. #include <iostream>
  11. #include <cstdlib>
  12. #include <cstring>
  13. /*
  14. typedef char MY_TYPE;
  15. #define FORMAT RTAUDIO_SINT8
  16. */
  17. typedef signed short MY_TYPE;
  18. #define FORMAT RTAUDIO_SINT16
  19. /*
  20. typedef S24 MY_TYPE;
  21. #define FORMAT RTAUDIO_SINT24
  22. typedef signed long MY_TYPE;
  23. #define FORMAT RTAUDIO_SINT32
  24. typedef float MY_TYPE;
  25. #define FORMAT RTAUDIO_FLOAT32
  26. typedef double MY_TYPE;
  27. #define FORMAT RTAUDIO_FLOAT64
  28. */
  29. void usage( void ) {
  30. // Error function in case of incorrect command-line
  31. // argument specifications
  32. std::cout << "\nuseage: duplex N fs <iDevice> <oDevice> <iChannelOffset> <oChannelOffset>\n";
  33. std::cout << " where N = number of channels,\n";
  34. std::cout << " fs = the sample rate,\n";
  35. std::cout << " iDevice = optional input device to use (default = 0),\n";
  36. std::cout << " oDevice = optional output device to use (default = 0),\n";
  37. std::cout << " iChannelOffset = an optional input channel offset (default = 0),\n";
  38. std::cout << " and oChannelOffset = optional output channel offset (default = 0).\n\n";
  39. exit( 0 );
  40. }
  41. double streamTimePrintIncrement = 1.0; // seconds
  42. double streamTimePrintTime = 1.0; // seconds
  43. int inout( void *outputBuffer, void *inputBuffer, unsigned int /*nBufferFrames*/,
  44. double streamTime, RtAudioStreamStatus status, void *data )
  45. {
  46. // Since the number of input and output channels is equal, we can do
  47. // a simple buffer copy operation here.
  48. if ( status ) std::cout << "Stream over/underflow detected." << std::endl;
  49. if ( streamTime >= streamTimePrintTime ) {
  50. std::cout << "streamTime = " << streamTime << std::endl;
  51. streamTimePrintTime += streamTimePrintIncrement;
  52. }
  53. unsigned int *bytes = (unsigned int *) data;
  54. memcpy( outputBuffer, inputBuffer, *bytes );
  55. return 0;
  56. }
  57. int main( int argc, char *argv[] )
  58. {
  59. unsigned int channels, fs, bufferBytes, oDevice = 0, iDevice = 0, iOffset = 0, oOffset = 0;
  60. // Minimal command-line checking
  61. if (argc < 3 || argc > 7 ) usage();
  62. RtAudio adac;
  63. if ( adac.getDeviceCount() < 1 ) {
  64. std::cout << "\nNo audio devices found!\n";
  65. exit( 1 );
  66. }
  67. channels = (unsigned int) atoi(argv[1]);
  68. fs = (unsigned int) atoi(argv[2]);
  69. if ( argc > 3 )
  70. iDevice = (unsigned int) atoi(argv[3]);
  71. if ( argc > 4 )
  72. oDevice = (unsigned int) atoi(argv[4]);
  73. if ( argc > 5 )
  74. iOffset = (unsigned int) atoi(argv[5]);
  75. if ( argc > 6 )
  76. oOffset = (unsigned int) atoi(argv[6]);
  77. // Let RtAudio print messages to stderr.
  78. adac.showWarnings( true );
  79. // Set the same number of channels for both input and output.
  80. unsigned int bufferFrames = 512;
  81. RtAudio::StreamParameters iParams, oParams;
  82. iParams.deviceId = iDevice;
  83. iParams.nChannels = channels;
  84. iParams.firstChannel = iOffset;
  85. oParams.deviceId = oDevice;
  86. oParams.nChannels = channels;
  87. oParams.firstChannel = oOffset;
  88. if ( iDevice == 0 )
  89. iParams.deviceId = adac.getDefaultInputDevice();
  90. if ( oDevice == 0 )
  91. oParams.deviceId = adac.getDefaultOutputDevice();
  92. RtAudio::StreamOptions options;
  93. //options.flags |= RTAUDIO_NONINTERLEAVED;
  94. bufferBytes = bufferFrames * channels * sizeof( MY_TYPE );
  95. try {
  96. adac.openStream( &oParams, &iParams, FORMAT, fs, &bufferFrames, &inout, (void *)&bufferBytes, &options );
  97. }
  98. catch ( RtAudioError& e ) {
  99. std::cout << '\n' << e.getMessage() << '\n' << std::endl;
  100. exit( 1 );
  101. }
  102. // Test RtAudio functionality for reporting latency.
  103. std::cout << "\nStream latency = " << adac.getStreamLatency() << " frames" << std::endl;
  104. try {
  105. adac.startStream();
  106. char input;
  107. std::cout << "\nRunning ... press <enter> to quit (buffer frames = " << bufferFrames << ").\n";
  108. std::cin.get(input);
  109. // Stop the stream.
  110. adac.stopStream();
  111. }
  112. catch ( RtAudioError& e ) {
  113. std::cout << '\n' << e.getMessage() << '\n' << std::endl;
  114. goto cleanup;
  115. }
  116. cleanup:
  117. if ( adac.isStreamOpen() ) adac.closeStream();
  118. return 0;
  119. }