2 * Copyright (C) 2002-2009 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()
34 static fltype recipsamp
; // inverse of sampling rate
35 static Bit16s wavtable
[WAVEPREC
*3]; // wave form table
37 // vibrato/tremolo tables
38 static Bit32s vib_table
[VIBTAB_SIZE
];
39 static Bit32s trem_table
[TREMTAB_SIZE
*2];
41 static Bit32s vibval_const
[BLOCKBUF_SIZE
];
42 static Bit32s tremval_const
[BLOCKBUF_SIZE
];
44 // vibrato value tables (used per-operator)
45 static Bit32s vibval_var1
[BLOCKBUF_SIZE
];
46 static Bit32s vibval_var2
[BLOCKBUF_SIZE
];
47 //static Bit32s vibval_var3[BLOCKBUF_SIZE];
48 //static Bit32s vibval_var4[BLOCKBUF_SIZE];
50 // vibrato/trmolo value table pointers
51 static Bit32s
*vibval1
, *vibval2
, *vibval3
, *vibval4
;
52 static Bit32s
*tremval1
, *tremval2
, *tremval3
, *tremval4
;
55 // key scale level lookup table
56 static const fltype kslmul
[4] = {
57 0.0, 0.5, 0.25, 1.0 // -> 0, 3, 1.5, 6 dB/oct
60 // frequency multiplicator lookup table
61 static const fltype frqmul_tab
[16] = {
62 0.5,1,2,3,4,5,6,7,8,9,10,10,12,12,15,15
64 // calculated frequency multiplication values (depend on sampling rate)
65 static float frqmul
[16];
68 static Bit8u kslev
[8][16];
70 // map a channel number to the register offset of the modulator (=register base)
71 static const Bit8u modulatorbase
[9] = {
77 // map a register base to a modulator operator number or operator number
78 #if defined(OPLTYPE_IS_OPL3)
79 static const Bit8u regbase2modop
[44] = {
80 0,1,2,0,1,2,0,0,3,4,5,3,4,5,0,0,6,7,8,6,7,8, // first set
81 18,19,20,18,19,20,0,0,21,22,23,21,22,23,0,0,24,25,26,24,25,26 // second set
83 static const Bit8u regbase2op
[44] = {
84 0,1,2,9,10,11,0,0,3,4,5,12,13,14,0,0,6,7,8,15,16,17, // first set
85 18,19,20,27,28,29,0,0,21,22,23,30,31,32,0,0,24,25,26,33,34,35 // second set
88 static const Bit8u regbase2modop
[22] = {
89 0,1,2,0,1,2,0,0,3,4,5,3,4,5,0,0,6,7,8,6,7,8
91 static const Bit8u regbase2op
[22] = {
92 0,1,2,9,10,11,0,0,3,4,5,12,13,14,0,0,6,7,8,15,16,17
97 // start of the waveform
98 static Bit32u waveform
[8] = {
109 // length of the waveform as mask
110 static Bit32u wavemask
[8] = {
121 // where the first entry resides
122 static Bit32u wavestart
[8] = {
133 // envelope generator function constants
134 static fltype attackconst
[4] = {
140 static fltype decrelconst
[4] = {
141 (fltype
)(1/39.28064),
142 (fltype
)(1/31.41608),
143 (fltype
)(1/26.17344),
148 void operator_advance(op_type
* op_pt
, Bit32s vib
) {
149 op_pt
->wfpos
= op_pt
->tcount
; // waveform position
151 // advance waveform time
152 op_pt
->tcount
+= op_pt
->tinc
;
153 op_pt
->tcount
+= (Bit32s
)(op_pt
->tinc
)*vib
/FIXEDPT
;
155 op_pt
->generator_pos
+= generator_add
;
158 void operator_advance_drums(op_type
* op_pt1
, Bit32s vib1
, op_type
* op_pt2
, Bit32s vib2
, op_type
* op_pt3
, Bit32s vib3
) {
159 Bit32u c1
= op_pt1
->tcount
/FIXEDPT
;
160 Bit32u c3
= op_pt3
->tcount
/FIXEDPT
;
161 Bit32u phasebit
= (((c1
& 0x88) ^ ((c1
<<5) & 0x80)) | ((c3
^ (c3
<<2)) & 0x20)) ? 0x02 : 0x00;
163 Bit32u noisebit
= rand()&1;
165 Bit32u snare_phase_bit
= (((Bitu
)((op_pt1
->tcount
/FIXEDPT
) / 0x100))&1);
168 Bit32u inttm
= (phasebit
<<8) | (0x34<<(phasebit
^ (noisebit
<<1)));
169 op_pt1
->wfpos
= inttm
*FIXEDPT
; // waveform position
170 // advance waveform time
171 op_pt1
->tcount
+= op_pt1
->tinc
;
172 op_pt1
->tcount
+= (Bit32s
)(op_pt1
->tinc
)*vib1
/FIXEDPT
;
173 op_pt1
->generator_pos
+= generator_add
;
176 inttm
= ((1+snare_phase_bit
) ^ noisebit
)<<8;
177 op_pt2
->wfpos
= inttm
*FIXEDPT
; // waveform position
178 // advance waveform time
179 op_pt2
->tcount
+= op_pt2
->tinc
;
180 op_pt2
->tcount
+= (Bit32s
)(op_pt2
->tinc
)*vib2
/FIXEDPT
;
181 op_pt2
->generator_pos
+= generator_add
;
184 inttm
= (1+phasebit
)<<8;
185 op_pt3
->wfpos
= inttm
*FIXEDPT
; // waveform position
186 // advance waveform time
187 op_pt3
->tcount
+= op_pt3
->tinc
;
188 op_pt3
->tcount
+= (Bit32s
)(op_pt3
->tinc
)*vib3
/FIXEDPT
;
189 op_pt3
->generator_pos
+= generator_add
;
193 // output level is sustained, mode changes only when operator is turned off (->release)
194 // or when the keep-sustained bit is turned off (->sustain_nokeep)
195 void operator_output(op_type
* op_pt
, Bit32s modulator
, Bit32s trem
) {
196 if (op_pt
->op_state
!= OF_TYPE_OFF
) {
197 op_pt
->lastcval
= op_pt
->cval
;
198 Bit32u i
= (Bit32u
)((op_pt
->wfpos
+modulator
)/FIXEDPT
);
200 // wform: -16384 to 16383 (0x4000)
201 // trem : 32768 to 65535 (0x10000)
202 // step_amp: 0.0 to 1.0
203 // vol : 1/2^14 to 1/2^29 (/0x4000; /1../0x8000)
205 op_pt
->cval
= (Bit32s
)(op_pt
->step_amp
*op_pt
->vol
*op_pt
->cur_wform
[i
&op_pt
->cur_wmask
]*trem
/16.0);
210 // no action, operator is off
211 void operator_off(op_type
* /*op_pt*/) {
214 // output level is sustained, mode changes only when operator is turned off (->release)
215 // or when the keep-sustained bit is turned off (->sustain_nokeep)
216 void operator_sustain(op_type
* op_pt
) {
217 Bit32u num_steps_add
= op_pt
->generator_pos
/FIXEDPT
; // number of (standardized) samples
218 for (Bit32u ct
=0; ct
<num_steps_add
; ct
++) {
219 op_pt
->cur_env_step
++;
221 op_pt
->generator_pos
-= num_steps_add
*FIXEDPT
;
224 // operator in release mode, if output level reaches zero the operator is turned off
225 void operator_release(op_type
* op_pt
) {
227 if (op_pt
->amp
> 0.00000001) {
229 op_pt
->amp
*= op_pt
->releasemul
;
232 Bit32u num_steps_add
= op_pt
->generator_pos
/FIXEDPT
; // number of (standardized) samples
233 for (Bit32u ct
=0; ct
<num_steps_add
; ct
++) {
234 op_pt
->cur_env_step
++; // sample counter
235 if ((op_pt
->cur_env_step
& op_pt
->env_step_r
)==0) {
236 if (op_pt
->amp
<= 0.00000001) {
237 // release phase finished, turn off this operator
239 if (op_pt
->op_state
== OF_TYPE_REL
) {
240 op_pt
->op_state
= OF_TYPE_OFF
;
243 op_pt
->step_amp
= op_pt
->amp
;
246 op_pt
->generator_pos
-= num_steps_add
*FIXEDPT
;
249 // operator in decay mode, if sustain level is reached the output level is either
250 // kept (sustain level keep enabled) or the operator is switched into release mode
251 void operator_decay(op_type
* op_pt
) {
252 if (op_pt
->amp
> op_pt
->sustain_level
) {
254 op_pt
->amp
*= op_pt
->decaymul
;
257 Bit32u num_steps_add
= op_pt
->generator_pos
/FIXEDPT
; // number of (standardized) samples
258 for (Bit32u ct
=0; ct
<num_steps_add
; ct
++) {
259 op_pt
->cur_env_step
++;
260 if ((op_pt
->cur_env_step
& op_pt
->env_step_d
)==0) {
261 if (op_pt
->amp
<= op_pt
->sustain_level
) {
262 // decay phase finished, sustain level reached
263 if (op_pt
->sus_keep
) {
264 // keep sustain level (until turned off)
265 op_pt
->op_state
= OF_TYPE_SUS
;
266 op_pt
->amp
= op_pt
->sustain_level
;
268 // next: release phase
269 op_pt
->op_state
= OF_TYPE_SUS_NOKEEP
;
272 op_pt
->step_amp
= op_pt
->amp
;
275 op_pt
->generator_pos
-= num_steps_add
*FIXEDPT
;
278 // operator in attack mode, if full output level is reached,
279 // the operator is switched into decay mode
280 void operator_attack(op_type
* op_pt
) {
281 op_pt
->amp
= ((op_pt
->a3
*op_pt
->amp
+ op_pt
->a2
)*op_pt
->amp
+ op_pt
->a1
)*op_pt
->amp
+ op_pt
->a0
;
283 Bit32u num_steps_add
= op_pt
->generator_pos
/FIXEDPT
; // number of (standardized) samples
284 for (Bit32u ct
=0; ct
<num_steps_add
; ct
++) {
285 op_pt
->cur_env_step
++; // next sample
286 if ((op_pt
->cur_env_step
& op_pt
->env_step_a
)==0) { // check if next step already reached
287 if (op_pt
->amp
> 1.0) {
288 // attack phase finished, next: decay
289 op_pt
->op_state
= OF_TYPE_DEC
;
291 op_pt
->step_amp
= 1.0;
293 op_pt
->step_skip_pos_a
<<= 1;
294 if (op_pt
->step_skip_pos_a
==0) op_pt
->step_skip_pos_a
= 1;
295 if (op_pt
->step_skip_pos_a
& op_pt
->env_step_skip_a
) { // check if required to skip next step
296 op_pt
->step_amp
= op_pt
->amp
;
300 op_pt
->generator_pos
-= num_steps_add
*FIXEDPT
;
304 typedef void (*optype_fptr
)(op_type
*);
306 optype_fptr opfuncs
[6] = {
310 operator_sustain
, // sustain phase (keeping level)
311 operator_release
, // sustain_nokeep phase (release-style)
315 void change_attackrate(Bitu regbase
, op_type
* op_pt
) {
316 Bits attackrate
= adlibreg
[ARC_ATTR_DECR
+regbase
]>>4;
318 fltype f
= (fltype
)(pow(FL2
,(fltype
)attackrate
+(op_pt
->toff
>>2)-1)*attackconst
[op_pt
->toff
&3]*recipsamp
);
319 // attack rate coefficients
320 op_pt
->a0
= (fltype
)(0.0377*f
);
321 op_pt
->a1
= (fltype
)(10.73*f
+1);
322 op_pt
->a2
= (fltype
)(-17.57*f
);
323 op_pt
->a3
= (fltype
)(7.42*f
);
325 Bits step_skip
= attackrate
*4 + op_pt
->toff
;
326 Bits steps
= step_skip
>> 2;
327 op_pt
->env_step_a
= (1<<(steps
<=12?12-steps
:0))-1;
329 Bits step_num
= (step_skip
<=48)?(4-(step_skip
&3)):0;
330 static Bit8u step_skip_mask
[5] = {0xff, 0xfe, 0xee, 0xba, 0xaa};
331 op_pt
->env_step_skip_a
= step_skip_mask
[step_num
];
333 #if defined(OPLTYPE_IS_OPL3)
338 op_pt
->a0
= (fltype
)(2.0); // something that triggers an immediate transition to amp:=1.0
339 op_pt
->a1
= (fltype
)(0.0);
340 op_pt
->a2
= (fltype
)(0.0);
341 op_pt
->a3
= (fltype
)(0.0);
349 op_pt
->env_step_a
= 0;
350 op_pt
->env_step_skip_a
= 0;
354 void change_decayrate(Bitu regbase
, op_type
* op_pt
) {
355 Bits decayrate
= adlibreg
[ARC_ATTR_DECR
+regbase
]&15;
356 // decaymul should be 1.0 when decayrate==0
358 fltype f
= (fltype
)(-7.4493*decrelconst
[op_pt
->toff
&3]*recipsamp
);
359 op_pt
->decaymul
= (fltype
)(pow(FL2
,f
*pow(FL2
,(fltype
)(decayrate
+(op_pt
->toff
>>2)))));
360 Bits steps
= (decayrate
*4 + op_pt
->toff
) >> 2;
361 op_pt
->env_step_d
= (1<<(steps
<=12?12-steps
:0))-1;
363 op_pt
->decaymul
= 1.0;
364 op_pt
->env_step_d
= 0;
368 void change_releaserate(Bitu regbase
, op_type
* op_pt
) {
369 Bits releaserate
= adlibreg
[ARC_SUSL_RELR
+regbase
]&15;
370 // releasemul should be 1.0 when releaserate==0
372 fltype f
= (fltype
)(-7.4493*decrelconst
[op_pt
->toff
&3]*recipsamp
);
373 op_pt
->releasemul
= (fltype
)(pow(FL2
,f
*pow(FL2
,(fltype
)(releaserate
+(op_pt
->toff
>>2)))));
374 Bits steps
= (releaserate
*4 + op_pt
->toff
) >> 2;
375 op_pt
->env_step_r
= (1<<(steps
<=12?12-steps
:0))-1;
377 op_pt
->releasemul
= 1.0;
378 op_pt
->env_step_r
= 0;
382 void change_sustainlevel(Bitu regbase
, op_type
* op_pt
) {
383 Bits sustainlevel
= adlibreg
[ARC_SUSL_RELR
+regbase
]>>4;
384 // sustainlevel should be 0.0 when sustainlevel==15 (max)
385 if (sustainlevel
<15) {
386 op_pt
->sustain_level
= (fltype
)(pow(FL2
,(fltype
)sustainlevel
* (-FL05
)));
388 op_pt
->sustain_level
= 0.0;
392 void change_waveform(Bitu regbase
, op_type
* op_pt
) {
393 #if defined(OPLTYPE_IS_OPL3)
394 if (regbase
>=ARC_SECONDSET
) regbase
-= (ARC_SECONDSET
-22); // second set starts at 22
396 // waveform selection
397 op_pt
->cur_wmask
= wavemask
[wave_sel
[regbase
]];
398 op_pt
->cur_wform
= &wavtable
[waveform
[wave_sel
[regbase
]]];
399 // (might need to be adapted to waveform type here...)
402 void change_keepsustain(Bitu regbase
, op_type
* op_pt
) {
403 op_pt
->sus_keep
= (adlibreg
[ARC_TVS_KSR_MUL
+regbase
]&0x20)>0;
404 if (op_pt
->op_state
==OF_TYPE_SUS
) {
405 if (!op_pt
->sus_keep
) op_pt
->op_state
= OF_TYPE_SUS_NOKEEP
;
406 } else if (op_pt
->op_state
==OF_TYPE_SUS_NOKEEP
) {
407 if (op_pt
->sus_keep
) op_pt
->op_state
= OF_TYPE_SUS
;
411 // enable/disable vibrato/tremolo LFO effects
412 void change_vibrato(Bitu regbase
, op_type
* op_pt
) {
413 op_pt
->vibrato
= (adlibreg
[ARC_TVS_KSR_MUL
+regbase
]&0x40)!=0;
414 op_pt
->tremolo
= (adlibreg
[ARC_TVS_KSR_MUL
+regbase
]&0x80)!=0;
417 // change amount of self-feedback
418 void change_feedback(Bitu chanbase
, op_type
* op_pt
) {
419 Bits feedback
= adlibreg
[ARC_FEEDBACK
+chanbase
]&14;
420 if (feedback
) op_pt
->mfbi
= (Bit32s
)(pow(FL2
,(fltype
)((feedback
>>1)+8)));
421 else op_pt
->mfbi
= 0;
424 void change_frequency(Bitu chanbase
, Bitu regbase
, op_type
* op_pt
) {
426 Bit32u frn
= ((((Bit32u
)adlibreg
[ARC_KON_BNUM
+chanbase
])&3)<<8) + (Bit32u
)adlibreg
[ARC_FREQ_NUM
+chanbase
];
427 // block number/octave
428 Bit32u oct
= ((((Bit32u
)adlibreg
[ARC_KON_BNUM
+chanbase
])>>2)&7);
429 op_pt
->freq_high
= (Bit32s
)((frn
>>7)&7);
432 Bit32u note_sel
= (adlibreg
[8]>>6)&1;
433 op_pt
->toff
= ((frn
>>9)&(note_sel
^1)) | ((frn
>>8)¬e_sel
);
434 op_pt
->toff
+= (oct
<<1);
436 // envelope scaling (KSR)
437 if (!(adlibreg
[ARC_TVS_KSR_MUL
+regbase
]&0x10)) op_pt
->toff
>>= 2;
440 op_pt
->tinc
= (Bit32u
)((((fltype
)(frn
<<oct
))*frqmul
[adlibreg
[ARC_TVS_KSR_MUL
+regbase
]&15]));
442 fltype vol_in
= (fltype
)((fltype
)(adlibreg
[ARC_KSL_OUTLEV
+regbase
]&63) +
443 kslmul
[adlibreg
[ARC_KSL_OUTLEV
+regbase
]>>6]*kslev
[oct
][frn
>>6]);
444 op_pt
->vol
= (fltype
)(pow(FL2
,(fltype
)(vol_in
* -0.125 - 14)));
446 // operator frequency changed, care about features that depend on it
447 change_attackrate(regbase
,op_pt
);
448 change_decayrate(regbase
,op_pt
);
449 change_releaserate(regbase
,op_pt
);
452 void enable_operator(Bitu regbase
, op_type
* op_pt
, Bit32u act_type
) {
453 // check if this is really an off-on transition
454 if (op_pt
->act_state
== OP_ACT_OFF
) {
455 Bits wselbase
= regbase
;
456 if (wselbase
>=ARC_SECONDSET
) wselbase
-= (ARC_SECONDSET
-22); // second set starts at 22
458 op_pt
->tcount
= wavestart
[wave_sel
[wselbase
]]*FIXEDPT
;
460 // start with attack mode
461 op_pt
->op_state
= OF_TYPE_ATT
;
462 op_pt
->act_state
|= act_type
;
466 void disable_operator(op_type
* op_pt
, Bit32u act_type
) {
467 // check if this is really an on-off transition
468 if (op_pt
->act_state
!= OP_ACT_OFF
) {
469 op_pt
->act_state
&= (~act_type
);
470 if (op_pt
->act_state
== OP_ACT_OFF
) {
471 if (op_pt
->op_state
!= OF_TYPE_OFF
) op_pt
->op_state
= OF_TYPE_REL
;
476 void adlib_init(Bit32u samplerate
) {
479 int_samplerate
= samplerate
;
481 generator_add
= (Bit32u
)(INTFREQU
*FIXEDPT
/int_samplerate
);
484 memset((void *)adlibreg
,0,sizeof(adlibreg
));
485 memset((void *)op
,0,sizeof(op_type
)*MAXOPERATORS
);
486 memset((void *)wave_sel
,0,sizeof(wave_sel
));
488 for (i
=0;i
<MAXOPERATORS
;i
++) {
489 op
[i
].op_state
= OF_TYPE_OFF
;
490 op
[i
].act_state
= OP_ACT_OFF
;
492 op
[i
].step_amp
= 0.0;
497 op
[i
].cur_wmask
= wavemask
[0];
498 op
[i
].cur_wform
= &wavtable
[waveform
[0]];
501 op
[i
].generator_pos
= 0;
502 op
[i
].cur_env_step
= 0;
503 op
[i
].env_step_a
= 0;
504 op
[i
].env_step_d
= 0;
505 op
[i
].env_step_r
= 0;
506 op
[i
].step_skip_pos_a
= 0;
507 op
[i
].env_step_skip_a
= 0;
509 #if defined(OPLTYPE_IS_OPL3)
510 op
[i
].is_4op
= false;
511 op
[i
].is_4op_attached
= false;
517 recipsamp
= 1.0 / (fltype
)int_samplerate
;
518 for (i
=15;i
>=0;i
--) {
519 frqmul
[i
] = (fltype
)(frqmul_tab
[i
]*INTFREQU
/(fltype
)WAVEPREC
*(fltype
)FIXEDPT
*recipsamp
);
526 // create vibrato table
531 for (i
=4; i
<VIBTAB_SIZE
; i
++) vib_table
[i
] = vib_table
[i
-4]*-1;
533 // vibrato at ~6.1 ?? (opl3 docs say 6.1, opl4 docs say 6.0, y8950 docs say 6.4)
534 vibtab_add
= static_cast<Bit32u
>(VIBTAB_SIZE
*FIXEDPT_LFO
/8192*INTFREQU
/int_samplerate
);
537 for (i
=0; i
<BLOCKBUF_SIZE
; i
++) vibval_const
[i
] = 0;
540 // create tremolo table
541 Bit32s trem_table_int
[TREMTAB_SIZE
];
542 for (i
=0; i
<14; i
++) trem_table_int
[i
] = i
-13; // upwards (13 to 26 -> -0.5/6 to 0)
543 for (i
=14; i
<41; i
++) trem_table_int
[i
] = -i
+14; // downwards (26 to 0 -> 0 to -1/6)
544 for (i
=41; i
<53; i
++) trem_table_int
[i
] = i
-40-26; // upwards (1 to 12 -> -1/6 to -0.5/6)
546 for (i
=0; i
<TREMTAB_SIZE
; i
++) {
547 // 0.0 .. -26/26*4.8/6 == [0.0 .. -0.8], 4/53 steps == [1 .. 0.57]
548 fltype trem_val1
=(fltype
)(((fltype
)trem_table_int
[i
])*4.8/26.0/6.0); // 4.8db
549 fltype trem_val2
=(fltype
)((fltype
)((Bit32s
)(trem_table_int
[i
]/4))*1.2/6.0/6.0); // 1.2db (larger stepping)
551 trem_table
[i
] = (Bit32s
)(pow(FL2
,trem_val1
)*FIXEDPT
);
552 trem_table
[TREMTAB_SIZE
+i
] = (Bit32s
)(pow(FL2
,trem_val2
)*FIXEDPT
);
556 tremtab_add
= (Bit32u
)((fltype
)TREMTAB_SIZE
* TREM_FREQ
* FIXEDPT_LFO
/ (fltype
)int_samplerate
);
559 for (i
=0; i
<BLOCKBUF_SIZE
; i
++) tremval_const
[i
] = FIXEDPT
;
562 static Bitu initfirstime
= 0;
566 // create waveform tables
567 for (i
=0;i
<(WAVEPREC
>>1);i
++) {
568 wavtable
[(i
<<1) +WAVEPREC
] = (Bit16s
)(16384*sin((fltype
)((i
<<1) )*PI
*2/WAVEPREC
));
569 wavtable
[(i
<<1)+1+WAVEPREC
] = (Bit16s
)(16384*sin((fltype
)((i
<<1)+1)*PI
*2/WAVEPREC
));
570 wavtable
[i
] = wavtable
[(i
<<1) +WAVEPREC
];
571 // alternative: (zero-less)
572 /* wavtable[(i<<1) +WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<2)+1)*PI/WAVEPREC));
573 wavtable[(i<<1)+1+WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<2)+3)*PI/WAVEPREC));
574 wavtable[i] = wavtable[(i<<1)-1+WAVEPREC]; */
576 for (i
=0;i
<(WAVEPREC
>>3);i
++) {
577 wavtable
[i
+(WAVEPREC
<<1)] = wavtable
[i
+(WAVEPREC
>>3)]-16384;
578 wavtable
[i
+((WAVEPREC
*17)>>3)] = wavtable
[i
+(WAVEPREC
>>2)]+16384;
581 // key scale level table verified ([table in book]*8/3)
582 kslev
[7][0] = 0; kslev
[7][1] = 24; kslev
[7][2] = 32; kslev
[7][3] = 37;
583 kslev
[7][4] = 40; kslev
[7][5] = 43; kslev
[7][6] = 45; kslev
[7][7] = 47;
585 for (i
=9;i
<16;i
++) kslev
[7][i
] = (Bit8u
)(i
+41);
588 oct
= (Bits
)kslev
[j
+1][i
]-8;
589 if (oct
< 0) oct
= 0;
590 kslev
[j
][i
] = (Bit8u
)oct
;
599 void adlib_write(Bitu idx
, Bit8u val
) {
600 Bit32u second_set
= idx
&0x100;
605 // here we check for the second set registers, too:
607 case 0x02: // timer1 counter
608 case 0x03: // timer2 counter
611 // IRQ reset, timer mask/start
613 // clear IRQ bits in status register
619 #if defined(OPLTYPE_IS_OPL3)
620 case 0x04|ARC_SECONDSET
:
621 // 4op enable/disable switches for each possible channel
622 op
[0].is_4op
= (val
&1)>0;
623 op
[3].is_4op_attached
= op
[0].is_4op
;
624 op
[1].is_4op
= (val
&2)>0;
625 op
[4].is_4op_attached
= op
[1].is_4op
;
626 op
[2].is_4op
= (val
&4)>0;
627 op
[5].is_4op_attached
= op
[2].is_4op
;
628 op
[18].is_4op
= (val
&8)>0;
629 op
[21].is_4op_attached
= op
[18].is_4op
;
630 op
[19].is_4op
= (val
&16)>0;
631 op
[22].is_4op_attached
= op
[19].is_4op
;
632 op
[20].is_4op
= (val
&32)>0;
633 op
[23].is_4op_attached
= op
[20].is_4op
;
635 case 0x05|ARC_SECONDSET
:
645 case ARC_TVS_KSR_MUL
:
646 case ARC_TVS_KSR_MUL
+0x10: {
647 // tremolo/vibrato/sustain keeping enabled; key scale rate; frequency multiplication
649 Bitu base
= (idx
-ARC_TVS_KSR_MUL
)&0xff;
650 if ((num
<6) && (base
<22)) {
651 Bitu modop
= regbase2modop
[second_set
?(base
+22):base
];
652 Bitu regbase
= base
+second_set
;
653 Bitu chanbase
= second_set
?(modop
-18+ARC_SECONDSET
):modop
;
655 // change tremolo/vibrato and sustain keeping of this operator
656 op_type
* op_ptr
= &op
[modop
+((num
<3) ? 0 : 9)];
657 change_keepsustain(regbase
,op_ptr
);
658 change_vibrato(regbase
,op_ptr
);
660 // change frequency calculations of this operator as
661 // key scale rate and frequency multiplicator can be changed
662 #if defined(OPLTYPE_IS_OPL3)
663 if ((adlibreg
[0x105]&1) && (op
[modop
].is_4op_attached
)) {
664 // operator uses frequency of channel
665 change_frequency(chanbase
-3,regbase
,op_ptr
);
667 change_frequency(chanbase
,regbase
,op_ptr
);
670 change_frequency(chanbase
,base
,op_ptr
);
676 case ARC_KSL_OUTLEV
+0x10: {
677 // key scale level; output rate
679 Bitu base
= (idx
-ARC_KSL_OUTLEV
)&0xff;
680 if ((num
<6) && (base
<22)) {
681 Bitu modop
= regbase2modop
[second_set
?(base
+22):base
];
682 Bitu chanbase
= second_set
?(modop
-18+ARC_SECONDSET
):modop
;
684 // change frequency calculations of this operator as
685 // key scale level and output rate can be changed
686 op_type
* op_ptr
= &op
[modop
+((num
<3) ? 0 : 9)];
687 #if defined(OPLTYPE_IS_OPL3)
688 Bitu regbase
= base
+second_set
;
689 if ((adlibreg
[0x105]&1) && (op
[modop
].is_4op_attached
)) {
690 // operator uses frequency of channel
691 change_frequency(chanbase
-3,regbase
,op_ptr
);
693 change_frequency(chanbase
,regbase
,op_ptr
);
696 change_frequency(chanbase
,base
,op_ptr
);
702 case ARC_ATTR_DECR
+0x10: {
703 // attack/decay rates
705 Bitu base
= (idx
-ARC_ATTR_DECR
)&0xff;
706 if ((num
<6) && (base
<22)) {
707 Bitu regbase
= base
+second_set
;
709 // change attack rate and decay rate of this operator
710 op_type
* op_ptr
= &op
[regbase2op
[second_set
?(base
+22):base
]];
711 change_attackrate(regbase
,op_ptr
);
712 change_decayrate(regbase
,op_ptr
);
717 case ARC_SUSL_RELR
+0x10: {
718 // sustain level; release rate
720 Bitu base
= (idx
-ARC_SUSL_RELR
)&0xff;
721 if ((num
<6) && (base
<22)) {
722 Bitu regbase
= base
+second_set
;
724 // change sustain level and release rate of this operator
725 op_type
* op_ptr
= &op
[regbase2op
[second_set
?(base
+22):base
]];
726 change_releaserate(regbase
,op_ptr
);
727 change_sustainlevel(regbase
,op_ptr
);
732 // 0xa0-0xa8 low8 frequency
733 Bitu base
= (idx
-ARC_FREQ_NUM
)&0xff;
735 Bits opbase
= second_set
?(base
+18):base
;
736 #if defined(OPLTYPE_IS_OPL3)
737 if ((adlibreg
[0x105]&1) && op
[opbase
].is_4op_attached
) break;
739 // regbase of modulator:
740 Bits modbase
= modulatorbase
[base
]+second_set
;
742 Bitu chanbase
= base
+second_set
;
744 change_frequency(chanbase
,modbase
,&op
[opbase
]);
745 change_frequency(chanbase
,modbase
+3,&op
[opbase
+9]);
746 #if defined(OPLTYPE_IS_OPL3)
747 // for 4op channels all four operators are modified to the frequency of the channel
748 if ((adlibreg
[0x105]&1) && op
[second_set
?(base
+18):base
].is_4op
) {
749 change_frequency(chanbase
,modbase
+8,&op
[opbase
+3]);
750 change_frequency(chanbase
,modbase
+3+8,&op
[opbase
+3+9]);
757 if (idx
== ARC_PERC_MODE
) {
758 #if defined(OPLTYPE_IS_OPL3)
759 if (second_set
) return;
762 if ((val
&0x30) == 0x30) { // BassDrum active
763 enable_operator(16,&op
[6],OP_ACT_PERC
);
764 change_frequency(6,16,&op
[6]);
765 enable_operator(16+3,&op
[6+9],OP_ACT_PERC
);
766 change_frequency(6,16+3,&op
[6+9]);
768 disable_operator(&op
[6],OP_ACT_PERC
);
769 disable_operator(&op
[6+9],OP_ACT_PERC
);
771 if ((val
&0x28) == 0x28) { // Snare active
772 enable_operator(17+3,&op
[16],OP_ACT_PERC
);
773 change_frequency(7,17+3,&op
[16]);
775 disable_operator(&op
[16],OP_ACT_PERC
);
777 if ((val
&0x24) == 0x24) { // TomTom active
778 enable_operator(18,&op
[8],OP_ACT_PERC
);
779 change_frequency(8,18,&op
[8]);
781 disable_operator(&op
[8],OP_ACT_PERC
);
783 if ((val
&0x22) == 0x22) { // Cymbal active
784 enable_operator(18+3,&op
[8+9],OP_ACT_PERC
);
785 change_frequency(8,18+3,&op
[8+9]);
787 disable_operator(&op
[8+9],OP_ACT_PERC
);
789 if ((val
&0x21) == 0x21) { // Hihat active
790 enable_operator(17,&op
[7],OP_ACT_PERC
);
791 change_frequency(7,17,&op
[7]);
793 disable_operator(&op
[7],OP_ACT_PERC
);
799 Bitu base
= (idx
-ARC_KON_BNUM
)&0xff;
801 Bits opbase
= second_set
?(base
+18):base
;
802 #if defined(OPLTYPE_IS_OPL3)
803 if ((adlibreg
[0x105]&1) && op
[opbase
].is_4op_attached
) break;
805 // regbase of modulator:
806 Bits modbase
= modulatorbase
[base
]+second_set
;
809 // operator switched on
810 enable_operator(modbase
,&op
[opbase
],OP_ACT_NORMAL
); // modulator (if 2op)
811 enable_operator(modbase
+3,&op
[opbase
+9],OP_ACT_NORMAL
); // carrier (if 2op)
812 #if defined(OPLTYPE_IS_OPL3)
813 // for 4op channels all four operators are switched on
814 if ((adlibreg
[0x105]&1) && op
[opbase
].is_4op
) {
815 // turn on chan+3 operators as well
816 enable_operator(modbase
+8,&op
[opbase
+3],OP_ACT_NORMAL
);
817 enable_operator(modbase
+3+8,&op
[opbase
+3+9],OP_ACT_NORMAL
);
821 // operator switched off
822 disable_operator(&op
[opbase
],OP_ACT_NORMAL
);
823 disable_operator(&op
[opbase
+9],OP_ACT_NORMAL
);
824 #if defined(OPLTYPE_IS_OPL3)
825 // for 4op channels all four operators are switched off
826 if ((adlibreg
[0x105]&1) && op
[opbase
].is_4op
) {
827 // turn off chan+3 operators as well
828 disable_operator(&op
[opbase
+3],OP_ACT_NORMAL
);
829 disable_operator(&op
[opbase
+3+9],OP_ACT_NORMAL
);
834 Bitu chanbase
= base
+second_set
;
836 // change frequency calculations of modulator and carrier (2op) as
837 // the frequency of the channel has changed
838 change_frequency(chanbase
,modbase
,&op
[opbase
]);
839 change_frequency(chanbase
,modbase
+3,&op
[opbase
+9]);
840 #if defined(OPLTYPE_IS_OPL3)
841 // for 4op channels all four operators are modified to the frequency of the channel
842 if ((adlibreg
[0x105]&1) && op
[second_set
?(base
+18):base
].is_4op
) {
843 // change frequency calculations of chan+3 operators as well
844 change_frequency(chanbase
,modbase
+8,&op
[opbase
+3]);
845 change_frequency(chanbase
,modbase
+3+8,&op
[opbase
+3+9]);
852 // 0xc0-0xc8 feedback/modulation type (AM/FM)
853 Bitu base
= (idx
-ARC_FEEDBACK
)&0xff;
855 Bits opbase
= second_set
?(base
+18):base
;
856 Bitu chanbase
= base
+second_set
;
857 change_feedback(chanbase
,&op
[opbase
]);
858 #if defined(OPLTYPE_IS_OPL3)
860 op
[opbase
].left_pan
= ((val
&0x10)>>4);
861 op
[opbase
].right_pan
= ((val
&0x20)>>5);
867 case ARC_WAVE_SEL
+0x10: {
869 Bitu base
= (idx
-ARC_WAVE_SEL
)&0xff;
870 if ((num
<6) && (base
<22)) {
871 #if defined(OPLTYPE_IS_OPL3)
872 Bits wselbase
= second_set
?(base
+22):base
; // for easier mapping onto wave_sel[]
874 if (adlibreg
[0x105]&1) wave_sel
[wselbase
] = val
&7; // opl3 mode enabled, all waveforms accessible
875 else wave_sel
[wselbase
] = val
&3;
876 op_type
* op_ptr
= &op
[regbase2modop
[wselbase
]+((num
<3) ? 0 : 9)];
877 change_waveform(wselbase
,op_ptr
);
879 if (adlibreg
[0x01]&0x20) {
880 // wave selection enabled, change waveform
881 wave_sel
[base
] = val
&3;
882 op_type
* op_ptr
= &op
[regbase2modop
[base
]+((num
<3) ? 0 : 9)];
883 change_waveform(base
,op_ptr
);
895 Bitu
adlib_reg_read(Bitu port
) {
896 #if defined(OPLTYPE_IS_OPL3)
897 // opl3-detection routines require ret&6 to be zero
903 // opl2-detection routines require ret&6 to be 6
911 void adlib_write_index(Bitu port
, Bit8u val
) {
913 #if defined(OPLTYPE_IS_OPL3)
915 // possibly second set
916 if (((adlibreg
[0x105]&1)!=0) || (opl_index
==5)) opl_index
|= ARC_SECONDSET
;
921 static void OPL_INLINE
clipit16(Bit32s ival
, Bit16s
* outval
) {
924 *outval
=(Bit16s
)ival
;
935 // be careful with this
936 // uses cptr and chanval, outputs into outbufl(/outbufr)
937 // for opl3 check if opl3-mode is enabled (which uses stereo panning)
939 #if defined(OPLTYPE_IS_OPL3)
940 #define CHANVAL_OUT \
941 if (adlibreg[0x105]&1) { \
942 outbufl[i] += chanval*cptr[0].left_pan; \
943 outbufr[i] += chanval*cptr[0].right_pan; \
945 outbufl[i] += chanval; \
948 #define CHANVAL_OUT \
949 outbufl[i] += chanval;
952 void adlib_getsample(Bit16s
* sndptr
, Bits numsamples
) {
956 Bit32s outbufl
[BLOCKBUF_SIZE
];
957 #if defined(OPLTYPE_IS_OPL3)
958 // second output buffer (right channel for opl3 stereo)
959 Bit32s outbufr
[BLOCKBUF_SIZE
];
962 // vibrato/tremolo lookup tables (global, to possibly be used by all operators)
963 Bit32s vib_lut
[BLOCKBUF_SIZE
];
964 Bit32s trem_lut
[BLOCKBUF_SIZE
];
966 Bits samples_to_process
= numsamples
;
968 for (Bits cursmp
=0; cursmp
<samples_to_process
; cursmp
+=endsamples
) {
969 endsamples
= samples_to_process
-cursmp
;
970 if (endsamples
>BLOCKBUF_SIZE
) endsamples
= BLOCKBUF_SIZE
;
972 memset((void*)&outbufl
,0,endsamples
*sizeof(Bit32s
));
973 #if defined(OPLTYPE_IS_OPL3)
974 // clear second output buffer (opl3 stereo)
975 if (adlibreg
[0x105]&1) memset((void*)&outbufr
,0,endsamples
*sizeof(Bit32s
));
978 // calculate vibrato/tremolo lookup tables
979 Bit32s vib_tshift
= ((adlibreg
[ARC_PERC_MODE
]&0x40)==0) ? 1 : 0; // 14cents/7cents switching
980 for (i
=0;i
<endsamples
;i
++) {
981 // cycle through vibrato table
982 vibtab_pos
+= vibtab_add
;
983 if (vibtab_pos
/FIXEDPT_LFO
>=VIBTAB_SIZE
) vibtab_pos
-=VIBTAB_SIZE
*FIXEDPT_LFO
;
984 vib_lut
[i
] = vib_table
[vibtab_pos
/FIXEDPT_LFO
]>>vib_tshift
; // 14cents (14/100 of a semitone) or 7cents
986 // cycle through tremolo table
987 tremtab_pos
+= tremtab_add
;
988 if (tremtab_pos
/FIXEDPT_LFO
>=TREMTAB_SIZE
) tremtab_pos
-=TREMTAB_SIZE
*FIXEDPT_LFO
;
989 if (adlibreg
[ARC_PERC_MODE
]&0x80) trem_lut
[i
] = trem_table
[tremtab_pos
/FIXEDPT_LFO
];
990 else trem_lut
[i
] = trem_table
[TREMTAB_SIZE
+tremtab_pos
/FIXEDPT_LFO
];
993 if (adlibreg
[ARC_PERC_MODE
]&0x20) {
996 if (adlibreg
[ARC_FEEDBACK
+6]&1) {
997 // additive synthesis
998 if (cptr
[9].op_state
!= OF_TYPE_OFF
) {
999 if (cptr
[9].vibrato
) {
1000 vibval1
= vibval_var1
;
1001 for (i
=0;i
<endsamples
;i
++)
1002 vibval1
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[9].freq_high
/8)*FIXEDPT
*VIBFAC
);
1003 } else vibval1
= vibval_const
;
1004 if (cptr
[9].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1005 else tremval1
= tremval_const
;
1007 // calculate channel output
1008 for (i
=0;i
<endsamples
;i
++) {
1009 operator_advance(&cptr
[9],vibval1
[i
]);
1010 opfuncs
[cptr
[9].op_state
](&cptr
[9]);
1011 operator_output(&cptr
[9],0,tremval1
[i
]);
1013 Bit32s chanval
= cptr
[9].cval
*2;
1018 // frequency modulation
1019 if ((cptr
[9].op_state
!= OF_TYPE_OFF
) || (cptr
[0].op_state
!= OF_TYPE_OFF
)) {
1020 if ((cptr
[0].vibrato
) && (cptr
[0].op_state
!= OF_TYPE_OFF
)) {
1021 vibval1
= vibval_var1
;
1022 for (i
=0;i
<endsamples
;i
++)
1023 vibval1
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[0].freq_high
/8)*FIXEDPT
*VIBFAC
);
1024 } else vibval1
= vibval_const
;
1025 if ((cptr
[9].vibrato
) && (cptr
[9].op_state
!= OF_TYPE_OFF
)) {
1026 vibval2
= vibval_var2
;
1027 for (i
=0;i
<endsamples
;i
++)
1028 vibval2
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[9].freq_high
/8)*FIXEDPT
*VIBFAC
);
1029 } else vibval2
= vibval_const
;
1030 if (cptr
[0].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1031 else tremval1
= tremval_const
;
1032 if (cptr
[9].tremolo
) tremval2
= trem_lut
; // tremolo enabled, use table
1033 else tremval2
= tremval_const
;
1035 // calculate channel output
1036 for (i
=0;i
<endsamples
;i
++) {
1037 operator_advance(&cptr
[0],vibval1
[i
]);
1038 opfuncs
[cptr
[0].op_state
](&cptr
[0]);
1039 operator_output(&cptr
[0],(cptr
[0].lastcval
+cptr
[0].cval
)*cptr
[0].mfbi
/2,tremval1
[i
]);
1041 operator_advance(&cptr
[9],vibval2
[i
]);
1042 opfuncs
[cptr
[9].op_state
](&cptr
[9]);
1043 operator_output(&cptr
[9],cptr
[0].cval
*FIXEDPT
,tremval2
[i
]);
1045 Bit32s chanval
= cptr
[9].cval
*2;
1052 if (op
[8].op_state
!= OF_TYPE_OFF
) {
1054 if (cptr
[0].vibrato
) {
1055 vibval3
= vibval_var1
;
1056 for (i
=0;i
<endsamples
;i
++)
1057 vibval3
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[0].freq_high
/8)*FIXEDPT
*VIBFAC
);
1058 } else vibval3
= vibval_const
;
1060 if (cptr
[0].tremolo
) tremval3
= trem_lut
; // tremolo enabled, use table
1061 else tremval3
= tremval_const
;
1063 // calculate channel output
1064 for (i
=0;i
<endsamples
;i
++) {
1065 operator_advance(&cptr
[0],vibval3
[i
]);
1066 opfuncs
[cptr
[0].op_state
](&cptr
[0]); //TomTom
1067 operator_output(&cptr
[0],0,tremval3
[i
]);
1068 Bit32s chanval
= cptr
[0].cval
*2;
1073 //Snare/Hihat (j=7), Cymbal (j=8)
1074 if ((op
[7].op_state
!= OF_TYPE_OFF
) || (op
[16].op_state
!= OF_TYPE_OFF
) ||
1075 (op
[17].op_state
!= OF_TYPE_OFF
)) {
1077 if ((cptr
[0].vibrato
) && (cptr
[0].op_state
!= OF_TYPE_OFF
)) {
1078 vibval1
= vibval_var1
;
1079 for (i
=0;i
<endsamples
;i
++)
1080 vibval1
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[0].freq_high
/8)*FIXEDPT
*VIBFAC
);
1081 } else vibval1
= vibval_const
;
1082 if ((cptr
[9].vibrato
) && (cptr
[9].op_state
== OF_TYPE_OFF
)) {
1083 vibval2
= vibval_var2
;
1084 for (i
=0;i
<endsamples
;i
++)
1085 vibval2
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[9].freq_high
/8)*FIXEDPT
*VIBFAC
);
1086 } else vibval2
= vibval_const
;
1088 if (cptr
[0].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1089 else tremval1
= tremval_const
;
1090 if (cptr
[9].tremolo
) tremval2
= trem_lut
; // tremolo enabled, use table
1091 else tremval2
= tremval_const
;
1094 if ((cptr
[9].vibrato
) && (cptr
[9].op_state
== OF_TYPE_OFF
)) {
1095 vibval4
= vibval_var2
;
1096 for (i
=0;i
<endsamples
;i
++)
1097 vibval4
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[9].freq_high
/8)*FIXEDPT
*VIBFAC
);
1098 } else vibval4
= vibval_const
;
1100 if (cptr
[9].tremolo
) tremval4
= trem_lut
; // tremolo enabled, use table
1101 else tremval4
= tremval_const
;
1103 // calculate channel output
1104 for (i
=0;i
<endsamples
;i
++) {
1105 operator_advance_drums(&op
[7],vibval1
[i
],&op
[7+9],vibval2
[i
],&op
[8+9],vibval4
[i
]);
1107 opfuncs
[op
[7].op_state
](&op
[7]); //Hihat
1108 operator_output(&op
[7],0,tremval1
[i
]);
1110 opfuncs
[op
[7+9].op_state
](&op
[7+9]); //Snare
1111 operator_output(&op
[7+9],0,tremval2
[i
]);
1113 opfuncs
[op
[8+9].op_state
](&op
[8+9]); //Cymbal
1114 operator_output(&op
[8+9],0,tremval4
[i
]);
1116 Bit32s chanval
= (op
[7].cval
+ op
[7+9].cval
+ op
[8+9].cval
)*2;
1122 Bitu max_channel
= NUM_CHANNELS
;
1123 #if defined(OPLTYPE_IS_OPL3)
1124 if ((adlibreg
[0x105]&1)==0) max_channel
= NUM_CHANNELS
/2;
1126 for (Bits cur_ch
=max_channel
-1; cur_ch
>=0; cur_ch
--) {
1127 // skip drum/percussion operators
1128 if ((adlibreg
[ARC_PERC_MODE
]&0x20) && (cur_ch
>= 6) && (cur_ch
< 9)) continue;
1131 #if defined(OPLTYPE_IS_OPL3)
1135 cptr
= &op
[cur_ch
+9]; // second set is operator18-operator35
1136 k
+= (-9+256); // second set uses registers 0x100 onwards
1138 // check if this operator is part of a 4-op
1139 if ((adlibreg
[0x105]&1) && cptr
->is_4op_attached
) continue;
1145 if (adlibreg
[ARC_FEEDBACK
+k
]&1) {
1146 #if defined(OPLTYPE_IS_OPL3)
1147 if ((adlibreg
[0x105]&1) && cptr
->is_4op
) {
1148 if (adlibreg
[ARC_FEEDBACK
+k
+3]&1) {
1149 // AM-AM-style synthesis (op1[fb] + (op2 * op3) + op4)
1150 if (cptr
[0].op_state
!= OF_TYPE_OFF
) {
1151 if (cptr
[0].vibrato
) {
1152 vibval1
= vibval_var1
;
1153 for (i
=0;i
<endsamples
;i
++)
1154 vibval1
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[0].freq_high
/8)*FIXEDPT
*VIBFAC
);
1155 } else vibval1
= vibval_const
;
1156 if (cptr
[0].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1157 else tremval1
= tremval_const
;
1159 // calculate channel output
1160 for (i
=0;i
<endsamples
;i
++) {
1161 operator_advance(&cptr
[0],vibval1
[i
]);
1162 opfuncs
[cptr
[0].op_state
](&cptr
[0]);
1163 operator_output(&cptr
[0],(cptr
[0].lastcval
+cptr
[0].cval
)*cptr
[0].mfbi
/2,tremval1
[i
]);
1165 Bit32s chanval
= cptr
[0].cval
;
1170 if ((cptr
[3].op_state
!= OF_TYPE_OFF
) || (cptr
[9].op_state
!= OF_TYPE_OFF
)) {
1171 if ((cptr
[9].vibrato
) && (cptr
[9].op_state
!= OF_TYPE_OFF
)) {
1172 vibval1
= vibval_var1
;
1173 for (i
=0;i
<endsamples
;i
++)
1174 vibval1
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[9].freq_high
/8)*FIXEDPT
*VIBFAC
);
1175 } else vibval1
= vibval_const
;
1176 if (cptr
[9].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1177 else tremval1
= tremval_const
;
1178 if (cptr
[3].tremolo
) tremval2
= trem_lut
; // tremolo enabled, use table
1179 else tremval2
= tremval_const
;
1181 // calculate channel output
1182 for (i
=0;i
<endsamples
;i
++) {
1183 operator_advance(&cptr
[9],vibval1
[i
]);
1184 opfuncs
[cptr
[9].op_state
](&cptr
[9]);
1185 operator_output(&cptr
[9],0,tremval1
[i
]);
1187 operator_advance(&cptr
[3],0);
1188 opfuncs
[cptr
[3].op_state
](&cptr
[3]);
1189 operator_output(&cptr
[3],cptr
[9].cval
*FIXEDPT
,tremval2
[i
]);
1191 Bit32s chanval
= cptr
[3].cval
;
1196 if (cptr
[3+9].op_state
!= OF_TYPE_OFF
) {
1197 if (cptr
[3+9].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1198 else tremval1
= tremval_const
;
1200 // calculate channel output
1201 for (i
=0;i
<endsamples
;i
++) {
1202 operator_advance(&cptr
[3+9],0);
1203 opfuncs
[cptr
[3+9].op_state
](&cptr
[3+9]);
1204 operator_output(&cptr
[3+9],0,tremval1
[i
]);
1206 Bit32s chanval
= cptr
[3+9].cval
;
1211 // AM-FM-style synthesis (op1[fb] + (op2 * op3 * op4))
1212 if (cptr
[0].op_state
!= OF_TYPE_OFF
) {
1213 if (cptr
[0].vibrato
) {
1214 vibval1
= vibval_var1
;
1215 for (i
=0;i
<endsamples
;i
++)
1216 vibval1
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[0].freq_high
/8)*FIXEDPT
*VIBFAC
);
1217 } else vibval1
= vibval_const
;
1218 if (cptr
[0].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1219 else tremval1
= tremval_const
;
1221 // calculate channel output
1222 for (i
=0;i
<endsamples
;i
++) {
1223 operator_advance(&cptr
[0],vibval1
[i
]);
1224 opfuncs
[cptr
[0].op_state
](&cptr
[0]);
1225 operator_output(&cptr
[0],(cptr
[0].lastcval
+cptr
[0].cval
)*cptr
[0].mfbi
/2,tremval1
[i
]);
1227 Bit32s chanval
= cptr
[0].cval
;
1232 if ((cptr
[9].op_state
!= OF_TYPE_OFF
) || (cptr
[3].op_state
!= OF_TYPE_OFF
) || (cptr
[3+9].op_state
!= OF_TYPE_OFF
)) {
1233 if ((cptr
[9].vibrato
) && (cptr
[9].op_state
!= OF_TYPE_OFF
)) {
1234 vibval1
= vibval_var1
;
1235 for (i
=0;i
<endsamples
;i
++)
1236 vibval1
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[9].freq_high
/8)*FIXEDPT
*VIBFAC
);
1237 } else vibval1
= vibval_const
;
1238 if (cptr
[9].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1239 else tremval1
= tremval_const
;
1240 if (cptr
[3].tremolo
) tremval2
= trem_lut
; // tremolo enabled, use table
1241 else tremval2
= tremval_const
;
1242 if (cptr
[3+9].tremolo
) tremval3
= trem_lut
; // tremolo enabled, use table
1243 else tremval3
= tremval_const
;
1245 // calculate channel output
1246 for (i
=0;i
<endsamples
;i
++) {
1247 operator_advance(&cptr
[9],vibval1
[i
]);
1248 opfuncs
[cptr
[9].op_state
](&cptr
[9]);
1249 operator_output(&cptr
[9],0,tremval1
[i
]);
1251 operator_advance(&cptr
[3],0);
1252 opfuncs
[cptr
[3].op_state
](&cptr
[3]);
1253 operator_output(&cptr
[3],cptr
[9].cval
*FIXEDPT
,tremval2
[i
]);
1255 operator_advance(&cptr
[3+9],0);
1256 opfuncs
[cptr
[3+9].op_state
](&cptr
[3+9]);
1257 operator_output(&cptr
[3+9],cptr
[3].cval
*FIXEDPT
,tremval3
[i
]);
1259 Bit32s chanval
= cptr
[3+9].cval
;
1267 // 2op additive synthesis
1268 if ((cptr
[9].op_state
== OF_TYPE_OFF
) && (cptr
[0].op_state
== OF_TYPE_OFF
)) continue;
1269 if ((cptr
[0].vibrato
) && (cptr
[0].op_state
!= OF_TYPE_OFF
)) {
1270 vibval1
= vibval_var1
;
1271 for (i
=0;i
<endsamples
;i
++)
1272 vibval1
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[0].freq_high
/8)*FIXEDPT
*VIBFAC
);
1273 } else vibval1
= vibval_const
;
1274 if ((cptr
[9].vibrato
) && (cptr
[9].op_state
!= OF_TYPE_OFF
)) {
1275 vibval2
= vibval_var2
;
1276 for (i
=0;i
<endsamples
;i
++)
1277 vibval2
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[9].freq_high
/8)*FIXEDPT
*VIBFAC
);
1278 } else vibval2
= vibval_const
;
1279 if (cptr
[0].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1280 else tremval1
= tremval_const
;
1281 if (cptr
[9].tremolo
) tremval2
= trem_lut
; // tremolo enabled, use table
1282 else tremval2
= tremval_const
;
1284 // calculate channel output
1285 for (i
=0;i
<endsamples
;i
++) {
1287 operator_advance(&cptr
[0],vibval1
[i
]);
1288 opfuncs
[cptr
[0].op_state
](&cptr
[0]);
1289 operator_output(&cptr
[0],(cptr
[0].lastcval
+cptr
[0].cval
)*cptr
[0].mfbi
/2,tremval1
[i
]);
1292 operator_advance(&cptr
[9],vibval2
[i
]);
1293 opfuncs
[cptr
[9].op_state
](&cptr
[9]);
1294 operator_output(&cptr
[9],0,tremval2
[i
]);
1296 Bit32s chanval
= cptr
[9].cval
+ cptr
[0].cval
;
1300 #if defined(OPLTYPE_IS_OPL3)
1301 if ((adlibreg
[0x105]&1) && cptr
->is_4op
) {
1302 if (adlibreg
[ARC_FEEDBACK
+k
+3]&1) {
1303 // FM-AM-style synthesis ((op1[fb] * op2) + (op3 * op4))
1304 if ((cptr
[0].op_state
!= OF_TYPE_OFF
) || (cptr
[9].op_state
!= OF_TYPE_OFF
)) {
1305 if ((cptr
[0].vibrato
) && (cptr
[0].op_state
!= OF_TYPE_OFF
)) {
1306 vibval1
= vibval_var1
;
1307 for (i
=0;i
<endsamples
;i
++)
1308 vibval1
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[0].freq_high
/8)*FIXEDPT
*VIBFAC
);
1309 } else vibval1
= vibval_const
;
1310 if ((cptr
[9].vibrato
) && (cptr
[9].op_state
!= OF_TYPE_OFF
)) {
1311 vibval2
= vibval_var2
;
1312 for (i
=0;i
<endsamples
;i
++)
1313 vibval2
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[9].freq_high
/8)*FIXEDPT
*VIBFAC
);
1314 } else vibval2
= vibval_const
;
1315 if (cptr
[0].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1316 else tremval1
= tremval_const
;
1317 if (cptr
[9].tremolo
) tremval2
= trem_lut
; // tremolo enabled, use table
1318 else tremval2
= tremval_const
;
1320 // calculate channel output
1321 for (i
=0;i
<endsamples
;i
++) {
1322 operator_advance(&cptr
[0],vibval1
[i
]);
1323 opfuncs
[cptr
[0].op_state
](&cptr
[0]);
1324 operator_output(&cptr
[0],(cptr
[0].lastcval
+cptr
[0].cval
)*cptr
[0].mfbi
/2,tremval1
[i
]);
1326 operator_advance(&cptr
[9],vibval2
[i
]);
1327 opfuncs
[cptr
[9].op_state
](&cptr
[9]);
1328 operator_output(&cptr
[9],cptr
[0].cval
*FIXEDPT
,tremval2
[i
]);
1330 Bit32s chanval
= cptr
[9].cval
;
1335 if ((cptr
[3].op_state
!= OF_TYPE_OFF
) || (cptr
[3+9].op_state
!= OF_TYPE_OFF
)) {
1336 if (cptr
[3].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1337 else tremval1
= tremval_const
;
1338 if (cptr
[3+9].tremolo
) tremval2
= trem_lut
; // tremolo enabled, use table
1339 else tremval2
= tremval_const
;
1341 // calculate channel output
1342 for (i
=0;i
<endsamples
;i
++) {
1343 operator_advance(&cptr
[3],0);
1344 opfuncs
[cptr
[3].op_state
](&cptr
[3]);
1345 operator_output(&cptr
[3],0,tremval1
[i
]);
1347 operator_advance(&cptr
[3+9],0);
1348 opfuncs
[cptr
[3+9].op_state
](&cptr
[3+9]);
1349 operator_output(&cptr
[3+9],cptr
[3].cval
*FIXEDPT
,tremval2
[i
]);
1351 Bit32s chanval
= cptr
[3+9].cval
;
1357 // FM-FM-style synthesis (op1[fb] * op2 * op3 * op4)
1358 if ((cptr
[0].op_state
!= OF_TYPE_OFF
) || (cptr
[9].op_state
!= OF_TYPE_OFF
) ||
1359 (cptr
[3].op_state
!= OF_TYPE_OFF
) || (cptr
[3+9].op_state
!= OF_TYPE_OFF
)) {
1360 if ((cptr
[0].vibrato
) && (cptr
[0].op_state
!= OF_TYPE_OFF
)) {
1361 vibval1
= vibval_var1
;
1362 for (i
=0;i
<endsamples
;i
++)
1363 vibval1
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[0].freq_high
/8)*FIXEDPT
*VIBFAC
);
1364 } else vibval1
= vibval_const
;
1365 if ((cptr
[9].vibrato
) && (cptr
[9].op_state
!= OF_TYPE_OFF
)) {
1366 vibval2
= vibval_var2
;
1367 for (i
=0;i
<endsamples
;i
++)
1368 vibval2
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[9].freq_high
/8)*FIXEDPT
*VIBFAC
);
1369 } else vibval2
= vibval_const
;
1370 if (cptr
[0].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1371 else tremval1
= tremval_const
;
1372 if (cptr
[9].tremolo
) tremval2
= trem_lut
; // tremolo enabled, use table
1373 else tremval2
= tremval_const
;
1374 if (cptr
[3].tremolo
) tremval3
= trem_lut
; // tremolo enabled, use table
1375 else tremval3
= tremval_const
;
1376 if (cptr
[3+9].tremolo
) tremval4
= trem_lut
; // tremolo enabled, use table
1377 else tremval4
= tremval_const
;
1379 // calculate channel output
1380 for (i
=0;i
<endsamples
;i
++) {
1381 operator_advance(&cptr
[0],vibval1
[i
]);
1382 opfuncs
[cptr
[0].op_state
](&cptr
[0]);
1383 operator_output(&cptr
[0],(cptr
[0].lastcval
+cptr
[0].cval
)*cptr
[0].mfbi
/2,tremval1
[i
]);
1385 operator_advance(&cptr
[9],vibval2
[i
]);
1386 opfuncs
[cptr
[9].op_state
](&cptr
[9]);
1387 operator_output(&cptr
[9],cptr
[0].cval
*FIXEDPT
,tremval2
[i
]);
1389 operator_advance(&cptr
[3],0);
1390 opfuncs
[cptr
[3].op_state
](&cptr
[3]);
1391 operator_output(&cptr
[3],cptr
[9].cval
*FIXEDPT
,tremval3
[i
]);
1393 operator_advance(&cptr
[3+9],0);
1394 opfuncs
[cptr
[3+9].op_state
](&cptr
[3+9]);
1395 operator_output(&cptr
[3+9],cptr
[3].cval
*FIXEDPT
,tremval4
[i
]);
1397 Bit32s chanval
= cptr
[3+9].cval
;
1405 // 2op frequency modulation
1406 if ((cptr
[9].op_state
== OF_TYPE_OFF
) && (cptr
[0].op_state
== OF_TYPE_OFF
)) continue;
1407 if ((cptr
[0].vibrato
) && (cptr
[0].op_state
!= OF_TYPE_OFF
)) {
1408 vibval1
= vibval_var1
;
1409 for (i
=0;i
<endsamples
;i
++)
1410 vibval1
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[0].freq_high
/8)*FIXEDPT
*VIBFAC
);
1411 } else vibval1
= vibval_const
;
1412 if ((cptr
[9].vibrato
) && (cptr
[9].op_state
!= OF_TYPE_OFF
)) {
1413 vibval2
= vibval_var2
;
1414 for (i
=0;i
<endsamples
;i
++)
1415 vibval2
[i
] = (Bit32s
)((vib_lut
[i
]*cptr
[9].freq_high
/8)*FIXEDPT
*VIBFAC
);
1416 } else vibval2
= vibval_const
;
1417 if (cptr
[0].tremolo
) tremval1
= trem_lut
; // tremolo enabled, use table
1418 else tremval1
= tremval_const
;
1419 if (cptr
[9].tremolo
) tremval2
= trem_lut
; // tremolo enabled, use table
1420 else tremval2
= tremval_const
;
1422 // calculate channel output
1423 for (i
=0;i
<endsamples
;i
++) {
1425 operator_advance(&cptr
[0],vibval1
[i
]);
1426 opfuncs
[cptr
[0].op_state
](&cptr
[0]);
1427 operator_output(&cptr
[0],(cptr
[0].lastcval
+cptr
[0].cval
)*cptr
[0].mfbi
/2,tremval1
[i
]);
1430 operator_advance(&cptr
[9],vibval2
[i
]);
1431 opfuncs
[cptr
[9].op_state
](&cptr
[9]);
1432 operator_output(&cptr
[9],cptr
[0].cval
*FIXEDPT
,tremval2
[i
]);
1434 Bit32s chanval
= cptr
[9].cval
;
1440 #if defined(OPLTYPE_IS_OPL3)
1441 if (adlibreg
[0x105]&1) {
1442 // convert to 16bit samples (stereo)
1443 for (i
=0;i
<endsamples
;i
++) {
1444 clipit16(outbufl
[i
],sndptr
++);
1445 clipit16(outbufr
[i
],sndptr
++);
1448 // convert to 16bit samples (mono)
1449 for (i
=0;i
<endsamples
;i
++) {
1450 clipit16(outbufl
[i
],sndptr
++);
1451 clipit16(outbufl
[i
],sndptr
++);
1455 // convert to 16bit samples
1456 for (i
=0;i
<endsamples
;i
++)
1457 clipit16(outbufl
[i
],sndptr
++);