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.

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