name peg file after TYPE
[software/lv2-mdametapiano.git] / src / mdaPiano.cpp
1 /* ======================================================
2 * LV2 port of the popular mda Piano and mda EPiano VSTi
3 * ======================================================
4 *
5 * Port
6 * Author: Ricardo Wurmus (rekado)
7 * Based on: mda-vst-src-2010-02-14.zip
8 *
9 * mda Piano / mda EPiano
10 * Copyright(c)1999-2000 Paul Kellett (maxim digital audio)
11 *
12 * ==================================================
13 */
14
15 #include "mdaPianoCommon.h"
16 #include "mdaPiano.h"
17 #include <cstdlib> //for exit
18
19 #define STRING_BUF 2048
20
21 mdaPiano::mdaPiano(double rate)
22 : lvtk::Synth<mdaPianoVoice, mdaPiano>(p_n_ports, p_midi) {
23
24 sustain = 0;
25
26 #ifdef PIANO
27 static const char* sample_names[] =
28 { "0c.raw", "0e.raw", "0g.raw"
29 , "1c.raw", "1e.raw", "1g.raw"
30 , "2c.raw", "2e.raw", "2g.raw"
31 , "3c.raw", "3e.raw", "3g.raw"
32 , "4c.raw", "4e.raw", "4a.raw"
33 };
34 #elif defined EPIANO
35 static const char* sample_names[] =
36 { "0c-0.raw", "0c-1.raw", "0c-2.raw"
37 , "0g-0.raw", "0g-1.raw", "0g-2.raw"
38 , "1c-0.raw", "1c-1.raw", "1c-2.raw"
39 , "1g-0.raw", "1g-1.raw", "1g-2.raw"
40 , "2c-0.raw", "2c-1.raw", "2c-2.raw"
41 , "2g-0.raw", "2g-1.raw", "2g-2.raw"
42 , "3c-0.raw", "3c-1.raw", "3c-2.raw"
43 , "3g-0.raw", "3g-1.raw", "3g-2.raw"
44 , "4c-0.raw", "4c-1.raw", "4c-2.raw"
45 , "4g-0.raw", "4g-1.raw", "4g-2.raw"
46 , "5c-0.raw", "5c-1.raw", "5c-2.raw"
47 };
48 #endif
49
50 load_kgrp(kgrp);
51 for (unsigned char i=0; i<NSAMPLES; ++i) {
52 load_sample(&samples[i], sample_names[i]);
53 #ifdef EPIANO
54 tweak_sample(&samples[i], kgrp[i].loop);
55 #endif
56 }
57
58 for(uint32_t i=0; i<NVOICES; ++i) {
59 voices[i] = new mdaPianoVoice(rate, samples, kgrp);
60 add_voices(voices[i]);
61 }
62
63 add_audio_outputs(p_left, p_right);
64 }
65
66
67 unsigned mdaPiano::find_free_voice(unsigned char key, unsigned char velocity) {
68 //is this a retriggered note during sustain?
69 if (sustain) {
70 for (unsigned i = 0; i < NVOICES; ++i) {
71 if ((voices[i]->get_key() == key) && (voices[i]->is_sustained())) {
72 return i;
73 }
74 }
75 }
76
77 //take the next free voice if
78 // ... notes are sustained but not this new one
79 // ... notes are not sustained
80 for (unsigned i = 0; i < NVOICES; ++i) {
81 if (voices[i]->get_key() == lvtk::INVALID_KEY)
82 {
83 return i;
84 }
85 }
86
87 //TODO: steal quietest note if all voices are used up
88 return 0;
89 }
90
91
92 void mdaPiano::setVolume(float value) {
93 for (uint32_t v=0; v<NVOICES; ++v)
94 voices[v]->set_volume(value);
95 }
96
97
98 //parameter change
99 void mdaPiano::update() {
100 for (uint32_t v=0; v<NVOICES; ++v) {
101 voices[v]->update(Current);
102 }
103 }
104
105
106 void mdaPiano::handle_midi(uint32_t size, unsigned char* data) {
107 #ifdef DEBUG
108 printf("%d\n", data[1]);
109 #endif
110
111 //discard invalid midi messages
112 if (size != 3)
113 return;
114
115 //receive on all channels
116 switch(data[0] & 0xf0)
117 {
118 case 0x80: //note off
119 for (unsigned i = 0; i < NVOICES; ++i) {
120 if (voices[i]->get_key() == data[1]) {
121 voices[i]->release(data[2]);
122 break;
123 }
124 }
125 break;
126
127 case 0x90: //note on
128 voices[ find_free_voice(data[1], data[2]) ]->on(data[1], data[2]);
129 break;
130
131 case 0xE0: break; //TODO: pitch bend
132
133 //controller
134 case 0xB0:
135 switch(data[1])
136 {
137 #ifdef PIANO
138 case 0x01: //mod wheel
139 case 0x43: //soft pedal
140 {
141 float muff = 0.01f * (float)((127 - data[2]) * (127 - data[2]));
142 for (unsigned i = 0; i < NVOICES; ++i) {
143 voices[i]->set_muff(muff);
144 }
145 break;
146 }
147 #elif defined EPIANO
148 case 0x01: //mod wheel
149 //scale the mod value to cover the range [0..1]
150 *p(p_modulation) = scale_midi_to_f(data[2]);
151 break;
152 #endif
153
154 case 0x07: //volume
155 setVolume(0.00002f * (float)(data[2] * data[2]));
156 break;
157
158 case 0x40: //sustain pedal
159 case 0x42: //sostenuto pedal
160 sustain = data[2] & 0x40;
161
162 for (unsigned i = 0; i < NVOICES; ++i) {
163 voices[i]->set_sustain(sustain);
164 //if pedal was released: dampen sustained notes
165 if((sustain==0) && (voices[i]->is_sustained())) {
166 voices[i]->release(0);
167 }
168 }
169 break;
170
171 //all sound off
172 case 0x78:
173 //all notes off
174 case 0x7b:
175 for(short v=0; v<NVOICES; v++) {
176 voices[v]->reset();
177 }
178 break;
179
180 default: break;
181 }
182 break;
183
184 default: break;
185 }
186 }
187
188
189 void mdaPiano::load_sample(Sample *s, const char* name) {
190 FILE *f;
191 long num, size;
192 char filepath[STRING_BUF];
193
194 strncpy(filepath, bundle_path(), STRING_BUF);
195 strncat(filepath,
196 name,
197 STRING_BUF - strlen(filepath));
198 f = fopen(filepath, "rb");
199 if (f == NULL) {
200 fputs("File error", stderr);
201 exit(1);
202 }
203
204 // obtain file size
205 fseek(f, 0, SEEK_END);
206 size = ftell(f);
207 rewind(f);
208
209 // allocate memory to contain the whole file
210 s->buffer = (short*) malloc (sizeof(short)*size);
211 if (s->buffer == NULL) {
212 fputs("Memory error", stderr);
213 exit(2);
214 }
215
216 // copy the file into the buffer
217 num = fread(s->buffer, 1, size, f);
218 if (num != size) {
219 fputs ("Reading error", stderr);
220 exit (3);
221 }
222 fclose (f);
223
224 // 16 bit
225 s->size = size / 2;
226
227 return;
228 }
229
230
231 // TODO: load keymapping from a file
232 void mdaPiano::load_kgrp(KGRP *kgrp) {
233 #ifdef PIANO
234 kgrp[ 0].root = 36; kgrp[ 0].high = 37; kgrp[ 0].loop = 14774;
235 kgrp[ 1].root = 40; kgrp[ 1].high = 41; kgrp[ 1].loop = 16268;
236 kgrp[ 2].root = 43; kgrp[ 2].high = 45; kgrp[ 2].loop = 33541;
237 kgrp[ 3].root = 48; kgrp[ 3].high = 49; kgrp[ 3].loop = 21156;
238 kgrp[ 4].root = 52; kgrp[ 4].high = 53; kgrp[ 4].loop = 17191;
239 kgrp[ 5].root = 55; kgrp[ 5].high = 57; kgrp[ 5].loop = 23286;
240 kgrp[ 6].root = 60; kgrp[ 6].high = 61; kgrp[ 6].loop = 18002;
241 kgrp[ 7].root = 64; kgrp[ 7].high = 65; kgrp[ 7].loop = 19746;
242 kgrp[ 8].root = 67; kgrp[ 8].high = 69; kgrp[ 8].loop = 22253;
243 kgrp[ 9].root = 72; kgrp[ 9].high = 73; kgrp[ 9].loop = 8852;
244 kgrp[10].root = 76; kgrp[10].high = 77; kgrp[10].loop = 9693;
245 kgrp[11].root = 79; kgrp[11].high = 81; kgrp[11].loop = 10596;
246 kgrp[12].root = 84; kgrp[12].high = 85; kgrp[12].loop = 6011;
247 kgrp[13].root = 88; kgrp[13].high = 89; kgrp[13].loop = 3414;
248 kgrp[14].root = 93; kgrp[14].high = 999; kgrp[14].loop = 2399;
249 #elif defined EPIANO
250 kgrp[ 0].root = 36; kgrp[ 0].high = 39; //C1
251 kgrp[ 3].root = 43; kgrp[ 3].high = 45; //G1
252 kgrp[ 6].root = 48; kgrp[ 6].high = 51; //C2
253 kgrp[ 9].root = 55; kgrp[ 9].high = 57; //G2
254 kgrp[12].root = 60; kgrp[12].high = 63; //C3
255 kgrp[15].root = 67; kgrp[15].high = 69; //G3
256 kgrp[18].root = 72; kgrp[18].high = 75; //C4
257 kgrp[21].root = 79; kgrp[21].high = 81; //G4
258 kgrp[24].root = 84; kgrp[24].high = 87; //C5
259 kgrp[27].root = 91; kgrp[27].high = 93; //G5
260 kgrp[30].root = 96; kgrp[30].high =999; //C6
261
262 kgrp[0].loop = 4400;
263 kgrp[1].loop = 4903;
264 kgrp[2].loop = 6398;
265 kgrp[3].loop = 3938;
266 kgrp[4].loop = 1633; //was 1636
267 kgrp[5].loop = 5245;
268 kgrp[6].loop = 2937;
269 kgrp[7].loop = 2203; //was 2204
270 kgrp[8].loop = 6368;
271 kgrp[9].loop = 10452;
272 kgrp[10].loop = 5217; //was 5220
273 kgrp[11].loop = 3099;
274 kgrp[12].loop = 4284;
275 kgrp[13].loop = 3916;
276 kgrp[14].loop = 2937;
277 kgrp[15].loop = 4732;
278 kgrp[16].loop = 4733;
279 kgrp[17].loop = 2285;
280 kgrp[18].loop = 4098;
281 kgrp[19].loop = 4099;
282 kgrp[20].loop = 3609;
283 kgrp[21].loop = 2446;
284 kgrp[22].loop = 6278;
285 kgrp[23].loop = 2283;
286 kgrp[24].loop = 2689;
287 kgrp[25].loop = 4370;
288 kgrp[26].loop = 5225;
289 kgrp[27].loop = 2811;
290 kgrp[28].loop = 2811; //ghost
291 kgrp[29].loop = 4522;
292 kgrp[30].loop = 2306;
293 kgrp[31].loop = 2306; //ghost
294 kgrp[32].loop = 2169;
295 #endif
296 }
297
298
299 void mdaPiano::tweak_sample(Sample *s, long loop_offset) {
300 //extra xfade looping...
301 long p0 = s->size;
302 long p1 = s->size - loop_offset;
303
304 float xf = 1.0f;
305 float dxf = -0.02f;
306
307 while(xf > 0.0f) {
308 s->buffer[p0] = (short)((1.0f - xf) * (float)s->buffer[p0] + xf * (float)s->buffer[p1]);
309 p0--;
310 p1--;
311 xf += dxf;
312 }
313 }
314
315 static int _ = mdaPiano::register_class(p_uri);