📄 spectrum.c
字号:
pSpectrum->ulBucket[sBand][pSpectrum->usCurrentBucket] = 0; } } }}//****************************************************************************//// SpectrumBufferIoctl is the buffer IOCTL handler for the tone control input// buffer.////****************************************************************************static longSpectrumBufferIoctl(unsigned long ulIoctl, BufferState *psBuffer, unsigned long ulParam1, unsigned long ulParam2, unsigned long ulParam3){ // // Determine what to do based on the specified IOCTL. // switch(ulIoctl) { // // Set the input buffer. // case IOCTL_BUFFER_SETBUFFER: { tSpectrumAnalyzer *pSpectrum; // // Get a pointer to the tone control state structure. // pSpectrum = (tSpectrumAnalyzer *)psBuffer->lInstanceData; // // Clear the sample buckets and delay lines. // SpectrumClear(pSpectrum); // // Set the buffer of the output buffer. // return(BufferSetBuffer(pSpectrum->pOutputBuffer, (short *)ulParam1, (short *)ulParam2, (long)ulParam3)); } // // Determine the amount of space available in the output buffer. // case IOCTL_BUFFER_SPACEAVAILABLE: { tSpectrumAnalyzer *pSpectrum; // // Get a pointer to the tone control state structure. // pSpectrum = (tSpectrumAnalyzer *)psBuffer->lInstanceData; // // Return the amount of space available in the output buffer. // return(BufferSpaceAvailable(pSpectrum->pOutputBuffer)); } // // Determine if the output buffer is empty. // case IOCTL_BUFFER_ISEMPTY: { tSpectrumAnalyzer *pSpectrum; // // Get a pointer to the tone control state structure. // pSpectrum = (tSpectrumAnalyzer *)psBuffer->lInstanceData; // // Return whether or not the output buffer is empty. // return(BufferIsEmpty(pSpectrum->pOutputBuffer)); } // // Get the write pointer. // case IOCTL_BUFFER_GETWRITEPOINTER: { tSpectrumAnalyzer *pSpectrum; // // Get a pointer to the tone control state structure. // pSpectrum = (tSpectrumAnalyzer *)psBuffer->lInstanceData; // // Return the write pointer of the output buffer. // return(BufferGetWritePointer(pSpectrum->pOutputBuffer, (short **)ulParam1, (short **)ulParam2, (long *)ulParam3)); } // // Update the write pointer. // case IOCTL_BUFFER_UPDATEWRITEPOINTER: { tSpectrumAnalyzer *pSpectrum; short *psLeft, *psRight; long lLoop, lLength; // // Get a pointer to the spectrum analzye state structure. // pSpectrum = (tSpectrumAnalyzer *)psBuffer->lInstanceData; // // See if the spectrum analyzer is enabled. // if(!pSpectrum->bProcessAudio) { // // The spectrum analyzer is disabled, so simply pass this // update request to the output buffer. // return(BufferUpdateWritePointer(pSpectrum->pOutputBuffer, ulParam1)); } // // Get the current write pointer for the output buffer. // BufferGetWritePointer(pSpectrum->pOutputBuffer, &psLeft, &psRight, &lLength); // // See if there is enough space in this buffer. // if(lLength < (long)ulParam1) { return(0); } // // If there is a right channel buffer, then perform spectral // analysis on the stereo stream. // if(psLeft != psRight) { // // Loop through the samples in four separate blocks. This is // to prevent starving the downstream task (performing four // separate buffer updates instead of one large one). // for(lLoop = 0; lLoop < (long)ulParam1; lLoop += ((ulParam1 + 15) / 4) & ~3) { // // Determine the number of samples to process in this // block. // lLength = ((ulParam1 + 15) / 4) & ~3; if((lLoop + lLength) > ulParam1) { lLength = ulParam1 - lLoop; } // // Process this block of samples. // FilterStereo(pSpectrum, psLeft + lLoop, psRight + lLoop, lLength); // // Update the buffer write pointer. // BufferUpdateWritePointer(pSpectrum->pOutputBuffer, lLength); } } // // Otherwise, perform spectral analysis on the mono stream. // else { // // Loop through the samples in four separate blocks. This is // to prevent starving the downstream task (performing four // separate buffer updates instead of one large one). // for(lLoop = 0; lLoop < (long)ulParam1; lLoop += ((ulParam1 + 15) / 4) & ~3) { // // Determine the number of samples to process in this // block. // lLength = ((ulParam1 + 15) / 4) & ~3; if((lLoop + lLength) > ulParam1) { lLength = ulParam1 - lLoop; } // // Process this block of samples. // FilterMono(pSpectrum, psLeft + lLoop, lLength); // // Update the buffer write pointer. // BufferUpdateWritePointer(pSpectrum->pOutputBuffer, lLength); } } // // Success. // return(1); } // // Set the sample rate of the data buffer. // case IOCTL_BUFFER_SETSAMPLERATE: { tSpectrumAnalyzer *pSpectrum; // // Get a pointer to the tone control state structure. // pSpectrum = (tSpectrumAnalyzer *)psBuffer->lInstanceData; // // We only support 32kHz, 44.1kHz, and 48kHz sample rates. // if((ulParam1 != 32000) && (ulParam1 != 44100) && (ulParam1 != 48000)) { return(0); } // // Pass the set sample rate request to the output buffer. // if(BufferSetSampleRate(pSpectrum->pOutputBuffer, (long)ulParam1, (long)ulParam2) == 0) { return(0); } // // Adjust the filter coefficents based on the sample rate. // switch(ulParam1) { // // The sample rate is 32000Hz. // case 32000: { // // Use the 32kHz filter. // pSpectrum->psFilter = ppsFilter[0]; // // We're done with this sample rate. // break; } // // The sample rate is 44100Hz. // case 44100: { // // Use the 44.1kHz filter. // pSpectrum->psFilter = ppsFilter[1]; // // We're done with this sample rate. // break; } // // The sample rate is 48000Hz. // case 48000: { // // Use the 48kHz filter. // pSpectrum->psFilter = ppsFilter[2]; // // We're done with this sample rate. // break; } } // // Clear the sample buckets and delay lines. // SpectrumClear(pSpectrum); // // Success. // return(1); } // // Fail all other IOCTLs as we do not want to allow any other actions // to be performed on the input buffer. // default: { return(0); } }}//****************************************************************************//// SpectrumInit sets the initial state of the tone control filter.////****************************************************************************voidSpectrumInit(tSpectrumAnalyzer *pSpectrum, BufferState *pOutputBuffer){ // // Initialize the delay lines to silence. // SpectrumClear(pSpectrum); // // The default filter to use is the 44.1kHz filter. // pSpectrum->psFilter = ppsFilter[1]; // // The spectrum analyzer is enabled by default. // pSpectrum->bProcessAudio = 1; // // Initialize the input buffer. // BufferInit(&(pSpectrum->sInputBuffer), SpectrumBufferIoctl, (long)pSpectrum); // // Save the pointer to the output buffer. // pSpectrum->pOutputBuffer = pOutputBuffer;}//****************************************************************************//// SpectrumGetBands gets the current value for the spectrum analzyer's bands.////****************************************************************************voidSpectrumGetBands(tSpectrumAnalyzer *pSpectrum, long *plBands){ unsigned long ulBand, ulIdx, ulSum; // // Loop through all the bands. // for(ulBand = 0; ulBand < NUMBANDS; ulBand++) { // // Loop through all the buckets in this band. // for(ulIdx = 0, ulSum = 0; ulIdx < NUMBUCKETS; ulIdx++) { // // See if this is the current bucket. // if(ulIdx != pSpectrum->usCurrentBucket) { // // This is not the current bucket, so add the samples from this // bucket to the running total. // ulSum += pSpectrum->ulBucket[ulBand][ulIdx]; } } // // Return the average of the samples in the buckets. //#if NUMBUCKETS == 8 plBands[ulBand] = (((ulSum / NUMSAMPLES) * 73) + 36) >> 9;#elif NUMBUCKETS == 16 plBands[ulBand] = (((ulSum / NUMSAMPLES) * 273) + 136) >> 12;#else plBands[ulBand] /= (NUMSAMPLES * (NUMBUCKETS - 1));#endif }}//****************************************************************************//// SpectrumEnable enables the spectrum analyzer processing of the audio// stream.////****************************************************************************voidSpectrumEnable(tSpectrumAnalyzer *pSpectrum){ // // Enable the spectrum analyzer. // pSpectrum->bProcessAudio = 1;}//****************************************************************************//// SpectrumDisable disables the spectrum analzyer processing of the audio// stream.////****************************************************************************voidSpectrumDisable(tSpectrumAnalyzer *pSpectrum){ // // Disable the spectrum analyzer. // pSpectrum->bProcessAudio = 0;}//****************************************************************************//// SpectrumGetInputBuffer returns a pointer to the input buffer for the tone// control.////****************************************************************************BufferState *SpectrumGetInputBuffer(tSpectrumAnalyzer *pSpectrum){ // // Return a pointer to the input buffer. // return(&(pSpectrum->sInputBuffer));}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -