| 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_CHANNELS (2) |
| 47 |
|
| 48 |
#define DITHER_FLAG (0) |
| 49 |
|
| 50 |
|
| 51 |
#if 1 |
| 52 |
#define PA_SAMPLE_TYPE paFloat32 |
| 53 |
typedef float SAMPLE; |
| 54 |
#define SAMPLE_SILENCE (0.0f) |
| 55 |
#define PRINTF_S_FORMAT "%.8f" |
| 56 |
#elif 1 |
| 57 |
#define PA_SAMPLE_TYPE paInt16 |
| 58 |
typedef short SAMPLE; |
| 59 |
#define SAMPLE_SILENCE (0) |
| 60 |
#define PRINTF_S_FORMAT "%d" |
| 61 |
#elif 0 |
| 62 |
#define PA_SAMPLE_TYPE paInt8 |
| 63 |
typedef char SAMPLE; |
| 64 |
#define SAMPLE_SILENCE (0) |
| 65 |
#define PRINTF_S_FORMAT "%d" |
| 66 |
#else |
| 67 |
#define PA_SAMPLE_TYPE paUInt8 |
| 68 |
typedef unsigned char SAMPLE; |
| 69 |
#define SAMPLE_SILENCE (128) |
| 70 |
#define PRINTF_S_FORMAT "%d" |
| 71 |
#endif |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
int main(void); |
| 76 |
int main(void) |
| 77 |
{ |
| 78 |
PaStreamParameters inputParameters, outputParameters; |
| 79 |
PaStream *stream; |
| 80 |
PaError err; |
| 81 |
SAMPLE *sampleBlock; |
| 82 |
int i; |
| 83 |
int numBytes; |
| 84 |
|
| 85 |
|
| 86 |
printf("patest_read_write_wire.c\n"); fflush(stdout); |
| 87 |
|
| 88 |
numBytes = FRAMES_PER_BUFFER * NUM_CHANNELS * sizeof(SAMPLE); |
| 89 |
sampleBlock = (SAMPLE *) malloc( numBytes ); |
| 90 |
if( sampleBlock == NULL ) |
| 91 |
{ |
| 92 |
printf("Could not allocate record array.\n"); |
| 93 |
exit(1); |
| 94 |
} |
| 95 |
for( i=0; i<FRAMES_PER_BUFFER*NUM_CHANNELS; i++ ) |
| 96 |
sampleBlock[i] = (SAMPLE_SILENCE); |
| 97 |
|
| 98 |
err = Pa_Initialize(); |
| 99 |
if( err != paNoError ) goto error; |
| 100 |
|
| 101 |
inputParameters.device = Pa_GetDefaultInputDevice(); |
| 102 |
printf( "Input device # %d.\n", inputParameters.device ); |
| 103 |
printf( "Input LL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency ); |
| 104 |
printf( "Input HL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ); |
| 105 |
inputParameters.channelCount = NUM_CHANNELS; |
| 106 |
inputParameters.sampleFormat = PA_SAMPLE_TYPE; |
| 107 |
inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ; |
| 108 |
inputParameters.hostApiSpecificStreamInfo = NULL; |
| 109 |
|
| 110 |
outputParameters.device = Pa_GetDefaultOutputDevice(); |
| 111 |
printf( "Output device # %d.\n", outputParameters.device ); |
| 112 |
printf( "Output LL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency ); |
| 113 |
printf( "Output HL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency ); |
| 114 |
outputParameters.channelCount = NUM_CHANNELS; |
| 115 |
outputParameters.sampleFormat = PA_SAMPLE_TYPE; |
| 116 |
outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; |
| 117 |
outputParameters.hostApiSpecificStreamInfo = NULL; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
err = Pa_OpenStream( |
| 122 |
&stream, |
| 123 |
&inputParameters, |
| 124 |
&outputParameters, |
| 125 |
SAMPLE_RATE, |
| 126 |
FRAMES_PER_BUFFER, |
| 127 |
paClipOff, |
| 128 |
NULL, |
| 129 |
NULL ); |
| 130 |
if( err != paNoError ) goto error; |
| 131 |
|
| 132 |
err = Pa_StartStream( stream ); |
| 133 |
if( err != paNoError ) goto error; |
| 134 |
printf("Wire on. Will run one minute.\n"); fflush(stdout); |
| 135 |
|
| 136 |
for( i=0; i<(60*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i ) |
| 137 |
{ |
| 138 |
err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER ); |
| 139 |
if( err ) goto xrun; |
| 140 |
err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER ); |
| 141 |
if( err ) goto xrun; |
| 142 |
} |
| 143 |
err = Pa_StopStream( stream ); |
| 144 |
if( err != paNoError ) goto error; |
| 145 |
|
| 146 |
for( i=0; i<FRAMES_PER_BUFFER*NUM_CHANNELS; i++ ) |
| 147 |
sampleBlock[i] = (SAMPLE_SILENCE); |
| 148 |
|
| 149 |
err = Pa_StartStream( stream ); |
| 150 |
if( err != paNoError ) goto error; |
| 151 |
printf("Wire on. Interrupt to stop.\n"); fflush(stdout); |
| 152 |
|
| 153 |
while( 1 ) |
| 154 |
{ |
| 155 |
err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER ); |
| 156 |
if( err ) goto xrun; |
| 157 |
err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER ); |
| 158 |
if( err ) goto xrun; |
| 159 |
} |
| 160 |
err = Pa_StopStream( stream ); |
| 161 |
if( err != paNoError ) goto error; |
| 162 |
|
| 163 |
free( sampleBlock ); |
| 164 |
|
| 165 |
Pa_Terminate(); |
| 166 |
return 0; |
| 167 |
|
| 168 |
xrun: |
| 169 |
Pa_Terminate(); |
| 170 |
if( err & paInputOverflow ) |
| 171 |
fprintf( stderr, "Input Overflow.\n" ); |
| 172 |
if( err & paOutputUnderflow ) |
| 173 |
fprintf( stderr, "Output Underflow.\n" ); |
| 174 |
return -2; |
| 175 |
|
| 176 |
error: |
| 177 |
Pa_Terminate(); |
| •178 |
fprintf( stderr, "An error occurred while using the portaudio stream\n" ); |
| 179 |
fprintf( stderr, "Error number: %d\n", err ); |
| 180 |
fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|