Instead of reading an entire compressed sound into a memory stream, use a

slightly extended SeekableSubReadStream to stream the sound from a file instead.
This change is experimental, so it should almost certainly not go into 0.13.

svn-id: r38637
This commit is contained in:
Torbjörn Andersson 2009-02-20 22:43:13 +00:00
parent dbb81ab516
commit 478257f624

View File

@ -49,6 +49,39 @@
namespace Sword2 {
// This class behaves like SeekableSubReadStream, except it remembers where the
// previous read() or seek() took it, so that it can continue from that point
// the next time. This is because we're frequently streaming two pieces of
// music from the same file.
class SafeSubReadStream : public Common::SeekableSubReadStream {
protected:
uint32 _previousPos;
public:
SafeSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, bool disposeParentStream);
virtual uint32 read(void *dataPtr, uint32 dataSize);
virtual bool seek(int32 offset, int whence = SEEK_SET);
};
SafeSubReadStream::SafeSubReadStream(SeekableReadStream *parentStream, uint32 begin, uint32 end, bool disposeParentStream)
: SeekableSubReadStream(parentStream, begin, end, disposeParentStream) {
_previousPos = 0;
}
uint32 SafeSubReadStream::read(void *dataPtr, uint32 dataSize) {
uint32 result;
SeekableSubReadStream::seek(_previousPos);
result = SeekableSubReadStream::read(dataPtr, dataSize);
_previousPos = pos();
return result;
}
bool SafeSubReadStream::seek(int32 offset, int whence) {
bool result = SeekableSubReadStream::seek(offset, whence);
_previousPos = pos();
return result;
}
static Audio::AudioStream *makeCLUStream(Common::File *fp, int size);
static Audio::AudioStream *getAudioStream(SoundFileHandle *fh, const char *base, int cd, uint32 id, uint32 *numSamples) {
@ -147,27 +180,24 @@ static Audio::AudioStream *getAudioStream(SoundFileHandle *fh, const char *base,
fh->file.seek(pos, SEEK_SET);
Common::MemoryReadStream *tmp = 0;
SafeSubReadStream *tmp = 0;
switch (fh->fileType) {
case kCLUMode:
return makeCLUStream(&fh->file, enc_len);
#ifdef USE_MAD
case kMP3Mode:
tmp = fh->file.readStream(enc_len);
assert(tmp);
tmp = new SafeSubReadStream(&fh->file, pos, pos + enc_len, false);
return Audio::makeMP3Stream(tmp, true);
#endif
#ifdef USE_VORBIS
case kVorbisMode:
tmp = fh->file.readStream(enc_len);
assert(tmp);
tmp = new SafeSubReadStream(&fh->file, pos, pos + enc_len, false);
return Audio::makeVorbisStream(tmp, true);
#endif
#ifdef USE_FLAC
case kFlacMode:
tmp = fh->file.readStream(enc_len);
assert(tmp);
tmp = new SafeSubReadStream(&fh->file, pos, pos + enc_len, false);
return Audio::makeFlacStream(tmp, true);
#endif
default: