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