annotate src/portaudio_20140130/examples/paex_record.c @ 39:7ddb4fc30dac

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