paex_read_write_wire.c
Go to the documentation of this file.00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <string.h>
00049 #include "portaudio.h"
00050
00051
00052 #define SAMPLE_RATE (44100)
00053 #define FRAMES_PER_BUFFER (1024)
00054 #define NUM_CHANNELS (2)
00055 #define NUM_SECONDS (15)
00056
00057 #define DITHER_FLAG (0)
00058
00059
00060 #define CHECK_OVERFLOW (0)
00061 #define CHECK_UNDERFLOW (0)
00062
00063
00064
00065 #if 0
00066 #define PA_SAMPLE_TYPE paFloat32
00067 #define SAMPLE_SIZE (4)
00068 #define SAMPLE_SILENCE (0.0f)
00069 #define CLEAR(a) memset( (a), 0, FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE )
00070 #define PRINTF_S_FORMAT "%.8f"
00071 #elif 0
00072 #define PA_SAMPLE_TYPE paInt16
00073 #define SAMPLE_SIZE (2)
00074 #define SAMPLE_SILENCE (0)
00075 #define CLEAR(a) memset( (a), 0, FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE )
00076 #define PRINTF_S_FORMAT "%d"
00077 #elif 1
00078 #define PA_SAMPLE_TYPE paInt24
00079 #define SAMPLE_SIZE (3)
00080 #define SAMPLE_SILENCE (0)
00081 #define CLEAR(a) memset( (a), 0, FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE )
00082 #define PRINTF_S_FORMAT "%d"
00083 #elif 0
00084 #define PA_SAMPLE_TYPE paInt8
00085 #define SAMPLE_SIZE (1)
00086 #define SAMPLE_SILENCE (0)
00087 #define CLEAR(a) memset( (a), 0, FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE )
00088 #define PRINTF_S_FORMAT "%d"
00089 #else
00090 #define PA_SAMPLE_TYPE paUInt8
00091 #define SAMPLE_SIZE (1)
00092 #define SAMPLE_SILENCE (128)
00093 #define CLEAR( a ) { \
00094 int i; \
00095 for( i=0; i<FRAMES_PER_BUFFER*NUM_CHANNELS; i++ ) \
00096 ((unsigned char *)a)[i] = (SAMPLE_SILENCE); \
00097 }
00098 #define PRINTF_S_FORMAT "%d"
00099 #endif
00100
00101
00102
00103 int main(void);
00104 int main(void)
00105 {
00106 PaStreamParameters inputParameters, outputParameters;
00107 PaStream *stream = NULL;
00108 PaError err;
00109 char *sampleBlock;
00110 int i;
00111 int numBytes;
00112
00113
00114 printf("patest_read_write_wire.c\n"); fflush(stdout);
00115
00116 numBytes = FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE ;
00117 sampleBlock = (char *) malloc( numBytes );
00118 if( sampleBlock == NULL )
00119 {
00120 printf("Could not allocate record array.\n");
00121 exit(1);
00122 }
00123 CLEAR( sampleBlock );
00124
00125 err = Pa_Initialize();
00126 if( err != paNoError ) goto error;
00127
00128 inputParameters.device = Pa_GetDefaultInputDevice();
00129 printf( "Input device # %d.\n", inputParameters.device );
00130 printf( "Input LL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency );
00131 printf( "Input HL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency );
00132 inputParameters.channelCount = NUM_CHANNELS;
00133 inputParameters.sampleFormat = PA_SAMPLE_TYPE;
00134 inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ;
00135 inputParameters.hostApiSpecificStreamInfo = NULL;
00136
00137 outputParameters.device = Pa_GetDefaultOutputDevice();
00138 printf( "Output device # %d.\n", outputParameters.device );
00139 printf( "Output LL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency );
00140 printf( "Output HL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency );
00141 outputParameters.channelCount = NUM_CHANNELS;
00142 outputParameters.sampleFormat = PA_SAMPLE_TYPE;
00143 outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
00144 outputParameters.hostApiSpecificStreamInfo = NULL;
00145
00146
00147
00148 err = Pa_OpenStream(
00149 &stream,
00150 &inputParameters,
00151 &outputParameters,
00152 SAMPLE_RATE,
00153 FRAMES_PER_BUFFER,
00154 paClipOff,
00155 NULL,
00156 NULL );
00157 if( err != paNoError ) goto error;
00158
00159 err = Pa_StartStream( stream );
00160 if( err != paNoError ) goto error;
00161 printf("Wire on. Will run %d seconds.\n", NUM_SECONDS); fflush(stdout);
00162
00163 for( i=0; i<(NUM_SECONDS*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i )
00164 {
00165 err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER );
00166 if( err && CHECK_UNDERFLOW ) goto xrun;
00167 err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER );
00168 if( err && CHECK_OVERFLOW ) goto xrun;
00169 }
00170 err = Pa_StopStream( stream );
00171 if( err != paNoError ) goto error;
00172
00173 CLEAR( sampleBlock );
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 free( sampleBlock );
00192
00193 Pa_Terminate();
00194 return 0;
00195
00196 xrun:
00197 if( stream ) {
00198 Pa_AbortStream( stream );
00199 Pa_CloseStream( stream );
00200 }
00201 free( sampleBlock );
00202 Pa_Terminate();
00203 if( err & paInputOverflow )
00204 fprintf( stderr, "Input Overflow.\n" );
00205 if( err & paOutputUnderflow )
00206 fprintf( stderr, "Output Underflow.\n" );
00207 return -2;
00208
00209 error:
00210 if( stream ) {
00211 Pa_AbortStream( stream );
00212 Pa_CloseStream( stream );
00213 }
00214 free( sampleBlock );
00215 Pa_Terminate();
00216 fprintf( stderr, "An error occured while using the portaudio stream\n" );
00217 fprintf( stderr, "Error number: %d\n", err );
00218 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
00219 return -1;
00220 }
00221