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.

134 lines
3.7KB

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