[Scummvm-git-logs] scummvm master -> c7e3415511317900a0ae28ddda4a607d094b7a5b

bluegr noreply at scummvm.org
Tue Jan 28 21:24:44 UTC 2025


This automated email contains information about 2 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
7187c9293f AUDIO: Mark more AdLib symbols as const
c7e3415511 AUDIO: Avoid duplicating pointers by making use of const_cast


Commit: 7187c9293f503d667174b28c9c1ba8ac30171124
    https://github.com/scummvm/scummvm/commit/7187c9293f503d667174b28c9c1ba8ac30171124
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2025-01-28T23:24:40+02:00

Commit Message:
AUDIO: Mark more AdLib symbols as const

Changed paths:
    audio/adlib_ms.cpp
    audio/adlib_ms.h
    engines/agos/drivers/accolade/adlib.cpp
    engines/agos/drivers/accolade/adlib.h
    engines/agos/drivers/simon1/adlib.cpp
    engines/agos/drivers/simon1/adlib.h
    engines/darkseed/adlib_worx.cpp
    engines/darkseed/adlib_worx.h
    engines/lure/sound.cpp
    engines/lure/sound.h
    engines/ultima/nuvie/sound/mididrv_m_adlib.cpp
    engines/ultima/nuvie/sound/mididrv_m_adlib.h


diff --git a/audio/adlib_ms.cpp b/audio/adlib_ms.cpp
index cd5cac691c5..d9d60ac6197 100644
--- a/audio/adlib_ms.cpp
+++ b/audio/adlib_ms.cpp
@@ -23,12 +23,12 @@
 
 #include "common/debug.h"
 
-bool OplInstrumentOperatorDefinition::isEmpty() {
+bool OplInstrumentOperatorDefinition::isEmpty() const {
 	return freqMultMisc == 0 && level == 0 && decayAttack == 0 &&
 		   releaseSustain == 0 && waveformSelect == 0;
 }
 
-bool OplInstrumentDefinition::isEmpty() {
+bool OplInstrumentDefinition::isEmpty() const {
 	if (rhythmType != RHYTHM_TYPE_UNDEFINED) {
 		return operator0.isEmpty() &&
 			(rhythmType != RHYTHM_TYPE_BASS_DRUM || operator1.isEmpty());
@@ -40,7 +40,7 @@ bool OplInstrumentDefinition::isEmpty() {
 	}
 }
 
-uint8 OplInstrumentDefinition::getNumberOfOperators() {
+uint8 OplInstrumentDefinition::getNumberOfOperators() const {
 	if (rhythmType == RHYTHM_TYPE_UNDEFINED) {
 		return fourOperator ? 4 : 2;
 	} else {
@@ -50,7 +50,7 @@ uint8 OplInstrumentDefinition::getNumberOfOperators() {
 	}
 }
 
-OplInstrumentOperatorDefinition &OplInstrumentDefinition::getOperatorDefinition(uint8 operatorNum) {
+const OplInstrumentOperatorDefinition &OplInstrumentDefinition::getOperatorDefinition(uint8 operatorNum) const {
 	assert((!fourOperator && operatorNum < 2) || operatorNum < 4);
 
 	switch (operatorNum) {
@@ -68,7 +68,7 @@ OplInstrumentOperatorDefinition &OplInstrumentDefinition::getOperatorDefinition(
 	}
 }
 
-void AdLibBnkInstrumentOperatorDefinition::toOplInstrumentOperatorDefinition(OplInstrumentOperatorDefinition &operatorDef, uint8 waveformSelect) {
+void AdLibBnkInstrumentOperatorDefinition::toOplInstrumentOperatorDefinition(OplInstrumentOperatorDefinition &operatorDef, uint8 waveformSelect) const {
 	// Combine the separate fields of the BNK format into complete register values.
 	operatorDef.freqMultMisc = frequencyMultiplier | (keyScalingRate == 0 ? 0 : 0x10) |
 		(envelopeGainType == 0 ? 0 : 0x20) | (vibrato == 0 ? 0 : 0x40) | (amplitudeModulation == 0 ? 0 : 0x80);
@@ -78,7 +78,7 @@ void AdLibBnkInstrumentOperatorDefinition::toOplInstrumentOperatorDefinition(Opl
 	operatorDef.waveformSelect = waveformSelect;
 }
 
-void AdLibBnkInstrumentDefinition::toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef) {
+void AdLibBnkInstrumentDefinition::toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef) const {
 	instrumentDef.fourOperator = false;
 
 	operator0.toOplInstrumentOperatorDefinition(instrumentDef.operator0, waveformSelect0);
@@ -93,7 +93,7 @@ void AdLibBnkInstrumentDefinition::toOplInstrumentDefinition(OplInstrumentDefini
 	instrumentDef.rhythmType = RHYTHM_TYPE_UNDEFINED;
 }
 
-void AdLibIbkInstrumentDefinition::toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef) {
+void AdLibIbkInstrumentDefinition::toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef) const {
 	instrumentDef.fourOperator = false;
 
 	instrumentDef.operator0.freqMultMisc = o0FreqMultMisc;
@@ -138,7 +138,7 @@ void AdLibIbkInstrumentDefinition::toOplInstrumentDefinition(OplInstrumentDefini
 }
 
 // These are the melodic instrument definitions used by the Win95 SB16 driver.
-OplInstrumentDefinition MidiDriver_ADLIB_Multisource::OPL_INSTRUMENT_BANK[128] = {
+const OplInstrumentDefinition MidiDriver_ADLIB_Multisource::OPL_INSTRUMENT_BANK[128] = {
 	// 0x00
 	{ false, { 0x01, 0x8F, 0xF2, 0xF4, 0x00 }, { 0x01, 0x06, 0xF2, 0xF7, 0x00 }, { 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00, 0x00, 0x00 }, 0x38, 0x00, 0x00, RHYTHM_TYPE_UNDEFINED },
 	{ false, { 0x01, 0x4B, 0xF2, 0xF4, 0x00 }, { 0x01, 0x00, 0xF2, 0xF7, 0x00 }, { 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00, 0x00, 0x00 }, 0x38, 0x00, 0x00, RHYTHM_TYPE_UNDEFINED },
@@ -286,7 +286,7 @@ OplInstrumentDefinition MidiDriver_ADLIB_Multisource::OPL_INSTRUMENT_BANK[128] =
 };
 
 // These are the rhythm instrument definitions used by the Win95 SB16 driver.
-OplInstrumentDefinition MidiDriver_ADLIB_Multisource::OPL_RHYTHM_BANK[62] = {
+const OplInstrumentDefinition MidiDriver_ADLIB_Multisource::OPL_RHYTHM_BANK[62] = {
 	// GS percussion start
 	// 0x1B
 	{ false, { 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00, 0x00, 0x00 }, { 0x00, 0x00, 0x00, 0x00, 0x00 }, 0x00, 0x00, 0x00, RHYTHM_TYPE_UNDEFINED },
@@ -1634,7 +1634,7 @@ int32 MidiDriver_ADLIB_Multisource::calculatePitchBend(uint8 channel, uint8 sour
 	return pitchBend;
 }
 
-uint8 MidiDriver_ADLIB_Multisource::calculateVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
+uint8 MidiDriver_ADLIB_Multisource::calculateVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
 	// Get the volume (level) for this operator from the instrument definition.
 	uint8 operatorDefVolume = instrumentDef.getOperatorDefinition(operatorNum).level & 0x3F;
 
@@ -1667,7 +1667,7 @@ uint8 MidiDriver_ADLIB_Multisource::calculateVolume(uint8 channel, uint8 source,
 	return scaledVolume;
 }
 
-uint8 MidiDriver_ADLIB_Multisource::calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
+uint8 MidiDriver_ADLIB_Multisource::calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
 	uint8 unscaledVolume;
 	// Get the volume (level) for this operator from the instrument definition.
 	uint8 operatorVolume = instrumentDef.getOperatorDefinition(operatorNum).level & 0x3F;
@@ -1704,7 +1704,7 @@ uint8 MidiDriver_ADLIB_Multisource::calculateUnscaledVolume(uint8 channel, uint8
 	return MIN((uint8)0x3F, unscaledVolume);
 }
 
-bool MidiDriver_ADLIB_Multisource::isVolumeApplicableToOperator(OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
+bool MidiDriver_ADLIB_Multisource::isVolumeApplicableToOperator(const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
 	// Determine if volume settings should be applied to this operator. Carrier
 	// operators in FM synthesis and all operators in additive synthesis need
 	// to have volume settings applied; modulator operators just use the
diff --git a/audio/adlib_ms.h b/audio/adlib_ms.h
index 0c3149643c7..01796d0fb50 100644
--- a/audio/adlib_ms.h
+++ b/audio/adlib_ms.h
@@ -68,7 +68,7 @@ struct OplInstrumentOperatorDefinition {
 	 *
 	 * @return True if this operator is empty; false otherwise.
 	 */
-	bool isEmpty();
+	bool isEmpty() const;
 };
 
 /**
@@ -120,13 +120,13 @@ struct OplInstrumentDefinition {
 	 *
 	 * @return True if this instrument is empty; false otherwise.
 	 */
-	bool isEmpty();
+	bool isEmpty() const;
 	/**
 	 * Returns the number of operators used by this instrument definition.
 	 *
 	 * @return The number of operators (2 or 4).
 	 */
-	uint8 getNumberOfOperators();
+	uint8 getNumberOfOperators() const;
 	/**
 	 * Returns the definition data for the operator with the specified number.
 	 * Specify 0 or 1 for 2 operator instruments or 0-3 for 4 operator
@@ -135,7 +135,7 @@ struct OplInstrumentDefinition {
 	 * @param operatorNum The operator for which the data should be returned.
 	 * @return Pointer to the definition data for the specified operator.
 	 */
-	OplInstrumentOperatorDefinition &getOperatorDefinition(uint8 operatorNum);
+	const OplInstrumentOperatorDefinition &getOperatorDefinition(uint8 operatorNum) const;
 };
 
 #include "common/pack-start.h" // START STRUCT PACKING
@@ -173,7 +173,7 @@ struct AdLibBnkInstrumentOperatorDefinition {
 	 * @param waveformSelect The value of the waveform select parameter for
 	 * this operator.
 	 */
-	void toOplInstrumentOperatorDefinition(OplInstrumentOperatorDefinition &operatorDef, uint8 waveformSelect);
+	void toOplInstrumentOperatorDefinition(OplInstrumentOperatorDefinition &operatorDef, uint8 waveformSelect) const;
 } PACKED_STRUCT;
 
 /**
@@ -209,7 +209,7 @@ struct AdLibBnkInstrumentDefinition {
 	 * @param instrumentDef The instrument definition to which the data should
 	 * be copied.
 	 */
-	void toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef);
+	void toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef) const;
 } PACKED_STRUCT;
 
 /**
@@ -248,7 +248,7 @@ struct AdLibIbkInstrumentDefinition {
 	 * @param instrumentDef The instrument definition to which the data should
 	 * be copied.
 	 */
-	void toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef);
+	void toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef) const;
 } PACKED_STRUCT;
 
 #include "common/pack-end.h" // END STRUCT PACKING
@@ -478,11 +478,11 @@ public:
 	/**
 	 * The default melodic instrument definitions.
 	 */
-	static OplInstrumentDefinition OPL_INSTRUMENT_BANK[];
+	static const OplInstrumentDefinition OPL_INSTRUMENT_BANK[];
 	/**
 	 * The default rhythm instrument definitions.
 	 */
-	static OplInstrumentDefinition OPL_RHYTHM_BANK[];
+	static const OplInstrumentDefinition OPL_RHYTHM_BANK[];
 
 protected:
 	/**
@@ -606,7 +606,7 @@ protected:
 		/**
 		 * Pointer to the instrument definition used to play the note.
 		 */
-		OplInstrumentDefinition *instrumentDef;
+		const OplInstrumentDefinition *instrumentDef;
 
 		/**
 		 * True if this OPL channel has been allocated to a MIDI channel.
@@ -635,7 +635,7 @@ protected:
 		/**
 		 * Pointer to the instrument definition.
 		 */
-		OplInstrumentDefinition *instrumentDef;
+		const OplInstrumentDefinition *instrumentDef;
 		/**
 		 * Unique identifer for this instrument (@see ActiveNote.instrumentId).
 		 */
@@ -1020,7 +1020,7 @@ protected:
 	 * instruments.
 	 * @return The calculated operator volume (level).
 	 */
-	virtual uint8 calculateVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
+	virtual uint8 calculateVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
 	/**
 	 * Calculates the unscaled volume for the specified operator of a note on
 	 * the specified MIDI channel and source, using the specified MIDI velocity
@@ -1042,7 +1042,7 @@ protected:
 	 * instruments.
 	 * @return The calculated unscaled operator volume (level).
 	 */
-	virtual uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
+	virtual uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
 	/**
 	 * Determines if volume settings should be applied to the operator level.
 	 * This depends on the type of the operator (carrier or modulator), which
@@ -1052,7 +1052,7 @@ protected:
 	 * @param operatorNum The number of the operator (0-1 or 0-3)
 	 * @return True if volume should be applied, false otherwise
 	 */
-	virtual bool isVolumeApplicableToOperator(OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
+	virtual bool isVolumeApplicableToOperator(const OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
 	/**
 	 * Determines the panning that should be applied to notes played on the
 	 * specified MIDI channel and source.
@@ -1225,9 +1225,9 @@ protected:
 	bool _rhythmMode;
 
 	// Pointer to the melodic instrument definitions.
-	OplInstrumentDefinition *_instrumentBank;
+	const OplInstrumentDefinition *_instrumentBank;
 	// Pointer to the rhythm instrument definitions.
-	OplInstrumentDefinition *_rhythmBank;
+	const OplInstrumentDefinition *_rhythmBank;
 	// The MIDI note value of the first rhythm instrument in the bank.
 	uint8 _rhythmBankFirstNote;
 	// The MIDI note value of the last rhythm instrument in the bank.
diff --git a/engines/agos/drivers/accolade/adlib.cpp b/engines/agos/drivers/accolade/adlib.cpp
index bbc2b4817e3..d304f619bdd 100644
--- a/engines/agos/drivers/accolade/adlib.cpp
+++ b/engines/agos/drivers/accolade/adlib.cpp
@@ -68,8 +68,8 @@ const uint16 MidiDriver_Accolade_AdLib::OPL_NOTE_FREQUENCIES_MUSIC_DRV[] = {
 // I have currently not implemented dynamic channel allocation.
 MidiDriver_Accolade_AdLib::MidiDriver_Accolade_AdLib(OPL::Config::OplType oplType, bool newVersion, int timerFrequency) :
 		MidiDriver_ADLIB_Multisource(oplType, timerFrequency) {
-	_instrumentBank = nullptr;
-	_rhythmBank = nullptr;
+	_instrumentBank = _instrumentBankPtr = nullptr;
+	_rhythmBank = _rhythmBankPtr = nullptr;
 	_newVersion = newVersion;
 	_oplNoteFrequencies = _newVersion ? OPL_NOTE_FREQUENCIES_MUSIC_DRV : OPL_NOTE_FREQUENCIES_INSTR_DAT;
 
@@ -81,10 +81,10 @@ MidiDriver_Accolade_AdLib::MidiDriver_Accolade_AdLib(OPL::Config::OplType oplTyp
 }
 
 MidiDriver_Accolade_AdLib::~MidiDriver_Accolade_AdLib() {
-	if (_instrumentBank)
-		delete[] _instrumentBank;
-	if (_rhythmBank)
-		delete[] _rhythmBank;
+	if (_instrumentBankPtr)
+		delete[] _instrumentBankPtr;
+	if (_rhythmBankPtr)
+		delete[] _rhythmBankPtr;
 }
 
 int MidiDriver_Accolade_AdLib::open() {
@@ -264,7 +264,7 @@ void MidiDriver_Accolade_AdLib::patchE1Instruments() {
 		return;
 
 	// Patch the attack and decay of instrument 0x18.
-	_instrumentBank[0x18].operator0.decayAttack = 0x42; // Was 0x24
+	_instrumentBankPtr[0x18].operator0.decayAttack = 0x42; // Was 0x24
 }
 
 void MidiDriver_Accolade_AdLib::patchWwInstruments() {
@@ -287,18 +287,18 @@ void MidiDriver_Accolade_AdLib::patchWwInstruments() {
 		return;
 
 	// Patch the attack of instrument 0x22.
-	_instrumentBank[0x22].operator1.decayAttack &= 0x0F;
-	_instrumentBank[0x22].operator1.decayAttack |= 0x50;
+	_instrumentBankPtr[0x22].operator1.decayAttack &= 0x0F;
+	_instrumentBankPtr[0x22].operator1.decayAttack |= 0x50;
 
 	// Patch the attack of instrument 0x25.
-	_instrumentBank[0x25].operator1.decayAttack &= 0x0F;
-	_instrumentBank[0x25].operator1.decayAttack |= 0x60;
+	_instrumentBankPtr[0x25].operator1.decayAttack &= 0x0F;
+	_instrumentBankPtr[0x25].operator1.decayAttack |= 0x60;
 
 	// Patch the attack of instrument 0x7F.
-	_instrumentBank[0x7F].operator0.decayAttack &= 0x0F;
-	_instrumentBank[0x7F].operator0.decayAttack |= 0x60;
-	_instrumentBank[0x7F].operator1.decayAttack &= 0x0F;
-	_instrumentBank[0x7F].operator1.decayAttack |= 0x90;
+	_instrumentBankPtr[0x7F].operator0.decayAttack &= 0x0F;
+	_instrumentBankPtr[0x7F].operator0.decayAttack |= 0x60;
+	_instrumentBankPtr[0x7F].operator1.decayAttack &= 0x0F;
+	_instrumentBankPtr[0x7F].operator1.decayAttack |= 0x90;
 }
 
 MidiDriver_Accolade_AdLib::InstrumentInfo MidiDriver_Accolade_AdLib::determineInstrument(uint8 channel, uint8 source, uint8 note) {
@@ -379,7 +379,7 @@ uint16 MidiDriver_Accolade_AdLib::calculateFrequency(uint8 channel, uint8 source
 	return block << 10 | frequency;
 }
 
-uint8 MidiDriver_Accolade_AdLib::calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
+uint8 MidiDriver_Accolade_AdLib::calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
 	// A volume adjustment is applied to the velocity of melodic notes.
 	int8 volumeAdjustment = 0;
 	if (_sources[source].type != SOURCE_TYPE_SFX) {
@@ -508,13 +508,13 @@ void MidiDriver_Accolade_AdLib::readDriverData(byte *driverData, uint16 driverDa
 	uint16 rhythmNoteOffset = newVersion ? 376 + 36 : 256 + 16 + 16;
 	uint16 instrumentDataOffset = newVersion ? 722 : 256 + 16 + 16 + 64 + 2;
 
-	_instrumentBank = new OplInstrumentDefinition[instrumentDefinitionCount];
+	_instrumentBank = _instrumentBankPtr = new OplInstrumentDefinition[instrumentDefinitionCount];
 	for (int i = 0; i < instrumentDefinitionCount; i++) {
 		byte *instrumentData = driverData + instrumentDataOffset + (i * 9);
-		loadInstrumentData(_instrumentBank[i], instrumentData, RHYTHM_TYPE_UNDEFINED, 0, newVersion);
+		loadInstrumentData(_instrumentBankPtr[i], instrumentData, RHYTHM_TYPE_UNDEFINED, 0, newVersion);
 	}
 
-	_rhythmBank = new OplInstrumentDefinition[40];
+	_rhythmBank = _rhythmBankPtr = new OplInstrumentDefinition[40];
 	_rhythmBankFirstNote = 36;
 	_rhythmBankLastNote = 75;
 	// Elvira 1 version uses instruments 1-5 for rhythm, Elvira 2 / Waxworks
@@ -527,7 +527,7 @@ void MidiDriver_Accolade_AdLib::readDriverData(byte *driverData, uint16 driverDa
 			static_cast<OplInstrumentRhythmType>(11 - RHYTHM_NOTE_INSTRUMENT_TYPES[i]);
 		byte *instrumentData = rhythmInstrumentDefinitions + (instrumentDefNumber * 9);
 
-		loadInstrumentData(_rhythmBank[i], instrumentData, rhythmType, rhythmNotes[i], newVersion);
+		loadInstrumentData(_rhythmBankPtr[i], instrumentData, rhythmType, rhythmNotes[i], newVersion);
 	}
 }
 
diff --git a/engines/agos/drivers/accolade/adlib.h b/engines/agos/drivers/accolade/adlib.h
index af2aa2fa953..d56c520ace7 100644
--- a/engines/agos/drivers/accolade/adlib.h
+++ b/engines/agos/drivers/accolade/adlib.h
@@ -64,7 +64,7 @@ protected:
 
 	uint8 allocateOplChannel(uint8 channel, uint8 source, uint8 instrumentId) override;
 	uint16 calculateFrequency(uint8 channel, uint8 source, uint8 note) override;
-	uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum) override;
+	uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) override;
 
 	void writePanning(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED) override;
 	void writeFrequency(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED) override;
@@ -87,6 +87,10 @@ protected:
 	// Points to one of the OPL_NOTE_FREQUENCIES arrays, depending on the driver version
 	const uint16 *_oplNoteFrequencies;
 
+	// Writable pointers to the instrument definitions
+	OplInstrumentDefinition *_instrumentBankPtr;
+	OplInstrumentDefinition *_rhythmBankPtr;
+
 	// Data used by AdLib SFX (Elvira 2 / Waxworks)
 
 	// Instrument definition for each SFX source
diff --git a/engines/agos/drivers/simon1/adlib.cpp b/engines/agos/drivers/simon1/adlib.cpp
index 7f22f50843f..5aea4ccd79d 100644
--- a/engines/agos/drivers/simon1/adlib.cpp
+++ b/engines/agos/drivers/simon1/adlib.cpp
@@ -88,8 +88,8 @@ MidiDriver_Simon1_AdLib::MidiDriver_Simon1_AdLib(OPL::Config::OplType oplType, c
 }
 
 MidiDriver_Simon1_AdLib::~MidiDriver_Simon1_AdLib() {
-	delete[] _instrumentBank;
-	delete[] _rhythmBank;
+	delete[] _instrumentBankPtr;
+	delete[] _rhythmBankPtr;
 }
 
 int MidiDriver_Simon1_AdLib::open() {
@@ -105,33 +105,33 @@ void MidiDriver_Simon1_AdLib::parseInstrumentData(const byte *instrumentData) {
 	const byte *dataPtr = instrumentData;
 
 	// The instrument data consists of 128 16-byte entries.
-	_instrumentBank = new OplInstrumentDefinition[128];
+	_instrumentBank = _instrumentBankPtr = new OplInstrumentDefinition[128];
 
 	for (int i = 0; i < 128; i++) {
-		_instrumentBank[i].fourOperator = false;
-
-		_instrumentBank[i].operator0.freqMultMisc = *dataPtr++;
-		_instrumentBank[i].operator1.freqMultMisc = *dataPtr++;
-		_instrumentBank[i].operator0.level = *dataPtr++;
-		_instrumentBank[i].operator1.level = *dataPtr++;
-		_instrumentBank[i].operator0.decayAttack = *dataPtr++;
-		_instrumentBank[i].operator1.decayAttack = *dataPtr++;
-		_instrumentBank[i].operator0.releaseSustain = *dataPtr++;
-		_instrumentBank[i].operator1.releaseSustain = *dataPtr++;
-		_instrumentBank[i].operator0.waveformSelect = *dataPtr++;
-		_instrumentBank[i].operator1.waveformSelect = *dataPtr++;
-
-		_instrumentBank[i].connectionFeedback0 = *dataPtr++;
-		_instrumentBank[i].connectionFeedback1 = 0;
-		_instrumentBank[i].rhythmNote = 0;
-		_instrumentBank[i].rhythmType = RHYTHM_TYPE_UNDEFINED;
+		_instrumentBankPtr[i].fourOperator = false;
+
+		_instrumentBankPtr[i].operator0.freqMultMisc = *dataPtr++;
+		_instrumentBankPtr[i].operator1.freqMultMisc = *dataPtr++;
+		_instrumentBankPtr[i].operator0.level = *dataPtr++;
+		_instrumentBankPtr[i].operator1.level = *dataPtr++;
+		_instrumentBankPtr[i].operator0.decayAttack = *dataPtr++;
+		_instrumentBankPtr[i].operator1.decayAttack = *dataPtr++;
+		_instrumentBankPtr[i].operator0.releaseSustain = *dataPtr++;
+		_instrumentBankPtr[i].operator1.releaseSustain = *dataPtr++;
+		_instrumentBankPtr[i].operator0.waveformSelect = *dataPtr++;
+		_instrumentBankPtr[i].operator1.waveformSelect = *dataPtr++;
+
+		_instrumentBankPtr[i].connectionFeedback0 = *dataPtr++;
+		_instrumentBankPtr[i].connectionFeedback1 = 0;
+		_instrumentBankPtr[i].rhythmNote = 0;
+		_instrumentBankPtr[i].rhythmType = RHYTHM_TYPE_UNDEFINED;
 
 		// Remaining bytes seem to be unused.
 		dataPtr += 5;
 	}
 
 	// Construct a rhythm bank from the original rhythm map data.
-	_rhythmBank = new OplInstrumentDefinition[39];
+	_rhythmBank = _rhythmBankPtr = new OplInstrumentDefinition[39];
 	// MIDI note range 36-74.
 	_rhythmBankFirstNote = 36;
 	_rhythmBankLastNote = 36 + 39 - 1;
@@ -139,18 +139,18 @@ void MidiDriver_Simon1_AdLib::parseInstrumentData(const byte *instrumentData) {
 	for (int i = 0; i < 39; i++) {
 		if (RHYTHM_MAP[i].channel == 0) {
 			// Some notes in the range have no definition.
-			_rhythmBank[i].rhythmType = RHYTHM_TYPE_UNDEFINED;
+			_rhythmBankPtr[i].rhythmType = RHYTHM_TYPE_UNDEFINED;
 		} else {
 			// The rhythm bank makes use of instruments defined in the main instrument bank.
-			_rhythmBank[i] = _instrumentBank[RHYTHM_MAP[i].program];
+			_rhythmBankPtr[i] = _instrumentBank[RHYTHM_MAP[i].program];
 			// The MIDI channels used in the rhythm map correspond to OPL rhythm instrument types:
 			// 11 - bass drum
 			// 12 - snare drum
 			// 13 - tom tom
 			// 14 - cymbal
 			// 15 - hi-hat
-			_rhythmBank[i].rhythmType = static_cast<OplInstrumentRhythmType>(6 - (RHYTHM_MAP[i].channel - 10));
-			_rhythmBank[i].rhythmNote = RHYTHM_MAP[i].note;
+			_rhythmBankPtr[i].rhythmType = static_cast<OplInstrumentRhythmType>(6 - (RHYTHM_MAP[i].channel - 10));
+			_rhythmBankPtr[i].rhythmNote = RHYTHM_MAP[i].note;
 		}
 	}
 }
@@ -283,7 +283,7 @@ uint16 MidiDriver_Simon1_AdLib::calculateFrequency(uint8 channel, uint8 source,
 	return (octave << 10) | octaveNoteFrequency;
 }
 
-uint8 MidiDriver_Simon1_AdLib::calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
+uint8 MidiDriver_Simon1_AdLib::calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
 	if (channel == MIDI_RHYTHM_CHANNEL && _sources[source].type != SOURCE_TYPE_SFX)
 		// The original interpreter halves the velocity for music rhythm notes.
 		// Note that SFX notes always use max velocity.
diff --git a/engines/agos/drivers/simon1/adlib.h b/engines/agos/drivers/simon1/adlib.h
index 7d4a5fee3c8..548962ffd53 100644
--- a/engines/agos/drivers/simon1/adlib.h
+++ b/engines/agos/drivers/simon1/adlib.h
@@ -64,11 +64,15 @@ private:
 	uint8 allocateOplChannel(uint8 channel, uint8 source, uint8 instrumentId) override;
 	uint16 calculateFrequency(uint8 channel, uint8 source, uint8 note) override;
 	uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity,
-								  OplInstrumentDefinition &instrumentDef, uint8 operatorNum) override;
+								  const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) override;
 	void parseInstrumentData(const byte *instrumentData);
 
 	// True if rhythm notes for sources with type MUSIC should not be played.
 	bool _musicRhythmNotesDisabled;
+
+	// Writable pointers to the instrument definitions
+	OplInstrumentDefinition *_instrumentBankPtr;
+	OplInstrumentDefinition *_rhythmBankPtr;
 };
 
 MidiDriver_Multisource *createMidiDriverSimon1AdLib(const char *instrumentFilename, OPL::Config::OplType);
diff --git a/engines/darkseed/adlib_worx.cpp b/engines/darkseed/adlib_worx.cpp
index 92b355887f1..b7aab07b3b2 100644
--- a/engines/darkseed/adlib_worx.cpp
+++ b/engines/darkseed/adlib_worx.cpp
@@ -23,7 +23,7 @@
 
 namespace Darkseed {
 
-AdLibIbkInstrumentDefinition MidiDriver_Worx_AdLib::WORX_INSTRUMENT_BANK[128] = {
+const AdLibIbkInstrumentDefinition MidiDriver_Worx_AdLib::WORX_INSTRUMENT_BANK[128] = {
 	// 0x00
 	{ 0x01, 0x01, 0x4f, 0x12, 0xf1, 0xd3, 0x50, 0x7c, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00 },
 	{ 0x02, 0x01, 0x50, 0x12, 0xf1, 0xd2, 0x50, 0x76, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00 },
@@ -177,26 +177,26 @@ const uint16 MidiDriver_Worx_AdLib::OPL_NOTE_FREQUENCIES[12] = {
 MidiDriver_Worx_AdLib::MidiDriver_Worx_AdLib(OPL::Config::OplType oplType, int timerFrequency) :
 	MidiDriver_ADLIB_Multisource::MidiDriver_ADLIB_Multisource(oplType, timerFrequency) {
 
-	_instrumentBank = new OplInstrumentDefinition[128];
+	_instrumentBank = _instrumentBankPtr = new OplInstrumentDefinition[128];
 
 	for (int i = 0; i < 128; i++) {
-		WORX_INSTRUMENT_BANK[i].toOplInstrumentDefinition(_instrumentBank[i]);
+		WORX_INSTRUMENT_BANK[i].toOplInstrumentDefinition(_instrumentBankPtr[i]);
 
 		// The original code does not add the key scale level bits (bits 6 and 7)
 		// from the instrument definition to the level before it writes the 0x4x
 		// register value, so effectively, KSL is always disabled for operator 1.
 		// This is probably an oversight, but this behavior is implemented here
 		// by clearing the KSL bits of operator 1 in the instrument definition.
-		_instrumentBank[i].operator1.level &= 0x3F;
+		_instrumentBankPtr[i].operator1.level &= 0x3F;
 	}
-	 
+
 	_defaultChannelVolume = 0x7F;
 	_channel10Melodic = true;
 	_instrumentWriteMode = INSTRUMENT_WRITE_MODE_FIRST_NOTE_ON;
 }
 
 MidiDriver_Worx_AdLib::~MidiDriver_Worx_AdLib() {
-	delete[] _instrumentBank;
+	delete[] _instrumentBankPtr;
 }
 
 uint8 MidiDriver_Worx_AdLib::allocateOplChannel(uint8 channel, uint8 source, uint8 instrumentId) {
@@ -275,7 +275,7 @@ uint16 MidiDriver_Worx_AdLib::calculateFrequency(uint8 channel, uint8 source, ui
 	return oplFrequency | (block << 10);
 }
 
-uint8 MidiDriver_Worx_AdLib::calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
+uint8 MidiDriver_Worx_AdLib::calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
 	// Worx calculates volume by scaling the instrument operator volume by the
 	// channel volume. Note velocity is not used.
 	uint8 operatorVolume = 0x3F - (instrumentDef.getOperatorDefinition(operatorNum).level & 0x3F);
diff --git a/engines/darkseed/adlib_worx.h b/engines/darkseed/adlib_worx.h
index a111f9b705c..57cf36fda19 100644
--- a/engines/darkseed/adlib_worx.h
+++ b/engines/darkseed/adlib_worx.h
@@ -44,7 +44,7 @@ private:
 	 * This was taken from the Dark Seed executable and might have been
 	 * customized for the game.
 	 */
-	static AdLibIbkInstrumentDefinition WORX_INSTRUMENT_BANK[];
+	static const AdLibIbkInstrumentDefinition WORX_INSTRUMENT_BANK[];
 	/**
 	 * The OPL frequency (F-num) for each octave note.
 	 */
@@ -57,7 +57,10 @@ public:
 protected:
 	uint8 allocateOplChannel(uint8 channel, uint8 source, uint8 instrumentId) override;
 	uint16 calculateFrequency(uint8 channel, uint8 source, uint8 note) override;
-	uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum) override;
+	uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) override;
+
+	// Writable pointers to the instrument definitions
+	OplInstrumentDefinition *_instrumentBankPtr;
 };
 
 } // namespace Darkseed
diff --git a/engines/lure/sound.cpp b/engines/lure/sound.cpp
index 18631cb4f67..5bc1e64e00b 100644
--- a/engines/lure/sound.cpp
+++ b/engines/lure/sound.cpp
@@ -1049,7 +1049,7 @@ int32 MidiDriver_ADLIB_Lure::calculatePitchBend(uint8 channel, uint8 source, uin
 	return newPitchBend;
 }
 
-uint8 MidiDriver_ADLIB_Lure::calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
+uint8 MidiDriver_ADLIB_Lure::calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) {
 	uint8 operatorVolume = instrumentDef.getOperatorDefinition(operatorNum).level & OPL_MASK_LEVEL;
 
 	// Scale the instrument definition operator volume by velocity.
diff --git a/engines/lure/sound.h b/engines/lure/sound.h
index 2cb9d5f63b4..89313235f94 100644
--- a/engines/lure/sound.h
+++ b/engines/lure/sound.h
@@ -184,7 +184,7 @@ protected:
 	// Returns the number of semitones in bits 8+ and an 8 bit fraction of a
 	// semitone.
 	int32 calculatePitchBend(uint8 channel, uint8 source, uint16 oplFrequency) override;
-	uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum) override;
+	uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) override;
 
 	// Stores the instrument definitions set by sequencer meta events.
 	OplInstrumentDefinition _instrumentDefs[LURE_MAX_SOURCES][MIDI_CHANNEL_COUNT];
diff --git a/engines/ultima/nuvie/sound/mididrv_m_adlib.cpp b/engines/ultima/nuvie/sound/mididrv_m_adlib.cpp
index 54cc51eabf9..9aa7505dc78 100644
--- a/engines/ultima/nuvie/sound/mididrv_m_adlib.cpp
+++ b/engines/ultima/nuvie/sound/mididrv_m_adlib.cpp
@@ -45,11 +45,11 @@ MidiDriver_M_AdLib::MidiDriver_M_AdLib() : MidiDriver_ADLIB_Multisource(OPL::Con
 	Common::fill(_fadeStepDelays, _fadeStepDelays + ARRAYSIZE(_fadeStepDelays), 0);
 	Common::fill(_fadeCurrentDelays, _fadeCurrentDelays + ARRAYSIZE(_fadeCurrentDelays), 0);
 
-	_instrumentBank = new OplInstrumentDefinition[16];
+	_instrumentBank = _instrumentBankPtr = new OplInstrumentDefinition[16];
 }
 
 MidiDriver_M_AdLib::~MidiDriver_M_AdLib() {
-	delete[] _instrumentBank;
+	delete[] _instrumentBankPtr;
 }
 
 void MidiDriver_M_AdLib::send(int8 source, uint32 b) {
@@ -244,7 +244,7 @@ void MidiDriver_M_AdLib::metaEvent(int8 source, byte type, byte* data, uint16 le
 
 		byte instrumentNumber = data[0];
 		assert(instrumentNumber < 16);
-		OplInstrumentDefinition *instrument = &_instrumentBank[instrumentNumber];
+		OplInstrumentDefinition *instrument = &_instrumentBankPtr[instrumentNumber];
 
 		instrument->fourOperator = false;
 		instrument->rhythmType = RHYTHM_TYPE_UNDEFINED;
@@ -309,7 +309,7 @@ uint16 MidiDriver_M_AdLib::calculateFrequency(uint8 channel, uint8 source, uint8
 	return oplFrequency | (block << 10);
 }
 
-uint8 MidiDriver_M_AdLib::calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition& instrumentDef, uint8 operatorNum) {
+uint8 MidiDriver_M_AdLib::calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition& instrumentDef, uint8 operatorNum) {
 	// M directy uses OPL level values, so no calculation is necessary.
 	return _controlData[source][channel].volume & OPL_MASK_LEVEL;
 }
diff --git a/engines/ultima/nuvie/sound/mididrv_m_adlib.h b/engines/ultima/nuvie/sound/mididrv_m_adlib.h
index a1404c051db..2351bd4e9e2 100644
--- a/engines/ultima/nuvie/sound/mididrv_m_adlib.h
+++ b/engines/ultima/nuvie/sound/mididrv_m_adlib.h
@@ -65,7 +65,7 @@ protected:
 
 	uint8 allocateOplChannel(uint8 channel, uint8 source, uint8 instrumentId) override;
 	uint16 calculateFrequency(uint8 channel, uint8 source, uint8 note) override;
-	uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum) override;
+	uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) override;
 	void writeVolume(uint8 oplChannel, uint8 operatorNum, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED) override;
 
 	void deinitSource(uint8 source) override;
@@ -94,6 +94,8 @@ protected:
 	uint8 _fadeStepDelays[9];
 	// The current fade delay counter value for each channel.
 	uint8 _fadeCurrentDelays[9];
+	// Writable pointers to the instrument definitions
+	OplInstrumentDefinition *_instrumentBankPtr;
 };
 
 } // End of namespace Nuvie


Commit: c7e3415511317900a0ae28ddda4a607d094b7a5b
    https://github.com/scummvm/scummvm/commit/c7e3415511317900a0ae28ddda4a607d094b7a5b
Author: Le Philousophe (lephilousophe at users.noreply.github.com)
Date: 2025-01-28T23:24:40+02:00

Commit Message:
AUDIO: Avoid duplicating pointers by making use of const_cast

const_cast is valid in these cases because the pointed data is
dynamically allocated.

Changed paths:
    engines/agos/drivers/accolade/adlib.cpp
    engines/agos/drivers/accolade/adlib.h
    engines/agos/drivers/simon1/adlib.cpp
    engines/agos/drivers/simon1/adlib.h
    engines/darkseed/adlib_worx.cpp
    engines/darkseed/adlib_worx.h
    engines/ultima/nuvie/sound/mididrv_m_adlib.cpp
    engines/ultima/nuvie/sound/mididrv_m_adlib.h


diff --git a/engines/agos/drivers/accolade/adlib.cpp b/engines/agos/drivers/accolade/adlib.cpp
index d304f619bdd..3b7033237b8 100644
--- a/engines/agos/drivers/accolade/adlib.cpp
+++ b/engines/agos/drivers/accolade/adlib.cpp
@@ -68,8 +68,8 @@ const uint16 MidiDriver_Accolade_AdLib::OPL_NOTE_FREQUENCIES_MUSIC_DRV[] = {
 // I have currently not implemented dynamic channel allocation.
 MidiDriver_Accolade_AdLib::MidiDriver_Accolade_AdLib(OPL::Config::OplType oplType, bool newVersion, int timerFrequency) :
 		MidiDriver_ADLIB_Multisource(oplType, timerFrequency) {
-	_instrumentBank = _instrumentBankPtr = nullptr;
-	_rhythmBank = _rhythmBankPtr = nullptr;
+	_instrumentBank = nullptr;
+	_rhythmBank = nullptr;
 	_newVersion = newVersion;
 	_oplNoteFrequencies = _newVersion ? OPL_NOTE_FREQUENCIES_MUSIC_DRV : OPL_NOTE_FREQUENCIES_INSTR_DAT;
 
@@ -81,10 +81,10 @@ MidiDriver_Accolade_AdLib::MidiDriver_Accolade_AdLib(OPL::Config::OplType oplTyp
 }
 
 MidiDriver_Accolade_AdLib::~MidiDriver_Accolade_AdLib() {
-	if (_instrumentBankPtr)
-		delete[] _instrumentBankPtr;
-	if (_rhythmBankPtr)
-		delete[] _rhythmBankPtr;
+	if (_instrumentBank)
+		delete[] _instrumentBank;
+	if (_rhythmBank)
+		delete[] _rhythmBank;
 }
 
 int MidiDriver_Accolade_AdLib::open() {
@@ -263,8 +263,11 @@ void MidiDriver_Accolade_AdLib::patchE1Instruments() {
 		// This workaround is only needed for OPL3 mode.
 		return;
 
+	// This is allocated in readDriverData so it's not really const
+	OplInstrumentDefinition *instrumentBank = const_cast<OplInstrumentDefinition *>(_instrumentBank);
+
 	// Patch the attack and decay of instrument 0x18.
-	_instrumentBankPtr[0x18].operator0.decayAttack = 0x42; // Was 0x24
+	instrumentBank[0x18].operator0.decayAttack = 0x42; // Was 0x24
 }
 
 void MidiDriver_Accolade_AdLib::patchWwInstruments() {
@@ -286,19 +289,22 @@ void MidiDriver_Accolade_AdLib::patchWwInstruments() {
 		// This workaround is only needed for OPL3 mode.
 		return;
 
+	// This is allocated in readDriverData so it's not really const
+	OplInstrumentDefinition *instrumentBank = const_cast<OplInstrumentDefinition *>(_instrumentBank);
+
 	// Patch the attack of instrument 0x22.
-	_instrumentBankPtr[0x22].operator1.decayAttack &= 0x0F;
-	_instrumentBankPtr[0x22].operator1.decayAttack |= 0x50;
+	instrumentBank[0x22].operator1.decayAttack &= 0x0F;
+	instrumentBank[0x22].operator1.decayAttack |= 0x50;
 
 	// Patch the attack of instrument 0x25.
-	_instrumentBankPtr[0x25].operator1.decayAttack &= 0x0F;
-	_instrumentBankPtr[0x25].operator1.decayAttack |= 0x60;
+	instrumentBank[0x25].operator1.decayAttack &= 0x0F;
+	instrumentBank[0x25].operator1.decayAttack |= 0x60;
 
 	// Patch the attack of instrument 0x7F.
-	_instrumentBankPtr[0x7F].operator0.decayAttack &= 0x0F;
-	_instrumentBankPtr[0x7F].operator0.decayAttack |= 0x60;
-	_instrumentBankPtr[0x7F].operator1.decayAttack &= 0x0F;
-	_instrumentBankPtr[0x7F].operator1.decayAttack |= 0x90;
+	instrumentBank[0x7F].operator0.decayAttack &= 0x0F;
+	instrumentBank[0x7F].operator0.decayAttack |= 0x60;
+	instrumentBank[0x7F].operator1.decayAttack &= 0x0F;
+	instrumentBank[0x7F].operator1.decayAttack |= 0x90;
 }
 
 MidiDriver_Accolade_AdLib::InstrumentInfo MidiDriver_Accolade_AdLib::determineInstrument(uint8 channel, uint8 source, uint8 note) {
@@ -508,13 +514,13 @@ void MidiDriver_Accolade_AdLib::readDriverData(byte *driverData, uint16 driverDa
 	uint16 rhythmNoteOffset = newVersion ? 376 + 36 : 256 + 16 + 16;
 	uint16 instrumentDataOffset = newVersion ? 722 : 256 + 16 + 16 + 64 + 2;
 
-	_instrumentBank = _instrumentBankPtr = new OplInstrumentDefinition[instrumentDefinitionCount];
+	OplInstrumentDefinition *instrumentBank = new OplInstrumentDefinition[instrumentDefinitionCount];
 	for (int i = 0; i < instrumentDefinitionCount; i++) {
 		byte *instrumentData = driverData + instrumentDataOffset + (i * 9);
-		loadInstrumentData(_instrumentBankPtr[i], instrumentData, RHYTHM_TYPE_UNDEFINED, 0, newVersion);
+		loadInstrumentData(instrumentBank[i], instrumentData, RHYTHM_TYPE_UNDEFINED, 0, newVersion);
 	}
 
-	_rhythmBank = _rhythmBankPtr = new OplInstrumentDefinition[40];
+	OplInstrumentDefinition *rhythmBank = new OplInstrumentDefinition[40];
 	_rhythmBankFirstNote = 36;
 	_rhythmBankLastNote = 75;
 	// Elvira 1 version uses instruments 1-5 for rhythm, Elvira 2 / Waxworks
@@ -527,8 +533,12 @@ void MidiDriver_Accolade_AdLib::readDriverData(byte *driverData, uint16 driverDa
 			static_cast<OplInstrumentRhythmType>(11 - RHYTHM_NOTE_INSTRUMENT_TYPES[i]);
 		byte *instrumentData = rhythmInstrumentDefinitions + (instrumentDefNumber * 9);
 
-		loadInstrumentData(_rhythmBankPtr[i], instrumentData, rhythmType, rhythmNotes[i], newVersion);
+		loadInstrumentData(rhythmBank[i], instrumentData, rhythmType, rhythmNotes[i], newVersion);
 	}
+
+	// Set the const class variables with our just allocated banks
+	_instrumentBank = instrumentBank;
+	_rhythmBank = rhythmBank;
 }
 
 MidiDriver_Multisource *MidiDriver_Accolade_AdLib_create(Common::String driverFilename, OPL::Config::OplType oplType, int timerFrequency) {
diff --git a/engines/agos/drivers/accolade/adlib.h b/engines/agos/drivers/accolade/adlib.h
index d56c520ace7..abbdd03d104 100644
--- a/engines/agos/drivers/accolade/adlib.h
+++ b/engines/agos/drivers/accolade/adlib.h
@@ -87,10 +87,6 @@ protected:
 	// Points to one of the OPL_NOTE_FREQUENCIES arrays, depending on the driver version
 	const uint16 *_oplNoteFrequencies;
 
-	// Writable pointers to the instrument definitions
-	OplInstrumentDefinition *_instrumentBankPtr;
-	OplInstrumentDefinition *_rhythmBankPtr;
-
 	// Data used by AdLib SFX (Elvira 2 / Waxworks)
 
 	// Instrument definition for each SFX source
diff --git a/engines/agos/drivers/simon1/adlib.cpp b/engines/agos/drivers/simon1/adlib.cpp
index 5aea4ccd79d..b402aa8d575 100644
--- a/engines/agos/drivers/simon1/adlib.cpp
+++ b/engines/agos/drivers/simon1/adlib.cpp
@@ -88,8 +88,8 @@ MidiDriver_Simon1_AdLib::MidiDriver_Simon1_AdLib(OPL::Config::OplType oplType, c
 }
 
 MidiDriver_Simon1_AdLib::~MidiDriver_Simon1_AdLib() {
-	delete[] _instrumentBankPtr;
-	delete[] _rhythmBankPtr;
+	delete[] _instrumentBank;
+	delete[] _rhythmBank;
 }
 
 int MidiDriver_Simon1_AdLib::open() {
@@ -105,33 +105,33 @@ void MidiDriver_Simon1_AdLib::parseInstrumentData(const byte *instrumentData) {
 	const byte *dataPtr = instrumentData;
 
 	// The instrument data consists of 128 16-byte entries.
-	_instrumentBank = _instrumentBankPtr = new OplInstrumentDefinition[128];
+	OplInstrumentDefinition *instrumentBank = new OplInstrumentDefinition[128];
 
 	for (int i = 0; i < 128; i++) {
-		_instrumentBankPtr[i].fourOperator = false;
-
-		_instrumentBankPtr[i].operator0.freqMultMisc = *dataPtr++;
-		_instrumentBankPtr[i].operator1.freqMultMisc = *dataPtr++;
-		_instrumentBankPtr[i].operator0.level = *dataPtr++;
-		_instrumentBankPtr[i].operator1.level = *dataPtr++;
-		_instrumentBankPtr[i].operator0.decayAttack = *dataPtr++;
-		_instrumentBankPtr[i].operator1.decayAttack = *dataPtr++;
-		_instrumentBankPtr[i].operator0.releaseSustain = *dataPtr++;
-		_instrumentBankPtr[i].operator1.releaseSustain = *dataPtr++;
-		_instrumentBankPtr[i].operator0.waveformSelect = *dataPtr++;
-		_instrumentBankPtr[i].operator1.waveformSelect = *dataPtr++;
-
-		_instrumentBankPtr[i].connectionFeedback0 = *dataPtr++;
-		_instrumentBankPtr[i].connectionFeedback1 = 0;
-		_instrumentBankPtr[i].rhythmNote = 0;
-		_instrumentBankPtr[i].rhythmType = RHYTHM_TYPE_UNDEFINED;
+		instrumentBank[i].fourOperator = false;
+
+		instrumentBank[i].operator0.freqMultMisc = *dataPtr++;
+		instrumentBank[i].operator1.freqMultMisc = *dataPtr++;
+		instrumentBank[i].operator0.level = *dataPtr++;
+		instrumentBank[i].operator1.level = *dataPtr++;
+		instrumentBank[i].operator0.decayAttack = *dataPtr++;
+		instrumentBank[i].operator1.decayAttack = *dataPtr++;
+		instrumentBank[i].operator0.releaseSustain = *dataPtr++;
+		instrumentBank[i].operator1.releaseSustain = *dataPtr++;
+		instrumentBank[i].operator0.waveformSelect = *dataPtr++;
+		instrumentBank[i].operator1.waveformSelect = *dataPtr++;
+
+		instrumentBank[i].connectionFeedback0 = *dataPtr++;
+		instrumentBank[i].connectionFeedback1 = 0;
+		instrumentBank[i].rhythmNote = 0;
+		instrumentBank[i].rhythmType = RHYTHM_TYPE_UNDEFINED;
 
 		// Remaining bytes seem to be unused.
 		dataPtr += 5;
 	}
 
 	// Construct a rhythm bank from the original rhythm map data.
-	_rhythmBank = _rhythmBankPtr = new OplInstrumentDefinition[39];
+	OplInstrumentDefinition *rhythmBank = new OplInstrumentDefinition[39];
 	// MIDI note range 36-74.
 	_rhythmBankFirstNote = 36;
 	_rhythmBankLastNote = 36 + 39 - 1;
@@ -139,20 +139,24 @@ void MidiDriver_Simon1_AdLib::parseInstrumentData(const byte *instrumentData) {
 	for (int i = 0; i < 39; i++) {
 		if (RHYTHM_MAP[i].channel == 0) {
 			// Some notes in the range have no definition.
-			_rhythmBankPtr[i].rhythmType = RHYTHM_TYPE_UNDEFINED;
+			rhythmBank[i].rhythmType = RHYTHM_TYPE_UNDEFINED;
 		} else {
 			// The rhythm bank makes use of instruments defined in the main instrument bank.
-			_rhythmBankPtr[i] = _instrumentBank[RHYTHM_MAP[i].program];
+			rhythmBank[i] = _instrumentBank[RHYTHM_MAP[i].program];
 			// The MIDI channels used in the rhythm map correspond to OPL rhythm instrument types:
 			// 11 - bass drum
 			// 12 - snare drum
 			// 13 - tom tom
 			// 14 - cymbal
 			// 15 - hi-hat
-			_rhythmBankPtr[i].rhythmType = static_cast<OplInstrumentRhythmType>(6 - (RHYTHM_MAP[i].channel - 10));
-			_rhythmBankPtr[i].rhythmNote = RHYTHM_MAP[i].note;
+			rhythmBank[i].rhythmType = static_cast<OplInstrumentRhythmType>(6 - (RHYTHM_MAP[i].channel - 10));
+			rhythmBank[i].rhythmNote = RHYTHM_MAP[i].note;
 		}
 	}
+
+	// Set the const class variables with our just allocated banks
+	_instrumentBank = instrumentBank;
+	_rhythmBank = rhythmBank;
 }
 
 void MidiDriver_Simon1_AdLib::noteOn(uint8 channel, uint8 note, uint8 velocity, uint8 source) {
diff --git a/engines/agos/drivers/simon1/adlib.h b/engines/agos/drivers/simon1/adlib.h
index 548962ffd53..a878e9c407c 100644
--- a/engines/agos/drivers/simon1/adlib.h
+++ b/engines/agos/drivers/simon1/adlib.h
@@ -69,10 +69,6 @@ private:
 
 	// True if rhythm notes for sources with type MUSIC should not be played.
 	bool _musicRhythmNotesDisabled;
-
-	// Writable pointers to the instrument definitions
-	OplInstrumentDefinition *_instrumentBankPtr;
-	OplInstrumentDefinition *_rhythmBankPtr;
 };
 
 MidiDriver_Multisource *createMidiDriverSimon1AdLib(const char *instrumentFilename, OPL::Config::OplType);
diff --git a/engines/darkseed/adlib_worx.cpp b/engines/darkseed/adlib_worx.cpp
index b7aab07b3b2..976abeca229 100644
--- a/engines/darkseed/adlib_worx.cpp
+++ b/engines/darkseed/adlib_worx.cpp
@@ -177,26 +177,29 @@ const uint16 MidiDriver_Worx_AdLib::OPL_NOTE_FREQUENCIES[12] = {
 MidiDriver_Worx_AdLib::MidiDriver_Worx_AdLib(OPL::Config::OplType oplType, int timerFrequency) :
 	MidiDriver_ADLIB_Multisource::MidiDriver_ADLIB_Multisource(oplType, timerFrequency) {
 
-	_instrumentBank = _instrumentBankPtr = new OplInstrumentDefinition[128];
+	OplInstrumentDefinition *instrumentBank = new OplInstrumentDefinition[128];
 
 	for (int i = 0; i < 128; i++) {
-		WORX_INSTRUMENT_BANK[i].toOplInstrumentDefinition(_instrumentBankPtr[i]);
+		WORX_INSTRUMENT_BANK[i].toOplInstrumentDefinition(instrumentBank[i]);
 
 		// The original code does not add the key scale level bits (bits 6 and 7)
 		// from the instrument definition to the level before it writes the 0x4x
 		// register value, so effectively, KSL is always disabled for operator 1.
 		// This is probably an oversight, but this behavior is implemented here
 		// by clearing the KSL bits of operator 1 in the instrument definition.
-		_instrumentBankPtr[i].operator1.level &= 0x3F;
+		instrumentBank[i].operator1.level &= 0x3F;
 	}
 
+	// Set the const class variable with our just allocated bank
+	_instrumentBank = instrumentBank;
+
 	_defaultChannelVolume = 0x7F;
 	_channel10Melodic = true;
 	_instrumentWriteMode = INSTRUMENT_WRITE_MODE_FIRST_NOTE_ON;
 }
 
 MidiDriver_Worx_AdLib::~MidiDriver_Worx_AdLib() {
-	delete[] _instrumentBankPtr;
+	delete[] _instrumentBank;
 }
 
 uint8 MidiDriver_Worx_AdLib::allocateOplChannel(uint8 channel, uint8 source, uint8 instrumentId) {
diff --git a/engines/darkseed/adlib_worx.h b/engines/darkseed/adlib_worx.h
index 57cf36fda19..b6eda97195a 100644
--- a/engines/darkseed/adlib_worx.h
+++ b/engines/darkseed/adlib_worx.h
@@ -58,9 +58,6 @@ protected:
 	uint8 allocateOplChannel(uint8 channel, uint8 source, uint8 instrumentId) override;
 	uint16 calculateFrequency(uint8 channel, uint8 source, uint8 note) override;
 	uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum) override;
-
-	// Writable pointers to the instrument definitions
-	OplInstrumentDefinition *_instrumentBankPtr;
 };
 
 } // namespace Darkseed
diff --git a/engines/ultima/nuvie/sound/mididrv_m_adlib.cpp b/engines/ultima/nuvie/sound/mididrv_m_adlib.cpp
index 9aa7505dc78..e52dd1f9d57 100644
--- a/engines/ultima/nuvie/sound/mididrv_m_adlib.cpp
+++ b/engines/ultima/nuvie/sound/mididrv_m_adlib.cpp
@@ -45,11 +45,11 @@ MidiDriver_M_AdLib::MidiDriver_M_AdLib() : MidiDriver_ADLIB_Multisource(OPL::Con
 	Common::fill(_fadeStepDelays, _fadeStepDelays + ARRAYSIZE(_fadeStepDelays), 0);
 	Common::fill(_fadeCurrentDelays, _fadeCurrentDelays + ARRAYSIZE(_fadeCurrentDelays), 0);
 
-	_instrumentBank = _instrumentBankPtr = new OplInstrumentDefinition[16];
+	_instrumentBank = new OplInstrumentDefinition[16];
 }
 
 MidiDriver_M_AdLib::~MidiDriver_M_AdLib() {
-	delete[] _instrumentBankPtr;
+	delete[] _instrumentBank;
 }
 
 void MidiDriver_M_AdLib::send(int8 source, uint32 b) {
@@ -244,7 +244,8 @@ void MidiDriver_M_AdLib::metaEvent(int8 source, byte type, byte* data, uint16 le
 
 		byte instrumentNumber = data[0];
 		assert(instrumentNumber < 16);
-		OplInstrumentDefinition *instrument = &_instrumentBankPtr[instrumentNumber];
+		// This was allocated in the constructor so it's not really const
+		OplInstrumentDefinition *instrument = const_cast<OplInstrumentDefinition *>(&_instrumentBank[instrumentNumber]);
 
 		instrument->fourOperator = false;
 		instrument->rhythmType = RHYTHM_TYPE_UNDEFINED;
diff --git a/engines/ultima/nuvie/sound/mididrv_m_adlib.h b/engines/ultima/nuvie/sound/mididrv_m_adlib.h
index 2351bd4e9e2..35e1d69fa6f 100644
--- a/engines/ultima/nuvie/sound/mididrv_m_adlib.h
+++ b/engines/ultima/nuvie/sound/mididrv_m_adlib.h
@@ -94,8 +94,6 @@ protected:
 	uint8 _fadeStepDelays[9];
 	// The current fade delay counter value for each channel.
 	uint8 _fadeCurrentDelays[9];
-	// Writable pointers to the instrument definitions
-	OplInstrumentDefinition *_instrumentBankPtr;
 };
 
 } // End of namespace Nuvie




More information about the Scummvm-git-logs mailing list