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.

129 lines
2.7KB

  1. /******************************************/
  2. /*
  3. play_saw.c
  4. by Gary P. Scavone, 2001
  5. Play sawtooth waveforms of distinct frequency.
  6. Takes number of channels and sample rate as
  7. input arguments. Uses blocking functionality.
  8. */
  9. /******************************************/
  10. #include "RtAudio.h"
  11. #include <iostream.h>
  12. /*
  13. typedef signed long MY_TYPE;
  14. #define FORMAT RtAudio::RTAUDIO_SINT24
  15. #define SCALE 2147483647.0
  16. typedef char MY_TYPE;
  17. #define FORMAT RtAudio::RTAUDIO_SINT8
  18. #define SCALE 127.0
  19. typedef signed short MY_TYPE;
  20. #define FORMAT RtAudio::RTAUDIO_SINT16
  21. #define SCALE 32767.0
  22. typedef signed long MY_TYPE;
  23. #define FORMAT RtAudio::RTAUDIO_SINT32
  24. #define SCALE 2147483647.0
  25. */
  26. typedef float MY_TYPE;
  27. #define FORMAT RtAudio::RTAUDIO_FLOAT32
  28. #define SCALE 1.0
  29. /*
  30. typedef double MY_TYPE;
  31. #define FORMAT RtAudio::RTAUDIO_FLOAT64
  32. #define SCALE 1.0
  33. */
  34. #define BASE_RATE 0.005
  35. #define TIME 1.0
  36. void usage(void) {
  37. /* Error function in case of incorrect command-line
  38. argument specifications
  39. */
  40. cout << "\nuseage: play_saw N fs\n";
  41. cout << " where N = number of channels,\n";
  42. cout << " and fs = the sample rate.\n\n";
  43. exit(0);
  44. }
  45. int main(int argc, char *argv[])
  46. {
  47. int chans, fs, device, buffer_size, stream;
  48. long frames, counter = 0, i, j;
  49. MY_TYPE *buffer;
  50. RtAudio *audio;
  51. double *data;
  52. // minimal command-line checking
  53. if (argc != 3) usage();
  54. chans = (int) atoi(argv[1]);
  55. fs = (int) atoi(argv[2]);
  56. // Open the realtime output device
  57. buffer_size = 256;
  58. device = 0; // default device
  59. try {
  60. audio = new RtAudio(&stream, device, chans, 0, 0,
  61. FORMAT, fs, &buffer_size, 4);
  62. }
  63. catch (RtAudioError &m) {
  64. m.printMessage();
  65. exit(EXIT_FAILURE);
  66. }
  67. frames = (long) (fs * TIME);
  68. data = (double *) calloc(chans, sizeof(double));
  69. try {
  70. buffer = (MY_TYPE *) audio->getStreamBuffer(stream);
  71. audio->startStream(stream);
  72. }
  73. catch (RtAudioError &m) {
  74. m.printMessage();
  75. goto cleanup;
  76. }
  77. cout << "\nPlaying for " << TIME << " seconds." << endl;
  78. while (counter < frames) {
  79. for (i=0; i<buffer_size; i++) {
  80. for (j=0; j<chans; j++) {
  81. buffer[i*chans+j] = (MY_TYPE) (data[j] * SCALE);
  82. data[j] += BASE_RATE * (j+1+(j*0.1));
  83. if (data[j] >= 1.0) data[j] -= 2.0;
  84. }
  85. }
  86. try {
  87. //cout << "frames until no block = " << audio->streamWillBlock(stream) << endl;
  88. audio->tickStream(stream);
  89. }
  90. catch (RtAudioError &m) {
  91. m.printMessage();
  92. goto cleanup;
  93. }
  94. counter += buffer_size;
  95. }
  96. try {
  97. audio->stopStream(stream);
  98. }
  99. catch (RtAudioError &m) {
  100. m.printMessage();
  101. }
  102. cleanup:
  103. audio->closeStream(stream);
  104. delete audio;
  105. if (data) free(data);
  106. return 0;
  107. }