Next: 5 Operating system specific Up: 4 Advanced topics Previous: Using µ-law compression

# 4.3 Using ADPCM compression

ADPCM streams provide a space-effective way of storing long sections of audio with a fairly good sound quality. Although ADPCM streams have lower sound quality than uncompressed 16-bit ones, they do sound better than 8-bit ones, and, as they only use effectively 4-bit samples, they provide 1:4 compression to the sound. This section descibes how ADPCM streams are encoded and used with MIDAS Digital Audio System.

## 4.3.1 Encoding ADPCM streams

Encoding ADPCM streams is fairly simple. The `tools/` directory in the MIDAS distribution contains directories for each supported platform, and these directories contain a program called `adpcm`. This program can be used to encode 16-bit streams into ADPCM ones, and decode ADPCM streams back to 16-bit. The syntax is similar to the µ-law encoder, although a bit more complicated. To encode a 16-bit stream into ADPCM, use:

            adpcm e input-file-name output-file-name channels frame-length

Where channels is the number of channels in the stream (1 for mono, 2 for stereo) and frame-length the ADPCM frame length in bytes, including the frame header. As ADPCM sample data is adaptative delta encoded, it is normally impossible to start decoding an ADPCM stream from the middle. To get around this problem, MIDAS divides the ADPCM stream into ``frames'', and is able to start decoding from the beginning of any frame.

The frame length you should use depends on the needs of your application. If your application will always play the streams from beginning to end, any value will do -- 1024 is a reasonable choice. However, if stream playback can start from the middle of the stream, you should consider how the stream is accessed. In particular, if the stream is read in blocks of a set number of bytes, the frame length should be equal to the block size.

If you wish to make the ADPCM frames of a given length of time, remember that each ADPCM sample is 4 bits. Therefore, one byte of ADPCM data will contain data for two mono samples or one stereo sample. The ADPCM frame header is 9 bytes long for mono streams and 12 bytes long for stereo ones. Therefore, to get 59ms long frames for a stereo stream played at 22050Hz, the frame length should be 453 bytes.

To decode an ADPCM stream back to a 16-bit one, use:

            adpcm d input-file-name output-file-name channels frame-length

Like with the µ-law encoder/decoder, the files should contain just raw sample data, with no headers.

## 4.3.2 Using ADPCM streams

ADPCM streams are used just like other streams in MIDAS Digital Audio System. Simply pass MIDAS_SAMPLE_ADPCM_MONO or MIDAS_SAMPLE_ADPCM_STEREO as the sample type to the MIDAS stream playback functions, and everything will work normally. ADPCM sample data can only be used for streams. If you are feeding the stream data manually, however, remember that playback can only start from the beginning of an ADPCM frame.

Next: 5 Operating system specific Up: 4 Advanced topics Previous: Using µ-law compression