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