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.

170 lines
4.7KB

  1. /******************************************/
  2. /*
  3. record.cpp
  4. by Gary P. Scavone, 2007
  5. This program records audio from a device and writes it to a
  6. header-less binary file. Use the 'playraw', with the same
  7. parameters and format settings, to playback the audio.
  8. */
  9. /******************************************/
  10. #include "RtAudio.h"
  11. #include <iostream>
  12. /*
  13. typedef char MY_TYPE;
  14. #define FORMAT RTAUDIO_SINT8
  15. typedef signed short MY_TYPE;
  16. #define FORMAT RTAUDIO_SINT16
  17. typedef signed long MY_TYPE;
  18. #define FORMAT RTAUDIO_SINT24
  19. typedef signed long MY_TYPE;
  20. #define FORMAT RTAUDIO_SINT32
  21. */
  22. typedef float MY_TYPE;
  23. #define FORMAT RTAUDIO_FLOAT32
  24. /*
  25. typedef double MY_TYPE;
  26. #define FORMAT RTAUDIO_FLOAT64
  27. */
  28. // Platform-dependent sleep routines.
  29. #if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ )
  30. #include <windows.h>
  31. #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
  32. #else // Unix variants
  33. #include <unistd.h>
  34. #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
  35. #endif
  36. void usage( void ) {
  37. // Error function in case of incorrect command-line
  38. // argument specifications
  39. std::cout << "\nuseage: record N fs <duration> <device> <channelOffset>\n";
  40. std::cout << " where N = number of channels,\n";
  41. std::cout << " fs = the sample rate,\n";
  42. std::cout << " duration = optional time in seconds to record (default = 2.0),\n";
  43. std::cout << " device = optional device to use (default = 0),\n";
  44. std::cout << " and channelOffset = an optional channel offset on the device (default = 0).\n\n";
  45. exit( 0 );
  46. }
  47. struct InputData {
  48. MY_TYPE* buffer;
  49. unsigned long bufferBytes;
  50. unsigned long totalFrames;
  51. unsigned long frameCounter;
  52. unsigned int channels;
  53. };
  54. // Interleaved buffers
  55. int input( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
  56. double streamTime, RtAudioStreamStatus status, void *data )
  57. {
  58. InputData *iData = (InputData *) data;
  59. // Simply copy the data to our allocated buffer.
  60. unsigned int frames = nBufferFrames;
  61. if ( iData->frameCounter + nBufferFrames > iData->totalFrames ) {
  62. frames = iData->totalFrames - iData->frameCounter;
  63. iData->bufferBytes = frames * iData->channels * sizeof( MY_TYPE );
  64. }
  65. unsigned long offset = iData->frameCounter * iData->channels;
  66. memcpy( iData->buffer+offset, inputBuffer, iData->bufferBytes );
  67. iData->frameCounter += frames;
  68. if ( iData->frameCounter >= iData->totalFrames ) return 2;
  69. return 0;
  70. }
  71. int main( int argc, char *argv[] )
  72. {
  73. unsigned int channels, fs, bufferFrames, device = 0, offset = 0;
  74. double time = 2.0;
  75. FILE *fd;
  76. // minimal command-line checking
  77. if ( argc < 3 || argc > 6 ) usage();
  78. RtAudio adc;
  79. if ( adc.getDeviceCount() < 1 ) {
  80. std::cout << "\nNo audio devices found!\n";
  81. exit( 0 );
  82. }
  83. channels = (unsigned int) atoi( argv[1] );
  84. fs = (unsigned int) atoi( argv[2] );
  85. if ( argc > 3 )
  86. time = (double) atof( argv[3] );
  87. if ( argc > 4 )
  88. device = (unsigned int) atoi( argv[4] );
  89. if ( argc > 5 )
  90. offset = (unsigned int) atoi( argv[5] );
  91. // Let RtAudio print messages to stderr.
  92. adc.showWarnings( true );
  93. // Set our stream parameters for input only.
  94. bufferFrames = 512;
  95. RtAudio::StreamParameters iParams;
  96. iParams.deviceId = device;
  97. iParams.nChannels = channels;
  98. iParams.firstChannel = offset;
  99. InputData data;
  100. data.buffer = 0;
  101. try {
  102. adc.openStream( NULL, &iParams, FORMAT, fs, &bufferFrames, &input, (void *)&data );
  103. }
  104. catch ( RtError& e ) {
  105. std::cout << '\n' << e.getMessage() << '\n' << std::endl;
  106. goto cleanup;
  107. }
  108. data.bufferBytes = bufferFrames * channels * sizeof( MY_TYPE );
  109. data.totalFrames = (unsigned long) (fs * time);
  110. data.frameCounter = 0;
  111. data.channels = channels;
  112. unsigned long totalBytes;
  113. totalBytes = data.totalFrames * channels * sizeof( MY_TYPE );
  114. // Allocate the entire data buffer before starting stream.
  115. data.buffer = (MY_TYPE*) malloc( totalBytes );
  116. if ( data.buffer == 0 ) {
  117. std::cout << "Memory allocation error ... quitting!\n";
  118. goto cleanup;
  119. }
  120. try {
  121. adc.startStream();
  122. }
  123. catch ( RtError& e ) {
  124. std::cout << '\n' << e.getMessage() << '\n' << std::endl;
  125. goto cleanup;
  126. }
  127. std::cout << "\nRecording for " << time << " seconds ... writing file 'record.raw' (buffer frames = " << bufferFrames << ")." << std::endl;
  128. while ( 1 ) {
  129. SLEEP( 100 ); // wake every 100 ms to check if we're done
  130. if ( adc.isStreamRunning() == false ) break;
  131. }
  132. // Now write the entire data to the file.
  133. fd = fopen( "record.raw", "wb" );
  134. fwrite( data.buffer, sizeof( MY_TYPE ), data.totalFrames * channels, fd );
  135. fclose(fd);
  136. cleanup:
  137. if ( adc.isStreamOpen() ) adc.closeStream();
  138. if ( data.buffer ) free( data.buffer );
  139. return 0;
  140. }