update voice on initialisation
[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 static const char* sample_file = "samples.raw";
25
26
27 mdaPiano::mdaPiano(double rate)
28 : LV2::Synth<mdaPianoVoice, mdaPiano>(p_n_ports, p_midi) {
29
30 load_samples(&waves);
31 load_kgrp(kgrp);
32
33 for(uint32_t i=0; i<NVOICES; ++i) {
34 voices[i] = new mdaPianoVoice(rate, waves, kgrp);
35 add_voices(voices[i]);
36 }
37
38 sustain = 0;
39
40 }
41
42 //parameter change
43 void mdaPiano::update() {
44 for (uint32_t v=0; v<NVOICES; ++v) {
45 voices[v]->update(Current);
46 }
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() == LV2::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 {
77 for (uint32_t v=0; v<NVOICES; ++v)
78 voices[v]->set_volume(value);
79 }
80
81
82 void mdaPiano::setParameter(unsigned char id, float value)
83 {
84 if(id>=NPARAMS)
85 return;
86 *p(id+PARAM_OFFSET) = value;
87 update();
88 #ifdef DEBUG
89 printf("changed %i to %f\n", id, value);
90 #endif
91 }
92
93
94 void mdaPiano::handle_midi(uint32_t size, unsigned char* data) {
95 #ifdef DEBUG
96 printf("%d\n", data[1]);
97 #endif
98
99 //discard invalid midi messages
100 if (size != 3)
101 return;
102
103 //receive on all channels
104 switch(data[0] & 0xf0)
105 {
106 case 0x80: //note off
107 for (unsigned i = 0; i < NVOICES; ++i) {
108 if (voices[i]->get_key() == data[1]) {
109 voices[i]->release(data[2]);
110 break;
111 }
112 }
113 break;
114
115 case 0x90: //note on
116 {
117 unsigned int v = find_free_voice(data[1], data[2]);
118 if (v < NVOICES) {
119 voices[v]->on(data[1], data[2]);
120 }
121 break;
122 }
123
124 case 0xB0: //controller
125 switch(data[1])
126 {
127 case 0x01: //mod wheel
128 case 0x43: //soft pedal
129 {
130 float muff = 0.01f * (float)((127 - data[2]) * (127 - data[2]));
131 for (unsigned i = 0; i < NVOICES; ++i) {
132 voices[i]->set_muff(muff);
133 }
134 break;
135 }
136
137 case 0x07: //volume
138 setVolume(0.00002f * (float)(data[2] * data[2]));
139 break;
140
141 case 0x40: //sustain pedal
142 case 0x42: //sustenuto pedal
143 sustain = data[2] & 0x40;
144
145 for (unsigned i = 0; i < NVOICES; ++i) {
146 voices[i]->set_sustain(sustain);
147 //if pedal was released: dampen sustained notes
148 if((sustain==0) && (voices[i]->is_sustained())) {
149 voices[i]->release(0);
150 }
151 }
152 break;
153
154 //all sound off
155 case 0x78:
156 //all notes off
157 case 0x7b:
158 default:
159 for(short v=0; v<NVOICES; v++) {
160 voices[v]->reset();
161 }
162 break;
163 }
164 break;
165
166 default: break;
167 }
168 }
169
170
171 // TODO: load this from a file
172 void mdaPiano::load_kgrp(KGRP *kgrp)
173 {
174 //Waveform data and keymapping is hard-wired in *this* version
175 kgrp[ 0].root = 36; kgrp[ 0].high = 37; kgrp[ 0].pos = 0; kgrp[ 0].end = 36275; kgrp[ 0].loop = 14774;
176 kgrp[ 1].root = 40; kgrp[ 1].high = 41; kgrp[ 1].pos = 36278; kgrp[ 1].end = 83135; kgrp[ 1].loop = 16268;
177 kgrp[ 2].root = 43; kgrp[ 2].high = 45; kgrp[ 2].pos = 83137; kgrp[ 2].end = 146756; kgrp[ 2].loop = 33541;
178 kgrp[ 3].root = 48; kgrp[ 3].high = 49; kgrp[ 3].pos = 146758; kgrp[ 3].end = 204997; kgrp[ 3].loop = 21156;
179 kgrp[ 4].root = 52; kgrp[ 4].high = 53; kgrp[ 4].pos = 204999; kgrp[ 4].end = 244908; kgrp[ 4].loop = 17191;
180 kgrp[ 5].root = 55; kgrp[ 5].high = 57; kgrp[ 5].pos = 244910; kgrp[ 5].end = 290978; kgrp[ 5].loop = 23286;
181 kgrp[ 6].root = 60; kgrp[ 6].high = 61; kgrp[ 6].pos = 290980; kgrp[ 6].end = 342948; kgrp[ 6].loop = 18002;
182 kgrp[ 7].root = 64; kgrp[ 7].high = 65; kgrp[ 7].pos = 342950; kgrp[ 7].end = 391750; kgrp[ 7].loop = 19746;
183 kgrp[ 8].root = 67; kgrp[ 8].high = 69; kgrp[ 8].pos = 391752; kgrp[ 8].end = 436915; kgrp[ 8].loop = 22253;
184 kgrp[ 9].root = 72; kgrp[ 9].high = 73; kgrp[ 9].pos = 436917; kgrp[ 9].end = 468807; kgrp[ 9].loop = 8852;
185 kgrp[10].root = 76; kgrp[10].high = 77; kgrp[10].pos = 468809; kgrp[10].end = 492772; kgrp[10].loop = 9693;
186 kgrp[11].root = 79; kgrp[11].high = 81; kgrp[11].pos = 492774; kgrp[11].end = 532293; kgrp[11].loop = 10596;
187 kgrp[12].root = 84; kgrp[12].high = 85; kgrp[12].pos = 532295; kgrp[12].end = 560192; kgrp[12].loop = 6011;
188 kgrp[13].root = 88; kgrp[13].high = 89; kgrp[13].pos = 560194; kgrp[13].end = 574121; kgrp[13].loop = 3414;
189 kgrp[14].root = 93; kgrp[14].high = 999; kgrp[14].pos = 574123; kgrp[14].end = 586343; kgrp[14].loop = 2399;
190 }
191
192
193 void mdaPiano::load_samples(short **buffer)
194 {
195 FILE *f;
196 long num, size;
197 char filepath[STRING_BUF];
198
199 strncpy(filepath, bundle_path(), STRING_BUF);
200 strncat(filepath,
201 sample_file,
202 STRING_BUF - strlen(filepath));
203 f = fopen(filepath, "rb");
204 if (f == NULL) {
205 fputs("File error", stderr);
206 exit(1);
207 }
208
209 // obtain file size
210 fseek(f, 0, SEEK_END);
211 size = ftell(f);
212 rewind(f);
213
214 // allocate memory to contain the whole file
215 *buffer = (short*) malloc (sizeof(short)*size);
216 if (*buffer == NULL) {
217 fputs("Memory error", stderr);
218 exit(2);
219 }
220
221 // copy the file into the buffer
222 num = fread(*buffer, 1, size, f);
223 if (num != size) {
224 fputs ("Reading error", stderr);
225 exit (3);
226 }
227 fclose (f);
228 return;
229 }
230
231 static int _ = mdaPiano::register_class(p_uri);