Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

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