49 #define SAMPLE_RATE (44100)
50 #define FRAMES_PER_BUFFER (512)
51 #define NUM_SECONDS (5)
52 #define NUM_CHANNELS (1)
54 #define DITHER_FLAG (0)
56 #define WRITE_TO_FILE (0)
60 #define PA_SAMPLE_TYPE paFloat32
62 #define SAMPLE_SILENCE (0.0f)
63 #define PRINTF_S_FORMAT "%.8f"
65 #define PA_SAMPLE_TYPE paInt16
67 #define SAMPLE_SILENCE (0)
68 #define PRINTF_S_FORMAT "%d"
70 #define PA_SAMPLE_TYPE paInt8
72 #define SAMPLE_SILENCE (0)
73 #define PRINTF_S_FORMAT "%d"
75 #define PA_SAMPLE_TYPE paUInt8
76 typedef unsigned char SAMPLE;
77 #define SAMPLE_SILENCE (128)
78 #define PRINTF_S_FORMAT "%d"
85 SAMPLE *recordedSamples;
93 static int recordCallback(
const void *inputBuffer,
void *outputBuffer,
94 unsigned long framesPerBuffer,
100 const SAMPLE *rptr = (
const SAMPLE*)inputBuffer;
101 SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
105 unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
112 if( framesLeft < framesPerBuffer )
114 framesToCalc = framesLeft;
119 framesToCalc = framesPerBuffer;
123 if( inputBuffer == NULL )
125 for( i=0; i<framesToCalc; i++ )
127 *wptr++ = SAMPLE_SILENCE;
128 if( NUM_CHANNELS == 2 ) *wptr++ = SAMPLE_SILENCE;
133 for( i=0; i<framesToCalc; i++ )
136 if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++;
139 data->frameIndex += framesToCalc;
147 static int playCallback(
const void *inputBuffer,
void *outputBuffer,
148 unsigned long framesPerBuffer,
154 SAMPLE *rptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
155 SAMPLE *wptr = (SAMPLE*)outputBuffer;
158 unsigned int framesLeft = data->maxFrameIndex - data->frameIndex;
165 if( framesLeft < framesPerBuffer )
168 for( i=0; i<framesLeft; i++ )
171 if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++;
173 for( ; i<framesPerBuffer; i++ )
176 if( NUM_CHANNELS == 2 ) *wptr++ = 0;
178 data->frameIndex += framesLeft;
183 for( i=0; i<framesPerBuffer; i++ )
186 if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++;
188 data->frameIndex += framesPerBuffer;
210 printf(
"patest_record.c\n"); fflush(stdout);
212 data.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE;
214 numSamples = totalFrames * NUM_CHANNELS;
215 numBytes = numSamples *
sizeof(SAMPLE);
216 data.recordedSamples = (SAMPLE *) malloc( numBytes );
217 if( data.recordedSamples == NULL )
219 printf(
"Could not allocate record array.\n");
222 for( i=0; i<numSamples; i++ ) data.recordedSamples[i] = 0;
225 if( err != paNoError )
goto done;
229 fprintf(stderr,
"Error: No default input device.\n");
247 if( err != paNoError )
goto done;
250 if( err != paNoError )
goto done;
251 printf(
"\n=== Now recording!! Please speak into the microphone. ===\n"); fflush(stdout);
256 printf(
"index = %d\n", data.frameIndex ); fflush(stdout);
258 if( err < 0 )
goto done;
261 if( err != paNoError )
goto done;
266 for( i=0; i<numSamples; i++ )
268 val = data.recordedSamples[i];
269 if( val < 0 ) val = -val;
277 average = average / (double)numSamples;
279 printf(
"sample max amplitude = "PRINTF_S_FORMAT
"\n", max );
280 printf(
"sample average = %lf\n", average );
286 fid = fopen(
"recorded.raw",
"wb");
289 printf(
"Could not open file.");
293 fwrite( data.recordedSamples, NUM_CHANNELS *
sizeof(SAMPLE), totalFrames, fid );
295 printf(
"Wrote data to 'recorded.raw'\n");
305 fprintf(stderr,
"Error: No default output device.\n");
313 printf(
"\n=== Now playing back. ===\n"); fflush(stdout);
323 if( err != paNoError )
goto done;
328 if( err != paNoError )
goto done;
330 printf(
"Waiting for playback to finish.\n"); fflush(stdout);
333 if( err < 0 )
goto done;
336 if( err != paNoError )
goto done;
338 printf(
"Done.\n"); fflush(stdout);
343 if( data.recordedSamples )
344 free( data.recordedSamples );
345 if( err != paNoError )
347 fprintf( stderr,
"An error occurred while using the portaudio stream\n" );
348 fprintf( stderr,
"Error number: %d\n", err );
PaError Pa_Initialize(void)
PaDeviceIndex Pa_GetDefaultInputDevice(void)
PaError Pa_OpenStream(PaStream **stream, const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate, unsigned long framesPerBuffer, PaStreamFlags streamFlags, PaStreamCallback *streamCallback, void *userData)
PaTime defaultLowInputLatency
PaError Pa_StartStream(PaStream *stream)
void * hostApiSpecificStreamInfo
The portable PortAudio API.
PaSampleFormat sampleFormat
PaError Pa_IsStreamActive(PaStream *stream)
unsigned long PaStreamCallbackFlags
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
PaDeviceIndex Pa_GetDefaultOutputDevice(void)
const char * Pa_GetErrorText(PaError errorCode)
PaError Pa_CloseStream(PaStream *stream)
PaError Pa_Terminate(void)