Android PCM Codec

Source: Internet
Author: User
1. What is the audio sampling rate and sampling size?
In nature, sound is very complex, and waveforms are extremely complex. We usually use pulse code modulation coding. That is, PCM encoding. PCM converts a continuously changing analog signal into a digital code by sampling, quantization, and encoding.
Sampling: sampling rate is called in audio collection. Because sound is actually an energy wave, there are also frequency and amplitude features, frequency corresponds to the time axis, amplitude corresponds to the level axis. The wave is infinitely smooth, and the string line can be regarded as composed of countless points. Because the storage space is relatively limited, the point of the string line must be sampled during the digital encoding process. The sampling process is to extract the frequency value of a certain point. Obviously, the more points are extracted in one second, the richer the frequency information is obtained. In order to restore the waveform, there must be two sampling points, and the highest frequency that the ears can feel is 20 kHz. Therefore, to meet the auditory requirements of the ears, at least 40 kb of sampling per second is required and expressed at 40 kHz, this 40 kHz is the sampling rate. Our common CD, sampling rate is 44.1 kHz. Quantization: here the sampling size is the quantization process, which quantifies the energy value of the frequency to indicate the signal strength. The Quantization level is an integer power of 2. We usually use 16-bit CD sampling size, that is, the 16 power of 2. Encoding: According to the sampling rate and sampling size, we can know that audio encoding can only be infinitely close to natural signals, at least the current technology can only do this, relative to natural signals, any digital audio encoding scheme is lossy because it cannot be completely restored. In computer applications, PCM encoding can achieve the highest fidelity. It is widely used for material storage and music appreciation. CD, DVD, and common wav files are all used. Therefore, PCM is about lossless encoding, because PCM represents the best fidelity level in digital audio, does not mean that PCM can ensure absolute fidelity of the signal, PCM can only achieve the maximum infinite proximity. We habitually include MP3 in the lossy Audio Encoding category, which is relative to PCM encoding. The purpose of emphasizing the relative lossy and lossless nature of coding is to tell everyone that it is difficult to achieve true lossless, just like using numbers to express the circumference rate, no matter how high the accuracy is, it is just infinitely close, not the value that truly equals the circumference Rate
Why audio compression technology?
It is easy to calculate the bit rate of a pcm audio stream. The sampling rate value × the sample size Value × the number of audio channels bps. A pcm-encoded WAV file with a sampling rate of 44.1 kHz and a sampling size of 16 bit. The data rate is 44.1k × 16 × 2 = 1411.2 kbps. We often say that the 1411.2 kb MP3 file corresponds to the wav parameter, which is kbps. this parameter is also called data bandwidth, and it is a concept of bandwidth in ADSL. Divide the bitrate by 8 to get the data rate of this wav, that is, 176.4kb/s. This indicates that the sampling rate for one second is 44.1 kHz, the sampling size is 16 bit, And the PCM-encoded audio signal in two channels requires a space of 176.4kb, which is about 10.34 m in 1 minute, this is unacceptable for most users, especially those who like to listen to music on their computers. To reduce disk usage, there are only two ways to reduce sampling metrics or compression. Reducing indicators is not desirable, so experts have developed various compression solutions. The purpose is different from the target market. The audio compression encoding quality and compression ratio are different. We will refer to them in the following articles. One thing is certain, they are all compressed.
Relationship between frequency and Sampling Rate
The sampling rate indicates the number of times the original signal is sampled per second. The common audio file sampling rate is 44.1 kHz. What does this mean? Assume that we have two sine wave signals, 20Hz and 20 kHz respectively, with a length of one second, corresponding to the lowest frequency and the highest frequency we can hear, what kind of result can we obtain when we sample the two signals at 40 kHz? The result is that the 20Hz signal is sampled for 40 KB/20 = 2000 times each time, while the 20 KB signal is only sampled twice each time. Obviously, At the same sampling rate, the information of low frequency is far more detailed than that of high frequency. This is also the reason why some audio enthusiasts accuse the CD of having insufficient digital sound. The 44.1khz sampling of the CD cannot ensure that the high-frequency signal is well recorded. To better record high-frequency signals, it seems that a higher sampling rate is required, so some friends use a 48 KHz sampling rate when capturing the CD audio. This is not desirable! In fact, this does not have any benefits for sound quality. For rail capture software, maintaining the same sampling rate as the 44.1khz provided by CD is one of the best quality assurance, rather than improving it. A high sampling rate is useful only when it is relative to a simulated signal. If the sampled signal is digital, do not try to increase the sampling rate.
Stream features
With the development of the network, people have put forward requirements for online music listening. Therefore, they also require that audio files can be read and played simultaneously, instead of reading all the files and then playing back them, in this way, you can achieve listening without downloading. It is also possible to play videos while encoding. This feature enables online live broadcast and sets up your own digital broadcast stations. 2. Description of Audio Acquisition Parameters in Android audiorecord the API for collecting audio in Android is Android. Media. audiorecord

Several parameters of the constructor are standard sound collection parameters.

The meanings of parameters are as follows:

Public audiorecord (INT audiosource, int samplerateinhz, int channelconfig, int audioformat, int buffersizeinbytes)

Since: API level 3

Class constructor.

Parameters

Audiosource

The recording source. See mediarecorder. audiosource for recording source definitions.

Audio Source: Where to collect audio. Here we certainly collect audio from the microphone, so the value of this parameter is mic

Samplerateinhz

The sample rate expressed in hertz. Examples of rates are (but not limited to) 44100,220 50 and 11025.

Sampling Rate: the sampling frequency of the audio. The number of samples per second. The higher the sampling rate, the higher the sound quality. The instances provided are 44100, 22050, 11025, but are not limited to these parameters. For example, low sampling rates such as 4000 and 8000 can be used to collect low-quality audio.

Channelconfig

Describes the configuration of the audio channels. See channel_in_mono and channel_in_stereo

Sound channel settings: Android supports dual-channel stereo and single-channel. Mono single channel, stereo

Audioformat

The format in which the audio data is represented. See encoding_pcm_16bit and encoding_pcm_8bit

Encoding standard and sampling size: Of course, the collected data uses PCM encoding (pulse code modulation encoding, that is, PCM encoding. PCM converts a continuously changing analog signal to a digital code by sampling, quantization, and encoding .) The sample size supported by Android is 16-bit or 8-bit. Of course, the larger the sampling size, the more information, the higher the sound quality. Now the mainstream sampling size is 16 bit, 8 bit is enough for low-quality voice transmission.

Buffersizeinbytes

The total size (in bytes) of the buffer where audio data is written to during the recording. new audio data can be read from this buffer in smaller chunks than this size. seegetminbuffersize (INT, Int, INT) to determine the minimum required buffer size for the successful creation of an audiorecord instance. using values smaller than getminbuffersize () will result in an initialization failure.

The size of the buffer required for data collection. If you do not know the minimum size, you can view it in getminbuffersize.

 

// Audio retrieval source private int audiosource = mediarecorder. audiosource. mic; // sets the audio sampling rate. 44100 is the current standard, but some devices still support 11025, 44100, Private Static int samplerateinhz =; // set the audio recording channel channel_in_stereo to dual-channel, and channel_configuration_mono to single-channel Private Static int channelconfig = audioformat. channel_configuration_mono; // Audio Data Format: PCM 16-bit each sample. Ensure device support. PCM 8-bit each sample. Not necessarily supported by devices. Private Static int audioformat = audioformat. encoding_pcm_16bit;
File file = new file (environment. getexternalstoragedirectory (). getabsolutepath () + "/test. PCM "); // Delete the recording file if (file. exists () file. delete (); // create the recording file try {file. createnewfile ();} catch (ioexception e) {Throw new illegalstateexception ("failed to create" + file. tostring ();} Try {// create a dataouputstream to write the audio data into the // saved file. fileoutputstream Fos = new fileoutputstream (File); // creates an accessable Byte File // create a new audiorecord object to record the audio. // obtain the minimum buffer size that meets the condition buffersizeinbytes = audiorecord. getminbuffersize (samplerateinhz, channelconfig, audioformat); // create the audiorecord object audiorecord = new audiorecord (audiosource, samplerateinhz, channelconfig, audioformat, buffersizeinbytes ); byte [] buffer = new byte [buffersizeinbytes]; audiorecord. startrecording (); isrecording = true; while (isrecording) {audiorecord. read (buffer, 0, buffersizeinbytes); FOS. write (buffer);} audiorecord. stop (); audiorecord. stop (); audiorecord. release (); // release the resource audiorecord = NULL; FOS. close ();} catch (throwable t) {log. E ("audiorecord", "Recording failed ");}
// The audio file = new file (environment. getexternalstoragedirectory (). getabsolutepath () + "/test. PCM "); fileinputstream in = NULL; try {In = new fileinputstream (File);} catch (filenotfoundexception e) {// todo auto-generated Catch Block E. printstacktrace ();} // obtain the minimum buffer size that meets the conditions buffersizeinbytes = audiorecord. getminbuffersize (samplerateinhz, channelconfig, audioformat); byte [] buffer = new Te [buffersizeinbytes]; int byteread = 0; audiotrack = new audiotrack (audiomanager. stream_music, samplerateinhz, channelconfig, audioformat, buffersizeinbytes, audiotrack. mode_stream); // audio audiotrack. play (); try {While (byteread = in. read (buffer ))! =-1) {system. out. write (buffer, 0, byteread); system. out. flush (); audiotrack. write (buffer, 0, buffersizeinbytes) ;}} catch (exception e) {log. E ("audiotrack", "playback failed ");}

Mobile development QQ group: 59516399

 

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.