preface

I have always been very interested in music visualization, which happens to be involved in my work, so I tidy up and use Android to achieve a simple music visualization player.

Basic knowledge of

1, volume

Subjectively perceived sound size (also known as loudness) is determined by “amplitude” and the distance from the sound source.

2, tone

The frequency of a sound is determined by the frequency. The higher the frequency, the higher the pitch.

3, timbre Timbre is an abstract thing, waveform determines the sound timbre. Sound has different characteristics due to different sounding object materials, and waveform is the visual expression of this abstract characteristics. Typical timbre waveform is square wave, sawtooth wave, sine wave, pulse wave and so on.

4, sampling sampling, that is, according to a certain sampling frequency of analog signals into discrete sampling signals on the time axis process. In principle, the higher the sampling frequency, the better the quality of the sound, and the more authentic the sound is. The sampling rate is the number of samples per second extracted from analog signals to form discrete signals, expressed in Hertz (Hz). When it comes to sampling rate, there is a famous law that has to be mentioned, namely Shannon/Nyquist sampling law, which states that the sampling frequency must be greater than or equal to 2 times the highest frequency of the transmitted analog signal in order to restore the analog signal without distortion.

5, quantitativeFront while sampling the sampling signal is discrete signal on a timeline, but still is analog signals, the sampling value within a certain scope, can have an unlimited number of values, must adopt the method of “round” classification “integer”, the sample value is within a certain scope to make the sample by an unlimited number of values into a finite number of values, a process known as quantitative.6, codingAfter quantization, the sampling signal is transformed into a series of decimal digital code stream arranged according to sampling time sequence, namely decimal digital signal. A simple and efficient data system is a binary code system. Therefore, decimal numeric codes should be transformed into binary codes. This process of converting the quantized sampling signal into a binary code stream of a given word length (quantized bits) is called encoding. In Comparison of audio coding formats, there are four kinds of PCM audio coding formats: Linear PCM (PCM), Adaptive differential PCM (ADPCM), A-law (a-law 13 line code), μ-law (μ law 15 line code), the simplest of course is LPCM as shown in the figure below (example: 4bit), which is A uniform quantization code. Widely used in Audio CD, AES3, WAV, AIFF, AU, M2TS, VOB.

Analog signal to digital signal process

As can be seen from the above figure, the analog signal generates the sampling signal through PAM, and then generates the data between -127 and +127 through quantization. Finally, the data is stored in binary.

Visualizer

With the above basic knowledge, we can know that music visualization can actually generate music visualization content through quantized data. Google officially provides Visualizer class to help achieve music visualization. 1. Waveform data is the processed data of the analog signal in the figure above. What Google provides is an 8bit unsigned data.

例 句 : Frequency data is Frequency data, which is converted from continuous signal intensity in the time domain (waveform) to discrete signal intensity in the Frequency domain (spectrum) by fast Fourier transform.

Visualizer to use

1. Obtain an instance

visualizer = new Visualizer(mediaPlayer.getAudioSessionId());
Copy the code

2. Set the sampling value

visualizer.setCaptureSize(Visualizer.getCaptureSizeRange()[1]);
Copy the code

Through the Visualizer. GetCaptureSizeRange () the underlying implementation to return a sampling value array, the range of 0 to the minimum 128, 1 to a maximum of 1024! All samples are two to the NTH power!

3. Set the listener

setDataCaptureListener(OnDataCaptureListener listener, rate,iswave,isfft )
Copy the code

Behind the first three parameters: the frequency of the sampling rate, down through the method of Visualizer. GetMaxCaptureRate () returns the maximum sampling frequency, the unit is milliHertz milli Hertz, iswave waveform signal, isfft is frequency domain signals. The first argument is the OnDataCaptureListener interface, where it can have an anonymous inner class, and then it has two callback methods:

onWaveFormDataCapture(Visualizer visualizer, byte[] waveform, int samplingRate)
Copy the code

and

onFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate)
Copy the code

These callbacks correspond to the two arguments iswave and isFFt! OnWaveFormDataCapture method is called if isWave is true and ISFFt is false, onFftDataCapture method is called if ISWave is false and isFFt is true.

Music Visualization

Code implementation

To be continued….

The project address

Github.com/hankinghu/A…

reference

1, developer.android.com/reference/a…