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.

179 lines
3.7KB

  1. /******************************************/
  2. /*
  3. call_playtwo.cpp
  4. by Gary P. Scavone, 2002.
  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. #define BASE_RATE1 0.005
  32. #define BASE_RATE2 0.004
  33. void usage(void) {
  34. /* Error function in case of incorrect command-line
  35. argument specifications
  36. */
  37. cout << "\nuseage: call_twostreams N fs\n";
  38. cout << " where N = number of channels,\n";
  39. cout << " and fs = the sample rate.\n\n";
  40. exit(0);
  41. }
  42. int chans;
  43. int saw1(char *buffer, int buffer_size, void *data)
  44. {
  45. int i, j;
  46. extern int chans;
  47. MY_TYPE *my_buffer = (MY_TYPE *) buffer;
  48. double *my_data = (double *) data;
  49. for (i=0; i<buffer_size; i++) {
  50. for (j=0; j<chans; j++) {
  51. *my_buffer++ = (MY_TYPE) (my_data[j] * SCALE);
  52. my_data[j] += BASE_RATE1 * (j+1+(j*0.1));
  53. if (my_data[j] >= 1.0) my_data[j] -= 2.0;
  54. }
  55. }
  56. return 0;
  57. }
  58. int saw2(char *buffer, int buffer_size, void *data)
  59. {
  60. int i, j;
  61. extern int chans;
  62. MY_TYPE *my_buffer = (MY_TYPE *) buffer;
  63. double *my_data = (double *) data;
  64. for (i=0; i<buffer_size; i++) {
  65. for (j=0; j<chans; j++) {
  66. *my_buffer++ = (MY_TYPE) (my_data[j] * SCALE);
  67. my_data[j] += BASE_RATE2 * (j+1+(j*0.1));
  68. if (my_data[j] >= 1.0) my_data[j] -= 2.0;
  69. }
  70. }
  71. return 0;
  72. }
  73. int main(int argc, char *argv[])
  74. {
  75. int device, buffer_size, stream1 = 0, stream2 = 0, fs;
  76. double *data1 = 0;
  77. double *data2 = 0;
  78. RtAudio *audio;
  79. char input;
  80. // minimal command-line checking
  81. if (argc != 3) usage();
  82. chans = (int) atoi(argv[1]);
  83. fs = (int) atoi(argv[2]);
  84. // Open the realtime output device
  85. buffer_size = 512;
  86. device = 0; // default device
  87. try {
  88. audio = new RtAudio();
  89. }
  90. catch (RtError &) {
  91. exit(EXIT_FAILURE);
  92. }
  93. try {
  94. stream1 = audio->openStream(device, chans, 0, 0,
  95. FORMAT, fs, &buffer_size, 8);
  96. stream2 = audio->openStream(device, chans, 0, 0,
  97. FORMAT, fs, &buffer_size, 8);
  98. }
  99. catch (RtError &) {
  100. goto cleanup;
  101. }
  102. data1 = (double *) calloc(chans, sizeof(double));
  103. data2 = (double *) calloc(chans, sizeof(double));
  104. try {
  105. audio->setStreamCallback(stream1, &saw1, (void *)data1);
  106. audio->setStreamCallback(stream2, &saw2, (void *)data2);
  107. audio->startStream(stream1);
  108. audio->startStream(stream2);
  109. }
  110. catch (RtError &) {
  111. goto cleanup;
  112. }
  113. cout << "\nRunning two streams ... press <enter> to quit.\n";
  114. cin.get(input);
  115. cout << "\nStopping both streams.\n";
  116. try {
  117. audio->stopStream(stream1);
  118. audio->stopStream(stream2);
  119. }
  120. catch (RtError &) {
  121. goto cleanup;
  122. }
  123. cout << "\nPress <enter> to restart streams:\n";
  124. cin.get(input);
  125. try {
  126. audio->startStream(stream1);
  127. audio->startStream(stream2);
  128. }
  129. catch (RtError &) {
  130. goto cleanup;
  131. }
  132. cout << "\nRunning two streams (quasi-duplex) ... press <enter> to quit.\n";
  133. cin.get(input);
  134. try {
  135. audio->stopStream(stream1);
  136. audio->stopStream(stream2);
  137. }
  138. catch (RtError &) {
  139. }
  140. cleanup:
  141. audio->closeStream(stream1);
  142. audio->closeStream(stream2);
  143. delete audio;
  144. if (data1) free(data1);
  145. if (data2) free(data2);
  146. return 0;
  147. }