-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfmtoy.c
337 lines (282 loc) · 10.9 KB
/
fmtoy.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
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include "libfmvoice/fm_voice.h"
#include "fmtoy.h"
#include "fmtoy_ym2151.h"
#include "fmtoy_ym2203.h"
#include "fmtoy_ym2608.h"
#include "fmtoy_ym2610.h"
#include "fmtoy_ym2610b.h"
#include "fmtoy_ym2612.h"
#include "fmtoy_ym3812.h"
#include "fmtoy_ymf262.h"
#include "midilib/midi.h"
struct fmtoy *fmtoy_new(int clock, int sample_rate) {
struct fmtoy *t = malloc(sizeof(struct fmtoy));
if(!t) return 0;
fmtoy_init(t, clock, sample_rate);
return t;
}
void fmtoy_init(struct fmtoy *fmtoy, int clock, int sample_rate) {
memset(fmtoy, 0, sizeof(*fmtoy));
fmtoy->num_voices = 0;
fmtoy->opl_voices = 0;
fmtoy->opm_voices = 0;
fmtoy->opn_voices = 0;
fmtoy->sample_rate = sample_rate;
fmtoy->clock = clock;
fmtoy->pitch_bend_range = 2;
fmtoy->render_buf_l = fmtoy->render_buf_r = 0;
fmtoy->chip_buf_l = fmtoy->chip_buf_r = 0;
fmtoy->channels[0].chip = &fmtoy_chip_ym2151;
fmtoy->channels[1].chip = &fmtoy_chip_ym2203;
fmtoy->channels[2].chip = &fmtoy_chip_ym2608;
fmtoy->channels[3].chip = &fmtoy_chip_ym2610;
fmtoy->channels[4].chip = &fmtoy_chip_ym2610b;
fmtoy->channels[5].chip = &fmtoy_chip_ym2612;
fmtoy->channels[6].chip = &fmtoy_chip_ym3812;
fmtoy->channels[7].chip = &fmtoy_chip_ymf262;
fmtoy->lfo_clock_period = sample_rate / 100; // every 10 ms fire the timer
fmtoy->lfo_clock_phase = 0;
for(int i = 0; i < 16; i++) {
if(fmtoy->channels[i].chip && fmtoy->channels[i].chip->init) {
int r = fmtoy->channels[i].chip->init(fmtoy, fmtoy->clock, fmtoy->sample_rate, &fmtoy->channels[i]);
if(r) {
fprintf(stderr, "Could not init chip %d: %d\n", i, r);
fmtoy->channels[i].chip = 0;
}
}
}
}
void fmtoy_destroy(struct fmtoy *fmtoy) {
for(int i = 0; i < 16; i++) {
if(fmtoy->channels[i].chip && fmtoy->channels[i].chip->destroy) {
fmtoy->channels[i].chip->destroy(fmtoy, &fmtoy->channels[i]);
}
}
}
int fmtoy_append_fm_voice_bank(struct fmtoy *fmtoy, struct fm_voice_bank *bank) {
int old_num_voices = fmtoy->num_voices;
fmtoy->num_voices += bank->num_opl_voices + bank->num_opm_voices + bank->num_opn_voices;
fmtoy->opl_voices = realloc(fmtoy->opl_voices, fmtoy->num_voices * sizeof(fmtoy->opl_voices[0]));
if(!fmtoy->opl_voices) return -1;
fmtoy->opm_voices = realloc(fmtoy->opm_voices, fmtoy->num_voices * sizeof(fmtoy->opm_voices[0]));
if(!fmtoy->opm_voices) return -1;
fmtoy->opn_voices = realloc(fmtoy->opn_voices, fmtoy->num_voices * sizeof(fmtoy->opn_voices[0]));
if(!fmtoy->opn_voices) return -1;
struct opl_voice *foplv = fmtoy->opl_voices + old_num_voices;
struct opm_voice *fopmv = fmtoy->opm_voices + old_num_voices;
struct opn_voice *fopnv = fmtoy->opn_voices + old_num_voices;
for(int i = 0; i < bank->num_opl_voices; i++) {
struct opl_voice *oplv = &bank->opl_voices[i];
memcpy(foplv, oplv, sizeof(*foplv));
opm_voice_init(fopmv);
opm_voice_load_opl_voice(fopmv, oplv);
opn_voice_init(fopnv);
opn_voice_load_opl_voice(fopnv, oplv);
foplv++;
fopmv++;
fopnv++;
}
for(int i = 0; i < bank->num_opm_voices; i++) {
struct opm_voice *opmv = &bank->opm_voices[i];
opl_voice_init(foplv);
opl_voice_load_opm_voice(foplv, opmv);
memcpy(fopmv, opmv, sizeof(*fopmv));
opn_voice_init(fopnv);
opn_voice_load_opm_voice(fopnv, opmv);
foplv++;
fopmv++;
fopnv++;
}
for(int i = 0; i < bank->num_opn_voices; i++) {
struct opn_voice *opnv = &bank->opn_voices[i];
opl_voice_init(foplv);
opl_voice_load_opn_voice(foplv, opnv);
opm_voice_init(fopmv);
opm_voice_load_opn_voice(fopmv, opnv);
memcpy(fopnv, opnv, sizeof(*fopnv));
foplv++;
fopmv++;
fopnv++;
}
return 0;
}
int fmtoy_allocate_voices(struct fmtoy *fmtoy, int num_voices) {
fmtoy->num_voices += num_voices;
fmtoy->opl_voices = realloc(fmtoy->opl_voices, fmtoy->num_voices * sizeof(fmtoy->opl_voices[0]));
if(!fmtoy->opl_voices) return -1;
fmtoy->opm_voices = realloc(fmtoy->opm_voices, fmtoy->num_voices * sizeof(fmtoy->opm_voices[0]));
if(!fmtoy->opm_voices) return -1;
fmtoy->opn_voices = realloc(fmtoy->opn_voices, fmtoy->num_voices * sizeof(fmtoy->opn_voices[0]));
if(!fmtoy->opn_voices) return -1;
return 0;
}
int fmtoy_load_opm_voice(struct fmtoy *fmtoy, int voice_num, struct opm_voice *voice) {
if(voice_num >= fmtoy->num_voices) return -1;
struct opl_voice *oplv = &fmtoy->opl_voices[voice_num];
opl_voice_init(oplv);
opl_voice_load_opm_voice(oplv, voice);
struct opm_voice *opmv = &fmtoy->opm_voices[voice_num];
memcpy(opmv, voice, sizeof(*voice));
struct opn_voice *opnv = &fmtoy->opn_voices[voice_num];
opn_voice_init(opnv);
opn_voice_load_opm_voice(opnv, voice);
return 0;
}
void fmtoy_program_change(struct fmtoy *fmtoy, uint8_t channel, uint8_t program) {
// change on all channels
fmtoy->channels[channel].program = program;
if(fmtoy->channels[channel].chip && fmtoy->channels[channel].chip->program_change)
fmtoy->channels[channel].chip->program_change(fmtoy, program, &fmtoy->channels[channel]);
}
static int find_unused_channel(struct fmtoy_chip_channel *channels, int num_channels) {
int chip_channel = 0;
uint32_t min_frames = UINT32_MAX;
for(int i = 0; i < num_channels; i++) {
if(!channels[i].on) {
chip_channel = i;
break;
}
if(channels[i].frames < min_frames) {
min_frames = channels[i].frames;
chip_channel = i;
}
}
return chip_channel;
}
static int find_used_channel(struct fmtoy_chip_channel *channels, int num_channels, int note) {
int chip_channel = -1;
for(int i = 0; i < num_channels; i++)
if(channels[i].note == note)
return i;
return chip_channel;
}
// just a bogus function,
// returns unique ever increasing values,
// like a timer would
static uint32_t frame_time() {
static uint32_t t = 1;
return t++;
}
void fmtoy_note_on(struct fmtoy *fmtoy, uint8_t channel, uint8_t note, uint8_t velocity) {
if(velocity == 0) {
fmtoy_note_off(fmtoy, channel, note, velocity);
return;
}
if(channel >= 16) return;
if(!fmtoy->channels[channel].chip) return;
if(!fmtoy->channels[channel].chip->note_on) return;
int chip_channel = find_unused_channel(fmtoy->channels[channel].chip->channels, fmtoy->channels[channel].chip->max_poliphony);
fmtoy->channels[channel].chip->channels[chip_channel].note = note;
fmtoy->channels[channel].chip->channels[chip_channel].frames = frame_time();
fmtoy->channels[channel].chip->channels[chip_channel].on = 1;
float pitch = midi_note_freq(fmtoy->channels[channel].chip->channels[chip_channel].note, (float)fmtoy->channels[channel].pitch_bend * (float)fmtoy->pitch_bend_range / 8191.0);
fmtoy->channels[channel].chip->channels[chip_channel].pitch = pitch;
fmtoy->channels[channel].chip->note_on(fmtoy, chip_channel, pitch, velocity, &fmtoy->channels[channel]);
}
void fmtoy_note_off(struct fmtoy *fmtoy, uint8_t channel, uint8_t note, uint8_t velocity) {
if(channel >= 16) return;
if(!fmtoy->channels[channel].chip) return;
if(!fmtoy->channels[channel].chip->note_off) return;
int chip_channel = find_used_channel(fmtoy->channels[channel].chip->channels, fmtoy->channels[channel].chip->max_poliphony, note);
if(chip_channel < 0) return;
fmtoy->channels[channel].chip->channels[chip_channel].on = 0;
fmtoy->channels[channel].chip->note_off(fmtoy, chip_channel, velocity, &fmtoy->channels[channel]);
}
void fmtoy_pitch_bend(struct fmtoy *fmtoy, uint8_t channel, int bend) {
fmtoy->channels[channel].pitch_bend = bend;
if(channel < 16 && fmtoy->channels[channel].chip && fmtoy->channels[channel].chip->pitch_bend) {
for(int i = 0; i < fmtoy->channels[channel].chip->max_poliphony; i++) {
if(!fmtoy->channels[channel].chip->channels[i].on) continue;
float pitch = midi_note_freq(fmtoy->channels[channel].chip->channels[i].note, (float)fmtoy->channels[channel].pitch_bend * (float)fmtoy->pitch_bend_range / 8191.0);
fmtoy->channels[channel].chip->channels[i].pitch = pitch;
fmtoy->channels[channel].chip->pitch_bend(fmtoy, i, pitch, &fmtoy->channels[channel]);
}
}
}
void fmtoy_mod_wheel(struct fmtoy *fmtoy, uint8_t channel, int mod) {
if(channel < 16 && fmtoy->channels[channel].chip && fmtoy->channels[channel].chip->mod_wheel)
fmtoy->channels[channel].chip->mod_wheel(fmtoy, mod, &fmtoy->channels[channel]);
}
void fmtoy_cc(struct fmtoy *fmtoy, uint8_t channel, int cc, int value) {
switch(cc) {
case 0x01:
fmtoy_mod_wheel(fmtoy, channel, value);
break;
}
}
static void fmtoy_set_buf_size(struct fmtoy *fmtoy, int size) {
if(fmtoy->buf_size >= size) return;
fmtoy->buf_size = size;
fmtoy->render_buf_l = realloc(fmtoy->render_buf_l, size * sizeof(*fmtoy->render_buf_l));
fmtoy->render_buf_r = realloc(fmtoy->render_buf_r, size * sizeof(*fmtoy->render_buf_r));
fmtoy->chip_buf_l = realloc(fmtoy->chip_buf_l, size * sizeof(*fmtoy->chip_buf_l));
fmtoy->chip_buf_r = realloc(fmtoy->chip_buf_r, size * sizeof(*fmtoy->chip_buf_r));
}
// handle portamento and software LFO
static void fmtoy_timer_tick(struct fmtoy *fmtoy) {
for(int i = 0; i < 16; i++) {
struct fmtoy_chip *chip = fmtoy->channels[i].chip;
if(!chip) continue;
for(int j = 0; j < chip->max_poliphony; j++) {
struct fmtoy_chip_channel *channel = chip->channels + j;
if(channel->on) {
}
}
}
}
void fmtoy_render(struct fmtoy *fmtoy, int samples) {
fmtoy_set_buf_size(fmtoy, samples);
memset(fmtoy->render_buf_l, 0, sizeof(fmtoy->render_buf_l[0]) * samples);
memset(fmtoy->render_buf_r, 0, sizeof(fmtoy->render_buf_r[0]) * samples);
stream_sample_t *chipBufs[2] = { fmtoy->chip_buf_l, fmtoy->chip_buf_r };
stream_sample_t *renderBufs[2] = { fmtoy->render_buf_l, fmtoy->render_buf_r };
for(int s = 0, x = 0; s < samples; s++) {
if(fmtoy->lfo_clock_phase == 0) {
fmtoy_timer_tick(fmtoy);
}
fmtoy->lfo_clock_phase++;
int render_samples = 0;
if(fmtoy->lfo_clock_phase >= fmtoy->lfo_clock_period) {
fmtoy->lfo_clock_phase = 0;
render_samples = s - x + 1;
x = s + 1;
} else if(s == samples - 1) {
render_samples = samples - x;
}
if(render_samples == 0) continue;
for(int i = 0; i < 16; i++) {
if(!fmtoy->channels[i].chip) continue;
if(!fmtoy->channels[i].chip->render) continue;
fmtoy->channels[i].chip->render(fmtoy, chipBufs, render_samples, &fmtoy->channels[i]);
for(int j = 0; j < render_samples; j++) {
renderBufs[0][j] += chipBufs[0][j];
renderBufs[1][j] += chipBufs[1][j];
}
}
chipBufs[0] += render_samples;
chipBufs[1] += render_samples;
renderBufs[0] += render_samples;
renderBufs[1] += render_samples;
}
for(int i = 0; i < samples; i++) {
if(fmtoy->render_buf_l[i] > 32767) fmtoy->render_buf_l[i] = 32767;
if(fmtoy->render_buf_l[i] < -32768) fmtoy->render_buf_l[i] = -32768;
if(fmtoy->render_buf_r[i] > 32767) fmtoy->render_buf_r[i] = 32767;
if(fmtoy->render_buf_r[i] < -32768) fmtoy->render_buf_r[i] = -32768;
}
}
stream_sample_t *fmtoy_get_buf_l(struct fmtoy *fmtoy) {
return fmtoy->render_buf_l;
}
stream_sample_t *fmtoy_get_buf_r(struct fmtoy *fmtoy) {
return fmtoy->render_buf_r;
}
const char *fmtoy_channel_name(struct fmtoy *fmtoy, uint8_t channel) {
if(fmtoy->channels[channel].chip)
return fmtoy->channels[channel].chip->name;
return 0;
}