1 /*
2 * Copyright (C) 2002-2011 The DOSBox Team
3 * OPL2/OPL3 emulation library
4 *
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.
9 *
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.
14 *
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
18 */
19
20
21 /*
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"
25 */
26
27 #include "doomtype.h"
28 #include "../opl.h"
29 #include "../muslib.h"
30 #include <math.h>
31 #include "m_random.h"
32
33 static FRandom pr_opl;
34
35 typedef uintptr_t Bitu;
36 typedef intptr_t Bits;
37 typedef DWORD Bit32u;
38 typedef SDWORD Bit32s;
39 typedef WORD Bit16u;
40 typedef SWORD Bit16s;
41 typedef BYTE Bit8u;
42 typedef SBYTE Bit8s;
43
44 #define OPLTYPE_IS_OPL3
45 #undef PI
46
47 #include "opl.h"
48
49 static Bit16s wavtable[WAVEPREC*3]; // wave form table
50
51 // key scale levels
52 static Bit8u kslev[8][16];
53
54 // key scale level lookup table
55 static const fltype kslmul[4] = {
56 0.0, 0.5, 0.25, 1.0 // -> 0, 3, 1.5, 6 dB/oct
57 };
58
59 // frequency multiplicator lookup table
60 static const fltype frqmul_tab[16] = {
61 0.5,1,2,3,4,5,6,7,8,9,10,10,12,12,15,15
62 };
63
64 // map a channel number to the register offset of the modulator (=register base)
65 static const Bit8u modulatorbase[9] = {
66 0,1,2,
67 8,9,10,
68 16,17,18
69 };
70
71 // map a register base to a modulator operator number or operator number
72 #if defined(OPLTYPE_IS_OPL3)
73 static const Bit8u regbase2modop[44] = {
74 0,1,2,0,1,2,0,0,3,4,5,3,4,5,0,0,6,7,8,6,7,8, // first set
75 18,19,20,18,19,20,0,0,21,22,23,21,22,23,0,0,24,25,26,24,25,26 // second set
76 };
77 static const Bit8u regbase2op[44] = {
78 0,1,2,9,10,11,0,0,3,4,5,12,13,14,0,0,6,7,8,15,16,17, // first set
79 18,19,20,27,28,29,0,0,21,22,23,30,31,32,0,0,24,25,26,33,34,35 // second set
80 };
81 #else
82 static const Bit8u regbase2modop[22] = {
83 0,1,2,0,1,2,0,0,3,4,5,3,4,5,0,0,6,7,8,6,7,8
84 };
85 static const Bit8u regbase2op[22] = {
86 0,1,2,9,10,11,0,0,3,4,5,12,13,14,0,0,6,7,8,15,16,17
87 };
88 #endif
89
90
91 // start of the waveform
92 static const Bit32u waveform[8] = {
93 WAVEPREC,
94 WAVEPREC>>1,
95 WAVEPREC,
96 (WAVEPREC*3)>>2,
97 0,
98 0,
99 (WAVEPREC*5)>>2,
100 WAVEPREC<<1
101 };
102
103 // length of the waveform as mask
104 static const Bit32u wavemask[8] = {
105 WAVEPREC-1,
106 WAVEPREC-1,
107 (WAVEPREC>>1)-1,
108 (WAVEPREC>>1)-1,
109 WAVEPREC-1,
110 ((WAVEPREC*3)>>2)-1,
111 WAVEPREC>>1,
112 WAVEPREC-1
113 };
114
115 // where the first entry resides
116 static const Bit32u wavestart[8] = {
117 0,
118 WAVEPREC>>1,
119 0,
120 WAVEPREC>>2,
121 0,
122 0,
123 0,
124 WAVEPREC>>3
125 };
126
127 // envelope generator function constants
128 static const fltype attackconst[4] = {
129 (fltype)(1/2.82624),
130 (fltype)(1/2.25280),
131 (fltype)(1/1.88416),
132 (fltype)(1/1.59744)
133 };
134 static const fltype decrelconst[4] = {
135 (fltype)(1/39.28064),
136 (fltype)(1/31.41608),
137 (fltype)(1/26.17344),
138 (fltype)(1/22.44608)
139 };
140
141
operator_advance(op_type * op_pt,Bit32s vib)142 void operator_advance(op_type* op_pt, Bit32s vib) {
143 op_pt->wfpos = op_pt->tcount; // waveform position
144
145 // advance waveform time
146 op_pt->tcount += op_pt->tinc;
147 op_pt->tcount += (Bit32s)(op_pt->tinc)*vib/FIXEDPT;
148
149 op_pt->generator_pos += generator_add;
150 }
151
operator_advance_drums(op_type * op_pt1,Bit32s vib1,op_type * op_pt2,Bit32s vib2,op_type * op_pt3,Bit32s vib3)152 void operator_advance_drums(op_type* op_pt1, Bit32s vib1, op_type* op_pt2, Bit32s vib2, op_type* op_pt3, Bit32s vib3) {
153 Bit32u c1 = op_pt1->tcount/FIXEDPT;
154 Bit32u c3 = op_pt3->tcount/FIXEDPT;
155 Bit32u phasebit = (((c1 & 0x88) ^ ((c1<<5) & 0x80)) | ((c3 ^ (c3<<2)) & 0x20)) ? 0x02 : 0x00;
156
157 Bit32u noisebit = pr_opl.GenRand32() & 1;
158
159 Bit32u snare_phase_bit = (Bit32u)(((Bitu)((op_pt1->tcount/FIXEDPT) / 0x100))&1);
160
161 //Hihat
162 Bit32u inttm = (phasebit<<8) | (0x34<<(phasebit ^ (noisebit<<1)));
163 op_pt1->wfpos = inttm*FIXEDPT; // waveform position
164 // advance waveform time
165 op_pt1->tcount += op_pt1->tinc;
166 op_pt1->tcount += (Bit32s)(op_pt1->tinc)*vib1/FIXEDPT;
167 op_pt1->generator_pos += generator_add;
168
169 //Snare
170 inttm = ((1+snare_phase_bit) ^ noisebit)<<8;
171 op_pt2->wfpos = inttm*FIXEDPT; // waveform position
172 // advance waveform time
173 op_pt2->tcount += op_pt2->tinc;
174 op_pt2->tcount += (Bit32s)(op_pt2->tinc)*vib2/FIXEDPT;
175 op_pt2->generator_pos += generator_add;
176
177 //Cymbal
178 inttm = (1+phasebit)<<8;
179 op_pt3->wfpos = inttm*FIXEDPT; // waveform position
180 // advance waveform time
181 op_pt3->tcount += op_pt3->tinc;
182 op_pt3->tcount += (Bit32s)(op_pt3->tinc)*vib3/FIXEDPT;
183 op_pt3->generator_pos += generator_add;
184 }
185
186
187 // output level is sustained, mode changes only when operator is turned off (->release)
188 // or when the keep-sustained bit is turned off (->sustain_nokeep)
operator_output(op_type * op_pt,Bit32s modulator,Bit32s trem)189 void operator_output(op_type* op_pt, Bit32s modulator, Bit32s trem) {
190 if (op_pt->op_state != OF_TYPE_OFF) {
191 op_pt->lastcval = op_pt->cval;
192 Bit32u i = (Bit32u)((op_pt->wfpos+modulator)/FIXEDPT);
193
194 // wform: -16384 to 16383 (0x4000)
195 // trem : 32768 to 65535 (0x10000)
196 // step_amp: 0.0 to 1.0
197 // vol : 1/2^14 to 1/2^29 (/0x4000; /1../0x8000)
198
199 op_pt->cval = (Bit32s)(op_pt->step_amp*op_pt->vol*op_pt->cur_wform[i&op_pt->cur_wmask]*trem/16.0);
200 }
201 }
202
203
204 // no action, operator is off
operator_off(op_type *)205 void operator_off(op_type* /*op_pt*/) {
206 }
207
208 // output level is sustained, mode changes only when operator is turned off (->release)
209 // or when the keep-sustained bit is turned off (->sustain_nokeep)
operator_sustain(op_type * op_pt)210 void operator_sustain(op_type* op_pt) {
211 Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
212 for (Bit32u ct=0; ct<num_steps_add; ct++) {
213 op_pt->cur_env_step++;
214 }
215 op_pt->generator_pos -= num_steps_add*FIXEDPT;
216 }
217
218 // operator in release mode, if output level reaches zero the operator is turned off
operator_release(op_type * op_pt)219 void operator_release(op_type* op_pt) {
220 // ??? boundary?
221 if (op_pt->amp > 0.00000001) {
222 // release phase
223 op_pt->amp *= op_pt->releasemul;
224 }
225
226 Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
227 for (Bit32u ct=0; ct<num_steps_add; ct++) {
228 op_pt->cur_env_step++; // sample counter
229 if ((op_pt->cur_env_step & op_pt->env_step_r)==0) {
230 if (op_pt->amp <= 0.00000001) {
231 // release phase finished, turn off this operator
232 op_pt->amp = 0.0;
233 if (op_pt->op_state == OF_TYPE_REL) {
234 op_pt->op_state = OF_TYPE_OFF;
235 }
236 }
237 op_pt->step_amp = op_pt->amp;
238 }
239 }
240 op_pt->generator_pos -= num_steps_add*FIXEDPT;
241 }
242
243 // operator in decay mode, if sustain level is reached the output level is either
244 // kept (sustain level keep enabled) or the operator is switched into release mode
operator_decay(op_type * op_pt)245 void operator_decay(op_type* op_pt) {
246 if (op_pt->amp > op_pt->sustain_level) {
247 // decay phase
248 op_pt->amp *= op_pt->decaymul;
249 }
250
251 Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
252 for (Bit32u ct=0; ct<num_steps_add; ct++) {
253 op_pt->cur_env_step++;
254 if ((op_pt->cur_env_step & op_pt->env_step_d)==0) {
255 if (op_pt->amp <= op_pt->sustain_level) {
256 // decay phase finished, sustain level reached
257 if (op_pt->sus_keep) {
258 // keep sustain level (until turned off)
259 op_pt->op_state = OF_TYPE_SUS;
260 op_pt->amp = op_pt->sustain_level;
261 } else {
262 // next: release phase
263 op_pt->op_state = OF_TYPE_SUS_NOKEEP;
264 }
265 }
266 op_pt->step_amp = op_pt->amp;
267 }
268 }
269 op_pt->generator_pos -= num_steps_add*FIXEDPT;
270 }
271
272 // operator in attack mode, if full output level is reached,
273 // the operator is switched into decay mode
operator_attack(op_type * op_pt)274 void operator_attack(op_type* op_pt) {
275 op_pt->amp = ((op_pt->a3*op_pt->amp + op_pt->a2)*op_pt->amp + op_pt->a1)*op_pt->amp + op_pt->a0;
276
277 Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
278 for (Bit32u ct=0; ct<num_steps_add; ct++) {
279 op_pt->cur_env_step++; // next sample
280 if ((op_pt->cur_env_step & op_pt->env_step_a)==0) { // check if next step already reached
281 if (op_pt->amp > 1.0) {
282 // attack phase finished, next: decay
283 op_pt->op_state = OF_TYPE_DEC;
284 op_pt->amp = 1.0;
285 op_pt->step_amp = 1.0;
286 }
287 op_pt->step_skip_pos_a <<= 1;
288 if (op_pt->step_skip_pos_a==0) op_pt->step_skip_pos_a = 1;
289 if (op_pt->step_skip_pos_a & op_pt->env_step_skip_a) { // check if required to skip next step
290 op_pt->step_amp = op_pt->amp;
291 }
292 }
293 }
294 op_pt->generator_pos -= num_steps_add*FIXEDPT;
295 }
296
297
298 typedef void (*optype_fptr)(op_type*);
299
300 optype_fptr opfuncs[6] = {
301 operator_attack,
302 operator_decay,
303 operator_release,
304 operator_sustain, // sustain phase (keeping level)
305 operator_release, // sustain_nokeep phase (release-style)
306 operator_off
307 };
308
change_attackrate(Bitu regbase,op_type * op_pt)309 void DBOPL::change_attackrate(Bitu regbase, op_type* op_pt) {
310 Bits attackrate = adlibreg[ARC_ATTR_DECR+regbase]>>4;
311 if (attackrate) {
312 fltype f = (fltype)(pow(FL2,(fltype)attackrate+(op_pt->toff>>2)-1)*attackconst[op_pt->toff&3]*recipsamp);
313 // attack rate coefficients
314 op_pt->a0 = (fltype)(0.0377*f);
315 op_pt->a1 = (fltype)(10.73*f+1);
316 op_pt->a2 = (fltype)(-17.57*f);
317 op_pt->a3 = (fltype)(7.42*f);
318
319 Bits step_skip = attackrate*4 + op_pt->toff;
320 Bits steps = step_skip >> 2;
321 op_pt->env_step_a = (1<<(steps<=12?12-steps:0))-1;
322
323 Bits step_num = (step_skip<=48)?(4-(step_skip&3)):0;
324 static Bit8u step_skip_mask[5] = {0xff, 0xfe, 0xee, 0xba, 0xaa};
325 op_pt->env_step_skip_a = step_skip_mask[step_num];
326
327 #if defined(OPLTYPE_IS_OPL3)
328 if (step_skip>=60) {
329 #else
330 if (step_skip>=62) {
331 #endif
332 op_pt->a0 = (fltype)(2.0); // something that triggers an immediate transition to amp:=1.0
333 op_pt->a1 = (fltype)(0.0);
334 op_pt->a2 = (fltype)(0.0);
335 op_pt->a3 = (fltype)(0.0);
336 }
337 } else {
338 // attack disabled
339 op_pt->a0 = 0.0;
340 op_pt->a1 = 1.0;
341 op_pt->a2 = 0.0;
342 op_pt->a3 = 0.0;
343 op_pt->env_step_a = 0;
344 op_pt->env_step_skip_a = 0;
345 }
346 }
347
348 void DBOPL::change_decayrate(Bitu regbase, op_type* op_pt) {
349 Bits decayrate = adlibreg[ARC_ATTR_DECR+regbase]&15;
350 // decaymul should be 1.0 when decayrate==0
351 if (decayrate) {
352 fltype f = (fltype)(-7.4493*decrelconst[op_pt->toff&3]*recipsamp);
353 op_pt->decaymul = (fltype)(pow(FL2,f*pow(FL2,(fltype)(decayrate+(op_pt->toff>>2)))));
354 Bits steps = (decayrate*4 + op_pt->toff) >> 2;
355 op_pt->env_step_d = (1<<(steps<=12?12-steps:0))-1;
356 } else {
357 op_pt->decaymul = 1.0;
358 op_pt->env_step_d = 0;
359 }
360 }
361
362 void DBOPL::change_releaserate(Bitu regbase, op_type* op_pt) {
363 Bits releaserate = adlibreg[ARC_SUSL_RELR+regbase]&15;
364 // releasemul should be 1.0 when releaserate==0
365 if (releaserate) {
366 fltype f = (fltype)(-7.4493*decrelconst[op_pt->toff&3]*recipsamp);
367 op_pt->releasemul = (fltype)(pow(FL2,f*pow(FL2,(fltype)(releaserate+(op_pt->toff>>2)))));
368 Bits steps = (releaserate*4 + op_pt->toff) >> 2;
369 op_pt->env_step_r = (1<<(steps<=12?12-steps:0))-1;
370 } else {
371 op_pt->releasemul = 1.0;
372 op_pt->env_step_r = 0;
373 }
374 }
375
376 void DBOPL::change_sustainlevel(Bitu regbase, op_type* op_pt) {
377 Bits sustainlevel = adlibreg[ARC_SUSL_RELR+regbase]>>4;
378 // sustainlevel should be 0.0 when sustainlevel==15 (max)
379 if (sustainlevel<15) {
380 op_pt->sustain_level = (fltype)(pow(FL2,(fltype)sustainlevel * (-FL05)));
381 } else {
382 op_pt->sustain_level = 0.0;
383 }
384 }
385
386 void DBOPL::change_waveform(Bitu regbase, op_type* op_pt) {
387 #if defined(OPLTYPE_IS_OPL3)
388 if (regbase>=ARC_SECONDSET) regbase -= (ARC_SECONDSET-22); // second set starts at 22
389 #endif
390 // waveform selection
391 op_pt->cur_wmask = wavemask[wave_sel[regbase]];
392 op_pt->cur_wform = &wavtable[waveform[wave_sel[regbase]]];
393 // (might need to be adapted to waveform type here...)
394 }
395
396 void DBOPL::change_keepsustain(Bitu regbase, op_type* op_pt) {
397 op_pt->sus_keep = (adlibreg[ARC_TVS_KSR_MUL+regbase]&0x20)>0;
398 if (op_pt->op_state==OF_TYPE_SUS) {
399 if (!op_pt->sus_keep) op_pt->op_state = OF_TYPE_SUS_NOKEEP;
400 } else if (op_pt->op_state==OF_TYPE_SUS_NOKEEP) {
401 if (op_pt->sus_keep) op_pt->op_state = OF_TYPE_SUS;
402 }
403 }
404
405 // enable/disable vibrato/tremolo LFO effects
406 void DBOPL::change_vibrato(Bitu regbase, op_type* op_pt) {
407 op_pt->vibrato = (adlibreg[ARC_TVS_KSR_MUL+regbase]&0x40)!=0;
408 op_pt->tremolo = (adlibreg[ARC_TVS_KSR_MUL+regbase]&0x80)!=0;
409 }
410
411 // change amount of self-feedback
412 void DBOPL::change_feedback(Bitu chanbase, op_type* op_pt) {
413 Bits feedback = adlibreg[ARC_FEEDBACK+chanbase]&14;
414 if (feedback) op_pt->mfbi = (Bit32s)(pow(FL2,(fltype)((feedback>>1)+8)));
415 else op_pt->mfbi = 0;
416 }
417
418 void DBOPL::change_frequency(Bitu chanbase, Bitu regbase, op_type* op_pt) {
419 // frequency
420 Bit32u frn = ((((Bit32u)adlibreg[ARC_KON_BNUM+chanbase])&3)<<8) + (Bit32u)adlibreg[ARC_FREQ_NUM+chanbase];
421 // block number/octave
422 Bit32u oct = ((((Bit32u)adlibreg[ARC_KON_BNUM+chanbase])>>2)&7);
423 op_pt->freq_high = (Bit32s)((frn>>7)&7);
424
425 // keysplit
426 Bit32u note_sel = (adlibreg[8]>>6)&1;
427 op_pt->toff = ((frn>>9)&(note_sel^1)) | ((frn>>8)¬e_sel);
428 op_pt->toff += (oct<<1);
429
430 // envelope scaling (KSR)
431 if (!(adlibreg[ARC_TVS_KSR_MUL+regbase]&0x10)) op_pt->toff >>= 2;
432
433 // 20+a0+b0:
434 op_pt->tinc = (Bit32u)((((fltype)(frn<<oct))*frqmul[adlibreg[ARC_TVS_KSR_MUL+regbase]&15]));
435 // 40+a0+b0:
436 fltype vol_in = (fltype)((fltype)(adlibreg[ARC_KSL_OUTLEV+regbase]&63) +
437 kslmul[adlibreg[ARC_KSL_OUTLEV+regbase]>>6]*kslev[oct][frn>>6]);
438 op_pt->vol = (fltype)(pow(FL2,(fltype)(vol_in * -0.125 - 14)));
439
440 // operator frequency changed, care about features that depend on it
441 change_attackrate(regbase,op_pt);
442 change_decayrate(regbase,op_pt);
443 change_releaserate(regbase,op_pt);
444 }
445
446 void DBOPL::enable_operator(Bitu regbase, op_type* op_pt, Bit32u act_type) {
447 // check if this is really an off-on transition
448 if (op_pt->act_state == OP_ACT_OFF) {
449 Bits wselbase = regbase;
450 if (wselbase>=ARC_SECONDSET) wselbase -= (ARC_SECONDSET-22); // second set starts at 22
451
452 op_pt->tcount = wavestart[wave_sel[wselbase]]*FIXEDPT;
453
454 // start with attack mode
455 op_pt->op_state = OF_TYPE_ATT;
456 op_pt->act_state |= act_type;
457 }
458 }
459
460 void DBOPL::disable_operator(op_type* op_pt, Bit32u act_type) {
461 // check if this is really an on-off transition
462 if (op_pt->act_state != OP_ACT_OFF) {
463 op_pt->act_state &= (~act_type);
464 if (op_pt->act_state == OP_ACT_OFF) {
465 if (op_pt->op_state != OF_TYPE_OFF) op_pt->op_state = OF_TYPE_REL;
466 }
467 }
468 }
469
470 void DBOPL::Reset() {
471 Bit32u samplerate = (Bit32u)OPL_SAMPLE_RATE;
472 Bits i, j, oct;
473
474 int_samplerate = samplerate;
475
476 generator_add = (Bit32u)(INTFREQU*FIXEDPT/int_samplerate);
477
478
479 memset((void *)adlibreg,0,sizeof(adlibreg));
480 memset((void *)op,0,sizeof(op_type)*MAXOPERATORS);
481 memset((void *)wave_sel,0,sizeof(wave_sel));
482
483 for (i=0;i<MAXOPERATORS;i++) {
484 op[i].op_state = OF_TYPE_OFF;
485 op[i].act_state = OP_ACT_OFF;
486 op[i].amp = 0.0;
487 op[i].step_amp = 0.0;
488 op[i].vol = 0.0;
489 op[i].tcount = 0;
490 op[i].tinc = 0;
491 op[i].toff = 0;
492 op[i].cur_wmask = wavemask[0];
493 op[i].cur_wform = &wavtable[waveform[0]];
494 op[i].freq_high = 0;
495
496 op[i].generator_pos = 0;
497 op[i].cur_env_step = 0;
498 op[i].env_step_a = 0;
499 op[i].env_step_d = 0;
500 op[i].env_step_r = 0;
501 op[i].step_skip_pos_a = 0;
502 op[i].env_step_skip_a = 0;
503
504 #if defined(OPLTYPE_IS_OPL3)
505 op[i].is_4op = false;
506 op[i].is_4op_attached = false;
507 op[i].right_pan = op[i].left_pan = FullPan ? (float)CENTER_PANNING_POWER : 1;
508 #endif
509 }
510
511 recipsamp = 1.0 / (fltype)int_samplerate;
512 for (i=15;i>=0;i--) {
513 frqmul[i] = (fltype)(frqmul_tab[i]*INTFREQU/(fltype)WAVEPREC*(fltype)FIXEDPT*recipsamp);
514 }
515
516 status = 0;
517 opl_index = 0;
518
519
520 // create vibrato table
521 vib_table[0] = 8;
522 vib_table[1] = 4;
523 vib_table[2] = 0;
524 vib_table[3] = -4;
525 for (i=4; i<VIBTAB_SIZE; i++) vib_table[i] = vib_table[i-4]*-1;
526
527 // vibrato at ~6.1 ?? (opl3 docs say 6.1, opl4 docs say 6.0, y8950 docs say 6.4)
528 vibtab_add = static_cast<Bit32u>(VIBTAB_SIZE*FIXEDPT_LFO/8192*INTFREQU/int_samplerate);
529 vibtab_pos = 0;
530
531 for (i=0; i<BLOCKBUF_SIZE; i++) vibval_const[i] = 0;
532
533
534 // create tremolo table
535 Bit32s trem_table_int[TREMTAB_SIZE];
536 for (i=0; i<14; i++) trem_table_int[i] = Bit32s(i-13); // upwards (13 to 26 -> -0.5/6 to 0)
537 for (i=14; i<41; i++) trem_table_int[i] = Bit32s(-i+14); // downwards (26 to 0 -> 0 to -1/6)
538 for (i=41; i<53; i++) trem_table_int[i] = Bit32s(i-40-26); // upwards (1 to 12 -> -1/6 to -0.5/6)
539
540 for (i=0; i<TREMTAB_SIZE; i++) {
541 // 0.0 .. -26/26*4.8/6 == [0.0 .. -0.8], 4/53 steps == [1 .. 0.57]
542 fltype trem_val1=(fltype)(((fltype)trem_table_int[i])*4.8/26.0/6.0); // 4.8db
543 fltype trem_val2=(fltype)((fltype)((Bit32s)(trem_table_int[i]/4))*1.2/6.0/6.0); // 1.2db (larger stepping)
544
545 trem_table[i] = (Bit32s)(pow(FL2,trem_val1)*FIXEDPT);
546 trem_table[TREMTAB_SIZE+i] = (Bit32s)(pow(FL2,trem_val2)*FIXEDPT);
547 }
548
549 // tremolo at 3.7hz
550 tremtab_add = (Bit32u)((fltype)TREMTAB_SIZE * TREM_FREQ * FIXEDPT_LFO / (fltype)int_samplerate);
551 tremtab_pos = 0;
552
553 for (i=0; i<BLOCKBUF_SIZE; i++) tremval_const[i] = FIXEDPT;
554
555
556 static Bitu initfirstime = 0;
557 if (!initfirstime) {
558 initfirstime = 1;
559
560 // create waveform tables
561 for (i=0;i<(WAVEPREC>>1);i++) {
562 wavtable[(i<<1) +WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<1) )*PI*2/WAVEPREC));
563 wavtable[(i<<1)+1+WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<1)+1)*PI*2/WAVEPREC));
564 wavtable[i] = wavtable[(i<<1) +WAVEPREC];
565 // alternative: (zero-less)
566 /* wavtable[(i<<1) +WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<2)+1)*PI/WAVEPREC));
567 wavtable[(i<<1)+1+WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<2)+3)*PI/WAVEPREC));
568 wavtable[i] = wavtable[(i<<1)-1+WAVEPREC]; */
569 }
570 for (i=0;i<(WAVEPREC>>3);i++) {
571 wavtable[i+(WAVEPREC<<1)] = wavtable[i+(WAVEPREC>>3)]-16384;
572 wavtable[i+((WAVEPREC*17)>>3)] = wavtable[i+(WAVEPREC>>2)]+16384;
573 }
574
575 // key scale level table verified ([table in book]*8/3)
576 kslev[7][0] = 0; kslev[7][1] = 24; kslev[7][2] = 32; kslev[7][3] = 37;
577 kslev[7][4] = 40; kslev[7][5] = 43; kslev[7][6] = 45; kslev[7][7] = 47;
578 kslev[7][8] = 48;
579 for (i=9;i<16;i++) kslev[7][i] = (Bit8u)(i+41);
580 for (j=6;j>=0;j--) {
581 for (i=0;i<16;i++) {
582 oct = (Bits)kslev[j+1][i]-8;
583 if (oct < 0) oct = 0;
584 kslev[j][i] = (Bit8u)oct;
585 }
586 }
587 }
588
589 }
590
591
592
593 void DBOPL::WriteReg(int idx, int val) {
594 Bit32u second_set = (Bit32u)idx&0x100;
595 adlibreg[idx] = val;
596
597 switch (idx&0xf0) {
598 case ARC_CONTROL:
599 // here we check for the second set registers, too:
600 switch (idx) {
601 case 0x02: // timer1 counter
602 case 0x03: // timer2 counter
603 break;
604 case 0x04:
605 // IRQ reset, timer mask/start
606 if (val&0x80) {
607 // clear IRQ bits in status register
608 status &= ~0x60;
609 } else {
610 status = 0;
611 }
612 break;
613 #if defined(OPLTYPE_IS_OPL3)
614 case 0x04|ARC_SECONDSET:
615 // 4op enable/disable switches for each possible channel
616 op[0].is_4op = (val&1)>0;
617 op[3].is_4op_attached = op[0].is_4op;
618 op[1].is_4op = (val&2)>0;
619 op[4].is_4op_attached = op[1].is_4op;
620 op[2].is_4op = (val&4)>0;
621 op[5].is_4op_attached = op[2].is_4op;
622 op[18].is_4op = (val&8)>0;
623 op[21].is_4op_attached = op[18].is_4op;
624 op[19].is_4op = (val&16)>0;
625 op[22].is_4op_attached = op[19].is_4op;
626 op[20].is_4op = (val&32)>0;
627 op[23].is_4op_attached = op[20].is_4op;
628 break;
629 case 0x05|ARC_SECONDSET:
630 break;
631 #endif
632 case 0x08:
633 // CSW, note select
634 break;
635 default:
636 break;
637 }
638 break;
639 case ARC_TVS_KSR_MUL:
640 case ARC_TVS_KSR_MUL+0x10: {
641 // tremolo/vibrato/sustain keeping enabled; key scale rate; frequency multiplication
642 int num = (int)idx&7;
643 Bitu base = (idx-ARC_TVS_KSR_MUL)&0xff;
644 if ((num<6) && (base<22)) {
645 Bitu modop = regbase2modop[second_set?(base+22):base];
646 Bitu regbase = base+second_set;
647 Bitu chanbase = second_set?(modop-18+ARC_SECONDSET):modop;
648
649 // change tremolo/vibrato and sustain keeping of this operator
650 op_type* op_ptr = &op[modop+((num<3) ? 0 : 9)];
651 change_keepsustain(regbase,op_ptr);
652 change_vibrato(regbase,op_ptr);
653
654 // change frequency calculations of this operator as
655 // key scale rate and frequency multiplicator can be changed
656 #if defined(OPLTYPE_IS_OPL3)
657 if ((adlibreg[0x105]&1) && (op[modop].is_4op_attached)) {
658 // operator uses frequency of channel
659 change_frequency(chanbase-3,regbase,op_ptr);
660 } else {
661 change_frequency(chanbase,regbase,op_ptr);
662 }
663 #else
664 change_frequency(chanbase,base,op_ptr);
665 #endif
666 }
667 }
668 break;
669 case ARC_KSL_OUTLEV:
670 case ARC_KSL_OUTLEV+0x10: {
671 // key scale level; output rate
672 int num = (int)idx&7;
673 Bitu base = (idx-ARC_KSL_OUTLEV)&0xff;
674 if ((num<6) && (base<22)) {
675 Bitu modop = regbase2modop[second_set?(base+22):base];
676 Bitu chanbase = second_set?(modop-18+ARC_SECONDSET):modop;
677
678 // change frequency calculations of this operator as
679 // key scale level and output rate can be changed
680 op_type* op_ptr = &op[modop+((num<3) ? 0 : 9)];
681 #if defined(OPLTYPE_IS_OPL3)
682 Bitu regbase = base+second_set;
683 if ((adlibreg[0x105]&1) && (op[modop].is_4op_attached)) {
684 // operator uses frequency of channel
685 change_frequency(chanbase-3,regbase,op_ptr);
686 } else {
687 change_frequency(chanbase,regbase,op_ptr);
688 }
689 #else
690 change_frequency(chanbase,base,op_ptr);
691 #endif
692 }
693 }
694 break;
695 case ARC_ATTR_DECR:
696 case ARC_ATTR_DECR+0x10: {
697 // attack/decay rates
698 int num = (int)idx&7;
699 Bitu base = (idx-ARC_ATTR_DECR)&0xff;
700 if ((num<6) && (base<22)) {
701 Bitu regbase = base+second_set;
702
703 // change attack rate and decay rate of this operator
704 op_type* op_ptr = &op[regbase2op[second_set?(base+22):base]];
705 change_attackrate(regbase,op_ptr);
706 change_decayrate(regbase,op_ptr);
707 }
708 }
709 break;
710 case ARC_SUSL_RELR:
711 case ARC_SUSL_RELR+0x10: {
712 // sustain level; release rate
713 int num = (int)idx&7;
714 Bitu base = (idx-ARC_SUSL_RELR)&0xff;
715 if ((num<6) && (base<22)) {
716 Bitu regbase = base+second_set;
717
718 // change sustain level and release rate of this operator
719 op_type* op_ptr = &op[regbase2op[second_set?(base+22):base]];
720 change_releaserate(regbase,op_ptr);
721 change_sustainlevel(regbase,op_ptr);
722 }
723 }
724 break;
725 case ARC_FREQ_NUM: {
726 // 0xa0-0xa8 low8 frequency
727 Bitu base = (idx-ARC_FREQ_NUM)&0xff;
728 if (base<9) {
729 Bits opbase = second_set?(base+18):base;
730 #if defined(OPLTYPE_IS_OPL3)
731 if ((adlibreg[0x105]&1) && op[opbase].is_4op_attached) break;
732 #endif
733 // regbase of modulator:
734 Bits modbase = modulatorbase[base]+second_set;
735
736 Bitu chanbase = base+second_set;
737
738 change_frequency(chanbase,modbase,&op[opbase]);
739 change_frequency(chanbase,modbase+3,&op[opbase+9]);
740 #if defined(OPLTYPE_IS_OPL3)
741 // for 4op channels all four operators are modified to the frequency of the channel
742 if ((adlibreg[0x105]&1) && op[second_set?(base+18):base].is_4op) {
743 change_frequency(chanbase,modbase+8,&op[opbase+3]);
744 change_frequency(chanbase,modbase+3+8,&op[opbase+3+9]);
745 }
746 #endif
747 }
748 }
749 break;
750 case ARC_KON_BNUM: {
751 if (idx == ARC_PERC_MODE) {
752 #if defined(OPLTYPE_IS_OPL3)
753 if (second_set) return;
754 #endif
755
756 if ((val&0x30) == 0x30) { // BassDrum active
757 enable_operator(16,&op[6],OP_ACT_PERC);
758 change_frequency(6,16,&op[6]);
759 enable_operator(16+3,&op[6+9],OP_ACT_PERC);
760 change_frequency(6,16+3,&op[6+9]);
761 } else {
762 disable_operator(&op[6],OP_ACT_PERC);
763 disable_operator(&op[6+9],OP_ACT_PERC);
764 }
765 if ((val&0x28) == 0x28) { // Snare active
766 enable_operator(17+3,&op[16],OP_ACT_PERC);
767 change_frequency(7,17+3,&op[16]);
768 } else {
769 disable_operator(&op[16],OP_ACT_PERC);
770 }
771 if ((val&0x24) == 0x24) { // TomTom active
772 enable_operator(18,&op[8],OP_ACT_PERC);
773 change_frequency(8,18,&op[8]);
774 } else {
775 disable_operator(&op[8],OP_ACT_PERC);
776 }
777 if ((val&0x22) == 0x22) { // Cymbal active
778 enable_operator(18+3,&op[8+9],OP_ACT_PERC);
779 change_frequency(8,18+3,&op[8+9]);
780 } else {
781 disable_operator(&op[8+9],OP_ACT_PERC);
782 }
783 if ((val&0x21) == 0x21) { // Hihat active
784 enable_operator(17,&op[7],OP_ACT_PERC);
785 change_frequency(7,17,&op[7]);
786 } else {
787 disable_operator(&op[7],OP_ACT_PERC);
788 }
789
790 break;
791 }
792 // regular 0xb0-0xb8
793 Bitu base = (idx-ARC_KON_BNUM)&0xff;
794 if (base<9) {
795 Bits opbase = second_set?(base+18):base;
796 #if defined(OPLTYPE_IS_OPL3)
797 if ((adlibreg[0x105]&1) && op[opbase].is_4op_attached) break;
798 #endif
799 // regbase of modulator:
800 Bits modbase = modulatorbase[base]+second_set;
801
802 if (val&32) {
803 // operator switched on
804 enable_operator(modbase,&op[opbase],OP_ACT_NORMAL); // modulator (if 2op)
805 enable_operator(modbase+3,&op[opbase+9],OP_ACT_NORMAL); // carrier (if 2op)
806 #if defined(OPLTYPE_IS_OPL3)
807 // for 4op channels all four operators are switched on
808 if ((adlibreg[0x105]&1) && op[opbase].is_4op) {
809 // turn on chan+3 operators as well
810 enable_operator(modbase+8,&op[opbase+3],OP_ACT_NORMAL);
811 enable_operator(modbase+3+8,&op[opbase+3+9],OP_ACT_NORMAL);
812 }
813 #endif
814 } else {
815 // operator switched off
816 disable_operator(&op[opbase],OP_ACT_NORMAL);
817 disable_operator(&op[opbase+9],OP_ACT_NORMAL);
818 #if defined(OPLTYPE_IS_OPL3)
819 // for 4op channels all four operators are switched off
820 if ((adlibreg[0x105]&1) && op[opbase].is_4op) {
821 // turn off chan+3 operators as well
822 disable_operator(&op[opbase+3],OP_ACT_NORMAL);
823 disable_operator(&op[opbase+3+9],OP_ACT_NORMAL);
824 }
825 #endif
826 }
827
828 Bitu chanbase = base+second_set;
829
830 // change frequency calculations of modulator and carrier (2op) as
831 // the frequency of the channel has changed
832 change_frequency(chanbase,modbase,&op[opbase]);
833 change_frequency(chanbase,modbase+3,&op[opbase+9]);
834 #if defined(OPLTYPE_IS_OPL3)
835 // for 4op channels all four operators are modified to the frequency of the channel
836 if ((adlibreg[0x105]&1) && op[second_set?(base+18):base].is_4op) {
837 // change frequency calculations of chan+3 operators as well
838 change_frequency(chanbase,modbase+8,&op[opbase+3]);
839 change_frequency(chanbase,modbase+3+8,&op[opbase+3+9]);
840 }
841 #endif
842 }
843 }
844 break;
845 case ARC_FEEDBACK: {
846 // 0xc0-0xc8 feedback/modulation type (AM/FM)
847 Bitu base = (idx-ARC_FEEDBACK)&0xff;
848 if (base<9) {
849 Bits opbase = second_set?(base+18):base;
850 Bitu chanbase = base+second_set;
851 change_feedback(chanbase,&op[opbase]);
852 #if defined(OPLTYPE_IS_OPL3)
853 // OPL3 panning
854 if (!FullPan)
855 {
856 op[opbase].left_pan = (float)((val&0x10)>>4);
857 op[opbase].right_pan = (float)((val&0x20)>>5);
858 }
859 #endif
860 }
861 }
862 break;
863 case ARC_WAVE_SEL:
864 case ARC_WAVE_SEL+0x10: {
865 int num = (int)idx&7;
866 Bitu base = (idx-ARC_WAVE_SEL)&0xff;
867 if ((num<6) && (base<22)) {
868 #if defined(OPLTYPE_IS_OPL3)
869 Bits wselbase = second_set?(base+22):base; // for easier mapping onto wave_sel[]
870 // change waveform
871 if (adlibreg[0x105]&1) wave_sel[wselbase] = val&7; // opl3 mode enabled, all waveforms accessible
872 else wave_sel[wselbase] = val&3;
873 op_type* op_ptr = &op[regbase2modop[wselbase]+((num<3) ? 0 : 9)];
874 change_waveform(wselbase,op_ptr);
875 #else
876 if (adlibreg[0x01]&0x20) {
877 // wave selection enabled, change waveform
878 wave_sel[base] = val&3;
879 op_type* op_ptr = &op[regbase2modop[base]+((num<3) ? 0 : 9)];
880 change_waveform(base,op_ptr);
881 }
882 #endif
883 }
884 }
885 break;
886 default:
887 break;
888 }
889 }
890
891 static void OPL_INLINE clipit16(float ival, float* outval) {
892 *outval += ival / 10240.f;
893 }
894
895
896
897 // be careful with this
898 // uses cptr and chanval, outputs into outbufl(/outbufr)
899 // for opl3 check if opl3-mode is enabled (which uses stereo panning)
900 #undef CHANVAL_OUT
901 #if defined(OPLTYPE_IS_OPL3)
902 #define CHANVAL_OUT \
903 if (adlibreg[0x105]&1) { \
904 outbufl[i] += chanval*cptr[0].left_pan; \
905 outbufr[i] += chanval*cptr[0].right_pan; \
906 } else { \
907 outbufl[i] += chanval; \
908 }
909 #else
910 #define CHANVAL_OUT \
911 outbufl[i] += chanval;
912 #endif
913
914 void DBOPL::Update(float* sndptr, int numsamples) {
915 Bits i, endsamples;
916 op_type* cptr;
917
918 float outbufl[BLOCKBUF_SIZE];
919 #if defined(OPLTYPE_IS_OPL3)
920 // second output buffer (right channel for opl3 stereo)
921 float outbufr[BLOCKBUF_SIZE];
922 #endif
923
924 // vibrato/tremolo lookup tables (global, to possibly be used by all operators)
925 Bit32s vib_lut[BLOCKBUF_SIZE];
926 Bit32s trem_lut[BLOCKBUF_SIZE];
927
928 Bits samples_to_process = numsamples;
929
930 for (Bits cursmp=0; cursmp<samples_to_process; cursmp+=endsamples) {
931 endsamples = samples_to_process-cursmp;
932 if (endsamples>BLOCKBUF_SIZE) endsamples = BLOCKBUF_SIZE;
933
934 memset((void*)&outbufl,0,endsamples*sizeof(Bit32s));
935 #if defined(OPLTYPE_IS_OPL3)
936 // clear second output buffer (opl3 stereo)
937 if (adlibreg[0x105]&1) memset((void*)&outbufr,0,endsamples*sizeof(Bit32s));
938 #endif
939
940 // calculate vibrato/tremolo lookup tables
941 Bit32s vib_tshift = ((adlibreg[ARC_PERC_MODE]&0x40)==0) ? 1 : 0; // 14cents/7cents switching
942 for (i=0;i<endsamples;i++) {
943 // cycle through vibrato table
944 vibtab_pos += vibtab_add;
945 if (vibtab_pos/FIXEDPT_LFO>=VIBTAB_SIZE) vibtab_pos-=VIBTAB_SIZE*FIXEDPT_LFO;
946 vib_lut[i] = vib_table[vibtab_pos/FIXEDPT_LFO]>>vib_tshift; // 14cents (14/100 of a semitone) or 7cents
947
948 // cycle through tremolo table
949 tremtab_pos += tremtab_add;
950 if (tremtab_pos/FIXEDPT_LFO>=TREMTAB_SIZE) tremtab_pos-=TREMTAB_SIZE*FIXEDPT_LFO;
951 if (adlibreg[ARC_PERC_MODE]&0x80) trem_lut[i] = trem_table[tremtab_pos/FIXEDPT_LFO];
952 else trem_lut[i] = trem_table[TREMTAB_SIZE+tremtab_pos/FIXEDPT_LFO];
953 }
954
955 if (adlibreg[ARC_PERC_MODE]&0x20) {
956 //BassDrum
957 cptr = &op[6];
958 if (adlibreg[ARC_FEEDBACK+6]&1) {
959 // additive synthesis
960 if (cptr[9].op_state != OF_TYPE_OFF) {
961 if (cptr[9].vibrato) {
962 vibval1 = vibval_var1;
963 for (i=0;i<endsamples;i++)
964 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
965 } else vibval1 = vibval_const;
966 if (cptr[9].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
967 else tremval1 = tremval_const;
968
969 // calculate channel output
970 for (i=0;i<endsamples;i++) {
971 operator_advance(&cptr[9],vibval1[i]);
972 opfuncs[cptr[9].op_state](&cptr[9]);
973 operator_output(&cptr[9],0,tremval1[i]);
974
975 Bit32s chanval = cptr[9].cval*2;
976 CHANVAL_OUT
977 }
978 }
979 } else {
980 // frequency modulation
981 if ((cptr[9].op_state != OF_TYPE_OFF) || (cptr[0].op_state != OF_TYPE_OFF)) {
982 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
983 vibval1 = vibval_var1;
984 for (i=0;i<endsamples;i++)
985 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
986 } else vibval1 = vibval_const;
987 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
988 vibval2 = vibval_var2;
989 for (i=0;i<endsamples;i++)
990 vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
991 } else vibval2 = vibval_const;
992 if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
993 else tremval1 = tremval_const;
994 if (cptr[9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
995 else tremval2 = tremval_const;
996
997 // calculate channel output
998 for (i=0;i<endsamples;i++) {
999 operator_advance(&cptr[0],vibval1[i]);
1000 opfuncs[cptr[0].op_state](&cptr[0]);
1001 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1002
1003 operator_advance(&cptr[9],vibval2[i]);
1004 opfuncs[cptr[9].op_state](&cptr[9]);
1005 operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
1006
1007 Bit32s chanval = cptr[9].cval*2;
1008 CHANVAL_OUT
1009 }
1010 }
1011 }
1012
1013 //TomTom (j=8)
1014 if (op[8].op_state != OF_TYPE_OFF) {
1015 cptr = &op[8];
1016 if (cptr[0].vibrato) {
1017 vibval3 = vibval_var1;
1018 for (i=0;i<endsamples;i++)
1019 vibval3[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1020 } else vibval3 = vibval_const;
1021
1022 if (cptr[0].tremolo) tremval3 = trem_lut; // tremolo enabled, use table
1023 else tremval3 = tremval_const;
1024
1025 // calculate channel output
1026 for (i=0;i<endsamples;i++) {
1027 operator_advance(&cptr[0],vibval3[i]);
1028 opfuncs[cptr[0].op_state](&cptr[0]); //TomTom
1029 operator_output(&cptr[0],0,tremval3[i]);
1030 Bit32s chanval = cptr[0].cval*2;
1031 CHANVAL_OUT
1032 }
1033 }
1034
1035 //Snare/Hihat (j=7), Cymbal (j=8)
1036 if ((op[7].op_state != OF_TYPE_OFF) || (op[16].op_state != OF_TYPE_OFF) ||
1037 (op[17].op_state != OF_TYPE_OFF)) {
1038 cptr = &op[7];
1039 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1040 vibval1 = vibval_var1;
1041 for (i=0;i<endsamples;i++)
1042 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1043 } else vibval1 = vibval_const;
1044 if ((cptr[9].vibrato) && (cptr[9].op_state == OF_TYPE_OFF)) {
1045 vibval2 = vibval_var2;
1046 for (i=0;i<endsamples;i++)
1047 vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1048 } else vibval2 = vibval_const;
1049
1050 if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1051 else tremval1 = tremval_const;
1052 if (cptr[9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1053 else tremval2 = tremval_const;
1054
1055 cptr = &op[8];
1056 if ((cptr[9].vibrato) && (cptr[9].op_state == OF_TYPE_OFF)) {
1057 vibval4 = vibval_var2;
1058 for (i=0;i<endsamples;i++)
1059 vibval4[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1060 } else vibval4 = vibval_const;
1061
1062 if (cptr[9].tremolo) tremval4 = trem_lut; // tremolo enabled, use table
1063 else tremval4 = tremval_const;
1064
1065 // calculate channel output
1066 for (i=0;i<endsamples;i++) {
1067 operator_advance_drums(&op[7],vibval1[i],&op[7+9],vibval2[i],&op[8+9],vibval4[i]);
1068
1069 opfuncs[op[7].op_state](&op[7]); //Hihat
1070 operator_output(&op[7],0,tremval1[i]);
1071
1072 opfuncs[op[7+9].op_state](&op[7+9]); //Snare
1073 operator_output(&op[7+9],0,tremval2[i]);
1074
1075 opfuncs[op[8+9].op_state](&op[8+9]); //Cymbal
1076 operator_output(&op[8+9],0,tremval4[i]);
1077
1078 Bit32s chanval = (op[7].cval + op[7+9].cval + op[8+9].cval)*2;
1079 CHANVAL_OUT
1080 }
1081 }
1082 }
1083
1084 Bitu max_channel = NUM_CHANNELS;
1085 #if defined(OPLTYPE_IS_OPL3)
1086 if ((adlibreg[0x105]&1)==0) max_channel = NUM_CHANNELS/2;
1087 #endif
1088 for (Bits cur_ch=max_channel-1; cur_ch>=0; cur_ch--) {
1089 // skip drum/percussion operators
1090 if ((adlibreg[ARC_PERC_MODE]&0x20) && (cur_ch >= 6) && (cur_ch < 9)) continue;
1091
1092 Bitu k = cur_ch;
1093 #if defined(OPLTYPE_IS_OPL3)
1094 if (cur_ch < 9) {
1095 cptr = &op[cur_ch];
1096 } else {
1097 cptr = &op[cur_ch+9]; // second set is operator18-operator35
1098 k += (-9+256); // second set uses registers 0x100 onwards
1099 }
1100 // check if this operator is part of a 4-op
1101 if ((adlibreg[0x105]&1) && cptr->is_4op_attached) continue;
1102 #else
1103 cptr = &op[cur_ch];
1104 #endif
1105
1106 // check for FM/AM
1107 if (adlibreg[ARC_FEEDBACK+k]&1) {
1108 #if defined(OPLTYPE_IS_OPL3)
1109 if ((adlibreg[0x105]&1) && cptr->is_4op) {
1110 if (adlibreg[ARC_FEEDBACK+k+3]&1) {
1111 // AM-AM-style synthesis (op1[fb] + (op2 * op3) + op4)
1112 if (cptr[0].op_state != OF_TYPE_OFF) {
1113 if (cptr[0].vibrato) {
1114 vibval1 = vibval_var1;
1115 for (i=0;i<endsamples;i++)
1116 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1117 } else vibval1 = vibval_const;
1118 if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1119 else tremval1 = tremval_const;
1120
1121 // calculate channel output
1122 for (i=0;i<endsamples;i++) {
1123 operator_advance(&cptr[0],vibval1[i]);
1124 opfuncs[cptr[0].op_state](&cptr[0]);
1125 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1126
1127 Bit32s chanval = cptr[0].cval;
1128 CHANVAL_OUT
1129 }
1130 }
1131
1132 if ((cptr[3].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF)) {
1133 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1134 vibval1 = vibval_var1;
1135 for (i=0;i<endsamples;i++)
1136 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1137 } else vibval1 = vibval_const;
1138 if (cptr[9].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1139 else tremval1 = tremval_const;
1140 if (cptr[3].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1141 else tremval2 = tremval_const;
1142
1143 // calculate channel output
1144 for (i=0;i<endsamples;i++) {
1145 operator_advance(&cptr[9],vibval1[i]);
1146 opfuncs[cptr[9].op_state](&cptr[9]);
1147 operator_output(&cptr[9],0,tremval1[i]);
1148
1149 operator_advance(&cptr[3],0);
1150 opfuncs[cptr[3].op_state](&cptr[3]);
1151 operator_output(&cptr[3],cptr[9].cval*FIXEDPT,tremval2[i]);
1152
1153 Bit32s chanval = cptr[3].cval;
1154 CHANVAL_OUT
1155 }
1156 }
1157
1158 if (cptr[3+9].op_state != OF_TYPE_OFF) {
1159 if (cptr[3+9].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1160 else tremval1 = tremval_const;
1161
1162 // calculate channel output
1163 for (i=0;i<endsamples;i++) {
1164 operator_advance(&cptr[3+9],0);
1165 opfuncs[cptr[3+9].op_state](&cptr[3+9]);
1166 operator_output(&cptr[3+9],0,tremval1[i]);
1167
1168 Bit32s chanval = cptr[3+9].cval;
1169 CHANVAL_OUT
1170 }
1171 }
1172 } else {
1173 // AM-FM-style synthesis (op1[fb] + (op2 * op3 * op4))
1174 if (cptr[0].op_state != OF_TYPE_OFF) {
1175 if (cptr[0].vibrato) {
1176 vibval1 = vibval_var1;
1177 for (i=0;i<endsamples;i++)
1178 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1179 } else vibval1 = vibval_const;
1180 if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1181 else tremval1 = tremval_const;
1182
1183 // calculate channel output
1184 for (i=0;i<endsamples;i++) {
1185 operator_advance(&cptr[0],vibval1[i]);
1186 opfuncs[cptr[0].op_state](&cptr[0]);
1187 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1188
1189 Bit32s chanval = cptr[0].cval;
1190 CHANVAL_OUT
1191 }
1192 }
1193
1194 if ((cptr[9].op_state != OF_TYPE_OFF) || (cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
1195 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1196 vibval1 = vibval_var1;
1197 for (i=0;i<endsamples;i++)
1198 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1199 } else vibval1 = vibval_const;
1200 if (cptr[9].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1201 else tremval1 = tremval_const;
1202 if (cptr[3].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1203 else tremval2 = tremval_const;
1204 if (cptr[3+9].tremolo) tremval3 = trem_lut; // tremolo enabled, use table
1205 else tremval3 = tremval_const;
1206
1207 // calculate channel output
1208 for (i=0;i<endsamples;i++) {
1209 operator_advance(&cptr[9],vibval1[i]);
1210 opfuncs[cptr[9].op_state](&cptr[9]);
1211 operator_output(&cptr[9],0,tremval1[i]);
1212
1213 operator_advance(&cptr[3],0);
1214 opfuncs[cptr[3].op_state](&cptr[3]);
1215 operator_output(&cptr[3],cptr[9].cval*FIXEDPT,tremval2[i]);
1216
1217 operator_advance(&cptr[3+9],0);
1218 opfuncs[cptr[3+9].op_state](&cptr[3+9]);
1219 operator_output(&cptr[3+9],cptr[3].cval*FIXEDPT,tremval3[i]);
1220
1221 Bit32s chanval = cptr[3+9].cval;
1222 CHANVAL_OUT
1223 }
1224 }
1225 }
1226 continue;
1227 }
1228 #endif
1229 // 2op additive synthesis
1230 if ((cptr[9].op_state == OF_TYPE_OFF) && (cptr[0].op_state == OF_TYPE_OFF)) continue;
1231 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1232 vibval1 = vibval_var1;
1233 for (i=0;i<endsamples;i++)
1234 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1235 } else vibval1 = vibval_const;
1236 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1237 vibval2 = vibval_var2;
1238 for (i=0;i<endsamples;i++)
1239 vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1240 } else vibval2 = vibval_const;
1241 if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1242 else tremval1 = tremval_const;
1243 if (cptr[9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1244 else tremval2 = tremval_const;
1245
1246 // calculate channel output
1247 for (i=0;i<endsamples;i++) {
1248 // carrier1
1249 operator_advance(&cptr[0],vibval1[i]);
1250 opfuncs[cptr[0].op_state](&cptr[0]);
1251 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1252
1253 // carrier2
1254 operator_advance(&cptr[9],vibval2[i]);
1255 opfuncs[cptr[9].op_state](&cptr[9]);
1256 operator_output(&cptr[9],0,tremval2[i]);
1257
1258 Bit32s chanval = cptr[9].cval + cptr[0].cval;
1259 CHANVAL_OUT
1260 }
1261 } else {
1262 #if defined(OPLTYPE_IS_OPL3)
1263 if ((adlibreg[0x105]&1) && cptr->is_4op) {
1264 if (adlibreg[ARC_FEEDBACK+k+3]&1) {
1265 // FM-AM-style synthesis ((op1[fb] * op2) + (op3 * op4))
1266 if ((cptr[0].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF)) {
1267 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1268 vibval1 = vibval_var1;
1269 for (i=0;i<endsamples;i++)
1270 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1271 } else vibval1 = vibval_const;
1272 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1273 vibval2 = vibval_var2;
1274 for (i=0;i<endsamples;i++)
1275 vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1276 } else vibval2 = vibval_const;
1277 if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1278 else tremval1 = tremval_const;
1279 if (cptr[9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1280 else tremval2 = tremval_const;
1281
1282 // calculate channel output
1283 for (i=0;i<endsamples;i++) {
1284 operator_advance(&cptr[0],vibval1[i]);
1285 opfuncs[cptr[0].op_state](&cptr[0]);
1286 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1287
1288 operator_advance(&cptr[9],vibval2[i]);
1289 opfuncs[cptr[9].op_state](&cptr[9]);
1290 operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
1291
1292 Bit32s chanval = cptr[9].cval;
1293 CHANVAL_OUT
1294 }
1295 }
1296
1297 if ((cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
1298 if (cptr[3].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1299 else tremval1 = tremval_const;
1300 if (cptr[3+9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1301 else tremval2 = tremval_const;
1302
1303 // calculate channel output
1304 for (i=0;i<endsamples;i++) {
1305 operator_advance(&cptr[3],0);
1306 opfuncs[cptr[3].op_state](&cptr[3]);
1307 operator_output(&cptr[3],0,tremval1[i]);
1308
1309 operator_advance(&cptr[3+9],0);
1310 opfuncs[cptr[3+9].op_state](&cptr[3+9]);
1311 operator_output(&cptr[3+9],cptr[3].cval*FIXEDPT,tremval2[i]);
1312
1313 Bit32s chanval = cptr[3+9].cval;
1314 CHANVAL_OUT
1315 }
1316 }
1317
1318 } else {
1319 // FM-FM-style synthesis (op1[fb] * op2 * op3 * op4)
1320 if ((cptr[0].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF) ||
1321 (cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
1322 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1323 vibval1 = vibval_var1;
1324 for (i=0;i<endsamples;i++)
1325 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1326 } else vibval1 = vibval_const;
1327 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1328 vibval2 = vibval_var2;
1329 for (i=0;i<endsamples;i++)
1330 vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1331 } else vibval2 = vibval_const;
1332 if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1333 else tremval1 = tremval_const;
1334 if (cptr[9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1335 else tremval2 = tremval_const;
1336 if (cptr[3].tremolo) tremval3 = trem_lut; // tremolo enabled, use table
1337 else tremval3 = tremval_const;
1338 if (cptr[3+9].tremolo) tremval4 = trem_lut; // tremolo enabled, use table
1339 else tremval4 = tremval_const;
1340
1341 // calculate channel output
1342 for (i=0;i<endsamples;i++) {
1343 operator_advance(&cptr[0],vibval1[i]);
1344 opfuncs[cptr[0].op_state](&cptr[0]);
1345 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1346
1347 operator_advance(&cptr[9],vibval2[i]);
1348 opfuncs[cptr[9].op_state](&cptr[9]);
1349 operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
1350
1351 operator_advance(&cptr[3],0);
1352 opfuncs[cptr[3].op_state](&cptr[3]);
1353 operator_output(&cptr[3],cptr[9].cval*FIXEDPT,tremval3[i]);
1354
1355 operator_advance(&cptr[3+9],0);
1356 opfuncs[cptr[3+9].op_state](&cptr[3+9]);
1357 operator_output(&cptr[3+9],cptr[3].cval*FIXEDPT,tremval4[i]);
1358
1359 Bit32s chanval = cptr[3+9].cval;
1360 CHANVAL_OUT
1361 }
1362 }
1363 }
1364 continue;
1365 }
1366 #endif
1367 // 2op frequency modulation
1368 if ((cptr[9].op_state == OF_TYPE_OFF) && (cptr[0].op_state == OF_TYPE_OFF)) continue;
1369 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1370 vibval1 = vibval_var1;
1371 for (i=0;i<endsamples;i++)
1372 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1373 } else vibval1 = vibval_const;
1374 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1375 vibval2 = vibval_var2;
1376 for (i=0;i<endsamples;i++)
1377 vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1378 } else vibval2 = vibval_const;
1379 if (cptr[0].tremolo) tremval1 = trem_lut; // tremolo enabled, use table
1380 else tremval1 = tremval_const;
1381 if (cptr[9].tremolo) tremval2 = trem_lut; // tremolo enabled, use table
1382 else tremval2 = tremval_const;
1383
1384 // calculate channel output
1385 for (i=0;i<endsamples;i++) {
1386 // modulator
1387 operator_advance(&cptr[0],vibval1[i]);
1388 opfuncs[cptr[0].op_state](&cptr[0]);
1389 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
1390
1391 // carrier
1392 operator_advance(&cptr[9],vibval2[i]);
1393 opfuncs[cptr[9].op_state](&cptr[9]);
1394 operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
1395
1396 Bit32s chanval = cptr[9].cval;
1397 CHANVAL_OUT
1398 }
1399 }
1400 }
1401
1402 #if defined(OPLTYPE_IS_OPL3)
1403 if (adlibreg[0x105]&1) {
1404 // convert to float samples (stereo->stereo)
1405 for (i=0;i<endsamples;i++) {
1406 clipit16(outbufl[i],sndptr++);
1407 clipit16(outbufr[i],sndptr++);
1408 }
1409 } else {
1410 // convert to float samples (mono->stereo)
1411 for (i=0;i<endsamples;i++) {
1412 clipit16(outbufl[i],sndptr++);
1413 clipit16(outbufl[i],sndptr++);
1414 }
1415 }
1416 #else
1417 // convert to float samples
1418 for (i=0;i<endsamples;i++)
1419 clipit16(outbufl[i],sndptr++);
1420 #endif
1421
1422 }
1423 }
1424
1425 void DBOPL::SetPanning(int c, float left, float right)
1426 {
1427 if (FullPan)
1428 {
1429 if (c >= 9)
1430 {
1431 c += 9;
1432 }
1433 op[c].left_pan = left;
1434 op[c].right_pan = right;
1435 }
1436 }
1437
1438 DBOPL::DBOPL(bool fullpan)
1439 {
1440 FullPan = fullpan;
1441 Reset();
1442 }
1443
1444 OPLEmul *DBOPLCreate(bool fullpan)
1445 {
1446 return new DBOPL(fullpan);
1447 }
1448