📄 buffer.c
字号:
//****************************************************************************//// BUFFER.C - Routines for handling a circular data buffer.//// Copyright (c) 1999,2000,2001 Cirrus Logic, Inc.////****************************************************************************#include "buffer.h"//****************************************************************************//// BufferInit initializes a circular buffer.////****************************************************************************voidBufferInit(BufferState *psBuffer, long (*pfnIoctl)(unsigned long ulIoctl, BufferState *psBuffer, unsigned long ulParam1, unsigned long ulParam2, unsigned long ulParam3), long lInstanceData){ // // Save the IOCTL handler and instance data. // psBuffer->pfnIoctl = pfnIoctl; psBuffer->lInstanceData = lInstanceData; // // Initialize the circular buffer. // psBuffer->psLeft = 0; psBuffer->psRight = 0; psBuffer->lLength = 0; // // Initialize the circular buffer read and write pointers. // psBuffer->lReadPtr = 0; psBuffer->lWritePtr = 0;}//****************************************************************************//// BufferSetBuffer sets the address and length of the data buffer for this// circular buffer.////****************************************************************************longBufferSetBuffer(BufferState *psBuffer, short *psLeft, short *psRight, long lLength){ long lRet; // // Make sure that the size of the buffer is a multiple of four samples. // if(lLength & 3) { return(0); } // // Send the set buffer request to the entry point for this buffer. // if(psBuffer->pfnIoctl) { lRet = (psBuffer->pfnIoctl)(IOCTL_BUFFER_SETBUFFER, psBuffer, (unsigned long)psLeft, (unsigned long)psRight, (unsigned long)lLength); } else { lRet = -1; } // // If the return value is -1, then we should handle the request. // if(lRet == -1) { // // Remember the address and size of the buffer. // psBuffer->psLeft = psLeft; psBuffer->psRight = psRight; psBuffer->lLength = lLength; // // Reset the read and write pointers. // psBuffer->lReadPtr = 0; psBuffer->lWritePtr = 0; // // Success. // lRet = 1; } // // Return to the caller. // return(lRet);}//****************************************************************************//// BufferDataAvailable determines the number of samples that are currently in// the circular buffer.////****************************************************************************longBufferDataAvailable(BufferState *psBuffer){ long lRet, lRead, lWrite; // // Send the data available request to the entry point for this buffer. // if(psBuffer->pfnIoctl) { lRet = (psBuffer->pfnIoctl)(IOCTL_BUFFER_DATAAVAILABLE, psBuffer, 0, 0, 0); } else { lRet = -1; } // // If the return value is -1, then we should handle the request. // if(lRet == -1) { // // Get the current read and write pointers. // lRead = psBuffer->lReadPtr; lWrite = psBuffer->lWritePtr; // // Determine the samples available. // if(lWrite >= lRead) { // // The read pointer is before the write pointer in the bufer. // lRet = lWrite - lRead; } else { // // The write pointer is before the read pointer in the buffer. // lRet = psBuffer->lLength - (lRead - lWrite); } } // // Return to the caller. // return(lRet);}//****************************************************************************//// BufferSpaceAvailable determines the number of samples that can be added to// the circular buffer.////****************************************************************************longBufferSpaceAvailable(BufferState *psBuffer){ long lRet, lRead, lWrite; // // Send the space available request to the entry point for this buffer. // if(psBuffer->pfnIoctl) { lRet = (psBuffer->pfnIoctl)(IOCTL_BUFFER_SPACEAVAILABLE, psBuffer, 0, 0, 0); } else { lRet = -1; } // // If the return value is -1, then we should handle the request. // if(lRet == -1) { // // Get the current read and write pointers. // lRead = psBuffer->lReadPtr; lWrite = psBuffer->lWritePtr; // // Determine the space available. // if(lWrite == lRead) { // // The entire buffer is available (minus four samples). // lRet = psBuffer->lLength - 4; } else if(lWrite > lRead) { // // The read pointer is before the write pointer in the buffer. // lRet = psBuffer->lLength - (lWrite - lRead) - 4; } else { // // The write pointer is before the read pointer in the buffer. // lRet = lRead - lWrite - 4; } } // // Return to the caller. // return(lRet);}//****************************************************************************//// BufferIsEmpty determines if the circular buffer is empty.////****************************************************************************longBufferIsEmpty(BufferState *psBuffer){ long lRet; // // Send the is empty request to the entry point for this buffer. // if(psBuffer->pfnIoctl) { lRet = (psBuffer->pfnIoctl)(IOCTL_BUFFER_ISEMPTY, psBuffer, 0, 0, 0); } else { lRet = -1; } // // If the return value is -1, then we should handle the request. // if(lRet == -1) { // // The circular buffer is empty if its read pointer and write pointer // are identical. // if(psBuffer->lReadPtr == psBuffer->lWritePtr) { // // The buffer is empty. // lRet = 1; } else { // // The buffer is not empty. // lRet = 0; } } // // Return to the caller. // return(lRet);}//****************************************************************************//// BufferGetWritePointer provides pointers into the circular buffer where data// can be written.////****************************************************************************longBufferGetWritePointer(BufferState *psBuffer, short **ppsLeft, short **ppsRight, long *plLength){ long lRet, lRead; // // Send the get write pointer request to the entry point for this buffer. // if(psBuffer->pfnIoctl) { lRet = (psBuffer->pfnIoctl)(IOCTL_BUFFER_GETWRITEPOINTER, psBuffer, (unsigned long)ppsLeft, (unsigned long)ppsRight, (unsigned long)plLength); } else { lRet = -1; } // // If the return value is -1, then we should handle the request. // if(lRet == -1) { // // Get the current read pointer. // lRead = psBuffer->lReadPtr; // // Get pointers to the next write position within the circular buffer. // *ppsLeft = psBuffer->psLeft + psBuffer->lWritePtr; *ppsRight = psBuffer->psRight + psBuffer->lWritePtr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -