-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
synth.c
402 lines (347 loc) · 11.6 KB
/
synth.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "synth.h"
#include "bits.h"
#include <memory.h>
#include <stdbool.h>
#include <ym2612.h>
static Global global = { .lfoEnable = 1, .lfoFrequency = 0 };
static FmChannel fmChannels[MAX_FM_CHANS];
static u8 noteOn;
static u8 volumes[MAX_FM_CHANS];
static ParameterUpdatedCallback* parameterUpdatedCallback = NULL;
static const u8 MAX_VOLUME = 0x7F;
static const u8 VOLUME_TO_TOTAL_LEVELS[] = { 127, 122, 117, 113, 108, 104, 100,
97, 93, 89, 86, 83, 80, 77, 74, 71, 68, 66, 63, 61, 58, 56, 54, 52, 50, 48,
46, 44, 43, 41, 40, 38, 37, 35, 34, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23,
22, 21, 20, 19, 19, 18, 17, 17, 16, 15, 15, 14, 13, 13, 12, 12, 11, 11, 11,
10, 10, 9, 9, 9, 8, 8, 7, 7, 7, 7, 6, 6, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 3,
3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
static void updateChannel(u8 chan);
static void updateGlobalLfo(void);
static void updateAlgorithmAndFeedback(u8 channel);
static void updateOperatorMultipleAndDetune(u8 channel, u8 op);
static void updateOperatorRateScalingAndAttackRate(u8 channel, u8 operator);
static void updateOperatorAmplitudeModulationAndFirstDecayRate(
u8 channel, u8 operator);
static void updateOperatorReleaseRateAndSecondaryAmplitude(
u8 channel, u8 operator);
static void updateOperatorTotalLevel(u8 channel, u8 operator);
static void updateOperatorSecondaryDecayRate(u8 channel, u8 operator);
static void updateOperatorSsgEg(u8 channel, u8 operator);
static void updateStereoAmsFms(u8 channel);
static void writeChannelReg(u8 channel, u8 baseReg, u8 data);
static void writeOperatorReg(u8 channel, u8 op, u8 baseReg, u8 data);
static void updateOctaveAndFrequency(u8 channel);
static u8 keyOnOffRegOffset(u8 channel);
static FmChannel* fmChannel(u8 channel);
static Operator* getOperator(u8 channel, u8 operator);
static u8 effectiveTotalLevel(u8 channel, u8 operator, u8 totalLevel);
static bool isOutputOperator(u8 algorithm, u8 operator);
static u8 volumeAdjustedTotalLevel(u8 channel, u8 totalLevel);
static void channelParameterUpdated(u8 channel);
static void otherParameterUpdated(
u8 channel, ParameterUpdated parameterUpdated);
void synth_init(const FmChannel* initialPreset)
{
YM2612_writeReg(0, 0x27, 0); // Ch 3 Normal
for (u8 chan = 0; chan < MAX_FM_CHANS; chan++) {
volumes[chan] = MAX_VOLUME;
synth_noteOff(chan);
memcpy(&fmChannels[chan], initialPreset, sizeof(FmChannel));
updateChannel(chan);
}
updateGlobalLfo();
}
static void updateChannel(u8 chan)
{
updateAlgorithmAndFeedback(chan);
updateStereoAmsFms(chan);
for (u8 op = 0; op < MAX_FM_OPERATORS; op++) {
updateOperatorMultipleAndDetune(chan, op);
updateOperatorRateScalingAndAttackRate(chan, op);
updateOperatorAmplitudeModulationAndFirstDecayRate(chan, op);
updateOperatorSecondaryDecayRate(chan, op);
updateOperatorReleaseRateAndSecondaryAmplitude(chan, op);
updateOperatorTotalLevel(chan, op);
updateOperatorSsgEg(chan, op);
}
}
void synth_noteOn(u8 channel)
{
YM2612_writeReg(0, 0x28, 0xF0 + keyOnOffRegOffset(channel));
SET_BIT(noteOn, channel);
}
void synth_noteOff(u8 channel)
{
YM2612_writeReg(0, 0x28, keyOnOffRegOffset(channel));
CLEAR_BIT(noteOn, channel);
}
void synth_pitch(u8 channel, u8 octave, u16 freqNumber)
{
FmChannel* chan = fmChannel(channel);
chan->octave = octave;
chan->freqNumber = freqNumber;
updateOctaveAndFrequency(channel);
}
void synth_volume(u8 channel, u8 volume)
{
if (volumes[channel] == volume) {
return;
}
volumes[channel] = volume;
for (u8 op = 0; op < MAX_FM_OPERATORS; op++) {
updateOperatorTotalLevel(channel, op);
}
}
void synth_stereo(u8 channel, u8 stereo)
{
fmChannel(channel)->stereo = stereo;
updateStereoAmsFms(channel);
channelParameterUpdated(channel);
}
void synth_algorithm(u8 channel, u8 algorithm)
{
fmChannel(channel)->algorithm = algorithm;
updateAlgorithmAndFeedback(channel);
channelParameterUpdated(channel);
}
void synth_feedback(u8 channel, u8 feedback)
{
fmChannel(channel)->feedback = feedback;
updateAlgorithmAndFeedback(channel);
channelParameterUpdated(channel);
}
void synth_operatorTotalLevel(u8 channel, u8 op, u8 totalLevel)
{
Operator* oper = getOperator(channel, op);
if (oper->totalLevel == totalLevel) {
return;
}
oper->totalLevel = totalLevel;
updateOperatorTotalLevel(channel, op);
channelParameterUpdated(channel);
}
void synth_operatorMultiple(u8 channel, u8 op, u8 multiple)
{
getOperator(channel, op)->multiple = multiple;
updateOperatorMultipleAndDetune(channel, op);
channelParameterUpdated(channel);
}
void synth_operatorDetune(u8 channel, u8 op, u8 detune)
{
getOperator(channel, op)->detune = detune;
updateOperatorMultipleAndDetune(channel, op);
channelParameterUpdated(channel);
}
void synth_operatorRateScaling(u8 channel, u8 op, u8 rateScaling)
{
getOperator(channel, op)->rateScaling = rateScaling;
updateOperatorRateScalingAndAttackRate(channel, op);
channelParameterUpdated(channel);
}
void synth_operatorAttackRate(u8 channel, u8 op, u8 attackRate)
{
getOperator(channel, op)->attackRate = attackRate;
updateOperatorRateScalingAndAttackRate(channel, op);
channelParameterUpdated(channel);
}
void synth_operatorSecondDecayRate(u8 channel, u8 op, u8 secondDecayRate)
{
getOperator(channel, op)->secondaryDecayRate = secondDecayRate;
updateOperatorSecondaryDecayRate(channel, op);
channelParameterUpdated(channel);
}
void synth_operatorReleaseRate(u8 channel, u8 op, u8 releaseRate)
{
getOperator(channel, op)->releaseRate = releaseRate;
updateOperatorReleaseRateAndSecondaryAmplitude(channel, op);
channelParameterUpdated(channel);
}
void synth_operatorSsgEg(u8 channel, u8 op, u8 ssgEg)
{
getOperator(channel, op)->ssgEg = ssgEg;
updateOperatorSsgEg(channel, op);
channelParameterUpdated(channel);
}
void synth_operatorSecondaryAmplitude(u8 channel, u8 op, u8 secondaryAmplitude)
{
getOperator(channel, op)->secondaryAmplitude = secondaryAmplitude;
updateOperatorReleaseRateAndSecondaryAmplitude(channel, op);
channelParameterUpdated(channel);
}
void synth_operatorFirstDecayRate(u8 channel, u8 op, u8 firstDecayRate)
{
getOperator(channel, op)->firstDecayRate = firstDecayRate;
updateOperatorAmplitudeModulationAndFirstDecayRate(channel, op);
channelParameterUpdated(channel);
}
void synth_operatorAmplitudeModulation(
u8 channel, u8 op, u8 amplitudeModulation)
{
getOperator(channel, op)->amplitudeModulation = amplitudeModulation;
updateOperatorAmplitudeModulationAndFirstDecayRate(channel, op);
channelParameterUpdated(channel);
}
void synth_enableLfo(u8 enable)
{
global.lfoEnable = enable;
updateGlobalLfo();
otherParameterUpdated(0, Lfo);
}
void synth_globalLfoFrequency(u8 freq)
{
global.lfoFrequency = freq;
updateGlobalLfo();
otherParameterUpdated(0, Lfo);
}
void synth_ams(u8 channel, u8 ams)
{
fmChannel(channel)->ams = ams;
updateStereoAmsFms(channel);
channelParameterUpdated(channel);
}
void synth_fms(u8 channel, u8 fms)
{
fmChannel(channel)->fms = fms;
updateStereoAmsFms(channel);
channelParameterUpdated(channel);
}
u8 synth_busy(void)
{
return noteOn;
}
void synth_preset(u8 channel, const FmChannel* preset)
{
memcpy(&fmChannels[channel], preset, sizeof(FmChannel));
updateChannel(channel);
channelParameterUpdated(channel);
}
static void otherParameterUpdated(u8 channel, ParameterUpdated parameterUpdated)
{
if (parameterUpdatedCallback) {
parameterUpdatedCallback(channel, parameterUpdated);
}
}
static void channelParameterUpdated(u8 channel)
{
if (parameterUpdatedCallback) {
parameterUpdatedCallback(channel, Channel);
}
}
static void writeChannelReg(u8 channel, u8 baseReg, u8 data)
{
YM2612_writeReg(channel > 2 ? 1 : 0, baseReg + (channel % 3), data);
}
static void writeOperatorReg(u8 channel, u8 op, u8 baseReg, u8 data)
{
writeChannelReg(channel, baseReg + (op * 4), data);
}
static u8 keyOnOffRegOffset(u8 channel)
{
return (channel < 3) ? channel : (channel + 1);
}
static FmChannel* fmChannel(u8 channel)
{
return &fmChannels[channel];
}
static Operator* getOperator(u8 channel, u8 operator)
{
return &fmChannel(channel)->operators[operator];
}
static void updateGlobalLfo(void)
{
YM2612_writeReg(0, 0x22, (global.lfoEnable << 3) | global.lfoFrequency);
}
static void updateOctaveAndFrequency(u8 channel)
{
FmChannel* chan = fmChannel(channel);
writeChannelReg(
channel, 0xA4, (chan->freqNumber >> 8) | (chan->octave << 3));
writeChannelReg(channel, 0xA0, chan->freqNumber);
}
static void updateAlgorithmAndFeedback(u8 channel)
{
FmChannel* chan = fmChannel(channel);
writeChannelReg(channel, 0xB0, (chan->feedback << 3) + chan->algorithm);
}
static void updateStereoAmsFms(u8 channel)
{
FmChannel* chan = fmChannel(channel);
writeChannelReg(
channel, 0xB4, (chan->stereo << 6) + (chan->ams << 4) + chan->fms);
}
static void updateOperatorMultipleAndDetune(u8 channel, u8 operator)
{
Operator* op = getOperator(channel, operator);
writeOperatorReg(channel, operator, 0x30, op->multiple +(op->detune << 4));
}
static void updateOperatorRateScalingAndAttackRate(u8 channel, u8 operator)
{
Operator* op = getOperator(channel, operator);
writeOperatorReg(
channel, operator, 0x50, op->attackRate +(op->rateScaling << 6));
}
static void updateOperatorAmplitudeModulationAndFirstDecayRate(
u8 channel, u8 operator)
{
Operator* op = getOperator(channel, operator);
writeOperatorReg(channel, operator, 0x60,
op->firstDecayRate +(op->amplitudeModulation << 7));
}
static void updateOperatorSecondaryDecayRate(u8 channel, u8 operator)
{
Operator* op = getOperator(channel, operator);
writeOperatorReg(channel, operator, 0x70, op->secondaryDecayRate);
}
static void updateOperatorReleaseRateAndSecondaryAmplitude(
u8 channel, u8 operator)
{
Operator* op = getOperator(channel, operator);
writeOperatorReg(channel, operator, 0x80,
op->releaseRate +(op->secondaryAmplitude << 4));
}
static void updateOperatorSsgEg(u8 channel, u8 operator)
{
Operator* op = getOperator(channel, operator);
writeOperatorReg(channel, operator, 0x90, op->ssgEg);
}
static void updateOperatorTotalLevel(u8 channel, u8 operator)
{
Operator* op = getOperator(channel, operator);
writeOperatorReg(channel, operator, 0x40,
effectiveTotalLevel(channel, operator, op->totalLevel));
}
static u8 effectiveTotalLevel(u8 channel, u8 operator, u8 totalLevel)
{
return isOutputOperator(fmChannel(channel)->algorithm, operator)
? volumeAdjustedTotalLevel(channel, totalLevel)
: totalLevel;
}
static bool isOutputOperator(u8 algorithm, u8 operator)
{
return (algorithm < 4 && operator== 3)
|| (algorithm == 4 && (operator== 2 || operator== 3))
|| ((algorithm == 5 || algorithm == 6) && operator> 0)
|| algorithm == 7;
}
static u8 volumeAdjustedTotalLevel(u8 channel, u8 totalLevel)
{
u8 volume = volumes[channel];
u8 logarithmicVolume = 0x7F - VOLUME_TO_TOTAL_LEVELS[volume];
u8 inverseTotalLevel = 0x7F - totalLevel;
u8 inverseNewTotalLevel
= (u16)inverseTotalLevel * (u16)logarithmicVolume / (u16)0x7F;
return 0x7F - inverseNewTotalLevel;
}
const FmChannel* synth_channelParameters(u8 channel)
{
return fmChannel(channel);
}
const Global* synth_globalParameters()
{
return &global;
}
void synth_setParameterUpdateCallback(ParameterUpdatedCallback* cb)
{
parameterUpdatedCallback = cb;
}