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