Visualizer.GetFft(Byte[]) Method

Definition

Returns a frequency capture of currently playing audio content.

[Android.Runtime.Register("getFft", "([B)I", "GetGetFft_arrayBHandler")]
public virtual Android.Media.Audiofx.VisualizerStatus GetFft (byte[]? fft);
[<Android.Runtime.Register("getFft", "([B)I", "GetGetFft_arrayBHandler")>]
abstract member GetFft : byte[] -> Android.Media.Audiofx.VisualizerStatus
override this.GetFft : byte[] -> Android.Media.Audiofx.VisualizerStatus

Parameters

fft
Byte[]

array of bytes where the FFT should be returned

Returns

#SUCCESS in case of success, #ERROR_NO_MEMORY, #ERROR_INVALID_OPERATION or #ERROR_DEAD_OBJECT in case of failure.

Attributes

Exceptions

Remarks

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: <ul> <li> n is the capture size returned by getCaptureSize()</li> <li> Rfk, Ifk are respectively the real and imaginary parts of the kth frequency component</li> <li> If Fs is the sampling frequency retuned by getSamplingRate() the kth frequency is: k * Fs / n </li> </ul> <table border="0" cellspacing="0" cellpadding="0"> <tr><td>Index

</td> <td>0 </p></td> <td>1 </p></td> <td>2 </p></td> <td>3 </p></td> <td>4 </p></td> <td>5 </p></td> <td>... </p></td> <td>n - 2 </p></td> <td>n - 1 </p></td></tr> <tr><td>Data </p></td> <td>Rf0 </p></td> <td>Rf(n/2) </p></td> <td>Rf1 </p></td> <td>If1 </p></td> <td>Rf2 </p></td> <td>If2 </p></td> <td>... </p></td> <td>Rf(n/2-1) </p></td> <td>If(n/2-1) </p></td></tr> </table>

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 &lt; 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]);
                  }

Java documentation for android.media.audiofx.Visualizer.getFft(byte[]).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to