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