Chris@39: /** @file patest_wire.c Chris@39: @ingroup test_src Chris@39: @brief Pass input directly to output. Chris@39: Chris@39: Note that some HW devices, for example many ISA audio cards Chris@39: on PCs, do NOT support full duplex! For a PC, you normally need Chris@39: a PCI based audio card such as the SBLive. Chris@39: Chris@39: @author Phil Burk http://www.softsynth.com Chris@39: Chris@39: While adapting to V19-API, I excluded configs with framesPerCallback=0 Chris@39: because of an assert in file pa_common/pa_process.c. Pieter, Oct 9, 2003. Chris@39: Chris@39: */ Chris@39: /* Chris@39: * $Id: patest_wire.c 1843 2012-06-22 21:58:10Z philburk $ Chris@39: * Chris@39: * This program uses the PortAudio Portable Audio Library. Chris@39: * For more information see: http://www.portaudio.com Chris@39: * Copyright (c) 1999-2000 Ross Bencina and Phil Burk Chris@39: * Chris@39: * Permission is hereby granted, free of charge, to any person obtaining Chris@39: * a copy of this software and associated documentation files Chris@39: * (the "Software"), to deal in the Software without restriction, Chris@39: * including without limitation the rights to use, copy, modify, merge, Chris@39: * publish, distribute, sublicense, and/or sell copies of the Software, Chris@39: * and to permit persons to whom the Software is furnished to do so, Chris@39: * subject to the following conditions: Chris@39: * Chris@39: * The above copyright notice and this permission notice shall be Chris@39: * included in all copies or substantial portions of the Software. Chris@39: * Chris@39: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@39: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@39: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. Chris@39: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR Chris@39: * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@39: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@39: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@39: */ Chris@39: Chris@39: /* Chris@39: * The text above constitutes the entire PortAudio license; however, Chris@39: * the PortAudio community also makes the following non-binding requests: Chris@39: * Chris@39: * Any person wishing to distribute modifications to the Software is Chris@39: * requested to send the modifications to the original developer so that Chris@39: * they can be incorporated into the canonical version. It is also Chris@39: * requested that these non-binding requests be included along with the Chris@39: * license above. Chris@39: */ Chris@39: Chris@39: #include Chris@39: #include Chris@39: #include "portaudio.h" Chris@39: Chris@39: #define SAMPLE_RATE (44100) Chris@39: Chris@39: typedef struct WireConfig_s Chris@39: { Chris@39: int isInputInterleaved; Chris@39: int isOutputInterleaved; Chris@39: int numInputChannels; Chris@39: int numOutputChannels; Chris@39: int framesPerCallback; Chris@39: /* count status flags */ Chris@39: int numInputUnderflows; Chris@39: int numInputOverflows; Chris@39: int numOutputUnderflows; Chris@39: int numOutputOverflows; Chris@39: int numPrimingOutputs; Chris@39: int numCallbacks; Chris@39: } WireConfig_t; Chris@39: Chris@39: #define USE_FLOAT_INPUT (1) Chris@39: #define USE_FLOAT_OUTPUT (1) Chris@39: Chris@39: /* Latencies set to defaults. */ Chris@39: Chris@39: #if USE_FLOAT_INPUT Chris@39: #define INPUT_FORMAT paFloat32 Chris@39: typedef float INPUT_SAMPLE; Chris@39: #else Chris@39: #define INPUT_FORMAT paInt16 Chris@39: typedef short INPUT_SAMPLE; Chris@39: #endif Chris@39: Chris@39: #if USE_FLOAT_OUTPUT Chris@39: #define OUTPUT_FORMAT paFloat32 Chris@39: typedef float OUTPUT_SAMPLE; Chris@39: #else Chris@39: #define OUTPUT_FORMAT paInt16 Chris@39: typedef short OUTPUT_SAMPLE; Chris@39: #endif Chris@39: Chris@39: double gInOutScaler = 1.0; Chris@39: #define CONVERT_IN_TO_OUT(in) ((OUTPUT_SAMPLE) ((in) * gInOutScaler)) Chris@39: Chris@39: #define INPUT_DEVICE (Pa_GetDefaultInputDevice()) Chris@39: #define OUTPUT_DEVICE (Pa_GetDefaultOutputDevice()) Chris@39: Chris@39: static PaError TestConfiguration( WireConfig_t *config ); Chris@39: Chris@39: static int wireCallback( const void *inputBuffer, void *outputBuffer, Chris@39: unsigned long framesPerBuffer, Chris@39: const PaStreamCallbackTimeInfo* timeInfo, Chris@39: PaStreamCallbackFlags statusFlags, Chris@39: void *userData ); Chris@39: Chris@39: /* This routine will be called by the PortAudio engine when audio is needed. Chris@39: ** It may be called at interrupt level on some machines so don't do anything Chris@39: ** that could mess up the system like calling malloc() or free(). Chris@39: */ Chris@39: Chris@39: static int wireCallback( const void *inputBuffer, void *outputBuffer, Chris@39: unsigned long framesPerBuffer, Chris@39: const PaStreamCallbackTimeInfo* timeInfo, Chris@39: PaStreamCallbackFlags statusFlags, Chris@39: void *userData ) Chris@39: { Chris@39: INPUT_SAMPLE *in; Chris@39: OUTPUT_SAMPLE *out; Chris@39: int inStride; Chris@39: int outStride; Chris@39: int inDone = 0; Chris@39: int outDone = 0; Chris@39: WireConfig_t *config = (WireConfig_t *) userData; Chris@39: unsigned int i; Chris@39: int inChannel, outChannel; Chris@39: Chris@39: /* This may get called with NULL inputBuffer during initial setup. */ Chris@39: if( inputBuffer == NULL) return 0; Chris@39: Chris@39: /* Count flags */ Chris@39: if( (statusFlags & paInputUnderflow) != 0 ) config->numInputUnderflows += 1; Chris@39: if( (statusFlags & paInputOverflow) != 0 ) config->numInputOverflows += 1; Chris@39: if( (statusFlags & paOutputUnderflow) != 0 ) config->numOutputUnderflows += 1; Chris@39: if( (statusFlags & paOutputOverflow) != 0 ) config->numOutputOverflows += 1; Chris@39: if( (statusFlags & paPrimingOutput) != 0 ) config->numPrimingOutputs += 1; Chris@39: config->numCallbacks += 1; Chris@39: Chris@39: inChannel=0, outChannel=0; Chris@39: while( !(inDone && outDone) ) Chris@39: { Chris@39: if( config->isInputInterleaved ) Chris@39: { Chris@39: in = ((INPUT_SAMPLE*)inputBuffer) + inChannel; Chris@39: inStride = config->numInputChannels; Chris@39: } Chris@39: else Chris@39: { Chris@39: in = ((INPUT_SAMPLE**)inputBuffer)[inChannel]; Chris@39: inStride = 1; Chris@39: } Chris@39: Chris@39: if( config->isOutputInterleaved ) Chris@39: { Chris@39: out = ((OUTPUT_SAMPLE*)outputBuffer) + outChannel; Chris@39: outStride = config->numOutputChannels; Chris@39: } Chris@39: else Chris@39: { Chris@39: out = ((OUTPUT_SAMPLE**)outputBuffer)[outChannel]; Chris@39: outStride = 1; Chris@39: } Chris@39: Chris@39: for( i=0; inumInputChannels - 1)) inChannel++; Chris@39: else inDone = 1; Chris@39: if(outChannel < (config->numOutputChannels - 1)) outChannel++; Chris@39: else outDone = 1; Chris@39: } Chris@39: return 0; Chris@39: } Chris@39: Chris@39: /*******************************************************************/ Chris@39: int main(void); Chris@39: int main(void) Chris@39: { Chris@39: PaError err = paNoError; Chris@39: WireConfig_t CONFIG; Chris@39: WireConfig_t *config = &CONFIG; Chris@39: int configIndex = 0;; Chris@39: Chris@39: err = Pa_Initialize(); Chris@39: if( err != paNoError ) goto error; Chris@39: Chris@39: printf("Please connect audio signal to input and listen for it on output!\n"); Chris@39: printf("input format = %lu\n", INPUT_FORMAT ); Chris@39: printf("output format = %lu\n", OUTPUT_FORMAT ); Chris@39: printf("input device ID = %d\n", INPUT_DEVICE ); Chris@39: printf("output device ID = %d\n", OUTPUT_DEVICE ); Chris@39: Chris@39: if( INPUT_FORMAT == OUTPUT_FORMAT ) Chris@39: { Chris@39: gInOutScaler = 1.0; Chris@39: } Chris@39: else if( (INPUT_FORMAT == paInt16) && (OUTPUT_FORMAT == paFloat32) ) Chris@39: { Chris@39: gInOutScaler = 1.0/32768.0; Chris@39: } Chris@39: else if( (INPUT_FORMAT == paFloat32) && (OUTPUT_FORMAT == paInt16) ) Chris@39: { Chris@39: gInOutScaler = 32768.0; Chris@39: } Chris@39: Chris@39: for( config->isInputInterleaved = 0; config->isInputInterleaved < 2; config->isInputInterleaved++ ) Chris@39: { Chris@39: for( config->isOutputInterleaved = 0; config->isOutputInterleaved < 2; config->isOutputInterleaved++ ) Chris@39: { Chris@39: for( config->numInputChannels = 1; config->numInputChannels < 3; config->numInputChannels++ ) Chris@39: { Chris@39: for( config->numOutputChannels = 1; config->numOutputChannels < 3; config->numOutputChannels++ ) Chris@39: { Chris@39: /* If framesPerCallback = 0, assertion fails in file pa_common/pa_process.c, line 1413: EX. */ Chris@39: for( config->framesPerCallback = 64; config->framesPerCallback < 129; config->framesPerCallback += 64 ) Chris@39: { Chris@39: printf("-----------------------------------------------\n" ); Chris@39: printf("Configuration #%d\n", configIndex++ ); Chris@39: err = TestConfiguration( config ); Chris@39: /* Give user a chance to bail out. */ Chris@39: if( err == 1 ) Chris@39: { Chris@39: err = paNoError; Chris@39: goto done; Chris@39: } Chris@39: else if( err != paNoError ) goto error; Chris@39: } Chris@39: } Chris@39: } Chris@39: } Chris@39: } Chris@39: Chris@39: done: Chris@39: Pa_Terminate(); Chris@39: printf("Full duplex sound test complete.\n"); fflush(stdout); Chris@39: printf("Hit ENTER to quit.\n"); fflush(stdout); Chris@39: getchar(); Chris@39: return 0; Chris@39: Chris@39: error: Chris@39: Pa_Terminate(); Chris@39: fprintf( stderr, "An error occured while using the portaudio stream\n" ); Chris@39: fprintf( stderr, "Error number: %d\n", err ); Chris@39: fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); Chris@39: printf("Hit ENTER to quit.\n"); fflush(stdout); Chris@39: getchar(); Chris@39: return -1; Chris@39: } Chris@39: Chris@39: static PaError TestConfiguration( WireConfig_t *config ) Chris@39: { Chris@39: int c; Chris@39: PaError err = paNoError; Chris@39: PaStream *stream; Chris@39: PaStreamParameters inputParameters, outputParameters; Chris@39: Chris@39: printf("input %sinterleaved!\n", (config->isInputInterleaved ? " " : "NOT ") ); Chris@39: printf("output %sinterleaved!\n", (config->isOutputInterleaved ? " " : "NOT ") ); Chris@39: printf("input channels = %d\n", config->numInputChannels ); Chris@39: printf("output channels = %d\n", config->numOutputChannels ); Chris@39: printf("framesPerCallback = %d\n", config->framesPerCallback ); Chris@39: Chris@39: inputParameters.device = INPUT_DEVICE; /* default input device */ Chris@39: if (inputParameters.device == paNoDevice) { Chris@39: fprintf(stderr,"Error: No default input device.\n"); Chris@39: goto error; Chris@39: } Chris@39: inputParameters.channelCount = config->numInputChannels; Chris@39: inputParameters.sampleFormat = INPUT_FORMAT | (config->isInputInterleaved ? 0 : paNonInterleaved); Chris@39: inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultLowInputLatency; Chris@39: inputParameters.hostApiSpecificStreamInfo = NULL; Chris@39: Chris@39: outputParameters.device = OUTPUT_DEVICE; /* default output device */ Chris@39: if (outputParameters.device == paNoDevice) { Chris@39: fprintf(stderr,"Error: No default output device.\n"); Chris@39: goto error; Chris@39: } Chris@39: outputParameters.channelCount = config->numOutputChannels; Chris@39: outputParameters.sampleFormat = OUTPUT_FORMAT | (config->isOutputInterleaved ? 0 : paNonInterleaved); Chris@39: outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency; Chris@39: outputParameters.hostApiSpecificStreamInfo = NULL; Chris@39: Chris@39: config->numInputUnderflows = 0; Chris@39: config->numInputOverflows = 0; Chris@39: config->numOutputUnderflows = 0; Chris@39: config->numOutputOverflows = 0; Chris@39: config->numPrimingOutputs = 0; Chris@39: config->numCallbacks = 0; Chris@39: Chris@39: err = Pa_OpenStream( Chris@39: &stream, Chris@39: &inputParameters, Chris@39: &outputParameters, Chris@39: SAMPLE_RATE, Chris@39: config->framesPerCallback, /* frames per buffer */ Chris@39: paClipOff, /* we won't output out of range samples so don't bother clipping them */ Chris@39: wireCallback, Chris@39: config ); Chris@39: if( err != paNoError ) goto error; Chris@39: Chris@39: err = Pa_StartStream( stream ); Chris@39: if( err != paNoError ) goto error; Chris@39: Chris@39: printf("Now recording and playing. - Hit ENTER for next configuration, or 'q' to quit.\n"); fflush(stdout); Chris@39: c = getchar(); Chris@39: Chris@39: printf("Closing stream.\n"); Chris@39: err = Pa_CloseStream( stream ); Chris@39: if( err != paNoError ) goto error; Chris@39: Chris@39: #define CHECK_FLAG_COUNT(member) \ Chris@39: if( config->member > 0 ) printf("FLAGS SET: " #member " = %d\n", config->member ); Chris@39: CHECK_FLAG_COUNT( numInputUnderflows ); Chris@39: CHECK_FLAG_COUNT( numInputOverflows ); Chris@39: CHECK_FLAG_COUNT( numOutputUnderflows ); Chris@39: CHECK_FLAG_COUNT( numOutputOverflows ); Chris@39: CHECK_FLAG_COUNT( numPrimingOutputs ); Chris@39: printf("number of callbacks = %d\n", config->numCallbacks ); Chris@39: Chris@39: if( c == 'q' ) return 1; Chris@39: Chris@39: error: Chris@39: return err; Chris@39: }