annotate src/portaudio/pablio/pablio.c @ 10:37bf6b4a2645

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