-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperator.go
491 lines (445 loc) · 13.5 KB
/
operator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package opl2
// This file is a Pure Go conversion of dbopl.h/.cpp
/*
* Copyright (C) 2002-2013 The DOSBox Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
DOSBox implementation of a combined Yamaha YMF262 and Yamaha YM3812 emulator.
Enabling the opl3 bit will switch the emulator to stereo opl3 output instead of regular mono opl2
Except for the table generation it's all integer math
Can choose different types of generators, using muls and bigger tables, try different ones for slower platforms
The generation was based on the MAME implementation but tried to have it use less memory and be faster in general
MAME uses much bigger envelope tables and this will be the biggest cause of it sounding different at times
//TODO Don't delay first operator 1 sample in opl3 mode
//TODO Maybe not use class method pointers but a regular function pointers with operator as first parameter
//TODO Fix panning for the Percussion channels, would any opl3 player use it and actually really change it though?
//TODO Check if having the same accuracy in all frequency multipliers sounds better or not
//DUNNO Keyon in 4op, switch to 2op without keyoff.
*/
//Masks for operator 20 values
const (
cMaskKSR = 0x10
cMaskSustain = 0x20
cMaskVibrato = 0x40
cMaskTremolo = 0x80
)
// OperatorState is a state of the operator's envelope
type OperatorState uint8
const (
// OperatorStateOff is the OFF state of the operator envelope
OperatorStateOff = OperatorState(iota)
// OperatorStateRelease is the RELEASE state of the operator envelope
OperatorStateRelease
// OperatorStateSustain is the SUSTAIN state of the operator envelope
OperatorStateSustain
// OperatorStateDecay is the DECAY state of the operator envelope
OperatorStateDecay
// OperatorStateAttack is the ATTACK state of the operator envelope
OperatorStateAttack
)
type volumeHandler func() int
// Operator is an OPL2/3 channel operator
type Operator struct {
volHandler volumeHandler
waveHandler waveHandler //Routine that generate a wave
waveBase []int16
waveMask int
waveStart int
waveIndex int //WAVE_BITS shifted counter of the frequency index
waveAdd int //The base frequency without vibrato
waveCurrent int //waveAdd + vibratao
chanData uint32 //Frequency/octave and derived data coming from whatever channel controls this
freqMul uint32 //Scale channel frequency with this, TODO maybe remove?
vibrato uint32 //Scaled up vibrato strength
sustainLevel int32 //When stopping at sustain level stop here
totalLevel int32 //totalLevel is added to every generated volume
currentLevel int //totalLevel + tremolo
volume int32 //The currently active volume
attackAdd uint32 //Timers for the different states of the envelope
decayAdd uint32
releaseAdd uint32
rateIndex uint32 //Current position of the evenlope
rateZero uint8 //int for the different states of the envelope having no changes
keyOn uint8 //Bitmask of different values that can generate keyon
//Registers, also used to check for changes
reg20, reg40, reg60, reg80, regE0 uint8
//Active part of the envelope we're in
state OperatorState
//0xff when tremolo is enabled
tremoloMask uint8
//Strength of the vibrato
vibStrength uint8
//Keep track of the calculated KSR so we can check for changes
ksr uint8
}
// NewOperator creates a new OPL2/3 channel operator
func NewOperator() *Operator {
o := Operator{}
o.SetupOperator()
return &o
}
// SetupOperator sets up a channel operator to defaults
func (o *Operator) SetupOperator() {
o.SetState(OperatorStateOff)
o.rateZero = (1 << OperatorStateOff)
o.sustainLevel = cEnvMax
o.currentLevel = cEnvMax
o.totalLevel = cEnvMax
o.volume = cEnvMax
}
// UpdateAttack updates the attack rate on the envelope
//We zero out when rate == 0
func (o *Operator) UpdateAttack(chip *Chip) {
rate := uint8(o.reg60 >> 4)
if rate != 0 {
val := uint8((rate << 2) + o.ksr)
o.attackAdd = chip.attackRates[val]
o.rateZero &^= uint8(1 << OperatorStateAttack)
} else {
o.attackAdd = 0
o.rateZero |= (1 << OperatorStateAttack)
}
}
// UpdateDecay updates the decay rate on the envelope
func (o *Operator) UpdateDecay(chip *Chip) {
rate := uint8(o.reg60 & 0xf)
if rate != 0 {
val := uint8((rate << 2) + o.ksr)
o.decayAdd = chip.linearRates[val]
o.rateZero &^= uint8(1 << OperatorStateDecay)
} else {
o.decayAdd = 0
o.rateZero |= (1 << OperatorStateDecay)
}
}
// UpdateRelease updates the release rate on the envelope
func (o *Operator) UpdateRelease(chip *Chip) {
rate := uint8(o.reg80 & 0xf)
if rate != 0 {
val := uint8((rate << 2) + o.ksr)
o.releaseAdd = chip.linearRates[val]
o.rateZero &^= uint8(1 << OperatorStateRelease)
if (o.reg20 & cMaskSustain) == 0 {
o.rateZero &^= uint8(1 << OperatorStateSustain)
}
} else {
o.rateZero |= (1 << OperatorStateRelease)
o.releaseAdd = 0
if (o.reg20 & cMaskSustain) == 0 {
o.rateZero |= (1 << OperatorStateSustain)
}
}
}
// UpdateAttenuation updates the attenuation on the operator
func (o *Operator) UpdateAttenuation() {
base := o.chanData >> cShiftKSLBase
kslBase := uint8(base)
tl := int32(o.reg40) & 0x3f
kslShift := cKslShiftTable[o.reg40>>6]
//Make sure the attenuation goes to the right bits
o.totalLevel = tl << (cEnvBits - 7) //Total level goes 2 bits below max
baseShift := int32(kslBase) << cEnvExtra
o.totalLevel += baseShift >> kslShift
}
// UpdateFrequency updates the frequency on the operator
func (o *Operator) UpdateFrequency() {
freq := uint32(o.chanData & ((1 << 10) - 1))
block := uint32((o.chanData >> 10) & 0xff)
if cWavePrecision != 0 {
block = 7 - block
o.waveAdd = int(freq*o.freqMul) >> block
} else {
o.waveAdd = int(freq*o.freqMul) << block
}
if (o.reg20 & cMaskVibrato) != 0 {
o.vibStrength = (uint8)(freq >> 7)
if cWavePrecision != 0 {
o.vibrato = (uint32(o.vibStrength) * o.freqMul) >> block
} else {
o.vibrato = (uint32(o.vibStrength) << block) * o.freqMul
}
} else {
o.vibStrength = 0
o.vibrato = 0
}
}
// UpdateRates updates the envelope rates on the operator
func (o *Operator) UpdateRates(chip *Chip) {
//Mame seems to reverse this where enabling ksr actually lowers
//the rate, but pdf manuals says otherwise?
newKsr := uint8((uint8)((o.chanData >> cShiftKeyCode) & 0xff))
if (o.reg20 & cMaskKSR) == 0 {
newKsr >>= 2
}
if o.ksr == newKsr {
return
}
o.ksr = newKsr
o.UpdateAttack(chip)
o.UpdateDecay(chip)
o.UpdateRelease(chip)
}
// RateForward increments the operator's internal indexes
func (o *Operator) RateForward(add uint32) int32 {
o.rateIndex += add
ret := int32(o.rateIndex >> cRateSh)
o.rateIndex = o.rateIndex & cRateMask
return ret
}
// ForwardVolume updates the operator's current volume
func (o *Operator) ForwardVolume() int {
return o.currentLevel + o.volHandler()
}
// ForwardWave updates the operator's current waveform
func (o *Operator) ForwardWave() uint {
o.waveIndex += o.waveCurrent
return uint(o.waveIndex) >> cWaveSh
}
// Write20 writes data to register 0x20 on the operator
func (o *Operator) Write20(chip *Chip, val uint8) {
change := uint8((o.reg20 ^ val))
if change == 0 {
return
}
o.reg20 = val
//Shift the tremolo bit over the entire register, saved a branch, YES!
o.tremoloMask = val >> 7
o.tremoloMask &^= uint8((1 << cEnvExtra) - 1)
//Update specific features based on changes
if (change & cMaskKSR) != 0 {
o.UpdateRates(chip)
}
//With sustain enable the volume doesn't change
if (o.reg20&cMaskSustain) != 0 || o.releaseAdd == 0 {
o.rateZero |= (1 << OperatorStateSustain)
} else {
o.rateZero &^= uint8(1 << OperatorStateSustain)
}
//Frequency multiplier or vibrato changed
if (change & (0xf | cMaskVibrato)) != 0 {
o.freqMul = chip.freqMul[val&0xf]
o.UpdateFrequency()
}
}
// Write40 writes data to register 0x40 on the operator
func (o *Operator) Write40(chip *Chip, val uint8) {
if (o.reg40 ^ val) == 0 {
return
}
o.reg40 = val
o.UpdateAttenuation()
}
// Write60 writes data to register 0x60 on the operator
func (o *Operator) Write60(chip *Chip, val uint8) {
change := uint8(o.reg60 ^ val)
o.reg60 = val
if (change & 0x0f) != 0 {
o.UpdateDecay(chip)
}
if (change & 0xf0) != 0 {
o.UpdateAttack(chip)
}
}
// Write80 writes data to register 0x80 on the operator
func (o *Operator) Write80(chip *Chip, val uint8) {
change := uint8((o.reg80 ^ val))
if change == 0 {
return
}
o.reg80 = val
sustain := uint8(val >> 4)
//Turn 0xf into 0x1f
sustain |= (sustain + 1) & 0x10
o.sustainLevel = int32(sustain) << (cEnvBits - 5)
if (change & 0x0f) != 0 {
o.UpdateRelease(chip)
}
}
// WriteE0 writes data to register 0xE0 on the operator
func (o *Operator) WriteE0(chip *Chip, val uint8) {
if (o.regE0 ^ val) != 0 {
return
}
//in opl3 mode you can always selet 7 waveforms regardless of waveformselect
waveForm := uint8(val & (uint8(0x3&chip.waveFormMask) | (0x7 & uint8(chip.opl3Active))))
o.regE0 = val
if cDBOPLWave == cWaveHandler {
o.waveHandler = waveHandlerTable[waveForm]
} else {
o.waveBase = cWaveTable[cWaveBaseTable[waveForm]:]
o.waveStart = int(cWaveStartTable[waveForm]) << cWaveSh
o.waveMask = int(cWaveMaskTable[waveForm])
}
}
// SetState sets the current operator envelope state
func (o *Operator) SetState(s OperatorState) {
o.state = s
switch s {
default:
o.volHandler = o.volHandlerOFF
case OperatorStateRelease:
o.volHandler = o.volHandlerRELEASE
case OperatorStateSustain:
o.volHandler = o.volHandlerSUSTAIN
case OperatorStateDecay:
o.volHandler = o.volHandlerDECAY
case OperatorStateAttack:
o.volHandler = o.volHandlerATTACK
}
}
func (o *Operator) volHandlerOFF() int {
return cEnvMax
}
func (o *Operator) volHandlerRELEASE() int {
vol := o.volume
vol += o.RateForward(o.releaseAdd)
if vol >= cEnvMax {
o.volume = cEnvMax
o.SetState(OperatorStateOff)
return cEnvMax
}
o.volume = vol
return int(vol)
}
func (o *Operator) volHandlerSUSTAIN() int {
vol := o.volume
if (o.reg20 & cMaskSustain) != 0 {
return int(vol)
}
//In sustain phase, but not sustaining, do regular release
o.SetState(OperatorStateRelease)
return o.volHandlerRELEASE()
}
func (o *Operator) volHandlerDECAY() int {
vol := o.volume
vol += o.RateForward(o.decayAdd)
if vol >= o.sustainLevel {
//Check if we didn't overshoot max attenuation, then just go off
if vol >= cEnvMax {
o.volume = cEnvMax
o.SetState(OperatorStateOff)
return cEnvMax
}
//Continue as sustain
o.rateIndex = 0
o.SetState(OperatorStateSustain)
}
o.volume = vol
return int(vol)
}
func (o *Operator) volHandlerATTACK() int {
vol := o.volume
change := o.RateForward(o.attackAdd)
if change == 0 {
return int(vol)
}
evol := ^vol
evol *= change
evol >>= 3
vol += evol
if vol < cEnvMin {
o.volume = cEnvMin
o.rateIndex = 0
o.SetState(OperatorStateDecay)
return cEnvMin
}
o.volume = vol
return int(vol)
}
// Silent returns true if the operator is currently silent
func (o *Operator) Silent() bool {
if !envSilent(int(o.totalLevel + o.volume)) {
return false
}
if (o.rateZero & (1 << o.state)) == 0 {
return false
}
return true
}
// Prepare prepares the operator's data
func (o *Operator) Prepare(chip *Chip) {
o.currentLevel = int(o.totalLevel) + int(chip.tremoloValue&o.tremoloMask)
o.waveCurrent = o.waveAdd
if (o.vibStrength >> chip.vibratoShift) != 0 {
add := int(o.vibrato) >> chip.vibratoShift
//Sign extend over the shift value
neg := int(chip.vibratoSign)
//Negate the add with -1 or 0
add ^= neg
add -= neg
o.waveCurrent += add
}
}
// KeyOn updates the key-on state of the operator to true
func (o *Operator) KeyOn(mask uint8) {
if o.keyOn == 0 {
//Restart the frequency generator
if cDBOPLWave > cWaveHandler {
o.waveIndex = o.waveStart
} else {
o.waveIndex = 0
}
o.rateIndex = 0
o.SetState(OperatorStateAttack)
}
o.keyOn |= mask
}
// KeyOff updates the key-on state of the operator to false
func (o *Operator) KeyOff(mask uint8) {
o.keyOn &^= mask
if o.keyOn == 0 {
if o.state != OperatorStateOff {
o.SetState(OperatorStateRelease)
}
}
}
// GetWave gets the current waveform of the operator
func (o *Operator) GetWave(index uint, vol int) int {
if cDBOPLWave == cWaveHandler {
return o.waveHandler(index, vol<<(3-cEnvExtra))
} else if cDBOPLWave == cWaveTableMul {
wb := o.waveBase[index&uint(o.waveMask)]
base := int(wb)
mul := int(cMulTable[vol>>cEnvExtra])
val := (base * mul) >> cMulSh
return val
} else if cDBOPLWave == cWaveTableLog {
wave := int32(o.waveBase[index&uint(o.waveMask)])
total := uint32(int(wave&0x7fff) + vol<<(3-cEnvExtra))
sig := int32(cExpTable[total&0xff])
exp := total >> 8
neg := int32(0)
if wave < 0 {
neg = -1
}
return int((sig^neg)-neg) >> exp
} else {
panic("No valid wave routine")
}
}
// GetSample gets the current waveform of the operator, as a sample
func (o *Operator) GetSample(modulation int) int {
vol := o.ForwardVolume()
if envSilent(int(vol)) {
//Simply forward the wave
o.waveIndex += o.waveCurrent
return 0
}
index := o.ForwardWave()
index = uint(int(index) + modulation)
return o.GetWave(index, vol)
}