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.

115 lines
2.3KB

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