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