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.

161 lines
3.3KB

  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 (RtError &) {
  76. exit(EXIT_FAILURE);
  77. }
  78. try {
  79. stream1 = audio->openStream(0, 0, device, chans,
  80. FORMAT, fs, &buffer_size, 8);
  81. stream2 = audio->openStream(device, chans, 0, 0,
  82. FORMAT, fs, &buffer_size, 8);
  83. }
  84. catch (RtError &) {
  85. goto cleanup;
  86. }
  87. data = (MY_TYPE *) calloc(chans*buffer_size, sizeof(MY_TYPE));
  88. try {
  89. audio->setStreamCallback(stream1, &in, (void *)data);
  90. audio->setStreamCallback(stream2, &out, (void *)data);
  91. audio->startStream(stream1);
  92. audio->startStream(stream2);
  93. }
  94. catch (RtError &) {
  95. goto cleanup;
  96. }
  97. cout << "\nRunning two streams (quasi-duplex) ... press <enter> to quit.\n";
  98. cin.get(input);
  99. cout << "\nStopping both streams.\n";
  100. try {
  101. audio->stopStream(stream1);
  102. audio->stopStream(stream2);
  103. }
  104. catch (RtError &) {
  105. goto cleanup;
  106. }
  107. cout << "\nPress <enter> to restart streams:\n";
  108. cin.get(input);
  109. try {
  110. audio->startStream(stream1);
  111. audio->startStream(stream2);
  112. }
  113. catch (RtError &) {
  114. goto cleanup;
  115. }
  116. cout << "\nRunning two streams (quasi-duplex) ... press <enter> to quit.\n";
  117. cin.get(input);
  118. try {
  119. audio->stopStream(stream1);
  120. audio->stopStream(stream2);
  121. }
  122. catch (RtError &) {
  123. }
  124. cleanup:
  125. audio->closeStream(stream1);
  126. audio->closeStream(stream2);
  127. delete audio;
  128. if (data) free(data);
  129. return 0;
  130. }