DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/hardware/opl.cpp
00001 /*
00002  *  Copyright (C) 2002-2019  The DOSBox Team
00003  *  OPL2/OPL3 emulation library
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Lesser General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2.1 of the License, or (at your option) any later version.
00009  * 
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Lesser General Public License for more details.
00014  * 
00015  *  You should have received a copy of the GNU Lesser General Public
00016  *  License along with this library; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 
00021 /*
00022  * Originally based on ADLIBEMU.C, an AdLib/OPL2 emulation library by Ken Silverman
00023  * Copyright (C) 1998-2001 Ken Silverman
00024  * Ken Silverman's official web site: "http://www.advsys.net/ken"
00025  */
00026 
00027 
00028 #include <math.h>
00029 #include <stdlib.h> // rand()
00030 #include <string.h> // memset()
00031 #include "dosbox.h"
00032 #include "opl.h"
00033 #include <string.h>
00034 
00035 
00036 static fltype recipsamp;        // inverse of sampling rate
00037 static Bit16s wavtable[WAVEPREC*3];     // wave form table
00038 
00039 // vibrato/tremolo tables
00040 static Bit32s vib_table[VIBTAB_SIZE];
00041 static Bit32s trem_table[TREMTAB_SIZE*2];
00042 
00043 static Bit32s vibval_const[BLOCKBUF_SIZE];
00044 static Bit32s tremval_const[BLOCKBUF_SIZE];
00045 
00046 // vibrato value tables (used per-operator)
00047 static Bit32s vibval_var1[BLOCKBUF_SIZE];
00048 static Bit32s vibval_var2[BLOCKBUF_SIZE];
00049 //static Bit32s vibval_var3[BLOCKBUF_SIZE];
00050 //static Bit32s vibval_var4[BLOCKBUF_SIZE];
00051 
00052 // vibrato/trmolo value table pointers
00053 static Bit32s *vibval1, *vibval2, *vibval3, *vibval4;
00054 static Bit32s *tremval1, *tremval2, *tremval3, *tremval4;
00055 
00056 
00057 // key scale level lookup table
00058 static const fltype kslmul[4] = {
00059         0.0, 0.5, 0.25, 1.0             // -> 0, 3, 1.5, 6 dB/oct
00060 };
00061 
00062 // frequency multiplicator lookup table
00063 static const fltype frqmul_tab[16] = {
00064         0.5,1,2,3,4,5,6,7,8,9,10,10,12,12,15,15
00065 };
00066 // calculated frequency multiplication values (depend on sampling rate)
00067 static fltype frqmul[16];
00068 
00069 // key scale levels
00070 static Bit8u kslev[8][16];
00071 
00072 // map a channel number to the register offset of the modulator (=register base)
00073 static const Bit8u modulatorbase[9]     = {
00074         0,1,2,
00075         8,9,10,
00076         16,17,18
00077 };
00078 
00079 // map a register base to a modulator operator number or operator number
00080 #if defined(OPLTYPE_IS_OPL3)
00081 static const Bit8u regbase2modop[44] = {
00082         0,1,2,0,1,2,0,0,3,4,5,3,4,5,0,0,6,7,8,6,7,8,                                    // first set
00083         18,19,20,18,19,20,0,0,21,22,23,21,22,23,0,0,24,25,26,24,25,26   // second set
00084 };
00085 static const Bit8u regbase2op[44] = {
00086         0,1,2,9,10,11,0,0,3,4,5,12,13,14,0,0,6,7,8,15,16,17,                    // first set
00087         18,19,20,27,28,29,0,0,21,22,23,30,31,32,0,0,24,25,26,33,34,35   // second set
00088 };
00089 #else
00090 static const Bit8u regbase2modop[22*2] = {
00091         0,1,2,0,1,2,0,0,3,4,5,3,4,5,0,0,6,7,8,6,7,8,
00092         0,1,2,0,1,2,0,0,3,4,5,3,4,5,0,0,6,7,8,6,7,8
00093 };
00094 static const Bit8u regbase2op[22*2] = {
00095         0,1,2,9,10,11,0,0,3,4,5,12,13,14,0,0,6,7,8,15,16,17,
00096         0,1,2,9,10,11,0,0,3,4,5,12,13,14,0,0,6,7,8,15,16,17
00097 };
00098 #endif
00099 
00100 
00101 // start of the waveform
00102 static Bit32u waveform[8] = {
00103         WAVEPREC,
00104         WAVEPREC>>1,
00105         WAVEPREC,
00106         (WAVEPREC*3)>>2,
00107         0,
00108         0,
00109         (WAVEPREC*5)>>2,
00110         WAVEPREC<<1
00111 };
00112 
00113 // length of the waveform as mask
00114 static Bit32u wavemask[8] = {
00115         WAVEPREC-1,
00116         WAVEPREC-1,
00117         (WAVEPREC>>1)-1,
00118         (WAVEPREC>>1)-1,
00119         WAVEPREC-1,
00120         ((WAVEPREC*3)>>2)-1,
00121         WAVEPREC>>1,
00122         WAVEPREC-1
00123 };
00124 
00125 // where the first entry resides
00126 static Bit32u wavestart[8] = {
00127         0,
00128         WAVEPREC>>1,
00129         0,
00130         WAVEPREC>>2,
00131         0,
00132         0,
00133         0,
00134         WAVEPREC>>3
00135 };
00136 
00137 // envelope generator function constants
00138 static fltype attackconst[4] = {
00139         (fltype)(1/2.82624),
00140         (fltype)(1/2.25280),
00141         (fltype)(1/1.88416),
00142         (fltype)(1/1.59744)
00143 };
00144 static fltype decrelconst[4] = {
00145         (fltype)(1/39.28064),
00146         (fltype)(1/31.41608),
00147         (fltype)(1/26.17344),
00148         (fltype)(1/22.44608)
00149 };
00150 
00151 
00152 void operator_advance(op_type* op_pt, Bit32s vib) {
00153         op_pt->wfpos = op_pt->tcount;                                           // waveform position
00154         
00155         // advance waveform time
00156         op_pt->tcount += op_pt->tinc;
00157         op_pt->tcount += (Bit32u)((Bit32s)(op_pt->tinc)*vib)/FIXEDPT;
00158 
00159         op_pt->generator_pos += generator_add;
00160 }
00161 
00162 void operator_advance_drums(op_type* op_pt1, Bit32s vib1, op_type* op_pt2, Bit32s vib2, op_type* op_pt3, Bit32s vib3) {
00163         Bit32u c1 = op_pt1->tcount/FIXEDPT;
00164         Bit32u c3 = op_pt3->tcount/FIXEDPT;
00165         Bit32u phasebit = (((c1 & 0x88) ^ ((c1<<5) & 0x80)) | ((c3 ^ (c3<<2)) & 0x20)) ? 0x02 : 0x00;
00166 
00167         Bit32u noisebit = rand()&1;
00168 
00169         Bit32u snare_phase_bit = (((Bitu)((op_pt1->tcount/FIXEDPT) / 0x100))&1);
00170 
00171         //Hihat
00172         Bit32u inttm = (phasebit<<8u) | (0x34u<<(phasebit ^ (noisebit<<1u)));
00173         op_pt1->wfpos = inttm*FIXEDPT;                          // waveform position
00174         // advance waveform time
00175         op_pt1->tcount += op_pt1->tinc;
00176         op_pt1->tcount += (Bit32u)((Bit32s)(op_pt1->tinc)*vib1)/FIXEDPT;
00177         op_pt1->generator_pos += generator_add;
00178 
00179         //Snare
00180         inttm = ((1+snare_phase_bit) ^ noisebit)<<8;
00181         op_pt2->wfpos = inttm*FIXEDPT;                          // waveform position
00182         // advance waveform time
00183         op_pt2->tcount += op_pt2->tinc;
00184         op_pt2->tcount += (Bit32u)((Bit32s)(op_pt2->tinc)*vib2)/FIXEDPT;
00185         op_pt2->generator_pos += generator_add;
00186 
00187         //Cymbal
00188         inttm = (1+phasebit)<<8;
00189         op_pt3->wfpos = inttm*FIXEDPT;                          // waveform position
00190         // advance waveform time
00191         op_pt3->tcount += op_pt3->tinc;
00192         op_pt3->tcount += (Bit32u)((Bit32s)(op_pt3->tinc)*vib3)/FIXEDPT;
00193         op_pt3->generator_pos += generator_add;
00194 }
00195 
00196 
00197 // output level is sustained, mode changes only when operator is turned off (->release)
00198 // or when the keep-sustained bit is turned off (->sustain_nokeep)
00199 void operator_output(op_type* op_pt, Bit32s modulator, Bit32s trem) {
00200         if (op_pt->op_state != OF_TYPE_OFF) {
00201                 op_pt->lastcval = op_pt->cval;
00202                 Bit32u i = (Bit32u)(((Bit32s)op_pt->wfpos+modulator)/FIXEDPT);
00203 
00204                 // wform: -16384 to 16383 (0x4000)
00205                 // trem :  32768 to 65535 (0x10000)
00206                 // step_amp: 0.0 to 1.0
00207                 // vol  : 1/2^14 to 1/2^29 (/0x4000; /1../0x8000)
00208 
00209                 op_pt->cval = (Bit32s)(op_pt->step_amp*op_pt->vol*op_pt->cur_wform[i&op_pt->cur_wmask]*trem/16.0);
00210         }
00211 }
00212 
00213 
00214 // no action, operator is off
00215 void operator_off(op_type* /*op_pt*/) {
00216 }
00217 
00218 // output level is sustained, mode changes only when operator is turned off (->release)
00219 // or when the keep-sustained bit is turned off (->sustain_nokeep)
00220 void operator_sustain(op_type* op_pt) {
00221         Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT;    // number of (standardized) samples
00222         for (Bit32u ct=0; ct<num_steps_add; ct++) {
00223                 op_pt->cur_env_step++;
00224         }
00225         op_pt->generator_pos -= num_steps_add*FIXEDPT;
00226 }
00227 
00228 // operator in release mode, if output level reaches zero the operator is turned off
00229 void operator_release(op_type* op_pt) {
00230         // ??? boundary?
00231         if (op_pt->amp > 0.00000001) {
00232                 // release phase
00233                 op_pt->amp *= op_pt->releasemul;
00234         }
00235 
00236         Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT;    // number of (standardized) samples
00237         for (Bit32u ct=0; ct<num_steps_add; ct++) {
00238                 op_pt->cur_env_step++;                                          // sample counter
00239                 if ((op_pt->cur_env_step & op_pt->env_step_r)==0) {
00240                         if (op_pt->amp <= 0.00000001) {
00241                                 // release phase finished, turn off this operator
00242                                 op_pt->amp = 0.0;
00243                                 if (op_pt->op_state == OF_TYPE_REL) {
00244                                         op_pt->op_state = OF_TYPE_OFF;
00245                                 }
00246                         }
00247                         op_pt->step_amp = op_pt->amp;
00248                 }
00249         }
00250         op_pt->generator_pos -= num_steps_add*FIXEDPT;
00251 }
00252 
00253 // operator in decay mode, if sustain level is reached the output level is either
00254 // kept (sustain level keep enabled) or the operator is switched into release mode
00255 void operator_decay(op_type* op_pt) {
00256         if (op_pt->amp > op_pt->sustain_level) {
00257                 // decay phase
00258                 op_pt->amp *= op_pt->decaymul;
00259         }
00260 
00261         Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT;    // number of (standardized) samples
00262         for (Bit32u ct=0; ct<num_steps_add; ct++) {
00263                 op_pt->cur_env_step++;
00264                 if ((op_pt->cur_env_step & op_pt->env_step_d)==0) {
00265                         if (op_pt->amp <= op_pt->sustain_level) {
00266                                 // decay phase finished, sustain level reached
00267                                 if (op_pt->sus_keep) {
00268                                         // keep sustain level (until turned off)
00269                                         op_pt->op_state = OF_TYPE_SUS;
00270                                         op_pt->amp = op_pt->sustain_level;
00271                                 } else {
00272                                         // next: release phase
00273                                         op_pt->op_state = OF_TYPE_SUS_NOKEEP;
00274                                 }
00275                         }
00276                         op_pt->step_amp = op_pt->amp;
00277                 }
00278         }
00279         op_pt->generator_pos -= num_steps_add*FIXEDPT;
00280 }
00281 
00282 // operator in attack mode, if full output level is reached,
00283 // the operator is switched into decay mode
00284 void operator_attack(op_type* op_pt) {
00285         op_pt->amp = ((op_pt->a3*op_pt->amp + op_pt->a2)*op_pt->amp + op_pt->a1)*op_pt->amp + op_pt->a0;
00286 
00287         Bit32u num_steps_add = op_pt->generator_pos/FIXEDPT;            // number of (standardized) samples
00288         for (Bit32u ct=0; ct<num_steps_add; ct++) {
00289                 op_pt->cur_env_step++;  // next sample
00290                 if ((op_pt->cur_env_step & op_pt->env_step_a)==0) {             // check if next step already reached
00291                         if (op_pt->amp > 1.0) {
00292                                 // attack phase finished, next: decay
00293                                 op_pt->op_state = OF_TYPE_DEC;
00294                                 op_pt->amp = 1.0;
00295                                 op_pt->step_amp = 1.0;
00296                         }
00297                         op_pt->step_skip_pos_a <<= 1;
00298                         if (op_pt->step_skip_pos_a==0) op_pt->step_skip_pos_a = 1;
00299                         if (op_pt->step_skip_pos_a & op_pt->env_step_skip_a) {  // check if required to skip next step
00300                                 op_pt->step_amp = op_pt->amp;
00301                         }
00302                 }
00303         }
00304         op_pt->generator_pos -= num_steps_add*FIXEDPT;
00305 }
00306 
00307 
00308 typedef void (*optype_fptr)(op_type*);
00309 
00310 optype_fptr opfuncs[6] = {
00311         operator_attack,
00312         operator_decay,
00313         operator_release,
00314         operator_sustain,       // sustain phase (keeping level)
00315         operator_release,       // sustain_nokeep phase (release-style)
00316         operator_off
00317 };
00318 
00319 void change_attackrate(Bitu regbase, op_type* op_pt) {
00320         Bits attackrate = adlibreg[ARC_ATTR_DECR+regbase]>>4;
00321         if (attackrate) {
00322                 fltype f = (fltype)(pow(FL2,(fltype)attackrate+(op_pt->toff>>2)-1)*attackconst[op_pt->toff&3]*recipsamp);
00323                 // attack rate coefficients
00324                 op_pt->a0 = (fltype)(0.0377*f);
00325                 op_pt->a1 = (fltype)(10.73*f+1);
00326                 op_pt->a2 = (fltype)(-17.57*f);
00327                 op_pt->a3 = (fltype)(7.42*f);
00328 
00329                 Bits step_skip = attackrate*4 + op_pt->toff;
00330                 Bits steps = step_skip >> 2;
00331                 op_pt->env_step_a = (1<<(steps<=12?12-steps:0))-1;
00332 
00333                 Bits step_num = (step_skip<=48)?(4-(step_skip&3)):0;
00334                 static Bit8u step_skip_mask[5] = {0xff, 0xfe, 0xee, 0xba, 0xaa}; 
00335                 op_pt->env_step_skip_a = step_skip_mask[step_num];
00336 
00337 #if defined(OPLTYPE_IS_OPL3)
00338                 if (step_skip>=60) {
00339 #else
00340                 if (step_skip>=62) {
00341 #endif
00342                         op_pt->a0 = (fltype)(2.0);      // something that triggers an immediate transition to amp:=1.0
00343                         op_pt->a1 = (fltype)(0.0);
00344                         op_pt->a2 = (fltype)(0.0);
00345                         op_pt->a3 = (fltype)(0.0);
00346                 }
00347         } else {
00348                 // attack disabled
00349                 op_pt->a0 = 0.0;
00350                 op_pt->a1 = 1.0;
00351                 op_pt->a2 = 0.0;
00352                 op_pt->a3 = 0.0;
00353                 op_pt->env_step_a = 0;
00354                 op_pt->env_step_skip_a = 0;
00355         }
00356 }
00357 
00358 void change_decayrate(Bitu regbase, op_type* op_pt) {
00359         Bits decayrate = adlibreg[ARC_ATTR_DECR+regbase]&15;
00360         // decaymul should be 1.0 when decayrate==0
00361         if (decayrate) {
00362                 fltype f = (fltype)(-7.4493*decrelconst[op_pt->toff&3]*recipsamp);
00363                 op_pt->decaymul = (fltype)(pow(FL2,f*pow(FL2,(fltype)(decayrate+(op_pt->toff>>2)))));
00364                 Bits steps = (decayrate*4 + op_pt->toff) >> 2;
00365                 op_pt->env_step_d = (1<<(steps<=12?12-steps:0))-1;
00366         } else {
00367                 op_pt->decaymul = 1.0;
00368                 op_pt->env_step_d = 0;
00369         }
00370 }
00371 
00372 void change_releaserate(Bitu regbase, op_type* op_pt) {
00373         Bits releaserate = adlibreg[ARC_SUSL_RELR+regbase]&15;
00374         // releasemul should be 1.0 when releaserate==0
00375         if (releaserate) {
00376                 fltype f = (fltype)(-7.4493*decrelconst[op_pt->toff&3]*recipsamp);
00377                 op_pt->releasemul = (fltype)(pow(FL2,f*pow(FL2,(fltype)(releaserate+(op_pt->toff>>2)))));
00378                 Bits steps = (releaserate*4 + op_pt->toff) >> 2;
00379                 op_pt->env_step_r = (1<<(steps<=12?12-steps:0))-1;
00380         } else {
00381                 op_pt->releasemul = 1.0;
00382                 op_pt->env_step_r = 0;
00383         }
00384 }
00385 
00386 void change_sustainlevel(Bitu regbase, op_type* op_pt) {
00387         Bits sustainlevel = adlibreg[ARC_SUSL_RELR+regbase]>>4;
00388         // sustainlevel should be 0.0 when sustainlevel==15 (max)
00389         if (sustainlevel<15) {
00390                 op_pt->sustain_level = (fltype)(pow(FL2,(fltype)sustainlevel * (-FL05)));
00391         } else {
00392                 op_pt->sustain_level = 0.0;
00393         }
00394 }
00395 
00396 void change_waveform(Bitu regbase, op_type* op_pt) {
00397 #if defined(OPLTYPE_IS_OPL3)
00398         if (regbase>=ARC_SECONDSET) regbase -= (ARC_SECONDSET-22);      // second set starts at 22
00399 #endif
00400         // waveform selection
00401         op_pt->cur_wmask = wavemask[wave_sel[regbase]];
00402         op_pt->cur_wform = &wavtable[waveform[wave_sel[regbase]]];
00403         // (might need to be adapted to waveform type here...)
00404 }
00405 
00406 void change_keepsustain(Bitu regbase, op_type* op_pt) {
00407         op_pt->sus_keep = (adlibreg[ARC_TVS_KSR_MUL+regbase]&0x20)>0;
00408         if (op_pt->op_state==OF_TYPE_SUS) {
00409                 if (!op_pt->sus_keep) op_pt->op_state = OF_TYPE_SUS_NOKEEP;
00410         } else if (op_pt->op_state==OF_TYPE_SUS_NOKEEP) {
00411                 if (op_pt->sus_keep) op_pt->op_state = OF_TYPE_SUS;
00412         }
00413 }
00414 
00415 // enable/disable vibrato/tremolo LFO effects
00416 void change_vibrato(Bitu regbase, op_type* op_pt) {
00417         op_pt->vibrato = (adlibreg[ARC_TVS_KSR_MUL+regbase]&0x40)!=0;
00418         op_pt->tremolo = (adlibreg[ARC_TVS_KSR_MUL+regbase]&0x80)!=0;
00419 }
00420 
00421 // change amount of self-feedback
00422 void change_feedback(Bitu chanbase, op_type* op_pt) {
00423         Bits feedback = adlibreg[ARC_FEEDBACK+chanbase]&14;
00424         if (feedback) op_pt->mfbi = (Bit32s)(pow(FL2,(fltype)((feedback>>1)+8)));
00425         else op_pt->mfbi = 0;
00426 }
00427 
00428 void change_frequency(Bitu chanbase, Bitu regbase, op_type* op_pt) {
00429         // frequency
00430         Bit32u frn = ((((Bit32u)adlibreg[ARC_KON_BNUM+chanbase])&3)<<8) + (Bit32u)adlibreg[ARC_FREQ_NUM+chanbase];
00431         // block number/octave
00432         Bit32u oct = ((((Bit32u)adlibreg[ARC_KON_BNUM+chanbase])>>2)&7);
00433         op_pt->freq_high = (Bit32s)((frn>>7)&7);
00434 
00435         // keysplit
00436         Bit32u note_sel = (adlibreg[8]>>6)&1;
00437         op_pt->toff = ((frn>>9)&(note_sel^1)) | ((frn>>8)&note_sel);
00438         op_pt->toff += (oct<<1);
00439 
00440         // envelope scaling (KSR)
00441         if (!(adlibreg[ARC_TVS_KSR_MUL+regbase]&0x10)) op_pt->toff >>= 2;
00442 
00443         // 20+a0+b0:
00444         op_pt->tinc = (Bit32u)(((fltype)(frn<<oct))*frqmul[adlibreg[ARC_TVS_KSR_MUL+regbase]&15]);
00445         // 40+a0+b0:
00446         fltype vol_in = (fltype)((fltype)(adlibreg[ARC_KSL_OUTLEV+regbase]&63) +
00447                                                         kslmul[adlibreg[ARC_KSL_OUTLEV+regbase]>>6]*kslev[oct][frn>>6]);
00448         op_pt->vol = (fltype)(pow(FL2,(fltype)(vol_in * -0.125 - 14)));
00449 
00450         // operator frequency changed, care about features that depend on it
00451         change_attackrate(regbase,op_pt);
00452         change_decayrate(regbase,op_pt);
00453         change_releaserate(regbase,op_pt);
00454 }
00455 
00456 void enable_operator(Bitu regbase, op_type* op_pt, Bit32u act_type) {
00457         // check if this is really an off-on transition
00458         if (op_pt->act_state == OP_ACT_OFF) {
00459                 Bits wselbase = (Bits)regbase;
00460                 if (wselbase>=ARC_SECONDSET) wselbase -= (ARC_SECONDSET-22);    // second set starts at 22
00461 
00462                 op_pt->tcount = wavestart[wave_sel[wselbase]]*FIXEDPT;
00463 
00464                 // start with attack mode
00465                 op_pt->op_state = OF_TYPE_ATT;
00466                 op_pt->act_state |= act_type;
00467         }
00468 }
00469 
00470 void disable_operator(op_type* op_pt, Bit32u act_type) {
00471         // check if this is really an on-off transition
00472         if (op_pt->act_state != OP_ACT_OFF) {
00473                 op_pt->act_state &= (~act_type);
00474                 if (op_pt->act_state == OP_ACT_OFF) {
00475                         if (op_pt->op_state != OF_TYPE_OFF) op_pt->op_state = OF_TYPE_REL;
00476                 }
00477         }
00478 }
00479 
00480 void adlib_init(Bit32u samplerate) {
00481     Bit16s i;
00482         Bits j, oct;
00483 
00484         int_samplerate = samplerate;
00485 
00486         generator_add = (Bit32u)(INTFREQU*FIXEDPT/int_samplerate);
00487 
00488 
00489         memset((void *)adlibreg,0,sizeof(adlibreg));
00490         memset((void *)op,0,sizeof(op_type)*MAXOPERATORS);
00491         memset((void *)wave_sel,0,sizeof(wave_sel));
00492 
00493         for (i=0;i<MAXOPERATORS;i++) {
00494                 op[i].op_state = OF_TYPE_OFF;
00495                 op[i].act_state = OP_ACT_OFF;
00496                 op[i].amp = 0.0;
00497                 op[i].step_amp = 0.0;
00498                 op[i].vol = 0.0;
00499                 op[i].tcount = 0;
00500                 op[i].tinc = 0;
00501                 op[i].toff = 0;
00502                 op[i].cur_wmask = wavemask[0];
00503                 op[i].cur_wform = &wavtable[waveform[0]];
00504                 op[i].freq_high = 0;
00505 
00506                 op[i].generator_pos = 0;
00507                 op[i].cur_env_step = 0;
00508                 op[i].env_step_a = 0;
00509                 op[i].env_step_d = 0;
00510                 op[i].env_step_r = 0;
00511                 op[i].step_skip_pos_a = 0;
00512                 op[i].env_step_skip_a = 0;
00513 
00514 #if defined(OPLTYPE_IS_OPL3)
00515                 op[i].is_4op = false;
00516                 op[i].is_4op_attached = false;
00517                 op[i].left_pan = 1;
00518                 op[i].right_pan = 1;
00519 #endif
00520         }
00521 
00522         recipsamp = 1.0 / (fltype)int_samplerate;
00523         for (i=15;i>=0;i--) {
00524                 frqmul[i] = (fltype)(frqmul_tab[i]*INTFREQU/(fltype)WAVEPREC*(fltype)FIXEDPT*recipsamp);
00525         }
00526 
00527         status = 0;
00528         opl_index = 0;
00529 
00530 
00531         // create vibrato table
00532         vib_table[0] = 8;
00533         vib_table[1] = 4;
00534         vib_table[2] = 0;
00535         vib_table[3] = -4;
00536         for (i=4; i<VIBTAB_SIZE; i++) vib_table[i] = vib_table[i-4]*-1;
00537 
00538         // vibrato at ~6.1 ?? (opl3 docs say 6.1, opl4 docs say 6.0, y8950 docs say 6.4)
00539         vibtab_add = static_cast<Bit32u>(VIBTAB_SIZE*FIXEDPT_LFO/8192*INTFREQU/int_samplerate);
00540         vibtab_pos = 0;
00541 
00542         for (i=0; i<BLOCKBUF_SIZE; i++) vibval_const[i] = 0;
00543 
00544 
00545         // create tremolo table
00546         Bit32s trem_table_int[TREMTAB_SIZE];
00547         for (i=0; i<14; i++)    trem_table_int[i] = i-13;               // upwards (13 to 26 -> -0.5/6 to 0)
00548         for (i=14; i<41; i++)   trem_table_int[i] = -i+14;              // downwards (26 to 0 -> 0 to -1/6)
00549         for (i=41; i<53; i++)   trem_table_int[i] = i-40-26;    // upwards (1 to 12 -> -1/6 to -0.5/6)
00550 
00551         for (i=0; i<TREMTAB_SIZE; i++) {
00552                 // 0.0 .. -26/26*4.8/6 == [0.0 .. -0.8], 4/53 steps == [1 .. 0.57]
00553                 fltype trem_val1=(fltype)(((fltype)trem_table_int[i])*4.8/26.0/6.0);                            // 4.8db
00554                 fltype trem_val2=(fltype)((fltype)((Bit32s)(trem_table_int[i]/4))*1.2/6.0/6.0);         // 1.2db (larger stepping)
00555 
00556                 trem_table[i] = (Bit32s)(pow(FL2,trem_val1)*FIXEDPT);
00557                 trem_table[TREMTAB_SIZE+i] = (Bit32s)(pow(FL2,trem_val2)*FIXEDPT);
00558         }
00559 
00560         // tremolo at 3.7hz
00561         tremtab_add = (Bit32u)((fltype)TREMTAB_SIZE * TREM_FREQ * FIXEDPT_LFO / (fltype)int_samplerate);
00562         tremtab_pos = 0;
00563 
00564         for (i=0; i<BLOCKBUF_SIZE; i++) tremval_const[i] = FIXEDPT;
00565 
00566 
00567         static Bitu initfirstime = 0;
00568         if (!initfirstime) {
00569                 initfirstime = 1;
00570 
00571                 // create waveform tables
00572                 for (i=0;i<(WAVEPREC>>1);i++) {
00573                         wavtable[(i<<1)  +WAVEPREC]     = (Bit16s)(16384*sin((fltype)(i<<1)*PI*2/WAVEPREC));
00574                         wavtable[(i<<1)+1+WAVEPREC]     = (Bit16s)(16384*sin((fltype)((i<<1)+1)*PI*2/WAVEPREC));
00575                         wavtable[i]                                     = wavtable[(i<<1)  +WAVEPREC];
00576                         // alternative: (zero-less)
00577 /*                      wavtable[(i<<1)  +WAVEPREC]     = (Bit16s)(16384*sin((fltype)((i<<2)+1)*PI/WAVEPREC));
00578                         wavtable[(i<<1)+1+WAVEPREC]     = (Bit16s)(16384*sin((fltype)((i<<2)+3)*PI/WAVEPREC));
00579                         wavtable[i]                                     = wavtable[(i<<1)-1+WAVEPREC]; */
00580                 }
00581                 for (i=0;i<(WAVEPREC>>3);i++) {
00582                         wavtable[i+(WAVEPREC<<1)]               = wavtable[i+(WAVEPREC>>3)]-16384;
00583                         wavtable[i+((WAVEPREC*17)>>3)]  = wavtable[i+(WAVEPREC>>2)]+16384;
00584                 }
00585 
00586                 // key scale level table verified ([table in book]*8/3)
00587                 kslev[7][0] = 0;        kslev[7][1] = 24;       kslev[7][2] = 32;       kslev[7][3] = 37;
00588                 kslev[7][4] = 40;       kslev[7][5] = 43;       kslev[7][6] = 45;       kslev[7][7] = 47;
00589                 kslev[7][8] = 48;
00590                 for (i=9;i<16;i++) kslev[7][i] = (Bit8u)(i+41);
00591                 for (j=6;j>=0;j--) {
00592                         for (i=0;i<16;i++) {
00593                                 oct = (Bits)kslev[j+1][i]-8;
00594                                 if (oct < 0) oct = 0;
00595                                 kslev[j][i] = (Bit8u)oct;
00596                         }
00597                 }
00598         }
00599 
00600 }
00601 
00602 
00603 
00604 void adlib_write(Bitu idx, Bit8u val) {
00605         Bit32u second_set = idx&0x100;
00606         adlibreg[idx] = val;
00607 
00608         switch (idx&0xf0) {
00609         case ARC_CONTROL:
00610                 // here we check for the second set registers, too:
00611                 switch (idx) {
00612                 case 0x02:      // timer1 counter
00613                 case 0x03:      // timer2 counter
00614                         break;
00615                 case 0x04:
00616                         // IRQ reset, timer mask/start
00617                         if (val&0x80) {
00618                                 // clear IRQ bits in status register
00619                                 status &= ~0x60;
00620                         } else {
00621                                 status = 0;
00622                         }
00623                         break;
00624 #if defined(OPLTYPE_IS_OPL3)
00625                 case 0x04|ARC_SECONDSET:
00626                         // 4op enable/disable switches for each possible channel
00627                         op[0].is_4op = (val&1)>0;
00628                         op[3].is_4op_attached = op[0].is_4op;
00629                         op[1].is_4op = (val&2)>0;
00630                         op[4].is_4op_attached = op[1].is_4op;
00631                         op[2].is_4op = (val&4)>0;
00632                         op[5].is_4op_attached = op[2].is_4op;
00633                         op[18].is_4op = (val&8)>0;
00634                         op[21].is_4op_attached = op[18].is_4op;
00635                         op[19].is_4op = (val&16)>0;
00636                         op[22].is_4op_attached = op[19].is_4op;
00637                         op[20].is_4op = (val&32)>0;
00638                         op[23].is_4op_attached = op[20].is_4op;
00639                         break;
00640                 case 0x05|ARC_SECONDSET:
00641                         break;
00642 #endif
00643                 case 0x08:
00644                         // CSW, note select
00645                         break;
00646                 default:
00647                         break;
00648                 }
00649                 break;
00650         case ARC_TVS_KSR_MUL:
00651         case ARC_TVS_KSR_MUL+0x10: {
00652                 // tremolo/vibrato/sustain keeping enabled; key scale rate; frequency multiplication
00653                 int num = idx&7;
00654                 Bitu base = (idx-ARC_TVS_KSR_MUL)&0xff;
00655                 if ((num<6) && (base<22)) {
00656                         Bitu modop = regbase2modop[second_set?(base+22):base];
00657                         Bitu regbase = base+second_set;
00658                         Bitu chanbase = second_set?(modop-18+ARC_SECONDSET):modop;
00659 
00660                         // change tremolo/vibrato and sustain keeping of this operator
00661                         op_type* op_ptr = &op[modop+((num<3) ? 0 : 9)];
00662                         change_keepsustain(regbase,op_ptr);
00663                         change_vibrato(regbase,op_ptr);
00664 
00665                         // change frequency calculations of this operator as
00666                         // key scale rate and frequency multiplicator can be changed
00667 #if defined(OPLTYPE_IS_OPL3)
00668                         if ((adlibreg[0x105]&1) && (op[modop].is_4op_attached)) {
00669                                 // operator uses frequency of channel
00670                                 change_frequency(chanbase-3,regbase,op_ptr);
00671                         } else {
00672                                 change_frequency(chanbase,regbase,op_ptr);
00673                         }
00674 #else
00675                         change_frequency(chanbase,base,op_ptr);
00676 #endif
00677                 }
00678                 }
00679                 break;
00680         case ARC_KSL_OUTLEV:
00681         case ARC_KSL_OUTLEV+0x10: {
00682                 // key scale level; output rate
00683                 int num = idx&7;
00684                 Bitu base = (idx-ARC_KSL_OUTLEV)&0xff;
00685                 if ((num<6) && (base<22)) {
00686                         Bitu modop = regbase2modop[second_set?(base+22):base];
00687                         Bitu chanbase = second_set?(modop-18+ARC_SECONDSET):modop;
00688 
00689                         // change frequency calculations of this operator as
00690                         // key scale level and output rate can be changed
00691                         op_type* op_ptr = &op[modop+((num<3) ? 0 : 9)];
00692 #if defined(OPLTYPE_IS_OPL3)
00693                         Bitu regbase = base+second_set;
00694                         if ((adlibreg[0x105]&1) && (op[modop].is_4op_attached)) {
00695                                 // operator uses frequency of channel
00696                                 change_frequency(chanbase-3,regbase,op_ptr);
00697                         } else {
00698                                 change_frequency(chanbase,regbase,op_ptr);
00699                         }
00700 #else
00701                         change_frequency(chanbase,base,op_ptr);
00702 #endif
00703                 }
00704                 }
00705                 break;
00706         case ARC_ATTR_DECR:
00707         case ARC_ATTR_DECR+0x10: {
00708                 // attack/decay rates
00709                 int num = idx&7;
00710                 Bitu base = (idx-ARC_ATTR_DECR)&0xff;
00711                 if ((num<6) && (base<22)) {
00712                         Bitu regbase = base+second_set;
00713 
00714                         // change attack rate and decay rate of this operator
00715                         op_type* op_ptr = &op[regbase2op[second_set?(base+22):base]];
00716                         change_attackrate(regbase,op_ptr);
00717                         change_decayrate(regbase,op_ptr);
00718                 }
00719                 }
00720                 break;
00721         case ARC_SUSL_RELR:
00722         case ARC_SUSL_RELR+0x10: {
00723                 // sustain level; release rate
00724                 int num = idx&7;
00725                 Bitu base = (idx-ARC_SUSL_RELR)&0xff;
00726                 if ((num<6) && (base<22)) {
00727                         Bitu regbase = base+second_set;
00728 
00729                         // change sustain level and release rate of this operator
00730                         op_type* op_ptr = &op[regbase2op[second_set?(base+22):base]];
00731                         change_releaserate(regbase,op_ptr);
00732                         change_sustainlevel(regbase,op_ptr);
00733                 }
00734                 }
00735                 break;
00736         case ARC_FREQ_NUM: {
00737                 // 0xa0-0xa8 low8 frequency
00738                 Bitu base = (idx-ARC_FREQ_NUM)&0xff;
00739                 if (base<9) {
00740                         Bits opbase = (Bits)(second_set?(base+18u):base);
00741 #if defined(OPLTYPE_IS_OPL3)
00742                         if ((adlibreg[0x105]&1) && op[opbase].is_4op_attached) break;
00743 #endif
00744                         // regbase of modulator:
00745                         Bits modbase = modulatorbase[base]+second_set;
00746 
00747                         Bitu chanbase = base+second_set;
00748 
00749                         change_frequency(chanbase,(Bitu)modbase,&op[opbase]);
00750                         change_frequency(chanbase,(Bitu)modbase+3,&op[opbase+9]);
00751 #if defined(OPLTYPE_IS_OPL3)
00752                         // for 4op channels all four operators are modified to the frequency of the channel
00753                         if ((adlibreg[0x105]&1) && op[second_set?(base+18):base].is_4op) {
00754                                 change_frequency(chanbase,(Bitu)modbase+8,&op[opbase+3]);
00755                                 change_frequency(chanbase,(Bitu)modbase+3+8,&op[opbase+3+9]);
00756                         }
00757 #endif
00758                 }
00759                 }
00760                 break;
00761         case ARC_KON_BNUM: {
00762                 if (idx == ARC_PERC_MODE) {
00763 #if defined(OPLTYPE_IS_OPL3)
00764                         if (second_set) return;
00765 #endif
00766 
00767                         if ((val&0x30) == 0x30) {               // BassDrum active
00768                                 enable_operator(16,&op[6],OP_ACT_PERC);
00769                                 change_frequency(6,16,&op[6]);
00770                                 enable_operator(16+3,&op[6+9],OP_ACT_PERC);
00771                                 change_frequency(6,16+3,&op[6+9]);
00772                         } else {
00773                                 disable_operator(&op[6],OP_ACT_PERC);
00774                                 disable_operator(&op[6+9],OP_ACT_PERC);
00775                         }
00776                         if ((val&0x28) == 0x28) {               // Snare active
00777                                 enable_operator(17+3,&op[16],OP_ACT_PERC);
00778                                 change_frequency(7,17+3,&op[16]);
00779                         } else {
00780                                 disable_operator(&op[16],OP_ACT_PERC);
00781                         }
00782                         if ((val&0x24) == 0x24) {               // TomTom active
00783                                 enable_operator(18,&op[8],OP_ACT_PERC);
00784                                 change_frequency(8,18,&op[8]);
00785                         } else {
00786                                 disable_operator(&op[8],OP_ACT_PERC);
00787                         }
00788                         if ((val&0x22) == 0x22) {               // Cymbal active
00789                                 enable_operator(18+3,&op[8+9],OP_ACT_PERC);
00790                                 change_frequency(8,18+3,&op[8+9]);
00791                         } else {
00792                                 disable_operator(&op[8+9],OP_ACT_PERC);
00793                         }
00794                         if ((val&0x21) == 0x21) {               // Hihat active
00795                                 enable_operator(17,&op[7],OP_ACT_PERC);
00796                                 change_frequency(7,17,&op[7]);
00797                         } else {
00798                                 disable_operator(&op[7],OP_ACT_PERC);
00799                         }
00800 
00801                         break;
00802                 }
00803                 // regular 0xb0-0xb8
00804                 Bitu base = (idx-ARC_KON_BNUM)&0xff;
00805                 if (base<9) {
00806                         Bits opbase = (Bits)(second_set?(base+18):base);
00807 #if defined(OPLTYPE_IS_OPL3)
00808                         if ((adlibreg[0x105]&1) && op[opbase].is_4op_attached) break;
00809 #endif
00810                         // regbase of modulator:
00811                         Bits modbase = modulatorbase[base]+second_set;
00812 
00813                         if (val&32) {
00814                                 // operator switched on
00815                                 enable_operator((Bitu)modbase,&op[opbase],OP_ACT_NORMAL);               // modulator (if 2op)
00816                                 enable_operator((Bitu)modbase+3,&op[opbase+9],OP_ACT_NORMAL);   // carrier (if 2op)
00817 #if defined(OPLTYPE_IS_OPL3)
00818                                 // for 4op channels all four operators are switched on
00819                                 if ((adlibreg[0x105]&1) && op[opbase].is_4op) {
00820                                         // turn on chan+3 operators as well
00821                                         enable_operator((Bitu)modbase+8,&op[opbase+3],OP_ACT_NORMAL);
00822                                         enable_operator((Bitu)modbase+3+8,&op[opbase+3+9],OP_ACT_NORMAL);
00823                                 }
00824 #endif
00825                         } else {
00826                                 // operator switched off
00827                                 disable_operator(&op[opbase],OP_ACT_NORMAL);
00828                                 disable_operator(&op[opbase+9],OP_ACT_NORMAL);
00829 #if defined(OPLTYPE_IS_OPL3)
00830                                 // for 4op channels all four operators are switched off
00831                                 if ((adlibreg[0x105]&1) && op[opbase].is_4op) {
00832                                         // turn off chan+3 operators as well
00833                                         disable_operator(&op[opbase+3],OP_ACT_NORMAL);
00834                                         disable_operator(&op[opbase+3+9],OP_ACT_NORMAL);
00835                                 }
00836 #endif
00837                         }
00838 
00839                         Bitu chanbase = base+second_set;
00840 
00841                         // change frequency calculations of modulator and carrier (2op) as
00842                         // the frequency of the channel has changed
00843                         change_frequency(chanbase,(Bitu)modbase,&op[opbase]);
00844                         change_frequency(chanbase,(Bitu)modbase+3,&op[opbase+9]);
00845 #if defined(OPLTYPE_IS_OPL3)
00846                         // for 4op channels all four operators are modified to the frequency of the channel
00847                         if ((adlibreg[0x105]&1) && op[second_set?(base+18):base].is_4op) {
00848                                 // change frequency calculations of chan+3 operators as well
00849                                 change_frequency(chanbase,(Bitu)modbase+8,&op[opbase+3]);
00850                                 change_frequency(chanbase,(Bitu)modbase+3+8,&op[opbase+3+9]);
00851                         }
00852 #endif
00853                 }
00854                 }
00855                 break;
00856         case ARC_FEEDBACK: {
00857                 // 0xc0-0xc8 feedback/modulation type (AM/FM)
00858                 Bitu base = (idx-ARC_FEEDBACK)&0xff;
00859                 if (base<9) {
00860                         Bits opbase = (Bits)(second_set?(base+18):base);
00861                         Bitu chanbase = base+second_set;
00862                         change_feedback(chanbase,&op[opbase]);
00863 #if defined(OPLTYPE_IS_OPL3)
00864                         // OPL3 panning
00865                         op[opbase].left_pan = ((val&0x10)>>4);
00866                         op[opbase].right_pan = ((val&0x20)>>5);
00867 #endif
00868                 }
00869                 }
00870                 break;
00871         case ARC_WAVE_SEL:
00872         case ARC_WAVE_SEL+0x10: {
00873                 int num = idx&7;
00874                 Bitu base = (idx-ARC_WAVE_SEL)&0xff;
00875                 if ((num<6) && (base<22)) {
00876 #if defined(OPLTYPE_IS_OPL3)
00877                         Bits wselbase = (Bits)(second_set?(base+22):base);      // for easier mapping onto wave_sel[]
00878                         // change waveform
00879                         if (adlibreg[0x105]&1) wave_sel[wselbase] = val&7;      // opl3 mode enabled, all waveforms accessible
00880                         else wave_sel[wselbase] = val&3;
00881                         op_type* op_ptr = &op[regbase2modop[wselbase]+((num<3) ? 0 : 9)];
00882                         change_waveform((Bitu)wselbase,op_ptr);
00883 #else
00884                         if (adlibreg[0x01]&0x20) {
00885                                 // wave selection enabled, change waveform
00886                                 wave_sel[base] = val&3;
00887                                 op_type* op_ptr = &op[regbase2modop[base]+((num<3) ? 0 : 9)];
00888                                 change_waveform(base,op_ptr);
00889                         }
00890 #endif
00891                 }
00892                 }
00893                 break;
00894         default:
00895                 break;
00896         }
00897 }
00898 
00899 
00900 Bitu adlib_reg_read(Bitu port) {
00901 #if defined(OPLTYPE_IS_OPL3)
00902         // opl3-detection routines require ret&6 to be zero
00903         if ((port&1)==0) {
00904                 return status;
00905         }
00906         return 0x00;
00907 #else
00908         // opl2-detection routines require ret&6 to be 6
00909         if ((port&1)==0) {
00910                 return status|6;
00911         }
00912         return 0xff;
00913 #endif
00914 }
00915 
00916 void adlib_write_index(Bitu port, Bit8u val) {
00917     (void)port;//POSSIBLY UNUSED
00918         opl_index = val;
00919 #if defined(OPLTYPE_IS_OPL3)
00920         if ((port&3)!=0) {
00921                 // possibly second set
00922                 if (((adlibreg[0x105]&1)!=0) || (opl_index==5)) opl_index |= ARC_SECONDSET;
00923         }
00924 #endif
00925 }
00926 
00927 static void OPL_INLINE clipit16(Bit32s ival, Bit16s* outval) {
00928         if (ival<32768) {
00929                 if (ival>-32769) {
00930                         *outval=(Bit16s)ival;
00931                 } else {
00932                         *outval = -32768;
00933                 }
00934         } else {
00935                 *outval = 32767;
00936         }
00937 }
00938 
00939 
00940 
00941 // be careful with this
00942 // uses cptr and chanval, outputs into outbufl(/outbufr)
00943 // for opl3 check if opl3-mode is enabled (which uses stereo panning)
00944 #undef CHANVAL_OUT
00945 #if defined(OPLTYPE_IS_OPL3)
00946 #define CHANVAL_OUT                                                                     \
00947         if (adlibreg[0x105]&1) {                                                \
00948                 outbufl[i] += chanval*cptr[0].left_pan;         \
00949                 outbufr[i] += chanval*cptr[0].right_pan;        \
00950         } else {                                                                                \
00951                 outbufl[i] += chanval;                                          \
00952         }
00953 #else
00954 #define CHANVAL_OUT                                                                     \
00955         outbufl[i] += chanval;
00956 #endif
00957 
00958 void adlib_getsample(Bit16s* sndptr, Bits numsamples) {
00959         Bits i, endsamples;
00960         op_type* cptr;
00961 
00962         Bit32s outbufl[BLOCKBUF_SIZE];
00963 #if defined(OPLTYPE_IS_OPL3)
00964         // second output buffer (right channel for opl3 stereo)
00965         Bit32s outbufr[BLOCKBUF_SIZE];
00966 #endif
00967 
00968         // vibrato/tremolo lookup tables (global, to possibly be used by all operators)
00969         Bit32s vib_lut[BLOCKBUF_SIZE];
00970         Bit32s trem_lut[BLOCKBUF_SIZE];
00971 
00972         Bits samples_to_process = numsamples;
00973 
00974         for (Bits cursmp=0; cursmp<samples_to_process; cursmp+=endsamples) {
00975                 endsamples = samples_to_process-cursmp;
00976                 if (endsamples>BLOCKBUF_SIZE) endsamples = BLOCKBUF_SIZE;
00977 
00978                 memset((void*)&outbufl,0,(unsigned int)endsamples*sizeof(Bit32s));
00979 #if defined(OPLTYPE_IS_OPL3)
00980                 // clear second output buffer (opl3 stereo)
00981                 if (adlibreg[0x105]&1) memset((void*)&outbufr,0,(unsigned int)endsamples*sizeof(Bit32s));
00982 #endif
00983 
00984                 // calculate vibrato/tremolo lookup tables
00985                 Bit32s vib_tshift = ((adlibreg[ARC_PERC_MODE]&0x40)==0) ? 1 : 0;        // 14cents/7cents switching
00986                 for (i=0;i<endsamples;i++) {
00987                         // cycle through vibrato table
00988                         vibtab_pos += vibtab_add;
00989                         if (vibtab_pos/FIXEDPT_LFO>=VIBTAB_SIZE) vibtab_pos-=VIBTAB_SIZE*FIXEDPT_LFO;
00990                         vib_lut[i] = vib_table[vibtab_pos/FIXEDPT_LFO]>>vib_tshift;             // 14cents (14/100 of a semitone) or 7cents
00991 
00992                         // cycle through tremolo table
00993                         tremtab_pos += tremtab_add;
00994                         if (tremtab_pos/FIXEDPT_LFO>=TREMTAB_SIZE) tremtab_pos-=TREMTAB_SIZE*FIXEDPT_LFO;
00995                         if (adlibreg[ARC_PERC_MODE]&0x80) trem_lut[i] = trem_table[tremtab_pos/FIXEDPT_LFO];
00996                         else trem_lut[i] = trem_table[TREMTAB_SIZE+tremtab_pos/FIXEDPT_LFO];
00997                 }
00998 
00999                 if (adlibreg[ARC_PERC_MODE]&0x20) {
01000                         //BassDrum
01001                         cptr = &op[6];
01002                         if (adlibreg[ARC_FEEDBACK+6]&1) {
01003                                 // additive synthesis
01004                                 if (cptr[9].op_state != OF_TYPE_OFF) {
01005                                         if (cptr[9].vibrato) {
01006                                                 vibval1 = vibval_var1;
01007                                                 for (i=0;i<endsamples;i++)
01008                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01009                                         } else vibval1 = vibval_const;
01010                                         if (cptr[9].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01011                                         else tremval1 = tremval_const;
01012 
01013                                         // calculate channel output
01014                                         for (i=0;i<endsamples;i++) {
01015                                                 operator_advance(&cptr[9],vibval1[i]);
01016                                                 opfuncs[cptr[9].op_state](&cptr[9]);
01017                                                 operator_output(&cptr[9],0,tremval1[i]);
01018                                                 
01019                                                 Bit32s chanval = cptr[9].cval*2;
01020                                                 CHANVAL_OUT
01021                                         }
01022                                 }
01023                         } else {
01024                                 // frequency modulation
01025                                 if ((cptr[9].op_state != OF_TYPE_OFF) || (cptr[0].op_state != OF_TYPE_OFF)) {
01026                                         if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
01027                                                 vibval1 = vibval_var1;
01028                                                 for (i=0;i<endsamples;i++)
01029                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01030                                         } else vibval1 = vibval_const;
01031                                         if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01032                                                 vibval2 = vibval_var2;
01033                                                 for (i=0;i<endsamples;i++)
01034                                                         vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01035                                         } else vibval2 = vibval_const;
01036                                         if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01037                                         else tremval1 = tremval_const;
01038                                         if (cptr[9].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01039                                         else tremval2 = tremval_const;
01040 
01041                                         // calculate channel output
01042                                         for (i=0;i<endsamples;i++) {
01043                                                 operator_advance(&cptr[0],vibval1[i]);
01044                                                 opfuncs[cptr[0].op_state](&cptr[0]);
01045                                                 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01046 
01047                                                 operator_advance(&cptr[9],vibval2[i]);
01048                                                 opfuncs[cptr[9].op_state](&cptr[9]);
01049                                                 operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
01050                                                 
01051                                                 Bit32s chanval = cptr[9].cval*2;
01052                                                 CHANVAL_OUT
01053                                         }
01054                                 }
01055                         }
01056 
01057                         //TomTom (j=8)
01058                         if (op[8].op_state != OF_TYPE_OFF) {
01059                                 cptr = &op[8];
01060                                 if (cptr[0].vibrato) {
01061                                         vibval3 = vibval_var1;
01062                                         for (i=0;i<endsamples;i++)
01063                                                 vibval3[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01064                                 } else vibval3 = vibval_const;
01065 
01066                                 if (cptr[0].tremolo) tremval3 = trem_lut;       // tremolo enabled, use table
01067                                 else tremval3 = tremval_const;
01068 
01069                                 // calculate channel output
01070                                 for (i=0;i<endsamples;i++) {
01071                                         operator_advance(&cptr[0],vibval3[i]);
01072                                         opfuncs[cptr[0].op_state](&cptr[0]);            //TomTom
01073                                         operator_output(&cptr[0],0,tremval3[i]);
01074                                         Bit32s chanval = cptr[0].cval*2;
01075                                         CHANVAL_OUT
01076                                 }
01077                         }
01078 
01079                         //Snare/Hihat (j=7), Cymbal (j=8)
01080                         if ((op[7].op_state != OF_TYPE_OFF) || (op[16].op_state != OF_TYPE_OFF) ||
01081                                 (op[17].op_state != OF_TYPE_OFF)) {
01082                                 cptr = &op[7];
01083                                 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
01084                                         vibval1 = vibval_var1;
01085                                         for (i=0;i<endsamples;i++)
01086                                                 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01087                                 } else vibval1 = vibval_const;
01088                                 if ((cptr[9].vibrato) && (cptr[9].op_state == OF_TYPE_OFF)) {
01089                                         vibval2 = vibval_var2;
01090                                         for (i=0;i<endsamples;i++)
01091                                                 vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01092                                 } else vibval2 = vibval_const;
01093 
01094                                 if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01095                                 else tremval1 = tremval_const;
01096                                 if (cptr[9].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01097                                 else tremval2 = tremval_const;
01098 
01099                                 cptr = &op[8];
01100                                 if ((cptr[9].vibrato) && (cptr[9].op_state == OF_TYPE_OFF)) {
01101                                         vibval4 = vibval_var2;
01102                                         for (i=0;i<endsamples;i++)
01103                                                 vibval4[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01104                                 } else vibval4 = vibval_const;
01105 
01106                                 if (cptr[9].tremolo) tremval4 = trem_lut;       // tremolo enabled, use table
01107                                 else tremval4 = tremval_const;
01108 
01109                                 // calculate channel output
01110                                 for (i=0;i<endsamples;i++) {
01111                                         operator_advance_drums(&op[7],vibval1[i],&op[7+9],vibval2[i],&op[8+9],vibval4[i]);
01112 
01113                                         opfuncs[op[7].op_state](&op[7]);                        //Hihat
01114                                         operator_output(&op[7],0,tremval1[i]);
01115 
01116                                         opfuncs[op[7+9].op_state](&op[7+9]);            //Snare
01117                                         operator_output(&op[7+9],0,tremval2[i]);
01118 
01119                                         opfuncs[op[8+9].op_state](&op[8+9]);            //Cymbal
01120                                         operator_output(&op[8+9],0,tremval4[i]);
01121 
01122                                         Bit32s chanval = (op[7].cval + op[7+9].cval + op[8+9].cval)*2;
01123                                         CHANVAL_OUT
01124                                 }
01125                         }
01126                 }
01127 
01128                 Bitu max_channel = NUM_CHANNELS;
01129 #if defined(OPLTYPE_IS_OPL3)
01130                 if ((adlibreg[0x105]&1)==0) max_channel = NUM_CHANNELS/2;
01131 #endif
01132                 for (Bits cur_ch=(Bits)max_channel-1; cur_ch>=0; cur_ch--) {
01133                         // skip drum/percussion operators
01134                         if ((adlibreg[ARC_PERC_MODE]&0x20) && (cur_ch >= 6) && (cur_ch < 9)) continue;
01135 
01136                         Bitu k = (Bitu)cur_ch;
01137 #if defined(OPLTYPE_IS_OPL3)
01138                         if (cur_ch < 9) {
01139                                 cptr = &op[cur_ch];
01140                         } else {
01141                                 cptr = &op[cur_ch+9];   // second set is operator18-operator35
01142                                 k += (-9+256);          // second set uses registers 0x100 onwards
01143                         }
01144                         // check if this operator is part of a 4-op
01145                         if ((adlibreg[0x105]&1) && cptr->is_4op_attached) continue;
01146 #else
01147                         cptr = &op[cur_ch];
01148 #endif
01149 
01150                         // check for FM/AM
01151                         if (adlibreg[ARC_FEEDBACK+k]&1) {
01152 #if defined(OPLTYPE_IS_OPL3)
01153                                 if ((adlibreg[0x105]&1) && cptr->is_4op) {
01154                                         if (adlibreg[ARC_FEEDBACK+k+3]&1) {
01155                                                 // AM-AM-style synthesis (op1[fb] + (op2 * op3) + op4)
01156                                                 if (cptr[0].op_state != OF_TYPE_OFF) {
01157                                                         if (cptr[0].vibrato) {
01158                                                                 vibval1 = vibval_var1;
01159                                                                 for (i=0;i<endsamples;i++)
01160                                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01161                                                         } else vibval1 = vibval_const;
01162                                                         if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01163                                                         else tremval1 = tremval_const;
01164 
01165                                                         // calculate channel output
01166                                                         for (i=0;i<endsamples;i++) {
01167                                                                 operator_advance(&cptr[0],vibval1[i]);
01168                                                                 opfuncs[cptr[0].op_state](&cptr[0]);
01169                                                                 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01170 
01171                                                                 Bit32s chanval = cptr[0].cval;
01172                                                                 CHANVAL_OUT
01173                                                         }
01174                                                 }
01175 
01176                                                 if ((cptr[3].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF)) {
01177                                                         if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01178                                                                 vibval1 = vibval_var1;
01179                                                                 for (i=0;i<endsamples;i++)
01180                                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01181                                                         } else vibval1 = vibval_const;
01182                                                         if (cptr[9].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01183                                                         else tremval1 = tremval_const;
01184                                                         if (cptr[3].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01185                                                         else tremval2 = tremval_const;
01186 
01187                                                         // calculate channel output
01188                                                         for (i=0;i<endsamples;i++) {
01189                                                                 operator_advance(&cptr[9],vibval1[i]);
01190                                                                 opfuncs[cptr[9].op_state](&cptr[9]);
01191                                                                 operator_output(&cptr[9],0,tremval1[i]);
01192 
01193                                                                 operator_advance(&cptr[3],0);
01194                                                                 opfuncs[cptr[3].op_state](&cptr[3]);
01195                                                                 operator_output(&cptr[3],cptr[9].cval*FIXEDPT,tremval2[i]);
01196 
01197                                                                 Bit32s chanval = cptr[3].cval;
01198                                                                 CHANVAL_OUT
01199                                                         }
01200                                                 }
01201 
01202                                                 if (cptr[3+9].op_state != OF_TYPE_OFF) {
01203                                                         if (cptr[3+9].tremolo) tremval1 = trem_lut;     // tremolo enabled, use table
01204                                                         else tremval1 = tremval_const;
01205 
01206                                                         // calculate channel output
01207                                                         for (i=0;i<endsamples;i++) {
01208                                                                 operator_advance(&cptr[3+9],0);
01209                                                                 opfuncs[cptr[3+9].op_state](&cptr[3+9]);
01210                                                                 operator_output(&cptr[3+9],0,tremval1[i]);
01211 
01212                                                                 Bit32s chanval = cptr[3+9].cval;
01213                                                                 CHANVAL_OUT
01214                                                         }
01215                                                 }
01216                                         } else {
01217                                                 // AM-FM-style synthesis (op1[fb] + (op2 * op3 * op4))
01218                                                 if (cptr[0].op_state != OF_TYPE_OFF) {
01219                                                         if (cptr[0].vibrato) {
01220                                                                 vibval1 = vibval_var1;
01221                                                                 for (i=0;i<endsamples;i++)
01222                                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01223                                                         } else vibval1 = vibval_const;
01224                                                         if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01225                                                         else tremval1 = tremval_const;
01226 
01227                                                         // calculate channel output
01228                                                         for (i=0;i<endsamples;i++) {
01229                                                                 operator_advance(&cptr[0],vibval1[i]);
01230                                                                 opfuncs[cptr[0].op_state](&cptr[0]);
01231                                                                 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01232 
01233                                                                 Bit32s chanval = cptr[0].cval;
01234                                                                 CHANVAL_OUT
01235                                                         }
01236                                                 }
01237 
01238                                                 if ((cptr[9].op_state != OF_TYPE_OFF) || (cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
01239                                                         if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01240                                                                 vibval1 = vibval_var1;
01241                                                                 for (i=0;i<endsamples;i++)
01242                                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01243                                                         } else vibval1 = vibval_const;
01244                                                         if (cptr[9].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01245                                                         else tremval1 = tremval_const;
01246                                                         if (cptr[3].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01247                                                         else tremval2 = tremval_const;
01248                                                         if (cptr[3+9].tremolo) tremval3 = trem_lut;     // tremolo enabled, use table
01249                                                         else tremval3 = tremval_const;
01250 
01251                                                         // calculate channel output
01252                                                         for (i=0;i<endsamples;i++) {
01253                                                                 operator_advance(&cptr[9],vibval1[i]);
01254                                                                 opfuncs[cptr[9].op_state](&cptr[9]);
01255                                                                 operator_output(&cptr[9],0,tremval1[i]);
01256 
01257                                                                 operator_advance(&cptr[3],0);
01258                                                                 opfuncs[cptr[3].op_state](&cptr[3]);
01259                                                                 operator_output(&cptr[3],cptr[9].cval*FIXEDPT,tremval2[i]);
01260 
01261                                                                 operator_advance(&cptr[3+9],0);
01262                                                                 opfuncs[cptr[3+9].op_state](&cptr[3+9]);
01263                                                                 operator_output(&cptr[3+9],cptr[3].cval*FIXEDPT,tremval3[i]);
01264 
01265                                                                 Bit32s chanval = cptr[3+9].cval;
01266                                                                 CHANVAL_OUT
01267                                                         }
01268                                                 }
01269                                         }
01270                                         continue;
01271                                 }
01272 #endif
01273                                 // 2op additive synthesis
01274                                 if ((cptr[9].op_state == OF_TYPE_OFF) && (cptr[0].op_state == OF_TYPE_OFF)) continue;
01275                                 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
01276                                         vibval1 = vibval_var1;
01277                                         for (i=0;i<endsamples;i++)
01278                                                 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01279                                 } else vibval1 = vibval_const;
01280                                 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01281                                         vibval2 = vibval_var2;
01282                                         for (i=0;i<endsamples;i++)
01283                                                 vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01284                                 } else vibval2 = vibval_const;
01285                                 if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01286                                 else tremval1 = tremval_const;
01287                                 if (cptr[9].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01288                                 else tremval2 = tremval_const;
01289 
01290                                 // calculate channel output
01291                                 for (i=0;i<endsamples;i++) {
01292                                         // carrier1
01293                                         operator_advance(&cptr[0],vibval1[i]);
01294                                         opfuncs[cptr[0].op_state](&cptr[0]);
01295                                         operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01296 
01297                                         // carrier2
01298                                         operator_advance(&cptr[9],vibval2[i]);
01299                                         opfuncs[cptr[9].op_state](&cptr[9]);
01300                                         operator_output(&cptr[9],0,tremval2[i]);
01301 
01302                                         Bit32s chanval = cptr[9].cval + cptr[0].cval;
01303                                         CHANVAL_OUT
01304                                 }
01305                         } else {
01306 #if defined(OPLTYPE_IS_OPL3)
01307                                 if ((adlibreg[0x105]&1) && cptr->is_4op) {
01308                                         if (adlibreg[ARC_FEEDBACK+k+3]&1) {
01309                                                 // FM-AM-style synthesis ((op1[fb] * op2) + (op3 * op4))
01310                                                 if ((cptr[0].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF)) {
01311                                                         if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
01312                                                                 vibval1 = vibval_var1;
01313                                                                 for (i=0;i<endsamples;i++)
01314                                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01315                                                         } else vibval1 = vibval_const;
01316                                                         if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01317                                                                 vibval2 = vibval_var2;
01318                                                                 for (i=0;i<endsamples;i++)
01319                                                                         vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01320                                                         } else vibval2 = vibval_const;
01321                                                         if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01322                                                         else tremval1 = tremval_const;
01323                                                         if (cptr[9].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01324                                                         else tremval2 = tremval_const;
01325 
01326                                                         // calculate channel output
01327                                                         for (i=0;i<endsamples;i++) {
01328                                                                 operator_advance(&cptr[0],vibval1[i]);
01329                                                                 opfuncs[cptr[0].op_state](&cptr[0]);
01330                                                                 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01331 
01332                                                                 operator_advance(&cptr[9],vibval2[i]);
01333                                                                 opfuncs[cptr[9].op_state](&cptr[9]);
01334                                                                 operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
01335 
01336                                                                 Bit32s chanval = cptr[9].cval;
01337                                                                 CHANVAL_OUT
01338                                                         }
01339                                                 }
01340 
01341                                                 if ((cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
01342                                                         if (cptr[3].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01343                                                         else tremval1 = tremval_const;
01344                                                         if (cptr[3+9].tremolo) tremval2 = trem_lut;     // tremolo enabled, use table
01345                                                         else tremval2 = tremval_const;
01346 
01347                                                         // calculate channel output
01348                                                         for (i=0;i<endsamples;i++) {
01349                                                                 operator_advance(&cptr[3],0);
01350                                                                 opfuncs[cptr[3].op_state](&cptr[3]);
01351                                                                 operator_output(&cptr[3],0,tremval1[i]);
01352 
01353                                                                 operator_advance(&cptr[3+9],0);
01354                                                                 opfuncs[cptr[3+9].op_state](&cptr[3+9]);
01355                                                                 operator_output(&cptr[3+9],cptr[3].cval*FIXEDPT,tremval2[i]);
01356 
01357                                                                 Bit32s chanval = cptr[3+9].cval;
01358                                                                 CHANVAL_OUT
01359                                                         }
01360                                                 }
01361 
01362                                         } else {
01363                                                 // FM-FM-style synthesis (op1[fb] * op2 * op3 * op4)
01364                                                 if ((cptr[0].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF) || 
01365                                                         (cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
01366                                                         if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
01367                                                                 vibval1 = vibval_var1;
01368                                                                 for (i=0;i<endsamples;i++)
01369                                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01370                                                         } else vibval1 = vibval_const;
01371                                                         if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01372                                                                 vibval2 = vibval_var2;
01373                                                                 for (i=0;i<endsamples;i++)
01374                                                                         vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01375                                                         } else vibval2 = vibval_const;
01376                                                         if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01377                                                         else tremval1 = tremval_const;
01378                                                         if (cptr[9].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01379                                                         else tremval2 = tremval_const;
01380                                                         if (cptr[3].tremolo) tremval3 = trem_lut;       // tremolo enabled, use table
01381                                                         else tremval3 = tremval_const;
01382                                                         if (cptr[3+9].tremolo) tremval4 = trem_lut;     // tremolo enabled, use table
01383                                                         else tremval4 = tremval_const;
01384 
01385                                                         // calculate channel output
01386                                                         for (i=0;i<endsamples;i++) {
01387                                                                 operator_advance(&cptr[0],vibval1[i]);
01388                                                                 opfuncs[cptr[0].op_state](&cptr[0]);
01389                                                                 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01390 
01391                                                                 operator_advance(&cptr[9],vibval2[i]);
01392                                                                 opfuncs[cptr[9].op_state](&cptr[9]);
01393                                                                 operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
01394 
01395                                                                 operator_advance(&cptr[3],0);
01396                                                                 opfuncs[cptr[3].op_state](&cptr[3]);
01397                                                                 operator_output(&cptr[3],cptr[9].cval*FIXEDPT,tremval3[i]);
01398 
01399                                                                 operator_advance(&cptr[3+9],0);
01400                                                                 opfuncs[cptr[3+9].op_state](&cptr[3+9]);
01401                                                                 operator_output(&cptr[3+9],cptr[3].cval*FIXEDPT,tremval4[i]);
01402 
01403                                                                 Bit32s chanval = cptr[3+9].cval;
01404                                                                 CHANVAL_OUT
01405                                                         }
01406                                                 }
01407                                         }
01408                                         continue;
01409                                 }
01410 #endif
01411                                 // 2op frequency modulation
01412                                 if ((cptr[9].op_state == OF_TYPE_OFF) && (cptr[0].op_state == OF_TYPE_OFF)) continue;
01413                                 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
01414                                         vibval1 = vibval_var1;
01415                                         for (i=0;i<endsamples;i++)
01416                                                 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01417                                 } else vibval1 = vibval_const;
01418                                 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01419                                         vibval2 = vibval_var2;
01420                                         for (i=0;i<endsamples;i++)
01421                                                 vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01422                                 } else vibval2 = vibval_const;
01423                                 if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01424                                 else tremval1 = tremval_const;
01425                                 if (cptr[9].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01426                                 else tremval2 = tremval_const;
01427 
01428                                 // calculate channel output
01429                                 for (i=0;i<endsamples;i++) {
01430                                         // modulator
01431                                         operator_advance(&cptr[0],vibval1[i]);
01432                                         opfuncs[cptr[0].op_state](&cptr[0]);
01433                                         operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01434 
01435                                         // carrier
01436                                         operator_advance(&cptr[9],vibval2[i]);
01437                                         opfuncs[cptr[9].op_state](&cptr[9]);
01438                                         operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
01439 
01440                                         Bit32s chanval = cptr[9].cval;
01441                                         CHANVAL_OUT
01442                                 }
01443                         }
01444                 }
01445 
01446 #if defined(OPLTYPE_IS_OPL3)
01447                 if (adlibreg[0x105]&1) {
01448                         // convert to 16bit samples (stereo)
01449                         for (i=0;i<endsamples;i++) {
01450                                 clipit16(outbufl[i],sndptr++);
01451                                 clipit16(outbufr[i],sndptr++);
01452                         }
01453                 } else {
01454                         // convert to 16bit samples (mono)
01455                         for (i=0;i<endsamples;i++) {
01456                                 clipit16(outbufl[i],sndptr++);
01457                                 clipit16(outbufl[i],sndptr++);
01458                         }
01459                 }
01460 #else
01461                 // convert to 16bit samples
01462                 for (i=0;i<endsamples;i++)
01463                         clipit16(outbufl[i],sndptr++);
01464 #endif
01465 
01466         }
01467 }
01468