## ADPCM-XQ-NDS

The NDS contains a built-in ADPCM decoder.
However discussing the encoder architecture with Psy_Commando and reviewing notes on
the subject created by martin korth here http://problemkaputt.de/gbatek-ds-sound-notes.htm,
revealed that there appears to be a slight discrepancy between the standard IMA suggested
implementation and the actual implementation: this fork of ADPCM-XQ
seeks to solve that with an encoder dedicated to NDS audio.

During the step where the difference is added to the running prediction,
there is the possibility of the final sample value falling outside of the range
of a 16-bit signed integer, and so the NDS, like the reference implementation, 
handles this by clipping the final sample value to a good range. However this
range is different between the IMA reference and the NDS implementation. While
the reference clips the value to [-32768, 32767], utilizing the entire range of
the 16-bit signed int, the NDS decoder instead clips to [-32767, 32767],
preferring the symmetry.
ADPCM is cumulative, so this can lead to unexpected results. The main change
here is to modify this clipping to match that of the NDS.

Below is the original README for ADPCM-XQ:

////////////////////////////////////////////////////////////////////////////
//                           **** ADPCM-XQ ****                           //
//                  Xtreme Quality ADPCM Encoder/Decoder                  //
//                    Copyright (c) 2022 David Bryant.                    //
//                          All Rights Reserved.                          //
//      Distributed under the BSD Software License (see license.txt)      //
////////////////////////////////////////////////////////////////////////////

While very popular at the end of the last century, ADPCM is no longer a
common audio encoding format, and is certainly not recommended as a general
purpose encoder. However, it requires minimal CPU resources for decoding,
and so still is ideally suited for certain embedded games and applications
that contain canned audio samples.

This encoder combines two different techniques to achieve higher quality
than existing ADPCM encoders while remaining fully compatible with standard
decoders. The first is dynamic noise shaping, which shifts the quantization
noise up or down in frequency based on the spectrum of the source signal.
This technique is identical to the algorithm used in WavPack's lossy mode
and can make any audible quantization noise much less annoying (or, in some
cases, inaudible).

The other technique is "lookahead" in which the encoder exhaustively
searches ahead to find the optimum coding sequence based on future samples.
This process can reduce the quantization noise from about 1 to 6 dB (depending
on the source) and also reduces or eliminates the harmonic content in the
noise that sometimes plagues ADPCM. Unfortunately, at its maximum settings
this can be very slow, but this should be relatively irrelevant if the
encoder is being used to generate canned samples.

Adpcm-xq consists of two standard C files and builds with a single command
on most platforms. It has been designed with maximum portability in mind
and should work correctly even on 16-bit and big-endian architectures
(WAV files are of course always little-endian).

Linux:
% gcc -O2 *.c -o adpcm-xq

Darwin/Mac:
% clang -O2 *.c -o adpcm-xq

MS Visual Studio:
cl -O2 adpcm-xq.c adpcm-lib.c

Bugs:

1. Unknown RIFF chunk types are correctly parsed on input files, but are not
   passed to the output file.

2. The lookahead feature does not work for the last samples in an ADPCM
   block (i.e. it doesn't utilize samples in the _next_ block).

3. In some situations the lookahead can get very slow or seem to be stuck
   because it needs improved trellis pruning. However the default level 3
   should always be fine and then the user can simply try increasing levels
   until the time becomes untenable.

4. Pipes are not yet supported.

