| 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 |
|
| 39 |
#include <stdio.h> |
| 40 |
#include <stdlib.h> |
| 41 |
#include "portaudio.h" |
| 42 |
|
| 43 |
|
| 44 |
#define SAMPLE_RATE (44100) |
| 45 |
#define FRAMES_PER_BUFFER (1024) |
| 46 |
#define NUM_SECONDS (5) |
| 47 |
#define NUM_CHANNELS (2) |
| 48 |
|
| 49 |
#define DITHER_FLAG (0) |
| 50 |
|
| 51 |
|
| 52 |
#if 1 |
| 53 |
#define PA_SAMPLE_TYPE paFloat32 |
| 54 |
typedef float SAMPLE; |
| 55 |
#define SAMPLE_SILENCE (0.0f) |
| 56 |
#define PRINTF_S_FORMAT "%.8f" |
| 57 |
#elif 1 |
| 58 |
#define PA_SAMPLE_TYPE paInt16 |
| 59 |
typedef short SAMPLE; |
| 60 |
#define SAMPLE_SILENCE (0) |
| 61 |
#define PRINTF_S_FORMAT "%d" |
| 62 |
#elif 0 |
| 63 |
#define PA_SAMPLE_TYPE paInt8 |
| 64 |
typedef char SAMPLE; |
| 65 |
#define SAMPLE_SILENCE (0) |
| 66 |
#define PRINTF_S_FORMAT "%d" |
| 67 |
#else |
| 68 |
#define PA_SAMPLE_TYPE paUInt8 |
| 69 |
typedef unsigned char SAMPLE; |
| 70 |
#define SAMPLE_SILENCE (128) |
| 71 |
#define PRINTF_S_FORMAT "%d" |
| 72 |
#endif |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
int main(void); |
| 77 |
int main(void) |
| 78 |
{ |
| 79 |
PaStreamParameters inputParameters, outputParameters; |
| 80 |
PaStream *stream; |
| 81 |
PaError err; |
| 82 |
SAMPLE *recordedSamples; |
| 83 |
int i; |
| 84 |
int totalFrames; |
| 85 |
int numSamples; |
| 86 |
int numBytes; |
| 87 |
SAMPLE max, average, val; |
| 88 |
|
| 89 |
|
| 90 |
printf("patest_read_record.c\n"); fflush(stdout); |
| 91 |
|
| 92 |
totalFrames = NUM_SECONDS * SAMPLE_RATE; |
| 93 |
numSamples = totalFrames * NUM_CHANNELS; |
| 94 |
|
| 95 |
numBytes = numSamples * sizeof(SAMPLE); |
| 96 |
recordedSamples = (SAMPLE *) malloc( numBytes ); |
| 97 |
if( recordedSamples == NULL ) |
| 98 |
{ |
| 99 |
printf("Could not allocate record array.\n"); |
| 100 |
exit(1); |
| 101 |
} |
| 102 |
for( i=0; i<numSamples; i++ ) recordedSamples[i] = 0; |
| 103 |
|
| 104 |
err = Pa_Initialize(); |
| 105 |
if( err != paNoError ) goto error; |
| 106 |
|
| 107 |
inputParameters.device = Pa_GetDefaultInputDevice(); |
| 108 |
inputParameters.channelCount = NUM_CHANNELS; |
| 109 |
inputParameters.sampleFormat = PA_SAMPLE_TYPE; |
| 110 |
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency; |
| 111 |
inputParameters.hostApiSpecificStreamInfo = NULL; |
| 112 |
|
| 113 |
|
| 114 |
err = Pa_OpenStream( |
| 115 |
&stream, |
| 116 |
&inputParameters, |
| 117 |
NULL, |
| 118 |
SAMPLE_RATE, |
| 119 |
FRAMES_PER_BUFFER, |
| 120 |
paClipOff, |
| 121 |
NULL, |
| 122 |
NULL ); |
| 123 |
if( err != paNoError ) goto error; |
| 124 |
|
| 125 |
err = Pa_StartStream( stream ); |
| 126 |
if( err != paNoError ) goto error; |
| 127 |
printf("Now recording!!\n"); fflush(stdout); |
| 128 |
|
| 129 |
err = Pa_ReadStream( stream, recordedSamples, totalFrames ); |
| 130 |
if( err != paNoError ) goto error; |
| 131 |
|
| 132 |
err = Pa_CloseStream( stream ); |
| 133 |
if( err != paNoError ) goto error; |
| 134 |
|
| 135 |
|
| 136 |
max = 0; |
| 137 |
average = 0; |
| 138 |
for( i=0; i<numSamples; i++ ) |
| 139 |
{ |
| 140 |
val = recordedSamples[i]; |
| 141 |
if( val < 0 ) val = -val; |
| 142 |
if( val > max ) |
| 143 |
{ |
| 144 |
max = val; |
| 145 |
} |
| 146 |
average += val; |
| 147 |
} |
| 148 |
|
| 149 |
average = average / numSamples; |
| 150 |
|
| 151 |
printf("Sample max amplitude = "PRINTF_S_FORMAT"\n", max ); |
| 152 |
printf("Sample average = "PRINTF_S_FORMAT"\n", average ); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
#if 0 |
| 168 |
{ |
| 169 |
FILE *fid; |
| 170 |
fid = fopen("recorded.raw", "wb"); |
| 171 |
if( fid == NULL ) |
| 172 |
{ |
| 173 |
printf("Could not open file."); |
| 174 |
} |
| 175 |
else |
| 176 |
{ |
| 177 |
fwrite( recordedSamples, NUM_CHANNELS * sizeof(SAMPLE), totalFrames, fid ); |
| 178 |
fclose( fid ); |
| 179 |
printf("Wrote data to 'recorded.raw'\n"); |
| 180 |
} |
| 181 |
} |
| 182 |
#endif |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 187 |
outputParameters.channelCount = NUM_CHANNELS; |
| 188 |
outputParameters.sampleFormat = PA_SAMPLE_TYPE; |
| 189 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; |
| 190 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 191 |
|
| 192 |
printf("Begin playback.\n"); fflush(stdout); |
| 193 |
err = Pa_OpenStream( |
| 194 |
&stream, |
| 195 |
NULL, |
| 196 |
&outputParameters, |
| 197 |
SAMPLE_RATE, |
| 198 |
FRAMES_PER_BUFFER, |
| 199 |
paClipOff, |
| 200 |
NULL, |
| 201 |
NULL ); |
| 202 |
if( err != paNoError ) goto error; |
| 203 |
|
| 204 |
if( stream ) |
| 205 |
{ |
| 206 |
err = Pa_StartStream( stream ); |
| 207 |
if( err != paNoError ) goto error; |
| 208 |
printf("Waiting for playback to finish.\n"); fflush(stdout); |
| 209 |
|
| 210 |
err = Pa_WriteStream( stream, recordedSamples, totalFrames ); |
| 211 |
if( err != paNoError ) goto error; |
| 212 |
|
| 213 |
err = Pa_CloseStream( stream ); |
| 214 |
if( err != paNoError ) goto error; |
| 215 |
printf("Done.\n"); fflush(stdout); |
| 216 |
} |
| 217 |
free( recordedSamples ); |
| 218 |
|
| 219 |
Pa_Terminate(); |
| 220 |
return 0; |
| 221 |
|
| 222 |
error: |
| 223 |
Pa_Terminate(); |
| •224 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 225 |
fprintf( stderr, "Error number: %d\n", err ); |
| 226 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 227 |
return -1; |
| 228 |
} |
| 229 |
|