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.

110 lines
2.2KB

  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 <stdio.h>
  12. /*
  13. typedef char MY_TYPE;
  14. #define FORMAT RtAudio::RTAUDIO_SINT8
  15. typedef signed short MY_TYPE;
  16. #define FORMAT RtAudio::RTAUDIO_SINT16
  17. typedef signed long MY_TYPE;
  18. #define FORMAT RtAudio::RTAUDIO_SINT24
  19. typedef signed long MY_TYPE;
  20. #define FORMAT RtAudio::RTAUDIO_SINT32
  21. typedef float MY_TYPE;
  22. #define FORMAT RtAudio::RTAUDIO_FLOAT32
  23. */
  24. typedef double MY_TYPE;
  25. #define FORMAT RtAudio::RTAUDIO_FLOAT64
  26. #define TIME 2.0
  27. void usage(void) {
  28. /* Error function in case of incorrect command-line
  29. argument specifications
  30. */
  31. cout << "\nuseage: record_raw N fs\n";
  32. cout << " where N = number of channels,\n";
  33. cout << " and fs = the sample rate.\n\n";
  34. exit(0);
  35. }
  36. int main(int argc, char *argv[])
  37. {
  38. int chans, fs, device, buffer_size, stream;
  39. long frames, counter = 0;
  40. MY_TYPE *buffer;
  41. FILE *fd;
  42. RtAudio *audio;
  43. // minimal command-line checking
  44. if (argc != 3) usage();
  45. chans = (int) atoi(argv[1]);
  46. fs = (int) atoi(argv[2]);
  47. // Open the realtime output device
  48. buffer_size = 512;
  49. device = 0; // default device
  50. try {
  51. audio = new RtAudio(&stream, 0, 0, device, chans,
  52. FORMAT, fs, &buffer_size, 8);
  53. }
  54. catch (RtError &) {
  55. exit(EXIT_FAILURE);
  56. }
  57. fd = fopen("test.raw","wb");
  58. frames = (long) (fs * TIME);
  59. try {
  60. buffer = (MY_TYPE *) audio->getStreamBuffer(stream);
  61. audio->startStream(stream);
  62. }
  63. catch (RtError &) {
  64. goto cleanup;
  65. }
  66. cout << "\nRecording for " << TIME << " seconds ... writing file test.raw." << endl;
  67. while (counter < frames) {
  68. try {
  69. audio->tickStream(stream);
  70. }
  71. catch (RtError &) {
  72. goto cleanup;
  73. }
  74. fwrite(buffer, sizeof(MY_TYPE), chans * buffer_size, fd);
  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. fclose(fd);
  86. return 0;
  87. }