remove program change handler
[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 "mdaPianoCommon.h"
17 #include "mdaPiano.h"
18
19 #include <stdio.h>
20 #include <math.h>
21
22 #define STRING_BUF 2048
23 static const char* sample_file = "samples.raw";
24
25
26 mdaPiano::mdaPiano(double rate)
27 : LV2::Synth<mdaPianoVoice, mdaPiano>(NPROGS, NPARAMS)
28 {
29 cmax = 0x7F; //just in case...
30
31 load_samples(&waves);
32 load_kgrp(kgrp);
33
34 notes[0] = EVENTS_DONE;
35 cpos = sustain = activevoices = 0;
36 comb = new float[256];
37
38 update();
39 suspend();
40 }
41
42
43 void mdaPiano::update() //parameter change
44 {
45 float * param = programs[curProgram].param;
46 size = (uint32_t)(12.0f * param[2] - 6.0f);
47 sizevel = 0.12f * param[3];
48 muffvel = param[5] * param[5] * 5.0f;
49
50 velsens = 1.0f + param[6] + param[6];
51 if(param[6] < 0.25f) velsens -= 0.75f - 3.0f * param[6];
52
53 fine = param[9] - 0.5f;
54 random = 0.077f * param[10] * param[10];
55 stretch = 0.000434f * (param[11] - 0.5f);
56
57 cdep = param[7] * param[7];
58 trim = 1.50f - 0.79f * cdep;
59 width = 0.04f * param[7]; if(width > 0.03f) width = 0.03f;
60
61 poly = 8 + (uint32_t)(24.9f * param[8]);
62 }
63
64
65 void mdaPiano::resume()
66 {
67 Fs = getSampleRate();
68 iFs = 1.0f / Fs;
69 if(Fs > 64000.0f) cmax = 0xFF; else cmax = 0x7F;
70 memset(comb, 0, sizeof(float) * 256);
71 }
72
73
74 mdaPiano::~mdaPiano () //destroy any buffers...
75 {
76 if(programs) delete [] programs;
77 if(comb) delete[] comb;
78 }
79
80
81 void mdaPiano::setParameter(uint32_t index, float value)
82 {
83 programs[curProgram].param[index] = value;
84 update();
85 }
86
87
88 void mdaPiano::process(float **inputs, float **outputs, uint32_t sampleFrames)
89 {
90 float* out0 = outputs[0];
91 float* out1 = outputs[1];
92 uint32_t event=0, frame=0, frames, v;
93 float x, l, r;
94 uint32_t i;
95
96 while(frame<sampleFrames)
97 {
98 frames = notes[event++];
99 if(frames>sampleFrames) frames = sampleFrames;
100 frames -= frame;
101 frame += frames;
102
103 while(--frames>=0)
104 {
105 VOICE *V = voice;
106 l = r = 0.0f;
107
108 for(v=0; v<activevoices; v++)
109 {
110 V->frac += V->delta; //integer-based linear interpolation
111 V->pos += V->frac >> 16;
112 V->frac &= 0xFFFF;
113 if(V->pos > V->end) V->pos -= V->loop;
114 i = waves[V->pos];
115 i = (i << 7) + (V->frac >> 9) * (waves[V->pos + 1] - i) + 0x40400000;
116 x = V->env * (*(float *)&i - 3.0f); //fast int->float
117
118 V->env = V->env * V->dec; //envelope
119 V->f0 += V->ff * (x + V->f1 - V->f0); //muffle filter
120 V->f1 = x;
121
122 l += V->outl * V->f0;
123 r += V->outr * V->f0;
124
125 V++;
126 }
127 comb[cpos] = l + r;
128 ++cpos &= cmax;
129 x = cdep * comb[cpos]; //stereo simulator
130
131 *out0++ += l + x;
132 *out1++ += r - x;
133 }
134
135 if(frame<sampleFrames)
136 {
137 uint32_t note = notes[event++];
138 uint32_t vel = notes[event++];
139 noteOn(note, vel);
140 }
141 }
142 for(v=0; v<activevoices; v++) if(voice[v].env < SILENCE) voice[v] = voice[--activevoices];
143 notes[0] = EVENTS_DONE; //mark events buffer as done
144 }
145
146
147 void mdaPiano::processReplacing(float **inputs, float **outputs, uint32_t sampleFrames)
148 {
149 float* out0 = outputs[0];
150 float* out1 = outputs[1];
151 uint32_t event=0, frame=0, frames, v;
152 float x, l, r;
153 uint32_t i;
154
155 while(frame<sampleFrames)
156 {
157 frames = notes[event++];
158 if(frames>sampleFrames) frames = sampleFrames;
159 frames -= frame;
160 frame += frames;
161
162 while(--frames>=0)
163 {
164 VOICE *V = voice;
165 l = r = 0.0f;
166
167 for(v=0; v<activevoices; v++)
168 {
169 V->frac += V->delta; //integer-based linear interpolation
170 V->pos += V->frac >> 16;
171 V->frac &= 0xFFFF;
172 if(V->pos > V->end) V->pos -= V->loop;
173 //i = (i << 7) + (V->frac >> 9) * (waves[V->pos + 1] - i) + 0x40400000; //not working on intel mac !?!
174 i = waves[V->pos] + ((V->frac * (waves[V->pos + 1] - waves[V->pos])) >> 16);
175 x = V->env * (float)i / 32768.0f;
176 //x = V->env * (*(float *)&i - 3.0f); //fast int->float
177
178 V->env = V->env * V->dec; //envelope
179 V->f0 += V->ff * (x + V->f1 - V->f0); //muffle filter
180 V->f1 = x;
181
182 l += V->outl * V->f0;
183 r += V->outr * V->f0;
184
185 if(!(l > -2.0f) || !(l < 2.0f))
186 {
187 printf("what is this shit? %d, %f, %f\n", i, x, V->f0);
188 l = 0.0f;
189 }
190 if(!(r > -2.0f) || !(r < 2.0f))
191 {
192 r = 0.0f;
193 }
194
195 V++;
196 }
197 comb[cpos] = l + r;
198 ++cpos &= cmax;
199 x = cdep * comb[cpos]; //stereo simulator
200
201 *out0++ = l + x;
202 *out1++ = r - x;
203 }
204
205 if(frame<sampleFrames)
206 {
207 uint32_t note = notes[event++];
208 uint32_t vel = notes[event++];
209 noteOn(note, vel);
210 }
211 }
212 for(v=0; v<activevoices; v++) if(voice[v].env < SILENCE) voice[v] = voice[--activevoices];
213 notes[0] = EVENTS_DONE; //mark events buffer as done
214 }
215
216
217 void mdaPiano::noteOn(uint32_t note, uint32_t velocity)
218 {
219 float * param = programs[curProgram].param;
220 float l=99.0f;
221 uint32_t v, vl=0, k, s;
222
223 if(velocity>0)
224 {
225 if(activevoices < poly) //add a note
226 {
227 vl = activevoices;
228 activevoices++;
229 }
230 else //steal a note
231 {
232 for(v=0; v<poly; v++) //find quietest voice
233 {
234 if(voice[v].env < l) { l = voice[v].env; vl = v; }
235 }
236 }
237
238 k = (note - 60) * (note - 60);
239 l = fine + random * ((float)(k % 13) - 6.5f); //random & fine tune
240 if(note > 60) l += stretch * (float)k; //stretch
241
242 s = size;
243 if(velocity > 40) s += (uint32_t)(sizevel * (float)(velocity - 40));
244
245 k = 0;
246 while(note > (kgrp[k].high + s)) k++; //find keygroup
247
248 l += (float)(note - kgrp[k].root); //pitch
249 l = 22050.0f * iFs * (float)exp(0.05776226505 * l);
250 voice[vl].delta = (uint32_t)(65536.0f * l);
251 voice[vl].frac = 0;
252 voice[vl].pos = kgrp[k].pos;
253 voice[vl].end = kgrp[k].end;
254 voice[vl].loop = kgrp[k].loop;
255
256 voice[vl].env = (0.5f + velsens) * (float)pow(0.0078f * velocity, velsens); //velocity
257
258 l = 50.0f + param[4] * param[4] * muff + muffvel * (float)(velocity - 64); //muffle
259 if(l < (55.0f + 0.25f * (float)note)) l = 55.0f + 0.25f * (float)note;
260 if(l > 210.0f) l = 210.0f;
261 voice[vl].ff = l * l * iFs;
262 voice[vl].f0 = voice[vl].f1 = 0.0f;
263
264 voice[vl].note = note; //note->pan
265 if(note < 12) note = 12;
266 if(note > 108) note = 108;
267 l = volume * trim;
268 voice[vl].outr = l + l * width * (float)(note - 60);
269 voice[vl].outl = l + l - voice[vl].outr;
270
271 if(note < 44) note = 44; //limit max decay length
272 l = 2.0f * param[0];
273 if(l < 1.0f) l += 0.25f - 0.5f * param[0];
274 voice[vl].dec = (float)exp(-iFs * exp(-0.6 + 0.033 * (double)note - l));
275 }
276 else //note off
277 {
278 for(v=0; v<NVOICES; v++) if(voice[v].note==note) //any voices playing that note?
279 {
280 if(sustain==0)
281 {
282 if(note < 94 || note == SUSTAIN) //no release on highest notes
283 voice[v].dec = (float)exp(-iFs * exp(2.0 + 0.017 * (double)note - 2.0 * param[1]));
284 }
285 else voice[v].note = SUSTAIN;
286 }
287 }
288 }
289
290
291 uint32_t mdaPiano::processEvents(VstEvents* ev)
292 {
293 uint32_t npos=0;
294
295 for (uint32_t i=0; i<ev->numEvents; i++)
296 {
297 if((ev->events[i])->type != kVstMidiType) continue;
298 VstMidiEvent* event = (VstMidiEvent*)ev->events[i];
299 char* midiData = event->midiData;
300
301 switch(midiData[0] & 0xf0) //status byte (all channels)
302 {
303 case 0x80: //note off
304 notes[npos++] = event->deltaFrames; //delta
305 notes[npos++] = midiData[1] & 0x7F; //note
306 notes[npos++] = 0; //vel
307 break;
308
309 case 0x90: //note on
310 notes[npos++] = event->deltaFrames; //delta
311 notes[npos++] = midiData[1] & 0x7F; //note
312 notes[npos++] = midiData[2] & 0x7F; //vel
313 break;
314
315 case 0xB0: //controller
316 switch(midiData[1])
317 {
318 case 0x01: //mod wheel
319 case 0x43: //soft pedal
320 muff = 0.01f * (float)((127 - midiData[2]) * (127 - midiData[2]));
321 break;
322
323 case 0x07: //volume
324 volume = 0.00002f * (float)(midiData[2] * midiData[2]);
325 break;
326
327 case 0x40: //sustain pedal
328 case 0x42: //sustenuto pedal
329 sustain = midiData[2] & 0x40;
330 if(sustain==0)
331 {
332 notes[npos++] = event->deltaFrames;
333 notes[npos++] = SUSTAIN; //end all sustained notes
334 notes[npos++] = 0;
335 }
336 break;
337
338 default: //all notes off
339 if(midiData[1]>0x7A)
340 {
341 for(uint32_t v=0; v<NVOICES; v++) voice[v].dec=0.99f;
342 sustain = 0;
343 muff = 160.0f;
344 }
345 break;
346 }
347 break;
348
349 default: break;
350 }
351
352 if(npos>EVENTBUFFER) npos -= 3; //discard events if buffer full!!
353 event++; //?
354 }
355 notes[npos] = EVENTS_DONE;
356 return 1;
357 }
358
359
360 // TODO: load this from a file
361 void mdaPiano::load_kgrp(KGRP *kgrp)
362 {
363 //Waveform data and keymapping is hard-wired in *this* version
364 kgrp[ 0].root = 36; kgrp[ 0].high = 37; kgrp[ 0].pos = 0; kgrp[ 0].end = 36275; kgrp[ 0].loop = 14774;
365 kgrp[ 1].root = 40; kgrp[ 1].high = 41; kgrp[ 1].pos = 36278; kgrp[ 1].end = 83135; kgrp[ 1].loop = 16268;
366 kgrp[ 2].root = 43; kgrp[ 2].high = 45; kgrp[ 2].pos = 83137; kgrp[ 2].end = 146756; kgrp[ 2].loop = 33541;
367 kgrp[ 3].root = 48; kgrp[ 3].high = 49; kgrp[ 3].pos = 146758; kgrp[ 3].end = 204997; kgrp[ 3].loop = 21156;
368 kgrp[ 4].root = 52; kgrp[ 4].high = 53; kgrp[ 4].pos = 204999; kgrp[ 4].end = 244908; kgrp[ 4].loop = 17191;
369 kgrp[ 5].root = 55; kgrp[ 5].high = 57; kgrp[ 5].pos = 244910; kgrp[ 5].end = 290978; kgrp[ 5].loop = 23286;
370 kgrp[ 6].root = 60; kgrp[ 6].high = 61; kgrp[ 6].pos = 290980; kgrp[ 6].end = 342948; kgrp[ 6].loop = 18002;
371 kgrp[ 7].root = 64; kgrp[ 7].high = 65; kgrp[ 7].pos = 342950; kgrp[ 7].end = 391750; kgrp[ 7].loop = 19746;
372 kgrp[ 8].root = 67; kgrp[ 8].high = 69; kgrp[ 8].pos = 391752; kgrp[ 8].end = 436915; kgrp[ 8].loop = 22253;
373 kgrp[ 9].root = 72; kgrp[ 9].high = 73; kgrp[ 9].pos = 436917; kgrp[ 9].end = 468807; kgrp[ 9].loop = 8852;
374 kgrp[10].root = 76; kgrp[10].high = 77; kgrp[10].pos = 468809; kgrp[10].end = 492772; kgrp[10].loop = 9693;
375 kgrp[11].root = 79; kgrp[11].high = 81; kgrp[11].pos = 492774; kgrp[11].end = 532293; kgrp[11].loop = 10596;
376 kgrp[12].root = 84; kgrp[12].high = 85; kgrp[12].pos = 532295; kgrp[12].end = 560192; kgrp[12].loop = 6011;
377 kgrp[13].root = 88; kgrp[13].high = 89; kgrp[13].pos = 560194; kgrp[13].end = 574121; kgrp[13].loop = 3414;
378 kgrp[14].root = 93; kgrp[14].high = 999; kgrp[14].pos = 574123; kgrp[14].end = 586343; kgrp[14].loop = 2399;
379 }
380
381
382 void mdaPiano::load_samples(short **buffer)
383 {
384 FILE *f;
385 long num, size;
386 char filepath[STRING_BUF];
387
388 strncpy(filepath, bundle_path(), STRING_BUF);
389 strncat(filepath,
390 sample_file,
391 STRING_BUF - strlen(filepath));
392 f = fopen(filepath, "rb");
393 if (f == NULL) {
394 fputs("File error", stderr);
395 exit(1);
396 }
397
398 // obtain file size
399 fseek(f, 0, SEEK_END);
400 size = ftell(f);
401 rewind(f);
402
403 // allocate memory to contain the whole file
404 *buffer = (short*) malloc (sizeof(short)*size);
405 if (*buffer == NULL) {
406 fputs("Memory error", stderr);
407 exit(2);
408 }
409
410 // copy the file into the buffer
411 num = fread(*buffer, 1, size, f);
412 if (num != size) {
413 fputs ("Reading error", stderr);
414 exit (3);
415 }
416 fclose (f);
417 return;
418 }