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.6KB

  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 <device>\n";
  41. cout << " where N = number of channels,\n";
  42. cout << " fs = the sample rate,\n";
  43. 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 stream, buffer_size, fs, device = 0;
  65. RtAudio *audio;
  66. double *data;
  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(&stream, device, chans, 0, 0,
  78. FORMAT, fs, &buffer_size, 4);
  79. }
  80. catch (RtError &) {
  81. exit(EXIT_FAILURE);
  82. }
  83. data = (double *) calloc(chans, sizeof(double));
  84. try {
  85. audio->setStreamCallback(stream, &saw, (void *)data);
  86. audio->startStream(stream);
  87. }
  88. catch (RtError &) {
  89. goto cleanup;
  90. }
  91. cout << "\nPlaying ... press <enter> to quit (buffer size = " << buffer_size << ").\n";
  92. cin.get(input);
  93. // Stop the stream.
  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. }