[Scummvm-cvs-logs] SF.net SVN: scummvm: [21337] scummvm/trunk/engines/kyra/sound_adlib.cpp

eriktorbjorn at users.sourceforge.net eriktorbjorn at users.sourceforge.net
Thu Mar 16 10:54:02 CET 2006


Revision: 21337
Author:   eriktorbjorn
Date:     2006-03-16 10:53:02 -0800 (Thu, 16 Mar 2006)
ViewCVS:  http://svn.sourceforge.net/scummvm/?rev=21337&view=rev

Log Message:
-----------
We have several functions where we need to find the pointer to a program or an
instrument. These are found using the lookup tables in _soundData on offsets 0
and 500 respectively. Added helper functions for that, which makes the code a
lot tidier. Particularly in update_setupRhythmSection(), where it's now much
clearer where it's getting all those "unk" values from.

Use the checkValue() function (which limits the "total level" to its valid
range) for the calculateOpLevel1() and 2() functions as well.

Renamed updateCallback16() to update_waitForEndOfProgram().

Modified Paths:
--------------
    scummvm/trunk/engines/kyra/sound_adlib.cpp
Modified: scummvm/trunk/engines/kyra/sound_adlib.cpp
===================================================================
--- scummvm/trunk/engines/kyra/sound_adlib.cpp	2006-03-16 18:01:14 UTC (rev 21336)
+++ scummvm/trunk/engines/kyra/sound_adlib.cpp	2006-03-16 18:53:02 UTC (rev 21337)
@@ -204,11 +204,24 @@
 	uint16 checkValue(int16 val) {
 		if (val < 0)
 			val = 0;
-		if (val > 0x3F)
+		else if (val > 0x3F)
 			val = 0x3F;
 		return val;
 	}
 
+	// The sound data has at least two lookup tables:
+	//
+	// * One for programs, starting at offset 0.
+	// * One for instruments, starting at offset 500.
+
+	uint8 *getProgram(int progId) {
+		return _soundData + READ_LE_UINT16(_soundData + 2 * progId);
+	}
+
+	uint8 *getInstrument(int instrumentId) {
+		return _soundData + READ_LE_UINT16(_soundData + 500 + 2 * instrumentId);
+	}
+
 	void setupPrograms();
 	void executePrograms();
 
@@ -235,7 +248,7 @@
 	int update_setBaseNote(uint8 *&dataptr, Channel &channel, uint8 value);
 	int update_setupSecondaryEffect1(uint8 *&dataptr, Channel &channel, uint8 value);
 	int update_stopOtherChannel(uint8 *&dataptr, Channel &channel, uint8 value);
-	int updateCallback16(uint8 *&dataptr, Channel &channel, uint8 value);
+	int update_waitForEndOfProgram(uint8 *&dataptr, Channel &channel, uint8 value);
 	int update_setupInstrument(uint8 *&dataptr, Channel &channel, uint8 value);
 	int update_setupPrimaryEffect1(uint8 *&dataptr, Channel &channel, uint8 value);
 	int update_removePrimaryEffect1(uint8 *&dataptr, Channel &channel, uint8 value);
@@ -468,11 +481,12 @@
 	int songId = va_arg(list, int);
 	_flags |= 8;
 	_flagTrigger = 1;
-	uint16 offset = READ_LE_UINT16(&_soundData[songId << 1]);
-	uint8 firstByte = *(_soundData + offset);
 
+	uint8 *ptr = getProgram(songId);
+	uint8 chan = *ptr;
+
 	if ((songId << 1) != 0) {
-		if (firstByte == 9) {
+		if (chan == 9) {
 			if (_flags & 2)
 				return 0;
 		} else {
@@ -593,8 +607,7 @@
 
 void AdlibDriver::setupPrograms() {
 	while (_lastProcessed != _soundsPlaying) {
-		uint16 add = _soundIdTable[_lastProcessed] << 1;
-		uint8 *ptr = _soundData + READ_LE_UINT16(_soundData + add);
+		uint8 *ptr = getProgram(_soundIdTable[_lastProcessed]);
 		uint8 chan = *ptr++;
 		uint8 priority = *ptr++;
 
@@ -1152,16 +1165,9 @@
 		value += channel.opExtraLevel3;
 	}
 
-	// Don't allow the total level to overflow into the scaling level bits.
-
-	if (value > 0x3F) {
-		value = 0x3F;
-	} else if (value < 0)
-		value = 0;
-
 	// Preserve the scaling level bits from opLevel1
 
-	return value | (channel.opLevel1 & 0xC0);
+	return checkValue(value) | (channel.opLevel1 & 0xC0);
 }
 
 uint8 AdlibDriver::calculateOpLevel2(Channel &channel) {
@@ -1171,16 +1177,9 @@
 	value += channel.opExtraLevel2;
 	value += channel.opExtraLevel3;
 
-	// Don't allow the total level to overflow into the scaling level bits.
-
-	if (value > 0x3F) {
-		value = 0x3F;
-	} else if (value < 0)
-		value = 0;
-
 	// Preserve the scaling level bits from opLevel2
 
-	return value | (channel.opLevel2 & 0xC0);
+	return checkValue(value) | (channel.opLevel2 & 0xC0);
 }
 
 // parser opcodes
@@ -1203,8 +1202,7 @@
 	if (value == 0xFF)
 		return 0;
 
-	uint16 add = value << 1;
-	uint8 *ptr = _soundData + READ_LE_UINT16(_soundData + add);
+	uint8 *ptr = getProgram(value);
 	uint8 chan = *ptr++;
 	uint8 priority = *ptr++;
 
@@ -1304,21 +1302,20 @@
 	return 0;
 }
 
-int AdlibDriver::updateCallback16(uint8 *&dataptr, Channel &channel, uint8 value) {
-	uint8 *ptr = _soundData;
-	ptr += READ_LE_UINT16(&_soundData[value << 1]);
-	Channel &channel2 = _channels[*ptr];
-	if (!channel2.dataptr) {
+int AdlibDriver::update_waitForEndOfProgram(uint8 *&dataptr, Channel &channel, uint8 value) {
+	uint8 *ptr = getProgram(value);
+	uint8 chan = *ptr;
+
+	if (!_channels[chan].dataptr) {
 		return 0;
 	}
+
 	dataptr -= 2;
 	return 2;
 }
 
 int AdlibDriver::update_setupInstrument(uint8 *&dataptr, Channel &channel, uint8 value) {
-	uint8 *ptr = _soundData;
-	ptr += READ_LE_UINT16(_soundData + (value << 1) + 0x1F4);
-	setupInstrument(_curRegOffset, ptr, channel);
+	setupInstrument(_curRegOffset, getInstrument(value), channel);
 	return 0;
 }
 
@@ -1592,34 +1589,25 @@
 	int channelBackUp = _curChannel;
 	int regOffsetBackUp = _curRegOffset;
 
-	uint8 entry = value << 1;
-	uint8 *ptr = _soundData + READ_LE_UINT16(_soundData + entry + 0x1F4);
-
 	_curChannel = 6;
 	_curRegOffset = _regOffset[6];
 
-	_unkValue6 = *(ptr + 6);
-	setupInstrument(_curRegOffset, ptr, channel);
+	setupInstrument(_curRegOffset, getInstrument(value), channel);
+	_unkValue6 = channel.opLevel2;
 
-	entry = *dataptr++ << 1;
-	ptr = _soundData + READ_LE_UINT16(_soundData + entry + 0x1F4);
-
 	_curChannel = 7;
 	_curRegOffset = _regOffset[7];
 
-	_unkValue7 = entry = *(ptr + 5);
-	_unkValue8 = entry = *(ptr + 6);
-	setupInstrument(_curRegOffset, ptr, channel);
+	setupInstrument(_curRegOffset, getInstrument(*dataptr++), channel);
+	_unkValue7 = channel.opLevel1;
+	_unkValue8 = channel.opLevel2;
 
-	entry = *dataptr++ << 1;
-	ptr = _soundData + READ_LE_UINT16(_soundData + entry + 0x1F4);
-
 	_curChannel = 8;
 	_curRegOffset = _regOffset[8];
 
-	_unkValue9 = entry = *(ptr + 5);
-	_unkValue10 = entry = *(ptr + 6);
-	setupInstrument(_curRegOffset, ptr, channel);
+	setupInstrument(_curRegOffset, getInstrument(*dataptr++), channel);
+	_unkValue9 = channel.opLevel1;
+	_unkValue10 = channel.opLevel2;
 
 	// Octave / F-Number / Key-On for channels 6, 7 and 8
 
@@ -1905,7 +1893,7 @@
 	COMMAND(update_setBaseNote),
 	COMMAND(update_setupSecondaryEffect1),
 	COMMAND(update_stopOtherChannel),
-	COMMAND(updateCallback16),
+	COMMAND(update_waitForEndOfProgram),
 
 	// 16
 	COMMAND(update_setupInstrument),


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