~ubuntu-branches/debian/stretch/opentyrian/stretch

« back to all changes in this revision

Viewing changes to src/opl.c

  • Committer: Package Import Robot
  • Author(s): Etienne Millon
  • Date: 2015-03-31 08:48:54 UTC
  • Revision ID: package-import@ubuntu.com-20150331084854-f5a4uoz7uv3vopk6
Tags: upstream-2.1.20130907+dfsg
ImportĀ upstreamĀ versionĀ 2.1.20130907+dfsg

Show diffs side-by-side

added added

removed removed

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