[epiano] fix: pick different sample for different velocity
[software/lv2-mdametapiano.git] / src / mdaPianoVoice.cpp
1 #include "mdaPianoVoice.h"
2
3 mdaPianoVoice::mdaPianoVoice(double rate, Sample * master_samples, KGRP * master_kgrp) {
4 //set tuning
5 Fs = rate;
6 iFs = 1.0f/Fs;
7
8 samples = master_samples;
9 kgrp = master_kgrp;
10
11 // TODO: load default values from ttl file
12 #ifdef PIANO
13 default_preset[p_offset(p_envelope_decay)] = 0.500f;
14 default_preset[p_offset(p_envelope_release)] = 0.500f;
15 default_preset[p_offset(p_hardness_offset)] = 0.500f;
16 default_preset[p_offset(p_velocity_to_hardness)] = 0.500f;
17 default_preset[p_offset(p_muffling_filter)] = 0.803f;
18 default_preset[p_offset(p_velocity_to_muffling)] = 0.251f;
19 default_preset[p_offset(p_velocity_sensitivity)] = 0.376f;
20 default_preset[p_offset(p_stereo_width)] = 0.500f;
21 default_preset[p_offset(p_polyphony)] = 0.330f; // unused
22 default_preset[p_offset(p_fine_tuning)] = 0.500f;
23 default_preset[p_offset(p_random_detuning)] = 0.246f;
24 default_preset[p_offset(p_stretch_tuning)] = 0.500f;
25 #elif defined EPIANO
26 default_preset[p_offset(p_envelope_decay)] = 0.500f;
27 default_preset[p_offset(p_envelope_release)] = 0.500f;
28 default_preset[p_offset(p_hardness)] = 0.500f;
29 default_preset[p_offset(p_treble_boost)] = 0.500f;
30 default_preset[p_offset(p_modulation)] = 0.500f;
31 default_preset[p_offset(p_lfo_rate)] = 0.650f;
32 default_preset[p_offset(p_velocity_sensitivity)] = 0.250f;
33 default_preset[p_offset(p_stereo_width)] = 0.500f;
34 default_preset[p_offset(p_polyphony)] = 0.500f; // unused
35 default_preset[p_offset(p_fine_tuning)] = 0.500f;
36 default_preset[p_offset(p_random_tuning)] = 0.146f;
37 default_preset[p_offset(p_overdrive)] = 0.000f;
38 #endif
39
40 reset();
41 update(Default);
42 }
43
44
45 float mdaPianoVoice::p_helper(unsigned short id, Param d) {
46 if (d == Default)
47 return default_preset[p_offset(id)];
48 else
49 return *p(id);
50 }
51
52
53 void mdaPianoVoice::on(unsigned char key, unsigned char velocity) {
54 // store key that turned this voice on (used in 'get_key')
55 m_key = key;
56 update(Current);
57
58 float l=99.0f;
59 #ifdef PIANO
60 uint32_t k, s;
61 #elif defined EPIANO
62 long k, s;
63 #endif
64
65 if(velocity > 0)
66 {
67 k = (key - 60) * (key - 60);
68 l = fine + random * ((float)(k % 13) - 6.5f); //random & fine tune
69 #ifdef PIANO
70 if(key > 60) l += stretch * (float)k; //stretch
71 #endif
72
73 s = size;
74 #ifdef PIANO
75 if(velocity > 40) s += (uint32_t)(sizevel * (float)(velocity - 40));
76 #endif
77
78 k = 0;
79 while(key > (kgrp[k].high + s)) k += SAMPLES_PER_NOTE; // find keygroup
80 sample_index = k; // store sample index
81
82 l += (float)(key - kgrp[k].root); // pitch
83 #ifdef PIANO
84 l = 22050.0f * iFs * (float)exp(0.05776226505 * l);
85 delta = (uint32_t)(65536.0f * l);
86 #elif defined EPIANO
87 l = 32000.0f * iFs * (float)exp(0.05776226505 * l);
88 delta = (long)(65536.0f * l);
89 #endif
90 frac = 0;
91 pos = 0;
92
93 #ifdef EPIANO
94 if(velocity > 48) sample_index++; // mid velocity sample
95 if(velocity > 80) sample_index++; // high velocity sample
96 #endif
97
98 #ifdef PIANO
99 end = samples[sample_index].size;
100 #elif defined EPIANO
101 end = samples[sample_index].size - 1;
102 #endif
103 loop = kgrp[sample_index].loop;
104
105
106 #ifdef PIANO
107 env = (0.5f + velsens) * (float)pow(0.0078f * velocity, velsens); //velocity
108
109 l = 50.0f + *p(p_muffling_filter) * *p(p_muffling_filter) * muff + muffvel * (float)(velocity - 64); //muffle
110 if(l < (55.0f + 0.25f * (float)key)) l = 55.0f + 0.25f * (float)key;
111 if(l > 210.0f) l = 210.0f;
112 ff = l * l * iFs;
113 f0 = f1 = 0.0f;
114 #elif defined EPIANO
115 env = (3.0f + 2.0f * velsens) * (float)pow(0.0078f * velocity, velsens); // velocity
116 // high notes quieter
117 if(key > 60) {
118 env *= (float)exp(0.01f * (float)(60 - key));
119 }
120 #endif
121
122 // note->pan
123 if(key < 12) key = 12;
124 if(key > 108) key = 108;
125
126 #ifdef PIANO
127 l = volume * trim;
128 outr = l + l * width * (float)(key - 60);
129 outl = l + l - outr;
130
131 if(key < 44) key = 44; //limit max decay length
132 l = 2.0f * *p(p_envelope_decay);
133 if(l < 1.0f) l += 0.25f - 0.5f * *p(p_envelope_decay);
134 dec = (float)exp(-iFs * exp(-0.6 + 0.033 * (double)key - l));
135 #elif defined EPIANO
136 outr = volume + volume * width * (float)(key - 60);
137 outl = volume + volume - outr;
138
139 if(key < 44) key = 44; // limit max decay length
140 dec = (float)exp(-iFs * exp(-1.0 + 0.03 * (double)key - 2.0f * *p(p_envelope_decay)));
141 #endif
142 } else {
143 // some keyboards send note off events as 'note on' with velocity 0
144 release(0);
145 }
146 }
147
148
149 void mdaPianoVoice::reset() {
150 env = 0.0f;
151 dec = 0.99f;
152 sustain = 0;
153 volume = 0.2f;
154 #ifdef PIANO
155 muff = 160.0f;
156 cpos = 0;
157 #elif defined EPIANO
158 tl = tr = lfo0 = dlfo = 0.0f;
159 lfo1 = 1.0f;
160 #endif
161 m_key = lvtk::INVALID_KEY;
162 }
163
164
165 void mdaPianoVoice::release(unsigned char velocity) {
166 if(sustain==0) {
167 #ifdef PIANO
168 //no release on highest notes
169 if(m_key < 94 || m_key == SUSTAIN) {
170 dec = (float)exp(-iFs * exp(2.0 + 0.017 * (double)m_key - 2.0 * *p(p_envelope_release)));
171 }
172 #elif defined EPIANO
173 dec = (float)exp(-iFs * exp(6.0 + 0.01 * (double)m_key - 5.0 * *p(p_envelope_release)));
174 #endif
175 } else {
176 m_key = SUSTAIN;
177 }
178
179 //Mark the voice to be turned off later. It may not be set to
180 //INVALID_KEY yet, because the release sound still needs to be
181 //rendered. m_key is finally set to INVALID_KEY by 'render' when
182 //env < SILENCE
183 m_key = SUSTAIN;
184 }
185
186
187 void mdaPianoVoice::render(uint32_t from, uint32_t to) {
188 // abort if no key is pressed
189 // initially m_key is INVALID_KEY, so no sound will be rendered
190 if (m_key == lvtk::INVALID_KEY)
191 return;
192
193 float x, l, r;
194 #ifdef PIANO
195 uint32_t i;
196 #elif defined EPIANO
197 long i;
198 #endif
199
200 update(Current);
201 for (uint32_t frame = from; frame < to; ++frame) {
202 // initialize left and right output
203 l = r = 0.0f;
204
205 frac += delta; // integer-based linear interpolation
206 pos += frac >> 16;
207 frac &= 0xFFFF; // why AND it with all ones?
208 if(pos > end) pos -= loop; // jump back to loop sample
209
210 #ifdef PIANO
211 i = samples[sample_index].buffer[pos];
212 i = (i << 7) + (frac >> 9) * (samples[sample_index].buffer[pos + 1] - i) + 0x40400000;
213 x = env * (*(float *)&i - 3.0f); //fast int->float
214
215 env = env * dec; //envelope
216 f0 += ff * (x + f1 - f0); //muffle filter
217 f1 = x;
218
219 l += outl * f0;
220 r += outr * f0;
221
222 comb[cpos] = l + r;
223 ++cpos &= 0x7F;
224 x = cdep * comb[cpos]; //stereo simulator
225
226 // write to output
227 p(p_left)[frame] += l + x;
228 p(p_right)[frame] += r - x;
229 #elif defined EPIANO
230 // alternative method (probably faster), said to not work on intel mac
231 //i = samples[sample_index].buffer[pos];
232 //i = (i << 7) + (frac >> 9) * (samples[sample_index].buffer[pos + 1] - i) + 0x40400000;
233
234 i = samples[sample_index].buffer[pos] + ((frac * (samples[sample_index].buffer[pos + 1] - samples[sample_index].buffer[pos])) >> 16);
235 x = env * (float)i / 32768.0f;
236 env = env * dec; // envelope
237
238 // overdrive
239 if(x>0.0f) {
240 x -= overdrive * x * x;
241 if(x < -env)
242 x = -env;
243 }
244
245 l += outl * x;
246 r += outr * x;
247
248 // treble boost
249 tl += tfrq * (l - tl);
250 tr += tfrq * (r - tr);
251 r += treb * (r - tr);
252 l += treb * (l - tl);
253
254 lfo0 += dlfo * lfo1; // LFO for tremolo and autopan
255 lfo1 -= dlfo * lfo0;
256 l += l * lmod * lfo1;
257 r += r * rmod * lfo1; // TODO: worth making all these local variables?
258
259 // write to output
260 p(p_left)[frame] += l;
261 p(p_right)[frame] += r;
262 #endif
263 }
264
265 // turn off further processing when the envelope has rendered the voice silent
266 if (env < SILENCE) {
267 m_key = lvtk::INVALID_KEY;
268 }
269
270 #ifdef EPIANO
271 if(fabs(tl)<1.0e-10) tl = 0.0f; // anti-denormal
272 if(fabs(tr)<1.0e-10) tr = 0.0f;
273 #endif
274 }
275
276
277 void mdaPianoVoice::update(Param par) {
278 #ifdef PIANO
279 size = (uint32_t)(12.0f * p_helper(p_hardness_offset, par) - 6.0f);
280 sizevel = 0.12f * p_helper(p_velocity_to_hardness, par);
281 muffvel = p_helper(p_velocity_to_muffling, par) * p_helper(p_velocity_to_muffling, par) * 5.0f;
282
283 velsens = 1.0f + p_helper(p_velocity_sensitivity, par) + p_helper(p_velocity_sensitivity, par);
284 if(p_helper(p_velocity_sensitivity, par) < 0.25f) {
285 velsens -= 0.75f - 3.0f * p_helper(p_velocity_sensitivity, par);
286 }
287
288 fine = p_helper(p_fine_tuning, par) - 0.5f;
289 random = 0.077f * p_helper(p_random_detuning, par) * p_helper(p_random_detuning, par);
290 stretch = 0.000434f * (p_helper(p_stretch_tuning, par) - 0.5f);
291
292 cdep = p_helper(p_stereo_width, par) * p_helper(p_stereo_width, par);
293 trim = 1.50f - 0.79f * cdep;
294 width = 0.04f * p_helper(p_stereo_width, par); if(width > 0.03f) width = 0.03f;
295 #elif defined EPIANO
296 size = (long)(12.0f * p_helper(p_hardness, par) - 6.0f);
297 treb = 4.0f * p_helper(p_treble_boost, par) * p_helper(p_treble_boost, par) - 1.0f; // treble gain
298
299 // treble frequency
300 if(p_helper(p_treble_boost, par) > 0.5f) {
301 tfrq = 14000.0f;
302 } else {
303 tfrq = 5000.0f;
304 }
305 tfrq = 1.0f - (float)exp(-iFs * tfrq);
306
307 rmod = lmod = p_helper(p_modulation, par) + p_helper(p_modulation, par) - 1.0f; // lfo depth
308 if(p_helper(p_modulation, par) < 0.5f) rmod = -rmod;
309 dlfo = 6.283f * iFs * (float)exp(6.22f * p_helper(p_lfo_rate, par) - 2.61f); // lfo rate
310
311 velsens = 1.0f + p_helper(p_velocity_sensitivity, par) + p_helper(p_velocity_sensitivity, par);
312 if(p_helper(p_velocity_sensitivity, par) < 0.25f) {
313 velsens -= 0.75f - 3.0f * p_helper(p_velocity_sensitivity, par);
314 }
315
316 width = 0.03f * p_helper(p_stereo_width, par);
317 fine = p_helper(p_fine_tuning, par) - 0.5f;
318 random = 0.077f * p_helper(p_random_tuning, par) * p_helper(p_random_tuning, par);
319 overdrive = 1.8f * p_helper(p_overdrive, par);
320 #endif
321 }