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 /******************************************/
19 #define FORMAT RTAUDIO_SINT8
22 typedef signed short MY_TYPE;
23 #define FORMAT RTAUDIO_SINT16
26 typedef signed long MY_TYPE;
27 #define FORMAT RTAUDIO_SINT24
29 typedef signed long MY_TYPE;
30 #define FORMAT RTAUDIO_SINT32
32 typedef float MY_TYPE;
33 #define FORMAT RTAUDIO_FLOAT32
35 typedef double MY_TYPE;
36 #define FORMAT RTAUDIO_FLOAT64
39 // Platform-dependent sleep routines.
40 #if defined( __WINDOWS_ASIO__ ) || defined( __WINDOWS_DS__ )
42 #define SLEEP( milliseconds ) Sleep( (DWORD) milliseconds )
43 #else // Unix variants
45 #define SLEEP( milliseconds ) usleep( (unsigned long) (milliseconds * 1000.0) )
49 // Error function in case of incorrect command-line
50 // argument specifications
51 std::cout << "\nuseage: record N fs <duration> <device> <channelOffset>\n";
52 std::cout << " where N = number of channels,\n";
53 std::cout << " fs = the sample rate,\n";
54 std::cout << " duration = optional time in seconds to record (default = 2.0),\n";
55 std::cout << " device = optional device to use (default = 0),\n";
56 std::cout << " and channelOffset = an optional channel offset on the device (default = 0).\n\n";
62 unsigned long bufferBytes;
63 unsigned long totalFrames;
64 unsigned long frameCounter;
65 unsigned int channels;
68 // Interleaved buffers
69 int input( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
70 double streamTime, RtAudioStreamStatus status, void *data )
72 InputData *iData = (InputData *) data;
74 // Simply copy the data to our allocated buffer.
75 unsigned int frames = nBufferFrames;
76 if ( iData->frameCounter + nBufferFrames > iData->totalFrames ) {
77 frames = iData->totalFrames - iData->frameCounter;
78 iData->bufferBytes = frames * iData->channels * sizeof( MY_TYPE );
81 unsigned long offset = iData->frameCounter * iData->channels;
82 memcpy( iData->buffer+offset, inputBuffer, iData->bufferBytes );
83 iData->frameCounter += frames;
85 if ( iData->frameCounter >= iData->totalFrames ) return 2;
89 int main( int argc, char *argv[] )
91 unsigned int channels, fs, bufferFrames, device = 0, offset = 0;
95 // minimal command-line checking
96 if ( argc < 3 || argc > 6 ) usage();
99 if ( adc.getDeviceCount() < 1 ) {
100 std::cout << "\nNo audio devices found!\n";
104 channels = (unsigned int) atoi( argv[1] );
105 fs = (unsigned int) atoi( argv[2] );
107 time = (double) atof( argv[3] );
109 device = (unsigned int) atoi( argv[4] );
111 offset = (unsigned int) atoi( argv[5] );
113 // Let RtAudio print messages to stderr.
114 adc.showWarnings( true );
116 // Set our stream parameters for input only.
118 RtAudio::StreamParameters iParams;
119 iParams.deviceId = device;
120 iParams.nChannels = channels;
121 iParams.firstChannel = offset;
126 adc.openStream( NULL, &iParams, FORMAT, fs, &bufferFrames, &input, (void *)&data );
128 catch ( RtError& e ) {
129 std::cout << '\n' << e.getMessage() << '\n' << std::endl;
133 data.bufferBytes = bufferFrames * channels * sizeof( MY_TYPE );
134 data.totalFrames = (unsigned long) (fs * time);
135 data.frameCounter = 0;
136 data.channels = channels;
137 unsigned long totalBytes;
138 totalBytes = data.totalFrames * channels * sizeof( MY_TYPE );
140 // Allocate the entire data buffer before starting stream.
141 data.buffer = (MY_TYPE*) malloc( totalBytes );
142 if ( data.buffer == 0 ) {
143 std::cout << "Memory allocation error ... quitting!\n";
150 catch ( RtError& e ) {
151 std::cout << '\n' << e.getMessage() << '\n' << std::endl;
155 std::cout << "\nRecording for " << time << " seconds ... writing file 'record.raw' (buffer frames = " << bufferFrames << ")." << std::endl;
156 while ( adc.isStreamRunning() ) {
157 SLEEP( 100 ); // wake every 100 ms to check if we're done
160 // Now write the entire data to the file.
161 fd = fopen( "record.raw", "wb" );
162 fwrite( data.buffer, sizeof( MY_TYPE ), data.totalFrames * channels, fd );
166 if ( adc.isStreamOpen() ) adc.closeStream();
167 if ( data.buffer ) free( data.buffer );