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.

130 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>
  12. /*
  13. typedef signed long MY_TYPE;
  14. #define FORMAT RTAUDIO_SINT24
  15. #define SCALE 2147483647.0
  16. typedef char MY_TYPE;
  17. #define FORMAT RTAUDIO_SINT8
  18. #define SCALE 127.0
  19. */
  20. typedef signed short MY_TYPE;
  21. #define FORMAT RTAUDIO_SINT16
  22. #define SCALE 32767.0
  23. /*
  24. typedef signed long MY_TYPE;
  25. #define FORMAT RTAUDIO_SINT32
  26. #define SCALE 2147483647.0
  27. typedef float MY_TYPE;
  28. #define FORMAT RTAUDIO_FLOAT32
  29. #define SCALE 1.0
  30. typedef double MY_TYPE;
  31. #define FORMAT RTAUDIO_FLOAT64
  32. #define SCALE 1.0
  33. */
  34. #define BASE_RATE 0.005
  35. #define TIME 4.0
  36. void usage(void) {
  37. // Error function in case of incorrect command-line
  38. // argument specifications.
  39. std::cout << "\nuseage: play_saw N fs <device>\n";
  40. std::cout << " where N = number of channels,\n";
  41. std::cout << " fs = the sample rate,\n";
  42. std::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, device = 0;
  48. long frames, counter = 0, i, j;
  49. MY_TYPE *buffer;
  50. RtAudio *audio;
  51. double *data = 0;
  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(device, chans, 0, 0,
  62. FORMAT, fs, &buffer_size, 4);
  63. }
  64. catch (RtError &error) {
  65. error.printMessage();
  66. exit(EXIT_FAILURE);
  67. }
  68. frames = (long) (fs * TIME);
  69. data = (double *) calloc(chans, sizeof(double));
  70. try {
  71. buffer = (MY_TYPE *) audio->getStreamBuffer();
  72. audio->startStream();
  73. }
  74. catch (RtError &error) {
  75. error.printMessage();
  76. goto cleanup;
  77. }
  78. std::cout << "\nPlaying for " << TIME << " seconds ... buffer size = " << buffer_size << "." << std::endl;
  79. while (counter < frames) {
  80. for (i=0; i<buffer_size; i++) {
  81. for (j=0; j<chans; j++) {
  82. buffer[i*chans+j] = (MY_TYPE) (data[j] * SCALE);
  83. data[j] += BASE_RATE * (j+1+(j*0.1));
  84. if (data[j] >= 1.0) data[j] -= 2.0;
  85. }
  86. }
  87. try {
  88. audio->tickStream();
  89. }
  90. catch (RtError &error) {
  91. error.printMessage();
  92. goto cleanup;
  93. }
  94. counter += buffer_size;
  95. }
  96. try {
  97. audio->stopStream();
  98. }
  99. catch (RtError &error) {
  100. error.printMessage();
  101. }
  102. cleanup:
  103. audio->closeStream();
  104. delete audio;
  105. if (data) free(data);
  106. return 0;
  107. }