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