1 /******************************************/
4 by Gary P. Scavone, 2007
6 This program records audio from a device and writes it to a
7 header-less binary file. Use the 'playraw', with the same
8 parameters and format settings, to playback the audio.
10 /******************************************/
20 #define FORMAT RTAUDIO_SINT8
23 typedef signed short MY_TYPE;
24 #define FORMAT RTAUDIO_SINT16
27 typedef signed long MY_TYPE;
28 #define FORMAT RTAUDIO_SINT24
30 typedef signed long MY_TYPE;
31 #define FORMAT RTAUDIO_SINT32
33 typedef float MY_TYPE;
34 #define FORMAT RTAUDIO_FLOAT32
36 typedef double MY_TYPE;
37 #define FORMAT RTAUDIO_FLOAT64
40 // Platform-dependent sleep routines.
41 #if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ )
43 #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
44 #else // Unix variants
46 #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
50 // Error function in case of incorrect command-line
51 // argument specifications
52 std::cout << "\nuseage: record N fs <duration> <device> <channelOffset>\n";
53 std::cout << " where N = number of channels,\n";
54 std::cout << " fs = the sample rate,\n";
55 std::cout << " duration = optional time in seconds to record (default = 2.0),\n";
56 std::cout << " device = optional device to use (default = 0),\n";
57 std::cout << " and channelOffset = an optional channel offset on the device (default = 0).\n\n";
63 unsigned long bufferBytes;
64 unsigned long totalFrames;
65 unsigned long frameCounter;
66 unsigned int channels;
69 // Interleaved buffers
70 int input( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
71 double streamTime, RtAudioStreamStatus status, void *data )
73 InputData *iData = (InputData *) data;
75 // Simply copy the data to our allocated buffer.
76 unsigned int frames = nBufferFrames;
77 if ( iData->frameCounter + nBufferFrames > iData->totalFrames ) {
78 frames = iData->totalFrames - iData->frameCounter;
79 iData->bufferBytes = frames * iData->channels * sizeof( MY_TYPE );
82 unsigned long offset = iData->frameCounter * iData->channels;
83 memcpy( iData->buffer+offset, inputBuffer, iData->bufferBytes );
84 iData->frameCounter += frames;
86 if ( iData->frameCounter >= iData->totalFrames ) return 2;
90 int main( int argc, char *argv[] )
92 unsigned int channels, fs, bufferFrames, device = 0, offset = 0;
96 // minimal command-line checking
97 if ( argc < 3 || argc > 6 ) usage();
100 if ( adc.getDeviceCount() < 1 ) {
101 std::cout << "\nNo audio devices found!\n";
105 channels = (unsigned int) atoi( argv[1] );
106 fs = (unsigned int) atoi( argv[2] );
108 time = (double) atof( argv[3] );
110 device = (unsigned int) atoi( argv[4] );
112 offset = (unsigned int) atoi( argv[5] );
114 // Let RtAudio print messages to stderr.
115 adc.showWarnings( true );
117 // Set our stream parameters for input only.
119 RtAudio::StreamParameters iParams;
120 iParams.deviceId = device;
121 iParams.nChannels = channels;
122 iParams.firstChannel = offset;
127 adc.openStream( NULL, &iParams, FORMAT, fs, &bufferFrames, &input, (void *)&data );
129 catch ( RtError& e ) {
130 std::cout << '\n' << e.getMessage() << '\n' << std::endl;
134 data.bufferBytes = bufferFrames * channels * sizeof( MY_TYPE );
135 data.totalFrames = (unsigned long) (fs * time);
136 data.frameCounter = 0;
137 data.channels = channels;
138 unsigned long totalBytes;
139 totalBytes = data.totalFrames * channels * sizeof( MY_TYPE );
141 // Allocate the entire data buffer before starting stream.
142 data.buffer = (MY_TYPE*) malloc( totalBytes );
143 if ( data.buffer == 0 ) {
144 std::cout << "Memory allocation error ... quitting!\n";
151 catch ( RtError& e ) {
152 std::cout << '\n' << e.getMessage() << '\n' << std::endl;
156 std::cout << "\nRecording for " << time << " seconds ... writing file 'record.raw' (buffer frames = " << bufferFrames << ")." << std::endl;
157 while ( adc.isStreamRunning() ) {
158 SLEEP( 100 ); // wake every 100 ms to check if we're done
161 // Now write the entire data to the file.
162 fd = fopen( "record.raw", "wb" );
163 fwrite( data.buffer, sizeof( MY_TYPE ), data.totalFrames * channels, fd );
167 if ( adc.isStreamOpen() ) adc.closeStream();
168 if ( data.buffer ) free( data.buffer );