OSDN Git Service

bbfa9f84c24e55edf7bfc640f66214cab62059dc
[csp-qt/common_source_project-fm7.git] / source / src / vm / fmgen / fmgen.h
1 // ---------------------------------------------------------------------------
2 //      FM Sound Generator
3 //      Copyright (C) cisc 1998, 2001.
4 // ---------------------------------------------------------------------------
5 //      $Id: fmgen.h,v 1.37 2003/08/25 13:33:11 cisc Exp $
6
7 #ifndef FM_GEN_H
8 #define FM_GEN_H
9
10 #include "types.h"
11 #include "common.h"
12
13 // ---------------------------------------------------------------------------
14 //      \8fo\97Í\83T\83\93\83v\83\8b\82Ì\8c^
15 //
16 #define FM_SAMPLETYPE   int32                           // int16 or int32
17
18 // ---------------------------------------------------------------------------
19 //      \92è\90\94\82»\82Ì\82P
20 //      \90Ã\93I\83e\81[\83u\83\8b\82Ì\83T\83C\83Y
21
22 #define FM_LFOBITS              8                                       // \95Ï\8dX\95s\89Â
23 #define FM_TLBITS               7
24
25 // ---------------------------------------------------------------------------
26
27 #define FM_TLENTS               (1 << FM_TLBITS)
28 #define FM_LFOENTS              (1 << FM_LFOBITS)
29 #define FM_TLPOS                (FM_TLENTS/4)
30
31 //      \83T\83C\83\93\94g\82Ì\90¸\93x\82Í 2^(1/256)
32 #define FM_CLENTS               (0x1000 * 2)    // sin + TL + LFO
33
34 // ---------------------------------------------------------------------------
35
36 class csp_state_utils;
37 class CSP_Logger;
38 namespace FM
39 {       
40         //      Types ----------------------------------------------------------------
41         typedef FM_SAMPLETYPE   Sample;
42         typedef int32                   ISample;
43
44         enum OpType { typeN=0, typeM=1 };
45
46         void StoreSample(ISample& dest, int data);
47
48         class Chip;
49         //      Operator -------------------------------------------------------------
50         class Operator
51         {
52         protected:
53                 csp_state_utils *state_entry;
54                 class CSP_Logger *p_logger;
55                 int tmp_eg_phase;
56                 int tmp_ams;
57                 int operators_num;
58         public:
59                 Operator();
60                 void    SetChip(Chip* chip) { chip_ = chip; }
61
62                 static void     MakeTimeTable(uint ratio);
63                 
64                 ISample Calc(ISample in);
65                 ISample CalcL(ISample in);
66                 ISample CalcFB(uint fb);
67                 ISample CalcFBL(uint fb);
68                 ISample CalcN(uint noise);
69                 void    Prepare();
70                 void    KeyOn();
71                 void    KeyOff();
72                 void    Reset();
73                 void    ResetFB();
74                 int             IsOn();
75
76                 void    SetDT(uint dt);
77                 void    SetDT2(uint dt2);
78                 void    SetMULTI(uint multi);
79                 void    SetTL(uint tl, bool csm);
80                 void    SetKS(uint ks);
81                 void    SetAR(uint ar);
82                 void    SetDR(uint dr);
83                 void    SetSR(uint sr);
84                 void    SetRR(uint rr);
85                 void    SetSL(uint sl);
86                 void    SetSSGEC(uint ssgec);
87                 void    SetFNum(uint fnum);
88                 void    SetDPBN(uint dp, uint bn);
89                 void    SetMode(bool modulator);
90                 void    SetAMON(bool on);
91                 void    SetMS(uint ms);
92                 void    Mute(bool);
93                 
94 //              static void SetAML(uint l);
95 //              static void SetPML(uint l);
96
97                 int             Out() { return out_; }
98
99                 int             dbgGetIn2() { return in2_; } 
100                 void    dbgStopPG() { pg_diff_ = 0; pg_diff_lfo_ = 0; }
101                 
102         protected:
103                 void DeclState(void *f);
104                 void SaveState(void *f);
105                 bool LoadState(void *f);
106                 
107         private:
108                 typedef uint32 Counter;
109                 
110                 Chip*   chip_;
111                 ISample out_, out2_;
112                 ISample in2_;
113
114         //      Phase Generator ------------------------------------------------------
115                 uint32  PGCalc();
116                 uint32  PGCalcL();
117
118                 uint    dp_;            // \83¢P
119                 uint    detune_;                // Detune
120                 uint    detune2_;       // DT2
121                 uint    multiple_;      // Multiple
122                 uint32  pg_count_;      // Phase \8c»\8dÝ\92l
123                 uint32  pg_diff_;       // Phase \8d·\95ª\92l
124                 int32   pg_diff_lfo_;   // Phase \8d·\95ª\92l >> x
125
126         //      Envelop Generator ---------------------------------------------------
127                 enum    EGPhase { next, attack, decay, sustain, release, off };
128                 
129                 void    EGCalc();
130                 void    EGStep();
131                 void    ShiftPhase(EGPhase nextphase);
132                 void    SSGShiftPhase(int mode);
133                 void    SetEGRate(uint);
134                 void    EGUpdate();
135                 int             FBCalc(int fb);
136                 ISample LogToLin(uint a);
137
138                 
139                 OpType  type_;          // OP \82Ì\8eí\97Þ (M, N...)
140                 uint    bn_;            // Block/Note
141                 int             eg_level_;      // EG \82Ì\8fo\97Í\92l
142                 int             eg_level_on_next_phase_;        // \8e\9f\82Ì eg_phase_ \82É\88Ú\82é\92l
143                 int             eg_count_;              // EG \82Ì\8e\9f\82Ì\95Ï\88Ú\82Ü\82Å\82Ì\8e\9e\8aÔ
144                 int             eg_count_diff_; // eg_count_ \82Ì\8d·\95ª
145                 int             eg_out_;                // EG+TL \82ð\8d\87\82í\82¹\82½\8fo\97Í\92l
146                 int             tl_out_;                // TL \95ª\82Ì\8fo\97Í\92l
147 //              int             pm_depth_;              // PM depth
148 //              int             am_depth_;              // AM depth
149                 int             eg_rate_;
150                 int             eg_curve_count_;
151                 int             ssg_offset_;
152                 int             ssg_vector_;
153                 int             ssg_phase_;
154
155
156                 uint    key_scale_rate_;                // key scale rate
157                 EGPhase eg_phase_;
158                 volatile uint*  ams_;
159                 uint    ms_;
160                 
161                 uint    tl_;                    // Total Level   (0-127)
162                 uint    tl_latch_;              // Total Level Latch (for CSM mode)
163                 uint    ar_;                    // Attack Rate   (0-63)
164                 uint    dr_;                    // Decay Rate    (0-63)
165                 uint    sr_;                    // Sustain Rate  (0-63)
166                 uint    sl_;                    // Sustain Level (0-127)
167                 uint    rr_;                    // Release Rate  (0-63)
168                 uint    ks_;                    // Keyscale      (0-3)
169                 uint    ssg_type_;      // SSG-Type Envelop Control
170
171                 bool    keyon_;
172                 bool    amon_;          // enable Amplitude Modulation
173                 bool    param_changed_; // \83p\83\89\83\81\81[\83^\82ª\8dX\90V\82³\82ê\82½
174                 bool    mute_;
175                 
176         //      Tables ---------------------------------------------------------------
177                 static Counter rate_table[16];
178                 static uint32 multable[4][16];
179
180                 static const uint8 notetable[128];
181                 static const int8 dttable[256];
182                 static const int8 decaytable1[64][8];
183                 static const int decaytable2[16];
184                 static const int8 attacktable[64][8];
185                 static const int ssgenvtable[8][2][3][2];
186
187                 static uint     sinetable[1024];
188                 static int32 cltable[FM_CLENTS];
189
190                 static bool tablehasmade;
191                 static void MakeTable();
192
193
194
195         //      friends --------------------------------------------------------------
196                 friend class Channel4;
197 //              friend void __stdcall FM_NextPhase(Operator* op);
198
199         public:
200                 int             dbgopout_;
201                 int             dbgpgout_;
202                 static const int32* dbgGetClTable() { return cltable; }
203                 static const uint* dbgGetSineTable() { return sinetable; }
204         };
205         
206         //      4-op Channel ---------------------------------------------------------
207         class Channel4
208         {
209         protected:
210                 int tmp_in_bufptr[3];
211                 int tmp_out_bufptr[3];
212                 int tmp_pms;
213                 int channel4s_num;
214                 csp_state_utils *state_entry;
215                 class CSP_Logger *p_logger;
216         public:
217                 Channel4();
218                 void SetChip(Chip* chip);
219                 void SetType(OpType type);
220                 
221                 ISample Calc();
222                 ISample CalcL();
223                 ISample CalcN(uint noise);
224                 ISample CalcLN(uint noise);
225                 void SetFNum(uint fnum);
226                 void SetFB(uint fb);
227                 void SetKCKF(uint kc, uint kf);
228                 void SetAlgorithm(uint algo);
229                 int Prepare();
230                 void KeyControl(uint key);
231                 void Reset();
232                 void SetMS(uint ms);
233                 void Mute(bool);
234                 void Refresh();
235
236                 void dbgStopPG() { for (int i=0; i<4; i++) op[i].dbgStopPG(); }
237                 
238                 void DeclState(void *f);
239                 void SaveState(void *f);
240                 bool LoadState(void *f);
241                 
242         private:
243                 static const uint8 fbtable[8];
244                 uint    fb;
245                 int             buf[4];
246                 int*    in[3];                  // \8ae OP \82Ì\93ü\97Í\83|\83C\83\93\83^
247                 int*    out[3];                 // \8ae OP \82Ì\8fo\97Í\83|\83C\83\93\83^
248                 int*    pms;
249                 int             algo_;
250                 Chip*   chip_;
251
252                 static void MakeTable();
253
254                 static bool tablehasmade;
255                 static int      kftable[64];
256
257
258         public:
259                 Operator op[4];
260         };
261
262         //      Chip resource
263         class Chip
264         {
265         protected:
266                 class CSP_Logger *p_logger;
267                 csp_state_utils *state_entry;
268                 int chip_num;
269         public:
270                 Chip();
271                 void    SetRatio(uint ratio);
272                 void    SetAML(uint l);
273                 void    SetPML(uint l);
274                 void    SetPMV(int pmv) { pmv_ = pmv; }
275
276                 uint32  GetMulValue(uint dt2, uint mul) { return multable_[dt2][mul]; }
277                 uint    GetAML() { return aml_; }
278                 uint    GetPML() { return pml_; }
279                 int             GetPMV() { return pmv_; }
280                 uint    GetRatio() { return ratio_; }
281
282                 void DeclState(void *f);
283                 void SaveState(void *f);
284                 bool LoadState(void *f);
285                 
286         private:
287                 void    MakeTable();
288
289                 uint    ratio_;
290                 uint    aml_;
291                 uint    pml_;
292                 int             pmv_;
293                 OpType  optype_;
294                 uint32  multable_[4][16];
295         };
296 }
297
298 #endif // FM_GEN_H