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