update voice on initialisation
[software/lv2-mdametapiano.git] / src / mdaPianoVoice.cpp
1 #include "mdaPianoVoice.h"
2
3 mdaPianoVoice::mdaPianoVoice(double rate, short * samples, KGRP * master_kgrp) {
4 //set tuning
5 Fs = rate;
6 iFs = 1.0f/Fs;
7
8 waves = samples;
9 kgrp = master_kgrp;
10
11 default_preset[p_offset(p_envelope_decay)] = 0.500f;
12 default_preset[p_offset(p_envelope_release)] = 0.500f;
13 default_preset[p_offset(p_hardness_offset)] = 0.500f;
14 default_preset[p_offset(p_velocity_to_hardness)] = 0.500f;
15 default_preset[p_offset(p_muffling_filter)] = 0.803f;
16 default_preset[p_offset(p_velocity_to_muffling)] = 0.251f;
17 default_preset[p_offset(p_velocity_sensitivity)] = 0.376f;
18 default_preset[p_offset(p_stereo_width)] = 0.500f;
19 default_preset[p_offset(p_polyphony)] = 0.330f;
20 default_preset[p_offset(p_fine_tuning)] = 0.500f;
21 default_preset[p_offset(p_random_detuning)] = 0.246f;
22 default_preset[p_offset(p_stretch_tuning)] = 0.500f;
23
24 reset();
25 update(Default);
26 }
27
28
29 void mdaPianoVoice::reset() {
30 env = 0.0f;
31 dec = 0.99f;
32 muff = 160.0f;
33 volume = 0.2f;
34 sustain = 0;
35 cpos = 0;
36 m_key = LV2::INVALID_KEY;
37 }
38
39
40 float mdaPianoVoice::p_helper(unsigned short id, Param d) {
41 if (d == Default)
42 return default_preset[p_offset(id)];
43 else
44 return *p(id);
45 }
46
47
48 void mdaPianoVoice::on(unsigned char note, unsigned char velocity)
49 {
50 // store key that turned this voice on (used in 'get_key')
51 m_key = note;
52
53 float l=99.0f;
54 uint32_t k, s;
55
56 if(velocity>0)
57 {
58 // TODO: move this to mdaPiano.cpp
59 /*
60 if(activevoices < poly) //add a note
61 {
62 vl = activevoices;
63 activevoices++;
64 }
65 else //steal a note
66 {
67 for(v=0; v<poly; v++) //find quietest voice
68 {
69 if(voice[v].env < l) { l = voice[v].env; vl = v; }
70 }
71 }
72 */
73
74 k = (note - 60) * (note - 60);
75 l = fine + random * ((float)(k % 13) - 6.5f); //random & fine tune
76 if(note > 60) l += stretch * (float)k; //stretch
77
78 s = size;
79 if(velocity > 40) s += (uint32_t)(sizevel * (float)(velocity - 40));
80
81 k = 0;
82 while(note > (kgrp[k].high + s)) k++; //find keygroup
83
84 l += (float)(note - kgrp[k].root); //pitch
85 l = 22050.0f * iFs * (float)exp(0.05776226505 * l);
86 delta = (uint32_t)(65536.0f * l);
87 frac = 0;
88 pos = kgrp[k].pos;
89 end = kgrp[k].end;
90 loop = kgrp[k].loop;
91
92 env = (0.5f + velsens) * (float)pow(0.0078f * velocity, velsens); //velocity
93
94 l = 50.0f + *p(p_muffling_filter) * *p(p_muffling_filter) * muff + muffvel * (float)(velocity - 64); //muffle
95 if(l < (55.0f + 0.25f * (float)note)) l = 55.0f + 0.25f * (float)note;
96 if(l > 210.0f) l = 210.0f;
97 ff = l * l * iFs;
98 f0 = f1 = 0.0f;
99
100 if(note < 12) note = 12;
101 if(note > 108) note = 108;
102 l = volume * trim;
103 outr = l + l * width * (float)(note - 60);
104 outl = l + l - outr;
105
106 if(note < 44) note = 44; //limit max decay length
107 l = 2.0f * *p(p_envelope_decay);
108 if(l < 1.0f) l += 0.25f - 0.5f * *p(p_envelope_decay);
109 dec = (float)exp(-iFs * exp(-0.6 + 0.033 * (double)note - l));
110 }
111 else //note off
112 {
113 // TODO: move the loop to mdaPiano.cpp
114 //for(v=0; v<NVOICES; v++) if(voice[v].note==note) //any voices playing that note?
115 //{
116 release(0);
117 //}
118 }
119 }
120
121
122 void mdaPianoVoice::release(unsigned char velocity)
123 {
124 if(sustain==0) {
125 //no release on highest notes
126 if(note < 94 || note == SUSTAIN) {
127 dec = (float)exp(-iFs * exp(2.0 + 0.017 * (double)note - 2.0 * *p(p_envelope_release)));
128 }
129 } else {
130 note = SUSTAIN;
131 }
132
133 //Mark the voice to be turned off later. It may not be set to
134 //INVALID_KEY yet, because the release sound still needs to be
135 //rendered. m_key is finally set to INVALID_KEY by 'render' when
136 //env < SILENCE
137 m_key = SUSTAIN;
138 }
139
140
141 void mdaPianoVoice::render(uint32_t from, uint32_t to)
142 {
143 // abort if no key is pressed
144 // initially m_key is INVALID_KEY, so no sound will be rendered
145 if (m_key == LV2::INVALID_KEY)
146 return;
147
148 float x, l, r;
149 uint32_t i;
150
151 for (uint32_t frame = from; frame < to; ++frame) {
152 l = r = 0.0f;
153
154 frac += delta; //integer-based linear interpolation
155 pos += frac >> 16;
156 frac &= 0xFFFF;
157 if(pos > end) pos -= loop;
158
159 i = waves[pos];
160 i = (i << 7) + (frac >> 9) * (waves[pos + 1] - i) + 0x40400000;
161 x = env * (*(float *)&i - 3.0f); //fast int->float
162
163 /////////////////////
164 //TODO: This was used in processReplacing instead of the above
165 /*
166 //i = (i << 7) + (frac >> 9) * (waves[pos + 1] - i) + 0x40400000; //not working on intel mac !?!
167 i = waves[pos] + ((frac * (waves[pos + 1] - waves[pos])) >> 16);
168 x = env * (float)i / 32768.0f;
169 //x = env * (*(float *)&i - 3.0f); //fast int->float
170 */
171 /////////////////////
172
173 env = env * dec; //envelope
174 f0 += ff * (x + f1 - f0); //muffle filter
175 f1 = x;
176
177 l += outl * f0;
178 r += outr * f0;
179
180 //TODO: this was used in processReplacing
181 /////////////////////
182 /*
183 if(!(l > -2.0f) || !(l < 2.0f))
184 {
185 printf("what is this shit? %d, %f, %f\n", i, x, f0);
186 l = 0.0f;
187 }
188 if(!(r > -2.0f) || !(r < 2.0f))
189 {
190 r = 0.0f;
191 }
192 */
193 /////////////////////
194 comb[cpos] = l + r;
195 ++cpos &= 0x7F;
196 x = cdep * comb[cpos]; //stereo simulator
197
198 // TODO: processReplacing simply assigned instead of adding
199 // write to output
200 p(p_left)[frame] += l + x;
201 p(p_right)[frame] += r - x;
202 }
203 }
204
205
206 void mdaPianoVoice::update(Param par)
207 {
208 size = (uint32_t)(12.0f * p_helper(p_hardness_offset, par) - 6.0f);
209 sizevel = 0.12f * p_helper(p_velocity_to_hardness, par);
210 muffvel = p_helper(p_velocity_to_muffling, par) * p_helper(p_velocity_to_muffling, par) * 5.0f;
211
212 velsens = 1.0f + p_helper(p_velocity_sensitivity, par) + p_helper(p_velocity_sensitivity, par);
213 if(p_helper(p_velocity_sensitivity, par) < 0.25f) velsens -= 0.75f - 3.0f * p_helper(p_velocity_sensitivity, par);
214
215 fine = p_helper(p_fine_tuning, par) - 0.5f;
216 random = 0.077f * p_helper(p_random_detuning, par) * p_helper(p_random_detuning, par);
217 stretch = 0.000434f * (p_helper(p_stretch_tuning, par) - 0.5f);
218
219 cdep = p_helper(p_stereo_width, par) * p_helper(p_stereo_width, par);
220 trim = 1.50f - 0.79f * cdep;
221 width = 0.04f * p_helper(p_stereo_width, par); if(width > 0.03f) width = 0.03f;
222
223 poly = 8 + (uint32_t)(24.9f * p_helper(p_polyphony, par));
224 }