|
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 (512) 00054 #define NUM_SECONDS (10) 00055 /* #define DITHER_FLAG (paDitherOff) */ 00056 #define DITHER_FLAG (0) 00057 00058 /* Select sample format. */ 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(); /* default input device */ 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(); /* default output device */ 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 /* -- setup -- */ 00136 00137 err = Pa_OpenStream( 00138 &stream, 00139 &inputParameters, 00140 &outputParameters, 00141 SAMPLE_RATE, 00142 FRAMES_PER_BUFFER, 00143 paClipOff, /* we won't output out of range samples so don't bother clipping them */ 00144 NULL, /* no callback, use blocking API */ 00145 NULL ); /* no callback, so no callback userData */ 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 // You may get underruns or overruns if the output is not primed by PortAudio. 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
1.7.3