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