API Reference Manual - DhrBaksteen/ArduinoOPL2 GitHub Wiki

API Reference

This section describes all API functions that the OPL2 library has to offer and their effects on the YM3812 if applicable.

Constructor

Constructor

Before the OPL2 library can be used we first must create an instance of it. There are two constructors available for this; a default constructor which will create a new library instance with default pin numbers and a constructor that allows you to define custom pin numbers for the reset, address and latch pins of the OPL2 Audio Board. The default pins are 8, 9 and 10 respectively (GPIO 18, 16 and 12 for Raspberry Pi). The data and clock pins cannot be reassigned. After an instance is created the library must be initialized by calling the init() function.

Prototype

OPL2::OPL2();
OPL2::OPL2(byte reset, byte address, byte latch);

Arguments

  • byte reset - The Arduino pin connected to the reset pin of the OPL2 Audio Board
  • byte address - The Arduino pin connected to the address pin of the OPL2 Audio Board
  • byte latch - The Arduino pin connected to the latch pin of the OPL2 Audio Board

See also

init, reset

init

init

The init function initializes the library and the OPL2 Audio Board by preparing the IO pins of the Arduino and resetting the chip by calling the reset() function. This function must be called after creating the library instance, before calling any of the library functions.

Prototype

OPL2::init();

Example

// Initialize the OPL2 library.
OPL2 opl2();
opl2.init();

See also

Constructor, reset

reset

reset

Reset the OPL2 and clear internal registers. This stops all sound and resets the registers to all zeros. This function is called internally when the library is initialized though the init function.

Prototype

OPL2::reset();

See also

Constructor, init

Register getters:

getAttack

getAttack

Get the attck rate of the given channel and operator. The higher the value the faster the attack.

  • When set to 0 the sound will never attack
  • When set to 15 the sound will immediately be at maximum level

Prototype

`byte OPL2::getAttack(byte channel, byte operatorNum);`
Arguments
  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to set attack rate: OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER
Returns

byte The attack rate of the operator [0, 15]

OPL Registers

Reads register based at 0x60, bit 4 through 7

See also

setAttack, getDecay, getSustain, getRelease

getBlock

getBlock

Get the current frequency block for the given channel. The frequency block determines the frequency range of the F-numbers. This is sometimes interpreted as the current octave.

Prototype

`byte OPL2::getBlock(byte channel);`
Arguments
  • byte channel - Index of the channel [0, 8]
Returns

byte - The current frequency block of the channel

Block F-num = 1 F-num = 1023
0 0.047 Hz 45.503 Hz
1 0.094 Hz 97.006 Hz
2 0.189 Hz 194.013 Hz
3 0.379 Hz 388.026 Hz
4 0.758 Hz 776.053 Hz
5 1.517 Hz 1,552.107 Hz
6 3.034 Hz 3,104.215 Hz
7 6.068 Hz 6,208.431 Hz

OPL Registers

Reads register based at 0xB0, bits 2 through 4

See also

setBlock, getFrequencyBlock, getFNumber, getFrequency, getFrequencyStep

getDecay

getDecay

Get the decay rate of the given channel and operator. The higher the value the quicker the decay to sustain level will be.

  • When set to 0 the sound will never decay to sustain level, but remain at maximum output level

Prototype

`byte OPL2::getDecay(byte channel, byte operatorNum);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to get decay rate of: OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER

Returns

byte The decay rate of the operator [0, 15]

OPL Registers

Reads register based at 0x60, bits 0 through 3

See also

setDecay, getAttack, getSustain, getRelease

getDeepTremolo

getDeepTremolo

Returns whether deep tremolo is enabled. This is a chip-wide setting that applies to all operators.

  • When disabled (default) tremolo depth is 1.0 dB
  • when deep tremolo is enabled all tremolos have a depth of 4.8 dB

Prototype

`bool OPL2::getDeepTremolo();`

Returns

bool If true then deep tremolo applies for all operators

OPL Registers

Reads register 0xBD, bit 7

See also

setDeepTremolo, getTremolo, getDeepVibrato

getDeepVibrato

getDeepVibrato

Returns whether deep vibrato is enabled. This is a chip-wide setting that applies to all operators.

  • When disabled (default) vibrato will be 7/100 semi-tones
  • When enabled it will be doubled to 14/100 semi-tones

Prototype

`bool OPL2::getDeepVibrato();`

Returns

bool When true then deep vibrato is enabled for all operators

OPL Registers

Reads register 0xBD, bit 6

See also

setDeepVibrato, getVibrato, getDeepTremolo

getDrums

getDrums

Returns a value where each bit indicates what drum sound is currently active. In order to play drum sounds the OPL2 must be set into percussive mode first using setPercussive.

Prototype

`byte OPL2::getDrums();`

Returns

byte A value that indicates what drum sounds are currently playing:

Bit Drum Sound Bit mask Library Definition
0 Hi-hat 0x01 DRUM_HI_HAT
1 Cymbal 0x02 DRUM_CYMBAL
2 Tom tom 0x04 DRUM_TOM
3 Snare drum 0x08 DRUM_SNARE
4 Bass drum 0x10 DRUM_BASS

OPL Registers

Reads from register 0xBD, bits 0 through 4

See also

setDrums, getPercussive

getEnvelopeScaling

getEnvelopeScaling

Returns wheter envelope scaling (KSR) is enabled. When enabled the duration of the envelope will be shorter as the frequency of the operator increases. By default envelope scaling will be disabled.

Prototype

`bool OPL2::getEnvelopeScaling(byte channel, byte operatorNum);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to get KSR: OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER

Returns

bool If true then envelope scaling is enabled

OPL Registers

Reads from register based at 0x20, bit 4

See also

setEnvelopeScaling

getFeedback

getFeedback

Get the feedback modulation factor for the given channel. When set to a value greater than 0 then operator 1 will send part of its otput back into itself.

Prototype

`byte OPL2::getFeedback(byte channel);`

Arguments

  • byte channel - Index of the channel for which to get feedback factor [0, 8]

Returns

byte Feedback modulation factor of the given channel

Feedback Factor
0 0, No feedback
1 π/16
2 π/8
3 π/4
4 π/2
5 π
6
7

OPL Registers

Reads from register based at 0xC0, bits 1 through 3

See also

setFeedback

getFNumber

getFNumber

Get the frequency number for the given channel. The actual frequency of the channel depends on the frequency block set using the setBlock function. When the F-number of a channel is set to 0 then its output frequency will always be 0 Hz, regardless of the block.

Prototype

`short OPL2::getFNumber(byte channel);`

Arguments

  • byte channel - Index of the channel for which to get the F-number [0, 8]

Returns

short The frequency number [0, 1023]

OPL Registers

Reads from register based at 0xB0, bits 0 and 1 for the hi bits of the F-number and register based at 0xA0 for the lower 8 bits

See also

setFNumber, getFrequency, getBlock, getFrequencyBlock

getKeyOn

getKeyOn

Returns whether the channel is currently generating a sound.

Prototype

`bool OPL2::getKeyOn(byte channel);`

Arguments

  • byte channel - Index of the channel for which to get key on [0, 8]

Returns

bool If true then the channel is generating sound, if false then the channel is in the release portion of its envelope or silent

OPL Registers

Reads from register based at 0xB0, bit 5

See also

setKeyOn

getMaintainSustain

getMaintainSustain

Returns whether sustain is enabled on the given channel and operator.

  • If sustain is enabled then setSustain can be used to define a sustain level of an operator. The operator will remain at sustain level until the key is set to off with setKeyOn (false)
  • If disabled (default) then the operator will immediately jump to the release portion of its envelope after the decay period

Prototype

`bool OPL2::getMaintainSustain(byte channel, byte operatorNum);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to get wheter sustain is enabled: OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER

Returns

bool If true then sustain is enabled on the operator

OPL Registers

Reads from register based at 0x20, bit 5

See also

setMaintainSustain, setSustain, setKeyOn

getMultiplier

getMultiplier

Get the frequency multiplication factor of the given channel and operator. The multiplication factor will be equal to the value set, except when set to 0 (default) when then multiplication factor will be 0.5. The F-number is multiplied by this multiplication factor to get the eventual frequency number.

Prototype

`byte OPL2::getMultiplier(byte channel, byte operatorNum);`

Arguments

  • channel - Index of the channel [0, 8]
  • operatorNum - The operator for which to get frequency multiplication rate OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER

Returns

byte The F-number multiplication factor [0, 15]

OPL Registers

Reads from register based at 0x20, bits 0 through 3

See also

setMultiplier, setFNumber

getPercussion

getPercussion

Returns whether percussion mode is currently enabled or not. When enabled setDrums can be used to play various drum sounds, but the number of available channels will drop from 9 to 6 as the OPL2 requires 6 operators to produce the drum sounds. By default percussion mode is disabled.

Prototype

`bool OPL2::getPercussion();`

Returns

bool When true then the OPL2 is in percussive mode, if false then the OPL2 is in melodic mode

OPL Registers

Reads from register 0xBD, bit 5

See also

setPercussion, setDrums

getRelease

getRelease

Get the release rate of the given channel and operator. The higher the value the quicker the release.

Prototype

`byte OPL2::getRelease(byte channel, byte operatorNum);`

Arguments

  • channel - Index of the channel [0, 8]
  • operatorNum - The operator for which to get the release rate OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER

Returns

byte The release rate of the operator [0, 15]

OPL Registers

Reads from register based at 0x80, bits 0 through 3

See also:

setRelease, setAttack, getDecay, getSustain

getScalingLevel

getScalingLevel

Get the key scale level of the given channel and operator. This determines the attenuation of the output level as the frequency of an operator increases.

Prototype

`byte OPL2::getScalingLevel(byte channel, byte operatorNum);`

Arguments

  • channel - Index of the channel [0, 8]
  • operatorNum - The operator for which to get key scale level OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER

Returns

byte Key scaling level of the given operator [0, 3]

Value Attenuation
0 -
1 3.0 dB / Octave
2 1.5 dB / Octave
3 6.0 dB / Octave

OPL Registers

Reads from register based at 0x40, bits 6 and 7

See also

setScalingLevel

getSustain

getSustain

Get the sustain level of the given channel and operator. The lower the sustain value the louder the sound. Note that in order for the OPL2 to apply a sustain the sustain bit of the operator must be set using setMaintainSustain, oterwise sustain will not be applied and the envelope will continue with its release portion immediately after the decay period.

Prototype

`byte OPL2::getSustain(byte channel, byte operatorNum);`

Arguments

  • channel - Index of the channel [0, 8]
  • operatorNum - The operator for which to get sustain level OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER

Returns

byte - The sustain level of the operator [0, 15]

OPL Registers

Reads from register based at 0x80, bits 4 through 7

See also

setSustain, getMaintainSustain, setAttack, getDecay, getRelease

getSynthMode

getSynthMode

Returns whether the given channel is using additive synth of frequency modulation. By default each channel will operate in FM-synthesis mode.

  • When a channel is put into additive synth mode the output of the two operators is added together
  • In FM-synthesis mode only operator 2 will produce sound, but operator 1, the modulator, will be sent to the input of operator 2, the carrier, to modulate the frequency of operator 2

prototype

`bool OPL2::getSynthMode(byte channel);`

Arguments

  • channel - Index of the channel [0, 8]

Returns

bool When true the channel is in additive synth mode, if false it is in frequency modulation mode

OPL Registers

Reads from register based at 0xC0, bit 0

See also

setSynthMode

getTremolo

getTremolo

Returns whether tremolo is enabled on the given channel and operator. When enabled tremolo causes the operator's amplitude to vibrate. By default this setting is disabled. When enabled normally this will cause the output level to fluctuate by 1.0 dB, but when deep tremolo is enabled by setDeepTremolo(true) then tremolos on all channels will be 4.8 dB.

Prototype

`bool OPL2::getTremolo(byte channel, byte operatorNum);`

Arguments

  • channel - Index of the channel [0, 8]
  • operatorNum - The operator for which to get whether tremolo is enabled OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER

Returns

bool If true then tremolo is enabled

OPL Registers

Reads from register based at 0x20, bit 7

See also

setTremolo, getDeepTremolo, getVibrato

getVibrato

getVibrato

Returns whether vibrato is enabled on the given channel and operator. Vibrato causes the frequency of an operator to fluctuate. By default this setting is disabled. When enabled, normally the frequency will vibrate by 7/100 of a semi-tone. When deep vibrato is enabled by setDeepVibrato(true) then vibrato on all channels will be 14/100 of a semi-tone.

Prototype

`bool OPL2::getVibrato(byte channel, byte operatorNum);`

Arguments

  • channel - Index of the channel [0, 8]
  • operatorNum - The operator for which to get whether vibrato is enabled OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER

Returns

bool If true then vibrato is enabled

OPL Registers

Reads from register based at 0x20, bit 6

See also

setVibrato, getDeepVibrato, getTremolo

getVolume

getVolume

Get the volume of the given channel and operator. For the volume; the lower the value, the greater the volume.

Prototype

`byte OPL2::getVolume(byte channel, byte operatorNum);`

Arguments

  • channel - Index of the channel [0, 8]
  • operatorNum - The operator for which to get the output level OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER

Returns

byte - The volume of the given operator [0, 63]. The lower the value, the louder the volume.

OPL Registers

Reads from register based at 0x40, bits 0 through 5

See also

setVolume

getWaveForm

getWaveForm

Return the currently selected waveform for the given channel and operator. Note that the waveform select bit must be enabled on the OPL2 using setWaveFormSelect to allow for different waveforms per operator, otherwise all channels will use a sine wave.

Prototype

`byte OPL2::getWaveForm(byte channel, byte operatorNum);`

Arguments

  • channel - Index of the channel [0, 8]
  • operatorNum - The operator for which to get the current wave form OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER

Returns

byte The currently selected wave form for the operator

Value Waveform
0
1
2
3

OPL Registers

Reads from register based at 0xE0, bits 0 through 2

See also

setWaveForm, getWaveFormSelect

getWaveFormSelect

getWaveFormSelect

Returns whether waveform selection is enabled on the OPL2. If true then setWaveForm can be used to select different waveforms per operator. If false then all operators will use a sinewave. By default this will be set to false.

Prototype

`bool OPL2::getWaveFormSelect();`

Returns

bool When true then different wave forms can be set per channel operator

OPL Registers

Reads from register 0x01, bit 5

See also

setWaveFormSelect, getWaveForm

Register Setters

setAttack

setAttack

Set the attack rate of a channel operator. This defines the time it takes for the operator's output level to rise from 0 to maximum. The attack portion of the envelope is triggered when keyOn of a channel is set to true. The higher the value the faster the attack.

  • When set to 0 the sound will never attack
  • When set to 15 the sound will immediately be at maximum level

Prototype

`byte OPL2::setAttack(byte channel, byte operatorNum, byte attack);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to set attack rate OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER
  • byte attack - Attack rate [0, 15] where 0 is slowest and 15 is quickest.

Returns

byte The OPL2 register that was changed [0x60, 0x75]

OPL2 Registers

This function alters bit 7-4 of the register at base address 0x60.

See also

getAttack, setDecay, setSustain, setRelease, setKeyOn

setBlock

setBlock

Set the frequency block for the given channel. The frequency block determines the frequency range of the F-numbers. The interval between two F-numbers is the same as the frequency of F-num = 1 in the table below.

Prototype

`byte OPL2::byte setBlock(byte channel, byte block);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte block - Frequency block [0, 7]
Block F-num = 1 F-num = 1023
0 0.047 Hz 45.503 Hz
1 0.094 Hz 97.006 Hz
2 0.189 Hz 194.013 Hz
3 0.379 Hz 388.026 Hz
4 0.758 Hz 776.053 Hz
5 1.517 Hz 1,552.107 Hz
6 3.034 Hz 3,104.215 Hz
7 6.068 Hz 6,208.431 Hz

Returns

byte The OPL2 register that was changed [0xB0, 0xB8]

OPL2 Registers

This function alters bits 2 through 4 of the register at base address 0xB0

See also

getBlock, getFrequencyStep, setFNumber, setFrequency, playNote

setDecay

setDecay

Set the decay rate of the channel operator. This defines the time it takes for the operator's output level to drop from maximum to sustain level. The decay portion of the envelope is triggered immediately after attack. The higher the value the quicker the decay to sustain level will be.

  • When set to 0 the sound will never decay to sustain level, but remain at maximum output level

Prototype

`byte OPL2::setDecay(byte channel, byte operatorNum, byte decay);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to set the decay rate OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER
  • byte decay - Decay rate [0, 15] where 0 is slowest and 15 is quickest

Returns

byte The OPL2 register that was changed [0x60, 0x75]

OPL Registers

This function alters bits 0 through 3 of register at base address 0x60.

See also

getDecaysetAttack, setSustain, setRelease

setDeepTremolo

setDeepTremolo

This setting enables deeper tremolos. Normally tremolos will make the channel's output level vibrate with a 1.0dB depth, but when this setting is enabled tremolos wil have a 4.8dB depth. This is a chip-wide setting, meaning that it will affect all channels. By default this setting is disabled.

Prototype

`byte OPL2::byte setDeepTremolo(bool enable);`

Arguments

  • bool enable - Enables deeper tremolos when true

Returns

  • byte The OPL2 register that was changed (0xBD)

OPL Registers

Alters register 0xBD, bit 7.

See also

getDeepTremolo, setTremolo, setVibrato

setDrums

setDrums

Plays various drum sounds. In order to play a drumsound the OPL2 must be placed in percussive mode using setPercussion(true). This function works the same as setKeyOn. To play a drum sound the respective parameter must be set to true and disabled first by setting it to false before the same drum sound can be triggered again.

Prototype

`byte OPL2::byte setDrums(bool bass, bool snare, bool tom, bool cymbal, bool hihat);`

Arguments

  • bool bass - Plays a bass drum sound
  • bool snare - Plays a snare drum sound
  • bool tom - Plays a tom tom drum sound
  • bool cymbal - Plays a cymbal sound
  • bool hihat - Plays a hi-hat sound

Returns

  • byte The OPL2 register that was changed (0xBD)

OPL Registers

Alters register 0xBD by setting bits 0 through 4 depending on what drum sounds are enabled or disabled

See also

getDrums, setPercussion

setEnvelopeScaling

setEnvelopeScaling

When enabled scale the duration of the envelope as the frequency rises. By default envelope scaling is disabled.

Prototype

`byte OPL2::setEnvelopeScaling(byte channel, byte operatorNum, bool enable);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to set envelope scaline OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER
  • bool enable - Enabled envelope scaling when set to true

Returns

byte The OPL2 register that was changed with base 0x20

OPL2 Registers

This function alters bit 4 of register at base address 0x20.

See also

getEnvelopeScaling

setFeedback

setFeedback

Set the feedback modulation factor of the given channel. This causes operator 1 to send a portion of its output back into itself when enabled. By default operator feedback is disabled.

Prototype

`byte OPL2::byte setFeedback(byte channel, byte feedback);`

Arguments

  • byte channel - The channel to set feedback factor
  • byte feedback - Feedback modulation factor [0, 7]
Feedback Factor
0 0, No feedback
1 π/16
2 π/8
3 π/4
4 π/2
5 π
6
7

Returns

byte The OPL2 register that was changed [0xC0, 0xC8]

OPL Registers

This function alters bits 1 through 3 of register with base address 0xC0

See also

getFeedback

setFNumber

setFNumber

Sets the frequency number for the given channel. The actual frequency of the channel depends on the frequency block set using the setBlock function. When the F-number of a channel is set to 0 then its output frequency will always be 0 Hz, regardless of the block.

Prototype

`byte OPL2::setFNumber(byte channel, short fNumber);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • short fNumber - The frequency number [0, 1023]

Returns

byte The OPL2 register that was changed [0xA0, 0xA8]

OPL Registers

This function sets the lower byte of the F-number to register at base address 0xA0 and the upper two bits to bits 0 and 1 of register at base address 0xB0

See also

getFNumber, setBlock

setKeyOn

setKeyOn

Enable or disable audio output on a given channel. When a channel is enabled the attack portion of the channel envelope is triggered and the channel starts to output a tone at the set frequency that rises in volume according to the set attack rate. When disabled the release portion of the envelope is triggered that makes the note fade out at the set release rate until the channel is silent.

  • Note that when percussion mode is enabled keyOn of channels 6, 7 and 8 must remain false as drum sounds are triggered differently.

Prototype

`byte OPL2::setKeyOn(byte channel, bool keyOn);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • bool keyOn - Boolean value that enables audio output on the given channel when true

Returns

byte The OPL2 register that was changed [0xB0, 0xB8]

OPL Registers

This function sets or clears bit 5 of register at base 0xB0.

See also

getKeyOn, setDrums

setMaintainSustain

setMaintainSustain

Alters whether the operator's envelope should maintain sustain level. When enabled the operator will hold the output at sustain level until the release phase of the envelope is triggered by disabling output with setKeyOn(false). If disabled the release phase of the envelope will trigger immediately after the decay phase. Note that setKeyOn must still be disabled before triggering the next note in this case. By default sustain will be disabled.

Prototype

`byte OPL2::setMaintainSustain(byte channel, byte operatorNum, bool enable);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to enable or disable sustain OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER
  • bool enable - Enables sustain for the given operator when true
Returns

byte The OPL2 register that was changed with base address 0x20.

OPL Registers

This function alters bit 5 of register at base address 0x20.

See also

getMaintainSustain, setSustain

setMultiplier

setMultiplier

Set the harmonic for the given operator in which it will product sound or modulation in relation to the channel's frequency.

Prototype

`byte OPL2::setMultiplier(byte channel, byte operatorNum, byte multiplier);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to set the multiplier OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER
  • byte multiplier - The multiplier to set for the operator:
0x00 one octave below
0x01 at the voice's specified frequency
0x02 one octave above
0x03 an octave and a fifth above
0x04 two octaves above
0x05 two octaves and a major third above
0x06 two octaves and a fifth above
0x07 two octaves and a minor seventh above
0x08 three octaves above
0x09 three octaves and a major second above
0x0A three octaves and a major third above
0x0B three octaves and a major third above
0x0C three octaves and a fifth above
0x0D three octaves and a fifth above
0x0E three octaves and a major seventh above
0x0F three octaves and a major seventh above

Returns

byte The OPL2 register that was changed [0xB0, 0xB8]

OPL Registers

This function alters bits 0 through 3 of register at abse address 0x20

See also

getMultiplier

setPercussion

setPercussion

Set whether percussion mode is enabled or not. When enabled setDrums can be used to play various drum sounds, but the number of available channels will drop from 9 to 6 as the OPL2 requires 6 operators to produce the drum sounds. By default percussion mode is disabled.

Prototype

`byte OPL2::byte setPercussion(bool enable);`

Arguments

  • bool enable - Enables percussion mode when true

Returns

byte The OPL2 register that was changed: 0xBD

OPL Registers

This function alters bit 5 of register 0xBD

See also

getPercussion, setDrums

setTremolo

setTremolo

Apply amplitude modulation to a certain channel operator, causing a tremolo effect. The depth of the tremolo can either be 1 dB, or 4.8 dB. This is controlled by setDeepTremolo which is common to all operators. By default tremolo is disabled.

Prototype

`byte OPL2::setTremolo(byte channel, byte operatorNum, bool enable);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to set tremolo OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER
  • bool enable - Enables tremolo for the given operator when true

Returns

byte The OPL2 register that was changed [0x20, 0x35]

OPL Registers

This function alters bit 7 of registers at base address 0x20

See also

getTremolo, getDeepTremolo, setVibrato

setRelease

setRelease

Set the release rate of a certain operator. This defines the time it takes for the operator's output level to drop from sustain level to 0 when the channel is released by setting its setKeyOn to false. If setMaintainSustain is false then the release portion of the envelope wil automatically trigger after the decay period.

Prototype

`byte OPL2::setRelease(byte channel, byte operatorNum, byte release);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to change the release rate OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER
  • byte release - Release rate of the operator [0, 15] where 0 is slowest and 15 is fastest.

Returns

byte The OPL2 register that was changed [0x80, 0x95]

OPL2 Reference

This function alters bits 0 through 3 of register at base address 0x80

See also

getRelease, setAttack, setDecay, setSustain, setKeyOn

setScalingLevel

setScalingLevel

Causes the output level of a channel operator to decrease as the frequency rises.

Prototype

`byte OPL2::setScalingLevel(byte channel, byte operatorNum, byte scaling);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to set key scaling level.
  • byte scaling - Amount of scaling to apply to the operator [0, 3]
Value Attenuation
0 -
1 3.0 dB / Octave
2 1.5 dB / Octave
3 6.0 dB / Octave

Returns

byte The OPL2 register that was changed [0x40, 0x55]

OPL Registers

This function alters bits 6 and 7 of register at base 0x40.

See also

getScalingLevel, setVolume

setSustain

setSustain

Set the sustain level of a certain operator. This defines the output level of the operator when sustaining a note. The lower the sustain value the louder the sound. The sustain portion of the envelope is triggered immediately after decay. In order to enable sustain on an operator maintainSustain must be true.

Prototype

`byte OPL2::setSustain(byte channel, byte operatorNum, byte sustain);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to set sustain level OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER
  • byte sustain - Sustain level of the operator [0, 15] where 0 is loudest and 15 is softest.

Returns

byte The OPL2 register that was changed [0x80, 0x95]

OPL Registers

This function alters bits 4 through 7 of register at base address 0x80.

See also

getSustain, setMaintainSustain, setAttack, setDecay, setRelease

setSynthMode

setSynthMode

Set the sound synthesis type of the given channel to either additive or frequency modulation. By default a channel will be in FM-synthesis mode after reset.

  • When a channel is put into additive synth mode the output of the two operators is added together
  • In FM-synthesis mode only operator 2 will produce sound, but operator 1, the modulator, will be sent to the input of operator 2, the carrier, to modulate the frequency of operator 2

Prototype

`byte OPL2::byte setSynthMode(byte channel, bool isAdditive);`

Arguments

  • byte channel - Inde of the channel [0, 8]
  • bool isAdditive - Sets the channel to additive synth when true, otherwise FM-synthesis

Returns

byte The OPL2 register that was changed [0xC0, 0xC8]

OPL Registers

Alters bit 0 of register at base address 0xC0

See also

getSynthMode

setVibrato

setVibrato

Sets whether vibrato is enabled on the given channel and operator. Vibrato causes the frequency of an operator to fluctuate. By default this setting is disabled. When enabled, normally the frequency will vibrate by 7/100 of a semi-tone. When deep vibrato is enabled by setDeepVibrato(true) then vibrato on all channels will be 14/100 of a semi-tone.

Prototype

`byte OPL2::setVibrato(byte channel, byte operatorNum, bool enable);`

Arguments

  • byte channel - Index of the channe; [0, 8]
  • byte operatorNum - The operator for which to set vibrato OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER
  • byte enable - Enables vibrato for the given operator when true

Returns

byte The OPL2 register that was changed [0x20, 0x35]

OPL2 Registers

This function alters bit 6 of registers at base address 0x20

See also

getVibrato, setTremolo

setVolume

setVolume

Set the output level of a channel's operator. The lower the value, the higher the output level.

Prototype

`byte OPL2::setVolume(byte channel, byte operatorNum, byte volume);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - Operator to change volume OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER
  • byte volume - Volume to set [0, 63] where 0 is loudest, 63 is silent

Returns

byte The OPL2 register that was changed [0x40, 0x55]

OPL Registers

This function alters bits 0 through 5 of register at base 0x40

See also

getVolume, setScalingLevel

setWaveFowm

setWaveForm

Select the waveform to use for a certain channel operator. To take effect waveform selection must be enabled with setWaveFormSelect(true) first. By default each operator will use a sinewave.

Prototype

`byte OPL2::setWaveForm(byte channel, byte operatorNum, byte waveForm);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator for which to change the waveform OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER
  • byte waveForm - The wavefor to set for the operator [0, 3]
Value Waveform
0
1
2
3

Returns

byte The OPL2 register that was changed [0xE0, 0xF5]

OPL Registers

This function alters bits 0 through 2 of register at base address 0xE0

See also

getWaveForm, setWaveFormSelect

setWaveFormSelect

setWaveFormSelect

Enable or disable waveform selection for each individual channel operator. If disabled then all operators will output a basic sine wave. By default wave form selection is disabled.

Prototype

`byte OPL2::setWaveFormSelect(bool enable);`

Arguments

  • bool enable - Enables wave form selection on all operators when set to true

Returns

byte The OPL2 register that was changed: 0x01

OPL2 Reference

This function alters bit 5 of register 0x01

See also

getWaveFormSelect, setWaveForm

Instrument functions

createInstrument

createInstrument

Create a new blank instrument structure.

Prototype

`Instrument OPL2::createInstrument();`

Returns

Instrument A new blank Instrument structure with all parameters reset

getDrumInstrument

getDrumInstrument

Convert the channel parameters of the given drum type into an instrument structure.

Prototype

`Instrument OPL2::getDrumInstrument(byte drumType);`

Arguments

  • byte drumType - Type of drum from which to create the instrument [6, 10]
Drum Instrument type definition Value
Bass INSTRUMENT_TYPE_BASS 6
Snare INSTRUMENT_TYPE_SNARE 7
Tom tom INSTRUMENT_TYPE_TOM 8
Cymbal INSTRUMENT_TYPE_CYMBAL 9
Hi-hat INSTRUMENT_TYPE_HI_HAT 10

Returns

Instrument New Instrument structure converted from the the channel used for the given drum type

getInstrument

getInstrument

Convert the given channel into an instrument structure.

Prototype

`Instrument OPL2::getInstrument(byte channel);`

Arguments

  • byte channel - Index of the channel [0, 8]

Returns

Instrument New Instrument structure converted from the given channel

loadInstrument

loadInstrument

Create an Instrument structure from an array of 12-bytes of instrument data either in PROGMEM or SRAM. On Raspberry Pi instrument data con only be loaded from SRAM.

Prototype

`Instrument OPL2::loadInstrument(const unsigned char *instrument, bool fromProgmem = INSTRUMENT_DATA_PROGMEM);`
`Instrument OPL2::loadInstrument(const unsigned char *instrument);    // Raspberry Pi only!`

Arguments

  • const unsigned char *instrument - Pointer to the start of the instrument data array
  • bool fromProgmem - Set to true (default) the data of the instrument located in PROGMEM (Arduino only!)

Returns

Instrument New Instrument structure created from the 12-byte data structure

setDrumInstrument

setDrumInstrument

Set the given drum instrument to the drum channel that is defined in the Instrument structure.

Prototype

`void OPL2::setDrumInstrument(Instrument instrument, float volume = 1.0);`

Arguments

  • Instrument instrument - The instrument structure
  • float volume - Optional volume to apply to the drum channel. Defaults to full volume
setInstrument

##setInstrument Set the given instrument to a certain channel. This will load all the operators of the channel with the instrument data.

Prototype

`void OPL2::setInstrument(byte channel, Instrument instrument, float volume = 1.0);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • Instrument instrument - The instrument structure
  • float volume - Optional volume to apply to the channel. Defaults to full volume
setInstrument (deprecated!)

setInstrument (deprecated)

Load a 12-byte array of instrument data and apply it to the given channel. On Arduino the instrument data will always be loaded from PROGMEM.

This function is deprecated and should not be used anymore. Use loadInstrument and setInstrument instead!

Prototype

`void OPL2::setInstrument(byte channel, const unsigned char *instrument);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • const unsigned char *instrument - Pointer to the start of the instrument data array

Helper & utility functions

getFrequency

getFrequency

Get the frequency of the given channel in Hz based on the current block and F-number.

Prototype

`float OPL2::getFrequency(byte channel);`

Arguments

  • byte channel - Index of the channel [0, 8]

Returns

float The frequency of the given channelv in Hz

See also

setFrequency, getFNumber, getBlock, getFrequencyBlock, getFrequencyStep

getFrequencyBlock

getFrequencyBlock

Get the block number that is appropriate for the given frequency. This block number can then be set using setBlock.

Prototype

`byte OPL2::getFrequencyBlock(float frequency);`

Arguments

  • float frequency - The desired frequency in Hz for which we want to know the block number

Returns

byte The block number to use for the given frequency [0, 7]

See also

setBlock, setFNumber, setFrequency, getFrequencyStep

getFrequencyFNumber

getFrequencyFNumber

Get the F-number to play a given frequency on a channel. The F-number that is returned depends on the current frequency block of channel that was set prior using setBlock. The F-number that is returned can be played using setFNumber.

Prototype

`short OPL2::getFrequencyFNumber(byte channel, float frequency);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • float frequency - The desired frequency in Hz

Returns

short The F-number of the given frequency [0, 1023]

See also

setFNumber, getFNumber, getBlock, getFrequencyBlock, getFrequencyStep

getFrequencyStep

getFrequencyStep

Get the difference in frequency between two F-numbers on the given channel. This depends on the current frequency block of the channel that was set prior using setBlock.

Prototype

`float OPL2::getFrequencyStep(byte channel);`

Arguments

  • byte channel - Index of the channel for which to set attack rate [0, 8]

Returns

float The difference in frequency (in Hz) between two F-numbers for the current block

See also

getFrequencyFNumber, getFrequencyBlock, getFrequency, getFNumber, getBlock

getNoteFNumber

getNoteFNumber

Get the appropriate F-number fot the given note. This function assumes that block numbers are interpreted as octave.

Prototype

`short OPL2::getNoteFNumber(byte note);`

Arguments

  • byte note - The note index [0, 11] where 0 is a C, 1 is a C# and 11 is a B.

Following notes are defined in the library:

Note Definition Value
C NOTE_C 0
C# NOTE_CS 1
D NOTE_D 2
D# NOTE_DS 3
E NOTE_E 4
F NOTE_F 5
F# NOTE_FS 6
G NOTE_G 7
G# NOTE_GS 8
A NOTE_A 9
A# NOTE_AS 10
B NOTE_B 11

Returns

short The F-number of the note [0, 1023]

See also

getFrequencyFNumber, playNote

getRegister

getRegister

Get the current value of one of the OPL2 registers.

Prototype

`byte OPL2::getRegister(byte reg);`

Arguments

  • byte reg - The register to query [0x00, 0xFF]

Returns

byte The current value of the requested OPL2 register.

See also

setRegister

getRegisterOffset

getRegisterOffset

Returns the register offset to be added to the base address for the given channel and operator.

Prototype

`byte OPL2::getRegisterOffset(byte channel, byte operatorNum);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • byte operatorNum - The operator OPERATOR1 / MODULATOR or OPERATOR2 / CARRIER

Returns

byte The offset to the operator's register [0x00, 0x15]

See also

setRegister

playNote

playNote

Plays a note on a particular channel. This function combines the setKeyOn, setBlock and setFNumber functions into a single convenient function to play a note. It disables output on the given channel, sets the block number (octave) and frequency of the note and enables channel output to trigger playing the note.

Prototype

`void OPL2::playNote(byte channel, byte octave, byte note);`

Arguments

  • byte channel - The channel to play the note on [0, 8]
  • byte octave - The octave of the note [0, 7]
  • byte note - The note number [0, 11] or NOTE_C, NOTE_CS throught NOTE_B

Following notes are defined in the library:

Note Definition Value
C NOTE_C 0
C# NOTE_CS 1
D NOTE_D 2
D# NOTE_DS 3
E NOTE_E 4
F NOTE_F 5
F# NOTE_FS 6
G NOTE_G 7
G# NOTE_GS 8
A NOTE_A 9
A# NOTE_AS 10
B NOTE_B 11
setFrequency

setFrequency

Set the frequency of a channel to the given frequency and change the block number if needed. This will not change the keyOn state of the channel.

Prototype:

`byte OPL2::setFrequency(byte channel, float frequency);`

Arguments

  • byte channel - Index of the channel [0, 8]
  • float frequency - The frequency to set in Hz

Returns

byte The OPL2 register that was changed [0xA0, 0xA8]

setRegister

setRegister

Set the internal OPL2 register to the given value and write the change to the chip by calling write.

Prototype

`byte OPL2::setRegister(byte reg, byte value);`

Arguments

  • byte reg - Index of the register to change [0x00, 0xFF]
  • byte value - The value to write to the register [0x00, 0xFF]

Returns

byte The OPL2 register that was changed [0x00, 0xFF]

See also

getRegister

write

write

Write the given value directly to one of the registers of the OPL2 chip through the SPI interface. This function does not update the internal register states of the library!

Normally when changing the value of a register directly, i.e. not using any of the getter / setter functions of the OPL2 library, the setRegister function should be used, which allows reading back a register value later using getRegister. This is required for proper functioning of the library. The write function is used internally by the library to communicate with the OPL2 Audio Board and should only be used when a lot of data needs to be sent to the OPL2 Audio Board at the highest speed possible without caring about any value set.

Prototype

`OPL2::void write(byte reg, byte data);`

Arguments

  • byte reg - The register to write to [0x00, 0xFF]
  • byte data - The byte value to write

Example

opl2.reset();                    // Reset all registers to 0x00.
opl2.setRegister(0x60, 0x88);    // Set channel 0, operator 1 attack to 8 and decay to 8.
opl2.getRegiter(0x60);           // Reads 0x88.
opl2.write(0x60, 0xFF);          // Set channel 0, operator 1 attack to F and decay to F (chip only!).
opl2.getRegister(0x60);          // Reads 0x88.
⚠️ **GitHub.com Fallback** ⚠️