OSDN Git Service

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