1 /*
2 	wildmidi_lib.c
3 
4  	Midi Wavetable Processing library
5 
6  	Copyright (C)2001-2004 Chris Ison
7 
8 	This library is free software; you can redistribute it and/or
9 	modify it under the terms of the GNU Lesser General Public
10 	License as published by the Free Software Foundation; either
11 	version 2.1 of the License, or (at your option) any later version.
12 
13 	This library is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 	Lesser General Public License for more details.
17 
18 	You should have received a copy of the GNU Lesser General Public
19 	License along with this library; if not, write to the Free Software
20 	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 
22 	Email: cisos@bigpond.net.au
23 		 wildcode@users.sourceforge.net
24 
25  	$Id: wildmidi_lib.c,v 1.18 2004/01/28 05:45:09 wildcode Exp $
26 
27 	========================================
28 	Changelog
29 	----------------------------------------
30 	Aug 3, 2003
31 		- changed kill envelope from 6th to 5th
32 		- changed data_length, loop_start, loop_end to 22:10 fixed point
33 		- added fractional position to loop_start and loop_end
34 		- added fake reverb
35 
36 	Aug 4, 2003
37 		- added MIDI_EVENT_DEBUG to midi event functions
38 		- fixed hold release killing off notes that hadn't been turned off
39 		  by note off
40 		- sped up fake reverb by doing it outside the channel mixing loop
41 
42 	Aug 5, 2003
43 		- removed note_table init from ParseNewMidi, entries are now fully reset
44 		  in do_note_on
45 		- moved fast kill envelope setting to sample loading
46 		- removed envelopes from notes, use ones from sample instead
47 		- optimized do_amp_setup functions
48 		- do_control_volume, do_control_expression, do_channel_pressure,
49 		  changed from setting note_table[] to note[] for performance.
50 		- optimizations of sample conversion,
51 
52 	Aug 6, 2003
53 		- removed changing sample volumes, the amp setting is now apart of
54 		  the midi volume maths.
55 		- re-write conversion functions to make them more clearer and less bug prone
56 
57 	Aug 7, 2003
58 		- fixed volume, expression and preasure changes effecting all notes.
59 
60 	Aug 8, 2003
61 		- spead up midi processing by using an event index
62 
63 	Aug 9, 2003
64 		- spead up sampling and mixing by using a seperate function depending on fixed
65 		  modes
66 		- fixed data lock where it would sleep reguardless of lock state
67 		- fixed memory leak ... oops, forgot a free
68 		- removed track data storge, isn't required by the core functions
69 
70 	Aug 10, 2003
71 		- created error function and changed all error messages to use it
72 		- malloc, calloc, realloc audit ensuring all are error checked.
73 		- fixed potential reading beyond end of midi bug
74 
75 	Aug 11, 2003
76 		- fixed expensive interpolation over-running the sample buffer
77 		- changed stereo option so that changing it worked right away
78 
79 	Aug 14, 2003
80 		- optimizations for and to the frequency calc code
81 		- removal of wide stereo (it sucked anyway)
82 
83 	Aug 15, 2003
84 		- fixed volume levels for panning when non-linear volumes are used
85 		- removed fake reberb, it sucked on better sound systems.
86 
87 	Aug 17, 2003
88 		- fixed autoamp
89 		- added env_time#= and env_level#= to timiidty.cfg parser.
90 		- fixed bug where last event in the midi before the last eot would
91 		  have a large delta. This is a bug in the midi file itself.
92 		- fixed some midi's having no sound cause they don't supply patch information.
93 		  Now defaulting all channels to bank 0, patch 0.
94 
95 	Aug 18, 2003
96 		- preload samples
97 		- optimized envelope checking and controler code
98 		- fixed bug where some samples have an envelope rate setting that doesn't
99 		  actually mean anything .. ie: 0x00 0x40 0x80 0xC0
100 		- fixed amp bug where initial left/right levels were set to 0
101 		- added timidity's keep=[loop|env] support for drum patches,
102 		  now ignores loops and env settings for drum patches unless keep=[loop|env]
103 		  is in the patch line in the config.
104 
105 	Aug 21, 2003
106 		- float to fixed point math conversions.
107 		- frequency range locked to 100 steps between notes
108 		  NOTE:need to test slow pitchbends with this, fast ones are fine
109 
110 	Aug 24, 2003
111 		- optimized sample conversions
112 		- optimized note handling and sample/envelope position checks
113 
114 	Aug 28, 2003
115 		- compile level optimizations
116 		  NOTE: gcc builtins used
117 		- numerous bug fixes
118 
119 	Aug 30, 2003
120 		- fixed sample inc calculation bug
121 		- fixed panning volumes, now percieved volume of the sample is the same no matter
122 		  panning position.
123 
124 `	Sep 2, 2003
125 		- made noteoff/hold behaviour match midi standard
126 		- added remove=sustain to patch line
127 		- added all sound off controller
128 		- fixed all notes off to only effect notes that aren't being held
129 		- changed envelope behaviour so that only non-sustaned samples hit envelope 4
130 		  while note is on.
131 		- Added all controllers off event
132 
133 
134 	Sep 4, 2003
135 		- added master sample data lock
136 		- improved performance of the resampling algo
137 
138 	========================================
139 */
140 #ifdef HAVE_CONFIG_H
141 #  include <config.h>
142 #endif
143 #include <ctype.h>
144 #include <errno.h>
145 #include <fcntl.h>
146 #include <math.h>
147 #ifndef _WIN32
148 #include <pwd.h>
149 #endif
150 #include <stdarg.h>
151 #include <stdio.h>
152 #include <stdlib.h>
153 #include <string.h>
154 #include <sys/stat.h>
155 #include <sys/types.h>
156 #include <unistd.h>
157 #ifdef _WIN32
158 # include <windows.h>
159 #endif
160 #include "wildmidi_lib.h"
161 
162 /*
163  * =========================
164  * Global Data and Data Structs
165  * =========================
166  */
167 
168 int WM_Initialized = 0;
169 signed short int WM_MasterVolume = 948;
170 unsigned short int WM_SampleRate = 0;
171 unsigned short int WM_MixerOptions = 0;
172 
173 char WM_Version[] = "WildMidi Processing Library " WILDMIDILIB_VERSION;
174 
175 struct _lowpass {
176 	signed long int in[2];
177 	signed long int out[2];
178 };
179 
180 struct _filter {
181 	signed long int *delay[4][2];
182 	unsigned long int delay_pos[4][2];
183 	struct _lowpass lowpass[4][2];
184 	signed long int in[2][2];
185 	signed long int out[2][2];
186 };
187 
188 struct _env {
189 	float time;
190 	float level;
191 	unsigned char set;
192 };
193 
194 struct _sample {
195 	unsigned long int data_length;
196 	unsigned long int loop_start;
197 	unsigned long int loop_end;
198 	unsigned long int loop_size;
199 	unsigned char loop_fraction;
200 	unsigned short int rate;
201 	unsigned long int freq_low;
202 	unsigned long int freq_high;
203 	unsigned long int freq_root;
204 	unsigned char modes;
205 	unsigned long int env_rate[7];
206 	unsigned long int env_target[7];
207 	unsigned long int inc_div;
208 	signed short *data;
209 	signed short max_peek;
210 	signed short min_peek;
211 	signed long int peek_adjust;
212 	struct _sample *next;
213 };
214 
215 struct _patch {
216 	unsigned short patchid;
217 	unsigned char loaded;
218 	char *filename;
219 	signed short int amp;
220 	unsigned char keep;
221 	unsigned char remove;
222 	struct _env env[6];
223 	unsigned char note;
224 	unsigned long int inuse_count;
225 	struct _sample *first_sample;
226 	struct _patch *next;
227 };
228 
229 struct _patch *patch[128];
230 
231 int patch_lock;
232 
233 struct _channel {
234 	unsigned char bank;
235 	struct _patch *patch;
236 	unsigned char hold;
237 	unsigned char volume;
238 	unsigned char pressure;
239 	unsigned char expression;
240 	signed char balance;
241 	signed char pan;
242 	signed short int left_adjust;
243 	signed short int right_adjust;
244 	signed short int pitch;
245 	signed short int pitch_range;
246 	signed long int pitch_adjust;
247 	unsigned short reg_data;
248 };
249 
250 #define HOLD_OFF 0x02
251 
252 struct _note {
253 	unsigned short noteid;
254 	unsigned char velocity;
255 	struct _patch *patch;
256 	struct _sample *sample;
257 	unsigned long int sample_pos;
258 	unsigned long int sample_inc;
259 	signed long int env_inc;
260 	unsigned char env;
261 	unsigned long int env_level;
262 	unsigned char modes;
263 	unsigned char hold;
264 	unsigned char active;
265 	struct _note *next;
266 	signed short int vol_lvl;
267 };
268 
269 struct _miditrack {
270 	unsigned long int length;
271 	unsigned long int ptr;
272 	unsigned long int delta;
273 	unsigned char running_event;
274 	unsigned char EOT;
275 };
276 
277 struct _mdi_patches {
278 	struct _patch *patch;
279 	struct _mdi_patch *next;
280 };
281 
282 struct _mdi_index {
283 	unsigned long int offset;
284 	unsigned char event;
285 	unsigned long int delta;
286 };
287 
288 struct _mdi {
289 	int lock;
290 	unsigned char *data;
291 	unsigned long int size;
292 	unsigned short int divisions ;
293 	unsigned short midi_master_vol;
294 	unsigned long int samples_per_delta;
295 	unsigned long int samples_to_mix;
296 	struct _mdi_index * index;
297 	unsigned long int index_count;
298 	unsigned long int index_size;
299 	struct _WM_Info info;
300 	struct _WM_Info *tmp_info;
301 	unsigned char recalc_samples;
302 	struct _channel channel[16];
303 	struct _note *note[128];
304 	struct _note **last_note;
305 	struct _note note_table[2][16][128];
306 
307 	struct _patch **patches;
308 	unsigned long int patch_count;
309 	unsigned long int sample_count;
310 	signed short int amp;
311 
312 // setup data for auto amp
313 	signed long int log_cur_vol;
314 	signed long int lin_cur_vol;
315 	signed long int log_max_vol;
316 	signed long int lin_max_vol;
317 
318 	unsigned char ch_vol[16];
319 	unsigned char ch_exp[16];
320 	unsigned char note_vel[16][128];
321 
322 	struct _filter filter;
323 };
324 
325 /* Gauss Interpolation code adapted from code supplied by Eric. A. Welsh */
326 
327 double newt_coeffs[58][58];		/* for start/end of samples */
328 float *gauss_table[(1<<10)] = {0};	/* don't need doubles */
329 int gauss_window[35] = {0};
330 int gauss_n = 34;	/* do not set this value higher than 34 */
331 			/* 34 is as high as we can go before errors crop up */
332 
init_gauss(void)333 void init_gauss (void) {
334 	/* init gauss table */
335 	int n = 34;
336 	int m, i, k, n_half = (n>>1);
337 	int j;
338 	int sign;
339 	double ck;
340 	double x, x_inc, xz;
341 	double z[35];
342 	float *gptr;
343 
344 	newt_coeffs[0][0] = 1;
345 
346 	for (i = 0; i <= n; i++) {
347 		newt_coeffs[i][0] = 1;
348 		newt_coeffs[i][i] = 1;
349 
350 		if (i > 1) {
351 			newt_coeffs[i][0] = newt_coeffs[i-1][0] / i;
352 			newt_coeffs[i][i] = newt_coeffs[i-1][0] / i;
353 		}
354 
355 		for (j = 1; j < i; j++) {
356 			newt_coeffs[i][j] = newt_coeffs[i-1][j-1] + newt_coeffs[i-1][j];
357 			if (i > 1)
358 				newt_coeffs[i][j] /= i;
359 		}
360 		z[i] = i / (4*M_PI);
361 	}
362 
363 	for (i = 0; i <= n; i++)
364 		for (j = 0, sign = pow(-1, i); j <= i; j++, sign *= -1)
365 			newt_coeffs[i][j] *= sign;
366 
367 
368 	x_inc = 1.0 / (1<<10);
369 	for (m = 0, x = 0.0; m < (1<<10); m++, x += x_inc) {
370 		xz = (x + n_half) / (4*M_PI);
371 		gptr = gauss_table[m] = realloc(gauss_table[m], (n+1)*sizeof(float));
372 
373 		for (k = 0; k <= n; k++) {
374 			ck = 1.0;
375 
376 			for (i = 0; i <= n; i++) {
377 				if (i == k)
378 					continue;
379 
380 				ck *= (sin(xz - z[i])) / (sin(z[k] - z[i]));
381     			}
382 			*gptr++ = ck;
383 		}
384 	}
385 }
386 
387 void
free_gauss(void)388 free_gauss (void) {
389     for (int i = 0; i < (1<<10); i++) {
390         if (gauss_table[i]) {
391             free (gauss_table[i]);
392         }
393         gauss_table[i] = NULL;
394     }
395 }
396 
397 unsigned long int delay_size[4][2];
398 signed long int a[5][2];
399 signed long int b[5][2];
400 signed long int gain_in[4];
401 signed long int gain_out[4];
402 
init_lowpass(void)403 void init_lowpass (void) {
404 	float c = 0;
405 	int i;
406 	float f[] = { 512.0, 1024.0, 2048.0, 4096.0 , 8192.0};
407 	float aa, ab, ba, bb;
408 
409 	for (i = 0; i < 5; i++) {
410 		c = 1.0 / tan(3.141592654 * f[i] / WM_SampleRate);
411 		aa = 1.0 / (1.0 + 1.4 * c + c * c);
412 		ab = 2.0 * aa;
413 		ba = 2.0 * (1.0 - c * c) * aa;
414 		bb = (1.0 - 1.4 * c + c * c) * aa;
415 		a[i][0] = (signed long int)(aa * 1024.0);
416 		a[i][1] = (signed long int)(ab * 1024.0);
417 		b[i][0] = (signed long int)(ba * 1024.0);
418 		b[i][1] = (signed long int)(bb * 1024.0);
419 	}
420 	gain_in[0] = 772;
421 	gain_out[0] = 772;
422 	gain_in[1] = 570;
423 	gain_out[1] = 570;
424 	gain_in[2] = 520;
425 	gain_out[2] = 520;
426 	gain_in[3] = 512;
427 	gain_out[3] = 512;
428 
429 	delay_size[0][0] = 2191 * WM_SampleRate / 44100;
430 	delay_size[0][1] = (2191 + 19) * WM_SampleRate / 44100;
431 	delay_size[1][0] = (2971 + 19) * WM_SampleRate / 44100;
432 	delay_size[1][1] = 2971 * WM_SampleRate / 44100;
433 	delay_size[2][0] = 3253 * WM_SampleRate / 44100;
434 	delay_size[2][1] = (3253 + 19) * WM_SampleRate / 44100;
435 	delay_size[3][0] = (3307 + 19) * WM_SampleRate / 44100;
436 	delay_size[3][1] = 3307 * WM_SampleRate / 44100;
437 
438 }
439 
440 struct _hndl {
441 	void * handle;
442 	struct _hndl *next;
443 	struct _hndl *prev;
444 };
445 
446 struct _hndl * first_handle = NULL;
447 
448 //f: ( VOLUME / 127 )
449 //f: pow(( VOLUME / 127 ), 1.660964047 )
450 //f: pow(( VOLUME / 127 ), 2.0 )
451 //f: pow(( VOLUME / 127 ), 0.602059991 )
452 //f: pow(( VOLUME / 127 ), 0.5 )
453 
454 signed short int lin_volume[] = { 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96, 104, 112, 120, 129, 137, 145, 153, 161, 169, 177, 185, 193, 201, 209, 217, 225, 233, 241, 249, 258, 266, 274, 282, 290, 298, 306, 314, 322, 330, 338, 346, 354, 362, 370, 378, 387, 395, 403, 411, 419, 427, 435, 443, 451, 459, 467, 475, 483, 491, 499, 507, 516, 524, 532, 540, 548, 556, 564, 572, 580, 588, 596, 604, 612, 620, 628, 636, 645, 653, 661, 669, 677, 685, 693, 701, 709, 717, 725, 733, 741, 749, 757, 765, 774, 782, 790, 798, 806, 814, 822, 830, 838, 846, 854, 862, 870, 878, 886, 894, 903, 911, 919, 927, 935, 943, 951, 959, 967, 975, 983, 991, 999, 1007, 1015, 1024 };
455 signed short int log_volume[] = { 0, 0, 1, 2, 3, 4, 6, 8, 10, 12, 15, 17, 20, 23, 26, 29, 32, 36, 39, 43, 47, 51, 55, 59, 64, 68, 73, 78, 83, 88, 93, 98, 103, 109, 114, 120, 126, 132, 138, 144, 150, 156, 162, 169, 176, 182, 189, 196, 203, 210, 217, 224, 232, 239, 247, 255, 262, 270, 278, 286, 294, 302, 311, 319, 328, 336, 345, 353, 362, 371, 380, 389, 398, 408, 417, 426, 436, 446, 455, 465, 475, 485, 495, 505, 515, 525, 535, 546, 556, 567, 577, 588, 599, 610, 621, 632, 643, 654, 665, 677, 688, 699, 711, 723, 734, 746, 758, 770, 782, 794, 806, 818, 831, 843, 855, 868, 880, 893, 906, 919, 931, 944, 957, 970, 984, 997, 1010, 1024 };
456 signed short int sqr_volume[] = { 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 7, 9, 10, 12, 14, 16, 18, 20, 22, 25, 27, 30, 33, 36, 39, 42, 46, 49, 53, 57, 61, 65, 69, 73, 77, 82, 86, 91, 96, 101, 106, 111, 117, 122, 128, 134, 140, 146, 152, 158, 165, 171, 178, 185, 192, 199, 206, 213, 221, 228, 236, 244, 251, 260, 268, 276, 284, 293, 302, 311, 320, 329, 338, 347, 357, 366, 376, 386, 396, 406, 416, 426, 437, 447, 458, 469, 480, 491, 502, 514, 525, 537, 549, 560, 572, 585, 597, 609, 622, 634, 647, 660, 673, 686, 699, 713, 726, 740, 754, 768, 782, 796, 810, 825, 839, 854, 869, 884, 899, 914, 929, 944, 960, 976, 992, 1007, 1024 };
457 //signed short int pan_volume[] = { 0, 55, 84, 107, 127, 146, 162, 178, 193, 208, 221, 234, 247, 259, 271, 282, 294, 305, 315, 326, 336, 346, 356, 366, 375, 384, 394, 403, 412, 420, 429, 438, 446, 454, 463, 471, 479, 487, 495, 503, 510, 518, 525, 533, 540, 548, 555, 562, 570, 577, 584, 591, 598, 605, 611, 618, 625, 632, 638, 645, 651, 658, 664, 671, 677, 684, 690, 696, 703, 709, 715, 721, 727, 733, 739, 745, 751, 757, 763, 769, 775, 781, 786, 792, 798, 804, 809, 815, 821, 826, 832, 837, 843, 848, 854, 859, 865, 870, 876, 881, 886, 892, 897, 902, 907, 913, 918, 923, 928, 933, 939, 944, 949, 954, 959, 964, 969, 974, 979, 984, 989, 994, 999, 1004, 1009, 1014, 1019, 1024 };
458 signed short int pan_volume[] = { 0, 90, 128, 157, 181, 203, 222, 240, 257, 272, 287, 301, 314, 327, 339, 351, 363, 374, 385, 396, 406, 416, 426, 435, 445, 454, 463, 472, 480, 489, 497, 505, 514, 521, 529, 537, 545, 552, 560, 567, 574, 581, 588, 595, 602, 609, 616, 622, 629, 636, 642, 648, 655, 661, 667, 673, 679, 686, 692, 697, 703, 709, 715, 721, 726, 732, 738, 743, 749, 754, 760, 765, 771, 776, 781, 786, 792, 797, 802, 807, 812, 817, 822, 827, 832, 837, 842, 847, 852, 857, 862, 866, 871, 876, 880, 885, 890, 894, 899, 904, 908, 913, 917, 922, 926, 931, 935, 939, 944, 948, 953, 957, 961, 965, 970, 974, 978, 982, 987, 991, 995, 999, 1003, 1007, 1011, 1015, 1019, 1024 };
459 
460 unsigned long int reverb_val = 92;
461 unsigned long int comb_size[8][2];
462 unsigned long int allpass_size[2][2];
463 
464 float env_time_table[] = {
465 	0.0, 0.092857143, 0.046428571, 0.030952381, 0.023214286, 0.018571429, 0.015476190, 0.013265306, 0.011607143, 0.010317460, 0.009285714, 0.008441558, 0.007738095, 0.007142857, 0.006632653, 0.006190476, 0.005803571, 0.005462185, 0.005158730, 0.004887218, 0.004642857, 0.004421769, 0.004220779, 0.004037267, 0.003869048, 0.003714286, 0.003571429, 0.003439153, 0.003316327, 0.003201970, 0.003095238, 0.002995392, 0.002901786, 0.002813853, 0.002731092, 0.002653061, 0.002579365, 0.002509653, 0.002443609, 0.002380952, 0.002321429, 0.002264808, 0.002210884, 0.002159468, 0.002110390, 0.002063492, 0.002018634, 0.001975684, 0.001934524, 0.001895044, 0.001857143, 0.001820728, 0.001785714, 0.001752022, 0.001719577, 0.001688312, 0.001658163, 0.001629073, 0.001600985, 0.001573850, 0.001547619, 0.001522248, 0.001497696, 0.001473923,
466 	0.0, 0.742857143, 0.371428571, 0.247619048, 0.185714286, 0.148571429, 0.123809524, 0.106122449, 0.092857143, 0.082539683, 0.074285714, 0.067532468, 0.061904762, 0.057142857, 0.053061224, 0.049523810, 0.046428571, 0.043697479, 0.041269841, 0.039097744, 0.037142857, 0.035374150, 0.033766234, 0.032298137, 0.030952381, 0.029714286, 0.028571429, 0.027513228, 0.026530612, 0.025615764, 0.024761905, 0.023963134, 0.023214286, 0.022510823, 0.021848739, 0.021224490, 0.020634921, 0.020077220, 0.019548872, 0.019047619, 0.018571429, 0.018118467, 0.017687075, 0.017275748, 0.016883117, 0.016507937, 0.016149068, 0.015805471, 0.015476190, 0.015160350, 0.014857143, 0.014565826, 0.014285714, 0.014016173, 0.013756614, 0.013506494, 0.013265306, 0.013032581, 0.012807882, 0.012590799, 0.012380952, 0.012177986, 0.011981567, 0.011791383,
467 	0.0, 5.942857143, 2.971428571, 1.980952381, 1.485714286, 1.188571429, 0.990476190, 0.848979592, 0.742857143, 0.660317460, 0.594285714, 0.540259740, 0.495238095, 0.457142857, 0.424489796, 0.396190476, 0.371428571, 0.349579832, 0.330158730, 0.312781955, 0.297142857, 0.282993197, 0.270129870, 0.258385093, 0.247619048, 0.237714286, 0.228571429, 0.220105820, 0.212244898, 0.204926108, 0.198095238, 0.191705069, 0.185714286, 0.180086580, 0.174789916, 0.169795918, 0.165079365, 0.160617761, 0.156390977, 0.152380952, 0.148571429, 0.144947735, 0.141496599, 0.138205980, 0.135064935, 0.132063492, 0.129192547, 0.126443769, 0.123809524, 0.121282799, 0.118857143, 0.116526611, 0.114285714, 0.112129380, 0.110052910, 0.108051948, 0.106122449, 0.104260652, 0.102463054, 0.100726392, 0.099047619, 0.097423888, 0.095852535, 0.094331066,
468 	0.0, 47.542857143, 23.771428571, 15.847619048, 11.885714286, 9.508571429, 7.923809524, 6.791836735, 5.942857143, 5.282539683, 4.754285714, 4.322077922, 3.961904762, 3.657142857, 3.395918367, 3.169523810, 2.971428571, 2.796638655, 2.641269841, 2.502255639, 2.377142857, 2.263945578, 2.161038961, 2.067080745, 1.980952381, 1.901714286, 1.828571429, 1.760846561, 1.697959184, 1.639408867, 1.584761905, 1.533640553, 1.485714286, 1.440692641, 1.398319328, 1.358367347, 1.320634921, 1.284942085, 1.251127820, 1.219047619, 1.188571429, 1.159581882, 1.131972789, 1.105647841, 1.080519481, 1.056507937, 1.033540373, 1.011550152, 0.990476190, 0.970262391, 0.950857143, 0.932212885, 0.914285714, 0.897035040, 0.880423280, 0.864415584, 0.848979592, 0.834085213, 0.819704433, 0.805811138, 0.792380952, 0.779391101, 0.766820276, 0.754648526
469 };
470 
471 #if 0
472 unsigned long int freq_table[] = {
473 	8372018, 8376855, 8381695, 8386538, 8391384, 8396232, 8401084, 8405938, 8410795, 8415654, 8420517, 8425382, 8430250, 8435121, 8439995, 8444871, 8449751, 8454633, 8459518, 8464406, 8469296, 8474190, 8479086, 8483985, 8488887, 8493792, 8498700, 8503610, 8508523, 8513439, 8518358, 8523280, 8528205, 8533132, 8538063, 8542996, 8547932, 8552871, 8557813, 8562757, 8567705, 8572655, 8577608, 8582564, 8587523, 8592485, 8597450, 8602417, 8607387, 8612361, 8617337, 8622316, 8627298, 8632282, 8637270, 8642261, 8647254, 8652250, 8657250, 8662252, 8667257, 8672264, 8677275, 8682289, 8687305, 8692325, 8697347, 8702372, 8707400, 8712431, 8717465, 8722502, 8727542, 8732585, 8737630, 8742679, 8747730, 8752785, 8757842, 8762902, 8767965, 8773031, 8778100, 8783172, 8788247, 8793325, 8798405, 8803489, 8808575, 8813665, 8818757, 8823853, 8828951, 8834052, 8839157, 8844264, 8849374, 8854487, 8859603, 8864722, 8869844, 8874969, 8880097, 8885227, 8890361, 8895498, 8900638, 8905780, 8910926, 8916075, 8921226, 8926381, 8931538, 8936699, 8941863, 8947029, 8952199, 8957371, 8962546, 8967725, 8972906, 8978091, 8983278, 8988469, 8993662, 8998859, 9004058, 9009260, 9014466, 9019674, 9024886, 9030100, 9035318, 9040538, 9045762, 9050988, 9056218, 9061451, 9066686, 9071925, 9077166, 9082411, 9087659, 9092910, 9098163, 9103420, 9108680, 9113943, 9119209, 9124478, 9129750, 9135025, 9140303, 9145584, 9150868, 9156156, 9161446, 9166739, 9172036, 9177335, 9182638, 9187944, 9193252, 9198564, 9203879, 9209197, 9214518, 9219842, 9225169, 9230499, 9235832, 9241169, 9246508, 9251851, 9257196, 9262545, 9267897, 9273252, 9278610, 9283971, 9289335, 9294702, 9300073, 9305446, 9310823, 9316202, 9321585, 9326971, 9332360, 9337752, 9343147, 9348546, 9353947, 9359352, 9364760, 9370171, 9375585, 9381002, 9386422, 9391845, 9397272, 9402701, 9408134, 9413570, 9419009, 9424451, 9429897, 9435345, 9440797, 9446252, 9451710, 9457171, 9462635, 9468102, 9473573, 9479047, 9484524, 9490004, 9495487, 9500973, 9506463, 9511955, 9517451, 9522950, 9528453, 9533958, 9539467, 9544979, 9550494, 9556012, 9561533, 9567058, 9572585, 9578116, 9583650, 9589188, 9594728, 9600272, 9605819, 9611369, 9616922, 9622479, 9628039, 9633602, 9639168, 9644737, 9650310, 9655886, 9661465, 9667047, 9672633, 9678221, 9683813, 9689409, 9695007, 9700609, 9706214, 9711822, 9717433, 9723048, 9728666, 9734287, 9739911, 9745539, 9751170, 9756804, 9762441, 9768082, 9773726, 9779373, 9785023, 9790677, 9796334, 9801994, 9807657, 9813324, 9818994, 9824667, 9830344, 9836024, 9841707, 9847394, 9853083, 9858776, 9864473, 9870172, 9875875, 9881581, 9887291, 9893003, 9898719, 9904439, 9910162, 9915887, 9921617, 9927349, 9933085, 9938825, 9944567, 9950313, 9956062, 9961815, 9967570, 9973330, 9979092, 9984858, 9990627, 9996399, 10002175, 10007954, 10013737, 10019523, 10025312, 10031104, 10036900, 10042700, 10048502, 10054308, 10060117, 10065930, 10071746, 10077565, 10083388, 10089214, 10095043, 10100876, 10106712, 10112552, 10118395, 10124241, 10130091, 10135944, 10141800, 10147660, 10153523, 10159390, 10165260, 10171133, 10177010, 10182890, 10188774, 10194661, 10200551, 10206445, 10212342, 10218243, 10224147, 10230054, 10235965, 10241879, 10247797, 10253718, 10259642, 10265570, 10271502, 10277436, 10283374, 10289316, 10295261, 10301210, 10307162, 10313117, 10319076, 10325038, 10331004, 10336973, 10342945, 10348921, 10354901, 10360884, 10366870, 10372860, 10378853, 10384850, 10390850, 10396854, 10402861, 10408872, 10414886, 10420904, 10426925, 10432949, 10438977, 10445009, 10451044, 10457083, 10463124, 10469170, 10475219, 10481271, 10487327, 10493387, 10499450, 10505516, 10511586, 10517660, 10523737, 10529817, 10535901, 10541989, 10548080, 10554174, 10560273, 10566374, 10572480, 10578589, 10584701, 10590817, 10596936, 10603059, 10609186, 10615316, 10621449, 10627586, 10633727, 10639871, 10646019, 10652170, 10658325, 10664483, 10670645, 10676811, 10682980, 10689153, 10695329, 10701509, 10707692, 10713879, 10720069, 10726264, 10732461, 10738662, 10744867, 10751076, 10757288, 10763503, 10769722, 10775945, 10782172, 10788402, 10794635, 10800872, 10807113, 10813357, 10819605, 10825857, 10832112, 10838371, 10844634, 10850900, 10857169, 10863443, 10869720, 10876000, 10882284, 10888572, 10894864, 10901159, 10907457, 10913760, 10920066, 10926375, 10932689, 10939006, 10945326, 10951650, 10957978, 10964310, 10970645, 10976984, 10983326, 10989673, 10996022, 11002376, 11008733, 11015094, 11021459, 11027827, 11034199, 11040574, 11046954, 11053337, 11059723, 11066114, 11072508, 11078905, 11085307, 11091712, 11098121, 11104533, 11110949, 11117369, 11123793, 11130220, 11136651, 11143086, 11149525, 11155967, 11162413, 11168863, 11175316, 11181773, 11188234, 11194699, 11201167, 11207639, 11214115, 11220594, 11227078, 11233565, 11240055, 11246550, 11253048, 11259550, 11266056, 11272566, 11279079, 11285596, 11292117, 11298642, 11305170, 11311702, 11318238, 11324778, 11331321, 11337868, 11344420, 11350974, 11357533, 11364095, 11370662, 11377232, 11383805, 11390383, 11396964, 11403550, 11410139, 11416731, 11423328, 11429928, 11436533, 11443141, 11449753, 11456368, 11462988, 11469611, 11476238, 11482869, 11489504, 11496143, 11502785, 11509432, 11516082, 11522736, 11529394, 11536056, 11542721, 11549390, 11556064, 11562741, 11569422, 11576107, 11582795, 11589488, 11596184, 11602885, 11609589, 11616297, 11623009, 11629725, 11636444, 11643168, 11649895, 11656627, 11663362, 11670101, 11676844, 11683591, 11690342, 11697097, 11703855, 11710618, 11717384, 11724154, 11730929, 11737707, 11744489, 11751275, 11758065, 11764859, 11771656, 11778458, 11785264, 11792073, 11798887, 11805704, 11812526, 11819351, 11826180, 11833013, 11839851, 11846692, 11853537, 11860386, 11867239, 11874096, 11880956, 11887821, 11894690, 11901563, 11908440, 11915320, 11922205, 11929094, 11935986, 11942883, 11949784, 11956688, 11963597, 11970510, 11977426, 11984347, 11991271, 11998200, 12005133, 12012069, 12019010, 12025954, 12032903, 12039856, 12046812, 12053773, 12060738, 12067706, 12074679, 12081656, 12088637, 12095622, 12102610, 12109603, 12116600, 12123601, 12130606, 12137615, 12144629, 12151646, 12158667, 12165692, 12172722, 12179755, 12186793, 12193834, 12200880, 12207930, 12214983, 12222041, 12229103, 12236169, 12243239, 12250313, 12257392, 12264474, 12271561, 12278651, 12285746, 12292844, 12299947, 12307054, 12314165, 12321280, 12328400, 12335523, 12342651, 12349782, 12356918, 12364058, 12371202, 12378350, 12385502, 12392658, 12399819, 12406984, 12414152, 12421325, 12428502, 12435684, 12442869, 12450059, 12457252, 12464450, 12471652, 12478858, 12486069, 12493283, 12500502, 12507725, 12514952, 12522183, 12529418, 12536658, 12543901, 12551149, 12558401, 12565658, 12572918, 12580183, 12587452, 12594725, 12602002, 12609283, 12616569, 12623859, 12631153, 12638451, 12645754, 12653061, 12660372, 12667687, 12675006, 12682330, 12689658, 12696990, 12704326, 12711667, 12719012, 12726361, 12733714, 12741072, 12748433, 12755800, 12763170, 12770544, 12777923, 12785306, 12792694, 12800085, 12807481, 12814882, 12822286, 12829695, 12837108, 12844525, 12851947, 12859373, 12866803, 12874237, 12881676, 12889119, 12896567, 12904018, 12911474, 12918934, 12926399, 12933868, 12941341, 12948819, 12956301, 12963787, 12971277, 12978772, 12986271, 12993775, 13001283, 13008795, 13016311, 13023832, 13031357, 13038887, 13046421, 13053959, 13061502, 13069049, 13076600, 13084156, 13091716, 13099280, 13106849, 13114422, 13122000, 13129582, 13137168, 13144759, 13152354, 13159953, 13167557, 13175165, 13182778, 13190395, 13198016, 13205642, 13213273, 13220907, 13228546, 13236190, 13243838, 13251490, 13259147, 13266808, 13274474, 13282144, 13289818, 13297497, 13305180, 13312868, 13320560, 13328257, 13335958, 13343663, 13351373, 13359088, 13366807, 13374530, 13382258, 13389990, 13397727, 13405468, 13413214, 13420964, 13428719, 13436478, 13444241, 13452010, 13459782, 13467559, 13475341, 13483127, 13490918, 13498713, 13506512, 13514316, 13522125, 13529938, 13537756, 13545578, 13553405, 13561236, 13569071, 13576912, 13584756, 13592606, 13600460, 13608318, 13616181, 13624048, 13631920, 13639797, 13647678, 13655564, 13663454, 13671349, 13679248, 13687152, 13695060, 13702974, 13710891, 13718813, 13726740, 13734671, 13742607, 13750548, 13758493, 13766443, 13774397, 13782356, 13790319, 13798287, 13806260, 13814237, 13822219, 13830206, 13838197, 13846193, 13854193, 13862198, 13870208, 13878222, 13886241, 13894264, 13902292, 13910325, 13918363, 13926405, 13934451, 13942503, 13950559, 13958619, 13966685, 13974755, 13982829, 13990909, 13998993, 14007081, 14015175, 14023273, 14031375, 14039483, 14047595, 14055712, 14063833, 14071959, 14080090, 14088225, 14096366, 14104511, 14112660, 14120815, 14128974, 14137137, 14145306, 14153479, 14161657, 14169840, 14178027, 14186219, 14194416, 14202618, 14210824, 14219035, 14227251, 14235471, 14243697, 14251927, 14260161, 14268401, 14276645, 14284894, 14293148, 14301407, 14309670, 14317938, 14326211, 14334489, 14342772, 14351059, 14359351, 14367648, 14375949, 14384256, 14392567, 14400883, 14409204, 14417530, 14425860, 14434196, 14442536, 14450881, 14459230, 14467585, 14475944, 14484309, 14492678, 14501052, 14509430, 14517814, 14526202, 14534596, 14542994, 14551397, 14559805, 14568217, 14576635, 14585057, 14593485, 14601917, 14610354, 14618796, 14627242, 14635694, 14644151, 14652612, 14661078, 14669550, 14678026, 14686507, 14694993, 14703483, 14711979, 14720480, 14728985, 14737496, 14746011, 14754531, 14763057, 14771587, 14780122, 14788662, 14797207, 14805757, 14814311, 14822871, 14831436, 14840005, 14848580, 14857160, 14865744, 14874334, 14882928, 14891527, 14900132, 14908741, 14917355, 14925975, 14934599, 14943228, 14951862, 14960502, 14969146, 14977795, 14986449, 14995109, 15003773, 15012442, 15021116, 15029795, 15038480, 15047169, 15055863, 15064563, 15073267, 15081976, 15090691, 15099410, 15108135, 15116864, 15125599, 15134338, 15143083, 15151833, 15160587, 15169347, 15178112, 15186882, 15195657, 15204437, 15213222, 15222013, 15230808, 15239608, 15248414, 15257224, 15266040, 15274861, 15283687, 15292518, 15301354, 15310195, 15319041, 15327893, 15336749, 15345611, 15354477, 15363349, 15372226, 15381108, 15389996, 15398888, 15407786, 15416688, 15425596, 15434509, 15443427, 15452350, 15461279, 15470212, 15479151, 15488095, 15497044, 15505998, 15514958, 15523922, 15532892, 15541867, 15550847, 15559832, 15568823, 15577819, 15586820, 15595826, 15604837, 15613853, 15622875, 15631902, 15640934, 15649972, 15659014, 15668062, 15677115, 15686173, 15695237, 15704306, 15713380, 15722459, 15731543, 15740633, 15749728, 15758828, 15767934, 15777045, 15786161, 15795282, 15804408, 15813540, 15822677, 15831820, 15840967, 15850120, 15859279, 15868442, 15877611, 15886785, 15895965, 15905149, 15914339, 15923535, 15932735, 15941941, 15951153, 15960369, 15969591, 15978818, 15988051, 15997289, 16006532, 16015781, 16025035, 16034294, 16043559, 16052829, 16062104, 16071385, 16080671, 16089962, 16099259, 16108561, 16117869, 16127182, 16136500, 16145824, 16155153, 16164488, 16173828, 16183173, 16192523, 16201880, 16211241, 16220608, 16229980, 16239358, 16248741, 16258130, 16267524, 16276923, 16286328, 16295738, 16305154, 16314575, 16324002, 16333434, 16342871, 16352314, 16361763, 16371217, 16380676, 16390141, 16399611, 16409087, 16418568, 16428055, 16437547, 16447044, 16456548, 16466056, 16475570, 16485090, 16494615, 16504146, 16513682, 16523224, 16532771, 16542323, 16551882, 16561445, 16571015, 16580589, 16590170, 16599755, 16609347, 16618944, 16628546, 16638154, 16647768, 16657387, 16667012, 16676642, 16686278, 16695919, 16705566, 16715219, 16724877, 16734540
474 };
475 #endif
476 
477 unsigned long int freq_table[] = {
478 	837201792, 837685632, 838169728, 838653568, 839138240, 839623232, 840108480, 840593984, 841079680, 841565184, 842051648, 842538240, 843025152, 843512320, 843999232, 844486976, 844975040, 845463360, 845951936, 846440320, 846929536, 847418944, 847908608, 848398656, 848888960, 849378944, 849869824, 850361024, 850852416, 851344192, 851835584, 852327872, 852820480, 853313280, 853806464, 854299328, 854793024, 855287040, 855781312, 856275904, 856770752, 857265344, 857760704, 858256448, 858752448, 859248704, 859744768, 860241600, 860738752, 861236160, 861733888, 862231360, 862729600, 863228160, 863727104, 864226176, 864725696, 865224896, 865724864, 866225152, 866725760, 867226688, 867727296, 868228736, 868730496, 869232576, 869734912, 870236928, 870739904, 871243072, 871746560, 872250368, 872754496, 873258240, 873762880, 874267840, 874773184, 875278720, 875783936, 876290112, 876796480, 877303232, 877810176, 878317504, 878824512, 879332416, 879840576, 880349056, 880857792, 881366272, 881875712, 882385280, 882895296, 883405440, 883915456, 884426304, 884937408, 885448832, 885960512, 886472512,
479 	886984192, 887496768, 888009728, 888522944, 889036352, 889549632, 890063680, 890578048, 891092736, 891607680, 892122368, 892637952, 893153792, 893670016, 894186496, 894703232, 895219648, 895737024, 896254720, 896772672, 897290880, 897808896, 898327744, 898846912, 899366336, 899886144, 900405568, 900925952, 901446592, 901967552, 902488768, 903010368, 903531584, 904053760, 904576256, 905099008, 905622016, 906144896, 906668480, 907192512, 907716800, 908241408, 908765632, 909290816, 909816256, 910342144, 910868160, 911394624, 911920768, 912447680, 912975104, 913502720, 914030592, 914558208, 915086784, 915615552, 916144768, 916674176, 917203968, 917733440, 918263744, 918794496, 919325440, 919856704, 920387712, 920919616, 921451840, 921984320, 922517184, 923049728, 923583168, 924116928, 924651008, 925185344, 925720000, 926254336, 926789696, 927325312, 927861120, 928397440, 928933376, 929470208, 930007296, 930544768, 931082560, 931619968, 932158464, 932697152, 933236160, 933775488, 934315072, 934854464, 935394688, 935935296, 936476224, 937017344, 937558208, 938100160, 938642304, 939184640,
480 	939727488, 940269888, 940813312, 941357056, 941900992, 942445440, 942990016, 943534400, 944079680, 944625280, 945171200, 945717440, 946263360, 946810176, 947357376, 947904832, 948452672, 949000192, 949548608, 950097280, 950646400, 951195776, 951745472, 952294912, 952845184, 953395904, 953946880, 954498176, 955049216, 955601088, 956153408, 956705920, 957258816, 957812032, 958364928, 958918848, 959472960, 960027456, 960582272, 961136768, 961692224, 962248000, 962804032, 963360448, 963916608, 964473600, 965031040, 965588736, 966146816, 966705152, 967263168, 967822144, 968381440, 968941120, 969501056, 970060736, 970621376, 971182272, 971743488, 972305088, 972866368, 973428608, 973991104, 974554048, 975117312, 975680768, 976243968, 976808192, 977372736, 977937536, 978502656, 979067584, 979633344, 980199488, 980765888, 981332736, 981899200, 982466688, 983034432, 983602624, 984171008, 984739776, 985308160, 985877632, 986447360, 987017472, 987587904, 988157952, 988729088, 989300416, 989872192, 990444224, 991016000, 991588672, 992161728, 992735168, 993308864, 993882880, 994456576, 995031296,
481 	995606336, 996181696, 996757440, 997332800, 997909184, 998485888, 999062912, 999640256, 1000217984, 1000795392, 1001373696, 1001952448, 1002531520, 1003110848, 1003689920, 1004270016, 1004850304, 1005431040, 1006012160, 1006592832, 1007174592, 1007756608, 1008339008, 1008921792, 1009504768, 1010087552, 1010671296, 1011255360, 1011839808, 1012424576, 1013009024, 1013594368, 1014180160, 1014766272, 1015352768, 1015938880, 1016526016, 1017113472, 1017701248, 1018289408, 1018877824, 1019465984, 1020055104, 1020644672, 1021234496, 1021824768, 1022414528, 1023005440, 1023596608, 1024188160, 1024780096, 1025371584, 1025964160, 1026557120, 1027150336, 1027744000, 1028337920, 1028931520, 1029526144, 1030121152, 1030716480, 1031312128, 1031907456, 1032503808, 1033100480, 1033697536, 1034294912, 1034892032, 1035490048, 1036088512, 1036687232, 1037286336, 1037885824, 1038484928, 1039085056, 1039685632, 1040286464, 1040887680, 1041488448, 1042090368, 1042692608, 1043295168, 1043898176, 1044501440, 1045104384, 1045708288, 1046312640, 1046917376, 1047522368, 1048127040, 1048732800, 1049338816, 1049945280, 1050552128, 1051158528, 1051765952, 1052373824, 1052982016, 1053590592, 1054199424,
482 	1054807936, 1055417600, 1056027456, 1056637760, 1057248448, 1057858752, 1058470016, 1059081728, 1059693824, 1060306304, 1060918336, 1061531392, 1062144896, 1062758656, 1063372928, 1063987392, 1064601664, 1065216896, 1065832448, 1066448448, 1067064704, 1067680704, 1068297728, 1068915136, 1069532864, 1070150976, 1070768640, 1071387520, 1072006720, 1072626240, 1073246080, 1073866368, 1074486272, 1075107200, 1075728512, 1076350208, 1076972160, 1077593856, 1078216704, 1078839680, 1079463296, 1080087040, 1080710528, 1081335168, 1081960064, 1082585344, 1083211008, 1083836928, 1084462592, 1085089280, 1085716352, 1086343936, 1086971648, 1087599104, 1088227712, 1088856576, 1089485824, 1090115456, 1090745472, 1091375104, 1092005760, 1092636928, 1093268352, 1093900160, 1094531584, 1095164160, 1095796992, 1096430336, 1097064064, 1097697280, 1098331648, 1098966400, 1099601536, 1100237056, 1100872832, 1101508224, 1102144768, 1102781824, 1103419136, 1104056832, 1104694144, 1105332608, 1105971328, 1106610432, 1107249920, 1107889152, 1108529408, 1109170048, 1109811072, 1110452352, 1111094144, 1111735552, 1112377984, 1113020928, 1113664128, 1114307712, 1114950912, 1115595264, 1116240000, 1116885120,
483 	1117530624, 1118175744, 1118821888, 1119468416, 1120115456, 1120762752, 1121410432, 1122057856, 1122706176, 1123355136, 1124004224, 1124653824, 1125303040, 1125953408, 1126604160, 1127255168, 1127906560, 1128557696, 1129209984, 1129862528, 1130515456, 1131168768, 1131822592, 1132475904, 1133130368, 1133785216, 1134440448, 1135096064, 1135751296, 1136407680, 1137064448, 1137721472, 1138379008, 1139036800, 1139694336, 1140353024, 1141012096, 1141671424, 1142331264, 1142990592, 1143651200, 1144312192, 1144973440, 1145635200, 1146296448, 1146958976, 1147621760, 1148285056, 1148948608, 1149612672, 1150276224, 1150940928, 1151606144, 1152271616, 1152937600, 1153603072, 1154269824, 1154936832, 1155604352, 1156272128, 1156939648, 1157608192, 1158277248, 1158946560, 1159616384, 1160286464, 1160956288, 1161627264, 1162298624, 1162970240, 1163642368, 1164314112, 1164987008, 1165660160, 1166333824, 1167007872, 1167681536, 1168356352, 1169031552, 1169707136, 1170383104, 1171059584, 1171735552, 1172412672, 1173090304, 1173768192, 1174446592, 1175124480, 1175803648, 1176483072, 1177163008, 1177843328, 1178523264, 1179204352, 1179885824, 1180567680, 1181249920, 1181932544, 1182614912, 1183298304,
484 	1183982208, 1184666368, 1185351040, 1186035328, 1186720640, 1187406464, 1188092672, 1188779264, 1189466368, 1190152960, 1190840832, 1191528960, 1192217600, 1192906624, 1193595136, 1194285056, 1194975232, 1195665792, 1196356736, 1197047296, 1197739136, 1198431360, 1199123968, 1199816960, 1200510336, 1201203328, 1201897600, 1202592128, 1203287040, 1203982464, 1204677504, 1205373696, 1206070272, 1206767232, 1207464704, 1208161664, 1208859904, 1209558528, 1210257536, 1210956928, 1211656832, 1212356224, 1213056768, 1213757952, 1214459392, 1215161216, 1215862656, 1216565376, 1217268352, 1217971840, 1218675712, 1219379200, 1220083840, 1220788992, 1221494528, 1222200448, 1222906752, 1223612672, 1224319872, 1225027456, 1225735424, 1226443648, 1227151616, 1227860864, 1228570496, 1229280512, 1229990912, 1230700928, 1231412096, 1232123776, 1232835840, 1233548288, 1234261248, 1234973696, 1235687424, 1236401536, 1237116032, 1237831040, 1238545536, 1239261312, 1239977472, 1240694144, 1241411072, 1242128512, 1242845568, 1243563776, 1244282496, 1245001600, 1245721088, 1246440192, 1247160448, 1247881216, 1248602368, 1249324032, 1250045184, 1250767616, 1251490432, 1252213632, 1252937344, 1253661440,
485 	1254385152, 1255110016, 1255835392, 1256561152, 1257287424, 1258013184, 1258740096, 1259467648, 1260195456, 1260923648, 1261651584, 1262380800, 1263110272, 1263840256, 1264570624, 1265301504, 1266031872, 1266763520, 1267495552, 1268227968, 1268961024, 1269693440, 1270427264, 1271161472, 1271896064, 1272631168, 1273365760, 1274101632, 1274838016, 1275574784, 1276311808, 1277049472, 1277786624, 1278525056, 1279264000, 1280003328, 1280743040, 1281482368, 1282222976, 1282963968, 1283705344, 1284447232, 1285188736, 1285931392, 1286674560, 1287418240, 1288162176, 1288906624, 1289650688, 1290395904, 1291141760, 1291887872, 1292634496, 1293380608, 1294128128, 1294875904, 1295624320, 1296373120, 1297122304, 1297870976, 1298621056, 1299371520, 1300122496, 1300873856, 1301624832, 1302376960, 1303129600, 1303882752, 1304636288, 1305389312, 1306143872, 1306898688, 1307654016, 1308409600, 1309165696, 1309921536, 1310678528, 1311435904, 1312193920, 1312952192, 1313710080, 1314469248, 1315228928, 1315988992, 1316749568, 1317509632, 1318271104, 1319032960, 1319795200, 1320557952, 1321321088, 1322083840, 1322847872, 1323612416, 1324377216, 1325142656, 1325907584, 1326673920, 1327440512, 1328207744,
486 	1328975360, 1329742464, 1330510976, 1331279872, 1332049152, 1332819072, 1333589248, 1334359168, 1335130240, 1335901824, 1336673920, 1337446400, 1338218368, 1338991744, 1339765632, 1340539904, 1341314560, 1342088832, 1342864512, 1343640576, 1344417024, 1345193984, 1345971456, 1346748416, 1347526656, 1348305408, 1349084672, 1349864320, 1350643456, 1351424000, 1352205056, 1352986496, 1353768448, 1354550784, 1355332608, 1356115968, 1356899712, 1357683840, 1358468480, 1359252608, 1360038144, 1360824192, 1361610624, 1362397440, 1363183872, 1363971712, 1364760064, 1365548672, 1366337792, 1367127424, 1367916672, 1368707200, 1369498240, 1370289664, 1371081472, 1371873024, 1372665856, 1373459072, 1374252800, 1375047040, 1375840768, 1376635904, 1377431552, 1378227584, 1379024000, 1379820928, 1380617472, 1381415296, 1382213760, 1383012480, 1383811840, 1384610560, 1385410816, 1386211456, 1387012480, 1387814144, 1388615168, 1389417728, 1390220672, 1391024128, 1391827968, 1392632320, 1393436288, 1394241536, 1395047296, 1395853568, 1396660224, 1397466368, 1398274048, 1399082112, 1399890688, 1400699648, 1401508224, 1402318080, 1403128576, 1403939456, 1404750848, 1405562624, 1406374016, 1407186816,
487 	1408000000, 1408813696, 1409627904, 1410441728, 1411256704, 1412072320, 1412888320, 1413704960, 1414521856, 1415338368, 1416156288, 1416974720, 1417793664, 1418612992, 1419431808, 1420252160, 1421072896, 1421894144, 1422715904, 1423537280, 1424359808, 1425183104, 1426006784, 1426830848, 1427655296, 1428479488, 1429305088, 1430131072, 1430957568, 1431784576, 1432611072, 1433438976, 1434267392, 1435096192, 1435925632, 1436754432, 1437584768, 1438415616, 1439246848, 1440078720, 1440910848, 1441742720, 1442575872, 1443409664, 1444243584, 1445078400, 1445912576, 1446748032, 1447584256, 1448420864, 1449257856, 1450094464, 1450932480, 1451771008, 1452609920, 1453449472, 1454289408, 1455128960, 1455969920, 1456811264, 1457653248, 1458495616, 1459337600, 1460180864, 1461024768, 1461869056, 1462713984, 1463558272, 1464404096, 1465250304, 1466097152, 1466944384, 1467792128, 1468639488, 1469488256, 1470337408, 1471187200, 1472037376, 1472887168, 1473738368, 1474589952, 1475442304, 1476294912, 1477148160, 1478000768, 1478854912, 1479709696, 1480564608, 1481420288, 1482275456, 1483132160, 1483989248, 1484846976, 1485704960, 1486562688, 1487421696, 1488281344, 1489141504, 1490002048, 1490863104,
488 	1491723776, 1492585856, 1493448448, 1494311424, 1495175040, 1496038144, 1496902656, 1497767808, 1498633344, 1499499392, 1500365056, 1501232128, 1502099712, 1502967808, 1503836416, 1504705536, 1505574016, 1506444032, 1507314688, 1508185856, 1509057408, 1509928576, 1510801280, 1511674240, 1512547840, 1513421952, 1514295680, 1515170816, 1516046464, 1516922624, 1517799296, 1518676224, 1519552896, 1520431104, 1521309824, 1522188928, 1523068800, 1523948032, 1524828672, 1525709824, 1526591616, 1527473792, 1528355456, 1529238784, 1530122496, 1531006720, 1531891712, 1532776832, 1533661824, 1534547968, 1535434880, 1536322304, 1537210112, 1538097408, 1538986368, 1539875840, 1540765696, 1541656192, 1542547072, 1543437440, 1544329472, 1545221888, 1546114944, 1547008384, 1547901440, 1548796032, 1549691136, 1550586624, 1551482752, 1552378368, 1553275520, 1554173184, 1555071232, 1555970048, 1556869248, 1557767936, 1558668288, 1559568896, 1560470272, 1561372032, 1562273408, 1563176320, 1564079616, 1564983424, 1565888000, 1566791808, 1567697408, 1568603392, 1569509760, 1570416896, 1571324416, 1572231424, 1573140096, 1574049152, 1574958976, 1575869184, 1576778752, 1577689984, 1578601728, 1579514112,
489 	1580426880, 1581339264, 1582253056, 1583167488, 1584082432, 1584997888, 1585913984, 1586829440, 1587746304, 1588663936, 1589582080, 1590500736, 1591418880, 1592338560, 1593258752, 1594179584, 1595100928, 1596021632, 1596944000, 1597866880, 1598790272, 1599714304, 1600638848, 1601562752, 1602488320, 1603414272, 1604340992, 1605268224, 1606194816, 1607123072, 1608051968, 1608981120, 1609911040, 1610841344, 1611771264, 1612702848, 1613634688, 1614567168, 1615500288, 1616432896, 1617367040, 1618301824, 1619237120, 1620172800, 1621108096, 1622044928, 1622982272, 1623920128, 1624858752, 1625797632, 1626736256, 1627676416, 1628616960, 1629558272, 1630499968, 1631441152, 1632384000, 1633327232, 1634271232, 1635215744, 1636159744, 1637105152, 1638051328, 1638998016, 1639945088, 1640892928, 1641840128, 1642788992, 1643738368, 1644688384, 1645638784, 1646588672, 1647540352, 1648492416, 1649445120, 1650398464, 1651351168, 1652305408, 1653260288, 1654215808, 1655171712, 1656128256, 1657084288, 1658041856, 1659000064, 1659958784, 1660918272, 1661876992, 1662837376, 1663798400, 1664759936, 1665721984, 1666683520, 1667646720, 1668610560, 1669574784, 1670539776, 1671505024, 1672470016, 1673436544,
490 };
491 
492 #define SAMPLE_16BIT 0x01
493 #define SAMPLE_UNSIGNED 0x02
494 #define SAMPLE_LOOP 0x04
495 #define SAMPLE_PINGPONG 0x08
496 #define SAMPLE_REVERSE 0x10
497 #define SAMPLE_SUSTAIN 0x20
498 #define SAMPLE_ENVELOPE 0x40
499 
500 #ifdef DEBUG_SAMPLES
501 #define SAMPLE_CONVERT_DEBUG(dx) printf("\r%s\n",dx)
502 #else
503 #define SAMPLE_CONVERT_DEBUG(dx)
504 #endif
505 
506 #ifdef DEBUG_MIDI
507 #define MIDI_EVENT_DEBUG(dx,dy) printf("\r%s, %x\n",dx,dy)
508 #else
509 #define MIDI_EVENT_DEBUG(dx,dy)
510 #endif
511 
512 #define WM_ERR_MEM		0
513 #define WM_ERR_STAT		1
514 #define WM_ERR_LOAD		2
515 #define WM_ERR_OPEN		3
516 #define WM_ERR_READ		4
517 #define WM_ERR_INVALID		5
518 #define WM_ERR_CORUPT		6
519 #define WM_ERR_NOT_INIT		7
520 #define WM_ERR_INVALID_ARG	8
521 
522 #define MAX_AUTO_AMP 2.0
523 
524 #define FPBITS 10
525 #define FPMASK ((1L<<FPBITS)-1L)
526 
527 
528 void do_note_off (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
529 void do_note_on (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
530 void do_aftertouch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
531 void do_control (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
532 void do_patch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
533 void do_channel_pressure (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
534 void do_pitch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
535 void do_message (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
536 
537 void (*do_event[])(unsigned char ch, struct _mdi *midifile, unsigned long int ptr) = {
538 	*do_note_off,
539 	*do_note_on,
540 	*do_aftertouch,
541 	*do_control,
542 	*do_patch,
543 	*do_channel_pressure,
544 	*do_pitch,
545 	*do_message
546 };
547 
548 /*
549  * =========================
550  * Internal Functions
551  * =========================
552  */
553 
554 
555 void
WM_ERROR(const char * func,unsigned long int lne,int wmerno,const char * wmfor,int error)556 WM_ERROR( const char * func, unsigned long int lne, int wmerno, const char * wmfor, int error) {
557 	const char * errors[] = {
558 		"Unable to obtain memory\0",
559 		"Unable to stat\0",
560 		"Unable to load\0",
561 		"Unable to open\0",
562 		"Unable to read\0",
563 		"Invalid or Unsuported file format\0",
564 		"File corrupt\0",
565 		"Library not Initialized\0",
566 		"Invalid argument\0"
567 	};
568 	if (wmfor != NULL) {
569 		if (error != 0) {
570 			fprintf(stderr,"\rlibWildMidi(%s:%lu): ERROR %s %s (%s)\n",func, lne, errors[wmerno], wmfor, strerror(error));
571 		} else {
572 			fprintf(stderr,"\rlibWildMidi(%s:%lu): ERROR %s %s\n",func, lne, errors[wmerno], wmfor);
573 		}
574 	} else {
575 		if (error != 0) {
576 			fprintf(stderr,"\rlibWildMidi(%s:%lu): ERROR %s (%s)\n",func, lne, errors[wmerno], strerror(error));
577 		} else {
578 			fprintf(stderr,"\rlibWildMidi(%s:%lu): ERROR %s\n",func, lne, errors[wmerno]);
579 		}
580 	}
581 
582 }
583 
584 unsigned char *
WM_BufferFile(const char * filename,unsigned long int * size)585 WM_BufferFile (const char *filename, unsigned long int *size) {
586 	int buffer_fd;
587 	char *data;
588 	struct stat buffer_stat;
589 #ifndef _WIN32
590 	char *home = NULL;
591 	struct passwd *pwd_ent;
592 	char buffer_dir[1024];
593 #endif
594 
595 	char *buffer_file = malloc(strlen(filename) + 1);
596 
597 	if (buffer_file == NULL) {
598 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
599 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, filename, errno);
600 		return NULL;
601 	}
602 
603 	strcpy (buffer_file, filename);
604 #ifndef _WIN32
605 	if (strncmp(buffer_file,"~/",2) == 0) {
606 		if ((pwd_ent = getpwuid (getuid ()))) {
607 			home = pwd_ent->pw_dir;
608 		} else {
609 			home = getenv ("HOME");
610 		}
611 		if (home) {
612 			buffer_file = realloc(buffer_file,(strlen(buffer_file) + strlen(home) + 1));
613 			if (buffer_file == NULL) {
614 				WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
615 				WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, filename, errno);
616 				free(buffer_file);
617 				return NULL;
618 			}
619 			memmove((buffer_file + strlen(home)), (buffer_file + 1), (strlen(buffer_file)));
620 			strncpy (buffer_file, home,strlen(home));
621 		}
622 	} else if (buffer_file[0] != '/') {
623 		char *res = getcwd(buffer_dir,1024);
624 		if (buffer_dir[strlen(buffer_dir)-1] != '/') {
625 			buffer_dir[strlen(buffer_dir)+1] = '\0';
626 			buffer_dir[strlen(buffer_dir)] = '/';
627 		}
628 		buffer_file = realloc(buffer_file,(strlen(buffer_file) + strlen(buffer_dir) + 1));
629 		if (buffer_file == NULL) {
630 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
631 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, filename, errno);
632 			free(buffer_file);
633 			return NULL;
634 		}
635 		memmove((buffer_file + strlen(buffer_dir)), buffer_file, strlen(buffer_file)+1);
636 		strncpy (buffer_file,buffer_dir,strlen(buffer_dir));
637 	}
638 #endif
639 	if (stat(buffer_file,&buffer_stat)) {
640 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_STAT, filename, errno);
641 		free(buffer_file);
642 		return NULL;
643 	}
644 	*size = buffer_stat.st_size;
645 	data = malloc(*size);
646 	if (data == NULL) {
647 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
648 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, filename, errno);
649 		free(buffer_file);
650 		return NULL;
651 	}
652 #ifdef _WIN32
653 	if ((buffer_fd = open(buffer_file,(O_RDONLY | O_BINARY))) == -1) {
654 #else
655 	if ((buffer_fd = open(buffer_file,O_RDONLY)) == -1) {
656 #endif
657 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_OPEN, filename, errno);
658 		free(buffer_file);
659 		free(data);
660 		return NULL;
661 	}
662 	if (read(buffer_fd,data,*size) != buffer_stat.st_size) {
663 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_READ, filename, errno);
664 		free(buffer_file);
665 		free(data);
666 		close(buffer_fd);
667 		return NULL;
668 	}
669 	close(buffer_fd);
670 	free(buffer_file);
671 	return data;
672 }
673 
674 static inline void
675 WM_Lock (int * wmlock) {
676 	LOCK_START:
677 	if (__builtin_expect(((*wmlock) == 0),1)) {
678 		(*wmlock)++;
679 		if (__builtin_expect(((*wmlock) == 1), 1)) {
680 			return;
681 		}
682 		(*wmlock)--;
683 	}
684 #ifdef _WIN32
685 	Sleep(10);
686 #else
687 	usleep(500);
688 #endif
689 	goto LOCK_START;
690 }
691 
692 static inline void
693 WM_Unlock (int *wmlock) {
694 	(*wmlock)--;
695 }
696 
697 void
698 WM_InitPatches ( void ) {
699 	int i;
700 	for (i = 0; i < 128; i++) {
701 		patch[i] = NULL;
702 	}
703 }
704 
705 void
706 WM_FreePatches ( void ) {
707 	int i;
708 	struct _patch * tmp_patch;
709 	struct _sample * tmp_sample;
710 
711 	WM_Lock(&patch_lock);
712 	for (i = 0; i < 128; i++) {
713 		if (patch[i] != NULL) {
714 			while (patch[i] != NULL) {
715 				if (patch[i]->filename != NULL) {
716 					if (patch[i]->first_sample != NULL) {
717 						while (patch[i]->first_sample != NULL) {
718 							tmp_sample = patch[i]->first_sample->next;
719 							if (patch[i]->first_sample->data != NULL)
720 								free (patch[i]->first_sample->data);
721 							free (patch[i]->first_sample);
722 							patch[i]->first_sample = tmp_sample;
723 						}
724 					}
725 					free (patch[i]->filename);
726 				}
727 				tmp_patch = patch[i]->next;
728 				free(patch[i]);
729 				patch[i] = tmp_patch;
730 			}
731 		}
732 	}
733 	WM_Unlock(&patch_lock);
734 }
735 
736 int
737 WM_LoadConfig (const char *config_file, const char *top_config_dir) {
738 	unsigned long int config_size = 0;
739 	unsigned char *config_buffer =  NULL;
740 	char * dir_end =  NULL;
741 	char * config_dir =  NULL;
742 	unsigned long int config_ptr = 0;
743 	unsigned long int line_start_ptr = 0;
744 	char * line_buffer = NULL;
745 	unsigned long int line_ptr = 0;
746 	char * chr_ptr = NULL;
747 	unsigned short int patchid = 0;
748 	char * new_config = NULL;
749 	struct _patch * tmp_patch;
750 
751 	if ((config_buffer = WM_BufferFile(config_file, &config_size)) == NULL) {
752 		return -1;
753 	}
754 
755 	if (top_config_dir) {
756         config_dir = strdup (top_config_dir);
757     }
758 
759 	if (config_buffer == NULL) {
760 		WM_FreePatches();
761 		if (config_dir) {
762             free (config_dir);
763         }
764 		return -1;
765 	}
766 
767 	if (!config_dir) {
768         dir_end = strrchr(config_file,'/');
769         if (dir_end != NULL) {
770             config_dir = malloc((dir_end - config_file + 2) + 4);
771             if (config_dir == NULL) {
772                 WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config", errno);
773                 WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
774                 WM_FreePatches();
775                 free (config_buffer);
776                 return -1;
777             }
778             strncpy(config_dir, config_file, (dir_end - config_file + 1));
779             config_dir[dir_end - config_file + 1] = '\0';
780         }
781     }
782 	config_ptr = 0;
783 	line_start_ptr = 0;
784 	while (config_ptr < config_size) {
785 		// find end of line
786 		if (config_buffer[config_ptr] != '\n') {
787 			// remove unwanted crud
788 			if (config_buffer[config_ptr] == '\t') {
789 				config_buffer[config_ptr] = ' ';
790 			} else if (config_buffer[config_ptr] == '\r') {
791 				config_buffer[config_ptr] = ' ';
792 			}
793 			if ((config_buffer[config_ptr] == ' ') && (config_ptr == line_start_ptr)) {
794 				line_start_ptr++;
795 			}
796 
797 			config_ptr++;
798 			continue;
799 		}
800 		config_buffer[config_ptr] = '\0';
801 		if (config_ptr == line_start_ptr) {
802 			config_ptr++;
803 			line_start_ptr++;
804 			continue;
805 		}
806 		if (config_buffer[line_start_ptr] == '#') {
807 			config_ptr++;
808 			line_start_ptr = config_ptr;
809 			continue;
810 		}
811 
812 		// copy line into a workable buffer
813 		line_buffer = realloc(line_buffer, (config_ptr - line_start_ptr + 1) + 4);
814 		if (line_buffer == NULL) {
815 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config", errno);
816 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
817 			WM_FreePatches();
818 			if (config_dir != NULL)
819 				free(config_dir);
820 			free (config_buffer);
821 			return -1;
822 		}
823 		strcpy(line_buffer, &config_buffer[line_start_ptr]);
824 		config_ptr++;
825 		line_start_ptr = config_ptr;
826 		// remove unwnted crud from line for easier parsing
827 		if ((chr_ptr = strstr(line_buffer,"  ")) != NULL) {
828 			while ((chr_ptr = strstr(line_buffer,"  ")) != NULL) {
829 				memmove(chr_ptr, &chr_ptr[1], strlen(chr_ptr));
830 			}
831 		}
832 		if ((chr_ptr = strchr(line_buffer, '#')) != NULL) {
833 			*chr_ptr = '\0';
834 		}
835 		if (line_buffer[strlen(line_buffer) -1] == ' ') {
836 			while (line_buffer[strlen(line_buffer) -1] == ' ') {
837 				line_buffer[strlen(line_buffer) -1] = '\0';
838 			}
839 		}
840 
841 		// now parse line
842 		if (strncasecmp(line_buffer, "dir ", 4) == 0) {
843 			if (line_buffer[strlen(line_buffer) - 1] == '/') {
844 				config_dir = realloc(config_dir, strlen(&line_buffer[4]) + 1 + 4);
845 				if (config_dir == NULL) {
846 					WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config", errno);
847 					WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
848 					WM_FreePatches();
849 					free (line_buffer);
850 					free (config_buffer);
851 					return -1;
852 				}
853 				strcpy(config_dir, &line_buffer[4]);
854 			} else {
855 				config_dir = realloc(config_dir, strlen(&line_buffer[4]) + 2 + 4);
856 				if (config_dir == NULL) {
857 					WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config", errno);
858 					WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
859 					WM_FreePatches();
860 					free (line_buffer);
861 					free (config_buffer);
862 					return -1;
863 				}
864 				strcpy(config_dir, &line_buffer[4]);
865 				strcat(config_dir,"/");
866 			}
867 			continue;
868 		} else if (strncasecmp(line_buffer, "source ", 7) == 0) {
869 			if (config_dir != NULL && line_buffer[7] != '/') {
870 				new_config = malloc(strlen(config_dir) + strlen(&line_buffer[7]) + 1 + 4);
871 				if (new_config == NULL) {
872 					WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config", errno);
873 					WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
874 					WM_FreePatches();
875 					free (config_dir);
876 					free (line_buffer);
877 					free (config_buffer);
878 					return -1;
879 				}
880 				strcpy(new_config,config_dir);
881 				strcpy(&new_config[strlen(config_dir)], &line_buffer[7]);
882 			} else {
883 				new_config = malloc(strlen(&line_buffer[7]) + 1 + 4);
884 				if (new_config == NULL) {
885 					WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config", errno);
886 					WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
887 					WM_FreePatches();
888 					free (line_buffer);
889 					free (config_buffer);
890 					return -1;
891 				}
892 				strcpy(new_config, &line_buffer[7]);
893 			}
894 			if (WM_LoadConfig(new_config, config_dir) == -1) {
895 				free (new_config);
896 				free (line_buffer);
897 				free (config_buffer);
898 				//if (config_dir != NULL)
899 				//	free (config_dir);
900 				return -1;
901 			}
902 			free (new_config);
903 			continue;
904 		} else if (strncasecmp(line_buffer, "bank ", 5) == 0) {
905 			if (!isdigit(line_buffer[5])) {
906 				WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in bank line)", 0);
907 				WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
908 				WM_FreePatches();
909 				if (config_dir != NULL)
910 					free (config_dir);
911 				free (line_buffer);
912 				free (config_buffer);
913 				return -1;
914 			}
915 			patchid = (atoi(&line_buffer[5]) & 0xFF ) << 8;
916 			continue;
917 		} else if (strncasecmp(line_buffer, "drumset ", 8) == 0) {
918 			if (!isdigit(line_buffer[8])) {
919 				WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in drumset line)", 0);
920 				WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
921 				WM_FreePatches();
922 				if (config_dir != NULL)
923 					free (config_dir);
924 				free (line_buffer);
925 				free (config_buffer);
926 				return -1;
927 			}
928 			patchid = ((atoi(&line_buffer[8]) & 0xFF ) << 8) | 0x80;
929 			continue;
930 		} else if (isdigit(line_buffer[0])) {
931 			patchid = (patchid & 0xFF80) | (atoi(line_buffer) & 0x7F);
932 			if (patch[(patchid & 0x7F)] == NULL) {
933 				patch[(patchid & 0x7F)] = malloc (sizeof(struct _patch));
934 				if (patch[(patchid & 0x7F)] == NULL) {
935 					WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
936 					WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
937 					WM_FreePatches();
938 					if (config_dir != NULL)
939 						free (config_dir);
940 					free (line_buffer);
941 					free (config_buffer);
942 					return -1;
943 				}
944 				tmp_patch = patch[(patchid & 0x7F)];
945 				tmp_patch->patchid = patchid;
946 				tmp_patch->filename = NULL;
947 				tmp_patch->amp = 1024;
948 				tmp_patch->note = 0;
949 				tmp_patch->next = NULL;
950 				tmp_patch->first_sample = NULL;
951 				tmp_patch->loaded = 0;
952 				tmp_patch->inuse_count = 0;
953 			} else {
954 				tmp_patch = patch[(patchid & 0x7F)];
955 				if (tmp_patch->patchid == patchid) {
956 					free (tmp_patch->filename);
957 					tmp_patch->filename = NULL;
958 					tmp_patch->amp = 1024;
959 					tmp_patch->note = 0;
960 				} else {
961 					if (tmp_patch->next != NULL) {
962 						while (tmp_patch->next != NULL) {
963 							if (tmp_patch->next->patchid == patchid)
964 								break;
965 							tmp_patch = tmp_patch->next;
966 						}
967 						if (tmp_patch->next == NULL) {
968 							tmp_patch->next = malloc (sizeof(struct _patch));
969 							if (tmp_patch->next == NULL) {
970 								WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
971 								WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
972 								WM_FreePatches();
973 								if (config_dir != NULL)
974 									free (config_dir);
975 								free (line_buffer);
976 								free (config_buffer);
977 								return -1;
978 							}
979 							tmp_patch = tmp_patch->next;
980 							tmp_patch->patchid = patchid;
981 							tmp_patch->filename = NULL;
982 							tmp_patch->amp = 1024;
983 							tmp_patch->note = 0;
984 							tmp_patch->next = NULL;
985 							tmp_patch->first_sample = NULL;
986 							tmp_patch->loaded = 0;
987 							tmp_patch->inuse_count = 0;
988 						} else {
989 							tmp_patch = tmp_patch->next;
990 							free (tmp_patch->filename);
991 							tmp_patch->filename = NULL;
992 							tmp_patch->amp = 1024;
993 							tmp_patch->note = 0;
994 						}
995 					} else {
996 						tmp_patch->next = malloc (sizeof(struct _patch));
997 						if (tmp_patch->next == NULL) {
998 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
999 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
1000 							WM_FreePatches();
1001 							if (config_dir != NULL)
1002 								free (config_dir);
1003 							free (line_buffer);
1004 							free (config_buffer);
1005 							return -1;
1006 						}
1007 						tmp_patch = tmp_patch->next;
1008 						tmp_patch->patchid = patchid;
1009 						tmp_patch->filename = NULL;
1010 						tmp_patch->amp = 1024;
1011 						tmp_patch->note = 0;
1012 						tmp_patch->next = NULL;
1013 						tmp_patch->first_sample = NULL;
1014 						tmp_patch->loaded = 0;
1015 						tmp_patch->inuse_count = 0;
1016 					}
1017 				}
1018 			}
1019 
1020 			chr_ptr = strchr(line_buffer,' ') + 1;
1021 			if (chr_ptr == NULL) {
1022 				WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1023 				WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
1024 				WM_FreePatches();
1025 				if (config_dir != NULL)
1026 					free (config_dir);
1027 				free (line_buffer);
1028 				free (config_buffer);
1029 				return -1;
1030 			}
1031 			line_ptr = chr_ptr - line_buffer;
1032 			chr_ptr = strchr(&line_buffer[line_ptr],' ');
1033 			if (chr_ptr != NULL) {
1034 				*chr_ptr = '\0';
1035 			}
1036 			if (strncasecmp(&line_buffer[(line_ptr + strlen(&line_buffer[line_ptr]) - 5)], ".pat", 4) != 0, 0) {
1037 				if (config_dir != NULL) {
1038 					tmp_patch->filename = malloc(strlen(config_dir) + strlen(&line_buffer[line_ptr]) + 5);
1039 					if (tmp_patch->filename == NULL) {
1040 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
1041 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
1042 						WM_FreePatches();
1043 						if (config_dir != NULL)
1044 							free (config_dir);
1045 						free (line_buffer);
1046 						free (config_buffer);
1047 						return -1;
1048 					}
1049 					strcpy(tmp_patch->filename, config_dir);
1050 					strcat(tmp_patch->filename, &line_buffer[line_ptr]);
1051 				} else {
1052 					tmp_patch->filename = malloc(strlen(&line_buffer[line_ptr]) + 5);
1053 					if (tmp_patch->filename == NULL) {
1054 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
1055 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
1056 						WM_FreePatches();
1057 						free (line_buffer);
1058 						free (config_buffer);
1059 						return -1;
1060 					}
1061 					strcpy(tmp_patch->filename, &line_buffer[line_ptr]);
1062 				}
1063 				strcat(tmp_patch->filename, ".pat");
1064 			} else {
1065 				if (config_dir != NULL) {
1066 					tmp_patch->filename = malloc(strlen(config_dir) + strlen(&line_buffer[line_ptr]) + 1);
1067 					if (tmp_patch->filename == NULL) {
1068 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
1069 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
1070 						WM_FreePatches();
1071 						if (config_dir != NULL)
1072 							free (config_dir);
1073 						free (line_buffer);
1074 						free (config_buffer);
1075 						return -1;
1076 					}
1077 					strcpy(tmp_patch->filename, config_dir);
1078 					strcat(tmp_patch->filename, &line_buffer[line_ptr]);
1079 				} else {
1080 					tmp_patch->filename = malloc(strlen(&line_buffer[line_ptr]) + 1);
1081 					if (tmp_patch->filename == NULL) {
1082 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
1083 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
1084 						WM_FreePatches();
1085 						free (line_buffer);
1086 						free (config_buffer);
1087 						return -1;
1088 					}
1089 					strcpy(tmp_patch->filename, &line_buffer[line_ptr]);
1090 				}
1091 			}
1092 
1093 			tmp_patch->env[0].set = 0x00;
1094 			tmp_patch->env[1].set = 0x00;
1095 			tmp_patch->env[2].set = 0x00;
1096 			tmp_patch->env[3].set = 0x00;
1097 			tmp_patch->env[4].set = 0x00;
1098 			tmp_patch->env[5].set = 0x00;
1099 			tmp_patch->keep = 0;
1100 			tmp_patch->remove = 0;
1101 
1102 			if (chr_ptr != NULL) {
1103 				line_ptr = chr_ptr - line_buffer + 1;
1104 				chr_ptr = strstr(&line_buffer[line_ptr], "amp=");
1105 				if (chr_ptr != NULL) {
1106 					if (!isdigit(chr_ptr[4])) {
1107 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1108 					} else {
1109 						tmp_patch->amp = ((atoi(&chr_ptr[4]) << 10) / 100);
1110 					}
1111 				}
1112 				chr_ptr = strstr(&line_buffer[line_ptr], "note=");
1113 				if (chr_ptr != NULL) {
1114 					if (!isdigit(chr_ptr[5])) {
1115 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1116 					} else {
1117 						tmp_patch->note = atoi(&chr_ptr[5]);
1118 					}
1119 				}
1120 				chr_ptr = strstr(&line_buffer[line_ptr], "keep=loop");
1121 				if (chr_ptr != NULL) {
1122 					tmp_patch->keep |= SAMPLE_LOOP;
1123 				}
1124 				chr_ptr = strstr(&line_buffer[line_ptr], "keep=env");
1125 				if (chr_ptr != NULL) {
1126 					tmp_patch->keep |= SAMPLE_ENVELOPE;
1127 				}
1128 				chr_ptr = strstr(&line_buffer[line_ptr], "remove=sustain");
1129 				if (chr_ptr != NULL) {
1130 					tmp_patch->remove |= SAMPLE_SUSTAIN;
1131 				}
1132 				chr_ptr = strstr(&line_buffer[line_ptr], "env_time0=");
1133 				if (chr_ptr != NULL) {
1134 					if (!isdigit(chr_ptr[10])) {
1135 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1136 					} else {
1137 						tmp_patch->env[0].time = atof(&chr_ptr[10]);
1138 						if ((tmp_patch->env[0].time > 45000.0) || (tmp_patch->env[0].time < 1.47)) {
1139 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
1140 							tmp_patch->env[0].set &= 0xFE;
1141 						} else {
1142 							tmp_patch->env[0].set |= 0x01;
1143 						}
1144 					}
1145 				}
1146 
1147 				chr_ptr = strstr(&line_buffer[line_ptr], "env_time1=");
1148 				if (chr_ptr != NULL) {
1149 					if (!isdigit(chr_ptr[10])) {
1150 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1151 					} else {
1152 						tmp_patch->env[1].time = atof(&chr_ptr[10]);
1153 						if ((tmp_patch->env[1].time > 45000.0) || (tmp_patch->env[1].time < 1.47)) {
1154 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
1155 							tmp_patch->env[1].set &= 0xFE;
1156 						} else {
1157 							tmp_patch->env[1].set |= 0x01;
1158 						}
1159 					}
1160 				}
1161 				chr_ptr = strstr(&line_buffer[line_ptr], "env_time2=");
1162 				if (chr_ptr != NULL) {
1163 					if (!isdigit(chr_ptr[10])) {
1164 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1165 					} else {
1166 						tmp_patch->env[2].time = atof(&chr_ptr[10]);
1167 						if ((tmp_patch->env[2].time > 45000.0) || (tmp_patch->env[2].time < 1.47)) {
1168 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
1169 							tmp_patch->env[2].set &= 0xFE;
1170 						} else {
1171 							tmp_patch->env[2].set |= 0x01;
1172 						}
1173 					}
1174 				}
1175 				chr_ptr = strstr(&line_buffer[line_ptr], "env_time3=");
1176 				if (chr_ptr != NULL) {
1177 					if (!isdigit(chr_ptr[10])) {
1178 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1179 					} else {
1180 						tmp_patch->env[3].time = atof(&chr_ptr[10]);
1181 						if ((tmp_patch->env[3].time > 45000.0) || (tmp_patch->env[3].time < 1.47)) {
1182 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
1183 							tmp_patch->env[3].set &= 0xFE;
1184 						} else {
1185 							tmp_patch->env[3].set |= 0x01;
1186 						}
1187 					}
1188 				}
1189 				chr_ptr = strstr(&line_buffer[line_ptr], "env_time4=");
1190 				if (chr_ptr != NULL) {
1191 					if (!isdigit(chr_ptr[10])) {
1192 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1193 					} else {
1194 						tmp_patch->env[4].time = atof(&chr_ptr[10]);
1195 						if ((tmp_patch->env[4].time > 45000.0) || (tmp_patch->env[4].time < 1.47)) {
1196 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
1197 							tmp_patch->env[4].set &= 0xFE;
1198 						} else {
1199 							tmp_patch->env[4].set |= 0x01;
1200 						}
1201 					}
1202 				}
1203 				chr_ptr = strstr(&line_buffer[line_ptr], "env_time5=");
1204 				if (chr_ptr != NULL) {
1205 					if (!isdigit(chr_ptr[10])) {
1206 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1207 					} else {
1208 						tmp_patch->env[5].time = atof(&chr_ptr[10]);
1209 						if ((tmp_patch->env[5].time > 45000.0) || (tmp_patch->env[5].time < 1.47)) {
1210 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
1211 							tmp_patch->env[5].set &= 0xFE;
1212 						} else {
1213 							tmp_patch->env[5].set |= 0x01;
1214 						}
1215 					}
1216 				}
1217 				chr_ptr = strstr(&line_buffer[line_ptr], "env_level0=");
1218 				if (chr_ptr != NULL) {
1219 					if (!isdigit(chr_ptr[11])) {
1220 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1221 					} else {
1222 						tmp_patch->env[0].level = atof(&chr_ptr[10]);
1223 						if ((tmp_patch->env[0].level > 1.0) || (tmp_patch->env[0].level < 0.0)) {
1224 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
1225 							tmp_patch->env[0].set &= 0xFD;
1226 						} else {
1227 							tmp_patch->env[0].set |= 0x02;
1228 						}
1229 					}
1230 				}
1231 
1232 				chr_ptr = strstr(&line_buffer[line_ptr], "env_level1=");
1233 				if (chr_ptr != NULL) {
1234 					if (!isdigit(chr_ptr[11])) {
1235 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1236 					} else {
1237 						tmp_patch->env[1].level = atof(&chr_ptr[10]);
1238 						if ((tmp_patch->env[1].level > 1.0) || (tmp_patch->env[1].level < 0.0)) {
1239 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
1240 							tmp_patch->env[1].set &= 0xFD;
1241 						} else {
1242 							tmp_patch->env[1].set |= 0x02;
1243 						}
1244 					}
1245 				}
1246 				chr_ptr = strstr(&line_buffer[line_ptr], "env_level2=");
1247 				if (chr_ptr != NULL) {
1248 					if (!isdigit(chr_ptr[11])) {
1249 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1250 					} else {
1251 						tmp_patch->env[2].level = atof(&chr_ptr[10]);
1252 						if ((tmp_patch->env[2].level > 1.0) || (tmp_patch->env[2].level < 0.0)) {
1253 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
1254 							tmp_patch->env[2].set &= 0xFD;
1255 						} else {
1256 							tmp_patch->env[2].set |= 0x02;
1257 						}
1258 					}
1259 				}
1260 				chr_ptr = strstr(&line_buffer[line_ptr], "env_level3=");
1261 				if (chr_ptr != NULL) {
1262 					if (!isdigit(chr_ptr[11])) {
1263 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1264 					} else {
1265 						tmp_patch->env[3].level = atof(&chr_ptr[10]);
1266 						if ((tmp_patch->env[3].level > 1.0) || (tmp_patch->env[3].level < 0.0)) {
1267 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
1268 							tmp_patch->env[3].set &= 0xFD;
1269 						} else {
1270 							tmp_patch->env[3].set |= 0x02;
1271 						}
1272 					}
1273 				}
1274 				chr_ptr = strstr(&line_buffer[line_ptr], "env_level4=");
1275 				if (chr_ptr != NULL) {
1276 					if (!isdigit(chr_ptr[11])) {
1277 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1278 					} else {
1279 						tmp_patch->env[4].level = atof(&chr_ptr[10]);
1280 						if ((tmp_patch->env[4].level > 1.0) || (tmp_patch->env[4].level < 0.0)) {
1281 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
1282 							tmp_patch->env[4].set &= 0xFD;
1283 						} else {
1284 							tmp_patch->env[4].set |= 0x02;
1285 						}
1286 					}
1287 				}
1288 				chr_ptr = strstr(&line_buffer[line_ptr], "env_level5=");
1289 				if (chr_ptr != NULL) {
1290 					if (!isdigit(chr_ptr[11])) {
1291 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
1292 					} else {
1293 						tmp_patch->env[5].level = atof(&chr_ptr[10]);
1294 						if ((tmp_patch->env[5].level > 1.0) || (tmp_patch->env[5].level < 0.0)) {
1295 							WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
1296 							tmp_patch->env[5].set &= 0xFD;
1297 						} else {
1298 							tmp_patch->env[5].set |= 0x02;
1299 						}
1300 					}
1301 				}
1302 
1303 			}
1304 		}
1305 	}
1306 
1307 	if (line_buffer != NULL)
1308 		free (line_buffer);
1309 
1310 	free (config_buffer);
1311 
1312 	if (config_dir != NULL)
1313 		free (config_dir);
1314 
1315 	return 0;
1316 }
1317 
1318 /*
1319  * sample data conversion functions
1320  * convert data to signed shorts
1321  */
1322 
1323 /* 8bit signed */
1324 int
1325 convert_8s (unsigned char *data, struct _sample *gus_sample ) {
1326 	unsigned char *read_data = data;
1327 	unsigned char *read_end = data + gus_sample->data_length;
1328 	signed short int *write_data = NULL;
1329 
1330 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1331 	gus_sample->data = calloc((gus_sample->data_length + 1), sizeof(signed short int));
1332 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1333 		write_data = gus_sample->data;
1334 		do {
1335 			*write_data = (*read_data++) << 8;
1336 			if (*write_data > gus_sample->max_peek) {
1337 				gus_sample->max_peek = *write_data;
1338 			} else if (*write_data < gus_sample->min_peek) {
1339 				gus_sample->min_peek = *write_data;
1340 			}
1341 			write_data++;
1342 		} while (read_data != read_end);
1343 		return 0;
1344 	}
1345 
1346 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1347 	return -1;
1348 }
1349 
1350 /* 8bit signed ping pong */
1351 int
1352 convert_8sp (unsigned char *data, struct _sample *gus_sample ) {
1353 	unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
1354 	unsigned long int dloop_length = loop_length * 2;
1355 	unsigned long int new_length = gus_sample->data_length + dloop_length;
1356 	unsigned char *read_data = data;
1357 	unsigned char *read_end = data + gus_sample->loop_start;
1358 	signed short int *write_data = NULL;
1359 	signed short int *write_data_a = NULL;
1360 	signed short int *write_data_b = NULL;
1361 
1362 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1363 	gus_sample->data = calloc((new_length + 1), sizeof(signed short int));
1364 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1365 		write_data = gus_sample->data;
1366 		do {
1367 			*write_data = (*read_data++) << 8;
1368 			if (*write_data > gus_sample->max_peek) {
1369 				gus_sample->max_peek = *write_data;
1370 			} else if (*write_data < gus_sample->min_peek) {
1371 				gus_sample->min_peek = *write_data;
1372 			}
1373 			write_data++;
1374 		} while (read_data != read_end);
1375 
1376 		*write_data = (*read_data++ << 8);
1377 		write_data_a = write_data + dloop_length;
1378 		*write_data_a-- = *write_data;
1379 		write_data++;
1380 		write_data_b = write_data + dloop_length;
1381 		read_end = data + gus_sample->loop_end;
1382 		do {
1383 			*write_data = (*read_data++) << 8;
1384 			*write_data_a-- = *write_data;
1385 			*write_data_b++ = *write_data;
1386 			if (*write_data > gus_sample->max_peek) {
1387 				gus_sample->max_peek = *write_data;
1388 			} else if (*write_data < gus_sample->min_peek) {
1389 				gus_sample->min_peek = *write_data;
1390 			}
1391 			write_data++;
1392 		} while (read_data != read_end);
1393 
1394 		*write_data = (*read_data++ << 8);
1395 		*write_data_b++ = *write_data;
1396 		read_end = data + gus_sample->data_length;
1397 		if (__builtin_expect((read_data != read_end),1)) {
1398 			do {
1399 				*write_data_b = (*read_data++) << 8;
1400 				if (*write_data_b > gus_sample->max_peek) {
1401 					gus_sample->max_peek = *write_data_b;
1402 				} else if (*write_data_b < gus_sample->min_peek) {
1403 					gus_sample->min_peek = *write_data_b;
1404 				}
1405 				write_data_b++;
1406 			} while (read_data != read_end);
1407 		}
1408 		gus_sample->loop_start += loop_length;
1409 		gus_sample->loop_end += dloop_length;
1410 		gus_sample->data_length = new_length;
1411 		gus_sample->modes ^= SAMPLE_PINGPONG;
1412 		return 0;
1413 	}
1414 
1415 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1416 	return -1;
1417 }
1418 
1419 /* 8bit signed reverse */
1420 int
1421 convert_8sr (unsigned char *data, struct _sample *gus_sample ) {
1422 	unsigned char *read_data = data;
1423 	unsigned char *read_end = data + gus_sample->data_length;
1424 	signed short int *write_data = NULL;
1425 	unsigned long int tmp_loop = 0;
1426 
1427 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1428 	gus_sample->data = calloc((gus_sample->data_length + 1), sizeof(signed short int));
1429 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1430 		write_data = gus_sample->data + gus_sample->data_length - 1;
1431 		do {
1432 			*write_data = (*read_data++) << 8;
1433 			if (*write_data > gus_sample->max_peek) {
1434 				gus_sample->max_peek = *write_data;
1435 			} else if (*write_data < gus_sample->min_peek) {
1436 				gus_sample->min_peek = *write_data;
1437 			}
1438 			write_data--;
1439 		} while (read_data != read_end);
1440 		tmp_loop = gus_sample->loop_end;
1441 		gus_sample->loop_end = gus_sample->data_length - gus_sample->loop_start;
1442 		gus_sample->loop_start = gus_sample->data_length - tmp_loop;
1443 		gus_sample->loop_fraction  = ((gus_sample->loop_fraction & 0x0f) << 4) | ((gus_sample->loop_fraction & 0xf0) >> 4);
1444 		gus_sample->modes ^= SAMPLE_REVERSE;
1445 		return 0;
1446 	}
1447 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1448 	return -1;
1449 }
1450 
1451 
1452 /* 8bit signed reverse ping pong */
1453 int
1454 convert_8srp (unsigned char *data, struct _sample *gus_sample ) {
1455 	unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
1456 	unsigned long int dloop_length = loop_length * 2;
1457 	unsigned long int new_length = gus_sample->data_length + dloop_length;
1458 	unsigned char *read_data = data + gus_sample->data_length - 1;
1459 	unsigned char *read_end = data + gus_sample->loop_end;
1460 	signed short int *write_data = NULL;
1461 	signed short int *write_data_a = NULL;
1462 	signed short int *write_data_b = NULL;
1463 
1464 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1465 	gus_sample->data = calloc((new_length + 1), sizeof(signed short int));
1466 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1467 		write_data = gus_sample->data;
1468 		do {
1469 			*write_data = (*read_data--) << 8;
1470 			if (*write_data > gus_sample->max_peek) {
1471 				gus_sample->max_peek = *write_data;
1472 			} else if (*write_data < gus_sample->min_peek) {
1473 				gus_sample->min_peek = *write_data;
1474 			}
1475 			write_data++;
1476 		} while (read_data != read_end);
1477 
1478 		*write_data = (*read_data-- << 8);
1479 		write_data_a = write_data + dloop_length;
1480 		*write_data_a-- = *write_data;
1481 		write_data++;
1482 		write_data_b = write_data + dloop_length;
1483 		read_end = data + gus_sample->loop_start;
1484 		do {
1485 			*write_data = (*read_data--) << 8;
1486 			*write_data_a-- = *write_data;
1487 			*write_data_b++ = *write_data;
1488 			if (*write_data > gus_sample->max_peek) {
1489 				gus_sample->max_peek = *write_data;
1490 			} else if (*write_data < gus_sample->min_peek) {
1491 				gus_sample->min_peek = *write_data;
1492 			}
1493 			write_data++;
1494 		} while (read_data != read_end);
1495 
1496 		*write_data = (*read_data-- << 8);
1497 		*write_data_b++ = *write_data;
1498 		read_end = data - 1;
1499 		do {
1500 			*write_data_b = (*read_data--) << 8;
1501 			if (*write_data_b > gus_sample->max_peek) {
1502 				gus_sample->max_peek = *write_data_b;
1503 			} else if (*write_data_b < gus_sample->min_peek) {
1504 				gus_sample->min_peek = *write_data_b;
1505 			}
1506 			write_data_b++;
1507 		} while (read_data != read_end);
1508 		gus_sample->loop_start += loop_length;
1509 		gus_sample->loop_end += dloop_length;
1510 		gus_sample->data_length = new_length;
1511 		gus_sample->modes ^= SAMPLE_PINGPONG | SAMPLE_REVERSE;
1512 		return 0;
1513 	}
1514 
1515 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1516 	return -1;
1517 }
1518 
1519 /* 8bit unsigned */
1520 int
1521 convert_8u (unsigned char *data, struct _sample *gus_sample ) {
1522 	unsigned char *read_data = data;
1523 	unsigned char *read_end = data + gus_sample->data_length;
1524 	signed short int *write_data = NULL;
1525 
1526 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1527 	gus_sample->data = calloc((gus_sample->data_length + 1), sizeof(signed short int));
1528 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1529 		write_data = gus_sample->data;
1530 		do {
1531 			*write_data = ((*read_data++) ^ 0x80) << 8;
1532 			if (*write_data > gus_sample->max_peek) {
1533 				gus_sample->max_peek = *write_data;
1534 			} else if (*write_data < gus_sample->min_peek) {
1535 				gus_sample->min_peek = *write_data;
1536 			}
1537 			write_data++;
1538 		} while (read_data != read_end);
1539 		gus_sample->modes ^= SAMPLE_UNSIGNED;
1540 		return 0;
1541 	}
1542 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1543 	return -1;
1544 }
1545 
1546 /* 8bit unsigned ping pong */
1547 int
1548 convert_8up (unsigned char *data, struct _sample *gus_sample ) {
1549 	unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
1550 	unsigned long int dloop_length = loop_length * 2;
1551 	unsigned long int new_length = gus_sample->data_length + dloop_length;
1552 	unsigned char *read_data = data;
1553 	unsigned char *read_end = data + gus_sample->loop_start;
1554 	signed short int *write_data = NULL;
1555 	signed short int *write_data_a = NULL;
1556 	signed short int *write_data_b = NULL;
1557 
1558 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1559 	gus_sample->data = calloc((new_length + 1), sizeof(signed short int));
1560 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1561 		write_data = gus_sample->data;
1562 		do {
1563 			*write_data = ((*read_data++) ^ 0x80) << 8;
1564 			if (*write_data > gus_sample->max_peek) {
1565 				gus_sample->max_peek = *write_data;
1566 			} else if (*write_data < gus_sample->min_peek) {
1567 				gus_sample->min_peek = *write_data;
1568 			}
1569 			write_data++;
1570 		} while (read_data != read_end);
1571 
1572 		*write_data = ((*read_data++) ^ 0x80) << 8;
1573 		write_data_a = write_data + dloop_length;
1574 		*write_data_a-- = *write_data;
1575 		write_data++;
1576 		write_data_b = write_data + dloop_length;
1577 		read_end = data + gus_sample->loop_end;
1578 		do {
1579 			*write_data = ((*read_data++) ^ 0x80) << 8;
1580 			*write_data_a-- = *write_data;
1581 			*write_data_b++ = *write_data;
1582 			if (*write_data > gus_sample->max_peek) {
1583 				gus_sample->max_peek = *write_data;
1584 			} else if (*write_data < gus_sample->min_peek) {
1585 				gus_sample->min_peek = *write_data;
1586 			}
1587 			write_data++;
1588 		} while (read_data != read_end);
1589 
1590 		*write_data = ((*read_data++) ^ 0x80) << 8;
1591 		*write_data_b++ = *write_data;
1592 		read_end = data + gus_sample->data_length;
1593 		if (__builtin_expect((read_data != read_end),1)) {
1594 			do {
1595 				*write_data_b = ((*read_data++) ^ 0x80) << 8;
1596 				if (*write_data_b > gus_sample->max_peek) {
1597 					gus_sample->max_peek = *write_data_b;
1598 				} else if (*write_data_b < gus_sample->min_peek) {
1599 					gus_sample->min_peek = *write_data_b;
1600 				}
1601 				write_data_b++;
1602 			} while (read_data != read_end);
1603 		}
1604 		gus_sample->loop_start += loop_length;
1605 		gus_sample->loop_end += dloop_length;
1606 		gus_sample->data_length = new_length;
1607 		gus_sample->modes ^= SAMPLE_PINGPONG | SAMPLE_UNSIGNED;
1608 		return 0;
1609 	}
1610 
1611 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1612 	return -1;
1613 }
1614 
1615 /* 8bit unsigned reverse */
1616 int
1617 convert_8ur (unsigned char *data, struct _sample *gus_sample ) {
1618 	unsigned char *read_data = data;
1619 	unsigned char *read_end = data + gus_sample->data_length;
1620 	signed short int *write_data = NULL;
1621 	unsigned long int tmp_loop = 0;
1622 
1623 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1624 	gus_sample->data = calloc((gus_sample->data_length + 1), sizeof(signed short int));
1625 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1626 		write_data = gus_sample->data + gus_sample->data_length - 1;
1627 		do {
1628 			*write_data = ((*read_data++) ^ 0x80) << 8;
1629 			if (*write_data > gus_sample->max_peek) {
1630 				gus_sample->max_peek = *write_data;
1631 			} else if (*write_data < gus_sample->min_peek) {
1632 				gus_sample->min_peek = *write_data;
1633 			}
1634 			write_data--;
1635 		} while (read_data != read_end);
1636 		tmp_loop = gus_sample->loop_end;
1637 		gus_sample->loop_end = gus_sample->data_length - gus_sample->loop_start;
1638 		gus_sample->loop_start = gus_sample->data_length - tmp_loop;
1639 		gus_sample->loop_fraction  = ((gus_sample->loop_fraction & 0x0f) << 4) | ((gus_sample->loop_fraction & 0xf0) >> 4);
1640 		gus_sample->modes ^= SAMPLE_REVERSE | SAMPLE_UNSIGNED;
1641 		return 0;
1642 	}
1643 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1644 	return -1;
1645 }
1646 
1647 /* 8bit unsigned reverse ping pong */
1648 int
1649 convert_8urp (unsigned char *data, struct _sample *gus_sample ) {
1650 	unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
1651 	unsigned long int dloop_length = loop_length * 2;
1652 	unsigned long int new_length = gus_sample->data_length + dloop_length;
1653 	unsigned char *read_data = data + gus_sample->data_length - 1;
1654 	unsigned char *read_end = data + gus_sample->loop_end;
1655 	signed short int *write_data = NULL;
1656 	signed short int *write_data_a = NULL;
1657 	signed short int *write_data_b = NULL;
1658 
1659 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1660 	gus_sample->data = calloc((new_length + 1), sizeof(signed short int));
1661 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1662 		write_data = gus_sample->data;
1663 		do {
1664 			*write_data = ((*read_data--) ^ 0x80) << 8;
1665 			if (*write_data > gus_sample->max_peek) {
1666 				gus_sample->max_peek = *write_data;
1667 			} else if (*write_data < gus_sample->min_peek) {
1668 				gus_sample->min_peek = *write_data;
1669 			}
1670 			write_data++;
1671 		} while (read_data != read_end);
1672 
1673 		*write_data = ((*read_data--) ^ 0x80) << 8;
1674 		write_data_a = write_data + dloop_length;
1675 		*write_data_a-- = *write_data;
1676 		write_data++;
1677 		write_data_b = write_data + dloop_length;
1678 		read_end = data + gus_sample->loop_start;
1679 		do {
1680 			*write_data = ((*read_data--) ^ 0x80) << 8;
1681 			*write_data_a-- = *write_data;
1682 			*write_data_b++ = *write_data;
1683 			if (*write_data > gus_sample->max_peek) {
1684 				gus_sample->max_peek = *write_data;
1685 			} else if (*write_data < gus_sample->min_peek) {
1686 				gus_sample->min_peek = *write_data;
1687 			}
1688 			write_data++;
1689 		} while (read_data != read_end);
1690 
1691 		*write_data = ((*read_data--) ^ 0x80) << 8;
1692 		*write_data_b++ = *write_data;
1693 		read_end = data - 1;
1694 		do {
1695 			*write_data_b = ((*read_data--) ^ 0x80) << 8;
1696 			if (*write_data_b > gus_sample->max_peek) {
1697 				gus_sample->max_peek = *write_data_b;
1698 			} else if (*write_data_b < gus_sample->min_peek) {
1699 				gus_sample->min_peek = *write_data_b;
1700 			}
1701 			write_data_b++;
1702 		} while (read_data != read_end);
1703 		gus_sample->loop_start += loop_length;
1704 		gus_sample->loop_end += dloop_length;
1705 		gus_sample->data_length = new_length;
1706 		gus_sample->modes ^= SAMPLE_PINGPONG | SAMPLE_REVERSE | SAMPLE_UNSIGNED;
1707 		return 0;
1708 	}
1709 
1710 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1711 	return -1;
1712 }
1713 
1714 /* 16bit signed */
1715 int
1716 convert_16s (unsigned char *data, struct _sample *gus_sample ) {
1717 	unsigned char *read_data = data;
1718 	unsigned char *read_end = data + gus_sample->data_length;
1719 	signed short int *write_data = NULL;
1720 
1721 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1722 	gus_sample->data = calloc(((gus_sample->data_length >> 1) + 1),sizeof(signed short int));
1723 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1724 		write_data = gus_sample->data;
1725 		do {
1726 			*write_data = *read_data++;
1727 			*write_data |= (*read_data++) << 8;
1728 			if (*write_data > gus_sample->max_peek) {
1729 				gus_sample->max_peek = *write_data;
1730 			} else if (*write_data < gus_sample->min_peek) {
1731 				gus_sample->min_peek = *write_data;
1732 			}
1733 			write_data++;
1734 		} while (read_data < read_end);
1735 
1736 		gus_sample->loop_start >>= 1;
1737 		gus_sample->loop_end >>= 1;
1738 		gus_sample->data_length >>= 1;
1739 		return 0;
1740 	}
1741 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1742 	return -1;
1743 }
1744 
1745 /* 16bit signed ping pong */
1746 int
1747 convert_16sp (unsigned char *data, struct _sample *gus_sample ) {
1748 	unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
1749 	unsigned long int dloop_length = loop_length * 2;
1750 	unsigned long int new_length = gus_sample->data_length + dloop_length;
1751 	unsigned char *read_data = data;
1752 	unsigned char *read_end = data + gus_sample->loop_start;
1753 	signed short int *write_data = NULL;
1754 	signed short int *write_data_a = NULL;
1755 	signed short int *write_data_b = NULL;
1756 
1757 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1758 	gus_sample->data = calloc(((new_length >> 1) + 1), sizeof(signed short int));
1759 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1760 		write_data = gus_sample->data;
1761 		do {
1762 			*write_data = (*read_data++);
1763 			*write_data |= (*read_data++) << 8;
1764 			if (*write_data > gus_sample->max_peek) {
1765 				gus_sample->max_peek = *write_data;
1766 			} else if (*write_data < gus_sample->min_peek) {
1767 				gus_sample->min_peek = *write_data;
1768 			}
1769 			write_data++;
1770 		} while (read_data < read_end);
1771 
1772 		*write_data = (*read_data++);
1773 		*write_data |= (*read_data++) << 8;
1774 		write_data_a = write_data + (dloop_length >> 1);
1775 		*write_data_a-- = *write_data;
1776 		write_data++;
1777 		write_data_b = write_data + (dloop_length >> 1);
1778 		read_end = data + gus_sample->loop_end;
1779 		do {
1780 			*write_data = (*read_data++);
1781 			*write_data |= (*read_data++) << 8;
1782 			*write_data_a-- = *write_data;
1783 			*write_data_b++ = *write_data;
1784 			if (*write_data > gus_sample->max_peek) {
1785 				gus_sample->max_peek = *write_data;
1786 			} else if (*write_data < gus_sample->min_peek) {
1787 				gus_sample->min_peek = *write_data;
1788 			}
1789 			write_data++;
1790 		} while (read_data < read_end);
1791 
1792 		*write_data = *(read_data++);
1793 		*write_data |= (*read_data++) << 8;
1794 		*write_data_b++ = *write_data;
1795 		read_end = data + gus_sample->data_length;
1796 		if (__builtin_expect((read_data != read_end),1)) {
1797 			do {
1798 				*write_data_b = *(read_data++);
1799 				*write_data_b |= (*read_data++) << 8;
1800 				if (*write_data_b > gus_sample->max_peek) {
1801 					gus_sample->max_peek = *write_data_b;
1802 				} else if (*write_data_b < gus_sample->min_peek) {
1803 					gus_sample->min_peek = *write_data_b;
1804 				}
1805 				write_data_b++;
1806 			} while (read_data < read_end);
1807 		}
1808 		gus_sample->loop_start += loop_length;
1809 		gus_sample->loop_end += dloop_length;
1810 		gus_sample->data_length = new_length;
1811 		gus_sample->modes ^= SAMPLE_PINGPONG;
1812 		gus_sample->loop_start >>= 1;
1813 		gus_sample->loop_end >>= 1;
1814 		gus_sample->data_length >>= 1;
1815 		return 0;
1816 	}
1817 
1818 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1819 	return -1;
1820 }
1821 
1822 /* 16bit signed reverse */
1823 int
1824 convert_16sr (unsigned char *data, struct _sample *gus_sample ) {
1825 	unsigned char *read_data = data;
1826 	unsigned char *read_end = data + gus_sample->data_length;
1827 	signed short int *write_data = NULL;
1828 	unsigned long int tmp_loop = 0;
1829 
1830 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1831 	gus_sample->data = calloc(((gus_sample->data_length >> 1) + 1), sizeof(signed short int));
1832 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1833 		write_data = gus_sample->data + (gus_sample->data_length >> 1) - 1;
1834 		do {
1835 			*write_data = *read_data++;
1836 			*write_data |= (*read_data++) << 8;
1837 			if (*write_data > gus_sample->max_peek) {
1838 				gus_sample->max_peek = *write_data;
1839 			} else if (*write_data < gus_sample->min_peek) {
1840 				gus_sample->min_peek = *write_data;
1841 			}
1842 			write_data--;
1843 		} while (read_data < read_end);
1844 		tmp_loop = gus_sample->loop_end;
1845 		gus_sample->loop_end = gus_sample->data_length - gus_sample->loop_start;
1846 		gus_sample->loop_start = gus_sample->data_length - tmp_loop;
1847 		gus_sample->loop_fraction  = ((gus_sample->loop_fraction & 0x0f) << 4) | ((gus_sample->loop_fraction & 0xf0) >> 4);
1848 		gus_sample->loop_start >>= 1;
1849 		gus_sample->loop_end >>= 1;
1850 		gus_sample->data_length >>= 1;
1851 		gus_sample->modes ^= SAMPLE_REVERSE;
1852 		return 0;
1853 	}
1854 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1855 	return -1;
1856 }
1857 
1858 
1859 /* 16bit signed reverse ping pong */
1860 int
1861 convert_16srp (unsigned char *data, struct _sample *gus_sample ) {
1862 	unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
1863 	unsigned long int dloop_length = loop_length * 2;
1864 	unsigned long int new_length = gus_sample->data_length + dloop_length;
1865 	unsigned char *read_data = data + gus_sample->data_length - 1;
1866 	unsigned char *read_end = data + gus_sample->loop_end;
1867 	signed short int *write_data = NULL;
1868 	signed short int *write_data_a = NULL;
1869 	signed short int *write_data_b = NULL;
1870 
1871 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1872 	gus_sample->data = calloc(((new_length >> 1) + 1), sizeof(signed short int));
1873 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1874 		write_data = gus_sample->data;
1875 		do {
1876 
1877 			*write_data = (*read_data--) << 8;
1878 			*write_data |= *read_data--;
1879 			if (*write_data > gus_sample->max_peek) {
1880 				gus_sample->max_peek = *write_data;
1881 			} else if (*write_data < gus_sample->min_peek) {
1882 				gus_sample->min_peek = *write_data;
1883 			}
1884 			write_data++;
1885 		} while (read_data < read_end);
1886 
1887 		*write_data = (*read_data-- << 8);
1888 		*write_data |= *read_data--;
1889 		write_data_a = write_data + (dloop_length >> 1);
1890 		*write_data_a-- = *write_data;
1891 		write_data++;
1892 		write_data_b = write_data + (dloop_length >> 1);
1893 		read_end = data + gus_sample->loop_start;
1894 		do {
1895 			*write_data = (*read_data--) << 8;
1896 			*write_data |= *read_data--;
1897 			*write_data_a-- = *write_data;
1898 			*write_data_b++ = *write_data;
1899 			if (*write_data > gus_sample->max_peek) {
1900 				gus_sample->max_peek = *write_data;
1901 			} else if (*write_data < gus_sample->min_peek) {
1902 				gus_sample->min_peek = *write_data;
1903 			}
1904 			write_data++;
1905 		} while (read_data < read_end);
1906 
1907 		*write_data = ((*read_data--) << 8);
1908 		*write_data |= *read_data--;
1909 		*write_data_b++ = *write_data;
1910 		read_end = data - 1;
1911 		do {
1912 			*write_data_b = (*read_data--) << 8;
1913 			*write_data_b |= *read_data--;
1914 			if (*write_data_b > gus_sample->max_peek) {
1915 				gus_sample->max_peek = *write_data_b;
1916 			} else if (*write_data_b < gus_sample->min_peek) {
1917 				gus_sample->min_peek = *write_data_b;
1918 			}
1919 			write_data_b++;
1920 		} while (read_data < read_end);
1921 		gus_sample->loop_start += loop_length;
1922 		gus_sample->loop_end += dloop_length;
1923 		gus_sample->data_length = new_length;
1924 		gus_sample->modes ^= SAMPLE_PINGPONG | SAMPLE_REVERSE;
1925 		return 0;
1926 	}
1927 
1928 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1929 	return -1;
1930 }
1931 
1932 /* 16bit unsigned */
1933 int
1934 convert_16u (unsigned char *data, struct _sample *gus_sample ) {
1935 	unsigned char *read_data = data;
1936 	unsigned char *read_end = data + gus_sample->data_length;
1937 	signed short int *write_data = NULL;
1938 
1939 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1940 	gus_sample->data = calloc(((gus_sample->data_length >> 1) + 1),sizeof(signed short int));
1941 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1942 		write_data = gus_sample->data;
1943 		do {
1944 			*write_data = *read_data++;
1945 			*write_data |= ((*read_data++) ^ 0x80) << 8;
1946 			if (*write_data > gus_sample->max_peek) {
1947 				gus_sample->max_peek = *write_data;
1948 			} else if (*write_data < gus_sample->min_peek) {
1949 				gus_sample->min_peek = *write_data;
1950 			}
1951 			write_data++;
1952 		} while (read_data < read_end);
1953 		gus_sample->loop_start >>= 1;
1954 		gus_sample->loop_end >>= 1;
1955 		gus_sample->data_length >>= 1;
1956 		gus_sample->modes ^= SAMPLE_UNSIGNED;
1957 		return 0;
1958 	}
1959 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1960 	return -1;
1961 }
1962 
1963 /* 16bit unsigned ping pong */
1964 int
1965 convert_16up (unsigned char *data, struct _sample *gus_sample ) {
1966 	unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
1967 	unsigned long int dloop_length = loop_length * 2;
1968 	unsigned long int new_length = gus_sample->data_length + dloop_length;
1969 	unsigned char *read_data = data;
1970 	unsigned char *read_end = data + gus_sample->loop_start;
1971 	signed short int *write_data = NULL;
1972 	signed short int *write_data_a = NULL;
1973 	signed short int *write_data_b = NULL;
1974 
1975 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
1976 	gus_sample->data = calloc(((new_length >> 1) + 1), sizeof(signed short int));
1977 	if (__builtin_expect((gus_sample->data != NULL),1)) {
1978 		write_data = gus_sample->data;
1979 		do {
1980 			*write_data = (*read_data++);
1981 			*write_data |= ((*read_data++) ^ 0x80) << 8;
1982 			if (*write_data > gus_sample->max_peek) {
1983 				gus_sample->max_peek = *write_data;
1984 			} else if (*write_data < gus_sample->min_peek) {
1985 				gus_sample->min_peek = *write_data;
1986 			}
1987 			write_data++;
1988 		} while (read_data < read_end);
1989 
1990 		*write_data = (*read_data++);
1991 		*write_data |= ((*read_data++) ^ 0x80) << 8;
1992 		write_data_a = write_data + (dloop_length >> 1);
1993 		*write_data_a-- = *write_data;
1994 		write_data++;
1995 		write_data_b = write_data + (dloop_length >> 1);
1996 		read_end = data + gus_sample->loop_end;
1997 		do {
1998 			*write_data = (*read_data++);
1999 			*write_data |= ((*read_data++) ^ 0x80) << 8;
2000 			*write_data_a-- = *write_data;
2001 			*write_data_b++ = *write_data;
2002 			if (*write_data > gus_sample->max_peek) {
2003 				gus_sample->max_peek = *write_data;
2004 			} else if (*write_data < gus_sample->min_peek) {
2005 				gus_sample->min_peek = *write_data;
2006 			}
2007 			write_data++;
2008 		} while (read_data < read_end);
2009 
2010 		*write_data = (*read_data++);
2011 		*write_data |= ((*read_data++) ^ 0x80) << 8;
2012 		*write_data_b++ = *write_data;
2013 		read_end = data + gus_sample->data_length;
2014 		if (__builtin_expect((read_data != read_end),1)) {
2015 			do {
2016 				*write_data_b = (*read_data++);
2017 				*write_data_b |= ((*read_data++) ^ 0x80) << 8;
2018 				if (*write_data_b > gus_sample->max_peek) {
2019 					gus_sample->max_peek = *write_data_b;
2020 				} else if (*write_data_b < gus_sample->min_peek) {
2021 					gus_sample->min_peek = *write_data_b;
2022 				}
2023 				write_data_b++;
2024 			} while (read_data < read_end);
2025 		}
2026 		gus_sample->loop_start += loop_length;
2027 		gus_sample->loop_end += dloop_length;
2028 		gus_sample->data_length = new_length;
2029 		gus_sample->modes ^= SAMPLE_PINGPONG;
2030 		gus_sample->loop_start >>= 1;
2031 		gus_sample->loop_end >>= 1;
2032 		gus_sample->data_length >>= 1;
2033 		return 0;
2034 	}
2035 
2036 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
2037 	return -1;
2038 }
2039 
2040 /* 16bit unsigned reverse */
2041 int
2042 convert_16ur (unsigned char *data, struct _sample *gus_sample ) {
2043 	unsigned char *read_data = data;
2044 	unsigned char *read_end = data + gus_sample->data_length;
2045 	signed short int *write_data = NULL;
2046 	unsigned long int tmp_loop = 0;
2047 
2048 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
2049 	gus_sample->data = calloc(((gus_sample->data_length >> 1) + 1), sizeof(signed short int));
2050 	if (__builtin_expect((gus_sample->data != NULL),1)) {
2051 		write_data = gus_sample->data + (gus_sample->data_length >> 1) - 1;
2052 		do {
2053 			*write_data = *read_data++;
2054 			*write_data |= ((*read_data++) ^ 0x80) << 8;
2055 			if (*write_data > gus_sample->max_peek) {
2056 				gus_sample->max_peek = *write_data;
2057 			} else if (*write_data < gus_sample->min_peek) {
2058 				gus_sample->min_peek = *write_data;
2059 			}
2060 			write_data--;
2061 		} while (read_data < read_end);
2062 		tmp_loop = gus_sample->loop_end;
2063 		gus_sample->loop_end = gus_sample->data_length - gus_sample->loop_start;
2064 		gus_sample->loop_start = gus_sample->data_length - tmp_loop;
2065 		gus_sample->loop_fraction  = ((gus_sample->loop_fraction & 0x0f) << 4) | ((gus_sample->loop_fraction & 0xf0) >> 4);
2066 		gus_sample->loop_start >>= 1;
2067 		gus_sample->loop_end >>= 1;
2068 		gus_sample->data_length >>= 1;
2069 		gus_sample->modes ^= SAMPLE_REVERSE | SAMPLE_UNSIGNED;
2070 		return 0;
2071 	}
2072 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
2073 	return -1;
2074 }
2075 
2076 /* 16bit unsigned reverse ping pong */
2077 int
2078 convert_16urp (unsigned char *data, struct _sample *gus_sample ) {
2079 	unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
2080 	unsigned long int dloop_length = loop_length * 2;
2081 	unsigned long int new_length = gus_sample->data_length + dloop_length;
2082 	unsigned char *read_data = data + gus_sample->data_length - 1;
2083 	unsigned char *read_end = data + gus_sample->loop_end;
2084 	signed short int *write_data = NULL;
2085 	signed short int *write_data_a = NULL;
2086 	signed short int *write_data_b = NULL;
2087 
2088 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
2089 	gus_sample->data = calloc(((new_length >> 1) + 1), sizeof(signed short int));
2090 	if (__builtin_expect((gus_sample->data != NULL),1)) {
2091 		write_data = gus_sample->data;
2092 		do {
2093 			*write_data = ((*read_data--) ^ 0x80) << 8;
2094 			*write_data |= *read_data--;
2095 			if (*write_data > gus_sample->max_peek) {
2096 				gus_sample->max_peek = *write_data;
2097 			} else if (*write_data < gus_sample->min_peek) {
2098 				gus_sample->min_peek = *write_data;
2099 			}
2100 			write_data++;
2101 		} while (read_data < read_end);
2102 
2103 		*write_data = ((*read_data--) ^ 0x80) << 8;
2104 		*write_data |= *read_data--;
2105 		write_data_a = write_data + (dloop_length >> 1);
2106 		*write_data_a-- = *write_data;
2107 		write_data++;
2108 		write_data_b = write_data + (dloop_length >> 1);
2109 		read_end = data + gus_sample->loop_start;
2110 		do {
2111 			*write_data = ((*read_data--) ^ 0x80) << 8;
2112 			*write_data |= *read_data--;
2113 			*write_data_a-- = *write_data;
2114 			*write_data_b++ = *write_data;
2115 			if (*write_data > gus_sample->max_peek) {
2116 				gus_sample->max_peek = *write_data;
2117 			} else if (*write_data < gus_sample->min_peek) {
2118 				gus_sample->min_peek = *write_data;
2119 			}
2120 			write_data++;
2121 		} while (read_data < read_end);
2122 
2123 		*write_data = ((*read_data--) ^ 0x80) << 8;
2124 		*write_data |= *read_data--;
2125 		*write_data_b++ = *write_data;
2126 		read_end = data - 1;
2127 		do {
2128 			*write_data_b = ((*read_data--) ^ 0x80) << 8;
2129 			*write_data_b |= *read_data--;
2130 			if (*write_data_b > gus_sample->max_peek) {
2131 				gus_sample->max_peek = *write_data_b;
2132 			} else if (*write_data_b < gus_sample->min_peek) {
2133 				gus_sample->min_peek = *write_data_b;
2134 			}
2135 			write_data_b++;
2136 		} while (read_data < read_end);
2137 		gus_sample->loop_start += loop_length;
2138 		gus_sample->loop_end += dloop_length;
2139 		gus_sample->data_length = new_length;
2140 		gus_sample->modes ^= SAMPLE_PINGPONG | SAMPLE_REVERSE | SAMPLE_UNSIGNED;
2141 		return 0;
2142 	}
2143 
2144 	WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
2145 	return -1;
2146 }
2147 
2148 
2149 /* sample loading */
2150 
2151 int
2152 load_sample (struct _patch *sample_patch) {
2153 	unsigned char *gus_patch;
2154 	unsigned long int gus_size;
2155 	unsigned long int gus_ptr;
2156 	unsigned char no_of_samples;
2157 	struct _sample *gus_sample = NULL;
2158 	unsigned long int i = 0;
2159 
2160 	int (*do_convert[])(unsigned char *data, struct _sample *gus_sample ) = {
2161 		convert_8s,
2162 		convert_16s,
2163 		convert_8u,
2164 		convert_16u,
2165 		convert_8sp,
2166 		convert_16sp,
2167 		convert_8up,
2168 		convert_16up,
2169 		convert_8sr,
2170 		convert_16sr,
2171 		convert_8ur,
2172 		convert_16ur,
2173 		convert_8srp,
2174 		convert_16srp,
2175 		convert_8urp,
2176 		convert_16urp
2177 	};
2178 	unsigned long int tmp_loop;
2179 
2180 	SAMPLE_CONVERT_DEBUG(__FUNCTION__);
2181 	SAMPLE_CONVERT_DEBUG(sample_patch->filename);
2182 	sample_patch->loaded = 1;
2183 	if ((gus_patch = WM_BufferFile(sample_patch->filename,&gus_size)) == NULL) {
2184 		return -1;
2185 	}
2186 	if (gus_size < 239) {
2187 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(too short)", 0);
2188 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, sample_patch->filename, 0);
2189 		free(gus_patch);
2190 		return -1;
2191 	}
2192 	if (memcmp(gus_patch, "GF1PATCH110\0ID#000002", 22) && memcmp(gus_patch, "GF1PATCH100\0ID#000002", 22)) {
2193 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID,"(unsupported format)", 0);
2194 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, sample_patch->filename, 0);
2195 		free(gus_patch);
2196 		return -1;
2197 	}
2198 	if (gus_patch[82] > 1) {
2199 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID,"(unsupported format)", 0);
2200 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, sample_patch->filename, 0);
2201 		free(gus_patch);
2202 		return -1;
2203 	}
2204 	if (gus_patch[151] > 1) {
2205 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID,"(unsupported format)", 0);
2206 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, sample_patch->filename, 0);
2207 		free(gus_patch);
2208 		return -1;
2209 	}
2210 
2211 	no_of_samples = gus_patch[198];
2212 	sample_patch->first_sample = NULL;
2213 	gus_ptr = 239;
2214 	while (no_of_samples) {
2215 		unsigned long int tmp_cnt;
2216 		if (sample_patch->first_sample == NULL) {
2217 			sample_patch->first_sample = malloc(sizeof(struct _sample));
2218 			gus_sample = sample_patch->first_sample;
2219 		} else {
2220 			gus_sample->next = malloc(sizeof(struct _sample));
2221 			gus_sample = gus_sample->next;
2222 		}
2223 		if (gus_sample == NULL) {
2224 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
2225 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, sample_patch->filename, 0);
2226 			return -1;
2227 		}
2228 
2229 		gus_sample->next = NULL;
2230 		gus_sample->loop_fraction = gus_patch[gus_ptr+7];
2231 		gus_sample->data_length = (gus_patch[gus_ptr+11] << 24) | (gus_patch[gus_ptr+10] << 16) | (gus_patch[gus_ptr+9] << 8) | gus_patch[gus_ptr+8];
2232 		gus_sample->loop_start = (gus_patch[gus_ptr+15] << 24) | (gus_patch[gus_ptr+14] << 16) | (gus_patch[gus_ptr+13] << 8) | gus_patch[gus_ptr+12];
2233 		gus_sample->loop_end = (gus_patch[gus_ptr+19] << 24) | (gus_patch[gus_ptr+18] << 16) | (gus_patch[gus_ptr+17] << 8) | gus_patch[gus_ptr+16];
2234 		gus_sample->rate = (gus_patch[gus_ptr+21] << 8) | gus_patch[gus_ptr+20];
2235 		gus_sample->freq_low = ((gus_patch[gus_ptr+25] << 24) | (gus_patch[gus_ptr+24] << 16) | (gus_patch[gus_ptr+23] << 8) | gus_patch[gus_ptr+22]);
2236 		gus_sample->freq_high = ((gus_patch[gus_ptr+29] << 24) | (gus_patch[gus_ptr+28] << 16) | (gus_patch[gus_ptr+27] << 8) | gus_patch[gus_ptr+26]);
2237 		gus_sample->freq_root = ((gus_patch[gus_ptr+33] << 24) | (gus_patch[gus_ptr+32] << 16) | (gus_patch[gus_ptr+31] << 8) | gus_patch[gus_ptr+30]);
2238 
2239 		/* This is done this way instead of ((freq * 1024) / rate) to avoid 32bit overflow. */
2240 		/* Result is 0.001% inacurate */
2241 		gus_sample->inc_div = ((gus_sample->freq_root * 512) / gus_sample->rate) * 2;
2242 
2243 #if 0
2244 		printf("\rTremolo Sweep: %i, Rate: %i, Depth %i\n",
2245 			gus_patch[gus_ptr+49], gus_patch[gus_ptr+50], gus_patch[gus_ptr+51]);
2246 		printf("\rVibrato Sweep: %i, Rate: %i, Depth %i\n",
2247 			gus_patch[gus_ptr+52], gus_patch[gus_ptr+53], gus_patch[gus_ptr+54]);
2248 #endif
2249 		gus_sample->modes = gus_patch[gus_ptr+55] & 0x7F;
2250 		if ((sample_patch->remove & SAMPLE_SUSTAIN) && (gus_sample->modes & SAMPLE_SUSTAIN)) {
2251 			gus_sample->modes ^= SAMPLE_SUSTAIN;
2252 		}
2253 		if (sample_patch->patchid & 0x0080) {
2254 			if (!(sample_patch->keep & SAMPLE_LOOP)) {
2255  				gus_sample->modes &= 0xFB;
2256 			}
2257 			if (!(sample_patch->keep & SAMPLE_ENVELOPE)) {
2258 				gus_sample->modes &= 0xBF;
2259 			}
2260 		}
2261 
2262 
2263 		if (gus_sample->loop_start > gus_sample->loop_end) {
2264 			tmp_loop = gus_sample->loop_end;
2265 			gus_sample->loop_end = gus_sample->loop_start;
2266 			gus_sample->loop_start = tmp_loop;
2267 			gus_sample->loop_fraction  = ((gus_sample->loop_fraction & 0x0f) << 4) | ((gus_sample->loop_fraction & 0xf0) >> 4);
2268 		}
2269 		for (i = 0; i < 6; i++) {
2270 			if (gus_sample->modes & SAMPLE_ENVELOPE) {
2271 				unsigned char env_rate = gus_patch[gus_ptr+37+i];
2272 				if (sample_patch->env[i].set & 0x02) {
2273 					gus_sample->env_target[i] = 16448 * (unsigned long int)(255.0 * sample_patch->env[i].level);
2274 				} else {
2275 					gus_sample->env_target[i] = 16448 * gus_patch[gus_ptr+43+i];
2276 				}
2277 
2278 				if (sample_patch->env[i].set & 0x01) {
2279 					gus_sample->env_rate[i]  = (unsigned long int)(4194303.0 / ((float)WM_SampleRate * (sample_patch->env[i].time / 1000.0)));
2280 				} else {
2281 					gus_sample->env_rate[i]  = (unsigned long int)(4194303.0 / ((float)WM_SampleRate * env_time_table[env_rate]));
2282 					if (gus_sample->env_rate[i] == 0) {
2283 						fprintf(stderr,"\rWarning: libWildMidi %s found invalid envelope(%lu) rate setting in %s. Using %f instead.\n",__FUNCTION__, i, sample_patch->filename, env_time_table[63]);
2284 						gus_sample->env_rate[i]  = (unsigned long int)(4194303.0 / ((float)WM_SampleRate * env_time_table[63]));
2285 					}
2286 				}
2287 			} else {
2288 				gus_sample->env_target[i] = 4194303;
2289 				gus_sample->env_rate[i]  = (unsigned long int)(4194303.0 / ((float)WM_SampleRate * env_time_table[63]));
2290 			}
2291 		}
2292 
2293 		gus_sample->env_target[6] = 0;
2294 		gus_sample->env_rate[6]  = (unsigned long int)(4194303.0 / ((float)WM_SampleRate * env_time_table[63]));
2295 
2296 		if ((sample_patch->patchid == 47) && (!(gus_sample->modes & SAMPLE_LOOP))) {
2297 			for (i = 3; i < 6; i++) {
2298 				gus_sample->env_target[i] = gus_sample->env_target[2];
2299 				gus_sample->env_rate[i] = gus_sample->env_rate[2];
2300 			}
2301 		}
2302 
2303 		gus_ptr += 96;
2304 		tmp_cnt = gus_sample->data_length;
2305 
2306 /* convert to float */
2307 		gus_sample->min_peek = 0;
2308 		gus_sample->max_peek = 0;
2309 
2310 		if (do_convert[(((gus_sample->modes & 0x18) >> 1)| (gus_sample->modes & 0x03))](&gus_patch[gus_ptr], gus_sample) == -1) {
2311 			return -1;
2312 
2313 		};
2314 
2315 		if (gus_sample->max_peek > (-gus_sample->min_peek)) {
2316 			gus_sample->peek_adjust = 33553408 / gus_sample->max_peek;
2317 		} else {
2318 			gus_sample->peek_adjust = 33554432 / (-gus_sample->min_peek);
2319 		}
2320 		gus_sample->peek_adjust = (gus_sample->peek_adjust * sample_patch->amp) >> 10;
2321 
2322 		gus_ptr += tmp_cnt;
2323 		gus_sample->loop_start = (gus_sample->loop_start << 10) | (((gus_sample->loop_fraction & 0x0f) << 10) / 16);
2324 		gus_sample->loop_end = (gus_sample->loop_end << 10) | (((gus_sample->loop_fraction & 0xf0) << 6) / 16);
2325 		gus_sample->loop_size = gus_sample->loop_end - gus_sample->loop_start;
2326 		gus_sample->data_length = gus_sample->data_length << 10;
2327 		no_of_samples--;
2328 	}
2329 	free(gus_patch);
2330 	return 0;
2331 }
2332 
2333 
2334 struct _patch *
2335 get_patch_data(struct _mdi *mdi, unsigned short patchid) {
2336 	struct _patch *search_patch;
2337 
2338 	WM_Lock(&patch_lock);
2339 
2340 	search_patch = patch[patchid & 0x007F];
2341 
2342 	if (search_patch == NULL) {
2343 		WM_Unlock(&patch_lock);
2344 		return NULL;
2345 	}
2346 
2347 	while(search_patch != NULL) {
2348 		if (search_patch->patchid == patchid) {
2349 			WM_Unlock(&patch_lock);
2350 			return search_patch;
2351 		}
2352 		search_patch = search_patch->next;
2353 	}
2354 	if ((patchid >> 8) != 0) {
2355 		WM_Unlock(&patch_lock);
2356 		return (get_patch_data(mdi, patchid & 0x00FF));
2357 	}
2358 	WM_Unlock(&patch_lock);
2359 	return NULL;
2360 }
2361 
2362 void
2363 load_patch (struct _mdi *mdi, unsigned short patchid) {
2364 	int i;
2365 	struct _patch *tmp_patch = NULL;
2366 
2367 	for (i = 0; i < mdi->patch_count; i++) {
2368 		if (mdi->patches[i]->patchid == patchid) {
2369 			return;
2370 		}
2371 	}
2372 
2373 	tmp_patch = get_patch_data(mdi, patchid);
2374 	if (tmp_patch == NULL) {
2375 		return;
2376 	}
2377 
2378 	WM_Lock(&patch_lock);
2379 	if (!tmp_patch->loaded) {
2380 		if (load_sample(tmp_patch) == -1) {
2381 			WM_Unlock(&patch_lock);
2382 			return;
2383 		}
2384 	}
2385 
2386 	if (tmp_patch->first_sample == NULL) {
2387 		WM_Unlock(&patch_lock);
2388 		return;
2389 	}
2390 
2391 	mdi->patch_count++;
2392 	mdi->patches = realloc(mdi->patches, (sizeof(struct _patch) * mdi->patch_count));
2393 	mdi->patches[mdi->patch_count -1] = tmp_patch;
2394 	tmp_patch->inuse_count++;
2395 	WM_Unlock(&patch_lock);
2396 	return;
2397 }
2398 
2399 
2400 struct _sample *
2401 get_sample_data (struct _patch *sample_patch, unsigned long int freq) {
2402 	struct _sample *last_sample = NULL;
2403 	struct _sample *return_sample = NULL;
2404 
2405 	WM_Lock(&patch_lock);
2406 	if (sample_patch == NULL) {
2407 		WM_Unlock(&patch_lock);
2408 		return NULL;
2409 	}
2410 	if (sample_patch->first_sample == NULL) {
2411 		WM_Unlock(&patch_lock);
2412 		return NULL;
2413 	}
2414 	if (freq == 0) {
2415 		WM_Unlock(&patch_lock);
2416 		return sample_patch->first_sample;
2417 	}
2418 
2419 	return_sample = sample_patch->first_sample;
2420 	last_sample = sample_patch->first_sample;
2421 	while (last_sample != NULL) {
2422 		if (freq > last_sample->freq_low) {
2423 			if (freq < last_sample->freq_high) {
2424 				WM_Unlock(&patch_lock);
2425 				return last_sample;
2426 			} else {
2427 				return_sample = last_sample;
2428 			}
2429 		}
2430 		last_sample = last_sample->next;
2431 	}
2432 	WM_Unlock(&patch_lock);
2433 	return return_sample;
2434 }
2435 
2436 unsigned long int
2437 read_var_length (struct _mdi *mdi, struct _miditrack *track) {
2438 	unsigned long int var_data = 0;
2439 	if (mdi->data[track->ptr] > 0x7f) {
2440 		while(mdi->data[track->ptr] > 0x7f) {
2441 			var_data |= mdi->data[track->ptr] & 0x7f;
2442 			track->ptr++;
2443 			if (track->ptr > mdi->size) {
2444 				WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(too short)", 0);
2445 				return 0xFFFFFFFF;
2446 			}
2447 			var_data = (var_data << 7);
2448 		}
2449 	}
2450 	var_data |= mdi->data[track->ptr] & 0x7f;
2451 	track->ptr++;
2452 
2453 	if (track->ptr > mdi->size) {
2454 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(too short)", 0);
2455 		return 0xFFFFFFFF;
2456 	}
2457 
2458 	return var_data;
2459 }
2460 
2461 void
2462 do_note_off (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
2463 	struct _note *nte;
2464 
2465 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
2466 
2467 	nte = &mdi->note_table[0][ch][mdi->data[ptr]];
2468 	if (!nte->active)
2469 		nte = &mdi->note_table[1][ch][mdi->data[ptr]];
2470 	if (!nte->active) {
2471 		return;
2472 	}
2473 
2474 	if ((ch == 9) && (!(nte->modes & SAMPLE_LOOP))) {
2475 		return;
2476 	}
2477 
2478 	if (nte->hold) {
2479 		nte->hold |= HOLD_OFF;
2480 	} else {
2481 #if 0
2482 		if (nte->modes & SAMPLE_SUSTAIN) {
2483 			nte->env = 3;
2484 			if (nte->env_level > nte->sample->env_target[3]) {
2485 				nte->env_inc = -nte->sample->env_rate[3];
2486 			} else {
2487 				nte->env_inc = nte->sample->env_rate[3];
2488 			}
2489 		} else
2490 #endif
2491 		{
2492 			if (nte->env < 4) {
2493 				nte->env = 4;
2494 				if (nte->env_level > nte->sample->env_target[4]) {
2495 					nte->env_inc = -nte->sample->env_rate[4];
2496 				} else {
2497 					nte->env_inc = nte->sample->env_rate[4];
2498 				}
2499 			}
2500 		}
2501 	}
2502 	return;
2503 }
2504 
2505 static inline unsigned long int
2506 get_inc (struct _mdi *mdi, struct _note *nte) {
2507 	int ch = nte->noteid >> 8;
2508 	signed long int note_f;
2509 	unsigned long int freq;
2510 
2511 	if (__builtin_expect((nte->patch->note != 0),0)) {
2512 		note_f = nte->patch->note * 100;
2513 	} else {
2514 		note_f = (nte->noteid & 0x7f) * 100;
2515 	}
2516 	note_f += mdi->channel[ch].pitch_adjust;
2517 	if (__builtin_expect((note_f < 0), 0)) {
2518 		note_f = 0;
2519 	} else if (__builtin_expect((note_f > 12700), 0)) {
2520 		note_f = 12700;
2521 	}
2522 	freq = freq_table[(note_f % 1200)] >> (10 - (note_f / 1200));
2523 	return (((freq / ((WM_SampleRate * 100) / 1024)) * 1024 / nte->sample->inc_div));
2524 }
2525 
2526 static inline signed short int
2527 get_volume(struct _mdi *mdi, unsigned char ch, struct _note *nte) {
2528 	signed long int volume;
2529 
2530  	if (mdi->info.mixer_options & WM_MO_LINEAR_VOLUME) {
2531 		volume = (lin_volume[mdi->channel[ch].volume] *
2532 			lin_volume[mdi->channel[ch].expression] *
2533 			lin_volume[nte->velocity]) / 1048576;
2534 	} else {
2535 		volume = (sqr_volume[mdi->channel[ch].volume] *
2536 			sqr_volume[mdi->channel[ch].expression] *
2537 			sqr_volume[nte->velocity]) / 1048576;
2538 	}
2539 	return ((volume * nte->sample->peek_adjust) >> 10);
2540 }
2541 
2542 void
2543 do_note_on (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
2544 	struct _note *nte;
2545 	unsigned long int freq = 0;
2546 	struct _patch *patch;
2547 	struct _sample *sample;
2548 
2549 	if (mdi->data[ptr+1] == 0x00) {
2550 		do_note_off(ch, mdi, ptr);
2551 		return;
2552 	}
2553 
2554 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
2555 
2556 	if (ch != 9) {
2557 		patch = mdi->channel[ch].patch;
2558 		if (patch == NULL) {
2559 			return;
2560 		}
2561 		freq = freq_table[(mdi->data[ptr] % 12) * 100] >> (10 -(mdi->data[ptr] / 12));
2562 	} else {
2563 		patch = get_patch_data(mdi, ((mdi->channel[ch].bank << 8) | mdi->data[ptr] | 0x80));
2564 		if (patch == NULL) {
2565 			return;
2566 		}
2567 		if (patch->note) {
2568 			freq = freq_table[(patch->note % 12) * 100] >> (10 -(patch->note / 12));
2569 		} else {
2570 			freq = freq_table[(mdi->data[ptr] % 12) * 100] >> (10 -(mdi->data[ptr] / 12));
2571 		}
2572 	}
2573 
2574 	sample = get_sample_data(patch, (freq / 100));
2575 
2576 	if (sample == NULL) {
2577 		return;
2578 	}
2579 
2580 	nte = &mdi->note_table[0][ch][mdi->data[ptr]];
2581 
2582 	if (nte->active) {
2583 		if ((nte->modes & SAMPLE_ENVELOPE) && (nte->env < 3) && (!(nte->hold & HOLD_OFF)))
2584 			return;
2585 		nte->next = &mdi->note_table[1][ch][mdi->data[ptr]];
2586 		nte->env = 6;
2587 		nte->env_inc = -nte->sample->env_rate[6];
2588 		nte = &mdi->note_table[1][ch][mdi->data[ptr]];
2589 	} else {
2590 		if (mdi->note_table[1][ch][mdi->data[ptr]].active) {
2591 			if ((nte->modes & SAMPLE_ENVELOPE) && (nte->env < 3) && (!(nte->hold & HOLD_OFF)))
2592 				return;
2593 			mdi->note_table[1][ch][mdi->data[ptr]].next = nte;
2594 			mdi->note_table[1][ch][mdi->data[ptr]].env = 6;
2595 			mdi->note_table[1][ch][mdi->data[ptr]].env_inc = -mdi->note_table[1][ch][mdi->data[ptr]].sample->env_rate[6];
2596 		} else {
2597 			*mdi->last_note = nte;
2598 			mdi->last_note++;
2599 			nte->active = 1;
2600 		}
2601 	}
2602 	nte->noteid = (ch << 8) | mdi->data[ptr];
2603 	nte->patch = patch;
2604 	nte->sample = sample;
2605 	nte->sample_pos = 0;
2606 	nte->sample_inc = get_inc (mdi, nte);
2607 	nte->velocity = mdi->data[ptr+1];
2608 	nte->env = 0;
2609 	nte->env_inc = nte->sample->env_rate[0];
2610 	nte->env_level = 0;
2611 	nte->modes = sample->modes;
2612 	nte->hold = mdi->channel[ch].hold;
2613 	nte->vol_lvl = get_volume(mdi, ch, nte);
2614 	nte->next = NULL;
2615 }
2616 
2617 void
2618 do_aftertouch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
2619 	struct _note *nte;
2620 
2621 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
2622 
2623 	nte = &mdi->note_table[0][ch][mdi->data[ptr]];
2624 	if (!nte->active) {
2625 		nte = &mdi->note_table[1][ch][mdi->data[ptr]];
2626 		if (!nte->active) {
2627 			return;
2628 		}
2629 	}
2630 
2631 	nte->velocity = mdi->data[ptr+1];
2632 	nte->vol_lvl = get_volume(mdi, ch, nte);
2633 
2634 	if (nte->next) {
2635 		nte->next->velocity = mdi->data[ptr+1];
2636 		nte->next->vol_lvl = get_volume(mdi, ch, nte->next);
2637 	}
2638 }
2639 
2640 
2641 void
2642 do_pan_adjust (struct _mdi *mdi, unsigned char ch) {
2643 	signed short int pan_adjust = mdi->channel[ch].balance + mdi->channel[ch].pan;
2644 	signed long int left, right;
2645 
2646 	if (pan_adjust > 63) {
2647 		pan_adjust = 63;
2648 	} else if (pan_adjust < -64) {
2649 		pan_adjust = -64;
2650 	}
2651 
2652 	pan_adjust += 64;
2653 
2654 	if (mdi->info.mixer_options & WM_MO_LINEAR_VOLUME) {
2655 		left = (lin_volume[127 - pan_adjust] * WM_MasterVolume * mdi->amp) / 1048576;
2656 		right= (lin_volume[pan_adjust] * WM_MasterVolume * mdi->amp) / 1048576;
2657 	} else {
2658 		left =  (pan_volume[127 - pan_adjust] * WM_MasterVolume * mdi->amp) / 1048576;
2659 		right = (pan_volume[pan_adjust] * WM_MasterVolume * mdi->amp) / 1048576;
2660 	}
2661 
2662 	mdi->channel[ch].left_adjust = left;
2663 	mdi->channel[ch].right_adjust = right;
2664 }
2665 
2666 void
2667 do_control (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
2668 	struct _note **note_data = mdi->note;
2669 
2670 	switch (mdi->data[ptr]) {
2671 		case 0: // Bank Select
2672 			mdi->channel[ch].bank = mdi->data[ptr+1];
2673 			break;
2674 		case 1:
2675 		case 2:
2676 		case 3:
2677 		case 4:
2678 		case 5:
2679 			break;
2680 		case 6: // Data Entry Course
2681 			if (mdi->channel[ch].reg_data == 0x0000) { // Pitch Bend Range
2682 				int data_tmp;
2683 				data_tmp = mdi->channel[ch].pitch_range % 100;
2684 				mdi->channel[ch].pitch_range = mdi->data[ptr+1] * 100 + data_tmp;
2685 			}
2686 			break;
2687 		case 7: // Channel Volume
2688 			mdi->channel[ch].volume = mdi->data[ptr+1];
2689 
2690 			if (note_data != mdi->last_note) {
2691 				do {
2692 					if (((*note_data)->noteid >> 8) == ch) {
2693 						(*note_data)->vol_lvl = get_volume(mdi, ch, *note_data);
2694 						if ((*note_data)->next)
2695 							(*note_data)->next->vol_lvl = get_volume(mdi, ch, (*note_data)->next);
2696 					}
2697 					note_data++;
2698 				} while (note_data != mdi->last_note);
2699 			}
2700 			break;
2701 		case 8: // Channel Balance
2702 			mdi->channel[ch].balance = mdi->data[ptr+1] - 64;
2703 			do_pan_adjust(mdi, ch);
2704 			break;
2705 		case 9:
2706 			break;
2707 		case 10: // Channel Pan
2708 			mdi->channel[ch].pan = mdi->data[ptr+1] - 64;
2709 			do_pan_adjust(mdi, ch);
2710 			break;
2711 		case 11: // Channel Expression
2712 			mdi->channel[ch].expression = mdi->data[ptr+1];
2713 
2714 			if (note_data != mdi->last_note) {
2715 				do {
2716 					if (((*note_data)->noteid >> 8) == ch) {
2717 						(*note_data)->vol_lvl = get_volume(mdi, ch, *note_data);
2718 						if ((*note_data)->next)
2719 							(*note_data)->next->vol_lvl = get_volume(mdi, ch, (*note_data)->next);
2720 					}
2721 					note_data++;
2722 				} while (note_data != mdi->last_note);
2723 			}
2724 			break;
2725 		case 12:
2726 		case 13:
2727 		case 14:
2728 		case 15:
2729 		case 16:
2730 		case 17:
2731 		case 18:
2732 		case 19:
2733 		case 20:
2734 		case 21:
2735 		case 22:
2736 		case 23:
2737 		case 24:
2738 		case 25:
2739 		case 26:
2740 		case 27:
2741 		case 28:
2742 		case 29:
2743 		case 30:
2744 		case 31:
2745 		case 32:
2746 		case 33:
2747 		case 34:
2748 		case 35:
2749 		case 36:
2750 		case 37:
2751 			break;
2752 		case 38: // Data Entry Fine
2753 			if (mdi->channel[ch].reg_data == 0x0000) { // Pitch Bend Range
2754 				int data_tmp;
2755 				data_tmp = mdi->channel[ch].pitch_range / 100;
2756 				mdi->channel[ch].pitch_range = (data_tmp * 100) + mdi->data[ptr+1];
2757 			}
2758 			break;
2759 		case 39:
2760 		case 40:
2761 		case 41:
2762 		case 42:
2763 		case 43:
2764 			printf("\rController %i used\n",mdi->data[ptr]);
2765 			break;
2766 		case 44:
2767 		case 45:
2768 		case 46:
2769 		case 47:
2770 		case 48:
2771 		case 49:
2772 		case 50:
2773 		case 51:
2774 		case 52:
2775 		case 53:
2776 		case 54:
2777 		case 55:
2778 		case 56:
2779 		case 57:
2780 		case 58:
2781 		case 59:
2782 		case 60:
2783 		case 61:
2784 		case 62:
2785 		case 63:
2786 			break;
2787 		case 64: // Channel Hold
2788 			if (mdi->data[ptr+1] > 63) {
2789 				mdi->channel[ch].hold = 1;
2790 			} else {
2791 				mdi->channel[ch].hold = 0;
2792 				if (note_data != mdi->last_note) {
2793 					do {
2794 						if (((*note_data)->noteid >> 8) == ch) {
2795 							if ((*note_data)->hold & HOLD_OFF) {
2796 								if ((*note_data)->modes & SAMPLE_ENVELOPE) {
2797 #if 0
2798 									if ((*note_data)->modes & SAMPLE_SUSTAIN) {
2799 										(*note_data)->env = 3;
2800 										if ((*note_data)->env_level > (*note_data)->sample->env_target[3]) {
2801 											(*note_data)->env_inc = -(*note_data)->sample->env_rate[3];
2802 										} else {
2803 											(*note_data)->env_inc = (*note_data)->sample->env_rate[3];
2804 										}
2805 									} else
2806 #endif
2807 									{
2808 										if ((*note_data)->env < 4) {
2809 											(*note_data)->env = 4;
2810 											if ((*note_data)->env_level > (*note_data)->sample->env_target[4]) {
2811 												(*note_data)->env_inc = -(*note_data)->sample->env_rate[4];
2812 											} else {
2813 												(*note_data)->env_inc = (*note_data)->sample->env_rate[4];
2814 											}
2815 										}
2816 									}
2817 								}
2818 							}
2819 							(*note_data)->hold = 0x00;
2820 						}
2821 						note_data++;
2822 					} while (note_data != mdi->last_note);
2823 				}
2824 			}
2825 			break;
2826 		case 65:
2827 		case 66:
2828 		case 67:
2829 		case 68:
2830 		case 69:
2831 		case 70:
2832 		case 71:
2833 		case 72:
2834 		case 73:
2835 		case 74:
2836 		case 75:
2837 		case 76:
2838 		case 77:
2839 		case 78:
2840 		case 79:
2841 		case 80:
2842 		case 81:
2843 		case 82:
2844 		case 83:
2845 		case 84:
2846 		case 85:
2847 		case 86:
2848 		case 87:
2849 		case 88:
2850 		case 89:
2851 		case 90:
2852 		case 91:
2853 		case 92:
2854 		case 93:
2855 		case 94:
2856 		case 95:
2857 		case 96:
2858 		case 97:
2859 		case 98:
2860 		case 99:
2861 			break;
2862 		case 100: // Registered Param Fine
2863 			mdi->channel[ch].reg_data = (mdi->channel[ch].reg_data & 0xFF00) | mdi->data[ptr+1];
2864 			break;
2865 		case 101: // Registered Param Course
2866 			mdi->channel[ch].reg_data = (mdi->channel[ch].reg_data & 0xFF) | (mdi->data[ptr+1] << 8);
2867 			break;
2868 		case 102:
2869 		case 103:
2870 		case 104:
2871 		case 105:
2872 		case 106:
2873 		case 107:
2874 		case 108:
2875 		case 109:
2876 		case 110:
2877 		case 111:
2878 		case 112:
2879 		case 113:
2880 		case 114:
2881 		case 115:
2882 		case 116:
2883 		case 117:
2884 		case 118:
2885 		case 119:
2886 			break;
2887 		case 120: // All Channel Sound Off
2888 			if (note_data != mdi->last_note) {
2889 				do {
2890 					if (((*note_data)->noteid >> 8) == ch) {
2891 						(*note_data)->active = 0;
2892 						if ((*note_data)->next) {
2893 							(*note_data)->next = NULL;
2894 						}
2895 					}
2896 					note_data++;
2897 				} while (note_data != mdi->last_note);
2898 				mdi->last_note = mdi->note;
2899 			}
2900 			break;
2901 		case 121: // All Controlers Off
2902 			mdi->channel[ch].expression = 127;
2903 			mdi->channel[ch].pressure = 0;
2904 			mdi->channel[ch].volume = 100;
2905 			mdi->channel[ch].pan = 0;
2906 			mdi->channel[ch].balance = 0;
2907 			mdi->channel[ch].reg_data = 0xffff;
2908 			mdi->channel[ch].pitch_range = 200;
2909 			mdi->channel[ch].pitch = 0;
2910 			mdi->channel[ch].pitch_adjust = 0;
2911 			mdi->channel[ch].hold = 0;
2912 			do_pan_adjust(mdi, ch);
2913 
2914 			if (note_data != mdi->last_note) {
2915 				do {
2916 					if (((*note_data)->noteid >> 8 ) == ch) {
2917 						(*note_data)->sample_inc = get_inc (mdi, *note_data);
2918 						(*note_data)->velocity = 0;
2919 						(*note_data)->vol_lvl = get_volume(mdi, ch, *note_data);
2920 						(*note_data)->hold = 0;
2921 
2922 						if ((*note_data)->next) {
2923 							(*note_data)->next->velocity = mdi->data[ptr];
2924 							(*note_data)->next->vol_lvl = get_volume(mdi, ch, (*note_data)->next);
2925 						}
2926 
2927 					}
2928 					note_data++;
2929 				} while (note_data != mdi->last_note);
2930 			}
2931 			break;
2932 		case 122:
2933 			break;
2934 		case 123: // All Channel Notes Off
2935 			if (ch == 9)
2936 				return;
2937 			if (note_data != mdi->last_note) {
2938 				do {
2939 					if (((*note_data)->noteid >> 8) == ch) {
2940 						if (!(*note_data)->hold){
2941 							if ((*note_data)->modes & SAMPLE_ENVELOPE) {
2942 								if ((*note_data)->env < 5) {
2943 									if ((*note_data)->env_level > (*note_data)->sample->env_target[5]) {
2944 										(*note_data)->env_inc = -(*note_data)->sample->env_rate[5];
2945 									} else {
2946 										(*note_data)->env_inc = (*note_data)->sample->env_rate[5];
2947 									}
2948 									(*note_data)->env = 5;
2949 								}
2950 							}
2951 						} else {
2952 							(*note_data)->hold |= HOLD_OFF;
2953 						}
2954 					}
2955 					note_data++;
2956 				} while (note_data != mdi->last_note);
2957 			}
2958 			break;
2959 		case 124:
2960 		case 125:
2961 		case 126:
2962 		case 127:
2963 			break;
2964 	}
2965 }
2966 
2967 void
2968 do_patch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
2969 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
2970 	if (ch != 9) {
2971 		mdi->channel[ch].patch = get_patch_data(mdi, ((mdi->channel[ch].bank << 8) | mdi->data[ptr]));
2972 	} else {
2973 		mdi->channel[ch].bank = mdi->data[ptr];
2974 	}
2975 }
2976 
2977 void
2978 do_channel_pressure (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
2979 	struct _note **note_data = mdi->note;
2980 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
2981 
2982 	if (note_data != mdi->last_note) {
2983 		do {
2984 			if (((*note_data)->noteid >> 8 ) == ch) {
2985 				(*note_data)->velocity = mdi->data[ptr];
2986 				(*note_data)->vol_lvl = get_volume(mdi, ch, *note_data);
2987 
2988 				if ((*note_data)->next) {
2989 					(*note_data)->next->velocity = mdi->data[ptr];
2990 					(*note_data)->next->vol_lvl = get_volume(mdi, ch, (*note_data)->next);
2991 				}
2992 			}
2993 			note_data++;
2994 		} while (note_data != mdi->last_note);
2995 	}
2996 }
2997 
2998 void
2999 do_pitch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
3000 	struct _note **note_data = mdi->note;
3001 
3002 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3003 	mdi->channel[ch].pitch = ((mdi->data[ptr+1] << 7) | mdi->data[ptr]) - 0x2000;
3004 
3005 	if (mdi->channel[ch].pitch < 0) {
3006 		mdi->channel[ch].pitch_adjust = mdi->channel[ch].pitch_range * mdi->channel[ch].pitch / 8192;
3007 	} else {
3008 		mdi->channel[ch].pitch_adjust = mdi->channel[ch].pitch_range * mdi->channel[ch].pitch / 8191;
3009 	}
3010 
3011 	if (note_data != mdi->last_note) {
3012 		do {
3013 			if (((*note_data)->noteid >> 8 ) == ch) {
3014 				(*note_data)->sample_inc = get_inc (mdi, *note_data);
3015 			}
3016 			note_data++;
3017 		} while (note_data != mdi->last_note);
3018 	}
3019 }
3020 
3021 void
3022 do_message (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
3023 	unsigned char event_type = 0xF0 | ch;
3024 	static unsigned long int tempo = 500000;
3025 
3026 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3027 	if (event_type == 0xFF) {
3028 		if ((mdi->data[ptr] == 0x51) && (mdi->data[ptr+1] == 3)) {
3029 			tempo = (mdi->data[ptr+2] << 16) | (mdi->data[ptr+3] << 8) | mdi->data[ptr+4];
3030 			if (tempo == 0)
3031 				mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
3032 			else
3033 				mdi->samples_per_delta = (WM_SampleRate << 10) / ((1000000 * mdi->divisions) / tempo);
3034 		}
3035 	}
3036 }
3037 
3038 
3039 void
3040 do_null (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
3041 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3042 };
3043 
3044 
3045 void
3046 WM_ResetToStart(midi * handle) {
3047 	struct _mdi *mdi = (struct _mdi *)handle;
3048 	int i;
3049 
3050 	mdi->index_count = 0;
3051 	mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
3052 	mdi->samples_to_mix = 0;
3053 	mdi->info.current_sample= 0;
3054 
3055 	for (i=0; i<16; i++) {
3056 		mdi->channel[i].bank = 0;
3057 		mdi->channel[i].patch = NULL;
3058 		mdi->channel[i].hold = 0;
3059 		mdi->channel[i].volume = 100;
3060 		mdi->channel[i].pressure = 127;
3061 		mdi->channel[i].expression = 127;
3062 		mdi->channel[i].balance = 0;
3063 		mdi->channel[i].pan = 0;
3064 		mdi->channel[i].left_adjust = 1.0;
3065 		mdi->channel[i].right_adjust = 1.0;
3066 		mdi->channel[i].pitch = 0;
3067 		mdi->channel[i].pitch_range = 200;
3068 		mdi->channel[i].reg_data = 0xFFFF;
3069 	}
3070 }
3071 
3072 void
3073 WM_RecalcSamples (midi * handle) {
3074 	struct _mdi *mdi = (struct _mdi *)handle;
3075 	struct _note **note_data = mdi->note;
3076 	unsigned long int total_samples = 0;
3077 	unsigned long int count_a;
3078 	unsigned long int count_b;
3079 	unsigned long int env_level;
3080 
3081 	if (note_data != mdi->last_note) {
3082 		do {
3083 			env_level = (*note_data)->env_level;
3084 			count_a = 0;
3085 			count_b = 0;
3086 			if ((*note_data)->env < 4) {
3087 				if (env_level > (*note_data)->sample->env_target[3]) {
3088 					count_a += (env_level - (*note_data)->sample->env_target[3] + (*note_data)->sample->env_rate[3] - 1) / (*note_data)->sample->env_rate[3];
3089 				} else {
3090 					count_a += ((*note_data)->sample->env_target[3] - env_level + (*note_data)->sample->env_rate[3] - 1) / (*note_data)->sample->env_rate[3];
3091 				}
3092 				env_level = (*note_data)->sample->env_target[3];
3093 			}
3094 			if ((*note_data)->env < 5) {
3095 				if (env_level > (*note_data)->sample->env_target[4]) {
3096 					count_a += (env_level - (*note_data)->sample->env_target[4] + (*note_data)->sample->env_rate[4] - 1) / (*note_data)->sample->env_rate[4];
3097 				} else {
3098 					count_a += ((*note_data)->sample->env_target[4] - env_level + (*note_data)->sample->env_rate[4] - 1) / (*note_data)->sample->env_rate[4];
3099 				}
3100 				env_level = (*note_data)->sample->env_target[4];
3101 			}
3102 			if ((*note_data)->env < 6) {
3103 				if (env_level > (*note_data)->sample->env_target[5]) {
3104 					count_a += (env_level - (*note_data)->sample->env_target[5] + (*note_data)->sample->env_rate[5] - 1) / (*note_data)->sample->env_rate[5];
3105 				} else {
3106 					count_a += ((*note_data)->sample->env_target[5] - env_level + (*note_data)->sample->env_rate[5] - 1) / (*note_data)->sample->env_rate[5];
3107 				}
3108 				env_level = (*note_data)->sample->env_target[5];
3109 			}
3110 			if ((*note_data)->env == 6) {
3111 				count_a = (env_level + (*note_data)->sample->env_rate[6] - 1) / (*note_data)->sample->env_rate[6];
3112 				env_level = (*note_data)->sample->env_target[6];
3113 			}
3114 			if (env_level != 0) {
3115 				if ((*note_data)->modes & SAMPLE_LOOP) {
3116 					unsigned long int smpl_pos = (*note_data)->sample_pos + (count_a * (*note_data)->sample_inc);
3117 					if (smpl_pos > ((*note_data)->sample->loop_end << 10)) {
3118 						while (smpl_pos > ((*note_data)->sample->loop_end << 10)) {
3119 							smpl_pos -= ((*note_data)->sample->loop_end - (*note_data)->sample->loop_start) << 10;
3120 						}
3121 						count_a += (((*note_data)->sample->data_length << 10) - smpl_pos + (*note_data)->sample_inc - 1) / (*note_data)->sample_inc;
3122 					}
3123 					} else {
3124 					count_b = (((*note_data)->sample->data_length << 10) - (*note_data)->sample_pos + (*note_data)->sample_inc - 1) / (*note_data)->sample_inc;
3125 				}
3126 				if (count_b != 0) {
3127 					if (count_b < count_a) {
3128 						if (total_samples < count_b)
3129 							 total_samples = count_b;
3130 					} else {
3131 						if (total_samples < count_a)
3132 							total_samples = count_a;
3133 					}
3134 				} else {
3135 					if (total_samples < count_a)
3136 							total_samples = count_a;
3137 				}
3138 			} else {
3139 				if (!((*note_data)->modes & SAMPLE_LOOP)) {
3140 					count_b = (((*note_data)->sample->data_length << 10) - (*note_data)->sample_pos) / (*note_data)->sample_inc;
3141 					if (count_b < count_a) {
3142 						if (total_samples < count_b)
3143 							total_samples = count_b;
3144 					} else {
3145 						if (total_samples < count_a)
3146 							total_samples = count_a;
3147 						}
3148 				} else {
3149 					if (total_samples < count_a)
3150 						total_samples = count_a;
3151 				}
3152 			}
3153 		note_data++;
3154 		} while (note_data != mdi->last_note);
3155 	}
3156 	mdi->info.approx_total_samples += total_samples;
3157 	mdi->recalc_samples = 0;
3158 }
3159 
3160 void
3161 do_amp_setup_note_off (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
3162 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3163 
3164 	mdi->lin_cur_vol -=  (lin_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] *
3165 		lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
3166 	mdi->log_cur_vol -= (sqr_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] *
3167 		log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
3168 
3169 	mdi->note_vel[ch][mdi->data[track->ptr]] = 0;
3170 
3171 	track->running_event = 0x80 | ch;
3172 	track->ptr += 2;
3173 	return;
3174 }
3175 
3176 void
3177 do_amp_setup_note_on (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
3178 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3179 	if (mdi->data[track->ptr+1] == 0x00) {
3180 		do_amp_setup_note_off(ch, mdi, track);
3181 		track->running_event = 0x90 | ch;
3182 		return;
3183 	}
3184 
3185 	if (mdi->note_vel[ch][mdi->data[track->ptr]]) {
3186 		mdi->lin_cur_vol -= (lin_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] *
3187 			lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
3188 		mdi->log_cur_vol -= (sqr_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] *
3189 			log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
3190 	}
3191 
3192 	mdi->note_vel[ch][mdi->data[track->ptr]] = mdi->data[track->ptr+1];
3193 
3194 	mdi->lin_cur_vol += (lin_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] *
3195 		lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
3196 	mdi->log_cur_vol += (sqr_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] *
3197 		log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
3198 
3199 	if (mdi->lin_cur_vol > mdi->lin_max_vol) {
3200 		mdi->lin_max_vol = mdi->lin_cur_vol;
3201 	}
3202 	if (mdi->log_cur_vol > mdi->log_max_vol) {
3203 		mdi->log_max_vol = mdi->log_cur_vol;
3204 	}
3205 	if (ch == 9) {
3206 		load_patch(mdi, ((mdi->channel[ch].bank << 8) | (mdi->data[track->ptr] | 0x80)));
3207 	}
3208 
3209 	track->running_event = 0x90 | ch;
3210 	track->ptr += 2;
3211 	return;
3212 }
3213 
3214 void
3215 do_amp_setup_aftertouch (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
3216 	unsigned char pres = mdi->data[track->ptr+1];
3217 
3218 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3219 
3220 	if (pres == 0)
3221 		pres = 1;
3222 
3223 	if (mdi->note_vel[ch][mdi->data[track->ptr]] != 0) {
3224 		mdi->lin_cur_vol -= (lin_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] *
3225 			lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
3226 		mdi->log_cur_vol -= (sqr_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] *
3227 			log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
3228 
3229 		mdi->note_vel[ch][mdi->data[track->ptr]] = pres;
3230 
3231 		mdi->lin_cur_vol += (lin_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] *
3232 			lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
3233 		mdi->log_cur_vol += (sqr_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] *
3234 			log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
3235 
3236 		if (mdi->lin_cur_vol > mdi->lin_max_vol) {
3237 			mdi->lin_max_vol = mdi->lin_cur_vol;
3238 		}
3239 		if (mdi->log_cur_vol > mdi->log_max_vol) {
3240 			mdi->log_max_vol = mdi->log_cur_vol;
3241 		}
3242 	}
3243 	track->running_event = 0xA0 | ch;
3244 	track->ptr += 2;
3245 	return;
3246 }
3247 
3248 
3249 void
3250 do_amp_setup_control (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
3251 	int i;
3252 
3253 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3254 	if (mdi->data[track->ptr] == 0x00) {
3255 		mdi->channel[ch].bank = mdi->data[track->ptr + 1];
3256 	} else if (mdi->data[track->ptr] == 0x07) {
3257 		for (i=0; i < 128; i++) {
3258 			if (mdi->note_vel[ch][i] == 0)
3259 				continue;
3260 			mdi->lin_cur_vol -= (lin_volume[mdi->note_vel[ch][i]] *
3261 				lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
3262 			mdi->log_cur_vol -= (sqr_volume[mdi->note_vel[ch][i]] *
3263 				log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
3264 
3265 			mdi->lin_cur_vol += (lin_volume[mdi->note_vel[ch][i]] *
3266 				lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->data[track->ptr + 1]]) / 1048576;
3267 			mdi->log_cur_vol += (sqr_volume[mdi->note_vel[ch][i]] *
3268 				log_volume[mdi->ch_exp[ch]] * log_volume[mdi->data[track->ptr + 1]]) / 1048576;
3269 
3270 		}
3271 		mdi->ch_vol[ch] = mdi->data[track->ptr + 1];
3272 		if (mdi->lin_cur_vol > mdi->lin_max_vol) {
3273 			mdi->lin_max_vol = mdi->lin_cur_vol;
3274 		}
3275 		if (mdi->log_cur_vol > mdi->log_max_vol) {
3276 			mdi->log_max_vol = mdi->log_cur_vol;
3277 		}
3278 	} else if (mdi->data[track->ptr] == 0x0B) {
3279 		for (i=0; i < 128; i++) {
3280 			if (mdi->note_vel[ch][i] == 0)
3281 				continue;
3282 			mdi->lin_cur_vol -= (lin_volume[mdi->note_vel[ch][i]] *
3283 				lin_volume[mdi->ch_vol[ch]] * lin_volume[mdi->ch_exp[ch]]) / 1048576;
3284 			mdi->log_cur_vol -= (sqr_volume[mdi->note_vel[ch][i]] *
3285 				log_volume[mdi->ch_vol[ch]] * log_volume[mdi->ch_exp[ch]]) / 1048576;
3286 
3287 			mdi->lin_cur_vol += (lin_volume[mdi->note_vel[ch][i]] *
3288 				lin_volume[mdi->ch_vol[ch]] * lin_volume[mdi->data[track->ptr + 1]]) / 1048576;
3289 			mdi->log_cur_vol += (sqr_volume[mdi->note_vel[ch][i]] *
3290 				log_volume[mdi->ch_vol[ch]] * log_volume[mdi->data[track->ptr + 1]]) / 1048576;
3291 
3292 		}
3293 		mdi->ch_exp[ch] = mdi->data[track->ptr + 1];
3294 		if (mdi->lin_cur_vol > mdi->lin_max_vol) {
3295 			mdi->lin_max_vol = mdi->lin_cur_vol;
3296 		}
3297 		if (mdi->log_cur_vol > mdi->log_max_vol) {
3298 			mdi->log_max_vol = mdi->log_cur_vol;
3299 		}
3300 	}
3301 
3302 	track->running_event = 0xB0 | ch;
3303 	track->ptr += 2;
3304 	return;
3305 }
3306 
3307 void
3308 do_amp_setup_patch (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
3309 	if (ch == 9) {
3310 		mdi->channel[ch].bank = mdi->data[track->ptr];
3311 	} else {
3312 		load_patch(mdi, ((mdi->channel[ch].bank << 8) | mdi->data[track->ptr]));
3313 	}
3314 	track->running_event = 0xC0 | ch;
3315 	track->ptr++;
3316 	return;
3317 }
3318 
3319 void
3320 do_amp_setup_channel_pressure (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
3321 	int i;
3322 	unsigned char pres = mdi->data[track->ptr];
3323 
3324 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3325 
3326 	if (pres == 0)
3327 		pres = 1;
3328 
3329 	for (i=0; i < 128; i++) {
3330 		if (mdi->note_vel[ch][i] == 0)
3331 			continue;
3332 		mdi->lin_cur_vol -= (lin_volume[mdi->note_vel[ch][i]] *
3333 			lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
3334 		mdi->log_cur_vol -= (sqr_volume[mdi->note_vel[ch][i]] *
3335 			log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
3336 
3337 		mdi->note_vel[ch][i] = pres;
3338 
3339 		mdi->lin_cur_vol += (lin_volume[mdi->note_vel[ch][i]] *
3340 			lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
3341 		mdi->log_cur_vol += (sqr_volume[mdi->note_vel[ch][i]] *
3342 			log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
3343 
3344 	}
3345 	if (mdi->lin_cur_vol > mdi->lin_max_vol) {
3346 		mdi->lin_max_vol = mdi->lin_cur_vol;
3347 	}
3348 	if (mdi->log_cur_vol > mdi->log_max_vol) {
3349 		mdi->log_max_vol = mdi->log_cur_vol;
3350 	}
3351 	track->running_event = 0xD0 | ch;
3352 	track->ptr++;
3353 	return;
3354 }
3355 
3356 void
3357 do_amp_setup_pitch (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
3358 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3359 	track->running_event = 0xE0 | ch;
3360 	track->ptr += 2;
3361 	return;
3362 }
3363 
3364 void
3365 do_amp_setup_message (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
3366 	unsigned long int event_length;
3367 	unsigned char event_type = 0xF0 | ch;
3368 	unsigned char event_data = mdi->data[track->ptr];
3369 	static unsigned long int tempo = 500000;
3370 
3371 	MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3372 	if (event_type == 0xF0) {
3373 		track->running_event = 0x00;
3374 		do {
3375 			track->ptr++;
3376 		} while ( mdi->data[track->ptr] != 0xF7);
3377 		track->ptr++;
3378 	} else {
3379 		track->ptr += 1;
3380 		event_length = read_var_length(mdi, track);
3381 		if (event_length == 0xFFFFFFFF) {
3382 			track->delta = 0xFFFFFFFF;
3383 			return;
3384 		}
3385 		if (event_type == 0xFF) {
3386 			if ((event_data == 0x2F) && (event_length == 0)) { // Track End
3387 				track->EOT = 1;
3388 				return;
3389 			} else if ((event_data == 0x51) && (event_length == 3)) {  // Tempo Change
3390 				tempo = (mdi->data[track->ptr] << 16) | (mdi->data[track->ptr+1] << 8) | mdi->data[track->ptr+2];
3391 				if (tempo == 0)
3392 					mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
3393 				else
3394 					mdi->samples_per_delta = (WM_SampleRate << 10) / ((1000000 * mdi->divisions) / tempo);
3395 			}
3396 		}
3397 		track->ptr += event_length;
3398 	}
3399 }
3400 
3401 
3402 struct _mdi *
3403 WM_ParseNewMidi(unsigned char *mididata, unsigned long int midisize ) {
3404 	int i;
3405 	unsigned char eot[] = { 0xff, 0x2f, 0x00};
3406 	unsigned long int index_count = 0;
3407 	unsigned long int temp_delta = 0xffffff00;
3408 	struct _mdi *mdi = NULL;
3409 	void (*do_event[])(unsigned char ch, struct _mdi *midifile, struct _miditrack *track) = {
3410 		*do_amp_setup_note_off,
3411 		*do_amp_setup_note_on,
3412 		*do_amp_setup_aftertouch,
3413 		*do_amp_setup_control,
3414 		*do_amp_setup_patch,
3415 		*do_amp_setup_channel_pressure,
3416 		*do_amp_setup_pitch,
3417 		*do_amp_setup_message
3418 	};
3419 	unsigned long int midiofs = 0;
3420 	unsigned long int midi_track_counter = 0;
3421 	unsigned long int last_delta;
3422 	unsigned long int minus_delta = 0;
3423 	unsigned char current_event = 0;
3424 	unsigned short int no_tracks;
3425 	unsigned long int EOT_count = 0;
3426 	struct _miditrack *tmp_trackdata;
3427 	struct _hndl *tmp_handle = NULL;
3428 
3429 	mdi = malloc(sizeof(struct _mdi));
3430 	if (mdi == NULL) {
3431 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM," to parse midi data", errno);
3432 		free (mididata);
3433 		return NULL;
3434 	}
3435 
3436 	// Initialize data
3437 	memset(mdi, 0, sizeof(struct _mdi));
3438 	mdi->lock = 0;
3439 	mdi->data = mididata;
3440 	mdi->size = midisize;
3441 	mdi->info.mixer_options = WM_MixerOptions;
3442 
3443 	mdi->index = malloc(((midisize / 2) + 1)* sizeof(struct _mdi_index));
3444 	if (mdi->index == NULL) {
3445 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM," to parse midi data", errno);
3446 		free(mdi);
3447 		return NULL;
3448 	}
3449 
3450 	load_patch(mdi, 0x0000);
3451 
3452 	for (i=0; i<16; i++) {
3453 		mdi->channel[i].volume = 100;
3454 		mdi->channel[i].pressure = 127;
3455 		mdi->channel[i].expression = 127;
3456 		mdi->channel[i].pitch_range = 200;
3457 		mdi->channel[i].reg_data = 0xFFFF;
3458 		mdi->ch_vol[i] = 100;
3459 		mdi->ch_exp[i] = 127;
3460 		mdi->channel[i].patch = get_patch_data(mdi, 0x0000);
3461 	}
3462 
3463 	midiofs = 0;
3464 	if (strncmp(mididata,"RIFF",4) == 0)
3465 		midiofs = 20;
3466 
3467 	if (strncmp(&mididata[midiofs],"MThd",4) != 0) {
3468 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID,"(not a midi file)", 0);
3469 		free(mdi->index);
3470 		free(mdi);
3471 		return NULL;
3472 	}
3473 
3474 	if ((midiofs + 25) > midisize) {
3475 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT,"(too short)", 0);
3476 		free(mdi->index);
3477 		free(mdi);
3478 		return NULL;
3479 	}
3480 
3481 	midiofs += 9;
3482 	if (mididata[midiofs] > 1) {
3483 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, NULL, 0);
3484 		free(mdi->index);
3485 		free(mdi);
3486 		return NULL;
3487 	}
3488 	midiofs++;
3489 
3490 	no_tracks = mididata[midiofs] << 8 | mididata[midiofs+1];
3491 	midiofs += 2;
3492 
3493 	mdi->divisions = mididata[midiofs] << 8 | mididata[midiofs+1];
3494 	mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
3495 	midiofs += 2;
3496 
3497 	tmp_trackdata = calloc(no_tracks, sizeof(struct _miditrack));
3498 
3499 	if (first_handle == NULL) {
3500 		first_handle = malloc(sizeof(struct _hndl));
3501 		if (first_handle == NULL) {
3502 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM," to parse midi data", errno);
3503 			free (tmp_trackdata);
3504 			free(mdi->data);
3505 			free(mdi);
3506 			return NULL;
3507 		}
3508 		first_handle->handle = (void *)mdi;
3509 		first_handle->prev = NULL;
3510 		first_handle->next = NULL;
3511 	} else {
3512 		tmp_handle = first_handle;
3513 		if (tmp_handle->next != NULL) {
3514 			while (tmp_handle->next != NULL)
3515 				tmp_handle = tmp_handle->next;
3516 		}
3517 		tmp_handle->next = malloc(sizeof(struct _hndl));
3518 		if (tmp_handle->next == NULL) {
3519 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM," to parse midi data", errno);
3520             free(tmp_trackdata);
3521 			free(mdi->data);
3522 			free(mdi);
3523 			return NULL;
3524 		}
3525 		tmp_handle->next->prev = tmp_handle;
3526 		tmp_handle = tmp_handle->next;
3527 		tmp_handle->next = NULL;
3528 		tmp_handle->handle = (void *)mdi;
3529 	}
3530 
3531 
3532 	// grab track offsets;
3533 
3534 	midi_track_counter = 0;
3535 	while (midi_track_counter != no_tracks) {
3536 		if ((midiofs + 12) > midisize) {
3537 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(too short)", 0);
3538 			WildMidi_Close(mdi);
3539 			free(tmp_trackdata);
3540 			return NULL;
3541 		}
3542 		if (strncmp(&mididata[midiofs],"MTrk",4) != 0) {
3543 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(Expected track header)", 0);
3544 			WildMidi_Close(mdi);
3545 			free(tmp_trackdata);
3546 			return NULL;
3547 		}
3548 		midiofs += 4;
3549 		tmp_trackdata[midi_track_counter].length = mididata[midiofs] << 24 | mididata[midiofs+1] << 16 | mididata[midiofs+2] << 8 | mididata[midiofs+3];
3550 		midiofs += 4;
3551 
3552 		if (midisize < (midiofs + tmp_trackdata[midi_track_counter].length)) {
3553 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(too short)", 0);
3554 			WildMidi_Close(mdi);
3555 			free(tmp_trackdata);
3556 			return NULL;
3557 		}
3558 		tmp_trackdata[midi_track_counter].ptr = midiofs;
3559 		tmp_trackdata[midi_track_counter].EOT = 0;
3560 		tmp_trackdata[midi_track_counter].running_event = 0;
3561 		tmp_trackdata[midi_track_counter].delta = read_var_length(mdi, &tmp_trackdata[midi_track_counter]);
3562 		if (tmp_trackdata[midi_track_counter].delta == 0xFFFFFFFF) {
3563 			WildMidi_Close(mdi);
3564 			free(tmp_trackdata);
3565 			return NULL;
3566 		}
3567 		midiofs += tmp_trackdata[midi_track_counter].length;
3568 
3569 		if (memcmp(&mididata[midiofs-3], eot,3) != 0) {
3570 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(Expected EOT)", 0);
3571 			WildMidi_Close(mdi);
3572 			free(tmp_trackdata);
3573 			return NULL;
3574 		}
3575 		if (tmp_trackdata[midi_track_counter].delta < temp_delta) {
3576 			temp_delta = tmp_trackdata[midi_track_counter].delta;
3577 		}
3578 		midi_track_counter++;
3579 	}
3580 
3581 // set midi info
3582 	mdi->index[0].offset = 0;
3583 	mdi->index[0].delta = temp_delta;
3584 
3585 	while (EOT_count != no_tracks) {
3586 		last_delta = 0;
3587 		for (i = 0; i < no_tracks; i++) {
3588 			if (tmp_trackdata[i].EOT) {
3589 				continue;
3590 			}
3591 			if (tmp_trackdata[i].delta) {
3592 				tmp_trackdata[i].delta -= minus_delta;
3593 				if (tmp_trackdata[i].delta) {
3594 					if ((last_delta == 0) || (last_delta > tmp_trackdata[i].delta)) {
3595 						last_delta = tmp_trackdata[i].delta;
3596 					}
3597 					continue;
3598 				}
3599 			}
3600 			do {
3601 				if (mdi->data[tmp_trackdata[i].ptr] < 0x80) {
3602 					current_event = tmp_trackdata[i].running_event;
3603 					if (current_event < 0x80) {
3604 						WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(expected event)", 0);
3605 						WildMidi_Close(mdi);
3606 						free(tmp_trackdata);
3607 						return NULL;
3608 					}
3609 				} else {
3610 					current_event = mdi->data[tmp_trackdata[i].ptr];
3611 					tmp_trackdata[i].ptr++;
3612 				}
3613 
3614 
3615 				index_count++;
3616 				mdi->index[index_count].offset = tmp_trackdata[i].ptr;
3617 				mdi->index[index_count].delta = 0;
3618 				mdi->index[index_count].event = current_event;
3619 
3620 				do_event[((current_event & 0xF0) >> 4) - 8]((current_event & 0x0F), mdi, &tmp_trackdata[i]);
3621 				if (tmp_trackdata[i].delta == 0xFFFFFFFF) {
3622 					WildMidi_Close(mdi);
3623 					free(tmp_trackdata);
3624 					return NULL;
3625 				}
3626 				if (tmp_trackdata[i].EOT) {
3627 					EOT_count++;
3628 					break;
3629 				}
3630 				tmp_trackdata[i].delta = read_var_length(mdi, &tmp_trackdata[i]);
3631 				if (tmp_trackdata[i].delta == 0xFFFFFFFF) {
3632 					WildMidi_Close(mdi);
3633 					free(tmp_trackdata);
3634 					return NULL;
3635 				}
3636 			} while (!(tmp_trackdata[i].delta));
3637 			if ((last_delta == 0) || (last_delta > tmp_trackdata[i].delta)) {
3638 				if (tmp_trackdata[i].delta != 0) {
3639 					last_delta = tmp_trackdata[i].delta;
3640 				}
3641 			}
3642 		}
3643 //		printf("\rLast Delta %lu\n",last_delta);
3644 		mdi->index[index_count].delta = last_delta;
3645 		mdi->samples_to_mix += last_delta * mdi->samples_per_delta;
3646 		mdi->sample_count += mdi->samples_to_mix >> 10;
3647 		mdi->samples_to_mix %= 1024;
3648 		minus_delta = last_delta;
3649 	}
3650 	mdi->sample_count -= (mdi->index[index_count - 1].delta * mdi->samples_per_delta) >> 10;
3651 	mdi->index[index_count - 1].delta = 0;
3652 	mdi->index_size = index_count;
3653 	mdi->index = realloc(mdi->index, (sizeof(struct _mdi_index) * mdi->index_size));
3654 	if (mdi->index == NULL) {
3655 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
3656 		WildMidi_Close(mdi);
3657 		free(tmp_trackdata);
3658 		return NULL;
3659 	}
3660 	mdi->info.approx_total_samples = mdi->sample_count + 1;
3661 	mdi->samples_to_mix = 0;
3662 	mdi->sample_count = 0;
3663 	mdi->info.current_sample = 0;
3664 	mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
3665 	mdi->recalc_samples = 1;
3666 	mdi->last_note = mdi->note;
3667 	if ((mdi->info.mixer_options & WM_MO_LINEAR_VOLUME) || !mdi->log_max_vol) {
3668 		mdi->amp = 281;
3669 	} else {
3670 		mdi->amp = 281 * mdi->lin_max_vol / mdi->log_max_vol;
3671 	}
3672 
3673 	for (i = 0; i < 16; i++) {
3674 		mdi->channel[i].bank = 0;
3675 		do_pan_adjust(mdi, i);
3676 	}
3677 
3678 	for (i = 0; i < 4; i++) {
3679 		mdi->filter.lowpass[i][0].in[0] = 0;
3680 		mdi->filter.lowpass[i][0].in[1] = 0;
3681 		mdi->filter.lowpass[i][1].in[0] = 0;
3682 		mdi->filter.lowpass[i][1].in[1] = 0;
3683 
3684 		mdi->filter.lowpass[i][0].out[0] = 0;
3685 		mdi->filter.lowpass[i][0].out[1] = 0;
3686 		mdi->filter.lowpass[i][1].out[0] = 0;
3687 		mdi->filter.lowpass[i][1].out[1] = 0;
3688 
3689 		mdi->filter.delay_pos[i][0] = 0;
3690 		mdi->filter.delay_pos[i][1] = 0;
3691 
3692 		mdi->filter.delay[i][0] = malloc(delay_size[i][0] * sizeof(signed long int));
3693 		mdi->filter.delay[i][1] = malloc(delay_size[i][1] * sizeof(signed long int));
3694 		memset (mdi->filter.delay[i][0], 0, (delay_size[i][0] * sizeof(signed long int)));
3695 		memset (mdi->filter.delay[i][1], 0, (delay_size[i][1] * sizeof(signed long int)));
3696 
3697 	}
3698 	mdi->filter.in[0][0] = 0;
3699 	mdi->filter.in[0][1] = 0;
3700 	mdi->filter.in[1][0] = 0;
3701 	mdi->filter.in[1][1] = 0;
3702 	mdi->filter.out[0][0] = 0;
3703 	mdi->filter.out[0][1] = 0;
3704 	mdi->filter.out[1][0] = 0;
3705 	mdi->filter.out[1][1] = 0;
3706 
3707 	free(tmp_trackdata);
3708 	return (mdi);
3709 }
3710 
3711 /*
3712  * =========================
3713  * External Functions
3714  * =========================
3715  */
3716 
3717 const char *
3718 WildMidi_GetString (unsigned short int info) {
3719 	switch (info) {
3720 		case WM_GS_VERSION:
3721 			return WM_Version;
3722 	}
3723 	return NULL;
3724 }
3725 
3726 int
3727 WildMidi_Init (const char * config_file, unsigned short int rate, unsigned short int options) {
3728 	if (WM_Initialized) {
3729 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
3730 		return -1;
3731 	}
3732 
3733 	if (config_file == NULL) {
3734 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL config file pointer)", 0);
3735 		return -1;
3736 	}
3737 	WM_InitPatches();
3738 	if (WM_LoadConfig(config_file, NULL) == -1) {
3739 		return -1;
3740 	}
3741 
3742 	if (options & 0xFFD8) {
3743 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(invalid option)", 0);
3744 		WM_FreePatches();
3745 		return -1;
3746 	}
3747 	WM_MixerOptions = options;
3748 
3749 	if ((rate < 11000) || (rate > 65000)) {
3750 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(rate out of bounds, range is 11000 - 65000)", 0);
3751 		WM_FreePatches();
3752 		return -1;
3753 	}
3754 	WM_SampleRate = rate;
3755 	WM_Initialized = 1;
3756 	patch_lock = 0;
3757 
3758 	init_gauss();
3759 	init_lowpass();
3760 	return 0;
3761 }
3762 
3763 int
3764 WildMidi_MasterVolume (unsigned char master_volume) {
3765 	struct _mdi *mdi = NULL;
3766 	struct _hndl * tmp_handle = first_handle;
3767 	int i = 0;
3768 
3769 	if (!WM_Initialized) {
3770 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
3771 		return -1;
3772 	}
3773 	if (master_volume > 127) {
3774 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(master volume out of range, range is 0-127)", 0);
3775 		return -1;
3776 	}
3777 
3778 	WM_MasterVolume = lin_volume[master_volume];
3779 
3780 	if (tmp_handle != NULL) {
3781 		while(tmp_handle != NULL) {
3782 			mdi = (struct _mdi *)tmp_handle->handle;
3783 			for (i = 0; i < 16; i++) {
3784 				do_pan_adjust(mdi, i);
3785 			}
3786 			tmp_handle = tmp_handle->next;
3787 		}
3788 	}
3789 
3790 	return 0;
3791 }
3792 
3793 int
3794 WildMidi_Close (midi * handle) {
3795 	struct _mdi *mdi = (struct _mdi *)handle;
3796 	struct _hndl * tmp_handle;
3797 	struct _sample *tmp_sample;
3798 	int i;
3799 
3800 	if (!WM_Initialized) {
3801 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
3802 		return -1;
3803 	}
3804 	if (handle == NULL) {
3805 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
3806 		return -1;
3807 	}
3808 	if (first_handle == NULL) {
3809 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(no midi's open)", 0);
3810 		return -1;
3811 	}
3812 	WM_Lock(&mdi->lock);
3813 	if (first_handle->handle == handle) {
3814 		tmp_handle = first_handle->next;
3815 		free (first_handle);
3816 		first_handle = tmp_handle;
3817 		if (first_handle != NULL)
3818 			first_handle->prev = NULL;
3819 	} else {
3820 		tmp_handle = first_handle;
3821 		while (tmp_handle->handle != handle) {
3822 			tmp_handle = tmp_handle->next;
3823 			if (tmp_handle == NULL) {
3824 				WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(handle does not exist)", 0);
3825 				return -1;
3826 			}
3827 		}
3828 		tmp_handle->prev->next = tmp_handle->next;
3829 		if (tmp_handle->next != NULL) {
3830 			tmp_handle->next->prev = tmp_handle->prev;
3831 		}
3832 		free (tmp_handle);
3833 	}
3834 
3835 	if (mdi->patch_count != 0) {
3836 		WM_Lock(&patch_lock);
3837 		for (i = 0; i < mdi->patch_count; i++) {
3838 			mdi->patches[i]->inuse_count--;
3839 			if (mdi->patches[i]->inuse_count == 0) {
3840 				//free samples here
3841 				if (mdi->patches[i]->first_sample != NULL) {
3842 					while (mdi->patches[i]->first_sample != NULL) {
3843 						tmp_sample = mdi->patches[i]->first_sample->next;
3844 						if (mdi->patches[i]->first_sample->data)
3845 							free(mdi->patches[i]->first_sample->data);
3846 						free(mdi->patches[i]->first_sample);
3847 						mdi->patches[i]->first_sample = tmp_sample;
3848 					}
3849 					mdi->patches[i]->loaded = 0;
3850 				}
3851 			}
3852 		}
3853 		WM_Unlock(&patch_lock);
3854 		free (mdi->patches);
3855 	}
3856 	if (mdi->data != NULL) {
3857 		free (mdi->data);
3858 	}
3859 	if (mdi->tmp_info != NULL) {
3860 		free (mdi->tmp_info);
3861 	}
3862 	if (mdi->index != NULL)
3863 		free (mdi->index);
3864 
3865 	for (i = 0; i < 4; i++) {
3866 		free (mdi->filter.delay[i][0]);
3867 		free (mdi->filter.delay[i][1]);
3868     }
3869 	free (mdi);
3870 	// no need to unlock cause the struct containing the lock no-longer exists;
3871 	return 0;
3872 }
3873 
3874 midi *
3875 WildMidi_Open (const char *midifile) {
3876 	unsigned char *mididata = NULL;
3877 	unsigned long int midisize = 0;
3878 
3879 	if (!WM_Initialized) {
3880 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
3881 		return NULL;
3882 	}
3883 	if (midifile == NULL) {
3884 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL filename)", 0);
3885 		return NULL;
3886 	}
3887 
3888 	if ((mididata = WM_BufferFile(midifile, &midisize)) == NULL) {
3889 		return NULL;
3890 	}
3891 
3892 	return (void *)WM_ParseNewMidi(mididata,midisize);
3893 }
3894 
3895 midi *
3896 WildMidi_OpenBuffer (unsigned char *midibuffer, unsigned long int size) {
3897 	if (!WM_Initialized) {
3898 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
3899 		return NULL;
3900 	}
3901 	if (midibuffer == NULL) {
3902 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL midi data buffer)", 0);
3903 		return NULL;
3904 	}
3905 
3906 	return (void *)WM_ParseNewMidi(midibuffer,size);
3907 }
3908 
3909 int
3910 WildMidi_LoadSamples( midi * handle) {
3911 	return 0;
3912 }
3913 
3914 int
3915 WildMidi_FastSeek ( midi * handle, unsigned long int *sample_pos) {
3916 	struct _mdi *mdi = (struct _mdi *)handle;
3917 	struct _note **note_data = mdi->note;
3918 	void (*do_event[])(unsigned char ch, struct _mdi *midifile, unsigned long int ptr) = {
3919 		*do_null,
3920 		*do_null,
3921 		*do_aftertouch,
3922 		*do_control,
3923 		*do_patch,
3924 		*do_channel_pressure,
3925 		*do_pitch,
3926 		*do_message
3927 	};
3928 	unsigned long int real_samples_to_mix = 0;
3929 
3930 	if (!WM_Initialized) {
3931 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
3932 		return -1;
3933 	}
3934 	if (handle == NULL) {
3935 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
3936 		return -1;
3937 	}
3938 	WM_Lock(&mdi->lock);
3939 	if (sample_pos == NULL) {
3940 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL seek position pointer)", 0);
3941 		WM_Unlock(&mdi->lock);
3942 		return -1;
3943 	}
3944 
3945 	if (*sample_pos == mdi->info.current_sample) {
3946 		WM_Unlock(&mdi->lock);
3947 		return 0;
3948 	}
3949 
3950 	if (*sample_pos > mdi->info.current_sample) {
3951 		if ((mdi->sample_count == 0) && (mdi->index_count == mdi->index_size) && (mdi->last_note == 0)) {
3952 			*sample_pos = mdi->info.current_sample;
3953 			WM_Unlock(&mdi->lock);
3954 			return 0;
3955 		}
3956 	} else {
3957 		WM_ResetToStart(handle);
3958 	}
3959 
3960 	//reset all notes
3961 	if (note_data != mdi->last_note) {
3962 		do {
3963 			(*note_data)->active = 0;
3964 			*note_data = NULL;
3965 			note_data++;
3966 		} while (note_data != mdi->last_note);
3967 		mdi->last_note = mdi->note;
3968 	}
3969 
3970 	while (*sample_pos != mdi->info.current_sample) {
3971 		if (!mdi->sample_count) {
3972 			if (mdi->index_count != mdi->index_size) {
3973 
3974 				do {
3975 					if (mdi->index_count == mdi->index_size) {
3976 						break;
3977 					}
3978 
3979 					if (mdi->index_count != 0) {
3980 						do_event[((mdi->index[mdi->index_count].event & 0xF0) >> 4) - 8]((mdi->index[mdi->index_count].event & 0x0F), mdi, mdi->index[mdi->index_count].offset);
3981 					}
3982 				} while (mdi->index[mdi->index_count++].delta == 0);
3983 
3984 				mdi->samples_to_mix += mdi->index[mdi->index_count-1].delta * mdi->samples_per_delta;
3985 				mdi->sample_count = mdi->samples_to_mix >> 10;
3986 				mdi->samples_to_mix %= 1024;
3987 			} else {
3988 				mdi->sample_count = WM_SampleRate;
3989 			}
3990 		}
3991 
3992 		if (mdi->sample_count <= (*sample_pos - mdi->info.current_sample)) {
3993 			real_samples_to_mix = mdi->sample_count;
3994 			if (real_samples_to_mix == 0) {
3995 				continue;
3996 			}
3997 		} else {
3998 			real_samples_to_mix = (*sample_pos - mdi->info.current_sample);
3999 		}
4000 
4001 		mdi->info.current_sample += real_samples_to_mix;
4002 		mdi->sample_count -= real_samples_to_mix;
4003 		if ((mdi->index_count == mdi->index_size) && (mdi->last_note == 0)) {
4004 			mdi->sample_count = 0;
4005 			*sample_pos = mdi->info.current_sample;
4006 			WM_Unlock(&mdi->lock);
4007 			return 0;
4008 		}
4009 	}
4010 	WM_Unlock(&mdi->lock);
4011 	return 0;
4012 }
4013 
4014 int
4015 WildMidi_SampledSeek ( midi * handle, unsigned long int *sample_pos) {
4016 	struct _mdi *mdi = (struct _mdi *)handle;
4017 	struct _note **note_data = mdi->note;
4018 	unsigned long int real_samples_to_mix = 0;
4019 	unsigned long int tmp_samples_to_mix = 0;
4020 
4021 	if (!WM_Initialized) {
4022 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
4023 		return -1;
4024 	}
4025 	if (handle == NULL) {
4026 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
4027 		return -1;
4028 	}
4029 	WM_Lock(&mdi->lock);
4030 	if (sample_pos == NULL) {
4031 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL seek position pointer)", 0);
4032 		WM_Unlock(&mdi->lock);
4033 		return -1;
4034 	}
4035 
4036 	if (*sample_pos == mdi->info.current_sample) {
4037 		WM_Unlock(&mdi->lock);
4038 		return 0;
4039 	}
4040 
4041 	if (*sample_pos > mdi->info.current_sample) {
4042 		if ((mdi->sample_count == 0) && (mdi->index_count == mdi->index_size) && (mdi->last_note == 0)) {
4043 			*sample_pos = mdi->info.current_sample;
4044 			WM_Unlock(&mdi->lock);
4045 			return 0;
4046 		}
4047 	} else {
4048 		WM_ResetToStart(handle);
4049 		if (note_data != mdi->last_note) {
4050 			do {
4051 				(*note_data)->active = 0;
4052 				*note_data = NULL;
4053 				note_data++;
4054 			} while (note_data != mdi->last_note);
4055 			mdi->last_note = mdi->note;
4056 		}
4057 	}
4058 
4059 	while (*sample_pos != mdi->info.current_sample) {
4060 		if (!mdi->sample_count) {
4061 			if (mdi->index_count != mdi->index_size) {
4062 
4063 				do {
4064 					if (mdi->index_count == mdi->index_size) {
4065 						break;
4066 					}
4067 
4068 					if (mdi->index_count != 0) {
4069 						do_event[((mdi->index[mdi->index_count].event & 0xF0) >> 4) - 8]((mdi->index[mdi->index_count].event & 0x0F), mdi, mdi->index[mdi->index_count].offset);
4070 					}
4071 				} while (mdi->index[mdi->index_count++].delta == 0);
4072 
4073 				mdi->samples_to_mix += mdi->index[mdi->index_count-1].delta * mdi->samples_per_delta;
4074 				mdi->sample_count = mdi->samples_to_mix >> 10;
4075 				mdi->samples_to_mix %= 1024;
4076 			} else {
4077 				if (mdi->recalc_samples) {
4078 					WM_RecalcSamples(mdi);
4079 				}
4080 				mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
4081 				if (mdi->sample_count == 0) {
4082 					WM_Unlock(&mdi->lock);
4083 					return 0;
4084 				}
4085 			}
4086 		}
4087 
4088 		if (mdi->sample_count <= (*sample_pos - mdi->info.current_sample)) {
4089 			real_samples_to_mix = mdi->sample_count;
4090 			if (real_samples_to_mix == 0) {
4091 				continue;
4092 			}
4093 		} else {
4094 			real_samples_to_mix = (*sample_pos - mdi->info.current_sample);
4095 		}
4096 
4097 		// do mixing here
4098 		tmp_samples_to_mix = real_samples_to_mix;
4099 		do {
4100 
4101 			if (mdi->last_note != mdi->note) {
4102 				note_data = mdi->note;
4103 				while (note_data != mdi->last_note) {
4104 
4105 
4106 /*
4107  * ========================
4108  * sample position checking
4109  * ========================
4110  */
4111 					(*note_data)->sample_pos += (*note_data)->sample_inc;
4112 					if (__builtin_expect(((*note_data)->sample_pos > (*note_data)->sample->loop_end), 0)) {
4113 						if ((*note_data)->modes & SAMPLE_LOOP) {
4114 							(*note_data)->sample_pos = (*note_data)->sample->loop_start + (((*note_data)->sample_pos - (*note_data)->sample->loop_start) % (*note_data)->sample->loop_size);
4115 						} else if (__builtin_expect(((*note_data)->sample_pos >= (*note_data)->sample->data_length), 0)) {
4116 							if (__builtin_expect(((*note_data)->next == NULL), 1)) {
4117 								goto KILL_NOTE;
4118 							}
4119 							goto RESTART_NOTE;
4120 						}
4121 					}
4122 					if (__builtin_expect(((*note_data)->env_inc == 0), 0)) {
4123 						note_data++;
4124 						continue;
4125 					}
4126 					(*note_data)->env_level += (*note_data)->env_inc;
4127 					if (__builtin_expect(((*note_data)->env_level > 4194304), 0)) {
4128 						(*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
4129 					}
4130 					if (__builtin_expect((((*note_data)->env_inc < 0) &&
4131 							((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env])) ||
4132 							(((*note_data)->env_inc > 0) &&
4133 							((*note_data)->env_level < (*note_data)->sample->env_target[(*note_data)->env])), 1)) {
4134 						note_data++;
4135 							continue;
4136 					}
4137 					(*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
4138 					switch ((*note_data)->env) {
4139 						case 0:
4140 							if (!((*note_data)->modes & SAMPLE_ENVELOPE)) {
4141 								(*note_data)->env_inc = 0;
4142 								note_data++;
4143 								continue;
4144 							}
4145 							break;
4146 						case 2:
4147 							if ((*note_data)->modes & SAMPLE_SUSTAIN) {
4148 							(*note_data)->env_inc = 0;
4149 							note_data++;
4150 							continue;
4151 						}
4152 						break;
4153 					case 5:
4154 						if (__builtin_expect(((*note_data)->env_level == 0), 1)) {
4155 							goto KILL_NOTE;
4156 						}
4157 						// sample release
4158 						if ((*note_data)->modes & SAMPLE_LOOP)
4159 							(*note_data)->modes ^= SAMPLE_LOOP;
4160 						(*note_data)->env_inc = 0;
4161 						note_data++;
4162 						continue;
4163 					case 6:
4164 						if (__builtin_expect(((*note_data)->next != NULL), 1)) {
4165 							RESTART_NOTE:
4166 							(*note_data)->active = 0;
4167 							*note_data = (*note_data)->next;
4168 							(*note_data)->active = 1;
4169 							note_data++;
4170 
4171 						} else {
4172 							KILL_NOTE:
4173 							(*note_data)->active = 0;
4174 							mdi->last_note--;
4175 							if (note_data != mdi->last_note) {
4176 								*note_data = *mdi->last_note;
4177 							}
4178 						}
4179 						continue;
4180 					}
4181 					(*note_data)->env++;
4182 					if ((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env]) {
4183 						(*note_data)->env_inc = -(*note_data)->sample->env_rate[(*note_data)->env];
4184 					} else {
4185 					(*note_data)->env_inc = (*note_data)->sample->env_rate[(*note_data)->env];
4186 					}
4187 					note_data++;
4188 					continue;
4189 				}
4190 			} else {
4191 				break;
4192 			}
4193 		} while (--tmp_samples_to_mix);
4194 		mdi->info.current_sample += real_samples_to_mix;
4195 		mdi->sample_count -= real_samples_to_mix;
4196 		if (mdi->index_count == mdi->index_size) {
4197 			if (mdi->last_note == 0) {
4198 				mdi->sample_count = 0;
4199 				*sample_pos = mdi->info.current_sample;
4200 				WM_Unlock(&mdi->lock);
4201 				return 0;
4202 			}
4203 		}
4204 	}
4205 	WM_Unlock(&mdi->lock);
4206 	return 0;
4207 }
4208 
4209 int
4210 WildMidi_GetOutput_Linear (midi * handle, char * buffer, unsigned long int size) {
4211 	unsigned long int buffer_used = 0;
4212 	struct _mdi *mdi = (struct _mdi *)handle;
4213 	unsigned long int real_samples_to_mix = 0;
4214 	unsigned long int data_pos;
4215 	signed long int premix, left_mix, right_mix;
4216 	signed long int vol_mul;
4217 	struct _note **note_data = NULL;
4218 	unsigned long int count;
4219 
4220 	if (__builtin_expect((!WM_Initialized),0)) {
4221 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
4222 		return -1;
4223 	}
4224 	if (__builtin_expect((handle == NULL),0)) {
4225 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
4226 		return -1;
4227 	}
4228 	if (__builtin_expect((buffer == NULL),0)) {
4229 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL buffer pointer)", 0);
4230 		return -1;
4231 	}
4232 
4233 	if (__builtin_expect((size == 0),0)) {
4234 		return 0;
4235 	}
4236 
4237 	if (__builtin_expect((size % 4),0)) {
4238 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(size not a multiple of 4)", 0);
4239 		return -1;
4240 	}
4241 
4242 	WM_Lock(&mdi->lock);
4243 	if (__builtin_expect(((mdi->index_count == mdi->index_size) && (mdi->last_note == 0)), 0)) {
4244 		WM_Unlock(&mdi->lock);
4245 		return 0;
4246 	}
4247 
4248 	buffer_used = 0;
4249 	memset(buffer, 0, size);
4250 
4251 	do {
4252 		if (__builtin_expect((!mdi->sample_count),0)) {
4253 			if (__builtin_expect((mdi->index_count != mdi->index_size),1)) {
4254 				do {
4255 					if (__builtin_expect((mdi->index_count == mdi->index_size), 0)) {
4256 						break;
4257 					}
4258 
4259 					if (__builtin_expect((mdi->index_count != 0), 1)) {
4260 						do_event[((mdi->index[mdi->index_count].event & 0xF0) >> 4) - 8]((mdi->index[mdi->index_count].event & 0x0F), mdi, mdi->index[mdi->index_count].offset);
4261 					}
4262 				} while (mdi->index[mdi->index_count++].delta == 0);
4263 
4264 				mdi->samples_to_mix += mdi->index[mdi->index_count-1].delta * mdi->samples_per_delta;
4265 				mdi->sample_count = mdi->samples_to_mix >> 10;
4266 				mdi->samples_to_mix %= 1024;
4267 			} else {
4268 				if (mdi->recalc_samples) {
4269 					WM_RecalcSamples(mdi);
4270 				}
4271 				mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
4272 				if (mdi->sample_count == 0) {
4273 					WM_Unlock(&mdi->lock);
4274 					return buffer_used;
4275 				}
4276 			}
4277 		}
4278 		if (__builtin_expect((mdi->sample_count > (size >> 2)),1)) {
4279 			real_samples_to_mix = size >> 2;
4280 		} else {
4281 			real_samples_to_mix = mdi->sample_count;
4282 			if (real_samples_to_mix == 0) {
4283 				continue;
4284 			}
4285 		}
4286 
4287 		// do mixing here
4288 		count = real_samples_to_mix;
4289 		do {
4290 			note_data = mdi->note;
4291 			left_mix = right_mix = 0;
4292 			if (__builtin_expect((mdi->last_note != mdi->note),1)) {
4293 				while (note_data != mdi->last_note) {
4294 /*
4295  * ===================
4296  * resample the sample
4297  * ===================
4298  */
4299 					data_pos = (*note_data)->sample_pos >> FPBITS;
4300 					vol_mul = (((*note_data)->vol_lvl * ((*note_data)->env_level >> 12)) >> FPBITS);
4301 
4302 					premix = ((*note_data)->sample->data[data_pos] +
4303 						(((*note_data)->sample->data[data_pos + 1]  - (*note_data)->sample->data[data_pos]) *
4304 						(signed long int)((*note_data)->sample_pos & FPMASK) >> FPBITS)) * vol_mul / 1024;
4305 
4306 					left_mix += premix * mdi->channel[(*note_data)->noteid >> 8].left_adjust;
4307 					right_mix += premix * mdi->channel[(*note_data)->noteid >> 8].right_adjust;
4308 
4309 /*
4310  * ========================
4311  * sample position checking
4312  * ========================
4313  */
4314 					(*note_data)->sample_pos += (*note_data)->sample_inc;
4315 					if (__builtin_expect(((*note_data)->sample_pos > (*note_data)->sample->loop_end), 0)) {
4316 						if ((*note_data)->modes & SAMPLE_LOOP) {
4317 							(*note_data)->sample_pos = (*note_data)->sample->loop_start + (((*note_data)->sample_pos - (*note_data)->sample->loop_start) % (*note_data)->sample->loop_size);
4318 						} else if (__builtin_expect(((*note_data)->sample_pos >= (*note_data)->sample->data_length), 0)) {
4319 							if (__builtin_expect(((*note_data)->next == NULL), 1)) {
4320 								goto KILL_NOTE;
4321 
4322 							}
4323 							goto RESTART_NOTE;
4324 						}
4325 					}
4326 
4327 					if (__builtin_expect(((*note_data)->env_inc == 0), 0)) {
4328 						note_data++;
4329 						continue;
4330 					}
4331 
4332 					(*note_data)->env_level += (*note_data)->env_inc;
4333 					if (__builtin_expect(((*note_data)->env_level > 4194304), 0)) {
4334 						(*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
4335 					}
4336 					if (__builtin_expect((((*note_data)->env_inc < 0) &&
4337 							((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env])) ||
4338 							(((*note_data)->env_inc > 0) &&
4339 							((*note_data)->env_level < (*note_data)->sample->env_target[(*note_data)->env])), 1)) {
4340 						note_data++;
4341 						continue;
4342 					}
4343 
4344 					(*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
4345 					switch ((*note_data)->env) {
4346 						case 0:
4347 							if (!((*note_data)->modes & SAMPLE_ENVELOPE)) {
4348 								(*note_data)->env_inc = 0;
4349 								note_data++;
4350 								continue;
4351 							}
4352 							break;
4353 						case 2:
4354 							if ((*note_data)->modes & SAMPLE_SUSTAIN) {
4355 								(*note_data)->env_inc = 0;
4356 								note_data++;
4357 								continue;
4358 							}
4359 							break;
4360 						case 5:
4361 							if (__builtin_expect(((*note_data)->env_level == 0), 1)) {
4362 								goto KILL_NOTE;
4363 							}
4364 							// sample release
4365 							if ((*note_data)->modes & SAMPLE_LOOP)
4366 								(*note_data)->modes ^= SAMPLE_LOOP;
4367 							(*note_data)->env_inc = 0;
4368 							note_data++;
4369 							continue;
4370 						case 6:
4371 							if (__builtin_expect(((*note_data)->next != NULL), 1)) {
4372 								RESTART_NOTE:
4373 								(*note_data)->active = 0;
4374 								*note_data = (*note_data)->next;
4375 								(*note_data)->active = 1;
4376 								note_data++;
4377 
4378 							} else {
4379 								KILL_NOTE:
4380 								(*note_data)->active = 0;
4381 								mdi->last_note--;
4382 								if (note_data != mdi->last_note) {
4383 									*note_data = *mdi->last_note;
4384 								}
4385 							}
4386 							continue;
4387 					}
4388 					(*note_data)->env++;
4389 					if ((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env]) {
4390 						(*note_data)->env_inc = -(*note_data)->sample->env_rate[(*note_data)->env];
4391 					} else {
4392 						(*note_data)->env_inc = (*note_data)->sample->env_rate[(*note_data)->env];
4393 					}
4394 					note_data++;
4395 					continue;
4396 				}
4397 /*
4398  * =========================
4399  * mix the channels together
4400  * =========================
4401  */
4402 
4403 				left_mix /= 1024;
4404 				right_mix /= 1024;
4405 			}
4406 
4407 #ifdef EXPERIMENT_626
4408 /*
4409  * ==========================
4410  * Experimental Reverb Engine
4411  * ==========================
4412  */
4413 
4414 			if (mdi->info.mixer_options & WM_MO_REVERB) {
4415 				signed long int filteral = mdi->filter.delay[0][0][mdi->filter.delay_pos[0][0]];
4416 				signed long int filterar = mdi->filter.delay[0][1][mdi->filter.delay_pos[0][1]];
4417 				signed long int filterbl = mdi->filter.delay[1][0][mdi->filter.delay_pos[1][0]];
4418 				signed long int filterbr = mdi->filter.delay[1][1][mdi->filter.delay_pos[1][1]];
4419 				signed long int filtercl = mdi->filter.delay[2][0][mdi->filter.delay_pos[2][0]];
4420 				signed long int filtercr = mdi->filter.delay[2][1][mdi->filter.delay_pos[2][1]];
4421 				signed long int filterdl = mdi->filter.delay[3][0][mdi->filter.delay_pos[3][0]];
4422 				signed long int filterdr = mdi->filter.delay[3][1][mdi->filter.delay_pos[3][1]];
4423 				signed long int tfal = (a[0][0] * filteral + a[0][1] * mdi->filter.lowpass[0][0].in[0] + a[0][0] * mdi->filter.lowpass[0][0].in[1] - b[0][0] * mdi->filter.lowpass[0][0].out[0] - b[0][1] * mdi->filter.lowpass[0][0].out[1]) / 1024;
4424 				signed long int tfar = (a[0][0] * filterar + a[0][1] * mdi->filter.lowpass[0][1].in[0] + a[0][0] * mdi->filter.lowpass[0][1].in[1] - b[0][0] * mdi->filter.lowpass[0][1].out[0] - b[0][1] * mdi->filter.lowpass[0][1].out[1]) / 1024;
4425 				signed long int tfbl = (a[1][0] * filterbl + a[1][1] * mdi->filter.lowpass[1][0].in[0] + a[1][0] * mdi->filter.lowpass[1][0].in[1] - b[1][0] * mdi->filter.lowpass[1][0].out[0] - b[1][1] * mdi->filter.lowpass[1][0].out[1]) / 1024;
4426 				signed long int tfbr = (a[1][0] * filterbr + a[1][1] * mdi->filter.lowpass[1][1].in[0] + a[1][0] * mdi->filter.lowpass[1][1].in[1] - b[1][0] * mdi->filter.lowpass[1][1].out[0] - b[1][1] * mdi->filter.lowpass[1][1].out[1]) / 1024;
4427 				signed long int tfcl = (a[2][0] * filtercl + a[2][1] * mdi->filter.lowpass[2][0].in[0] + a[2][0] * mdi->filter.lowpass[2][0].in[1] - b[2][0] * mdi->filter.lowpass[2][0].out[0] - b[2][1] * mdi->filter.lowpass[2][0].out[1]) / 1024;
4428 				signed long int tfcr = (a[2][0] * filtercr + a[2][1] * mdi->filter.lowpass[2][1].in[0] + a[2][0] * mdi->filter.lowpass[2][1].in[1] - b[2][0] * mdi->filter.lowpass[2][1].out[0] - b[2][1] * mdi->filter.lowpass[2][1].out[1]) / 1024;
4429 				signed long int tfdl = (a[3][0] * filterdl + a[3][1] * mdi->filter.lowpass[3][0].in[0] + a[3][0] * mdi->filter.lowpass[3][0].in[1] - b[3][0] * mdi->filter.lowpass[3][0].out[0] - b[3][1] * mdi->filter.lowpass[3][0].out[1]) / 1024;
4430 				signed long int tfdr = (a[3][0] * filterdr + a[3][1] * mdi->filter.lowpass[3][1].in[0] + a[3][0] * mdi->filter.lowpass[3][1].in[1] - b[3][0] * mdi->filter.lowpass[3][1].out[0] - b[3][1] * mdi->filter.lowpass[3][1].out[1]) / 1024;
4431 				signed long int tfl, tflo;
4432 				signed long int tfr, tfro;
4433 
4434 				mdi->filter.lowpass[0][0].in[1] = mdi->filter.lowpass[0][0].in[0];
4435 				mdi->filter.lowpass[0][0].in[0] = filteral;
4436 				mdi->filter.lowpass[0][1].in[1] = mdi->filter.lowpass[0][1].in[0];
4437 				mdi->filter.lowpass[0][1].in[0] = filterar;
4438 				mdi->filter.lowpass[1][0].in[1] = mdi->filter.lowpass[1][0].in[0];
4439 				mdi->filter.lowpass[1][0].in[0] = filterbl;
4440 				mdi->filter.lowpass[1][1].in[1] = mdi->filter.lowpass[1][1].in[0];
4441 				mdi->filter.lowpass[1][1].in[0] = filterbr;
4442 				mdi->filter.lowpass[2][0].in[1] = mdi->filter.lowpass[2][0].in[0];
4443 				mdi->filter.lowpass[2][0].in[0] = filtercl;
4444 				mdi->filter.lowpass[2][1].in[1] = mdi->filter.lowpass[2][1].in[0];
4445 				mdi->filter.lowpass[2][1].in[0] = filtercr;
4446 				mdi->filter.lowpass[3][0].in[1] = mdi->filter.lowpass[3][0].in[0];
4447 				mdi->filter.lowpass[3][0].in[0] = filterdl;
4448 				mdi->filter.lowpass[3][1].in[1] = mdi->filter.lowpass[3][1].in[0];
4449 				mdi->filter.lowpass[3][1].in[0] = filterdr;
4450 
4451 				mdi->filter.lowpass[0][0].out[1] = mdi->filter.lowpass[0][0].out[0];
4452 				mdi->filter.lowpass[0][0].out[0] = tfal;
4453 				mdi->filter.lowpass[0][1].out[1] = mdi->filter.lowpass[0][1].out[0];
4454 				mdi->filter.lowpass[0][1].out[0] = tfar;
4455 				mdi->filter.lowpass[1][0].out[1] = mdi->filter.lowpass[1][0].out[0];
4456 				mdi->filter.lowpass[1][0].out[0] = tfbl;
4457 				mdi->filter.lowpass[1][1].out[1] = mdi->filter.lowpass[1][1].out[0];
4458 				mdi->filter.lowpass[1][1].out[0] = tfbr;
4459 				mdi->filter.lowpass[2][0].out[1] = mdi->filter.lowpass[2][0].out[0];
4460 				mdi->filter.lowpass[2][0].out[0] = tfcl;
4461 				mdi->filter.lowpass[2][1].out[1] = mdi->filter.lowpass[2][1].out[0];
4462 				mdi->filter.lowpass[2][1].out[0] = tfcr;
4463 				mdi->filter.lowpass[3][0].out[1] = mdi->filter.lowpass[3][0].out[0];
4464 				mdi->filter.lowpass[3][0].out[0] = tfdl;
4465 				mdi->filter.lowpass[3][1].out[1] = mdi->filter.lowpass[3][1].out[0];
4466 				mdi->filter.lowpass[3][1].out[0] = tfdr;
4467 
4468 				mdi->filter.delay[0][0][mdi->filter.delay_pos[0][0]] = (tfbr * 405 + tfcr * 368) / 1024 + (left_mix * gain_in[0] / 1024);
4469 				mdi->filter.delay[0][1][mdi->filter.delay_pos[0][1]] = (tfbl * 402 + tfcl * 370) / 1024 + (right_mix * gain_in[0] / 1024);
4470 				mdi->filter.delay[1][0][mdi->filter.delay_pos[1][0]] = (tfar * -545 + tfdr * -364) / 1024 + (left_mix * gain_in[1] / 1024);
4471 				mdi->filter.delay[1][1][mdi->filter.delay_pos[1][1]] = (tfal * -550 + tfdl * -362) / 1024 + (right_mix * gain_in[1] / 1024);
4472 				mdi->filter.delay[2][0][mdi->filter.delay_pos[2][0]] = (tfar * 545 + tfdr * -364) / 1024 + (left_mix * gain_in[2] / 1024);
4473 				mdi->filter.delay[2][1][mdi->filter.delay_pos[2][1]] = (tfal * 550 + tfdl * 362) / 1024 + (right_mix * gain_in[2] / 1024);
4474 				mdi->filter.delay[3][0][mdi->filter.delay_pos[3][0]] = (tfbr * 405 + tfcr * -368) / 1024 + (left_mix * gain_in[3] / 1024);
4475 				mdi->filter.delay[3][1][mdi->filter.delay_pos[3][1]] = (tfbl * 402 + tfcl * -370) / 1024 + (right_mix * gain_in[3] / 1024);
4476 
4477 				tfl = ((tfal * gain_out[0] / 1024) + (tfbl * gain_out[1] / 1024) + (tfcl * gain_out[2] / 1024) + (tfdl * gain_out[3] / 1024));
4478 				tfr = ((tfar * gain_out[0] / 1024) + (tfbr * gain_out[1] / 1024) + (tfcr * gain_out[2] / 1024) + (tfdr * gain_out[3] / 1024));
4479 
4480 				tflo = (a[4][0] * tfl + a[4][1] * mdi->filter.in[0][0] + a[4][0] * mdi->filter.in[1][0] - b[4][0] * mdi->filter.out[0][0] - b[4][1] * mdi->filter.out[1][0]) / 1024;
4481 				tfro = (a[4][0] * tfr + a[4][1] * mdi->filter.in[0][1] + a[4][0] * mdi->filter.in[1][1] - b[4][0] * mdi->filter.out[0][1] - b[4][1] * mdi->filter.out[1][1]) / 1024;
4482 
4483 				mdi->filter.in[1][0] =  mdi->filter.in[0][0];
4484 				mdi->filter.in[0][0] = tfl;
4485 				mdi->filter.in[1][1] =  mdi->filter.in[0][1];
4486 				mdi->filter.in[0][1] = tfr;
4487 				mdi->filter.out[1][0] =  mdi->filter.out[0][0];
4488 				mdi->filter.out[0][0] = tflo;
4489 				mdi->filter.out[1][1] =  mdi->filter.out[0][1];
4490 				mdi->filter.out[0][1] = tfro;
4491 
4492 				left_mix += tflo;
4493 				right_mix += tfro;
4494 
4495 				mdi->filter.delay_pos[0][0]++;
4496 				mdi->filter.delay_pos[0][1]++;
4497 				mdi->filter.delay_pos[1][0]++;
4498 				mdi->filter.delay_pos[1][1]++;
4499 				mdi->filter.delay_pos[2][0]++;
4500 				mdi->filter.delay_pos[2][1]++;
4501 				mdi->filter.delay_pos[3][0]++;
4502 				mdi->filter.delay_pos[3][1]++;
4503 
4504 				if (mdi->filter.delay_pos[0][0] == delay_size[0][0]) mdi->filter.delay_pos[0][0] = 0;
4505 				if (mdi->filter.delay_pos[0][1] == delay_size[0][1]) mdi->filter.delay_pos[0][1] = 0;
4506 				if (mdi->filter.delay_pos[1][0] == delay_size[1][0]) mdi->filter.delay_pos[1][0] = 0;
4507 				if (mdi->filter.delay_pos[1][1] == delay_size[1][1]) mdi->filter.delay_pos[1][1] = 0;
4508 				if (mdi->filter.delay_pos[2][0] == delay_size[2][0]) mdi->filter.delay_pos[2][0] = 0;
4509 				if (mdi->filter.delay_pos[2][1] == delay_size[2][1]) mdi->filter.delay_pos[2][1] = 0;
4510 				if (mdi->filter.delay_pos[3][0] == delay_size[3][0]) mdi->filter.delay_pos[3][0] = 0;
4511 				if (mdi->filter.delay_pos[3][1] == delay_size[3][1]) mdi->filter.delay_pos[3][1] = 0;
4512 
4513 			}
4514 #endif
4515 			if (left_mix > 32767) {
4516 				left_mix = 32767;
4517 			} else if (left_mix < -32768) {
4518 				left_mix = -32768;
4519 			}
4520 
4521 			if (right_mix > 32767) {
4522 				right_mix = 32767;
4523 			} else if (right_mix < -32768) {
4524 				right_mix = -32768;
4525 			}
4526 
4527 
4528 /*
4529  * ===================
4530  * Write to the buffer
4531  * ===================
4532  */
4533 			(*buffer++) = left_mix & 0xff;
4534 			(*buffer++) = (left_mix >> 8) & 0xff;
4535 			(*buffer++) = right_mix & 0xff;
4536 			(*buffer++) = (right_mix >> 8) & 0xff;
4537 		} while (--count);
4538 
4539 		buffer_used += real_samples_to_mix * 4;
4540 		size -= (real_samples_to_mix << 2);
4541 		mdi->info.current_sample += real_samples_to_mix;
4542 		mdi->sample_count -= real_samples_to_mix;
4543 		if (mdi->index_count == mdi->index_size) {
4544 			if (mdi->last_note == 0) {
4545 				mdi->sample_count = 0;
4546 				WM_Unlock(&mdi->lock);
4547 				return buffer_used;
4548 			}
4549 		}
4550 	} while (size);
4551 
4552 	if ((mdi->index_count == mdi->index_size) && (mdi->recalc_samples)) {
4553 		WM_RecalcSamples(mdi);
4554 		mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
4555 	}
4556 	WM_Unlock(&mdi->lock);
4557 	return buffer_used;
4558 }
4559 
4560 int
4561 WildMidi_GetOutput_Gauss (midi * handle, char * buffer, unsigned long int size) {
4562 	unsigned long int buffer_used = 0;
4563 	struct _mdi *mdi = (struct _mdi *)handle;
4564 	unsigned long int real_samples_to_mix = 0;
4565 	unsigned long int data_pos;
4566 	signed long int premix, left_mix, right_mix;
4567 	signed long int vol_mul;
4568 	struct _note **note_data = NULL;
4569 	unsigned long int count;
4570 	signed short int *sptr;
4571 	double y, xd;
4572 	float *gptr, *gend;
4573 	int left, right, temp_n;
4574 	int ii, jj;
4575 
4576 
4577 	if (__builtin_expect((!WM_Initialized),0)) {
4578 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
4579 		return -1;
4580 	}
4581 	if (__builtin_expect((handle == NULL),0)) {
4582 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
4583 		return -1;
4584 	}
4585 	if (__builtin_expect((buffer == NULL),0)) {
4586 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL buffer pointer)", 0);
4587 		return -1;
4588 	}
4589 
4590 	if (__builtin_expect((size == 0),0)) {
4591 		return 0;
4592 	}
4593 
4594 	if (__builtin_expect((size % 4),0)) {
4595 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(size not a multiple of 4)", 0);
4596 		return -1;
4597 	}
4598 
4599 	WM_Lock(&mdi->lock);
4600 	if (__builtin_expect(((mdi->index_count == mdi->index_size) && (mdi->last_note == 0)), 0)) {
4601 		WM_Unlock(&mdi->lock);
4602 		return 0;
4603 	}
4604 
4605 	buffer_used = 0;
4606 	memset(buffer, 0, size);
4607 
4608 	do {
4609 		if (__builtin_expect((!mdi->sample_count),0)) {
4610 			if (__builtin_expect((mdi->index_count != mdi->index_size),1)) {
4611 				do {
4612 					if (__builtin_expect((mdi->index_count == mdi->index_size), 0)) {
4613 						break;
4614 					}
4615 
4616 					if (__builtin_expect((mdi->index_count != 0), 1)) {
4617 						do_event[((mdi->index[mdi->index_count].event & 0xF0) >> 4) - 8]((mdi->index[mdi->index_count].event & 0x0F), mdi, mdi->index[mdi->index_count].offset);
4618 					}
4619 				} while (mdi->index[mdi->index_count++].delta == 0);
4620 
4621 				mdi->samples_to_mix += mdi->index[mdi->index_count-1].delta * mdi->samples_per_delta;
4622 				mdi->sample_count = mdi->samples_to_mix >> 10;
4623 				mdi->samples_to_mix %= 1024;
4624 			} else {
4625 				if (mdi->recalc_samples) {
4626 					WM_RecalcSamples(mdi);
4627 				}
4628 				mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
4629 				if (mdi->sample_count == 0) {
4630 					WM_Unlock(&mdi->lock);
4631 					return buffer_used;
4632 				}
4633 			}
4634 		}
4635 		if (__builtin_expect((mdi->sample_count > (size >> 2)),1)) {
4636 			real_samples_to_mix = size >> 2;
4637 		} else {
4638 			real_samples_to_mix = mdi->sample_count;
4639 			if (real_samples_to_mix == 0) {
4640 				continue;
4641 			}
4642 		}
4643 
4644 		// do mixing here
4645 		count = real_samples_to_mix;
4646 		do {
4647 			note_data = mdi->note;
4648 			left_mix = right_mix = 0;
4649 			if (__builtin_expect((mdi->last_note != mdi->note),1)) {
4650 				while (note_data != mdi->last_note) {
4651 /*
4652  * ===================
4653  * resample the sample
4654  * ===================
4655  */
4656 					data_pos = (*note_data)->sample_pos >> FPBITS;
4657 					vol_mul = (((*note_data)->vol_lvl * ((*note_data)->env_level >> 12)) >> FPBITS);
4658 
4659 					/* check to see if we're near one of the ends */
4660 					left = data_pos;
4661 					right = ((*note_data)->sample->data_length>>FPBITS)- left -1;
4662 					temp_n = (right<<1)-1;
4663 					if (temp_n <= 0)
4664 						temp_n = 1;
4665 					if (temp_n > (left<<1)+1)
4666 						temp_n = (left<<1)+1;
4667 
4668 					/* use Newton if we can't fill the window */
4669 					if (temp_n < gauss_n) {
4670 						xd = (*note_data)->sample_pos & FPMASK;
4671 						xd /= (1L<<FPBITS);
4672 						xd += temp_n>>1;
4673 						y = 0;
4674 						sptr = (*note_data)->sample->data + ((*note_data)->sample_pos>>FPBITS) - (temp_n>>1);
4675 						for (ii = temp_n; ii;) {
4676 							for (jj = 0; jj <= ii; jj++)
4677 								y += sptr[jj] * newt_coeffs[ii][jj];
4678 							y *= xd - --ii;
4679 						}
4680 						y += *sptr;
4681 					} else {			/* otherwise, use Gauss as usual */
4682 						y = 0;
4683 						gptr = gauss_table[(*note_data)->sample_pos & FPMASK];
4684 						gend = gptr + gauss_n;
4685 						sptr = (*note_data)->sample->data + ((*note_data)->sample_pos >> FPBITS) - (gauss_n>>1);
4686 						do {
4687 							y += *(sptr++) * *(gptr++);
4688 						} while (gptr <= gend);
4689 					}
4690 
4691 					premix = y * vol_mul / 1024;
4692 
4693 					left_mix += premix * mdi->channel[(*note_data)->noteid >> 8].left_adjust;
4694 					right_mix += premix * mdi->channel[(*note_data)->noteid >> 8].right_adjust;
4695 
4696 /*
4697  * ========================
4698  * sample position checking
4699  * ========================
4700  */
4701 					(*note_data)->sample_pos += (*note_data)->sample_inc;
4702 					if (__builtin_expect(((*note_data)->sample_pos > (*note_data)->sample->loop_end), 0)) {
4703 						if ((*note_data)->modes & SAMPLE_LOOP) {
4704 							(*note_data)->sample_pos = (*note_data)->sample->loop_start + (((*note_data)->sample_pos - (*note_data)->sample->loop_start) % (*note_data)->sample->loop_size);
4705 						} else if (__builtin_expect(((*note_data)->sample_pos >= (*note_data)->sample->data_length), 0)) {
4706 							if (__builtin_expect(((*note_data)->next == NULL), 1)) {
4707 								goto KILL_NOTE;
4708 
4709 							}
4710 							goto RESTART_NOTE;
4711 						}
4712 					}
4713 
4714 					if (__builtin_expect(((*note_data)->env_inc == 0), 0)) {
4715 						note_data++;
4716 						continue;
4717 					}
4718 
4719 					(*note_data)->env_level += (*note_data)->env_inc;
4720 					if (__builtin_expect(((*note_data)->env_level > 4194304), 0)) {
4721 						(*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
4722 					}
4723 					if (__builtin_expect((((*note_data)->env_inc < 0) &&
4724 							((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env])) ||
4725 							(((*note_data)->env_inc > 0) &&
4726 							((*note_data)->env_level < (*note_data)->sample->env_target[(*note_data)->env])), 1)) {
4727 						note_data++;
4728 						continue;
4729 					}
4730 
4731 					(*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
4732 					switch ((*note_data)->env) {
4733 						case 0:
4734 							if (!((*note_data)->modes & SAMPLE_ENVELOPE)) {
4735 								(*note_data)->env_inc = 0;
4736 								note_data++;
4737 								continue;
4738 							}
4739 							break;
4740 						case 2:
4741 							if ((*note_data)->modes & SAMPLE_SUSTAIN) {
4742 								(*note_data)->env_inc = 0;
4743 								note_data++;
4744 								continue;
4745 							}
4746 							break;
4747 						case 5:
4748 							if (__builtin_expect(((*note_data)->env_level == 0), 1)) {
4749 								goto KILL_NOTE;
4750 							}
4751 							// sample release
4752 							if ((*note_data)->modes & SAMPLE_LOOP)
4753 								(*note_data)->modes ^= SAMPLE_LOOP;
4754 							(*note_data)->env_inc = 0;
4755 							note_data++;
4756 							continue;
4757 						case 6:
4758 							if (__builtin_expect(((*note_data)->next != NULL), 1)) {
4759 								RESTART_NOTE:
4760 								(*note_data)->active = 0;
4761 								*note_data = (*note_data)->next;
4762 								(*note_data)->active = 1;
4763 								note_data++;
4764 
4765 							} else {
4766 								KILL_NOTE:
4767 								(*note_data)->active = 0;
4768 								mdi->last_note--;
4769 								if (note_data != mdi->last_note) {
4770 									*note_data = *mdi->last_note;
4771 								}
4772 							}
4773 							continue;
4774 					}
4775 					(*note_data)->env++;
4776 					if ((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env]) {
4777 						(*note_data)->env_inc = -(*note_data)->sample->env_rate[(*note_data)->env];
4778 					} else {
4779 						(*note_data)->env_inc = (*note_data)->sample->env_rate[(*note_data)->env];
4780 					}
4781 					note_data++;
4782 					continue;
4783 				}
4784 /*
4785  * =========================
4786  * mix the channels together
4787  * =========================
4788  */
4789 
4790 				left_mix /= 1024;
4791 				right_mix /= 1024;
4792 			}
4793 
4794 #ifdef EXPERIMENT_626
4795 /*
4796  * ==========================
4797  * Experimental Reverb Engine
4798  * ==========================
4799  */
4800 
4801 			if (mdi->info.mixer_options & WM_MO_REVERB) {
4802 				signed long int filteral = mdi->filter.delay[0][0][mdi->filter.delay_pos[0][0]];
4803 				signed long int filterar = mdi->filter.delay[0][1][mdi->filter.delay_pos[0][1]];
4804 				signed long int filterbl = mdi->filter.delay[1][0][mdi->filter.delay_pos[1][0]];
4805 				signed long int filterbr = mdi->filter.delay[1][1][mdi->filter.delay_pos[1][1]];
4806 				signed long int filtercl = mdi->filter.delay[2][0][mdi->filter.delay_pos[2][0]];
4807 				signed long int filtercr = mdi->filter.delay[2][1][mdi->filter.delay_pos[2][1]];
4808 				signed long int filterdl = mdi->filter.delay[3][0][mdi->filter.delay_pos[3][0]];
4809 				signed long int filterdr = mdi->filter.delay[3][1][mdi->filter.delay_pos[3][1]];
4810 				signed long int tfal = (a[0][0] * filteral + a[0][1] * mdi->filter.lowpass[0][0].in[0] + a[0][0] * mdi->filter.lowpass[0][0].in[1] - b[0][0] * mdi->filter.lowpass[0][0].out[0] - b[0][1] * mdi->filter.lowpass[0][0].out[1]) / 1024;
4811 				signed long int tfar = (a[0][0] * filterar + a[0][1] * mdi->filter.lowpass[0][1].in[0] + a[0][0] * mdi->filter.lowpass[0][1].in[1] - b[0][0] * mdi->filter.lowpass[0][1].out[0] - b[0][1] * mdi->filter.lowpass[0][1].out[1]) / 1024;
4812 				signed long int tfbl = (a[1][0] * filterbl + a[1][1] * mdi->filter.lowpass[1][0].in[0] + a[1][0] * mdi->filter.lowpass[1][0].in[1] - b[1][0] * mdi->filter.lowpass[1][0].out[0] - b[1][1] * mdi->filter.lowpass[1][0].out[1]) / 1024;
4813 				signed long int tfbr = (a[1][0] * filterbr + a[1][1] * mdi->filter.lowpass[1][1].in[0] + a[1][0] * mdi->filter.lowpass[1][1].in[1] - b[1][0] * mdi->filter.lowpass[1][1].out[0] - b[1][1] * mdi->filter.lowpass[1][1].out[1]) / 1024;
4814 				signed long int tfcl = (a[2][0] * filtercl + a[2][1] * mdi->filter.lowpass[2][0].in[0] + a[2][0] * mdi->filter.lowpass[2][0].in[1] - b[2][0] * mdi->filter.lowpass[2][0].out[0] - b[2][1] * mdi->filter.lowpass[2][0].out[1]) / 1024;
4815 				signed long int tfcr = (a[2][0] * filtercr + a[2][1] * mdi->filter.lowpass[2][1].in[0] + a[2][0] * mdi->filter.lowpass[2][1].in[1] - b[2][0] * mdi->filter.lowpass[2][1].out[0] - b[2][1] * mdi->filter.lowpass[2][1].out[1]) / 1024;
4816 				signed long int tfdl = (a[3][0] * filterdl + a[3][1] * mdi->filter.lowpass[3][0].in[0] + a[3][0] * mdi->filter.lowpass[3][0].in[1] - b[3][0] * mdi->filter.lowpass[3][0].out[0] - b[3][1] * mdi->filter.lowpass[3][0].out[1]) / 1024;
4817 				signed long int tfdr = (a[3][0] * filterdr + a[3][1] * mdi->filter.lowpass[3][1].in[0] + a[3][0] * mdi->filter.lowpass[3][1].in[1] - b[3][0] * mdi->filter.lowpass[3][1].out[0] - b[3][1] * mdi->filter.lowpass[3][1].out[1]) / 1024;
4818 				signed long int tfl, tflo;
4819 				signed long int tfr, tfro;
4820 
4821 				mdi->filter.lowpass[0][0].in[1] = mdi->filter.lowpass[0][0].in[0];
4822 				mdi->filter.lowpass[0][0].in[0] = filteral;
4823 				mdi->filter.lowpass[0][1].in[1] = mdi->filter.lowpass[0][1].in[0];
4824 				mdi->filter.lowpass[0][1].in[0] = filterar;
4825 				mdi->filter.lowpass[1][0].in[1] = mdi->filter.lowpass[1][0].in[0];
4826 				mdi->filter.lowpass[1][0].in[0] = filterbl;
4827 				mdi->filter.lowpass[1][1].in[1] = mdi->filter.lowpass[1][1].in[0];
4828 				mdi->filter.lowpass[1][1].in[0] = filterbr;
4829 				mdi->filter.lowpass[2][0].in[1] = mdi->filter.lowpass[2][0].in[0];
4830 				mdi->filter.lowpass[2][0].in[0] = filtercl;
4831 				mdi->filter.lowpass[2][1].in[1] = mdi->filter.lowpass[2][1].in[0];
4832 				mdi->filter.lowpass[2][1].in[0] = filtercr;
4833 				mdi->filter.lowpass[3][0].in[1] = mdi->filter.lowpass[3][0].in[0];
4834 				mdi->filter.lowpass[3][0].in[0] = filterdl;
4835 				mdi->filter.lowpass[3][1].in[1] = mdi->filter.lowpass[3][1].in[0];
4836 				mdi->filter.lowpass[3][1].in[0] = filterdr;
4837 
4838 				mdi->filter.lowpass[0][0].out[1] = mdi->filter.lowpass[0][0].out[0];
4839 				mdi->filter.lowpass[0][0].out[0] = tfal;
4840 				mdi->filter.lowpass[0][1].out[1] = mdi->filter.lowpass[0][1].out[0];
4841 				mdi->filter.lowpass[0][1].out[0] = tfar;
4842 				mdi->filter.lowpass[1][0].out[1] = mdi->filter.lowpass[1][0].out[0];
4843 				mdi->filter.lowpass[1][0].out[0] = tfbl;
4844 				mdi->filter.lowpass[1][1].out[1] = mdi->filter.lowpass[1][1].out[0];
4845 				mdi->filter.lowpass[1][1].out[0] = tfbr;
4846 				mdi->filter.lowpass[2][0].out[1] = mdi->filter.lowpass[2][0].out[0];
4847 				mdi->filter.lowpass[2][0].out[0] = tfcl;
4848 				mdi->filter.lowpass[2][1].out[1] = mdi->filter.lowpass[2][1].out[0];
4849 				mdi->filter.lowpass[2][1].out[0] = tfcr;
4850 				mdi->filter.lowpass[3][0].out[1] = mdi->filter.lowpass[3][0].out[0];
4851 				mdi->filter.lowpass[3][0].out[0] = tfdl;
4852 				mdi->filter.lowpass[3][1].out[1] = mdi->filter.lowpass[3][1].out[0];
4853 				mdi->filter.lowpass[3][1].out[0] = tfdr;
4854 
4855 				mdi->filter.delay[0][0][mdi->filter.delay_pos[0][0]] = (tfbr * 405 + tfcr * 368) / 1024 + (left_mix * gain_in[0] / 1024);
4856 				mdi->filter.delay[0][1][mdi->filter.delay_pos[0][1]] = (tfbl * 402 + tfcl * 370) / 1024 + (right_mix * gain_in[0] / 1024);
4857 				mdi->filter.delay[1][0][mdi->filter.delay_pos[1][0]] = (tfar * -545 + tfdr * -364) / 1024 + (left_mix * gain_in[1] / 1024);
4858 				mdi->filter.delay[1][1][mdi->filter.delay_pos[1][1]] = (tfal * -550 + tfdl * -362) / 1024 + (right_mix * gain_in[1] / 1024);
4859 				mdi->filter.delay[2][0][mdi->filter.delay_pos[2][0]] = (tfar * 545 + tfdr * -364) / 1024 + (left_mix * gain_in[2] / 1024);
4860 				mdi->filter.delay[2][1][mdi->filter.delay_pos[2][1]] = (tfal * 550 + tfdl * 362) / 1024 + (right_mix * gain_in[2] / 1024);
4861 				mdi->filter.delay[3][0][mdi->filter.delay_pos[3][0]] = (tfbr * 405 + tfcr * -368) / 1024 + (left_mix * gain_in[3] / 1024);
4862 				mdi->filter.delay[3][1][mdi->filter.delay_pos[3][1]] = (tfbl * 402 + tfcl * -370) / 1024 + (right_mix * gain_in[3] / 1024);
4863 
4864 				tfl = ((tfal * gain_out[0] / 1024) + (tfbl * gain_out[1] / 1024) + (tfcl * gain_out[2] / 1024) + (tfdl * gain_out[3] / 1024));
4865 				tfr = ((tfar * gain_out[0] / 1024) + (tfbr * gain_out[1] / 1024) + (tfcr * gain_out[2] / 1024) + (tfdr * gain_out[3] / 1024));
4866 
4867 				tflo = (a[4][0] * tfl + a[4][1] * mdi->filter.in[0][0] + a[4][0] * mdi->filter.in[1][0] - b[4][0] * mdi->filter.out[0][0] - b[4][1] * mdi->filter.out[1][0]) / 1024;
4868 				tfro = (a[4][0] * tfr + a[4][1] * mdi->filter.in[0][1] + a[4][0] * mdi->filter.in[1][1] - b[4][0] * mdi->filter.out[0][1] - b[4][1] * mdi->filter.out[1][1]) / 1024;
4869 
4870 				mdi->filter.in[1][0] =  mdi->filter.in[0][0];
4871 				mdi->filter.in[0][0] = tfl;
4872 				mdi->filter.in[1][1] =  mdi->filter.in[0][1];
4873 				mdi->filter.in[0][1] = tfr;
4874 				mdi->filter.out[1][0] =  mdi->filter.out[0][0];
4875 				mdi->filter.out[0][0] = tflo;
4876 				mdi->filter.out[1][1] =  mdi->filter.out[0][1];
4877 				mdi->filter.out[0][1] = tfro;
4878 
4879 				left_mix += tflo;
4880 				right_mix += tfro;
4881 
4882 				mdi->filter.delay_pos[0][0]++;
4883 				mdi->filter.delay_pos[0][1]++;
4884 				mdi->filter.delay_pos[1][0]++;
4885 				mdi->filter.delay_pos[1][1]++;
4886 				mdi->filter.delay_pos[2][0]++;
4887 				mdi->filter.delay_pos[2][1]++;
4888 				mdi->filter.delay_pos[3][0]++;
4889 				mdi->filter.delay_pos[3][1]++;
4890 
4891 				if (mdi->filter.delay_pos[0][0] == delay_size[0][0]) mdi->filter.delay_pos[0][0] = 0;
4892 				if (mdi->filter.delay_pos[0][1] == delay_size[0][1]) mdi->filter.delay_pos[0][1] = 0;
4893 				if (mdi->filter.delay_pos[1][0] == delay_size[1][0]) mdi->filter.delay_pos[1][0] = 0;
4894 				if (mdi->filter.delay_pos[1][1] == delay_size[1][1]) mdi->filter.delay_pos[1][1] = 0;
4895 				if (mdi->filter.delay_pos[2][0] == delay_size[2][0]) mdi->filter.delay_pos[2][0] = 0;
4896 				if (mdi->filter.delay_pos[2][1] == delay_size[2][1]) mdi->filter.delay_pos[2][1] = 0;
4897 				if (mdi->filter.delay_pos[3][0] == delay_size[3][0]) mdi->filter.delay_pos[3][0] = 0;
4898 				if (mdi->filter.delay_pos[3][1] == delay_size[3][1]) mdi->filter.delay_pos[3][1] = 0;
4899 
4900 			}
4901 #endif
4902 			if (left_mix > 32767) {
4903 				left_mix = 32767;
4904 			} else if (left_mix < -32768) {
4905 				left_mix = -32768;
4906 			}
4907 
4908 			if (right_mix > 32767) {
4909 				right_mix = 32767;
4910 			} else if (right_mix < -32768) {
4911 				right_mix = -32768;
4912 			}
4913 
4914 
4915 /*
4916  * ===================
4917  * Write to the buffer
4918  * ===================
4919  */
4920 			(*buffer++) = left_mix & 0xff;
4921 			(*buffer++) = (left_mix >> 8) & 0xff;
4922 			(*buffer++) = right_mix & 0xff;
4923 			(*buffer++) = (right_mix >> 8) & 0xff;
4924 		} while (--count);
4925 
4926 		buffer_used += real_samples_to_mix * 4;
4927 		size -= (real_samples_to_mix << 2);
4928 		mdi->info.current_sample += real_samples_to_mix;
4929 		mdi->sample_count -= real_samples_to_mix;
4930 		if (mdi->index_count == mdi->index_size) {
4931 			if (mdi->last_note == 0) {
4932 				mdi->sample_count = 0;
4933 				WM_Unlock(&mdi->lock);
4934 				return buffer_used;
4935 			}
4936 		}
4937 	} while (size);
4938 
4939 	if ((mdi->index_count == mdi->index_size) && (mdi->recalc_samples)) {
4940 		WM_RecalcSamples(mdi);
4941 		mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
4942 	}
4943 	WM_Unlock(&mdi->lock);
4944 	return buffer_used;
4945 }
4946 
4947 int
4948 WildMidi_GetOutput (midi * handle, char * buffer, unsigned long int size) {
4949 	struct _mdi *mdi = (struct _mdi *)handle;
4950 
4951 	if (__builtin_expect((!WM_Initialized),0)) {
4952 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
4953 		return -1;
4954 	}
4955 	if (__builtin_expect((handle == NULL),0)) {
4956 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
4957 		return -1;
4958 	}
4959 	if (__builtin_expect((buffer == NULL),0)) {
4960 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL buffer pointer)", 0);
4961 		return -1;
4962 	}
4963 
4964 	if (__builtin_expect((size == 0),0)) {
4965 		return 0;
4966 	}
4967 
4968 	if (__builtin_expect((size % 4),0)) {
4969 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(size not a multiple of 4)", 0);
4970 		return -1;
4971 	}
4972 	if (mdi->info.mixer_options & WM_MO_EXPENSIVE_INTERPOLATION) {
4973 		return WildMidi_GetOutput_Gauss (handle, buffer,size);
4974 	} else {
4975 		return WildMidi_GetOutput_Linear (handle, buffer, size);
4976 	}
4977 }
4978 
4979 int
4980 WildMidi_SetOption (midi * handle, unsigned short int options, unsigned short int setting) {
4981 	struct _mdi *mdi = (struct _mdi *)handle;
4982 	struct _note **note_data = mdi->note;
4983 	int i;
4984 
4985 	if (!WM_Initialized) {
4986 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
4987 		return -1;
4988 	}
4989 	if (handle == NULL) {
4990 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
4991 		return -1;
4992 	}
4993 	WM_Lock(&mdi->lock);
4994 	if ((!(options & 0x0007)) || (options & 0xFFF8)){
4995 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(invalid option)", 0);
4996 		WM_Unlock(&mdi->lock);
4997 		return -1;
4998 	}
4999 	if (setting & 0xFFF8) {
5000 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(invalid setting)", 0);
5001 		WM_Unlock(&mdi->lock);
5002 		return -1;
5003 	}
5004 
5005 	mdi->info.mixer_options = ((mdi->info.mixer_options & (0x00FF ^ options)) | (options & setting));
5006 
5007 	if (options & WM_MO_LINEAR_VOLUME) {
5008 		if (mdi->info.mixer_options & WM_MO_LINEAR_VOLUME) {
5009 			mdi->amp = 281;
5010 		} else {
5011 			mdi->amp = 281 * mdi->lin_max_vol / mdi->log_max_vol;
5012 		}
5013 		for (i = 0; i < 16; i++) {
5014 			do_pan_adjust(mdi, i);
5015 		}
5016 		if (note_data != mdi->last_note) {
5017 			do {
5018 				(*note_data)->vol_lvl = get_volume(mdi, ((*note_data)->noteid >> 8), *note_data);
5019 				if ((*note_data)->next)
5020 					(*note_data)->next->vol_lvl = get_volume(mdi, ((*note_data)->noteid >> 8), (*note_data)->next);
5021 				note_data++;
5022 			} while (note_data != mdi->last_note);
5023 		}
5024 	}
5025 
5026 	if (options & WM_MO_REVERB) {
5027 		for (i = 0; i < 4; i++) {
5028 			mdi->filter.lowpass[i][0].in[0] = 0;
5029 			mdi->filter.lowpass[i][0].in[1] = 0;
5030 			mdi->filter.lowpass[i][1].in[0] = 0;
5031 			mdi->filter.lowpass[i][1].in[1] = 0;
5032 
5033 			mdi->filter.lowpass[i][0].out[0] = 0;
5034 			mdi->filter.lowpass[i][0].out[1] = 0;
5035 			mdi->filter.lowpass[i][1].out[0] = 0;
5036 			mdi->filter.lowpass[i][1].out[1] = 0;
5037 
5038 			mdi->filter.delay_pos[i][0] = 0;
5039 			mdi->filter.delay_pos[i][1] = 0;
5040 
5041 			memset (mdi->filter.delay[i][0], 0, (delay_size[i][0] * sizeof(signed long int)));
5042 			memset (mdi->filter.delay[i][1], 0, (delay_size[i][1] * sizeof(signed long int)));
5043 		}
5044 	}
5045 	WM_Unlock(&mdi->lock);
5046 	return 0;
5047 }
5048 
5049 struct _WM_Info *
5050 WildMidi_GetInfo (midi * handle) {
5051 	struct _mdi *mdi = (struct _mdi *)handle;
5052 	if (!WM_Initialized) {
5053 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
5054 		return NULL;
5055 	}
5056 	if (handle == NULL) {
5057 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
5058 		return NULL;
5059 	}
5060 	WM_Lock(&mdi->lock);
5061 	if (mdi->tmp_info == NULL) {
5062 		mdi->tmp_info = malloc(sizeof(struct _WM_Info));
5063 		if (mdi->tmp_info == NULL) {
5064 			WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to set info", 0);
5065 			WM_Unlock(&mdi->lock);
5066 			return NULL;
5067 		}
5068 	}
5069 	mdi->tmp_info->current_sample = mdi->info.current_sample;
5070 	mdi->tmp_info->approx_total_samples = mdi->info.approx_total_samples;
5071 	mdi->tmp_info->mixer_options = mdi->info.mixer_options;
5072 	WM_Unlock(&mdi->lock);
5073 	return mdi->tmp_info;
5074 }
5075 
5076 int
5077 WildMidi_Shutdown ( void ) {
5078 	struct _hndl * tmp_hdle;
5079 
5080 	if (!WM_Initialized) {
5081 		WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
5082 		return -1;
5083 	}
5084 	if (first_handle != NULL) {
5085 		while (first_handle != NULL) {
5086 			tmp_hdle = first_handle->next;
5087 			WildMidi_Close((struct _mdi *)first_handle->handle);
5088 			free (first_handle);
5089 			first_handle = tmp_hdle;
5090 		}
5091 	}
5092 	WM_FreePatches();
5093 	free_gauss ();
5094 	WM_Initialized = 0;
5095 	return 0;
5096 }
5097 
5098