diff options
Diffstat (limited to 'lib/rbcodec/codecs/libgme/emu2413.c')
-rw-r--r-- | lib/rbcodec/codecs/libgme/emu2413.c | 1981 |
1 files changed, 1981 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/emu2413.c b/lib/rbcodec/codecs/libgme/emu2413.c new file mode 100644 index 0000000000..01075821cb --- /dev/null +++ b/lib/rbcodec/codecs/libgme/emu2413.c | |||
@@ -0,0 +1,1981 @@ | |||
1 | /*********************************************************************************** | ||
2 | |||
3 | emu2413.c -- YM2413 emulator written by Mitsutaka Okazaki 2001 | ||
4 | |||
5 | 2001 01-08 : Version 0.10 -- 1st version. | ||
6 | 2001 01-15 : Version 0.20 -- semi-public version. | ||
7 | 2001 01-16 : Version 0.30 -- 1st public version. | ||
8 | 2001 01-17 : Version 0.31 -- Fixed bassdrum problem. | ||
9 | : Version 0.32 -- LPF implemented. | ||
10 | 2001 01-18 : Version 0.33 -- Fixed the drum problem, refine the mix-down method. | ||
11 | -- Fixed the LFO bug. | ||
12 | 2001 01-24 : Version 0.35 -- Fixed the drum problem, | ||
13 | support undocumented EG behavior. | ||
14 | 2001 02-02 : Version 0.38 -- Improved the performance. | ||
15 | Fixed the hi-hat and cymbal model. | ||
16 | Fixed the default percussive datas. | ||
17 | Noise reduction. | ||
18 | Fixed the feedback problem. | ||
19 | 2001 03-03 : Version 0.39 -- Fixed some drum bugs. | ||
20 | Improved the performance. | ||
21 | 2001 03-04 : Version 0.40 -- Improved the feedback. | ||
22 | Change the default table size. | ||
23 | Clock and Rate can be changed during play. | ||
24 | 2001 06-24 : Version 0.50 -- Improved the hi-hat and the cymbal tone. | ||
25 | Added VRC7 patch (OPLL_reset_patch is changed). | ||
26 | Fixed OPLL_reset() bug. | ||
27 | Added OPLL_setMask, OPLL_getMask and OPLL_toggleMask. | ||
28 | Added OPLL_writeIO. | ||
29 | 2001 09-28 : Version 0.51 -- Removed the noise table. | ||
30 | 2002 01-28 : Version 0.52 -- Added Stereo mode. | ||
31 | 2002 02-07 : Version 0.53 -- Fixed some drum bugs. | ||
32 | 2002 02-20 : Version 0.54 -- Added the best quality mode. | ||
33 | 2002 03-02 : Version 0.55 -- Removed OPLL_init & OPLL_close. | ||
34 | 2002 05-30 : Version 0.60 -- Fixed HH&CYM generator and all voice datas. | ||
35 | 2004 04-10 : Version 0.61 -- Added YMF281B tone (defined by Chabin). | ||
36 | |||
37 | 2011 03-22 : --------------- Modified by gama to use precalculated tables. | ||
38 | |||
39 | References: | ||
40 | fmopl.c -- 1999,2000 written by Tatsuyuki Satoh (MAME development). | ||
41 | fmopl.c(fixed) -- (C) 2002 Jarek Burczynski. | ||
42 | s_opl.c -- 2001 written by Mamiya (NEZplug development). | ||
43 | fmgen.cpp -- 1999,2000 written by cisc. | ||
44 | fmpac.ill -- 2000 created by NARUTO. | ||
45 | MSX-Datapack | ||
46 | YMU757 data sheet | ||
47 | YM2143 data sheet | ||
48 | |||
49 | **************************************************************************************/ | ||
50 | #include <stdio.h> | ||
51 | #include <stdlib.h> | ||
52 | #include <string.h> | ||
53 | #include <math.h> | ||
54 | #include "emu2413.h" | ||
55 | |||
56 | #include "emutables.h" | ||
57 | #if !defined(ROCKBOX) | ||
58 | #define EMU2413_CALCUL_TABLES | ||
59 | #else | ||
60 | #define EMU2413_COMPACTION | ||
61 | #include "emutables.h" | ||
62 | #endif | ||
63 | |||
64 | #if defined(EMU2413_COMPACTION) && !defined(ROCKBOX) | ||
65 | #define OPLL_TONE_NUM 1 | ||
66 | static unsigned char default_inst[OPLL_TONE_NUM][(16 + 3) * 16] = { | ||
67 | { | ||
68 | #include "2413tone.h" | ||
69 | } | ||
70 | }; | ||
71 | #else | ||
72 | #define OPLL_TONE_NUM 3 | ||
73 | static unsigned char default_inst[OPLL_TONE_NUM][(16 + 3) * 16] = { | ||
74 | { | ||
75 | #include "2413tone.h" | ||
76 | }, | ||
77 | { | ||
78 | #include "vrc7tone.h" | ||
79 | }, | ||
80 | { | ||
81 | #include "281btone.h" | ||
82 | } | ||
83 | }; | ||
84 | #endif | ||
85 | |||
86 | /* Size of Sintable ( 8 -- 18 can be used. 9 recommended.) */ | ||
87 | #define PG_BITS 9 | ||
88 | #define PG_WIDTH (1<<PG_BITS) | ||
89 | |||
90 | /* Phase increment counter */ | ||
91 | #define DP_BITS 18 | ||
92 | #define DP_WIDTH (1<<DP_BITS) | ||
93 | #define DP_BASE_BITS (DP_BITS - PG_BITS) | ||
94 | |||
95 | /* Dynamic range (Accuracy of sin table) */ | ||
96 | #define DB_PREC 48 | ||
97 | #define DB_BITS 8 | ||
98 | #define DB_STEP ((double)DB_PREC/(1<<DB_BITS)) | ||
99 | #define DB_MUTE (1<<DB_BITS) | ||
100 | |||
101 | /* Dynamic range of envelope */ | ||
102 | #define EG_STEP 0.375 | ||
103 | #define EG_BITS 7 | ||
104 | #define EG_MUTE (1<<EG_BITS) | ||
105 | |||
106 | /* Dynamic range of total level */ | ||
107 | #define TL_STEP 0.75 | ||
108 | #define TL_BITS 6 | ||
109 | #define TL_MUTE (1<<TL_BITS) | ||
110 | |||
111 | /* Dynamic range of sustine level */ | ||
112 | #define SL_STEP 3.0 | ||
113 | #define SL_BITS 4 | ||
114 | #define SL_MUTE (1<<SL_BITS) | ||
115 | |||
116 | #define EG2DB(d) ((d)*(e_int32)(EG_STEP/DB_STEP)) | ||
117 | #define TL2EG(d) ((d)*(e_int32)(TL_STEP/EG_STEP)) | ||
118 | #define SL2EG(d) ((d)*(e_int32)(SL_STEP/EG_STEP)) | ||
119 | |||
120 | #define DB_POS(x) (x*DB_MUTE/DB_PREC) | ||
121 | #define DB_NEG(x) (DB_MUTE+DB_MUTE+x*DB_MUTE/DB_PREC) | ||
122 | |||
123 | /* Bits for liner value */ | ||
124 | #define DB2LIN_AMP_BITS 8 | ||
125 | #define SLOT_AMP_BITS (DB2LIN_AMP_BITS) | ||
126 | |||
127 | /* Bits for envelope phase incremental counter */ | ||
128 | #define EG_DP_BITS 22 | ||
129 | #define EG_DP_WIDTH (1<<EG_DP_BITS) | ||
130 | |||
131 | /* Bits for Pitch and Amp modulator */ | ||
132 | #define PM_PG_BITS 8 | ||
133 | #define PM_PG_WIDTH (1<<PM_PG_BITS) | ||
134 | #define PM_DP_BITS 16 | ||
135 | #define PM_DP_WIDTH (1<<PM_DP_BITS) | ||
136 | #define AM_PG_BITS 8 | ||
137 | #define AM_PG_WIDTH (1<<AM_PG_BITS) | ||
138 | #define AM_DP_BITS 16 | ||
139 | #define AM_DP_WIDTH (1<<AM_DP_BITS) | ||
140 | |||
141 | /* PM table is calcurated by PM_AMP * pow(2,PM_DEPTH*sin(x)/1200) */ | ||
142 | #define PM_AMP_BITS 8 | ||
143 | #define PM_AMP (1<<PM_AMP_BITS) | ||
144 | |||
145 | /* PM speed(Hz) and depth(cent) */ | ||
146 | #define PM_SPEED 6.4 | ||
147 | #define PM_DEPTH 13.75 | ||
148 | |||
149 | /* AM speed(Hz) and depth(dB) */ | ||
150 | #define AM_SPEED 3.6413 | ||
151 | #define AM_DEPTH 4.875 | ||
152 | |||
153 | /* Cut the lower b bit(s) off. */ | ||
154 | #define HIGHBITS(c,b) ((c)>>(b)) | ||
155 | |||
156 | /* Leave the lower b bit(s). */ | ||
157 | #define LOWBITS(c,b) ((c)&((1<<(b))-1)) | ||
158 | |||
159 | /* Expand x which is s bits to d bits. */ | ||
160 | #define EXPAND_BITS(x,s,d) ((x)<<((d)-(s))) | ||
161 | |||
162 | /* Expand x which is s bits to d bits and fill expanded bits '1' */ | ||
163 | #define EXPAND_BITS_X(x,s,d) (((x)<<((d)-(s)))|((1<<((d)-(s)))-1)) | ||
164 | |||
165 | /* Adjust envelope speed which depends on sampling rate. */ | ||
166 | #define RATE_ADJUST(x) (rate==49716?(e_uint32)x:(e_uint32)(((long long)(x)*clk/rate+36)/72)) | ||
167 | |||
168 | #define MOD(o,x) (&(o)->slot[(x)<<1]) | ||
169 | #define CAR(o,x) (&(o)->slot[((x)<<1)|1]) | ||
170 | |||
171 | #define BIT(s,b) (((s)>>(b))&1) | ||
172 | |||
173 | /* Input clock */ | ||
174 | static e_uint32 clk = 844451141; | ||
175 | /* Sampling rate */ | ||
176 | static e_uint32 rate = 3354932; | ||
177 | |||
178 | /* WaveTable for each envelope amp */ | ||
179 | static e_uint16 fullsintable[PG_WIDTH]; | ||
180 | static e_uint16 halfsintable[PG_WIDTH]; | ||
181 | |||
182 | static e_uint16 *waveform[2] = { fullsintable, halfsintable }; | ||
183 | |||
184 | /* LFO Table */ | ||
185 | #ifdef EMU2413_CALCUL_TABLES | ||
186 | static e_int32 pmtable[PM_PG_WIDTH]; | ||
187 | static e_int32 amtable[AM_PG_WIDTH]; | ||
188 | #define PMTABLE(x) pmtable[x] | ||
189 | #define AMTABLE(x) amtable[x] | ||
190 | #else | ||
191 | #define PMTABLE(x) (e_int32)pm_coeff[x] | ||
192 | #if (PM_PG_WIDTH != 256) | ||
193 | #error PM_PG_WIDTH must be set to 256 if EMU2413_CALCUL_TABLES is not defined | ||
194 | #endif | ||
195 | #define AMTABLE(x) (e_int32)am_coeff[x] | ||
196 | #if (AM_PG_WIDTH != 256) | ||
197 | #error AM_PG_WIDTH must be set to 256 if EMU2413_CALCUL_TABLES is not defined | ||
198 | #endif | ||
199 | #endif | ||
200 | |||
201 | /* Phase delta for LFO */ | ||
202 | static e_uint32 pm_dphase; | ||
203 | static e_uint32 am_dphase; | ||
204 | |||
205 | /* dB to Liner table */ | ||
206 | static e_int16 DB2LIN_TABLE[(DB_MUTE + DB_MUTE) * 2]; | ||
207 | |||
208 | /* Liner to Log curve conversion table (for Attack rate). */ | ||
209 | #ifdef EMU2413_CALCUL_TABLES | ||
210 | static e_uint16 ar_adjust_table[1 << EG_BITS]; | ||
211 | #define AR_ADJUST_TABLE(x) ar_adjust_table[x] | ||
212 | #else | ||
213 | #define AR_ADJUST_TABLE(x) ar_adjust_coeff[x] | ||
214 | #if (EG_BITS != 7) | ||
215 | #error EG_BITS must be set to 7 if EMU2413_CALCUL_TABLES is not defined | ||
216 | #endif | ||
217 | #endif | ||
218 | |||
219 | /* Empty voice data */ | ||
220 | static OPLL_PATCH null_patch = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; | ||
221 | |||
222 | /* Basic voice Data */ | ||
223 | static OPLL_PATCH default_patch[OPLL_TONE_NUM][(16 + 3) * 2]; | ||
224 | |||
225 | /* Definition of envelope mode */ | ||
226 | enum OPLL_EG_STATE | ||
227 | { READY, ATTACK, DECAY, SUSHOLD, SUSTINE, RELEASE, SETTLE, FINISH }; | ||
228 | |||
229 | /* Phase incr table for Attack */ | ||
230 | static e_uint32 dphaseARTable[16][16]; | ||
231 | /* Phase incr table for Decay and Release */ | ||
232 | static e_uint32 dphaseDRTable[16][16]; | ||
233 | |||
234 | /* KSL + TL Table */ | ||
235 | e_uint8 tllTable[16][8][1 << TL_BITS][4]; | ||
236 | static e_int32 rksTable[2][8][2]; | ||
237 | |||
238 | /* We may not have too much SRAM in rockbox */ | ||
239 | #if !defined(ROCKBOX) | ||
240 | /* Phase incr table for PG */ | ||
241 | static e_uint32 dphaseTable[512][8][16]; | ||
242 | #endif | ||
243 | |||
244 | /*************************************************** | ||
245 | |||
246 | Create tables | ||
247 | |||
248 | ****************************************************/ | ||
249 | #ifdef EMU2413_CALCUL_TABLES | ||
250 | INLINE static e_int32 | ||
251 | Min (e_int32 i, e_int32 j) | ||
252 | { | ||
253 | if (i < j) | ||
254 | return i; | ||
255 | else | ||
256 | return j; | ||
257 | } | ||
258 | |||
259 | /* Table for AR to LogCurve. */ | ||
260 | static void | ||
261 | makeAdjustTable (void) | ||
262 | { | ||
263 | e_int32 i; | ||
264 | |||
265 | ar_adjust_table[0] = (1 << EG_BITS) - 1; | ||
266 | for (i = 1; i < (1<<EG_BITS); i++) | ||
267 | ar_adjust_table[i] = (e_uint16) ((double) (1<<EG_BITS)-1 - ((1<<EG_BITS)-1)*log(i)/log(127)); | ||
268 | } | ||
269 | #endif | ||
270 | |||
271 | /* Table for dB(0 -- (1<<DB_BITS)-1) to Liner(0 -- DB2LIN_AMP_WIDTH) */ | ||
272 | static void | ||
273 | makeDB2LinTable (void) | ||
274 | { | ||
275 | e_int32 i; | ||
276 | for (i = 0; i < DB_MUTE + DB_MUTE; i++) | ||
277 | { | ||
278 | #ifdef EMU2413_CALCUL_TABLES | ||
279 | DB2LIN_TABLE[i] = (e_int16) ((double) ((1 << DB2LIN_AMP_BITS) - 1) * pow (10, -(double) i * DB_STEP / 20)); | ||
280 | #else | ||
281 | DB2LIN_TABLE[i] = db2lin_coeff[i]; | ||
282 | #endif | ||
283 | if (i >= DB_MUTE) DB2LIN_TABLE[i] = 0; | ||
284 | DB2LIN_TABLE[i + DB_MUTE + DB_MUTE] = (e_int16) (-DB2LIN_TABLE[i]); | ||
285 | } | ||
286 | } | ||
287 | |||
288 | #ifdef EMU2413_CALCUL_TABLES | ||
289 | /* Liner(+0.0 - +1.0) to dB((1<<DB_BITS) - 1 -- 0) */ | ||
290 | static e_int32 | ||
291 | lin2db (double d) | ||
292 | { | ||
293 | if (d == 0) | ||
294 | return (DB_MUTE - 1); | ||
295 | else | ||
296 | return Min (-(e_int32) (20.0 * log10 (d) / DB_STEP), DB_MUTE-1); /* 0 -- 127 */ | ||
297 | } | ||
298 | #endif | ||
299 | |||
300 | /* Sin Table */ | ||
301 | static void | ||
302 | makeSinTable (void) | ||
303 | { | ||
304 | e_int32 i; | ||
305 | |||
306 | for (i = 0; i < PG_WIDTH / 4; i++) | ||
307 | #ifdef EMU2413_CALCUL_TABLES | ||
308 | fullsintable[i] = (e_uint32) lin2db (sin (2.0 * PI * i / PG_WIDTH) ); | ||
309 | #else | ||
310 | fullsintable[i] = sin_coeff[i]; | ||
311 | #endif | ||
312 | |||
313 | for (i = 0; i < PG_WIDTH / 4; i++) | ||
314 | { | ||
315 | fullsintable[PG_WIDTH / 2 - 1 - i] = fullsintable[i]; | ||
316 | } | ||
317 | |||
318 | for (i = 0; i < PG_WIDTH / 2; i++) | ||
319 | { | ||
320 | fullsintable[PG_WIDTH / 2 + i] = (e_uint32) (DB_MUTE + DB_MUTE + fullsintable[i]); | ||
321 | } | ||
322 | |||
323 | for (i = 0; i < PG_WIDTH / 2; i++) | ||
324 | halfsintable[i] = fullsintable[i]; | ||
325 | for (i = PG_WIDTH / 2; i < PG_WIDTH; i++) | ||
326 | halfsintable[i] = fullsintable[0]; | ||
327 | } | ||
328 | |||
329 | #ifdef EMU2413_CALCUL_TABLES | ||
330 | static double saw(double phase) | ||
331 | { | ||
332 | if(phase <= PI/2) | ||
333 | return phase * 2 / PI ; | ||
334 | else if(phase <= PI*3/2) | ||
335 | return 2.0 - ( phase * 2 / PI ); | ||
336 | else | ||
337 | return -4.0 + phase * 2 / PI; | ||
338 | } | ||
339 | |||
340 | /* Table for Pitch Modulator */ | ||
341 | static void | ||
342 | makePmTable (void) | ||
343 | { | ||
344 | e_int32 i; | ||
345 | |||
346 | for (i = 0; i < PM_PG_WIDTH; i++) | ||
347 | /* pmtable[i] = (e_int32) ((double) PM_AMP * pow (2, (double) PM_DEPTH * sin (2.0 * PI * i / PM_PG_WIDTH) / 1200)); */ | ||
348 | pmtable[i] = (e_int32) ((double) PM_AMP * pow (2, (double) PM_DEPTH * saw (2.0 * PI * i / PM_PG_WIDTH) / 1200)); | ||
349 | } | ||
350 | |||
351 | /* Table for Amp Modulator */ | ||
352 | static void | ||
353 | makeAmTable (void) | ||
354 | { | ||
355 | e_int32 i; | ||
356 | |||
357 | for (i = 0; i < AM_PG_WIDTH; i++) | ||
358 | /* amtable[i] = (e_int32) ((double) AM_DEPTH / 2 / DB_STEP * (1.0 + sin (2.0 * PI * i / PM_PG_WIDTH))); */ | ||
359 | amtable[i] = (e_int32) ((double) AM_DEPTH / 2 / DB_STEP * (1.0 + saw (2.0 * PI * i / PM_PG_WIDTH))); | ||
360 | } | ||
361 | #endif | ||
362 | |||
363 | #if !defined(ROCKBOX) | ||
364 | /* Phase increment counter table */ | ||
365 | static void | ||
366 | makeDphaseTable (void) | ||
367 | { | ||
368 | e_uint32 fnum, block, ML; | ||
369 | e_uint32 mltable[16] = | ||
370 | { 1, 1 * 2, 2 * 2, 3 * 2, 4 * 2, 5 * 2, 6 * 2, 7 * 2, 8 * 2, 9 * 2, 10 * 2, 10 * 2, 12 * 2, 12 * 2, 15 * 2, 15 * 2 }; | ||
371 | |||
372 | for (fnum = 0; fnum < 512; fnum++) | ||
373 | for (block = 0; block < 8; block++) | ||
374 | for (ML = 0; ML < 16; ML++) | ||
375 | dphaseTable[fnum][block][ML] = RATE_ADJUST (((fnum * mltable[ML]) << block) >> (20 - DP_BITS)); | ||
376 | } | ||
377 | #endif | ||
378 | |||
379 | static void | ||
380 | makeTllTable (void) | ||
381 | { | ||
382 | /* Multiplication owith 8 to have an integer result. This allows to remove floating point operation. */ | ||
383 | #define dB2(x) (int)((x)*2*8) | ||
384 | |||
385 | static int kltable[16] = { | ||
386 | dB2 ( 0.000), dB2 ( 9.000), dB2 (12.000), dB2 (13.875), dB2 (15.000), dB2 (16.125), dB2 (16.875), dB2 (17.625), | ||
387 | dB2 (18.000), dB2 (18.750), dB2 (19.125), dB2 (19.500), dB2 (19.875), dB2 (20.250), dB2 (20.625), dB2 (21.000) | ||
388 | }; | ||
389 | |||
390 | e_int32 tmp; | ||
391 | e_int32 fnum, block, TL, KL; | ||
392 | |||
393 | for (fnum = 0; fnum < 16; fnum++) | ||
394 | for (block = 0; block < 8; block++) | ||
395 | for (TL = 0; TL < 64; TL++) | ||
396 | for (KL = 0; KL < 4; KL++) | ||
397 | { | ||
398 | if (KL == 0) | ||
399 | { | ||
400 | tllTable[fnum][block][TL][KL] = TL2EG (TL); | ||
401 | } | ||
402 | else | ||
403 | { | ||
404 | tmp = (e_int32) ((kltable[fnum] - dB2 (3.000) * (7 - block))/8); | ||
405 | if (tmp <= 0) | ||
406 | tllTable[fnum][block][TL][KL] = TL2EG (TL); | ||
407 | else | ||
408 | /* tllTable[fnum][block][TL][KL] = (e_uint32) ((tmp >> (3 - KL)) / EG_STEP) + TL2EG (TL); */ | ||
409 | tllTable[fnum][block][TL][KL] = (e_uint32) ((tmp << KL) / (int)(EG_STEP*8)) + TL2EG (TL); | ||
410 | } | ||
411 | } | ||
412 | } | ||
413 | |||
414 | #ifdef USE_SPEC_ENV_SPEED | ||
415 | static double attacktime[16][4] = { | ||
416 | {0, 0, 0, 0}, | ||
417 | {1730.15, 1400.60, 1153.43, 988.66}, | ||
418 | {865.08, 700.30, 576.72, 494.33}, | ||
419 | {432.54, 350.15, 288.36, 247.16}, | ||
420 | {216.27, 175.07, 144.18, 123.58}, | ||
421 | {108.13, 87.54, 72.09, 61.79}, | ||
422 | {54.07, 43.77, 36.04, 30.90}, | ||
423 | {27.03, 21.88, 18.02, 15.45}, | ||
424 | {13.52, 10.94, 9.01, 7.72}, | ||
425 | {6.76, 5.47, 4.51, 3.86}, | ||
426 | {3.38, 2.74, 2.25, 1.93}, | ||
427 | {1.69, 1.37, 1.13, 0.97}, | ||
428 | {0.84, 0.70, 0.60, 0.54}, | ||
429 | {0.50, 0.42, 0.34, 0.30}, | ||
430 | {0.28, 0.22, 0.18, 0.14}, | ||
431 | {0.00, 0.00, 0.00, 0.00} | ||
432 | }; | ||
433 | |||
434 | static double decaytime[16][4] = { | ||
435 | {0, 0, 0, 0}, | ||
436 | {20926.60, 16807.20, 14006.00, 12028.60}, | ||
437 | {10463.30, 8403.58, 7002.98, 6014.32}, | ||
438 | {5231.64, 4201.79, 3501.49, 3007.16}, | ||
439 | {2615.82, 2100.89, 1750.75, 1503.58}, | ||
440 | {1307.91, 1050.45, 875.37, 751.79}, | ||
441 | {653.95, 525.22, 437.69, 375.90}, | ||
442 | {326.98, 262.61, 218.84, 187.95}, | ||
443 | {163.49, 131.31, 109.42, 93.97}, | ||
444 | {81.74, 65.65, 54.71, 46.99}, | ||
445 | {40.87, 32.83, 27.36, 23.49}, | ||
446 | {20.44, 16.41, 13.68, 11.75}, | ||
447 | {10.22, 8.21, 6.84, 5.87}, | ||
448 | {5.11, 4.10, 3.42, 2.94}, | ||
449 | {2.55, 2.05, 1.71, 1.47}, | ||
450 | {1.27, 1.27, 1.27, 1.27} | ||
451 | }; | ||
452 | #endif | ||
453 | |||
454 | /* Rate Table for Attack */ | ||
455 | static void | ||
456 | makeDphaseARTable (void) | ||
457 | { | ||
458 | e_int32 AR, Rks, RM, RL; | ||
459 | |||
460 | #ifdef USE_SPEC_ENV_SPEED | ||
461 | e_uint32 attacktable[16][4]; | ||
462 | |||
463 | for (RM = 0; RM < 16; RM++) | ||
464 | for (RL = 0; RL < 4; RL++) | ||
465 | { | ||
466 | if (RM == 0) | ||
467 | attacktable[RM][RL] = 0; | ||
468 | else if (RM == 15) | ||
469 | attacktable[RM][RL] = EG_DP_WIDTH; | ||
470 | else | ||
471 | attacktable[RM][RL] = (e_uint32) ((double) (1 << EG_DP_BITS) / (attacktime[RM][RL] * 3579545 / 72000)); | ||
472 | |||
473 | } | ||
474 | #endif | ||
475 | |||
476 | for (AR = 0; AR < 16; AR++) | ||
477 | for (Rks = 0; Rks < 16; Rks++) | ||
478 | { | ||
479 | RM = AR + (Rks >> 2); | ||
480 | RL = Rks & 3; | ||
481 | if (RM > 15) | ||
482 | RM = 15; | ||
483 | switch (AR) | ||
484 | { | ||
485 | case 0: | ||
486 | dphaseARTable[AR][Rks] = 0; | ||
487 | break; | ||
488 | case 15: | ||
489 | dphaseARTable[AR][Rks] = 0;/*EG_DP_WIDTH;*/ | ||
490 | break; | ||
491 | default: | ||
492 | #ifdef USE_SPEC_ENV_SPEED | ||
493 | dphaseARTable[AR][Rks] = RATE_ADJUST (attacktable[RM][RL]); | ||
494 | #else | ||
495 | dphaseARTable[AR][Rks] = RATE_ADJUST ((3 * (RL + 4) << (RM + 1))); | ||
496 | #endif | ||
497 | break; | ||
498 | } | ||
499 | } | ||
500 | } | ||
501 | |||
502 | /* Rate Table for Decay and Release */ | ||
503 | static void | ||
504 | makeDphaseDRTable (void) | ||
505 | { | ||
506 | e_int32 DR, Rks, RM, RL; | ||
507 | |||
508 | #ifdef USE_SPEC_ENV_SPEED | ||
509 | e_uint32 decaytable[16][4]; | ||
510 | |||
511 | for (RM = 0; RM < 16; RM++) | ||
512 | for (RL = 0; RL < 4; RL++) | ||
513 | if (RM == 0) | ||
514 | decaytable[RM][RL] = 0; | ||
515 | else | ||
516 | decaytable[RM][RL] = (e_uint32) ((double) (1 << EG_DP_BITS) / (decaytime[RM][RL] * 3579545 / 72000)); | ||
517 | #endif | ||
518 | |||
519 | for (DR = 0; DR < 16; DR++) | ||
520 | for (Rks = 0; Rks < 16; Rks++) | ||
521 | { | ||
522 | RM = DR + (Rks >> 2); | ||
523 | RL = Rks & 3; | ||
524 | if (RM > 15) | ||
525 | RM = 15; | ||
526 | switch (DR) | ||
527 | { | ||
528 | case 0: | ||
529 | dphaseDRTable[DR][Rks] = 0; | ||
530 | break; | ||
531 | default: | ||
532 | #ifdef USE_SPEC_ENV_SPEED | ||
533 | dphaseDRTable[DR][Rks] = RATE_ADJUST (decaytable[RM][RL]); | ||
534 | #else | ||
535 | dphaseDRTable[DR][Rks] = RATE_ADJUST ((RL + 4) << (RM - 1)); | ||
536 | #endif | ||
537 | break; | ||
538 | } | ||
539 | } | ||
540 | } | ||
541 | |||
542 | static void | ||
543 | makeRksTable (void) | ||
544 | { | ||
545 | |||
546 | e_int32 fnum8, block, KR; | ||
547 | |||
548 | for (fnum8 = 0; fnum8 < 2; fnum8++) | ||
549 | for (block = 0; block < 8; block++) | ||
550 | for (KR = 0; KR < 2; KR++) | ||
551 | { | ||
552 | if (KR != 0) | ||
553 | rksTable[fnum8][block][KR] = (block << 1) + fnum8; | ||
554 | else | ||
555 | rksTable[fnum8][block][KR] = block >> 1; | ||
556 | } | ||
557 | } | ||
558 | |||
559 | void | ||
560 | OPLL_dump2patch (const e_uint8 * dump, OPLL_PATCH * patch) | ||
561 | { | ||
562 | patch[0].AM = (dump[0] >> 7) & 1; | ||
563 | patch[1].AM = (dump[1] >> 7) & 1; | ||
564 | patch[0].PM = (dump[0] >> 6) & 1; | ||
565 | patch[1].PM = (dump[1] >> 6) & 1; | ||
566 | patch[0].EG = (dump[0] >> 5) & 1; | ||
567 | patch[1].EG = (dump[1] >> 5) & 1; | ||
568 | patch[0].KR = (dump[0] >> 4) & 1; | ||
569 | patch[1].KR = (dump[1] >> 4) & 1; | ||
570 | patch[0].ML = (dump[0]) & 15; | ||
571 | patch[1].ML = (dump[1]) & 15; | ||
572 | patch[0].KL = (dump[2] >> 6) & 3; | ||
573 | patch[1].KL = (dump[3] >> 6) & 3; | ||
574 | patch[0].TL = (dump[2]) & 63; | ||
575 | patch[0].FB = (dump[3]) & 7; | ||
576 | patch[0].WF = (dump[3] >> 3) & 1; | ||
577 | patch[1].WF = (dump[3] >> 4) & 1; | ||
578 | patch[0].AR = (dump[4] >> 4) & 15; | ||
579 | patch[1].AR = (dump[5] >> 4) & 15; | ||
580 | patch[0].DR = (dump[4]) & 15; | ||
581 | patch[1].DR = (dump[5]) & 15; | ||
582 | patch[0].SL = (dump[6] >> 4) & 15; | ||
583 | patch[1].SL = (dump[7] >> 4) & 15; | ||
584 | patch[0].RR = (dump[6]) & 15; | ||
585 | patch[1].RR = (dump[7]) & 15; | ||
586 | } | ||
587 | |||
588 | void | ||
589 | OPLL_getDefaultPatch (e_int32 type, e_int32 num, OPLL_PATCH * patch) | ||
590 | { | ||
591 | OPLL_dump2patch (default_inst[type] + num * 16, patch); | ||
592 | } | ||
593 | |||
594 | static void | ||
595 | makeDefaultPatch ( void ) | ||
596 | { | ||
597 | e_int32 i, j; | ||
598 | |||
599 | for (i = 0; i < OPLL_TONE_NUM; i++) | ||
600 | for (j = 0; j < 19; j++) | ||
601 | OPLL_getDefaultPatch (i, j, &default_patch[i][j * 2]); | ||
602 | |||
603 | } | ||
604 | |||
605 | void | ||
606 | OPLL_setPatch (OPLL * opll, const e_uint8 * dump) | ||
607 | { | ||
608 | OPLL_PATCH patch[2]; | ||
609 | int i; | ||
610 | |||
611 | for (i = 0; i < 19; i++) | ||
612 | { | ||
613 | OPLL_dump2patch (dump + i * 16, patch); | ||
614 | memcpy (&opll->patch[i*2+0], &patch[0], sizeof (OPLL_PATCH)); | ||
615 | memcpy (&opll->patch[i*2+1], &patch[1], sizeof (OPLL_PATCH)); | ||
616 | } | ||
617 | } | ||
618 | |||
619 | void | ||
620 | OPLL_patch2dump (const OPLL_PATCH * patch, e_uint8 * dump) | ||
621 | { | ||
622 | dump[0] = (e_uint8) ((patch[0].AM << 7) + (patch[0].PM << 6) + (patch[0].EG << 5) + (patch[0].KR << 4) + patch[0].ML); | ||
623 | dump[1] = (e_uint8) ((patch[1].AM << 7) + (patch[1].PM << 6) + (patch[1].EG << 5) + (patch[1].KR << 4) + patch[1].ML); | ||
624 | dump[2] = (e_uint8) ((patch[0].KL << 6) + patch[0].TL); | ||
625 | dump[3] = (e_uint8) ((patch[1].KL << 6) + (patch[1].WF << 4) + (patch[0].WF << 3) + patch[0].FB); | ||
626 | dump[4] = (e_uint8) ((patch[0].AR << 4) + patch[0].DR); | ||
627 | dump[5] = (e_uint8) ((patch[1].AR << 4) + patch[1].DR); | ||
628 | dump[6] = (e_uint8) ((patch[0].SL << 4) + patch[0].RR); | ||
629 | dump[7] = (e_uint8) ((patch[1].SL << 4) + patch[1].RR); | ||
630 | dump[8] = 0; | ||
631 | dump[9] = 0; | ||
632 | dump[10] = 0; | ||
633 | dump[11] = 0; | ||
634 | dump[12] = 0; | ||
635 | dump[13] = 0; | ||
636 | dump[14] = 0; | ||
637 | dump[15] = 0; | ||
638 | } | ||
639 | |||
640 | /************************************************************ | ||
641 | |||
642 | Calc Parameters | ||
643 | |||
644 | ************************************************************/ | ||
645 | |||
646 | INLINE static e_uint32 | ||
647 | calc_eg_dphase (OPLL_SLOT * slot) | ||
648 | { | ||
649 | |||
650 | switch (slot->eg_mode) | ||
651 | { | ||
652 | case ATTACK: | ||
653 | return dphaseARTable[slot->patch->AR][slot->rks]; | ||
654 | |||
655 | case DECAY: | ||
656 | return dphaseDRTable[slot->patch->DR][slot->rks]; | ||
657 | |||
658 | case SUSHOLD: | ||
659 | return 0; | ||
660 | |||
661 | case SUSTINE: | ||
662 | return dphaseDRTable[slot->patch->RR][slot->rks]; | ||
663 | |||
664 | case RELEASE: | ||
665 | if (slot->sustine) | ||
666 | return dphaseDRTable[5][slot->rks]; | ||
667 | else if (slot->patch->EG) | ||
668 | return dphaseDRTable[slot->patch->RR][slot->rks]; | ||
669 | else | ||
670 | return dphaseDRTable[7][slot->rks]; | ||
671 | |||
672 | case SETTLE: | ||
673 | return dphaseDRTable[15][0]; | ||
674 | |||
675 | case FINISH: | ||
676 | return 0; | ||
677 | |||
678 | default: | ||
679 | return 0; | ||
680 | } | ||
681 | } | ||
682 | |||
683 | /************************************************************* | ||
684 | |||
685 | OPLL internal interfaces | ||
686 | |||
687 | *************************************************************/ | ||
688 | #define SLOT_BD1 12 | ||
689 | #define SLOT_BD2 13 | ||
690 | #define SLOT_HH 14 | ||
691 | #define SLOT_SD 15 | ||
692 | #define SLOT_TOM 16 | ||
693 | #define SLOT_CYM 17 | ||
694 | |||
695 | /* We will set this dinamically, but not sure if this affects playback */ | ||
696 | #if defined(ROCKBOX) | ||
697 | INLINE static void | ||
698 | UPDATE_PG(OPLL_SLOT * slot) | ||
699 | { | ||
700 | static const e_uint32 mltable[16] = | ||
701 | { 1, 1 * 2, 2 * 2, 3 * 2, 4 * 2, 5 * 2, 6 * 2, 7 * 2, 8 * 2, 9 * 2, 10 * 2, 10 * 2, 12 * 2, 12 * 2, 15 * 2, 15 * 2 }; | ||
702 | |||
703 | slot->dphase = RATE_ADJUST (((slot->fnum * mltable[slot->patch->ML]) << slot->block) >> (20 - DP_BITS)); | ||
704 | } | ||
705 | #else | ||
706 | #define UPDATE_PG(S) (S)->dphase = dphaseTable[(S)->fnum][(S)->block][(S)->patch->ML] | ||
707 | #endif | ||
708 | |||
709 | #define UPDATE_TLL(S)\ | ||
710 | (((S)->type==0)?\ | ||
711 | ((S)->tll = tllTable[((S)->fnum)>>5][(S)->block][(S)->patch->TL][(S)->patch->KL]):\ | ||
712 | ((S)->tll = tllTable[((S)->fnum)>>5][(S)->block][(S)->volume][(S)->patch->KL])) | ||
713 | #define UPDATE_RKS(S) (S)->rks = rksTable[((S)->fnum)>>8][(S)->block][(S)->patch->KR] | ||
714 | #define UPDATE_WF(S) (S)->sintbl = waveform[(S)->patch->WF] | ||
715 | #define UPDATE_EG(S) (S)->eg_dphase = calc_eg_dphase(S) | ||
716 | #define UPDATE_ALL(S)\ | ||
717 | UPDATE_PG(S);\ | ||
718 | UPDATE_TLL(S);\ | ||
719 | UPDATE_RKS(S);\ | ||
720 | UPDATE_WF(S); \ | ||
721 | UPDATE_EG(S) /* EG should be updated last. */ | ||
722 | |||
723 | |||
724 | /* Slot key on */ | ||
725 | INLINE static void | ||
726 | slotOn (OPLL_SLOT * slot) | ||
727 | { | ||
728 | slot->eg_mode = ATTACK; | ||
729 | slot->eg_phase = 0; | ||
730 | slot->phase = 0; | ||
731 | UPDATE_EG(slot); | ||
732 | } | ||
733 | |||
734 | /* Slot key on without reseting the phase */ | ||
735 | INLINE static void | ||
736 | slotOn2 (OPLL_SLOT * slot) | ||
737 | { | ||
738 | slot->eg_mode = ATTACK; | ||
739 | slot->eg_phase = 0; | ||
740 | UPDATE_EG(slot); | ||
741 | } | ||
742 | |||
743 | /* Slot key off */ | ||
744 | INLINE static void | ||
745 | slotOff (OPLL_SLOT * slot) | ||
746 | { | ||
747 | if (slot->eg_mode == ATTACK) | ||
748 | slot->eg_phase = EXPAND_BITS (AR_ADJUST_TABLE(HIGHBITS (slot->eg_phase, EG_DP_BITS - EG_BITS)), EG_BITS, EG_DP_BITS); | ||
749 | slot->eg_mode = RELEASE; | ||
750 | UPDATE_EG(slot); | ||
751 | } | ||
752 | |||
753 | /* Channel key on */ | ||
754 | INLINE static void | ||
755 | keyOn (OPLL * opll, e_int32 i) | ||
756 | { | ||
757 | if (!opll->slot_on_flag[i * 2]) | ||
758 | slotOn (MOD(opll,i)); | ||
759 | if (!opll->slot_on_flag[i * 2 + 1]) | ||
760 | slotOn (CAR(opll,i)); | ||
761 | opll->key_status[i] = 1; | ||
762 | } | ||
763 | |||
764 | /* Channel key off */ | ||
765 | INLINE static void | ||
766 | keyOff (OPLL * opll, e_int32 i) | ||
767 | { | ||
768 | if (opll->slot_on_flag[i * 2 + 1]) | ||
769 | slotOff (CAR(opll,i)); | ||
770 | opll->key_status[i] = 0; | ||
771 | } | ||
772 | |||
773 | INLINE static void | ||
774 | keyOn_BD (OPLL * opll) | ||
775 | { | ||
776 | keyOn (opll, 6); | ||
777 | } | ||
778 | INLINE static void | ||
779 | keyOn_SD (OPLL * opll) | ||
780 | { | ||
781 | if (!opll->slot_on_flag[SLOT_SD]) | ||
782 | slotOn (CAR(opll,7)); | ||
783 | } | ||
784 | INLINE static void | ||
785 | keyOn_TOM (OPLL * opll) | ||
786 | { | ||
787 | if (!opll->slot_on_flag[SLOT_TOM]) | ||
788 | slotOn (MOD(opll,8)); | ||
789 | } | ||
790 | INLINE static void | ||
791 | keyOn_HH (OPLL * opll) | ||
792 | { | ||
793 | if (!opll->slot_on_flag[SLOT_HH]) | ||
794 | slotOn2 (MOD(opll,7)); | ||
795 | } | ||
796 | INLINE static void | ||
797 | keyOn_CYM (OPLL * opll) | ||
798 | { | ||
799 | if (!opll->slot_on_flag[SLOT_CYM]) | ||
800 | slotOn2 (CAR(opll,8)); | ||
801 | } | ||
802 | |||
803 | /* Drum key off */ | ||
804 | INLINE static void | ||
805 | keyOff_BD (OPLL * opll) | ||
806 | { | ||
807 | keyOff (opll, 6); | ||
808 | } | ||
809 | INLINE static void | ||
810 | keyOff_SD (OPLL * opll) | ||
811 | { | ||
812 | if (opll->slot_on_flag[SLOT_SD]) | ||
813 | slotOff (CAR(opll,7)); | ||
814 | } | ||
815 | INLINE static void | ||
816 | keyOff_TOM (OPLL * opll) | ||
817 | { | ||
818 | if (opll->slot_on_flag[SLOT_TOM]) | ||
819 | slotOff (MOD(opll,8)); | ||
820 | } | ||
821 | INLINE static void | ||
822 | keyOff_HH (OPLL * opll) | ||
823 | { | ||
824 | if (opll->slot_on_flag[SLOT_HH]) | ||
825 | slotOff (MOD(opll,7)); | ||
826 | } | ||
827 | INLINE static void | ||
828 | keyOff_CYM (OPLL * opll) | ||
829 | { | ||
830 | if (opll->slot_on_flag[SLOT_CYM]) | ||
831 | slotOff (CAR(opll,8)); | ||
832 | } | ||
833 | |||
834 | /* Change a voice */ | ||
835 | INLINE static void | ||
836 | setPatch (OPLL * opll, e_int32 i, e_int32 num) | ||
837 | { | ||
838 | opll->patch_number[i] = num; | ||
839 | MOD(opll,i)->patch = &opll->patch[num * 2 + 0]; | ||
840 | CAR(opll,i)->patch = &opll->patch[num * 2 + 1]; | ||
841 | } | ||
842 | |||
843 | /* Change a rhythm voice */ | ||
844 | INLINE static void | ||
845 | setSlotPatch (OPLL_SLOT * slot, OPLL_PATCH * patch) | ||
846 | { | ||
847 | slot->patch = patch; | ||
848 | } | ||
849 | |||
850 | /* Set sustine parameter */ | ||
851 | INLINE static void | ||
852 | setSustine (OPLL * opll, e_int32 c, e_int32 sustine) | ||
853 | { | ||
854 | CAR(opll,c)->sustine = sustine; | ||
855 | if (MOD(opll,c)->type) | ||
856 | MOD(opll,c)->sustine = sustine; | ||
857 | } | ||
858 | |||
859 | /* Volume : 6bit ( Volume register << 2 ) */ | ||
860 | INLINE static void | ||
861 | setVolume (OPLL * opll, e_int32 c, e_int32 volume) | ||
862 | { | ||
863 | CAR(opll,c)->volume = volume; | ||
864 | } | ||
865 | |||
866 | INLINE static void | ||
867 | setSlotVolume (OPLL_SLOT * slot, e_int32 volume) | ||
868 | { | ||
869 | slot->volume = volume; | ||
870 | } | ||
871 | |||
872 | /* Set F-Number ( fnum : 9bit ) */ | ||
873 | INLINE static void | ||
874 | setFnumber (OPLL * opll, e_int32 c, e_int32 fnum) | ||
875 | { | ||
876 | CAR(opll,c)->fnum = fnum; | ||
877 | MOD(opll,c)->fnum = fnum; | ||
878 | } | ||
879 | |||
880 | /* Set Block data (block : 3bit ) */ | ||
881 | INLINE static void | ||
882 | setBlock (OPLL * opll, e_int32 c, e_int32 block) | ||
883 | { | ||
884 | CAR(opll,c)->block = block; | ||
885 | MOD(opll,c)->block = block; | ||
886 | } | ||
887 | |||
888 | /* Change Rhythm Mode */ | ||
889 | INLINE static void | ||
890 | update_rhythm_mode (OPLL * opll) | ||
891 | { | ||
892 | if (opll->patch_number[6] & 0x10) | ||
893 | { | ||
894 | if (!(opll->slot_on_flag[SLOT_BD2] | (opll->reg[0x0e] & 32))) | ||
895 | { | ||
896 | opll->slot[SLOT_BD1].eg_mode = FINISH; | ||
897 | opll->slot[SLOT_BD2].eg_mode = FINISH; | ||
898 | setPatch (opll, 6, opll->reg[0x36] >> 4); | ||
899 | } | ||
900 | } | ||
901 | else if (opll->reg[0x0e] & 32) | ||
902 | { | ||
903 | opll->patch_number[6] = 16; | ||
904 | opll->slot[SLOT_BD1].eg_mode = FINISH; | ||
905 | opll->slot[SLOT_BD2].eg_mode = FINISH; | ||
906 | setSlotPatch (&opll->slot[SLOT_BD1], &opll->patch[16 * 2 + 0]); | ||
907 | setSlotPatch (&opll->slot[SLOT_BD2], &opll->patch[16 * 2 + 1]); | ||
908 | } | ||
909 | |||
910 | if (opll->patch_number[7] & 0x10) | ||
911 | { | ||
912 | if (!((opll->slot_on_flag[SLOT_HH] && opll->slot_on_flag[SLOT_SD]) | (opll->reg[0x0e] & 32))) | ||
913 | { | ||
914 | opll->slot[SLOT_HH].type = 0; | ||
915 | opll->slot[SLOT_HH].eg_mode = FINISH; | ||
916 | opll->slot[SLOT_SD].eg_mode = FINISH; | ||
917 | setPatch (opll, 7, opll->reg[0x37] >> 4); | ||
918 | } | ||
919 | } | ||
920 | else if (opll->reg[0x0e] & 32) | ||
921 | { | ||
922 | opll->patch_number[7] = 17; | ||
923 | opll->slot[SLOT_HH].type = 1; | ||
924 | opll->slot[SLOT_HH].eg_mode = FINISH; | ||
925 | opll->slot[SLOT_SD].eg_mode = FINISH; | ||
926 | setSlotPatch (&opll->slot[SLOT_HH], &opll->patch[17 * 2 + 0]); | ||
927 | setSlotPatch (&opll->slot[SLOT_SD], &opll->patch[17 * 2 + 1]); | ||
928 | } | ||
929 | |||
930 | if (opll->patch_number[8] & 0x10) | ||
931 | { | ||
932 | if (!((opll->slot_on_flag[SLOT_CYM] && opll->slot_on_flag[SLOT_TOM]) | (opll->reg[0x0e] & 32))) | ||
933 | { | ||
934 | opll->slot[SLOT_TOM].type = 0; | ||
935 | opll->slot[SLOT_TOM].eg_mode = FINISH; | ||
936 | opll->slot[SLOT_CYM].eg_mode = FINISH; | ||
937 | setPatch (opll, 8, opll->reg[0x38] >> 4); | ||
938 | } | ||
939 | } | ||
940 | else if (opll->reg[0x0e] & 32) | ||
941 | { | ||
942 | opll->patch_number[8] = 18; | ||
943 | opll->slot[SLOT_TOM].type = 1; | ||
944 | opll->slot[SLOT_TOM].eg_mode = FINISH; | ||
945 | opll->slot[SLOT_CYM].eg_mode = FINISH; | ||
946 | setSlotPatch (&opll->slot[SLOT_TOM], &opll->patch[18 * 2 + 0]); | ||
947 | setSlotPatch (&opll->slot[SLOT_CYM], &opll->patch[18 * 2 + 1]); | ||
948 | } | ||
949 | } | ||
950 | |||
951 | INLINE static void | ||
952 | update_key_status (OPLL * opll) | ||
953 | { | ||
954 | int ch; | ||
955 | |||
956 | for (ch = 0; ch < 9; ch++) | ||
957 | opll->slot_on_flag[ch * 2] = opll->slot_on_flag[ch * 2 + 1] = (opll->reg[0x20 + ch]) & 0x10; | ||
958 | |||
959 | if (opll->reg[0x0e] & 32) | ||
960 | { | ||
961 | opll->slot_on_flag[SLOT_BD1] |= (opll->reg[0x0e] & 0x10); | ||
962 | opll->slot_on_flag[SLOT_BD2] |= (opll->reg[0x0e] & 0x10); | ||
963 | opll->slot_on_flag[SLOT_SD] |= (opll->reg[0x0e] & 0x08); | ||
964 | opll->slot_on_flag[SLOT_HH] |= (opll->reg[0x0e] & 0x01); | ||
965 | opll->slot_on_flag[SLOT_TOM] |= (opll->reg[0x0e] & 0x04); | ||
966 | opll->slot_on_flag[SLOT_CYM] |= (opll->reg[0x0e] & 0x02); | ||
967 | } | ||
968 | } | ||
969 | |||
970 | void | ||
971 | OPLL_copyPatch (OPLL * opll, e_int32 num, OPLL_PATCH * patch) | ||
972 | { | ||
973 | memcpy (&opll->patch[num], patch, sizeof (OPLL_PATCH)); | ||
974 | } | ||
975 | |||
976 | /*********************************************************** | ||
977 | |||
978 | Initializing | ||
979 | |||
980 | ***********************************************************/ | ||
981 | |||
982 | static void | ||
983 | OPLL_SLOT_reset (OPLL_SLOT * slot, int type) | ||
984 | { | ||
985 | slot->type = type; | ||
986 | slot->sintbl = waveform[0]; | ||
987 | slot->phase = 0; | ||
988 | slot->dphase = 0; | ||
989 | slot->output[0] = 0; | ||
990 | slot->output[1] = 0; | ||
991 | slot->feedback = 0; | ||
992 | slot->eg_mode = FINISH; | ||
993 | slot->eg_phase = EG_DP_WIDTH; | ||
994 | slot->eg_dphase = 0; | ||
995 | slot->rks = 0; | ||
996 | slot->tll = 0; | ||
997 | slot->sustine = 0; | ||
998 | slot->fnum = 0; | ||
999 | slot->block = 0; | ||
1000 | slot->volume = 0; | ||
1001 | slot->pgout = 0; | ||
1002 | slot->egout = 0; | ||
1003 | slot->patch = &null_patch; | ||
1004 | } | ||
1005 | |||
1006 | static void | ||
1007 | internal_refresh (void) | ||
1008 | { | ||
1009 | #if !defined(ROCKBOX) | ||
1010 | makeDphaseTable (); | ||
1011 | #endif | ||
1012 | makeDphaseARTable (); | ||
1013 | makeDphaseDRTable (); | ||
1014 | pm_dphase = (e_uint32) RATE_ADJUST ((int)(PM_SPEED * PM_DP_WIDTH) / (clk / 72)); | ||
1015 | am_dphase = (e_uint32) RATE_ADJUST ((int)(AM_SPEED * AM_DP_WIDTH) / (clk / 72)); | ||
1016 | } | ||
1017 | |||
1018 | static void | ||
1019 | maketables (e_uint32 c, e_uint32 r) | ||
1020 | { | ||
1021 | if (c != clk) | ||
1022 | { | ||
1023 | clk = c; | ||
1024 | #ifdef EMU2413_CALCUL_TABLES | ||
1025 | makePmTable (); | ||
1026 | makeAmTable (); | ||
1027 | makeAdjustTable (); | ||
1028 | #endif | ||
1029 | makeDB2LinTable (); | ||
1030 | makeTllTable (); | ||
1031 | makeRksTable (); | ||
1032 | makeSinTable (); | ||
1033 | makeDefaultPatch (); | ||
1034 | } | ||
1035 | |||
1036 | if (r != rate) | ||
1037 | { | ||
1038 | rate = r; | ||
1039 | internal_refresh (); | ||
1040 | } | ||
1041 | } | ||
1042 | |||
1043 | void | ||
1044 | OPLL_new (OPLL *opll, e_uint32 clk, e_uint32 rate) | ||
1045 | { | ||
1046 | e_int32 i; | ||
1047 | |||
1048 | maketables (clk, rate); | ||
1049 | |||
1050 | memset(opll, 0, sizeof (OPLL)); | ||
1051 | for (i = 0; i < 19 * 2; i++) | ||
1052 | memcpy(&opll->patch[i],&null_patch,sizeof(OPLL_PATCH)); | ||
1053 | |||
1054 | opll->mask = 0; | ||
1055 | |||
1056 | OPLL_reset (opll); | ||
1057 | OPLL_reset_patch (opll, 0); | ||
1058 | } | ||
1059 | |||
1060 | |||
1061 | void | ||
1062 | OPLL_delete (OPLL * opll) | ||
1063 | { | ||
1064 | (void) opll; | ||
1065 | } | ||
1066 | |||
1067 | |||
1068 | /* Reset patch datas by system default. */ | ||
1069 | void | ||
1070 | OPLL_reset_patch (OPLL * opll, e_int32 type) | ||
1071 | { | ||
1072 | e_int32 i; | ||
1073 | |||
1074 | for (i = 0; i < 19 * 2; i++) | ||
1075 | OPLL_copyPatch (opll, i, &default_patch[type % OPLL_TONE_NUM][i]); | ||
1076 | } | ||
1077 | |||
1078 | /* Reset whole of OPLL except patch datas. */ | ||
1079 | void | ||
1080 | OPLL_reset (OPLL * opll) | ||
1081 | { | ||
1082 | e_int32 i; | ||
1083 | |||
1084 | if (!opll) | ||
1085 | return; | ||
1086 | |||
1087 | opll->adr = 0; | ||
1088 | opll->out = 0; | ||
1089 | |||
1090 | opll->pm_phase = 0; | ||
1091 | opll->am_phase = 0; | ||
1092 | |||
1093 | opll->noise_seed = 0xffff; | ||
1094 | opll->mask = 0; | ||
1095 | |||
1096 | for (i = 0; i <18; i++) | ||
1097 | OPLL_SLOT_reset(&opll->slot[i], i%2); | ||
1098 | |||
1099 | for (i = 0; i < 9; i++) | ||
1100 | { | ||
1101 | opll->key_status[i] = 0; | ||
1102 | setPatch (opll, i, 0); | ||
1103 | } | ||
1104 | |||
1105 | for (i = 0; i < 0x40; i++) | ||
1106 | OPLL_writeReg (opll, i, 0); | ||
1107 | |||
1108 | #ifndef EMU2413_COMPACTION | ||
1109 | opll->realstep = (e_uint32) ((1 << 31) / rate); | ||
1110 | opll->opllstep = (e_uint32) ((1 << 31) / (clk / 72)); | ||
1111 | opll->oplltime = 0; | ||
1112 | for (i = 0; i < 14; i++) | ||
1113 | opll->pan[i] = 2; | ||
1114 | opll->sprev[0] = opll->sprev[1] = 0; | ||
1115 | opll->snext[0] = opll->snext[1] = 0; | ||
1116 | #endif | ||
1117 | } | ||
1118 | |||
1119 | /* Force Refresh (When external program changes some parameters). */ | ||
1120 | void | ||
1121 | OPLL_forceRefresh (OPLL * opll) | ||
1122 | { | ||
1123 | e_int32 i; | ||
1124 | |||
1125 | if (opll == NULL) | ||
1126 | return; | ||
1127 | |||
1128 | for (i = 0; i < 9; i++) | ||
1129 | setPatch(opll,i,opll->patch_number[i]); | ||
1130 | |||
1131 | for (i = 0; i < 18; i++) | ||
1132 | { | ||
1133 | UPDATE_PG (&opll->slot[i]); | ||
1134 | UPDATE_RKS (&opll->slot[i]); | ||
1135 | UPDATE_TLL (&opll->slot[i]); | ||
1136 | UPDATE_WF (&opll->slot[i]); | ||
1137 | UPDATE_EG (&opll->slot[i]); | ||
1138 | } | ||
1139 | } | ||
1140 | |||
1141 | void | ||
1142 | OPLL_set_rate (OPLL * opll, e_uint32 r) | ||
1143 | { | ||
1144 | if (rate == r) return; | ||
1145 | if (opll->quality) | ||
1146 | rate = 49716; | ||
1147 | else | ||
1148 | rate = r; | ||
1149 | internal_refresh (); | ||
1150 | rate = r; | ||
1151 | } | ||
1152 | |||
1153 | void | ||
1154 | OPLL_set_quality (OPLL * opll, e_uint32 q) | ||
1155 | { | ||
1156 | opll->quality = q; | ||
1157 | OPLL_set_rate (opll, rate); | ||
1158 | } | ||
1159 | |||
1160 | /********************************************************* | ||
1161 | |||
1162 | Generate wave data | ||
1163 | |||
1164 | *********************************************************/ | ||
1165 | /* Convert Amp(0 to EG_HEIGHT) to Phase(0 to 2PI). */ | ||
1166 | #if ( SLOT_AMP_BITS - PG_BITS ) > 0 | ||
1167 | #define wave2_2pi(e) ( (e) >> ( SLOT_AMP_BITS - PG_BITS )) | ||
1168 | #else | ||
1169 | #define wave2_2pi(e) ( (e) << ( PG_BITS - SLOT_AMP_BITS )) | ||
1170 | #endif | ||
1171 | |||
1172 | /* Convert Amp(0 to EG_HEIGHT) to Phase(0 to 4PI). */ | ||
1173 | #if ( SLOT_AMP_BITS - PG_BITS - 1 ) == 0 | ||
1174 | #define wave2_4pi(e) (e) | ||
1175 | #elif ( SLOT_AMP_BITS - PG_BITS - 1 ) > 0 | ||
1176 | #define wave2_4pi(e) ( (e) >> ( SLOT_AMP_BITS - PG_BITS - 1 )) | ||
1177 | #else | ||
1178 | #define wave2_4pi(e) ( (e) << ( 1 + PG_BITS - SLOT_AMP_BITS )) | ||
1179 | #endif | ||
1180 | |||
1181 | /* Convert Amp(0 to EG_HEIGHT) to Phase(0 to 8PI). */ | ||
1182 | #if ( SLOT_AMP_BITS - PG_BITS - 2 ) == 0 | ||
1183 | #define wave2_8pi(e) (e) | ||
1184 | #elif ( SLOT_AMP_BITS - PG_BITS - 2 ) > 0 | ||
1185 | #define wave2_8pi(e) ( (e) >> ( SLOT_AMP_BITS - PG_BITS - 2 )) | ||
1186 | #else | ||
1187 | #define wave2_8pi(e) ( (e) << ( 2 + PG_BITS - SLOT_AMP_BITS )) | ||
1188 | #endif | ||
1189 | |||
1190 | /* Update AM, PM unit */ | ||
1191 | INLINE static void | ||
1192 | update_ampm (OPLL * opll) | ||
1193 | { | ||
1194 | opll->pm_phase = (opll->pm_phase + pm_dphase) & (PM_DP_WIDTH - 1); | ||
1195 | opll->am_phase = (opll->am_phase + am_dphase) & (AM_DP_WIDTH - 1); | ||
1196 | opll->lfo_am = AMTABLE(HIGHBITS (opll->am_phase, AM_DP_BITS - AM_PG_BITS)); | ||
1197 | opll->lfo_pm = PMTABLE(HIGHBITS (opll->pm_phase, PM_DP_BITS - PM_PG_BITS)); | ||
1198 | } | ||
1199 | |||
1200 | /* PG */ | ||
1201 | INLINE static void | ||
1202 | calc_phase (OPLL_SLOT * slot, e_int32 lfo) | ||
1203 | { | ||
1204 | if (slot->patch->PM) | ||
1205 | slot->phase += (slot->dphase * lfo) >> PM_AMP_BITS; | ||
1206 | else | ||
1207 | slot->phase += slot->dphase; | ||
1208 | |||
1209 | slot->phase &= (DP_WIDTH - 1); | ||
1210 | |||
1211 | slot->pgout = HIGHBITS (slot->phase, DP_BASE_BITS); | ||
1212 | } | ||
1213 | |||
1214 | /* Update Noise unit */ | ||
1215 | INLINE static void | ||
1216 | update_noise (OPLL * opll) | ||
1217 | { | ||
1218 | if(opll->noise_seed&1) opll->noise_seed ^= 0x8003020; | ||
1219 | opll->noise_seed >>= 1; | ||
1220 | } | ||
1221 | |||
1222 | /* EG */ | ||
1223 | INLINE static void | ||
1224 | calc_envelope (OPLL_SLOT * slot, e_int32 lfo) | ||
1225 | { | ||
1226 | #define S2E(x) (SL2EG((e_int32)(x/SL_STEP))<<(EG_DP_BITS-EG_BITS)) | ||
1227 | |||
1228 | static e_uint32 SL[16] = { | ||
1229 | S2E (0.0), S2E (3.0), S2E (6.0), S2E (9.0), S2E (12.0), S2E (15.0), S2E (18.0), S2E (21.0), | ||
1230 | S2E (24.0), S2E (27.0), S2E (30.0), S2E (33.0), S2E (36.0), S2E (39.0), S2E (42.0), S2E (48.0) | ||
1231 | }; | ||
1232 | |||
1233 | e_uint32 egout; | ||
1234 | |||
1235 | switch (slot->eg_mode) | ||
1236 | { | ||
1237 | case ATTACK: | ||
1238 | egout = AR_ADJUST_TABLE(HIGHBITS (slot->eg_phase, EG_DP_BITS - EG_BITS)); | ||
1239 | slot->eg_phase += slot->eg_dphase; | ||
1240 | if((EG_DP_WIDTH & slot->eg_phase)||(slot->patch->AR==15)) | ||
1241 | { | ||
1242 | egout = 0; | ||
1243 | slot->eg_phase = 0; | ||
1244 | slot->eg_mode = DECAY; | ||
1245 | UPDATE_EG (slot); | ||
1246 | } | ||
1247 | break; | ||
1248 | |||
1249 | case DECAY: | ||
1250 | egout = HIGHBITS (slot->eg_phase, EG_DP_BITS - EG_BITS); | ||
1251 | slot->eg_phase += slot->eg_dphase; | ||
1252 | if (slot->eg_phase >= SL[slot->patch->SL]) | ||
1253 | { | ||
1254 | if (slot->patch->EG) | ||
1255 | { | ||
1256 | slot->eg_phase = SL[slot->patch->SL]; | ||
1257 | slot->eg_mode = SUSHOLD; | ||
1258 | UPDATE_EG (slot); | ||
1259 | } | ||
1260 | else | ||
1261 | { | ||
1262 | slot->eg_phase = SL[slot->patch->SL]; | ||
1263 | slot->eg_mode = SUSTINE; | ||
1264 | UPDATE_EG (slot); | ||
1265 | } | ||
1266 | } | ||
1267 | break; | ||
1268 | |||
1269 | case SUSHOLD: | ||
1270 | egout = HIGHBITS (slot->eg_phase, EG_DP_BITS - EG_BITS); | ||
1271 | if (slot->patch->EG == 0) | ||
1272 | { | ||
1273 | slot->eg_mode = SUSTINE; | ||
1274 | UPDATE_EG (slot); | ||
1275 | } | ||
1276 | break; | ||
1277 | |||
1278 | case SUSTINE: | ||
1279 | case RELEASE: | ||
1280 | egout = HIGHBITS (slot->eg_phase, EG_DP_BITS - EG_BITS); | ||
1281 | slot->eg_phase += slot->eg_dphase; | ||
1282 | if (egout >= (1 << EG_BITS)) | ||
1283 | { | ||
1284 | slot->eg_mode = FINISH; | ||
1285 | egout = (1 << EG_BITS) - 1; | ||
1286 | } | ||
1287 | break; | ||
1288 | |||
1289 | case SETTLE: | ||
1290 | egout = HIGHBITS (slot->eg_phase, EG_DP_BITS - EG_BITS); | ||
1291 | slot->eg_phase += slot->eg_dphase; | ||
1292 | if (egout >= (1 << EG_BITS)) | ||
1293 | { | ||
1294 | slot->eg_mode = ATTACK; | ||
1295 | egout = (1 << EG_BITS) - 1; | ||
1296 | UPDATE_EG(slot); | ||
1297 | } | ||
1298 | break; | ||
1299 | |||
1300 | case FINISH: | ||
1301 | egout = (1 << EG_BITS) - 1; | ||
1302 | break; | ||
1303 | |||
1304 | default: | ||
1305 | egout = (1 << EG_BITS) - 1; | ||
1306 | break; | ||
1307 | } | ||
1308 | |||
1309 | if (slot->patch->AM) | ||
1310 | egout = EG2DB (egout + slot->tll) + lfo; | ||
1311 | else | ||
1312 | egout = EG2DB (egout + slot->tll); | ||
1313 | |||
1314 | if (egout >= DB_MUTE) | ||
1315 | egout = DB_MUTE - 1; | ||
1316 | |||
1317 | slot->egout = egout | 3; | ||
1318 | } | ||
1319 | |||
1320 | /* CARRIOR */ | ||
1321 | INLINE static e_int32 | ||
1322 | calc_slot_car (OPLL_SLOT * slot, e_int32 fm) | ||
1323 | { | ||
1324 | if (slot->egout >= (DB_MUTE - 1)) | ||
1325 | { | ||
1326 | slot->output[0] = 0; | ||
1327 | } | ||
1328 | else | ||
1329 | { | ||
1330 | slot->output[0] = DB2LIN_TABLE[slot->sintbl[(slot->pgout+wave2_8pi(fm))&(PG_WIDTH-1)] + slot->egout]; | ||
1331 | } | ||
1332 | |||
1333 | slot->output[1] = (slot->output[1] + slot->output[0]) >> 1; | ||
1334 | return slot->output[1]; | ||
1335 | } | ||
1336 | |||
1337 | /* MODULATOR */ | ||
1338 | INLINE static e_int32 | ||
1339 | calc_slot_mod (OPLL_SLOT * slot) | ||
1340 | { | ||
1341 | e_int32 fm; | ||
1342 | |||
1343 | slot->output[1] = slot->output[0]; | ||
1344 | |||
1345 | if (slot->egout >= (DB_MUTE - 1)) | ||
1346 | { | ||
1347 | slot->output[0] = 0; | ||
1348 | } | ||
1349 | else if (slot->patch->FB != 0) | ||
1350 | { | ||
1351 | fm = wave2_4pi (slot->feedback) >> (7 - slot->patch->FB); | ||
1352 | slot->output[0] = DB2LIN_TABLE[slot->sintbl[(slot->pgout+fm)&(PG_WIDTH-1)] + slot->egout]; | ||
1353 | } | ||
1354 | else | ||
1355 | { | ||
1356 | slot->output[0] = DB2LIN_TABLE[slot->sintbl[slot->pgout] + slot->egout]; | ||
1357 | } | ||
1358 | |||
1359 | slot->feedback = (slot->output[1] + slot->output[0]) >> 1; | ||
1360 | |||
1361 | return slot->feedback; | ||
1362 | |||
1363 | } | ||
1364 | |||
1365 | /* TOM */ | ||
1366 | INLINE static e_int32 | ||
1367 | calc_slot_tom (OPLL_SLOT * slot) | ||
1368 | { | ||
1369 | if (slot->egout >= (DB_MUTE - 1)) | ||
1370 | return 0; | ||
1371 | |||
1372 | return DB2LIN_TABLE[slot->sintbl[slot->pgout] + slot->egout]; | ||
1373 | |||
1374 | } | ||
1375 | |||
1376 | /* SNARE */ | ||
1377 | INLINE static e_int32 | ||
1378 | calc_slot_snare (OPLL_SLOT * slot, e_uint32 noise) | ||
1379 | { | ||
1380 | if(slot->egout>=(DB_MUTE-1)) | ||
1381 | return 0; | ||
1382 | |||
1383 | if(BIT(slot->pgout,7)) | ||
1384 | return DB2LIN_TABLE[(noise?DB_POS(0):DB_POS(15))+slot->egout]; | ||
1385 | else | ||
1386 | return DB2LIN_TABLE[(noise?DB_NEG(0):DB_NEG(15))+slot->egout]; | ||
1387 | } | ||
1388 | |||
1389 | /* | ||
1390 | TOP-CYM | ||
1391 | */ | ||
1392 | INLINE static e_int32 | ||
1393 | calc_slot_cym (OPLL_SLOT * slot, e_uint32 pgout_hh) | ||
1394 | { | ||
1395 | e_uint32 dbout; | ||
1396 | |||
1397 | if (slot->egout >= (DB_MUTE - 1)) | ||
1398 | return 0; | ||
1399 | else if( | ||
1400 | /* the same as fmopl.c */ | ||
1401 | ((BIT(pgout_hh,PG_BITS-8)^BIT(pgout_hh,PG_BITS-1))|BIT(pgout_hh,PG_BITS-7)) ^ | ||
1402 | /* different from fmopl.c */ | ||
1403 | (BIT(slot->pgout,PG_BITS-7)&!BIT(slot->pgout,PG_BITS-5)) | ||
1404 | ) | ||
1405 | dbout = DB_NEG(3); | ||
1406 | else | ||
1407 | dbout = DB_POS(3); | ||
1408 | |||
1409 | return DB2LIN_TABLE[dbout + slot->egout]; | ||
1410 | } | ||
1411 | |||
1412 | /* | ||
1413 | HI-HAT | ||
1414 | */ | ||
1415 | INLINE static e_int32 | ||
1416 | calc_slot_hat (OPLL_SLOT *slot, e_int32 pgout_cym, e_uint32 noise) | ||
1417 | { | ||
1418 | e_uint32 dbout; | ||
1419 | |||
1420 | if (slot->egout >= (DB_MUTE - 1)) | ||
1421 | return 0; | ||
1422 | else if( | ||
1423 | /* the same as fmopl.c */ | ||
1424 | ((BIT(slot->pgout,PG_BITS-8)^BIT(slot->pgout,PG_BITS-1))|BIT(slot->pgout,PG_BITS-7)) ^ | ||
1425 | /* different from fmopl.c */ | ||
1426 | (BIT(pgout_cym,PG_BITS-7)&!BIT(pgout_cym,PG_BITS-5)) | ||
1427 | ) | ||
1428 | { | ||
1429 | if(noise) | ||
1430 | dbout = DB_NEG(12); | ||
1431 | else | ||
1432 | dbout = DB_NEG(24); | ||
1433 | } | ||
1434 | else | ||
1435 | { | ||
1436 | if(noise) | ||
1437 | dbout = DB_POS(12); | ||
1438 | else | ||
1439 | dbout = DB_POS(24); | ||
1440 | } | ||
1441 | |||
1442 | return DB2LIN_TABLE[dbout + slot->egout]; | ||
1443 | } | ||
1444 | |||
1445 | static e_int16 | ||
1446 | calc (OPLL * opll) EMU2413_CALC_ICODE; | ||
1447 | static e_int16 | ||
1448 | calc (OPLL * opll) | ||
1449 | { | ||
1450 | e_int32 i; | ||
1451 | |||
1452 | update_ampm (opll); | ||
1453 | update_noise (opll); | ||
1454 | |||
1455 | for (i = 0; i < 18; i++) | ||
1456 | { | ||
1457 | calc_phase(&opll->slot[i],opll->lfo_pm); | ||
1458 | calc_envelope(&opll->slot[i],opll->lfo_am); | ||
1459 | } | ||
1460 | |||
1461 | e_uint32 channel_mask = opll->mask; | ||
1462 | for (i = 0; i < 9; i++) { | ||
1463 | if (CAR(opll,i)->eg_mode != FINISH) | ||
1464 | channel_mask |= (1 << i); | ||
1465 | } | ||
1466 | |||
1467 | e_int32 mix = 0; | ||
1468 | |||
1469 | /* CH6 */ | ||
1470 | if (opll->patch_number[6] & 0x10) { | ||
1471 | if (channel_mask & OPLL_MASK_CH (6)) { | ||
1472 | mix += calc_slot_car (CAR(opll,6), calc_slot_mod(MOD(opll,6))); | ||
1473 | channel_mask &= ~(1 << 6); | ||
1474 | } | ||
1475 | } | ||
1476 | |||
1477 | /* CH7 */ | ||
1478 | if (opll->patch_number[7] & 0x10) { | ||
1479 | if (MOD(opll,7)->eg_mode != FINISH) | ||
1480 | mix += calc_slot_hat (MOD(opll,7), CAR(opll,8)->pgout, opll->noise_seed&1); | ||
1481 | if (channel_mask & OPLL_MASK_SD) { | ||
1482 | mix -= calc_slot_snare (CAR(opll,7), opll->noise_seed&1); | ||
1483 | channel_mask &= ~OPLL_MASK_SD; | ||
1484 | } | ||
1485 | } | ||
1486 | |||
1487 | /* CH8 */ | ||
1488 | if (opll->patch_number[8] & 0x10) { | ||
1489 | if (MOD(opll,8)->eg_mode != FINISH) | ||
1490 | mix += calc_slot_tom (MOD(opll,8)); | ||
1491 | if (channel_mask & OPLL_MASK_CYM) { | ||
1492 | mix -= calc_slot_cym (CAR(opll,8), MOD(opll,7)->pgout); | ||
1493 | channel_mask &= ~OPLL_MASK_CYM; | ||
1494 | } | ||
1495 | } | ||
1496 | |||
1497 | mix <<= 1; | ||
1498 | |||
1499 | opll->current_mask = channel_mask; | ||
1500 | for (i = 0; channel_mask; channel_mask >>= 1, ++i) { | ||
1501 | if (channel_mask & 1) { | ||
1502 | mix += calc_slot_car (CAR(opll,i), calc_slot_mod(MOD(opll,i))); | ||
1503 | } | ||
1504 | } | ||
1505 | |||
1506 | return (e_int16) mix << 3; | ||
1507 | } | ||
1508 | |||
1509 | void | ||
1510 | OPLL_set_internal_mute(OPLL * opll, e_uint32 mute) | ||
1511 | { | ||
1512 | opll->internal_mute = mute; | ||
1513 | } | ||
1514 | |||
1515 | e_uint32 | ||
1516 | OPLL_is_internal_muted(OPLL * opll) | ||
1517 | { | ||
1518 | return opll->internal_mute; | ||
1519 | } | ||
1520 | |||
1521 | static e_uint32 | ||
1522 | check_mute_helper(OPLL * opll) | ||
1523 | { | ||
1524 | for (int i = 0; i < 6; i++) { | ||
1525 | /* if (ch[i].car.eg_mode != FINISH) return 0; */ | ||
1526 | if (!(opll->current_mask & OPLL_MASK_CH (i)) && (CAR(opll,i)->eg_mode != FINISH)) return 0; | ||
1527 | } | ||
1528 | |||
1529 | if (!(opll->reg[0x0e] & 0x20)) { | ||
1530 | for(int i = 6; i < 9; i++) { | ||
1531 | /* if (ch[i].car.eg_mode != FINISH) return 0; */ | ||
1532 | if (!(opll->current_mask & OPLL_MASK_CH (i)) && (CAR(opll,i)->eg_mode != FINISH)) return 0; | ||
1533 | } | ||
1534 | } else { | ||
1535 | /* if (ch[6].car.eg_mode != FINISH) return false; | ||
1536 | if (ch[7].mod.eg_mode != FINISH) return false; | ||
1537 | if (ch[7].car.eg_mode != FINISH) return false; | ||
1538 | if (ch[8].mod.eg_mode != FINISH) return false; | ||
1539 | if (ch[8].car.eg_mode != FINISH) return false; */ | ||
1540 | if (!(opll->current_mask & OPLL_MASK_CH (6)) && (CAR(opll,6)->eg_mode != FINISH)) return 0; | ||
1541 | if (!(opll->current_mask & OPLL_MASK_CH (7)) && (MOD(opll,7)->eg_mode != FINISH)) return 0; | ||
1542 | if (!(opll->current_mask & OPLL_MASK_CH (7)) && (CAR(opll,7)->eg_mode != FINISH)) return 0; | ||
1543 | if (!(opll->current_mask & OPLL_MASK_CH (8)) && (MOD(opll,8)->eg_mode != FINISH)) return 0; | ||
1544 | if (!(opll->current_mask & OPLL_MASK_CH (8)) && (CAR(opll,8)->eg_mode != FINISH)) return 0; | ||
1545 | } | ||
1546 | |||
1547 | return 1; /* nothing is playing, then mute */ | ||
1548 | } | ||
1549 | |||
1550 | static void | ||
1551 | check_mute(OPLL * opll) | ||
1552 | { | ||
1553 | OPLL_set_internal_mute (opll, check_mute_helper (opll)); | ||
1554 | } | ||
1555 | |||
1556 | EMU2413_API e_int16 *OPLL_update_buffer(OPLL * opll, e_uint32 length) | ||
1557 | { | ||
1558 | e_int16* buf = opll->buffer; | ||
1559 | while (length--) { | ||
1560 | *(buf++) = calc (opll); | ||
1561 | } | ||
1562 | check_mute (opll); | ||
1563 | |||
1564 | return opll->buffer; | ||
1565 | } | ||
1566 | |||
1567 | #ifdef EMU2413_COMPACTION | ||
1568 | e_int16 | ||
1569 | OPLL_calc (OPLL * opll) | ||
1570 | { | ||
1571 | return calc (opll); | ||
1572 | } | ||
1573 | #else | ||
1574 | e_int16 | ||
1575 | OPLL_calc (OPLL * opll) | ||
1576 | { | ||
1577 | if (!opll->quality) | ||
1578 | return calc (opll); | ||
1579 | |||
1580 | while (opll->realstep > opll->oplltime) | ||
1581 | { | ||
1582 | opll->oplltime += opll->opllstep; | ||
1583 | opll->prev = opll->next; | ||
1584 | opll->next = calc (opll); | ||
1585 | } | ||
1586 | |||
1587 | opll->oplltime -= opll->realstep; | ||
1588 | opll->out = (e_int16) (((double) opll->next * (opll->opllstep - opll->oplltime) | ||
1589 | + (double) opll->prev * opll->oplltime) / opll->opllstep); | ||
1590 | |||
1591 | return (e_int16) opll->out; | ||
1592 | } | ||
1593 | #endif | ||
1594 | |||
1595 | e_uint32 | ||
1596 | OPLL_setMask (OPLL * opll, e_uint32 mask) | ||
1597 | { | ||
1598 | e_uint32 ret; | ||
1599 | |||
1600 | if (opll) | ||
1601 | { | ||
1602 | ret = opll->mask; | ||
1603 | opll->mask = mask; | ||
1604 | return ret; | ||
1605 | } | ||
1606 | else | ||
1607 | return 0; | ||
1608 | } | ||
1609 | |||
1610 | e_uint32 | ||
1611 | OPLL_toggleMask (OPLL * opll, e_uint32 mask) | ||
1612 | { | ||
1613 | e_uint32 ret; | ||
1614 | |||
1615 | if (opll) | ||
1616 | { | ||
1617 | ret = opll->mask; | ||
1618 | opll->mask ^= mask; | ||
1619 | return ret; | ||
1620 | } | ||
1621 | else | ||
1622 | return 0; | ||
1623 | } | ||
1624 | |||
1625 | /**************************************************** | ||
1626 | |||
1627 | I/O Ctrl | ||
1628 | |||
1629 | *****************************************************/ | ||
1630 | |||
1631 | void | ||
1632 | OPLL_writeReg (OPLL * opll, e_uint32 reg, e_uint32 data) | ||
1633 | { | ||
1634 | e_int32 i, v, ch; | ||
1635 | |||
1636 | data = data & 0xff; | ||
1637 | reg = reg & 0x3f; | ||
1638 | opll->reg[reg] = (e_uint8) data; | ||
1639 | |||
1640 | switch (reg) | ||
1641 | { | ||
1642 | case 0x00: | ||
1643 | opll->patch[0].AM = (data >> 7) & 1; | ||
1644 | opll->patch[0].PM = (data >> 6) & 1; | ||
1645 | opll->patch[0].EG = (data >> 5) & 1; | ||
1646 | opll->patch[0].KR = (data >> 4) & 1; | ||
1647 | opll->patch[0].ML = (data) & 15; | ||
1648 | for (i = 0; i < 9; i++) | ||
1649 | { | ||
1650 | if (opll->patch_number[i] == 0) | ||
1651 | { | ||
1652 | UPDATE_PG (MOD(opll,i)); | ||
1653 | UPDATE_RKS (MOD(opll,i)); | ||
1654 | UPDATE_EG (MOD(opll,i)); | ||
1655 | } | ||
1656 | } | ||
1657 | break; | ||
1658 | |||
1659 | case 0x01: | ||
1660 | opll->patch[1].AM = (data >> 7) & 1; | ||
1661 | opll->patch[1].PM = (data >> 6) & 1; | ||
1662 | opll->patch[1].EG = (data >> 5) & 1; | ||
1663 | opll->patch[1].KR = (data >> 4) & 1; | ||
1664 | opll->patch[1].ML = (data) & 15; | ||
1665 | for (i = 0; i < 9; i++) | ||
1666 | { | ||
1667 | if (opll->patch_number[i] == 0) | ||
1668 | { | ||
1669 | UPDATE_PG (CAR(opll,i)); | ||
1670 | UPDATE_RKS (CAR(opll,i)); | ||
1671 | UPDATE_EG (CAR(opll,i)); | ||
1672 | } | ||
1673 | } | ||
1674 | break; | ||
1675 | |||
1676 | case 0x02: | ||
1677 | opll->patch[0].KL = (data >> 6) & 3; | ||
1678 | opll->patch[0].TL = (data) & 63; | ||
1679 | for (i = 0; i < 9; i++) | ||
1680 | { | ||
1681 | if (opll->patch_number[i] == 0) | ||
1682 | { | ||
1683 | UPDATE_TLL(MOD(opll,i)); | ||
1684 | } | ||
1685 | } | ||
1686 | break; | ||
1687 | |||
1688 | case 0x03: | ||
1689 | opll->patch[1].KL = (data >> 6) & 3; | ||
1690 | opll->patch[1].WF = (data >> 4) & 1; | ||
1691 | opll->patch[0].WF = (data >> 3) & 1; | ||
1692 | opll->patch[0].FB = (data) & 7; | ||
1693 | for (i = 0; i < 9; i++) | ||
1694 | { | ||
1695 | if (opll->patch_number[i] == 0) | ||
1696 | { | ||
1697 | UPDATE_WF(MOD(opll,i)); | ||
1698 | UPDATE_WF(CAR(opll,i)); | ||
1699 | } | ||
1700 | } | ||
1701 | break; | ||
1702 | |||
1703 | case 0x04: | ||
1704 | opll->patch[0].AR = (data >> 4) & 15; | ||
1705 | opll->patch[0].DR = (data) & 15; | ||
1706 | for (i = 0; i < 9; i++) | ||
1707 | { | ||
1708 | if (opll->patch_number[i] == 0) | ||
1709 | { | ||
1710 | UPDATE_EG (MOD(opll,i)); | ||
1711 | } | ||
1712 | } | ||
1713 | break; | ||
1714 | |||
1715 | case 0x05: | ||
1716 | opll->patch[1].AR = (data >> 4) & 15; | ||
1717 | opll->patch[1].DR = (data) & 15; | ||
1718 | for (i = 0; i < 9; i++) | ||
1719 | { | ||
1720 | if (opll->patch_number[i] == 0) | ||
1721 | { | ||
1722 | UPDATE_EG(CAR(opll,i)); | ||
1723 | } | ||
1724 | } | ||
1725 | break; | ||
1726 | |||
1727 | case 0x06: | ||
1728 | opll->patch[0].SL = (data >> 4) & 15; | ||
1729 | opll->patch[0].RR = (data) & 15; | ||
1730 | for (i = 0; i < 9; i++) | ||
1731 | { | ||
1732 | if (opll->patch_number[i] == 0) | ||
1733 | { | ||
1734 | UPDATE_EG (MOD(opll,i)); | ||
1735 | } | ||
1736 | } | ||
1737 | break; | ||
1738 | |||
1739 | case 0x07: | ||
1740 | opll->patch[1].SL = (data >> 4) & 15; | ||
1741 | opll->patch[1].RR = (data) & 15; | ||
1742 | for (i = 0; i < 9; i++) | ||
1743 | { | ||
1744 | if (opll->patch_number[i] == 0) | ||
1745 | { | ||
1746 | UPDATE_EG (CAR(opll,i)); | ||
1747 | } | ||
1748 | } | ||
1749 | break; | ||
1750 | |||
1751 | case 0x0e: | ||
1752 | update_rhythm_mode (opll); | ||
1753 | if (data & 32) | ||
1754 | { | ||
1755 | if (data & 0x10) | ||
1756 | keyOn_BD (opll); | ||
1757 | else | ||
1758 | keyOff_BD (opll); | ||
1759 | if (data & 0x8) | ||
1760 | keyOn_SD (opll); | ||
1761 | else | ||
1762 | keyOff_SD (opll); | ||
1763 | if (data & 0x4) | ||
1764 | keyOn_TOM (opll); | ||
1765 | else | ||
1766 | keyOff_TOM (opll); | ||
1767 | if (data & 0x2) | ||
1768 | keyOn_CYM (opll); | ||
1769 | else | ||
1770 | keyOff_CYM (opll); | ||
1771 | if (data & 0x1) | ||
1772 | keyOn_HH (opll); | ||
1773 | else | ||
1774 | keyOff_HH (opll); | ||
1775 | } | ||
1776 | update_key_status (opll); | ||
1777 | |||
1778 | UPDATE_ALL (MOD(opll,6)); | ||
1779 | UPDATE_ALL (CAR(opll,6)); | ||
1780 | UPDATE_ALL (MOD(opll,7)); | ||
1781 | UPDATE_ALL (CAR(opll,7)); | ||
1782 | UPDATE_ALL (MOD(opll,8)); | ||
1783 | UPDATE_ALL (CAR(opll,8)); | ||
1784 | |||
1785 | break; | ||
1786 | |||
1787 | case 0x0f: | ||
1788 | break; | ||
1789 | |||
1790 | case 0x10: | ||
1791 | case 0x11: | ||
1792 | case 0x12: | ||
1793 | case 0x13: | ||
1794 | case 0x14: | ||
1795 | case 0x15: | ||
1796 | case 0x16: | ||
1797 | case 0x17: | ||
1798 | case 0x18: | ||
1799 | ch = reg - 0x10; | ||
1800 | setFnumber (opll, ch, data + ((opll->reg[0x20 + ch] & 1) << 8)); | ||
1801 | UPDATE_ALL (MOD(opll,ch)); | ||
1802 | UPDATE_ALL (CAR(opll,ch)); | ||
1803 | break; | ||
1804 | |||
1805 | case 0x20: | ||
1806 | case 0x21: | ||
1807 | case 0x22: | ||
1808 | case 0x23: | ||
1809 | case 0x24: | ||
1810 | case 0x25: | ||
1811 | case 0x26: | ||
1812 | case 0x27: | ||
1813 | case 0x28: | ||
1814 | ch = reg - 0x20; | ||
1815 | setFnumber (opll, ch, ((data & 1) << 8) + opll->reg[0x10 + ch]); | ||
1816 | setBlock (opll, ch, (data >> 1) & 7); | ||
1817 | setSustine (opll, ch, (data >> 5) & 1); | ||
1818 | if (data & 0x10) | ||
1819 | keyOn (opll, ch); | ||
1820 | else | ||
1821 | keyOff (opll, ch); | ||
1822 | UPDATE_ALL (MOD(opll,ch)); | ||
1823 | UPDATE_ALL (CAR(opll,ch)); | ||
1824 | update_key_status (opll); | ||
1825 | update_rhythm_mode (opll); | ||
1826 | break; | ||
1827 | |||
1828 | case 0x30: | ||
1829 | case 0x31: | ||
1830 | case 0x32: | ||
1831 | case 0x33: | ||
1832 | case 0x34: | ||
1833 | case 0x35: | ||
1834 | case 0x36: | ||
1835 | case 0x37: | ||
1836 | case 0x38: | ||
1837 | i = (data >> 4) & 15; | ||
1838 | v = data & 15; | ||
1839 | if ((opll->reg[0x0e] & 32) && (reg >= 0x36)) | ||
1840 | { | ||
1841 | switch (reg) | ||
1842 | { | ||
1843 | case 0x37: | ||
1844 | setSlotVolume (MOD(opll,7), i << 2); | ||
1845 | break; | ||
1846 | case 0x38: | ||
1847 | setSlotVolume (MOD(opll,8), i << 2); | ||
1848 | break; | ||
1849 | default: | ||
1850 | break; | ||
1851 | } | ||
1852 | } | ||
1853 | else | ||
1854 | { | ||
1855 | setPatch (opll, reg - 0x30, i); | ||
1856 | } | ||
1857 | setVolume (opll, reg - 0x30, v << 2); | ||
1858 | UPDATE_ALL (MOD(opll,reg - 0x30)); | ||
1859 | UPDATE_ALL (CAR(opll,reg - 0x30)); | ||
1860 | break; | ||
1861 | |||
1862 | default: | ||
1863 | break; | ||
1864 | |||
1865 | } | ||
1866 | } | ||
1867 | |||
1868 | void | ||
1869 | OPLL_writeIO (OPLL * opll, e_uint32 adr, e_uint32 val) | ||
1870 | { | ||
1871 | if (adr & 1) | ||
1872 | OPLL_writeReg (opll, opll->adr, val); | ||
1873 | else | ||
1874 | opll->adr = val; | ||
1875 | } | ||
1876 | |||
1877 | e_uint32 | ||
1878 | OPLL_read(OPLL * opll, e_uint32 a) | ||
1879 | { | ||
1880 | if( !(a&1) ) | ||
1881 | { | ||
1882 | /* status port */ | ||
1883 | return opll->status; | ||
1884 | } | ||
1885 | return 0xff; | ||
1886 | } | ||
1887 | |||
1888 | #ifndef EMU2413_COMPACTION | ||
1889 | /* STEREO MODE (OPT) */ | ||
1890 | void | ||
1891 | OPLL_set_pan (OPLL * opll, e_uint32 ch, e_uint32 pan) | ||
1892 | { | ||
1893 | opll->pan[ch & 15] = pan & 3; | ||
1894 | } | ||
1895 | |||
1896 | static void | ||
1897 | calc_stereo (OPLL * opll, e_int32 out[2]) | ||
1898 | { | ||
1899 | e_int32 b[4] = { 0, 0, 0, 0 }; /* Ignore, Right, Left, Center */ | ||
1900 | e_int32 r[4] = { 0, 0, 0, 0 }; /* Ignore, Right, Left, Center */ | ||
1901 | e_int32 i; | ||
1902 | |||
1903 | update_ampm (opll); | ||
1904 | update_noise (opll); | ||
1905 | |||
1906 | for(i=0;i<18;i++) | ||
1907 | { | ||
1908 | calc_phase(&opll->slot[i],opll->lfo_pm); | ||
1909 | calc_envelope(&opll->slot[i],opll->lfo_am); | ||
1910 | } | ||
1911 | |||
1912 | for (i = 0; i < 6; i++) | ||
1913 | if (!(opll->mask & OPLL_MASK_CH (i)) && (CAR(opll,i)->eg_mode != FINISH)) | ||
1914 | b[opll->pan[i]] += calc_slot_car (CAR(opll,i), calc_slot_mod (MOD(opll,i))); | ||
1915 | |||
1916 | |||
1917 | if (opll->patch_number[6] <= 15) | ||
1918 | { | ||
1919 | if (!(opll->mask & OPLL_MASK_CH (6)) && (CAR(opll,6)->eg_mode != FINISH)) | ||
1920 | b[opll->pan[6]] += calc_slot_car (CAR(opll,6), calc_slot_mod (MOD(opll,6))); | ||
1921 | } | ||
1922 | else | ||
1923 | { | ||
1924 | if (!(opll->mask & OPLL_MASK_BD) && (CAR(opll,6)->eg_mode != FINISH)) | ||
1925 | r[opll->pan[9]] += calc_slot_car (CAR(opll,6), calc_slot_mod (MOD(opll,6))); | ||
1926 | } | ||
1927 | |||
1928 | if (opll->patch_number[7] <= 15) | ||
1929 | { | ||
1930 | if (!(opll->mask & OPLL_MASK_CH (7)) && (CAR (opll,7)->eg_mode != FINISH)) | ||
1931 | b[opll->pan[7]] += calc_slot_car (CAR (opll,7), calc_slot_mod (MOD (opll,7))); | ||
1932 | } | ||
1933 | else | ||
1934 | { | ||
1935 | if (!(opll->mask & OPLL_MASK_HH) && (MOD (opll,7)->eg_mode != FINISH)) | ||
1936 | r[opll->pan[10]] += calc_slot_hat (MOD (opll,7), CAR(opll,8)->pgout, opll->noise_seed&1); | ||
1937 | if (!(opll->mask & OPLL_MASK_SD) && (CAR (opll,7)->eg_mode != FINISH)) | ||
1938 | r[opll->pan[11]] -= calc_slot_snare (CAR (opll,7), opll->noise_seed&1); | ||
1939 | } | ||
1940 | |||
1941 | if (opll->patch_number[8] <= 15) | ||
1942 | { | ||
1943 | if (!(opll->mask & OPLL_MASK_CH (8)) && (CAR (opll,8)->eg_mode != FINISH)) | ||
1944 | b[opll->pan[8]] += calc_slot_car (CAR (opll,8), calc_slot_mod (MOD (opll,8))); | ||
1945 | } | ||
1946 | else | ||
1947 | { | ||
1948 | if (!(opll->mask & OPLL_MASK_TOM) && (MOD (opll,8)->eg_mode != FINISH)) | ||
1949 | r[opll->pan[12]] += calc_slot_tom (MOD (opll,8)); | ||
1950 | if (!(opll->mask & OPLL_MASK_CYM) && (CAR (opll,8)->eg_mode != FINISH)) | ||
1951 | r[opll->pan[13]] -= calc_slot_cym (CAR (opll,8), MOD(opll,7)->pgout); | ||
1952 | } | ||
1953 | |||
1954 | out[1] = (b[1] + b[3] + ((r[1] + r[3]) << 1)) <<3; | ||
1955 | out[0] = (b[2] + b[3] + ((r[2] + r[3]) << 1)) <<3; | ||
1956 | } | ||
1957 | |||
1958 | void | ||
1959 | OPLL_calc_stereo (OPLL * opll, e_int32 out[2]) | ||
1960 | { | ||
1961 | if (!opll->quality) | ||
1962 | { | ||
1963 | calc_stereo (opll, out); | ||
1964 | return; | ||
1965 | } | ||
1966 | |||
1967 | while (opll->realstep > opll->oplltime) | ||
1968 | { | ||
1969 | opll->oplltime += opll->opllstep; | ||
1970 | opll->sprev[0] = opll->snext[0]; | ||
1971 | opll->sprev[1] = opll->snext[1]; | ||
1972 | calc_stereo (opll, opll->snext); | ||
1973 | } | ||
1974 | |||
1975 | opll->oplltime -= opll->realstep; | ||
1976 | out[0] = (e_int16) (((double) opll->snext[0] * (opll->opllstep - opll->oplltime) | ||
1977 | + (double) opll->sprev[0] * opll->oplltime) / opll->opllstep); | ||
1978 | out[1] = (e_int16) (((double) opll->snext[1] * (opll->opllstep - opll->oplltime) | ||
1979 | + (double) opll->sprev[1] * opll->oplltime) / opll->opllstep); | ||
1980 | } | ||
1981 | #endif /* EMU2413_COMPACTION */ | ||