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