Chris@39: /* Chris@39: * $Id: pablio.c 1151 2006-11-29 02:11:16Z leland_lucius $ Chris@39: * pablio.c Chris@39: * Portable Audio Blocking Input/Output utility. Chris@39: * Chris@39: * Author: Phil Burk, http://www.softsynth.com 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 Chris@39: #include "portaudio.h" Chris@39: #include "pa_ringbuffer.h" Chris@39: #include "pablio.h" Chris@39: #include Chris@39: Chris@39: /************************************************************************/ Chris@39: /******** Constants *****************************************************/ Chris@39: /************************************************************************/ Chris@39: Chris@39: #define FRAMES_PER_BUFFER (256) Chris@39: Chris@39: /************************************************************************/ Chris@39: /******** Prototypes ****************************************************/ Chris@39: /************************************************************************/ Chris@39: Chris@39: static int blockingIOCallback( void *inputBuffer, void *outputBuffer, Chris@39: unsigned long framesPerBuffer, Chris@39: PaTimestamp outTime, void *userData ); Chris@39: static PaError PABLIO_InitFIFO( RingBuffer *rbuf, long numFrames, long bytesPerFrame ); Chris@39: static PaError PABLIO_TermFIFO( RingBuffer *rbuf ); Chris@39: Chris@39: /************************************************************************/ Chris@39: /******** Functions *****************************************************/ Chris@39: /************************************************************************/ Chris@39: Chris@39: /* Called from PortAudio. Chris@39: * Read and write data only if there is room in FIFOs. Chris@39: */ Chris@39: static int blockingIOCallback( void *inputBuffer, void *outputBuffer, Chris@39: unsigned long framesPerBuffer, Chris@39: PaTimestamp outTime, void *userData ) Chris@39: { Chris@39: PABLIO_Stream *data = (PABLIO_Stream*)userData; Chris@39: long numBytes = data->bytesPerFrame * framesPerBuffer; Chris@39: (void) outTime; Chris@39: Chris@39: /* This may get called with NULL inputBuffer during initial setup. */ Chris@39: if( inputBuffer != NULL ) Chris@39: { Chris@39: PaUtil_WriteRingBuffer( &data->inFIFO, inputBuffer, numBytes ); Chris@39: } Chris@39: if( outputBuffer != NULL ) Chris@39: { Chris@39: int i; Chris@39: int numRead = PaUtil_ReadRingBuffer( &data->outFIFO, outputBuffer, numBytes ); Chris@39: /* Zero out remainder of buffer if we run out of data. */ Chris@39: for( i=numRead; ibuffer ) free( rbuf->buffer ); Chris@39: rbuf->buffer = NULL; Chris@39: return paNoError; Chris@39: } Chris@39: Chris@39: /************************************************************ Chris@39: * Write data to ring buffer. Chris@39: * Will not return until all the data has been written. Chris@39: */ Chris@39: long WriteAudioStream( PABLIO_Stream *aStream, void *data, long numFrames ) Chris@39: { Chris@39: long bytesWritten; Chris@39: char *p = (char *) data; Chris@39: long numBytes = aStream->bytesPerFrame * numFrames; Chris@39: while( numBytes > 0) Chris@39: { Chris@39: bytesWritten = PaUtil_WriteRingBuffer( &aStream->outFIFO, p, numBytes ); Chris@39: numBytes -= bytesWritten; Chris@39: p += bytesWritten; Chris@39: if( numBytes > 0) Pa_Sleep(10); Chris@39: } Chris@39: return numFrames; Chris@39: } Chris@39: Chris@39: /************************************************************ Chris@39: * Read data from ring buffer. Chris@39: * Will not return until all the data has been read. Chris@39: */ Chris@39: long ReadAudioStream( PABLIO_Stream *aStream, void *data, long numFrames ) Chris@39: { Chris@39: long bytesRead; Chris@39: char *p = (char *) data; Chris@39: long numBytes = aStream->bytesPerFrame * numFrames; Chris@39: while( numBytes > 0) Chris@39: { Chris@39: bytesRead = PaUtil_ReadRingBuffer( &aStream->inFIFO, p, numBytes ); Chris@39: numBytes -= bytesRead; Chris@39: p += bytesRead; Chris@39: if( numBytes > 0) Pa_Sleep(10); Chris@39: } Chris@39: return numFrames; Chris@39: } Chris@39: Chris@39: /************************************************************ Chris@39: * Return the number of frames that could be written to the stream without Chris@39: * having to wait. Chris@39: */ Chris@39: long GetAudioStreamWriteable( PABLIO_Stream *aStream ) Chris@39: { Chris@39: int bytesEmpty = PaUtil_GetRingBufferWriteAvailable( &aStream->outFIFO ); Chris@39: return bytesEmpty / aStream->bytesPerFrame; Chris@39: } Chris@39: Chris@39: /************************************************************ Chris@39: * Return the number of frames that are available to be read from the Chris@39: * stream without having to wait. Chris@39: */ Chris@39: long GetAudioStreamReadable( PABLIO_Stream *aStream ) Chris@39: { Chris@39: int bytesFull = PaUtil_GetRingBufferReadAvailable( &aStream->inFIFO ); Chris@39: return bytesFull / aStream->bytesPerFrame; Chris@39: } Chris@39: Chris@39: /************************************************************/ Chris@39: static unsigned long RoundUpToNextPowerOf2( unsigned long n ) Chris@39: { Chris@39: long numBits = 0; Chris@39: if( ((n-1) & n) == 0) return n; /* Already Power of two. */ Chris@39: while( n > 0 ) Chris@39: { Chris@39: n= n>>1; Chris@39: numBits++; Chris@39: } Chris@39: return (1<samplesPerFrame = ((flags&PABLIO_MONO) != 0) ? 1 : 2; Chris@39: aStream->bytesPerFrame = bytesPerSample * aStream->samplesPerFrame; Chris@39: Chris@39: /* Initialize PortAudio */ Chris@39: err = Pa_Initialize(); Chris@39: if( err != paNoError ) goto error; Chris@39: Chris@39: /* Warning: numFrames must be larger than amount of data processed per interrupt Chris@39: * inside PA to prevent glitches. Just to be safe, adjust size upwards. Chris@39: */ Chris@39: minNumBuffers = 2 * Pa_GetMinNumBuffers( FRAMES_PER_BUFFER, sampleRate ); Chris@39: numFrames = minNumBuffers * FRAMES_PER_BUFFER; Chris@39: numFrames = RoundUpToNextPowerOf2( numFrames ); Chris@39: Chris@39: /* Initialize Ring Buffers */ Chris@39: doRead = ((flags & PABLIO_READ) != 0); Chris@39: doWrite = ((flags & PABLIO_WRITE) != 0); Chris@39: if(doRead) Chris@39: { Chris@39: err = PABLIO_InitFIFO( &aStream->inFIFO, numFrames, aStream->bytesPerFrame ); Chris@39: if( err != paNoError ) goto error; Chris@39: } Chris@39: if(doWrite) Chris@39: { Chris@39: long numBytes; Chris@39: err = PABLIO_InitFIFO( &aStream->outFIFO, numFrames, aStream->bytesPerFrame ); Chris@39: if( err != paNoError ) goto error; Chris@39: /* Make Write FIFO appear full initially. */ Chris@39: numBytes = PaUtil_GetRingBufferWriteAvailable( &aStream->outFIFO ); Chris@39: PaUtil_AdvanceRingBufferWriteIndex( &aStream->outFIFO, numBytes ); Chris@39: } Chris@39: Chris@39: /* Open a PortAudio stream that we will use to communicate with the underlying Chris@39: * audio drivers. */ Chris@39: err = Pa_OpenStream( Chris@39: &aStream->stream, Chris@39: (doRead ? Pa_GetDefaultInputDeviceID() : paNoDevice), Chris@39: (doRead ? aStream->samplesPerFrame : 0 ), Chris@39: format, Chris@39: NULL, Chris@39: (doWrite ? Pa_GetDefaultOutputDeviceID() : paNoDevice), Chris@39: (doWrite ? aStream->samplesPerFrame : 0 ), Chris@39: format, Chris@39: NULL, Chris@39: sampleRate, Chris@39: FRAMES_PER_BUFFER, Chris@39: minNumBuffers, Chris@39: paClipOff, /* we won't output out of range samples so don't bother clipping them */ Chris@39: blockingIOCallback, Chris@39: aStream ); Chris@39: if( err != paNoError ) goto error; Chris@39: Chris@39: err = Pa_StartStream( aStream->stream ); Chris@39: if( err != paNoError ) goto error; Chris@39: Chris@39: *rwblPtr = aStream; Chris@39: return paNoError; Chris@39: Chris@39: error: Chris@39: CloseAudioStream( aStream ); Chris@39: *rwblPtr = NULL; Chris@39: return err; Chris@39: } Chris@39: Chris@39: /************************************************************/ Chris@39: PaError CloseAudioStream( PABLIO_Stream *aStream ) Chris@39: { Chris@39: PaError err; Chris@39: int bytesEmpty; Chris@39: int byteSize = aStream->outFIFO.bufferSize; Chris@39: Chris@39: /* If we are writing data, make sure we play everything written. */ Chris@39: if( byteSize > 0 ) Chris@39: { Chris@39: bytesEmpty = PaUtil_GetRingBufferWriteAvailable( &aStream->outFIFO ); Chris@39: while( bytesEmpty < byteSize ) Chris@39: { Chris@39: Pa_Sleep( 10 ); Chris@39: bytesEmpty = PaUtil_GetRingBufferWriteAvailable( &aStream->outFIFO ); Chris@39: } Chris@39: } Chris@39: Chris@39: err = Pa_StopStream( aStream->stream ); Chris@39: if( err != paNoError ) goto error; Chris@39: err = Pa_CloseStream( aStream->stream ); Chris@39: if( err != paNoError ) goto error; Chris@39: Pa_Terminate(); Chris@39: Chris@39: error: Chris@39: PABLIO_TermFIFO( &aStream->inFIFO ); Chris@39: PABLIO_TermFIFO( &aStream->outFIFO ); Chris@39: free( aStream ); Chris@39: return err; Chris@39: }