JPC-RR r11.7
[jpcrr.git] / streamtools / opl.cpp
blob0d949fddf22610d93f5a6e213a4450451a060b65
1 /*
2 * Copyright (C) 2002-2009 The DOSBox Team
3 * Copyright (C) 2011 H. Ilari Liusvaara
4 * OPL2/OPL3 emulation library
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Originally based on ADLIBEMU.C, an AdLib/OPL2 emulation library by Ken Silverman
24 * Copyright (C) 1998-2001 Ken Silverman
25 * Ken Silverman's official web site: "http://www.advsys.net/ken"
29 * Modified 20th April 2011 by Ilari:
30 * - Replace static state with context.
31 * - Remove OPL2 support (OPL3 is superset anyway)
34 #ifndef OPL_INLINE
35 #define OPL_INLINE
36 #endif
38 #include <math.h>
39 #include <stdlib.h> // rand()
40 #include <string.h>
41 #include "opl.h"
43 // key scale level lookup table
44 static const fltype kslmul[4] = {
45 0.0, 0.5, 0.25, 1.0 // -> 0, 3, 1.5, 6 dB/oct
48 // frequency multiplicator lookup table
49 static const fltype frqmul_tab[16] = {
50 0.5,1,2,3,4,5,6,7,8,9,10,10,12,12,15,15
53 // map a channel number to the register offset of the modulator (=register base)
54 static const Bit8u modulatorbase[9] = {
55 0,1,2,
56 8,9,10,
57 16,17,18
60 // map a register base to a modulator operator number or operator number
61 static const Bit8u regbase2modop[44] = {
62 0,1,2,0,1,2,0,0,3,4,5,3,4,5,0,0,6,7,8,6,7,8, // first set
63 18,19,20,18,19,20,0,0,21,22,23,21,22,23,0,0,24,25,26,24,25,26 // second set
65 static const Bit8u regbase2op[44] = {
66 0,1,2,9,10,11,0,0,3,4,5,12,13,14,0,0,6,7,8,15,16,17, // first set
67 18,19,20,27,28,29,0,0,21,22,23,30,31,32,0,0,24,25,26,33,34,35 // second set
70 // start of the waveform
71 static Bit32u waveform[8] = {
72 WAVEPREC,
73 WAVEPREC>>1,
74 WAVEPREC,
75 (WAVEPREC*3)>>2,
78 (WAVEPREC*5)>>2,
79 WAVEPREC<<1
82 // length of the waveform as mask
83 static Bit32u wavemask[8] = {
84 WAVEPREC-1,
85 WAVEPREC-1,
86 (WAVEPREC>>1)-1,
87 (WAVEPREC>>1)-1,
88 WAVEPREC-1,
89 ((WAVEPREC*3)>>2)-1,
90 WAVEPREC>>1,
91 WAVEPREC-1
94 // where the first entry resides
95 static Bit32u wavestart[8] = {
97 WAVEPREC>>1,
99 WAVEPREC>>2,
103 WAVEPREC>>3
106 // envelope generator function constants
107 static fltype attackconst[4] = {
108 (fltype)(1/2.82624),
109 (fltype)(1/2.25280),
110 (fltype)(1/1.88416),
111 (fltype)(1/1.59744)
113 static fltype decrelconst[4] = {
114 (fltype)(1/39.28064),
115 (fltype)(1/31.41608),
116 (fltype)(1/26.17344),
117 (fltype)(1/22.44608)
121 void operator_advance(opl_context* ctx, op_type* op_pt, Bit32s vib) {
122 op_pt->wfpos = op_pt->tcount; // waveform position
124 // advance waveform time
125 op_pt->tcount += op_pt->tinc;
126 op_pt->tcount += (Bit32s)(op_pt->tinc)*vib/FIXEDPT;
128 op_pt->generator_pos += (ctx->generator_add);
131 void operator_advance_drums(opl_context* ctx, op_type* op_pt1, Bit32s vib1, op_type* op_pt2, Bit32s vib2, op_type* op_pt3, Bit32s vib3) {
132 Bit32u c1 = op_pt1->tcount/FIXEDPT;
133 Bit32u c3 = op_pt3->tcount/FIXEDPT;
134 Bit32u phasebit = (((c1 & 0x88) ^ ((c1<<5) & 0x80)) | ((c3 ^ (c3<<2)) & 0x20)) ? 0x02 : 0x00;
136 Bit32u noisebit = rand()&1;
138 Bit32u snare_phase_bit = (((Bitu)((op_pt1->tcount/FIXEDPT) / 0x100))&1);
140 //Hihat
141 Bit32u inttm = (phasebit<<8) | (0x34<<(phasebit ^ (noisebit<<1)));
142 op_pt1->wfpos = inttm*FIXEDPT; // waveform position
143 // advance waveform time
144 op_pt1->tcount += op_pt1->tinc;
145 op_pt1->tcount += (Bit32s)(op_pt1->tinc)*vib1/FIXEDPT;
146 op_pt1->generator_pos += (ctx->generator_add);
148 //Snare
149 inttm = ((1+snare_phase_bit) ^ noisebit)<<8;
150 op_pt2->wfpos = inttm*FIXEDPT; // waveform position
151 // advance waveform time
152 op_pt2->tcount += op_pt2->tinc;
153 op_pt2->tcount += (Bit32s)(op_pt2->tinc)*vib2/FIXEDPT;
154 op_pt2->generator_pos += (ctx->generator_add);
156 //Cymbal
157 inttm = (1+phasebit)<<8;
158 op_pt3->wfpos = inttm*FIXEDPT; // waveform position
159 // advance waveform time
160 op_pt3->tcount += op_pt3->tinc;
161 op_pt3->tcount += (Bit32s)(op_pt3->tinc)*vib3/FIXEDPT;
162 op_pt3->generator_pos += (ctx->generator_add);
166 // output level is sustained, mode changes only when operator is turned off (->release)
167 // or when the keep-sustained bit is turned off (->sustain_nokeep)
168 void operator_output(opl_context* ctx, op_type* op_pt, Bit32s modulator, Bit32s trem) {
169 if (op_pt->op_state != OF_TYPE_OFF) {
170 op_pt->lastcval = op_pt->cval;
171 Bit32u i = (Bit32u)((op_pt->wfpos+modulator)/FIXEDPT);
173 // wform: -16384 to 16383 (0x4000)
174 // trem : 32768 to 65535 (0x10000)
175 // step_amp: 0.0 to 1.0
176 // vol : 1/2^14 to 1/2^29 (/0x4000; /1../0x8000)
178 op_pt->cval = (Bit32s)(op_pt->step_amp*op_pt->vol*op_pt->cur_wform[i&op_pt->cur_wmask]*trem/16.0);
183 // no action, operator is off
184 void operator_off(opl_context* ctx, op_type* /*op_pt*/) {
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)
189 void operator_sustain(opl_context* ctx, op_type* op_pt) {
190 Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
191 for (Bit32u ct=0; ct<num_steps_add; ct++) {
192 op_pt->cur_env_step++;
194 op_pt->generator_pos -= num_steps_add*FIXEDPT;
197 // operator in release mode, if output level reaches zero the operator is turned off
198 void operator_release(opl_context* ctx, op_type* op_pt) {
199 // ??? boundary?
200 if (op_pt->amp > 0.00000001) {
201 // release phase
202 op_pt->amp *= op_pt->releasemul;
205 Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
206 for (Bit32u ct=0; ct<num_steps_add; ct++) {
207 op_pt->cur_env_step++; // sample counter
208 if ((op_pt->cur_env_step & op_pt->env_step_r)==0) {
209 if (op_pt->amp <= 0.00000001) {
210 // release phase finished, turn off this operator
211 op_pt->amp = 0.0;
212 if (op_pt->op_state == OF_TYPE_REL) {
213 op_pt->op_state = OF_TYPE_OFF;
216 op_pt->step_amp = op_pt->amp;
219 op_pt->generator_pos -= num_steps_add*FIXEDPT;
222 // operator in decay mode, if sustain level is reached the output level is either
223 // kept (sustain level keep enabled) or the operator is switched into release mode
224 void operator_decay(opl_context* ctx, op_type* op_pt) {
225 if (op_pt->amp > op_pt->sustain_level) {
226 // decay phase
227 op_pt->amp *= op_pt->decaymul;
230 Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
231 for (Bit32u ct=0; ct<num_steps_add; ct++) {
232 op_pt->cur_env_step++;
233 if ((op_pt->cur_env_step & op_pt->env_step_d)==0) {
234 if (op_pt->amp <= op_pt->sustain_level) {
235 // decay phase finished, sustain level reached
236 if (op_pt->sus_keep) {
237 // keep sustain level (until turned off)
238 op_pt->op_state = OF_TYPE_SUS;
239 op_pt->amp = op_pt->sustain_level;
240 } else {
241 // next: release phase
242 op_pt->op_state = OF_TYPE_SUS_NOKEEP;
245 op_pt->step_amp = op_pt->amp;
248 op_pt->generator_pos -= num_steps_add*FIXEDPT;
251 // operator in attack mode, if full output level is reached,
252 // the operator is switched into decay mode
253 void operator_attack(opl_context* ctx, op_type* op_pt) {
254 op_pt->amp = ((op_pt->a3*op_pt->amp + op_pt->a2)*op_pt->amp + op_pt->a1)*op_pt->amp + op_pt->a0;
256 Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT; // number of (standardized) samples
257 for (Bit32u ct=0; ct<num_steps_add; ct++) {
258 op_pt->cur_env_step++; // next sample
259 if ((op_pt->cur_env_step & op_pt->env_step_a)==0) { // check if next step already reached
260 if (op_pt->amp > 1.0) {
261 // attack phase finished, next: decay
262 op_pt->op_state = OF_TYPE_DEC;
263 op_pt->amp = 1.0;
264 op_pt->step_amp = 1.0;
266 op_pt->step_skip_pos_a <<= 1;
267 if (op_pt->step_skip_pos_a==0) op_pt->step_skip_pos_a = 1;
268 if (op_pt->step_skip_pos_a & op_pt->env_step_skip_a) { // check if required to skip next step
269 op_pt->step_amp = op_pt->amp;
273 op_pt->generator_pos -= num_steps_add*FIXEDPT;
277 typedef void (*optype_fptr)(opl_context* ctx, op_type*);
279 optype_fptr opfuncs[6] = {
280 operator_attack,
281 operator_decay,
282 operator_release,
283 operator_sustain, // sustain phase (keeping level)
284 operator_release, // sustain_nokeep phase (release-style)
285 operator_off
288 void change_attackrate(opl_context* ctx, Bitu regbase, op_type* op_pt) {
289 Bits attackrate = (ctx->adlibreg)[ARC_ATTR_DECR+regbase]>>4;
290 if (attackrate) {
291 fltype f = (fltype)(pow(FL2,(fltype)attackrate+(op_pt->toff>>2)-1)*attackconst[op_pt->toff&3]*(ctx->recipsamp));
292 // attack rate coefficients
293 op_pt->a0 = (fltype)(0.0377*f);
294 op_pt->a1 = (fltype)(10.73*f+1);
295 op_pt->a2 = (fltype)(-17.57*f);
296 op_pt->a3 = (fltype)(7.42*f);
298 Bits step_skip = attackrate*4 + op_pt->toff;
299 Bits steps = step_skip >> 2;
300 op_pt->env_step_a = (1<<(steps<=12?12-steps:0))-1;
302 Bits step_num = (step_skip<=48)?(4-(step_skip&3)):0;
303 static Bit8u step_skip_mask[5] = {0xff, 0xfe, 0xee, 0xba, 0xaa};
304 op_pt->env_step_skip_a = step_skip_mask[step_num];
306 if (step_skip>=60) {
307 op_pt->a0 = (fltype)(2.0); // something that triggers an immediate transition to amp:=1.0
308 op_pt->a1 = (fltype)(0.0);
309 op_pt->a2 = (fltype)(0.0);
310 op_pt->a3 = (fltype)(0.0);
312 } else {
313 // attack disabled
314 op_pt->a0 = 0.0;
315 op_pt->a1 = 1.0;
316 op_pt->a2 = 0.0;
317 op_pt->a3 = 0.0;
318 op_pt->env_step_a = 0;
319 op_pt->env_step_skip_a = 0;
323 void change_decayrate(opl_context* ctx, Bitu regbase, op_type* op_pt) {
324 Bits decayrate = (ctx->adlibreg)[ARC_ATTR_DECR+regbase]&15;
325 // decaymul should be 1.0 when decayrate==0
326 if (decayrate) {
327 fltype f = (fltype)(-7.4493*decrelconst[op_pt->toff&3]*(ctx->recipsamp));
328 op_pt->decaymul = (fltype)(pow(FL2,f*pow(FL2,(fltype)(decayrate+(op_pt->toff>>2)))));
329 Bits steps = (decayrate*4 + op_pt->toff) >> 2;
330 op_pt->env_step_d = (1<<(steps<=12?12-steps:0))-1;
331 } else {
332 op_pt->decaymul = 1.0;
333 op_pt->env_step_d = 0;
337 void change_releaserate(opl_context* ctx, Bitu regbase, op_type* op_pt) {
338 Bits releaserate = (ctx->adlibreg)[ARC_SUSL_RELR+regbase]&15;
339 // releasemul should be 1.0 when releaserate==0
340 if (releaserate) {
341 fltype f = (fltype)(-7.4493*decrelconst[op_pt->toff&3]*(ctx->recipsamp));
342 op_pt->releasemul = (fltype)(pow(FL2,f*pow(FL2,(fltype)(releaserate+(op_pt->toff>>2)))));
343 Bits steps = (releaserate*4 + op_pt->toff) >> 2;
344 op_pt->env_step_r = (1<<(steps<=12?12-steps:0))-1;
345 } else {
346 op_pt->releasemul = 1.0;
347 op_pt->env_step_r = 0;
351 void change_sustainlevel(opl_context* ctx, Bitu regbase, op_type* op_pt) {
352 Bits sustainlevel = (ctx->adlibreg)[ARC_SUSL_RELR+regbase]>>4;
353 // sustainlevel should be 0.0 when sustainlevel==15 (max)
354 if (sustainlevel<15) {
355 op_pt->sustain_level = (fltype)(pow(FL2,(fltype)sustainlevel * (-FL05)));
356 } else {
357 op_pt->sustain_level = 0.0;
361 void change_waveform(opl_context* ctx, Bitu regbase, op_type* op_pt) {
362 if (regbase>=ARC_SECONDSET) regbase -= (ARC_SECONDSET-22); // second set starts at 22
363 // waveform selection
364 op_pt->cur_wmask = wavemask[(ctx->wave_sel)[regbase]];
365 op_pt->cur_wform = &(ctx->wavtable)[waveform[(ctx->wave_sel)[regbase]]];
366 // (might need to be adapted to waveform type here...)
369 void change_keepsustain(opl_context* ctx, Bitu regbase, op_type* op_pt) {
370 op_pt->sus_keep = ((ctx->adlibreg)[ARC_TVS_KSR_MUL+regbase]&0x20)>0;
371 if (op_pt->op_state==OF_TYPE_SUS) {
372 if (!op_pt->sus_keep) op_pt->op_state = OF_TYPE_SUS_NOKEEP;
373 } else if (op_pt->op_state==OF_TYPE_SUS_NOKEEP) {
374 if (op_pt->sus_keep) op_pt->op_state = OF_TYPE_SUS;
378 // enable/disable vibrato/tremolo LFO effects
379 void change_vibrato(opl_context* ctx, Bitu regbase, op_type* op_pt) {
380 op_pt->vibrato = ((ctx->adlibreg)[ARC_TVS_KSR_MUL+regbase]&0x40)!=0;
381 op_pt->tremolo = ((ctx->adlibreg)[ARC_TVS_KSR_MUL+regbase]&0x80)!=0;
384 // change amount of self-feedback
385 void change_feedback(opl_context* ctx, Bitu chanbase, op_type* op_pt) {
386 Bits feedback = (ctx->adlibreg)[ARC_FEEDBACK+chanbase]&14;
387 if (feedback) op_pt->mfbi = (Bit32s)(pow(FL2,(fltype)((feedback>>1)+8)));
388 else op_pt->mfbi = 0;
391 void change_frequency(opl_context* ctx, Bitu chanbase, Bitu regbase, op_type* op_pt) {
392 // frequency
393 Bit32u frn = ((((Bit32u)(ctx->adlibreg)[ARC_KON_BNUM+chanbase])&3)<<8) + (Bit32u)(ctx->adlibreg)[ARC_FREQ_NUM+chanbase];
394 // block number/octave
395 Bit32u oct = ((((Bit32u)(ctx->adlibreg)[ARC_KON_BNUM+chanbase])>>2)&7);
396 op_pt->freq_high = (Bit32s)((frn>>7)&7);
398 // keysplit
399 Bit32u note_sel = ((ctx->adlibreg)[8]>>6)&1;
400 op_pt->toff = ((frn>>9)&(note_sel^1)) | ((frn>>8)&note_sel);
401 op_pt->toff += (oct<<1);
403 // envelope scaling (KSR)
404 if (!((ctx->adlibreg)[ARC_TVS_KSR_MUL+regbase]&0x10)) op_pt->toff >>= 2;
406 // 20+a0+b0:
407 op_pt->tinc = (Bit32u)((((fltype)(frn<<oct))*(ctx->frqmul)[(ctx->adlibreg)[ARC_TVS_KSR_MUL+regbase]&15]));
408 // 40+a0+b0:
409 fltype vol_in = (fltype)((fltype)((ctx->adlibreg)[ARC_KSL_OUTLEV+regbase]&63) +
410 kslmul[(ctx->adlibreg)[ARC_KSL_OUTLEV+regbase]>>6]*(ctx->kslev)[oct][frn>>6]);
411 op_pt->vol = (fltype)(pow(FL2,(fltype)(vol_in * -0.125 - 14)));
413 // operator frequency changed, care about features that depend on it
414 change_attackrate(ctx, regbase,op_pt);
415 change_decayrate(ctx, regbase,op_pt);
416 change_releaserate(ctx, regbase,op_pt);
419 void enable_operator(opl_context* ctx, Bitu regbase, op_type* op_pt, Bit32u act_type) {
420 // check if this is really an off-on transition
421 if (op_pt->act_state == OP_ACT_OFF) {
422 Bits wselbase = regbase;
423 if (wselbase>=ARC_SECONDSET) wselbase -= (ARC_SECONDSET-22); // second set starts at 22
425 op_pt->tcount = wavestart[(ctx->wave_sel)[wselbase]]*FIXEDPT;
427 // start with attack mode
428 op_pt->op_state = OF_TYPE_ATT;
429 op_pt->act_state |= act_type;
433 void disable_operator(opl_context* ctx, op_type* op_pt, Bit32u act_type) {
434 // check if this is really an on-off transition
435 if (op_pt->act_state != OP_ACT_OFF) {
436 op_pt->act_state &= (~act_type);
437 if (op_pt->act_state == OP_ACT_OFF) {
438 if (op_pt->op_state != OF_TYPE_OFF) op_pt->op_state = OF_TYPE_REL;
443 void adlib_init(opl_context* ctx, Bit32u samplerate) {
444 Bits i, j, oct;
446 //Clear the entiere state.
447 memset((void *)ctx,0,sizeof(*ctx));
449 (ctx->int_samplerate) = samplerate;
451 (ctx->generator_add) = (Bit32u)(INTFREQU*FIXEDPT/(ctx->int_samplerate));
453 for (i=0;i<MAXOPERATORS;i++) {
454 (ctx->op)[i].op_state = OF_TYPE_OFF;
455 (ctx->op)[i].act_state = OP_ACT_OFF;
456 (ctx->op)[i].amp = 0.0;
457 (ctx->op)[i].step_amp = 0.0;
458 (ctx->op)[i].vol = 0.0;
459 (ctx->op)[i].tcount = 0;
460 (ctx->op)[i].tinc = 0;
461 (ctx->op)[i].toff = 0;
462 (ctx->op)[i].cur_wmask = wavemask[0];
463 (ctx->op)[i].cur_wform = &(ctx->wavtable)[waveform[0]];
464 (ctx->op)[i].freq_high = 0;
466 (ctx->op)[i].generator_pos = 0;
467 (ctx->op)[i].cur_env_step = 0;
468 (ctx->op)[i].env_step_a = 0;
469 (ctx->op)[i].env_step_d = 0;
470 (ctx->op)[i].env_step_r = 0;
471 (ctx->op)[i].step_skip_pos_a = 0;
472 (ctx->op)[i].env_step_skip_a = 0;
474 (ctx->op)[i].is_4op = false;
475 (ctx->op)[i].is_4op_attached = false;
476 (ctx->op)[i].left_pan = 1;
477 (ctx->op)[i].right_pan = 1;
480 (ctx->recipsamp) = 1.0 / (fltype)(ctx->int_samplerate);
481 for (i=15;i>=0;i--) {
482 (ctx->frqmul)[i] = (fltype)(frqmul_tab[i]*INTFREQU/(fltype)WAVEPREC*(fltype)FIXEDPT*(ctx->recipsamp));
485 (ctx->status) = 0;
486 (ctx->opl_index) = 0;
489 // create vibrato table
490 (ctx->vib_table)[0] = 8;
491 (ctx->vib_table)[1] = 4;
492 (ctx->vib_table)[2] = 0;
493 (ctx->vib_table)[3] = -4;
494 for (i=4; i<VIBTAB_SIZE; i++) (ctx->vib_table)[i] = (ctx->vib_table)[i-4]*-1;
496 // vibrato at ~6.1 ?? (opl3 docs say 6.1, opl4 docs say 6.0, y8950 docs say 6.4)
497 (ctx->vibtab_add) = static_cast<Bit32u>(VIBTAB_SIZE*FIXEDPT_LFO/8192*INTFREQU/(ctx->int_samplerate));
498 (ctx->vibtab_pos) = 0;
500 for (i=0; i<BLOCKBUF_SIZE; i++) (ctx->vibval_const)[i] = 0;
503 // create tremolo table
504 Bit32s trem_table_int[TREMTAB_SIZE];
505 for (i=0; i<14; i++) trem_table_int[i] = i-13; // upwards (13 to 26 -> -0.5/6 to 0)
506 for (i=14; i<41; i++) trem_table_int[i] = -i+14; // downwards (26 to 0 -> 0 to -1/6)
507 for (i=41; i<53; i++) trem_table_int[i] = i-40-26; // upwards (1 to 12 -> -1/6 to -0.5/6)
509 for (i=0; i<TREMTAB_SIZE; i++) {
510 // 0.0 .. -26/26*4.8/6 == [0.0 .. -0.8], 4/53 steps == [1 .. 0.57]
511 fltype trem_val1=(fltype)(((fltype)trem_table_int[i])*4.8/26.0/6.0); // 4.8db
512 fltype trem_val2=(fltype)((fltype)((Bit32s)(trem_table_int[i]/4))*1.2/6.0/6.0); // 1.2db (larger stepping)
514 (ctx->trem_table)[i] = (Bit32s)(pow(FL2,trem_val1)*FIXEDPT);
515 (ctx->trem_table)[TREMTAB_SIZE+i] = (Bit32s)(pow(FL2,trem_val2)*FIXEDPT);
518 // tremolo at 3.7hz
519 (ctx->tremtab_add) = (Bit32u)((fltype)TREMTAB_SIZE * TREM_FREQ * FIXEDPT_LFO / (fltype)(ctx->int_samplerate));
520 (ctx->tremtab_pos) = 0;
522 for (i=0; i<BLOCKBUF_SIZE; i++) (ctx->tremval_const)[i] = FIXEDPT;
525 static Bitu initfirstime = 0;
526 if (!initfirstime) {
527 initfirstime = 1;
529 // create waveform tables
530 for (i=0;i<(WAVEPREC>>1);i++) {
531 (ctx->wavtable)[(i<<1) +WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<1) )*PI*2/WAVEPREC));
532 (ctx->wavtable)[(i<<1)+1+WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<1)+1)*PI*2/WAVEPREC));
533 (ctx->wavtable)[i] = (ctx->wavtable)[(i<<1) +WAVEPREC];
534 // alternative: (zero-less)
535 /* wavtable[(i<<1) +WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<2)+1)*PI/WAVEPREC));
536 wavtable[(i<<1)+1+WAVEPREC] = (Bit16s)(16384*sin((fltype)((i<<2)+3)*PI/WAVEPREC));
537 wavtable[i] = wavtable[(i<<1)-1+WAVEPREC]; */
539 for (i=0;i<(WAVEPREC>>3);i++) {
540 (ctx->wavtable)[i+(WAVEPREC<<1)] = (ctx->wavtable)[i+(WAVEPREC>>3)]-16384;
541 (ctx->wavtable)[i+((WAVEPREC*17)>>3)] = (ctx->wavtable)[i+(WAVEPREC>>2)]+16384;
544 // key scale level table verified ([table in book]*8/3)
545 (ctx->kslev)[7][0] = 0; (ctx->kslev)[7][1] = 24; (ctx->kslev)[7][2] = 32; (ctx->kslev)[7][3] = 37;
546 (ctx->kslev)[7][4] = 40; (ctx->kslev)[7][5] = 43; (ctx->kslev)[7][6] = 45; (ctx->kslev)[7][7] = 47;
547 (ctx->kslev)[7][8] = 48;
548 for (i=9;i<16;i++) (ctx->kslev)[7][i] = (Bit8u)(i+41);
549 for (j=6;j>=0;j--) {
550 for (i=0;i<16;i++) {
551 oct = (Bits)(ctx->kslev)[j+1][i]-8;
552 if (oct < 0) oct = 0;
553 (ctx->kslev)[j][i] = (Bit8u)oct;
562 void adlib_write(opl_context* ctx, Bitu idx, Bit8u val) {
563 Bit32u second_set = idx&0x100;
564 (ctx->adlibreg)[idx] = val;
566 switch (idx&0xf0) {
567 case ARC_CONTROL:
568 // here we check for the second set registers, too:
569 switch (idx) {
570 case 0x02: // timer1 counter
571 case 0x03: // timer2 counter
572 break;
573 case 0x04:
574 // IRQ reset, timer mask/start
575 if (val&0x80) {
576 // clear IRQ bits in status register
577 (ctx->status) &= ~0x60;
578 } else {
579 (ctx->status) = 0;
581 break;
582 case 0x04|ARC_SECONDSET:
583 // 4op enable/disable switches for each possible channel
584 (ctx->op)[0].is_4op = (val&1)>0;
585 (ctx->op)[3].is_4op_attached = (ctx->op)[0].is_4op;
586 (ctx->op)[1].is_4op = (val&2)>0;
587 (ctx->op)[4].is_4op_attached = (ctx->op)[1].is_4op;
588 (ctx->op)[2].is_4op = (val&4)>0;
589 (ctx->op)[5].is_4op_attached = (ctx->op)[2].is_4op;
590 (ctx->op)[18].is_4op = (val&8)>0;
591 (ctx->op)[21].is_4op_attached = (ctx->op)[18].is_4op;
592 (ctx->op)[19].is_4op = (val&16)>0;
593 (ctx->op)[22].is_4op_attached = (ctx->op)[19].is_4op;
594 (ctx->op)[20].is_4op = (val&32)>0;
595 (ctx->op)[23].is_4op_attached = (ctx->op)[20].is_4op;
596 break;
597 case 0x05|ARC_SECONDSET:
598 break;
599 case 0x08:
600 // CSW, note select
601 break;
602 default:
603 break;
605 break;
606 case ARC_TVS_KSR_MUL:
607 case ARC_TVS_KSR_MUL+0x10: {
608 // tremolo/vibrato/sustain keeping enabled; key scale rate; frequency multiplication
609 int num = idx&7;
610 Bitu base = (idx-ARC_TVS_KSR_MUL)&0xff;
611 if ((num<6) && (base<22)) {
612 Bitu modop = regbase2modop[second_set?(base+22):base];
613 Bitu regbase = base+second_set;
614 Bitu chanbase = second_set?(modop-18+ARC_SECONDSET):modop;
616 // change tremolo/vibrato and sustain keeping of this operator
617 op_type* op_ptr = &(ctx->op)[modop+((num<3) ? 0 : 9)];
618 change_keepsustain(ctx, regbase,op_ptr);
619 change_vibrato(ctx, regbase,op_ptr);
621 // change frequency calculations of this operator as
622 // key scale rate and frequency multiplicator can be changed
623 if (((ctx->adlibreg)[0x105]&1) && ((ctx->op)[modop].is_4op_attached)) {
624 // operator uses frequency of channel
625 change_frequency(ctx, chanbase-3,regbase,op_ptr);
626 } else {
627 change_frequency(ctx, chanbase,regbase,op_ptr);
631 break;
632 case ARC_KSL_OUTLEV:
633 case ARC_KSL_OUTLEV+0x10: {
634 // key scale level; output rate
635 int num = idx&7;
636 Bitu base = (idx-ARC_KSL_OUTLEV)&0xff;
637 if ((num<6) && (base<22)) {
638 Bitu modop = regbase2modop[second_set?(base+22):base];
639 Bitu chanbase = second_set?(modop-18+ARC_SECONDSET):modop;
641 // change frequency calculations of this operator as
642 // key scale level and output rate can be changed
643 op_type* op_ptr = &(ctx->op)[modop+((num<3) ? 0 : 9)];
644 Bitu regbase = base+second_set;
645 if (((ctx->adlibreg)[0x105]&1) && ((ctx->op)[modop].is_4op_attached)) {
646 // operator uses frequency of channel
647 change_frequency(ctx, chanbase-3,regbase,op_ptr);
648 } else {
649 change_frequency(ctx, chanbase,regbase,op_ptr);
653 break;
654 case ARC_ATTR_DECR:
655 case ARC_ATTR_DECR+0x10: {
656 // attack/decay rates
657 int num = idx&7;
658 Bitu base = (idx-ARC_ATTR_DECR)&0xff;
659 if ((num<6) && (base<22)) {
660 Bitu regbase = base+second_set;
662 // change attack rate and decay rate of this operator
663 op_type* op_ptr = &(ctx->op)[regbase2op[second_set?(base+22):base]];
664 change_attackrate(ctx, regbase,op_ptr);
665 change_decayrate(ctx, regbase,op_ptr);
668 break;
669 case ARC_SUSL_RELR:
670 case ARC_SUSL_RELR+0x10: {
671 // sustain level; release rate
672 int num = idx&7;
673 Bitu base = (idx-ARC_SUSL_RELR)&0xff;
674 if ((num<6) && (base<22)) {
675 Bitu regbase = base+second_set;
677 // change sustain level and release rate of this operator
678 op_type* op_ptr = &(ctx->op)[regbase2op[second_set?(base+22):base]];
679 change_releaserate(ctx, regbase,op_ptr);
680 change_sustainlevel(ctx, regbase,op_ptr);
683 break;
684 case ARC_FREQ_NUM: {
685 // 0xa0-0xa8 low8 frequency
686 Bitu base = (idx-ARC_FREQ_NUM)&0xff;
687 if (base<9) {
688 Bits opbase = second_set?(base+18):base;
689 if (((ctx->adlibreg)[0x105]&1) && (ctx->op)[opbase].is_4op_attached) break;
690 // regbase of modulator:
691 Bits modbase = modulatorbase[base]+second_set;
693 Bitu chanbase = base+second_set;
695 change_frequency(ctx, chanbase,modbase,&(ctx->op)[opbase]);
696 change_frequency(ctx, chanbase,modbase+3,&(ctx->op)[opbase+9]);
697 // for 4op channels all four operators are modified to the frequency of the channel
698 if (((ctx->adlibreg)[0x105]&1) && (ctx->op)[second_set?(base+18):base].is_4op) {
699 change_frequency(ctx, chanbase,modbase+8,&(ctx->op)[opbase+3]);
700 change_frequency(ctx, chanbase,modbase+3+8,&(ctx->op)[opbase+3+9]);
704 break;
705 case ARC_KON_BNUM: {
706 if (idx == ARC_PERC_MODE) {
707 if (second_set) return;
709 if ((val&0x30) == 0x30) { // BassDrum active
710 enable_operator(ctx, 16,&(ctx->op)[6],OP_ACT_PERC);
711 change_frequency(ctx, 6,16,&(ctx->op)[6]);
712 enable_operator(ctx, 16+3,&(ctx->op)[6+9],OP_ACT_PERC);
713 change_frequency(ctx, 6,16+3,&(ctx->op)[6+9]);
714 } else {
715 disable_operator(ctx, &(ctx->op)[6],OP_ACT_PERC);
716 disable_operator(ctx, &(ctx->op)[6+9],OP_ACT_PERC);
718 if ((val&0x28) == 0x28) { // Snare active
719 enable_operator(ctx, 17+3,&(ctx->op)[16],OP_ACT_PERC);
720 change_frequency(ctx, 7,17+3,&(ctx->op)[16]);
721 } else {
722 disable_operator(ctx, &(ctx->op)[16],OP_ACT_PERC);
724 if ((val&0x24) == 0x24) { // TomTom active
725 enable_operator(ctx, 18,&(ctx->op)[8],OP_ACT_PERC);
726 change_frequency(ctx, 8,18,&(ctx->op)[8]);
727 } else {
728 disable_operator(ctx, &(ctx->op)[8],OP_ACT_PERC);
730 if ((val&0x22) == 0x22) { // Cymbal active
731 enable_operator(ctx, 18+3,&(ctx->op)[8+9],OP_ACT_PERC);
732 change_frequency(ctx, 8,18+3,&(ctx->op)[8+9]);
733 } else {
734 disable_operator(ctx, &(ctx->op)[8+9],OP_ACT_PERC);
736 if ((val&0x21) == 0x21) { // Hihat active
737 enable_operator(ctx, 17,&(ctx->op)[7],OP_ACT_PERC);
738 change_frequency(ctx, 7,17,&(ctx->op)[7]);
739 } else {
740 disable_operator(ctx, &(ctx->op)[7],OP_ACT_PERC);
743 break;
745 // regular 0xb0-0xb8
746 Bitu base = (idx-ARC_KON_BNUM)&0xff;
747 if (base<9) {
748 Bits opbase = second_set?(base+18):base;
749 if (((ctx->adlibreg)[0x105]&1) && (ctx->op)[opbase].is_4op_attached) break;
750 // regbase of modulator:
751 Bits modbase = modulatorbase[base]+second_set;
753 if (val&32) {
754 // operator switched on
755 enable_operator(ctx, modbase,&(ctx->op)[opbase],OP_ACT_NORMAL); // modulator (if 2op)
756 enable_operator(ctx, modbase+3,&(ctx->op)[opbase+9],OP_ACT_NORMAL); // carrier (if 2op)
757 // for 4op channels all four operators are switched on
758 if (((ctx->adlibreg)[0x105]&1) && (ctx->op)[opbase].is_4op) {
759 // turn on chan+3 operators as well
760 enable_operator(ctx, modbase+8,&(ctx->op)[opbase+3],OP_ACT_NORMAL);
761 enable_operator(ctx, modbase+3+8,&(ctx->op)[opbase+3+9],OP_ACT_NORMAL);
763 } else {
764 // operator switched off
765 disable_operator(ctx, &(ctx->op)[opbase],OP_ACT_NORMAL);
766 disable_operator(ctx, &(ctx->op)[opbase+9],OP_ACT_NORMAL);
767 // for 4op channels all four operators are switched off
768 if (((ctx->adlibreg)[0x105]&1) && (ctx->op)[opbase].is_4op) {
769 // turn off chan+3 operators as well
770 disable_operator(ctx, &(ctx->op)[opbase+3],OP_ACT_NORMAL);
771 disable_operator(ctx, &(ctx->op)[opbase+3+9],OP_ACT_NORMAL);
775 Bitu chanbase = base+second_set;
777 // change frequency calculations of modulator and carrier (2op) as
778 // the frequency of the channel has changed
779 change_frequency(ctx, chanbase,modbase,&(ctx->op)[opbase]);
780 change_frequency(ctx, chanbase,modbase+3,&(ctx->op)[opbase+9]);
781 // for 4op channels all four operators are modified to the frequency of the channel
782 if (((ctx->adlibreg)[0x105]&1) && (ctx->op)[second_set?(base+18):base].is_4op) {
783 // change frequency calculations of chan+3 operators as well
784 change_frequency(ctx, chanbase,modbase+8,&(ctx->op)[opbase+3]);
785 change_frequency(ctx, chanbase,modbase+3+8,&(ctx->op)[opbase+3+9]);
789 break;
790 case ARC_FEEDBACK: {
791 // 0xc0-0xc8 feedback/modulation type (AM/FM)
792 Bitu base = (idx-ARC_FEEDBACK)&0xff;
793 if (base<9) {
794 Bits opbase = second_set?(base+18):base;
795 Bitu chanbase = base+second_set;
796 change_feedback(ctx, chanbase,&(ctx->op)[opbase]);
797 // OPL3 panning
798 (ctx->op)[opbase].left_pan = ((val&0x10)>>4);
799 (ctx->op)[opbase].right_pan = ((val&0x20)>>5);
802 break;
803 case ARC_WAVE_SEL:
804 case ARC_WAVE_SEL+0x10: {
805 int num = idx&7;
806 Bitu base = (idx-ARC_WAVE_SEL)&0xff;
807 if ((num<6) && (base<22)) {
808 Bits wselbase = second_set?(base+22):base; // for easier mapping onto (ctx->wave_sel)[]
809 // change waveform
810 if ((ctx->adlibreg)[0x105]&1) (ctx->wave_sel)[wselbase] = val&7; // opl3 mode enabled, all waveforms accessible
811 else (ctx->wave_sel)[wselbase] = val&3;
812 op_type* op_ptr = &(ctx->op)[regbase2modop[wselbase]+((num<3) ? 0 : 9)];
813 change_waveform(ctx, wselbase,op_ptr);
816 break;
817 default:
818 break;
823 Bitu adlib_reg_read(opl_context* ctx, Bitu port) {
824 // opl3-detection routines require ret&6 to be zero
825 if ((port&1)==0) {
826 return (ctx->status);
828 return 0x00;
831 void adlib_write_index(opl_context* ctx, Bitu port, Bit8u val) {
832 (ctx->opl_index) = val;
833 if ((port&3)!=0) {
834 // possibly second set
835 if ((((ctx->adlibreg)[0x105]&1)!=0) || ((ctx->opl_index)==5)) (ctx->opl_index) |= ARC_SECONDSET;
839 static void OPL_INLINE clipit16(Bit32s ival, Bit16s* outval) {
840 if (ival<32768) {
841 if (ival>-32769) {
842 *outval=(Bit16s)ival;
843 } else {
844 *outval = -32768;
846 } else {
847 *outval = 32767;
853 // be careful with this
854 // uses cptr and chanval, outputs into outbufl(/outbufr)
855 // for opl3 check if opl3-mode is enabled (which uses stereo panning)
856 #undef CHANVAL_OUT
857 #define CHANVAL_OUT \
858 if ((ctx->adlibreg)[0x105]&1) { \
859 outbufl[i] += chanval*cptr[0].left_pan; \
860 outbufr[i] += chanval*cptr[0].right_pan; \
861 } else { \
862 outbufl[i] += chanval; \
865 void adlib_getsample(opl_context* ctx, Bit16s* sndptr, Bits numsamples) {
866 Bits i, endsamples;
867 op_type* cptr;
869 Bit32s outbufl[BLOCKBUF_SIZE];
870 // second output buffer (right channel for opl3 stereo)
871 Bit32s outbufr[BLOCKBUF_SIZE];
873 // vibrato/tremolo lookup tables (global, to possibly be used by all operators)
874 Bit32s vib_lut[BLOCKBUF_SIZE];
875 Bit32s trem_lut[BLOCKBUF_SIZE];
877 Bits samples_to_process = numsamples;
879 for (Bits cursmp=0; cursmp<samples_to_process; cursmp+=endsamples) {
880 endsamples = samples_to_process-cursmp;
881 if (endsamples>BLOCKBUF_SIZE) endsamples = BLOCKBUF_SIZE;
883 memset((void*)&outbufl,0,endsamples*sizeof(Bit32s));
884 // clear second output buffer (opl3 stereo)
885 if ((ctx->adlibreg)[0x105]&1) memset((void*)&outbufr,0,endsamples*sizeof(Bit32s));
887 // calculate vibrato/tremolo lookup tables
888 Bit32s vib_tshift = (((ctx->adlibreg)[ARC_PERC_MODE]&0x40)==0) ? 1 : 0; // 14cents/7cents switching
889 for (i=0;i<endsamples;i++) {
890 // cycle through vibrato table
891 (ctx->vibtab_pos) += (ctx->vibtab_add);
892 if ((ctx->vibtab_pos)/FIXEDPT_LFO>=VIBTAB_SIZE) (ctx->vibtab_pos)-=VIBTAB_SIZE*FIXEDPT_LFO;
893 vib_lut[i] = (ctx->vib_table)[(ctx->vibtab_pos)/FIXEDPT_LFO]>>vib_tshift; // 14cents (14/100 of a semitone) or 7cents
895 // cycle through tremolo table
896 (ctx->tremtab_pos) += (ctx->tremtab_add);
897 if ((ctx->tremtab_pos)/FIXEDPT_LFO>=TREMTAB_SIZE) (ctx->tremtab_pos)-=TREMTAB_SIZE*FIXEDPT_LFO;
898 if ((ctx->adlibreg)[ARC_PERC_MODE]&0x80) trem_lut[i] = (ctx->trem_table)[(ctx->tremtab_pos)/FIXEDPT_LFO];
899 else trem_lut[i] = (ctx->trem_table)[TREMTAB_SIZE+(ctx->tremtab_pos)/FIXEDPT_LFO];
902 if ((ctx->adlibreg)[ARC_PERC_MODE]&0x20) {
903 //BassDrum
904 cptr = &(ctx->op)[6];
905 if ((ctx->adlibreg)[ARC_FEEDBACK+6]&1) {
906 // additive synthesis
907 if (cptr[9].op_state != OF_TYPE_OFF) {
908 if (cptr[9].vibrato) {
909 (ctx->vibval1) = (ctx->vibval_var1);
910 for (i=0;i<endsamples;i++)
911 (ctx->vibval1)[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
912 } else (ctx->vibval1) = (ctx->vibval_const);
913 if (cptr[9].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
914 else (ctx->tremval1) = (ctx->tremval_const);
916 // calculate channel output
917 for (i=0;i<endsamples;i++) {
918 operator_advance(ctx, &cptr[9],(ctx->vibval1)[i]);
919 opfuncs[cptr[9].op_state](ctx, &cptr[9]);
920 operator_output(ctx, &cptr[9],0,(ctx->tremval1)[i]);
922 Bit32s chanval = cptr[9].cval*2;
923 CHANVAL_OUT
926 } else {
927 // frequency modulation
928 if ((cptr[9].op_state != OF_TYPE_OFF) || (cptr[0].op_state != OF_TYPE_OFF)) {
929 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
930 (ctx->vibval1) = (ctx->vibval_var1);
931 for (i=0;i<endsamples;i++)
932 (ctx->vibval1)[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
933 } else (ctx->vibval1) = (ctx->vibval_const);
934 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
935 (ctx->vibval2) = (ctx->vibval_var2);
936 for (i=0;i<endsamples;i++)
937 (ctx->vibval2)[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
938 } else (ctx->vibval2) = (ctx->vibval_const);
939 if (cptr[0].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
940 else (ctx->tremval1) = (ctx->tremval_const);
941 if (cptr[9].tremolo) (ctx->tremval2) = trem_lut; // tremolo enabled, use table
942 else (ctx->tremval2) = (ctx->tremval_const);
944 // calculate channel output
945 for (i=0;i<endsamples;i++) {
946 operator_advance(ctx, &cptr[0],(ctx->vibval1)[i]);
947 opfuncs[cptr[0].op_state](ctx, &cptr[0]);
948 operator_output(ctx, &cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,(ctx->tremval1)[i]);
950 operator_advance(ctx, &cptr[9],(ctx->vibval2)[i]);
951 opfuncs[cptr[9].op_state](ctx, &cptr[9]);
952 operator_output(ctx, &cptr[9],cptr[0].cval*FIXEDPT,(ctx->tremval2)[i]);
954 Bit32s chanval = cptr[9].cval*2;
955 CHANVAL_OUT
960 //TomTom (j=8)
961 if ((ctx->op)[8].op_state != OF_TYPE_OFF) {
962 cptr = &(ctx->op)[8];
963 if (cptr[0].vibrato) {
964 (ctx->vibval3) = (ctx->vibval_var1);
965 for (i=0;i<endsamples;i++)
966 (ctx->vibval3)[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
967 } else (ctx->vibval3) = (ctx->vibval_const);
969 if (cptr[0].tremolo) (ctx->tremval3) = trem_lut; // tremolo enabled, use table
970 else (ctx->tremval3) = (ctx->tremval_const);
972 // calculate channel output
973 for (i=0;i<endsamples;i++) {
974 operator_advance(ctx, &cptr[0],(ctx->vibval3)[i]);
975 opfuncs[cptr[0].op_state](ctx, &cptr[0]); //TomTom
976 operator_output(ctx, &cptr[0],0,(ctx->tremval3)[i]);
977 Bit32s chanval = cptr[0].cval*2;
978 CHANVAL_OUT
982 //Snare/Hihat (j=7), Cymbal (j=8)
983 if (((ctx->op)[7].op_state != OF_TYPE_OFF) || ((ctx->op)[16].op_state != OF_TYPE_OFF) ||
984 ((ctx->op)[17].op_state != OF_TYPE_OFF)) {
985 cptr = &(ctx->op)[7];
986 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
987 (ctx->vibval1) = (ctx->vibval_var1);
988 for (i=0;i<endsamples;i++)
989 (ctx->vibval1)[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
990 } else (ctx->vibval1) = (ctx->vibval_const);
991 if ((cptr[9].vibrato) && (cptr[9].op_state == OF_TYPE_OFF)) {
992 (ctx->vibval2) = (ctx->vibval_var2);
993 for (i=0;i<endsamples;i++)
994 (ctx->vibval2)[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
995 } else (ctx->vibval2) = (ctx->vibval_const);
997 if (cptr[0].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
998 else (ctx->tremval1) = (ctx->tremval_const);
999 if (cptr[9].tremolo) (ctx->tremval2) = trem_lut; // tremolo enabled, use table
1000 else (ctx->tremval2) = (ctx->tremval_const);
1002 cptr = &(ctx->op)[8];
1003 if ((cptr[9].vibrato) && (cptr[9].op_state == OF_TYPE_OFF)) {
1004 (ctx->vibval4) = (ctx->vibval_var2);
1005 for (i=0;i<endsamples;i++)
1006 (ctx->vibval4)[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1007 } else (ctx->vibval4) = (ctx->vibval_const);
1009 if (cptr[9].tremolo) (ctx->tremval4) = trem_lut; // tremolo enabled, use table
1010 else (ctx->tremval4) = (ctx->tremval_const);
1012 // calculate channel output
1013 for (i=0;i<endsamples;i++) {
1014 operator_advance_drums(ctx, &(ctx->op)[7],(ctx->vibval1)[i],&(ctx->op)[7+9],(ctx->vibval2)[i],&(ctx->op)[8+9],(ctx->vibval4)[i]);
1016 opfuncs[(ctx->op)[7].op_state](ctx, &(ctx->op)[7]); //Hihat
1017 operator_output(ctx, &(ctx->op)[7],0,(ctx->tremval1)[i]);
1019 opfuncs[(ctx->op)[7+9].op_state](ctx, &(ctx->op)[7+9]); //Snare
1020 operator_output(ctx, &(ctx->op)[7+9],0,(ctx->tremval2)[i]);
1022 opfuncs[(ctx->op)[8+9].op_state](ctx, &(ctx->op)[8+9]); //Cymbal
1023 operator_output(ctx, &(ctx->op)[8+9],0,(ctx->tremval4)[i]);
1025 Bit32s chanval = ((ctx->op)[7].cval + (ctx->op)[7+9].cval + (ctx->op)[8+9].cval)*2;
1026 CHANVAL_OUT
1031 Bitu max_channel = NUM_CHANNELS;
1032 if (((ctx->adlibreg)[0x105]&1)==0) max_channel = NUM_CHANNELS/2;
1033 for (Bits cur_ch=max_channel-1; cur_ch>=0; cur_ch--) {
1034 // skip drum/percussion operators
1035 if (((ctx->adlibreg)[ARC_PERC_MODE]&0x20) && (cur_ch >= 6) && (cur_ch < 9)) continue;
1037 Bitu k = cur_ch;
1038 if (cur_ch < 9) {
1039 cptr = &(ctx->op)[cur_ch];
1040 } else {
1041 cptr = &(ctx->op)[cur_ch+9]; // second set is operator18-operator35
1042 k += (-9+256); // second set uses registers 0x100 onwards
1044 // check if this operator is part of a 4-op
1045 if (((ctx->adlibreg)[0x105]&1) && cptr->is_4op_attached) continue;
1047 // check for FM/AM
1048 if ((ctx->adlibreg)[ARC_FEEDBACK+k]&1) {
1049 if (((ctx->adlibreg)[0x105]&1) && cptr->is_4op) {
1050 if ((ctx->adlibreg)[ARC_FEEDBACK+k+3]&1) {
1051 // AM-AM-style synthesis (op1[fb] + (op2 * op3) + op4)
1052 if (cptr[0].op_state != OF_TYPE_OFF) {
1053 if (cptr[0].vibrato) {
1054 (ctx->vibval1) = (ctx->vibval_var1);
1055 for (i=0;i<endsamples;i++)
1056 (ctx->vibval1)[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1057 } else (ctx->vibval1) = (ctx->vibval_const);
1058 if (cptr[0].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
1059 else (ctx->tremval1) = (ctx->tremval_const);
1061 // calculate channel output
1062 for (i=0;i<endsamples;i++) {
1063 operator_advance(ctx, &cptr[0],(ctx->vibval1)[i]);
1064 opfuncs[cptr[0].op_state](ctx, &cptr[0]);
1065 operator_output(ctx, &cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,(ctx->tremval1)[i]);
1067 Bit32s chanval = cptr[0].cval;
1068 CHANVAL_OUT
1072 if ((cptr[3].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF)) {
1073 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1074 (ctx->vibval1) = (ctx->vibval_var1);
1075 for (i=0;i<endsamples;i++)
1076 (ctx->vibval1)[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1077 } else (ctx->vibval1) = (ctx->vibval_const);
1078 if (cptr[9].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
1079 else (ctx->tremval1) = (ctx->tremval_const);
1080 if (cptr[3].tremolo) (ctx->tremval2) = trem_lut; // tremolo enabled, use table
1081 else (ctx->tremval2) = (ctx->tremval_const);
1083 // calculate channel output
1084 for (i=0;i<endsamples;i++) {
1085 operator_advance(ctx, &cptr[9],(ctx->vibval1)[i]);
1086 opfuncs[cptr[9].op_state](ctx, &cptr[9]);
1087 operator_output(ctx, &cptr[9],0,(ctx->tremval1)[i]);
1089 operator_advance(ctx, &cptr[3],0);
1090 opfuncs[cptr[3].op_state](ctx, &cptr[3]);
1091 operator_output(ctx, &cptr[3],cptr[9].cval*FIXEDPT,(ctx->tremval2)[i]);
1093 Bit32s chanval = cptr[3].cval;
1094 CHANVAL_OUT
1098 if (cptr[3+9].op_state != OF_TYPE_OFF) {
1099 if (cptr[3+9].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
1100 else (ctx->tremval1) = (ctx->tremval_const);
1102 // calculate channel output
1103 for (i=0;i<endsamples;i++) {
1104 operator_advance(ctx, &cptr[3+9],0);
1105 opfuncs[cptr[3+9].op_state](ctx, &cptr[3+9]);
1106 operator_output(ctx, &cptr[3+9],0,(ctx->tremval1)[i]);
1108 Bit32s chanval = cptr[3+9].cval;
1109 CHANVAL_OUT
1112 } else {
1113 // AM-FM-style synthesis (op1[fb] + (op2 * op3 * op4))
1114 if (cptr[0].op_state != OF_TYPE_OFF) {
1115 if (cptr[0].vibrato) {
1116 (ctx->vibval1) = (ctx->vibval_var1);
1117 for (i=0;i<endsamples;i++)
1118 (ctx->vibval1)[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1119 } else (ctx->vibval1) = (ctx->vibval_const);
1120 if (cptr[0].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
1121 else (ctx->tremval1) = (ctx->tremval_const);
1123 // calculate channel output
1124 for (i=0;i<endsamples;i++) {
1125 operator_advance(ctx, &cptr[0],(ctx->vibval1)[i]);
1126 opfuncs[cptr[0].op_state](ctx, &cptr[0]);
1127 operator_output(ctx, &cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,(ctx->tremval1)[i]);
1129 Bit32s chanval = cptr[0].cval;
1130 CHANVAL_OUT
1134 if ((cptr[9].op_state != OF_TYPE_OFF) || (cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
1135 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1136 (ctx->vibval1) = (ctx->vibval_var1);
1137 for (i=0;i<endsamples;i++)
1138 (ctx->vibval1)[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1139 } else (ctx->vibval1) = (ctx->vibval_const);
1140 if (cptr[9].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
1141 else (ctx->tremval1) = (ctx->tremval_const);
1142 if (cptr[3].tremolo) (ctx->tremval2) = trem_lut; // tremolo enabled, use table
1143 else (ctx->tremval2) = (ctx->tremval_const);
1144 if (cptr[3+9].tremolo) (ctx->tremval3) = trem_lut; // tremolo enabled, use table
1145 else (ctx->tremval3) = (ctx->tremval_const);
1147 // calculate channel output
1148 for (i=0;i<endsamples;i++) {
1149 operator_advance(ctx, &cptr[9],(ctx->vibval1)[i]);
1150 opfuncs[cptr[9].op_state](ctx, &cptr[9]);
1151 operator_output(ctx, &cptr[9],0,(ctx->tremval1)[i]);
1153 operator_advance(ctx, &cptr[3],0);
1154 opfuncs[cptr[3].op_state](ctx, &cptr[3]);
1155 operator_output(ctx, &cptr[3],cptr[9].cval*FIXEDPT,(ctx->tremval2)[i]);
1157 operator_advance(ctx, &cptr[3+9],0);
1158 opfuncs[cptr[3+9].op_state](ctx, &cptr[3+9]);
1159 operator_output(ctx, &cptr[3+9],cptr[3].cval*FIXEDPT,(ctx->tremval3)[i]);
1161 Bit32s chanval = cptr[3+9].cval;
1162 CHANVAL_OUT
1166 continue;
1168 // 2op additive synthesis
1169 if ((cptr[9].op_state == OF_TYPE_OFF) && (cptr[0].op_state == OF_TYPE_OFF)) continue;
1170 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1171 (ctx->vibval1) = (ctx->vibval_var1);
1172 for (i=0;i<endsamples;i++)
1173 (ctx->vibval1)[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1174 } else (ctx->vibval1) = (ctx->vibval_const);
1175 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1176 (ctx->vibval2) = (ctx->vibval_var2);
1177 for (i=0;i<endsamples;i++)
1178 (ctx->vibval2)[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1179 } else (ctx->vibval2) = (ctx->vibval_const);
1180 if (cptr[0].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
1181 else (ctx->tremval1) = (ctx->tremval_const);
1182 if (cptr[9].tremolo) (ctx->tremval2) = trem_lut; // tremolo enabled, use table
1183 else (ctx->tremval2) = (ctx->tremval_const);
1185 // calculate channel output
1186 for (i=0;i<endsamples;i++) {
1187 // carrier1
1188 operator_advance(ctx, &cptr[0],(ctx->vibval1)[i]);
1189 opfuncs[cptr[0].op_state](ctx, &cptr[0]);
1190 operator_output(ctx, &cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,(ctx->tremval1)[i]);
1192 // carrier2
1193 operator_advance(ctx, &cptr[9],(ctx->vibval2)[i]);
1194 opfuncs[cptr[9].op_state](ctx, &cptr[9]);
1195 operator_output(ctx, &cptr[9],0,(ctx->tremval2)[i]);
1197 Bit32s chanval = cptr[9].cval + cptr[0].cval;
1198 CHANVAL_OUT
1200 } else {
1201 if (((ctx->adlibreg)[0x105]&1) && cptr->is_4op) {
1202 if ((ctx->adlibreg)[ARC_FEEDBACK+k+3]&1) {
1203 // FM-AM-style synthesis ((op1[fb] * op2) + (op3 * op4))
1204 if ((cptr[0].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF)) {
1205 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1206 (ctx->vibval1) = (ctx->vibval_var1);
1207 for (i=0;i<endsamples;i++)
1208 (ctx->vibval1)[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1209 } else (ctx->vibval1) = (ctx->vibval_const);
1210 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1211 (ctx->vibval2) = (ctx->vibval_var2);
1212 for (i=0;i<endsamples;i++)
1213 (ctx->vibval2)[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1214 } else (ctx->vibval2) = (ctx->vibval_const);
1215 if (cptr[0].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
1216 else (ctx->tremval1) = (ctx->tremval_const);
1217 if (cptr[9].tremolo) (ctx->tremval2) = trem_lut; // tremolo enabled, use table
1218 else (ctx->tremval2) = (ctx->tremval_const);
1220 // calculate channel output
1221 for (i=0;i<endsamples;i++) {
1222 operator_advance(ctx, &cptr[0],(ctx->vibval1)[i]);
1223 opfuncs[cptr[0].op_state](ctx, &cptr[0]);
1224 operator_output(ctx, &cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,(ctx->tremval1)[i]);
1226 operator_advance(ctx, &cptr[9],(ctx->vibval2)[i]);
1227 opfuncs[cptr[9].op_state](ctx, &cptr[9]);
1228 operator_output(ctx, &cptr[9],cptr[0].cval*FIXEDPT,(ctx->tremval2)[i]);
1230 Bit32s chanval = cptr[9].cval;
1231 CHANVAL_OUT
1235 if ((cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
1236 if (cptr[3].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
1237 else (ctx->tremval1) = (ctx->tremval_const);
1238 if (cptr[3+9].tremolo) (ctx->tremval2) = trem_lut; // tremolo enabled, use table
1239 else (ctx->tremval2) = (ctx->tremval_const);
1241 // calculate channel output
1242 for (i=0;i<endsamples;i++) {
1243 operator_advance(ctx, &cptr[3],0);
1244 opfuncs[cptr[3].op_state](ctx, &cptr[3]);
1245 operator_output(ctx, &cptr[3],0,(ctx->tremval1)[i]);
1247 operator_advance(ctx, &cptr[3+9],0);
1248 opfuncs[cptr[3+9].op_state](ctx, &cptr[3+9]);
1249 operator_output(ctx, &cptr[3+9],cptr[3].cval*FIXEDPT,(ctx->tremval2)[i]);
1251 Bit32s chanval = cptr[3+9].cval;
1252 CHANVAL_OUT
1256 } else {
1257 // FM-FM-style synthesis (op1[fb] * op2 * op3 * op4)
1258 if ((cptr[0].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF) ||
1259 (cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
1260 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1261 (ctx->vibval1) = (ctx->vibval_var1);
1262 for (i=0;i<endsamples;i++)
1263 (ctx->vibval1)[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1264 } else (ctx->vibval1) = (ctx->vibval_const);
1265 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1266 (ctx->vibval2) = (ctx->vibval_var2);
1267 for (i=0;i<endsamples;i++)
1268 (ctx->vibval2)[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1269 } else (ctx->vibval2) = (ctx->vibval_const);
1270 if (cptr[0].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
1271 else (ctx->tremval1) = (ctx->tremval_const);
1272 if (cptr[9].tremolo) (ctx->tremval2) = trem_lut; // tremolo enabled, use table
1273 else (ctx->tremval2) = (ctx->tremval_const);
1274 if (cptr[3].tremolo) (ctx->tremval3) = trem_lut; // tremolo enabled, use table
1275 else (ctx->tremval3) = (ctx->tremval_const);
1276 if (cptr[3+9].tremolo) (ctx->tremval4) = trem_lut; // tremolo enabled, use table
1277 else (ctx->tremval4) = (ctx->tremval_const);
1279 // calculate channel output
1280 for (i=0;i<endsamples;i++) {
1281 operator_advance(ctx, &cptr[0],(ctx->vibval1)[i]);
1282 opfuncs[cptr[0].op_state](ctx, &cptr[0]);
1283 operator_output(ctx, &cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,(ctx->tremval1)[i]);
1285 operator_advance(ctx, &cptr[9],(ctx->vibval2)[i]);
1286 opfuncs[cptr[9].op_state](ctx, &cptr[9]);
1287 operator_output(ctx, &cptr[9],cptr[0].cval*FIXEDPT,(ctx->tremval2)[i]);
1289 operator_advance(ctx, &cptr[3],0);
1290 opfuncs[cptr[3].op_state](ctx, &cptr[3]);
1291 operator_output(ctx, &cptr[3],cptr[9].cval*FIXEDPT,(ctx->tremval3)[i]);
1293 operator_advance(ctx, &cptr[3+9],0);
1294 opfuncs[cptr[3+9].op_state](ctx, &cptr[3+9]);
1295 operator_output(ctx, &cptr[3+9],cptr[3].cval*FIXEDPT,(ctx->tremval4)[i]);
1297 Bit32s chanval = cptr[3+9].cval;
1298 CHANVAL_OUT
1302 continue;
1304 // 2op frequency modulation
1305 if ((cptr[9].op_state == OF_TYPE_OFF) && (cptr[0].op_state == OF_TYPE_OFF)) continue;
1306 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
1307 (ctx->vibval1) = (ctx->vibval_var1);
1308 for (i=0;i<endsamples;i++)
1309 (ctx->vibval1)[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
1310 } else (ctx->vibval1) = (ctx->vibval_const);
1311 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
1312 (ctx->vibval2) = (ctx->vibval_var2);
1313 for (i=0;i<endsamples;i++)
1314 (ctx->vibval2)[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
1315 } else (ctx->vibval2) = (ctx->vibval_const);
1316 if (cptr[0].tremolo) (ctx->tremval1) = trem_lut; // tremolo enabled, use table
1317 else (ctx->tremval1) = (ctx->tremval_const);
1318 if (cptr[9].tremolo) (ctx->tremval2) = trem_lut; // tremolo enabled, use table
1319 else (ctx->tremval2) = (ctx->tremval_const);
1321 // calculate channel output
1322 for (i=0;i<endsamples;i++) {
1323 // modulator
1324 operator_advance(ctx, &cptr[0],(ctx->vibval1)[i]);
1325 opfuncs[cptr[0].op_state](ctx, &cptr[0]);
1326 operator_output(ctx, &cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,(ctx->tremval1)[i]);
1328 // carrier
1329 operator_advance(ctx, &cptr[9],(ctx->vibval2)[i]);
1330 opfuncs[cptr[9].op_state](ctx, &cptr[9]);
1331 operator_output(ctx, &cptr[9],cptr[0].cval*FIXEDPT,(ctx->tremval2)[i]);
1333 Bit32s chanval = cptr[9].cval;
1334 CHANVAL_OUT
1339 if ((ctx->adlibreg)[0x105]&1) {
1340 // convert to 16bit samples (stereo)
1341 for (i=0;i<endsamples;i++) {
1342 clipit16(outbufl[i],sndptr++);
1343 clipit16(outbufr[i],sndptr++);
1345 } else {
1346 // convert to 16bit samples (mono)
1347 for (i=0;i<endsamples;i++) {
1348 clipit16(outbufl[i],sndptr++);
1349 clipit16(outbufl[i],sndptr++);