annotate src/portaudio_20161030/examples/paex_record.c @ 151:fe80428a60a5

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