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.

128 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. */
  17. typedef char MY_TYPE;
  18. #define FORMAT RtAudio::RTAUDIO_SINT8
  19. #define SCALE 127.0
  20. /*
  21. typedef signed short MY_TYPE;
  22. #define FORMAT RtAudio::RTAUDIO_SINT16
  23. #define SCALE 32767.0
  24. typedef signed long MY_TYPE;
  25. #define FORMAT RtAudio::RTAUDIO_SINT32
  26. #define SCALE 2147483647.0
  27. typedef float MY_TYPE;
  28. #define FORMAT RtAudio::RTAUDIO_FLOAT32
  29. #define SCALE 1.0
  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. cout << "\nuseage: play_saw N fs <device>\n";
  40. cout << " where N = number of channels,\n";
  41. cout << " fs = the sample rate,\n";
  42. cout << " and device = the device to use (default = 0).\n\n";
  43. exit(0);
  44. }
  45. int main(int argc, char *argv[])
  46. {
  47. int chans, fs, buffer_size, stream, device = 0;
  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 && argc != 4 ) usage();
  54. chans = (int) atoi(argv[1]);
  55. fs = (int) atoi(argv[2]);
  56. if ( argc == 4 )
  57. device = (int) atoi(argv[3]);
  58. // Open the realtime output device
  59. buffer_size = 512;
  60. try {
  61. audio = new RtAudio(&stream, device, chans, 0, 0,
  62. FORMAT, fs, &buffer_size, 4);
  63. }
  64. catch (RtError &) {
  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 (RtError &) {
  74. goto cleanup;
  75. }
  76. cout << "\nPlaying for " << TIME << " seconds ... buffer size = " << buffer_size << "." << endl;
  77. while (counter < frames) {
  78. for (i=0; i<buffer_size; i++) {
  79. for (j=0; j<chans; j++) {
  80. buffer[i*chans+j] = (MY_TYPE) (data[j] * SCALE);
  81. data[j] += BASE_RATE * (j+1+(j*0.1));
  82. if (data[j] >= 1.0) data[j] -= 2.0;
  83. }
  84. }
  85. try {
  86. //cout << "frames until no block = " << audio->streamWillBlock(stream) << endl;
  87. audio->tickStream(stream);
  88. }
  89. catch (RtError &) {
  90. goto cleanup;
  91. }
  92. counter += buffer_size;
  93. }
  94. try {
  95. audio->stopStream(stream);
  96. }
  97. catch (RtError &) {
  98. }
  99. cleanup:
  100. audio->closeStream(stream);
  101. delete audio;
  102. if (data) free(data);
  103. return 0;
  104. }