| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <memory.h> |
| 41 |
#include "portaudio.h" |
| 42 |
#define SAMPLE_RATE (22050) |
| 43 |
#define NUM_SECONDS (10) |
| 44 |
#define SLEEP_DUR_MSEC (200) |
| 45 |
#define FRAMES_PER_BUFFER (1<<10) |
| 46 |
#define NUM_REC_BUFS (0) |
| 47 |
|
| 48 |
#if 1 |
| 49 |
#define PA_SAMPLE_TYPE paFloat32 |
| 50 |
typedef float SAMPLE; |
| 51 |
#else |
| 52 |
#define PA_SAMPLE_TYPE paInt16 |
| 53 |
typedef short SAMPLE; |
| 54 |
#endif |
| 55 |
|
| 56 |
typedef struct |
| 57 |
{ |
| 58 |
long frameIndex; |
| 59 |
long maxFrameIndex; |
| 60 |
long samplesPerFrame; |
| 61 |
long numSamples; |
| 62 |
SAMPLE *recordedSamples; |
| 63 |
} |
| 64 |
paTestData; |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static int recordCallback( void *inputBuffer, void *outputBuffer, |
| 70 |
unsigned long framesPerBuffer, |
| 71 |
PaTimestamp outTime, void *userData ) |
| 72 |
{ |
| 73 |
paTestData *data = (paTestData*)userData; |
| 74 |
SAMPLE *rptr = (SAMPLE*)inputBuffer; |
| 75 |
SAMPLE *wptr = &data->recordedSamples[data->frameIndex * data->samplesPerFrame]; |
| 76 |
long framesToCalc; |
| 77 |
unsigned long i; |
| 78 |
int finished; |
| 79 |
unsigned long framesLeft = data->maxFrameIndex - data->frameIndex; |
| 80 |
|
| 81 |
(void) outputBuffer; |
| 82 |
(void) outTime; |
| 83 |
|
| 84 |
if( framesLeft < framesPerBuffer ) |
| 85 |
{ |
| 86 |
framesToCalc = framesLeft; |
| 87 |
finished = 1; |
| 88 |
} |
| 89 |
else |
| 90 |
{ |
| 91 |
framesToCalc = framesPerBuffer; |
| 92 |
finished = 0; |
| 93 |
} |
| 94 |
if( inputBuffer == NULL ) |
| 95 |
{ |
| 96 |
for( i=0; i<framesToCalc; i++ ) |
| 97 |
{ |
| 98 |
*wptr++ = 0; |
| 99 |
*wptr++ = 0; |
| 100 |
} |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
for( i=0; i<framesToCalc; i++ ) |
| 105 |
{ |
| 106 |
*wptr++ = *rptr++; |
| 107 |
*wptr++ = *rptr++; |
| 108 |
} |
| 109 |
} |
| 110 |
data->frameIndex += framesToCalc; |
| 111 |
return finished; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
static int playCallback( void *inputBuffer, void *outputBuffer, |
| 118 |
unsigned long framesPerBuffer, |
| 119 |
PaTimestamp outTime, void *userData ) |
| 120 |
{ |
| 121 |
paTestData *data = (paTestData*)userData; |
| 122 |
SAMPLE *rptr = &data->recordedSamples[data->frameIndex * data->samplesPerFrame]; |
| 123 |
SAMPLE *wptr = (SAMPLE*)outputBuffer; |
| 124 |
unsigned long i; |
| 125 |
int finished; |
| 126 |
unsigned int framesLeft = data->maxFrameIndex - data->frameIndex; |
| 127 |
if( outputBuffer == NULL ) return 0; |
| 128 |
(void) inputBuffer; |
| 129 |
(void) outTime; |
| 130 |
|
| 131 |
if( framesLeft < framesPerBuffer ) |
| 132 |
{ |
| 133 |
|
| 134 |
for( i=0; i<framesLeft; i++ ) |
| 135 |
{ |
| 136 |
*wptr++ = *rptr++; |
| 137 |
*wptr++ = *rptr++; |
| 138 |
} |
| 139 |
for( ; i<framesPerBuffer; i++ ) |
| 140 |
{ |
| 141 |
*wptr++ = 0; |
| 142 |
*wptr++ = 0; |
| 143 |
} |
| 144 |
data->frameIndex += framesLeft; |
| 145 |
finished = 1; |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
for( i=0; i<framesPerBuffer; i++ ) |
| 150 |
{ |
| 151 |
*wptr++ = *rptr++; |
| 152 |
*wptr++ = *rptr++; |
| 153 |
} |
| 154 |
data->frameIndex += framesPerBuffer; |
| 155 |
finished = 0; |
| 156 |
} |
| 157 |
return finished; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
PaError TestRecording( paTestData *dataPtr ) |
| 162 |
{ |
| 163 |
PortAudioStream *stream; |
| 164 |
PaError err; |
| 165 |
int i; |
| 166 |
|
| 167 |
|
| 168 |
err = Pa_OpenStream( |
| 169 |
&stream, |
| 170 |
Pa_GetDefaultInputDeviceID(), |
| 171 |
dataPtr->samplesPerFrame, |
| 172 |
PA_SAMPLE_TYPE, |
| 173 |
NULL, |
| 174 |
paNoDevice, |
| 175 |
0, |
| 176 |
PA_SAMPLE_TYPE, |
| 177 |
NULL, |
| 178 |
SAMPLE_RATE, |
| 179 |
FRAMES_PER_BUFFER, |
| 180 |
NUM_REC_BUFS, |
| 181 |
paClipOff, |
| 182 |
recordCallback, |
| 183 |
dataPtr ); |
| 184 |
if( err != paNoError ) goto error; |
| 185 |
err = Pa_StartStream( stream ); |
| 186 |
if( err != paNoError ) goto error; |
| 187 |
|
| 188 |
printf("Now recording!\n"); fflush(stdout); |
| 189 |
for( i=0; i<(NUM_SECONDS*1000/SLEEP_DUR_MSEC); i++ ) |
| 190 |
{ |
| 191 |
if( Pa_StreamActive( stream ) <= 0) |
| 192 |
{ |
| 193 |
printf("Stream inactive!\n"); |
| 194 |
break; |
| 195 |
} |
| 196 |
if( dataPtr->maxFrameIndex <= dataPtr->frameIndex ) |
| 197 |
{ |
| 198 |
printf("Buffer recording complete.\n"); |
| 199 |
break; |
| 200 |
} |
| 201 |
Pa_Sleep(100); |
| 202 |
printf("index = %d\n", dataPtr->frameIndex ); fflush(stdout); |
| 203 |
} |
| 204 |
|
| 205 |
printf("Finished loop. Close stream.\n"); fflush(stdout); |
| 206 |
|
| 207 |
err = Pa_CloseStream( stream ); |
| 208 |
if( err != paNoError ) goto error; |
| 209 |
|
| 210 |
printf("Done.\n"); fflush(stdout); |
| 211 |
{ |
| 212 |
SAMPLE max = 0; |
| 213 |
SAMPLE posVal; |
| 214 |
int i; |
| 215 |
for( i=0; i<dataPtr->numSamples; i++ ) |
| 216 |
{ |
| 217 |
posVal = dataPtr->recordedSamples[i]; |
| 218 |
if( posVal < 0 ) posVal = -posVal; |
| 219 |
if( posVal > max ) max = posVal; |
| 220 |
} |
| 221 |
printf("Largest recorded sample = %d\n", max ); |
| 222 |
} |
| 223 |
|
| 224 |
#if 0 |
| 225 |
{ |
| 226 |
FILE *fid; |
| 227 |
fid = fopen("recorded.raw", "wb"); |
| 228 |
if( fid == NULL ) |
| 229 |
{ |
| 230 |
printf("Could not open file."); |
| 231 |
} |
| 232 |
else |
| 233 |
{ |
| 234 |
fwrite( dataPtr->recordedSamples, dataPtr->samplesPerFrame * sizeof(SAMPLE), totalFrames, fid ); |
| 235 |
fclose( fid ); |
| 236 |
printf("Wrote data to 'recorded.raw'\n"); |
| 237 |
} |
| 238 |
} |
| 239 |
#endif |
| 240 |
|
| 241 |
error: |
| 242 |
return err; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
PaError TestPlayback( paTestData *dataPtr ) |
| 247 |
{ |
| 248 |
PortAudioStream *stream; |
| 249 |
PaError err; |
| 250 |
int i; |
| 251 |
|
| 252 |
|
| 253 |
dataPtr->frameIndex = 0; |
| 254 |
printf("Begin playback.\n"); fflush(stdout); |
| 255 |
err = Pa_OpenStream( |
| 256 |
&stream, |
| 257 |
paNoDevice, |
| 258 |
0, |
| 259 |
PA_SAMPLE_TYPE, |
| 260 |
NULL, |
| 261 |
Pa_GetDefaultOutputDeviceID(), |
| 262 |
dataPtr->samplesPerFrame, |
| 263 |
PA_SAMPLE_TYPE, |
| 264 |
NULL, |
| 265 |
SAMPLE_RATE, |
| 266 |
FRAMES_PER_BUFFER, |
| 267 |
0, |
| 268 |
paClipOff, |
| 269 |
playCallback, |
| 270 |
dataPtr ); |
| 271 |
if( err != paNoError ) goto error; |
| 272 |
err = Pa_StartStream( stream ); |
| 273 |
if( err != paNoError ) goto error; |
| 274 |
printf("Waiting for playback to finish.\n"); fflush(stdout); |
| 275 |
for( i=0; i<(NUM_SECONDS*1000/SLEEP_DUR_MSEC); i++ ) |
| 276 |
{ |
| 277 |
Pa_Sleep(100); |
| 278 |
printf("index = %d\n", dataPtr->frameIndex ); |
| 279 |
} |
| 280 |
err = Pa_CloseStream( stream ); |
| 281 |
if( err != paNoError ) goto error; |
| 282 |
|
| 283 |
error: |
| 284 |
return err; |
| 285 |
} |
| 286 |
|
| 287 |
int main(void); |
| 288 |
int main(void) |
| 289 |
{ |
| 290 |
PaError err; |
| 291 |
paTestData data; |
| 292 |
long totalFrames; |
| 293 |
long numBytes; |
| 294 |
long i; |
| 295 |
printf("patest_record.c\n"); fflush(stdout); |
| 296 |
|
| 297 |
data.frameIndex = 0; |
| 298 |
data.samplesPerFrame = 2; |
| 299 |
data.maxFrameIndex = totalFrames = NUM_SECONDS*SAMPLE_RATE; |
| 300 |
|
| 301 |
printf("totalFrames = %d\n", totalFrames ); fflush(stdout); |
| 302 |
data.numSamples = totalFrames * data.samplesPerFrame; |
| 303 |
|
| 304 |
numBytes = data.numSamples * sizeof(SAMPLE); |
| 305 |
data.recordedSamples = (SAMPLE *) malloc( numBytes ); |
| 306 |
if( data.recordedSamples == NULL ) |
| 307 |
{ |
| 308 |
printf("Could not allocate record array.\n"); |
| 309 |
exit(1); |
| 310 |
} |
| 311 |
for( i=0; i<data.numSamples; i++ ) data.recordedSamples[i] = 0; |
| 312 |
|
| 313 |
err = Pa_Initialize(); |
| 314 |
if( err != paNoError ) goto error; |
| 315 |
|
| 316 |
for( i=0; i<2; i++ ) |
| 317 |
{ |
| 318 |
err = TestRecording( &data ); |
| 319 |
if( err != paNoError ) goto error; |
| 320 |
|
| 321 |
err = TestPlayback( &data ); |
| 322 |
if( err != paNoError ) goto error; |
| 323 |
} |
| 324 |
|
| 325 |
free( data.recordedSamples ); |
| 326 |
Pa_Terminate(); |
| 327 |
return 0; |
| 328 |
|
| 329 |
error: |
| 330 |
Pa_Terminate(); |
| •331 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 332 |
fprintf( stderr, "Error number: %d\n", err ); |
| 333 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 334 |
if( err == paHostError ) |
| 335 |
{ |
| 336 |
fprintf( stderr, "Host Error number: %d\n", Pa_GetHostError() ); |
| 337 |
} |
| 338 |
return -1; |
| 339 |
} |