remove call to `suspend`
[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
47 void mdaPiano::update() //parameter change
48 {
49 float * param = programs[curProgram].param;
50 size = (uint32_t)(12.0f * param[2] - 6.0f);
51 sizevel = 0.12f * param[3];
52 muffvel = param[5] * param[5] * 5.0f;
53
54 velsens = 1.0f + param[6] + param[6];
55 if(param[6] < 0.25f) velsens -= 0.75f - 3.0f * param[6];
56
57 fine = param[9] - 0.5f;
58 random = 0.077f * param[10] * param[10];
59 stretch = 0.000434f * (param[11] - 0.5f);
60
61 cdep = param[7] * param[7];
62 trim = 1.50f - 0.79f * cdep;
63 width = 0.04f * param[7]; if(width > 0.03f) width = 0.03f;
64
65 poly = 8 + (uint32_t)(24.9f * param[8]);
66 }
67
68
69 void mdaPiano::resume()
70 {
71 Fs = getSampleRate();
72 iFs = 1.0f / Fs;
73 if(Fs > 64000.0f) cmax = 0xFF; else cmax = 0x7F;
74 memset(comb, 0, sizeof(float) * 256);
75 }
76
77
78 void mdaPiano::setParameter(uint32_t index, float value)
79 {
80 programs[curProgram].param[index] = value;
81 update();
82 }
83
84
85 uint32_t mdaPiano::processEvents(VstEvents* ev)
86 {
87 uint32_t npos=0;
88
89 for (uint32_t i=0; i<ev->numEvents; i++)
90 {
91 if((ev->events[i])->type != kVstMidiType) continue;
92 VstMidiEvent* event = (VstMidiEvent*)ev->events[i];
93 char* midiData = event->midiData;
94
95 switch(midiData[0] & 0xf0) //status byte (all channels)
96 {
97 case 0x80: //note off
98 notes[npos++] = event->deltaFrames; //delta
99 notes[npos++] = midiData[1] & 0x7F; //note
100 notes[npos++] = 0; //vel
101 break;
102
103 case 0x90: //note on
104 notes[npos++] = event->deltaFrames; //delta
105 notes[npos++] = midiData[1] & 0x7F; //note
106 notes[npos++] = midiData[2] & 0x7F; //vel
107 break;
108
109 case 0xB0: //controller
110 switch(midiData[1])
111 {
112 case 0x01: //mod wheel
113 case 0x43: //soft pedal
114 muff = 0.01f * (float)((127 - midiData[2]) * (127 - midiData[2]));
115 break;
116
117 case 0x07: //volume
118 volume = 0.00002f * (float)(midiData[2] * midiData[2]);
119 break;
120
121 case 0x40: //sustain pedal
122 case 0x42: //sustenuto pedal
123 sustain = midiData[2] & 0x40;
124 if(sustain==0)
125 {
126 notes[npos++] = event->deltaFrames;
127 notes[npos++] = SUSTAIN; //end all sustained notes
128 notes[npos++] = 0;
129 }
130 break;
131
132 //all sound off
133 case 0x78:
134 //all notes off
135 case 0x7b:
136 default:
137 for(short v=0; v<NVOICES; v++) {
138 voices[v]->reset();
139 }
140 break;
141 }
142 break;
143
144 default: break;
145 }
146
147 if(npos>EVENTBUFFER) npos -= 3; //discard events if buffer full!!
148 event++; //?
149 }
150 notes[npos] = EVENTS_DONE;
151 return 1;
152 }
153
154
155 // TODO: load this from a file
156 void mdaPiano::load_kgrp(KGRP *kgrp)
157 {
158 //Waveform data and keymapping is hard-wired in *this* version
159 kgrp[ 0].root = 36; kgrp[ 0].high = 37; kgrp[ 0].pos = 0; kgrp[ 0].end = 36275; kgrp[ 0].loop = 14774;
160 kgrp[ 1].root = 40; kgrp[ 1].high = 41; kgrp[ 1].pos = 36278; kgrp[ 1].end = 83135; kgrp[ 1].loop = 16268;
161 kgrp[ 2].root = 43; kgrp[ 2].high = 45; kgrp[ 2].pos = 83137; kgrp[ 2].end = 146756; kgrp[ 2].loop = 33541;
162 kgrp[ 3].root = 48; kgrp[ 3].high = 49; kgrp[ 3].pos = 146758; kgrp[ 3].end = 204997; kgrp[ 3].loop = 21156;
163 kgrp[ 4].root = 52; kgrp[ 4].high = 53; kgrp[ 4].pos = 204999; kgrp[ 4].end = 244908; kgrp[ 4].loop = 17191;
164 kgrp[ 5].root = 55; kgrp[ 5].high = 57; kgrp[ 5].pos = 244910; kgrp[ 5].end = 290978; kgrp[ 5].loop = 23286;
165 kgrp[ 6].root = 60; kgrp[ 6].high = 61; kgrp[ 6].pos = 290980; kgrp[ 6].end = 342948; kgrp[ 6].loop = 18002;
166 kgrp[ 7].root = 64; kgrp[ 7].high = 65; kgrp[ 7].pos = 342950; kgrp[ 7].end = 391750; kgrp[ 7].loop = 19746;
167 kgrp[ 8].root = 67; kgrp[ 8].high = 69; kgrp[ 8].pos = 391752; kgrp[ 8].end = 436915; kgrp[ 8].loop = 22253;
168 kgrp[ 9].root = 72; kgrp[ 9].high = 73; kgrp[ 9].pos = 436917; kgrp[ 9].end = 468807; kgrp[ 9].loop = 8852;
169 kgrp[10].root = 76; kgrp[10].high = 77; kgrp[10].pos = 468809; kgrp[10].end = 492772; kgrp[10].loop = 9693;
170 kgrp[11].root = 79; kgrp[11].high = 81; kgrp[11].pos = 492774; kgrp[11].end = 532293; kgrp[11].loop = 10596;
171 kgrp[12].root = 84; kgrp[12].high = 85; kgrp[12].pos = 532295; kgrp[12].end = 560192; kgrp[12].loop = 6011;
172 kgrp[13].root = 88; kgrp[13].high = 89; kgrp[13].pos = 560194; kgrp[13].end = 574121; kgrp[13].loop = 3414;
173 kgrp[14].root = 93; kgrp[14].high = 999; kgrp[14].pos = 574123; kgrp[14].end = 586343; kgrp[14].loop = 2399;
174 }
175
176
177 void mdaPiano::load_samples(short **buffer)
178 {
179 FILE *f;
180 long num, size;
181 char filepath[STRING_BUF];
182
183 strncpy(filepath, bundle_path(), STRING_BUF);
184 strncat(filepath,
185 sample_file,
186 STRING_BUF - strlen(filepath));
187 f = fopen(filepath, "rb");
188 if (f == NULL) {
189 fputs("File error", stderr);
190 exit(1);
191 }
192
193 // obtain file size
194 fseek(f, 0, SEEK_END);
195 size = ftell(f);
196 rewind(f);
197
198 // allocate memory to contain the whole file
199 *buffer = (short*) malloc (sizeof(short)*size);
200 if (*buffer == NULL) {
201 fputs("Memory error", stderr);
202 exit(2);
203 }
204
205 // copy the file into the buffer
206 num = fread(*buffer, 1, size, f);
207 if (num != size) {
208 fputs ("Reading error", stderr);
209 exit (3);
210 }
211 fclose (f);
212 return;
213 }