1 /*
2 wildmidi_lib.c
3
4 Midi Wavetable Processing library
5
6 Copyright (C) Chris Ison 2001-2014
7 Copyright (C) Bret Curtis 2013-2014
8
9 This file is part of WildMIDI.
10
11 WildMIDI is free software: you can redistribute and/or modify the player
12 under the terms of the GNU General Public License and you can redistribute
13 and/or modify the library under the terms of the GNU Lesser General Public
14 License as published by the Free Software Foundation, either version 3 of
15 the licenses, or(at your option) any later version.
16
17 WildMIDI is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License and
20 the GNU Lesser General Public License for more details.
21
22 You should have received a copy of the GNU General Public License and the
23 GNU Lesser General Public License along with WildMIDI. If not, see
24 <http://www.gnu.org/licenses/>.
25 */
26
27 //#include "config.h"
28
29 #define UNUSED(x) (void)(x)
30
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <math.h>
34 #include <string.h>
35 #ifndef _WIN32
36 #include <strings.h>
37 #endif
38 #include <stdlib.h>
39 #include <memory>
40 #include <algorithm>
41
42 #include "common.h"
43 #include "wm_error.h"
44 #include "file_io.h"
45 #include "reverb.h"
46 #include "gus_pat.h"
47 #include "wildmidi_lib.h"
48
49 namespace WildMidi
50 {
51
52 #define IS_DIR_SEPARATOR(c) ((c) == '/' || (c) == '\\')
53 #ifdef _WIN32
54 #define HAS_DRIVE_SPEC(f) ((f)[0] && ((f)[1] == ':'))
55 #else
56 #define HAS_DRIVE_SPEC(f) (0)
57 #endif
58 #define IS_ABSOLUTE_PATH(f) (IS_DIR_SEPARATOR((f)[0]) || HAS_DRIVE_SPEC((f)))
59
60
61 /*
62 * =========================
63 * Global Data and Data Structs
64 * =========================
65 */
66
67 #define MEM_CHUNK 8192
68
69
70 static const char WM_Version[] = "WildMidi Processing Library";
71
72 struct _channel {
73 unsigned char bank;
74 struct _patch *patch;
75 unsigned char hold;
76 unsigned char volume;
77 unsigned char pressure;
78 unsigned char expression;
79 signed char balance;
80 signed char pan;
81 signed short int left_adjust;
82 signed short int right_adjust;
83 signed short int pitch;
84 signed short int pitch_range;
85 signed long int pitch_adjust;
86 unsigned short reg_data;
87 unsigned char reg_non;
88 unsigned char isdrum;
89 };
90
91 #define HOLD_OFF 0x02
92
93 struct _note {
94 unsigned short noteid;
95 unsigned char velocity;
96 struct _patch *patch;
97 struct _sample *sample;
98 unsigned int sample_pos;
99 unsigned int sample_inc;
100 signed int env_inc;
101 unsigned char env;
102 signed int env_level;
103 unsigned char modes;
104 unsigned char hold;
105 unsigned char active;
106 struct _note *replay;
107 struct _note *next;
108 unsigned int left_mix_volume;
109 unsigned int right_mix_volume;
110 unsigned char is_off;
111 };
112
113 struct _event_data {
114 unsigned char channel;
115 unsigned int data;
116 };
117
118 struct _mdi {
_mdiWildMidi::_mdi119 _mdi()
120 {
121 samples_to_mix = 0;
122 midi_master_vol = 0;
123 memset(&info, 0, sizeof(info));
124 tmp_info = NULL;
125 memset(&channel, 0, sizeof(channel));
126 note = NULL;
127 memset(note_table, 0, sizeof(note_table));
128 patches = NULL;
129 patch_count = 0;
130 amp = 0;
131 mix_buffer = NULL;
132 mix_buffer_size = 0;
133 reverb = NULL;
134 }
135
136 unsigned long int samples_to_mix;
137
138 unsigned short midi_master_vol;
139 struct _WM_Info info;
140 struct _WM_Info *tmp_info;
141 struct _channel channel[16];
142 struct _note *note;
143 struct _note note_table[2][16][128];
144
145 struct _patch **patches;
146 unsigned long int patch_count;
147 signed short int amp;
148
149 signed int *mix_buffer;
150 unsigned long int mix_buffer_size;
151
152 struct _rvb *reverb;
153 };
154
155 #define FPBITS 10
156 #define FPMASK ((1L<<FPBITS)-1L)
157
158 /* Gauss Interpolation code adapted from code supplied by Eric. A. Welsh */
159 static double newt_coeffs[58][58]; /* for start/end of samples */
160 static std::vector<double> gauss_table; /* *gauss_table[1<<FPBITS] */
161 static const int gauss_n = 34; /* 34 is as high as we can go before errors crop up */
162
init_gauss(void)163 static void init_gauss(void) {
164 /* init gauss table */
165 int n = gauss_n;
166 int m, i, k, n_half = (n >> 1);
167 int j;
168 int sign;
169 double ck;
170 double x, x_inc, xz;
171 double z[35];
172 double *gptr, *t;
173
174 if (gauss_table.size()) {
175 return;
176 }
177
178 newt_coeffs[0][0] = 1;
179 for (i = 0; i <= n; i++) {
180 newt_coeffs[i][0] = 1;
181 newt_coeffs[i][i] = 1;
182
183 if (i > 1) {
184 newt_coeffs[i][0] = newt_coeffs[i - 1][0] / i;
185 newt_coeffs[i][i] = newt_coeffs[i - 1][0] / i;
186 }
187
188 for (j = 1; j < i; j++) {
189 newt_coeffs[i][j] = newt_coeffs[i - 1][j - 1]
190 + newt_coeffs[i - 1][j];
191 if (i > 1)
192 newt_coeffs[i][j] /= i;
193 }
194 z[i] = i / (4 * M_PI);
195 }
196
197 for (i = 0; i <= n; i++)
198 for (j = 0, sign = (int)pow(-1., i); j <= i; j++, sign *= -1)
199 newt_coeffs[i][j] *= sign;
200
201 gauss_table.resize((1<<FPBITS) * (n + 1));
202 t = gauss_table.data();
203 x_inc = 1.0 / (1<<FPBITS);
204 for (m = 0, x = 0.0; m < (1<<FPBITS); m++, x += x_inc) {
205 xz = (x + n_half) / (4 * M_PI);
206 gptr = &t[m * (n + 1)];
207
208 for (k = 0; k <= n; k++) {
209 ck = 1.0;
210
211 for (i = 0; i <= n; i++) {
212 if (i == k)
213 continue;
214
215 ck *= (sin(xz - z[i])) / (sin(z[k] - z[i]));
216 }
217 *gptr++ = ck;
218 }
219 }
220 }
221
222
223 /* f: ( VOLUME / 127.0 ) * 1024.0 */
224 static const signed short int lin_volume[] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 72,
225 80, 88, 96, 104, 112, 120, 129, 137, 145, 153, 161, 169, 177, 185, 193,
226 201, 209, 217, 225, 233, 241, 249, 258, 266, 274, 282, 290, 298, 306,
227 314, 322, 330, 338, 346, 354, 362, 370, 378, 387, 395, 403, 411, 419,
228 427, 435, 443, 451, 459, 467, 475, 483, 491, 499, 507, 516, 524, 532,
229 540, 548, 556, 564, 572, 580, 588, 596, 604, 612, 620, 628, 636, 645,
230 653, 661, 669, 677, 685, 693, 701, 709, 717, 725, 733, 741, 749, 757,
231 765, 774, 782, 790, 798, 806, 814, 822, 830, 838, 846, 854, 862, 870,
232 878, 886, 894, 903, 911, 919, 927, 935, 943, 951, 959, 967, 975, 983,
233 991, 999, 1007, 1015, 1024 };
234
235 /* f: As per midi 2 standard */
236 static const float dBm_volume[] = { -999999.999999f, -84.15214884f, -72.11094901f,
237 -65.06729865f, -60.06974919f, -56.19334866f, -53.02609882f, -50.34822724f,
238 -48.02854936f, -45.98244846f, -44.15214884f, -42.49644143f, -40.984899f,
239 -39.59441475f, -38.30702741f, -37.10849848f, -35.98734953f, -34.93419198f,
240 -33.94124863f, -33.0020048f, -32.11094901f, -31.26337705f, -30.45524161f,
241 -29.6830354f, -28.94369917f, -28.23454849f, -27.55321492f, -26.89759827f,
242 -26.26582758f, -25.65622892f, -25.06729865f, -24.49768108f, -23.94614971f,
243 -23.41159124f, -22.89299216f, -22.38942706f, -21.90004881f, -21.42407988f,
244 -20.96080497f, -20.50956456f, -20.06974919f, -19.64079457f, -19.22217722f,
245 -18.81341062f, -18.41404178f, -18.02364829f, -17.64183557f, -17.26823452f,
246 -16.90249934f, -16.54430564f, -16.19334866f, -15.84934179f, -15.51201509f,
247 -15.18111405f, -14.85639845f, -14.53764126f, -14.22462776f, -13.91715461f,
248 -13.6150291f, -13.31806837f, -13.02609882f, -12.73895544f, -12.45648126f,
249 -12.17852686f, -11.90494988f, -11.63561457f, -11.37039142f, -11.10915673f,
250 -10.85179233f, -10.59818521f, -10.34822724f, -10.10181489f, -9.858848981f,
251 -9.619234433f, -9.382880049f, -9.149698303f, -8.919605147f, -8.692519831f,
252 -8.468364731f, -8.247065187f, -8.028549359f, -7.812748083f, -7.599594743f,
253 -7.389025143f, -7.180977396f, -6.97539181f, -6.772210788f, -6.571378733f,
254 -6.372841952f, -6.176548572f, -5.982448461f, -5.790493145f, -5.600635744f,
255 -5.412830896f, -5.227034694f, -5.043204627f, -4.861299517f, -4.681279468f,
256 -4.503105811f, -4.326741054f, -4.152148838f, -3.979293887f, -3.808141968f,
257 -3.63865985f, -3.470815266f, -3.304576875f, -3.139914228f, -2.976797731f,
258 -2.815198619f, -2.655088921f, -2.496441432f, -2.339229687f, -2.183427931f,
259 -2.029011099f, -1.875954785f, -1.724235224f, -1.573829269f, -1.424714368f,
260 -1.276868546f, -1.130270383f, -0.9848989963f, -0.8407340256f, -0.6977556112f,
261 -0.5559443807f, -0.4152814317f, -0.2757483179f, -0.1373270335f, 0 };
262
263 /* f: As per midi 2 standard */
264 static const float dBm_pan_volume[127] = {
265 -999999.999999f, -87.6945020928f, -73.8331126923f, -65.7264009888f,
266 -59.9763864074f, -55.5181788833f, -51.8774481743f, -48.8011722841f,
267 -46.1383198371f, -43.7914727130f, -41.6941147218f, -39.7988027954f,
268 -38.0705069530f, -36.4826252703f, -35.0144798827f, -33.6496789707f,
269 -32.3750084716f, -31.1796603753f, -30.0546819321f, -28.9925739783f,
270 -27.9869924122f, -27.0325225804f, -26.1245061976f, -25.2589067713f,
271 -24.4322036893f, -23.6413079424f, -22.8834943857f, -22.1563467917f,
272 -21.4577129008f, -20.7856673630f, -20.1384809653f, -19.5145949062f,
273 -18.9125991563f, -18.3312141503f, -17.7692752119f, -17.2257192381f,
274 -16.6995732597f, -16.1899445690f, -15.6960121652f, -15.2170193110f,
275 -14.7522670314f, -14.3011084168f, -13.8629436112f, -13.4372153915f,
276 -13.0234052546f, -12.6210299451f, -12.2296383638f, -11.8488088095f,
277 -11.4781465116f, -11.1172814164f, -10.7658661983f, -10.4235744668f,
278 -10.0900991491f, -9.7651510261f, -9.4484574055f, -9.1397609172f,
279 -8.8388184168f, -8.5453999868f, -8.2592880250f, -7.9802764101f,
280 -7.7081697387f, -7.4427826248f, -7.1839390567f, -6.9314718056f,
281 -6.6852218807f, -6.4450380272f, -6.2107762624f, -5.9822994468f,
282 -5.7594768878f, -5.5421839719f, -5.3303018237f, -5.1237169899f,
283 -4.9223211445f, -4.7260108155f, -4.5346871303f, -4.3482555779f,
284 -4.1666257875f, -3.9897113219f, -3.8174294843f, -3.6497011373f,
285 -3.4864505345f, -3.3276051620f, -3.1730955900f, -3.0228553340f,
286 -2.8768207245f, -2.7349307844f, -2.5971271143f, -2.4633537845f,
287 -2.3335572335f, -2.2076861725f, -2.0856914960f, -1.9675261968f,
288 -1.8531452871f, -1.7425057233f, -1.6355663356f, -1.5322877618f,
289 -1.4326323846f, -1.3365642732f, -1.2440491272f, -1.1550542250f,
290 -1.0695483746f, -0.9875018671f, -0.9088864335f, -0.8336752037f,
291 -0.7618426682f, -0.6933646420f, -0.6282182304f, -0.5663817981f,
292 -0.5078349388f, -0.4525584478f, -0.4005342959f, -0.3517456058f,
293 -0.3061766293f, -0.2638127266f, -0.2246403475f, -0.1886470134f,
294 -0.1558213016f, -0.1261528303f, -0.0996322457f, -0.0762512098f,
295 -0.0560023899f, -0.0388794497f, -0.0248770409f, -0.0139907967f,
296 -0.0062173263f, -0.0015542108f, 0.0000000000f };
297
298 static const unsigned int freq_table[] = { 837201792, 837685632, 838169728,
299 838653568, 839138240, 839623232, 840108480, 840593984, 841079680,
300 841565184, 842051648, 842538240, 843025152, 843512320, 843999232,
301 844486976, 844975040, 845463360, 845951936, 846440320, 846929536,
302 847418944, 847908608, 848398656, 848888960, 849378944, 849869824,
303 850361024, 850852416, 851344192, 851835584, 852327872, 852820480,
304 853313280, 853806464, 854299328, 854793024, 855287040, 855781312,
305 856275904, 856770752, 857265344, 857760704, 858256448, 858752448,
306 859248704, 859744768, 860241600, 860738752, 861236160, 861733888,
307 862231360, 862729600, 863228160, 863727104, 864226176, 864725696,
308 865224896, 865724864, 866225152, 866725760, 867226688, 867727296,
309 868228736, 868730496, 869232576, 869734912, 870236928, 870739904,
310 871243072, 871746560, 872250368, 872754496, 873258240, 873762880,
311 874267840, 874773184, 875278720, 875783936, 876290112, 876796480,
312 877303232, 877810176, 878317504, 878824512, 879332416, 879840576,
313 880349056, 880857792, 881366272, 881875712, 882385280, 882895296,
314 883405440, 883915456, 884426304, 884937408, 885448832, 885960512,
315 886472512, 886984192, 887496768, 888009728, 888522944, 889036352,
316 889549632, 890063680, 890578048, 891092736, 891607680, 892122368,
317 892637952, 893153792, 893670016, 894186496, 894703232, 895219648,
318 895737024, 896254720, 896772672, 897290880, 897808896, 898327744,
319 898846912, 899366336, 899886144, 900405568, 900925952, 901446592,
320 901967552, 902488768, 903010368, 903531584, 904053760, 904576256,
321 905099008, 905622016, 906144896, 906668480, 907192512, 907716800,
322 908241408, 908765632, 909290816, 909816256, 910342144, 910868160,
323 911394624, 911920768, 912447680, 912975104, 913502720, 914030592,
324 914558208, 915086784, 915615552, 916144768, 916674176, 917203968,
325 917733440, 918263744, 918794496, 919325440, 919856704, 920387712,
326 920919616, 921451840, 921984320, 922517184, 923049728, 923583168,
327 924116928, 924651008, 925185344, 925720000, 926254336, 926789696,
328 927325312, 927861120, 928397440, 928933376, 929470208, 930007296,
329 930544768, 931082560, 931619968, 932158464, 932697152, 933236160,
330 933775488, 934315072, 934854464, 935394688, 935935296, 936476224,
331 937017344, 937558208, 938100160, 938642304, 939184640, 939727488,
332 940269888, 940813312, 941357056, 941900992, 942445440, 942990016,
333 943534400, 944079680, 944625280, 945171200, 945717440, 946263360,
334 946810176, 947357376, 947904832, 948452672, 949000192, 949548608,
335 950097280, 950646400, 951195776, 951745472, 952294912, 952845184,
336 953395904, 953946880, 954498176, 955049216, 955601088, 956153408,
337 956705920, 957258816, 957812032, 958364928, 958918848, 959472960,
338 960027456, 960582272, 961136768, 961692224, 962248000, 962804032,
339 963360448, 963916608, 964473600, 965031040, 965588736, 966146816,
340 966705152, 967263168, 967822144, 968381440, 968941120, 969501056,
341 970060736, 970621376, 971182272, 971743488, 972305088, 972866368,
342 973428608, 973991104, 974554048, 975117312, 975680768, 976243968,
343 976808192, 977372736, 977937536, 978502656, 979067584, 979633344,
344 980199488, 980765888, 981332736, 981899200, 982466688, 983034432,
345 983602624, 984171008, 984739776, 985308160, 985877632, 986447360,
346 987017472, 987587904, 988157952, 988729088, 989300416, 989872192,
347 990444224, 991016000, 991588672, 992161728, 992735168, 993308864,
348 993882880, 994456576, 995031296, 995606336, 996181696, 996757440,
349 997332800, 997909184, 998485888, 999062912, 999640256, 1000217984,
350 1000795392, 1001373696, 1001952448, 1002531520, 1003110848, 1003689920,
351 1004270016, 1004850304, 1005431040, 1006012160, 1006592832, 1007174592,
352 1007756608, 1008339008, 1008921792, 1009504768, 1010087552, 1010671296,
353 1011255360, 1011839808, 1012424576, 1013009024, 1013594368, 1014180160,
354 1014766272, 1015352768, 1015938880, 1016526016, 1017113472, 1017701248,
355 1018289408, 1018877824, 1019465984, 1020055104, 1020644672, 1021234496,
356 1021824768, 1022414528, 1023005440, 1023596608, 1024188160, 1024780096,
357 1025371584, 1025964160, 1026557120, 1027150336, 1027744000, 1028337920,
358 1028931520, 1029526144, 1030121152, 1030716480, 1031312128, 1031907456,
359 1032503808, 1033100480, 1033697536, 1034294912, 1034892032, 1035490048,
360 1036088512, 1036687232, 1037286336, 1037885824, 1038484928, 1039085056,
361 1039685632, 1040286464, 1040887680, 1041488448, 1042090368, 1042692608,
362 1043295168, 1043898176, 1044501440, 1045104384, 1045708288, 1046312640,
363 1046917376, 1047522368, 1048127040, 1048732800, 1049338816, 1049945280,
364 1050552128, 1051158528, 1051765952, 1052373824, 1052982016, 1053590592,
365 1054199424, 1054807936, 1055417600, 1056027456, 1056637760, 1057248448,
366 1057858752, 1058470016, 1059081728, 1059693824, 1060306304, 1060918336,
367 1061531392, 1062144896, 1062758656, 1063372928, 1063987392, 1064601664,
368 1065216896, 1065832448, 1066448448, 1067064704, 1067680704, 1068297728,
369 1068915136, 1069532864, 1070150976, 1070768640, 1071387520, 1072006720,
370 1072626240, 1073246080, 1073866368, 1074486272, 1075107200, 1075728512,
371 1076350208, 1076972160, 1077593856, 1078216704, 1078839680, 1079463296,
372 1080087040, 1080710528, 1081335168, 1081960064, 1082585344, 1083211008,
373 1083836928, 1084462592, 1085089280, 1085716352, 1086343936, 1086971648,
374 1087599104, 1088227712, 1088856576, 1089485824, 1090115456, 1090745472,
375 1091375104, 1092005760, 1092636928, 1093268352, 1093900160, 1094531584,
376 1095164160, 1095796992, 1096430336, 1097064064, 1097697280, 1098331648,
377 1098966400, 1099601536, 1100237056, 1100872832, 1101508224, 1102144768,
378 1102781824, 1103419136, 1104056832, 1104694144, 1105332608, 1105971328,
379 1106610432, 1107249920, 1107889152, 1108529408, 1109170048, 1109811072,
380 1110452352, 1111094144, 1111735552, 1112377984, 1113020928, 1113664128,
381 1114307712, 1114950912, 1115595264, 1116240000, 1116885120, 1117530624,
382 1118175744, 1118821888, 1119468416, 1120115456, 1120762752, 1121410432,
383 1122057856, 1122706176, 1123355136, 1124004224, 1124653824, 1125303040,
384 1125953408, 1126604160, 1127255168, 1127906560, 1128557696, 1129209984,
385 1129862528, 1130515456, 1131168768, 1131822592, 1132475904, 1133130368,
386 1133785216, 1134440448, 1135096064, 1135751296, 1136407680, 1137064448,
387 1137721472, 1138379008, 1139036800, 1139694336, 1140353024, 1141012096,
388 1141671424, 1142331264, 1142990592, 1143651200, 1144312192, 1144973440,
389 1145635200, 1146296448, 1146958976, 1147621760, 1148285056, 1148948608,
390 1149612672, 1150276224, 1150940928, 1151606144, 1152271616, 1152937600,
391 1153603072, 1154269824, 1154936832, 1155604352, 1156272128, 1156939648,
392 1157608192, 1158277248, 1158946560, 1159616384, 1160286464, 1160956288,
393 1161627264, 1162298624, 1162970240, 1163642368, 1164314112, 1164987008,
394 1165660160, 1166333824, 1167007872, 1167681536, 1168356352, 1169031552,
395 1169707136, 1170383104, 1171059584, 1171735552, 1172412672, 1173090304,
396 1173768192, 1174446592, 1175124480, 1175803648, 1176483072, 1177163008,
397 1177843328, 1178523264, 1179204352, 1179885824, 1180567680, 1181249920,
398 1181932544, 1182614912, 1183298304, 1183982208, 1184666368, 1185351040,
399 1186035328, 1186720640, 1187406464, 1188092672, 1188779264, 1189466368,
400 1190152960, 1190840832, 1191528960, 1192217600, 1192906624, 1193595136,
401 1194285056, 1194975232, 1195665792, 1196356736, 1197047296, 1197739136,
402 1198431360, 1199123968, 1199816960, 1200510336, 1201203328, 1201897600,
403 1202592128, 1203287040, 1203982464, 1204677504, 1205373696, 1206070272,
404 1206767232, 1207464704, 1208161664, 1208859904, 1209558528, 1210257536,
405 1210956928, 1211656832, 1212356224, 1213056768, 1213757952, 1214459392,
406 1215161216, 1215862656, 1216565376, 1217268352, 1217971840, 1218675712,
407 1219379200, 1220083840, 1220788992, 1221494528, 1222200448, 1222906752,
408 1223612672, 1224319872, 1225027456, 1225735424, 1226443648, 1227151616,
409 1227860864, 1228570496, 1229280512, 1229990912, 1230700928, 1231412096,
410 1232123776, 1232835840, 1233548288, 1234261248, 1234973696, 1235687424,
411 1236401536, 1237116032, 1237831040, 1238545536, 1239261312, 1239977472,
412 1240694144, 1241411072, 1242128512, 1242845568, 1243563776, 1244282496,
413 1245001600, 1245721088, 1246440192, 1247160448, 1247881216, 1248602368,
414 1249324032, 1250045184, 1250767616, 1251490432, 1252213632, 1252937344,
415 1253661440, 1254385152, 1255110016, 1255835392, 1256561152, 1257287424,
416 1258013184, 1258740096, 1259467648, 1260195456, 1260923648, 1261651584,
417 1262380800, 1263110272, 1263840256, 1264570624, 1265301504, 1266031872,
418 1266763520, 1267495552, 1268227968, 1268961024, 1269693440, 1270427264,
419 1271161472, 1271896064, 1272631168, 1273365760, 1274101632, 1274838016,
420 1275574784, 1276311808, 1277049472, 1277786624, 1278525056, 1279264000,
421 1280003328, 1280743040, 1281482368, 1282222976, 1282963968, 1283705344,
422 1284447232, 1285188736, 1285931392, 1286674560, 1287418240, 1288162176,
423 1288906624, 1289650688, 1290395904, 1291141760, 1291887872, 1292634496,
424 1293380608, 1294128128, 1294875904, 1295624320, 1296373120, 1297122304,
425 1297870976, 1298621056, 1299371520, 1300122496, 1300873856, 1301624832,
426 1302376960, 1303129600, 1303882752, 1304636288, 1305389312, 1306143872,
427 1306898688, 1307654016, 1308409600, 1309165696, 1309921536, 1310678528,
428 1311435904, 1312193920, 1312952192, 1313710080, 1314469248, 1315228928,
429 1315988992, 1316749568, 1317509632, 1318271104, 1319032960, 1319795200,
430 1320557952, 1321321088, 1322083840, 1322847872, 1323612416, 1324377216,
431 1325142656, 1325907584, 1326673920, 1327440512, 1328207744, 1328975360,
432 1329742464, 1330510976, 1331279872, 1332049152, 1332819072, 1333589248,
433 1334359168, 1335130240, 1335901824, 1336673920, 1337446400, 1338218368,
434 1338991744, 1339765632, 1340539904, 1341314560, 1342088832, 1342864512,
435 1343640576, 1344417024, 1345193984, 1345971456, 1346748416, 1347526656,
436 1348305408, 1349084672, 1349864320, 1350643456, 1351424000, 1352205056,
437 1352986496, 1353768448, 1354550784, 1355332608, 1356115968, 1356899712,
438 1357683840, 1358468480, 1359252608, 1360038144, 1360824192, 1361610624,
439 1362397440, 1363183872, 1363971712, 1364760064, 1365548672, 1366337792,
440 1367127424, 1367916672, 1368707200, 1369498240, 1370289664, 1371081472,
441 1371873024, 1372665856, 1373459072, 1374252800, 1375047040, 1375840768,
442 1376635904, 1377431552, 1378227584, 1379024000, 1379820928, 1380617472,
443 1381415296, 1382213760, 1383012480, 1383811840, 1384610560, 1385410816,
444 1386211456, 1387012480, 1387814144, 1388615168, 1389417728, 1390220672,
445 1391024128, 1391827968, 1392632320, 1393436288, 1394241536, 1395047296,
446 1395853568, 1396660224, 1397466368, 1398274048, 1399082112, 1399890688,
447 1400699648, 1401508224, 1402318080, 1403128576, 1403939456, 1404750848,
448 1405562624, 1406374016, 1407186816, 1408000000, 1408813696, 1409627904,
449 1410441728, 1411256704, 1412072320, 1412888320, 1413704960, 1414521856,
450 1415338368, 1416156288, 1416974720, 1417793664, 1418612992, 1419431808,
451 1420252160, 1421072896, 1421894144, 1422715904, 1423537280, 1424359808,
452 1425183104, 1426006784, 1426830848, 1427655296, 1428479488, 1429305088,
453 1430131072, 1430957568, 1431784576, 1432611072, 1433438976, 1434267392,
454 1435096192, 1435925632, 1436754432, 1437584768, 1438415616, 1439246848,
455 1440078720, 1440910848, 1441742720, 1442575872, 1443409664, 1444243584,
456 1445078400, 1445912576, 1446748032, 1447584256, 1448420864, 1449257856,
457 1450094464, 1450932480, 1451771008, 1452609920, 1453449472, 1454289408,
458 1455128960, 1455969920, 1456811264, 1457653248, 1458495616, 1459337600,
459 1460180864, 1461024768, 1461869056, 1462713984, 1463558272, 1464404096,
460 1465250304, 1466097152, 1466944384, 1467792128, 1468639488, 1469488256,
461 1470337408, 1471187200, 1472037376, 1472887168, 1473738368, 1474589952,
462 1475442304, 1476294912, 1477148160, 1478000768, 1478854912, 1479709696,
463 1480564608, 1481420288, 1482275456, 1483132160, 1483989248, 1484846976,
464 1485704960, 1486562688, 1487421696, 1488281344, 1489141504, 1490002048,
465 1490863104, 1491723776, 1492585856, 1493448448, 1494311424, 1495175040,
466 1496038144, 1496902656, 1497767808, 1498633344, 1499499392, 1500365056,
467 1501232128, 1502099712, 1502967808, 1503836416, 1504705536, 1505574016,
468 1506444032, 1507314688, 1508185856, 1509057408, 1509928576, 1510801280,
469 1511674240, 1512547840, 1513421952, 1514295680, 1515170816, 1516046464,
470 1516922624, 1517799296, 1518676224, 1519552896, 1520431104, 1521309824,
471 1522188928, 1523068800, 1523948032, 1524828672, 1525709824, 1526591616,
472 1527473792, 1528355456, 1529238784, 1530122496, 1531006720, 1531891712,
473 1532776832, 1533661824, 1534547968, 1535434880, 1536322304, 1537210112,
474 1538097408, 1538986368, 1539875840, 1540765696, 1541656192, 1542547072,
475 1543437440, 1544329472, 1545221888, 1546114944, 1547008384, 1547901440,
476 1548796032, 1549691136, 1550586624, 1551482752, 1552378368, 1553275520,
477 1554173184, 1555071232, 1555970048, 1556869248, 1557767936, 1558668288,
478 1559568896, 1560470272, 1561372032, 1562273408, 1563176320, 1564079616,
479 1564983424, 1565888000, 1566791808, 1567697408, 1568603392, 1569509760,
480 1570416896, 1571324416, 1572231424, 1573140096, 1574049152, 1574958976,
481 1575869184, 1576778752, 1577689984, 1578601728, 1579514112, 1580426880,
482 1581339264, 1582253056, 1583167488, 1584082432, 1584997888, 1585913984,
483 1586829440, 1587746304, 1588663936, 1589582080, 1590500736, 1591418880,
484 1592338560, 1593258752, 1594179584, 1595100928, 1596021632, 1596944000,
485 1597866880, 1598790272, 1599714304, 1600638848, 1601562752, 1602488320,
486 1603414272, 1604340992, 1605268224, 1606194816, 1607123072, 1608051968,
487 1608981120, 1609911040, 1610841344, 1611771264, 1612702848, 1613634688,
488 1614567168, 1615500288, 1616432896, 1617367040, 1618301824, 1619237120,
489 1620172800, 1621108096, 1622044928, 1622982272, 1623920128, 1624858752,
490 1625797632, 1626736256, 1627676416, 1628616960, 1629558272, 1630499968,
491 1631441152, 1632384000, 1633327232, 1634271232, 1635215744, 1636159744,
492 1637105152, 1638051328, 1638998016, 1639945088, 1640892928, 1641840128,
493 1642788992, 1643738368, 1644688384, 1645638784, 1646588672, 1647540352,
494 1648492416, 1649445120, 1650398464, 1651351168, 1652305408, 1653260288,
495 1654215808, 1655171712, 1656128256, 1657084288, 1658041856, 1659000064,
496 1659958784, 1660918272, 1661876992, 1662837376, 1663798400, 1664759936,
497 1665721984, 1666683520, 1667646720, 1668610560, 1669574784, 1670539776,
498 1671505024, 1672470016, 1673436544, };
499
500 #ifdef DEBUG_MIDI
501 #define MIDI_EVENT_DEBUG(dx,dy) printf("\r%s, %x\n",dx,dy)
502 #else
503 #define MIDI_EVENT_DEBUG(dx,dy)
504 #endif
505
506 #define MAX_AUTO_AMP 2.0
507
508 /*
509 * =========================
510 * Internal Functions
511 * =========================
512 */
513
514
FreePatches()515 void Instruments::FreePatches()
516 {
517 int i;
518 struct _patch * tmp_patch;
519 struct _sample * tmp_sample;
520
521 for (i = 0; i < 128; i++) {
522 while (patch[i]) {
523 while (patch[i]->first_sample) {
524 tmp_sample = patch[i]->first_sample->next;
525 free(patch[i]->first_sample->data);
526 free(patch[i]->first_sample);
527 patch[i]->first_sample = tmp_sample;
528 }
529 free(patch[i]->filename);
530 tmp_patch = patch[i]->next;
531 free(patch[i]);
532 patch[i] = tmp_patch;
533 }
534 }
535 }
536
537 /* wm_strdup -- adds extra space for appending up to 4 chars */
wm_strdup(const char * str)538 static char *wm_strdup (const char *str) {
539 size_t l = strlen(str) + 5;
540 char *d = (char *) malloc(l * sizeof(char));
541 if (d) {
542 strcpy(d, str);
543 return d;
544 }
545 return NULL;
546 }
547
wm_isdigit(int c)548 static inline int wm_isdigit(int c) {
549 return (c >= '0' && c <= '9');
550 }
551
552 #define TOKEN_CNT_INC 8
WM_LC_Tokenize_Line(char * line_data)553 static char** WM_LC_Tokenize_Line(char *line_data)
554 {
555 int line_length = (int)strlen(line_data);
556 int token_data_length = 0;
557 int line_ofs = 0;
558 int token_start = 0;
559 char **token_data = NULL;
560 int token_count = 0;
561 bool in_quotes = false;
562
563 if (line_length == 0)
564 return NULL;
565
566 do {
567 /* ignore everything after # */
568 if (line_data[line_ofs] == '#') {
569 break;
570 }
571 if (line_data[line_ofs] == '"')
572 {
573 in_quotes = !in_quotes;
574 }
575 else if (!in_quotes && ((line_data[line_ofs] == ' ') || (line_data[line_ofs] == '\t'))) {
576 /* whitespace means we aren't in a token */
577 if (token_start) {
578 token_start = 0;
579 line_data[line_ofs] = '\0';
580 }
581 } else {
582 if (!token_start) {
583 /* the start of a token in the line */
584 token_start = 1;
585 if (token_count >= token_data_length) {
586 token_data_length += TOKEN_CNT_INC;
587 token_data = (char**)realloc(token_data, token_data_length * sizeof(char *));
588 if (token_data == NULL) {
589 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM,"to parse config", errno);
590 return NULL;
591 }
592 }
593
594 token_data[token_count] = &line_data[line_ofs];
595 token_count++;
596 }
597 }
598 line_ofs++;
599 } while (line_ofs != line_length);
600
601 /* if we have found some tokens then add a null token to the end */
602 if (token_count) {
603 if (token_count >= token_data_length) {
604 token_data = (char**)realloc(token_data,
605 ((token_count + 1) * sizeof(char *)));
606 }
607 token_data[token_count] = NULL;
608 }
609
610 return token_data;
611 }
612
LoadConfig(const char * config_parm)613 int Instruments::LoadConfig(const char *config_parm)
614 {
615 unsigned long int config_size = 0;
616 char *config_buffer = NULL;
617 const char *dir_end = NULL;
618 char *config_dir = NULL;
619 unsigned long int config_ptr = 0;
620 unsigned long int line_start_ptr = 0;
621 unsigned short int patchid = 0;
622 struct _patch * tmp_patch;
623 char **line_tokens = NULL;
624 int token_count = 0;
625 std::string config_file_s;
626
627 config_buffer = (char *)_WM_BufferFile(sfreader, config_parm, &config_size, &config_file_s);
628 if (!config_buffer) {
629 FreePatches();
630 return -1;
631 }
632
633 auto config_file = config_file_s.c_str();
634
635 // This part was rewritten because the original depended on a header that was GPL'd.
636 dir_end = strrchr(config_file, '/');
637 #ifdef _WIN32
638 const char *dir_end2 = strrchr(config_file, '\\');
639 if (dir_end2 > dir_end) dir_end = dir_end2;
640 #endif
641
642 if (dir_end) {
643 config_dir = (char*)malloc((dir_end - config_file + 2));
644 if (config_dir == NULL) {
645 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config",
646 errno);
647 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
648 FreePatches();
649 free(config_buffer);
650 return -1;
651 }
652 strncpy(config_dir, config_file, (dir_end - config_file + 1));
653 config_dir[dir_end - config_file + 1] = '\0';
654 }
655
656 config_ptr = 0;
657 line_start_ptr = 0;
658
659 /* handle files without a newline at the end: this relies on
660 * _WM_BufferFile() allocating the buffer with one extra byte */
661 config_buffer[config_size] = '\n';
662
663 while (config_ptr <= config_size) {
664 if (config_buffer[config_ptr] == '\r' ||
665 config_buffer[config_ptr] == '\n')
666 {
667 config_buffer[config_ptr] = '\0';
668
669 if (config_ptr != line_start_ptr) {
670 line_tokens = WM_LC_Tokenize_Line(&config_buffer[line_start_ptr]);
671 if (line_tokens) {
672 if (stricmp(line_tokens[0], "dir") == 0) {
673 free(config_dir);
674 if (!line_tokens[1]) {
675 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
676 "(missing name in dir line)", 0);
677 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
678 config_file, 0);
679 FreePatches();
680 free(line_tokens);
681 free(config_buffer);
682 return -1;
683 } else if (!(config_dir = wm_strdup(line_tokens[1]))) {
684 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM,
685 "to parse config", errno);
686 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
687 config_file, 0);
688 FreePatches();
689 free(line_tokens);
690 free(config_buffer);
691 return -1;
692 }
693 if (!IS_DIR_SEPARATOR(config_dir[strlen(config_dir) - 1])) {
694 config_dir[strlen(config_dir) + 1] = '\0';
695 config_dir[strlen(config_dir)] = '/';
696 }
697 } else if (stricmp(line_tokens[0], "source") == 0) {
698 char *new_config = NULL;
699 if (!line_tokens[1]) {
700 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
701 "(missing name in source line)", 0);
702 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
703 config_file, 0);
704 FreePatches();
705 free(line_tokens);
706 free(config_buffer);
707 return -1;
708 } else if (!IS_ABSOLUTE_PATH(line_tokens[1]) && config_dir) {
709 new_config = (char*)malloc(
710 strlen(config_dir) + strlen(line_tokens[1])
711 + 1);
712 if (new_config == NULL) {
713 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM,
714 "to parse config", errno);
715 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
716 config_file, 0);
717 FreePatches();
718 free(config_dir);
719 free(line_tokens);
720 free(config_buffer);
721 return -1;
722 }
723 strcpy(new_config, config_dir);
724 strcpy(&new_config[strlen(config_dir)], line_tokens[1]);
725 } else {
726 if (!(new_config = wm_strdup(line_tokens[1]))) {
727 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM,
728 "to parse config", errno);
729 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
730 config_file, 0);
731 FreePatches();
732 free(line_tokens);
733 free(config_buffer);
734 return -1;
735 }
736 }
737 if (LoadConfig(new_config) == -1) {
738 free(new_config);
739 free(line_tokens);
740 free(config_buffer);
741 free(config_dir);
742 return -1;
743 }
744 free(new_config);
745 } else if (stricmp(line_tokens[0], "bank") == 0) {
746 if (!line_tokens[1] || !wm_isdigit(line_tokens[1][0])) {
747 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
748 "(syntax error in bank line)", 0);
749 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
750 config_file, 0);
751 FreePatches();
752 free(config_dir);
753 free(line_tokens);
754 free(config_buffer);
755 return -1;
756 }
757 patchid = (atoi(line_tokens[1]) & 0xFF) << 8;
758 } else if (stricmp(line_tokens[0], "drumset") == 0) {
759 if (!line_tokens[1] || !wm_isdigit(line_tokens[1][0])) {
760 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
761 "(syntax error in drumset line)", 0);
762 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
763 config_file, 0);
764 FreePatches();
765 free(config_dir);
766 free(line_tokens);
767 free(config_buffer);
768 return -1;
769 }
770 patchid = ((atoi(line_tokens[1]) & 0xFF) << 8) | 0x80;
771 } else if (stricmp(line_tokens[0], "reverb_room_width") == 0) {
772 if (!line_tokens[1] || !wm_isdigit(line_tokens[1][0])) {
773 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
774 "(syntax error in reverb_room_width line)",
775 0);
776 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
777 config_file, 0);
778 FreePatches();
779 free(config_dir);
780 free(line_tokens);
781 free(config_buffer);
782 return -1;
783 }
784 reverb_room_width = (float) atof(line_tokens[1]);
785 if (reverb_room_width < 1.0f) {
786 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
787 "(reverb_room_width < 1 meter, setting to minimum of 1 meter)",
788 0);
789 reverb_room_width = 1.0f;
790 } else if (reverb_room_width > 100.0f) {
791 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
792 "(reverb_room_width > 100 meters, setting to maximum of 100 meters)",
793 0);
794 reverb_room_width = 100.0f;
795 }
796 } else if (stricmp(line_tokens[0], "reverb_room_length") == 0) {
797 if (!line_tokens[1] || !wm_isdigit(line_tokens[1][0])) {
798 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
799 "(syntax error in reverb_room_length line)",
800 0);
801 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
802 config_file, 0);
803 FreePatches();
804 free(config_dir);
805 free(line_tokens);
806 free(config_buffer);
807 return -1;
808 }
809 reverb_room_length = (float) atof(line_tokens[1]);
810 if (reverb_room_length < 1.0f) {
811 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
812 "(reverb_room_length < 1 meter, setting to minimum of 1 meter)",
813 0);
814 reverb_room_length = 1.0f;
815 } else if (reverb_room_length > 100.0f) {
816 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
817 "(reverb_room_length > 100 meters, setting to maximum of 100 meters)",
818 0);
819 reverb_room_length = 100.0f;
820 }
821 } else if (stricmp(line_tokens[0], "reverb_listener_posx") == 0) {
822 if (!line_tokens[1] || !wm_isdigit(line_tokens[1][0])) {
823 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
824 "(syntax error in reverb_listen_posx line)",
825 0);
826 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
827 config_file, 0);
828 FreePatches();
829 free(config_dir);
830 free(line_tokens);
831 free(config_buffer);
832 return -1;
833 }
834 reverb_listen_posx = (float) atof(line_tokens[1]);
835 if ((reverb_listen_posx > reverb_room_width)
836 || (reverb_listen_posx < 0.0f)) {
837 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
838 "(reverb_listen_posx set outside of room)",
839 0);
840 reverb_listen_posx = reverb_room_width / 2.0f;
841 }
842 } else if (stricmp(line_tokens[0],
843 "reverb_listener_posy") == 0) {
844 if (!line_tokens[1] || !wm_isdigit(line_tokens[1][0])) {
845 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
846 "(syntax error in reverb_listen_posy line)",
847 0);
848 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
849 config_file, 0);
850 FreePatches();
851 free(config_dir);
852 free(line_tokens);
853 free(config_buffer);
854 return -1;
855 }
856 reverb_listen_posy = (float) atof(line_tokens[1]);
857 if ((reverb_listen_posy > reverb_room_width)
858 || (reverb_listen_posy < 0.0f)) {
859 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
860 "(reverb_listen_posy set outside of room)",
861 0);
862 reverb_listen_posy = reverb_room_length * 0.75f;
863 }
864 } else if (stricmp(line_tokens[0],
865 "guspat_editor_author_cant_read_so_fix_release_time_for_me")
866 == 0) {
867 fix_release = 1;
868 } else if (stricmp(line_tokens[0], "auto_amp") == 0) {
869 auto_amp = 1;
870 } else if (stricmp(line_tokens[0], "auto_amp_with_amp")
871 == 0) {
872 auto_amp = 1;
873 auto_amp_with_amp = 1;
874 } else if (wm_isdigit(line_tokens[0][0])) {
875 patchid = (patchid & 0xFF80)
876 | (atoi(line_tokens[0]) & 0x7F);
877 if (patch[(patchid & 0x7F)] == NULL) {
878 patch[(patchid & 0x7F)] = (struct _patch*)malloc(
879 sizeof(struct _patch));
880 if (patch[(patchid & 0x7F)] == NULL) {
881 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM,
882 NULL, errno);
883 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
884 config_file, 0);
885 FreePatches();
886 free(config_dir);
887 free(line_tokens);
888 free(config_buffer);
889 return -1;
890 }
891 tmp_patch = patch[(patchid & 0x7F)];
892 tmp_patch->patchid = patchid;
893 tmp_patch->filename = NULL;
894 tmp_patch->amp = 1024;
895 tmp_patch->note = 0;
896 tmp_patch->next = NULL;
897 tmp_patch->first_sample = NULL;
898 tmp_patch->loaded = 0;
899 tmp_patch->inuse_count = 0;
900 } else {
901 tmp_patch = patch[(patchid & 0x7F)];
902 if (tmp_patch->patchid == patchid) {
903 free(tmp_patch->filename);
904 tmp_patch->filename = NULL;
905 tmp_patch->amp = 1024;
906 tmp_patch->note = 0;
907 } else {
908 if (tmp_patch->next) {
909 while (tmp_patch->next) {
910 if (tmp_patch->next->patchid == patchid)
911 break;
912 tmp_patch = tmp_patch->next;
913 }
914 if (tmp_patch->next == NULL) {
915 if ((tmp_patch->next = (struct _patch*)malloc(
916 sizeof(struct _patch)))
917 == NULL) {
918 _WM_ERROR(__FUNCTION__, __LINE__,
919 WM_ERR_MEM, NULL, 0);
920 _WM_ERROR(__FUNCTION__, __LINE__,
921 WM_ERR_LOAD, config_file,
922 0);
923 FreePatches();
924 free(config_dir);
925 free(line_tokens);
926 free(config_buffer);
927 return -1;
928 }
929 tmp_patch = tmp_patch->next;
930 tmp_patch->patchid = patchid;
931 tmp_patch->filename = NULL;
932 tmp_patch->amp = 1024;
933 tmp_patch->note = 0;
934 tmp_patch->next = NULL;
935 tmp_patch->first_sample = NULL;
936 tmp_patch->loaded = 0;
937 tmp_patch->inuse_count = 0;
938 } else {
939 tmp_patch = tmp_patch->next;
940 free(tmp_patch->filename);
941 tmp_patch->filename = NULL;
942 tmp_patch->amp = 1024;
943 tmp_patch->note = 0;
944 }
945 } else {
946 tmp_patch->next = (struct _patch*)malloc(
947 sizeof(struct _patch));
948 if (tmp_patch->next == NULL) {
949 _WM_ERROR(__FUNCTION__, __LINE__,
950 WM_ERR_MEM, NULL, errno);
951 _WM_ERROR(__FUNCTION__, __LINE__,
952 WM_ERR_LOAD, config_file, 0);
953 FreePatches();
954 free(config_dir);
955 free(line_tokens);
956 free(config_buffer);
957 return -1;
958 }
959 tmp_patch = tmp_patch->next;
960 tmp_patch->patchid = patchid;
961 tmp_patch->filename = NULL;
962 tmp_patch->amp = 1024;
963 tmp_patch->note = 0;
964 tmp_patch->next = NULL;
965 tmp_patch->first_sample = NULL;
966 tmp_patch->loaded = 0;
967 tmp_patch->inuse_count = 0;
968 }
969 }
970 }
971 if (!line_tokens[1]) {
972 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
973 "(missing name in patch line)", 0);
974 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
975 config_file, 0);
976 FreePatches();
977 free(config_dir);
978 free(line_tokens);
979 free(config_buffer);
980 return -1;
981 } else if (!IS_ABSOLUTE_PATH(line_tokens[1]) && config_dir) {
982 tmp_patch->filename = (char*)malloc(
983 strlen(config_dir) + strlen(line_tokens[1])
984 + 5);
985 if (tmp_patch->filename == NULL) {
986 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM,
987 NULL, 0);
988 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
989 config_file, 0);
990 FreePatches();
991 free(config_dir);
992 free(line_tokens);
993 free(config_buffer);
994 return -1;
995 }
996 strcpy(tmp_patch->filename, config_dir);
997 strcat(tmp_patch->filename, line_tokens[1]);
998 } else {
999 if (!(tmp_patch->filename = wm_strdup(line_tokens[1]))) {
1000 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM,
1001 NULL, 0);
1002 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD,
1003 config_file, 0);
1004 FreePatches();
1005 free(config_dir);
1006 free(line_tokens);
1007 free(config_buffer);
1008 return -1;
1009 }
1010 }
1011 if (strnicmp(
1012 &tmp_patch->filename[strlen(tmp_patch->filename)
1013 - 4], ".pat", 4) != 0) {
1014 strcat(tmp_patch->filename, ".pat");
1015 }
1016 tmp_patch->env[0].set = 0x00;
1017 tmp_patch->env[1].set = 0x00;
1018 tmp_patch->env[2].set = 0x00;
1019 tmp_patch->env[3].set = 0x00;
1020 tmp_patch->env[4].set = 0x00;
1021 tmp_patch->env[5].set = 0x00;
1022 tmp_patch->keep = 0;
1023 tmp_patch->remove = 0;
1024
1025 token_count = 0;
1026 while (line_tokens[token_count]) {
1027 if (strnicmp(line_tokens[token_count], "amp=", 4)
1028 == 0) {
1029 if (!wm_isdigit(line_tokens[token_count][4])) {
1030 _WM_ERROR(__FUNCTION__, __LINE__,
1031 WM_ERR_INVALID_ARG,
1032 "(syntax error in patch line)", 0);
1033 } else {
1034 tmp_patch->amp = (atoi(
1035 &line_tokens[token_count][4]) << 10)
1036 / 100;
1037 }
1038 } else if (strnicmp(line_tokens[token_count],
1039 "note=", 5) == 0) {
1040 if (!wm_isdigit(line_tokens[token_count][5])) {
1041 _WM_ERROR(__FUNCTION__, __LINE__,
1042 WM_ERR_INVALID_ARG,
1043 "(syntax error in patch line)", 0);
1044 } else {
1045 tmp_patch->note = atoi(
1046 &line_tokens[token_count][5]);
1047 }
1048 } else if (strnicmp(line_tokens[token_count],
1049 "env_time", 8) == 0) {
1050 if ((!wm_isdigit(line_tokens[token_count][8]))
1051 || (!wm_isdigit(
1052 line_tokens[token_count][10]))
1053 || (line_tokens[token_count][9] != '=')) {
1054 _WM_ERROR(__FUNCTION__, __LINE__,
1055 WM_ERR_INVALID_ARG,
1056 "(syntax error in patch line)", 0);
1057 } else {
1058 unsigned int env_no = atoi(
1059 &line_tokens[token_count][8]);
1060 if (env_no > 5) {
1061 _WM_ERROR(__FUNCTION__, __LINE__,
1062 WM_ERR_INVALID_ARG,
1063 "(syntax error in patch line)",
1064 0);
1065 } else {
1066 tmp_patch->env[env_no].time =
1067 (float) atof(
1068 &line_tokens[token_count][10]);
1069 if ((tmp_patch->env[env_no].time
1070 > 45000.0f)
1071 || (tmp_patch->env[env_no].time
1072 < 1.47f)) {
1073 _WM_ERROR(__FUNCTION__, __LINE__,
1074 WM_ERR_INVALID_ARG,
1075 "(range error in patch line)",
1076 0);
1077 tmp_patch->env[env_no].set &= 0xFE;
1078 } else {
1079 tmp_patch->env[env_no].set |= 0x01;
1080 }
1081 }
1082 }
1083 } else if (strnicmp(line_tokens[token_count],
1084 "env_level", 9) == 0) {
1085 if ((!wm_isdigit(line_tokens[token_count][9]))
1086 || (!wm_isdigit(
1087 line_tokens[token_count][11]))
1088 || (line_tokens[token_count][10] != '=')) {
1089 _WM_ERROR(__FUNCTION__, __LINE__,
1090 WM_ERR_INVALID_ARG,
1091 "(syntax error in patch line)", 0);
1092 } else {
1093 unsigned int env_no = atoi(
1094 &line_tokens[token_count][9]);
1095 if (env_no > 5) {
1096 _WM_ERROR(__FUNCTION__, __LINE__,
1097 WM_ERR_INVALID_ARG,
1098 "(syntax error in patch line)",
1099 0);
1100 } else {
1101 tmp_patch->env[env_no].level =
1102 (float) atof(
1103 &line_tokens[token_count][11]);
1104 if ((tmp_patch->env[env_no].level > 1.0f)
1105 || (tmp_patch->env[env_no].level
1106 < 0.0f)) {
1107 _WM_ERROR(__FUNCTION__, __LINE__,
1108 WM_ERR_INVALID_ARG,
1109 "(range error in patch line)",
1110 0);
1111 tmp_patch->env[env_no].set &= 0xFD;
1112 } else {
1113 tmp_patch->env[env_no].set |= 0x02;
1114 }
1115 }
1116 }
1117 } else if (stricmp(line_tokens[token_count],
1118 "keep=loop") == 0) {
1119 tmp_patch->keep |= SAMPLE_LOOP;
1120 } else if (stricmp(line_tokens[token_count],
1121 "keep=env") == 0) {
1122 tmp_patch->keep |= SAMPLE_ENVELOPE;
1123 } else if (stricmp(line_tokens[token_count],
1124 "remove=sustain") == 0) {
1125 tmp_patch->remove |= SAMPLE_SUSTAIN;
1126 } else if (stricmp(line_tokens[token_count],
1127 "remove=clamped") == 0) {
1128 tmp_patch->remove |= SAMPLE_CLAMPED;
1129 }
1130 token_count++;
1131 }
1132 }
1133 }
1134 /* free up tokens */
1135 free(line_tokens);
1136 }
1137 line_start_ptr = config_ptr + 1;
1138 }
1139 config_ptr++;
1140 }
1141
1142 free(config_buffer);
1143 free(config_dir);
1144
1145 return 0;
1146 }
1147
1148 /* sample loading */
1149
load_sample(struct _patch * sample_patch)1150 int Instruments::load_sample(struct _patch *sample_patch)
1151 {
1152 struct _sample *guspat = NULL;
1153 struct _sample *tmp_sample = NULL;
1154 unsigned int i = 0;
1155
1156 /* we only want to try loading the guspat once. */
1157 sample_patch->loaded = 1;
1158
1159 if ((guspat = load_gus_pat(sample_patch->filename)) == NULL) {
1160 return -1;
1161 }
1162
1163 if (auto_amp) {
1164 signed short int tmp_max = 0;
1165 signed short int tmp_min = 0;
1166 signed short samp_max = 0;
1167 signed short samp_min = 0;
1168 tmp_sample = guspat;
1169 do {
1170 samp_max = 0;
1171 samp_min = 0;
1172 for (i = 0; i < (tmp_sample->data_length >> 10); i++) {
1173 if (tmp_sample->data[i] > samp_max)
1174 samp_max = tmp_sample->data[i];
1175 if (tmp_sample->data[i] < samp_min)
1176 samp_min = tmp_sample->data[i];
1177 }
1178 if (samp_max > tmp_max)
1179 tmp_max = samp_max;
1180 if (samp_min < tmp_min)
1181 tmp_min = samp_min;
1182 tmp_sample = tmp_sample->next;
1183 } while (tmp_sample);
1184 if (auto_amp_with_amp) {
1185 if (tmp_max >= -tmp_min) {
1186 sample_patch->amp = (sample_patch->amp
1187 * ((32767 << 10) / tmp_max)) >> 10;
1188 } else {
1189 sample_patch->amp = (sample_patch->amp
1190 * ((32768 << 10) / -tmp_min)) >> 10;
1191 }
1192 } else {
1193 if (tmp_max >= -tmp_min) {
1194 sample_patch->amp = (32767 << 10) / tmp_max;
1195 } else {
1196 sample_patch->amp = (32768 << 10) / -tmp_min;
1197 }
1198 }
1199 }
1200
1201 sample_patch->first_sample = guspat;
1202
1203 if (sample_patch->patchid & 0x0080) {
1204 if (!(sample_patch->keep & SAMPLE_LOOP)) {
1205 do {
1206 guspat->modes &= 0xFB;
1207 guspat = guspat->next;
1208 } while (guspat);
1209 }
1210 guspat = sample_patch->first_sample;
1211 if (!(sample_patch->keep & SAMPLE_ENVELOPE)) {
1212 do {
1213 guspat->modes &= 0xBF;
1214 guspat = guspat->next;
1215 } while (guspat);
1216 }
1217 guspat = sample_patch->first_sample;
1218 }
1219
1220 if (sample_patch->patchid == 47) {
1221 do {
1222 if (!(guspat->modes & SAMPLE_LOOP)) {
1223 for (i = 3; i < 6; i++) {
1224 guspat->env_target[i] = guspat->env_target[2];
1225 guspat->env_rate[i] = guspat->env_rate[2];
1226 }
1227 }
1228 guspat = guspat->next;
1229 } while (guspat);
1230 guspat = sample_patch->first_sample;
1231 }
1232
1233 do {
1234 if ((sample_patch->remove & SAMPLE_SUSTAIN)
1235 && (guspat->modes & SAMPLE_SUSTAIN)) {
1236 guspat->modes ^= SAMPLE_SUSTAIN;
1237 }
1238 if ((sample_patch->remove & SAMPLE_CLAMPED)
1239 && (guspat->modes & SAMPLE_CLAMPED)) {
1240 guspat->modes ^= SAMPLE_CLAMPED;
1241 }
1242 if (sample_patch->keep & SAMPLE_ENVELOPE) {
1243 guspat->modes |= SAMPLE_ENVELOPE;
1244 }
1245
1246 for (i = 0; i < 6; i++) {
1247 if (guspat->modes & SAMPLE_ENVELOPE) {
1248 if (sample_patch->env[i].set & 0x02) {
1249 guspat->env_target[i] = 16448
1250 * (signed long int) (255.0
1251 * sample_patch->env[i].level);
1252 }
1253
1254 if (sample_patch->env[i].set & 0x01) {
1255 guspat->env_rate[i] = (signed long int) (4194303.0
1256 / ((float) _WM_SampleRate
1257 * (sample_patch->env[i].time / 1000.0)));
1258 }
1259 } else {
1260 guspat->env_target[i] = 4194303;
1261 guspat->env_rate[i] = (signed long int) (4194303.0
1262 / ((float) _WM_SampleRate * env_time_table[63]));
1263 }
1264 }
1265
1266 guspat = guspat->next;
1267 } while (guspat);
1268 return 0;
1269 }
1270
get_patch_data(unsigned short patchid)1271 struct _patch *Instruments::get_patch_data(unsigned short patchid)
1272 {
1273 struct _patch *search_patch;
1274
1275 search_patch = patch[patchid & 0x007F];
1276
1277 if (search_patch == NULL) {
1278 return NULL;
1279 }
1280
1281 while (search_patch) {
1282 if (search_patch->patchid == patchid) {
1283 return search_patch;
1284 }
1285 search_patch = search_patch->next;
1286 }
1287 if ((patchid >> 8) != 0) {
1288 return (get_patch_data(patchid & 0x00FF));
1289 }
1290 return NULL;
1291 }
1292
load_patch(struct _mdi * mdi,unsigned short patchid)1293 void Instruments::load_patch(struct _mdi *mdi, unsigned short patchid)
1294 {
1295 unsigned int i;
1296 struct _patch *tmp_patch = NULL;
1297
1298 for (i = 0; i < mdi->patch_count; i++) {
1299 if (mdi->patches[i]->patchid == patchid) {
1300 return;
1301 }
1302 }
1303
1304 tmp_patch = get_patch_data(patchid);
1305 if (tmp_patch == NULL) {
1306 return;
1307 }
1308
1309 if (!tmp_patch->loaded) {
1310 if (load_sample(tmp_patch) == -1) {
1311 return;
1312 }
1313 }
1314
1315 if (tmp_patch->first_sample == NULL) {
1316 return;
1317 }
1318
1319 mdi->patch_count++;
1320 mdi->patches = (struct _patch**)realloc(mdi->patches,
1321 (sizeof(struct _patch*) * mdi->patch_count));
1322 mdi->patches[mdi->patch_count - 1] = tmp_patch;
1323 tmp_patch->inuse_count++;
1324 }
1325
~Instruments()1326 Instruments::~Instruments()
1327 {
1328 FreePatches();
1329 sfreader->close();
1330 }
1331
1332
get_sample_data(struct _patch * sample_patch,unsigned long int freq)1333 static struct _sample *get_sample_data(struct _patch *sample_patch, unsigned long int freq)
1334 {
1335 struct _sample *last_sample = NULL;
1336 struct _sample *return_sample = NULL;
1337
1338 if (sample_patch == NULL) {
1339 return NULL;
1340 }
1341 if (sample_patch->first_sample == NULL) {
1342 return NULL;
1343 }
1344 if (freq == 0) {
1345 return sample_patch->first_sample;
1346 }
1347
1348 return_sample = sample_patch->first_sample;
1349 last_sample = sample_patch->first_sample;
1350 while (last_sample) {
1351 if (freq > last_sample->freq_low) {
1352 if (freq < last_sample->freq_high) {
1353 return last_sample;
1354 } else {
1355 return_sample = last_sample;
1356 }
1357 }
1358 last_sample = last_sample->next;
1359 }
1360 return return_sample;
1361 }
1362
1363 /* Should be called in any function that effects note volumes */
AdjustNoteVolumes(struct _mdi * mdi,unsigned char ch,struct _note * nte)1364 void Renderer::AdjustNoteVolumes(struct _mdi *mdi, unsigned char ch, struct _note *nte)
1365 {
1366 double premix_dBm;
1367 double premix_lin;
1368 int pan_ofs;
1369 double premix_dBm_left;
1370 double premix_dBm_right;
1371 double premix_left;
1372 double premix_right;
1373 double volume_adj;
1374 unsigned vol_ofs;
1375
1376 /*
1377 Pointless CPU heating checks to shoosh up a compiler
1378 */
1379 if (ch > 0x0f) ch = 0x0f;
1380
1381 pan_ofs = mdi->channel[ch].balance + mdi->channel[ch].pan - 64;
1382
1383 vol_ofs = (nte->velocity * ((mdi->channel[ch].expression * mdi->channel[ch].volume) / 127)) / 127;
1384
1385 /*
1386 This value is to reduce the chance of clipping.
1387 Higher value means lower overall volume,
1388 Lower value means higher overall volume.
1389 NOTE: The lower the value the higher the chance of clipping.
1390 FIXME: Still needs tuning. Clipping heard at a value of 3.75
1391 */
1392 #define VOL_DIVISOR 4.0
1393 volume_adj = ((float)WM_MasterVolume / 1024.0) / VOL_DIVISOR;
1394
1395 // Pan 0 and 1 are both hard left so 64 can be centered
1396 if (pan_ofs > 127) pan_ofs = 127;
1397 if (--pan_ofs < 0) pan_ofs = 0;
1398 premix_dBm_left = dBm_pan_volume[126-pan_ofs];
1399 premix_dBm_right = dBm_pan_volume[pan_ofs];
1400
1401 if (mdi->info.mixer_options & WM_MO_LOG_VOLUME) {
1402 premix_dBm = dBm_volume[vol_ofs];
1403
1404 premix_dBm_left += premix_dBm;
1405 premix_dBm_right += premix_dBm;
1406
1407 premix_left = (pow(10.0,(premix_dBm_left / 20.0))) * volume_adj;
1408 premix_right = (pow(10.0,(premix_dBm_right / 20.0))) * volume_adj;
1409 } else {
1410 premix_lin = (float)(lin_volume[vol_ofs]) / 1024.0;
1411
1412 premix_left = premix_lin * pow(10.0, (premix_dBm_left / 20)) * volume_adj;
1413 premix_right = premix_lin * pow(10.0, (premix_dBm_right / 20)) * volume_adj;
1414 }
1415 nte->left_mix_volume = (int)(premix_left * 1024.0);
1416 nte->right_mix_volume = (int)(premix_right * 1024.0);
1417 }
1418
1419 /* Should be called in any function that effects channel volumes */
1420 /* Calling this function with a value > 15 will make it adjust notes on all channels */
AdjustChannelVolumes(struct _mdi * mdi,unsigned char ch)1421 void Renderer::AdjustChannelVolumes(struct _mdi *mdi, unsigned char ch)
1422 {
1423 struct _note *nte = mdi->note;
1424 if (nte != NULL) {
1425 do {
1426 if (ch <= 15) {
1427 if ((nte->noteid >> 8) == ch) {
1428 goto _DO_ADJUST;
1429 }
1430 } else {
1431 _DO_ADJUST:
1432 AdjustNoteVolumes(mdi, ch, nte);
1433 if (nte->replay) AdjustNoteVolumes(mdi, ch, nte->replay);
1434 }
1435 nte = nte->next;
1436 } while (nte != NULL);
1437 }
1438 }
1439
do_note_off_extra(struct _note * nte)1440 static void do_note_off_extra(struct _note *nte) {
1441
1442 nte->is_off = 0;
1443
1444
1445 if (!(nte->modes & SAMPLE_ENVELOPE)) {
1446 if (nte->modes & SAMPLE_LOOP) {
1447 nte->modes ^= SAMPLE_LOOP;
1448 }
1449 nte->env_inc = 0;
1450
1451 } else if (nte->hold) {
1452 nte->hold |= HOLD_OFF;
1453
1454 } else if (nte->modes & SAMPLE_SUSTAIN) {
1455 if (nte->env < 3) {
1456 nte->env = 3;
1457 if (nte->env_level > nte->sample->env_target[3]) {
1458 nte->env_inc = -nte->sample->env_rate[3];
1459 } else {
1460 nte->env_inc = nte->sample->env_rate[3];
1461 }
1462 }
1463
1464 } else if (nte->modes & SAMPLE_CLAMPED) {
1465 if (nte->env < 5) {
1466 nte->env = 5;
1467 if (nte->env_level > nte->sample->env_target[5]) {
1468 nte->env_inc = -nte->sample->env_rate[5];
1469 } else {
1470 nte->env_inc = nte->sample->env_rate[5];
1471 }
1472 }
1473 } else if (nte->env < 4) {
1474 nte->env = 4;
1475 if (nte->env_level > nte->sample->env_target[4]) {
1476 nte->env_inc = -nte->sample->env_rate[4];
1477 } else {
1478 nte->env_inc = nte->sample->env_rate[4];
1479 }
1480 }
1481 }
1482
do_note_off(struct _mdi * mdi,struct _event_data * data)1483 static void do_note_off(struct _mdi *mdi, struct _event_data *data) {
1484 struct _note *nte;
1485 unsigned char ch = data->channel;
1486
1487 MIDI_EVENT_DEBUG(__FUNCTION__,ch);
1488
1489 nte = &mdi->note_table[0][ch][(data->data >> 8)];
1490 if (!nte->active)
1491 nte = &mdi->note_table[1][ch][(data->data >> 8)];
1492 if (!nte->active) {
1493 return;
1494 }
1495
1496 if ((mdi->channel[ch].isdrum) && (!(nte->modes & SAMPLE_LOOP))) {
1497 return;
1498 }
1499
1500 if ((nte->modes & SAMPLE_ENVELOPE) && (nte->env == 0)) {
1501 // This is a fix for notes that end before the
1502 // initial step of the envelope has completed
1503 // making it impossible to hear them at times.
1504 nte->is_off = 1;
1505 } else {
1506 do_note_off_extra(nte);
1507 }
1508 }
1509
get_inc(struct _mdi * mdi,struct _note * nte)1510 unsigned long int Renderer::get_inc(struct _mdi *mdi, struct _note *nte)
1511 {
1512 int ch = nte->noteid >> 8;
1513 signed long int note_f;
1514 unsigned long int freq;
1515
1516 if (nte->patch->note != 0) {
1517 note_f = nte->patch->note * 100;
1518 } else {
1519 note_f = (nte->noteid & 0x7f) * 100;
1520 }
1521 note_f += mdi->channel[ch].pitch_adjust;
1522 if (note_f < 0) {
1523 note_f = 0;
1524 } else if (note_f > 12700) {
1525 note_f = 12700;
1526 }
1527 freq = freq_table[(note_f % 1200)] >> (10 - (note_f / 1200));
1528 return (((freq / ((instruments->GetSampleRate() * 100) / 1024)) * 1024
1529 / nte->sample->inc_div));
1530 }
1531
do_note_on(struct _mdi * mdi,struct _event_data * data)1532 void Renderer::do_note_on(struct _mdi *mdi, struct _event_data *data)
1533 {
1534 struct _note *nte;
1535 struct _note *prev_nte;
1536 struct _note *nte_array;
1537 unsigned long int freq = 0;
1538 struct _patch *patch;
1539 struct _sample *sample;
1540 unsigned char ch = data->channel;
1541 unsigned char note = (unsigned char)(data->data >> 8);
1542 unsigned char velocity = (unsigned char)(data->data & 0xFF);
1543
1544 if (velocity == 0x00) {
1545 do_note_off(mdi, data);
1546 return;
1547 }
1548
1549 MIDI_EVENT_DEBUG(__FUNCTION__,ch);
1550
1551 if (!mdi->channel[ch].isdrum) {
1552 patch = mdi->channel[ch].patch;
1553 if (patch == NULL) {
1554 return;
1555 }
1556 freq = freq_table[(note % 12) * 100] >> (10 - (note / 12));
1557 } else {
1558 patch = instruments->get_patch_data(((mdi->channel[ch].bank << 8) | note | 0x80));
1559 if (patch == NULL) {
1560 return;
1561 }
1562 if (patch->note) {
1563 freq = freq_table[(patch->note % 12) * 100]
1564 >> (10 - (patch->note / 12));
1565 } else {
1566 freq = freq_table[(note % 12) * 100] >> (10 - (note / 12));
1567 }
1568 }
1569
1570 sample = get_sample_data(patch, (freq / 100));
1571 if (sample == NULL) {
1572 return;
1573 }
1574
1575 nte = &mdi->note_table[0][ch][note];
1576
1577 if (nte->active) {
1578 if ((nte->modes & SAMPLE_ENVELOPE) && (nte->env < 3)
1579 && (!(nte->hold & HOLD_OFF)))
1580 return;
1581 nte->replay = &mdi->note_table[1][ch][note];
1582 nte->env = 6;
1583 nte->env_inc = -nte->sample->env_rate[6];
1584 nte = nte->replay;
1585 } else {
1586 if (mdi->note_table[1][ch][note].active) {
1587 if ((nte->modes & SAMPLE_ENVELOPE) && (nte->env < 3)
1588 && (!(nte->hold & HOLD_OFF)))
1589 return;
1590 mdi->note_table[1][ch][note].replay = nte;
1591 mdi->note_table[1][ch][note].env = 6;
1592 mdi->note_table[1][ch][note].env_inc =
1593 -mdi->note_table[1][ch][note].sample->env_rate[6];
1594 } else {
1595 nte_array = mdi->note;
1596 if (nte_array == NULL) {
1597 mdi->note = nte;
1598 } else {
1599 do {
1600 prev_nte = nte_array;
1601 nte_array = nte_array->next;
1602 } while (nte_array);
1603 prev_nte->next = nte;
1604 }
1605 nte->active = 1;
1606 nte->next = NULL;
1607 }
1608 }
1609 nte->noteid = (ch << 8) | note;
1610 nte->patch = patch;
1611 nte->sample = sample;
1612 nte->sample_pos = 0;
1613 nte->sample_inc = get_inc(mdi, nte);
1614 nte->velocity = velocity;
1615 nte->env = 0;
1616 nte->env_inc = nte->sample->env_rate[0];
1617 nte->env_level = 0;
1618 nte->modes = sample->modes;
1619 nte->hold = mdi->channel[ch].hold;
1620 nte->replay = NULL;
1621 nte->is_off = 0;
1622 AdjustNoteVolumes(mdi, ch, nte);
1623 }
1624
do_aftertouch(struct _mdi * mdi,struct _event_data * data)1625 void Renderer::do_aftertouch(struct _mdi *mdi, struct _event_data *data)
1626 {
1627 struct _note *nte;
1628 unsigned char ch = data->channel;
1629
1630 MIDI_EVENT_DEBUG(__FUNCTION__,ch);
1631
1632 nte = &mdi->note_table[0][ch][(data->data >> 8)];
1633 if (!nte->active) {
1634 nte = &mdi->note_table[1][ch][(data->data >> 8)];
1635 if (!nte->active) {
1636 return;
1637 }
1638 }
1639
1640 nte->velocity = (unsigned char)data->data;
1641 AdjustNoteVolumes(mdi, ch, nte);
1642 if (nte->replay) {
1643 nte->replay->velocity = (unsigned char)data->data;
1644 AdjustNoteVolumes(mdi, ch, nte->replay);
1645 }
1646 }
1647
do_control_bank_select(struct _mdi * mdi,struct _event_data * data)1648 static void do_control_bank_select(struct _mdi *mdi, struct _event_data *data) {
1649 unsigned char ch = data->channel;
1650 mdi->channel[ch].bank = (unsigned char)data->data;
1651 }
1652
do_control_data_entry_course(struct _mdi * mdi,struct _event_data * data)1653 static void do_control_data_entry_course(struct _mdi *mdi,
1654 struct _event_data *data) {
1655 unsigned char ch = data->channel;
1656 int data_tmp;
1657
1658 if ((mdi->channel[ch].reg_non == 0)
1659 && (mdi->channel[ch].reg_data == 0x0000)) { /* Pitch Bend Range */
1660 data_tmp = mdi->channel[ch].pitch_range % 100;
1661 mdi->channel[ch].pitch_range = short(data->data * 100 + data_tmp);
1662 /* printf("Data Entry Course: pitch_range: %i\n\r",mdi->channel[ch].pitch_range);*/
1663 /* printf("Data Entry Course: data %li\n\r",data->data);*/
1664 }
1665 }
1666
do_control_channel_volume(struct _mdi * mdi,struct _event_data * data)1667 void Renderer::do_control_channel_volume(struct _mdi *mdi, struct _event_data *data)
1668 {
1669 unsigned char ch = data->channel;
1670
1671 mdi->channel[ch].volume = (unsigned char)data->data;
1672 AdjustChannelVolumes(mdi, ch);
1673 }
1674
do_control_channel_balance(struct _mdi * mdi,struct _event_data * data)1675 void Renderer::do_control_channel_balance(struct _mdi *mdi, struct _event_data *data)
1676 {
1677 unsigned char ch = data->channel;
1678
1679 mdi->channel[ch].balance = (signed char)(data->data);
1680 AdjustChannelVolumes(mdi, ch);
1681 }
1682
do_control_channel_pan(struct _mdi * mdi,struct _event_data * data)1683 void Renderer::do_control_channel_pan(struct _mdi *mdi, struct _event_data *data)
1684 {
1685 unsigned char ch = data->channel;
1686
1687 mdi->channel[ch].pan = (signed char)(data->data);
1688 AdjustChannelVolumes(mdi, ch);
1689 }
1690
do_control_channel_expression(struct _mdi * mdi,struct _event_data * data)1691 void Renderer::do_control_channel_expression(struct _mdi *mdi, struct _event_data *data)
1692 {
1693 unsigned char ch = data->channel;
1694
1695 mdi->channel[ch].expression = (unsigned char)data->data;
1696 AdjustChannelVolumes(mdi, ch);
1697 }
1698
do_control_data_entry_fine(struct _mdi * mdi,struct _event_data * data)1699 static void do_control_data_entry_fine(struct _mdi *mdi,
1700 struct _event_data *data) {
1701 unsigned char ch = data->channel;
1702 int data_tmp;
1703
1704 if ((mdi->channel[ch].reg_non == 0)
1705 && (mdi->channel[ch].reg_data == 0x0000)) { /* Pitch Bend Range */
1706 data_tmp = mdi->channel[ch].pitch_range / 100;
1707 mdi->channel[ch].pitch_range = short((data_tmp * 100) + data->data);
1708 /* printf("Data Entry Fine: pitch_range: %i\n\r",mdi->channel[ch].pitch_range);*/
1709 /* printf("Data Entry Fine: data: %li\n\r", data->data);*/
1710 }
1711
1712 }
1713
do_control_channel_hold(struct _mdi * mdi,struct _event_data * data)1714 static void do_control_channel_hold(struct _mdi *mdi, struct _event_data *data) {
1715 struct _note *note_data = mdi->note;
1716 unsigned char ch = data->channel;
1717
1718 if (data->data > 63) {
1719 mdi->channel[ch].hold = 1;
1720 } else {
1721 mdi->channel[ch].hold = 0;
1722 if (note_data) {
1723 do {
1724 if ((note_data->noteid >> 8) == ch) {
1725 if (note_data->hold & HOLD_OFF) {
1726 if (note_data->modes & SAMPLE_ENVELOPE) {
1727 if (note_data->modes & SAMPLE_CLAMPED) {
1728 if (note_data->env < 5) {
1729 note_data->env = 5;
1730 if (note_data->env_level
1731 > note_data->sample->env_target[5]) {
1732 note_data->env_inc =
1733 -note_data->sample->env_rate[5];
1734 } else {
1735 note_data->env_inc =
1736 note_data->sample->env_rate[5];
1737 }
1738 }
1739 } else if (note_data->env < 4) {
1740 note_data->env = 4;
1741 if (note_data->env_level
1742 > note_data->sample->env_target[4]) {
1743 note_data->env_inc =
1744 -note_data->sample->env_rate[4];
1745 } else {
1746 note_data->env_inc =
1747 note_data->sample->env_rate[4];
1748 }
1749 }
1750 } else {
1751 if (note_data->modes & SAMPLE_LOOP) {
1752 note_data->modes ^= SAMPLE_LOOP;
1753 }
1754 note_data->env_inc = 0;
1755 }
1756 }
1757 note_data->hold = 0x00;
1758 }
1759 note_data = note_data->next;
1760 } while (note_data);
1761 }
1762 }
1763 }
1764
do_control_data_increment(struct _mdi * mdi,struct _event_data * data)1765 static void do_control_data_increment(struct _mdi *mdi,
1766 struct _event_data *data) {
1767 unsigned char ch = data->channel;
1768
1769 if ((mdi->channel[ch].reg_non == 0)
1770 && (mdi->channel[ch].reg_data == 0x0000)) { /* Pitch Bend Range */
1771 if (mdi->channel[ch].pitch_range < 0x3FFF)
1772 mdi->channel[ch].pitch_range++;
1773 }
1774 }
1775
do_control_data_decrement(struct _mdi * mdi,struct _event_data * data)1776 static void do_control_data_decrement(struct _mdi *mdi,
1777 struct _event_data *data) {
1778 unsigned char ch = data->channel;
1779
1780 if ((mdi->channel[ch].reg_non == 0)
1781 && (mdi->channel[ch].reg_data == 0x0000)) { /* Pitch Bend Range */
1782 if (mdi->channel[ch].pitch_range > 0)
1783 mdi->channel[ch].pitch_range--;
1784 }
1785 }
do_control_non_registered_param_fine(struct _mdi * mdi,struct _event_data * data)1786 static void do_control_non_registered_param_fine(struct _mdi *mdi,
1787 struct _event_data *data) {
1788 unsigned char ch = data->channel;
1789 mdi->channel[ch].reg_data = (mdi->channel[ch].reg_data & 0x3F80)
1790 | data->data;
1791 mdi->channel[ch].reg_non = 1;
1792 }
1793
do_control_non_registered_param_course(struct _mdi * mdi,struct _event_data * data)1794 static void do_control_non_registered_param_course(struct _mdi *mdi,
1795 struct _event_data *data) {
1796 unsigned char ch = data->channel;
1797 mdi->channel[ch].reg_data = (mdi->channel[ch].reg_data & 0x7F)
1798 | (data->data << 7);
1799 mdi->channel[ch].reg_non = 1;
1800 }
1801
do_control_registered_param_fine(struct _mdi * mdi,struct _event_data * data)1802 static void do_control_registered_param_fine(struct _mdi *mdi,
1803 struct _event_data *data) {
1804 unsigned char ch = data->channel;
1805 mdi->channel[ch].reg_data = (unsigned short) ((mdi->channel[ch].reg_data & 0x3F80)
1806 | data->data);
1807 mdi->channel[ch].reg_non = 0;
1808 }
1809
do_control_registered_param_course(struct _mdi * mdi,struct _event_data * data)1810 static void do_control_registered_param_course(struct _mdi *mdi,
1811 struct _event_data *data) {
1812 unsigned char ch = data->channel;
1813 mdi->channel[ch].reg_data = (unsigned short) ((mdi->channel[ch].reg_data & 0x7F)
1814 | (data->data << 7));
1815 mdi->channel[ch].reg_non = 0;
1816 }
1817
do_control_channel_sound_off(struct _mdi * mdi,struct _event_data * data)1818 static void do_control_channel_sound_off(struct _mdi *mdi,
1819 struct _event_data *data) {
1820 struct _note *note_data = mdi->note;
1821 unsigned char ch = data->channel;
1822
1823 if (note_data) {
1824 do {
1825 if ((note_data->noteid >> 8) == ch) {
1826 note_data->active = 0;
1827 if (note_data->replay) {
1828 note_data->replay = NULL;
1829 }
1830 }
1831 note_data = note_data->next;
1832 } while (note_data);
1833 }
1834 }
1835
do_control_channel_controllers_off(struct _mdi * mdi,struct _event_data * data)1836 void Renderer::do_control_channel_controllers_off(struct _mdi *mdi, struct _event_data *data)
1837 {
1838 unsigned char ch = data->channel;
1839
1840 mdi->channel[ch].expression = 127;
1841 mdi->channel[ch].pressure = 127;
1842 mdi->channel[ch].reg_data = 0xffff;
1843 mdi->channel[ch].pitch_range = 200;
1844 mdi->channel[ch].pitch = 0;
1845 mdi->channel[ch].pitch_adjust = 0;
1846 mdi->channel[ch].hold = 0;
1847
1848 AdjustChannelVolumes(mdi, ch);
1849 }
1850
do_control_channel_notes_off(struct _mdi * mdi,struct _event_data * data)1851 static void do_control_channel_notes_off(struct _mdi *mdi,
1852 struct _event_data *data) {
1853 struct _note *note_data = mdi->note;
1854 unsigned char ch = data->channel;
1855
1856 if (mdi->channel[ch].isdrum)
1857 return;
1858 if (note_data) {
1859 do {
1860 if ((note_data->noteid >> 8) == ch) {
1861 if (!note_data->hold) {
1862 if (note_data->modes & SAMPLE_ENVELOPE) {
1863 if (note_data->env < 5) {
1864 if (note_data->env_level
1865 > note_data->sample->env_target[5]) {
1866 note_data->env_inc =
1867 -note_data->sample->env_rate[5];
1868 } else {
1869 note_data->env_inc =
1870 note_data->sample->env_rate[5];
1871 }
1872 note_data->env = 5;
1873 }
1874 }
1875 } else {
1876 note_data->hold |= HOLD_OFF;
1877 }
1878 }
1879 note_data = note_data->next;
1880 } while (note_data);
1881 }
1882 }
1883
do_patch(struct _mdi * mdi,struct _event_data * data)1884 void Renderer::do_patch(struct _mdi *mdi, struct _event_data *data)
1885 {
1886 unsigned char ch = data->channel;
1887 MIDI_EVENT_DEBUG(__FUNCTION__,ch);
1888 if (!mdi->channel[ch].isdrum) {
1889 mdi->channel[ch].patch = instruments->get_patch_data((unsigned short)(((mdi->channel[ch].bank << 8) | data->data)));
1890 } else {
1891 mdi->channel[ch].bank = (unsigned char)data->data;
1892 }
1893 }
1894
do_channel_pressure(struct _mdi * mdi,struct _event_data * data)1895 void Renderer::do_channel_pressure(struct _mdi *mdi, struct _event_data *data)
1896 {
1897 struct _note *note_data = mdi->note;
1898 unsigned char ch = data->channel;
1899
1900 MIDI_EVENT_DEBUG(__FUNCTION__,ch);
1901
1902 while (note_data) {
1903 if ((note_data->noteid >> 8) == ch) {
1904 note_data->velocity = (unsigned char)data->data;
1905 AdjustNoteVolumes(mdi, ch, note_data);
1906 if (note_data->replay) {
1907 note_data->replay->velocity = (unsigned char)data->data;
1908 AdjustNoteVolumes(mdi, ch, note_data->replay);
1909 }
1910 }
1911 note_data = note_data->next;
1912 }
1913 }
1914
do_pitch(struct _mdi * mdi,struct _event_data * data)1915 void Renderer::do_pitch(struct _mdi *mdi, struct _event_data *data)
1916 {
1917 struct _note *note_data = mdi->note;
1918 unsigned char ch = data->channel;
1919
1920 MIDI_EVENT_DEBUG(__FUNCTION__,ch);
1921 mdi->channel[ch].pitch = short(data->data - 0x2000);
1922
1923 if (mdi->channel[ch].pitch < 0) {
1924 mdi->channel[ch].pitch_adjust = mdi->channel[ch].pitch_range
1925 * mdi->channel[ch].pitch / 8192;
1926 } else {
1927 mdi->channel[ch].pitch_adjust = mdi->channel[ch].pitch_range
1928 * mdi->channel[ch].pitch / 8191;
1929 }
1930
1931 if (note_data) {
1932 do {
1933 if ((note_data->noteid >> 8) == ch) {
1934 note_data->sample_inc = get_inc(mdi, note_data);
1935 }
1936 note_data = note_data->next;
1937 } while (note_data);
1938 }
1939 }
1940
do_sysex_roland_drum_track(struct _mdi * mdi,struct _event_data * data)1941 void Renderer::do_sysex_roland_drum_track(struct _mdi *mdi, struct _event_data *data)
1942 {
1943 unsigned char ch = data->channel;
1944
1945 MIDI_EVENT_DEBUG(__FUNCTION__,ch);
1946
1947 if (data->data > 0) {
1948 mdi->channel[ch].isdrum = 1;
1949 mdi->channel[ch].patch = NULL;
1950 } else {
1951 mdi->channel[ch].isdrum = 0;
1952 mdi->channel[ch].patch = instruments->get_patch_data(0);
1953 }
1954 }
1955
do_sysex_gm_reset(struct _mdi * mdi,struct _event_data * data)1956 void Renderer::do_sysex_gm_reset(struct _mdi *mdi, struct _event_data *data)
1957 {
1958 int i;
1959 for (i = 0; i < 16; i++) {
1960 mdi->channel[i].bank = 0;
1961 if (i != 9) {
1962 mdi->channel[i].patch = instruments->get_patch_data(0);
1963 } else {
1964 mdi->channel[i].patch = NULL;
1965 }
1966 mdi->channel[i].hold = 0;
1967 mdi->channel[i].volume = 100;
1968 mdi->channel[i].pressure = 127;
1969 mdi->channel[i].expression = 127;
1970 mdi->channel[i].balance = 64;
1971 mdi->channel[i].pan = 64;
1972 mdi->channel[i].pitch = 0;
1973 mdi->channel[i].pitch_range = 200;
1974 mdi->channel[i].reg_data = 0xFFFF;
1975 mdi->channel[i].isdrum = 0;
1976 }
1977 /* I would not expect notes to be active when this event
1978 triggers but we'll adjust active notes as well just in case */
1979 AdjustChannelVolumes(mdi,16); // A setting > 15 adjusts all channels
1980
1981 mdi->channel[9].isdrum = 1;
1982 UNUSED(data); /* NOOP, to please the compiler gods */
1983 }
1984
do_sysex_roland_reset(struct _mdi * mdi,struct _event_data * data)1985 void Renderer::do_sysex_roland_reset(struct _mdi *mdi, struct _event_data *data)
1986 {
1987 do_sysex_gm_reset(mdi, data);
1988 }
1989
do_sysex_yamaha_reset(struct _mdi * mdi,struct _event_data * data)1990 void Renderer::do_sysex_yamaha_reset(struct _mdi *mdi, struct _event_data *data)
1991 {
1992 do_sysex_gm_reset(mdi, data);
1993 }
1994
Init_MDI()1995 struct _mdi *Renderer::Init_MDI()
1996 {
1997 struct _mdi *mdi;
1998
1999 mdi = new _mdi;
2000
2001 mdi->info.copyright = NULL;
2002 mdi->info.mixer_options = WM_MixerOptions;
2003
2004 instruments->load_patch(mdi, 0x0000);
2005
2006 mdi->samples_to_mix = 0;
2007 mdi->info.current_sample = 0;
2008 mdi->info.total_midi_time = 0;
2009 mdi->info.approx_total_samples = 0;
2010
2011 do_sysex_roland_reset(mdi, NULL);
2012
2013 return mdi;
2014 }
2015
freeMDI(struct _mdi * mdi)2016 static void freeMDI(struct _mdi *mdi)
2017 {
2018 struct _sample *tmp_sample;
2019 unsigned long int i;
2020
2021 if (mdi->patch_count != 0) {
2022 for (i = 0; i < mdi->patch_count; i++) {
2023 mdi->patches[i]->inuse_count--;
2024 if (mdi->patches[i]->inuse_count == 0) {
2025 /* free samples here */
2026 while (mdi->patches[i]->first_sample) {
2027 tmp_sample = mdi->patches[i]->first_sample->next;
2028 free(mdi->patches[i]->first_sample->data);
2029 free(mdi->patches[i]->first_sample);
2030 mdi->patches[i]->first_sample = tmp_sample;
2031 }
2032 mdi->patches[i]->loaded = 0;
2033 }
2034 }
2035 free(mdi->patches);
2036 }
2037
2038 free(mdi->tmp_info);
2039 _WM_free_reverb(mdi->reverb);
2040 if (mdi->mix_buffer) free(mdi->mix_buffer);
2041 delete mdi;
2042 }
2043
WM_Mix_Linear(midi * handle,int * buffer,unsigned long int count)2044 static int *WM_Mix_Linear(midi * handle, int * buffer, unsigned long int count)
2045 {
2046 struct _mdi *mdi = (struct _mdi *)handle;
2047 unsigned long int data_pos;
2048 signed int premix, left_mix, right_mix;
2049 struct _note *note_data = NULL;
2050
2051 do {
2052 note_data = mdi->note;
2053 left_mix = right_mix = 0;
2054 if (note_data != NULL) {
2055 while (note_data) {
2056 /*
2057 * ===================
2058 * resample the sample
2059 * ===================
2060 */
2061 data_pos = note_data->sample_pos >> FPBITS;
2062 premix = ((note_data->sample->data[data_pos] + (((note_data->sample->data[data_pos + 1] - note_data->sample->data[data_pos]) * (int)(note_data->sample_pos & FPMASK)) / 1024)) * (note_data->env_level >> 12)) / 1024;
2063
2064
2065 left_mix += (premix * (int)note_data->left_mix_volume) / 1024;
2066 right_mix += (premix * (int)note_data->right_mix_volume) / 1024;
2067
2068 /*
2069 * ========================
2070 * sample position checking
2071 * ========================
2072 */
2073 note_data->sample_pos += note_data->sample_inc;
2074 if (note_data->modes & SAMPLE_LOOP) {
2075 if (note_data->sample_pos > note_data->sample->loop_end) {
2076 note_data->sample_pos =
2077 note_data->sample->loop_start
2078 + ((note_data->sample_pos
2079 - note_data->sample->loop_start)
2080 % note_data->sample->loop_size);
2081 }
2082 } else if (note_data->sample_pos >= note_data->sample->data_length) {
2083 goto END_THIS_NOTE;
2084 }
2085
2086 if (note_data->env_inc == 0) {
2087 note_data = note_data->next;
2088 continue;
2089 }
2090
2091 note_data->env_level += note_data->env_inc;
2092 if (note_data->env_inc < 0) {
2093 if (note_data->env_level > note_data->sample->env_target[note_data->env]) {
2094 note_data = note_data->next;
2095 continue;
2096 }
2097 } else if (note_data->env_inc > 0) {
2098 if (note_data->env_level < note_data->sample->env_target[note_data->env]) {
2099 note_data = note_data->next;
2100 continue;
2101 }
2102 }
2103
2104 // Yes could have a condition here but
2105 // it would create another bottleneck
2106 note_data->env_level =
2107 note_data->sample->env_target[note_data->env];
2108 switch (note_data->env) {
2109 case 0:
2110 if (!(note_data->modes & SAMPLE_ENVELOPE)) {
2111 note_data->env_inc = 0;
2112 note_data = note_data->next;
2113 continue;
2114 }
2115 break;
2116 case 2:
2117 if (note_data->modes & SAMPLE_SUSTAIN /*|| note_data->hold*/) {
2118 note_data->env_inc = 0;
2119 note_data = note_data->next;
2120 continue;
2121 } else if (note_data->modes & SAMPLE_CLAMPED) {
2122 note_data->env = 5;
2123 if (note_data->env_level
2124 > note_data->sample->env_target[5]) {
2125 note_data->env_inc =
2126 -note_data->sample->env_rate[5];
2127 } else {
2128 note_data->env_inc =
2129 note_data->sample->env_rate[5];
2130 }
2131 continue;
2132 }
2133 break;
2134 case 5:
2135 if (note_data->env_level == 0) {
2136 goto END_THIS_NOTE;
2137 }
2138 /* sample release */
2139 if (note_data->modes & SAMPLE_LOOP)
2140 note_data->modes ^= SAMPLE_LOOP;
2141 note_data->env_inc = 0;
2142 note_data = note_data->next;
2143 continue;
2144 case 6:
2145 END_THIS_NOTE:
2146 if (note_data->replay != NULL) {
2147 note_data->active = 0;
2148 {
2149 struct _note *prev_note = NULL;
2150 struct _note *nte_array = mdi->note;
2151
2152 if (nte_array != note_data) {
2153 do {
2154 prev_note = nte_array;
2155 nte_array = nte_array->next;
2156 } while (nte_array != note_data);
2157 }
2158 if (prev_note) {
2159 prev_note->next = note_data->replay;
2160 } else {
2161 mdi->note = note_data->replay;
2162 }
2163 note_data->replay->next = note_data->next;
2164 note_data = note_data->replay;
2165 note_data->active = 1;
2166 }
2167 } else {
2168 note_data->active = 0;
2169 {
2170 struct _note *prev_note = NULL;
2171 struct _note *nte_array = mdi->note;
2172
2173 if (nte_array != note_data) {
2174 do {
2175 prev_note = nte_array;
2176 nte_array = nte_array->next;
2177 } while ((nte_array != note_data)
2178 && (nte_array));
2179 }
2180 if (prev_note) {
2181 prev_note->next = note_data->next;
2182 } else {
2183 mdi->note = note_data->next;
2184 }
2185 note_data = note_data->next;
2186 }
2187 }
2188 continue;
2189 }
2190 note_data->env++;
2191
2192 if (note_data->is_off == 1) {
2193 do_note_off_extra(note_data);
2194 }
2195
2196 if (note_data->env_level
2197 > note_data->sample->env_target[note_data->env]) {
2198 note_data->env_inc =
2199 -note_data->sample->env_rate[note_data->env];
2200 } else {
2201 note_data->env_inc =
2202 note_data->sample->env_rate[note_data->env];
2203 }
2204 note_data = note_data->next;
2205 continue;
2206 }
2207
2208 /*
2209 * =========================
2210 * mix the channels together
2211 * =========================
2212 */
2213 }
2214 *buffer++ = left_mix;
2215 *buffer++ = right_mix;
2216 } while (--count);
2217 return buffer;
2218 }
2219
WM_Mix_Gauss(midi * handle,int * buffer,unsigned long int count)2220 static int *WM_Mix_Gauss(midi * handle, int * buffer, unsigned long int count)
2221 {
2222 if (!gauss_table.size()) init_gauss();
2223
2224 struct _mdi *mdi = (struct _mdi *)handle;
2225 unsigned long int data_pos;
2226 signed int premix, left_mix, right_mix;
2227 struct _note *note_data = NULL;
2228 signed short int *sptr;
2229 double y, xd;
2230 double *gptr, *gend;
2231 int left, right, temp_n;
2232 int ii, jj;
2233
2234 do {
2235 note_data = mdi->note;
2236 left_mix = right_mix = 0;
2237 if (note_data != NULL) {
2238 while (note_data) {
2239 /*
2240 * ===================
2241 * resample the sample
2242 * ===================
2243 */
2244 data_pos = note_data->sample_pos >> FPBITS;
2245
2246 /* check to see if we're near one of the ends */
2247 left = data_pos;
2248 right = (note_data->sample->data_length >> FPBITS) - left
2249 - 1;
2250 temp_n = (right << 1) - 1;
2251 if (temp_n <= 0)
2252 temp_n = 1;
2253 if (temp_n > (left << 1) + 1)
2254 temp_n = (left << 1) + 1;
2255
2256 /* use Newton if we can't fill the window */
2257 if (temp_n < gauss_n) {
2258 xd = note_data->sample_pos & FPMASK;
2259 xd /= (1L << FPBITS);
2260 xd += temp_n >> 1;
2261 y = 0;
2262 sptr = note_data->sample->data
2263 + (note_data->sample_pos >> FPBITS)
2264 - (temp_n >> 1);
2265 for (ii = temp_n; ii;) {
2266 for (jj = 0; jj <= ii; jj++)
2267 y += sptr[jj] * newt_coeffs[ii][jj];
2268 y *= xd - --ii;
2269 }
2270 y += *sptr;
2271 } else { /* otherwise, use Gauss as usual */
2272 y = 0;
2273 gptr = &gauss_table[(note_data->sample_pos & FPMASK) *
2274 (gauss_n + 1)];
2275 gend = gptr + gauss_n;
2276 sptr = note_data->sample->data
2277 + (note_data->sample_pos >> FPBITS)
2278 - (gauss_n >> 1);
2279 do {
2280 y += *(sptr++) * *(gptr++);
2281 } while (gptr <= gend);
2282 }
2283
2284 premix = (int)((y * (note_data->env_level >> 12)) / 1024);
2285
2286 left_mix += (premix * (int)note_data->left_mix_volume) / 1024;
2287 right_mix += (premix * (int)note_data->right_mix_volume) / 1024;
2288
2289 /*
2290 * ========================
2291 * sample position checking
2292 * ========================
2293 */
2294 note_data->sample_pos += note_data->sample_inc;
2295 if (note_data->sample_pos > note_data->sample->loop_end)
2296 {
2297 if (note_data->modes & SAMPLE_LOOP) {
2298 note_data->sample_pos =
2299 note_data->sample->loop_start
2300 + ((note_data->sample_pos
2301 - note_data->sample->loop_start)
2302 % note_data->sample->loop_size);
2303 } else if (note_data->sample_pos >= note_data->sample->data_length) {
2304 goto END_THIS_NOTE;
2305 }
2306 }
2307
2308 if (note_data->env_inc == 0) {
2309 note_data = note_data->next;
2310 continue;
2311 }
2312
2313 note_data->env_level += note_data->env_inc;
2314 if (note_data->env_inc < 0) {
2315 if (note_data->env_level
2316 > note_data->sample->env_target[note_data->env]) {
2317 note_data = note_data->next;
2318 continue;
2319 }
2320 } else if (note_data->env_inc > 0) {
2321 if (note_data->env_level
2322 < note_data->sample->env_target[note_data->env]) {
2323 note_data = note_data->next;
2324 continue;
2325 }
2326 }
2327
2328 // Yes could have a condition here but
2329 // it would create another bottleneck
2330
2331 note_data->env_level =
2332 note_data->sample->env_target[note_data->env];
2333 switch (note_data->env) {
2334 case 0:
2335 if (!(note_data->modes & SAMPLE_ENVELOPE)) {
2336 note_data->env_inc = 0;
2337 note_data = note_data->next;
2338 continue;
2339 }
2340 break;
2341 case 2:
2342 if (note_data->modes & SAMPLE_SUSTAIN) {
2343 note_data->env_inc = 0;
2344 note_data = note_data->next;
2345 continue;
2346 } else if (note_data->modes & SAMPLE_CLAMPED) {
2347 note_data->env = 5;
2348 if (note_data->env_level
2349 > note_data->sample->env_target[5]) {
2350 note_data->env_inc =
2351 -note_data->sample->env_rate[5];
2352 } else {
2353 note_data->env_inc =
2354 note_data->sample->env_rate[5];
2355 }
2356 continue;
2357 }
2358 break;
2359 case 5:
2360 if (note_data->env_level == 0) {
2361 goto END_THIS_NOTE;
2362 }
2363 /* sample release */
2364 if (note_data->modes & SAMPLE_LOOP)
2365 note_data->modes ^= SAMPLE_LOOP;
2366 note_data->env_inc = 0;
2367 note_data = note_data->next;
2368 continue;
2369 case 6:
2370 END_THIS_NOTE:
2371 if (note_data->replay != NULL) {
2372 note_data->active = 0;
2373 {
2374 struct _note *prev_note = NULL;
2375 struct _note *nte_array = mdi->note;
2376
2377 if (nte_array != note_data) {
2378 do {
2379 prev_note = nte_array;
2380 nte_array = nte_array->next;
2381 } while (nte_array != note_data);
2382 }
2383 if (prev_note) {
2384 prev_note->next = note_data->replay;
2385 } else {
2386 mdi->note = note_data->replay;
2387 }
2388 note_data->replay->next = note_data->next;
2389 note_data = note_data->replay;
2390 note_data->active = 1;
2391 }
2392 } else {
2393 note_data->active = 0;
2394 {
2395 struct _note *prev_note = NULL;
2396 struct _note *nte_array = mdi->note;
2397
2398 if (nte_array != note_data) {
2399 do {
2400 prev_note = nte_array;
2401 nte_array = nte_array->next;
2402 } while ((nte_array != note_data)
2403 && (nte_array));
2404 }
2405 if (prev_note) {
2406 prev_note->next = note_data->next;
2407 } else {
2408 mdi->note = note_data->next;
2409 }
2410 note_data = note_data->next;
2411 }
2412 }
2413 continue;
2414 }
2415 note_data->env++;
2416
2417 if (note_data->is_off == 1) {
2418 do_note_off_extra(note_data);
2419 }
2420
2421 if (note_data->env_level
2422 > note_data->sample->env_target[note_data->env]) {
2423 note_data->env_inc =
2424 -note_data->sample->env_rate[note_data->env];
2425 } else {
2426 note_data->env_inc =
2427 note_data->sample->env_rate[note_data->env];
2428 }
2429 note_data = note_data->next;
2430 continue;
2431 }
2432
2433 /*
2434 * =========================
2435 * mix the channels together
2436 * =========================
2437 */
2438 }
2439 *buffer++ = left_mix;
2440 *buffer++ = right_mix;
2441 } while (--count);
2442 return buffer;
2443 }
2444
WM_Mix(midi * handle,int * buffer,unsigned long count)2445 int *WM_Mix(midi *handle, int *buffer, unsigned long count)
2446 {
2447 if (((struct _mdi *)handle)->info.mixer_options & WM_MO_ENHANCED_RESAMPLING)
2448 {
2449 return WM_Mix_Gauss(handle, buffer, count);
2450 }
2451 else
2452 {
2453 return WM_Mix_Linear(handle, buffer, count);
2454 }
2455 }
2456
2457 /*
2458 * =========================
2459 * External Functions
2460 * =========================
2461 */
2462
WildMidi_GetString(unsigned short int info)2463 const char *WildMidi_GetString(unsigned short int info)
2464 {
2465 switch (info)
2466 {
2467 case WM_GS_VERSION:
2468 return WM_Version;
2469 }
2470 return NULL;
2471 }
2472
2473
NewMidi()2474 midi * Renderer::NewMidi()
2475 {
2476 midi * ret = NULL;
2477
2478 ret = Init_MDI();
2479
2480 ((_mdi*)ret)->reverb = _WM_init_reverb(instruments->GetSampleRate(), instruments->reverb_room_width,
2481 instruments->reverb_room_length, instruments->reverb_listen_posx, instruments->reverb_listen_posy);
2482 return ret;
2483 }
2484
SetOption(int options,int setting)2485 int Renderer::SetOption(int options, int setting)
2486 {
2487 struct _mdi *mdi;
2488
2489 if (handle == NULL) {
2490 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)",
2491 0);
2492 return -1;
2493 }
2494
2495 mdi = (struct _mdi *) handle;
2496 if ((!(options & 0x0007)) || (options & 0xFFF8)) {
2497 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(invalid option)",
2498 0);
2499 return -1;
2500 }
2501 if (setting & 0xFFF8) {
2502 _WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG,
2503 "(invalid setting)", 0);
2504 return -1;
2505 }
2506
2507 mdi->info.mixer_options = ((mdi->info.mixer_options & (0x00FF ^ options))
2508 | (options & setting));
2509
2510 if (options & WM_MO_LOG_VOLUME) {
2511 AdjustChannelVolumes(mdi, 16); // Settings greater than 15
2512 // adjust all channels
2513 } else if (options & WM_MO_REVERB) {
2514 _WM_reset_reverb(mdi->reverb);
2515 }
2516
2517 return 0;
2518 }
2519
2520
Renderer(Instruments * instr,unsigned mixOpt)2521 Renderer::Renderer(Instruments *instr, unsigned mixOpt)
2522 {
2523 init_gauss();
2524 instruments = instr;
2525 WM_MixerOptions = mixOpt;
2526 handle = NewMidi();
2527 }
2528
~Renderer()2529 Renderer::~Renderer()
2530 {
2531 freeMDI((_mdi *)handle);
2532 }
2533
ShortEvent(int status,int parm1,int parm2)2534 void Renderer::ShortEvent(int status, int parm1, int parm2)
2535 {
2536 _mdi *mdi = (_mdi *)handle;
2537 _event_data ev;
2538
2539 ev.channel = status & 0x0F;
2540 switch ((status & 0xF0) >> 4) // command
2541 {
2542 case 0x8:
2543 ev.data = (parm1 << 8) | parm2;
2544 do_note_off(mdi, &ev);
2545 break;
2546
2547 case 0x9:
2548 ev.data = (parm1 << 8) | parm2;
2549 do_note_on(mdi, &ev);
2550 break;
2551
2552 case 0xA:
2553 ev.data = (parm1 << 8) | parm2;
2554 do_aftertouch(mdi, &ev);
2555 break;
2556
2557 case 0xC:
2558 ev.data = parm1;
2559 do_patch(mdi, &ev);
2560 break;
2561
2562 case 0xD:
2563 ev.data = parm1;
2564 do_channel_pressure(mdi, &ev);
2565 break;
2566
2567 case 0xE:
2568 ev.data = parm1 | (parm2 << 7);
2569 do_pitch(mdi, &ev);
2570 break;
2571
2572 case 0xB: // Controllers
2573 ev.data = parm2;
2574 switch (parm1)
2575 {
2576 case 0: do_control_bank_select(mdi, &ev); break;
2577 case 6: do_control_data_entry_course(mdi, &ev); break; // [sic]
2578 case 7: do_control_channel_volume(mdi, &ev); break;
2579 case 8: do_control_channel_balance(mdi, &ev); break;
2580 case 10: do_control_channel_pan(mdi, &ev); break;
2581 case 11: do_control_channel_expression(mdi, &ev); break;
2582 case 38: do_control_data_entry_fine(mdi, &ev); break;
2583 case 64: do_control_channel_hold(mdi, &ev); break;
2584 case 96: do_control_data_increment(mdi, &ev); break;
2585 case 97: do_control_data_decrement(mdi, &ev); break;
2586 case 98: do_control_non_registered_param_fine(mdi, &ev); break;
2587 case 99: do_control_non_registered_param_course(mdi, &ev); break; // [sic]
2588 case 100: do_control_registered_param_fine(mdi, &ev); break;
2589 case 101: do_control_registered_param_course(mdi, &ev); break; // [sic]
2590 case 120: do_control_channel_sound_off(mdi, &ev); break;
2591 case 121: do_control_channel_controllers_off(mdi, &ev); break;
2592 case 123: do_control_channel_notes_off(mdi, &ev); break;
2593 }
2594 }
2595 }
2596
LongEvent(const unsigned char * data,int len)2597 void Renderer::LongEvent(const unsigned char *data, int len)
2598 {
2599 // Check for Roland SysEx
2600 if (len >= 11 && // Must be at least 11 bytes
2601 data[len-1] == 0xF7 && // SysEx end
2602 data[0] == 0xF0 && // SysEx
2603 data[1] == 0x41 && // Roland
2604 data[2] == 0x10 && // Device ID, defaults to 0x10
2605 data[3] == 0x42 && // Model ID, 0x42 indicates a GS synth
2606 data[4] == 0x12 && // The other end is sending data to us
2607 data[5] == 0x40) // We only care about addresses with this first byte
2608 {
2609 // Calculate checksum
2610 int cksum = 0;
2611 for (int i = 5; i < len - 2; ++i)
2612 {
2613 cksum += data[i];
2614 }
2615 cksum = 128 - (cksum & 0x7F);
2616 if (data[len-2] == cksum)
2617 { // Check destination address
2618 if (((data[6] & 0xF0) == 0x10) && data[7] == 0x15)
2619 { // Roland drum track setting
2620 unsigned char sysex_ch = data[6] & 0x0F;
2621 if (sysex_ch == 0)
2622 {
2623 sysex_ch = 9;
2624 }
2625 else if (sysex_ch <= 9)
2626 {
2627 sysex_ch -= 1;
2628 }
2629 _event_data ev = { sysex_ch, data[8] };
2630 do_sysex_roland_drum_track((_mdi *)handle, &ev);
2631 }
2632 else if (data[6] == 0x00 && data[7] == 0x7F && data[8] == 0x00)
2633 { // Roland GS reset
2634 do_sysex_roland_reset((_mdi *)handle, NULL);
2635 }
2636 }
2637 }
2638 // For non-Roland Sysex messages */
2639 else
2640 {
2641 const unsigned char gm_reset[] = { 0xf0, 0x7e, 0x7f, 0x09, 0x01, 0xf7 };
2642 const unsigned char yamaha_reset[] = { 0xf0, 0x43, 0x10, 0x4c, 0x00, 0x00, 0x7e, 0x00, 0xf7};
2643 if (len == 6 && memcmp(gm_reset, data, 6) == 0)
2644 {
2645 do_sysex_gm_reset((_mdi *)handle, NULL);
2646 }
2647 else if (len == 9 && memcmp(yamaha_reset, data, 9) == 0)
2648 {
2649 do_sysex_yamaha_reset((_mdi *)handle, NULL);
2650 }
2651 }
2652 }
2653
ComputeOutput(float * fbuffer,int len)2654 void Renderer::ComputeOutput(float *fbuffer, int len)
2655 {
2656 _mdi *mdi = (_mdi *)handle;
2657 int *buffer = (int *)fbuffer;
2658 int *newbuf = WM_Mix(handle, buffer, len);
2659 // assert(newbuf - buffer == len);
2660 if (mdi->info.mixer_options & WM_MO_REVERB) {
2661 _WM_do_reverb(mdi->reverb, buffer, len * 2);
2662 }
2663 for (; buffer < newbuf; ++buffer)
2664 {
2665 *(float *)buffer = (float)*buffer * (1.3f / 32768.f); // boost the volume because Wildmidi is far more quiet than the other synths and therefore hard to balance.
2666 }
2667 }
2668
LoadInstrument(int bank,int percussion,int instr)2669 void Renderer::LoadInstrument(int bank, int percussion, int instr)
2670 {
2671 instruments->load_patch((_mdi *)handle, (bank << 8) | instr | (percussion ? 0x80 : 0));
2672 }
2673
GetVoiceCount()2674 int Renderer::GetVoiceCount()
2675 {
2676 int count = 0;
2677 for (_note *note_data = ((_mdi *)handle)->note; note_data != NULL; note_data = note_data->next)
2678 {
2679 count++;
2680 }
2681 return count;
2682 }
2683
2684
SetMasterVolume(unsigned char master_volume)2685 void Renderer::SetMasterVolume(unsigned char master_volume)
2686 {
2687 WM_MasterVolume = lin_volume[std::min<unsigned char>(127, master_volume)];
2688 }
2689
2690 }
2691