Recently, because of the need to realize the music spectrum, so today for everyone to popularize. About music spectrum you need to understand the knowledge of digital signal processing, especially the FFT. Simply put, the continuous signal (waveform) intensity in the time domain is converted into a discrete frequency domain signal (spectrum). I understand that the waveform is the intensity of the signal, or the output power of the audio equipment, high power, the volume is large. But the melody of the song will not change, because the spectrum will not change.
The spectrum reflects the frequency of the sound of this music over a continuous period of time. I don't know if I understand that right.
The music spectrum implementation in this paper is modeled after an example of the Android API Demo, which requires Android 2.3 and above, because the visualizer class is used, which is only supported by Android 2.3 + API.
The visualizer is instantiated first, and the parameter SessionID can be obtained by MediaPlayer object
[Java]View Plaincopy
- Visualizer = New Visualizer (Mplayerinstance.getaudiosessionid ());
Then set the length of the music content that needs to be converted, professionally speaking this is sampling, the sample value is generally 2 of the number of times, such as 64,128,256,512,1024. Here I set the 128, because the longer the length, the FFT algorithm runs longer.
[Java]View Plaincopy
- Visualizer.setcapturesize (Visualizer.getcapturesizerange () [0]);
The listener is then set up for visualizer so that when a piece of data is capture, two functions are triggered to process it. Set the Listener function to
[Java]View Plaincopy
- Setdatacapturelistener (Ondatacapturelistener Listener, RATA,ISWAVE,ISFFT)
Parameter explanation:
Rate, which indicates the period of sampling, that is, how often sampling, the contact is the first time to sample 128 data, this article set to 512mHz update
Iswave, it's a waveform signal.
Isfft, is the FFT signal, which indicates whether to obtain the waveform signal or the frequency domain signal
Ondatacapturelistener, the table listener function, the anonymous inner class implements the interface, which needs to implement two functions
[Java]View Plaincopy
- Onwaveformdatacapture (Visualizer Visualizer,byte[] waveform, int samplingrate)
And
[Java]View Plaincopy
- Public void Onfftdatacapture (Visualizer Visualizer,byte[] FFT, int samplingrate)
The samplingrate is the sample rate, which is the value of the above, 512mHz.
Of these, two byte[] waveform and byte[] FFT arrays, respectively, to obtain the waveform data and FFT data, the size of the byte array is set before the sample value size 128, to obtain the data as shown.
where n is the sampled value, index 0 represents the DC component, and the RF represents the real part of the FFT calculation, if represents the imaginary part after the FFT calculation.
How to calculate the frequency, that is, the real part of the FFT and the corresponding imaginary part of the sum of each square and then the root, simply said is the square modulo.
For specific calculations, see the code below.
[Java]View Plaincopy
- Visualizer.setdatacapturelistener (
- New Visualizer.ondatacapturelistener () {
- @Override
- public void Onwaveformdatacapture (Visualizer Visualizer,
- byte[] waveform, int samplingrate) {
- //Add here to get data processing byte[] Array update out and draw. Here you can put this
- //Array to Runonmusic
- //Visualview.updatevisualizer (waveform);
- }
- @Override
- public void Onfftdatacapture (Visualizer Visualizer,
- byte[] FFT, int samplingrate) {
- byte[] model = new byte[fft.length/ 2 + 1];
- model[0] = (byte) math.abs (fft[1]);
- Int J = 1;
- For (int i = 2; i < 18;) {
- MODEL[J] = (byte) math.hypot (Fft[i], fft[i + 1]);
- i + = 2;
- j + +;
- }
- Visualview.updatevisualizer (model);
- }
- }, Visualizer.getmaxcapturerate ()/ 2, false, true);
- }
Where Visualview is the display program, Updatevisulizer is updating the spectral value obtained by the model to the view to be displayed.
[Java]View Plaincopy
- protected void OnDraw (canvas canvas) {
- Super.ondraw (canvas);
- if (mBytes = = null) {
- return;
- }
- if (mpoints = = Null | | Mpoints.length < MBYTES.LENGTH * 4) {
- mpoints = new float[mbytes.length * 4];
- Mrect.set (0, 0, GetWidth (), GetHeight ()- 50);
- For (int i = 0; i < 9; i++) {
- if (Mbytes[i] < 0)
- Mbytes[i] = 127;
- Mpoints[i * 4] = mrect.width () * I/ 9;
- Mpoints[i * 4 + 1] = Mrect.height ()/ 2;
- Mpoints[i * 4 + 2] = mrect.width () * I/ 9;
- Mpoints[i * 4 + 3] = 2 + mrect.height ()/ 2 + mbytes[i];
- }
- Canvas.drawlines (mpoints, mforepaint);
- }
- }
Android Music Spectrum implementation