# Monkey's Audio

- Extensions: ape
- Website: http://www.monkeysaudio.com/
- Samples: http://samples.mplayerhq.hu/A-codecs/lossless/ (luckynight.ape)
- Theory/Whitepaper: http://www.monkeysaudio.com/theory.html

Monkey's Audio is a lossless audio coding algorithm.

## General Details

Header (all data is little-endian):

4 bytes - 'MAC ' 2 bytes - version (3920 == version 3.92) 2 bytes - compression level

Compression levels:

- 1000 - fast
- 2000 - normal
- 3000 - high
- 4000 - extra high
- 5000 - insane

The rest of header data depends on file version but all of them contain information about sound parameters (number of channels, sampling rate) and internal structure (number of frames, seek table and even possible WAV header).

Audio data is packed with channel decorrelation, applying 1-3 IIR filters of different order and range coding of residual.

### General Decoding Process

read one frame of data unpack array of delta values from it if this is stereo signal then unpack the second array of delta values for each array apply all IIR filters onto values if this is stereo then do channel correlation output data

### Residual Coding

Each value is coded by dividing into some parts (parameter *k* is calculated based on previous coded values and splitting it into *k* lower bits and higher bits) and coding each part separately with range coder.

### IIR Filtering

This is relatively easy but computational-hungry part

for each value in = delta[0] t = 0 for i=0..order t += delta[-order + i] * par[i] if in < 0 for i=0..order par[i] -= delta[i] if in > 0 for i=0..order par[i] += delta[i] out = in + t correct delta[] array - different for many versions

### Channel Correlation

In general case restoring of channels L and R from decorrelated values X and Y is done by this formula:

R = X - Y/2 L = R + Y