FileDocCategorySizeDatePackage
EnergyProbe.javaAPI DocAndroid 5.1 API3812Thu Mar 12 22:22:30 GMT 2015com.android.mediaframeworktest.functional

EnergyProbe

public class EnergyProbe extends Object
The EnergyProbe class provides audio signal energy measurements based on the FFT returned by the Visualizer class. The measure is qualitative and not quantitative in that the returned value has no unit and is just proportional to the amount of energy present around the specified frequency.

Fields Summary
private String
TAG
private static int
CAPTURE_SIZE
private static int
MEASURE_COUNT
private static int
AVERAGE_COUNT
private android.media.audiofx.Visualizer
mVisualizer
private int
mMaxFrequency
private int
mCapturePeriodMs
private byte[]
mFft
Constructors Summary
public EnergyProbe(int session)


       
        try {
            mVisualizer = new Visualizer(session);
            if (mVisualizer != null) {
                mVisualizer.setCaptureSize(CAPTURE_SIZE);
                mMaxFrequency = mVisualizer.getSamplingRate() / 2000;
                mCapturePeriodMs = 1000000 / mVisualizer.getMaxCaptureRate();
            }
        } catch (UnsupportedOperationException e) {
            Log.e(TAG, "Error creating visualizer");
        } catch (IllegalStateException e) {
            Log.e(TAG, "Error configuring visualizer");
        }
    
Methods Summary
public intcapture(int freq)

        int energy = 0;
        int count = 0;

        if (freq > mMaxFrequency) {
            return 0;
        }

        if (mVisualizer != null) {
            try {
                mVisualizer.setEnabled(true);
                for (int i = 0; i < MEASURE_COUNT; i++) {
                    if (mVisualizer.getFft(mFft) == Visualizer.SUCCESS) {
                        if (freq == mMaxFrequency) {
                            energy += (int)mFft[0] * (int)mFft[0];
                        } else {
                            int bin = 2 * (freq * CAPTURE_SIZE / mMaxFrequency / 2);
                            if (bin < 2) bin = 2;
                            int tmp = 0;
                            int j;
                            for (j = 0;
                                 (j < AVERAGE_COUNT) && ((bin + 2 * j) < CAPTURE_SIZE);
                                 j++) {
                                tmp += (int)mFft[bin + 2 * j] * (int)mFft[bin + 2 * j] +
                                       (int)mFft[bin + 2 * j + 1] * (int)mFft[bin + 2 * j + 1];
                            }
                            // j is always != 0
                            energy += tmp/j;
                        }
                        count++;
                    }
                    Thread.sleep(mCapturePeriodMs);
                }
                mVisualizer.setEnabled(false);
            } catch (IllegalStateException e) {
                Log.e(TAG, "Error capturing audio");
            }
        }
        if (count == 0) {
            return 0;
        }
        return energy/count;
    
public voidrelease()

        if (mVisualizer != null) {
            mVisualizer.release();
            mVisualizer = null;
        }