I'm writing a plugin for a Unity application that uses the Android Visualizer class. I'm using the getFft() function and the code provided there to get the FFT magnitudes. The values returned are dependent on volume - much higher values with higher volume, and much lower values with lower volume.
Here is my constructor where I initialize the Visualizer:
private PluginClass() {
errors = new int[2];
int size = Visualizer.getCaptureSizeRange()[1];
// Equalizer
Equalizer mEqualizer = new Equalizer(0, 0);
// Visualizer
this.visualizer = new Visualizer(0);
this.visualizer.setEnabled(false);
mEqualizer.setEnabled(true);
this.visualizer.setCaptureSize(size);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
this.visualizer.setScalingMode(SCALING_MODE_NORMALIZED);
this.visualizer.setMeasurementMode(MEASUREMENT_MODE_PEAK_RMS);
}
this.visualizer.setEnabled(true);
this.waveFormData = new byte[size];
this.fftData = new byte[size];
}
I'm setting the equalizer (I've called setEnabled
before the Visualizer was created, after it was created but before it was disabled, after it was disabled, pretty much all over).
The scaling mode is set to be normalized, and when I call getScalingMode()
I can confirm that it is indeed set to SCALING_MODE_NORMALIZED
.
Does anyone have any ideas as to why this is? In the other duplicate of this question, with no explanation, the one answer says to use setVolumeControlStream(AudioManager.STREAM_MUSIC);
. I tried this to no avail, but I don't see why it would work anyways.
The OPs have abandoned their questions who have asked this before, with pending questions by answerers asked, and no code provided, so I had to open this one. This way, I can also add a bounty to the question.
I am running the app in VR mode in case this is some obscure bug with VR and Android Java and Unity not playing nicely together.
Thanks!
Edit Here is the code I use to actually generate the FFT Magnitudes:
public float[] getFftMagnitudes() {
this.errors[0] = this.visualizer.getFft(this.fftData);
int n = this.fftData.length;
float[] magnitudes = new float[n / 2 + 1];
magnitudes[0] = (float)Math.abs(this.fftData[0]); // DC
magnitudes[n / 2] = (float)Math.abs(this.fftData[1]); // Nyquist
for (int k = 1; k < n / 2; k++) {
int i = k * 2;
magnitudes[k] = (float)Math.hypot(this.fftData[i], this.fftData[i + 1]);
}
return magnitudes;
}