📄 pa_process.h
字号:
#ifndef PA_PROCESS_H#define PA_PROCESS_H/* * $Id: pa_process.h,v 1.2 2004/04/22 04:19:50 mbrubeck Exp $ * Portable Audio I/O Library callback buffer processing adapters * * Based on the Open Source API proposed by Ross Bencina * Copyright (c) 1999-2002 Phil Burk, Ross Bencina * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files * (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, * publish, distribute, sublicense, and/or sell copies of the Software, * and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * Any person wishing to distribute modifications to the Software is * requested to send the modifications to the original developer so that * they can be incorporated into the canonical version. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /** @file @brief Buffer Processor prototypes. A Buffer Processor performs buffer length adaption, coordinates sample format conversion, and interleaves/deinterleaves channels. <h3>Overview</h3> The "Buffer Processor" (PaUtilBufferProcessor) manages conversion of audio data from host buffers to user buffers and back again. Where required, the buffer processor takes care of converting between host and user sample formats, interleaving and deinterleaving multichannel buffers, and adapting between host and user buffers with different lengths. The buffer processor may be used with full and half duplex streams, for both callback streams and blocking read/write streams. One of the important capabilities provided by the buffer processor is the ability to adapt between user and host buffer sizes of different lengths with minimum latency. Although this task is relatively easy to perform when the host buffer size is an integer multiple of the user buffer size, the problem is more complicated when this is not the case - especially for full-duplex callback streams. Where necessary the adaption is implemented by internally buffering some input and/or output data. The buffer adation algorithm used by the buffer processor was originally implemented by Stephan Letz for the ASIO version of PortAudio, and is described in his Callback_adaption_.pdf which is included in the distribution. The buffer processor performs sample conversion using the functions provided by pa_converters.c. The following sections provide an overview of how to use the buffer processor. Interested readers are advised to consult the host API implementations for examples of buffer processor usage. <h4>Initialization, resetting and termination</h4> When a stream is opened, the buffer processor should be initialized using PaUtil_InitializeBufferProcessor. This function initializes internal state and allocates temporary buffers as neccesary according to the supplied configuration parameters. Some of the parameters correspond to those requested by the user in their call to Pa_OpenStream(), others reflect the requirements of the host API implementation - they indicate host buffer sizes, formats, and the type of buffering which the Host API uses. The buffer processor should be initialized for callback streams and blocking read/write streams. Call PaUtil_ResetBufferProcessor to clear any sample data which is present in the buffer processor before starting to use it (for example when Pa_StartStream is called). When the buffer processor is no longer used call PaUtil_TerminateBufferProcessor. <h4>Using the buffer processor for a callback stream</h4> The buffer processor's role in a callback stream is to take host input buffers process them with the stream callback, and fill host output buffers. For a full duplex stream, the buffer processor handles input and output simultaneously due to the requirements of the minimum-latency buffer adation algorithm. When a host buffer becomes available, the implementation should call the buffer processor to process the buffer. The buffer processor calls the stream callback to consume and/or produce audio data as necessary. The buffer processor will convert sample formats, interleave/deinterleave channels, and slice or chunk the data to the appropriate buffer lengths according to the requirements of the stream callback and the host API. To process a host buffer (or a pair of host buffers for a full-duplex stream) use the following calling sequence: -# Call PaUtil_BeginBufferProcessing -# For a stream which takes input: - Call PaUtil_SetInputFrameCount with the number of frames in the host input buffer. - Call one of the following functions one or more times to tell the buffer processor about the host input buffer(s): PaUtil_SetInputChannel, PaUtil_SetInterleavedInputChannels, PaUtil_SetNonInterleavedInputChannel. Which function you call will depend on whether the host buffer(s) are interleaved or not. - If the available host data is split accross two buffers (for example a data range at the end of a circular buffer and another range at the beginning of the circular buffer), also call PaUtil_Set2ndInputFrameCount, PaUtil_Set2ndInputChannel, PaUtil_Set2ndInterleavedInputChannels, PaUtil_Set2ndNonInterleavedInputChannel as necessary to tell the buffer processor about the second buffer. -# For a stream which generates output: - Call PaUtil_SetOutputFrameCount with the number of frames in the host output buffer. - Call one of the following functions one or more times to tell the buffer processor about the host output buffer(s): PaUtil_SetOutputChannel, PaUtil_SetInterleavedOutputChannels, PaUtil_SetNonInterleavedOutputChannel. Which function you call will depend on whether the host buffer(s) are interleaved or not. - If the available host output buffer space is split accross two buffers (for example a data range at the end of a circular buffer and another range at the beginning of the circular buffer), call PaUtil_Set2ndOutputFrameCount, PaUtil_Set2ndOutputChannel, PaUtil_Set2ndInterleavedOutputChannels, PaUtil_Set2ndNonInterleavedOutputChannel as necessary to tell the buffer processor about the second buffer. -# Call PaUtil_EndBufferProcessing, this function performs the actual data conversion and processing. <h4>Using the buffer processor for a blocking read/write stream</h4> Blocking read/write streams use the buffer processor to convert and copy user output data to a host buffer, and to convert and copy host input data to the user's buffer. The buffer processor does not perform any buffer adaption. When using the buffer processor in a blocking read/write stream the input and output conversion are performed separately by the PaUtil_CopyInput and PaUtil_CopyOutput functions. To copy data from a host input buffer to the buffer(s) which the user supplies to Pa_ReadStream, use the following calling sequence. - Repeat the following three steps until the user buffer(s) have been filled with samples from the host input buffers: -# Call PaUtil_SetInputFrameCount with the number of frames in the host input buffer. -# Call one of the following functions one or more times to tell the buffer processor about the host input buffer(s): PaUtil_SetInputChannel, PaUtil_SetInterleavedInputChannels, PaUtil_SetNonInterleavedInputChannel. Which function you call will depend on whether the host buffer(s) are interleaved or not. -# Call PaUtil_CopyInput with the user buffer pointer (or a copy of the array of buffer pointers for a non-interleaved stream) passed to Pa_ReadStream, along with the number of frames in the user buffer(s). Be careful to pass a <i>copy</i> of the user buffer pointers to PaUtil_CopyInput because PaUtil_CopyInput advances the pointers to the start of the next region to copy. - PaUtil_CopyInput will not copy more data than is available in the host buffer(s), so the above steps need to be repeated until the user buffer(s) are full. To copy data to the host output buffer from the user buffers(s) supplied to Pa_WriteStream use the following calling sequence. - Repeat the following three steps until all frames from the user buffer(s) have been copied to the host API: -# Call PaUtil_SetOutputFrameCount with the number of frames in the host output buffer. -# Call one of the following functions one or more times to tell the buffer processor about the host output buffer(s): PaUtil_SetOutputChannel, PaUtil_SetInterleavedOutputChannels, PaUtil_SetNonInterleavedOutputChannel. Which function you call will depend on whether the host buffer(s) are interleaved or not. -# Call PaUtil_CopyOutput with the user buffer pointer (or a copy of the array of buffer pointers for a non-interleaved stream) passed to Pa_WriteStream, along with the number of frames in the user buffer(s). Be careful to pass a <i>copy</i> of the user buffer pointers to PaUtil_CopyOutput because PaUtil_CopyOutput advances the pointers to the start of the next region to copy. - PaUtil_CopyOutput will not copy more data than fits in the host buffer(s), so the above steps need to be repeated until all user data is copied.*/#include "portaudio.h"#include "pa_converters.h"#include "pa_dither.h"#ifdef __cplusplusextern "C"{#endif /* __cplusplus *//** @brief Mode flag passed to PaUtil_InitializeBufferProcessor indicating the type of buffering that the host API uses. The mode used depends on whether the host API or the implementation manages the buffers, and how these buffers are used (scatter gather, circular buffer).*/typedef enum {/** The host buffer size is a fixed known size. */ paUtilFixedHostBufferSize,/** The host buffer size may vary, but has a known maximum size. */ paUtilBoundedHostBufferSize,/** Nothing is known about the host buffer size. */ paUtilUnknownHostBufferSize,/** The host buffer size varies, and the client does not require the buffer processor to consume all of the input and fill all of the output buffer. This is useful when the implementation has access to the host API's circular buffer and only needs to consume/fill some of it, not necessarily all of it, with each call to the buffer processor. This is the only mode where PaUtil_EndBufferProcessing() may not consume the whole buffer.*/ paUtilVariableHostBufferSizePartialUsageAllowed, }PaUtilHostBufferSizeMode;/** @brief An auxilliary data structure used internally by the buffer processor to represent host input and output buffers. */typedef struct PaUtilChannelDescriptor{ void *data; unsigned int stride; /**< stride in samples, not bytes */}PaUtilChannelDescriptor;/** @brief The main buffer processor data structure. Allocate one of these, initialize it with PaUtil_InitializeBufferProcessor and terminate it with PaUtil_TerminateBufferProcessor.*/typedef struct { unsigned long framesPerUserBuffer; unsigned long framesPerHostBuffer; PaUtilHostBufferSizeMode hostBufferSizeMode; int useNonAdaptingProcess; unsigned long framesPerTempBuffer; unsigned int inputChannelCount; unsigned int bytesPerHostInputSample; unsigned int bytesPerUserInputSample; int userInputIsInterleaved; PaUtilConverter *inputConverter; PaUtilZeroer *inputZeroer; unsigned int outputChannelCount; unsigned int bytesPerHostOutputSample; unsigned int bytesPerUserOutputSample; int userOutputIsInterleaved; PaUtilConverter *outputConverter; PaUtilZeroer *outputZeroer; unsigned long initialFramesInTempInputBuffer; unsigned long initialFramesInTempOutputBuffer; void *tempInputBuffer; /**< used for slips, block adaption, and conversion. */ void **tempInputBufferPtrs; /**< storage for non-interleaved buffer pointers, NULL for interleaved user input */ unsigned long framesInTempInputBuffer; /**< frames remaining in input buffer from previous adaption iteration */ void *tempOutputBuffer; /**< used for slips, block adaption, and conversion. */ void **tempOutputBufferPtrs; /**< storage for non-interleaved buffer pointers, NULL for interleaved user output */ unsigned long framesInTempOutputBuffer; /**< frames remaining in input buffer from previous adaption iteration */ PaStreamCallbackTimeInfo *timeInfo; PaStreamCallbackFlags callbackStatusFlags; unsigned long hostInputFrameCount[2]; PaUtilChannelDescriptor *hostInputChannels[2]; unsigned long hostOutputFrameCount[2]; PaUtilChannelDescriptor *hostOutputChannels[2]; PaUtilTriangularDitherGenerator ditherGenerator; double samplePeriod; PaStreamCallback *streamCallback; void *userData;} PaUtilBufferProcessor;/** @name Initialization, termination, resetting and info *//*@{*//** Initialize a buffer processor's representation stored in a PaUtilBufferProcessor structure. Be sure to call PaUtil_TerminateBufferProcessor after finishing with a buffer processor. @param bufferProcessor The buffer processor structure to initialize. @param inputChannelCount The number of input channels as passed to Pa_OpenStream or 0 for an output-only stream. @param userInputSampleFormat Format of user input samples, as passed to Pa_OpenStream. This parameter is ignored for ouput-only streams. @param hostInputSampleFormat Format of host input samples. This parameter is ignored for output-only streams. See note about host buffer interleave below. @param outputChannelCount The number of output channels as passed to Pa_OpenStream or 0 for an input-only stream. @param userOutputSampleFormat Format of user output samples, as passed to Pa_OpenStream. This parameter is ignored for input-only streams. @param hostOutputSampleFormat Format of host output samples. This parameter is ignored for input-only streams. See note about host buffer interleave below. @param sampleRate Sample rate of the stream. The more accurate this is the better - it is used for updating time stamps when adapting buffers. @param streamFlags Stream flags as passed to Pa_OpenStream, this parameter is used for selecting special sample conversion options such as clipping and dithering. @param framesPerUserBuffer Number of frames per user buffer, as requested by the framesPerBuffer parameter to Pa_OpenStream. This parameter may be zero to indicate that the user will accept any (and varying) buffer sizes. @param framesPerHostBuffer Specifies the number of frames per host buffer for the fixed buffer size mode, and the maximum number of frames per host buffer for the bounded host buffer size mode. It is ignored for the other modes. @param hostBufferSizeMode A mode flag indicating the size variability of host buffers that will be passed to the buffer processor. See PaUtilHostBufferSizeMode for further details. @param streamCallback The user stream callback passed to Pa_OpenStream. @param userData The user data field passed to Pa_OpenStream. @note The interleave flag is ignored for host buffer formats. Host interleave is determined by the use of different SetInput and SetOutput functions. @return An error code indicating whether the initialization was successful. If the error code is not PaNoError, the buffer processor was not initialized and should not be used. @see Pa_OpenStream, PaUtilHostBufferSizeMode, PaUtil_TerminateBufferProcessor*/PaError PaUtil_InitializeBufferProcessor( PaUtilBufferProcessor* bufferProcessor, int inputChannelCount, PaSampleFormat userInputSampleFormat, PaSampleFormat hostInputSampleFormat, int outputChannelCount, PaSampleFormat userOutputSampleFormat, PaSampleFormat hostOutputSampleFormat, double sampleRate, PaStreamFlags streamFlags, unsigned long framesPerUserBuffer, /* 0 indicates don't care */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -