[Scummvm-cvs-logs] CVS: scummvm/sound voc.cpp,1.18,1.19 voc.h,1.13,1.14

Max Horn fingolfin at users.sourceforge.net
Sat Jul 31 04:48:24 CEST 2004


Update of /cvsroot/scummvm/scummvm/sound
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8941/sound

Modified Files:
	voc.cpp voc.h 
Log Message:
changed loadVOCFile to work on a generic ReadStream instead of a File, and renamed it to loadVOCFromStream; removed readVOCFromMemory as it isn't used anymore, and in the future, a MemoryReadstream plus loadVOCFromStream can achieve the same effect

Index: voc.cpp
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/voc.cpp,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- voc.cpp	5 May 2004 10:58:07 -0000	1.18
+++ voc.cpp	31 Jul 2004 11:46:58 -0000	1.19
@@ -22,7 +22,7 @@
 
 #include "stdafx.h"
 #include "common/util.h"
-#include "common/file.h"
+#include "common/stream.h"
 
 #include "sound/audiostream.h"
 #include "sound/mixer.h"
@@ -43,118 +43,56 @@
 	}
 }
 
-byte *readVOCFromMemory(byte *ptr, int &size, int &rate, int &loops, int &begin_loop, int &end_loop) {
-	
-	// Verify the VOC header. We are a little bit lenient here to work around
-	// some invalid VOC headers used in various SCUMM games (they have 0x0
-	// instead of 0x1A after the "Creative Voice File" string).
-	if (memcmp(ptr, "Creative Voice File", 19) != 0)
-		error("readVOCFromMemory: Invalid header");
-	if (ptr[19] != 0x1A)
-		debug(3, "readVOCFromMemory: Partially invalid header");
-	
-	int32 offset = READ_LE_UINT16(ptr + 20);
-	int16 version = READ_LE_UINT16(ptr + 22);
-	int16 code = READ_LE_UINT16(ptr + 24);
-	assert(version == 0x010A || version == 0x0114);
-	assert(code == ~version + 0x1234);
-	
-	int len;
-	byte *ret_sound = 0;
-	size = 0;
-	begin_loop = 0;
-	end_loop = 0;
-	
-	ptr += offset;
-	while ((code = *ptr++)) {
-		len = *ptr++;
-		len |= *ptr++ << 8;
-		len |= *ptr++ << 16;
-
-		switch(code) {
-		case 1: {
-			int time_constant = *ptr++;
-			int packing = *ptr++;
-			len -= 2;
-			rate = getSampleRateFromVOCRate(time_constant);
-			debug(9, "VOC Data Block: %d, %d, %d", rate, packing, len);
-			if (packing == 0) {
-				if (size) {
-					ret_sound = (byte *)realloc(ret_sound, size + len);
-				} else {
-					ret_sound = (byte *)malloc(len);
-				}
-				memcpy(ret_sound + size, ptr, len);
-				begin_loop = size;
-				size += len;
-				end_loop = size;
-			} else {
-				warning("VOC file packing %d unsupported", packing);
-			}
-			} break;
-		case 6:	// begin of loop
-			loops = (uint16)READ_LE_UINT16(ptr);
-			break;
-		case 7:	// end of loop
-			break;
-		default:
-			warning("Invalid code in VOC file : %d", code);
-			return ret_sound;
-		}
-		// FIXME some FT samples (ex. 362) has bad length, 2 bytes too short
-		ptr += len;
-	}
-	debug(4, "VOC Data Size : %d", size);
-	return ret_sound;
+byte *loadVOCFromStream(Common::ReadStream *stream, int &size, int &rate) {
+	int loops, begin_loop, end_loop;
+	return loadVOCFromStream(stream, size, rate, loops, begin_loop, end_loop);
 }
 
-// FIXME/TODO: loadVOCFile() essentially duplicates all the code from
-// readCreativeVoc(). Obviously this is bad, they should share as much
-// code as possible. One way to do that would be to abstract the
-// reading from memory / from file into a stream class (similar to the
-// RWOps of SDL, for example).
-byte *loadVOCFile(File *file, int &size, int &rate) {
+byte *loadVOCFromStream(Common::ReadStream *stream, int &size, int &rate, int &loops, int &begin_loop, int &end_loop) {
 	VocFileHeader fileHeader;
 
-	if (file->read(&fileHeader, 8) != 8)
+	if (stream->read(&fileHeader, 8) != 8)
 		goto invalid;
 
 	if (!memcmp(&fileHeader, "VTLK", 4)) {
-		if (file->read(&fileHeader, sizeof(VocFileHeader)) != sizeof(VocFileHeader))
+		if (stream->read(&fileHeader, sizeof(VocFileHeader)) != sizeof(VocFileHeader))
 			goto invalid;
 	} else if (!memcmp(&fileHeader, "Creative", 8)) {
-		if (file->read(((byte *)&fileHeader) + 8, sizeof(VocFileHeader) - 8) != sizeof(VocFileHeader) - 8)
+		if (stream->read(((byte *)&fileHeader) + 8, sizeof(VocFileHeader) - 8) != sizeof(VocFileHeader) - 8)
 			goto invalid;
 	} else {
 	invalid:;
-		warning("loadVOCFile: Invalid header");
+		warning("loadVOCFromStream: Invalid header");
 		return NULL;
 	}
 
 	if (memcmp(fileHeader.desc, "Creative Voice File", 19) != 0)
-		error("loadVOCFile: Invalid header");
+		error("loadVOCFromStream: Invalid header");
 	if (fileHeader.desc[19] != 0x1A)
-		debug(3, "loadVOCFile: Partially invalid header");
+		debug(3, "loadVOCFromStream: Partially invalid header");
 
-	//int32 offset = FROM_LE_16(fileHeader.datablock_offset);
+	int32 offset = FROM_LE_16(fileHeader.datablock_offset);
 	int16 version = FROM_LE_16(fileHeader.version);
 	int16 code = FROM_LE_16(fileHeader.id);
+	assert(offset == sizeof(VocFileHeader));
 	assert(version == 0x010A || version == 0x0114);
 	assert(code == ~version + 0x1234);
 
 	int len;
 	byte *ret_sound = 0;
 	size = 0;
+	begin_loop = 0;
+	end_loop = 0;
 
-	while ((code = file->readByte())) {
-		len = file->readByte();
-		len |= file->readByte() << 8;
-		len |= file->readByte() << 16;
+	while ((code = stream->readByte())) {
+		len = stream->readByte();
+		len |= stream->readByte() << 8;
+		len |= stream->readByte() << 16;
 
 		switch(code) {
 		case 1: {
-			int time_constant = file->readByte();
-			int packing = file->readByte();
+			int time_constant = stream->readByte();
+			int packing = stream->readByte();
 			len -= 2;
 			rate = getSampleRateFromVOCRate(time_constant);
 			debug(9, "VOC Data Block: %d, %d, %d", rate, packing, len);
@@ -164,12 +102,21 @@
 				} else {
 					ret_sound = (byte *)malloc(len);
 				}
-				file->read(ret_sound + size, len);
+				stream->read(ret_sound + size, len);
 				size += len;
+				begin_loop = size;
+				end_loop = size;
 			} else {
 				warning("VOC file packing %d unsupported", packing);
 			}
 			} break;
+		case 6:	// begin of loop
+			assert(len == 2);
+			loops = stream->readUint16LE();
+			break;
+		case 7:	// end of loop
+			assert(len == 0);
+			break;
 		default:
 			warning("Invalid code in VOC file : %d", code);
 			return ret_sound;
@@ -179,19 +126,9 @@
 	return ret_sound;
 }
 
-AudioStream *makeVOCStream(byte *ptr) {
-	int size, rate, loops, begin_loop, end_loop;
-	byte *data = readVOCFromMemory(ptr, size, rate, loops, begin_loop, end_loop);
-
-	if (!data)
-		return 0;
-
-	return makeLinearInputStream(rate, SoundMixer::FLAG_AUTOFREE | SoundMixer::FLAG_UNSIGNED, data, size, 0, 0);
-}
-
-AudioStream *makeVOCStream(File *file) {
+AudioStream *makeVOCStream(Common::ReadStream *stream) {
 	int size, rate;
-	byte *data = loadVOCFile(file, size, rate);
+	byte *data = loadVOCFromStream(stream, size, rate);
 
 	if (!data)
 		return 0;

Index: voc.h
===================================================================
RCS file: /cvsroot/scummvm/scummvm/sound/voc.h,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -d -r1.13 -r1.14
--- voc.h	2 May 2004 22:33:28 -0000	1.13
+++ voc.h	31 Jul 2004 11:46:58 -0000	1.14
@@ -27,7 +27,7 @@
 #include "common/scummsys.h"
 
 class AudioStream;
-class File;
+namespace Common { class ReadStream; }
 
 #if !defined(__GNUC__)
 #pragma START_PACK_STRUCTS
@@ -63,10 +63,9 @@
  */
 extern int getSampleRateFromVOCRate(int vocSR);
 
-extern byte *readVOCFromMemory(byte *ptr, int &size, int &rate, int &loops, int &begin_loop, int &end_loop);
-extern byte *loadVOCFile(File *file, int &size, int &rate);
+extern byte *loadVOCFromStream(Common::ReadStream *stream, int &size, int &rate, int &loops, int &begin_loop, int &end_loop);
+extern byte *loadVOCFromStream(Common::ReadStream *stream, int &size, int &rate);
 
-AudioStream *makeVOCStream(byte *ptr);
-AudioStream *makeVOCStream(File *file);
+AudioStream *makeVOCStream(Common::ReadStream *stream);
 
 #endif





More information about the Scummvm-git-logs mailing list