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