I am trying to implement an audio spectrum analyzer in android using the Visualizer class.
I am getting FFT data in the onFftDataCapture()
method of OnDataCaptureListener()
event and I'm drawing that on the canvas using drawLines()
.
But the spectrum display is not showing properly. I can see changes on left side of graph only. But in Window Media Player, the output of the same song is different. What I am missing?
Can anyone help me on this with an example or a link?
CODE
mVisualizer.setDataCaptureListener(
new Visualizer.OnDataCaptureListener() {
public void onWaveFormDataCapture(Visualizer visualizer,
byte[] bytes, int samplingRate) {}
public void onFftDataCapture(Visualizer visualizer,
byte[] bytes, int samplingRate) {
mVisualizerView.updateVisualizer(bytes, samplingRate);
}
}, Visualizer.getMaxCaptureRate() / 2, false, true);
onPaint()
for (int i = 0; i < mBytes.length / 2; i++) {
mPoints[i * 4] = i * 8;
mPoints[i * 4 + 1] = 0;
mPoints[i * 4 + 2] = i * 8;
byte rfk = mBytes[2 * i];
byte ifk = mBytes[2 * i + 1];
magnitude = (float) (rfk * rfk + ifk * ifk);
int dbValue = (int) (10 * Math.log10(magnitude));
mPoints[i * 4 + 3] = (float) (dbValue * 7);
}
canvas.drawLines(mPoints, mForePaint);
Where mVisualizer
is Visualizer class object, and mBytes
is FFT Data got from onFftDataCapture
event.
You can read more about FFT data returned by event here.
This is what values I get onFftDataCapture()
:
[90, -1, -27, 102, 13, -18, 40, 33, -7, 16, -23, -23, -2, -8, -11, -9, -8, -33, -29, 44, 4, -9, -15, -1, -2, -17, -7, 1, 1, 0, 3, -11, -5, 10, -24, -6, -23, 1, -9, -21, -2, 4, 9, -10, -14, -5, -16, 8, 6, -16, 14, 3, 7, 15, 10, -2, -15, -14, -5, 10, 8, 23, -1, -16, -2, -6, 4, 9, -1, 0, 0, 9, 1, 4, -2, 6, -6, -6, 8, -4, 6, 6, -4, -5, -5, -2, 3, 0, -1, 0, -7, 0, 2, 1, 0, 1, -1, 0, -1, 1, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, -1]
Any idea, link would be helpful.
Update for @Chris Stratton
Now i am playing square wave at 1000 Hz file and took screen shot of that. What you suggest now?
Updated after @ruhalde suggestion
Now I am playing Frequency sweep (20-20000 Hz) file and this file generated following output.
invalidate()
inupdateVisualizer()
method. It is doing that task. – Perquisite