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 
12 // ---------------------------------------------------------------------------
13 //	���ϥ���ץ�η�
14 //
15 //#define FM_SAMPLETYPE	int32				// int16 or int32
16 // forQUASI88
17 #define FM_SAMPLETYPE	int16
18 
19 // ---------------------------------------------------------------------------
20 //	������Σ�
21 //	��Ū�ơ��֥�Υ�����
22 
23 #define FM_LFOBITS		8					// �ѹ��Բ�
24 #define FM_TLBITS		7
25 
26 // ---------------------------------------------------------------------------
27 
28 #define FM_TLENTS		(1 << FM_TLBITS)
29 #define FM_LFOENTS		(1 << FM_LFOBITS)
30 #define FM_TLPOS		(FM_TLENTS/4)
31 
32 //	�������Ȥ����٤� 2^(1/256)
33 #define FM_CLENTS		(0x1000 * 2)	// sin + TL + LFO
34 
35 // ---------------------------------------------------------------------------
36 
37 namespace FM
38 {
39 	//	Types ----------------------------------------------------------------
40 	typedef FM_SAMPLETYPE	Sample;
41 	typedef int32 			ISample;
42 
43 	enum OpType { typeN=0, typeM=1 };
44 
45 	void StoreSample(ISample& dest, int data);
46 
47 	class Chip;
48 
49 	//	Operator -------------------------------------------------------------
50 	class Operator
51 	{
52 	public:
53 		Operator();
SetChip(Chip * chip)54 		void	SetChip(Chip* chip) { chip_ = chip; }
55 
56 		static void	MakeTimeTable(uint ratio);
57 
58 		ISample	Calc(ISample in);
59 		ISample	CalcL(ISample in);
60 		ISample CalcFB(uint fb);
61 		ISample CalcFBL(uint fb);
62 		ISample CalcN(uint noise);
63 		void	Prepare();
64 		void	KeyOn();
65 		void	KeyOff();
66 		void	Reset();
67 		void	ResetFB();
68 		int		IsOn();
69 
70 		void	SetDT(uint dt);
71 		void	SetDT2(uint dt2);
72 		void	SetMULTI(uint multi);
73 		void	SetTL(uint tl, bool csm);
74 		void	SetKS(uint ks);
75 		void	SetAR(uint ar);
76 		void	SetDR(uint dr);
77 		void	SetSR(uint sr);
78 		void	SetRR(uint rr);
79 		void	SetSL(uint sl);
80 		void	SetSSGEC(uint ssgec);
81 		void	SetFNum(uint fnum);
82 		void	SetDPBN(uint dp, uint bn);
83 		void	SetMode(bool modulator);
84 		void	SetAMON(bool on);
85 		void	SetMS(uint ms);
86 		void	Mute(bool);
87 
88 //		static void SetAML(uint l);
89 //		static void SetPML(uint l);
90 
Out()91 		int		Out() { return out_; }
92 
dbgGetIn2()93 		int		dbgGetIn2() { return in2_; }
dbgStopPG()94 		void	dbgStopPG() { pg_diff_ = 0; pg_diff_lfo_ = 0; }
95 
96 	private:
97 		typedef uint32 Counter;
98 
99 		Chip*	chip_;
100 		ISample	out_, out2_;
101 		ISample in2_;
102 
103 	//	Phase Generator ------------------------------------------------------
104 		uint32	PGCalc();
105 		uint32	PGCalcL();
106 
107 		uint	dp_;		// ��P
108 		uint	detune_;		// Detune
109 		uint	detune2_;	// DT2
110 		uint	multiple_;	// Multiple
111 		uint32	pg_count_;	// Phase ������
112 		uint32	pg_diff_;	// Phase ��ʬ��
113 		int32	pg_diff_lfo_;	// Phase ��ʬ�� >> x
114 
115 	//	Envelop Generator ---------------------------------------------------
116 		enum	EGPhase { next, attack, decay, sustain, release, off };
117 
118 		void	EGCalc();
119 		void	EGStep();
120 		void	ShiftPhase(EGPhase nextphase);
121 		void	SSGShiftPhase(int mode);
122 		void	SetEGRate(uint);
123 		void	EGUpdate();
124 		int		FBCalc(int fb);
125 		ISample LogToLin(uint a);
126 
127 
128 		OpType	type_;		// OP �μ��� (M, N...)
129 		uint	bn_;		// Block/Note
130 		int		eg_level_;	// EG �ν�����
131 		int		eg_level_on_next_phase_;	// ���� eg_phase_ �˰ܤ���
132 		int		eg_count_;		// EG �μ����ѰܤޤǤλ���
133 		int		eg_count_diff_;	// eg_count_ �κ�ʬ
134 		int		eg_out_;		// EG+TL ���碌��������
135 		int		tl_out_;		// TL ʬ�ν�����
136 //		int		pm_depth_;		// PM depth
137 //		int		am_depth_;		// AM depth
138 		int		eg_rate_;
139 		int		eg_curve_count_;
140 		int		ssg_offset_;
141 		int		ssg_vector_;
142 		int		ssg_phase_;
143 
144 
145 		uint	key_scale_rate_;		// key scale rate
146 		EGPhase	eg_phase_;
147 		uint*	ams_;
148 		uint	ms_;
149 
150 		uint	tl_;			// Total Level	 (0-127)
151 		uint	tl_latch_;		// Total Level Latch (for CSM mode)
152 		uint	ar_;			// Attack Rate   (0-63)
153 		uint	dr_;			// Decay Rate    (0-63)
154 		uint	sr_;			// Sustain Rate  (0-63)
155 		uint	sl_;			// Sustain Level (0-127)
156 		uint	rr_;			// Release Rate  (0-63)
157 		uint	ks_;			// Keyscale      (0-3)
158 		uint	ssg_type_;	// SSG-Type Envelop Control
159 
160 		bool	keyon_;
161 		bool	amon_;		// enable Amplitude Modulation
162 		bool	param_changed_;	// �ѥ�᡼�����������줿
163 		bool	mute_;
164 
165 	//	Tables ---------------------------------------------------------------
166 		static Counter rate_table[16];
167 		static uint32 multable[4][16];
168 
169 		static const uint8 notetable[128];
170 		static const int8 dttable[256];
171 		static const int8 decaytable1[64][8];
172 		static const int decaytable2[16];
173 		static const int8 attacktable[64][8];
174 		static const int ssgenvtable[8][2][3][2];
175 
176 		static uint	sinetable[1024];
177 		static int32 cltable[FM_CLENTS];
178 
179 		static bool tablehasmade;
180 		static void MakeTable();
181 
182 
183 
184 	//	friends --------------------------------------------------------------
185 		friend class Channel4;
186 		friend void __stdcall FM_NextPhase(Operator* op);
187 
188 	public:
189 		int		dbgopout_;
190 		int		dbgpgout_;
dbgGetClTable()191 		static const int32* dbgGetClTable() { return cltable; }
dbgGetSineTable()192 		static const uint* dbgGetSineTable() { return sinetable; }
193 	};
194 
195 	//	4-op Channel ---------------------------------------------------------
196 	class Channel4
197 	{
198 	public:
199 		Channel4();
200 		void SetChip(Chip* chip);
201 		void SetType(OpType type);
202 
203 		ISample Calc();
204 		ISample CalcL();
205 		ISample CalcN(uint noise);
206 		ISample CalcLN(uint noise);
207 		void SetFNum(uint fnum);
208 		void SetFB(uint fb);
209 		void SetKCKF(uint kc, uint kf);
210 		void SetAlgorithm(uint algo);
211 		int Prepare();
212 		void KeyControl(uint key);
213 		void Reset();
214 		void SetMS(uint ms);
215 		void Mute(bool);
216 		void Refresh();
217 
dbgStopPG()218 		void dbgStopPG() { for (int i=0; i<4; i++) op[i].dbgStopPG(); }
219 
220 	private:
221 		static const uint8 fbtable[8];
222 		uint	fb;
223 		int		buf[4];
224 		int*	in[3];			// �� OP �����ϥݥ���
225 		int*	out[3];			// �� OP �ν��ϥݥ���
226 		int*	pms;
227 		int		algo_;
228 		Chip*	chip_;
229 
230 		static void MakeTable();
231 
232 		static bool tablehasmade;
233 		static int 	kftable[64];
234 
235 
236 	public:
237 		Operator op[4];
238 	};
239 
240 	//	Chip resource
241 	class Chip
242 	{
243 	public:
244 		Chip();
245 		void	SetRatio(uint ratio);
246 		void	SetAML(uint l);
247 		void	SetPML(uint l);
SetPMV(int pmv)248 		void	SetPMV(int pmv) { pmv_ = pmv; }
249 
GetMulValue(uint dt2,uint mul)250 		uint32	GetMulValue(uint dt2, uint mul) { return multable_[dt2][mul]; }
GetAML()251 		uint	GetAML() { return aml_; }
GetPML()252 		uint	GetPML() { return pml_; }
GetPMV()253 		int		GetPMV() { return pmv_; }
GetRatio()254 		uint	GetRatio() { return ratio_; }
255 
256 	private:
257 		void	MakeTable();
258 
259 		uint	ratio_;
260 		uint	aml_;
261 		uint	pml_;
262 		int		pmv_;
263 		OpType	optype_;
264 		uint32	multable_[4][16];
265 	};
266 }
267 
268 #endif // FM_GEN_H
269