annotate src/portaudio_20161030_catalina_patch/pablio/pablio.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents d43aab368df9
children
rev   line source
cannam@162 1 /*
cannam@162 2 * $Id$
cannam@162 3 * pablio.c
cannam@162 4 * Portable Audio Blocking Input/Output utility.
cannam@162 5 *
cannam@162 6 * Author: Phil Burk, http://www.softsynth.com
cannam@162 7 *
cannam@162 8 * This program uses the PortAudio Portable Audio Library.
cannam@162 9 * For more information see: http://www.portaudio.com
cannam@162 10 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
cannam@162 11 *
cannam@162 12 * Permission is hereby granted, free of charge, to any person obtaining
cannam@162 13 * a copy of this software and associated documentation files
cannam@162 14 * (the "Software"), to deal in the Software without restriction,
cannam@162 15 * including without limitation the rights to use, copy, modify, merge,
cannam@162 16 * publish, distribute, sublicense, and/or sell copies of the Software,
cannam@162 17 * and to permit persons to whom the Software is furnished to do so,
cannam@162 18 * subject to the following conditions:
cannam@162 19 *
cannam@162 20 * The above copyright notice and this permission notice shall be
cannam@162 21 * included in all copies or substantial portions of the Software.
cannam@162 22 *
cannam@162 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@162 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@162 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
cannam@162 26 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
cannam@162 27 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@162 28 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@162 29 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@162 30 */
cannam@162 31
cannam@162 32 /*
cannam@162 33 * The text above constitutes the entire PortAudio license; however,
cannam@162 34 * the PortAudio community also makes the following non-binding requests:
cannam@162 35 *
cannam@162 36 * Any person wishing to distribute modifications to the Software is
cannam@162 37 * requested to send the modifications to the original developer so that
cannam@162 38 * they can be incorporated into the canonical version. It is also
cannam@162 39 * requested that these non-binding requests be included along with the
cannam@162 40 * license above.
cannam@162 41 */
cannam@162 42
cannam@162 43 #include <stdio.h>
cannam@162 44 #include <stdlib.h>
cannam@162 45 #include <math.h>
cannam@162 46 #include "portaudio.h"
cannam@162 47 #include "pa_ringbuffer.h"
cannam@162 48 #include "pablio.h"
cannam@162 49 #include <string.h>
cannam@162 50
cannam@162 51 /************************************************************************/
cannam@162 52 /******** Constants *****************************************************/
cannam@162 53 /************************************************************************/
cannam@162 54
cannam@162 55 #define FRAMES_PER_BUFFER (256)
cannam@162 56
cannam@162 57 /************************************************************************/
cannam@162 58 /******** Prototypes ****************************************************/
cannam@162 59 /************************************************************************/
cannam@162 60
cannam@162 61 static int blockingIOCallback( void *inputBuffer, void *outputBuffer,
cannam@162 62 unsigned long framesPerBuffer,
cannam@162 63 PaTimestamp outTime, void *userData );
cannam@162 64 static PaError PABLIO_InitFIFO( RingBuffer *rbuf, long numFrames, long bytesPerFrame );
cannam@162 65 static PaError PABLIO_TermFIFO( RingBuffer *rbuf );
cannam@162 66
cannam@162 67 /************************************************************************/
cannam@162 68 /******** Functions *****************************************************/
cannam@162 69 /************************************************************************/
cannam@162 70
cannam@162 71 /* Called from PortAudio.
cannam@162 72 * Read and write data only if there is room in FIFOs.
cannam@162 73 */
cannam@162 74 static int blockingIOCallback( void *inputBuffer, void *outputBuffer,
cannam@162 75 unsigned long framesPerBuffer,
cannam@162 76 PaTimestamp outTime, void *userData )
cannam@162 77 {
cannam@162 78 PABLIO_Stream *data = (PABLIO_Stream*)userData;
cannam@162 79 long numBytes = data->bytesPerFrame * framesPerBuffer;
cannam@162 80 (void) outTime;
cannam@162 81
cannam@162 82 /* This may get called with NULL inputBuffer during initial setup. */
cannam@162 83 if( inputBuffer != NULL )
cannam@162 84 {
cannam@162 85 PaUtil_WriteRingBuffer( &data->inFIFO, inputBuffer, numBytes );
cannam@162 86 }
cannam@162 87 if( outputBuffer != NULL )
cannam@162 88 {
cannam@162 89 int i;
cannam@162 90 int numRead = PaUtil_ReadRingBuffer( &data->outFIFO, outputBuffer, numBytes );
cannam@162 91 /* Zero out remainder of buffer if we run out of data. */
cannam@162 92 for( i=numRead; i<numBytes; i++ )
cannam@162 93 {
cannam@162 94 ((char *)outputBuffer)[i] = 0;
cannam@162 95 }
cannam@162 96 }
cannam@162 97
cannam@162 98 return 0;
cannam@162 99 }
cannam@162 100
cannam@162 101 /* Allocate buffer. */
cannam@162 102 static PaError PABLIO_InitFIFO( RingBuffer *rbuf, long numFrames, long bytesPerFrame )
cannam@162 103 {
cannam@162 104 long numBytes = numFrames * bytesPerFrame;
cannam@162 105 char *buffer = (char *) malloc( numBytes );
cannam@162 106 if( buffer == NULL ) return paInsufficientMemory;
cannam@162 107 memset( buffer, 0, numBytes );
cannam@162 108 return (PaError) PaUtil_InitializeRingBuffer( rbuf, numBytes, buffer );
cannam@162 109 }
cannam@162 110
cannam@162 111 /* Free buffer. */
cannam@162 112 static PaError PABLIO_TermFIFO( RingBuffer *rbuf )
cannam@162 113 {
cannam@162 114 if( rbuf->buffer ) free( rbuf->buffer );
cannam@162 115 rbuf->buffer = NULL;
cannam@162 116 return paNoError;
cannam@162 117 }
cannam@162 118
cannam@162 119 /************************************************************
cannam@162 120 * Write data to ring buffer.
cannam@162 121 * Will not return until all the data has been written.
cannam@162 122 */
cannam@162 123 long WriteAudioStream( PABLIO_Stream *aStream, void *data, long numFrames )
cannam@162 124 {
cannam@162 125 long bytesWritten;
cannam@162 126 char *p = (char *) data;
cannam@162 127 long numBytes = aStream->bytesPerFrame * numFrames;
cannam@162 128 while( numBytes > 0)
cannam@162 129 {
cannam@162 130 bytesWritten = PaUtil_WriteRingBuffer( &aStream->outFIFO, p, numBytes );
cannam@162 131 numBytes -= bytesWritten;
cannam@162 132 p += bytesWritten;
cannam@162 133 if( numBytes > 0) Pa_Sleep(10);
cannam@162 134 }
cannam@162 135 return numFrames;
cannam@162 136 }
cannam@162 137
cannam@162 138 /************************************************************
cannam@162 139 * Read data from ring buffer.
cannam@162 140 * Will not return until all the data has been read.
cannam@162 141 */
cannam@162 142 long ReadAudioStream( PABLIO_Stream *aStream, void *data, long numFrames )
cannam@162 143 {
cannam@162 144 long bytesRead;
cannam@162 145 char *p = (char *) data;
cannam@162 146 long numBytes = aStream->bytesPerFrame * numFrames;
cannam@162 147 while( numBytes > 0)
cannam@162 148 {
cannam@162 149 bytesRead = PaUtil_ReadRingBuffer( &aStream->inFIFO, p, numBytes );
cannam@162 150 numBytes -= bytesRead;
cannam@162 151 p += bytesRead;
cannam@162 152 if( numBytes > 0) Pa_Sleep(10);
cannam@162 153 }
cannam@162 154 return numFrames;
cannam@162 155 }
cannam@162 156
cannam@162 157 /************************************************************
cannam@162 158 * Return the number of frames that could be written to the stream without
cannam@162 159 * having to wait.
cannam@162 160 */
cannam@162 161 long GetAudioStreamWriteable( PABLIO_Stream *aStream )
cannam@162 162 {
cannam@162 163 int bytesEmpty = PaUtil_GetRingBufferWriteAvailable( &aStream->outFIFO );
cannam@162 164 return bytesEmpty / aStream->bytesPerFrame;
cannam@162 165 }
cannam@162 166
cannam@162 167 /************************************************************
cannam@162 168 * Return the number of frames that are available to be read from the
cannam@162 169 * stream without having to wait.
cannam@162 170 */
cannam@162 171 long GetAudioStreamReadable( PABLIO_Stream *aStream )
cannam@162 172 {
cannam@162 173 int bytesFull = PaUtil_GetRingBufferReadAvailable( &aStream->inFIFO );
cannam@162 174 return bytesFull / aStream->bytesPerFrame;
cannam@162 175 }
cannam@162 176
cannam@162 177 /************************************************************/
cannam@162 178 static unsigned long RoundUpToNextPowerOf2( unsigned long n )
cannam@162 179 {
cannam@162 180 long numBits = 0;
cannam@162 181 if( ((n-1) & n) == 0) return n; /* Already Power of two. */
cannam@162 182 while( n > 0 )
cannam@162 183 {
cannam@162 184 n= n>>1;
cannam@162 185 numBits++;
cannam@162 186 }
cannam@162 187 return (1<<numBits);
cannam@162 188 }
cannam@162 189
cannam@162 190 /************************************************************
cannam@162 191 * Opens a PortAudio stream with default characteristics.
cannam@162 192 * Allocates PABLIO_Stream structure.
cannam@162 193 *
cannam@162 194 * flags parameter can be an ORed combination of:
cannam@162 195 * PABLIO_READ, PABLIO_WRITE, or PABLIO_READ_WRITE,
cannam@162 196 * and either PABLIO_MONO or PABLIO_STEREO
cannam@162 197 */
cannam@162 198 PaError OpenAudioStream( PABLIO_Stream **rwblPtr, double sampleRate,
cannam@162 199 PaSampleFormat format, long flags )
cannam@162 200 {
cannam@162 201 long bytesPerSample;
cannam@162 202 long doRead = 0;
cannam@162 203 long doWrite = 0;
cannam@162 204 PaError err;
cannam@162 205 PABLIO_Stream *aStream;
cannam@162 206 long minNumBuffers;
cannam@162 207 long numFrames;
cannam@162 208
cannam@162 209 /* Allocate PABLIO_Stream structure for caller. */
cannam@162 210 aStream = (PABLIO_Stream *) malloc( sizeof(PABLIO_Stream) );
cannam@162 211 if( aStream == NULL ) return paInsufficientMemory;
cannam@162 212 memset( aStream, 0, sizeof(PABLIO_Stream) );
cannam@162 213
cannam@162 214 /* Determine size of a sample. */
cannam@162 215 bytesPerSample = Pa_GetSampleSize( format );
cannam@162 216 if( bytesPerSample < 0 )
cannam@162 217 {
cannam@162 218 err = (PaError) bytesPerSample;
cannam@162 219 goto error;
cannam@162 220 }
cannam@162 221 aStream->samplesPerFrame = ((flags&PABLIO_MONO) != 0) ? 1 : 2;
cannam@162 222 aStream->bytesPerFrame = bytesPerSample * aStream->samplesPerFrame;
cannam@162 223
cannam@162 224 /* Initialize PortAudio */
cannam@162 225 err = Pa_Initialize();
cannam@162 226 if( err != paNoError ) goto error;
cannam@162 227
cannam@162 228 /* Warning: numFrames must be larger than amount of data processed per interrupt
cannam@162 229 * inside PA to prevent glitches. Just to be safe, adjust size upwards.
cannam@162 230 */
cannam@162 231 minNumBuffers = 2 * Pa_GetMinNumBuffers( FRAMES_PER_BUFFER, sampleRate );
cannam@162 232 numFrames = minNumBuffers * FRAMES_PER_BUFFER;
cannam@162 233 numFrames = RoundUpToNextPowerOf2( numFrames );
cannam@162 234
cannam@162 235 /* Initialize Ring Buffers */
cannam@162 236 doRead = ((flags & PABLIO_READ) != 0);
cannam@162 237 doWrite = ((flags & PABLIO_WRITE) != 0);
cannam@162 238 if(doRead)
cannam@162 239 {
cannam@162 240 err = PABLIO_InitFIFO( &aStream->inFIFO, numFrames, aStream->bytesPerFrame );
cannam@162 241 if( err != paNoError ) goto error;
cannam@162 242 }
cannam@162 243 if(doWrite)
cannam@162 244 {
cannam@162 245 long numBytes;
cannam@162 246 err = PABLIO_InitFIFO( &aStream->outFIFO, numFrames, aStream->bytesPerFrame );
cannam@162 247 if( err != paNoError ) goto error;
cannam@162 248 /* Make Write FIFO appear full initially. */
cannam@162 249 numBytes = PaUtil_GetRingBufferWriteAvailable( &aStream->outFIFO );
cannam@162 250 PaUtil_AdvanceRingBufferWriteIndex( &aStream->outFIFO, numBytes );
cannam@162 251 }
cannam@162 252
cannam@162 253 /* Open a PortAudio stream that we will use to communicate with the underlying
cannam@162 254 * audio drivers. */
cannam@162 255 err = Pa_OpenStream(
cannam@162 256 &aStream->stream,
cannam@162 257 (doRead ? Pa_GetDefaultInputDeviceID() : paNoDevice),
cannam@162 258 (doRead ? aStream->samplesPerFrame : 0 ),
cannam@162 259 format,
cannam@162 260 NULL,
cannam@162 261 (doWrite ? Pa_GetDefaultOutputDeviceID() : paNoDevice),
cannam@162 262 (doWrite ? aStream->samplesPerFrame : 0 ),
cannam@162 263 format,
cannam@162 264 NULL,
cannam@162 265 sampleRate,
cannam@162 266 FRAMES_PER_BUFFER,
cannam@162 267 minNumBuffers,
cannam@162 268 paClipOff, /* we won't output out of range samples so don't bother clipping them */
cannam@162 269 blockingIOCallback,
cannam@162 270 aStream );
cannam@162 271 if( err != paNoError ) goto error;
cannam@162 272
cannam@162 273 err = Pa_StartStream( aStream->stream );
cannam@162 274 if( err != paNoError ) goto error;
cannam@162 275
cannam@162 276 *rwblPtr = aStream;
cannam@162 277 return paNoError;
cannam@162 278
cannam@162 279 error:
cannam@162 280 CloseAudioStream( aStream );
cannam@162 281 *rwblPtr = NULL;
cannam@162 282 return err;
cannam@162 283 }
cannam@162 284
cannam@162 285 /************************************************************/
cannam@162 286 PaError CloseAudioStream( PABLIO_Stream *aStream )
cannam@162 287 {
cannam@162 288 PaError err;
cannam@162 289 int bytesEmpty;
cannam@162 290 int byteSize = aStream->outFIFO.bufferSize;
cannam@162 291
cannam@162 292 /* If we are writing data, make sure we play everything written. */
cannam@162 293 if( byteSize > 0 )
cannam@162 294 {
cannam@162 295 bytesEmpty = PaUtil_GetRingBufferWriteAvailable( &aStream->outFIFO );
cannam@162 296 while( bytesEmpty < byteSize )
cannam@162 297 {
cannam@162 298 Pa_Sleep( 10 );
cannam@162 299 bytesEmpty = PaUtil_GetRingBufferWriteAvailable( &aStream->outFIFO );
cannam@162 300 }
cannam@162 301 }
cannam@162 302
cannam@162 303 err = Pa_StopStream( aStream->stream );
cannam@162 304 if( err != paNoError ) goto error;
cannam@162 305 err = Pa_CloseStream( aStream->stream );
cannam@162 306 if( err != paNoError ) goto error;
cannam@162 307 Pa_Terminate();
cannam@162 308
cannam@162 309 error:
cannam@162 310 PABLIO_TermFIFO( &aStream->inFIFO );
cannam@162 311 PABLIO_TermFIFO( &aStream->outFIFO );
cannam@162 312 free( aStream );
cannam@162 313 return err;
cannam@162 314 }