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