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