| 1 | #ifndef PA_PROCESS_H |
| 2 | #define PA_PROCESS_H |
| 3 | /* |
| 4 | * $Id: pa_process.h,v 1.1 2006/06/10 21:30:55 dmazzoni Exp $ |
| 5 | * Portable Audio I/O Library callback buffer processing adapters |
| 6 | * |
| 7 | * Based on the Open Source API proposed by Ross Bencina |
| 8 | * Copyright (c) 1999-2002 Phil Burk, Ross Bencina |
| 9 | * |
| 10 | * Permission is hereby granted, free of charge, to any person obtaining |
| 11 | * a copy of this software and associated documentation files |
| 12 | * (the "Software"), to deal in the Software without restriction, |
| 13 | * including without limitation the rights to use, copy, modify, merge, |
| 14 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 15 | * and to permit persons to whom the Software is furnished to do so, |
| 16 | * subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be |
| 19 | * included in all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * Any person wishing to distribute modifications to the Software is |
| 22 | * requested to send the modifications to the original developer so that |
| 23 | * they can be incorporated into the canonical version. |
| 24 | * |
| 25 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 26 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 27 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 28 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 29 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 30 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 31 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 32 | */ |
| 33 | |
| 34 | /** @file |
| 35 | @brief Buffer Processor prototypes. A Buffer Processor performs buffer length |
| 36 | adaption, coordinates sample format conversion, and interleaves/deinterleaves |
| 37 | channels. |
| 38 | |
| 39 | <h3>Overview</h3> |
| 40 | |
| 41 | The "Buffer Processor" (PaUtilBufferProcessor) manages conversion of audio |
| 42 | data from host buffers to user buffers and back again. Where required, the |
| 43 | buffer processor takes care of converting between host and user sample formats, |
| 44 | interleaving and deinterleaving multichannel buffers, and adapting between host |
| 45 | and user buffers with different lengths. The buffer processor may be used with |
| 46 | full and half duplex streams, for both callback streams and blocking read/write |
| 47 | streams. |
| 48 | |
| 49 | One of the important capabilities provided by the buffer processor is |
| 50 | the ability to adapt between user and host buffer sizes of different lengths |
| 51 | with minimum latency. Although this task is relatively easy to perform when |
| 52 | the host buffer size is an integer multiple of the user buffer size, the |
| 53 | problem is more complicated when this is not the case - especially for |
| 54 | full-duplex callback streams. Where necessary the adaption is implemented by |
| 55 | internally buffering some input and/or output data. The buffer adation |
| 56 | algorithm used by the buffer processor was originally implemented by |
| 57 | Stephan Letz for the ASIO version of PortAudio, and is described in his |
| 58 | Callback_adaption_.pdf which is included in the distribution. |
| 59 | |
| 60 | The buffer processor performs sample conversion using the functions provided |
| 61 | by pa_converters.c. |
| 62 | |
| 63 | The following sections provide an overview of how to use the buffer processor. |
| 64 | Interested readers are advised to consult the host API implementations for |
| 65 | examples of buffer processor usage. |
| 66 | |
| 67 | |
| 68 | <h4>Initialization, resetting and termination</h4> |
| 69 | |
| 70 | When a stream is opened, the buffer processor should be initialized using |
| 71 | PaUtil_InitializeBufferProcessor. This function initializes internal state |
| 72 | and allocates temporary buffers as neccesary according to the supplied |
| 73 | configuration parameters. Some of the parameters correspond to those requested |
| 74 | by the user in their call to Pa_OpenStream(), others reflect the requirements |
| 75 | of the host API implementation - they indicate host buffer sizes, formats, |
| 76 | and the type of buffering which the Host API uses. The buffer processor should |
| 77 | be initialized for callback streams and blocking read/write streams. |
| 78 | |
| 79 | Call PaUtil_ResetBufferProcessor to clear any sample data which is present |
| 80 | in the buffer processor before starting to use it (for example when |
| 81 | Pa_StartStream is called). |
| 82 | |
| 83 | When the buffer processor is no longer used call |
| 84 | PaUtil_TerminateBufferProcessor. |
| 85 | |
| 86 | |
| 87 | <h4>Using the buffer processor for a callback stream</h4> |
| 88 | |
| 89 | The buffer processor's role in a callback stream is to take host input buffers |
| 90 | process them with the stream callback, and fill host output buffers. For a |
| 91 | full duplex stream, the buffer processor handles input and output simultaneously |
| 92 | due to the requirements of the minimum-latency buffer adation algorithm. |
| 93 | |
| 94 | When a host buffer becomes available, the implementation should call |
| 95 | the buffer processor to process the buffer. The buffer processor calls the |
| 96 | stream callback to consume and/or produce audio data as necessary. The buffer |
| 97 | processor will convert sample formats, interleave/deinterleave channels, |
| 98 | and slice or chunk the data to the appropriate buffer lengths according to |
| 99 | the requirements of the stream callback and the host API. |
| 100 | |
| 101 | To process a host buffer (or a pair of host buffers for a full-duplex stream) |
| 102 | use the following calling sequence: |
| 103 | |
| 104 | -# Call PaUtil_BeginBufferProcessing |
| 105 | -# For a stream which takes input: |
| 106 | - Call PaUtil_SetInputFrameCount with the number of frames in the host input |
| 107 | buffer. |
| 108 | - Call one of the following functions one or more times to tell the |
| 109 | buffer processor about the host input buffer(s): PaUtil_SetInputChannel, |
| 110 | PaUtil_SetInterleavedInputChannels, PaUtil_SetNonInterleavedInputChannel. |
| 111 | Which function you call will depend on whether the host buffer(s) are |
| 112 | interleaved or not. |
| 113 | - If the available host data is split accross two buffers (for example a |
| 114 | data range at the end of a circular buffer and another range at the |
| 115 | beginning of the circular buffer), also call |
| 116 | PaUtil_Set2ndInputFrameCount, PaUtil_Set2ndInputChannel, |
| 117 | PaUtil_Set2ndInterleavedInputChannels, |
| 118 | PaUtil_Set2ndNonInterleavedInputChannel as necessary to tell the buffer |
| 119 | processor about the second buffer. |
| 120 | -# For a stream which generates output: |
| 121 | - Call PaUtil_SetOutputFrameCount with the number of frames in the host |
| 122 | output buffer. |
| 123 | - Call one of the following functions one or more times to tell the |
| 124 | buffer processor about the host output buffer(s): PaUtil_SetOutputChannel, |
| 125 | PaUtil_SetInterleavedOutputChannels, PaUtil_SetNonInterleavedOutputChannel. |
| 126 | Which function you call will depend on whether the host buffer(s) are |
| 127 | interleaved or not. |
| 128 | - If the available host output buffer space is split accross two buffers |
| 129 | (for example a data range at the end of a circular buffer and another |
| 130 | range at the beginning of the circular buffer), call |
| 131 | PaUtil_Set2ndOutputFrameCount, PaUtil_Set2ndOutputChannel, |
| 132 | PaUtil_Set2ndInterleavedOutputChannels, |
| 133 | PaUtil_Set2ndNonInterleavedOutputChannel as necessary to tell the buffer |
| 134 | processor about the second buffer. |
| 135 | -# Call PaUtil_EndBufferProcessing, this function performs the actual data |
| 136 | conversion and processing. |
| 137 | |
| 138 | |
| 139 | <h4>Using the buffer processor for a blocking read/write stream</h4> |
| 140 | |
| 141 | Blocking read/write streams use the buffer processor to convert and copy user |
| 142 | output data to a host buffer, and to convert and copy host input data to |
| 143 | the user's buffer. The buffer processor does not perform any buffer adaption. |
| 144 | When using the buffer processor in a blocking read/write stream the input and |
| 145 | output conversion are performed separately by the PaUtil_CopyInput and |
| 146 | PaUtil_CopyOutput functions. |
| 147 | |
| 148 | To copy data from a host input buffer to the buffer(s) which the user supplies |
| 149 | to Pa_ReadStream, use the following calling sequence. |
| 150 | |
| 151 | - Repeat the following three steps until the user buffer(s) have been filled |
| 152 | with samples from the host input buffers: |
| 153 | -# Call PaUtil_SetInputFrameCount with the number of frames in the host |
| 154 | input buffer. |
| 155 | -# Call one of the following functions one or more times to tell the |
| 156 | buffer processor about the host input buffer(s): PaUtil_SetInputChannel, |
| 157 | PaUtil_SetInterleavedInputChannels, PaUtil_SetNonInterleavedInputChannel. |
| 158 | Which function you call will depend on whether the host buffer(s) are |
| 159 | interleaved or not. |
| 160 | -# Call PaUtil_CopyInput with the user buffer pointer (or a copy of the |
| 161 | array of buffer pointers for a non-interleaved stream) passed to |
| 162 | Pa_ReadStream, along with the number of frames in the user buffer(s). |
| 163 | Be careful to pass a <i>copy</i> of the user buffer pointers to |
| 164 | PaUtil_CopyInput because PaUtil_CopyInput advances the pointers to |
| 165 | the start of the next region to copy. |
| 166 | - PaUtil_CopyInput will not copy more data than is available in the |
| 167 | host buffer(s), so the above steps need to be repeated until the user |
| 168 | buffer(s) are full. |
| 169 | |
| 170 | |
| 171 | To copy data to the host output buffer from the user buffers(s) supplied |
| 172 | to Pa_WriteStream use the following calling sequence. |
| 173 | |
| 174 | - Repeat the following three steps until all frames from the user buffer(s) |
| 175 | have been copied to the host API: |
| 176 | -# Call PaUtil_SetOutputFrameCount with the number of frames in the host |
| 177 | output buffer. |
| 178 | -# Call one of the following functions one or more times to tell the |
| 179 | buffer processor about the host output buffer(s): PaUtil_SetOutputChannel, |
| 180 | PaUtil_SetInterleavedOutputChannels, PaUtil_SetNonInterleavedOutputChannel. |
| 181 | Which function you call will depend on whether the host buffer(s) are |
| 182 | interleaved or not. |
| 183 | -# Call PaUtil_CopyOutput with the user buffer pointer (or a copy of the |
| 184 | array of buffer pointers for a non-interleaved stream) passed to |
| 185 | Pa_WriteStream, along with the number of frames in the user buffer(s). |
| 186 | Be careful to pass a <i>copy</i> of the user buffer pointers to |
| 187 | PaUtil_CopyOutput because PaUtil_CopyOutput advances the pointers to |
| 188 | the start of the next region to copy. |
| 189 | - PaUtil_CopyOutput will not copy more data than fits in the host buffer(s), |
| 190 | so the above steps need to be repeated until all user data is copied. |
| 191 | */ |
| 192 | |
| 193 | |
| 194 | #include "portaudio.h" |
| 195 | #include "pa_converters.h" |
| 196 | #include "pa_dither.h" |
| 197 | |
| 198 | #ifdef __cplusplus |
| 199 | extern "C" |
| 200 | { |
| 201 | #endif /* __cplusplus */ |
| 202 | |
| 203 | |
| 204 | /** @brief Mode flag passed to PaUtil_InitializeBufferProcessor indicating the type |
| 205 | of buffering that the host API uses. |
| 206 | |
| 207 | The mode used depends on whether the host API or the implementation manages |
| 208 | the buffers, and how these buffers are used (scatter gather, circular buffer). |
| 209 | */ |
| 210 | typedef enum { |
| 211 | /** The host buffer size is a fixed known size. */ |
| 212 | paUtilFixedHostBufferSize, |
| 213 | |
| 214 | /** The host buffer size may vary, but has a known maximum size. */ |
| 215 | paUtilBoundedHostBufferSize, |
| 216 | |
| 217 | /** Nothing is known about the host buffer size. */ |
| 218 | paUtilUnknownHostBufferSize, |
| 219 | |
| 220 | /** The host buffer size varies, and the client does not require the buffer |
| 221 | processor to consume all of the input and fill all of the output buffer. This |
| 222 | is useful when the implementation has access to the host API's circular buffer |
| 223 | and only needs to consume/fill some of it, not necessarily all of it, with each |
| 224 | call to the buffer processor. This is the only mode where |
| 225 | PaUtil_EndBufferProcessing() may not consume the whole buffer. |
| 226 | */ |
| 227 | paUtilVariableHostBufferSizePartialUsageAllowed |
| 228 | }PaUtilHostBufferSizeMode; |
| 229 | |
| 230 | |
| 231 | /** @brief An auxilliary data structure used internally by the buffer processor |
| 232 | to represent host input and output buffers. */ |
| 233 | typedef struct PaUtilChannelDescriptor{ |
| 234 | void *data; |
| 235 | unsigned int stride; /**< stride in samples, not bytes */ |
| 236 | }PaUtilChannelDescriptor; |
| 237 | |
| 238 | |
| 239 | /** @brief The main buffer processor data structure. |
| 240 | |
| 241 | Allocate one of these, initialize it with PaUtil_InitializeBufferProcessor |
| 242 | and terminate it with PaUtil_TerminateBufferProcessor. |
| 243 | */ |
| 244 | typedef struct { |
| 245 | unsigned long framesPerUserBuffer; |
| 246 | unsigned long framesPerHostBuffer; |
| 247 | |
| 248 | PaUtilHostBufferSizeMode hostBufferSizeMode; |
| 249 | int useNonAdaptingProcess; |
| 250 | unsigned long framesPerTempBuffer; |
| 251 | |
| 252 | unsigned int inputChannelCount; |
| 253 | unsigned int bytesPerHostInputSample; |
| 254 | unsigned int bytesPerUserInputSample; |
| 255 | int userInputIsInterleaved; |
| 256 | PaUtilConverter *inputConverter; |
| 257 | PaUtilZeroer *inputZeroer; |
| 258 | |
| 259 | unsigned int outputChannelCount; |
| 260 | unsigned int bytesPerHostOutputSample; |
| 261 | unsigned int bytesPerUserOutputSample; |
| 262 | int userOutputIsInterleaved; |
| 263 | PaUtilConverter *outputConverter; |
| 264 | PaUtilZeroer *outputZeroer; |
| 265 | |
| 266 | unsigned long initialFramesInTempInputBuffer; |
| 267 | unsigned long initialFramesInTempOutputBuffer; |
| 268 | |
| 269 | void *tempInputBuffer; /**< used for slips, block adaption, and conversion. */ |
| 270 | void **tempInputBufferPtrs; /**< storage for non-interleaved buffer pointers, NULL for interleaved user input */ |
| 271 | unsigned long framesInTempInputBuffer; /**< frames remaining in input buffer from previous adaption iteration */ |
| 272 | |
| 273 | void *tempOutputBuffer; /**< used for slips, block adaption, and conversion. */ |
| 274 | void **tempOutputBufferPtrs; /**< storage for non-interleaved buffer pointers, NULL for interleaved user output */ |
| 275 | unsigned long framesInTempOutputBuffer; /**< frames remaining in input buffer from previous adaption iteration */ |
| 276 | |
| 277 | PaStreamCallbackTimeInfo *timeInfo; |
| 278 | |
| 279 | PaStreamCallbackFlags callbackStatusFlags; |
| 280 | |
| 281 | unsigned long hostInputFrameCount[2]; |
| 282 | PaUtilChannelDescriptor *hostInputChannels[2]; /**< pointers to arrays of channel descriptors. |
| 283 | pointers are NULL for half-duplex output processing. |
| 284 | hostInputChannels[i].data is NULL when the caller |
| 285 | calls PaUtil_SetNoInput() |
| 286 | */ |
| 287 | unsigned long hostOutputFrameCount[2]; |
| 288 | PaUtilChannelDescriptor *hostOutputChannels[2]; /**< pointers to arrays of channel descriptors. |
| 289 | pointers are NULL for half-duplex input processing. |
| 290 | hostOutputChannels[i].data is NULL when the caller |
| 291 | calls PaUtil_SetNoOutput() |
| 292 | */ |
| 293 | |
| 294 | PaUtilTriangularDitherGenerator ditherGenerator; |
| 295 | |
| 296 | double samplePeriod; |
| 297 | |
| 298 | PaStreamCallback *streamCallback; |
| 299 | void *userData; |
| 300 | } PaUtilBufferProcessor; |
| 301 | |
| 302 | |
| 303 | /** @name Initialization, termination, resetting and info */ |
| 304 | /*@{*/ |
| 305 | |
| 306 | /** Initialize a buffer processor's representation stored in a |
| 307 | PaUtilBufferProcessor structure. Be sure to call |
| 308 | PaUtil_TerminateBufferProcessor after finishing with a buffer processor. |
| 309 | |
| 310 | @param bufferProcessor The buffer processor structure to initialize. |
| 311 | |
| 312 | @param inputChannelCount The number of input channels as passed to |
| 313 | Pa_OpenStream or 0 for an output-only stream. |
| 314 | |
| 315 | @param userInputSampleFormat Format of user input samples, as passed to |
| 316 | Pa_OpenStream. This parameter is ignored for ouput-only streams. |
| 317 | |
| 318 | @param hostInputSampleFormat Format of host input samples. This parameter is |
| 319 | ignored for output-only streams. See note about host buffer interleave below. |
| 320 | |
| 321 | @param outputChannelCount The number of output channels as passed to |
| 322 | Pa_OpenStream or 0 for an input-only stream. |
| 323 | |
| 324 | @param userOutputSampleFormat Format of user output samples, as passed to |
| 325 | Pa_OpenStream. This parameter is ignored for input-only streams. |
| 326 | |
| 327 | @param hostOutputSampleFormat Format of host output samples. This parameter is |
| 328 | ignored for input-only streams. See note about host buffer interleave below. |
| 329 | |
| 330 | @param sampleRate Sample rate of the stream. The more accurate this is the |
| 331 | better - it is used for updating time stamps when adapting buffers. |
| 332 | |
| 333 | @param streamFlags Stream flags as passed to Pa_OpenStream, this parameter is |
| 334 | used for selecting special sample conversion options such as clipping and |
| 335 | dithering. |
| 336 | |
| 337 | @param framesPerUserBuffer Number of frames per user buffer, as requested |
| 338 | by the framesPerBuffer parameter to Pa_OpenStream. This parameter may be |
| 339 | zero to indicate that the user will accept any (and varying) buffer sizes. |
| 340 | |
| 341 | @param framesPerHostBuffer Specifies the number of frames per host buffer |
| 342 | for the fixed buffer size mode, and the maximum number of frames |
| 343 | per host buffer for the bounded host buffer size mode. It is ignored for |
| 344 | the other modes. |
| 345 | |
| 346 | @param hostBufferSizeMode A mode flag indicating the size variability of |
| 347 | host buffers that will be passed to the buffer processor. See |
| 348 | PaUtilHostBufferSizeMode for further details. |
| 349 | |
| 350 | @param streamCallback The user stream callback passed to Pa_OpenStream. |
| 351 | |
| 352 | @param userData The user data field passed to Pa_OpenStream. |
| 353 | |
| 354 | @note The interleave flag is ignored for host buffer formats. Host |
| 355 | interleave is determined by the use of different SetInput and SetOutput |
| 356 | functions. |
| 357 | |
| 358 | @return An error code indicating whether the initialization was successful. |
| 359 | If the error code is not PaNoError, the buffer processor was not initialized |
| 360 | and should not be used. |
| 361 | |
| 362 | @see Pa_OpenStream, PaUtilHostBufferSizeMode, PaUtil_TerminateBufferProcessor |
| 363 | */ |
| 364 | PaError PaUtil_InitializeBufferProcessor( PaUtilBufferProcessor* bufferProcessor, |
| 365 | int inputChannelCount, PaSampleFormat userInputSampleFormat, |
| 366 | PaSampleFormat hostInputSampleFormat, |
| 367 | int outputChannelCount, PaSampleFormat userOutputSampleFormat, |
| 368 | PaSampleFormat hostOutputSampleFormat, |
| 369 | double sampleRate, |
| 370 | PaStreamFlags streamFlags, |
| 371 | unsigned long framesPerUserBuffer, /* 0 indicates don't care */ |
| 372 | unsigned long framesPerHostBuffer, |
| 373 | PaUtilHostBufferSizeMode hostBufferSizeMode, |
| 374 | PaStreamCallback *streamCallback, void *userData ); |
| 375 | |
| 376 | |
| 377 | /** Terminate a buffer processor's representation. Deallocates any temporary |
| 378 | buffers allocated by PaUtil_InitializeBufferProcessor. |
| 379 | |
| 380 | @param bufferProcessor The buffer processor structure to terminate. |
| 381 | |
| 382 | @see PaUtil_InitializeBufferProcessor. |
| 383 | */ |
| 384 | void PaUtil_TerminateBufferProcessor( PaUtilBufferProcessor* bufferProcessor ); |
| 385 | |
| 386 | |
| 387 | /** Clear any internally buffered data. If you call |
| 388 | PaUtil_InitializeBufferProcessor in your OpenStream routine, make sure you |
| 389 | call PaUtil_ResetBufferProcessor in your StartStream call. |
| 390 | |
| 391 | @param bufferProcessor The buffer processor to reset. |
| 392 | */ |
| 393 | void PaUtil_ResetBufferProcessor( PaUtilBufferProcessor* bufferProcessor ); |
| 394 | |
| 395 | |
| 396 | /** Retrieve the input latency of a buffer processor. |
| 397 | |
| 398 | @param bufferProcessor The buffer processor examine. |
| 399 | |
| 400 | @return The input latency introduced by the buffer processor, in frames. |
| 401 | |
| 402 | @see PaUtil_GetBufferProcessorOutputLatency |
| 403 | */ |
| 404 | unsigned long PaUtil_GetBufferProcessorInputLatency( PaUtilBufferProcessor* bufferProcessor ); |
| 405 | |
| 406 | /** Retrieve the output latency of a buffer processor. |
| 407 | |
| 408 | @param bufferProcessor The buffer processor examine. |
| 409 | |
| 410 | @return The output latency introduced by the buffer processor, in frames. |
| 411 | |
| 412 | @see PaUtil_GetBufferProcessorInputLatency |
| 413 | */ |
| 414 | unsigned long PaUtil_GetBufferProcessorOutputLatency( PaUtilBufferProcessor* bufferProcessor ); |
| 415 | |
| 416 | /*@}*/ |
| 417 | |
| 418 | |
| 419 | /** @name Host buffer pointer configuration |
| 420 | |
| 421 | Functions to set host input and output buffers, used by both callback streams |
| 422 | and blocking read/write streams. |
| 423 | */ |
| 424 | /*@{*/ |
| 425 | |
| 426 | |
| 427 | /** Set the number of frames in the input host buffer(s) specified by the |
| 428 | PaUtil_Set*InputChannel functions. |
| 429 | |
| 430 | @param bufferProcessor The buffer processor. |
| 431 | |
| 432 | @param frameCount The number of host input frames. A 0 frameCount indicates to |
| 433 | use the framesPerHostBuffer value passed to PaUtil_InitializeBufferProcessor. |
| 434 | |
| 435 | @see PaUtil_SetNoInput, PaUtil_SetInputChannel, |
| 436 | PaUtil_SetInterleavedInputChannels, PaUtil_SetNonInterleavedInputChannel |
| 437 | */ |
| 438 | void PaUtil_SetInputFrameCount( PaUtilBufferProcessor* bufferProcessor, |
| 439 | unsigned long frameCount ); |
| 440 | |
| 441 | |
| 442 | /** Indicate that no input is avalable. This function should be used when |
| 443 | priming the output of a full-duplex stream opened with the |
| 444 | paPrimeOutputBuffersUsingStreamCallback flag. Note that it is not necessary |
| 445 | to call this or any othe PaUtil_Set*Input* functions for ouput-only streams. |
| 446 | |
| 447 | @param bufferProcessor The buffer processor. |
| 448 | */ |
| 449 | void PaUtil_SetNoInput( PaUtilBufferProcessor* bufferProcessor ); |
| 450 | |
| 451 | |
| 452 | /** Provide the buffer processor with a pointer to a host input channel. |
| 453 | |
| 454 | @param bufferProcessor The buffer processor. |
| 455 | @param channel The channel number. |
| 456 | @param data The buffer. |
| 457 | @param stride The stride from one sample to the next, in samples. For |
| 458 | interleaved host buffers, the stride will usually be the same as the number of |
| 459 | channels in the buffer. |
| 460 | */ |
| 461 | void PaUtil_SetInputChannel( PaUtilBufferProcessor* bufferProcessor, |
| 462 | unsigned int channel, void *data, unsigned int stride ); |
| 463 | |
| 464 | |
| 465 | /** Provide the buffer processor with a pointer to an number of interleaved |
| 466 | host input channels. |
| 467 | |
| 468 | @param bufferProcessor The buffer processor. |
| 469 | @param firstChannel The first channel number. |
| 470 | @param data The buffer. |
| 471 | @param channelCount The number of interleaved channels in the buffer. If |
| 472 | channelCount is zero, the number of channels specified to |
| 473 | PaUtil_InitializeBufferProcessor will be used. |
| 474 | */ |
| 475 | void PaUtil_SetInterleavedInputChannels( PaUtilBufferProcessor* bufferProcessor, |
| 476 | unsigned int firstChannel, void *data, unsigned int channelCount ); |
| 477 | |
| 478 | |
| 479 | /** Provide the buffer processor with a pointer to one non-interleaved host |
| 480 | output channel. |
| 481 | |
| 482 | @param bufferProcessor The buffer processor. |
| 483 | @param channel The channel number. |
| 484 | @param data The buffer. |
| 485 | */ |
| 486 | void PaUtil_SetNonInterleavedInputChannel( PaUtilBufferProcessor* bufferProcessor, |
| 487 | unsigned int channel, void *data ); |
| 488 | |
| 489 | |
| 490 | /** Use for the second buffer half when the input buffer is split in two halves. |
| 491 | @see PaUtil_SetInputFrameCount |
| 492 | */ |
| 493 | void PaUtil_Set2ndInputFrameCount( PaUtilBufferProcessor* bufferProcessor, |
| 494 | unsigned long frameCount ); |
| 495 | |
| 496 | /** Use for the second buffer half when the input buffer is split in two halves. |
| 497 | @see PaUtil_SetInputChannel |
| 498 | */ |
| 499 | void PaUtil_Set2ndInputChannel( PaUtilBufferProcessor* bufferProcessor, |
| 500 | unsigned int channel, void *data, unsigned int stride ); |
| 501 | |
| 502 | /** Use for the second buffer half when the input buffer is split in two halves. |
| 503 | @see PaUtil_SetInterleavedInputChannels |
| 504 | */ |
| 505 | void PaUtil_Set2ndInterleavedInputChannels( PaUtilBufferProcessor* bufferProcessor, |
| 506 | unsigned int firstChannel, void *data, unsigned int channelCount ); |
| 507 | |
| 508 | /** Use for the second buffer half when the input buffer is split in two halves. |
| 509 | @see PaUtil_SetNonInterleavedInputChannel |
| 510 | */ |
| 511 | void PaUtil_Set2ndNonInterleavedInputChannel( PaUtilBufferProcessor* bufferProcessor, |
| 512 | unsigned int channel, void *data ); |
| 513 | |
| 514 | |
| 515 | /** Set the number of frames in the output host buffer(s) specified by the |
| 516 | PaUtil_Set*OutputChannel functions. |
| 517 | |
| 518 | @param bufferProcessor The buffer processor. |
| 519 | |
| 520 | @param frameCount The number of host output frames. A 0 frameCount indicates to |
| 521 | use the framesPerHostBuffer value passed to PaUtil_InitializeBufferProcessor. |
| 522 | |
| 523 | @see PaUtil_SetOutputChannel, PaUtil_SetInterleavedOutputChannels, |
| 524 | PaUtil_SetNonInterleavedOutputChannel |
| 525 | */ |
| 526 | void PaUtil_SetOutputFrameCount( PaUtilBufferProcessor* bufferProcessor, |
| 527 | unsigned long frameCount ); |
| 528 | |
| 529 | |
| 530 | /** Indicate that the output will be discarded. This function should be used |
| 531 | when implementing the paNeverDropInput mode for full duplex streams. |
| 532 | |
| 533 | @param bufferProcessor The buffer processor. |
| 534 | */ |
| 535 | void PaUtil_SetNoOutput( PaUtilBufferProcessor* bufferProcessor ); |
| 536 | |
| 537 | |
| 538 | /** Provide the buffer processor with a pointer to a host output channel. |
| 539 | |
| 540 | @param bufferProcessor The buffer processor. |
| 541 | @param channel The channel number. |
| 542 | @param data The buffer. |
| 543 | @param stride The stride from one sample to the next, in samples. For |
| 544 | interleaved host buffers, the stride will usually be the same as the number of |
| 545 | channels in the buffer. |
| 546 | */ |
| 547 | void PaUtil_SetOutputChannel( PaUtilBufferProcessor* bufferProcessor, |
| 548 | unsigned int channel, void *data, unsigned int stride ); |
| 549 | |
| 550 | |
| 551 | /** Provide the buffer processor with a pointer to a number of interleaved |
| 552 | host output channels. |
| 553 | |
| 554 | @param bufferProcessor The buffer processor. |
| 555 | @param firstChannel The first channel number. |
| 556 | @param data The buffer. |
| 557 | @param channelCount The number of interleaved channels in the buffer. If |
| 558 | channelCount is zero, the number of channels specified to |
| 559 | PaUtil_InitializeBufferProcessor will be used. |
| 560 | */ |
| 561 | void PaUtil_SetInterleavedOutputChannels( PaUtilBufferProcessor* bufferProcessor, |
| 562 | unsigned int firstChannel, void *data, unsigned int channelCount ); |
| 563 | |
| 564 | |
| 565 | /** Provide the buffer processor with a pointer to one non-interleaved host |
| 566 | output channel. |
| 567 | |
| 568 | @param bufferProcessor The buffer processor. |
| 569 | @param channel The channel number. |
| 570 | @param data The buffer. |
| 571 | */ |
| 572 | void PaUtil_SetNonInterleavedOutputChannel( PaUtilBufferProcessor* bufferProcessor, |
| 573 | unsigned int channel, void *data ); |
| 574 | |
| 575 | |
| 576 | /** Use for the second buffer half when the output buffer is split in two halves. |
| 577 | @see PaUtil_SetOutputFrameCount |
| 578 | */ |
| 579 | void PaUtil_Set2ndOutputFrameCount( PaUtilBufferProcessor* bufferProcessor, |
| 580 | unsigned long frameCount ); |
| 581 | |
| 582 | /** Use for the second buffer half when the output buffer is split in two halves. |
| 583 | @see PaUtil_SetOutputChannel |
| 584 | */ |
| 585 | void PaUtil_Set2ndOutputChannel( PaUtilBufferProcessor* bufferProcessor, |
| 586 | unsigned int channel, void *data, unsigned int stride ); |
| 587 | |
| 588 | /** Use for the second buffer half when the output buffer is split in two halves. |
| 589 | @see PaUtil_SetInterleavedOutputChannels |
| 590 | */ |
| 591 | void PaUtil_Set2ndInterleavedOutputChannels( PaUtilBufferProcessor* bufferProcessor, |
| 592 | unsigned int firstChannel, void *data, unsigned int channelCount ); |
| 593 | |
| 594 | /** Use for the second buffer half when the output buffer is split in two halves. |
| 595 | @see PaUtil_SetNonInterleavedOutputChannel |
| 596 | */ |
| 597 | void PaUtil_Set2ndNonInterleavedOutputChannel( PaUtilBufferProcessor* bufferProcessor, |
| 598 | unsigned int channel, void *data ); |
| 599 | |
| 600 | /*@}*/ |
| 601 | |
| 602 | |
| 603 | /** @name Buffer processing functions for callback streams |
| 604 | */ |
| 605 | /*@{*/ |
| 606 | |
| 607 | /** Commence processing a host buffer (or a pair of host buffers in the |
| 608 | full-duplex case) for a callback stream. |
| 609 | |
| 610 | @param bufferProcessor The buffer processor. |
| 611 | |
| 612 | @param timeInfo Timing information for the first sample of the host |
| 613 | buffer(s). This information may be adjusted when buffer adaption is being |
| 614 | performed. |
| 615 | |
| 616 | @param callbackStatusFlags Flags indicating whether underruns and overruns |
| 617 | have occurred since the last time the buffer processor was called. |
| 618 | */ |
| 619 | void PaUtil_BeginBufferProcessing( PaUtilBufferProcessor* bufferProcessor, |
| 620 | PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags callbackStatusFlags ); |
| 621 | |
| 622 | |
| 623 | /** Finish processing a host buffer (or a pair of host buffers in the |
| 624 | full-duplex case) for a callback stream. |
| 625 | |
| 626 | @param bufferProcessor The buffer processor. |
| 627 | |
| 628 | @param callbackResult On input, indicates a previous callback result, and on |
| 629 | exit, the result of the user stream callback, if it is called. |
| 630 | On entry callbackResult should contain one of { paContinue, paComplete, or |
| 631 | paAbort}. If paComplete is passed, the stream callback will not be called |
| 632 | but any audio that was generated by previous stream callbacks will be copied |
| 633 | to the output buffer(s). You can check whether the buffer processor's internal |
| 634 | buffer is empty by calling PaUtil_IsBufferProcessorOutputEmpty. |
| 635 | |
| 636 | If the stream callback is called its result is stored in *callbackResult. If |
| 637 | the stream callback returns paComplete or paAbort, all output buffers will be |
| 638 | full of valid data - some of which may be zeros to acount for data that |
| 639 | wasn't generated by the terminating callback. |
| 640 | |
| 641 | @return The number of frames processed. This usually corresponds to the |
| 642 | number of frames specified by the PaUtil_Set*FrameCount functions, exept in |
| 643 | the paUtilVariableHostBufferSizePartialUsageAllowed buffer size mode when a |
| 644 | smaller value may be returned. |
| 645 | */ |
| 646 | unsigned long PaUtil_EndBufferProcessing( PaUtilBufferProcessor* bufferProcessor, |
| 647 | int *callbackResult ); |
| 648 | |
| 649 | |
| 650 | /** Determine whether any callback generated output remains in the bufffer |
| 651 | processor's internal buffers. This method may be used to determine when to |
| 652 | continue calling PaUtil_EndBufferProcessing() after the callback has returned |
| 653 | a callbackResult of paComplete. |
| 654 | |
| 655 | @param bufferProcessor The buffer processor. |
| 656 | |
| 657 | @return Returns non-zero when callback generated output remains in the internal |
| 658 | buffer and zero (0) when there internal buffer contains no callback generated |
| 659 | data. |
| 660 | */ |
| 661 | int PaUtil_IsBufferProcessorOutputEmpty( PaUtilBufferProcessor* bufferProcessor ); |
| 662 | |
| 663 | /*@}*/ |
| 664 | |
| 665 | |
| 666 | /** @name Buffer processing functions for blocking read/write streams |
| 667 | */ |
| 668 | /*@{*/ |
| 669 | |
| 670 | /** Copy samples from host input channels set up by the PaUtil_Set*InputChannels |
| 671 | functions to a user supplied buffer. This function is intended for use with |
| 672 | blocking read/write streams. Copies the minimum of the number of |
| 673 | user frames (specified by the frameCount parameter) and the number of available |
| 674 | host frames (specified in a previous call to SetInputFrameCount()). |
| 675 | |
| 676 | @param bufferProcessor The buffer processor. |
| 677 | |
| 678 | @param buffer A pointer to the user buffer pointer, or a pointer to a pointer |
| 679 | to an array of user buffer pointers for a non-interleaved stream. It is |
| 680 | important that this parameter points to a copy of the user buffer pointers, |
| 681 | not to the actual user buffer pointers, because this function updates the |
| 682 | pointers before returning. |
| 683 | |
| 684 | @param frameCount The number of frames of data in the buffer(s) pointed to by |
| 685 | the buffer parameter. |
| 686 | |
| 687 | @return The number of frames copied. The buffer pointer(s) pointed to by the |
| 688 | buffer parameter are advanced to point to the frame(s) following the last one |
| 689 | filled. |
| 690 | */ |
| 691 | unsigned long PaUtil_CopyInput( PaUtilBufferProcessor* bufferProcessor, |
| 692 | void **buffer, unsigned long frameCount ); |
| 693 | |
| 694 | |
| 695 | /* Copy samples from a user supplied buffer to host output channels set up by |
| 696 | the PaUtil_Set*OutputChannels functions. This function is intended for use with |
| 697 | blocking read/write streams. Copies the minimum of the number of |
| 698 | user frames (specified by the frameCount parameter) and the number of |
| 699 | host frames (specified in a previous call to SetOutputFrameCount()). |
| 700 | |
| 701 | @param bufferProcessor The buffer processor. |
| 702 | |
| 703 | @param buffer A pointer to the user buffer pointer, or a pointer to a pointer |
| 704 | to an array of user buffer pointers for a non-interleaved stream. It is |
| 705 | important that this parameter points to a copy of the user buffer pointers, |
| 706 | not to the actual user buffer pointers, because this function updates the |
| 707 | pointers before returning. |
| 708 | |
| 709 | @param frameCount The number of frames of data in the buffer(s) pointed to by |
| 710 | the buffer parameter. |
| 711 | |
| 712 | @return The number of frames copied. The buffer pointer(s) pointed to by the |
| 713 | buffer parameter are advanced to point to the frame(s) following the last one |
| 714 | copied. |
| 715 | */ |
| 716 | unsigned long PaUtil_CopyOutput( PaUtilBufferProcessor* bufferProcessor, |
| 717 | const void ** buffer, unsigned long frameCount ); |
| 718 | |
| 719 | |
| 720 | /* Zero samples in host output channels set up by the PaUtil_Set*OutputChannels |
| 721 | functions. This function is useful for flushing streams. |
| 722 | Zeros the minimum of frameCount and the number of host frames specified in a |
| 723 | previous call to SetOutputFrameCount(). |
| 724 | |
| 725 | @param bufferProcessor The buffer processor. |
| 726 | |
| 727 | @param frameCount The maximum number of frames to zero. |
| 728 | |
| 729 | @return The number of frames zeroed. |
| 730 | */ |
| 731 | unsigned long PaUtil_ZeroOutput( PaUtilBufferProcessor* bufferProcessor, |
| 732 | unsigned long frameCount ); |
| 733 | |
| 734 | |
| 735 | /*@}*/ |
| 736 | |
| 737 | |
| 738 | #ifdef __cplusplus |
| 739 | } |
| 740 | #endif /* __cplusplus */ |
| 741 | #endif /* PA_PROCESS_H */ |