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