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