[Scummvm-cvs-logs] SF.net SVN: scummvm:[38637] scummvm/trunk/engines/sword2/music.cpp

eriktorbjorn at users.sourceforge.net eriktorbjorn at users.sourceforge.net
Fri Feb 20 23:43:13 CET 2009


Revision: 38637
          http://scummvm.svn.sourceforge.net/scummvm/?rev=38637&view=rev
Author:   eriktorbjorn
Date:     2009-02-20 22:43:13 +0000 (Fri, 20 Feb 2009)

Log Message:
-----------
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.

Modified Paths:
--------------
    scummvm/trunk/engines/sword2/music.cpp

Modified: scummvm/trunk/engines/sword2/music.cpp
===================================================================
--- scummvm/trunk/engines/sword2/music.cpp	2009-02-20 22:32:56 UTC (rev 38636)
+++ scummvm/trunk/engines/sword2/music.cpp	2009-02-20 22:43:13 UTC (rev 38637)
@@ -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 @@
 
 	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:


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list