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.

119 lines
2.4KB

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