annotate src/portaudio_20140130/examples/paex_record.c @ 124:e3d5853d5918

Current stable PortAudio source
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 18 Oct 2016 13:11:05 +0100
parents
children
rev   line source
cannam@124 1 /** @file paex_record.c
cannam@124 2 @ingroup examples_src
cannam@124 3 @brief Record input into an array; Save array to a file; Playback recorded data.
cannam@124 4 @author Phil Burk http://www.softsynth.com
cannam@124 5 */
cannam@124 6 /*
cannam@124 7 * $Id: paex_record.c 1752 2011-09-08 03:21:55Z philburk $
cannam@124 8 *
cannam@124 9 * This program uses the PortAudio Portable Audio Library.
cannam@124 10 * For more information see: http://www.portaudio.com
cannam@124 11 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
cannam@124 12 *
cannam@124 13 * Permission is hereby granted, free of charge, to any person obtaining
cannam@124 14 * a copy of this software and associated documentation files
cannam@124 15 * (the "Software"), to deal in the Software without restriction,
cannam@124 16 * including without limitation the rights to use, copy, modify, merge,
cannam@124 17 * publish, distribute, sublicense, and/or sell copies of the Software,
cannam@124 18 * and to permit persons to whom the Software is furnished to do so,
cannam@124 19 * subject to the following conditions:
cannam@124 20 *
cannam@124 21 * The above copyright notice and this permission notice shall be
cannam@124 22 * included in all copies or substantial portions of the Software.
cannam@124 23 *
cannam@124 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@124 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@124 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
cannam@124 27 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
cannam@124 28 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@124 29 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@124 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@124 31 */
cannam@124 32
cannam@124 33 /*
cannam@124 34 * The text above constitutes the entire PortAudio license; however,
cannam@124 35 * the PortAudio community also makes the following non-binding requests:
cannam@124 36 *
cannam@124 37 * Any person wishing to distribute modifications to the Software is
cannam@124 38 * requested to send the modifications to the original developer so that
cannam@124 39 * they can be incorporated into the canonical version. It is also
cannam@124 40 * requested that these non-binding requests be included along with the
cannam@124 41 * license above.
cannam@124 42 */
cannam@124 43
cannam@124 44 #include <stdio.h>
cannam@124 45 #include <stdlib.h>
cannam@124 46 #include "portaudio.h"
cannam@124 47
cannam@124 48 /* #define SAMPLE_RATE (17932) // Test failure to open with this value. */
cannam@124 49 #define SAMPLE_RATE (44100)
cannam@124 50 #define FRAMES_PER_BUFFER (512)
cannam@124 51 #define NUM_SECONDS (5)
cannam@124 52 #define NUM_CHANNELS (2)
cannam@124 53 /* #define DITHER_FLAG (paDitherOff) */
cannam@124 54 #define DITHER_FLAG (0) /**/
cannam@124 55 /** Set to 1 if you want to capture the recording to a file. */
cannam@124 56 #define WRITE_TO_FILE (0)
cannam@124 57
cannam@124 58 /* Select sample format. */
cannam@124 59 #if 1
cannam@124 60 #define PA_SAMPLE_TYPE paFloat32
cannam@124 61 typedef float SAMPLE;
cannam@124 62 #define SAMPLE_SILENCE (0.0f)
cannam@124 63 #define PRINTF_S_FORMAT "%.8f"
cannam@124 64 #elif 1
cannam@124 65 #define PA_SAMPLE_TYPE paInt16
cannam@124 66 typedef short SAMPLE;
cannam@124 67 #define SAMPLE_SILENCE (0)
cannam@124 68 #define PRINTF_S_FORMAT "%d"
cannam@124 69 #elif 0
cannam@124 70 #define PA_SAMPLE_TYPE paInt8
cannam@124 71 typedef char SAMPLE;
cannam@124 72 #define SAMPLE_SILENCE (0)
cannam@124 73 #define PRINTF_S_FORMAT "%d"
cannam@124 74 #else
cannam@124 75 #define PA_SAMPLE_TYPE paUInt8
cannam@124 76 typedef unsigned char SAMPLE;
cannam@124 77 #define SAMPLE_SILENCE (128)
cannam@124 78 #define PRINTF_S_FORMAT "%d"
cannam@124 79 #endif
cannam@124 80
cannam@124 81 typedef struct
cannam@124 82 {
cannam@124 83 int frameIndex; /* Index into sample array. */
cannam@124 84 int maxFrameIndex;
cannam@124 85 SAMPLE *recordedSamples;
cannam@124 86 }
cannam@124 87 paTestData;
cannam@124 88
cannam@124 89 /* This routine will be called by the PortAudio engine when audio is needed.
cannam@124 90 ** It may be called at interrupt level on some machines so don't do anything
cannam@124 91 ** that could mess up the system like calling malloc() or free().
cannam@124 92 */
cannam@124 93 static int recordCallback( const void *inputBuffer, void *outputBuffer,
cannam@124 94 unsigned long framesPerBuffer,
cannam@124 95 const PaStreamCallbackTimeInfo* timeInfo,
cannam@124 96 PaStreamCallbackFlags statusFlags,
cannam@124 97 void *userData )
cannam@124 98 {
cannam@124 99 paTestData *data = (paTestData*)userData;
cannam@124 100 const SAMPLE *rptr = (const SAMPLE*)inputBuffer;
cannam@124 101 SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
cannam@124 102 long framesToCalc;
cannam@124 103 long i;
cannam@124 104 int finished;
cannam@124 105 unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
cannam@124 106
cannam@124 107 (void) outputBuffer; /* Prevent unused variable warnings. */
cannam@124 108 (void) timeInfo;
cannam@124 109 (void) statusFlags;
cannam@124 110 (void) userData;
cannam@124 111
cannam@124 112 if( framesLeft < framesPerBuffer )
cannam@124 113 {
cannam@124 114 framesToCalc = framesLeft;
cannam@124 115 finished = paComplete;
cannam@124 116 }
cannam@124 117 else
cannam@124 118 {
cannam@124 119 framesToCalc = framesPerBuffer;
cannam@124 120 finished = paContinue;
cannam@124 121 }
cannam@124 122
cannam@124 123 if( inputBuffer == NULL )
cannam@124 124 {
cannam@124 125 for( i=0; i<framesToCalc; i++ )
cannam@124 126 {
cannam@124 127 *wptr++ = SAMPLE_SILENCE; /* left */
cannam@124 128 if( NUM_CHANNELS == 2 ) *wptr++ = SAMPLE_SILENCE; /* right */
cannam@124 129 }
cannam@124 130 }
cannam@124 131 else
cannam@124 132 {
cannam@124 133 for( i=0; i<framesToCalc; i++ )
cannam@124 134 {
cannam@124 135 *wptr++ = *rptr++; /* left */
cannam@124 136 if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++; /* right */
cannam@124 137 }
cannam@124 138 }
cannam@124 139 data->frameIndex += framesToCalc;
cannam@124 140 return finished;
cannam@124 141 }
cannam@124 142
cannam@124 143 /* This routine will be called by the PortAudio engine when audio is needed.
cannam@124 144 ** It may be called at interrupt level on some machines so don't do anything
cannam@124 145 ** that could mess up the system like calling malloc() or free().
cannam@124 146 */
cannam@124 147 static int playCallback( const void *inputBuffer, void *outputBuffer,
cannam@124 148 unsigned long framesPerBuffer,
cannam@124 149 const PaStreamCallbackTimeInfo* timeInfo,
cannam@124 150 PaStreamCallbackFlags statusFlags,
cannam@124 151 void *userData )
cannam@124 152 {
cannam@124 153 paTestData *data = (paTestData*)userData;
cannam@124 154 SAMPLE *rptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
cannam@124 155 SAMPLE *wptr = (SAMPLE*)outputBuffer;
cannam@124 156 unsigned int i;
cannam@124 157 int finished;
cannam@124 158 unsigned int framesLeft = data->maxFrameIndex - data->frameIndex;
cannam@124 159
cannam@124 160 (void) inputBuffer; /* Prevent unused variable warnings. */
cannam@124 161 (void) timeInfo;
cannam@124 162 (void) statusFlags;
cannam@124 163 (void) userData;
cannam@124 164
cannam@124 165 if( framesLeft < framesPerBuffer )
cannam@124 166 {
cannam@124 167 /* final buffer... */
cannam@124 168 for( i=0; i<framesLeft; i++ )
cannam@124 169 {
cannam@124 170 *wptr++ = *rptr++; /* left */
cannam@124 171 if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++; /* right */
cannam@124 172 }
cannam@124 173 for( ; i<framesPerBuffer; i++ )
cannam@124 174 {
cannam@124 175 *wptr++ = 0; /* left */
cannam@124 176 if( NUM_CHANNELS == 2 ) *wptr++ = 0; /* right */
cannam@124 177 }
cannam@124 178 data->frameIndex += framesLeft;
cannam@124 179 finished = paComplete;
cannam@124 180 }
cannam@124 181 else
cannam@124 182 {
cannam@124 183 for( i=0; i<framesPerBuffer; i++ )
cannam@124 184 {
cannam@124 185 *wptr++ = *rptr++; /* left */
cannam@124 186 if( NUM_CHANNELS == 2 ) *wptr++ = *rptr++; /* right */
cannam@124 187 }
cannam@124 188 data->frameIndex += framesPerBuffer;
cannam@124 189 finished = paContinue;
cannam@124 190 }
cannam@124 191 return finished;
cannam@124 192 }
cannam@124 193
cannam@124 194 /*******************************************************************/
cannam@124 195 int main(void);
cannam@124 196 int main(void)
cannam@124 197 {
cannam@124 198 PaStreamParameters inputParameters,
cannam@124 199 outputParameters;
cannam@124 200 PaStream* stream;
cannam@124 201 PaError err = paNoError;
cannam@124 202 paTestData data;
cannam@124 203 int i;
cannam@124 204 int totalFrames;
cannam@124 205 int numSamples;
cannam@124 206 int numBytes;
cannam@124 207 SAMPLE max, val;
cannam@124 208 double average;
cannam@124 209
cannam@124 210 printf("patest_record.c\n"); fflush(stdout);
cannam@124 211
cannam@124 212 data.maxFrameIndex = totalFrames = NUM_SECONDS * SAMPLE_RATE; /* Record for a few seconds. */
cannam@124 213 data.frameIndex = 0;
cannam@124 214 numSamples = totalFrames * NUM_CHANNELS;
cannam@124 215 numBytes = numSamples * sizeof(SAMPLE);
cannam@124 216 data.recordedSamples = (SAMPLE *) malloc( numBytes ); /* From now on, recordedSamples is initialised. */
cannam@124 217 if( data.recordedSamples == NULL )
cannam@124 218 {
cannam@124 219 printf("Could not allocate record array.\n");
cannam@124 220 goto done;
cannam@124 221 }
cannam@124 222 for( i=0; i<numSamples; i++ ) data.recordedSamples[i] = 0;
cannam@124 223
cannam@124 224 err = Pa_Initialize();
cannam@124 225 if( err != paNoError ) goto done;
cannam@124 226
cannam@124 227 inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
cannam@124 228 if (inputParameters.device == paNoDevice) {
cannam@124 229 fprintf(stderr,"Error: No default input device.\n");
cannam@124 230 goto done;
cannam@124 231 }
cannam@124 232 inputParameters.channelCount = 2; /* stereo input */
cannam@124 233 inputParameters.sampleFormat = PA_SAMPLE_TYPE;
cannam@124 234 inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency;
cannam@124 235 inputParameters.hostApiSpecificStreamInfo = NULL;
cannam@124 236
cannam@124 237 /* Record some audio. -------------------------------------------- */
cannam@124 238 err = Pa_OpenStream(
cannam@124 239 &stream,
cannam@124 240 &inputParameters,
cannam@124 241 NULL, /* &outputParameters, */
cannam@124 242 SAMPLE_RATE,
cannam@124 243 FRAMES_PER_BUFFER,
cannam@124 244 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@124 245 recordCallback,
cannam@124 246 &data );
cannam@124 247 if( err != paNoError ) goto done;
cannam@124 248
cannam@124 249 err = Pa_StartStream( stream );
cannam@124 250 if( err != paNoError ) goto done;
cannam@124 251 printf("\n=== Now recording!! Please speak into the microphone. ===\n"); fflush(stdout);
cannam@124 252
cannam@124 253 while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
cannam@124 254 {
cannam@124 255 Pa_Sleep(1000);
cannam@124 256 printf("index = %d\n", data.frameIndex ); fflush(stdout);
cannam@124 257 }
cannam@124 258 if( err < 0 ) goto done;
cannam@124 259
cannam@124 260 err = Pa_CloseStream( stream );
cannam@124 261 if( err != paNoError ) goto done;
cannam@124 262
cannam@124 263 /* Measure maximum peak amplitude. */
cannam@124 264 max = 0;
cannam@124 265 average = 0.0;
cannam@124 266 for( i=0; i<numSamples; i++ )
cannam@124 267 {
cannam@124 268 val = data.recordedSamples[i];
cannam@124 269 if( val < 0 ) val = -val; /* ABS */
cannam@124 270 if( val > max )
cannam@124 271 {
cannam@124 272 max = val;
cannam@124 273 }
cannam@124 274 average += val;
cannam@124 275 }
cannam@124 276
cannam@124 277 average = average / (double)numSamples;
cannam@124 278
cannam@124 279 printf("sample max amplitude = "PRINTF_S_FORMAT"\n", max );
cannam@124 280 printf("sample average = %lf\n", average );
cannam@124 281
cannam@124 282 /* Write recorded data to a file. */
cannam@124 283 #if WRITE_TO_FILE
cannam@124 284 {
cannam@124 285 FILE *fid;
cannam@124 286 fid = fopen("recorded.raw", "wb");
cannam@124 287 if( fid == NULL )
cannam@124 288 {
cannam@124 289 printf("Could not open file.");
cannam@124 290 }
cannam@124 291 else
cannam@124 292 {
cannam@124 293 fwrite( data.recordedSamples, NUM_CHANNELS * sizeof(SAMPLE), totalFrames, fid );
cannam@124 294 fclose( fid );
cannam@124 295 printf("Wrote data to 'recorded.raw'\n");
cannam@124 296 }
cannam@124 297 }
cannam@124 298 #endif
cannam@124 299
cannam@124 300 /* Playback recorded data. -------------------------------------------- */
cannam@124 301 data.frameIndex = 0;
cannam@124 302
cannam@124 303 outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
cannam@124 304 if (outputParameters.device == paNoDevice) {
cannam@124 305 fprintf(stderr,"Error: No default output device.\n");
cannam@124 306 goto done;
cannam@124 307 }
cannam@124 308 outputParameters.channelCount = 2; /* stereo output */
cannam@124 309 outputParameters.sampleFormat = PA_SAMPLE_TYPE;
cannam@124 310 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
cannam@124 311 outputParameters.hostApiSpecificStreamInfo = NULL;
cannam@124 312
cannam@124 313 printf("\n=== Now playing back. ===\n"); fflush(stdout);
cannam@124 314 err = Pa_OpenStream(
cannam@124 315 &stream,
cannam@124 316 NULL, /* no input */
cannam@124 317 &outputParameters,
cannam@124 318 SAMPLE_RATE,
cannam@124 319 FRAMES_PER_BUFFER,
cannam@124 320 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@124 321 playCallback,
cannam@124 322 &data );
cannam@124 323 if( err != paNoError ) goto done;
cannam@124 324
cannam@124 325 if( stream )
cannam@124 326 {
cannam@124 327 err = Pa_StartStream( stream );
cannam@124 328 if( err != paNoError ) goto done;
cannam@124 329
cannam@124 330 printf("Waiting for playback to finish.\n"); fflush(stdout);
cannam@124 331
cannam@124 332 while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(100);
cannam@124 333 if( err < 0 ) goto done;
cannam@124 334
cannam@124 335 err = Pa_CloseStream( stream );
cannam@124 336 if( err != paNoError ) goto done;
cannam@124 337
cannam@124 338 printf("Done.\n"); fflush(stdout);
cannam@124 339 }
cannam@124 340
cannam@124 341 done:
cannam@124 342 Pa_Terminate();
cannam@124 343 if( data.recordedSamples ) /* Sure it is NULL or valid. */
cannam@124 344 free( data.recordedSamples );
cannam@124 345 if( err != paNoError )
cannam@124 346 {
cannam@124 347 fprintf( stderr, "An error occured while using the portaudio stream\n" );
cannam@124 348 fprintf( stderr, "Error number: %d\n", err );
cannam@124 349 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
cannam@124 350 err = 1; /* Always return 0 or 1, but no other return codes. */
cannam@124 351 }
cannam@124 352 return err;
cannam@124 353 }
cannam@124 354