From ca6a9f8feff41ac2fdc78a3df3fbfbb79361b100 Mon Sep 17 00:00:00 2001 From: Mikhail Naganov Date: Mon, 3 Dec 2018 12:44:10 -0800 Subject: Update and fix Visualizer class docs 1) Fix the formula for the 'kth frequency' in getFft method: since the 'fft' array holds n / 2 frequencies ranging from 0 to Fs / 2, the correct formula is k*Fs/n. Also fix the last frequency index in the table. 2) Move the sample code for calculating magnitudes and phases from the fft array from 'onFftDataCapture' to 'getFft', provide a link. 3) Fix the sample code which wasn't taking into account that fft[0] is the real part of FFT at DC, and fft[1] is the real part of FFT at Fs / 2. Improve the sample code by linking the variable names closer to the variable names in the documentation. Bug: 113790190 Test: atest CtsMediaTestCases:VisualizerTest Change-Id: I07cc9cfcda5b8c44aae2742a26173e7a9475120a --- media/java/android/media/audiofx/Visualizer.java | 43 ++++++++++++------------ 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/media/java/android/media/audiofx/Visualizer.java b/media/java/android/media/audiofx/Visualizer.java index a7bdf4f506b2..89a509f64a1b 100644 --- a/media/java/android/media/audiofx/Visualizer.java +++ b/media/java/android/media/audiofx/Visualizer.java @@ -18,11 +18,12 @@ package android.media.audiofx; import android.annotation.UnsupportedAppUsage; import android.app.ActivityThread; -import android.util.Log; -import java.lang.ref.WeakReference; import android.os.Handler; import android.os.Looper; import android.os.Message; +import android.util.Log; + +import java.lang.ref.WeakReference; /** * The Visualizer class enables application to retrieve part of the currently playing audio for @@ -455,7 +456,7 @@ public class Visualizer { *
  • 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)
  • + * k * Fs / n * * * @@ -476,9 +477,23 @@ public class Visualizer { * * * - * - * + * + * *
    Index

    Rf2

    If2

    ...

    Rf(n-1)/2

    If(n-1)/2

    Rf(n/2-1)

    If(n/2-1)

    + *

    In order to obtain magnitude and phase values the following code can + * be used: + *

    +     *       int n = fft.size();
    +     *       float[] magnitudes = new float[n / 2 + 1];
    +     *       float[] phases = new float[n / 2 + 1];
    +     *       magnitudes[0] = (float)Math.abs(fft[0]);      // DC
    +     *       magnitudes[n / 2] = (float)Math.abs(fft[1]);  // Nyquist
    +     *       phases[0] = phases[n / 2] = 0;
    +     *       for (int k = 1; k < n / 2; k++) {
    +     *           int i = k * 2;
    +     *           magnitudes[k] = (float)Math.hypot(fft[i], fft[i + 1]);
    +     *           phases[k] = (float)Math.atan2(fft[i + 1], fft[i]);
    +     *       }
    * @param fft array of bytes where the FFT should be returned * @return {@link #SUCCESS} in case of success, * {@link #ERROR_NO_MEMORY}, {@link #ERROR_INVALID_OPERATION} or {@link #ERROR_DEAD_OBJECT} @@ -561,25 +576,11 @@ public class Visualizer { *

    Data in the fft buffer is valid only within the scope of the callback. * Applications which need access to the fft data after returning from the callback * should make a copy of the data instead of holding a reference. + *

    For the explanation of the fft data array layout, and the example + * code for processing it, please see the documentation for {@link #getFft(byte[])} method. * - *

    In order to obtain magnitude and phase values the following formulas can - * be used: - *

    -         *       for (int i = 0; i < fft.size(); i += 2) {
    -         *           float magnitude = (float)Math.hypot(fft[i], fft[i + 1]);
    -         *           float phase = (float)Math.atan2(fft[i + 1], fft[i]);
    -         *       }
    * @param visualizer Visualizer object on which the listener is registered. * @param fft array of bytes containing the frequency representation. - * The fft array only contains the first half of the actual - * FFT spectrum (frequencies up to Nyquist frequency), exploiting - * the symmetry of the spectrum. For each frequencies bin i: - * * @param samplingRate sampling rate of the visualized audio. */ void onFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate); -- cgit v1.2.3-59-g8ed1b