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.

131 lines
2.7KB

  1. /******************************************/
  2. /*
  3. call_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. Use callback 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. typedef signed short MY_TYPE;
  20. #define FORMAT RTAUDIO_SINT16
  21. #define SCALE 32767.0
  22. typedef signed long MY_TYPE;
  23. #define FORMAT RTAUDIO_SINT32
  24. #define SCALE 2147483647.0
  25. */
  26. typedef float MY_TYPE;
  27. #define FORMAT RTAUDIO_FLOAT32
  28. #define SCALE 1.0
  29. /*
  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 1.0
  36. void usage(void) {
  37. /* Error function in case of incorrect command-line
  38. argument specifications
  39. */
  40. std::cout << "\nuseage: call_saw N fs <device>\n";
  41. std::cout << " where N = number of channels,\n";
  42. std::cout << " fs = the sample rate,\n";
  43. std::cout << " and device = the device to use (default = 0).\n\n";
  44. exit(0);
  45. }
  46. int chans;
  47. int saw(char *buffer, int buffer_size, void *data)
  48. {
  49. int i, j;
  50. extern int chans;
  51. MY_TYPE *my_buffer = (MY_TYPE *) buffer;
  52. double *my_data = (double *) data;
  53. for (i=0; i<buffer_size; i++) {
  54. for (j=0; j<chans; j++) {
  55. *my_buffer++ = (MY_TYPE) (my_data[j] * SCALE);
  56. my_data[j] += BASE_RATE * (j+1+(j*0.1));
  57. if (my_data[j] >= 1.0) my_data[j] -= 2.0;
  58. }
  59. }
  60. return 0;
  61. }
  62. int main(int argc, char *argv[])
  63. {
  64. int buffer_size, fs, device = 0;
  65. RtAudio *audio = 0;
  66. double *data = 0;
  67. char input;
  68. // minimal command-line checking
  69. if (argc != 3 && argc != 4 ) usage();
  70. chans = (int) atoi(argv[1]);
  71. fs = (int) atoi(argv[2]);
  72. if ( argc == 4 )
  73. device = (int) atoi(argv[3]);
  74. // Open the realtime output device
  75. buffer_size = 1024;
  76. try {
  77. audio = new RtAudio(device, chans, 0, 0,
  78. FORMAT, fs, &buffer_size, 4);
  79. }
  80. catch (RtError &error) {
  81. error.printMessage();
  82. exit(EXIT_FAILURE);
  83. }
  84. data = (double *) calloc(chans, sizeof(double));
  85. try {
  86. audio->setStreamCallback(&saw, (void *)data);
  87. audio->startStream();
  88. }
  89. catch (RtError &error) {
  90. error.printMessage();
  91. goto cleanup;
  92. }
  93. std::cout << "\nPlaying ... press <enter> to quit (buffer size = " << buffer_size << ").\n";
  94. std::cin.get(input);
  95. // Stop the stream.
  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. }