1 // ---------------------------------------------------------------------------
2 //	FM Sound Generator
3 //	Copyright (C) cisc 1998, 2003.
4 // ---------------------------------------------------------------------------
5 //	$fmgen-Id: fmgeninl.h,v 1.26 2003/06/12 13:14:36 cisc Exp $
6 
7 #ifndef FM_GEN_INL_H
8 #define FM_GEN_INL_H
9 
10 // ---------------------------------------------------------------------------
11 //	������Σ�
12 //
13 #define FM_PI			3.14159265358979323846
14 
15 #define FM_SINEPRESIS	2			// EG�ȥ������Ȥ����٤κ�  0(��)-2(��)
16 
17 
18 #define FM_OPSINBITS	10
19 #define FM_OPSINENTS	(1 << FM_OPSINBITS)
20 
21 #define FM_EGCBITS		18			// eg �� count �Υ��ե���
22 #define FM_LFOCBITS		14
23 
24 #ifdef FM_TUNEBUILD
25  #define FM_PGBITS		2
26  #define FM_RATIOBITS	0
27 #else
28  #define FM_PGBITS		9
29  #define FM_RATIOBITS	7			// 8-12 ���餤�ޤǡ�
30 #endif
31 
32 #define FM_EGBITS		16
33 
34 //extern int paramcount[];
35 //#define PARAMCHANGE(i) paramcount[i]++;
36 #define PARAMCHANGE(i)
37 
38 namespace FM
39 {
40 
41 // ---------------------------------------------------------------------------
42 //	Operator
43 //
44 //	�ե����ɥХå��Хåե����ꥢ
ResetFB()45 inline void Operator::ResetFB()
46 {
47 	out_ = out2_ = 0;
48 }
49 
50 //	��������
KeyOn()51 inline void Operator::KeyOn()
52 {
53 	if (!keyon_)
54 	{
55 		keyon_ = true;
56 		if (eg_phase_ == off || eg_phase_ == release)
57 		{
58 			ssg_phase_ = -1;
59 			ShiftPhase(attack);
60 			EGUpdate();
61 			in2_ = out_ = out2_ = 0;
62 			pg_count_ = 0;
63 		}
64 	}
65 }
66 
67 //	��������
KeyOff()68 inline void	Operator::KeyOff()
69 {
70 	if (keyon_)
71 	{
72 		keyon_ = false;
73 		ShiftPhase(release);
74 	}
75 }
76 
77 //	���ڥ졼���ϲ�Ư�椫��
IsOn()78 inline int Operator::IsOn()
79 {
80 	return eg_phase_ - off;
81 }
82 
83 //	Detune (0-7)
SetDT(uint dt)84 inline void Operator::SetDT(uint dt)
85 {
86 	detune_ = dt * 0x20, param_changed_ = true;
87 	PARAMCHANGE(4);
88 }
89 
90 //	DT2 (0-3)
SetDT2(uint dt2)91 inline void Operator::SetDT2(uint dt2)
92 {
93 	detune2_ = dt2 & 3, param_changed_ = true;
94 	PARAMCHANGE(5);
95 }
96 
97 //	Multiple (0-15)
SetMULTI(uint mul)98 inline void Operator::SetMULTI(uint mul)
99 {
100 	multiple_ = mul, param_changed_ = true;
101 	PARAMCHANGE(6);
102 }
103 
104 //	Total Level (0-127) (0.75dB step)
SetTL(uint tl,bool csm)105 inline void Operator::SetTL(uint tl, bool csm)
106 {
107 	if (!csm)
108 	{
109 		tl_ = tl, param_changed_ = true;
110 		PARAMCHANGE(7);
111 	}
112 	tl_latch_ = tl;
113 }
114 
115 //	Attack Rate (0-63)
SetAR(uint ar)116 inline void Operator::SetAR(uint ar)
117 {
118 	ar_ = ar;
119 	param_changed_ = true;
120 	PARAMCHANGE(8);
121 }
122 
123 //	Decay Rate (0-63)
SetDR(uint dr)124 inline void Operator::SetDR(uint dr)
125 {
126 	dr_ = dr;
127 	param_changed_ = true;
128 	PARAMCHANGE(9);
129 }
130 
131 //	Sustain Rate (0-63)
SetSR(uint sr)132 inline void Operator::SetSR(uint sr)
133 {
134 	sr_ = sr;
135 	param_changed_ = true;
136 	PARAMCHANGE(10);
137 }
138 
139 //	Sustain Level (0-127)
SetSL(uint sl)140 inline void Operator::SetSL(uint sl)
141 {
142 	sl_ = sl;
143 	param_changed_ = true;
144 	PARAMCHANGE(11);
145 }
146 
147 //	Release Rate (0-63)
SetRR(uint rr)148 inline void Operator::SetRR(uint rr)
149 {
150 	rr_ = rr;
151 	param_changed_ = true;
152 	PARAMCHANGE(12);
153 }
154 
155 //	Keyscale (0-3)
SetKS(uint ks)156 inline void Operator::SetKS(uint ks)
157 {
158 	ks_ = ks;
159 	param_changed_ = true;
160 	PARAMCHANGE(13);
161 }
162 
163 //	SSG-type Envelop (0-15)
SetSSGEC(uint ssgec)164 inline void Operator::SetSSGEC(uint ssgec)
165 {
166 	if (ssgec & 8)
167 		ssg_type_ = ssgec;
168 	else
169 		ssg_type_ = 0;
170 }
171 
SetAMON(bool amon)172 inline void Operator::SetAMON(bool amon)
173 {
174 	amon_ = amon;
175 	param_changed_ = true;
176 	PARAMCHANGE(14);
177 }
178 
Mute(bool mute)179 inline void Operator::Mute(bool mute)
180 {
181 	mute_ = mute;
182 	param_changed_ = true;
183 	PARAMCHANGE(15);
184 }
185 
SetMS(uint ms)186 inline void Operator::SetMS(uint ms)
187 {
188 	ms_ = ms;
189 	param_changed_ = true;
190 	PARAMCHANGE(16);
191 }
192 
193 // ---------------------------------------------------------------------------
194 //	4-op Channel
195 
196 //	���ڥ졼���μ��� (LFO) ������
SetType(OpType type)197 inline void Channel4::SetType(OpType type)
198 {
199 	for (int i=0; i<4; i++)
200 		op[i].type_ = type;
201 }
202 
203 //	����ա��ե����ɥХå��졼�Ȥ����� (0-7)
SetFB(uint feedback)204 inline void Channel4::SetFB(uint feedback)
205 {
206 	fb = fbtable[feedback];
207 }
208 
209 //	OPNA �� LFO ������
SetMS(uint ms)210 inline void Channel4::SetMS(uint ms)
211 {
212 	op[0].SetMS(ms);
213 	op[1].SetMS(ms);
214 	op[2].SetMS(ms);
215 	op[3].SetMS(ms);
216 }
217 
218 //	�����ͥ롦�ޥ���
Mute(bool m)219 inline void Channel4::Mute(bool m)
220 {
221 	for (int i=0; i<4; i++)
222 		op[i].Mute(m);
223 }
224 
225 //	�����ѥ�᡼����Ʒ׻�
Refresh()226 inline void Channel4::Refresh()
227 {
228 	for (int i=0; i<4; i++)
229 		op[i].param_changed_ = true;
230 	PARAMCHANGE(3);
231 }
232 
SetChip(Chip * chip)233 inline void Channel4::SetChip(Chip* chip)
234 {
235 	chip_ = chip;
236 	for (int i=0; i<4; i++)
237 		op[i].SetChip(chip);
238 }
239 
240 // ---------------------------------------------------------------------------
241 //
242 //
StoreSample(Sample & dest,ISample data)243 inline void StoreSample(Sample& dest, ISample data)
244 {
245 	if (sizeof(Sample) == 2)
246 		dest = (Sample) Limit(dest + data, 0x7fff, -0x8000);
247 	else
248 		dest += data;
249 }
250 
251 
252 // ---------------------------------------------------------------------------
253 //	AM �Υ�٥������
SetAML(uint l)254 inline void Chip::SetAML(uint l)
255 {
256 	aml_ = l & (FM_LFOENTS - 1);
257 }
258 
259 //	PM �Υ�٥������
SetPML(uint l)260 inline void Chip::SetPML(uint l)
261 {
262 	pml_ = l & (FM_LFOENTS - 1);
263 }
264 
265 
266 }
267 
268 #endif // FM_GEN_INL_H
269