-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
fm-channel.c
268 lines (200 loc) · 9.73 KB
/
fm-channel.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "fm-channel.h"
#include <assert.h>
#include "clowncommon/clowncommon.h"
void FM_Channel_Constant_Initialise(FM_Channel_Constant* const constant)
{
FM_Operator_Constant_Initialise(&constant->operators);
}
void FM_Channel_State_Initialise(FM_Channel_State* const state)
{
cc_u16f i;
for (i = 0; i < CC_COUNT_OF(state->operators); ++i)
FM_Operator_State_Initialise(&state->operators[i]);
state->feedback_divisor = 1 << (9 - 0);
state->algorithm = 0;
for (i = 0; i < CC_COUNT_OF(state->operator_1_previous_samples); ++i)
state->operator_1_previous_samples[i] = 0;
}
void FM_Channel_Parameters_Initialise(FM_Channel* const channel, const FM_Channel_Constant* const constant, FM_Channel_State* const state)
{
cc_u16f i;
channel->constant = constant;
channel->state = state;
for (i = 0; i < CC_COUNT_OF(channel->operators); ++i)
{
channel->operators[i].constant = &constant->operators;
channel->operators[i].state = &state->operators[i];
}
}
void FM_Channel_SetFrequency(const FM_Channel* const channel, const cc_u16f f_number_and_block)
{
cc_u16f i;
for (i = 0; i < CC_COUNT_OF(channel->state->operators); ++i)
FM_Operator_SetFrequency(channel->operators[i].state, f_number_and_block);
}
void FM_Channel_SetFrequencies(const FM_Channel* const channel, const cc_u16l* const f_number_and_block)
{
cc_u16f i;
for (i = 0; i < CC_COUNT_OF(channel->state->operators); ++i)
FM_Operator_SetFrequency(channel->operators[i].state, f_number_and_block[i]);
}
void FM_Channel_SetFeedbackAndAlgorithm(const FM_Channel* const channel, const cc_u16f feedback, const cc_u16f algorithm)
{
channel->state->feedback_divisor = 1 << (9 - feedback);
channel->state->algorithm = algorithm;
}
void FM_Channel_SetSSGEG(const FM_Channel* const channel, const cc_u8f ssgeg)
{
cc_u16f i;
for (i = 0; i < CC_COUNT_OF(channel->state->operators); ++i)
FM_Operator_SetSSGEG(channel->operators[i].state, ssgeg);
}
void FM_Channel_SetKeyOn(const FM_Channel* const channel, const cc_u16f operator_index, const cc_bool key_on)
{
FM_Operator_SetKeyOn(channel->operators[operator_index].state, key_on);
}
void FM_Channel_SetDetuneAndMultiplier(const FM_Channel* const channel, const cc_u16f operator_index, const cc_u16f detune, const cc_u16f multiplier)
{
FM_Operator_SetDetuneAndMultiplier(channel->operators[operator_index].state, detune, multiplier);
}
void FM_Channel_SetTotalLevel(const FM_Channel* const channel, const cc_u16f operator_index, const cc_u16f total_level)
{
FM_Operator_SetTotalLevel(channel->operators[operator_index].state, total_level);
}
void FM_Channel_SetKeyScaleAndAttackRate(const FM_Channel* const channel, const cc_u16f operator_index, const cc_u16f key_scale, const cc_u16f attack_rate)
{
FM_Operator_SetKeyScaleAndAttackRate(channel->operators[operator_index].state, key_scale, attack_rate);
}
void FM_Channel_SetDecayRate(const FM_Channel* const channel, const cc_u16f operator_index, const cc_u16f decay_rate)
{
FM_Operator_SetDecayRate(channel->operators[operator_index].state, decay_rate);
}
void FM_Channel_SetSustainRate(const FM_Channel* const channel, const cc_u16f operator_index, const cc_u16f sustain_rate)
{
FM_Operator_SetSustainRate(channel->operators[operator_index].state, sustain_rate);
}
void FM_Channel_SetSustainLevelAndReleaseRate(const FM_Channel* const channel, const cc_u16f operator_index, const cc_u16f sustain_level, const cc_u16f release_rate)
{
FM_Operator_SetSustainLevelAndReleaseRate(channel->operators[operator_index].state, sustain_level, release_rate);
}
/* Portable equivalent to bit-shifting. */
/* TODO: Instead, maybe convert all signed integers to unsigned so that we can implement
two's-compliment manually which would allow us to be able to perform simple bit-shifting instead. */
static cc_s16f FM_Channel_DiscardLowerBits(const cc_s16f total_bits_to_discard, const cc_s16f value)
{
const cc_s16f divisor = 1 << total_bits_to_discard;
return (value - ((divisor - 1) * (value < 0))) / divisor;
}
static cc_s16f FM_Channel_14BitTo9Bit(const cc_s16f value)
{
return FM_Channel_DiscardLowerBits(14 - 9, value);
}
static cc_s16f FM_Channel_MixSamples(const cc_s16f a, const cc_s16f b)
{
return CC_CLAMP(-0x100, 0xFF, a + b);
}
cc_s16f FM_Channel_GetSample(const FM_Channel* const channel)
{
const FM_Operator* const operator1 = &channel->operators[0];
const FM_Operator* const operator2 = &channel->operators[2]; /* Yes, these really are swapped. */
const FM_Operator* const operator3 = &channel->operators[1];
const FM_Operator* const operator4 = &channel->operators[3];
cc_s16f feedback_modulation;
cc_s16f operator_1_sample;
cc_s16f operator_2_sample;
cc_s16f operator_3_sample;
cc_s16f operator_4_sample;
cc_s16f sample;
/* Compute operator 1's self-feedback modulation. */
if (channel->state->feedback_divisor == 1 << (9 - 0))
feedback_modulation = 0;
else
feedback_modulation = (channel->state->operator_1_previous_samples[0] + channel->state->operator_1_previous_samples[1]) / channel->state->feedback_divisor;
/* Feed the operators into each other to produce the final sample. */
/* Note that the operators output a 14-bit sample, meaning that, if all four are summed, then the result is a 16-bit sample,
so there is no possibility of overflow. */
/* http://gendev.spritesmind.net/forum/viewtopic.php?p=5958#p5958 */
switch (channel->state->algorithm)
{
default:
/* Should not happen. */
assert(0);
/* Fallthrough */
case 0:
/* "Four serial connection mode". */
operator_1_sample = FM_Operator_Process(operator1, feedback_modulation);
operator_2_sample = FM_Operator_Process(operator2, operator_1_sample);
operator_3_sample = FM_Operator_Process(operator3, operator_2_sample);
operator_4_sample = FM_Operator_Process(operator4, operator_3_sample);
sample = FM_Channel_14BitTo9Bit(operator_4_sample);
break;
case 1:
/* "Three double modulation serial connection mode". */
operator_1_sample = FM_Operator_Process(operator1, feedback_modulation);
operator_2_sample = FM_Operator_Process(operator2, 0);
operator_3_sample = FM_Operator_Process(operator3, operator_1_sample + operator_2_sample);
operator_4_sample = FM_Operator_Process(operator4, operator_3_sample);
sample = FM_Channel_14BitTo9Bit(operator_4_sample);
break;
case 2:
/* "Double modulation mode (1)". */
operator_1_sample = FM_Operator_Process(operator1, feedback_modulation);
operator_2_sample = FM_Operator_Process(operator2, 0);
operator_3_sample = FM_Operator_Process(operator3, operator_2_sample);
operator_4_sample = FM_Operator_Process(operator4, operator_1_sample + operator_3_sample);
sample = FM_Channel_14BitTo9Bit(operator_4_sample);
break;
case 3:
/* "Double modulation mode (2)". */
operator_1_sample = FM_Operator_Process(operator1, feedback_modulation);
operator_2_sample = FM_Operator_Process(operator2, operator_1_sample);
operator_3_sample = FM_Operator_Process(operator3, 0);
operator_4_sample = FM_Operator_Process(operator4, operator_2_sample + operator_3_sample);
sample = FM_Channel_14BitTo9Bit(operator_4_sample);
break;
case 4:
/* "Two serial connection and two parallel modes". */
operator_1_sample = FM_Operator_Process(operator1, feedback_modulation);
operator_2_sample = FM_Operator_Process(operator2, operator_1_sample);
operator_3_sample = FM_Operator_Process(operator3, 0);
operator_4_sample = FM_Operator_Process(operator4, operator_3_sample);
sample = FM_Channel_14BitTo9Bit(operator_2_sample);
sample = FM_Channel_MixSamples(sample, FM_Channel_14BitTo9Bit(operator_4_sample));
break;
case 5:
/* "Common modulation 3 parallel mode". */
operator_1_sample = FM_Operator_Process(operator1, feedback_modulation);
operator_2_sample = FM_Operator_Process(operator2, operator_1_sample);
operator_3_sample = FM_Operator_Process(operator3, operator_1_sample);
operator_4_sample = FM_Operator_Process(operator4, operator_1_sample);
sample = FM_Channel_14BitTo9Bit(operator_2_sample);
sample = FM_Channel_MixSamples(sample, FM_Channel_14BitTo9Bit(operator_3_sample));
sample = FM_Channel_MixSamples(sample, FM_Channel_14BitTo9Bit(operator_4_sample));
break;
case 6:
/* "Two serial connection + two sine mode". */
operator_1_sample = FM_Operator_Process(operator1, feedback_modulation);
operator_2_sample = FM_Operator_Process(operator2, operator_1_sample);
operator_3_sample = FM_Operator_Process(operator3, 0);
operator_4_sample = FM_Operator_Process(operator4, 0);
sample = FM_Channel_14BitTo9Bit(operator_2_sample);
sample = FM_Channel_MixSamples(sample, FM_Channel_14BitTo9Bit(operator_3_sample));
sample = FM_Channel_MixSamples(sample, FM_Channel_14BitTo9Bit(operator_4_sample));
break;
case 7:
/* "Four parallel sine synthesis mode". */
operator_1_sample = FM_Operator_Process(operator1, feedback_modulation);
operator_2_sample = FM_Operator_Process(operator2, 0);
operator_3_sample = FM_Operator_Process(operator3, 0);
operator_4_sample = FM_Operator_Process(operator4, 0);
sample = FM_Channel_14BitTo9Bit(operator_1_sample);
sample = FM_Channel_MixSamples(sample, FM_Channel_14BitTo9Bit(operator_2_sample));
sample = FM_Channel_MixSamples(sample, FM_Channel_14BitTo9Bit(operator_3_sample));
sample = FM_Channel_MixSamples(sample, FM_Channel_14BitTo9Bit(operator_4_sample));
break;
}
/* Update the feedback values. */
channel->state->operator_1_previous_samples[1] = channel->state->operator_1_previous_samples[0];
channel->state->operator_1_previous_samples[0] = operator_1_sample;
return sample;
}