diff --git a/media/java/android/media/audiofx/Visualizer.java b/media/java/android/media/audiofx/Visualizer.java index a7bdf4f506b23..89a509f64a1b1 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 { *
| Index | @@ -476,9 +477,23 @@ public class Visualizer { *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:
- * 2*i in the array contains
- * the real part of a complex number,2*i+1 contains the imaginary
- * part of the complex number.