PortAudio 2.0
|
00001 00008 /* 00009 * $Id: patest_read_record.c 757 2004-02-13 07:48:10Z rossbencina $ 00010 * 00011 * This program uses the PortAudio Portable Audio Library. 00012 * For more information see: http://www.portaudio.com 00013 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk 00014 * 00015 * Permission is hereby granted, free of charge, to any person obtaining 00016 * a copy of this software and associated documentation files 00017 * (the "Software"), to deal in the Software without restriction, 00018 * including without limitation the rights to use, copy, modify, merge, 00019 * publish, distribute, sublicense, and/or sell copies of the Software, 00020 * and to permit persons to whom the Software is furnished to do so, 00021 * subject to the following conditions: 00022 * 00023 * The above copyright notice and this permission notice shall be 00024 * included in all copies or substantial portions of the Software. 00025 * 00026 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00027 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00028 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00029 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 00030 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 00031 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00032 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00033 */ 00034 00035 /* 00036 * The text above constitutes the entire PortAudio license; however, 00037 * the PortAudio community also makes the following non-binding requests: 00038 * 00039 * Any person wishing to distribute modifications to the Software is 00040 * requested to send the modifications to the original developer so that 00041 * they can be incorporated into the canonical version. It is also 00042 * requested that these non-binding requests be included along with the 00043 * license above. 00044 */ 00045 00046 #include <stdio.h> 00047 #include <stdlib.h> 00048 #include <string.h> 00049 #include "portaudio.h" 00050 00051 /* #define SAMPLE_RATE (17932) // Test failure to open with this value. */ 00052 #define SAMPLE_RATE (44100) 00053 #define FRAMES_PER_BUFFER (1024) 00054 #define NUM_CHANNELS (2) 00055 #define NUM_SECONDS (15) 00056 /* #define DITHER_FLAG (paDitherOff) */ 00057 #define DITHER_FLAG (0) 00058 00059 /* @todo Underflow and overflow is disabled until we fix priming of blocking write. */ 00060 #define CHECK_OVERFLOW (0) 00061 #define CHECK_UNDERFLOW (0) 00062 00063 00064 /* Select sample format. */ 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(); /* default input device */ 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(); /* default output device */ 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 /* -- setup -- */ 00147 00148 err = Pa_OpenStream( 00149 &stream, 00150 &inputParameters, 00151 &outputParameters, 00152 SAMPLE_RATE, 00153 FRAMES_PER_BUFFER, 00154 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 00155 NULL, /* no callback, use blocking API */ 00156 NULL ); /* no callback, so no callback userData */ 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 err = Pa_StartStream( stream ); 00176 if( err != paNoError ) goto error; 00177 printf("Wire on. Interrupt to stop.\n"); fflush(stdout); 00178 00179 while( 1 ) 00180 { 00181 err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER ); 00182 if( err ) goto xrun; 00183 err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER ); 00184 if( err ) goto xrun; 00185 } 00186 err = Pa_StopStream( stream ); 00187 if( err != paNoError ) goto error; 00188 00189 Pa_CloseStream( stream ); 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