filoe / cscore

An advanced audio library, written in C#. Provides tons of features. From playing/recording audio to decoding/encoding audio streams/files to processing audio data in realtime (e.g. applying custom effects during playback, create visualizations,...). The possibilities are nearly unlimited.
Other
2.14k stars 450 forks source link

In SpectrumBase.cs ,why " _maxFftIndex = _fftSize / 2 - 1 " ? #470

Closed Personuo closed 2 years ago

Personuo commented 2 years ago

I'm reading the code carefully and trying to understand it, but I can't seem to figure it out

_maxFftIndex = _fftSize / 2 - 1
_maximumFrequencyIndex = Math.Min(_spectrumProvider.GetFftBandIndex(_maxFrequency) + 1, _maxFftIndex);

_maximumFrequencyIndex the maximum value is equal to _maxFftIndex

But In the following code, fftBuffer size is _fftSize ,so not all fftBuffer elements are processed? only work half.

 protected virtual SpectrumPointData[] CalculateSpectrumPoints(double maxValue, float[] fftBuffer)
        {
             ........................
            for (int i = _minimumFrequencyIndex; i <= _maximumFrequencyIndex; i++)
            {
                switch (ScalingStrategy)
                {
                    case ScalingStrategy.Decibel:
                        value0 = (((20 * Math.Log10(fftBuffer[i])) - MinDbValue) / DbScale) * actualMaxValue;
                        break;
                    case ScalingStrategy.Linear:
                        value0 = (fftBuffer[i] * ScaleFactorLinear) * actualMaxValue;
                        break;
                    case ScalingStrategy.Sqrt:
                        value0 = ((Math.Sqrt(fftBuffer[i])) * ScaleFactorSqr) * actualMaxValue;
                        break;
                }.........................................
filoe commented 2 years ago

Because the other half is mirrored. Like the values are 5, 4, 3, 2, 1, 1, 2, 3, 4, 5

Am 05.06.2022 um 19:19 schrieb Personuo @.***>:

 I'm reading the code carefully and trying to understand it, but I can't seem to figure it out

_maxFftIndex = _fftSize / 2 - 1 _maximumFrequencyIndex = Math.Min(_spectrumProvider.GetFftBandIndex(_maxFrequency) + 1, _maxFftIndex); _maximumFrequencyIndex the maximum value is equal to _maxFftIndex

But In the following code, fftBuffer size is _fftSize ,so not all fftBuffer elements are processed? only work half.

protected virtual SpectrumPointData[] CalculateSpectrumPoints(double maxValue, float[] fftBuffer) { ........................ for (int i = _minimumFrequencyIndex; i <= _maximumFrequencyIndex; i++) { switch (ScalingStrategy) { case ScalingStrategy.Decibel: value0 = (((20 Math.Log10(fftBuffer[i])) - MinDbValue) / DbScale) actualMaxValue; break; case ScalingStrategy.Linear: value0 = (fftBuffer[i] ScaleFactorLinear) actualMaxValue; break; case ScalingStrategy.Sqrt: value0 = ((Math.Sqrt(fftBuffer[i])) ScaleFactorSqr) actualMaxValue; break; }......................................... — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.

Personuo commented 2 years ago

thank you reply ! so the fftbuffer frequency range is 0 --- 48000 --- 0 ? Is 48000 in the middle?

filoe commented 2 years ago

Yes, but try it out. The SpectrumProvider has a GetFftBandIndex method.

Am 05.06.2022 um 19:54 schrieb Personuo @.***>:

 fftbuffer The frequency range is 0480000 ? Is 48000 in the middle?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.

Personuo commented 2 years ago

Thank you, I have another question, in the sample spectrumdata has buffering , when I turn off the audio immediately, the data does not go to zero until a few frames later. How do I get unbuffered data? I would like to control the buffering time myself to adjust the visualization.