# Microsoft ADPCM

- Audio ID: 0x0002
- FOURCC: 'm','s',0x00,0x02
- Company: Microsoft

This format is Microsoft's own custom variation of the ADPCM concept.

MS ADPCM is organized in blocks. Each block has a preamble and a series of coded ADPCM nibbles. The total number of bytes in an individual ADPCM block is obtained through the nBlockAlign field of a media file's WAVEFORMATEX data structure.

A monaural block begins with the following preamble:

byte 0 block predictor (should be in the range [0..6]) bytes 1-2 initial delta bytes 3-4 sample 1 bytes 5-6 sample 2

The initial delta and both samples are signed numbers (so take sign extension into account). The block predictor value is used as an index into two adaptation coefficient tables in order to initialize two coefficients, coeff1 and coeff2.

The initial 2 samples from the block preamble are sent directly to the output. Sample 2 is first, then sample 1. The remaining samples are decoded from the ADPCM nibbles, which comprise the rest of the bytes in the block. The bytes are decoded from the upper nibble (bits 7-4) first, then the lower nibble. For each nibble:

- predictor = ((sample1 * coeff1) + (sample2 * coeff2)) / 256
- predictor += (signed)nibble * delta (note that nibble is 2's complement)
- clamp predictor within signed 16-bit range
- PCM sample = predictor
- send PCM sample to the output
- shuffle samples: sample 2 = sample 1, sample 1 = calculated PCM sample
- compute next adaptive scale factor: delta = (AdaptationTable[nibble] * delta) / 256
- saturate delta to lower bound of 16

For stereo data, the block preamble stores interleaved initialization values for the left and right channels:

byte 0 left channel block predictor (should be [0..6]) byte 1 right channel block predictor (should be [0..6]) bytes 2-3 left channel initial idelta bytes 4-5 right channel initial idelta bytes 6-7 left channel sample 1 bytes 8-9 right channel sample 1 bytes 10-11 left channel sample 2 bytes 12-13 right channel sample 2

Following the preamble, the left and right ADPCM samples are interleaved within each byte. The upper nibble (bits 7-4) contains the left channel ADPCM code and the lower nibble contains the right channel ADPCM code.

The following tables define the values used to decode MS ADPCM data:

int AdaptationTable [] = { 230, 230, 230, 230, 307, 409, 512, 614, 768, 614, 512, 409, 307, 230, 230, 230 } ; int AdaptCoeff1 [] = { 256, 512, 0, 192, 240, 460, 392 } ; int AdaptCoeff2 [] = { 0, -256, 0, 64, 0, -208, -232 } ;