forked from geraintluff/jsfx
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Hammer And String.jsfx
224 lines (192 loc) · 6.59 KB
/
Hammer And String.jsfx
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
desc:Hammer And String (Synth/Resonator by Geraint Luff)
in_pin:Left
in_pin:Right
out_pin:Left
out_pin:Right
slider1:decay_wavelengths_on=50<1,1000,1>-Decay wavelengths
slider2:decay_wavelengths_off=5<1,100,1>-Off-Decay wavelengths
slider3:portamento_ms=5<0,100,1>-Portamento (ms)
slider4:impulse_brightness=30<0.1,100,0.01>-Impulse brightness
slider5:anticipation_ms=0<0,100,1>-Anticipation (ms) latency
slider6:resonator_scaling=0<0,1,0.01>-Input scaling (sustain, impulse)
slider7:invert_feedback=0<0,1,1{positive,negative}>-Feedback
slider8:impulse_amp=1<0,1,0.01>-Impulse volume
import ui-lib.jsfx-inc
import synth-framework.jsfx-inc
import delay-utils.jsfx-inc
@init
function tanh(x) (
x >= 20 ? (
1;
) : x <= -20 ? (
-1;
) : (
(exp(2*x) - 1)/(exp(2*x) + 1);
);
);
freemem = 0;
freemem = ui_setup(freemem);
freemem = synth_setup(freemem, 0);
min_freq = 10;
max_delay_samples = srate/min_freq;
delay_buffer0.delay_config("interpolation samples", 8);
delay_buffer1.delay_config("interpolation samples", 8);
freemem = delay_buffer0.delay_setup(freemem, max_delay_samples);
freemem = delay_buffer1.delay_setup(freemem, max_delay_samples);
delay_samples = max_delay_samples;
delay_samples_target = max_delay_samples;
latest_release = 0;
@block
anticipation_samples = ceil(anticipation_ms*srate/1000);
pdc_bot_ch = 0;
pdc_top_ch = 2;
pdc_delay = anticipation_samples;
synth_block();
portamento_time = portamento_ms/1000;
delay_samples_slew_factor = portamento_time ? (1 - exp(-1/srate/portamento_time)) : 1;
decay_factor_on = exp(-1/decay_wavelengths_on);
decay_factor_off = exp(-1/decay_wavelengths_off);
decay_factor_release_samples = 0.002*srate;
// Impulse timbre parameters
impulse_length_samples = 0.01*srate;
resonator_gain = 0;
@sample
synth_sample();
latest_note = -1;
excitation0 = 0;
excitation1 = 0;
function impulse(note) local(attack, phase, amp) (
attack = synth_attack(note) - anticipation_samples;
attack >= 0 && attack < impulse_length_samples ? (
phase = attack/impulse_length_samples - 0.5;
phase2 = impulse_brightness*phase*pow(synth_velocity(note)/127, 1);
amp = impulse_amp*pow(synth_velocity(note)/127, 1.5)*(1 + 0.25*impulse_brightness)/(0.1 + impulse_brightness);
tanh(phase2*2*$pi)*(0.5 + 0.5*cos(phase*2*$pi))*amp;
) : 0;
);
note = synth_note_first();
while (note > 0) (
note_min_release = min(note_min_release, synth_release(note));
synth_release(note) > impulse_decay_samples ? (
synth_stop(note);
) : (
impulse_value = impulse(note);
excitation0 += impulse_value;
excitation1 += impulse_value;
synth_release(note) < 0 ? (
latest_note < 0 || synth_attack(latest_note) > synth_attack(note) ? (
latest_note = note;
);
);
);
note = synth_note_next(note);
);
latest_note >= 0 ? (
latest_release = 0;
invert_feedback ? (
delay_samples_target = min(max_delay_samples, srate/synth_freq(latest_note)*0.5);
) : (
delay_samples_target = min(max_delay_samples, srate/synth_freq(latest_note));
);
) : (
latest_release += 1;
);
resonator_gain = sqrt(1 - abs(decay_factor_on));
resonator_gain += (1 - resonator_gain)*resonator_scaling;
latest_release <= 0 ? (
decay_factor = decay_factor_on;
) : latest_release < decay_factor_release_samples ? (
decay_factor = decay_factor_on + (decay_factor_off - decay_factor_on)*latest_release/decay_factor_release_samples;
resonator_gain *= latest_release/decay_factor_release_samples;
) : (
decay_factor = decay_factor_off;
resonator_gain = 0;
);
invert_feedback ? decay_factor = -sqrt(decay_factor);
delay_samples += (delay_samples_target - delay_samples)*delay_samples_slew_factor;
delayed0 = delay_buffer0.delay_output_linear(delay_samples - 1);
delayed1 = delay_buffer1.delay_output_linear(delay_samples - 1);
excitation0 += delayed0*decay_factor;
excitation1 += delayed1*decay_factor;
excitation0 += spl0*resonator_gain;
excitation1 += spl1*resonator_gain;
delay_buffer0.delay_input(excitation0);
delay_buffer1.delay_input(excitation1);
spl0 = excitation0;
spl1 = excitation1;
@gfx 410 410
function labelled_default_dial(value, low, high, bias, label, format_text, default) local(height) (
height = min(ui_height()/2, 50);
ui_push_height(height);
value = control_dial(value, low, high, bias);
ui_click() && ui_clickcount() == 2 ? (
value = default;
);
ui_pop(height);
ui_push_above(height);
ui_text(label);
ui_pop(height);
ui_push_below(height);
ui_textnumber(value, format_text);
ui_pop();
value;
);
function labelled_switch(value, label, format_text) local(height) (
height = min(ui_height()/2, 50);
ui_push_height(height);
ui_push_heighttext(-1);
ui_push_width(50);
value = control_switch(value);
ui_pop();
ui_pop();
ui_pop(height);
ui_push_above(height);
ui_text(label);
ui_pop(height);
ui_push_below(height);
ui_text(format_text);
ui_pop();
value;
);
control_start("main", "tron");
ui_screen() == "deprecated-popup" ? (
seen_popup = 1;
control_dialog("Notice");
ui_wraptext("Hammer and String is no longer maintained.\n\nAll its features should be present in Hammer and Chord. For portamento settings, see Hammer and Chord's \"config\" page.");
) : ui_screen() == "main" ? (
!seen_popup ? ui_screen_open("deprecated-popup");
control_navbar("Hammer And String", -1, -1);
ui_split_topratio(0.5);
ui_split_leftratio(0.5);
control_group("Envelope");
ui_split_leftratio(0.5);
decay_wavelengths_on = labelled_default_dial(decay_wavelengths_on, 1, 1000, log(1000), "Decay", "", 50);
ui_split_next();
decay_wavelengths_off = labelled_default_dial(decay_wavelengths_off, 1, 100, log(100), "Release", "", 5);
ui_pop();
ui_split_next();
control_group("Portamento");
ui_split_leftratio(0.5);
portamento_ms = labelled_default_dial(portamento_ms, 1, 100, 3, "Portamento", "%ims", 5);
ui_split_next();
anticipation_ms = labelled_default_dial(anticipation_ms, 1, 100, log(100), "Anticipation", "%ims", 0);
ui_pop();
ui_pop();
ui_split_next();
ui_split_leftratio(0.5);
control_group("Impulse");
ui_split_leftratio(0.5);
impulse_brightness = labelled_default_dial(impulse_brightness, 0.1, 100, log(1000), "Brightness", "", 30);
ui_split_next();
impulse_amp = labelled_default_dial(impulse_amp, 0, 1, log(100), "Volume", "", 1);
ui_pop();
ui_split_next();
control_group("Resonator");
ui_split_leftratio(0.5);
invert_feedback = labelled_switch(invert_feedback, "Feedback", invert_feedback ? "negative" : "positive");
ui_split_next();
resonator_scaling = labelled_default_dial(resonator_scaling, 0, 1, 1, "Input type", resonator_scaling < 0.1 ? "continuous" : resonator_scaling < 0.7 ? "hybrid" : "impulse", 1);
ui_pop();
ui_pop();
ui_pop();
) : ui_system();