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.

126 lines
2.5KB

  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.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: call_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 chans;
  46. int saw(char *buffer, int buffer_size, void *data)
  47. {
  48. int i, j;
  49. extern int chans;
  50. MY_TYPE *my_buffer = (MY_TYPE *) buffer;
  51. double *my_data = (double *) data;
  52. for (i=0; i<buffer_size; i++) {
  53. for (j=0; j<chans; j++) {
  54. *my_buffer++ = (MY_TYPE) (my_data[j] * SCALE);
  55. my_data[j] += BASE_RATE * (j+1+(j*0.1));
  56. if (my_data[j] >= 1.0) my_data[j] -= 2.0;
  57. }
  58. }
  59. return 0;
  60. }
  61. int main(int argc, char *argv[])
  62. {
  63. int device, stream, buffer_size, fs;
  64. RtAudio *audio;
  65. double *data;
  66. char input;
  67. // minimal command-line checking
  68. if (argc != 3) usage();
  69. chans = (int) atoi(argv[1]);
  70. fs = (int) atoi(argv[2]);
  71. // Open the realtime output device
  72. buffer_size = 256;
  73. device = 0; // default device
  74. try {
  75. audio = new RtAudio(&stream, device, chans, 0, 0,
  76. FORMAT, fs, &buffer_size, 4);
  77. }
  78. catch (RtError &) {
  79. exit(EXIT_FAILURE);
  80. }
  81. data = (double *) calloc(chans, sizeof(double));
  82. try {
  83. audio->setStreamCallback(stream, &saw, (void *)data);
  84. audio->startStream(stream);
  85. }
  86. catch (RtError &) {
  87. goto cleanup;
  88. }
  89. cout << "\nPlaying ... press <enter> to quit.\n";
  90. cin.get(input);
  91. // Stop the stream.
  92. try {
  93. audio->stopStream(stream);
  94. }
  95. catch (RtError &) {
  96. }
  97. cleanup:
  98. audio->closeStream(stream);
  99. delete audio;
  100. if (data) free(data);
  101. return 0;
  102. }