cannam@124: /** @file paex_read_write_wire.c cannam@124: @ingroup examples_src cannam@124: @brief Tests full duplex blocking I/O by passing input straight to output. cannam@124: @author Bjorn Roche. XO Audio LLC for Z-Systems Engineering. cannam@124: @author based on code by: Phil Burk http://www.softsynth.com cannam@124: @author based on code by: Ross Bencina rossb@audiomulch.com cannam@124: */ cannam@124: /* cannam@124: * $Id: patest_read_record.c 757 2004-02-13 07:48:10Z rossbencina $ cannam@124: * cannam@124: * This program uses the PortAudio Portable Audio Library. cannam@124: * For more information see: http://www.portaudio.com cannam@124: * Copyright (c) 1999-2000 Ross Bencina and Phil Burk cannam@124: * cannam@124: * Permission is hereby granted, free of charge, to any person obtaining cannam@124: * a copy of this software and associated documentation files cannam@124: * (the "Software"), to deal in the Software without restriction, cannam@124: * including without limitation the rights to use, copy, modify, merge, cannam@124: * publish, distribute, sublicense, and/or sell copies of the Software, cannam@124: * and to permit persons to whom the Software is furnished to do so, cannam@124: * subject to the following conditions: cannam@124: * cannam@124: * The above copyright notice and this permission notice shall be cannam@124: * included in all copies or substantial portions of the Software. cannam@124: * cannam@124: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@124: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@124: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. cannam@124: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR cannam@124: * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@124: * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@124: * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@124: */ cannam@124: cannam@124: /* cannam@124: * The text above constitutes the entire PortAudio license; however, cannam@124: * the PortAudio community also makes the following non-binding requests: cannam@124: * cannam@124: * Any person wishing to distribute modifications to the Software is cannam@124: * requested to send the modifications to the original developer so that cannam@124: * they can be incorporated into the canonical version. It is also cannam@124: * requested that these non-binding requests be included along with the cannam@124: * license above. cannam@124: */ cannam@124: cannam@124: #include cannam@124: #include cannam@124: #include cannam@124: #include "portaudio.h" cannam@124: cannam@124: /* #define SAMPLE_RATE (17932) // Test failure to open with this value. */ cannam@124: #define SAMPLE_RATE (44100) cannam@124: #define FRAMES_PER_BUFFER (1024) cannam@124: #define NUM_CHANNELS (2) cannam@124: #define NUM_SECONDS (15) cannam@124: /* #define DITHER_FLAG (paDitherOff) */ cannam@124: #define DITHER_FLAG (0) /**/ cannam@124: cannam@124: /* @todo Underflow and overflow is disabled until we fix priming of blocking write. */ cannam@124: #define CHECK_OVERFLOW (0) cannam@124: #define CHECK_UNDERFLOW (0) cannam@124: cannam@124: cannam@124: /* Select sample format. */ cannam@124: #if 0 cannam@124: #define PA_SAMPLE_TYPE paFloat32 cannam@124: #define SAMPLE_SIZE (4) cannam@124: #define SAMPLE_SILENCE (0.0f) cannam@124: #define CLEAR(a) memset( (a), 0, FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE ) cannam@124: #define PRINTF_S_FORMAT "%.8f" cannam@124: #elif 0 cannam@124: #define PA_SAMPLE_TYPE paInt16 cannam@124: #define SAMPLE_SIZE (2) cannam@124: #define SAMPLE_SILENCE (0) cannam@124: #define CLEAR(a) memset( (a), 0, FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE ) cannam@124: #define PRINTF_S_FORMAT "%d" cannam@124: #elif 1 cannam@124: #define PA_SAMPLE_TYPE paInt24 cannam@124: #define SAMPLE_SIZE (3) cannam@124: #define SAMPLE_SILENCE (0) cannam@124: #define CLEAR(a) memset( (a), 0, FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE ) cannam@124: #define PRINTF_S_FORMAT "%d" cannam@124: #elif 0 cannam@124: #define PA_SAMPLE_TYPE paInt8 cannam@124: #define SAMPLE_SIZE (1) cannam@124: #define SAMPLE_SILENCE (0) cannam@124: #define CLEAR(a) memset( (a), 0, FRAMES_PER_BUFFER * NUM_CHANNELS * SAMPLE_SIZE ) cannam@124: #define PRINTF_S_FORMAT "%d" cannam@124: #else cannam@124: #define PA_SAMPLE_TYPE paUInt8 cannam@124: #define SAMPLE_SIZE (1) cannam@124: #define SAMPLE_SILENCE (128) cannam@124: #define CLEAR( a ) { \ cannam@124: int i; \ cannam@124: for( i=0; idefaultLowInputLatency ); cannam@124: printf( "Input HL: %g s\n", Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ); cannam@124: inputParameters.channelCount = NUM_CHANNELS; cannam@124: inputParameters.sampleFormat = PA_SAMPLE_TYPE; cannam@124: inputParameters.suggestedLatency = Pa_GetDeviceInfo( inputParameters.device )->defaultHighInputLatency ; cannam@124: inputParameters.hostApiSpecificStreamInfo = NULL; cannam@124: cannam@124: outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */ cannam@124: printf( "Output device # %d.\n", outputParameters.device ); cannam@124: printf( "Output LL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency ); cannam@124: printf( "Output HL: %g s\n", Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency ); cannam@124: outputParameters.channelCount = NUM_CHANNELS; cannam@124: outputParameters.sampleFormat = PA_SAMPLE_TYPE; cannam@124: outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency; cannam@124: outputParameters.hostApiSpecificStreamInfo = NULL; cannam@124: cannam@124: /* -- setup -- */ cannam@124: cannam@124: err = Pa_OpenStream( cannam@124: &stream, cannam@124: &inputParameters, cannam@124: &outputParameters, cannam@124: SAMPLE_RATE, cannam@124: FRAMES_PER_BUFFER, cannam@124: paClipOff, /* we won't output out of range samples so don't bother clipping them */ cannam@124: NULL, /* no callback, use blocking API */ cannam@124: NULL ); /* no callback, so no callback userData */ cannam@124: if( err != paNoError ) goto error; cannam@124: cannam@124: err = Pa_StartStream( stream ); cannam@124: if( err != paNoError ) goto error; cannam@124: printf("Wire on. Will run %d seconds.\n", NUM_SECONDS); fflush(stdout); cannam@124: cannam@124: for( i=0; i<(NUM_SECONDS*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i ) cannam@124: { cannam@124: err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER ); cannam@124: if( err && CHECK_UNDERFLOW ) goto xrun; cannam@124: err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER ); cannam@124: if( err && CHECK_OVERFLOW ) goto xrun; cannam@124: } cannam@124: err = Pa_StopStream( stream ); cannam@124: if( err != paNoError ) goto error; cannam@124: cannam@124: CLEAR( sampleBlock ); cannam@124: /* cannam@124: err = Pa_StartStream( stream ); cannam@124: if( err != paNoError ) goto error; cannam@124: printf("Wire on. Interrupt to stop.\n"); fflush(stdout); cannam@124: cannam@124: while( 1 ) cannam@124: { cannam@124: err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER ); cannam@124: if( err ) goto xrun; cannam@124: err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER ); cannam@124: if( err ) goto xrun; cannam@124: } cannam@124: err = Pa_StopStream( stream ); cannam@124: if( err != paNoError ) goto error; cannam@124: cannam@124: Pa_CloseStream( stream ); cannam@124: */ cannam@124: free( sampleBlock ); cannam@124: cannam@124: Pa_Terminate(); cannam@124: return 0; cannam@124: cannam@124: xrun: cannam@124: if( stream ) { cannam@124: Pa_AbortStream( stream ); cannam@124: Pa_CloseStream( stream ); cannam@124: } cannam@124: free( sampleBlock ); cannam@124: Pa_Terminate(); cannam@124: if( err & paInputOverflow ) cannam@124: fprintf( stderr, "Input Overflow.\n" ); cannam@124: if( err & paOutputUnderflow ) cannam@124: fprintf( stderr, "Output Underflow.\n" ); cannam@124: return -2; cannam@124: cannam@124: error: cannam@124: if( stream ) { cannam@124: Pa_AbortStream( stream ); cannam@124: Pa_CloseStream( stream ); cannam@124: } cannam@124: free( sampleBlock ); cannam@124: Pa_Terminate(); cannam@124: fprintf( stderr, "An error occured while using the portaudio stream\n" ); cannam@124: fprintf( stderr, "Error number: %d\n", err ); cannam@124: fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) ); cannam@124: return -1; cannam@124: } cannam@124: