src/OPL2.cpp
/**
 * Set the given instrument to a channel. An optional volume may be provided to assign to proper output levels for the
 * operators.
 */
Set the given instrument to a channel. An optional volume may be provided to assign to proper output levels for the
operators.
Set the given instrument to a channel . An optional volume may be provided to assign to proper output levels for the operators .
void OPL2::setInstrument(byte channel, Instrument instrument, float volume) {
	channel = max(ZERO, min(channel, CHANNEL_MAX));
	volume = max(VOLUME_MIN, min(volume, VOLUME_MAX));
	setWaveFormSelect(true);
	for (byte op = OPERATOR1; op <= OPERATOR2; op ++) {
		byte outputLevel = 63 - (byte)((63.0 - (float)instrument.operators[op].outputLevel) * volume);
		byte registerOffset = registerOffsets[op][channel];
		write(0x20 + registerOffset,
			(instrument.operators[op].hasTremolo ? 0x80 : 0x00) +
			(instrument.operators[op].hasVibrato ? 0x40 : 0x00) +
			(instrument.operators[op].hasSustain ? 0x20 : 0x00) +
			(instrument.operators[op].hasEnvelopeScaling ? 0x10 : 0x00) +
			(instrument.operators[op].frequencyMultiplier & 0x0F));
		write(0x40 + registerOffset,
			((instrument.operators[op].keyScaleLevel & 0x03) << 6) +
			(outputLevel & 0x3F));
		write(0x60 + registerOffset,
			((instrument.operators[op].attack & 0x0F) << 4) +
			(instrument.operators[op].decay & 0x0F));
		write(0x80 + registerOffset,
			((instrument.operators[op].sustain & 0x0F) << 4) +
			(instrument.operators[op].release & 0x0F));
		write(0xE0 + registerOffset,
			(instrument.operators[op].waveForm & 0x03));
	}
	write(0xC0 + channel,
		((instrument.feedback & 0x07) << 1) +
		(instrument.isAdditiveSynth ? 0x01 : 0x00));
}
void OPL2 :: setInstrument ( byte channel , Instrument instrument , float volume ) { channel = max ( ZERO , min ( channel , CHANNEL_MAX ) ) ; volume = max ( VOLUME_MIN , min ( volume , VOLUME_MAX ) ) ; setWaveFormSelect ( true ) ; for ( byte op = OPERATOR1 ; op <= OPERATOR2 ; op ++ ) { byte outputLevel = 63 - ( byte ) ( ( 63.0 - ( float ) instrument . operators [ op ] . outputLevel ) * volume ) ; byte registerOffset = registerOffsets [ op ] [ channel ] ; write ( 0x20 + registerOffset , ( instrument . operators [ op ] . hasTremolo ? 0x80 : 0x00 ) + ( instrument . operators [ op ] . hasVibrato ? 0x40 : 0x00 ) + ( instrument . operators [ op ] . hasSustain ? 0x20 : 0x00 ) + ( instrument . operators [ op ] . hasEnvelopeScaling ? 0x10 : 0x00 ) + ( instrument . operators [ op ] . frequencyMultiplier & 0x0F ) ) ; write ( 0x40 + registerOffset , ( ( instrument . operators [ op ] . keyScaleLevel & 0x03 ) << 6 ) + ( outputLevel & 0x3F ) ) ; write ( 0x60 + registerOffset , ( ( instrument . operators [ op ] . attack & 0x0F ) << 4 ) + ( instrument . operators [ op ] . decay & 0x0F ) ) ; write ( 0x80 + registerOffset , ( ( instrument . operators [ op ] . sustain & 0x0F ) << 4 ) + ( instrument . operators [ op ] . release & 0x0F ) ) ; write ( 0xE0 + registerOffset , ( instrument . operators [ op ] . waveForm & 0x03 ) ) ; } write ( 0xC0 + channel , ( ( instrument . feedback & 0x07 ) << 1 ) + ( instrument . isAdditiveSynth ? 0x01 : 0x00 ) ) ; }
Set the given instrument to a channel.
Set the given instrument to a channel .