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.

132 lines
3.6KB

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