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 <iostream.h>
  12. #include <stdio.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. */
  23. typedef float MY_TYPE;
  24. #define FORMAT RtAudio::RTAUDIO_FLOAT32
  25. /*
  26. typedef double MY_TYPE;
  27. #define FORMAT RtAudio::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. cout << "\nuseage: record_raw N fs <device>\n";
  35. cout << " where N = number of channels,\n";
  36. cout << " fs = the sample rate,\n";
  37. 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, stream, device = 0;
  43. long frames, counter = 0;
  44. MY_TYPE *buffer;
  45. FILE *fd;
  46. RtAudio *audio;
  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(&stream, 0, 0, device, chans,
  57. FORMAT, fs, &buffer_size, 8);
  58. }
  59. catch (RtError &) {
  60. exit(EXIT_FAILURE);
  61. }
  62. fd = fopen("test.raw","wb");
  63. frames = (long) (fs * TIME);
  64. try {
  65. buffer = (MY_TYPE *) audio->getStreamBuffer(stream);
  66. audio->startStream(stream);
  67. }
  68. catch (RtError &) {
  69. goto cleanup;
  70. }
  71. cout << "\nRecording for " << TIME << " seconds ... writing file test.raw (buffer size = " << buffer_size << ")." << endl;
  72. while (counter < frames) {
  73. try {
  74. audio->tickStream(stream);
  75. }
  76. catch (RtError &) {
  77. goto cleanup;
  78. }
  79. fwrite(buffer, sizeof(MY_TYPE), chans * buffer_size, fd);
  80. counter += buffer_size;
  81. }
  82. try {
  83. audio->stopStream(stream);
  84. }
  85. catch (RtError &) {
  86. }
  87. cleanup:
  88. audio->closeStream(stream);
  89. delete audio;
  90. fclose(fd);
  91. return 0;
  92. }