Chris@4: /** @file paex_record.c Chris@4: @ingroup examples_src Chris@4: @brief Record input into an array; Save array to a file; Playback recorded data. Chris@4: @author Phil Burk http://www.softsynth.com Chris@4: */ Chris@4: /* Chris@4: * $Id: paex_record.c 1752 2011-09-08 03:21:55Z philburk $ Chris@4: * Chris@4: * This program uses the PortAudio Portable Audio Library. Chris@4: * For more information see: http://www.portaudio.com Chris@4: * Copyright (c) 1999-2000 Ross Bencina and Phil Burk Chris@4: * Chris@4: * Permission is hereby granted, free of charge, to any person obtaining Chris@4: * a copy of this software and associated documentation files Chris@4: * (the "Software"), to deal in the Software without restriction, Chris@4: * including without limitation the rights to use, copy, modify, merge, Chris@4: * publish, distribute, sublicense, and/or sell copies of the Software, Chris@4: * and to permit persons to whom the Software is furnished to do so, Chris@4: * subject to the following conditions: Chris@4: * Chris@4: * The above copyright notice and this permission notice shall be Chris@4: * included in all copies or substantial portions of the Software. Chris@4: * Chris@4: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@4: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@4: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. Chris@4: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR Chris@4: * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@4: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@4: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@4: */ Chris@4: Chris@4: /* Chris@4: * The text above constitutes the entire PortAudio license; however, Chris@4: * the PortAudio community also makes the following non-binding requests: Chris@4: * Chris@4: * Any person wishing to distribute modifications to the Software is Chris@4: * requested to send the modifications to the original developer so that Chris@4: * they can be incorporated into the canonical version. It is also Chris@4: * requested that these non-binding requests be included along with the Chris@4: * license above. Chris@4: */ Chris@4: Chris@4: #include Chris@4: #include Chris@4: #include "portaudio.h" Chris@4: Chris@4: /* #define SAMPLE_RATE (17932) // Test failure to open with this value. */ Chris@4: #define SAMPLE_RATE (44100) Chris@4: #define FRAMES_PER_BUFFER (512) Chris@4: #define NUM_SECONDS (5) Chris@4: #define NUM_CHANNELS (2) Chris@4: /* #define DITHER_FLAG (paDitherOff) */ Chris@4: #define DITHER_FLAG (0) /**/ Chris@4: /** Set to 1 if you want to capture the recording to a file. */ Chris@4: #define WRITE_TO_FILE (0) Chris@4: Chris@4: /* Select sample format. */ Chris@4: #if 1 Chris@4: #define PA_SAMPLE_TYPE paFloat32 Chris@4: typedef float SAMPLE; Chris@4: #define SAMPLE_SILENCE (0.0f) Chris@4: #define PRINTF_S_FORMAT "%.8f" Chris@4: #elif 1 Chris@4: #define PA_SAMPLE_TYPE paInt16 Chris@4: typedef short SAMPLE; Chris@4: #define SAMPLE_SILENCE (0) Chris@4: #define PRINTF_S_FORMAT "%d" Chris@4: #elif 0 Chris@4: #define PA_SAMPLE_TYPE paInt8 Chris@4: typedef char SAMPLE; Chris@4: #define SAMPLE_SILENCE (0) Chris@4: #define PRINTF_S_FORMAT "%d" Chris@4: #else Chris@4: #define PA_SAMPLE_TYPE paUInt8 Chris@4: typedef unsigned char SAMPLE; Chris@4: #define SAMPLE_SILENCE (128) Chris@4: #define PRINTF_S_FORMAT "%d" Chris@4: #endif Chris@4: Chris@4: typedef struct Chris@4: { Chris@4: int frameIndex; /* Index into sample array. */ Chris@4: int maxFrameIndex; Chris@4: SAMPLE *recordedSamples; Chris@4: } Chris@4: paTestData; Chris@4: Chris@4: /* This routine will be called by the PortAudio engine when audio is needed. Chris@4: ** It may be called at interrupt level on some machines so don't do anything Chris@4: ** that could mess up the system like calling malloc() or free(). Chris@4: */ Chris@4: static int recordCallback( const void *inputBuffer, void *outputBuffer, Chris@4: unsigned long framesPerBuffer, Chris@4: const PaStreamCallbackTimeInfo* timeInfo, Chris@4: PaStreamCallbackFlags statusFlags, Chris@4: void *userData ) Chris@4: { Chris@4: paTestData *data = (paTestData*)userData; Chris@4: const SAMPLE *rptr = (const SAMPLE*)inputBuffer; Chris@4: SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS]; Chris@4: long framesToCalc; Chris@4: long i; Chris@4: int finished; Chris@4: unsigned long framesLeft = data->maxFrameIndex - data->frameIndex; Chris@4: Chris@4: (void) outputBuffer; /* Prevent unused variable warnings. */ Chris@4: (void) timeInfo; Chris@4: (void) statusFlags; Chris@4: (void) userData; Chris@4: Chris@4: if( framesLeft < framesPerBuffer ) Chris@4: { Chris@4: framesToCalc = framesLeft; Chris@4: finished = paComplete; Chris@4: } Chris@4: else Chris@4: { Chris@4: framesToCalc = framesPerBuffer; Chris@4: finished = paContinue; Chris@4: } Chris@4: Chris@4: if( inputBuffer == NULL ) Chris@4: { Chris@4: for( i=0; iframeIndex += framesToCalc; Chris@4: return finished; Chris@4: } Chris@4: Chris@4: /* This routine will be called by the PortAudio engine when audio is needed. Chris@4: ** It may be called at interrupt level on some machines so don't do anything Chris@4: ** that could mess up the system like calling malloc() or free(). Chris@4: */ Chris@4: static int playCallback( const void *inputBuffer, void *outputBuffer, Chris@4: unsigned long framesPerBuffer, Chris@4: const PaStreamCallbackTimeInfo* timeInfo, Chris@4: PaStreamCallbackFlags statusFlags, Chris@4: void *userData ) Chris@4: { Chris@4: paTestData *data = (paTestData*)userData; Chris@4: SAMPLE *rptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS]; Chris@4: SAMPLE *wptr = (SAMPLE*)outputBuffer; Chris@4: unsigned int i; Chris@4: int finished; Chris@4: unsigned int framesLeft = data->maxFrameIndex - data->frameIndex; Chris@4: Chris@4: (void) inputBuffer; /* Prevent unused variable warnings. */ Chris@4: (void) timeInfo; Chris@4: (void) statusFlags; Chris@4: (void) userData; Chris@4: Chris@4: if( framesLeft < framesPerBuffer ) Chris@4: { Chris@4: /* final buffer... */ Chris@4: for( i=0; iframeIndex += framesLeft; Chris@4: finished = paComplete; Chris@4: } Chris@4: else Chris@4: { Chris@4: for( i=0; iframeIndex += framesPerBuffer; Chris@4: finished = paContinue; Chris@4: } Chris@4: return finished; Chris@4: } Chris@4: Chris@4: /*******************************************************************/ Chris@4: int main(void); Chris@4: int main(void) Chris@4: { Chris@4: PaStreamParameters inputParameters, Chris@4: outputParameters; Chris@4: PaStream* stream; Chris@4: PaError err = paNoError; Chris@4: paTestData data; Chris@4: int i; Chris@4: int totalFrames; Chris@4: int numSamples; Chris@4: int numBytes; Chris@4: SAMPLE max, val; Chris@4: double average; Chris@4: Chris@4: printf("patest_record.c\n"); fflush(stdout); Chris@4: Chris@4: data.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */ Chris@4: data.frameIndex = 0; Chris@4: numSamples = totalFrames * NUM_CHANNELS; Chris@4: numBytes = numSamples * sizeof(SAMPLE); Chris@4: data.recordedSamples = (SAMPLE *) malloc( numBytes ); /* From now on, recordedSamples is initialised. */ Chris@4: if( data.recordedSamples == NULL ) Chris@4: { Chris@4: printf("Could not allocate record array.\n"); Chris@4: goto done; Chris@4: } Chris@4: for( i=0; idefaultLowInputLatency; Chris@4: inputParameters.hostApiSpecificStreamInfo = NULL; Chris@4: Chris@4: /* Record some audio. -------------------------------------------- */ Chris@4: err = Pa_OpenStream( Chris@4: &stream, Chris@4: &inputParameters, Chris@4: NULL, /* &outputParameters, */ Chris@4: SAMPLE_RATE, Chris@4: FRAMES_PER_BUFFER, Chris@4: paClipOff, /* we won't output out of range samples so don't bother clipping them */ Chris@4: recordCallback, Chris@4: &data ); Chris@4: if( err != paNoError ) goto done; Chris@4: Chris@4: err = Pa_StartStream( stream ); Chris@4: if( err != paNoError ) goto done; Chris@4: printf("\n=== Now recording!! Please speak into the microphone. ===\n"); fflush(stdout); Chris@4: Chris@4: while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Chris@4: { Chris@4: Pa_Sleep(1000); Chris@4: printf("index = %d\n", data.frameIndex ); fflush(stdout); Chris@4: } Chris@4: if( err < 0 ) goto done; Chris@4: Chris@4: err = Pa_CloseStream( stream ); Chris@4: if( err != paNoError ) goto done; Chris@4: Chris@4: /* Measure maximum peak amplitude. */ Chris@4: max = 0; Chris@4: average = 0.0; Chris@4: for( i=0; i max ) Chris@4: { Chris@4: max = val; Chris@4: } Chris@4: average += val; Chris@4: } Chris@4: Chris@4: average = average / (double)numSamples; Chris@4: Chris@4: printf("sample max amplitude = "PRINTF_S_FORMAT"\n", max ); Chris@4: printf("sample average = %lf\n", average ); Chris@4: Chris@4: /* Write recorded data to a file. */ Chris@4: #if WRITE_TO_FILE Chris@4: { Chris@4: FILE *fid; Chris@4: fid = fopen("recorded.raw", "wb"); Chris@4: if( fid == NULL ) Chris@4: { Chris@4: printf("Could not open file."); Chris@4: } Chris@4: else Chris@4: { Chris@4: fwrite( data.recordedSamples, NUM_CHANNELS * sizeof(SAMPLE), totalFrames, fid ); Chris@4: fclose( fid ); Chris@4: printf("Wrote data to 'recorded.raw'\n"); Chris@4: } Chris@4: } Chris@4: #endif Chris@4: Chris@4: /* Playback recorded data. -------------------------------------------- */ Chris@4: data.frameIndex = 0; Chris@4: Chris@4: outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ Chris@4: if (outputParameters.device == paNoDevice) { Chris@4: fprintf(stderr,"Error: No default output device.\n"); Chris@4: goto done; Chris@4: } Chris@4: outputParameters.channelCount = 2; /* stereo output */ Chris@4: outputParameters.sampleFormat = PA_SAMPLE_TYPE; Chris@4: outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; Chris@4: outputParameters.hostApiSpecificStreamInfo = NULL; Chris@4: Chris@4: printf("\n=== Now playing back. ===\n"); fflush(stdout); Chris@4: err = Pa_OpenStream( Chris@4: &stream, Chris@4: NULL, /* no input */ Chris@4: &outputParameters, Chris@4: SAMPLE_RATE, Chris@4: FRAMES_PER_BUFFER, Chris@4: paClipOff, /* we won't output out of range samples so don't bother clipping them */ Chris@4: playCallback, Chris@4: &data ); Chris@4: if( err != paNoError ) goto done; Chris@4: Chris@4: if( stream ) Chris@4: { Chris@4: err = Pa_StartStream( stream ); Chris@4: if( err != paNoError ) goto done; Chris@4: Chris@4: printf("Waiting for playback to finish.\n"); fflush(stdout); Chris@4: Chris@4: while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(100); Chris@4: if( err < 0 ) goto done; Chris@4: Chris@4: err = Pa_CloseStream( stream ); Chris@4: if( err != paNoError ) goto done; Chris@4: Chris@4: printf("Done.\n"); fflush(stdout); Chris@4: } Chris@4: Chris@4: done: Chris@4: Pa_Terminate(); Chris@4: if( data.recordedSamples ) /* Sure it is NULL or valid. */ Chris@4: free( data.recordedSamples ); Chris@4: if( err != paNoError ) Chris@4: { Chris@4: fprintf( stderr, "An error occured while using the portaudio stream\n" ); Chris@4: fprintf( stderr, "Error number: %d\n", err ); Chris@4: fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); Chris@4: err = 1; /* Always return 0 or 1, but no other return codes. */ Chris@4: } Chris@4: return err; Chris@4: } Chris@4: