2
* Copyright (C) 2002-2010 The DOSBox Team
3
* OPL2/OPL3 emulation library
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Lesser General Public
7
* License as published by the Free Software Foundation; either
8
* version 2.1 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Lesser General Public License for more details.
15
* You should have received a copy of the GNU Lesser General Public
16
* License along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
* Originally based on ADLIBEMU.C, an AdLib/OPL2 emulation library by Ken Silverman
23
* Copyright (C) 1998-2001 Ken Silverman
24
* Ken Silverman's official web site: "http://www.advsys.net/ken"
29
#include <stdlib.h> // rand()
30
#include <string.h> // memset()
33
static Bit32u generator_add; // should be a chip parameter
35
static fltype recipsamp; // inverse of sampling rate
36
static Bit16s wavtable[WAVEPREC*3]; // wave form table
38
// vibrato/tremolo tables
39
static Bit32s vib_table[VIBTAB_SIZE];
40
static Bit32s trem_table[TREMTAB_SIZE*2];
42
static Bit32s vibval_const[BLOCKBUF_SIZE];
43
static Bit32s tremval_const[BLOCKBUF_SIZE];
45
// vibrato value tables (used per-operator)
46
static Bit32s vibval_var1[BLOCKBUF_SIZE];
47
static Bit32s vibval_var2[BLOCKBUF_SIZE];
48
//static Bit32s vibval_var3[BLOCKBUF_SIZE];
49
//static Bit32s vibval_var4[BLOCKBUF_SIZE];
51
// vibrato/trmolo value table pointers
52
static Bit32s *vibval1, *vibval2, *vibval3, *vibval4;
53
static Bit32s *tremval1, *tremval2, *tremval3, *tremval4;
56
// key scale level lookup table
57
static const fltype kslmul[4] = {
58
0.0, 0.5, 0.25, 1.0 // -> 0, 3, 1.5, 6 dB/oct
61
// frequency multiplicator lookup table
62
static const fltype frqmul_tab[16] = {
63
0.5,1,2,3,4,5,6,7,8,9,10,10,12,12,15,15
65
// calculated frequency multiplication values (depend on sampling rate)
66
static fltype frqmul[16];
69
static Bit8u kslev[8][16];
71
// map a channel number to the register offset of the modulator (=register base)
72
static const Bit8u modulatorbase[9] = {
78
// map a register base to a modulator operator number or operator number
79
#if defined(OPLTYPE_IS_OPL3)
80
static const Bit8u regbase2modop[44] = {
81
0,1,2,0,1,2,0,0,3,4,5,3,4,5,0,0,6,7,8,6,7,8, // first set
82
18,19,20,18,19,20,0,0,21,22,23,21,22,23,0,0,24,25,26,24,25,26 // second set
84
static const Bit8u regbase2op[44] = {
85
0,1,2,9,10,11,0,0,3,4,5,12,13,14,0,0,6,7,8,15,16,17, // first set
86
18,19,20,27,28,29,0,0,21,22,23,30,31,32,0,0,24,25,26,33,34,35 // second set
89
static const Bit8u regbase2modop[22] = {
90
0,1,2,0,1,2,0,0,3,4,5,3,4,5,0,0,6,7,8,6,7,8
92
static const Bit8u regbase2op[22] = {
93
0,1,2,9,10,11,0,0,3,4,5,12,13,14,0,0,6,7,8,15,16,17
98
// start of the waveform
99
static Bit32u waveform[8] = {
110
// length of the waveform as mask
111
static Bit32u wavemask[8] = {
122
// where the first entry resides
123
static Bit32u wavestart[8] = {
134
// envelope generator function constants
135
static fltype attackconst[4] = {
141
static fltype decrelconst[4] = {
142
(fltype)(1/39.28064),
143
(fltype)(1/31.41608),
144
(fltype)(1/26.17344),
149
void operator_advance(op_type* op_pt, Bit32s vib) {
150
op_pt->wfpos = op_pt->tcount; // waveform position
152
// advance waveform time
153
op_pt->tcount += op_pt->tinc;
154
op_pt->tcount += (Bit32s)(op_pt->tinc)*vib/FIXEDPT;
156
op_pt->generator_pos += generator_add;
159
void operator_advance_drums(op_type* op_pt1, Bit32s vib1, op_type* op_pt2, Bit32s vib2, op_type* op_pt3, Bit32s vib3) {
160
Bit32u c1 = op_pt1->tcount/FIXEDPT;
161
Bit32u c3 = op_pt3->tcount/FIXEDPT;
162
Bit32u phasebit = (((c1 & 0x88) ^ ((c1<<5) & 0x80)) | ((c3 ^ (c3<<2)) & 0x20)) ? 0x02 : 0x00;
164
Bit32u noisebit = rand()&1;
166
Bit32u snare_phase_bit = (((Bitu)((op_pt1->tcount/FIXEDPT) / 0x100))&1);
169
Bit32u inttm = (phasebit<<8) | (0x34<<(phasebit ^ (noisebit<<1)));
170
op_pt1->wfpos = inttm*FIXEDPT; // waveform position
171
// advance waveform time
172
op_pt1->tcount += op_pt1->tinc;
173
op_pt1->tcount += (Bit32s)(op_pt1->tinc)*vib1/FIXEDPT;
174
op_pt1->generator_pos += generator_add;
177
inttm = ((1+snare_phase_bit) ^ noisebit)<<8;
178
op_pt2->wfpos = inttm*FIXEDPT; // waveform position
179
// advance waveform time
180
op_pt2->tcount += op_pt2->tinc;
181
op_pt2->tcount += (Bit32s)(op_pt2->tinc)*vib2/FIXEDPT;
182
op_pt2->generator_pos += generator_add;
185
inttm = (1+phasebit)<<8;
186
op_pt3->wfpos = inttm*FIXEDPT; // waveform position
187
// advance waveform time
188
op_pt3->tcount += op_pt3->tinc;
189
op_pt3->tcount += (Bit32s)(op_pt3->tinc)*vib3/FIXEDPT;
190
op_pt3->generator_pos += generator_add;
194
// output level is sustained, mode changes only when operator is turned off (->release)
195
// or when the keep-sustained bit is turned off (->sustain_nokeep)
196
void operator_output(op_type* op_pt, Bit32s modulator, Bit32s trem) {
197
if (op_pt->op_state != OF_TYPE_OFF) {
198
op_pt->lastcval = op_pt->cval;
199
Bit32u i = (Bit32u)((op_pt->wfpos+modulator)/FIXEDPT);
201
// wform: -16384 to 16383 (0x4000)
202
// trem : 32768 to 65535 (0x10000)
203
// step_amp: 0.0 to 1.0
204
// vol : 1/2^14 to 1/2^29 (/0x4000; /1../0x8000)
206
op_pt->cval = (Bit32s)(op_pt->step_amp*op_pt->vol*op_pt->cur_wform[i&op_pt->cur_wmask]*trem/16.0);
211
// no action, operator is off
212
void operator_off(op_type* op_pt) {
216
// output level is sustained, mode changes only when operator is turned off (->release)
217
// or when the keep-sustained bit is turned off (->sustain_nokeep)
218
void operator_sustain(op_type* op_pt) {
219
Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
220
for (Bit32u ct=0; ct<num_steps_add; ct++) {
221
op_pt->cur_env_step++;
223
op_pt->generator_pos -= num_steps_add*FIXEDPT;
226
// operator in release mode, if output level reaches zero the operator is turned off
227
void operator_release(op_type* op_pt) {
229
if (op_pt->amp > 0.00000001) {
231
op_pt->amp *= op_pt->releasemul;
234
Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
235
for (Bit32u ct=0; ct<num_steps_add; ct++) {
236
op_pt->cur_env_step++; // sample counter
237
if ((op_pt->cur_env_step & op_pt->env_step_r)==0) {
238
if (op_pt->amp <= 0.00000001) {
239
// release phase finished, turn off this operator
241
if (op_pt->op_state == OF_TYPE_REL) {
242
op_pt->op_state = OF_TYPE_OFF;
245
op_pt->step_amp = op_pt->amp;
248
op_pt->generator_pos -= num_steps_add*FIXEDPT;
251
// operator in decay mode, if sustain level is reached the output level is either
252
// kept (sustain level keep enabled) or the operator is switched into release mode
253
void operator_decay(op_type* op_pt) {
254
if (op_pt->amp > op_pt->sustain_level) {
256
op_pt->amp *= op_pt->decaymul;
259
Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
260
for (Bit32u ct=0; ct<num_steps_add; ct++) {
261
op_pt->cur_env_step++;
262
if ((op_pt->cur_env_step & op_pt->env_step_d)==0) {
263
if (op_pt->amp <= op_pt->sustain_level) {
264
// decay phase finished, sustain level reached
265
if (op_pt->sus_keep) {
266
// keep sustain level (until turned off)
267
op_pt->op_state = OF_TYPE_SUS;
268
op_pt->amp = op_pt->sustain_level;
270
// next: release phase
271
op_pt->op_state = OF_TYPE_SUS_NOKEEP;
274
op_pt->step_amp = op_pt->amp;
277
op_pt->generator_pos -= num_steps_add*FIXEDPT;
280
// operator in attack mode, if full output level is reached,
281
// the operator is switched into decay mode
282
void operator_attack(op_type* op_pt) {
283
op_pt->amp = ((op_pt->a3*op_pt->amp + op_pt->a2)*op_pt->amp + op_pt->a1)*op_pt->amp + op_pt->a0;
285
Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
286
for (Bit32u ct=0; ct<num_steps_add; ct++) {
287
op_pt->cur_env_step++; // next sample
288
if ((op_pt->cur_env_step & op_pt->env_step_a)==0) { // check if next step already reached
289
if (op_pt->amp > 1.0) {
290
// attack phase finished, next: decay
291
op_pt->op_state = OF_TYPE_DEC;
293
op_pt->step_amp = 1.0;
295
op_pt->step_skip_pos_a <<= 1;
296
if (op_pt->step_skip_pos_a==0) op_pt->step_skip_pos_a = 1;
297
if (op_pt->step_skip_pos_a & op_pt->env_step_skip_a) { // check if required to skip next step
298
op_pt->step_amp = op_pt->amp;
302
op_pt->generator_pos -= num_steps_add*FIXEDPT;
306
typedef void (*optype_fptr)(op_type*);
308
optype_fptr opfuncs[6] = {
312
operator_sustain, // sustain phase (keeping level)
313
operator_release, // sustain_nokeep phase (release-style)
317
void change_attackrate(Bitu regbase, op_type* op_pt) {
318
Bits attackrate = adlibreg[ARC_ATTR_DECR+regbase]>>4;
320
fltype f = (fltype)(pow(FL2,(fltype)attackrate+(op_pt->toff>>2)-1)*attackconst[op_pt->toff&3]*recipsamp);
321
// attack rate coefficients
322
op_pt->a0 = (fltype)(0.0377*f);
323
op_pt->a1 = (fltype)(10.73*f+1);
324
op_pt->a2 = (fltype)(-17.57*f);
325
op_pt->a3 = (fltype)(7.42*f);
327
Bits step_skip = attackrate*4 + op_pt->toff;
328
Bits steps = step_skip >> 2;
329
op_pt->env_step_a = (1<<(steps<=12?12-steps:0))-1;
331
Bits step_num = (step_skip<=48)?(4-(step_skip&3)):0;
332
static Bit8u step_skip_mask[5] = {0xff, 0xfe, 0xee, 0xba, 0xaa};
333
op_pt->env_step_skip_a = step_skip_mask[step_num];
335
#if defined(OPLTYPE_IS_OPL3)
340
op_pt->a0 = (fltype)(2.0); // something that triggers an immediate transition to amp:=1.0
341
op_pt->a1 = (fltype)(0.0);
342
op_pt->a2 = (fltype)(0.0);
343
op_pt->a3 = (fltype)(0.0);
351
op_pt->env_step_a = 0;
352
op_pt->env_step_skip_a = 0;
356
void change_decayrate(Bitu regbase, op_type* op_pt) {
357
Bits decayrate = adlibreg[ARC_ATTR_DECR+regbase]&15;
358
// decaymul should be 1.0 when decayrate==0
360
fltype f = (fltype)(-7.4493*decrelconst[op_pt->toff&3]*recipsamp);
361
op_pt->decaymul = (fltype)(pow(FL2,f*pow(FL2,(fltype)(decayrate+(op_pt->toff>>2)))));
362
Bits steps = (decayrate*4 + op_pt->toff) >> 2;
363
op_pt->env_step_d = (1<<(steps<=12?12-steps:0))-1;
365
op_pt->decaymul = 1.0;
366
op_pt->env_step_d = 0;
370
void change_releaserate(Bitu regbase, op_type* op_pt) {
371
Bits releaserate = adlibreg[ARC_SUSL_RELR+regbase]&15;
372
// releasemul should be 1.0 when releaserate==0
374
fltype f = (fltype)(-7.4493*decrelconst[op_pt->toff&3]*recipsamp);
375
op_pt->releasemul = (fltype)(pow(FL2,f*pow(FL2,(fltype)(releaserate+(op_pt->toff>>2)))));
376
Bits steps = (releaserate*4 + op_pt->toff) >> 2;
377
op_pt->env_step_r = (1<<(steps<=12?12-steps:0))-1;
379
op_pt->releasemul = 1.0;
380
op_pt->env_step_r = 0;
384
void change_sustainlevel(Bitu regbase, op_type* op_pt) {
385
Bits sustainlevel = adlibreg[ARC_SUSL_RELR+regbase]>>4;
386
// sustainlevel should be 0.0 when sustainlevel==15 (max)
387
if (sustainlevel<15) {
388
op_pt->sustain_level = (fltype)(pow(FL2,(fltype)sustainlevel * (-FL05)));
390
op_pt->sustain_level = 0.0;
394
void change_waveform(Bitu regbase, op_type* op_pt) {
395
#if defined(OPLTYPE_IS_OPL3)
396
if (regbase>=ARC_SECONDSET) regbase -= (ARC_SECONDSET-22); // second set starts at 22
398
// waveform selection
399
op_pt->cur_wmask = wavemask[wave_sel[regbase]];
400
op_pt->cur_wform = &wavtable[waveform[wave_sel[regbase]]];
401
// (might need to be adapted to waveform type here...)
404
void change_keepsustain(Bitu regbase, op_type* op_pt) {
405
op_pt->sus_keep = (adlibreg[ARC_TVS_KSR_MUL+regbase]&0x20)>0;
406
if (op_pt->op_state==OF_TYPE_SUS) {
407
if (!op_pt->sus_keep) op_pt->op_state = OF_TYPE_SUS_NOKEEP;
408
} else if (op_pt->op_state==OF_TYPE_SUS_NOKEEP) {
409
if (op_pt->sus_keep) op_pt->op_state = OF_TYPE_SUS;
413
// enable/disable vibrato/tremolo LFO effects
414
void change_vibrato(Bitu regbase, op_type* op_pt) {
415
op_pt->vibrato = (adlibreg[ARC_TVS_KSR_MUL+regbase]&0x40)!=0;
416
op_pt->tremolo = (adlibreg[ARC_TVS_KSR_MUL+regbase]&0x80)!=0;
419
// change amount of self-feedback
420
void change_feedback(Bitu chanbase, op_type* op_pt) {
421
Bits feedback = adlibreg[ARC_FEEDBACK+chanbase]&14;
422
if (feedback) op_pt->mfbi = (Bit32s)(pow(FL2,(fltype)((feedback>>1)+8)));
423
else op_pt->mfbi = 0;
426
void change_frequency(Bitu chanbase, Bitu regbase, op_type* op_pt) {
428
Bit32u frn = ((((Bit32u)adlibreg[ARC_KON_BNUM+chanbase])&3)<<8) + (Bit32u)adlibreg[ARC_FREQ_NUM+chanbase];
429
// block number/octave
430
Bit32u oct = ((((Bit32u)adlibreg[ARC_KON_BNUM+chanbase])>>2)&7);
431
op_pt->freq_high = (Bit32s)((frn>>7)&7);
434
Bit32u note_sel = (adlibreg[8]>>6)&1;
435
op_pt->toff = ((frn>>9)&(note_sel^1)) | ((frn>>8)¬e_sel);
436
op_pt->toff += (oct<<1);
438
// envelope scaling (KSR)
439
if (!(adlibreg[ARC_TVS_KSR_MUL+regbase]&0x10)) op_pt->toff >>= 2;
442
op_pt->tinc = (Bit32u)((((fltype)(frn<<oct))*frqmul[adlibreg[ARC_TVS_KSR_MUL+regbase]&15]));
444
fltype vol_in = (fltype)((fltype)(adlibreg[ARC_KSL_OUTLEV+regbase]&63) +
445
kslmul[adlibreg[ARC_KSL_OUTLEV+regbase]>>6]*kslev[oct][frn>>6]);
446
op_pt->vol = (fltype)(pow(FL2,(fltype)(vol_in * -0.125 - 14)));
448
// operator frequency changed, care about features that depend on it
449
change_attackrate(regbase,op_pt);
450
change_decayrate(regbase,op_pt);
451
change_releaserate(regbase,op_pt);
454
void enable_operator(Bitu regbase, op_type* op_pt, Bit32u act_type) {
455
// check if this is really an off-on transition
456
if (op_pt->act_state == OP_ACT_OFF) {
457
Bits wselbase = regbase;
458
if (wselbase>=ARC_SECONDSET) wselbase -= (ARC_SECONDSET-22); // second set starts at 22
460
op_pt->tcount = wavestart[wave_sel[wselbase]]*FIXEDPT;
462
// start with attack mode
463
op_pt->op_state = OF_TYPE_ATT;
464
op_pt->act_state |= act_type;
468
void disable_operator(op_type* op_pt, Bit32u act_type) {
469
// check if this is really an on-off transition
470
if (op_pt->act_state != OP_ACT_OFF) {
471
op_pt->act_state &= (~act_type);
472
if (op_pt->act_state == OP_ACT_OFF) {
473
if (op_pt->op_state != OF_TYPE_OFF) op_pt->op_state = OF_TYPE_REL;
478
void adlib_init(Bit32u samplerate) {
481
int_samplerate = samplerate;
483
generator_add = (Bit32u)(INTFREQU*FIXEDPT/int_samplerate);
486
memset((void *)adlibreg,0,sizeof(adlibreg));
487
memset((void *)op,0,sizeof(op_type)*MAXOPERATORS);
488
memset((void *)wave_sel,0,sizeof(wave_sel));
490
for (i=0;i<MAXOPERATORS;i++) {
491
op[i].op_state = OF_TYPE_OFF;
492
op[i].act_state = OP_ACT_OFF;
494
op[i].step_amp = 0.0;
499
op[i].cur_wmask = wavemask[0];
500
op[i].cur_wform = &wavtable[waveform[0]];
503
op[i].generator_pos = 0;
504
op[i].cur_env_step = 0;
505
op[i].env_step_a = 0;
506
op[i].env_step_d = 0;
507
op[i].env_step_r = 0;
508
op[i].step_skip_pos_a = 0;
509
op[i].env_step_skip_a = 0;
511
#if defined(OPLTYPE_IS_OPL3)
512
op[i].is_4op = false;
513
op[i].is_4op_attached = false;
519
recipsamp = 1.0 / (fltype)int_samplerate;
520
for (i=15;i>=0;i--) {
521
frqmul[i] = (fltype)(frqmul_tab[i]*INTFREQU/(fltype)WAVEPREC*(fltype)FIXEDPT*recipsamp);
528
// create vibrato table
533
for (i=4; i<VIBTAB_SIZE; i++) vib_table[i] = vib_table[i-4]*-1;
535
// vibrato at ~6.1 ?? (opl3 docs say 6.1, opl4 docs say 6.0, y8950 docs say 6.4)
536
vibtab_add = (Bit32u)(VIBTAB_SIZE*FIXEDPT_LFO/8192*INTFREQU/int_samplerate);
539
for (i=0; i<BLOCKBUF_SIZE; i++) vibval_const[i] = 0;
542
// create tremolo table
543
Bit32s trem_table_int[TREMTAB_SIZE];
544
for (i=0; i<14; i++) trem_table_int[i] = i-13; // upwards (13 to 26 -> -0.5/6 to 0)
545
for (i=14; i<41; i++) trem_table_int[i] = -i+14; // downwards (26 to 0 -> 0 to -1/6)
546
for (i=41; i<53; i++) trem_table_int[i] = i-40-26; // upwards (1 to 12 -> -1/6 to -0.5/6)
548
for (i=0; i<TREMTAB_SIZE; i++) {
549
// 0.0 .. -26/26*4.8/6 == [0.0 .. -0.8], 4/53 steps == [1 .. 0.57]
550
fltype trem_val1=(fltype)(((fltype)trem_table_int[i])*4.8/26.0/6.0); // 4.8db
551
fltype trem_val2=(fltype)((fltype)((Bit32s)(trem_table_int[i]/4))*1.2/6.0/6.0); // 1.2db (larger stepping)
553
trem_table[i] = (Bit32s)(pow(FL2,trem_val1)*FIXEDPT);
554
trem_table[TREMTAB_SIZE+i] = (Bit32s)(pow(FL2,trem_val2)*FIXEDPT);
558
tremtab_add = (Bit32u)((fltype)TREMTAB_SIZE * TREM_FREQ * FIXEDPT_LFO / (fltype)int_samplerate);
561
for (i=0; i<BLOCKBUF_SIZE; i++) tremval_const[i] = FIXEDPT;
564
static Bitu initfirstime = 0;
568
// create waveform tables
569
for (i=0;i<(WAVEPREC>>1);i++) {
570
wavtable[(i<<1) +WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<1) )*PI*2/WAVEPREC));
571
wavtable[(i<<1)+1+WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<1)+1)*PI*2/WAVEPREC));
572
wavtable[i] = wavtable[(i<<1) +WAVEPREC];
573
// alternative: (zero-less)
574
/* wavtable[(i<<1) +WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<2)+1)*PI/WAVEPREC));
575
wavtable[(i<<1)+1+WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<2)+3)*PI/WAVEPREC));
576
wavtable[i] = wavtable[(i<<1)-1+WAVEPREC]; */
578
for (i=0;i<(WAVEPREC>>3);i++) {
579
wavtable[i+(WAVEPREC<<1)] = wavtable[i+(WAVEPREC>>3)]-16384;
580
wavtable[i+((WAVEPREC*17)>>3)] = wavtable[i+(WAVEPREC>>2)]+16384;
583
// key scale level table verified ([table in book]*8/3)
584
kslev[7][0] = 0; kslev[7][1] = 24; kslev[7][2] = 32; kslev[7][3] = 37;
585
kslev[7][4] = 40; kslev[7][5] = 43; kslev[7][6] = 45; kslev[7][7] = 47;
587
for (i=9;i<16;i++) kslev[7][i] = (Bit8u)(i+41);
590
oct = (Bits)kslev[j+1][i]-8;
591
if (oct < 0) oct = 0;
592
kslev[j][i] = (Bit8u)oct;
601
void adlib_write(Bitu idx, Bit8u val) {
602
Bit32u second_set = idx&0x100;
607
// here we check for the second set registers, too:
609
case 0x02: // timer1 counter
610
case 0x03: // timer2 counter
613
// IRQ reset, timer mask/start
615
// clear IRQ bits in status register
621
#if defined(OPLTYPE_IS_OPL3)
622
case 0x04|ARC_SECONDSET:
623
// 4op enable/disable switches for each possible channel
624
op[0].is_4op = (val&1)>0;
625
op[3].is_4op_attached = op[0].is_4op;
626
op[1].is_4op = (val&2)>0;
627
op[4].is_4op_attached = op[1].is_4op;
628
op[2].is_4op = (val&4)>0;
629
op[5].is_4op_attached = op[2].is_4op;
630
op[18].is_4op = (val&8)>0;
631
op[21].is_4op_attached = op[18].is_4op;
632
op[19].is_4op = (val&16)>0;
633
op[22].is_4op_attached = op[19].is_4op;
634
op[20].is_4op = (val&32)>0;
635
op[23].is_4op_attached = op[20].is_4op;
637
case 0x05|ARC_SECONDSET:
647
case ARC_TVS_KSR_MUL:
648
case ARC_TVS_KSR_MUL+0x10: {
649
// tremolo/vibrato/sustain keeping enabled; key scale rate; frequency multiplication
651
Bitu base = (idx-ARC_TVS_KSR_MUL)&0xff;
652
if ((num<6) && (base<22)) {
653
Bitu modop = regbase2modop[second_set?(base+22):base];
654
Bitu regbase = base+second_set;
655
Bitu chanbase = second_set?(modop-18+ARC_SECONDSET):modop;
657
// change tremolo/vibrato and sustain keeping of this operator
658
op_type* op_ptr = &op[modop+((num<3) ? 0 : 9)];
659
change_keepsustain(regbase,op_ptr);
660
change_vibrato(regbase,op_ptr);
662
// change frequency calculations of this operator as
663
// key scale rate and frequency multiplicator can be changed
664
#if defined(OPLTYPE_IS_OPL3)
665
if ((adlibreg[0x105]&1) && (op[modop].is_4op_attached)) {
666
// operator uses frequency of channel
667
change_frequency(chanbase-3,regbase,op_ptr);
669
change_frequency(chanbase,regbase,op_ptr);
672
change_frequency(chanbase,base,op_ptr);
678
case ARC_KSL_OUTLEV+0x10: {
679
// key scale level; output rate
681
Bitu base = (idx-ARC_KSL_OUTLEV)&0xff;
682
if ((num<6) && (base<22)) {
683
Bitu modop = regbase2modop[second_set?(base+22):base];
684
Bitu chanbase = second_set?(modop-18+ARC_SECONDSET):modop;
686
// change frequency calculations of this operator as
687
// key scale level and output rate can be changed
688
op_type* op_ptr = &op[modop+((num<3) ? 0 : 9)];
689
#if defined(OPLTYPE_IS_OPL3)
690
Bitu regbase = base+second_set;
691
if ((adlibreg[0x105]&1) && (op[modop].is_4op_attached)) {
692
// operator uses frequency of channel
693
change_frequency(chanbase-3,regbase,op_ptr);
695
change_frequency(chanbase,regbase,op_ptr);
698
change_frequency(chanbase,base,op_ptr);
704
case ARC_ATTR_DECR+0x10: {
705
// attack/decay rates
707
Bitu base = (idx-ARC_ATTR_DECR)&0xff;
708
if ((num<6) && (base<22)) {
709
Bitu regbase = base+second_set;
711
// change attack rate and decay rate of this operator
712
op_type* op_ptr = &op[regbase2op[second_set?(base+22):base]];
713
change_attackrate(regbase,op_ptr);
714
change_decayrate(regbase,op_ptr);
719
case ARC_SUSL_RELR+0x10: {
720
// sustain level; release rate
722
Bitu base = (idx-ARC_SUSL_RELR)&0xff;
723
if ((num<6) && (base<22)) {
724
Bitu regbase = base+second_set;
726
// change sustain level and release rate of this operator
727
op_type* op_ptr = &op[regbase2op[second_set?(base+22):base]];
728
change_releaserate(regbase,op_ptr);
729
change_sustainlevel(regbase,op_ptr);
734
// 0xa0-0xa8 low8 frequency
735
Bitu base = (idx-ARC_FREQ_NUM)&0xff;
737
Bits opbase = second_set?(base+18):base;
738
#if defined(OPLTYPE_IS_OPL3)
739
if ((adlibreg[0x105]&1) && op[opbase].is_4op_attached) break;
741
// regbase of modulator:
742
Bits modbase = modulatorbase[base]+second_set;
744
Bitu chanbase = base+second_set;
746
change_frequency(chanbase,modbase,&op[opbase]);
747
change_frequency(chanbase,modbase+3,&op[opbase+9]);
748
#if defined(OPLTYPE_IS_OPL3)
749
// for 4op channels all four operators are modified to the frequency of the channel
750
if ((adlibreg[0x105]&1) && op[second_set?(base+18):base].is_4op) {
751
change_frequency(chanbase,modbase+8,&op[opbase+3]);
752
change_frequency(chanbase,modbase+3+8,&op[opbase+3+9]);
759
if (idx == ARC_PERC_MODE) {
760
#if defined(OPLTYPE_IS_OPL3)
761
if (second_set) return;
764
if ((val&0x30) == 0x30) { // BassDrum active
765
enable_operator(16,&op[6],OP_ACT_PERC);
766
change_frequency(6,16,&op[6]);
767
enable_operator(16+3,&op[6+9],OP_ACT_PERC);
768
change_frequency(6,16+3,&op[6+9]);
770
disable_operator(&op[6],OP_ACT_PERC);
771
disable_operator(&op[6+9],OP_ACT_PERC);
773
if ((val&0x28) == 0x28) { // Snare active
774
enable_operator(17+3,&op[16],OP_ACT_PERC);
775
change_frequency(7,17+3,&op[16]);
777
disable_operator(&op[16],OP_ACT_PERC);
779
if ((val&0x24) == 0x24) { // TomTom active
780
enable_operator(18,&op[8],OP_ACT_PERC);
781
change_frequency(8,18,&op[8]);
783
disable_operator(&op[8],OP_ACT_PERC);
785
if ((val&0x22) == 0x22) { // Cymbal active
786
enable_operator(18+3,&op[8+9],OP_ACT_PERC);
787
change_frequency(8,18+3,&op[8+9]);
789
disable_operator(&op[8+9],OP_ACT_PERC);
791
if ((val&0x21) == 0x21) { // Hihat active
792
enable_operator(17,&op[7],OP_ACT_PERC);
793
change_frequency(7,17,&op[7]);
795
disable_operator(&op[7],OP_ACT_PERC);
801
Bitu base = (idx-ARC_KON_BNUM)&0xff;
803
Bits opbase = second_set?(base+18):base;
804
#if defined(OPLTYPE_IS_OPL3)
805
if ((adlibreg[0x105]&1) && op[opbase].is_4op_attached) break;
807
// regbase of modulator:
808
Bits modbase = modulatorbase[base]+second_set;
811
// operator switched on
812
enable_operator(modbase,&op[opbase],OP_ACT_NORMAL); // modulator (if 2op)
813
enable_operator(modbase+3,&op[opbase+9],OP_ACT_NORMAL); // carrier (if 2op)
814
#if defined(OPLTYPE_IS_OPL3)
815
// for 4op channels all four operators are switched on
816
if ((adlibreg[0x105]&1) && op[opbase].is_4op) {
817
// turn on chan+3 operators as well
818
enable_operator(modbase+8,&op[opbase+3],OP_ACT_NORMAL);
819
enable_operator(modbase+3+8,&op[opbase+3+9],OP_ACT_NORMAL);
823
// operator switched off
824
disable_operator(&op[opbase],OP_ACT_NORMAL);
825
disable_operator(&op[opbase+9],OP_ACT_NORMAL);
826
#if defined(OPLTYPE_IS_OPL3)
827
// for 4op channels all four operators are switched off
828
if ((adlibreg[0x105]&1) && op[opbase].is_4op) {
829
// turn off chan+3 operators as well
830
disable_operator(&op[opbase+3],OP_ACT_NORMAL);
831
disable_operator(&op[opbase+3+9],OP_ACT_NORMAL);
836
Bitu chanbase = base+second_set;
838
// change frequency calculations of modulator and carrier (2op) as
839
// the frequency of the channel has changed
840
change_frequency(chanbase,modbase,&op[opbase]);
841
change_frequency(chanbase,modbase+3,&op[opbase+9]);
842
#if defined(OPLTYPE_IS_OPL3)
843
// for 4op channels all four operators are modified to the frequency of the channel
844
if ((adlibreg[0x105]&1) && op[second_set?(base+18):base].is_4op) {
845
// change frequency calculations of chan+3 operators as well
846
change_frequency(chanbase,modbase+8,&op[opbase+3]);
847
change_frequency(chanbase,modbase+3+8,&op[opbase+3+9]);
854
// 0xc0-0xc8 feedback/modulation type (AM/FM)
855
Bitu base = (idx-ARC_FEEDBACK)&0xff;
857
Bits opbase = second_set?(base+18):base;
858
Bitu chanbase = base+second_set;
859
change_feedback(chanbase,&op[opbase]);
860
#if defined(OPLTYPE_IS_OPL3)
862
op[opbase].left_pan = ((val&0x10)>>4);
863
op[opbase].right_pan = ((val&0x20)>>5);
869
case ARC_WAVE_SEL+0x10: {
871
Bitu base = (idx-ARC_WAVE_SEL)&0xff;
872
if ((num<6) && (base<22)) {
873
#if defined(OPLTYPE_IS_OPL3)
874
Bits wselbase = second_set?(base+22):base; // for easier mapping onto wave_sel[]
876
if (adlibreg[0x105]&1) wave_sel[wselbase] = val&7; // opl3 mode enabled, all waveforms accessible
877
else wave_sel[wselbase] = val&3;
878
op_type* op_ptr = &op[regbase2modop[wselbase]+((num<3) ? 0 : 9)];
879
change_waveform(wselbase,op_ptr);
881
if (adlibreg[0x01]&0x20) {
882
// wave selection enabled, change waveform
883
wave_sel[base] = val&3;
884
op_type* op_ptr = &op[regbase2modop[base]+((num<3) ? 0 : 9)];
885
change_waveform(base,op_ptr);
897
Bitu adlib_reg_read(Bitu port) {
898
#if defined(OPLTYPE_IS_OPL3)
899
// opl3-detection routines require ret&6 to be zero
905
// opl2-detection routines require ret&6 to be 6
913
void adlib_write_index(Bitu port, Bit8u val) {
916
#if defined(OPLTYPE_IS_OPL3)
918
// possibly second set
919
if (((adlibreg[0x105]&1)!=0) || (opl_index==5)) opl_index |= ARC_SECONDSET;
924
OPL_INLINE static void clipit16(Bit32s ival, Bit16s* outval) {
927
*outval=(Bit16s)ival;
938
// be careful with this
939
// uses cptr and chanval, outputs into outbufl(/outbufr)
940
// for opl3 check if opl3-mode is enabled (which uses stereo panning)
942
#if defined(OPLTYPE_IS_OPL3)
943
#define CHANVAL_OUT \
944
if (adlibreg[0x105]&1) { \
945
outbufl[i] += chanval*cptr[0].left_pan; \
946
outbufr[i] += chanval*cptr[0].right_pan; \
948
outbufl[i] += chanval; \
951
#define CHANVAL_OUT \
952
outbufl[i] += chanval;
955
void adlib_getsample(Bit16s* sndptr, Bits numsamples) {
959
Bit32s outbufl[BLOCKBUF_SIZE];
960
#if defined(OPLTYPE_IS_OPL3)
961
// second output buffer (right channel for opl3 stereo)
962
Bit32s outbufr[BLOCKBUF_SIZE];
965
// vibrato/tremolo lookup tables (global, to possibly be used by all operators)
966
Bit32s vib_lut[BLOCKBUF_SIZE];
967
Bit32s trem_lut[BLOCKBUF_SIZE];
969
Bits samples_to_process = numsamples;
971
for (Bits cursmp=0; cursmp<samples_to_process; cursmp+=endsamples) {
972
endsamples = samples_to_process-cursmp;
973
if (endsamples>BLOCKBUF_SIZE) endsamples = BLOCKBUF_SIZE;
975
memset((void*)&outbufl,0,endsamples*sizeof(Bit32s));
976
#if defined(OPLTYPE_IS_OPL3)
977
// clear second output buffer (opl3 stereo)
978
if (adlibreg[0x105]&1) memset((void*)&outbufr,0,endsamples*sizeof(Bit32s));
981
// calculate vibrato/tremolo lookup tables
982
Bit32s vib_tshift = ((adlibreg[ARC_PERC_MODE]&0x40)==0) ? 1 : 0; // 14cents/7cents switching
983
for (i=0;i<endsamples;i++) {
984
// cycle through vibrato table
985
vibtab_pos += vibtab_add;
986
if (vibtab_pos/FIXEDPT_LFO>=VIBTAB_SIZE) vibtab_pos-=VIBTAB_SIZE*FIXEDPT_LFO;
987
vib_lut[i] = vib_table[vibtab_pos/FIXEDPT_LFO]>>vib_tshift; // 14cents (14/100 of a semitone) or 7cents
989
// cycle through tremolo table
990
tremtab_pos += tremtab_add;
991
if (tremtab_pos/FIXEDPT_LFO>=TREMTAB_SIZE) tremtab_pos-=TREMTAB_SIZE*FIXEDPT_LFO;
992
if (adlibreg[ARC_PERC_MODE]&0x80) trem_lut[i] = trem_table[tremtab_pos/FIXEDPT_LFO];
993
else trem_lut[i] = trem_table[TREMTAB_SIZE+tremtab_pos/FIXEDPT_LFO];
996
if (adlibreg[ARC_PERC_MODE]&0x20) {
999
if (adlibreg[ARC_FEEDBACK+6]&1) {
1000
// additive synthesis
1001
if (cptr[9].op_state != OF_TYPE_OFF) {
1002
if (cptr[9].vibrato) {
1003
vibval1 = vibval_var1;
1004
for (i=0;i<endsamples;i++)
1005
vibval1[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1006
} else vibval1 = vibval_const;
1007
if (cptr[9].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1008
else tremval1 = tremval_const;
1010
// calculate channel output
1011
for (i=0;i<endsamples;i++) {
1012
operator_advance(&cptr[9],vibval1[i]);
1013
opfuncs[cptr[9].op_state](&cptr[9]);
1014
operator_output(&cptr[9],0,tremval1[i]);
1016
Bit32s chanval = cptr[9].cval*2;
1021
// frequency modulation
1022
if ((cptr[9].op_state != OF_TYPE_OFF) || (cptr[0].op_state != OF_TYPE_OFF)) {
1023
if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1024
vibval1 = vibval_var1;
1025
for (i=0;i<endsamples;i++)
1026
vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1027
} else vibval1 = vibval_const;
1028
if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1029
vibval2 = vibval_var2;
1030
for (i=0;i<endsamples;i++)
1031
vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1032
} else vibval2 = vibval_const;
1033
if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1034
else tremval1 = tremval_const;
1035
if (cptr[9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1036
else tremval2 = tremval_const;
1038
// calculate channel output
1039
for (i=0;i<endsamples;i++) {
1040
operator_advance(&cptr[0],vibval1[i]);
1041
opfuncs[cptr[0].op_state](&cptr[0]);
1042
operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1044
operator_advance(&cptr[9],vibval2[i]);
1045
opfuncs[cptr[9].op_state](&cptr[9]);
1046
operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
1048
Bit32s chanval = cptr[9].cval*2;
1055
if (op[8].op_state != OF_TYPE_OFF) {
1057
if (cptr[0].vibrato) {
1058
vibval3 = vibval_var1;
1059
for (i=0;i<endsamples;i++)
1060
vibval3[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1061
} else vibval3 = vibval_const;
1063
if (cptr[0].tremolo) tremval3 = trem_lut; // tremolo enabled, use table
1064
else tremval3 = tremval_const;
1066
// calculate channel output
1067
for (i=0;i<endsamples;i++) {
1068
operator_advance(&cptr[0],vibval3[i]);
1069
opfuncs[cptr[0].op_state](&cptr[0]); //TomTom
1070
operator_output(&cptr[0],0,tremval3[i]);
1071
Bit32s chanval = cptr[0].cval*2;
1076
//Snare/Hihat (j=7), Cymbal (j=8)
1077
if ((op[7].op_state != OF_TYPE_OFF) || (op[16].op_state != OF_TYPE_OFF) ||
1078
(op[17].op_state != OF_TYPE_OFF)) {
1080
if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1081
vibval1 = vibval_var1;
1082
for (i=0;i<endsamples;i++)
1083
vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1084
} else vibval1 = vibval_const;
1085
if ((cptr[9].vibrato) && (cptr[9].op_state == OF_TYPE_OFF)) {
1086
vibval2 = vibval_var2;
1087
for (i=0;i<endsamples;i++)
1088
vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1089
} else vibval2 = vibval_const;
1091
if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1092
else tremval1 = tremval_const;
1093
if (cptr[9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1094
else tremval2 = tremval_const;
1097
if ((cptr[9].vibrato) && (cptr[9].op_state == OF_TYPE_OFF)) {
1098
vibval4 = vibval_var2;
1099
for (i=0;i<endsamples;i++)
1100
vibval4[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1101
} else vibval4 = vibval_const;
1103
if (cptr[9].tremolo) tremval4 = trem_lut; // tremolo enabled, use table
1104
else tremval4 = tremval_const;
1106
// calculate channel output
1107
for (i=0;i<endsamples;i++) {
1108
operator_advance_drums(&op[7],vibval1[i],&op[7+9],vibval2[i],&op[8+9],vibval4[i]);
1110
opfuncs[op[7].op_state](&op[7]); //Hihat
1111
operator_output(&op[7],0,tremval1[i]);
1113
opfuncs[op[7+9].op_state](&op[7+9]); //Snare
1114
operator_output(&op[7+9],0,tremval2[i]);
1116
opfuncs[op[8+9].op_state](&op[8+9]); //Cymbal
1117
operator_output(&op[8+9],0,tremval4[i]);
1119
Bit32s chanval = (op[7].cval + op[7+9].cval + op[8+9].cval)*2;
1125
Bitu max_channel = NUM_CHANNELS;
1126
#if defined(OPLTYPE_IS_OPL3)
1127
if ((adlibreg[0x105]&1)==0) max_channel = NUM_CHANNELS/2;
1129
for (Bits cur_ch=max_channel-1; cur_ch>=0; cur_ch--) {
1130
// skip drum/percussion operators
1131
if ((adlibreg[ARC_PERC_MODE]&0x20) && (cur_ch >= 6) && (cur_ch < 9)) continue;
1134
#if defined(OPLTYPE_IS_OPL3)
1138
cptr = &op[cur_ch+9]; // second set is operator18-operator35
1139
k += (-9+256); // second set uses registers 0x100 onwards
1141
// check if this operator is part of a 4-op
1142
if ((adlibreg[0x105]&1) && cptr->is_4op_attached) continue;
1148
if (adlibreg[ARC_FEEDBACK+k]&1) {
1149
#if defined(OPLTYPE_IS_OPL3)
1150
if ((adlibreg[0x105]&1) && cptr->is_4op) {
1151
if (adlibreg[ARC_FEEDBACK+k+3]&1) {
1152
// AM-AM-style synthesis (op1[fb] + (op2 * op3) + op4)
1153
if (cptr[0].op_state != OF_TYPE_OFF) {
1154
if (cptr[0].vibrato) {
1155
vibval1 = vibval_var1;
1156
for (i=0;i<endsamples;i++)
1157
vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1158
} else vibval1 = vibval_const;
1159
if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1160
else tremval1 = tremval_const;
1162
// calculate channel output
1163
for (i=0;i<endsamples;i++) {
1164
operator_advance(&cptr[0],vibval1[i]);
1165
opfuncs[cptr[0].op_state](&cptr[0]);
1166
operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1168
Bit32s chanval = cptr[0].cval;
1173
if ((cptr[3].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF)) {
1174
if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1175
vibval1 = vibval_var1;
1176
for (i=0;i<endsamples;i++)
1177
vibval1[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1178
} else vibval1 = vibval_const;
1179
if (cptr[9].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1180
else tremval1 = tremval_const;
1181
if (cptr[3].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1182
else tremval2 = tremval_const;
1184
// calculate channel output
1185
for (i=0;i<endsamples;i++) {
1186
operator_advance(&cptr[9],vibval1[i]);
1187
opfuncs[cptr[9].op_state](&cptr[9]);
1188
operator_output(&cptr[9],0,tremval1[i]);
1190
operator_advance(&cptr[3],0);
1191
opfuncs[cptr[3].op_state](&cptr[3]);
1192
operator_output(&cptr[3],cptr[9].cval*FIXEDPT,tremval2[i]);
1194
Bit32s chanval = cptr[3].cval;
1199
if (cptr[3+9].op_state != OF_TYPE_OFF) {
1200
if (cptr[3+9].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1201
else tremval1 = tremval_const;
1203
// calculate channel output
1204
for (i=0;i<endsamples;i++) {
1205
operator_advance(&cptr[3+9],0);
1206
opfuncs[cptr[3+9].op_state](&cptr[3+9]);
1207
operator_output(&cptr[3+9],0,tremval1[i]);
1209
Bit32s chanval = cptr[3+9].cval;
1214
// AM-FM-style synthesis (op1[fb] + (op2 * op3 * op4))
1215
if (cptr[0].op_state != OF_TYPE_OFF) {
1216
if (cptr[0].vibrato) {
1217
vibval1 = vibval_var1;
1218
for (i=0;i<endsamples;i++)
1219
vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1220
} else vibval1 = vibval_const;
1221
if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1222
else tremval1 = tremval_const;
1224
// calculate channel output
1225
for (i=0;i<endsamples;i++) {
1226
operator_advance(&cptr[0],vibval1[i]);
1227
opfuncs[cptr[0].op_state](&cptr[0]);
1228
operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1230
Bit32s chanval = cptr[0].cval;
1235
if ((cptr[9].op_state != OF_TYPE_OFF) || (cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
1236
if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1237
vibval1 = vibval_var1;
1238
for (i=0;i<endsamples;i++)
1239
vibval1[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1240
} else vibval1 = vibval_const;
1241
if (cptr[9].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1242
else tremval1 = tremval_const;
1243
if (cptr[3].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1244
else tremval2 = tremval_const;
1245
if (cptr[3+9].tremolo) tremval3 = trem_lut; // tremolo enabled, use table
1246
else tremval3 = tremval_const;
1248
// calculate channel output
1249
for (i=0;i<endsamples;i++) {
1250
operator_advance(&cptr[9],vibval1[i]);
1251
opfuncs[cptr[9].op_state](&cptr[9]);
1252
operator_output(&cptr[9],0,tremval1[i]);
1254
operator_advance(&cptr[3],0);
1255
opfuncs[cptr[3].op_state](&cptr[3]);
1256
operator_output(&cptr[3],cptr[9].cval*FIXEDPT,tremval2[i]);
1258
operator_advance(&cptr[3+9],0);
1259
opfuncs[cptr[3+9].op_state](&cptr[3+9]);
1260
operator_output(&cptr[3+9],cptr[3].cval*FIXEDPT,tremval3[i]);
1262
Bit32s chanval = cptr[3+9].cval;
1270
// 2op additive synthesis
1271
if ((cptr[9].op_state == OF_TYPE_OFF) && (cptr[0].op_state == OF_TYPE_OFF)) continue;
1272
if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1273
vibval1 = vibval_var1;
1274
for (i=0;i<endsamples;i++)
1275
vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1276
} else vibval1 = vibval_const;
1277
if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1278
vibval2 = vibval_var2;
1279
for (i=0;i<endsamples;i++)
1280
vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1281
} else vibval2 = vibval_const;
1282
if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1283
else tremval1 = tremval_const;
1284
if (cptr[9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1285
else tremval2 = tremval_const;
1287
// calculate channel output
1288
for (i=0;i<endsamples;i++) {
1290
operator_advance(&cptr[0],vibval1[i]);
1291
opfuncs[cptr[0].op_state](&cptr[0]);
1292
operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1295
operator_advance(&cptr[9],vibval2[i]);
1296
opfuncs[cptr[9].op_state](&cptr[9]);
1297
operator_output(&cptr[9],0,tremval2[i]);
1299
Bit32s chanval = cptr[9].cval + cptr[0].cval;
1303
#if defined(OPLTYPE_IS_OPL3)
1304
if ((adlibreg[0x105]&1) && cptr->is_4op) {
1305
if (adlibreg[ARC_FEEDBACK+k+3]&1) {
1306
// FM-AM-style synthesis ((op1[fb] * op2) + (op3 * op4))
1307
if ((cptr[0].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF)) {
1308
if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1309
vibval1 = vibval_var1;
1310
for (i=0;i<endsamples;i++)
1311
vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1312
} else vibval1 = vibval_const;
1313
if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1314
vibval2 = vibval_var2;
1315
for (i=0;i<endsamples;i++)
1316
vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1317
} else vibval2 = vibval_const;
1318
if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1319
else tremval1 = tremval_const;
1320
if (cptr[9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1321
else tremval2 = tremval_const;
1323
// calculate channel output
1324
for (i=0;i<endsamples;i++) {
1325
operator_advance(&cptr[0],vibval1[i]);
1326
opfuncs[cptr[0].op_state](&cptr[0]);
1327
operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1329
operator_advance(&cptr[9],vibval2[i]);
1330
opfuncs[cptr[9].op_state](&cptr[9]);
1331
operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
1333
Bit32s chanval = cptr[9].cval;
1338
if ((cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
1339
if (cptr[3].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1340
else tremval1 = tremval_const;
1341
if (cptr[3+9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1342
else tremval2 = tremval_const;
1344
// calculate channel output
1345
for (i=0;i<endsamples;i++) {
1346
operator_advance(&cptr[3],0);
1347
opfuncs[cptr[3].op_state](&cptr[3]);
1348
operator_output(&cptr[3],0,tremval1[i]);
1350
operator_advance(&cptr[3+9],0);
1351
opfuncs[cptr[3+9].op_state](&cptr[3+9]);
1352
operator_output(&cptr[3+9],cptr[3].cval*FIXEDPT,tremval2[i]);
1354
Bit32s chanval = cptr[3+9].cval;
1360
// FM-FM-style synthesis (op1[fb] * op2 * op3 * op4)
1361
if ((cptr[0].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF) ||
1362
(cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
1363
if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1364
vibval1 = vibval_var1;
1365
for (i=0;i<endsamples;i++)
1366
vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1367
} else vibval1 = vibval_const;
1368
if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1369
vibval2 = vibval_var2;
1370
for (i=0;i<endsamples;i++)
1371
vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1372
} else vibval2 = vibval_const;
1373
if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1374
else tremval1 = tremval_const;
1375
if (cptr[9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1376
else tremval2 = tremval_const;
1377
if (cptr[3].tremolo) tremval3 = trem_lut; // tremolo enabled, use table
1378
else tremval3 = tremval_const;
1379
if (cptr[3+9].tremolo) tremval4 = trem_lut; // tremolo enabled, use table
1380
else tremval4 = tremval_const;
1382
// calculate channel output
1383
for (i=0;i<endsamples;i++) {
1384
operator_advance(&cptr[0],vibval1[i]);
1385
opfuncs[cptr[0].op_state](&cptr[0]);
1386
operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1388
operator_advance(&cptr[9],vibval2[i]);
1389
opfuncs[cptr[9].op_state](&cptr[9]);
1390
operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
1392
operator_advance(&cptr[3],0);
1393
opfuncs[cptr[3].op_state](&cptr[3]);
1394
operator_output(&cptr[3],cptr[9].cval*FIXEDPT,tremval3[i]);
1396
operator_advance(&cptr[3+9],0);
1397
opfuncs[cptr[3+9].op_state](&cptr[3+9]);
1398
operator_output(&cptr[3+9],cptr[3].cval*FIXEDPT,tremval4[i]);
1400
Bit32s chanval = cptr[3+9].cval;
1408
// 2op frequency modulation
1409
if ((cptr[9].op_state == OF_TYPE_OFF) && (cptr[0].op_state == OF_TYPE_OFF)) continue;
1410
if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1411
vibval1 = vibval_var1;
1412
for (i=0;i<endsamples;i++)
1413
vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1414
} else vibval1 = vibval_const;
1415
if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1416
vibval2 = vibval_var2;
1417
for (i=0;i<endsamples;i++)
1418
vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1419
} else vibval2 = vibval_const;
1420
if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1421
else tremval1 = tremval_const;
1422
if (cptr[9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1423
else tremval2 = tremval_const;
1425
// calculate channel output
1426
for (i=0;i<endsamples;i++) {
1428
operator_advance(&cptr[0],vibval1[i]);
1429
opfuncs[cptr[0].op_state](&cptr[0]);
1430
operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1433
operator_advance(&cptr[9],vibval2[i]);
1434
opfuncs[cptr[9].op_state](&cptr[9]);
1435
operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
1437
Bit32s chanval = cptr[9].cval;
1443
#if defined(OPLTYPE_IS_OPL3)
1444
if (adlibreg[0x105]&1) {
1445
// convert to 16bit samples (stereo)
1446
for (i=0;i<endsamples;i++) {
1447
clipit16(outbufl[i],sndptr++);
1448
clipit16(outbufr[i],sndptr++);
1451
// convert to 16bit samples (mono)
1452
for (i=0;i<endsamples;i++) {
1453
clipit16(outbufl[i],sndptr++);
1454
clipit16(outbufl[i],sndptr++);
1458
// convert to 16bit samples
1459
for (i=0;i<endsamples;i++)
1460
clipit16(outbufl[i],sndptr++);