remove voice[vl] from `mdaPianoVoice::on`
[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 comb = new float[256];
25 reset();
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 // TODO: replace with this voice's local copy
54 float * param = programs[curProgram].param;
55 float l=99.0f;
56 uint32_t v, k, s;
57
58 if(velocity>0)
59 {
60 // TODO: move this to mdaPiano.cpp
61 /*
62 if(activevoices < poly) //add a note
63 {
64 vl = activevoices;
65 activevoices++;
66 }
67 else //steal a note
68 {
69 for(v=0; v<poly; v++) //find quietest voice
70 {
71 if(voice[v].env < l) { l = voice[v].env; vl = v; }
72 }
73 }
74 */
75
76 k = (note - 60) * (note - 60);
77 l = fine + random * ((float)(k % 13) - 6.5f); //random & fine tune
78 if(note > 60) l += stretch * (float)k; //stretch
79
80 s = size;
81 if(velocity > 40) s += (uint32_t)(sizevel * (float)(velocity - 40));
82
83 k = 0;
84 while(note > (kgrp[k].high + s)) k++; //find keygroup
85
86 l += (float)(note - kgrp[k].root); //pitch
87 l = 22050.0f * iFs * (float)exp(0.05776226505 * l);
88 delta = (uint32_t)(65536.0f * l);
89 frac = 0;
90 pos = kgrp[k].pos;
91 end = kgrp[k].end;
92 loop = kgrp[k].loop;
93
94 env = (0.5f + velsens) * (float)pow(0.0078f * velocity, velsens); //velocity
95
96 l = 50.0f + param[4] * param[4] * muff + muffvel * (float)(velocity - 64); //muffle
97 if(l < (55.0f + 0.25f * (float)note)) l = 55.0f + 0.25f * (float)note;
98 if(l > 210.0f) l = 210.0f;
99 ff = l * l * iFs;
100 f0 = f1 = 0.0f;
101
102 if(note < 12) note = 12;
103 if(note > 108) note = 108;
104 l = volume * trim;
105 outr = l + l * width * (float)(note - 60);
106 outl = l + l - outr;
107
108 if(note < 44) note = 44; //limit max decay length
109 l = 2.0f * param[0];
110 if(l < 1.0f) l += 0.25f - 0.5f * param[0];
111 dec = (float)exp(-iFs * exp(-0.6 + 0.033 * (double)note - l));
112 }
113 else //note off
114 {
115 // TODO: move the loop to mdaPiano.cpp
116 //for(v=0; v<NVOICES; v++) if(voice[v].note==note) //any voices playing that note?
117 //{
118 release(0);
119 //}
120 }
121 }
122
123
124 void mdaPianoVoice::release(unsigned char velocity)
125 {
126 if(sustain==0) {
127 //no release on highest notes
128 if(note < 94 || note == SUSTAIN) {
129 dec = (float)exp(-iFs * exp(2.0 + 0.017 * (double)note - 2.0 * *p(p_envelope_release)));
130 }
131 } else {
132 note = SUSTAIN;
133 }
134
135 //Mark the voice to be turned off later. It may not be set to
136 //INVALID_KEY yet, because the release sound still needs to be
137 //rendered. m_key is finally set to INVALID_KEY by 'render' when
138 //env < SILENCE
139 m_key = SUSTAIN;
140 }
141
142
143 void mdaPianoVoice::process(float **inputs, float **outputs, uint32_t sampleFrames)
144 {
145 float* out0 = outputs[0];
146 float* out1 = outputs[1];
147 uint32_t event=0, frame=0, frames, v;
148 float x, l, r;
149 uint32_t i;
150
151 while(frame<sampleFrames)
152 {
153 frames = notes[event++];
154 if(frames>sampleFrames) frames = sampleFrames;
155 frames -= frame;
156 frame += frames;
157
158 while(--frames>=0)
159 {
160 VOICE *V = voice;
161 l = r = 0.0f;
162
163 for(v=0; v<activevoices; v++)
164 {
165 V->frac += V->delta; //integer-based linear interpolation
166 V->pos += V->frac >> 16;
167 V->frac &= 0xFFFF;
168 if(V->pos > V->end) V->pos -= V->loop;
169
170 i = waves[V->pos];
171 i = (i << 7) + (V->frac >> 9) * (waves[V->pos + 1] - i) + 0x40400000;
172 x = V->env * (*(float *)&i - 3.0f); //fast int->float
173
174 /////////////////////
175 //TODO: This was used in processReplacing instead of the above
176 /*
177 //i = (i << 7) + (V->frac >> 9) * (waves[V->pos + 1] - i) + 0x40400000; //not working on intel mac !?!
178 i = waves[V->pos] + ((V->frac * (waves[V->pos + 1] - waves[V->pos])) >> 16);
179 x = V->env * (float)i / 32768.0f;
180 //x = V->env * (*(float *)&i - 3.0f); //fast int->float
181 */
182 /////////////////////
183
184 V->env = V->env * V->dec; //envelope
185 V->f0 += V->ff * (x + V->f1 - V->f0); //muffle filter
186 V->f1 = x;
187
188 l += V->outl * V->f0;
189 r += V->outr * V->f0;
190
191 //TODO: this was used in processReplacing
192 /////////////////////
193 /*
194 if(!(l > -2.0f) || !(l < 2.0f))
195 {
196 printf("what is this shit? %d, %f, %f\n", i, x, V->f0);
197 l = 0.0f;
198 }
199 if(!(r > -2.0f) || !(r < 2.0f))
200 {
201 r = 0.0f;
202 }
203 */
204 /////////////////////
205
206 V++;
207 }
208 comb[cpos] = l + r;
209 ++cpos &= cmax;
210 x = cdep * comb[cpos]; //stereo simulator
211
212 // TODO: processReplacing simply assigned instead of adding
213 *out0++ += l + x;
214 *out1++ += r - x;
215 }
216
217 if(frame<sampleFrames)
218 {
219 uint32_t note = notes[event++];
220 uint32_t vel = notes[event++];
221 noteOn(note, vel);
222 }
223 }
224 for(v=0; v<activevoices; v++) if(voice[v].env < SILENCE) voice[v] = voice[--activevoices];
225 notes[0] = EVENTS_DONE; //mark events buffer as done
226 }
227
228
229 void mdaPianoVoice::update(Param par)
230 {
231 //TODO: use local copy
232 float * param = programs[curProgram].param;
233 size = (uint32_t)(12.0f * param[2] - 6.0f);
234 sizevel = 0.12f * param[3];
235 muffvel = param[5] * param[5] * 5.0f;
236
237 velsens = 1.0f + param[6] + param[6];
238 if(param[6] < 0.25f) velsens -= 0.75f - 3.0f * param[6];
239
240 fine = param[9] - 0.5f;
241 random = 0.077f * param[10] * param[10];
242 stretch = 0.000434f * (param[11] - 0.5f);
243
244 cdep = param[7] * param[7];
245 trim = 1.50f - 0.79f * cdep;
246 width = 0.04f * param[7]; if(width > 0.03f) width = 0.03f;
247
248 poly = 8 + (uint32_t)(24.9f * param[8]);
249 }