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.

167 lines
3.5KB

  1. /******************************************/
  2. /*
  3. twostreams.cpp
  4. by Gary P. Scavone, 2001
  5. Text executable using two streams with
  6. callbacks.
  7. */
  8. /******************************************/
  9. #include "RtAudio.h"
  10. #include <iostream.h>
  11. /*
  12. typedef signed long MY_TYPE;
  13. #define FORMAT RtAudio::RTAUDIO_SINT24
  14. #define SCALE 2147483647.0
  15. typedef char MY_TYPE;
  16. #define FORMAT RtAudio::RTAUDIO_SINT8
  17. #define SCALE 127.0
  18. typedef signed short MY_TYPE;
  19. #define FORMAT RtAudio::RTAUDIO_SINT16
  20. #define SCALE 32767.0
  21. typedef signed long MY_TYPE;
  22. #define FORMAT RtAudio::RTAUDIO_SINT32
  23. #define SCALE 2147483647.0
  24. typedef float MY_TYPE;
  25. #define FORMAT RtAudio::RTAUDIO_FLOAT32
  26. #define SCALE 1.0
  27. */
  28. typedef double MY_TYPE;
  29. #define FORMAT RtAudio::RTAUDIO_FLOAT64
  30. #define SCALE 1.0
  31. void usage(void) {
  32. /* Error function in case of incorrect command-line
  33. argument specifications
  34. */
  35. cout << "\nuseage: call_twostreams N fs\n";
  36. cout << " where N = number of channels,\n";
  37. cout << " and fs = the sample rate.\n\n";
  38. exit(0);
  39. }
  40. int chans;
  41. int in(char *buffer, int buffer_size, void *data)
  42. {
  43. extern int chans;
  44. MY_TYPE *my_buffer = (MY_TYPE *) buffer;
  45. MY_TYPE *my_data = (MY_TYPE *) data;
  46. long buffer_bytes = buffer_size * chans * sizeof(MY_TYPE);
  47. memcpy(my_data, my_buffer, buffer_bytes);
  48. return 0;
  49. }
  50. int out(char *buffer, int buffer_size, void *data)
  51. {
  52. extern int chans;
  53. MY_TYPE *my_buffer = (MY_TYPE *) buffer;
  54. MY_TYPE *my_data = (MY_TYPE *) data;
  55. long buffer_bytes = buffer_size * chans * sizeof(MY_TYPE);
  56. memcpy(my_buffer, my_data, buffer_bytes);
  57. return 0;
  58. }
  59. int main(int argc, char *argv[])
  60. {
  61. int device, buffer_size, stream1, stream2, fs;
  62. MY_TYPE *data = 0;
  63. RtAudio *audio;
  64. char input;
  65. // minimal command-line checking
  66. if (argc != 3) usage();
  67. chans = (int) atoi(argv[1]);
  68. fs = (int) atoi(argv[2]);
  69. // Open the realtime output device
  70. buffer_size = 512;
  71. device = 0; // default device
  72. try {
  73. audio = new RtAudio();
  74. }
  75. catch (RtAudioError &m) {
  76. m.printMessage();
  77. exit(EXIT_FAILURE);
  78. }
  79. try {
  80. stream1 = audio->openStream(0, 0, device, chans,
  81. FORMAT, fs, &buffer_size, 8);
  82. stream2 = audio->openStream(device, chans, 0, 0,
  83. FORMAT, fs, &buffer_size, 8);
  84. }
  85. catch (RtAudioError &m) {
  86. m.printMessage();
  87. goto cleanup;
  88. }
  89. data = (MY_TYPE *) calloc(chans*buffer_size, sizeof(MY_TYPE));
  90. try {
  91. audio->setStreamCallback(stream1, &in, (void *)data);
  92. audio->setStreamCallback(stream2, &out, (void *)data);
  93. audio->startStream(stream1);
  94. audio->startStream(stream2);
  95. }
  96. catch (RtAudioError &m) {
  97. m.printMessage();
  98. goto cleanup;
  99. }
  100. cout << "\nRunning two streams (quasi-duplex) ... press <enter> to quit.\n";
  101. cin.get(input);
  102. cout << "\nStopping both streams.\n";
  103. try {
  104. audio->stopStream(stream1);
  105. audio->stopStream(stream2);
  106. }
  107. catch (RtAudioError &m) {
  108. m.printMessage();
  109. goto cleanup;
  110. }
  111. cout << "\nPress <enter> to restart streams:\n";
  112. cin.get(input);
  113. try {
  114. audio->startStream(stream1);
  115. audio->startStream(stream2);
  116. }
  117. catch (RtAudioError &m) {
  118. m.printMessage();
  119. goto cleanup;
  120. }
  121. cout << "\nRunning two streams (quasi-duplex) ... press <enter> to quit.\n";
  122. cin.get(input);
  123. try {
  124. audio->stopStream(stream1);
  125. audio->stopStream(stream2);
  126. }
  127. catch (RtAudioError &m) {
  128. m.printMessage();
  129. }
  130. cleanup:
  131. audio->closeStream(stream1);
  132. audio->closeStream(stream2);
  133. delete audio;
  134. if (data) free(data);
  135. return 0;
  136. }