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