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.

duplex.cpp 3.7KB

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