annotate src/portaudio/examples/paex_record.c @ 123:0cef3a1bd1ae

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