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.

108 lines
2.2KB

  1. /******************************************/
  2. /*
  3. in_out.c
  4. by Gary P. Scavone, 2001
  5. Records from default input and passes it
  6. through to the output. Takes number of
  7. channels and sample rate as input arguments.
  8. Use blocking functionality.
  9. */
  10. /******************************************/
  11. #include "RtAudio.h"
  12. #include <iostream.h>
  13. /*
  14. typedef signed long MY_TYPE;
  15. #define FORMAT RtAudio::RTAUDIO_SINT24
  16. typedef char MY_TYPE;
  17. #define FORMAT RtAudio::RTAUDIO_SINT8
  18. typedef signed short MY_TYPE;
  19. #define FORMAT RtAudio::RTAUDIO_SINT16
  20. typedef signed long MY_TYPE;
  21. #define FORMAT RtAudio::RTAUDIO_SINT32
  22. typedef float MY_TYPE;
  23. #define FORMAT RtAudio::RTAUDIO_FLOAT32
  24. */
  25. typedef double MY_TYPE;
  26. #define FORMAT RtAudio::RTAUDIO_FLOAT64
  27. #define TIME 4.0
  28. void usage(void) {
  29. /* Error function in case of incorrect command-line
  30. argument specifications
  31. */
  32. cout << "\nuseage: in_out N fs <device>\n";
  33. cout << " where N = number of channels,\n";
  34. cout << " fs = the sample rate,\n";
  35. cout << " and device = the device to use (default = 0).\n\n";
  36. exit(0);
  37. }
  38. int main(int argc, char *argv[])
  39. {
  40. int chans, fs, buffer_size, stream, device = 0;
  41. long frames, counter = 0;
  42. MY_TYPE *buffer;
  43. RtAudio *audio;
  44. // minimal command-line checking
  45. if (argc != 3 && argc != 4 ) usage();
  46. chans = (int) atoi(argv[1]);
  47. fs = (int) atoi(argv[2]);
  48. if ( argc == 4 )
  49. device = (int) atoi(argv[3]);
  50. // Open the realtime output device
  51. buffer_size = 512;
  52. try {
  53. audio = new RtAudio(&stream, device, chans, device, chans,
  54. FORMAT, fs, &buffer_size, 8);
  55. }
  56. catch (RtError &) {
  57. exit(EXIT_FAILURE);
  58. }
  59. frames = (long) (fs * TIME);
  60. try {
  61. buffer = (MY_TYPE *) audio->getStreamBuffer(stream);
  62. audio->startStream(stream);
  63. }
  64. catch (RtError &) {
  65. goto cleanup;
  66. }
  67. cout << "\nRunning for " << TIME << " seconds ... fragment_size = " << buffer_size << endl;
  68. while (counter < frames) {
  69. try {
  70. audio->tickStream(stream);
  71. }
  72. catch (RtError &) {
  73. goto cleanup;
  74. }
  75. counter += buffer_size;
  76. }
  77. try {
  78. audio->stopStream(stream);
  79. }
  80. catch (RtError &) {
  81. }
  82. cleanup:
  83. audio->closeStream(stream);
  84. delete audio;
  85. return 0;
  86. }