DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/hardware/opl.cpp
00001 /*
00002  *  Copyright (C) 2002-2020  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 
00483         int_samplerate = samplerate;
00484 
00485         generator_add = (Bit32u)(INTFREQU*FIXEDPT/int_samplerate);
00486 
00487 
00488         memset((void *)adlibreg,0,sizeof(adlibreg));
00489         memset((void *)op,0,sizeof(op_type)*MAXOPERATORS);
00490         memset((void *)wave_sel,0,sizeof(wave_sel));
00491 
00492         for (i=0;i<MAXOPERATORS;i++) {
00493                 op[i].op_state = OF_TYPE_OFF;
00494                 op[i].act_state = OP_ACT_OFF;
00495                 op[i].amp = 0.0;
00496                 op[i].step_amp = 0.0;
00497                 op[i].vol = 0.0;
00498                 op[i].tcount = 0;
00499                 op[i].tinc = 0;
00500                 op[i].toff = 0;
00501                 op[i].cur_wmask = wavemask[0];
00502                 op[i].cur_wform = &wavtable[waveform[0]];
00503                 op[i].freq_high = 0;
00504 
00505                 op[i].generator_pos = 0;
00506                 op[i].cur_env_step = 0;
00507                 op[i].env_step_a = 0;
00508                 op[i].env_step_d = 0;
00509                 op[i].env_step_r = 0;
00510                 op[i].step_skip_pos_a = 0;
00511                 op[i].env_step_skip_a = 0;
00512 
00513 #if defined(OPLTYPE_IS_OPL3)
00514                 op[i].is_4op = false;
00515                 op[i].is_4op_attached = false;
00516                 op[i].left_pan = 1;
00517                 op[i].right_pan = 1;
00518 #endif
00519         }
00520 
00521         recipsamp = 1.0 / (fltype)int_samplerate;
00522         for (i=15;i>=0;i--) {
00523                 frqmul[i] = (fltype)(frqmul_tab[i]*INTFREQU/(fltype)WAVEPREC*(fltype)FIXEDPT*recipsamp);
00524         }
00525 
00526         status = 0;
00527         opl_index = 0;
00528 
00529 
00530         // create vibrato table
00531         vib_table[0] = 8;
00532         vib_table[1] = 4;
00533         vib_table[2] = 0;
00534         vib_table[3] = -4;
00535         for (i=4; i<VIBTAB_SIZE; i++) vib_table[i] = vib_table[i-4]*-1;
00536 
00537         // vibrato at ~6.1 ?? (opl3 docs say 6.1, opl4 docs say 6.0, y8950 docs say 6.4)
00538         vibtab_add = static_cast<Bit32u>(VIBTAB_SIZE*FIXEDPT_LFO/8192*INTFREQU/int_samplerate);
00539         vibtab_pos = 0;
00540 
00541         for (i=0; i<BLOCKBUF_SIZE; i++) vibval_const[i] = 0;
00542 
00543 
00544         // create tremolo table
00545         Bit32s trem_table_int[TREMTAB_SIZE];
00546         for (i=0; i<14; i++)    trem_table_int[i] = i-13;               // upwards (13 to 26 -> -0.5/6 to 0)
00547         for (i=14; i<41; i++)   trem_table_int[i] = -i+14;              // downwards (26 to 0 -> 0 to -1/6)
00548         for (i=41; i<53; i++)   trem_table_int[i] = i-40-26;    // upwards (1 to 12 -> -1/6 to -0.5/6)
00549 
00550         for (i=0; i<TREMTAB_SIZE; i++) {
00551                 // 0.0 .. -26/26*4.8/6 == [0.0 .. -0.8], 4/53 steps == [1 .. 0.57]
00552                 fltype trem_val1=(fltype)(((fltype)trem_table_int[i])*4.8/26.0/6.0);                            // 4.8db
00553                 fltype trem_val2=(fltype)((fltype)((Bit32s)(trem_table_int[i]/4))*1.2/6.0/6.0);         // 1.2db (larger stepping)
00554 
00555                 trem_table[i] = (Bit32s)(pow(FL2,trem_val1)*FIXEDPT);
00556                 trem_table[TREMTAB_SIZE+i] = (Bit32s)(pow(FL2,trem_val2)*FIXEDPT);
00557         }
00558 
00559         // tremolo at 3.7hz
00560         tremtab_add = (Bit32u)((fltype)TREMTAB_SIZE * TREM_FREQ * FIXEDPT_LFO / (fltype)int_samplerate);
00561         tremtab_pos = 0;
00562 
00563         for (i=0; i<BLOCKBUF_SIZE; i++) tremval_const[i] = FIXEDPT;
00564 
00565 
00566         static Bitu initfirstime = 0;
00567         if (!initfirstime) {
00568                 initfirstime = 1;
00569 
00570                 // create waveform tables
00571                 for (i=0;i<(WAVEPREC>>1);i++) {
00572                         wavtable[(i<<1)  +WAVEPREC]     = (Bit16s)(16384*sin((fltype)(i<<1)*PI*2/WAVEPREC));
00573                         wavtable[(i<<1)+1+WAVEPREC]     = (Bit16s)(16384*sin((fltype)((i<<1)+1)*PI*2/WAVEPREC));
00574                         wavtable[i]                                     = wavtable[(i<<1)  +WAVEPREC];
00575                         // alternative: (zero-less)
00576 /*                      wavtable[(i<<1)  +WAVEPREC]     = (Bit16s)(16384*sin((fltype)((i<<2)+1)*PI/WAVEPREC));
00577                         wavtable[(i<<1)+1+WAVEPREC]     = (Bit16s)(16384*sin((fltype)((i<<2)+3)*PI/WAVEPREC));
00578                         wavtable[i]                                     = wavtable[(i<<1)-1+WAVEPREC]; */
00579                 }
00580                 for (i=0;i<(WAVEPREC>>3);i++) {
00581                         wavtable[i+(WAVEPREC<<1)]               = wavtable[i+(WAVEPREC>>3)]-16384;
00582                         wavtable[i+((WAVEPREC*17)>>3)]  = wavtable[i+(WAVEPREC>>2)]+16384;
00583                 }
00584 
00585                 // key scale level table verified ([table in book]*8/3)
00586                 kslev[7][0] = 0;        kslev[7][1] = 24;       kslev[7][2] = 32;       kslev[7][3] = 37;
00587                 kslev[7][4] = 40;       kslev[7][5] = 43;       kslev[7][6] = 45;       kslev[7][7] = 47;
00588                 kslev[7][8] = 48;
00589                 for (i=9;i<16;i++) kslev[7][i] = (Bit8u)(i+41);
00590                 for (Bits j=6;j>=0;j--) {
00591                         for (i=0;i<16;i++) {
00592                                 Bits oct = (Bits)kslev[j+1][i]-8;
00593                                 if (oct < 0) oct = 0;
00594                                 kslev[j][i] = (Bit8u)oct;
00595                         }
00596                 }
00597         }
00598 
00599 }
00600 
00601 
00602 
00603 void adlib_write(Bitu idx, Bit8u val) {
00604         Bit32u second_set = idx&0x100;
00605         adlibreg[idx] = val;
00606 
00607         switch (idx&0xf0) {
00608         case ARC_CONTROL:
00609                 // here we check for the second set registers, too:
00610                 switch (idx) {
00611                 case 0x02:      // timer1 counter
00612                 case 0x03:      // timer2 counter
00613                         break;
00614                 case 0x04:
00615                         // IRQ reset, timer mask/start
00616                         if (val&0x80) {
00617                                 // clear IRQ bits in status register
00618                                 status &= ~0x60;
00619                         } else {
00620                                 status = 0;
00621                         }
00622                         break;
00623 #if defined(OPLTYPE_IS_OPL3)
00624                 case 0x04|ARC_SECONDSET:
00625                         // 4op enable/disable switches for each possible channel
00626                         op[0].is_4op = (val&1)>0;
00627                         op[3].is_4op_attached = op[0].is_4op;
00628                         op[1].is_4op = (val&2)>0;
00629                         op[4].is_4op_attached = op[1].is_4op;
00630                         op[2].is_4op = (val&4)>0;
00631                         op[5].is_4op_attached = op[2].is_4op;
00632                         op[18].is_4op = (val&8)>0;
00633                         op[21].is_4op_attached = op[18].is_4op;
00634                         op[19].is_4op = (val&16)>0;
00635                         op[22].is_4op_attached = op[19].is_4op;
00636                         op[20].is_4op = (val&32)>0;
00637                         op[23].is_4op_attached = op[20].is_4op;
00638                         break;
00639                 case 0x05|ARC_SECONDSET:
00640                         break;
00641 #endif
00642                 case 0x08:
00643                         // CSW, note select
00644                         break;
00645                 default:
00646                         break;
00647                 }
00648                 break;
00649         case ARC_TVS_KSR_MUL:
00650         case ARC_TVS_KSR_MUL+0x10: {
00651                 // tremolo/vibrato/sustain keeping enabled; key scale rate; frequency multiplication
00652                 int num = idx&7;
00653                 Bitu base = (idx-ARC_TVS_KSR_MUL)&0xff;
00654                 if ((num<6) && (base<22)) {
00655                         Bitu modop = regbase2modop[second_set?(base+22):base];
00656                         Bitu regbase = base+second_set;
00657                         Bitu chanbase = second_set?(modop-18+ARC_SECONDSET):modop;
00658 
00659                         // change tremolo/vibrato and sustain keeping of this operator
00660                         op_type* op_ptr = &op[modop+((num<3) ? 0 : 9)];
00661                         change_keepsustain(regbase,op_ptr);
00662                         change_vibrato(regbase,op_ptr);
00663 
00664                         // change frequency calculations of this operator as
00665                         // key scale rate and frequency multiplicator can be changed
00666 #if defined(OPLTYPE_IS_OPL3)
00667                         if ((adlibreg[0x105]&1) && (op[modop].is_4op_attached)) {
00668                                 // operator uses frequency of channel
00669                                 change_frequency(chanbase-3,regbase,op_ptr);
00670                         } else {
00671                                 change_frequency(chanbase,regbase,op_ptr);
00672                         }
00673 #else
00674                         change_frequency(chanbase,base,op_ptr);
00675 #endif
00676                 }
00677                 }
00678                 break;
00679         case ARC_KSL_OUTLEV:
00680         case ARC_KSL_OUTLEV+0x10: {
00681                 // key scale level; output rate
00682                 int num = idx&7;
00683                 Bitu base = (idx-ARC_KSL_OUTLEV)&0xff;
00684                 if ((num<6) && (base<22)) {
00685                         Bitu modop = regbase2modop[second_set?(base+22):base];
00686                         Bitu chanbase = second_set?(modop-18+ARC_SECONDSET):modop;
00687 
00688                         // change frequency calculations of this operator as
00689                         // key scale level and output rate can be changed
00690                         op_type* op_ptr = &op[modop+((num<3) ? 0 : 9)];
00691 #if defined(OPLTYPE_IS_OPL3)
00692                         Bitu regbase = base+second_set;
00693                         if ((adlibreg[0x105]&1) && (op[modop].is_4op_attached)) {
00694                                 // operator uses frequency of channel
00695                                 change_frequency(chanbase-3,regbase,op_ptr);
00696                         } else {
00697                                 change_frequency(chanbase,regbase,op_ptr);
00698                         }
00699 #else
00700                         change_frequency(chanbase,base,op_ptr);
00701 #endif
00702                 }
00703                 }
00704                 break;
00705         case ARC_ATTR_DECR:
00706         case ARC_ATTR_DECR+0x10: {
00707                 // attack/decay rates
00708                 int num = idx&7;
00709                 Bitu base = (idx-ARC_ATTR_DECR)&0xff;
00710                 if ((num<6) && (base<22)) {
00711                         Bitu regbase = base+second_set;
00712 
00713                         // change attack rate and decay rate of this operator
00714                         op_type* op_ptr = &op[regbase2op[second_set?(base+22):base]];
00715                         change_attackrate(regbase,op_ptr);
00716                         change_decayrate(regbase,op_ptr);
00717                 }
00718                 }
00719                 break;
00720         case ARC_SUSL_RELR:
00721         case ARC_SUSL_RELR+0x10: {
00722                 // sustain level; release rate
00723                 int num = idx&7;
00724                 Bitu base = (idx-ARC_SUSL_RELR)&0xff;
00725                 if ((num<6) && (base<22)) {
00726                         Bitu regbase = base+second_set;
00727 
00728                         // change sustain level and release rate of this operator
00729                         op_type* op_ptr = &op[regbase2op[second_set?(base+22):base]];
00730                         change_releaserate(regbase,op_ptr);
00731                         change_sustainlevel(regbase,op_ptr);
00732                 }
00733                 }
00734                 break;
00735         case ARC_FREQ_NUM: {
00736                 // 0xa0-0xa8 low8 frequency
00737                 Bitu base = (idx-ARC_FREQ_NUM)&0xff;
00738                 if (base<9) {
00739                         Bits opbase = (Bits)(second_set?(base+18u):base);
00740 #if defined(OPLTYPE_IS_OPL3)
00741                         if ((adlibreg[0x105]&1) && op[opbase].is_4op_attached) break;
00742 #endif
00743                         // regbase of modulator:
00744                         Bits modbase = modulatorbase[base]+second_set;
00745 
00746                         Bitu chanbase = base+second_set;
00747 
00748                         change_frequency(chanbase,(Bitu)modbase,&op[opbase]);
00749                         change_frequency(chanbase,(Bitu)modbase+3,&op[opbase+9]);
00750 #if defined(OPLTYPE_IS_OPL3)
00751                         // for 4op channels all four operators are modified to the frequency of the channel
00752                         if ((adlibreg[0x105]&1) && op[second_set?(base+18):base].is_4op) {
00753                                 change_frequency(chanbase,(Bitu)modbase+8,&op[opbase+3]);
00754                                 change_frequency(chanbase,(Bitu)modbase+3+8,&op[opbase+3+9]);
00755                         }
00756 #endif
00757                 }
00758                 }
00759                 break;
00760         case ARC_KON_BNUM: {
00761                 if (idx == ARC_PERC_MODE) {
00762 #if defined(OPLTYPE_IS_OPL3)
00763                         if (second_set) return;
00764 #endif
00765 
00766                         if ((val&0x30) == 0x30) {               // BassDrum active
00767                                 enable_operator(16,&op[6],OP_ACT_PERC);
00768                                 change_frequency(6,16,&op[6]);
00769                                 enable_operator(16+3,&op[6+9],OP_ACT_PERC);
00770                                 change_frequency(6,16+3,&op[6+9]);
00771                         } else {
00772                                 disable_operator(&op[6],OP_ACT_PERC);
00773                                 disable_operator(&op[6+9],OP_ACT_PERC);
00774                         }
00775                         if ((val&0x28) == 0x28) {               // Snare active
00776                                 enable_operator(17+3,&op[16],OP_ACT_PERC);
00777                                 change_frequency(7,17+3,&op[16]);
00778                         } else {
00779                                 disable_operator(&op[16],OP_ACT_PERC);
00780                         }
00781                         if ((val&0x24) == 0x24) {               // TomTom active
00782                                 enable_operator(18,&op[8],OP_ACT_PERC);
00783                                 change_frequency(8,18,&op[8]);
00784                         } else {
00785                                 disable_operator(&op[8],OP_ACT_PERC);
00786                         }
00787                         if ((val&0x22) == 0x22) {               // Cymbal active
00788                                 enable_operator(18+3,&op[8+9],OP_ACT_PERC);
00789                                 change_frequency(8,18+3,&op[8+9]);
00790                         } else {
00791                                 disable_operator(&op[8+9],OP_ACT_PERC);
00792                         }
00793                         if ((val&0x21) == 0x21) {               // Hihat active
00794                                 enable_operator(17,&op[7],OP_ACT_PERC);
00795                                 change_frequency(7,17,&op[7]);
00796                         } else {
00797                                 disable_operator(&op[7],OP_ACT_PERC);
00798                         }
00799 
00800                         break;
00801                 }
00802                 // regular 0xb0-0xb8
00803                 Bitu base = (idx-ARC_KON_BNUM)&0xff;
00804                 if (base<9) {
00805                         Bits opbase = (Bits)(second_set?(base+18):base);
00806 #if defined(OPLTYPE_IS_OPL3)
00807                         if ((adlibreg[0x105]&1) && op[opbase].is_4op_attached) break;
00808 #endif
00809                         // regbase of modulator:
00810                         Bits modbase = modulatorbase[base]+second_set;
00811 
00812                         if (val&32) {
00813                                 // operator switched on
00814                                 enable_operator((Bitu)modbase,&op[opbase],OP_ACT_NORMAL);               // modulator (if 2op)
00815                                 enable_operator((Bitu)modbase+3,&op[opbase+9],OP_ACT_NORMAL);   // carrier (if 2op)
00816 #if defined(OPLTYPE_IS_OPL3)
00817                                 // for 4op channels all four operators are switched on
00818                                 if ((adlibreg[0x105]&1) && op[opbase].is_4op) {
00819                                         // turn on chan+3 operators as well
00820                                         enable_operator((Bitu)modbase+8,&op[opbase+3],OP_ACT_NORMAL);
00821                                         enable_operator((Bitu)modbase+3+8,&op[opbase+3+9],OP_ACT_NORMAL);
00822                                 }
00823 #endif
00824                         } else {
00825                                 // operator switched off
00826                                 disable_operator(&op[opbase],OP_ACT_NORMAL);
00827                                 disable_operator(&op[opbase+9],OP_ACT_NORMAL);
00828 #if defined(OPLTYPE_IS_OPL3)
00829                                 // for 4op channels all four operators are switched off
00830                                 if ((adlibreg[0x105]&1) && op[opbase].is_4op) {
00831                                         // turn off chan+3 operators as well
00832                                         disable_operator(&op[opbase+3],OP_ACT_NORMAL);
00833                                         disable_operator(&op[opbase+3+9],OP_ACT_NORMAL);
00834                                 }
00835 #endif
00836                         }
00837 
00838                         Bitu chanbase = base+second_set;
00839 
00840                         // change frequency calculations of modulator and carrier (2op) as
00841                         // the frequency of the channel has changed
00842                         change_frequency(chanbase,(Bitu)modbase,&op[opbase]);
00843                         change_frequency(chanbase,(Bitu)modbase+3,&op[opbase+9]);
00844 #if defined(OPLTYPE_IS_OPL3)
00845                         // for 4op channels all four operators are modified to the frequency of the channel
00846                         if ((adlibreg[0x105]&1) && op[second_set?(base+18):base].is_4op) {
00847                                 // change frequency calculations of chan+3 operators as well
00848                                 change_frequency(chanbase,(Bitu)modbase+8,&op[opbase+3]);
00849                                 change_frequency(chanbase,(Bitu)modbase+3+8,&op[opbase+3+9]);
00850                         }
00851 #endif
00852                 }
00853                 }
00854                 break;
00855         case ARC_FEEDBACK: {
00856                 // 0xc0-0xc8 feedback/modulation type (AM/FM)
00857                 Bitu base = (idx-ARC_FEEDBACK)&0xff;
00858                 if (base<9) {
00859                         Bits opbase = (Bits)(second_set?(base+18):base);
00860                         Bitu chanbase = base+second_set;
00861                         change_feedback(chanbase,&op[opbase]);
00862 #if defined(OPLTYPE_IS_OPL3)
00863                         // OPL3 panning
00864                         op[opbase].left_pan = ((val&0x10)>>4);
00865                         op[opbase].right_pan = ((val&0x20)>>5);
00866 #endif
00867                 }
00868                 }
00869                 break;
00870         case ARC_WAVE_SEL:
00871         case ARC_WAVE_SEL+0x10: {
00872                 int num = idx&7;
00873                 Bitu base = (idx-ARC_WAVE_SEL)&0xff;
00874                 if ((num<6) && (base<22)) {
00875 #if defined(OPLTYPE_IS_OPL3)
00876                         Bits wselbase = (Bits)(second_set?(base+22):base);      // for easier mapping onto wave_sel[]
00877                         // change waveform
00878                         if (adlibreg[0x105]&1) wave_sel[wselbase] = val&7;      // opl3 mode enabled, all waveforms accessible
00879                         else wave_sel[wselbase] = val&3;
00880                         op_type* op_ptr = &op[regbase2modop[wselbase]+((num<3) ? 0 : 9)];
00881                         change_waveform((Bitu)wselbase,op_ptr);
00882 #else
00883                         if (adlibreg[0x01]&0x20) {
00884                                 // wave selection enabled, change waveform
00885                                 wave_sel[base] = val&3;
00886                                 op_type* op_ptr = &op[regbase2modop[base]+((num<3) ? 0 : 9)];
00887                                 change_waveform(base,op_ptr);
00888                         }
00889 #endif
00890                 }
00891                 }
00892                 break;
00893         default:
00894                 break;
00895         }
00896 }
00897 
00898 
00899 Bitu adlib_reg_read(Bitu port) {
00900 #if defined(OPLTYPE_IS_OPL3)
00901         // opl3-detection routines require ret&6 to be zero
00902         if ((port&1)==0) {
00903                 return status;
00904         }
00905         return 0x00;
00906 #else
00907         // opl2-detection routines require ret&6 to be 6
00908         if ((port&1)==0) {
00909                 return status|6;
00910         }
00911         return 0xff;
00912 #endif
00913 }
00914 
00915 void adlib_write_index(Bitu port, Bit8u val) {
00916     (void)port;//POSSIBLY UNUSED
00917         opl_index = val;
00918 #if defined(OPLTYPE_IS_OPL3)
00919         if ((port&3)!=0) {
00920                 // possibly second set
00921                 if (((adlibreg[0x105]&1)!=0) || (opl_index==5)) opl_index |= ARC_SECONDSET;
00922         }
00923 #endif
00924 }
00925 
00926 static void OPL_INLINE clipit16(Bit32s ival, Bit16s* outval) {
00927         if (ival<32768) {
00928                 if (ival>-32769) {
00929                         *outval=(Bit16s)ival;
00930                 } else {
00931                         *outval = -32768;
00932                 }
00933         } else {
00934                 *outval = 32767;
00935         }
00936 }
00937 
00938 
00939 
00940 // be careful with this
00941 // uses cptr and chanval, outputs into outbufl(/outbufr)
00942 // for opl3 check if opl3-mode is enabled (which uses stereo panning)
00943 #undef CHANVAL_OUT
00944 #if defined(OPLTYPE_IS_OPL3)
00945 #define CHANVAL_OUT                                                                     \
00946         if (adlibreg[0x105]&1) {                                                \
00947                 outbufl[i] += chanval*cptr[0].left_pan;         \
00948                 outbufr[i] += chanval*cptr[0].right_pan;        \
00949         } else {                                                                                \
00950                 outbufl[i] += chanval;                                          \
00951         }
00952 #else
00953 #define CHANVAL_OUT                                                                     \
00954         outbufl[i] += chanval;
00955 #endif
00956 
00957 void adlib_getsample(Bit16s* sndptr, Bits numsamples) {
00958         Bits i, endsamples;
00959         op_type* cptr;
00960 
00961         Bit32s outbufl[BLOCKBUF_SIZE];
00962 #if defined(OPLTYPE_IS_OPL3)
00963         // second output buffer (right channel for opl3 stereo)
00964         Bit32s outbufr[BLOCKBUF_SIZE];
00965 #endif
00966 
00967         // vibrato/tremolo lookup tables (global, to possibly be used by all operators)
00968         Bit32s vib_lut[BLOCKBUF_SIZE];
00969         Bit32s trem_lut[BLOCKBUF_SIZE];
00970 
00971         Bits samples_to_process = numsamples;
00972 
00973         for (Bits cursmp=0; cursmp<samples_to_process; cursmp+=endsamples) {
00974                 endsamples = samples_to_process-cursmp;
00975                 if (endsamples>BLOCKBUF_SIZE) endsamples = BLOCKBUF_SIZE;
00976 
00977                 memset((void*)&outbufl,0,(unsigned int)endsamples*sizeof(Bit32s));
00978 #if defined(OPLTYPE_IS_OPL3)
00979                 // clear second output buffer (opl3 stereo)
00980                 if (adlibreg[0x105]&1) memset((void*)&outbufr,0,(unsigned int)endsamples*sizeof(Bit32s));
00981 #endif
00982 
00983                 // calculate vibrato/tremolo lookup tables
00984                 Bit32s vib_tshift = ((adlibreg[ARC_PERC_MODE]&0x40)==0) ? 1 : 0;        // 14cents/7cents switching
00985                 for (i=0;i<endsamples;i++) {
00986                         // cycle through vibrato table
00987                         vibtab_pos += vibtab_add;
00988                         if (vibtab_pos/FIXEDPT_LFO>=VIBTAB_SIZE) vibtab_pos-=VIBTAB_SIZE*FIXEDPT_LFO;
00989                         vib_lut[i] = vib_table[vibtab_pos/FIXEDPT_LFO]>>vib_tshift;             // 14cents (14/100 of a semitone) or 7cents
00990 
00991                         // cycle through tremolo table
00992                         tremtab_pos += tremtab_add;
00993                         if (tremtab_pos/FIXEDPT_LFO>=TREMTAB_SIZE) tremtab_pos-=TREMTAB_SIZE*FIXEDPT_LFO;
00994                         if (adlibreg[ARC_PERC_MODE]&0x80) trem_lut[i] = trem_table[tremtab_pos/FIXEDPT_LFO];
00995                         else trem_lut[i] = trem_table[TREMTAB_SIZE+tremtab_pos/FIXEDPT_LFO];
00996                 }
00997 
00998                 if (adlibreg[ARC_PERC_MODE]&0x20) {
00999                         //BassDrum
01000                         cptr = &op[6];
01001                         if (adlibreg[ARC_FEEDBACK+6]&1) {
01002                                 // additive synthesis
01003                                 if (cptr[9].op_state != OF_TYPE_OFF) {
01004                                         if (cptr[9].vibrato) {
01005                                                 vibval1 = vibval_var1;
01006                                                 for (i=0;i<endsamples;i++)
01007                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01008                                         } else vibval1 = vibval_const;
01009                                         if (cptr[9].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01010                                         else tremval1 = tremval_const;
01011 
01012                                         // calculate channel output
01013                                         for (i=0;i<endsamples;i++) {
01014                                                 operator_advance(&cptr[9],vibval1[i]);
01015                                                 opfuncs[cptr[9].op_state](&cptr[9]);
01016                                                 operator_output(&cptr[9],0,tremval1[i]);
01017                                                 
01018                                                 Bit32s chanval = cptr[9].cval*2;
01019                                                 CHANVAL_OUT
01020                                         }
01021                                 }
01022                         } else {
01023                                 // frequency modulation
01024                                 if ((cptr[9].op_state != OF_TYPE_OFF) || (cptr[0].op_state != OF_TYPE_OFF)) {
01025                                         if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
01026                                                 vibval1 = vibval_var1;
01027                                                 for (i=0;i<endsamples;i++)
01028                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01029                                         } else vibval1 = vibval_const;
01030                                         if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01031                                                 vibval2 = vibval_var2;
01032                                                 for (i=0;i<endsamples;i++)
01033                                                         vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01034                                         } else vibval2 = vibval_const;
01035                                         if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01036                                         else tremval1 = tremval_const;
01037                                         if (cptr[9].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01038                                         else tremval2 = tremval_const;
01039 
01040                                         // calculate channel output
01041                                         for (i=0;i<endsamples;i++) {
01042                                                 operator_advance(&cptr[0],vibval1[i]);
01043                                                 opfuncs[cptr[0].op_state](&cptr[0]);
01044                                                 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01045 
01046                                                 operator_advance(&cptr[9],vibval2[i]);
01047                                                 opfuncs[cptr[9].op_state](&cptr[9]);
01048                                                 operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
01049                                                 
01050                                                 Bit32s chanval = cptr[9].cval*2;
01051                                                 CHANVAL_OUT
01052                                         }
01053                                 }
01054                         }
01055 
01056                         //TomTom (j=8)
01057                         if (op[8].op_state != OF_TYPE_OFF) {
01058                                 cptr = &op[8];
01059                                 if (cptr[0].vibrato) {
01060                                         vibval3 = vibval_var1;
01061                                         for (i=0;i<endsamples;i++)
01062                                                 vibval3[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01063                                 } else vibval3 = vibval_const;
01064 
01065                                 if (cptr[0].tremolo) tremval3 = trem_lut;       // tremolo enabled, use table
01066                                 else tremval3 = tremval_const;
01067 
01068                                 // calculate channel output
01069                                 for (i=0;i<endsamples;i++) {
01070                                         operator_advance(&cptr[0],vibval3[i]);
01071                                         opfuncs[cptr[0].op_state](&cptr[0]);            //TomTom
01072                                         operator_output(&cptr[0],0,tremval3[i]);
01073                                         Bit32s chanval = cptr[0].cval*2;
01074                                         CHANVAL_OUT
01075                                 }
01076                         }
01077 
01078                         //Snare/Hihat (j=7), Cymbal (j=8)
01079                         if ((op[7].op_state != OF_TYPE_OFF) || (op[16].op_state != OF_TYPE_OFF) ||
01080                                 (op[17].op_state != OF_TYPE_OFF)) {
01081                                 cptr = &op[7];
01082                                 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
01083                                         vibval1 = vibval_var1;
01084                                         for (i=0;i<endsamples;i++)
01085                                                 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01086                                 } else vibval1 = vibval_const;
01087                                 if ((cptr[9].vibrato) && (cptr[9].op_state == OF_TYPE_OFF)) {
01088                                         vibval2 = vibval_var2;
01089                                         for (i=0;i<endsamples;i++)
01090                                                 vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01091                                 } else vibval2 = vibval_const;
01092 
01093                                 if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01094                                 else tremval1 = tremval_const;
01095                                 if (cptr[9].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01096                                 else tremval2 = tremval_const;
01097 
01098                                 cptr = &op[8];
01099                                 if ((cptr[9].vibrato) && (cptr[9].op_state == OF_TYPE_OFF)) {
01100                                         vibval4 = vibval_var2;
01101                                         for (i=0;i<endsamples;i++)
01102                                                 vibval4[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01103                                 } else vibval4 = vibval_const;
01104 
01105                                 if (cptr[9].tremolo) tremval4 = trem_lut;       // tremolo enabled, use table
01106                                 else tremval4 = tremval_const;
01107 
01108                                 // calculate channel output
01109                                 for (i=0;i<endsamples;i++) {
01110                                         operator_advance_drums(&op[7],vibval1[i],&op[7+9],vibval2[i],&op[8+9],vibval4[i]);
01111 
01112                                         opfuncs[op[7].op_state](&op[7]);                        //Hihat
01113                                         operator_output(&op[7],0,tremval1[i]);
01114 
01115                                         opfuncs[op[7+9].op_state](&op[7+9]);            //Snare
01116                                         operator_output(&op[7+9],0,tremval2[i]);
01117 
01118                                         opfuncs[op[8+9].op_state](&op[8+9]);            //Cymbal
01119                                         operator_output(&op[8+9],0,tremval4[i]);
01120 
01121                                         Bit32s chanval = (op[7].cval + op[7+9].cval + op[8+9].cval)*2;
01122                                         CHANVAL_OUT
01123                                 }
01124                         }
01125                 }
01126 
01127                 Bitu max_channel = NUM_CHANNELS;
01128 #if defined(OPLTYPE_IS_OPL3)
01129                 if ((adlibreg[0x105]&1)==0) max_channel = NUM_CHANNELS/2;
01130 #endif
01131                 for (Bits cur_ch=(Bits)max_channel-1; cur_ch>=0; cur_ch--) {
01132                         // skip drum/percussion operators
01133                         if ((adlibreg[ARC_PERC_MODE]&0x20) && (cur_ch >= 6) && (cur_ch < 9)) continue;
01134 
01135                         Bitu k = (Bitu)cur_ch;
01136 #if defined(OPLTYPE_IS_OPL3)
01137                         if (cur_ch < 9) {
01138                                 cptr = &op[cur_ch];
01139                         } else {
01140                                 cptr = &op[cur_ch+9];   // second set is operator18-operator35
01141                                 k += (-9+256);          // second set uses registers 0x100 onwards
01142                         }
01143                         // check if this operator is part of a 4-op
01144                         if ((adlibreg[0x105]&1) && cptr->is_4op_attached) continue;
01145 #else
01146                         cptr = &op[cur_ch];
01147 #endif
01148 
01149                         // check for FM/AM
01150                         if (adlibreg[ARC_FEEDBACK+k]&1) {
01151 #if defined(OPLTYPE_IS_OPL3)
01152                                 if ((adlibreg[0x105]&1) && cptr->is_4op) {
01153                                         if (adlibreg[ARC_FEEDBACK+k+3]&1) {
01154                                                 // AM-AM-style synthesis (op1[fb] + (op2 * op3) + op4)
01155                                                 if (cptr[0].op_state != OF_TYPE_OFF) {
01156                                                         if (cptr[0].vibrato) {
01157                                                                 vibval1 = vibval_var1;
01158                                                                 for (i=0;i<endsamples;i++)
01159                                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01160                                                         } else vibval1 = vibval_const;
01161                                                         if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01162                                                         else tremval1 = tremval_const;
01163 
01164                                                         // calculate channel output
01165                                                         for (i=0;i<endsamples;i++) {
01166                                                                 operator_advance(&cptr[0],vibval1[i]);
01167                                                                 opfuncs[cptr[0].op_state](&cptr[0]);
01168                                                                 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01169 
01170                                                                 Bit32s chanval = cptr[0].cval;
01171                                                                 CHANVAL_OUT
01172                                                         }
01173                                                 }
01174 
01175                                                 if ((cptr[3].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF)) {
01176                                                         if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01177                                                                 vibval1 = vibval_var1;
01178                                                                 for (i=0;i<endsamples;i++)
01179                                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01180                                                         } else vibval1 = vibval_const;
01181                                                         if (cptr[9].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01182                                                         else tremval1 = tremval_const;
01183                                                         if (cptr[3].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01184                                                         else tremval2 = tremval_const;
01185 
01186                                                         // calculate channel output
01187                                                         for (i=0;i<endsamples;i++) {
01188                                                                 operator_advance(&cptr[9],vibval1[i]);
01189                                                                 opfuncs[cptr[9].op_state](&cptr[9]);
01190                                                                 operator_output(&cptr[9],0,tremval1[i]);
01191 
01192                                                                 operator_advance(&cptr[3],0);
01193                                                                 opfuncs[cptr[3].op_state](&cptr[3]);
01194                                                                 operator_output(&cptr[3],cptr[9].cval*FIXEDPT,tremval2[i]);
01195 
01196                                                                 Bit32s chanval = cptr[3].cval;
01197                                                                 CHANVAL_OUT
01198                                                         }
01199                                                 }
01200 
01201                                                 if (cptr[3+9].op_state != OF_TYPE_OFF) {
01202                                                         if (cptr[3+9].tremolo) tremval1 = trem_lut;     // tremolo enabled, use table
01203                                                         else tremval1 = tremval_const;
01204 
01205                                                         // calculate channel output
01206                                                         for (i=0;i<endsamples;i++) {
01207                                                                 operator_advance(&cptr[3+9],0);
01208                                                                 opfuncs[cptr[3+9].op_state](&cptr[3+9]);
01209                                                                 operator_output(&cptr[3+9],0,tremval1[i]);
01210 
01211                                                                 Bit32s chanval = cptr[3+9].cval;
01212                                                                 CHANVAL_OUT
01213                                                         }
01214                                                 }
01215                                         } else {
01216                                                 // AM-FM-style synthesis (op1[fb] + (op2 * op3 * op4))
01217                                                 if (cptr[0].op_state != OF_TYPE_OFF) {
01218                                                         if (cptr[0].vibrato) {
01219                                                                 vibval1 = vibval_var1;
01220                                                                 for (i=0;i<endsamples;i++)
01221                                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01222                                                         } else vibval1 = vibval_const;
01223                                                         if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01224                                                         else tremval1 = tremval_const;
01225 
01226                                                         // calculate channel output
01227                                                         for (i=0;i<endsamples;i++) {
01228                                                                 operator_advance(&cptr[0],vibval1[i]);
01229                                                                 opfuncs[cptr[0].op_state](&cptr[0]);
01230                                                                 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01231 
01232                                                                 Bit32s chanval = cptr[0].cval;
01233                                                                 CHANVAL_OUT
01234                                                         }
01235                                                 }
01236 
01237                                                 if ((cptr[9].op_state != OF_TYPE_OFF) || (cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
01238                                                         if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01239                                                                 vibval1 = vibval_var1;
01240                                                                 for (i=0;i<endsamples;i++)
01241                                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01242                                                         } else vibval1 = vibval_const;
01243                                                         if (cptr[9].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01244                                                         else tremval1 = tremval_const;
01245                                                         if (cptr[3].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01246                                                         else tremval2 = tremval_const;
01247                                                         if (cptr[3+9].tremolo) tremval3 = trem_lut;     // tremolo enabled, use table
01248                                                         else tremval3 = tremval_const;
01249 
01250                                                         // calculate channel output
01251                                                         for (i=0;i<endsamples;i++) {
01252                                                                 operator_advance(&cptr[9],vibval1[i]);
01253                                                                 opfuncs[cptr[9].op_state](&cptr[9]);
01254                                                                 operator_output(&cptr[9],0,tremval1[i]);
01255 
01256                                                                 operator_advance(&cptr[3],0);
01257                                                                 opfuncs[cptr[3].op_state](&cptr[3]);
01258                                                                 operator_output(&cptr[3],cptr[9].cval*FIXEDPT,tremval2[i]);
01259 
01260                                                                 operator_advance(&cptr[3+9],0);
01261                                                                 opfuncs[cptr[3+9].op_state](&cptr[3+9]);
01262                                                                 operator_output(&cptr[3+9],cptr[3].cval*FIXEDPT,tremval3[i]);
01263 
01264                                                                 Bit32s chanval = cptr[3+9].cval;
01265                                                                 CHANVAL_OUT
01266                                                         }
01267                                                 }
01268                                         }
01269                                         continue;
01270                                 }
01271 #endif
01272                                 // 2op additive synthesis
01273                                 if ((cptr[9].op_state == OF_TYPE_OFF) && (cptr[0].op_state == OF_TYPE_OFF)) continue;
01274                                 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
01275                                         vibval1 = vibval_var1;
01276                                         for (i=0;i<endsamples;i++)
01277                                                 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01278                                 } else vibval1 = vibval_const;
01279                                 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01280                                         vibval2 = vibval_var2;
01281                                         for (i=0;i<endsamples;i++)
01282                                                 vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01283                                 } else vibval2 = vibval_const;
01284                                 if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01285                                 else tremval1 = tremval_const;
01286                                 if (cptr[9].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01287                                 else tremval2 = tremval_const;
01288 
01289                                 // calculate channel output
01290                                 for (i=0;i<endsamples;i++) {
01291                                         // carrier1
01292                                         operator_advance(&cptr[0],vibval1[i]);
01293                                         opfuncs[cptr[0].op_state](&cptr[0]);
01294                                         operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01295 
01296                                         // carrier2
01297                                         operator_advance(&cptr[9],vibval2[i]);
01298                                         opfuncs[cptr[9].op_state](&cptr[9]);
01299                                         operator_output(&cptr[9],0,tremval2[i]);
01300 
01301                                         Bit32s chanval = cptr[9].cval + cptr[0].cval;
01302                                         CHANVAL_OUT
01303                                 }
01304                         } else {
01305 #if defined(OPLTYPE_IS_OPL3)
01306                                 if ((adlibreg[0x105]&1) && cptr->is_4op) {
01307                                         if (adlibreg[ARC_FEEDBACK+k+3]&1) {
01308                                                 // FM-AM-style synthesis ((op1[fb] * op2) + (op3 * op4))
01309                                                 if ((cptr[0].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF)) {
01310                                                         if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
01311                                                                 vibval1 = vibval_var1;
01312                                                                 for (i=0;i<endsamples;i++)
01313                                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01314                                                         } else vibval1 = vibval_const;
01315                                                         if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01316                                                                 vibval2 = vibval_var2;
01317                                                                 for (i=0;i<endsamples;i++)
01318                                                                         vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01319                                                         } else vibval2 = vibval_const;
01320                                                         if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01321                                                         else tremval1 = tremval_const;
01322                                                         if (cptr[9].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01323                                                         else tremval2 = tremval_const;
01324 
01325                                                         // calculate channel output
01326                                                         for (i=0;i<endsamples;i++) {
01327                                                                 operator_advance(&cptr[0],vibval1[i]);
01328                                                                 opfuncs[cptr[0].op_state](&cptr[0]);
01329                                                                 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01330 
01331                                                                 operator_advance(&cptr[9],vibval2[i]);
01332                                                                 opfuncs[cptr[9].op_state](&cptr[9]);
01333                                                                 operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
01334 
01335                                                                 Bit32s chanval = cptr[9].cval;
01336                                                                 CHANVAL_OUT
01337                                                         }
01338                                                 }
01339 
01340                                                 if ((cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
01341                                                         if (cptr[3].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01342                                                         else tremval1 = tremval_const;
01343                                                         if (cptr[3+9].tremolo) tremval2 = trem_lut;     // tremolo enabled, use table
01344                                                         else tremval2 = tremval_const;
01345 
01346                                                         // calculate channel output
01347                                                         for (i=0;i<endsamples;i++) {
01348                                                                 operator_advance(&cptr[3],0);
01349                                                                 opfuncs[cptr[3].op_state](&cptr[3]);
01350                                                                 operator_output(&cptr[3],0,tremval1[i]);
01351 
01352                                                                 operator_advance(&cptr[3+9],0);
01353                                                                 opfuncs[cptr[3+9].op_state](&cptr[3+9]);
01354                                                                 operator_output(&cptr[3+9],cptr[3].cval*FIXEDPT,tremval2[i]);
01355 
01356                                                                 Bit32s chanval = cptr[3+9].cval;
01357                                                                 CHANVAL_OUT
01358                                                         }
01359                                                 }
01360 
01361                                         } else {
01362                                                 // FM-FM-style synthesis (op1[fb] * op2 * op3 * op4)
01363                                                 if ((cptr[0].op_state != OF_TYPE_OFF) || (cptr[9].op_state != OF_TYPE_OFF) || 
01364                                                         (cptr[3].op_state != OF_TYPE_OFF) || (cptr[3+9].op_state != OF_TYPE_OFF)) {
01365                                                         if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
01366                                                                 vibval1 = vibval_var1;
01367                                                                 for (i=0;i<endsamples;i++)
01368                                                                         vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01369                                                         } else vibval1 = vibval_const;
01370                                                         if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01371                                                                 vibval2 = vibval_var2;
01372                                                                 for (i=0;i<endsamples;i++)
01373                                                                         vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01374                                                         } else vibval2 = vibval_const;
01375                                                         if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01376                                                         else tremval1 = tremval_const;
01377                                                         if (cptr[9].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01378                                                         else tremval2 = tremval_const;
01379                                                         if (cptr[3].tremolo) tremval3 = trem_lut;       // tremolo enabled, use table
01380                                                         else tremval3 = tremval_const;
01381                                                         if (cptr[3+9].tremolo) tremval4 = trem_lut;     // tremolo enabled, use table
01382                                                         else tremval4 = tremval_const;
01383 
01384                                                         // calculate channel output
01385                                                         for (i=0;i<endsamples;i++) {
01386                                                                 operator_advance(&cptr[0],vibval1[i]);
01387                                                                 opfuncs[cptr[0].op_state](&cptr[0]);
01388                                                                 operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01389 
01390                                                                 operator_advance(&cptr[9],vibval2[i]);
01391                                                                 opfuncs[cptr[9].op_state](&cptr[9]);
01392                                                                 operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
01393 
01394                                                                 operator_advance(&cptr[3],0);
01395                                                                 opfuncs[cptr[3].op_state](&cptr[3]);
01396                                                                 operator_output(&cptr[3],cptr[9].cval*FIXEDPT,tremval3[i]);
01397 
01398                                                                 operator_advance(&cptr[3+9],0);
01399                                                                 opfuncs[cptr[3+9].op_state](&cptr[3+9]);
01400                                                                 operator_output(&cptr[3+9],cptr[3].cval*FIXEDPT,tremval4[i]);
01401 
01402                                                                 Bit32s chanval = cptr[3+9].cval;
01403                                                                 CHANVAL_OUT
01404                                                         }
01405                                                 }
01406                                         }
01407                                         continue;
01408                                 }
01409 #endif
01410                                 // 2op frequency modulation
01411                                 if ((cptr[9].op_state == OF_TYPE_OFF) && (cptr[0].op_state == OF_TYPE_OFF)) continue;
01412                                 if ((cptr[0].vibrato) && (cptr[0].op_state != OF_TYPE_OFF)) {
01413                                         vibval1 = vibval_var1;
01414                                         for (i=0;i<endsamples;i++)
01415                                                 vibval1[i] = (Bit32s)((vib_lut[i]*cptr[0].freq_high/8)*FIXEDPT*VIBFAC);
01416                                 } else vibval1 = vibval_const;
01417                                 if ((cptr[9].vibrato) && (cptr[9].op_state != OF_TYPE_OFF)) {
01418                                         vibval2 = vibval_var2;
01419                                         for (i=0;i<endsamples;i++)
01420                                                 vibval2[i] = (Bit32s)((vib_lut[i]*cptr[9].freq_high/8)*FIXEDPT*VIBFAC);
01421                                 } else vibval2 = vibval_const;
01422                                 if (cptr[0].tremolo) tremval1 = trem_lut;       // tremolo enabled, use table
01423                                 else tremval1 = tremval_const;
01424                                 if (cptr[9].tremolo) tremval2 = trem_lut;       // tremolo enabled, use table
01425                                 else tremval2 = tremval_const;
01426 
01427                                 // calculate channel output
01428                                 for (i=0;i<endsamples;i++) {
01429                                         // modulator
01430                                         operator_advance(&cptr[0],vibval1[i]);
01431                                         opfuncs[cptr[0].op_state](&cptr[0]);
01432                                         operator_output(&cptr[0],(cptr[0].lastcval+cptr[0].cval)*cptr[0].mfbi/2,tremval1[i]);
01433 
01434                                         // carrier
01435                                         operator_advance(&cptr[9],vibval2[i]);
01436                                         opfuncs[cptr[9].op_state](&cptr[9]);
01437                                         operator_output(&cptr[9],cptr[0].cval*FIXEDPT,tremval2[i]);
01438 
01439                                         Bit32s chanval = cptr[9].cval;
01440                                         CHANVAL_OUT
01441                                 }
01442                         }
01443                 }
01444 
01445 #if defined(OPLTYPE_IS_OPL3)
01446                 if (adlibreg[0x105]&1) {
01447                         // convert to 16bit samples (stereo)
01448                         for (i=0;i<endsamples;i++) {
01449                                 clipit16(outbufl[i],sndptr++);
01450                                 clipit16(outbufr[i],sndptr++);
01451                         }
01452                 } else {
01453                         // convert to 16bit samples (mono)
01454                         for (i=0;i<endsamples;i++) {
01455                                 clipit16(outbufl[i],sndptr++);
01456                                 clipit16(outbufl[i],sndptr++);
01457                         }
01458                 }
01459 #else
01460                 // convert to 16bit samples
01461                 for (i=0;i<endsamples;i++)
01462                         clipit16(outbufl[i],sndptr++);
01463 #endif
01464 
01465         }
01466 }
01467 
01468 // save state support
01469 void adlib_savestate( std::ostream& stream )
01470 {
01471         Bit32u cur_wform_idx[MAXOPERATORS];
01472 
01473 
01474         for( int lcv=0; lcv<MAXOPERATORS; lcv++ ) {
01475                 cur_wform_idx[lcv] = ((Bitu) (op[lcv].cur_wform)) - ((Bitu) &wavtable);
01476         }
01477 
01478         //****************************************************
01479         //****************************************************
01480         //****************************************************
01481 
01482         // opl.cpp
01483 
01484         // - pure data
01485         WRITE_POD( &recipsamp, recipsamp );
01486         WRITE_POD( &wavtable, wavtable );
01487 
01488         WRITE_POD( &vibval_var1, vibval_var1 );
01489         WRITE_POD( &vibval_var2, vibval_var2 );
01490 
01491         //****************************************************
01492         //****************************************************
01493         //****************************************************
01494 
01495         // opl.h
01496 
01497         // - pure data
01498         WRITE_POD( &chip_num, chip_num );
01499 
01500         // - near-pure data
01501         WRITE_POD( &op, op );
01502 
01503         // - pure data
01504         WRITE_POD( &int_samplerate, int_samplerate );
01505         WRITE_POD( &status, status );
01506         WRITE_POD( &opl_index, opl_index );
01507         WRITE_POD( &adlibreg, adlibreg );
01508         WRITE_POD( &wave_sel, wave_sel );
01509 
01510         WRITE_POD( &vibtab_pos, vibtab_pos );
01511         WRITE_POD( &vibtab_add, vibtab_add );
01512         WRITE_POD( &tremtab_pos, tremtab_pos );
01513         WRITE_POD( &tremtab_add, tremtab_add );
01514         WRITE_POD( &generator_add, generator_add );
01515 
01516 
01517 
01518 
01519         // - reloc ptr (!!!)
01520         WRITE_POD( &cur_wform_idx, cur_wform_idx );
01521 }
01522 
01523 
01524 void adlib_loadstate( std::istream& stream )
01525 {
01526         Bit32u cur_wform_idx[MAXOPERATORS];
01527 
01528         //****************************************************
01529         //****************************************************
01530         //****************************************************
01531 
01532         // opl.cpp
01533 
01534         // - pure data
01535         READ_POD( &recipsamp, recipsamp );
01536         READ_POD( &wavtable, wavtable );
01537 
01538         READ_POD( &vibval_var1, vibval_var1 );
01539         READ_POD( &vibval_var2, vibval_var2 );
01540 
01541         //****************************************************
01542         //****************************************************
01543         //****************************************************
01544 
01545         // opl.h
01546 
01547         // - pure data
01548         READ_POD( &chip_num, chip_num );
01549 
01550         // - near-pure data
01551         READ_POD( &op, op );
01552 
01553         // - pure data
01554         READ_POD( &int_samplerate, int_samplerate );
01555         READ_POD( &status, status );
01556         READ_POD( &opl_index, opl_index );
01557         READ_POD( &adlibreg, adlibreg );
01558         READ_POD( &wave_sel, wave_sel );
01559 
01560         READ_POD( &vibtab_pos, vibtab_pos );
01561         READ_POD( &vibtab_add, vibtab_add );
01562         READ_POD( &tremtab_pos, tremtab_pos );
01563         READ_POD( &tremtab_add, tremtab_add );
01564         READ_POD( &generator_add, generator_add );
01565 
01566 
01567 
01568 
01569         // - reloc ptr (!!!)
01570         READ_POD( &cur_wform_idx, cur_wform_idx );
01571 
01572         //****************************************************
01573         //****************************************************
01574         //****************************************************
01575 
01576         for( int lcv=0; lcv<MAXOPERATORS; lcv++ ) {
01577                 op[lcv].cur_wform = (Bit16s *) ((Bitu) &wavtable + cur_wform_idx[lcv]);
01578         }
01579 }