API Reference Manual - DhrBaksteen/ArduinoOPL2 GitHub Wiki
This section describes all API functions that the OPL2 library has to offer and their effects on the YM3812 if applicable.
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.
OPL2::OPL2();
OPL2::OPL2(byte reset, byte address, byte latch);
-
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
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.
OPL2::init();
// Initialize the OPL2 library.
OPL2 opl2();
opl2.init();
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.
OPL2::reset();
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
`byte OPL2::getAttack(byte channel, byte operatorNum);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operator for which to set attack rate:OPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
byte
The attack rate of the operator [0
, 15
]
Reads register based at 0x60
, bit 4 through 7
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.
`byte OPL2::getBlock(byte channel);`
-
byte
channel
- Index of the channel [0
,8
]
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 |
Reads register based at 0xB0
, bits 2 through 4
setBlock
, getFrequencyBlock
, getFNumber
, getFrequency
, getFrequencyStep
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
`byte OPL2::getDecay(byte channel, byte operatorNum);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operator for which to get decay rate of:OPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
byte
The decay rate of the operator [0
, 15
]
Reads register based at 0x60
, bits 0 through 3
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
`bool OPL2::getDeepTremolo();`
bool
If true
then deep tremolo applies for all operators
Reads register 0xBD
, bit 7
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
`bool OPL2::getDeepVibrato();`
bool
When true
then deep vibrato is enabled for all operators
Reads register 0xBD
, bit 6
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
.
`byte OPL2::getDrums();`
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 |
Reads from register 0xBD
, bits 0 through 4
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.
`bool OPL2::getEnvelopeScaling(byte channel, byte operatorNum);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operator for which to get KSR:OPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
bool
If true
then envelope scaling is enabled
Reads from register based at 0x20
, bit 4
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.
`byte OPL2::getFeedback(byte channel);`
-
byte
channel
- Index of the channel for which to get feedback factor [0
,8
]
byte
Feedback modulation factor of the given channel
Feedback | Factor |
---|---|
0 | 0, No feedback |
1 | π/16 |
2 | π/8 |
3 | π/4 |
4 | π/2 |
5 | π |
6 | 2π |
7 | 4π |
Reads from register based at 0xC0
, bits 1 through 3
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.
`short OPL2::getFNumber(byte channel);`
-
byte
channel
- Index of the channel for which to get the F-number [0
,8
]
short
The frequency number [0
, 1023
]
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
getKeyOn
Returns whether the channel is currently generating a sound.
`bool OPL2::getKeyOn(byte channel);`
-
byte
channel
- Index of the channel for which to get key on [0
,8
]
bool
If true
then the channel is generating sound, if false
then the channel is in the release portion of its envelope or silent
Reads from register based at 0xB0
, bit 5
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 withsetKeyOn (false)
- If disabled (default) then the operator will immediately jump to the release portion of its envelope after the decay period
`bool OPL2::getMaintainSustain(byte channel, byte operatorNum);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operator for which to get wheter sustain is enabled:OPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
bool
If true
then sustain is enabled on the operator
Reads from register based at 0x20
, bit 5
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.
`byte OPL2::getMultiplier(byte channel, byte operatorNum);`
-
channel
- Index of the channel [0
,8
] -
operatorNum
- The operator for which to get frequency multiplication rateOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
byte
The F-number multiplication factor [0
, 15
]
Reads from register based at 0x20
, bits 0 through 3
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.
`bool OPL2::getPercussion();`
bool
When true
then the OPL2 is in percussive mode, if false
then the OPL2 is in melodic mode
Reads from register 0xBD
, bit 5
getRelease
Get the release rate of the given channel and operator. The higher the value the quicker the release.
`byte OPL2::getRelease(byte channel, byte operatorNum);`
-
channel
- Index of the channel [0
,8
] -
operatorNum
- The operator for which to get the release rateOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
byte
The release rate of the operator [0
, 15
]
Reads from register based at 0x80
, bits 0 through 3
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.
`byte OPL2::getScalingLevel(byte channel, byte operatorNum);`
-
channel
- Index of the channel [0
,8
] -
operatorNum
- The operator for which to get key scale levelOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
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 |
Reads from register based at 0x40
, bits 6 and 7
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.
`byte OPL2::getSustain(byte channel, byte operatorNum);`
-
channel
- Index of the channel [0
,8
] -
operatorNum
- The operator for which to get sustain levelOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
byte
- The sustain level of the operator [0
, 15
]
Reads from register based at 0x80
, bits 4 through 7
setSustain
, getMaintainSustain
, setAttack
, getDecay
, getRelease
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
`bool OPL2::getSynthMode(byte channel);`
-
channel
- Index of the channel [0
,8
]
bool
When true
the channel is in additive synth mode, if false
it is in frequency modulation mode
Reads from register based at 0xC0
, bit 0
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.
`bool OPL2::getTremolo(byte channel, byte operatorNum);`
-
channel
- Index of the channel [0
,8
] -
operatorNum
- The operator for which to get whether tremolo is enabledOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
bool
If true
then tremolo is enabled
Reads from register based at 0x20
, bit 7
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.
`bool OPL2::getVibrato(byte channel, byte operatorNum);`
-
channel
- Index of the channel [0
,8
] -
operatorNum
- The operator for which to get whether vibrato is enabledOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
bool
If true
then vibrato is enabled
Reads from register based at 0x20
, bit 6
getVolume
Get the volume of the given channel and operator. For the volume; the lower the value, the greater the volume.
`byte OPL2::getVolume(byte channel, byte operatorNum);`
-
channel
- Index of the channel [0
,8
] -
operatorNum
- The operator for which to get the output levelOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
byte
- The volume of the given operator [0
, 63
]. The lower the value, the louder the volume.
Reads from register based at 0x40
, bits 0 through 5
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.
`byte OPL2::getWaveForm(byte channel, byte operatorNum);`
-
channel
- Index of the channel [0
,8
] -
operatorNum
- The operator for which to get the current wave formOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
byte
The currently selected wave form for the operator
Value | Waveform |
---|---|
0 | ![]() |
1 | ![]() |
2 | ![]() |
3 | ![]() |
Reads from register based at 0xE0
, bits 0 through 2
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
.
`bool OPL2::getWaveFormSelect();`
bool
When true
then different wave forms can be set per channel operator
Reads from register 0x01
, bit 5
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
`byte OPL2::setAttack(byte channel, byte operatorNum, byte attack);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operator for which to set attack rateOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
-
byte
attack
- Attack rate [0
,15
] where 0 is slowest and 15 is quickest.
byte
The OPL2 register that was changed [0x60
, 0x75
]
This function alters bit 7-4 of the register at base address 0x60
.
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.
`byte OPL2::byte setBlock(byte channel, byte block);`
-
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 |
byte
The OPL2 register that was changed [0xB0
, 0xB8
]
This function alters bits 2 through 4 of the register at base address 0xB0
getBlock
, getFrequencyStep
, setFNumber
, setFrequency
, playNote
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
`byte OPL2::setDecay(byte channel, byte operatorNum, byte decay);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operator for which to set the decay rateOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
-
byte
decay
- Decay rate [0
,15
] where 0 is slowest and 15 is quickest
byte
The OPL2 register that was changed [0x60
, 0x75
]
This function alters bits 0 through 3 of register at base address 0x60
.
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.
`byte OPL2::byte setDeepTremolo(bool enable);`
-
bool
enable
- Enables deeper tremolos whentrue
-
byte
The OPL2 register that was changed (0xBD
)
Alters register 0xBD
, bit 7.
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.
`byte OPL2::byte setDrums(bool bass, bool snare, bool tom, bool cymbal, bool hihat);`
-
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
-
byte
The OPL2 register that was changed (0xBD
)
Alters register 0xBD
by setting bits 0 through 4 depending on what drum sounds are enabled or disabled
setEnvelopeScaling
When enabled scale the duration of the envelope as the frequency rises. By default envelope scaling is disabled.
`byte OPL2::setEnvelopeScaling(byte channel, byte operatorNum, bool enable);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operator for which to set envelope scalineOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
-
bool
enable
- Enabled envelope scaling when set totrue
byte
The OPL2 register that was changed with base 0x20
This function alters bit 4 of register at base address 0x20
.
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.
`byte OPL2::byte setFeedback(byte channel, byte feedback);`
-
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 | 2π |
7 | 4π |
byte
The OPL2 register that was changed [0xC0
, 0xC8
]
This function alters bits 1 through 3 of register with base address 0xC0
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.
`byte OPL2::setFNumber(byte channel, short fNumber);`
-
byte
channel
- Index of the channel [0
,8
] -
short
fNumber
- The frequency number [0
,1023
]
byte
The OPL2 register that was changed [0xA0
, 0xA8
]
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
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.
`byte OPL2::setKeyOn(byte channel, bool keyOn);`
-
byte
channel
- Index of the channel [0
,8
] -
bool
keyOn
- Boolean value that enables audio output on the given channel whentrue
byte
The OPL2 register that was changed [0xB0
, 0xB8
]
This function sets or clears bit 5 of register at base 0xB0
.
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.
`byte OPL2::setMaintainSustain(byte channel, byte operatorNum, bool enable);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operator for which to enable or disable sustainOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
-
bool
enable
- Enables sustain for the given operator whentrue
byte
The OPL2 register that was changed with base address 0x20
.
This function alters bit 5 of register at base address 0x20
.
setMultiplier
Set the harmonic for the given operator in which it will product sound or modulation in relation to the channel's frequency.
`byte OPL2::setMultiplier(byte channel, byte operatorNum, byte multiplier);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operator for which to set the multiplierOPERATOR1
/MODULATOR
orOPERATOR2
/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 |
byte
The OPL2 register that was changed [0xB0
, 0xB8
]
This function alters bits 0 through 3 of register at abse address 0x20
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.
`byte OPL2::byte setPercussion(bool enable);`
-
bool
enable
- Enables percussion mode whentrue
byte
The OPL2 register that was changed: 0xBD
This function alters bit 5 of register 0xBD
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.
`byte OPL2::setTremolo(byte channel, byte operatorNum, bool enable);`
-
byte
channel
- Index of the channel [0, 8] -
byte
operatorNum
- The operator for which to set tremoloOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
-
bool
enable
- Enables tremolo for the given operator whentrue
byte
The OPL2 register that was changed [0x20
, 0x35
]
This function alters bit 7 of registers at base address 0x20
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.
`byte OPL2::setRelease(byte channel, byte operatorNum, byte release);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operator for which to change the release rateOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
-
byte
release
- Release rate of the operator [0
,15
] where 0 is slowest and15
is fastest.
byte
The OPL2 register that was changed [0x80
, 0x95
]
This function alters bits 0 through 3 of register at base address 0x80
setScalingLevel
Causes the output level of a channel operator to decrease as the frequency rises.
`byte OPL2::setScalingLevel(byte channel, byte operatorNum, byte scaling);`
-
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 |
byte
The OPL2 register that was changed [0x40
, 0x55
]
This function alters bits 6 and 7 of register at base 0x40
.
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
.
`byte OPL2::setSustain(byte channel, byte operatorNum, byte sustain);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operator for which to set sustain levelOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
-
byte
sustain
- Sustain level of the operator [0
,15
] where 0 is loudest and 15 is softest.
byte
The OPL2 register that was changed [0x80
, 0x95
]
This function alters bits 4 through 7 of register at base address 0x80
.
getSustain
, setMaintainSustain
, setAttack
, setDecay
, setRelease
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
`byte OPL2::byte setSynthMode(byte channel, bool isAdditive);`
-
byte
channel
- Inde of the channel [0
,8
] -
bool
isAdditive
- Sets the channel to additive synth whentrue
, otherwise FM-synthesis
byte
The OPL2 register that was changed [0xC0
, 0xC8
]
Alters bit 0 of register at base address 0xC0
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.
`byte OPL2::setVibrato(byte channel, byte operatorNum, bool enable);`
-
byte
channel
- Index of the channe; [0, 8] -
byte
operatorNum
- The operator for which to set vibratoOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
-
byte
enable
- Enables vibrato for the given operator whentrue
byte
The OPL2 register that was changed [0x20
, 0x35
]
This function alters bit 6 of registers at base address 0x20
setVolume
Set the output level of a channel's operator. The lower the value, the higher the output level.
`byte OPL2::setVolume(byte channel, byte operatorNum, byte volume);`
-
byte
channel
- Index of the channel [0, 8] -
byte
operatorNum
- Operator to change volumeOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
-
byte
volume
- Volume to set [0, 63] where 0 is loudest, 63 is silent
byte
The OPL2 register that was changed [0x40
, 0x55
]
This function alters bits 0 through 5 of register at base 0x40
setWaveFowm
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.
`byte OPL2::setWaveForm(byte channel, byte operatorNum, byte waveForm);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operator for which to change the waveformOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
-
byte
waveForm
- The wavefor to set for the operator [0
,3
]
Value | Waveform |
---|---|
0 | ![]() |
1 | ![]() |
2 | ![]() |
3 | ![]() |
byte
The OPL2 register that was changed [0xE0
, 0xF5
]
This function alters bits 0 through 2 of register at base address 0xE0
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.
`byte OPL2::setWaveFormSelect(bool enable);`
-
bool
enable
- Enables wave form selection on all operators when set totrue
byte
The OPL2 register that was changed: 0x01
This function alters bit 5 of register 0x01
createInstrument
Create a new blank instrument structure.
`Instrument OPL2::createInstrument();`
Instrument
A new blank Instrument
structure with all parameters reset
getDrumInstrument
Convert the channel parameters of the given drum type into an instrument structure.
`Instrument OPL2::getDrumInstrument(byte drumType);`
-
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 |
Instrument
New Instrument
structure converted from the the channel used for the given drum type
getInstrument
Convert the given channel into an instrument structure.
`Instrument OPL2::getInstrument(byte channel);`
-
byte
channel
- Index of the channel [0
,8
]
Instrument
New Instrument
structure converted from the given channel
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.
`Instrument OPL2::loadInstrument(const unsigned char *instrument, bool fromProgmem = INSTRUMENT_DATA_PROGMEM);`
`Instrument OPL2::loadInstrument(const unsigned char *instrument); // Raspberry Pi only!`
-
const unsigned char
*instrument
- Pointer to the start of the instrument data array -
bool
fromProgmem
- Set totrue
(default) the data of the instrument located inPROGMEM
(Arduino only!)
Instrument
New Instrument
structure created from the 12-byte data structure
setDrumInstrument
Set the given drum instrument to the drum channel that is defined in the Instrument
structure.
`void OPL2::setDrumInstrument(Instrument instrument, float volume = 1.0);`
-
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.
`void OPL2::setInstrument(byte channel, Instrument instrument, float volume = 1.0);`
-
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!)
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!
`void OPL2::setInstrument(byte channel, const unsigned char *instrument);`
-
byte
channel
- Index of the channel [0
,8
] -
const unsigned char
*instrument
- Pointer to the start of the instrument data array
getFrequency
Get the frequency of the given channel in Hz based on the current block and F-number.
`float OPL2::getFrequency(byte channel);`
-
byte
channel
- Index of the channel [0
,8
]
float
The frequency of the given channelv in Hz
setFrequency
, getFNumber
, getBlock
, getFrequencyBlock
, getFrequencyStep
getFrequencyBlock
Get the block number that is appropriate for the given frequency. This block number can then be set using setBlock
.
`byte OPL2::getFrequencyBlock(float frequency);`
-
float
frequency
- The desired frequency in Hz for which we want to know the block number
byte
The block number to use for the given frequency [0
, 7
]
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
.
`short OPL2::getFrequencyFNumber(byte channel, float frequency);`
-
byte
channel
- Index of the channel [0
,8
] -
float
frequency
- The desired frequency in Hz
short
The F-number of the given frequency [0
, 1023
]
setFNumber
, getFNumber
, getBlock
, getFrequencyBlock
, 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
.
`float OPL2::getFrequencyStep(byte channel);`
-
byte
channel
- Index of the channel for which to set attack rate [0
,8
]
float
The difference in frequency (in Hz) between two F-numbers for the current block
getFrequencyFNumber
, getFrequencyBlock
, getFrequency
, getFNumber
, getBlock
getNoteFNumber
Get the appropriate F-number fot the given note. This function assumes that block numbers are interpreted as octave.
`short OPL2::getNoteFNumber(byte note);`
-
byte
note
- The note index [0
,11
] where0
is a C,1
is a C# and11
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 |
short
The F-number of the note [0
, 1023
]
getRegister
Get the current value of one of the OPL2 registers.
`byte OPL2::getRegister(byte reg);`
-
byte
reg
- The register to query [0x00
,0xFF
]
byte
The current value of the requested OPL2 register.
getRegisterOffset
Returns the register offset to be added to the base address for the given channel and operator.
`byte OPL2::getRegisterOffset(byte channel, byte operatorNum);`
-
byte
channel
- Index of the channel [0
,8
] -
byte
operatorNum
- The operatorOPERATOR1
/MODULATOR
orOPERATOR2
/CARRIER
byte
The offset to the operator's register [0x00
, 0x15
]
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.
`void OPL2::playNote(byte channel, byte octave, byte note);`
-
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
] orNOTE_C
,NOTE_CS
throughtNOTE_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
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.
`byte OPL2::setFrequency(byte channel, float frequency);`
-
byte
channel
- Index of the channel [0
,8
] -
float
frequency
- The frequency to set in Hz
byte
The OPL2 register that was changed [0xA0
, 0xA8
]
setRegister
Set the internal OPL2 register to the given value and write the change to the chip by calling write
.
`byte OPL2::setRegister(byte reg, byte value);`
-
byte
reg
- Index of the register to change [0x00
,0xFF
] -
byte
value
- The value to write to the register [0x00
,0xFF
]
byte
The OPL2 register that was changed [0x00
, 0xFF
]
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.
`OPL2::void write(byte reg, byte data);`
-
byte
reg
- The register to write to [0x00
,0xFF
] -
byte
data
- The byte value to write
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.