IMA-ADPCM algorithm

zhaozj2021-02-08  267

IMA-ADPCM (ADPCM Adaptive Differential Pulse Code Modulation) is a lossless compression algorithm for 16bit (or higher?) Sound waveform data, which stores 16bit data in the sound stream at 4BIT, so Compression ratio 1: 4. The compression / decompression algorithm is very simple, so it is a good way to get low space consumption, high quality sound. The famous Westwood uses this technology in many games, Dune II, C & C, RA, etc., save the sound data file suffix. Most of them use IMA-ADPCM compressed. (However, Westwood's game data files have been packaged, these small documents are all in one. MIX file package, about Unlock the .MIX file package, see http://www.geocities.com/siliconvalley/8682)

The ADPCM is mainly for continuous waveform data, saved is the change of the waveform, to achieve the purpose of describing the entire waveform. This article does not want to introduce the principle of the ADPCM algorithm in detail, those friends who have high mathematics, and those who have higher mathematics can study themselves. Cloud wind mathematics horse tiger, here is not clear, but its code and decoding process is very simple, listed behind, I believe everyone can understand.

First give a lesson for a store that is not familiar with the sound signal, you don't want to see it, just jump, ^ _ ^: The sound used in the normal game has two different nature, one is waveform data, it is recorded by prior sound sampling. When sampling, the sound intensity of each sampling is generally recorded at a frequency (8kHz ~ 44.4 kHz) per second (8kHz ~ 44.4 kHz), when playing, in the same frequency, trigger a speaker according to the strong change of the sample sound, The sound is reproduced. If you will sampling the data bid on the coordinate paper, it will find a waveform curve. If the sound signal is strong, the sound signal is weak is 256, which is the 8bit sample of the 8bit, if divided into 65536 Level, 16bit sampled; the other is the MIDI class, it is recorded in advance, and the data stream is still recorded according to a certain frequency, but not tens of thousands per second. Approximately a few Hz to dozens HZ, a few instruments can be placed in a sound card, and the sound card is used to become a waveform signal.

The 8bit sampled sound man ear is acceptable, such as Win95 launched music, and 16bit sampled sound can be high, and it is also used in modern games. (Take more signs of sound intensity points. " It is usually increasing the sampling frequency to improve the sound quality) The ADPCM algorithm can compress the 16bit data obtained from each sample to 4BIT ;-) Need to note that if you want to compress / press the stereo signal, please pay attention to the sample, sound The signal is placed together, and the two channels need to be handled separately. OK, listed below, please taste fine:

IMA-ADPCM compression process

First we think that the sound signal is started from zero, then it needs to initialize two variables.

INDEX = 0, prev_sample: = 0;

The following loop will process the sound data stream in turn, pay attention to the getNextSample () should get a 16bit sampling data, and OutputData () can save the calculated data, STEP_TABLE [], index_adjust [] behind:

INDEX = 0, prev_sample: = 0;

While (also data to process) {

Cur_sample = getNextSample (); // Get current sampling data

Delta = cur_sample-prev_sample; // calculate and the increment

IF (delta <0) Delta = -delta, SB = 8;

Else SB = 0; // SB Save is symbolic bit

Code = 4 * delta / step_table [index]; / / According to Steptable [] Gets a value of 0 ~ 7

IF (Code> 7) Code = 7; // It describes the amount of change in sound intensity index = index_adjust [code]; / / According to the sound intensity adjustment Next time Steptable serial number

IF (INDEX <0) index = 0; // Easy to get more accurate change in the next time

Else IF (INDEX> 88) INDEX = 88;

prev_sample = cur_sample;

Outputode (Code | SB); // Plus the symbolic position to save

}

IMA-ADPCM decompression process

The actual compression is actually a reverse process of compression, and the getNextCode () which is the same should be encoded, and OutputSample () can save the decoded sound signal. This code also uses the same setp_table [] and index_adjust. Attached to:

INDEX = 0, cur_sample: = 0;

While (also data to process) {

Code = getNextCode (); // Get the next data

IF ((Code & 8)! = 0) SB = 1 else Sb = 0;

Code & = 7; // Separate CODE as data and symbols

Delta = (Step_Table [index] * code) / 4 step_table [index] / 8;

// The next item is to reduce the error

IF (SB == 1) Delta = -delta;

Cur_sample = delta; / / Calculate 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

INDEX_ADJUST [8] = {-1, -1, -1, -1, 2, 4, 6, 8};

INT Step_Table [89] = {7, 8, 9, 10, 11, 12, 13, 14, 16, 28, 31, 34, 37, 41, 45, 50, 55 , 60, 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, 3328, 3660, 4358, 5894, 6484 , 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767};

About Westwood .aud file, the structure is relatively simple, here is here, interested in writing a program that handles the AUD file ;-) The 8bit sound compressive algorithm is still unknown, but the most extensive 16bit sound is used IMA-ADPCM compresses, each AUD file has a file header, the structure is as follows: struct {

Unsigned short int samplespespec; // frequency

Long int size; // Remove the size of the file header

long int outsize; // Output data size (usually 4 times)

Unsigned char flags; // bits 0 Describe if stereo, bit 1 describes whether or not 16 bit

Unsigned char type; // 1 = ww compression, 99 = IMA ADPCM

}

The sound signal of the AUD file is stored in blocks, about 512 bytes per piece, no piece has a block structure:

Struct {

Unsigned short int size; // Compressed data size

UNSIGNED short int out, // output data size (usually 4 times)

Long int id; // is always 0x0000DEAF

}

This article refers to the AUD file format written by Vladan Bato. You can go to his website http://www.geocities.com/siliconvalley/8682 to find the original text and his written AUD, WAV conversion program. In addition, Allegro's enthusiasts may want to think Join the AUD support (Allegro 3.1 new PLUG-IN support, add new file types very convenient), do http://www.alphalt.com.au/~tjaden, here there is a completed AUD support library.