OSDN Git Service

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