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 (512)
00054 #define NUM_SECONDS (10)
00055
00056 #define DITHER_FLAG (0)
00057
00058
00059 #if 1
00060 #define PA_SAMPLE_TYPE paFloat32
00061 #define SAMPLE_SIZE (4)
00062 #define SAMPLE_SILENCE (0.0f)
00063 #define PRINTF_S_FORMAT "%.8f"
00064 #elif 0
00065 #define PA_SAMPLE_TYPE paInt16
00066 #define SAMPLE_SIZE (2)
00067 #define SAMPLE_SILENCE (0)
00068 #define PRINTF_S_FORMAT "%d"
00069 #elif 0
00070 #define PA_SAMPLE_TYPE paInt24
00071 #define SAMPLE_SIZE (3)
00072 #define SAMPLE_SILENCE (0)
00073 #define PRINTF_S_FORMAT "%d"
00074 #elif 0
00075 #define PA_SAMPLE_TYPE paInt8
00076 #define SAMPLE_SIZE (1)
00077 #define SAMPLE_SILENCE (0)
00078 #define PRINTF_S_FORMAT "%d"
00079 #else
00080 #define PA_SAMPLE_TYPE paUInt8
00081 #define SAMPLE_SIZE (1)
00082 #define SAMPLE_SILENCE (128)
00083 #define PRINTF_S_FORMAT "%d"
00084 #endif
00085
00086
00087 int main(void);
00088 int main(void)
00089 {
00090 PaStreamParameters inputParameters, outputParameters;
00091 PaStream *stream = NULL;
00092 PaError err;
00093 const PaDeviceInfo* inputInfo;
00094 const PaDeviceInfo* outputInfo;
00095 char *sampleBlock = NULL;
00096 int i;
00097 int numBytes;
00098 int numChannels;
00099
00100 printf("patest_read_write_wire.c\n"); fflush(stdout);
00101 printf("sizeof(int) = %lu\n", sizeof(int)); fflush(stdout);
00102 printf("sizeof(long) = %lu\n", sizeof(long)); fflush(stdout);
00103
00104 err = Pa_Initialize();
00105 if( err != paNoError ) goto error2;
00106
00107 inputParameters.device = Pa_GetDefaultInputDevice();
00108 printf( "Input device # %d.\n", inputParameters.device );
00109 inputInfo = Pa_GetDeviceInfo( inputParameters.device );
00110 printf( " Name: %s\n", inputInfo->name );
00111 printf( " LL: %g s\n", inputInfo->defaultLowInputLatency );
00112 printf( " HL: %g s\n", inputInfo->defaultHighInputLatency );
00113
00114 outputParameters.device = Pa_GetDefaultOutputDevice();
00115 printf( "Output device # %d.\n", outputParameters.device );
00116 outputInfo = Pa_GetDeviceInfo( outputParameters.device );
00117 printf( " Name: %s\n", outputInfo->name );
00118 printf( " LL: %g s\n", outputInfo->defaultLowOutputLatency );
00119 printf( " HL: %g s\n", outputInfo->defaultHighOutputLatency );
00120
00121 numChannels = inputInfo->maxInputChannels < outputInfo->maxOutputChannels
00122 ? inputInfo->maxInputChannels : outputInfo->maxOutputChannels;
00123 printf( "Num channels = %d.\n", numChannels );
00124
00125 inputParameters.channelCount = numChannels;
00126 inputParameters.sampleFormat = PA_SAMPLE_TYPE;
00127 inputParameters.suggestedLatency = inputInfo->defaultHighInputLatency ;
00128 inputParameters.hostApiSpecificStreamInfo = NULL;
00129
00130 outputParameters.channelCount = numChannels;
00131 outputParameters.sampleFormat = PA_SAMPLE_TYPE;
00132 outputParameters.suggestedLatency = outputInfo->defaultHighOutputLatency;
00133 outputParameters.hostApiSpecificStreamInfo = NULL;
00134
00135
00136
00137 err = Pa_OpenStream(
00138 &stream,
00139 &inputParameters,
00140 &outputParameters,
00141 SAMPLE_RATE,
00142 FRAMES_PER_BUFFER,
00143 paClipOff,
00144 NULL,
00145 NULL );
00146 if( err != paNoError ) goto error2;
00147
00148 numBytes = FRAMES_PER_BUFFER * numChannels * SAMPLE_SIZE ;
00149 sampleBlock = (char *) malloc( numBytes );
00150 if( sampleBlock == NULL )
00151 {
00152 printf("Could not allocate record array.\n");
00153 goto error1;
00154 }
00155 memset( sampleBlock, SAMPLE_SILENCE, numBytes );
00156
00157 err = Pa_StartStream( stream );
00158 if( err != paNoError ) goto error1;
00159 printf("Wire on. Will run %d seconds.\n", NUM_SECONDS); fflush(stdout);
00160
00161 for( i=0; i<(NUM_SECONDS*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i )
00162 {
00163
00164 err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER );
00165 if( err ) goto xrun;
00166 err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER );
00167 if( err ) goto xrun;
00168 }
00169 printf("Wire off.\n"); fflush(stdout);
00170
00171 err = Pa_StopStream( stream );
00172 if( err != paNoError ) goto error1;
00173
00174 free( sampleBlock );
00175
00176 Pa_Terminate();
00177 return 0;
00178
00179 xrun:
00180 printf("err = %d\n", err); fflush(stdout);
00181 if( stream ) {
00182 Pa_AbortStream( stream );
00183 Pa_CloseStream( stream );
00184 }
00185 free( sampleBlock );
00186 Pa_Terminate();
00187 if( err & paInputOverflow )
00188 fprintf( stderr, "Input Overflow.\n" );
00189 if( err & paOutputUnderflow )
00190 fprintf( stderr, "Output Underflow.\n" );
00191 return -2;
00192 error1:
00193 free( sampleBlock );
00194 error2:
00195 if( stream ) {
00196 Pa_AbortStream( stream );
00197 Pa_CloseStream( stream );
00198 }
00199 Pa_Terminate();
00200 fprintf( stderr, "An error occured while using the portaudio stream\n" );
00201 fprintf( stderr, "Error number: %d\n", err );
00202 fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
00203 return -1;
00204 }
00205