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