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