OSDN Git Service

[VM][DEVICE] Enable to build with new state framework.
[csp-qt/common_source_project-fm7.git] / source / src / vm / fmgen / fmgen.cpp
1 // ---------------------------------------------------------------------------
2 //      FM Sound Generator - Core Unit
3 //      Copyright (C) cisc 1998, 2003.
4 // ---------------------------------------------------------------------------
5 //      $Id: fmgen.cpp,v 1.50 2003/09/10 13:19:34 cisc Exp $
6 // ---------------------------------------------------------------------------
7 //      \8eQ\8dl:
8 //              FM sound generator for M.A.M.E., written by Tatsuyuki Satoh.
9 //
10 //      \93ä:
11 //              OPNB \82Ì CSM \83\82\81[\83h(\8ed\97l\82ª\82æ\82­\82í\82©\82ç\82È\82¢)
12 //
13 //      \90§\8cÀ:
14 //              \81EAR!=31 \82Å SSGEC \82ð\8eg\82¤\82Æ\94g\8c`\82ª\8eÀ\8dÛ\82Æ\88Ù\82È\82é\89Â\94\\90«\82 \82è
15 //
16 //      \8eÓ\8e«:
17 //              Tatsuyuki Satoh \82³\82ñ(fm.c)
18 //              Hiromitsu Shioya \82³\82ñ(ADPCM-A)
19 //              DMP-SOFT. \82³\82ñ(OPNB)
20 //              KAJA \82³\82ñ(test program)
21 //              \82Ù\82©\8cf\8e¦\94Â\93\99\82Å\97l\81X\82È\82²\8f\95\8c¾\81C\82²\8ex\89\87\82ð\82¨\8añ\82¹\82¢\82½\82¾\82¢\82½\8aF\97l\82É
22 // ---------------------------------------------------------------------------
23
24 #include "headers.h"
25 #include "misc.h"
26 #include "fmgen.h"
27 #include "fmgeninl.h"
28
29 #include "../../fileio.h"
30
31 #define LOGNAME "fmgen"
32
33 // ---------------------------------------------------------------------------
34
35 #define FM_EG_BOTTOM 955
36
37 // ---------------------------------------------------------------------------
38 //      Table/etc
39 //
40 namespace FM
41 {
42         const uint8 Operator::notetable[128] =
43         {
44                  0,  0,  0,  0,  0,  0,  0,  1,  2,  3,  3,  3,  3,  3,  3,  3, 
45                  4,  4,  4,  4,  4,  4,  4,  5,  6,  7,  7,  7,  7,  7,  7,  7, 
46                  8,  8,  8,  8,  8,  8,  8,  9, 10, 11, 11, 11, 11, 11, 11, 11, 
47                 12, 12, 12, 12, 12, 12, 12, 13, 14, 15, 15, 15, 15, 15, 15, 15, 
48                 16, 16, 16, 16, 16, 16, 16, 17, 18, 19, 19, 19, 19, 19, 19, 19, 
49                 20, 20, 20, 20, 20, 20, 20, 21, 22, 23, 23, 23, 23, 23, 23, 23, 
50                 24, 24, 24, 24, 24, 24, 24, 25, 26, 27, 27, 27, 27, 27, 27, 27, 
51                 28, 28, 28, 28, 28, 28, 28, 29, 30, 31, 31, 31, 31, 31, 31, 31, 
52         };
53         
54         const int8 Operator::dttable[256] =
55         {
56                   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
57                   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
58                   0,  0,  0,  0,  2,  2,  2,  2,  2,  2,  2,  2,  4,  4,  4,  4,
59                   4,  6,  6,  6,  8,  8,  8, 10, 10, 12, 12, 14, 16, 16, 16, 16,
60                   2,  2,  2,  2,  4,  4,  4,  4,  4,  6,  6,  6,  8,  8,  8, 10,
61                  10, 12, 12, 14, 16, 16, 18, 20, 22, 24, 26, 28, 32, 32, 32, 32,
62                   4,  4,  4,  4,  4,  6,  6,  6,  8,  8,  8, 10, 10, 12, 12, 14,
63                  16, 16, 18, 20, 22, 24, 26, 28, 32, 34, 38, 40, 44, 44, 44, 44,
64                   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
65                   0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
66                   0,  0,  0,  0, -2, -2, -2, -2, -2, -2, -2, -2, -4, -4, -4, -4,
67                  -4, -6, -6, -6, -8, -8, -8,-10,-10,-12,-12,-14,-16,-16,-16,-16,
68                  -2, -2, -2, -2, -4, -4, -4, -4, -4, -6, -6, -6, -8, -8, -8,-10,
69                 -10,-12,-12,-14,-16,-16,-18,-20,-22,-24,-26,-28,-32,-32,-32,-32,
70                  -4, -4, -4, -4, -4, -6, -6, -6, -8, -8, -8,-10,-10,-12,-12,-14,
71                 -16,-16,-18,-20,-22,-24,-26,-28,-32,-34,-38,-40,-44,-44,-44,-44,
72         };
73
74         const int8 Operator::decaytable1[64][8] = 
75         {
76                 0, 0, 0, 0, 0, 0, 0, 0,         0, 0, 0, 0, 0, 0, 0, 0,
77                 1, 1, 1, 1, 1, 1, 1, 1,         1, 1, 1, 1, 1, 1, 1, 1,
78                 1, 1, 1, 1, 1, 1, 1, 1,         1, 1, 1, 1, 1, 1, 1, 1,
79                 1, 1, 1, 0, 1, 1, 1, 0,         1, 1, 1, 0, 1, 1, 1, 0,
80                 1, 0, 1, 0, 1, 0, 1, 0,         1, 1, 1, 0, 1, 0, 1, 0,
81                 1, 1, 1, 0, 1, 1, 1, 0,         1, 1, 1, 1, 1, 1, 1, 0,
82                 1, 0, 1, 0, 1, 0, 1, 0,         1, 1, 1, 0, 1, 0, 1, 0,
83                 1, 1, 1, 0, 1, 1, 1, 0,         1, 1, 1, 1, 1, 1, 1, 0,
84                 1, 0, 1, 0, 1, 0, 1, 0,         1, 1, 1, 0, 1, 0, 1, 0,
85                 1, 1, 1, 0, 1, 1, 1, 0,         1, 1, 1, 1, 1, 1, 1, 0,
86                 1, 0, 1, 0, 1, 0, 1, 0,         1, 1, 1, 0, 1, 0, 1, 0,
87                 1, 1, 1, 0, 1, 1, 1, 0,         1, 1, 1, 1, 1, 1, 1, 0,
88                 1, 0, 1, 0, 1, 0, 1, 0,         1, 1, 1, 0, 1, 0, 1, 0,
89                 1, 1, 1, 0, 1, 1, 1, 0,         1, 1, 1, 1, 1, 1, 1, 0,
90                 1, 0, 1, 0, 1, 0, 1, 0,         1, 1, 1, 0, 1, 0, 1, 0,
91                 1, 1, 1, 0, 1, 1, 1, 0,         1, 1, 1, 1, 1, 1, 1, 0,
92                 1, 0, 1, 0, 1, 0, 1, 0,         1, 1, 1, 0, 1, 0, 1, 0,
93                 1, 1, 1, 0, 1, 1, 1, 0,         1, 1, 1, 1, 1, 1, 1, 0,
94                 1, 0, 1, 0, 1, 0, 1, 0,         1, 1, 1, 0, 1, 0, 1, 0,
95                 1, 1, 1, 0, 1, 1, 1, 0,         1, 1, 1, 1, 1, 1, 1, 0,
96                 1, 0, 1, 0, 1, 0, 1, 0,         1, 1, 1, 0, 1, 0, 1, 0,
97                 1, 1, 1, 0, 1, 1, 1, 0,         1, 1, 1, 1, 1, 1, 1, 0,
98                 1, 0, 1, 0, 1, 0, 1, 0,         1, 1, 1, 0, 1, 0, 1, 0,
99                 1, 1, 1, 0, 1, 1, 1, 0,         1, 1, 1, 1, 1, 1, 1, 0,
100                 1, 1, 1, 1, 1, 1, 1, 1,         2, 1, 1, 1, 2, 1, 1, 1,
101                 2, 1, 2, 1, 2, 1, 2, 1,         2, 2, 2, 1, 2, 2, 2, 1,
102                 2, 2, 2, 2, 2, 2, 2, 2,         4, 2, 2, 2, 4, 2, 2, 2, 
103                 4, 2, 4, 2, 4, 2, 4, 2,         4, 4, 4, 2, 4, 4, 4, 2,
104                 4, 4, 4, 4, 4, 4, 4, 4,         8, 4, 4, 4, 8, 4, 4, 4, 
105                 8, 4, 8, 4, 8, 4, 8, 4,         8, 8, 8, 4, 8, 8, 8, 4,
106                 16,16,16,16,16,16,16,16,        16,16,16,16,16,16,16,16,
107                 16,16,16,16,16,16,16,16,        16,16,16,16,16,16,16,16,
108         };
109
110         const int Operator::decaytable2[16] = 
111         {
112                 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2047, 2047, 2047, 2047, 2047
113         };
114
115         const int8 Operator::attacktable[64][8] = 
116         {
117                 -1,-1,-1,-1,-1,-1,-1,-1,        -1,-1,-1,-1,-1,-1,-1,-1,
118                  4, 4, 4, 4, 4, 4, 4, 4,         4, 4, 4, 4, 4, 4, 4, 4,
119                  4, 4, 4, 4, 4, 4, 4, 4,         4, 4, 4, 4, 4, 4, 4, 4,
120                  4, 4, 4,-1, 4, 4, 4,-1,         4, 4, 4,-1, 4, 4, 4,-1,
121                  4,-1, 4,-1, 4,-1, 4,-1,         4, 4, 4,-1, 4,-1, 4,-1,
122                  4, 4, 4,-1, 4, 4, 4,-1,         4, 4, 4, 4, 4, 4, 4,-1,
123                  4,-1, 4,-1, 4,-1, 4,-1,         4, 4, 4,-1, 4,-1, 4,-1,
124                  4, 4, 4,-1, 4, 4, 4,-1,         4, 4, 4, 4, 4, 4, 4,-1,
125                  4,-1, 4,-1, 4,-1, 4,-1,         4, 4, 4,-1, 4,-1, 4,-1,
126                  4, 4, 4,-1, 4, 4, 4,-1,         4, 4, 4, 4, 4, 4, 4,-1,
127                  4,-1, 4,-1, 4,-1, 4,-1,         4, 4, 4,-1, 4,-1, 4,-1,
128                  4, 4, 4,-1, 4, 4, 4,-1,         4, 4, 4, 4, 4, 4, 4,-1,
129                  4,-1, 4,-1, 4,-1, 4,-1,         4, 4, 4,-1, 4,-1, 4,-1,
130                  4, 4, 4,-1, 4, 4, 4,-1,         4, 4, 4, 4, 4, 4, 4,-1,
131                  4,-1, 4,-1, 4,-1, 4,-1,         4, 4, 4,-1, 4,-1, 4,-1,
132                  4, 4, 4,-1, 4, 4, 4,-1,         4, 4, 4, 4, 4, 4, 4,-1,
133                  4,-1, 4,-1, 4,-1, 4,-1,         4, 4, 4,-1, 4,-1, 4,-1,
134                  4, 4, 4,-1, 4, 4, 4,-1,         4, 4, 4, 4, 4, 4, 4,-1,
135                  4,-1, 4,-1, 4,-1, 4,-1,         4, 4, 4,-1, 4,-1, 4,-1,
136                  4, 4, 4,-1, 4, 4, 4,-1,         4, 4, 4, 4, 4, 4, 4,-1,
137                  4,-1, 4,-1, 4,-1, 4,-1,         4, 4, 4,-1, 4,-1, 4,-1,
138                  4, 4, 4,-1, 4, 4, 4,-1,         4, 4, 4, 4, 4, 4, 4,-1,
139                  4,-1, 4,-1, 4,-1, 4,-1,         4, 4, 4,-1, 4,-1, 4,-1,
140                  4, 4, 4,-1, 4, 4, 4,-1,         4, 4, 4, 4, 4, 4, 4,-1,
141                  4, 4, 4, 4, 4, 4, 4, 4,         3, 4, 4, 4, 3, 4, 4, 4,
142                  3, 4, 3, 4, 3, 4, 3, 4,         3, 3, 3, 4, 3, 3, 3, 4,
143                  3, 3, 3, 3, 3, 3, 3, 3,         2, 3, 3, 3, 2, 3, 3, 3,
144                  2, 3, 2, 3, 2, 3, 2, 3,         2, 2, 2, 3, 2, 2, 2, 3,
145                  2, 2, 2, 2, 2, 2, 2, 2,         1, 2, 2, 2, 1, 2, 2, 2,
146                  1, 2, 1, 2, 1, 2, 1, 2,         1, 1, 1, 2, 1, 1, 1, 2,
147                  0, 0, 0, 0, 0, 0, 0, 0,         0, 0 ,0, 0, 0, 0, 0, 0,
148                  0, 0, 0, 0, 0, 0, 0, 0,         0, 0 ,0, 0, 0, 0, 0, 0,
149         };
150
151         const int Operator::ssgenvtable[8][2][3][2] =
152         {
153                 1, 1,  1, 1,  1, 1,             // 08 
154                 0, 1,  1, 1,  1, 1,             // 08 56~
155                 0, 1,  2, 0,  2, 0,             // 09
156                 0, 1,  2, 0,  2, 0,             // 09
157                 1,-1,  0, 1,  1,-1,             // 10
158                 0, 1,  1,-1,  0, 1,             // 10 60~
159                 1,-1,  0, 0,  0, 0,             // 11
160                 0, 1,  0, 0,  0, 0,             // 11 60~
161                 2,-1,  2,-1,  2,-1,             // 12
162                 1,-1,  2,-1,  2,-1,             // 12 56~
163                 1,-1,  0, 0,  0, 0,             // 13
164                 1,-1,  0, 0,  0, 0,             // 13
165                 0, 1,  1,-1,  0, 1,             // 14
166                 1,-1,  0, 1,  1,-1,             // 14 60~
167                 0, 1,  2, 0,  2, 0,             // 15
168                 1,-1,  2, 0,  2, 0,             // 15 60~
169         };
170
171         // fixed equasion-based tables
172         int             pmtable[2][8][FM_LFOENTS];
173         uint    volatile  amtable[2][4][FM_LFOENTS];
174
175         static bool tablemade = false;
176
177         const char *get_libfmgen_version(void)
178         {
179 #if defined(__LIBFMGEN_VERSION)
180                 return (const char *)__LIBFMGEN_VERSION;
181 #else
182                 return (const char *)"\0";
183 #endif
184         }
185 }
186
187 namespace FM
188 {
189
190 // ---------------------------------------------------------------------------
191 //      \83e\81[\83u\83\8b\8dì\90¬
192 //
193 void MakeLFOTable()
194 {
195         if (tablemade)
196                 return;
197
198         tablemade = true;
199         
200         int i;
201
202         static const double pms[2][8] = 
203         { 
204                 { 0, 1/360., 2/360., 3/360.,  4/360.,  6/360., 12/360.,  24/360., },    // OPNA
205 //              { 0, 1/240., 2/240., 4/240., 10/240., 20/240., 80/240., 140/240., },    // OPM
206                 { 0, 1/480., 2/480., 4/480., 10/480., 20/480., 80/480., 140/480., },    // OPM
207 //              { 0, 1/960., 2/960., 4/960., 10/960., 20/960., 80/960., 140/960., },    // OPM
208         };
209         //               3               6,      12      30       60       240      420         / 720
210         //      1.000963
211         //      lfofref[level * max * wave];
212         //      pre = lfofref[level][pms * wave >> 8];
213         static const volatile uint8 amt[2][4] = 
214         {
215                 { 31, 6, 4, 3 }, // OPNA
216                 { 31, 2, 1, 0 }, //     OPM
217         };
218         
219         for (int type = 0; type < 2; type++)
220         {
221                 for (i=0; i<8; i++)
222                 {
223                         double pmb = pms[type][i];
224                         for (int j=0; j<FM_LFOENTS; j++)
225                         {
226 //                              double v = pow(2.0, pmb * (2 * j - FM_LFOENTS+1) / (FM_LFOENTS-1));
227                                 double w = 0.6 * pmb * sin(2 * j * 3.14159265358979323846 / FM_LFOENTS) + 1;
228 //                              pmtable[type][i][j] = int(0x10000 * (v - 1));
229 //                              if (type == 0)
230                                         pmtable[type][i][j] = int(0x10000 * (w - 1));
231 //                              else
232 //                                      pmtable[type][i][j] = int(0x10000 * (v - 1));
233
234 //                              printf("pmtable[%d][%d][%.2x] = %5d  %7.5f %7.5f\n", type, i, j, pmtable[type][i][j], v, w);
235                         }
236                 }
237                 for (i=0; i<4; i++)
238                 {
239                         for (int j=0; j<FM_LFOENTS; j++)
240                         {
241                                 amtable[type][i][j] = (((j * 4) >> amt[type][i]) * 2) << 2;
242                         }
243                 }
244         }
245 }
246
247
248 // ---------------------------------------------------------------------------
249 //      \83`\83b\83v\93à\82Å\8b¤\92Ê\82È\95\94\95ª
250 //
251 Chip::Chip()
252 : ratio_(0), aml_(0), pml_(0), pmv_(0), optype_(typeN)
253 {
254         static int __num = 0;
255         chip_num = __num++;
256 }
257
258 //      \83N\83\8d\83b\83N\81E\83T\83\93\83v\83\8a\83\93\83O\83\8c\81[\83g\94ä\82É\88Ë\91\82·\82é\83e\81[\83u\83\8b\82ð\8dì\90¬
259 void Chip::SetRatio(uint ratio)
260 {
261         if (ratio_ != ratio)
262         {
263                 ratio_ = ratio;
264                 MakeTable();
265         }
266 }
267
268 void Chip::MakeTable()
269 {
270         int h, l;
271         
272         // PG Part
273         static const float dt2lv[4] = { 1.f, 1.414f, 1.581f, 1.732f };
274         for (h=0; h<4; h++)
275         {
276                 assert(2 + FM_RATIOBITS - FM_PGBITS >= 0);
277                 double rr = dt2lv[h] * double(ratio_);
278                 for (l=0; l<16; l++)
279                 {
280                         int mul = l ? l * 2 : 1;
281                         multable_[h][l] = uint(mul * rr);
282                 }
283         }
284 }
285
286 // ---------------------------------------------------------------------------
287 //      \83X\83e\81[\83g\83Z\81[\83u
288 //
289 #define CHIP_STATE_VERSION      1
290
291 bool Chip::ProcessState(void *f, bool loading)
292 {
293         FILEIO *state_fio = (FILEIO *)f;
294         
295         if(!state_fio->StateCheckUint32(CHIP_STATE_VERSION)) {
296                 return false;
297         }
298         state_fio->StateValue(ratio_);
299         state_fio->StateValue(aml_);
300         state_fio->StateValue(pml_);
301         state_fio->StateValue(pmv_);
302         return true;
303 }
304
305
306 // ---------------------------------------------------------------------------
307 //      Operator
308 //
309 bool FM::Operator::tablehasmade = false;
310 uint FM::Operator::sinetable[1024];
311 int32 FM::Operator::cltable[FM_CLENTS];
312
313 //      \8d\\92z
314 FM::Operator::Operator()
315 : chip_(0)
316 {
317         if (!tablehasmade)
318                 MakeTable();
319
320         static int __num = 0;
321         operators_num = __num++;
322         // EG Part
323         ar_ = dr_ = sr_ = rr_ = key_scale_rate_ = 0;
324         ams_ = amtable[0][0];
325         mute_ = false;
326         keyon_ = false;
327         tl_out_ = false;
328         ssg_type_ = 0;
329
330         // PG Part
331         multiple_ = 0;
332         detune_ = 0;
333         detune2_ = 0;
334
335         // LFO
336         ms_ = 0;
337         
338 //      Reset();
339 }
340
341 //      \8f\89\8aú\89»
342 void FM::Operator::Reset()
343 {
344         // EG part
345         tl_ = tl_latch_ = 127;
346         ShiftPhase(off);
347         eg_count_ = 0;
348         eg_curve_count_ = 0;
349         ssg_phase_ = 0;
350
351         // PG part
352         pg_count_ = 0;
353
354         // OP part
355         out_ = out2_ = 0;
356
357         param_changed_ = true;
358         PARAMCHANGE(0);
359 }
360
361 void Operator::MakeTable()
362 {
363         // \91Î\90\94\83e\81[\83u\83\8b\82Ì\8dì\90¬
364         assert(FM_CLENTS >= 256);
365
366         int* p = cltable;
367         int i;
368         for (i=0; i<256; i++)
369         {
370                 int v = int(floor(pow(2., 13. - i / 256.)));
371                 v = (v + 2) & ~3;
372                 *p++ = v;
373                 *p++ = -v;
374         }
375         while (p < cltable + FM_CLENTS)
376         {
377                 *p = p[-512] / 2;
378                 p++;
379         }
380
381 //      for (i=0; i<13*256; i++)
382 //              printf("%4d, %d, %d\n", i, cltable[i*2], cltable[i*2+1]);
383
384         // \83T\83C\83\93\83e\81[\83u\83\8b\82Ì\8dì\90¬
385         double log2 = log(2.);
386         for (i=0; i<FM_OPSINENTS/2; i++)
387         {
388                 double r = (i * 2 + 1) * FM_PI / FM_OPSINENTS;
389                 double q = -256 * log(sin(r)) / log2;
390                 uint s = (int) (floor(q + 0.5)) + 1;
391 //              printf("%d, %d\n", s, cltable[s * 2] / 8);
392                 sinetable[i]                  = s * 2 ;
393                 sinetable[FM_OPSINENTS / 2 + i] = s * 2 + 1;
394         }
395
396         ::FM::MakeLFOTable();
397
398         tablehasmade = true;
399 }
400
401
402
403 inline void FM::Operator::SetDPBN(uint dp, uint bn)
404 {
405         dp_ = dp, bn_ = bn; param_changed_ = true; 
406         PARAMCHANGE(1);
407 }
408
409
410 //      \8f\80\94õ
411 void Operator::Prepare()
412 {
413         if (param_changed_)
414         {
415                 param_changed_ = false;
416                 //      PG Part
417                 pg_diff_ = (dp_ + dttable[detune_ + bn_]) * chip_->GetMulValue(detune2_, multiple_);
418                 pg_diff_ >>= (2 + FM_RATIOBITS - FM_PGBITS);
419                 pg_diff_lfo_ = pg_diff_ >> 11;
420
421                 // EG Part
422                 key_scale_rate_ = bn_ >> (3-ks_);
423                 tl_out_ = mute_ ? 0x3ff : tl_ * 8;
424                 
425                 switch (eg_phase_)
426                 {
427                 case attack:
428                         SetEGRate(ar_ ? Min(63, ar_ + key_scale_rate_) : 0);
429                         break;
430                 case decay:
431                         SetEGRate(dr_ ? Min(63, dr_ + key_scale_rate_) : 0);
432                         eg_level_on_next_phase_ = sl_ * 8;
433                         break;
434                 case sustain:
435                         SetEGRate(sr_ ? Min(63, sr_ + key_scale_rate_) : 0);
436                         break;
437                 case release:
438                         SetEGRate(Min(63, rr_ + key_scale_rate_));
439                         break;
440                 }
441
442                 // SSG-EG
443                 if (ssg_type_ && (eg_phase_ != release))
444                 {
445                         int m = ar_ >= (uint)((ssg_type_ == 8 || ssg_type_ == 12) ? 56 : 60);
446
447                         if (ssg_phase_ == -1)           // XXX quick fix
448                                 ssg_phase_ = 0;
449                         assert(0 <= ssg_phase_ && ssg_phase_ <= 2);
450                         const int* table = ssgenvtable[ssg_type_ & 7][m][ssg_phase_];
451
452                         ssg_offset_ = table[0] * 0x200;
453                         ssg_vector_ = table[1];
454                 }
455                 // LFO
456                 ams_ = amtable[type_][amon_ ? (ms_ >> 4) & 3 : 0];
457                 EGUpdate();
458
459                 dbgopout_ = 0;
460         }
461 }
462 //      envelop \82Ì eg_phase_ \95Ï\8dX
463 void Operator::ShiftPhase(EGPhase nextphase)
464 {
465         switch (nextphase)
466         {
467         case attack:            // Attack Phase
468                 tl_ = tl_latch_;
469                 if (ssg_type_)
470                 {
471                         ssg_phase_ = ssg_phase_ + 1;
472                         if (ssg_phase_ > 2)
473                                 ssg_phase_ = 1;
474                         
475                         int m = ar_ >= (uint)((ssg_type_ == 8 || ssg_type_ == 12) ? 56 : 60);
476
477                         assert(0 <= ssg_phase_ && ssg_phase_ <= 2);
478                         const int* table = ssgenvtable[ssg_type_ & 7][m][ssg_phase_];
479
480                         ssg_offset_ = table[0] * 0x200;
481                         ssg_vector_ = table[1];
482                 }
483                 if ((ar_ + key_scale_rate_) < 62)
484                 {
485                         SetEGRate(ar_ ? Min(63, ar_ + key_scale_rate_) : 0);
486                         eg_phase_ = attack;
487                         break;
488                 }
489         case decay:                     // Decay Phase
490                 if (sl_)
491                 {
492                         eg_level_ = 0;
493                         eg_level_on_next_phase_ = ssg_type_ ? Min(sl_ * 8, 0x200) : sl_ * 8;
494
495                         SetEGRate(dr_ ? Min(63, dr_ + key_scale_rate_) : 0);
496                         eg_phase_ = decay;
497                         break;
498                 }
499         case sustain:           // Sustain Phase
500                 eg_level_ = sl_ * 8;
501                 eg_level_on_next_phase_ = ssg_type_ ? 0x200 : 0x400;
502
503                 SetEGRate(sr_ ? Min(63, sr_ + key_scale_rate_) : 0);
504                 eg_phase_ = sustain;
505                 break;
506         
507         case release:           // Release Phase
508                 if (ssg_type_)
509                 {
510                         eg_level_ = eg_level_ * ssg_vector_ + ssg_offset_;
511                         ssg_vector_ = 1;
512                         ssg_offset_ = 0;
513                 }
514                 if (eg_phase_ == attack || (eg_level_ < FM_EG_BOTTOM)) //0x400/* && eg_phase_ != off*/))
515                 {
516                         eg_level_on_next_phase_ = 0x400;
517                         SetEGRate(Min(63, rr_ + key_scale_rate_));
518                         eg_phase_ = release;
519                         break;
520                 }
521         case off:                       // off
522         default:
523                 eg_level_ = FM_EG_BOTTOM;
524                 eg_level_on_next_phase_ = FM_EG_BOTTOM;
525                 EGUpdate();
526                 SetEGRate(0);
527                 eg_phase_ = off;
528                 break;
529         }
530 }
531
532 //      Block/F-Num
533 void Operator::SetFNum(uint f)
534 {
535         dp_ = (f & 2047) << ((f >> 11) & 7);
536         bn_ = notetable[(f >> 7) & 127];
537         param_changed_ = true;
538         PARAMCHANGE(2);
539 }
540
541 //      \82P\83T\83\93\83v\83\8b\8d\87\90¬
542
543 //      ISample \82ð envelop count (2\83Î) \82É\95Ï\8a·\82·\82é\83V\83t\83g\97Ê
544 #define IS2EC_SHIFT             ((20 + FM_PGBITS) - 13)
545
546
547 // \93ü\97Í: s = 20+FM_PGBITS = 29
548 #define Sine(s) sinetable[((s) >> (20+FM_PGBITS-FM_OPSINBITS))&(FM_OPSINENTS-1)]
549 #define SINE(s) sinetable[(s) & (FM_OPSINENTS-1)]
550
551 inline FM::ISample Operator::LogToLin(uint a)
552 {
553 #if 1 // FM_CLENTS < 0xc00              // 400 for TL, 400 for ENV, 400 for LFO.
554         return (a < FM_CLENTS) ? cltable[a] : 0;
555 #else
556         return cltable[a];
557 #endif
558 }
559
560 inline void Operator::EGUpdate()
561 {
562         if (!ssg_type_)
563         {
564                 eg_out_ = Min(tl_out_ + eg_level_, 0x3ff) << (1 + 2);
565         }
566         else
567         {
568                 eg_out_ = Min(tl_out_ + eg_level_ * ssg_vector_ + ssg_offset_, 0x3ff) << (1 + 2);
569         }
570 }
571
572 inline void Operator::SetEGRate(uint rate)
573 {
574         eg_rate_ = rate;
575         eg_count_diff_ = decaytable2[rate / 4] * chip_->GetRatio();
576 }
577
578 //      EG \8cv\8eZ
579 void FM::Operator::EGCalc()
580 {
581         eg_count_ = (2047 * 3) << FM_RATIOBITS;                         // ##\82±\82Ì\8eè\94²\82«\82Í\8dÄ\8c»\90«\82ð\92á\89º\82³\82¹\82é
582         
583         if (eg_phase_ == attack)
584         {
585                 int c = attacktable[eg_rate_][eg_curve_count_ & 7];
586                 if (c >= 0)
587                 {
588                         eg_level_ -= 1 + (eg_level_ >> c);
589                         if (eg_level_ <= 0)
590                                 ShiftPhase(decay);
591                 }
592                 EGUpdate();
593         }
594         else
595         {
596                 if (!ssg_type_)
597                 {
598                         eg_level_ += decaytable1[eg_rate_][eg_curve_count_ & 7];
599                         if (eg_level_ >= eg_level_on_next_phase_)
600                                 ShiftPhase(EGPhase(eg_phase_+1));
601                         EGUpdate();
602                 }
603                 else
604                 {
605                         eg_level_ += 4 * decaytable1[eg_rate_][eg_curve_count_ & 7];
606                         if (eg_level_ >= eg_level_on_next_phase_)
607                         {
608                                 switch (eg_phase_)
609                                 {
610                                 case decay:
611                                         ShiftPhase(sustain);
612                                         break;
613                                 case sustain:
614                                         ShiftPhase(attack);
615                                         break;
616                                 case release:
617                                         ShiftPhase(off);
618                                         break;
619                                 }
620                         }
621                         EGUpdate();
622                 }
623         }
624         eg_curve_count_++;
625 }
626
627 inline void FM::Operator::EGStep()
628 {
629         eg_count_ -= eg_count_diff_;
630
631         // EG \82Ì\95Ï\89»\82Í\91S\83X\83\8d\83b\83g\82Å\93¯\8aú\82µ\82Ä\82¢\82é\82Æ\82¢\82¤\89\\82à\82 \82é
632         if (eg_count_ <= 0)
633                 EGCalc();
634 }
635
636 //      PG \8cv\8eZ
637 //      ret:2^(20+PGBITS) / cycle
638 inline uint32 FM::Operator::PGCalc()
639 {
640         uint32 ret = pg_count_;
641         pg_count_ += pg_diff_;
642         dbgpgout_ = ret;
643         return ret;
644 }
645
646 inline uint32 FM::Operator::PGCalcL()
647 {
648         uint32 ret = pg_count_;
649         pg_count_ += pg_diff_ + ((pg_diff_lfo_ * chip_->GetPMV()) >> 5);// & -(1 << (2+IS2EC_SHIFT)));
650         dbgpgout_ = ret;
651         return ret /* + pmv * pg_diff_;*/;
652 }
653
654 //      OP \8cv\8eZ
655 //      in: ISample (\8dÅ\91å 8\83Î)
656 inline FM::ISample FM::Operator::Calc(ISample in)
657 {
658         EGStep();
659         out2_ = out_;
660
661         int pgin = PGCalc() >> (20+FM_PGBITS-FM_OPSINBITS);
662         pgin += in >> (20+FM_PGBITS-FM_OPSINBITS-(2+IS2EC_SHIFT));
663         out_ = LogToLin(eg_out_ + SINE(pgin));
664
665         dbgopout_ = out_;
666         return out_;
667 }
668
669 inline FM::ISample FM::Operator::CalcL(ISample in)
670 {
671         EGStep();
672
673         int pgin = PGCalcL() >> (20+FM_PGBITS-FM_OPSINBITS);
674         pgin += in >> (20+FM_PGBITS-FM_OPSINBITS-(2+IS2EC_SHIFT));
675         out_ = LogToLin(eg_out_ + SINE(pgin) + ams_[chip_->GetAML()]);
676
677         dbgopout_ = out_;
678         return out_;
679 }
680
681 inline FM::ISample FM::Operator::CalcN(uint noise)
682 {
683         EGStep();
684         
685         int lv = Max(0, 0x3ff - (tl_out_ + eg_level_)) << 1;
686         
687         // noise & 1 ? lv : -lv \82Æ\93\99\89¿ 
688         noise = (noise & 1) - 1;
689         out_ = (lv + noise) ^ noise;
690
691         dbgopout_ = out_;
692         return out_;
693 }
694
695 //      OP (FB) \8cv\8eZ
696 //      Self Feedback \82Ì\95Ï\92²\8dÅ\91å = 4\83Î
697 inline FM::ISample FM::Operator::CalcFB(uint fb)
698 {
699         EGStep();
700
701         ISample in = out_ + out2_;
702         out2_ = out_;
703
704         int pgin = PGCalc() >> (20+FM_PGBITS-FM_OPSINBITS);
705         if (fb < 31)
706         {
707                 pgin += ((in << (1 + IS2EC_SHIFT)) >> fb) >> (20+FM_PGBITS-FM_OPSINBITS);
708         }
709         out_ = LogToLin(eg_out_ + SINE(pgin));
710         dbgopout_ = out2_;
711
712         return out2_;
713 }
714
715 inline FM::ISample FM::Operator::CalcFBL(uint fb)
716 {
717         EGStep();
718         
719         ISample in = out_ + out2_;
720         out2_ = out_;
721
722         int pgin = PGCalcL() >> (20+FM_PGBITS-FM_OPSINBITS);
723         if (fb < 31)
724         {
725                 pgin += ((in << (1 + IS2EC_SHIFT)) >> fb) >> (20+FM_PGBITS-FM_OPSINBITS);
726         }
727
728         out_ = LogToLin(eg_out_ + SINE(pgin) + ams_[chip_->GetAML()]);
729         dbgopout_ = out_;
730
731         return out_;
732 }
733
734 // ---------------------------------------------------------------------------
735 //      \83X\83e\81[\83g\83Z\81[\83u
736 //
737 #define OPERATOR_STATE_VERSION  1
738
739 bool Operator::ProcessState(void *f, bool loading)
740 {
741         FILEIO *state_fio = (FILEIO *)f;
742         
743         if(!state_fio->StateCheckUint32(OPERATOR_STATE_VERSION)) {
744                 return false;
745         }
746         state_fio->StateValue(out_);
747         state_fio->StateValue(out2_);
748         state_fio->StateValue(in2_);
749         state_fio->StateValue(dp_);
750         state_fio->StateValue(detune_);
751         state_fio->StateValue(detune2_);
752         state_fio->StateValue(multiple_);
753         state_fio->StateValue(pg_count_);
754         state_fio->StateValue(pg_diff_);
755         state_fio->StateValue(pg_diff_lfo_);
756         state_fio->StateValue(bn_);
757         state_fio->StateValue(eg_level_);
758         state_fio->StateValue(eg_level_on_next_phase_);
759         state_fio->StateValue(eg_count_);
760         state_fio->StateValue(eg_count_diff_);
761         state_fio->StateValue(eg_out_);
762         state_fio->StateValue(tl_out_);
763         state_fio->StateValue(eg_rate_);
764         state_fio->StateValue(eg_curve_count_);
765         state_fio->StateValue(ssg_offset_);
766         state_fio->StateValue(ssg_vector_);
767         state_fio->StateValue(ssg_phase_);
768         state_fio->StateValue(key_scale_rate_);
769         if(loading) {
770                 eg_phase_ = static_cast<EGPhase>(state_fio->FgetInt32_LE());
771                 ams_ = &amtable[0][0][0] + state_fio->FgetInt32_LE();
772         } else {
773                 state_fio->FputInt32_LE(static_cast<int>(eg_phase_));
774                 state_fio->FputInt32_LE((int)(ams_ - &amtable[0][0][0]));
775         }
776         state_fio->StateValue(ms_);
777         state_fio->StateValue(tl_);
778         state_fio->StateValue(tl_latch_);
779         state_fio->StateValue(ar_);
780         state_fio->StateValue(dr_);
781         state_fio->StateValue(sr_);
782         state_fio->StateValue(sl_);
783         state_fio->StateValue(rr_);
784         state_fio->StateValue(ks_);
785         state_fio->StateValue(ssg_type_);
786         state_fio->StateValue(keyon_);
787         state_fio->StateValue(amon_);
788         state_fio->StateValue(param_changed_);
789         state_fio->StateValue(mute_);
790         state_fio->StateValue(dbgopout_);
791         state_fio->StateValue(dbgpgout_);
792         return true;
793 }
794
795 #undef Sine
796
797 // ---------------------------------------------------------------------------
798 //      4-op Channel
799 //
800 const uint8 Channel4::fbtable[8] = { 31, 7, 6, 5, 4, 3, 2, 1 };
801 int Channel4::kftable[64];
802
803 bool Channel4::tablehasmade = false;
804
805
806 Channel4::Channel4()
807 {
808         if (!tablehasmade)
809                 MakeTable();
810
811         static int __num = 0;
812         channel4s_num = __num++;
813         
814         SetAlgorithm(0);
815         pms = pmtable[0][0];
816 }
817
818 void Channel4::MakeTable()
819 {
820         // 100/64 cent =  2^(i*100/64*1200)
821         for (int i=0; i<64; i++)
822         {
823                 kftable[i] = int(0x10000 * pow(2., i / 768.) );
824         }
825 }
826
827 // \83\8a\83Z\83b\83g
828 void Channel4::Reset()
829 {
830         op[0].Reset();
831         op[1].Reset();
832         op[2].Reset();
833         op[3].Reset();
834 }
835
836 //      Calc \82Ì\97p\88Ó
837 int Channel4::Prepare()
838 {
839         op[0].Prepare();
840         op[1].Prepare();
841         op[2].Prepare();
842         op[3].Prepare();
843         
844         pms = pmtable[op[0].type_][op[0].ms_ & 7];
845         int key = (op[0].IsOn() | op[1].IsOn() | op[2].IsOn() | op[3].IsOn()) ? 1 : 0;
846         int lfo = op[0].ms_ & (op[0].amon_ | op[1].amon_ | op[2].amon_ | op[3].amon_ ? 0x37 : 7) ? 2 : 0;
847         return key | lfo;
848 }
849
850 //      F-Number/BLOCK \82ð\90Ý\92è
851 void Channel4::SetFNum(uint f)
852 {
853         for (int i=0; i<4; i++)
854                 op[i].SetFNum(f);
855 }
856
857 //      KC/KF \82ð\90Ý\92è
858 void Channel4::SetKCKF(uint kc, uint kf)
859 {
860         const static uint kctable[16] = 
861         { 
862                 5197, 5506, 5833, 6180, 6180, 6547, 6937, 7349, 
863                 7349, 7786, 8249, 8740, 8740, 9259, 9810, 10394, 
864         };
865
866         int oct = 19 - ((kc >> 4) & 7);
867
868 //printf("%p", this);
869         uint kcv = kctable[kc & 0x0f];
870         kcv = (kcv + 2) / 4 * 4;
871 //printf(" %.4x", kcv);
872         uint dp = kcv * kftable[kf & 0x3f];
873 //printf(" %.4x %.4x %.8x", kcv, kftable[kf & 0x3f], dp >> oct);
874         dp >>= 16 + 3;
875         dp <<= 16 + 3;
876         dp >>= oct;     
877         uint bn = (kc >> 2) & 31;
878         op[0].SetDPBN(dp, bn);
879         op[1].SetDPBN(dp, bn);
880         op[2].SetDPBN(dp, bn);
881         op[3].SetDPBN(dp, bn);
882 //printf(" %.8x\n", dp);
883 }
884
885 //      \83L\81[\90§\8cä
886 void Channel4::KeyControl(uint key)
887 {
888         if (key & 0x1) op[0].KeyOn(); else op[0].KeyOff();
889         if (key & 0x2) op[1].KeyOn(); else op[1].KeyOff();
890         if (key & 0x4) op[2].KeyOn(); else op[2].KeyOff();
891         if (key & 0x8) op[3].KeyOn(); else op[3].KeyOff();
892 }
893
894 //      \83A\83\8b\83S\83\8a\83Y\83\80\82ð\90Ý\92è
895 void Channel4::SetAlgorithm(uint algo)
896 {
897         static const uint8 table1[8][6] = 
898         {
899                 { 0, 1, 1, 2, 2, 3 },   { 1, 0, 0, 1, 1, 2 },
900                 { 1, 1, 1, 0, 0, 2 },   { 0, 1, 2, 1, 1, 2 },
901                 { 0, 1, 2, 2, 2, 1 },   { 0, 1, 0, 1, 0, 1 },
902                 { 0, 1, 2, 1, 2, 1 },   { 1, 0, 1, 0, 1, 0 },
903         };
904
905         in [0] = &buf[table1[algo][0]];
906         out[0] = &buf[table1[algo][1]];
907         in [1] = &buf[table1[algo][2]];
908         out[1] = &buf[table1[algo][3]];
909         in [2] = &buf[table1[algo][4]];
910         out[2] = &buf[table1[algo][5]];
911
912         op[0].ResetFB();
913         algo_ = algo;
914 }
915
916 //  \8d\87\90¬
917 ISample Channel4::Calc()
918 {
919         int r = 0;
920         switch (algo_)
921         {
922         case 0:
923                 op[2].Calc(op[1].Out());
924                 op[1].Calc(op[0].Out());
925                 r = op[3].Calc(op[2].Out());
926                 op[0].CalcFB(fb);
927                 break;
928         case 1:
929                 op[2].Calc(op[0].Out() + op[1].Out());
930                 op[1].Calc(0);
931                 r = op[3].Calc(op[2].Out());
932                 op[0].CalcFB(fb);
933                 break;
934         case 2:
935                 op[2].Calc(op[1].Out());
936                 op[1].Calc(0);
937                 r = op[3].Calc(op[0].Out() + op[2].Out());
938                 op[0].CalcFB(fb);
939                 break;
940         case 3:
941                 op[2].Calc(0);
942                 op[1].Calc(op[0].Out());
943                 r = op[3].Calc(op[1].Out() + op[2].Out());
944                 op[0].CalcFB(fb);
945                 break;
946         case 4:
947                 op[2].Calc(0);
948                 r = op[1].Calc(op[0].Out());
949                 r += op[3].Calc(op[2].Out());
950                 op[0].CalcFB(fb);
951                 break;
952         case 5:
953                 r =  op[2].Calc(op[0].Out());
954                 r += op[1].Calc(op[0].Out());
955                 r += op[3].Calc(op[0].Out());
956                 op[0].CalcFB(fb);
957                 break;
958         case 6:
959                 r  = op[2].Calc(0);
960                 r += op[1].Calc(op[0].Out());
961                 r += op[3].Calc(0);
962                 op[0].CalcFB(fb);
963                 break;
964         case 7:
965                 r  = op[2].Calc(0);
966                 r += op[1].Calc(0);
967                 r += op[3].Calc(0);
968                 r += op[0].CalcFB(fb);
969                 break;
970         }
971         return r;
972 }
973
974 //  \8d\87\90¬
975 ISample Channel4::CalcL()
976 {
977         chip_->SetPMV(pms[chip_->GetPML()]);
978
979         int r = 0;
980         switch (algo_)
981         {
982         case 0:
983                 op[2].CalcL(op[1].Out());
984                 op[1].CalcL(op[0].Out());
985                 r = op[3].CalcL(op[2].Out());
986                 op[0].CalcFBL(fb);
987                 break;
988         case 1:
989                 op[2].CalcL(op[0].Out() + op[1].Out());
990                 op[1].CalcL(0);
991                 r = op[3].CalcL(op[2].Out());
992                 op[0].CalcFBL(fb);
993                 break;
994         case 2:
995                 op[2].CalcL(op[1].Out());
996                 op[1].CalcL(0);
997                 r = op[3].CalcL(op[0].Out() + op[2].Out());
998                 op[0].CalcFBL(fb);
999                 break;
1000         case 3:
1001                 op[2].CalcL(0);
1002                 op[1].CalcL(op[0].Out());
1003                 r = op[3].CalcL(op[1].Out() + op[2].Out());
1004                 op[0].CalcFBL(fb);
1005                 break;
1006         case 4:
1007                 op[2].CalcL(0);
1008                 r = op[1].CalcL(op[0].Out());
1009                 r += op[3].CalcL(op[2].Out());
1010                 op[0].CalcFBL(fb);
1011                 break;
1012         case 5:
1013                 r =  op[2].CalcL(op[0].Out());
1014                 r += op[1].CalcL(op[0].Out());
1015                 r += op[3].CalcL(op[0].Out());
1016                 op[0].CalcFBL(fb);
1017                 break;
1018         case 6:
1019                 r  = op[2].CalcL(0);
1020                 r += op[1].CalcL(op[0].Out());
1021                 r += op[3].CalcL(0);
1022                 op[0].CalcFBL(fb);
1023                 break;
1024         case 7:
1025                 r  = op[2].CalcL(0);
1026                 r += op[1].CalcL(0);
1027                 r += op[3].CalcL(0);
1028                 r += op[0].CalcFBL(fb);
1029                 break;
1030         }
1031         return r;
1032 }
1033
1034 //  \8d\87\90¬
1035 ISample Channel4::CalcN(uint noise)
1036 {
1037         buf[1] = buf[2] = buf[3] = 0;
1038
1039         buf[0] = op[0].out_; op[0].CalcFB(fb);
1040         *out[0] += op[1].Calc(*in[0]);
1041         *out[1] += op[2].Calc(*in[1]);
1042         int o = op[3].out_;
1043         op[3].CalcN(noise);
1044         return *out[2] + o;
1045 }
1046
1047 //  \8d\87\90¬
1048 ISample Channel4::CalcLN(uint noise)
1049 {
1050         chip_->SetPMV(pms[chip_->GetPML()]);
1051         buf[1] = buf[2] = buf[3] = 0;
1052
1053         buf[0] = op[0].out_; op[0].CalcFBL(fb); 
1054         *out[0] += op[1].CalcL(*in[0]);
1055         *out[1] += op[2].CalcL(*in[1]);
1056         int o = op[3].out_;
1057         op[3].CalcN(noise);
1058         return *out[2] + o;
1059 }
1060
1061 // ---------------------------------------------------------------------------
1062 //      \83X\83e\81[\83g\83Z\81[\83u
1063 //
1064 #define CHANNEL4_STATE_VERSION  1
1065
1066 bool Channel4::ProcessState(void *f, bool loading)
1067 {
1068         FILEIO *state_fio = (FILEIO *)f;
1069         
1070         if(!state_fio->StateCheckUint32(CHANNEL4_STATE_VERSION)) {
1071                 return false;
1072         }
1073         state_fio->StateValue(fb);
1074         state_fio->StateArray(buf, sizeof(buf), 1);
1075         if(loading) {
1076                 for(int i = 0; i < 3; i++) {
1077                         in [i] = &buf[0] + state_fio->FgetInt32_LE();
1078                         out[i] = &buf[0] + state_fio->FgetInt32_LE();
1079                 }
1080                 pms = &pmtable[0][0][0] + state_fio->FgetInt32_LE();
1081         } else {
1082                 for(int i = 0; i < 3; i++) {
1083                         state_fio->FputInt32_LE((int)(in [i] - &buf[0]));
1084                         state_fio->FputInt32_LE((int)(out[i] - &buf[0]));
1085                 }
1086                 state_fio->FputInt32_LE((int)(pms - &pmtable[0][0][0]));
1087         }
1088         state_fio->StateValue(algo_);
1089         for(int i = 0; i < 4; i++) {
1090                 if(!op[i].ProcessState(f, loading)) {
1091                         return false;
1092                 }
1093         }
1094         return true;
1095 }
1096
1097 }       // namespace FM