-
Notifications
You must be signed in to change notification settings - Fork 21
/
synth-framework.jsfx-inc
255 lines (230 loc) · 7.21 KB
/
synth-framework.jsfx-inc
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
/* Generated by: JSFX Pre-Processor (https://github.com/geraintluff/jsfx-preprocessor) */
@init
function synth_reset_controllers() local(i) (
synthx_pitchbend = 0;
synthx_pitchbend_ratio = 1;
i = 0;
while (i < 128) (
(i == 7 || i == 10 || (i >= 91 && i <= 95) || (i >= 70 && i <= 79) || i >= 120) ? (
0; // These don't get reset
) : (i >= 98 && i <= 101) ? (
synthx_controllers[i] = 127;
) : (i != 0 && i != 32) ? (
synthx_controllers[i] = 0;
);
i += 1;
);
synthx_controllers[11] = 127;
);
function synth_option_midi_sink(value) (
synthx_midi_sink = value;
);
function synth_setup(freemem, custom_slots) local(i) (
synthx_polyphony = 16;
synthx_custom_offset = 9/*NOTE: ACTIVE, SAMPLES_FROM_RELEASE, SAMPLES_FROM_ATTACK, SAMPLES_FROM_SUSTAIN_RELEASE, NOTE, BASE_FREQ, VEL, CHANNEL, CUSTOM_INIT*/;
synthx_timestep = 1/srate;
synthx_step = synthx_custom_offset + custom_slots;
synthx_maxduration = 60*60*24*365*srate; // one year
synthx_pitchbend = 0;
synthx_pitchbend_ratio = 1;
synthx_pitchbend_range = 2;
synthx_max_active_note = 0;
synthx_current_note = 0;
freemem = (synthx_controllers = freemem) + 128;
freemem = (synthx_notestack = freemem) + synthx_polyphony*synthx_step;
synth_reset_controllers();
i = 0;
while (i < synthx_polyphony*synthx_step) (
synthx_notestack[i] = 0;
i += 1;
);
freemem;
);
function synth_init(freemem, custom_slots) (
synth_setup(freemem, custom_slots);
);
function synth_block() local(midi_offset, midi_msg1, midi_msg23, midi_msg2, midi_msg3, midi_type, midi_channel, i, note, selected_note) (
while (midirecv(midi_offset, midi_msg1, midi_msg23)) (
!synthx_midi_sink ? (
midisend(midi_offset, midi_msg1, midi_msg23); // passthrough
);
midi_type = midi_msg1>>4;
midi_channel = midi_msg1&0x0f;
midi_msg2 = midi_msg23&$xff; // note / controller
midi_msg3 = midi_msg23>>8; // velocity / value
(midi_type == $x9 && midi_msg3 != 0) ? (
// Note on
selected_note = -1;
// Choose inactive note
i = 0;
while (i < synthx_polyphony && selected_note < 0) (
note = synthx_notestack + synthx_step*i;
!note[0/*NOTE:ACTIVE*/] ? (
selected_note = note;
);
i += 1;
);
// If we didn't find one, stop a release
selected_note < 0 ? (
// Start by selecting random index
i = floor(rand()*synthx_polyphony);
selected_note = synthx_notestack + synthx_step*i;
// Choose note with longest release phase
i = 0;
while (i < synthx_polyphony && selected_note < 0) (
note = synthx_notestack + synthx_step*i;
note[1/*NOTE:SAMPLES_FROM_RELEASE*/] > selected[1/*NOTE:SAMPLES_FROM_RELEASE*/] ? (
selected_note = note;
);
i += 1;
);
);
// Reset the custom note parameters to 0
i = synthx_custom_offset;
while (i < synthx_step) (
selected_note[i] = 0;
i += 1;
);
// Set up the note parameters
selected_note[0/*NOTE:ACTIVE*/] = 1;
selected_note[2/*NOTE:SAMPLES_FROM_ATTACK*/] = -midi_offset;
selected_note[1/*NOTE:SAMPLES_FROM_RELEASE*/] = -synthx_maxduration;
selected_note[3/*NOTE:SAMPLES_FROM_SUSTAIN_RELEASE*/] = -synthx_maxduration;
selected_note[4/*NOTE:NOTE*/] = midi_msg2;
selected_note[5/*NOTE:BASE_FREQ*/] = 440*pow(2, (midi_msg2 - 69)/12);
selected_note[6/*NOTE:VEL*/] = midi_msg3;
selected_note[7/*NOTE:CHANNEL*/] = midi_channel;
selected_note[8/*NOTE:CUSTOM_INIT*/] = 0;
) : (midi_type == $x8 || (midi_type == $x9 && midi_msg3 == 0)) ? (
// Choose duplicate note if there is one
i = 0;
while (i < synthx_polyphony) (
note = synthx_notestack + synthx_step*i;
note[4/*NOTE:NOTE*/] == midi_msg2 ? (
note[1/*NOTE:SAMPLES_FROM_RELEASE*/] = max(note[1/*NOTE:SAMPLES_FROM_RELEASE*/], -midi_offset);
note[3/*NOTE:SAMPLES_FROM_SUSTAIN_RELEASE*/] = max(note[3/*NOTE:SAMPLES_FROM_SUSTAIN_RELEASE*/], -midi_offset);
);
i += 1;
);
) : (midi_type == 11) ? (
// Controller
midi_msg2 == 121 ? (
synth_reset_controllers();
) : midi_msg2== 123 ? (
// stop all notes (with release)
i = 0;
while (i < synthx_polyphony) (
note = synthx_notestack + synthx_step*i;
note[1/*NOTE:SAMPLES_FROM_RELEASE*/] = max(note[1/*NOTE:SAMPLES_FROM_RELEASE*/], 0);
note[3/*NOTE:SAMPLES_FROM_SUSTAIN_RELEASE*/] = max(note[3/*NOTE:SAMPLES_FROM_SUSTAIN_RELEASE*/], 0);
i += 1;
);
) : midi_msg2== 120 ? (
// stop all notes (no release)
i = 0;
while (i < synthx_polyphony) (
note = synthx_notestack + synthx_step*i;
note[0/*NOTE:ACTIVE*/] = 0;
i += 1;
);
) : (
synthx_controllers[midi_msg2] = midi_msg3;
);
) : (midi_type == 14) ? (
synthx_pitchbend = (midi_msg3*128 + midi_msg2) - 8192;
synthx_pitchbend_ratio = pow(2, synthx_pitchbend_range/12*synthx_pitchbend/8192);
);
);
synthx_max_active_note = -1;
i = 0;
note = synthx_notestack;
while (i < synthx_polyphony) (
note[0/*NOTE:ACTIVE*/] ? (
synthx_max_active_note = i;
);
i += 1;
note += synthx_step;
);
synthx_max_active_note + 1;
);
function synth_sample() local(i, note, have_active) (
i = 0;
while (i <= synthx_max_active_note) (
note = synthx_notestack + synthx_step*i;
note[0/*NOTE:ACTIVE*/] ? (
note[2/*NOTE:SAMPLES_FROM_ATTACK*/] += 1;
note[1/*NOTE:SAMPLES_FROM_RELEASE*/] += 1;
synthx_controllers[64] < 64 ? (
note[3/*NOTE:SAMPLES_FROM_SUSTAIN_RELEASE*/] += 1;
);
);
i += 1;
);
);
function synth_note_next(note) local(selected_note, index, note) (
index = floor((note - synthx_custom_offset - synthx_notestack)/synthx_step);
index < 0 ? (
index = 0;
) : (
index += 1;
);
selected_note = -1;
while (index <= synthx_max_active_note && selected_note < 0) (
note = synthx_notestack + synthx_step*index;
note[0/*NOTE:ACTIVE*/] && note[2/*NOTE:SAMPLES_FROM_ATTACK*/] >= 0 ? (
selected_note = note + synthx_custom_offset;
);
index += 1;
);
selected_note;
);
function synth_note_first() (
synth_note_next(-1);
);
function synth_needs_init(note) (
note -= synthx_custom_offset;
note[8/*NOTE:CUSTOM_INIT*/] ? (
0;
) : (
note[8/*NOTE:CUSTOM_INIT*/] = 1;
);
);
function synth_stop(note) (
(note - synthx_custom_offset)[0/*NOTE:ACTIVE*/] = 0;
);
function synth_midinote(note) (
(note - synthx_custom_offset)[4/*NOTE:NOTE*/];
);
function synth_freq(note) (
(note - synthx_custom_offset)[5/*NOTE:BASE_FREQ*/]*synthx_pitchbend_ratio;
);
function synth_velocity(note) (
(note - synthx_custom_offset)[6/*NOTE:VEL*/];
);
function synth_channel(note) (
(note - synthx_custom_offset)[7/*NOTE:CHANNEL*/];
);
function synth_pitchbend() (
synthx_pitchbend;
);
function synth_attack(note) (
(note - synthx_custom_offset)[2/*NOTE:SAMPLES_FROM_ATTACK*/];
);
function synth_attack_seconds(note) (
(note - synthx_custom_offset)[2/*NOTE:SAMPLES_FROM_ATTACK*/]*synthx_timestep;
);
function synth_release(note) (
(note - synthx_custom_offset)[1/*NOTE:SAMPLES_FROM_RELEASE*/];
);
function synth_sustain_release(note) (
(note - synthx_custom_offset)[3/*NOTE:SAMPLES_FROM_SUSTAIN_RELEASE*/];
);
function synth_release_seconds(note) (
(note - synthx_custom_offset)[1/*NOTE:SAMPLES_FROM_RELEASE*/]*synthx_timestep;
);
function synth_sustain_release_seconds(note) (
(note - synthx_custom_offset)[3/*NOTE:SAMPLES_FROM_SUSTAIN_RELEASE*/]*synthx_timestep;
);
function synth_controller(number) (
synthx_controllers[number];
);