]> git.elephly.net Git - software/lv2-mdametapiano.git/blob - src/mdaPiano.cpp
a358988599472e2700189169fefa12ee83c04c32
[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 #include <cstdlib> //for exit
19
20 #define STRING_BUF 2048
21
22 mdaPiano::mdaPiano(double rate)
23 : lvtk::Synth<mdaPianoVoice, mdaPiano>(p_n_ports, p_midi) {
24
25 sustain = 0;
26
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
35 for (unsigned char i=0; i<NSAMPLES; ++i) {
36 load_sample(&samples[i], sample_names[i]);
37 }
38
39 load_kgrp(kgrp);
40
41 for(uint32_t i=0; i<NVOICES; ++i) {
42 voices[i] = new mdaPianoVoice(rate, samples, kgrp);
43 add_voices(voices[i]);
44 }
45
46 add_audio_outputs(p_left, p_right);
47 }
48
49
50 unsigned mdaPiano::find_free_voice(unsigned char key, unsigned char velocity) {
51 //is this a retriggered note during sustain?
52 if (sustain) {
53 for (unsigned i = 0; i < NVOICES; ++i) {
54 if ((voices[i]->get_key() == key) && (voices[i]->is_sustained())) {
55 return i;
56 }
57 }
58 }
59
60 //take the next free voice if
61 // ... notes are sustained but not this new one
62 // ... notes are not sustained
63 for (unsigned i = 0; i < NVOICES; ++i) {
64 if (voices[i]->get_key() == lvtk::INVALID_KEY)
65 {
66 return i;
67 }
68 }
69
70 //TODO: steal quietest note if all voices are used up
71 return 0;
72 }
73
74
75 void mdaPiano::setVolume(float value) {
76 for (uint32_t v=0; v<NVOICES; ++v)
77 voices[v]->set_volume(value);
78 }
79
80
81 //parameter change
82 void mdaPiano::update() {
83 for (uint32_t v=0; v<NVOICES; ++v) {
84 voices[v]->update(Current);
85 }
86 }
87
88
89 void mdaPiano::handle_midi(uint32_t size, unsigned char* data) {
90 #ifdef DEBUG
91 printf("%d\n", data[1]);
92 #endif
93
94 //discard invalid midi messages
95 if (size != 3)
96 return;
97
98 //receive on all channels
99 switch(data[0] & 0xf0)
100 {
101 case 0x80: //note off
102 for (unsigned i = 0; i < NVOICES; ++i) {
103 if (voices[i]->get_key() == data[1]) {
104 voices[i]->release(data[2]);
105 break;
106 }
107 }
108 break;
109
110 case 0x90: //note on
111 voices[ find_free_voice(data[1], data[2]) ]->on(data[1], data[2]);
112 break;
113
114 case 0xE0: break; //TODO: pitch bend
115
116 //controller
117 case 0xB0:
118 switch(data[1])
119 {
120 case 0x01: //mod wheel
121 case 0x43: //soft pedal
122 {
123 float muff = 0.01f * (float)((127 - data[2]) * (127 - data[2]));
124 for (unsigned i = 0; i < NVOICES; ++i) {
125 voices[i]->set_muff(muff);
126 }
127 break;
128 }
129
130 case 0x07: //volume
131 setVolume(0.00002f * (float)(data[2] * data[2]));
132 break;
133
134 case 0x40: //sustain pedal
135 case 0x42: //sostenuto pedal
136 sustain = data[2] & 0x40;
137
138 for (unsigned i = 0; i < NVOICES; ++i) {
139 voices[i]->set_sustain(sustain);
140 //if pedal was released: dampen sustained notes
141 if((sustain==0) && (voices[i]->is_sustained())) {
142 voices[i]->release(0);
143 }
144 }
145 break;
146
147 //all sound off
148 case 0x78:
149 //all notes off
150 case 0x7b:
151 for(short v=0; v<NVOICES; v++) {
152 voices[v]->reset();
153 }
154 break;
155
156 default: break;
157 }
158 break;
159
160 default: break;
161 }
162 }
163
164
165 void mdaPiano::load_sample(Sample *s, const char* name) {
166 FILE *f;
167 long num, size;
168 char filepath[STRING_BUF];
169
170 strncpy(filepath, bundle_path(), STRING_BUF);
171 strncat(filepath,
172 name,
173 STRING_BUF - strlen(filepath));
174 f = fopen(filepath, "rb");
175 if (f == NULL) {
176 fputs("File error", stderr);
177 exit(1);
178 }
179
180 // obtain file size
181 fseek(f, 0, SEEK_END);
182 size = ftell(f);
183 rewind(f);
184
185 // allocate memory to contain the whole file
186 s->buffer = (short*) malloc (sizeof(short)*size);
187 if (s->buffer == NULL) {
188 fputs("Memory error", stderr);
189 exit(2);
190 }
191
192 // copy the file into the buffer
193 num = fread(s->buffer, 1, size, f);
194 if (num != size) {
195 fputs ("Reading error", stderr);
196 exit (3);
197 }
198 fclose (f);
199
200 // 16 bit
201 s->size = size / 2;
202
203 return;
204 }
205
206
207 // TODO: load keymapping from a file
208 void mdaPiano::load_kgrp(KGRP *kgrp) {
209 kgrp[ 0].root = 36; kgrp[ 0].high = 37; kgrp[ 0].loop = 14774;
210 kgrp[ 1].root = 40; kgrp[ 1].high = 41; kgrp[ 1].loop = 16268;
211 kgrp[ 2].root = 43; kgrp[ 2].high = 45; kgrp[ 2].loop = 33541;
212 kgrp[ 3].root = 48; kgrp[ 3].high = 49; kgrp[ 3].loop = 21156;
213 kgrp[ 4].root = 52; kgrp[ 4].high = 53; kgrp[ 4].loop = 17191;
214 kgrp[ 5].root = 55; kgrp[ 5].high = 57; kgrp[ 5].loop = 23286;
215 kgrp[ 6].root = 60; kgrp[ 6].high = 61; kgrp[ 6].loop = 18002;
216 kgrp[ 7].root = 64; kgrp[ 7].high = 65; kgrp[ 7].loop = 19746;
217 kgrp[ 8].root = 67; kgrp[ 8].high = 69; kgrp[ 8].loop = 22253;
218 kgrp[ 9].root = 72; kgrp[ 9].high = 73; kgrp[ 9].loop = 8852;
219 kgrp[10].root = 76; kgrp[10].high = 77; kgrp[10].loop = 9693;
220 kgrp[11].root = 79; kgrp[11].high = 81; kgrp[11].loop = 10596;
221 kgrp[12].root = 84; kgrp[12].high = 85; kgrp[12].loop = 6011;
222 kgrp[13].root = 88; kgrp[13].high = 89; kgrp[13].loop = 3414;
223 kgrp[14].root = 93; kgrp[14].high = 999; kgrp[14].loop = 2399;
224 }
225
226
227 static int _ = mdaPiano::register_class(p_uri);