Android Music Spectrum implementation

Source: Internet
Author: User

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
    1. 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
    1. 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
    1. 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
    1. Onwaveformdatacapture (Visualizer Visualizer,byte[] waveform, int samplingrate)

And

[Java]View Plaincopy
    1. 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
  1. Visualizer.setdatacapturelistener (
  2. New Visualizer.ondatacapturelistener () {
  3. @Override
  4. public void Onwaveformdatacapture (Visualizer Visualizer,
  5. byte[] waveform, int samplingrate) {
  6. //Add here to get data processing byte[] Array update out and draw. Here you can put this
  7. //Array to Runonmusic
  8. //Visualview.updatevisualizer (waveform);
  9. }
  10. @Override
  11. public void Onfftdatacapture (Visualizer Visualizer,
  12. byte[] FFT, int samplingrate) {
  13. byte[] model = new byte[fft.length/ 2 + 1];
  14. model[0] = (byte) math.abs (fft[1]);
  15. Int J = 1;
  16. For (int i = 2; i < 18;) {  
  17. MODEL[J] = (byte) math.hypot (Fft[i], fft[i + 1]);
  18. i + = 2;
  19. j + +;
  20. }
  21. Visualview.updatevisualizer (model);
  22. }
  23. }, Visualizer.getmaxcapturerate ()/ 2, false, true);
  24. }

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
  1. protected void OnDraw (canvas canvas) {
  2. Super.ondraw (canvas);
  3. if (mBytes = = null) {
  4. return;
  5. }
  6. if (mpoints = = Null | | Mpoints.length < MBYTES.LENGTH * 4) {
  7. mpoints = new float[mbytes.length * 4];
  8. Mrect.set (0, 0, GetWidth (), GetHeight ()- 50);
  9. For (int i = 0; i < 9; i++) {
  10. if (Mbytes[i] < 0)
  11. Mbytes[i] = 127;
  12. Mpoints[i * 4] = mrect.width () * I/ 9;
  13. Mpoints[i * 4 + 1] = Mrect.height ()/ 2;
  14. Mpoints[i * 4 + 2] = mrect.width () * I/ 9;
  15. Mpoints[i * 4 + 3] = 2 + mrect.height ()/ 2 + mbytes[i];
  16. }
  17. Canvas.drawlines (mpoints, mforepaint);
  18. }
  19. }

Android Music Spectrum implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.