Skip to content

Commit

Permalink
TOON: Tidy up decodeADPCM.
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzie committed Jun 6, 2011
1 parent 506bd3c commit 7c4f772
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
67 changes: 32 additions & 35 deletions engines/toon/audio.cpp
Expand Up @@ -232,8 +232,8 @@ AudioStreamInstance::AudioStreamInstance(AudioManager *man, Audio::Mixer *mixer,
_mixer = mixer;
_compBuffer = NULL;
_bufferOffset = 0;
_lastADPCMval1 = 0;
_lastADPCMval2 = 0;
_lastSample = 0;
_lastStepIndex = 0;
_file = stream;
_fadingIn = false;
_fadingOut = false;
Expand Down Expand Up @@ -307,8 +307,8 @@ bool AudioStreamInstance::readPacket() {
if (_looping) {
_file->seek(8);
_currentReadSize = 8;
_lastADPCMval1 = 0;
_lastADPCMval2 = 0;
_lastSample = 0;
_lastStepIndex = 0;
} else {
_bufferSize = 0;
stopNow();
Expand Down Expand Up @@ -342,52 +342,49 @@ bool AudioStreamInstance::readPacket() {
void AudioStreamInstance::decodeADPCM(uint8 *comp, int16 *dest, int32 packetSize) {
debugC(5, kDebugAudio, "decodeADPCM(comp, dest, %d)", packetSize);

// standard IMA ADPCM decoding
int32 numSamples = 2 * packetSize;
int32 v18 = _lastADPCMval1;
int32 v19 = _lastADPCMval2;
int32 samp = _lastSample;
int32 stepIndex = _lastStepIndex;
for (int32 i = 0; i < numSamples; i++) {
uint8 comm = *comp;
bool isOddSample = (i & 1);

int32 v29 = i & 1;
int32 v30;
if (v29 == 0)
v30 = comm & 0xf;
uint8 code;
if (!isOddSample)
code = comm & 0xf;
else
v30 = (comm & 0xf0) >> 4;
code = (comm & 0xf0) >> 4;

int32 v31 = v30 & 0x8;
int32 v32 = v30 & 0x7;
int32 v33 = Audio::Ima_ADPCMStream::_imaTable[v19];
int32 v34 = v33 >> 3;
if (v32 & 4)
v34 += v33;
uint8 sample = code & 0x7;

if (v32 & 2)
v34 += v33 >> 1;
int32 step = Audio::Ima_ADPCMStream::_imaTable[stepIndex];
int32 E = step >> 3;
if (sample & 4)
E += step;
if (sample & 2)
E += step >> 1;
if (sample & 1)
E += step >> 2;

if (v32 & 1)
v34 += v33 >> 2;
stepIndex += Audio::ADPCMStream::_stepAdjustTable[sample];
stepIndex = CLIP<int32>(stepIndex, 0, ARRAYSIZE(Audio::Ima_ADPCMStream::_imaTable) - 1);

v19 += Audio::ADPCMStream::_stepAdjustTable[v32];
v19 = CLIP<int32>(v19, 0, ARRAYSIZE(Audio::Ima_ADPCMStream::_imaTable) - 1);

if (v31)
v18 -= v34;
if (code & 0x8)
samp -= E;
else
v18 += v34;
samp += E;

if (v18 > 32767)
v18 = 32767;
else if (v18 < -32768)
v18 = -32768;
samp = CLIP<int32>(samp, -32768, 32767);

*dest = v18;
comp += v29;
*dest = samp;
if (isOddSample)
comp++;
dest++;
}

_lastADPCMval1 = v18;
_lastADPCMval2 = v19;
_lastSample = samp;
_lastStepIndex = stepIndex;
}

void AudioStreamInstance::play(bool fade, Audio::Mixer::SoundType soundType) {
Expand Down
4 changes: 2 additions & 2 deletions engines/toon/audio.h
Expand Up @@ -84,8 +84,8 @@ class AudioStreamInstance : public Audio::AudioStream {
Audio::SoundHandle _handle;
Audio::Mixer::SoundType _soundType;
Audio::Mixer *_mixer;
int32 _lastADPCMval1;
int32 _lastADPCMval2;
int32 _lastSample;
int32 _lastStepIndex;
bool _stopped;
AudioManager *_man;
int32 _totalSize;
Expand Down

0 comments on commit 7c4f772

Please sign in to comment.