ADPCM compression AlgorithmThe ADPCM (Adaptive differential Pulse Code modulation) is a lossy compression algorithm for 16bits (or 8bits or higher) sound waveform data, which saves 16bit of data per sample in the sound stream at 4bit Storage, so the compression ratio is 1:4. and the compression/decompression algorithm is very simple, so it is a low-space consumption, high-quality and high-efficiency sound to obtain a good way. The data file suffix that holds the sound is named. Most of the AUD is compressed with ADPCM.
ADPCM is mainly for continuous waveform data, saving is the change of waveform, to achieve the purpose of describing the entire waveform, because of its encoding and decoding process is very concise, listed in the back, I believe we can understand.
8bits sampled sound The human ear is acceptable, while the 16bit sampled sound can be considered high quality. The ADPCM algorithm compresses 16bit data from each sample to 4bit. It is important to note that if a stereo signal is to be compressed/decompressed, the sound signal is put together when sampled, and two channels need to be processed separately.
ADPCM Compression Process
First of all we think that the sound signal is zero-based, so we need to initialize two variables
int index=0,prev_sample=0;
The following loop will process the sound data stream in turn, noting that getnextsample () should get a 16bit sample, while Outputdata () can save the computed data, step_table[],index_ in the program Adjust[] attached to the back:
int index=0,prev_sample:=0;
While (there is data to be processed)
{
Cur_sample=getnextsample (); Get the current sampled data
Delta=cur_sample-prev_sample; Calculated and previous increments
if (delta<0) delta=-delta,sb=8; Take absolute value
else SB = 0; SB Saves the sign bit
Code = 4*delta/step_table[index]; Based on steptable[] get a value of 0-7
if (code>7) code=7; It describes the amount of change in sound intensity.
Index + = Index_adjust[code]; Adjust the number of next fetch steptable according to the sound intensity
if (index<0) index=0; The next time to get a more accurate description of the amount of change
else if (index>88) index=88;
Prev_sample=cur_sample;
Outputode (CODE|SB); Plus sign bits to save.
}
ADPCM Decompression Process
The compression is actually a reverse process of compression, the same getnextcode () should get a code, and Outputsample () can be decoded out of the sound signal to save. This code also uses the same setp_table[] and index_adjust () attached to the back:
int index=0,cur_sample=0;
While (there is data to be processed)
{
Code=getnextcode (); Get the next data
if (Code & 8)! = 0) sb=1 else sb=0;
code&=7; Separating code into data and symbols
Delta = (step_table[index]*code)/4+step_table[index]/8; And one of the following is to reduce the error
if (sb==1) Delta=-delta;
Cur_sample+=delta; Calculate the current waveform data
if (cur_sample>32767) output_sample (32767);
else if (cur_sample<-32768) output_sample (-32768);
else Output_sample (cur_sample);
Index+=index_adjust[code];
if (index<0) index=0;
if (index>88) index=88;
}
Schedule
Int Index_adjust[8] = { -1,-1,-1,-1,2,4,6,8}; int step_table[89] =
{
7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,
50,55,6 0,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,
408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,
2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,
10442,11487,12635,13899,15289,16818,18500,20350,22385,24623,27086,29794,32767
}