From cf6a5c0a04a23ac4b8044710f94dc224a1538a45 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Sat, 3 Jan 2004 19:12:23 +0000 Subject: cleanup svn-id: r12116 --- sword2/driver/d_sound.cpp | 94 ++++++++++++++++++++++------------------------- sword2/driver/d_sound.h | 3 +- 2 files changed, 45 insertions(+), 52 deletions(-) (limited to 'sword2/driver') diff --git a/sword2/driver/d_sound.cpp b/sword2/driver/d_sound.cpp index b061db823d..2ee80a0724 100644 --- a/sword2/driver/d_sound.cpp +++ b/sword2/driver/d_sound.cpp @@ -175,64 +175,58 @@ void MusicHandle::stop(void) { } int MusicHandle::readBuffer(int16 *buffer, const int numSamples) { + assert(numSamples > 0); int samples; - for (samples = 0; samples < numSamples && !endOfData(); samples++) - *buffer++ = read(); - return samples; -} - -int16 MusicHandle::read(void) { - uint8 in; - uint16 delta; - int16 out; - - if (!_streaming) - return 0; - - if (_firstTime) { - _lastSample = fpMus.readUint16LE(); - _filePos += 2; - _firstTime = false; - return _lastSample; - } // Assume the file handle has been correctly positioned already. - in = fpMus.readByte(); - delta = GetCompressedAmplitude(in) << GetCompressedShift(in); - - if (GetCompressedSign(in)) - out = _lastSample - delta; - else - out = _lastSample + delta; - - _filePos++; - _lastSample = out; - - if (_looping) { - if (_filePos >= _fileEnd) { - _firstTime = true; - _filePos = _fileStart; - fpMus.seek(_filePos, SEEK_SET); + for (samples = 0; samples < numSamples && !endOfData(); samples++) { + int16 out; + if (_firstTime) { + _lastSample = fpMus.readUint16LE(); + _filePos += 2; + _firstTime = false; + out = _lastSample; + } else { + uint8 in = fpMus.readByte(); + uint16 delta = GetCompressedAmplitude(in) << GetCompressedShift(in); + + if (GetCompressedSign(in)) + out = _lastSample - delta; + else + out = _lastSample + delta; + + _filePos++; + _lastSample = out; + + if (_looping) { + if (_filePos >= _fileEnd) { + _firstTime = true; + _filePos = _fileStart; + fpMus.seek(_filePos, SEEK_SET); + } + } else { + // Fade out at the end of the music, unless it already is. + if (_fileEnd - _filePos <= FADE_SAMPLES && _fading <= 0) + fadeDown(); + } + + if (_fading > 0) { + if (--_fading == 0) { + _streaming = false; + _looping = false; + } + out = (out * _fading) / FADE_SAMPLES; + } else if (_fading < 0) { + _fading++; + out = (out * (FADE_SAMPLES + _fading)) / FADE_SAMPLES; + } } - } else { - // Fade out at the end of the music, unless it already is. - if (_fileEnd - _filePos <= FADE_SAMPLES && _fading <= 0) - fadeDown(); - } - if (_fading > 0) { - if (--_fading == 0) { - _streaming = false; - _looping = false; - } - out = (out * _fading) / FADE_SAMPLES; - } else if (_fading < 0) { - _fading++; - out = (out * (FADE_SAMPLES + _fading)) / FADE_SAMPLES; + *buffer++ = out; } - return out; + return samples; } bool MusicHandle::endOfData(void) const { diff --git a/sword2/driver/d_sound.h b/sword2/driver/d_sound.h index 705d3f2baf..d333305dbd 100644 --- a/sword2/driver/d_sound.h +++ b/sword2/driver/d_sound.h @@ -63,8 +63,7 @@ public: void fadeUp(void); int32 play(const char *filename, uint32 musicId, bool looping); void stop(void); - virtual int readBuffer(int16 *buffer, const int numSamples); - int16 read(void); + int readBuffer(int16 *buffer, const int numSamples); bool endOfData(void) const; // This stream never 'ends' bool endOfStream(void) const { return false; } -- cgit v1.2.3