mirror of
https://github.com/libretro/scummvm.git
synced 2024-12-14 05:38:56 +00:00
Remove custom AiffAudioStream and replace it by the use of makeLoopingAudioStream and makeAIFFStream.
svn-id: r47165
This commit is contained in:
parent
c067bf053d
commit
549da55565
@ -44,121 +44,6 @@
|
||||
|
||||
namespace Sword1 {
|
||||
|
||||
class BaseAudioStream : public Audio::AudioStream {
|
||||
public:
|
||||
BaseAudioStream(Common::SeekableReadStream *source, bool loop);
|
||||
virtual ~BaseAudioStream();
|
||||
virtual int readBuffer(int16 *buffer, const int numSamples);
|
||||
virtual bool isStereo() const { return _isStereo; }
|
||||
virtual bool endOfData() const { return (_samplesLeft == 0); }
|
||||
virtual int getRate() const { return _rate; }
|
||||
protected:
|
||||
Common::SeekableReadStream *_sourceStream;
|
||||
uint8 *_sampleBuf;
|
||||
uint32 _rate;
|
||||
bool _isStereo;
|
||||
uint32 _samplesLeft;
|
||||
uint16 _bitsPerSample;
|
||||
bool _loop;
|
||||
|
||||
virtual void rewind() = 0;
|
||||
virtual void reinit(int size, int rate, byte flags);
|
||||
};
|
||||
|
||||
BaseAudioStream::BaseAudioStream(Common::SeekableReadStream *source, bool loop) {
|
||||
_sourceStream = source;
|
||||
_sampleBuf = (uint8*)malloc(SMP_BUFSIZE);
|
||||
|
||||
_samplesLeft = 0;
|
||||
_isStereo = false;
|
||||
_bitsPerSample = 16;
|
||||
_rate = 22050;
|
||||
_loop = loop;
|
||||
}
|
||||
|
||||
BaseAudioStream::~BaseAudioStream() {
|
||||
free(_sampleBuf);
|
||||
}
|
||||
|
||||
void BaseAudioStream::reinit(int size, int rate, byte flags) {
|
||||
_isStereo = (flags & Audio::Mixer::FLAG_STEREO) != 0;
|
||||
_rate = rate;
|
||||
assert(size <= (_sourceStream->size() - _sourceStream->pos()));
|
||||
_bitsPerSample = ((flags & Audio::Mixer::FLAG_16BITS) != 0) ? 16 : 8;
|
||||
_samplesLeft = (size * 8) / _bitsPerSample;
|
||||
if ((_bitsPerSample != 16) && (_bitsPerSample != 8))
|
||||
error("BaseAudioStream: unknown sound type");
|
||||
}
|
||||
|
||||
int BaseAudioStream::readBuffer(int16 *buffer, const int numSamples) {
|
||||
int retVal = 0;
|
||||
|
||||
while (retVal < numSamples && _samplesLeft > 0) {
|
||||
int samples = MIN((int)_samplesLeft, numSamples - retVal);
|
||||
retVal += samples;
|
||||
_samplesLeft -= samples;
|
||||
while (samples > 0) {
|
||||
int readBytes = MIN(samples * (_bitsPerSample >> 3), SMP_BUFSIZE);
|
||||
_sourceStream->read(_sampleBuf, readBytes);
|
||||
if (_bitsPerSample == 16) {
|
||||
samples -= (readBytes / 2);
|
||||
memcpy(buffer, _sampleBuf, readBytes);
|
||||
buffer += (readBytes / 2);
|
||||
} else {
|
||||
samples -= readBytes;
|
||||
int8 *src = (int8*)_sampleBuf;
|
||||
while (readBytes--)
|
||||
*buffer++ = (int16)*src++ << 8;
|
||||
}
|
||||
}
|
||||
|
||||
if (!_samplesLeft && _loop) {
|
||||
rewind();
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
||||
class AiffAudioStream : public BaseAudioStream {
|
||||
public:
|
||||
AiffAudioStream(Common::SeekableReadStream *source, bool loop);
|
||||
virtual int readBuffer(int16 *buffer, const int numSamples);
|
||||
private:
|
||||
void rewind();
|
||||
};
|
||||
|
||||
AiffAudioStream::AiffAudioStream(Common::SeekableReadStream *source, bool loop) : BaseAudioStream(source, loop) {
|
||||
rewind();
|
||||
|
||||
if (_samplesLeft == 0)
|
||||
_loop = false;
|
||||
}
|
||||
|
||||
void AiffAudioStream::rewind() {
|
||||
int rate, size;
|
||||
byte flags;
|
||||
|
||||
_sourceStream->seek(0);
|
||||
|
||||
if (Audio::loadAIFFFromStream(*_sourceStream, size, rate, flags)) {
|
||||
reinit(size, rate, flags);
|
||||
}
|
||||
}
|
||||
|
||||
int AiffAudioStream::readBuffer(int16 *buffer, const int numSamples) {
|
||||
int retVal = BaseAudioStream::readBuffer(buffer, numSamples);
|
||||
|
||||
if (_bitsPerSample == 16) {
|
||||
for (int i = 0; i < retVal; i++) {
|
||||
buffer[i] = (int16)READ_BE_UINT16(buffer + i);
|
||||
}
|
||||
}
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
// This means fading takes 3 seconds.
|
||||
#define FADE_LENGTH 3
|
||||
|
||||
@ -221,7 +106,7 @@ bool MusicHandle::play(const char *fileBase, bool loop) {
|
||||
if (!_audioSource) {
|
||||
sprintf(fileName, "%s.aif", fileBase);
|
||||
if (_file.open(fileName))
|
||||
_audioSource = new AiffAudioStream(&_file, loop);
|
||||
_audioSource = Audio::makeLoopingAudioStream(Audio::makeAIFFStream(_file), loop ? 0 : 1);
|
||||
}
|
||||
|
||||
if (!_audioSource)
|
||||
|
Loading…
Reference in New Issue
Block a user