ADPCM (adaptive Differential Pulse Code Modulation) is a Lossy Compression Algorithm for 16 bits (or 8bits or higher) Sound waveform data, it stores the 16-bit data sampled in the audio stream in 4 bits, so the compression ratio is. in addition, the compression/decompression algorithm is very simple, so it is a good way to obtain high-quality and efficient sound with low space consumption. The data file with the suffix ". Aud" for storing sound is mostly compressed by ADPCM.
ADPCM mainly targets continuous waveform data and stores waveform changes to describe the entire waveform. Because of its concise coding and decoding process, it is listed behind and I believe everyone can understand it.
8 bits sampling sound ears are barely acceptable, while 16 bit sampling sound can be regarded as high quality. The ADPCM Algorithm can compress 16-bit data from each sample to 4-bit data. Note that if you want to compress or decompress a stereo signal, the sound signal is put together during sampling, and the two channels must be processed separately.
ADPCM compression process
First, we think that the sound signal starts from scratch, so we need to initialize two variables.
Int Index = 0, prev_sample = 0;
The following loop processes audio data streams in sequence. Note that getnextsample () should obtain a 16-bit sample data, while outputdata () can save the calculated data, the step_table [] and index_adjust [] Used in the program are appended to the following:
Int Index = 0, prev_sample: = 0;
While (there is data to be processed)
{
Cur_sample = getnextsample (); // obtain the current sample data
Delta = cur_sample-prev_sample; // calculate the sum of the previous Increment
If (delta <0) Delta =-delta, SB = 8; // obtain the absolute value
Else sb = 0; // Sb stores the symbol bit
Code = 4 * Delta/step_table [Index]; // obtain a value ranging from 0 to 7 Based on steptable []
If (code> 7) code = 7; // It describes the variation of Sound Intensity
Index + = index_adjust [Code]; // adjust the sequence number of the Next Step table based on the sound intensity.
If (index <0) Index = 0; // you can get a more precise description of the change volume next time.
Else if (index> 88) Index = 88;
Prev_sample = cur_sample;
Outputode (Code | SB); // save it with the symbol bit
}
ADPCM decompression process
Compression is actually an inverse process of compression. The getnextcode () must be encoded, while outputsample () can save the decoded sound signal. This Code also uses the same setp_table [] and index_adjust () to be appended with the following code:
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; // separate the code into data and symbols
Delta = (step_table [Index] * Code)/4 + step_table [Index]/8; // The following item is added 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;
}
Appendix
Int index_adjust [8] = {-1,-1,-1,-, 8 };
Int step_table [89] =
{
,
50, 55, 60, 66, 107,118,130,143,157,173,190,209,230,253,279,307,337,371, 80,
408,449,494,544,598,658,724,796,876,963,106,
2272,2499, 2749,3024, 3327,3660, 4026,4428, 20171,5358, 5894,6484, 7132,7845, 8630,9493,
10442,11487, 12635,13899, 15289,16818, 18500,20350, 22385, 24623, 27086,29794, 32767
}