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