If bit 1 is set, add (step / 2 = step SHR 1) to diff. Thus, if bit 2 of the nibble is set, add step to diff. Since a nibble only carries 4 bits, and those 4 bits are a sign/magnitude number, there are only 3 bits of magnitude information. The first part of the equation can also be simplified. The step / 8 calculation can be expressed as a bit shift right by 3 (step SHR 3). However, some numerical manipulations reveal some useful simplifications:ĭiff = ((step * nibble) + (step / 2)) / 4 It is possible for these values to outrange which could cause undesirable program behavior if unchecked.ĭiff = ((sign/mag.)nibble + 0.5) * step / 4Īt first glance, it appears that this calculation requires floating point operations and an arbitrary (not power-of-2) multiplication. Regarding the step index and predictor calculations: Be sure to saturate the computed step index between 0 and 88 (table limits) and the predictor between -3277 (signed 16-bit number range). Step_index = step_index + ima_index_table Transform each nibble in the stream into a signed, 16-bit PCM sample using the following process: Where each nibble represents both a table index and a sign/magnitude number during the decoding process.
For this discussion, imagine the IMA bitstream as a series of nibbles representing a single audio channel: The specific data format will dictate whether the stream is decoded top nibble first or bottom nibble first, and whether there is stereo interleaving within the IMA nibbles. This means that each byte represents 2 IMA nibbles. The encoded IMA bitstream is comprised of a series of 4-bit nibbles. step: This variable is initialized to ima_step_table.step index: Similar to the initial predictor, this variable is initialized from the data chunk preamble or set to 0 at the start of the decoding process.predictor: This is either initialized from the data chunk preamble specified in the format or is initialized to 0 at the start of the decoding process.To decode IMA ADPCM, initialize 3 variables: