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