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

thebluegr at users.sourceforge.net thebluegr at users.sourceforge.net
Sat Jul 14 15:41:06 CEST 2007


Revision: 28069
          http://scummvm.svn.sourceforge.net/scummvm/?rev=28069&view=rev
Author:   thebluegr
Date:     2007-07-14 06:41:03 -0700 (Sat, 14 Jul 2007)

Log Message:
-----------
Cleanup of the digital music code. Now all kinds of music (uncompressed and compressed) are handled by the same class, DigitalMusicInputStream

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

Modified: scummvm/trunk/engines/saga/music.cpp
===================================================================
--- scummvm/trunk/engines/saga/music.cpp	2007-07-14 13:19:40 UTC (rev 28068)
+++ scummvm/trunk/engines/saga/music.cpp	2007-07-14 13:41:03 UTC (rev 28069)
@@ -38,17 +38,9 @@
 
 #define BUFFER_SIZE 4096
 
-// I haven't decided yet if it's a good idea to make looping part of the audio
-// stream class, or if I should use a "wrapper" class, like I did for Broken
-// Sword 2, to make it easier to add support for compressed music... but I'll
-// worry about that later.
-// Update by md5: Apparently, it wasn't a good idea. Compressed digital music
-// is handled outside of this class, so looping does not work for compressed
-// digital music yet
-// TODO/FIXME: Add looping support for compressed digital music
-
-class RAWInputStream : public Audio::AudioStream {
+class DigitalMusicInputStream : public Audio::AudioStream {
 private:
+	Audio::AudioStream *_stream;
 	ResourceContext *_context;
 	Common::File *_file;
 	uint32 _filePos;
@@ -67,7 +59,8 @@
 	}
 
 public:
-	RAWInputStream(SagaEngine *vm, ResourceContext *context, uint32 resourceId, bool looping, uint32 loopStart);
+	DigitalMusicInputStream(SagaEngine *vm, ResourceContext *context, uint32 resourceId, bool looping, uint32 loopStart);
+	~DigitalMusicInputStream();
 
 	int readBuffer(int16 *buffer, const int numSamples);
 
@@ -76,19 +69,64 @@
 	int getRate() const	{ return _musicInfo->frequency; }
 };
 
-RAWInputStream::RAWInputStream(SagaEngine *vm, ResourceContext *context, uint32 resourceId, bool looping, uint32 loopStart)
+DigitalMusicInputStream::DigitalMusicInputStream(SagaEngine *vm, ResourceContext *context, uint32 resourceId, bool looping, uint32 loopStart)
 	: _context(context), _finished(false), _looping(looping), _bufferEnd(_buf + BUFFER_SIZE) {
 
 	ResourceData * resourceData;
+	byte compressedHeader[10];
+	GameSoundTypes soundType;
 
 	resourceData = vm->_resource->getResourceData(context, resourceId);
 	_file = context->getFile(resourceData);
 	_musicInfo = vm->getMusicInfo();
 
 	if (_musicInfo == NULL) {
-		error("RAWInputStream() wrong musicInfo");
+		error("DigitalMusicInputStream() wrong musicInfo");
 	}
 
+	_stream = NULL;
+
+	if (vm->getFeatures() & GF_COMPRESSED_SOUNDS) {
+		// Read compressed header to determine compression type
+		_file->seek((long)resourceData->offset, SEEK_SET);
+		_file->read(compressedHeader, 9);
+
+		if (compressedHeader[0] == char(0)) {
+			soundType = kSoundMP3;
+		} else if (compressedHeader[0] == char(1)) {
+			soundType = kSoundOGG;
+		} else if (compressedHeader[0] == char(2)) {
+			soundType = kSoundFLAC;
+		}
+
+		switch (soundType) {
+#ifdef USE_MAD
+			case kSoundMP3:
+				debug(1, "Playing MP3 compressed digital music");
+				_stream = Audio::makeMP3Stream(_file, resourceData->size);
+				break;
+#endif
+#ifdef USE_VORBIS
+			case kSoundOGG:
+				debug(1, "Playing OGG compressed digital music");
+				_stream = Audio::makeVorbisStream(_file, resourceData->size);
+				break;
+#endif
+#ifdef USE_FLAC
+			case kSoundFLAC:
+				debug(1, "Playing FLAC compressed digital music");
+				_stream = Audio::makeFlacStream(_file, resourceData->size);
+				break;
+#endif
+			default:
+				// Unknown compression
+				error("Trying to play a compressed digital music, but the compression is not known");
+				break;
+		}
+
+		resourceData->offset += 9;	// Skip compressed header
+	}
+
 	// Determine the end position
 	_filePos = resourceData->offset;
 	_endPos = _filePos + resourceData->size;
@@ -100,10 +138,23 @@
 	refill();
 }
 
-int RAWInputStream::readBuffer(int16 *buffer, const int numSamples) {
+DigitalMusicInputStream::~DigitalMusicInputStream() {
+	delete _stream;
+}
+
+int DigitalMusicInputStream::readBuffer(int16 *buffer, const int numSamples) {
+	// TODO/FIXME: Add looping support for compressed digital music
+	//if (!_looping && _stream != NULL)
+	if (_stream != NULL)
+		return _stream->readBuffer(buffer, numSamples);
+
 	int samples = 0;
 	while (samples < numSamples && !eosIntern()) {
-		const int len = MIN(numSamples - samples, (int) (_bufferEnd - _pos));
+		int len = 0;
+		if (_stream != NULL)
+			len = _stream->readBuffer(buffer, numSamples);
+		else
+			len = MIN(numSamples - samples, (int) (_bufferEnd - _pos));
 		memcpy(buffer, _pos, len * 2);
 		buffer += len;
 		_pos += len;
@@ -115,7 +166,7 @@
 	return samples;
 }
 
-void RAWInputStream::refill() {
+void DigitalMusicInputStream::refill() {
 	if (_finished)
 		return;
 
@@ -417,61 +468,8 @@
 					loopStart = 4 * 18727;
 				}
 
-				if (!(_vm->getFeatures() & GF_COMPRESSED_SOUNDS)) {
-					// uncompressed digital music
-					audioStream = new RAWInputStream(_vm, _digitalMusicContext, resourceId - 9, flags == MUSIC_LOOP, loopStart);
-				} else {
-					// compressed digital music
-					ResourceData * musicResourceData;
-					Common::File *_file;
-					byte compressedHeader[10];
-					GameSoundTypes soundType;
-
-					musicResourceData = _vm->_resource->getResourceData(_digitalMusicContext, resourceId - 9);
-					_file = _digitalMusicContext->getFile(musicResourceData);
-
-					if (_vm->getMusicInfo() == NULL) {
-						error("RAWInputStream() wrong musicInfo");
-					}
-
-					_file->seek((long)musicResourceData->offset, SEEK_SET);
-
-					_file->read(compressedHeader, 9);
-
-					if (compressedHeader[0] == char(0)) {
-						soundType = kSoundMP3;
-					} else if (compressedHeader[0] == char(1)) {
-						soundType = kSoundOGG;
-					} else if (compressedHeader[0] == char(2)) {
-						soundType = kSoundFLAC;
-					}
-
-					switch (soundType) {
-#ifdef USE_MAD
-						case kSoundMP3:
-							debug(1, "Playing MP3 compressed digital music");
-							audioStream = Audio::makeMP3Stream(_file, musicResourceData->size);
-							break;
-#endif
-#ifdef USE_VORBIS
-						case kSoundOGG:
-							debug(1, "Playing OGG compressed digital music");
-							audioStream = Audio::makeVorbisStream(_file, musicResourceData->size);
-							break;
-#endif
-#ifdef USE_FLAC
-						case kSoundFLAC:
-							debug(1, "Playing FLAC compressed digital music");
-							audioStream = Audio::makeFlacStream(_file, musicResourceData->size);
-							break;
-#endif
-						default:
-							// Unknown compression
-							error("Trying to play a compressed digital music, but the compression is not known");
-							break;
-					}
-
-				}
+				// digital music
+				audioStream = new DigitalMusicInputStream(_vm, _digitalMusicContext, resourceId - 9, flags == MUSIC_LOOP, loopStart);
 			}
 		}
 	}


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