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.

125 lines
2.6KB

  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 (RtError &) {
  64. exit(EXIT_FAILURE);
  65. }
  66. frames = (long) (fs * TIME);
  67. data = (double *) calloc(chans, sizeof(double));
  68. try {
  69. buffer = (MY_TYPE *) audio->getStreamBuffer(stream);
  70. audio->startStream(stream);
  71. }
  72. catch (RtError &) {
  73. goto cleanup;
  74. }
  75. cout << "\nPlaying for " << TIME << " seconds." << endl;
  76. while (counter < frames) {
  77. for (i=0; i<buffer_size; i++) {
  78. for (j=0; j<chans; j++) {
  79. buffer[i*chans+j] = (MY_TYPE) (data[j] * SCALE);
  80. data[j] += BASE_RATE * (j+1+(j*0.1));
  81. if (data[j] >= 1.0) data[j] -= 2.0;
  82. }
  83. }
  84. try {
  85. //cout << "frames until no block = " << audio->streamWillBlock(stream) << endl;
  86. audio->tickStream(stream);
  87. }
  88. catch (RtError &) {
  89. goto cleanup;
  90. }
  91. counter += buffer_size;
  92. }
  93. try {
  94. audio->stopStream(stream);
  95. }
  96. catch (RtError &) {
  97. }
  98. cleanup:
  99. audio->closeStream(stream);
  100. delete audio;
  101. if (data) free(data);
  102. return 0;
  103. }