remove VST functions
[software/lv2-mdametapiano.git] / src / mdaPiano.cpp
1 /* ==================================================
2 * LV2 port of the famous mda Piano VSTi
3 * ==================================================
4 *
5 * Port
6 * Author: Ricardo Wurmus (rekado)
7 * Based on: mda-vst-src-2010-02-14.zip
8 *
9 * mda Piano v1.0
10 * Copyright(c)1999-2000 Paul Kellett (maxim digital audio)
11 * Based on VST2 SDK (c)1996-1999 Steinberg Soft und Hardware GmbH, All Rights Reserved
12 *
13 * ==================================================
14 */
15
16 #include "mdaPianoData.h"
17 #include "mdaPiano.h"
18
19 #include <stdio.h>
20 #include <math.h>
21
22
23 AudioEffect *createEffectInstance(audioMasterCallback audioMaster)
24 {
25 return new mdaPiano(audioMaster);
26 }
27
28 mdaPianoProgram::mdaPianoProgram()
29 {
30 param[0] = 0.50f; //Decay
31 param[1] = 0.50f; //Release
32 param[2] = 0.50f; //Hardness
33
34 param[3] = 0.50f; //Vel>Hard
35 param[4] = 1.00f; //Muffle
36 param[5] = 0.50f; //Vel>Muff
37
38 param[6] = 0.33f; //Vel Curve
39 param[7] = 0.50f; //Stereo
40 param[8] = 0.33f; //Max Poly
41
42 param[9] = 0.50f; //Tune
43 param[10] = 0.00f; //Random
44 param[11] = 0.50f; //Stretch
45
46 strcpy (name, "mda Piano");
47 }
48
49
50 mdaPiano::mdaPiano(audioMasterCallback audioMaster) : AudioEffectX(audioMaster, NPROGS, NPARAMS)
51 {
52 Fs = 44100.0f; iFs = 1.0f/Fs; cmax = 0x7F; //just in case...
53
54 programs = new mdaPianoProgram[NPROGS];
55 if(programs)
56 {
57 //fill patches...
58 VstInt32 i=0;
59 //TODO: load initial values from default preset
60 fillpatch(i++, "mda Piano", 0.500f, 0.500f, 0.500f, 0.5f, 0.803f, 0.251f, 0.376f, 0.500f, 0.330f, 0.500f, 0.246f, 0.500f);
61 setProgram(0);
62 }
63
64 if(audioMaster)
65 {
66 setNumInputs(0);
67 setNumOutputs(NOUTS);
68 canProcessReplacing();
69 isSynth();
70 setUniqueID('MDAp'); ///
71 }
72
73 waves = pianoData;
74
75
76 //Waveform data and keymapping is hard-wired in *this* version
77 kgrp[ 0].root = 36; kgrp[ 0].high = 37; kgrp[ 0].pos = 0; kgrp[ 0].end = 36275; kgrp[ 0].loop = 14774;
78 kgrp[ 1].root = 40; kgrp[ 1].high = 41; kgrp[ 1].pos = 36278; kgrp[ 1].end = 83135; kgrp[ 1].loop = 16268;
79 kgrp[ 2].root = 43; kgrp[ 2].high = 45; kgrp[ 2].pos = 83137; kgrp[ 2].end = 146756; kgrp[ 2].loop = 33541;
80 kgrp[ 3].root = 48; kgrp[ 3].high = 49; kgrp[ 3].pos = 146758; kgrp[ 3].end = 204997; kgrp[ 3].loop = 21156;
81 kgrp[ 4].root = 52; kgrp[ 4].high = 53; kgrp[ 4].pos = 204999; kgrp[ 4].end = 244908; kgrp[ 4].loop = 17191;
82 kgrp[ 5].root = 55; kgrp[ 5].high = 57; kgrp[ 5].pos = 244910; kgrp[ 5].end = 290978; kgrp[ 5].loop = 23286;
83 kgrp[ 6].root = 60; kgrp[ 6].high = 61; kgrp[ 6].pos = 290980; kgrp[ 6].end = 342948; kgrp[ 6].loop = 18002;
84 kgrp[ 7].root = 64; kgrp[ 7].high = 65; kgrp[ 7].pos = 342950; kgrp[ 7].end = 391750; kgrp[ 7].loop = 19746;
85 kgrp[ 8].root = 67; kgrp[ 8].high = 69; kgrp[ 8].pos = 391752; kgrp[ 8].end = 436915; kgrp[ 8].loop = 22253;
86 kgrp[ 9].root = 72; kgrp[ 9].high = 73; kgrp[ 9].pos = 436917; kgrp[ 9].end = 468807; kgrp[ 9].loop = 8852;
87 kgrp[10].root = 76; kgrp[10].high = 77; kgrp[10].pos = 468809; kgrp[10].end = 492772; kgrp[10].loop = 9693;
88 kgrp[11].root = 79; kgrp[11].high = 81; kgrp[11].pos = 492774; kgrp[11].end = 532293; kgrp[11].loop = 10596;
89 kgrp[12].root = 84; kgrp[12].high = 85; kgrp[12].pos = 532295; kgrp[12].end = 560192; kgrp[12].loop = 6011;
90 kgrp[13].root = 88; kgrp[13].high = 89; kgrp[13].pos = 560194; kgrp[13].end = 574121; kgrp[13].loop = 3414;
91 kgrp[14].root = 93; kgrp[14].high = 999; kgrp[14].pos = 574123; kgrp[14].end = 586343; kgrp[14].loop = 2399;
92
93 //initialise...
94 for(VstInt32 v=0; v<NVOICES; v++)
95 {
96 voice[v].env = 0.0f;
97 voice[v].dec = 0.99f; //all notes off
98 }
99 notes[0] = EVENTS_DONE;
100 volume = 0.2f;
101 muff = 160.0f;
102 cpos = sustain = activevoices = 0;
103 comb = new float[256];
104
105 update();
106 suspend();
107 }
108
109
110 void mdaPiano::update() //parameter change
111 {
112 float * param = programs[curProgram].param;
113 size = (VstInt32)(12.0f * param[2] - 6.0f);
114 sizevel = 0.12f * param[3];
115 muffvel = param[5] * param[5] * 5.0f;
116
117 velsens = 1.0f + param[6] + param[6];
118 if(param[6] < 0.25f) velsens -= 0.75f - 3.0f * param[6];
119
120 fine = param[9] - 0.5f;
121 random = 0.077f * param[10] * param[10];
122 stretch = 0.000434f * (param[11] - 0.5f);
123
124 cdep = param[7] * param[7];
125 trim = 1.50f - 0.79f * cdep;
126 width = 0.04f * param[7]; if(width > 0.03f) width = 0.03f;
127
128 poly = 8 + (VstInt32)(24.9f * param[8]);
129 }
130
131
132 void mdaPiano::resume()
133 {
134 Fs = getSampleRate();
135 iFs = 1.0f / Fs;
136 if(Fs > 64000.0f) cmax = 0xFF; else cmax = 0x7F;
137 memset(comb, 0, sizeof(float) * 256);
138
139 DECLARE_VST_DEPRECATED (wantEvents) ();
140 }
141
142
143 mdaPiano::~mdaPiano () //destroy any buffers...
144 {
145 if(programs) delete [] programs;
146 if(comb) delete[] comb;
147 }
148
149
150 void mdaPiano::setProgram(VstInt32 program)
151 {
152 curProgram = program;
153 update();
154 }
155
156
157 void mdaPiano::setParameter(VstInt32 index, float value)
158 {
159 programs[curProgram].param[index] = value;
160 update();
161 }
162
163
164 void mdaPiano::fillpatch(VstInt32 p, char *name, float p0, float p1, float p2, float p3, float p4,
165 float p5, float p6, float p7, float p8, float p9, float p10,float p11)
166 {
167 strcpy(programs[p].name, name);
168 programs[p].param[0] = p0; programs[p].param[1] = p1;
169 programs[p].param[2] = p2; programs[p].param[3] = p3;
170 programs[p].param[4] = p4; programs[p].param[5] = p5;
171 programs[p].param[6] = p6; programs[p].param[7] = p7;
172 programs[p].param[8] = p8; programs[p].param[9] = p9;
173 programs[p].param[10]= p10; programs[p].param[11] = p11;
174 }
175
176
177 float mdaPiano::getParameter(VstInt32 index) { return programs[curProgram].param[index]; }
178 void mdaPiano::setProgramName(char *name) { strcpy(programs[curProgram].name, name); }
179 void mdaPiano::getProgramName(char *name) { strcpy(name, programs[curProgram].name); }
180 void mdaPiano::setBlockSize(VstInt32 blockSize) { AudioEffectX::setBlockSize(blockSize); }
181
182
183 bool mdaPiano::copyProgram(VstInt32 destination)
184 {
185 if(destination<NPROGS)
186 {
187 programs[destination] = programs[curProgram];
188 return true;
189 }
190 return false;
191 }
192
193
194 void mdaPiano::getParameterName(VstInt32 index, char *label)
195 {
196 switch (index)
197 {
198 case 0: strcpy(label, "Envelope Decay"); break;
199 case 1: strcpy(label, "Envelope Release"); break;
200 case 2: strcpy(label, "Hardness Offset"); break;
201
202 case 3: strcpy(label, "Velocity to Hardness"); break;
203 case 4: strcpy(label, "Muffling Filter"); break;
204 case 5: strcpy(label, "Velocity to Muffling"); break;
205
206 case 6: strcpy(label, "Velocity Sensitivity"); break;
207 case 7: strcpy(label, "Stereo Width"); break;
208 case 8: strcpy(label, "Polyphony"); break;
209
210 case 9: strcpy(label, "Fine Tuning"); break;
211 case 10: strcpy(label, "Random Detuning"); break;
212 default: strcpy(label, "Stretch Tuning"); break;
213 }
214 }
215
216
217 void mdaPiano::getParameterDisplay(VstInt32 index, char *text)
218 {
219 char string[16];
220 float * param = programs[curProgram].param;
221
222 switch(index)
223 {
224 case 4: sprintf(string, "%.0f", 100.0f - 100.0f * param[index]); break;
225 case 7: sprintf(string, "%.0f", 200.0f * param[index]); break;
226 case 8: sprintf(string, "%d", poly); break;
227 case 10: sprintf(string, "%.1f", 50.0f * param[index] * param[index]); break;
228 case 2:
229 case 9:
230 case 11: sprintf(string, "%+.1f", 100.0f * param[index] - 50.0f); break;
231 default: sprintf(string, "%.0f", 100.0f * param[index]);
232 }
233 string[8] = 0;
234 strcpy(text, (char *)string);
235 }
236
237
238 void mdaPiano::getParameterLabel(VstInt32 index, char *label)
239 {
240 switch(index)
241 {
242 case 8: strcpy(label, "voices"); break;
243 case 9:
244 case 10:
245 case 11: strcpy(label, "cents"); break;
246 default: strcpy(label, "%");
247 }
248 }
249
250
251 void mdaPiano::process(float **inputs, float **outputs, VstInt32 sampleFrames)
252 {
253 float* out0 = outputs[0];
254 float* out1 = outputs[1];
255 VstInt32 event=0, frame=0, frames, v;
256 float x, l, r;
257 VstInt32 i;
258
259 while(frame<sampleFrames)
260 {
261 frames = notes[event++];
262 if(frames>sampleFrames) frames = sampleFrames;
263 frames -= frame;
264 frame += frames;
265
266 while(--frames>=0)
267 {
268 VOICE *V = voice;
269 l = r = 0.0f;
270
271 for(v=0; v<activevoices; v++)
272 {
273 V->frac += V->delta; //integer-based linear interpolation
274 V->pos += V->frac >> 16;
275 V->frac &= 0xFFFF;
276 if(V->pos > V->end) V->pos -= V->loop;
277 i = waves[V->pos];
278 i = (i << 7) + (V->frac >> 9) * (waves[V->pos + 1] - i) + 0x40400000;
279 x = V->env * (*(float *)&i - 3.0f); //fast int->float
280
281 V->env = V->env * V->dec; //envelope
282 V->f0 += V->ff * (x + V->f1 - V->f0); //muffle filter
283 V->f1 = x;
284
285 l += V->outl * V->f0;
286 r += V->outr * V->f0;
287
288 V++;
289 }
290 comb[cpos] = l + r;
291 ++cpos &= cmax;
292 x = cdep * comb[cpos]; //stereo simulator
293
294 *out0++ += l + x;
295 *out1++ += r - x;
296 }
297
298 if(frame<sampleFrames)
299 {
300 VstInt32 note = notes[event++];
301 VstInt32 vel = notes[event++];
302 noteOn(note, vel);
303 }
304 }
305 for(v=0; v<activevoices; v++) if(voice[v].env < SILENCE) voice[v] = voice[--activevoices];
306 notes[0] = EVENTS_DONE; //mark events buffer as done
307 }
308
309
310 void mdaPiano::processReplacing(float **inputs, float **outputs, VstInt32 sampleFrames)
311 {
312 float* out0 = outputs[0];
313 float* out1 = outputs[1];
314 VstInt32 event=0, frame=0, frames, v;
315 float x, l, r;
316 VstInt32 i;
317
318 while(frame<sampleFrames)
319 {
320 frames = notes[event++];
321 if(frames>sampleFrames) frames = sampleFrames;
322 frames -= frame;
323 frame += frames;
324
325 while(--frames>=0)
326 {
327 VOICE *V = voice;
328 l = r = 0.0f;
329
330 for(v=0; v<activevoices; v++)
331 {
332 V->frac += V->delta; //integer-based linear interpolation
333 V->pos += V->frac >> 16;
334 V->frac &= 0xFFFF;
335 if(V->pos > V->end) V->pos -= V->loop;
336 //i = (i << 7) + (V->frac >> 9) * (waves[V->pos + 1] - i) + 0x40400000; //not working on intel mac !?!
337 i = waves[V->pos] + ((V->frac * (waves[V->pos + 1] - waves[V->pos])) >> 16);
338 x = V->env * (float)i / 32768.0f;
339 //x = V->env * (*(float *)&i - 3.0f); //fast int->float
340
341 V->env = V->env * V->dec; //envelope
342 V->f0 += V->ff * (x + V->f1 - V->f0); //muffle filter
343 V->f1 = x;
344
345 l += V->outl * V->f0;
346 r += V->outr * V->f0;
347
348 if(!(l > -2.0f) || !(l < 2.0f))
349 {
350 printf("what is this shit? %d, %f, %f\n", i, x, V->f0);
351 l = 0.0f;
352 }
353 if(!(r > -2.0f) || !(r < 2.0f))
354 {
355 r = 0.0f;
356 }
357
358 V++;
359 }
360 comb[cpos] = l + r;
361 ++cpos &= cmax;
362 x = cdep * comb[cpos]; //stereo simulator
363
364 *out0++ = l + x;
365 *out1++ = r - x;
366 }
367
368 if(frame<sampleFrames)
369 {
370 VstInt32 note = notes[event++];
371 VstInt32 vel = notes[event++];
372 noteOn(note, vel);
373 }
374 }
375 for(v=0; v<activevoices; v++) if(voice[v].env < SILENCE) voice[v] = voice[--activevoices];
376 notes[0] = EVENTS_DONE; //mark events buffer as done
377 }
378
379
380 void mdaPiano::noteOn(VstInt32 note, VstInt32 velocity)
381 {
382 float * param = programs[curProgram].param;
383 float l=99.0f;
384 VstInt32 v, vl=0, k, s;
385
386 if(velocity>0)
387 {
388 if(activevoices < poly) //add a note
389 {
390 vl = activevoices;
391 activevoices++;
392 }
393 else //steal a note
394 {
395 for(v=0; v<poly; v++) //find quietest voice
396 {
397 if(voice[v].env < l) { l = voice[v].env; vl = v; }
398 }
399 }
400
401 k = (note - 60) * (note - 60);
402 l = fine + random * ((float)(k % 13) - 6.5f); //random & fine tune
403 if(note > 60) l += stretch * (float)k; //stretch
404
405 s = size;
406 if(velocity > 40) s += (VstInt32)(sizevel * (float)(velocity - 40));
407
408 k = 0;
409 while(note > (kgrp[k].high + s)) k++; //find keygroup
410
411 l += (float)(note - kgrp[k].root); //pitch
412 l = 22050.0f * iFs * (float)exp(0.05776226505 * l);
413 voice[vl].delta = (VstInt32)(65536.0f * l);
414 voice[vl].frac = 0;
415 voice[vl].pos = kgrp[k].pos;
416 voice[vl].end = kgrp[k].end;
417 voice[vl].loop = kgrp[k].loop;
418
419 voice[vl].env = (0.5f + velsens) * (float)pow(0.0078f * velocity, velsens); //velocity
420
421 l = 50.0f + param[4] * param[4] * muff + muffvel * (float)(velocity - 64); //muffle
422 if(l < (55.0f + 0.25f * (float)note)) l = 55.0f + 0.25f * (float)note;
423 if(l > 210.0f) l = 210.0f;
424 voice[vl].ff = l * l * iFs;
425 voice[vl].f0 = voice[vl].f1 = 0.0f;
426
427 voice[vl].note = note; //note->pan
428 if(note < 12) note = 12;
429 if(note > 108) note = 108;
430 l = volume * trim;
431 voice[vl].outr = l + l * width * (float)(note - 60);
432 voice[vl].outl = l + l - voice[vl].outr;
433
434 if(note < 44) note = 44; //limit max decay length
435 l = 2.0f * param[0];
436 if(l < 1.0f) l += 0.25f - 0.5f * param[0];
437 voice[vl].dec = (float)exp(-iFs * exp(-0.6 + 0.033 * (double)note - l));
438 }
439 else //note off
440 {
441 for(v=0; v<NVOICES; v++) if(voice[v].note==note) //any voices playing that note?
442 {
443 if(sustain==0)
444 {
445 if(note < 94 || note == SUSTAIN) //no release on highest notes
446 voice[v].dec = (float)exp(-iFs * exp(2.0 + 0.017 * (double)note - 2.0 * param[1]));
447 }
448 else voice[v].note = SUSTAIN;
449 }
450 }
451 }
452
453
454 VstInt32 mdaPiano::processEvents(VstEvents* ev)
455 {
456 VstInt32 npos=0;
457
458 for (VstInt32 i=0; i<ev->numEvents; i++)
459 {
460 if((ev->events[i])->type != kVstMidiType) continue;
461 VstMidiEvent* event = (VstMidiEvent*)ev->events[i];
462 char* midiData = event->midiData;
463
464 switch(midiData[0] & 0xf0) //status byte (all channels)
465 {
466 case 0x80: //note off
467 notes[npos++] = event->deltaFrames; //delta
468 notes[npos++] = midiData[1] & 0x7F; //note
469 notes[npos++] = 0; //vel
470 break;
471
472 case 0x90: //note on
473 notes[npos++] = event->deltaFrames; //delta
474 notes[npos++] = midiData[1] & 0x7F; //note
475 notes[npos++] = midiData[2] & 0x7F; //vel
476 break;
477
478 case 0xB0: //controller
479 switch(midiData[1])
480 {
481 case 0x01: //mod wheel
482 case 0x43: //soft pedal
483 muff = 0.01f * (float)((127 - midiData[2]) * (127 - midiData[2]));
484 break;
485
486 case 0x07: //volume
487 volume = 0.00002f * (float)(midiData[2] * midiData[2]);
488 break;
489
490 case 0x40: //sustain pedal
491 case 0x42: //sustenuto pedal
492 sustain = midiData[2] & 0x40;
493 if(sustain==0)
494 {
495 notes[npos++] = event->deltaFrames;
496 notes[npos++] = SUSTAIN; //end all sustained notes
497 notes[npos++] = 0;
498 }
499 break;
500
501 default: //all notes off
502 if(midiData[1]>0x7A)
503 {
504 for(VstInt32 v=0; v<NVOICES; v++) voice[v].dec=0.99f;
505 sustain = 0;
506 muff = 160.0f;
507 }
508 break;
509 }
510 break;
511
512 case 0xC0: //program change
513 if(midiData[1]<NPROGS) setProgram(midiData[1]);
514 break;
515
516 default: break;
517 }
518
519 if(npos>EVENTBUFFER) npos -= 3; //discard events if buffer full!!
520 event++; //?
521 }
522 notes[npos] = EVENTS_DONE;
523 return 1;
524 }
525