C
C
constv2012-09-28 11:47:48
Android
constv, 2012-09-28 11:47:48

How to solve the problem with android.media.audiofx.Visualizer?

Hello, I don’t understand what’s in byte[] fft, which is passed to onFftDataCapture.
In the docks
public int getFft (byte[] fft)
Since: API Level 9 Returns a frequency capture of currently playing audio content.
This method must be called when the Visualizer is enabled.
The capture is an 8-bit magnitude FFT, the frequency range covered being 0 (DC) to half of the sampling rate returned by getSamplingRate(). The capture returns the real and imaginary parts of a number of frequency points equal to half of the capture size plus one.
Note: only the real part is returned for the first point (DC) and the last point (sampling frequency / 2).
The layout in the returned byte array is as follows:
•n is the capture size returned by getCaptureSize()
•Rfk, Ifk are respectively the real and imaginary parts of the kth frequency component
•If Fs is the sampling frequency retuned by getSamplingRate() the kth frequency is: (k*Fs)/ (n / 2)
To calculate the "magnitude" for the frequency range wrote like this

@Override
    public void onFftDataCapture(Visualizer visualizer, byte[] fft,
            int samplingRate) {
        if(mVisualizer==null)
            return;

        int redCount=0;
        int redValue=0;
        int yelloCount=0;
        int yelloValue=0;    
        int greenCount=0;
        int greenValue=0;    
        int blueCount=0;
        int blueValue=0;        
        for(int i=2; i<fft.length; i+=2){
            int Fsi = i*((samplingRate/2)/(fft.length/2));
            double value = Math.sqrt(fft[i]*fft[i]+fft[i+1]*fft[i+1]);

            if(Fsi<=200){
                redCount++;
                redValue+=value;
            }
            if(Fsi>200&&Fsi<=800){
                yelloCount++;
                yelloValue+=value;
            }    
            if(Fsi>800&&Fsi<=3500){
                greenCount++;
                greenValue+=value;
            }    
            if(Fsi>3500){
                blueCount++;
                blueValue+=value;
            }            
        }
        blueValue=blueCount==0?0:blueValue/blueCount;
        greenValue=greenCount==0?0:greenValue/greenCount;
        yelloValue=yelloCount==0?0:yelloValue/yelloCount;
        redValue=redCount==0?0:redValue/redCount;
        Log.i("MyLog","Red="+redValue + " Yello="+yelloValue+ " Green="+greenValue+ " Blue="+blueValue);
    }

And I get something like this
09-28 11:47:59.917: INFO/MyLog(29701): Red=48 Yello=12 Green=5 Blue=0
09-28 11:48:00.089: INFO/MyLog(29701): Red= 44 Yello=9 Green=1 Blue=0
09-28 11:48:00.175: INFO/MyLog(29701): Red=45 Yello=12 Green=3 Blue=0
09-28 11:48:00.296: INFO/MyLog (29701): Red=43 Yello=11 Green=4 Blue=1
09-28 11:48:00.402: INFO/MyLog(29701): Red=32 Yello=10 Green=3 Blue=0
09-28 11:48 :00.531: INFO/MyLog(29701): Red=47 Yello=13 Green=4 Blue=0
Which, in my opinion, does not match the song being played (Forbidden Drummers - Cuba).
Please help me figure it out:
What is actually in fft[i]+fft[i+1], what is the maximum value of this element (as I understand it, 127, if this number can be negative)?
What frequencies correspond to the initial elements of the array (it seems that they are listed in the reverse order in the array)?
Is there an error in the docs here?
If Fs is the sampling frequency retuned by getSamplingRate() the kth frequency is: (k*Fs)/(n/2
) above they write
range covered being 0 (DC) to half of the sampling rate returned by getSamplingRate()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
constv, 2012-10-02
@constv

I figured it out using test samples, an error in my code, I considered the average value for frequency ranges, and since, for example, a very small number of fff elements fall into the low frequency range, the value turns out to be large, and vice versa, there are a lot of elements, the value of most of which is about 0, then the total value of the range turned out to be small. That. it is necessary to consider not the average, but the peaks for the ranges.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question