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.

163 lines
3.5KB

  1. /******************************************/
  2. /*
  3. twostreams.cpp
  4. by Gary P. Scavone, 2001
  5. Test 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 <device>\n";
  36. cout << " where N = number of channels,\n";
  37. cout << " fs = the sample rate,\n";
  38. cout << " and device = the device to use (default = 0).\n\n";
  39. exit(0);
  40. }
  41. int chans;
  42. int in(char *buffer, int buffer_size, void *data)
  43. {
  44. extern int chans;
  45. MY_TYPE *my_buffer = (MY_TYPE *) buffer;
  46. MY_TYPE *my_data = (MY_TYPE *) data;
  47. long buffer_bytes = buffer_size * chans * sizeof(MY_TYPE);
  48. memcpy(my_data, my_buffer, buffer_bytes);
  49. return 0;
  50. }
  51. int out(char *buffer, int buffer_size, void *data)
  52. {
  53. extern int chans;
  54. MY_TYPE *my_buffer = (MY_TYPE *) buffer;
  55. MY_TYPE *my_data = (MY_TYPE *) data;
  56. long buffer_bytes = buffer_size * chans * sizeof(MY_TYPE);
  57. memcpy(my_buffer, my_data, buffer_bytes);
  58. return 0;
  59. }
  60. int main(int argc, char *argv[])
  61. {
  62. int buffer_size, stream1 = 0, stream2 = 0, fs, device = 0;
  63. MY_TYPE *data = 0;
  64. RtAudio *audio;
  65. char input;
  66. // minimal command-line checking
  67. if (argc != 3 && argc != 4 ) usage();
  68. chans = (int) atoi(argv[1]);
  69. fs = (int) atoi(argv[2]);
  70. if ( argc == 4 )
  71. device = (int) atoi(argv[3]);
  72. // Open the realtime output device
  73. buffer_size = 512;
  74. try {
  75. audio = new RtAudio();
  76. }
  77. catch (RtError &) {
  78. exit(EXIT_FAILURE);
  79. }
  80. try {
  81. stream1 = audio->openStream(0, 0, device, chans,
  82. FORMAT, fs, &buffer_size, 8);
  83. stream2 = audio->openStream(device, chans, 0, 0,
  84. FORMAT, fs, &buffer_size, 8);
  85. }
  86. catch (RtError &) {
  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 (RtError &) {
  97. goto cleanup;
  98. }
  99. cout << "\nRunning two streams (quasi-duplex) ... press <enter> to quit.\n";
  100. cin.get(input);
  101. cout << "\nStopping both streams.\n";
  102. try {
  103. audio->stopStream(stream1);
  104. audio->stopStream(stream2);
  105. }
  106. catch (RtError &) {
  107. goto cleanup;
  108. }
  109. cout << "\nPress <enter> to restart streams:\n";
  110. cin.get(input);
  111. try {
  112. audio->startStream(stream1);
  113. audio->startStream(stream2);
  114. }
  115. catch (RtError &) {
  116. goto cleanup;
  117. }
  118. cout << "\nRunning two streams (quasi-duplex) ... press <enter> to quit.\n";
  119. cin.get(input);
  120. try {
  121. audio->stopStream(stream1);
  122. audio->stopStream(stream2);
  123. }
  124. catch (RtError &) {
  125. }
  126. cleanup:
  127. audio->closeStream(stream1);
  128. audio->closeStream(stream2);
  129. delete audio;
  130. if (data) free(data);
  131. return 0;
  132. }