~ubuntu-branches/ubuntu/maverick/wildmidi/maverick

« back to all changes in this revision

Viewing changes to .pc/07_invalid-free.patch/src/wildmidi_lib.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge
  • Date: 2010-05-11 06:31:11 UTC
  • Revision ID: james.westby@ubuntu.com-20100511063111-2a4vv5u5kg06xcz7
Tags: 0.2.2-3
* debian/libwildmidi0.symbols,
  debian/patches/08_export-symbols-regex.patch,
  debian/patches/99_autoreconf.patch:
  + Add export symbols regex to only export symbols that should be exported.
* debian/patches/07_invalid-free.patch:
  + Patch by Benjamin Otte to fix an invalid free.
    https://bugzilla.gnome.org/show_bug.cgi?id=618247
* debian/source/format,
  debian/rules:
  + Switch to source format 3.0 (quilt).
* debian/rules:
  + Generate correct shlibs file and fail the build if the symbols file
    is wrong.
* debian/control:
  + Update Standards-Version to 3.8.4.
  + Line-wrap control fields.
  + Add ${misc:Depends} to the -dev package.
  + Make shared library package dependency of the -dev package
    more strict to fix a lintian warning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
141
#include <ctype.h>
 
142
#include <errno.h>
 
143
#include <fcntl.h>
 
144
#include <math.h>
 
145
#ifndef _WIN32
 
146
#include <pwd.h>
 
147
#endif
 
148
#include <stdarg.h>
 
149
#include <stdio.h>
 
150
#include <stdlib.h>
 
151
#include <string.h>
 
152
#include <sys/stat.h>
 
153
#include <sys/types.h>
 
154
#include <unistd.h>
 
155
#ifdef _WIN32
 
156
# include <windows.h>
 
157
#endif
 
158
#include "config.h"
 
159
#include "wildmidi_lib.h"
 
160
 
 
161
/*
 
162
 * =========================
 
163
 * Global Data and Data Structs
 
164
 * =========================
 
165
 */
 
166
 
 
167
int WM_Initialized = 0;
 
168
signed short int WM_MasterVolume = 948;
 
169
unsigned short int WM_SampleRate = 0;
 
170
unsigned short int WM_MixerOptions = 0;
 
171
 
 
172
char WM_Version[] = "WildMidi Processing Library " WILDMIDILIB_VERSION;
 
173
 
 
174
struct _lowpass {
 
175
        signed long int in[2];
 
176
        signed long int out[2];
 
177
};
 
178
 
 
179
struct _filter {
 
180
        signed long int *delay[4][2];
 
181
        unsigned long int delay_pos[4][2];
 
182
        struct _lowpass lowpass[4][2];
 
183
        signed long int in[2][2];
 
184
        signed long int out[2][2];
 
185
};
 
186
 
 
187
struct _env {
 
188
        float time;
 
189
        float level;
 
190
        unsigned char set;
 
191
};
 
192
 
 
193
struct _sample {
 
194
        unsigned long int data_length;
 
195
        unsigned long int loop_start;
 
196
        unsigned long int loop_end;
 
197
        unsigned long int loop_size;
 
198
        unsigned char loop_fraction;
 
199
        unsigned short int rate;
 
200
        unsigned long int freq_low;
 
201
        unsigned long int freq_high;
 
202
        unsigned long int freq_root;
 
203
        unsigned char modes;
 
204
        unsigned long int env_rate[7];
 
205
        unsigned long int env_target[7];
 
206
        unsigned long int inc_div;
 
207
        signed short *data;
 
208
        signed short max_peek;
 
209
        signed short min_peek;
 
210
        signed long int peek_adjust;
 
211
        struct _sample *next;
 
212
};
 
213
 
 
214
struct _patch {
 
215
        unsigned short patchid;
 
216
        unsigned char loaded;
 
217
        char *filename;
 
218
        signed short int amp;
 
219
        unsigned char keep;
 
220
        unsigned char remove;
 
221
        struct _env env[6];
 
222
        unsigned char note;
 
223
        unsigned long int inuse_count;
 
224
        struct _sample *first_sample;
 
225
        struct _patch *next;
 
226
};
 
227
 
 
228
struct _patch *patch[128];
 
229
 
 
230
int patch_lock;
 
231
 
 
232
struct _channel {
 
233
        unsigned char bank;
 
234
        struct _patch *patch;
 
235
        unsigned char hold;
 
236
        unsigned char volume;
 
237
        unsigned char pressure;
 
238
        unsigned char expression;
 
239
        signed char balance;
 
240
        signed char pan;
 
241
        signed short int left_adjust;
 
242
        signed short int right_adjust;
 
243
        signed short int pitch;
 
244
        signed short int pitch_range;
 
245
        signed long int pitch_adjust;
 
246
        unsigned short reg_data;
 
247
};
 
248
 
 
249
#define HOLD_OFF 0x02
 
250
 
 
251
struct _note {
 
252
        unsigned short noteid;
 
253
        unsigned char velocity;
 
254
        struct _patch *patch;
 
255
        struct _sample *sample;
 
256
        unsigned long int sample_pos;
 
257
        unsigned long int sample_inc;
 
258
        signed long int env_inc;
 
259
        unsigned char env;
 
260
        unsigned long int env_level;
 
261
        unsigned char modes;
 
262
        unsigned char hold;
 
263
        unsigned char active;
 
264
        struct _note *next;
 
265
        signed short int vol_lvl;
 
266
};
 
267
 
 
268
struct _miditrack {
 
269
        unsigned long int length;
 
270
        unsigned long int ptr;
 
271
        unsigned long int delta;
 
272
        unsigned char running_event;
 
273
        unsigned char EOT;
 
274
};
 
275
 
 
276
struct _mdi_patches {
 
277
        struct _patch *patch;
 
278
        struct _mdi_patch *next;
 
279
};
 
280
 
 
281
struct _mdi_index {
 
282
        unsigned long int offset;
 
283
        unsigned char event;
 
284
        unsigned long int delta;
 
285
};
 
286
 
 
287
struct _mdi {
 
288
        int lock;
 
289
        unsigned char *data;
 
290
        unsigned long int size;
 
291
        unsigned short int divisions ;
 
292
        unsigned short midi_master_vol;
 
293
        unsigned long int samples_per_delta;
 
294
        unsigned long int samples_to_mix;
 
295
        struct _mdi_index * index;
 
296
        unsigned long int index_count;
 
297
        unsigned long int index_size;
 
298
        struct _WM_Info info;
 
299
        struct _WM_Info *tmp_info;
 
300
        unsigned char recalc_samples;
 
301
        struct _channel channel[16];
 
302
        struct _note *note[128];
 
303
        struct _note **last_note;
 
304
        struct _note note_table[2][16][128];
 
305
 
 
306
        struct _patch **patches;
 
307
        unsigned long int patch_count;
 
308
        unsigned long int sample_count;
 
309
        signed short int amp;
 
310
        
 
311
// setup data for auto amp
 
312
        signed long int log_cur_vol;
 
313
        signed long int lin_cur_vol;
 
314
        signed long int log_max_vol;
 
315
        signed long int lin_max_vol;
 
316
        
 
317
        unsigned char ch_vol[16];
 
318
        unsigned char ch_exp[16];
 
319
        unsigned char note_vel[16][128];
 
320
 
 
321
        struct _filter filter;
 
322
};
 
323
 
 
324
/* Gauss Interpolation code adapted from code supplied by Eric. A. Welsh */
 
325
 
 
326
double newt_coeffs[58][58];             /* for start/end of samples */
 
327
float *gauss_table[(1<<10)] = {0};      /* don't need doubles */
 
328
int gauss_window[35] = {0};
 
329
int gauss_n = 34;       /* do not set this value higher than 34 */
 
330
                        /* 34 is as high as we can go before errors crop up */
 
331
 
 
332
void init_gauss (void) {
 
333
        /* init gauss table */
 
334
        int n = 34;
 
335
        int m, i, k, n_half = (n>>1);
 
336
        int j;
 
337
        int sign;
 
338
        double ck;
 
339
        double x, x_inc, xz;
 
340
        double z[35];
 
341
        float *gptr;
 
342
 
 
343
        newt_coeffs[0][0] = 1;
 
344
 
 
345
        for (i = 0; i <= n; i++) {
 
346
                newt_coeffs[i][0] = 1;
 
347
                newt_coeffs[i][i] = 1;
 
348
 
 
349
                if (i > 1) {
 
350
                        newt_coeffs[i][0] = newt_coeffs[i-1][0] / i;
 
351
                        newt_coeffs[i][i] = newt_coeffs[i-1][0] / i;
 
352
                }
 
353
 
 
354
                for (j = 1; j < i; j++) {
 
355
                        newt_coeffs[i][j] = newt_coeffs[i-1][j-1] + newt_coeffs[i-1][j];
 
356
                        if (i > 1)
 
357
                                newt_coeffs[i][j] /= i;
 
358
                }
 
359
                z[i] = i / (4*M_PI);
 
360
        }
 
361
 
 
362
        for (i = 0; i <= n; i++) 
 
363
                for (j = 0, sign = pow(-1, i); j <= i; j++, sign *= -1)
 
364
                        newt_coeffs[i][j] *= sign;
 
365
        
 
366
 
 
367
        x_inc = 1.0 / (1<<10);
 
368
        for (m = 0, x = 0.0; m < (1<<10); m++, x += x_inc) {
 
369
                xz = (x + n_half) / (4*M_PI);
 
370
                gptr = gauss_table[m] = realloc(gauss_table[m], (n+1)*sizeof(float));
 
371
 
 
372
                for (k = 0; k <= n; k++) {
 
373
                        ck = 1.0;
 
374
 
 
375
                        for (i = 0; i <= n; i++) {
 
376
                                if (i == k)
 
377
                                        continue;
 
378
 
 
379
                                ck *= (sin(xz - z[i])) / (sin(z[k] - z[i]));
 
380
                        }
 
381
                        *gptr++ = ck;
 
382
                }
 
383
        }
 
384
}
 
385
 
 
386
unsigned long int delay_size[4][2];
 
387
signed long int a[5][2];
 
388
signed long int b[5][2];
 
389
signed long int gain_in[4];
 
390
signed long int gain_out[4];
 
391
 
 
392
void init_lowpass (void) {
 
393
        float c = 0;
 
394
        int i;
 
395
        float f[] = { 512.0, 1024.0, 2048.0, 4096.0 , 8192.0};
 
396
        float aa, ab, ba, bb;
 
397
        
 
398
        for (i = 0; i < 5; i++) {
 
399
                c = 1.0 / tan(3.141592654 * f[i] / WM_SampleRate);
 
400
                aa = 1.0 / (1.0 + 1.4 * c + c * c);
 
401
                ab = 2.0 * aa;
 
402
                ba = 2.0 * (1.0 - c * c) * aa;
 
403
                bb = (1.0 - 1.4 * c + c * c) * aa;
 
404
                a[i][0] = (signed long int)(aa * 1024.0);
 
405
                a[i][1] = (signed long int)(ab * 1024.0);
 
406
                b[i][0] = (signed long int)(ba * 1024.0);
 
407
                b[i][1] = (signed long int)(bb * 1024.0);
 
408
        }
 
409
        gain_in[0] = 772;
 
410
        gain_out[0] = 772;
 
411
        gain_in[1] = 570;
 
412
        gain_out[1] = 570;
 
413
        gain_in[2] = 520;
 
414
        gain_out[2] = 520;
 
415
        gain_in[3] = 512;
 
416
        gain_out[3] = 512;
 
417
 
 
418
        delay_size[0][0] = 2191 * WM_SampleRate / 44100;
 
419
        delay_size[0][1] = (2191 + 19) * WM_SampleRate / 44100;
 
420
        delay_size[1][0] = (2971 + 19) * WM_SampleRate / 44100;
 
421
        delay_size[1][1] = 2971 * WM_SampleRate / 44100;
 
422
        delay_size[2][0] = 3253 * WM_SampleRate / 44100;
 
423
        delay_size[2][1] = (3253 + 19) * WM_SampleRate / 44100;
 
424
        delay_size[3][0] = (3307 + 19) * WM_SampleRate / 44100;
 
425
        delay_size[3][1] = 3307 * WM_SampleRate / 44100;
 
426
 
 
427
}
 
428
 
 
429
struct _hndl {
 
430
        void * handle;
 
431
        struct _hndl *next;
 
432
        struct _hndl *prev;
 
433
};
 
434
 
 
435
struct _hndl * first_handle = NULL;
 
436
 
 
437
//f: ( VOLUME / 127 )
 
438
//f: pow(( VOLUME / 127 ), 1.660964047 )
 
439
//f: pow(( VOLUME / 127 ), 2.0 )
 
440
//f: pow(( VOLUME / 127 ), 0.602059991 )
 
441
//f: pow(( VOLUME / 127 ), 0.5 )
 
442
 
 
443
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 };
 
444
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 };
 
445
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 };
 
446
//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 };
 
447
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 };
 
448
 
 
449
unsigned long int reverb_val = 92;
 
450
unsigned long int comb_size[8][2];
 
451
unsigned long int allpass_size[2][2];
 
452
 
 
453
float env_time_table[] = {
 
454
        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,
 
455
        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,
 
456
        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,
 
457
        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
 
458
};
 
459
 
 
460
#if 0
 
461
unsigned long int freq_table[] = {
 
462
        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
 
463
};
 
464
#endif
 
465
 
 
466
unsigned long int freq_table[] = {
 
467
        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, 
 
468
        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, 
 
469
        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, 
 
470
        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, 
 
471
        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, 
 
472
        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, 
 
473
        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, 
 
474
        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, 
 
475
        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, 
 
476
        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, 
 
477
        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, 
 
478
        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, 
 
479
};
 
480
 
 
481
#define SAMPLE_16BIT 0x01
 
482
#define SAMPLE_UNSIGNED 0x02
 
483
#define SAMPLE_LOOP 0x04
 
484
#define SAMPLE_PINGPONG 0x08
 
485
#define SAMPLE_REVERSE 0x10
 
486
#define SAMPLE_SUSTAIN 0x20
 
487
#define SAMPLE_ENVELOPE 0x40
 
488
 
 
489
#ifdef DEBUG_SAMPLES
 
490
#define SAMPLE_CONVERT_DEBUG(dx) printf("\r%s\n",dx)
 
491
#else
 
492
#define SAMPLE_CONVERT_DEBUG(dx)
 
493
#endif
 
494
 
 
495
#ifdef DEBUG_MIDI
 
496
#define MIDI_EVENT_DEBUG(dx,dy) printf("\r%s, %x\n",dx,dy) 
 
497
#else
 
498
#define MIDI_EVENT_DEBUG(dx,dy)
 
499
#endif
 
500
 
 
501
#define WM_ERR_MEM              0
 
502
#define WM_ERR_STAT             1
 
503
#define WM_ERR_LOAD             2
 
504
#define WM_ERR_OPEN             3
 
505
#define WM_ERR_READ             4
 
506
#define WM_ERR_INVALID          5
 
507
#define WM_ERR_CORUPT           6
 
508
#define WM_ERR_NOT_INIT         7
 
509
#define WM_ERR_INVALID_ARG      8
 
510
 
 
511
#define MAX_AUTO_AMP 2.0
 
512
 
 
513
#define FPBITS 10
 
514
#define FPMASK ((1L<<FPBITS)-1L)
 
515
 
 
516
 
 
517
void do_note_off (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
 
518
void do_note_on (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
 
519
void do_aftertouch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
 
520
void do_control (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
 
521
void do_patch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
 
522
void do_channel_pressure (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
 
523
void do_pitch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
 
524
void do_message (unsigned char ch, struct _mdi *mdi, unsigned long int ptr);
 
525
 
 
526
void (*do_event[])(unsigned char ch, struct _mdi *midifile, unsigned long int ptr) = {
 
527
        *do_note_off,
 
528
        *do_note_on,
 
529
        *do_aftertouch,
 
530
        *do_control,
 
531
        *do_patch,
 
532
        *do_channel_pressure,
 
533
        *do_pitch,
 
534
        *do_message
 
535
};
 
536
 
 
537
/*
 
538
 * =========================
 
539
 * Internal Functions
 
540
 * =========================
 
541
 */
 
542
 
 
543
 
 
544
void
 
545
WM_ERROR( const char * func, unsigned long int lne, int wmerno, const char * wmfor, int error) {
 
546
        const char * errors[] = {
 
547
                "Unable to obtain memory\0",
 
548
                "Unable to stat\0",
 
549
                "Unable to load\0",
 
550
                "Unable to open\0",
 
551
                "Unable to read\0",
 
552
                "Invalid or Unsuported file format\0",
 
553
                "File corrupt\0",
 
554
                "Library not Initialized\0",
 
555
                "Invalid argument\0"
 
556
        };
 
557
        if (wmfor != NULL) {
 
558
                if (error != 0) {
 
559
                        fprintf(stderr,"\rlibWildMidi(%s:%lu): ERROR %s %s (%s)\n",func, lne, errors[wmerno], wmfor, strerror(error));
 
560
                } else {
 
561
                        fprintf(stderr,"\rlibWildMidi(%s:%lu): ERROR %s %s\n",func, lne, errors[wmerno], wmfor);
 
562
                }
 
563
        } else {
 
564
                if (error != 0) {
 
565
                        fprintf(stderr,"\rlibWildMidi(%s:%lu): ERROR %s (%s)\n",func, lne, errors[wmerno], strerror(error));
 
566
                } else {
 
567
                        fprintf(stderr,"\rlibWildMidi(%s:%lu): ERROR %s\n",func, lne, errors[wmerno]);
 
568
                }
 
569
        }
 
570
 
 
571
}
 
572
 
 
573
unsigned char *
 
574
WM_BufferFile (const char *filename, unsigned long int *size) {
 
575
        int buffer_fd;
 
576
        char *data;
 
577
        struct stat buffer_stat;
 
578
#ifndef _WIN32
 
579
        char *home = NULL;
 
580
        struct passwd *pwd_ent;
 
581
        char buffer_dir[1024];
 
582
#endif
 
583
        
 
584
        char *buffer_file = malloc(strlen(filename) + 1);
 
585
 
 
586
        if (buffer_file == NULL) {
 
587
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
 
588
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, filename, errno);
 
589
                return NULL;
 
590
        }
 
591
        
 
592
        strcpy (buffer_file, filename);
 
593
#ifndef _WIN32
 
594
        if (strncmp(buffer_file,"~/",2) == 0) {
 
595
                if ((pwd_ent = getpwuid (getuid ()))) {
 
596
                        home = pwd_ent->pw_dir;
 
597
                } else {
 
598
                        home = getenv ("HOME");
 
599
                }
 
600
                if (home) {
 
601
                        buffer_file = realloc(buffer_file,(strlen(buffer_file) + strlen(home) + 1));
 
602
                        if (buffer_file == NULL) {
 
603
                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
 
604
                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, filename, errno);
 
605
                                free(buffer_file);
 
606
                                return NULL;
 
607
                        }
 
608
                        memmove((buffer_file + strlen(home)), (buffer_file + 1), (strlen(buffer_file)));
 
609
                        strncpy (buffer_file, home,strlen(home));
 
610
                }
 
611
        } else if (buffer_file[0] != '/') {
 
612
                getcwd(buffer_dir,1024);
 
613
                if (buffer_dir[strlen(buffer_dir)-1] != '/') {
 
614
                        buffer_dir[strlen(buffer_dir)+1] = '\0';
 
615
                        buffer_dir[strlen(buffer_dir)] = '/';
 
616
                }
 
617
                buffer_file = realloc(buffer_file,(strlen(buffer_file) + strlen(buffer_dir) + 1));
 
618
                if (buffer_file == NULL) {
 
619
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
 
620
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, filename, errno);
 
621
                        free(buffer_file);
 
622
                        return NULL;
 
623
                }
 
624
                memmove((buffer_file + strlen(buffer_dir)), buffer_file, strlen(buffer_file)+1);
 
625
                strncpy (buffer_file,buffer_dir,strlen(buffer_dir));
 
626
        }
 
627
#endif
 
628
        if (stat(buffer_file,&buffer_stat)) {
 
629
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_STAT, filename, errno);
 
630
                free(buffer_file);
 
631
                return NULL;
 
632
        }
 
633
        *size = buffer_stat.st_size;
 
634
        data = malloc(*size);
 
635
        if (data == NULL) {
 
636
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
 
637
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, filename, errno);
 
638
                free(buffer_file);
 
639
                return NULL;
 
640
        }
 
641
#ifdef _WIN32
 
642
        if ((buffer_fd = open(buffer_file,(O_RDONLY | O_BINARY))) == -1) {
 
643
#else
 
644
        if ((buffer_fd = open(buffer_file,O_RDONLY)) == -1) {
 
645
#endif
 
646
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_OPEN, filename, errno);
 
647
                free(buffer_file);
 
648
                free(data);
 
649
                return NULL;
 
650
        }
 
651
        if (read(buffer_fd,data,*size) != buffer_stat.st_size) {
 
652
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_READ, filename, errno);
 
653
                free(buffer_file);
 
654
                free(data);
 
655
                close(buffer_fd);
 
656
                return NULL;
 
657
        }
 
658
        close(buffer_fd);
 
659
        free(buffer_file);
 
660
        return data;                                            
 
661
}
 
662
 
 
663
inline void
 
664
WM_Lock (int * wmlock) {
 
665
        LOCK_START:
 
666
        if (__builtin_expect(((*wmlock) == 0),1)) {
 
667
                (*wmlock)++;
 
668
                if (__builtin_expect(((*wmlock) == 1), 1)) {
 
669
                        return;
 
670
                }
 
671
                (*wmlock)--;
 
672
        }
 
673
#ifdef _WIN32
 
674
        Sleep(10);
 
675
#else
 
676
        usleep(500);
 
677
#endif
 
678
        goto LOCK_START;
 
679
}
 
680
 
 
681
inline void
 
682
WM_Unlock (int *wmlock) {
 
683
        (*wmlock)--;
 
684
}
 
685
 
 
686
void
 
687
WM_InitPatches ( void ) {
 
688
        int i;
 
689
        for (i = 0; i < 128; i++) {
 
690
                patch[i] = NULL;
 
691
        }       
 
692
}
 
693
 
 
694
void
 
695
WM_FreePatches ( void ) {
 
696
        int i;
 
697
        struct _patch * tmp_patch;
 
698
        struct _sample * tmp_sample;
 
699
 
 
700
        WM_Lock(&patch_lock);
 
701
        for (i = 0; i < 128; i++) {
 
702
                if (patch[i] != NULL) {
 
703
                        while (patch[i] != NULL) {
 
704
                                if (patch[i]->filename != NULL) {
 
705
                                        if (patch[i]->first_sample != NULL) {
 
706
                                                while (patch[i]->first_sample != NULL) {
 
707
                                                        tmp_sample = patch[i]->first_sample->next;
 
708
                                                        if (patch[i]->first_sample->data != NULL)
 
709
                                                                free (patch[i]->first_sample->data);
 
710
                                                        free (patch[i]->first_sample);
 
711
                                                        patch[i]->first_sample = tmp_sample;
 
712
                                                }
 
713
                                        }
 
714
                                        free (patch[i]->filename);
 
715
                                }
 
716
                                tmp_patch = patch[i]->next;
 
717
                                free(patch[i]);
 
718
                                patch[i] = tmp_patch;
 
719
                        }
 
720
                }
 
721
        }
 
722
        WM_Unlock(&patch_lock);
 
723
}
 
724
 
 
725
int
 
726
WM_LoadConfig (const char *config_file) {
 
727
        unsigned long int config_size = 0;
 
728
        unsigned char *config_buffer =  NULL;
 
729
        char * dir_end =  NULL;
 
730
        char * config_dir =  NULL;
 
731
        unsigned long int config_ptr = 0;
 
732
        unsigned long int line_start_ptr = 0;
 
733
        char * line_buffer = NULL;
 
734
        unsigned long int line_ptr = 0;
 
735
        char * chr_ptr = NULL;
 
736
        unsigned short int patchid = 0;
 
737
        char * new_config = NULL;
 
738
        struct _patch * tmp_patch;
 
739
 
 
740
        if ((config_buffer = WM_BufferFile(config_file, &config_size)) == NULL) {
 
741
                return -1;
 
742
        }
 
743
        if (config_buffer == NULL) {
 
744
                WM_FreePatches();
 
745
                return -1;
 
746
        }
 
747
        
 
748
        dir_end = strrchr(config_file,'/');
 
749
        if (dir_end != NULL) {
 
750
                config_dir = malloc((dir_end - config_file + 2));
 
751
                if (config_dir == NULL) {
 
752
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config", errno);
 
753
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
754
                        WM_FreePatches();
 
755
                        free (config_buffer);
 
756
                        return -1;
 
757
                }
 
758
                strncpy(config_dir, config_file, (dir_end - config_file + 1));
 
759
                config_dir[dir_end - config_file + 1] = '\0';
 
760
        }
 
761
        config_ptr = 0;
 
762
        line_start_ptr = 0;
 
763
        while (config_ptr < config_size) {
 
764
                // find end of line
 
765
                if (config_buffer[config_ptr] != '\n') {
 
766
                        // remove unwanted crud
 
767
                        if (config_buffer[config_ptr] == '\t') {
 
768
                                config_buffer[config_ptr] = ' ';
 
769
                        } else if (config_buffer[config_ptr] == '\r') {
 
770
                                config_buffer[config_ptr] = ' ';
 
771
                        }
 
772
                        if ((config_buffer[config_ptr] == ' ') && (config_ptr == line_start_ptr)) {
 
773
                                line_start_ptr++;
 
774
                        }
 
775
 
 
776
                        config_ptr++;
 
777
                        continue;
 
778
                }
 
779
                config_buffer[config_ptr] = '\0';
 
780
                if (config_ptr == line_start_ptr) {
 
781
                        config_ptr++;
 
782
                        line_start_ptr++;
 
783
                        continue;
 
784
                }
 
785
                if (config_buffer[line_start_ptr] == '#') {
 
786
                        config_ptr++;
 
787
                        line_start_ptr = config_ptr;
 
788
                        continue;
 
789
                }
 
790
                        
 
791
                // copy line into a workable buffer
 
792
                line_buffer = realloc(line_buffer, (config_ptr - line_start_ptr + 1));
 
793
                if (line_buffer == NULL) {
 
794
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config", errno);
 
795
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
796
                        WM_FreePatches();
 
797
                        if (config_dir != NULL)
 
798
                                free(config_dir);
 
799
                        free (config_buffer);
 
800
                        return -1;
 
801
                }
 
802
                strcpy(line_buffer, &config_buffer[line_start_ptr]);
 
803
                config_ptr++;
 
804
                line_start_ptr = config_ptr;
 
805
                // remove unwnted crud from line for easier parsing
 
806
                if ((chr_ptr = strstr(line_buffer,"  ")) != NULL) {
 
807
                        while ((chr_ptr = strstr(line_buffer,"  ")) != NULL) {
 
808
                                memmove(chr_ptr, &chr_ptr[1], strlen(chr_ptr));
 
809
                        }
 
810
                }
 
811
                if ((chr_ptr = strchr(line_buffer, '#')) != NULL) {
 
812
                        *chr_ptr = '\0';
 
813
                }
 
814
                if (line_buffer[strlen(line_buffer) -1] == ' ') {
 
815
                        while (line_buffer[strlen(line_buffer) -1] == ' ') {
 
816
                                line_buffer[strlen(line_buffer) -1] = '\0';
 
817
                        }
 
818
                }
 
819
 
 
820
                // now parse line
 
821
                if (strncasecmp(line_buffer, "dir ", 4) == 0) {
 
822
                        if (line_buffer[strlen(line_buffer) - 1] == '/') {
 
823
                                config_dir = realloc(config_dir, strlen(&line_buffer[4]) + 1);
 
824
                                if (config_dir == NULL) {
 
825
                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config", errno);
 
826
                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
827
                                        WM_FreePatches();
 
828
                                        free (line_buffer);
 
829
                                        free (config_buffer);
 
830
                                        return -1;
 
831
                                }
 
832
                                strcpy(config_dir, &line_buffer[4]);
 
833
                        } else {
 
834
                                config_dir = realloc(config_dir, strlen(&line_buffer[4]) + 2);
 
835
                                if (config_dir == NULL) {
 
836
                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config", errno);
 
837
                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
838
                                        WM_FreePatches();
 
839
                                        free (line_buffer);
 
840
                                        free (config_buffer);
 
841
                                        return -1;
 
842
                                }
 
843
                                strcpy(config_dir, &line_buffer[4]);
 
844
                                strcat(config_dir,"/");
 
845
                        }
 
846
                        continue;
 
847
                } else if (strncasecmp(line_buffer, "source ", 7) == 0) {
 
848
                        if (config_dir != NULL && line_buffer[7] != '/') {
 
849
                                new_config = malloc(strlen(config_dir) + strlen(&line_buffer[7]) + 1);
 
850
                                if (new_config == NULL) {
 
851
                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config", errno);
 
852
                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
853
                                        WM_FreePatches();
 
854
                                        free (config_dir);      
 
855
                                        free (line_buffer);
 
856
                                        free (config_buffer);
 
857
                                        return -1;
 
858
                                }
 
859
                                strcpy(new_config,config_dir);
 
860
                                strcpy(&new_config[strlen(config_dir)], &line_buffer[7]);
 
861
                        } else {
 
862
                                new_config = malloc(strlen(&line_buffer[7]) + 1);
 
863
                                if (new_config == NULL) {
 
864
                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse config", errno);
 
865
                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
866
                                        WM_FreePatches();
 
867
                                        free (line_buffer);
 
868
                                        free (config_buffer);
 
869
                                        return -1;
 
870
                                }
 
871
                                strcpy(new_config, &line_buffer[7]);
 
872
                        }
 
873
                        if (WM_LoadConfig(new_config) == -1) {
 
874
                                free (new_config);
 
875
                                free (line_buffer);
 
876
                                free (config_buffer);
 
877
                                if (config_dir != NULL)
 
878
                                        free (config_dir);
 
879
                                return -1;
 
880
                        }
 
881
                        free (new_config);
 
882
                        continue;
 
883
                } else if (strncasecmp(line_buffer, "bank ", 5) == 0) {
 
884
                        if (!isdigit(line_buffer[5])) {
 
885
                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in bank line)", 0);
 
886
                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
887
                                WM_FreePatches();
 
888
                                if (config_dir != NULL)
 
889
                                        free (config_dir);
 
890
                                free (line_buffer);
 
891
                                free (config_buffer);
 
892
                                return -1;
 
893
                        }
 
894
                        patchid = (atoi(&line_buffer[5]) & 0xFF ) << 8;
 
895
                        continue;
 
896
                } else if (strncasecmp(line_buffer, "drumset ", 8) == 0) {
 
897
                        if (!isdigit(line_buffer[8])) {
 
898
                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in drumset line)", 0);
 
899
                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
900
                                WM_FreePatches();
 
901
                                if (config_dir != NULL)
 
902
                                        free (config_dir);
 
903
                                free (line_buffer);
 
904
                                free (config_buffer);
 
905
                                return -1;
 
906
                        }
 
907
                        patchid = ((atoi(&line_buffer[8]) & 0xFF ) << 8) | 0x80;
 
908
                        continue;
 
909
                } else if (isdigit(line_buffer[0])) {
 
910
                        patchid = (patchid & 0xFF80) | (atoi(line_buffer) & 0x7F);
 
911
                        if (patch[(patchid & 0x7F)] == NULL) {
 
912
                                patch[(patchid & 0x7F)] = malloc (sizeof(struct _patch));
 
913
                                if (patch[(patchid & 0x7F)] == NULL) {
 
914
                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
 
915
                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
916
                                        WM_FreePatches();
 
917
                                        if (config_dir != NULL)
 
918
                                                free (config_dir);      
 
919
                                        free (line_buffer);
 
920
                                        free (config_buffer);
 
921
                                        return -1;
 
922
                                }
 
923
                                tmp_patch = patch[(patchid & 0x7F)];
 
924
                                tmp_patch->patchid = patchid;
 
925
                                tmp_patch->filename = NULL;
 
926
                                tmp_patch->amp = 1024;
 
927
                                tmp_patch->note = 0;
 
928
                                tmp_patch->next = NULL;
 
929
                                tmp_patch->first_sample = NULL;
 
930
                                tmp_patch->loaded = 0;
 
931
                                tmp_patch->inuse_count = 0;
 
932
                        } else {
 
933
                                tmp_patch = patch[(patchid & 0x7F)];
 
934
                                if (tmp_patch->patchid == patchid) {
 
935
                                        free (tmp_patch->filename);
 
936
                                        tmp_patch->filename = NULL;
 
937
                                        tmp_patch->amp = 1024;
 
938
                                        tmp_patch->note = 0; 
 
939
                                } else {
 
940
                                        if (tmp_patch->next != NULL) {
 
941
                                                while (tmp_patch->next != NULL) {
 
942
                                                        if (tmp_patch->next->patchid == patchid)
 
943
                                                                break;
 
944
                                                        tmp_patch = tmp_patch->next;
 
945
                                                }
 
946
                                                if (tmp_patch->next == NULL) {
 
947
                                                        tmp_patch->next = malloc (sizeof(struct _patch));
 
948
                                                        if (tmp_patch->next == NULL) {
 
949
                                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
 
950
                                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
951
                                                                WM_FreePatches();
 
952
                                                                if (config_dir != NULL)
 
953
                                                                        free (config_dir);      
 
954
                                                                free (line_buffer);
 
955
                                                                free (config_buffer);
 
956
                                                                return -1;
 
957
                                                        }
 
958
                                                        tmp_patch = tmp_patch->next;
 
959
                                                        tmp_patch->patchid = patchid;
 
960
                                                        tmp_patch->filename = NULL;
 
961
                                                        tmp_patch->amp = 1024;
 
962
                                                        tmp_patch->note = 0;
 
963
                                                        tmp_patch->next = NULL;
 
964
                                                        tmp_patch->first_sample = NULL;
 
965
                                                        tmp_patch->loaded = 0;
 
966
                                                        tmp_patch->inuse_count = 0;
 
967
                                                } else {
 
968
                                                        tmp_patch = tmp_patch->next;
 
969
                                                        free (tmp_patch->filename);
 
970
                                                        tmp_patch->filename = NULL;
 
971
                                                        tmp_patch->amp = 1024;
 
972
                                                        tmp_patch->note = 0; 
 
973
                                                }
 
974
                                        } else {
 
975
                                                tmp_patch->next = malloc (sizeof(struct _patch));
 
976
                                                if (tmp_patch->next == NULL) {
 
977
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
 
978
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
979
                                                        WM_FreePatches();
 
980
                                                        if (config_dir != NULL)
 
981
                                                                free (config_dir);      
 
982
                                                        free (line_buffer);
 
983
                                                        free (config_buffer);
 
984
                                                        return -1;
 
985
                                                }
 
986
                                                tmp_patch = tmp_patch->next;
 
987
                                                tmp_patch->patchid = patchid;
 
988
                                                tmp_patch->filename = NULL;
 
989
                                                tmp_patch->amp = 1024;
 
990
                                                tmp_patch->note = 0;
 
991
                                                tmp_patch->next = NULL;
 
992
                                                tmp_patch->first_sample = NULL;
 
993
                                                tmp_patch->loaded = 0;
 
994
                                                tmp_patch->inuse_count = 0;
 
995
                                        }
 
996
                                }
 
997
                        }
 
998
 
 
999
                        chr_ptr = strchr(line_buffer,' ') + 1;
 
1000
                        if (chr_ptr == NULL) {
 
1001
                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1002
                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
1003
                                WM_FreePatches();
 
1004
                                if (config_dir != NULL)
 
1005
                                        free (config_dir);      
 
1006
                                free (line_buffer);
 
1007
                                free (config_buffer);
 
1008
                                return -1;
 
1009
                        }
 
1010
                        line_ptr = chr_ptr - line_buffer;
 
1011
                        chr_ptr = strchr(&line_buffer[line_ptr],' ');
 
1012
                        if (chr_ptr != NULL) {
 
1013
                                *chr_ptr = '\0';
 
1014
                        }
 
1015
                        if (strncasecmp(&line_buffer[(line_ptr + strlen(&line_buffer[line_ptr]) - 4)], ".pat", 4) != 0) {
 
1016
                                if (config_dir != NULL && line_buffer[line_ptr] != '/') {
 
1017
                                        tmp_patch->filename = malloc(strlen(config_dir) + strlen(&line_buffer[line_ptr]) + 5);
 
1018
                                        if (tmp_patch->filename == NULL) {
 
1019
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
 
1020
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
1021
                                                WM_FreePatches();
 
1022
                                                if (config_dir != NULL)
 
1023
                                                        free (config_dir);      
 
1024
                                                free (line_buffer);
 
1025
                                                free (config_buffer);
 
1026
                                                return -1;
 
1027
                                        }
 
1028
                                        strcpy(tmp_patch->filename, config_dir);
 
1029
                                        strcat(tmp_patch->filename, &line_buffer[line_ptr]);
 
1030
                                } else {
 
1031
                                        tmp_patch->filename = malloc(strlen(&line_buffer[line_ptr]) + 5);
 
1032
                                        if (tmp_patch->filename == NULL) {
 
1033
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
 
1034
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
1035
                                                WM_FreePatches();
 
1036
                                                free (line_buffer);
 
1037
                                                free (config_buffer);
 
1038
                                                return -1;
 
1039
                                        }
 
1040
                                        strcpy(tmp_patch->filename, &line_buffer[line_ptr]);
 
1041
                                }
 
1042
                                strcat(tmp_patch->filename, ".pat");
 
1043
                        } else {
 
1044
                                if (config_dir != NULL && line_buffer[line_ptr] != '/') {
 
1045
                                        tmp_patch->filename = malloc(strlen(config_dir) + strlen(&line_buffer[line_ptr]) + 1);
 
1046
                                        if (tmp_patch->filename == NULL) {
 
1047
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
 
1048
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
1049
                                                WM_FreePatches();
 
1050
                                                if (config_dir != NULL)
 
1051
                                                        free (config_dir);      
 
1052
                                                free (line_buffer);
 
1053
                                                free (config_buffer);
 
1054
                                                return -1;
 
1055
                                        }
 
1056
                                        strcpy(tmp_patch->filename, config_dir);
 
1057
                                        strcat(tmp_patch->filename, &line_buffer[line_ptr]);
 
1058
                                } else {
 
1059
                                        tmp_patch->filename = malloc(strlen(&line_buffer[line_ptr]) + 1);
 
1060
                                        if (tmp_patch->filename == NULL) {
 
1061
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
 
1062
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, config_file, 0);
 
1063
                                                WM_FreePatches();
 
1064
                                                free (line_buffer);
 
1065
                                                free (config_buffer);
 
1066
                                                return -1;
 
1067
                                        }
 
1068
                                        strcpy(tmp_patch->filename, &line_buffer[line_ptr]);
 
1069
                                }
 
1070
                        }
 
1071
                        
 
1072
                        tmp_patch->env[0].set = 0x00;
 
1073
                        tmp_patch->env[1].set = 0x00;
 
1074
                        tmp_patch->env[2].set = 0x00;
 
1075
                        tmp_patch->env[3].set = 0x00;
 
1076
                        tmp_patch->env[4].set = 0x00;
 
1077
                        tmp_patch->env[5].set = 0x00;
 
1078
                        tmp_patch->keep = 0;
 
1079
                        tmp_patch->remove = 0;
 
1080
                        
 
1081
                        if (chr_ptr != NULL) {
 
1082
                                line_ptr = chr_ptr - line_buffer + 1;
 
1083
                                chr_ptr = strstr(&line_buffer[line_ptr], "amp=");
 
1084
                                if (chr_ptr != NULL) {
 
1085
                                        if (!isdigit(chr_ptr[4])) {
 
1086
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1087
                                        } else {
 
1088
                                                tmp_patch->amp = ((atoi(&chr_ptr[4]) << 10) / 100);
 
1089
                                        }
 
1090
                                }
 
1091
                                chr_ptr = strstr(&line_buffer[line_ptr], "note=");
 
1092
                                if (chr_ptr != NULL) {
 
1093
                                        if (!isdigit(chr_ptr[5])) {
 
1094
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1095
                                        } else {
 
1096
                                                tmp_patch->note = atoi(&chr_ptr[5]);
 
1097
                                        }
 
1098
                                }
 
1099
                                chr_ptr = strstr(&line_buffer[line_ptr], "keep=loop");
 
1100
                                if (chr_ptr != NULL) {
 
1101
                                        tmp_patch->keep |= SAMPLE_LOOP;
 
1102
                                }
 
1103
                                chr_ptr = strstr(&line_buffer[line_ptr], "keep=env");
 
1104
                                if (chr_ptr != NULL) {
 
1105
                                        tmp_patch->keep |= SAMPLE_ENVELOPE;
 
1106
                                }
 
1107
                                chr_ptr = strstr(&line_buffer[line_ptr], "remove=sustain");
 
1108
                                if (chr_ptr != NULL) {
 
1109
                                        tmp_patch->remove |= SAMPLE_SUSTAIN;
 
1110
                                }
 
1111
                                chr_ptr = strstr(&line_buffer[line_ptr], "env_time0=");
 
1112
                                if (chr_ptr != NULL) {
 
1113
                                        if (!isdigit(chr_ptr[10])) {
 
1114
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1115
                                        } else {
 
1116
                                                tmp_patch->env[0].time = atof(&chr_ptr[10]);
 
1117
                                                if ((tmp_patch->env[0].time > 45000.0) || (tmp_patch->env[0].time < 1.47)) {
 
1118
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
 
1119
                                                        tmp_patch->env[0].set &= 0xFE;
 
1120
                                                } else {
 
1121
                                                        tmp_patch->env[0].set |= 0x01;
 
1122
                                                }
 
1123
                                        }
 
1124
                                }
 
1125
                                
 
1126
                                chr_ptr = strstr(&line_buffer[line_ptr], "env_time1=");
 
1127
                                if (chr_ptr != NULL) {
 
1128
                                        if (!isdigit(chr_ptr[10])) {
 
1129
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1130
                                        } else {
 
1131
                                                tmp_patch->env[1].time = atof(&chr_ptr[10]);
 
1132
                                                if ((tmp_patch->env[1].time > 45000.0) || (tmp_patch->env[1].time < 1.47)) {
 
1133
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
 
1134
                                                        tmp_patch->env[1].set &= 0xFE;
 
1135
                                                } else {
 
1136
                                                        tmp_patch->env[1].set |= 0x01;
 
1137
                                                }
 
1138
                                        }
 
1139
                                }
 
1140
                                chr_ptr = strstr(&line_buffer[line_ptr], "env_time2=");
 
1141
                                if (chr_ptr != NULL) {
 
1142
                                        if (!isdigit(chr_ptr[10])) {
 
1143
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1144
                                        } else {
 
1145
                                                tmp_patch->env[2].time = atof(&chr_ptr[10]);
 
1146
                                                if ((tmp_patch->env[2].time > 45000.0) || (tmp_patch->env[2].time < 1.47)) {
 
1147
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
 
1148
                                                        tmp_patch->env[2].set &= 0xFE;
 
1149
                                                } else {
 
1150
                                                        tmp_patch->env[2].set |= 0x01;
 
1151
                                                }
 
1152
                                        }
 
1153
                                }
 
1154
                                chr_ptr = strstr(&line_buffer[line_ptr], "env_time3=");
 
1155
                                if (chr_ptr != NULL) {
 
1156
                                        if (!isdigit(chr_ptr[10])) {
 
1157
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1158
                                        } else {
 
1159
                                                tmp_patch->env[3].time = atof(&chr_ptr[10]);
 
1160
                                                if ((tmp_patch->env[3].time > 45000.0) || (tmp_patch->env[3].time < 1.47)) {
 
1161
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
 
1162
                                                        tmp_patch->env[3].set &= 0xFE;
 
1163
                                                } else {
 
1164
                                                        tmp_patch->env[3].set |= 0x01;
 
1165
                                                }
 
1166
                                        }
 
1167
                                }
 
1168
                                chr_ptr = strstr(&line_buffer[line_ptr], "env_time4=");
 
1169
                                if (chr_ptr != NULL) {
 
1170
                                        if (!isdigit(chr_ptr[10])) {
 
1171
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1172
                                        } else {
 
1173
                                                tmp_patch->env[4].time = atof(&chr_ptr[10]);
 
1174
                                                if ((tmp_patch->env[4].time > 45000.0) || (tmp_patch->env[4].time < 1.47)) {
 
1175
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
 
1176
                                                        tmp_patch->env[4].set &= 0xFE;
 
1177
                                                } else {
 
1178
                                                        tmp_patch->env[4].set |= 0x01;
 
1179
                                                }
 
1180
                                        }
 
1181
                                }
 
1182
                                chr_ptr = strstr(&line_buffer[line_ptr], "env_time5=");
 
1183
                                if (chr_ptr != NULL) {
 
1184
                                        if (!isdigit(chr_ptr[10])) {
 
1185
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1186
                                        } else {
 
1187
                                                tmp_patch->env[5].time = atof(&chr_ptr[10]);
 
1188
                                                if ((tmp_patch->env[5].time > 45000.0) || (tmp_patch->env[5].time < 1.47)) {
 
1189
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
 
1190
                                                        tmp_patch->env[5].set &= 0xFE;
 
1191
                                                } else {
 
1192
                                                        tmp_patch->env[5].set |= 0x01;
 
1193
                                                }
 
1194
                                        }
 
1195
                                }
 
1196
                                chr_ptr = strstr(&line_buffer[line_ptr], "env_level0=");
 
1197
                                if (chr_ptr != NULL) {
 
1198
                                        if (!isdigit(chr_ptr[11])) {
 
1199
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1200
                                        } else {
 
1201
                                                tmp_patch->env[0].level = atof(&chr_ptr[10]);
 
1202
                                                if ((tmp_patch->env[0].level > 1.0) || (tmp_patch->env[0].level < 0.0)) {
 
1203
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
 
1204
                                                        tmp_patch->env[0].set &= 0xFD;
 
1205
                                                } else {
 
1206
                                                        tmp_patch->env[0].set |= 0x02;
 
1207
                                                }
 
1208
                                        }
 
1209
                                }
 
1210
                                
 
1211
                                chr_ptr = strstr(&line_buffer[line_ptr], "env_level1=");
 
1212
                                if (chr_ptr != NULL) {
 
1213
                                        if (!isdigit(chr_ptr[11])) {
 
1214
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1215
                                        } else {
 
1216
                                                tmp_patch->env[1].level = atof(&chr_ptr[10]);
 
1217
                                                if ((tmp_patch->env[1].level > 1.0) || (tmp_patch->env[1].level < 0.0)) {
 
1218
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
 
1219
                                                        tmp_patch->env[1].set &= 0xFD;
 
1220
                                                } else {
 
1221
                                                        tmp_patch->env[1].set |= 0x02;
 
1222
                                                }
 
1223
                                        }
 
1224
                                }
 
1225
                                chr_ptr = strstr(&line_buffer[line_ptr], "env_level2=");
 
1226
                                if (chr_ptr != NULL) {
 
1227
                                        if (!isdigit(chr_ptr[11])) {
 
1228
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1229
                                        } else {
 
1230
                                                tmp_patch->env[2].level = atof(&chr_ptr[10]);
 
1231
                                                if ((tmp_patch->env[2].level > 1.0) || (tmp_patch->env[2].level < 0.0)) {
 
1232
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
 
1233
                                                        tmp_patch->env[2].set &= 0xFD;
 
1234
                                                } else {
 
1235
                                                        tmp_patch->env[2].set |= 0x02;
 
1236
                                                }
 
1237
                                        }
 
1238
                                }
 
1239
                                chr_ptr = strstr(&line_buffer[line_ptr], "env_level3=");
 
1240
                                if (chr_ptr != NULL) {
 
1241
                                        if (!isdigit(chr_ptr[11])) {
 
1242
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1243
                                        } else {
 
1244
                                                tmp_patch->env[3].level = atof(&chr_ptr[10]);
 
1245
                                                if ((tmp_patch->env[3].level > 1.0) || (tmp_patch->env[3].level < 0.0)) {
 
1246
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
 
1247
                                                        tmp_patch->env[3].set &= 0xFD;
 
1248
                                                } else {
 
1249
                                                        tmp_patch->env[3].set |= 0x02;
 
1250
                                                }
 
1251
                                        }
 
1252
                                }
 
1253
                                chr_ptr = strstr(&line_buffer[line_ptr], "env_level4=");
 
1254
                                if (chr_ptr != NULL) {
 
1255
                                        if (!isdigit(chr_ptr[11])) {
 
1256
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1257
                                        } else {
 
1258
                                                tmp_patch->env[4].level = atof(&chr_ptr[10]);
 
1259
                                                if ((tmp_patch->env[4].level > 1.0) || (tmp_patch->env[4].level < 0.0)) {
 
1260
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
 
1261
                                                        tmp_patch->env[4].set &= 0xFD;
 
1262
                                                } else {
 
1263
                                                        tmp_patch->env[4].set |= 0x02;
 
1264
                                                }
 
1265
                                        }
 
1266
                                }
 
1267
                                chr_ptr = strstr(&line_buffer[line_ptr], "env_level5=");
 
1268
                                if (chr_ptr != NULL) {
 
1269
                                        if (!isdigit(chr_ptr[11])) {
 
1270
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(syntax error in patch line)", 0);
 
1271
                                        } else {
 
1272
                                                tmp_patch->env[5].level = atof(&chr_ptr[10]);
 
1273
                                                if ((tmp_patch->env[5].level > 1.0) || (tmp_patch->env[5].level < 0.0)) {
 
1274
                                                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, "(range error in patch line)", 0);
 
1275
                                                        tmp_patch->env[5].set &= 0xFD;
 
1276
                                                } else {
 
1277
                                                        tmp_patch->env[5].set |= 0x02;
 
1278
                                                }
 
1279
                                        }
 
1280
                                }
 
1281
 
 
1282
                        }
 
1283
                }
 
1284
        }
 
1285
 
 
1286
        if (line_buffer != NULL)
 
1287
                free (line_buffer);
 
1288
 
 
1289
        free (config_buffer);
 
1290
 
 
1291
        if (config_dir != NULL)
 
1292
                free (config_dir);
 
1293
 
 
1294
        return 0;
 
1295
}
 
1296
 
 
1297
/*
 
1298
 * sample data conversion functions 
 
1299
 * convert data to signed shorts
 
1300
 */
 
1301
 
 
1302
/* 8bit signed */
 
1303
int
 
1304
convert_8s (unsigned char *data, struct _sample *gus_sample ) {
 
1305
        unsigned char *read_data = data;
 
1306
        unsigned char *read_end = data + gus_sample->data_length;
 
1307
        signed short int *write_data = NULL;
 
1308
        
 
1309
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1310
        gus_sample->data = calloc((gus_sample->data_length + 1), sizeof(signed short int));
 
1311
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1312
                write_data = gus_sample->data;
 
1313
                do {
 
1314
                        *write_data = (*read_data++) << 8;
 
1315
                        if (*write_data > gus_sample->max_peek) {
 
1316
                                gus_sample->max_peek = *write_data;
 
1317
                        } else if (*write_data < gus_sample->min_peek) {
 
1318
                                gus_sample->min_peek = *write_data;
 
1319
                        }
 
1320
                        write_data++;
 
1321
                } while (read_data != read_end);
 
1322
                return 0;
 
1323
        }
 
1324
        
 
1325
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1326
        return -1;
 
1327
}
 
1328
 
 
1329
/* 8bit signed ping pong */
 
1330
int
 
1331
convert_8sp (unsigned char *data, struct _sample *gus_sample ) {
 
1332
        unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
 
1333
        unsigned long int dloop_length = loop_length * 2;
 
1334
        unsigned long int new_length = gus_sample->data_length + dloop_length;
 
1335
        unsigned char *read_data = data;
 
1336
        unsigned char *read_end = data + gus_sample->loop_start;
 
1337
        signed short int *write_data = NULL;
 
1338
        signed short int *write_data_a = NULL;
 
1339
        signed short int *write_data_b = NULL;
 
1340
        
 
1341
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1342
        gus_sample->data = calloc((new_length + 1), sizeof(signed short int));
 
1343
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1344
                write_data = gus_sample->data;
 
1345
                do {
 
1346
                        *write_data = (*read_data++) << 8;
 
1347
                        if (*write_data > gus_sample->max_peek) {
 
1348
                                gus_sample->max_peek = *write_data;
 
1349
                        } else if (*write_data < gus_sample->min_peek) {
 
1350
                                gus_sample->min_peek = *write_data;
 
1351
                        }
 
1352
                        write_data++;
 
1353
                } while (read_data != read_end);
 
1354
                
 
1355
                *write_data = (*read_data++ << 8);
 
1356
                write_data_a = write_data + dloop_length;
 
1357
                *write_data_a-- = *write_data;
 
1358
                write_data++;
 
1359
                write_data_b = write_data + dloop_length;
 
1360
                read_end = data + gus_sample->loop_end;
 
1361
                do {
 
1362
                        *write_data = (*read_data++) << 8;
 
1363
                        *write_data_a-- = *write_data;
 
1364
                        *write_data_b++ = *write_data;
 
1365
                        if (*write_data > gus_sample->max_peek) {
 
1366
                                gus_sample->max_peek = *write_data;
 
1367
                        } else if (*write_data < gus_sample->min_peek) {
 
1368
                                gus_sample->min_peek = *write_data;
 
1369
                        }
 
1370
                        write_data++;
 
1371
                } while (read_data != read_end);
 
1372
                
 
1373
                *write_data = (*read_data++ << 8);
 
1374
                *write_data_b++ = *write_data;
 
1375
                read_end = data + gus_sample->data_length;
 
1376
                if (__builtin_expect((read_data != read_end),1)) {
 
1377
                        do {
 
1378
                                *write_data_b = (*read_data++) << 8;
 
1379
                                if (*write_data_b > gus_sample->max_peek) {
 
1380
                                        gus_sample->max_peek = *write_data_b;
 
1381
                                } else if (*write_data_b < gus_sample->min_peek) {
 
1382
                                        gus_sample->min_peek = *write_data_b;
 
1383
                                }
 
1384
                                write_data_b++;
 
1385
                        } while (read_data != read_end);
 
1386
                }
 
1387
                gus_sample->loop_start += loop_length;
 
1388
                gus_sample->loop_end += dloop_length;
 
1389
                gus_sample->data_length = new_length;
 
1390
                gus_sample->modes ^= SAMPLE_PINGPONG;
 
1391
                return 0;
 
1392
        }
 
1393
        
 
1394
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1395
        return -1;
 
1396
}
 
1397
 
 
1398
/* 8bit signed reverse */
 
1399
int
 
1400
convert_8sr (unsigned char *data, struct _sample *gus_sample ) {
 
1401
        unsigned char *read_data = data;
 
1402
        unsigned char *read_end = data + gus_sample->data_length;
 
1403
        signed short int *write_data = NULL;
 
1404
        unsigned long int tmp_loop = 0;
 
1405
        
 
1406
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1407
        gus_sample->data = calloc((gus_sample->data_length + 1), sizeof(signed short int));
 
1408
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1409
                write_data = gus_sample->data + gus_sample->data_length - 1;
 
1410
                do {
 
1411
                        *write_data = (*read_data++) << 8;
 
1412
                        if (*write_data > gus_sample->max_peek) {
 
1413
                                gus_sample->max_peek = *write_data;
 
1414
                        } else if (*write_data < gus_sample->min_peek) {
 
1415
                                gus_sample->min_peek = *write_data;
 
1416
                        }
 
1417
                        write_data--;
 
1418
                } while (read_data != read_end);        
 
1419
                tmp_loop = gus_sample->loop_end;
 
1420
                gus_sample->loop_end = gus_sample->data_length - gus_sample->loop_start;
 
1421
                gus_sample->loop_start = gus_sample->data_length - tmp_loop;
 
1422
                gus_sample->loop_fraction  = ((gus_sample->loop_fraction & 0x0f) << 4) | ((gus_sample->loop_fraction & 0xf0) >> 4);
 
1423
                gus_sample->modes ^= SAMPLE_REVERSE;
 
1424
                return 0;
 
1425
        }
 
1426
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1427
        return -1;
 
1428
}
 
1429
 
 
1430
 
 
1431
/* 8bit signed reverse ping pong */
 
1432
int
 
1433
convert_8srp (unsigned char *data, struct _sample *gus_sample ) {
 
1434
        unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
 
1435
        unsigned long int dloop_length = loop_length * 2;
 
1436
        unsigned long int new_length = gus_sample->data_length + dloop_length;
 
1437
        unsigned char *read_data = data + gus_sample->data_length - 1;
 
1438
        unsigned char *read_end = data + gus_sample->loop_end;
 
1439
        signed short int *write_data = NULL;
 
1440
        signed short int *write_data_a = NULL;
 
1441
        signed short int *write_data_b = NULL;
 
1442
        
 
1443
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1444
        gus_sample->data = calloc((new_length + 1), sizeof(signed short int));
 
1445
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1446
                write_data = gus_sample->data;
 
1447
                do {
 
1448
                        *write_data = (*read_data--) << 8;
 
1449
                        if (*write_data > gus_sample->max_peek) {
 
1450
                                gus_sample->max_peek = *write_data;
 
1451
                        } else if (*write_data < gus_sample->min_peek) {
 
1452
                                gus_sample->min_peek = *write_data;
 
1453
                        }
 
1454
                        write_data++;
 
1455
                } while (read_data != read_end);
 
1456
                
 
1457
                *write_data = (*read_data-- << 8);
 
1458
                write_data_a = write_data + dloop_length;
 
1459
                *write_data_a-- = *write_data;
 
1460
                write_data++;
 
1461
                write_data_b = write_data + dloop_length;
 
1462
                read_end = data + gus_sample->loop_start;
 
1463
                do {
 
1464
                        *write_data = (*read_data--) << 8;
 
1465
                        *write_data_a-- = *write_data;
 
1466
                        *write_data_b++ = *write_data;
 
1467
                        if (*write_data > gus_sample->max_peek) {
 
1468
                                gus_sample->max_peek = *write_data;
 
1469
                        } else if (*write_data < gus_sample->min_peek) {
 
1470
                                gus_sample->min_peek = *write_data;
 
1471
                        }
 
1472
                        write_data++;
 
1473
                } while (read_data != read_end);
 
1474
                
 
1475
                *write_data = (*read_data-- << 8);
 
1476
                *write_data_b++ = *write_data;
 
1477
                read_end = data - 1;
 
1478
                do {
 
1479
                        *write_data_b = (*read_data--) << 8;
 
1480
                        if (*write_data_b > gus_sample->max_peek) {
 
1481
                                gus_sample->max_peek = *write_data_b;
 
1482
                        } else if (*write_data_b < gus_sample->min_peek) {
 
1483
                                gus_sample->min_peek = *write_data_b;
 
1484
                        }
 
1485
                        write_data_b++;
 
1486
                } while (read_data != read_end);
 
1487
                gus_sample->loop_start += loop_length;
 
1488
                gus_sample->loop_end += dloop_length;
 
1489
                gus_sample->data_length = new_length;
 
1490
                gus_sample->modes ^= SAMPLE_PINGPONG | SAMPLE_REVERSE;
 
1491
                return 0;
 
1492
        }
 
1493
        
 
1494
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1495
        return -1;
 
1496
}
 
1497
 
 
1498
/* 8bit unsigned */
 
1499
int
 
1500
convert_8u (unsigned char *data, struct _sample *gus_sample ) {
 
1501
        unsigned char *read_data = data;
 
1502
        unsigned char *read_end = data + gus_sample->data_length;
 
1503
        signed short int *write_data = NULL;
 
1504
 
 
1505
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1506
        gus_sample->data = calloc((gus_sample->data_length + 1), sizeof(signed short int));
 
1507
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1508
                write_data = gus_sample->data;
 
1509
                do {
 
1510
                        *write_data = ((*read_data++) ^ 0x80) << 8;
 
1511
                        if (*write_data > gus_sample->max_peek) {
 
1512
                                gus_sample->max_peek = *write_data;
 
1513
                        } else if (*write_data < gus_sample->min_peek) {
 
1514
                                gus_sample->min_peek = *write_data;
 
1515
                        }
 
1516
                        write_data++;
 
1517
                } while (read_data != read_end);
 
1518
                gus_sample->modes ^= SAMPLE_UNSIGNED;
 
1519
                return 0;
 
1520
        }
 
1521
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1522
        return -1;
 
1523
}
 
1524
 
 
1525
/* 8bit unsigned ping pong */
 
1526
int
 
1527
convert_8up (unsigned char *data, struct _sample *gus_sample ) {
 
1528
        unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
 
1529
        unsigned long int dloop_length = loop_length * 2;
 
1530
        unsigned long int new_length = gus_sample->data_length + dloop_length;
 
1531
        unsigned char *read_data = data;
 
1532
        unsigned char *read_end = data + gus_sample->loop_start;
 
1533
        signed short int *write_data = NULL;
 
1534
        signed short int *write_data_a = NULL;
 
1535
        signed short int *write_data_b = NULL;
 
1536
        
 
1537
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1538
        gus_sample->data = calloc((new_length + 1), sizeof(signed short int));
 
1539
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1540
                write_data = gus_sample->data;
 
1541
                do {
 
1542
                        *write_data = ((*read_data++) ^ 0x80) << 8;
 
1543
                        if (*write_data > gus_sample->max_peek) {
 
1544
                                gus_sample->max_peek = *write_data;
 
1545
                        } else if (*write_data < gus_sample->min_peek) {
 
1546
                                gus_sample->min_peek = *write_data;
 
1547
                        }
 
1548
                        write_data++;
 
1549
                } while (read_data != read_end);
 
1550
                
 
1551
                *write_data = ((*read_data++) ^ 0x80) << 8;
 
1552
                write_data_a = write_data + dloop_length;
 
1553
                *write_data_a-- = *write_data;
 
1554
                write_data++;
 
1555
                write_data_b = write_data + dloop_length;
 
1556
                read_end = data + gus_sample->loop_end;
 
1557
                do {
 
1558
                        *write_data = ((*read_data++) ^ 0x80) << 8;
 
1559
                        *write_data_a-- = *write_data;
 
1560
                        *write_data_b++ = *write_data;
 
1561
                        if (*write_data > gus_sample->max_peek) {
 
1562
                                gus_sample->max_peek = *write_data;
 
1563
                        } else if (*write_data < gus_sample->min_peek) {
 
1564
                                gus_sample->min_peek = *write_data;
 
1565
                        }
 
1566
                        write_data++;
 
1567
                } while (read_data != read_end);
 
1568
                
 
1569
                *write_data = ((*read_data++) ^ 0x80) << 8;
 
1570
                *write_data_b++ = *write_data;
 
1571
                read_end = data + gus_sample->data_length;
 
1572
                if (__builtin_expect((read_data != read_end),1)) {
 
1573
                        do {
 
1574
                                *write_data_b = ((*read_data++) ^ 0x80) << 8;
 
1575
                                if (*write_data_b > gus_sample->max_peek) {
 
1576
                                        gus_sample->max_peek = *write_data_b;
 
1577
                                } else if (*write_data_b < gus_sample->min_peek) {
 
1578
                                        gus_sample->min_peek = *write_data_b;
 
1579
                                }
 
1580
                                write_data_b++;
 
1581
                        } while (read_data != read_end);
 
1582
                }
 
1583
                gus_sample->loop_start += loop_length;
 
1584
                gus_sample->loop_end += dloop_length;
 
1585
                gus_sample->data_length = new_length;
 
1586
                gus_sample->modes ^= SAMPLE_PINGPONG | SAMPLE_UNSIGNED;
 
1587
                return 0;
 
1588
        }
 
1589
        
 
1590
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1591
        return -1;
 
1592
}
 
1593
 
 
1594
/* 8bit unsigned reverse */
 
1595
int
 
1596
convert_8ur (unsigned char *data, struct _sample *gus_sample ) {
 
1597
        unsigned char *read_data = data;
 
1598
        unsigned char *read_end = data + gus_sample->data_length;
 
1599
        signed short int *write_data = NULL;
 
1600
        unsigned long int tmp_loop = 0;
 
1601
        
 
1602
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1603
        gus_sample->data = calloc((gus_sample->data_length + 1), sizeof(signed short int));
 
1604
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1605
                write_data = gus_sample->data + gus_sample->data_length - 1;
 
1606
                do {
 
1607
                        *write_data = ((*read_data++) ^ 0x80) << 8;
 
1608
                        if (*write_data > gus_sample->max_peek) {
 
1609
                                gus_sample->max_peek = *write_data;
 
1610
                        } else if (*write_data < gus_sample->min_peek) {
 
1611
                                gus_sample->min_peek = *write_data;
 
1612
                        }
 
1613
                        write_data--;
 
1614
                } while (read_data != read_end);        
 
1615
                tmp_loop = gus_sample->loop_end;
 
1616
                gus_sample->loop_end = gus_sample->data_length - gus_sample->loop_start;
 
1617
                gus_sample->loop_start = gus_sample->data_length - tmp_loop;
 
1618
                gus_sample->loop_fraction  = ((gus_sample->loop_fraction & 0x0f) << 4) | ((gus_sample->loop_fraction & 0xf0) >> 4);
 
1619
                gus_sample->modes ^= SAMPLE_REVERSE | SAMPLE_UNSIGNED;
 
1620
                return 0;
 
1621
        }
 
1622
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1623
        return -1;
 
1624
}
 
1625
 
 
1626
/* 8bit unsigned reverse ping pong */
 
1627
int
 
1628
convert_8urp (unsigned char *data, struct _sample *gus_sample ) {
 
1629
        unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
 
1630
        unsigned long int dloop_length = loop_length * 2;
 
1631
        unsigned long int new_length = gus_sample->data_length + dloop_length;
 
1632
        unsigned char *read_data = data + gus_sample->data_length - 1;
 
1633
        unsigned char *read_end = data + gus_sample->loop_end;
 
1634
        signed short int *write_data = NULL;
 
1635
        signed short int *write_data_a = NULL;
 
1636
        signed short int *write_data_b = NULL;
 
1637
        
 
1638
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1639
        gus_sample->data = calloc((new_length + 1), sizeof(signed short int));
 
1640
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1641
                write_data = gus_sample->data;
 
1642
                do {
 
1643
                        *write_data = ((*read_data--) ^ 0x80) << 8;
 
1644
                        if (*write_data > gus_sample->max_peek) {
 
1645
                                gus_sample->max_peek = *write_data;
 
1646
                        } else if (*write_data < gus_sample->min_peek) {
 
1647
                                gus_sample->min_peek = *write_data;
 
1648
                        }
 
1649
                        write_data++;
 
1650
                } while (read_data != read_end);
 
1651
                
 
1652
                *write_data = ((*read_data--) ^ 0x80) << 8;
 
1653
                write_data_a = write_data + dloop_length;
 
1654
                *write_data_a-- = *write_data;
 
1655
                write_data++;
 
1656
                write_data_b = write_data + dloop_length;
 
1657
                read_end = data + gus_sample->loop_start;
 
1658
                do {
 
1659
                        *write_data = ((*read_data--) ^ 0x80) << 8;
 
1660
                        *write_data_a-- = *write_data;
 
1661
                        *write_data_b++ = *write_data;
 
1662
                        if (*write_data > gus_sample->max_peek) {
 
1663
                                gus_sample->max_peek = *write_data;
 
1664
                        } else if (*write_data < gus_sample->min_peek) {
 
1665
                                gus_sample->min_peek = *write_data;
 
1666
                        }
 
1667
                        write_data++;
 
1668
                } while (read_data != read_end);
 
1669
                
 
1670
                *write_data = ((*read_data--) ^ 0x80) << 8;
 
1671
                *write_data_b++ = *write_data;
 
1672
                read_end = data - 1;
 
1673
                do {
 
1674
                        *write_data_b = ((*read_data--) ^ 0x80) << 8;
 
1675
                        if (*write_data_b > gus_sample->max_peek) {
 
1676
                                gus_sample->max_peek = *write_data_b;
 
1677
                        } else if (*write_data_b < gus_sample->min_peek) {
 
1678
                                gus_sample->min_peek = *write_data_b;
 
1679
                        }
 
1680
                        write_data_b++;
 
1681
                } while (read_data != read_end);
 
1682
                gus_sample->loop_start += loop_length;
 
1683
                gus_sample->loop_end += dloop_length;
 
1684
                gus_sample->data_length = new_length;
 
1685
                gus_sample->modes ^= SAMPLE_PINGPONG | SAMPLE_REVERSE | SAMPLE_UNSIGNED;
 
1686
                return 0;
 
1687
        }
 
1688
        
 
1689
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1690
        return -1;
 
1691
}
 
1692
 
 
1693
/* 16bit signed */
 
1694
int
 
1695
convert_16s (unsigned char *data, struct _sample *gus_sample ) {
 
1696
        unsigned char *read_data = data;
 
1697
        unsigned char *read_end = data + gus_sample->data_length;
 
1698
        signed short int *write_data = NULL;
 
1699
        
 
1700
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1701
        gus_sample->data = calloc(((gus_sample->data_length >> 1) + 1),sizeof(signed short int));
 
1702
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1703
                write_data = gus_sample->data;
 
1704
                do {
 
1705
                        *write_data = *read_data++;
 
1706
                        *write_data |= (*read_data++) << 8;
 
1707
                        if (*write_data > gus_sample->max_peek) {
 
1708
                                gus_sample->max_peek = *write_data;
 
1709
                        } else if (*write_data < gus_sample->min_peek) {
 
1710
                                gus_sample->min_peek = *write_data;
 
1711
                        }
 
1712
                        write_data++;
 
1713
                } while (read_data < read_end);
 
1714
                
 
1715
                gus_sample->loop_start >>= 1;
 
1716
                gus_sample->loop_end >>= 1;
 
1717
                gus_sample->data_length >>= 1;
 
1718
                return 0;
 
1719
        }
 
1720
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1721
        return -1;
 
1722
}       
 
1723
 
 
1724
/* 16bit signed ping pong */
 
1725
int
 
1726
convert_16sp (unsigned char *data, struct _sample *gus_sample ) {
 
1727
        unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
 
1728
        unsigned long int dloop_length = loop_length * 2;
 
1729
        unsigned long int new_length = gus_sample->data_length + dloop_length;
 
1730
        unsigned char *read_data = data;
 
1731
        unsigned char *read_end = data + gus_sample->loop_start;
 
1732
        signed short int *write_data = NULL;
 
1733
        signed short int *write_data_a = NULL;
 
1734
        signed short int *write_data_b = NULL;
 
1735
        
 
1736
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1737
        gus_sample->data = calloc(((new_length >> 1) + 1), sizeof(signed short int));
 
1738
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1739
                write_data = gus_sample->data;
 
1740
                do {
 
1741
                        *write_data = (*read_data++);
 
1742
                        *write_data |= (*read_data++) << 8;
 
1743
                        if (*write_data > gus_sample->max_peek) {
 
1744
                                gus_sample->max_peek = *write_data;
 
1745
                        } else if (*write_data < gus_sample->min_peek) {
 
1746
                                gus_sample->min_peek = *write_data;
 
1747
                        }
 
1748
                        write_data++;
 
1749
                } while (read_data < read_end);
 
1750
                
 
1751
                *write_data = (*read_data++);
 
1752
                *write_data |= (*read_data++) << 8;
 
1753
                write_data_a = write_data + (dloop_length >> 1);
 
1754
                *write_data_a-- = *write_data;
 
1755
                write_data++;
 
1756
                write_data_b = write_data + (dloop_length >> 1);
 
1757
                read_end = data + gus_sample->loop_end;
 
1758
                do {
 
1759
                        *write_data = (*read_data++);
 
1760
                        *write_data |= (*read_data++) << 8;
 
1761
                        *write_data_a-- = *write_data;
 
1762
                        *write_data_b++ = *write_data;
 
1763
                        if (*write_data > gus_sample->max_peek) {
 
1764
                                gus_sample->max_peek = *write_data;
 
1765
                        } else if (*write_data < gus_sample->min_peek) {
 
1766
                                gus_sample->min_peek = *write_data;
 
1767
                        }
 
1768
                        write_data++;
 
1769
                } while (read_data < read_end);
 
1770
                
 
1771
                *write_data = *(read_data++);
 
1772
                *write_data |= (*read_data++) << 8;
 
1773
                *write_data_b++ = *write_data;
 
1774
                read_end = data + gus_sample->data_length;
 
1775
                if (__builtin_expect((read_data != read_end),1)) {
 
1776
                        do {
 
1777
                                *write_data_b = *(read_data++);
 
1778
                                *write_data_b |= (*read_data++) << 8;
 
1779
                                if (*write_data_b > gus_sample->max_peek) {
 
1780
                                        gus_sample->max_peek = *write_data_b;
 
1781
                                } else if (*write_data_b < gus_sample->min_peek) {
 
1782
                                        gus_sample->min_peek = *write_data_b;
 
1783
                                }
 
1784
                                write_data_b++;
 
1785
                        } while (read_data < read_end);
 
1786
                }
 
1787
                gus_sample->loop_start += loop_length;
 
1788
                gus_sample->loop_end += dloop_length;
 
1789
                gus_sample->data_length = new_length;
 
1790
                gus_sample->modes ^= SAMPLE_PINGPONG;
 
1791
                gus_sample->loop_start >>= 1;
 
1792
                gus_sample->loop_end >>= 1;
 
1793
                gus_sample->data_length >>= 1;
 
1794
                return 0;
 
1795
        }
 
1796
        
 
1797
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1798
        return -1;
 
1799
}
 
1800
 
 
1801
/* 16bit signed reverse */
 
1802
int
 
1803
convert_16sr (unsigned char *data, struct _sample *gus_sample ) {
 
1804
        unsigned char *read_data = data;
 
1805
        unsigned char *read_end = data + gus_sample->data_length;
 
1806
        signed short int *write_data = NULL;
 
1807
        unsigned long int tmp_loop = 0;
 
1808
        
 
1809
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1810
        gus_sample->data = calloc(((gus_sample->data_length >> 1) + 1), sizeof(signed short int));
 
1811
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1812
                write_data = gus_sample->data + (gus_sample->data_length >> 1) - 1;
 
1813
                do {
 
1814
                        *write_data = *read_data++;
 
1815
                        *write_data |= (*read_data++) << 8;
 
1816
                        if (*write_data > gus_sample->max_peek) {
 
1817
                                gus_sample->max_peek = *write_data;
 
1818
                        } else if (*write_data < gus_sample->min_peek) {
 
1819
                                gus_sample->min_peek = *write_data;
 
1820
                        }
 
1821
                        write_data--;
 
1822
                } while (read_data < read_end); 
 
1823
                tmp_loop = gus_sample->loop_end;
 
1824
                gus_sample->loop_end = gus_sample->data_length - gus_sample->loop_start;
 
1825
                gus_sample->loop_start = gus_sample->data_length - tmp_loop;
 
1826
                gus_sample->loop_fraction  = ((gus_sample->loop_fraction & 0x0f) << 4) | ((gus_sample->loop_fraction & 0xf0) >> 4);
 
1827
                gus_sample->loop_start >>= 1;
 
1828
                gus_sample->loop_end >>= 1;
 
1829
                gus_sample->data_length >>= 1;
 
1830
                gus_sample->modes ^= SAMPLE_REVERSE;
 
1831
                return 0;
 
1832
        }
 
1833
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1834
        return -1;
 
1835
}
 
1836
 
 
1837
 
 
1838
/* 16bit signed reverse ping pong */
 
1839
int
 
1840
convert_16srp (unsigned char *data, struct _sample *gus_sample ) {
 
1841
        unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
 
1842
        unsigned long int dloop_length = loop_length * 2;
 
1843
        unsigned long int new_length = gus_sample->data_length + dloop_length;
 
1844
        unsigned char *read_data = data + gus_sample->data_length - 1;
 
1845
        unsigned char *read_end = data + gus_sample->loop_end;
 
1846
        signed short int *write_data = NULL;
 
1847
        signed short int *write_data_a = NULL;
 
1848
        signed short int *write_data_b = NULL;
 
1849
        
 
1850
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1851
        gus_sample->data = calloc(((new_length >> 1) + 1), sizeof(signed short int));
 
1852
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1853
                write_data = gus_sample->data;
 
1854
                do {
 
1855
                        
 
1856
                        *write_data = (*read_data--) << 8;
 
1857
                        *write_data |= *read_data--;
 
1858
                        if (*write_data > gus_sample->max_peek) {
 
1859
                                gus_sample->max_peek = *write_data;
 
1860
                        } else if (*write_data < gus_sample->min_peek) {
 
1861
                                gus_sample->min_peek = *write_data;
 
1862
                        }
 
1863
                        write_data++;
 
1864
                } while (read_data < read_end);
 
1865
                
 
1866
                *write_data = (*read_data-- << 8);
 
1867
                *write_data |= *read_data--;
 
1868
                write_data_a = write_data + (dloop_length >> 1);
 
1869
                *write_data_a-- = *write_data;
 
1870
                write_data++;
 
1871
                write_data_b = write_data + (dloop_length >> 1);
 
1872
                read_end = data + gus_sample->loop_start;
 
1873
                do {
 
1874
                        *write_data = (*read_data--) << 8;
 
1875
                        *write_data |= *read_data--;
 
1876
                        *write_data_a-- = *write_data;
 
1877
                        *write_data_b++ = *write_data;
 
1878
                        if (*write_data > gus_sample->max_peek) {
 
1879
                                gus_sample->max_peek = *write_data;
 
1880
                        } else if (*write_data < gus_sample->min_peek) {
 
1881
                                gus_sample->min_peek = *write_data;
 
1882
                        }
 
1883
                        write_data++;
 
1884
                } while (read_data < read_end);
 
1885
                
 
1886
                *write_data = ((*read_data--) << 8);
 
1887
                *write_data |= *read_data--;
 
1888
                *write_data_b++ = *write_data;
 
1889
                read_end = data - 1;
 
1890
                do {
 
1891
                        *write_data_b = (*read_data--) << 8;
 
1892
                        *write_data_b |= *read_data--;
 
1893
                        if (*write_data_b > gus_sample->max_peek) {
 
1894
                                gus_sample->max_peek = *write_data_b;
 
1895
                        } else if (*write_data_b < gus_sample->min_peek) {
 
1896
                                gus_sample->min_peek = *write_data_b;
 
1897
                        }
 
1898
                        write_data_b++;
 
1899
                } while (read_data < read_end);
 
1900
                gus_sample->loop_start += loop_length;
 
1901
                gus_sample->loop_end += dloop_length;
 
1902
                gus_sample->data_length = new_length;
 
1903
                gus_sample->modes ^= SAMPLE_PINGPONG | SAMPLE_REVERSE;
 
1904
                return 0;
 
1905
        }
 
1906
        
 
1907
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1908
        return -1;
 
1909
}
 
1910
 
 
1911
/* 16bit unsigned */
 
1912
int
 
1913
convert_16u (unsigned char *data, struct _sample *gus_sample ) {
 
1914
        unsigned char *read_data = data;
 
1915
        unsigned char *read_end = data + gus_sample->data_length;
 
1916
        signed short int *write_data = NULL;
 
1917
        
 
1918
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1919
        gus_sample->data = calloc(((gus_sample->data_length >> 1) + 1),sizeof(signed short int));
 
1920
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1921
                write_data = gus_sample->data;
 
1922
                do {
 
1923
                        *write_data = *read_data++;
 
1924
                        *write_data |= ((*read_data++) ^ 0x80) << 8;
 
1925
                        if (*write_data > gus_sample->max_peek) {
 
1926
                                gus_sample->max_peek = *write_data;
 
1927
                        } else if (*write_data < gus_sample->min_peek) {
 
1928
                                gus_sample->min_peek = *write_data;
 
1929
                        }
 
1930
                        write_data++;
 
1931
                } while (read_data < read_end);
 
1932
                gus_sample->loop_start >>= 1;
 
1933
                gus_sample->loop_end >>= 1;
 
1934
                gus_sample->data_length >>= 1;
 
1935
                gus_sample->modes ^= SAMPLE_UNSIGNED;
 
1936
                return 0;
 
1937
        }
 
1938
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
1939
        return -1;
 
1940
}
 
1941
 
 
1942
/* 16bit unsigned ping pong */
 
1943
int
 
1944
convert_16up (unsigned char *data, struct _sample *gus_sample ) {
 
1945
        unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
 
1946
        unsigned long int dloop_length = loop_length * 2;
 
1947
        unsigned long int new_length = gus_sample->data_length + dloop_length;
 
1948
        unsigned char *read_data = data;
 
1949
        unsigned char *read_end = data + gus_sample->loop_start;
 
1950
        signed short int *write_data = NULL;
 
1951
        signed short int *write_data_a = NULL;
 
1952
        signed short int *write_data_b = NULL;
 
1953
        
 
1954
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
1955
        gus_sample->data = calloc(((new_length >> 1) + 1), sizeof(signed short int));
 
1956
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
1957
                write_data = gus_sample->data;
 
1958
                do {
 
1959
                        *write_data = (*read_data++);
 
1960
                        *write_data |= ((*read_data++) ^ 0x80) << 8;
 
1961
                        if (*write_data > gus_sample->max_peek) {
 
1962
                                gus_sample->max_peek = *write_data;
 
1963
                        } else if (*write_data < gus_sample->min_peek) {
 
1964
                                gus_sample->min_peek = *write_data;
 
1965
                        }
 
1966
                        write_data++;
 
1967
                } while (read_data < read_end);
 
1968
                
 
1969
                *write_data = (*read_data++);
 
1970
                *write_data |= ((*read_data++) ^ 0x80) << 8;
 
1971
                write_data_a = write_data + (dloop_length >> 1);
 
1972
                *write_data_a-- = *write_data;
 
1973
                write_data++;
 
1974
                write_data_b = write_data + (dloop_length >> 1);
 
1975
                read_end = data + gus_sample->loop_end;
 
1976
                do {
 
1977
                        *write_data = (*read_data++);
 
1978
                        *write_data |= ((*read_data++) ^ 0x80) << 8;
 
1979
                        *write_data_a-- = *write_data;
 
1980
                        *write_data_b++ = *write_data;
 
1981
                        if (*write_data > gus_sample->max_peek) {
 
1982
                                gus_sample->max_peek = *write_data;
 
1983
                        } else if (*write_data < gus_sample->min_peek) {
 
1984
                                gus_sample->min_peek = *write_data;
 
1985
                        }
 
1986
                        write_data++;
 
1987
                } while (read_data < read_end);
 
1988
                
 
1989
                *write_data = (*read_data++);
 
1990
                *write_data |= ((*read_data++) ^ 0x80) << 8;
 
1991
                *write_data_b++ = *write_data;
 
1992
                read_end = data + gus_sample->data_length;
 
1993
                if (__builtin_expect((read_data != read_end),1)) {
 
1994
                        do {
 
1995
                                *write_data_b = (*read_data++);
 
1996
                                *write_data_b |= ((*read_data++) ^ 0x80) << 8;
 
1997
                                if (*write_data_b > gus_sample->max_peek) {
 
1998
                                        gus_sample->max_peek = *write_data_b;
 
1999
                                } else if (*write_data_b < gus_sample->min_peek) {
 
2000
                                        gus_sample->min_peek = *write_data_b;
 
2001
                                }
 
2002
                                write_data_b++;
 
2003
                        } while (read_data < read_end);
 
2004
                }
 
2005
                gus_sample->loop_start += loop_length;
 
2006
                gus_sample->loop_end += dloop_length;
 
2007
                gus_sample->data_length = new_length;
 
2008
                gus_sample->modes ^= SAMPLE_PINGPONG;
 
2009
                gus_sample->loop_start >>= 1;
 
2010
                gus_sample->loop_end >>= 1;
 
2011
                gus_sample->data_length >>= 1;
 
2012
                return 0;
 
2013
        }
 
2014
        
 
2015
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
2016
        return -1;
 
2017
}
 
2018
 
 
2019
/* 16bit unsigned reverse */
 
2020
int
 
2021
convert_16ur (unsigned char *data, struct _sample *gus_sample ) {
 
2022
        unsigned char *read_data = data;
 
2023
        unsigned char *read_end = data + gus_sample->data_length;
 
2024
        signed short int *write_data = NULL;
 
2025
        unsigned long int tmp_loop = 0;
 
2026
        
 
2027
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
2028
        gus_sample->data = calloc(((gus_sample->data_length >> 1) + 1), sizeof(signed short int));
 
2029
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
2030
                write_data = gus_sample->data + (gus_sample->data_length >> 1) - 1;
 
2031
                do {
 
2032
                        *write_data = *read_data++;
 
2033
                        *write_data |= ((*read_data++) ^ 0x80) << 8;
 
2034
                        if (*write_data > gus_sample->max_peek) {
 
2035
                                gus_sample->max_peek = *write_data;
 
2036
                        } else if (*write_data < gus_sample->min_peek) {
 
2037
                                gus_sample->min_peek = *write_data;
 
2038
                        }
 
2039
                        write_data--;
 
2040
                } while (read_data < read_end); 
 
2041
                tmp_loop = gus_sample->loop_end;
 
2042
                gus_sample->loop_end = gus_sample->data_length - gus_sample->loop_start;
 
2043
                gus_sample->loop_start = gus_sample->data_length - tmp_loop;
 
2044
                gus_sample->loop_fraction  = ((gus_sample->loop_fraction & 0x0f) << 4) | ((gus_sample->loop_fraction & 0xf0) >> 4);
 
2045
                gus_sample->loop_start >>= 1;
 
2046
                gus_sample->loop_end >>= 1;
 
2047
                gus_sample->data_length >>= 1;
 
2048
                gus_sample->modes ^= SAMPLE_REVERSE | SAMPLE_UNSIGNED;
 
2049
                return 0;
 
2050
        }
 
2051
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
2052
        return -1;
 
2053
}
 
2054
 
 
2055
/* 16bit unsigned reverse ping pong */
 
2056
int
 
2057
convert_16urp (unsigned char *data, struct _sample *gus_sample ) {
 
2058
        unsigned long int loop_length = gus_sample->loop_end - gus_sample->loop_start;
 
2059
        unsigned long int dloop_length = loop_length * 2;
 
2060
        unsigned long int new_length = gus_sample->data_length + dloop_length;
 
2061
        unsigned char *read_data = data + gus_sample->data_length - 1;
 
2062
        unsigned char *read_end = data + gus_sample->loop_end;
 
2063
        signed short int *write_data = NULL;
 
2064
        signed short int *write_data_a = NULL;
 
2065
        signed short int *write_data_b = NULL;
 
2066
        
 
2067
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
2068
        gus_sample->data = calloc(((new_length >> 1) + 1), sizeof(signed short int));
 
2069
        if (__builtin_expect((gus_sample->data != NULL),1)) {
 
2070
                write_data = gus_sample->data;
 
2071
                do {
 
2072
                        *write_data = ((*read_data--) ^ 0x80) << 8;
 
2073
                        *write_data |= *read_data--;
 
2074
                        if (*write_data > gus_sample->max_peek) {
 
2075
                                gus_sample->max_peek = *write_data;
 
2076
                        } else if (*write_data < gus_sample->min_peek) {
 
2077
                                gus_sample->min_peek = *write_data;
 
2078
                        }
 
2079
                        write_data++;
 
2080
                } while (read_data < read_end);
 
2081
                
 
2082
                *write_data = ((*read_data--) ^ 0x80) << 8;
 
2083
                *write_data |= *read_data--;
 
2084
                write_data_a = write_data + (dloop_length >> 1);
 
2085
                *write_data_a-- = *write_data;
 
2086
                write_data++;
 
2087
                write_data_b = write_data + (dloop_length >> 1);
 
2088
                read_end = data + gus_sample->loop_start;
 
2089
                do {
 
2090
                        *write_data = ((*read_data--) ^ 0x80) << 8;
 
2091
                        *write_data |= *read_data--;
 
2092
                        *write_data_a-- = *write_data;
 
2093
                        *write_data_b++ = *write_data;
 
2094
                        if (*write_data > gus_sample->max_peek) {
 
2095
                                gus_sample->max_peek = *write_data;
 
2096
                        } else if (*write_data < gus_sample->min_peek) {
 
2097
                                gus_sample->min_peek = *write_data;
 
2098
                        }
 
2099
                        write_data++;
 
2100
                } while (read_data < read_end);
 
2101
                
 
2102
                *write_data = ((*read_data--) ^ 0x80) << 8;
 
2103
                *write_data |= *read_data--;
 
2104
                *write_data_b++ = *write_data;
 
2105
                read_end = data - 1;
 
2106
                do {
 
2107
                        *write_data_b = ((*read_data--) ^ 0x80) << 8;
 
2108
                        *write_data_b |= *read_data--;
 
2109
                        if (*write_data_b > gus_sample->max_peek) {
 
2110
                                gus_sample->max_peek = *write_data_b;
 
2111
                        } else if (*write_data_b < gus_sample->min_peek) {
 
2112
                                gus_sample->min_peek = *write_data_b;
 
2113
                        }
 
2114
                        write_data_b++;
 
2115
                } while (read_data < read_end);
 
2116
                gus_sample->loop_start += loop_length;
 
2117
                gus_sample->loop_end += dloop_length;
 
2118
                gus_sample->data_length = new_length;
 
2119
                gus_sample->modes ^= SAMPLE_PINGPONG | SAMPLE_REVERSE | SAMPLE_UNSIGNED;
 
2120
                return 0;
 
2121
        }
 
2122
        
 
2123
        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
 
2124
        return -1;
 
2125
}
 
2126
 
 
2127
 
 
2128
/* sample loading */
 
2129
 
 
2130
int
 
2131
load_sample (struct _patch *sample_patch) {
 
2132
        unsigned char *gus_patch;
 
2133
        unsigned long int gus_size;
 
2134
        unsigned long int gus_ptr;
 
2135
        unsigned char no_of_samples;
 
2136
        struct _sample *gus_sample = NULL;
 
2137
        unsigned long int i = 0;
 
2138
        
 
2139
        int (*do_convert[])(unsigned char *data, struct _sample *gus_sample ) = {
 
2140
                convert_8s,
 
2141
                convert_16s,
 
2142
                convert_8u,
 
2143
                convert_16u,
 
2144
                convert_8sp,
 
2145
                convert_16sp,
 
2146
                convert_8up,
 
2147
                convert_16up,
 
2148
                convert_8sr,
 
2149
                convert_16sr,
 
2150
                convert_8ur,
 
2151
                convert_16ur,
 
2152
                convert_8srp,
 
2153
                convert_16srp,
 
2154
                convert_8urp,
 
2155
                convert_16urp
 
2156
        };
 
2157
        unsigned long int tmp_loop;
 
2158
 
 
2159
        SAMPLE_CONVERT_DEBUG(__FUNCTION__);
 
2160
        SAMPLE_CONVERT_DEBUG(sample_patch->filename);
 
2161
        sample_patch->loaded = 1;
 
2162
        if ((gus_patch = WM_BufferFile(sample_patch->filename,&gus_size)) == NULL) {
 
2163
                return -1;
 
2164
        }
 
2165
        if (gus_size < 239) {
 
2166
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(too short)", 0);
 
2167
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, sample_patch->filename, 0);
 
2168
                free(gus_patch);
 
2169
                return -1;
 
2170
        }
 
2171
        if (memcmp(gus_patch, "GF1PATCH110\0ID#000002", 22) && memcmp(gus_patch, "GF1PATCH100\0ID#000002", 22)) {
 
2172
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID,"(unsupported format)", 0);
 
2173
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, sample_patch->filename, 0);
 
2174
                free(gus_patch);
 
2175
                return -1;
 
2176
        }
 
2177
        if (gus_patch[82] > 1) {
 
2178
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID,"(unsupported format)", 0);
 
2179
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, sample_patch->filename, 0);
 
2180
                free(gus_patch);
 
2181
                return -1;
 
2182
        }
 
2183
        if (gus_patch[151] > 1) {
 
2184
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID,"(unsupported format)", 0);
 
2185
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, sample_patch->filename, 0);
 
2186
                free(gus_patch);
 
2187
                return -1;
 
2188
        }
 
2189
                                        
 
2190
        no_of_samples = gus_patch[198];
 
2191
        sample_patch->first_sample = NULL;
 
2192
        gus_ptr = 239;
 
2193
        while (no_of_samples) {
 
2194
                unsigned long int tmp_cnt;
 
2195
                if (sample_patch->first_sample == NULL) {
 
2196
                        sample_patch->first_sample = malloc(sizeof(struct _sample));
 
2197
                        gus_sample = sample_patch->first_sample;
 
2198
                } else {
 
2199
                        gus_sample->next = malloc(sizeof(struct _sample));
 
2200
                        gus_sample = gus_sample->next;
 
2201
                }
 
2202
                if (gus_sample == NULL) {
 
2203
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, 0);
 
2204
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, sample_patch->filename, 0);
 
2205
                        return -1;
 
2206
                }
 
2207
 
 
2208
                gus_sample->next = NULL;
 
2209
                gus_sample->loop_fraction = gus_patch[gus_ptr+7];
 
2210
                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];
 
2211
                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];
 
2212
                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];
 
2213
                gus_sample->rate = (gus_patch[gus_ptr+21] << 8) | gus_patch[gus_ptr+20];
 
2214
                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]);
 
2215
                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]);
 
2216
                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]);
 
2217
 
 
2218
                /* This is done this way instead of ((freq * 1024) / rate) to avoid 32bit overflow. */
 
2219
                /* Result is 0.001% inacurate */
 
2220
                gus_sample->inc_div = ((gus_sample->freq_root * 512) / gus_sample->rate) * 2;
 
2221
 
 
2222
#if 0
 
2223
                printf("\rTremolo Sweep: %i, Rate: %i, Depth %i\n", 
 
2224
                        gus_patch[gus_ptr+49], gus_patch[gus_ptr+50], gus_patch[gus_ptr+51]);
 
2225
                printf("\rVibrato Sweep: %i, Rate: %i, Depth %i\n", 
 
2226
                        gus_patch[gus_ptr+52], gus_patch[gus_ptr+53], gus_patch[gus_ptr+54]);
 
2227
#endif                  
 
2228
                gus_sample->modes = gus_patch[gus_ptr+55] & 0x7F;
 
2229
                if ((sample_patch->remove & SAMPLE_SUSTAIN) && (gus_sample->modes & SAMPLE_SUSTAIN)) {
 
2230
                        gus_sample->modes ^= SAMPLE_SUSTAIN;
 
2231
                } 
 
2232
                if (sample_patch->patchid & 0x0080) {
 
2233
                        if (!(sample_patch->keep & SAMPLE_LOOP)) {
 
2234
                                gus_sample->modes &= 0xFB;
 
2235
                        }
 
2236
                        if (!(sample_patch->keep & SAMPLE_ENVELOPE)) {
 
2237
                                gus_sample->modes &= 0xBF;
 
2238
                        }                       
 
2239
                }
 
2240
 
 
2241
 
 
2242
                if (gus_sample->loop_start > gus_sample->loop_end) {
 
2243
                        tmp_loop = gus_sample->loop_end;
 
2244
                        gus_sample->loop_end = gus_sample->loop_start;
 
2245
                        gus_sample->loop_start = tmp_loop;
 
2246
                        gus_sample->loop_fraction  = ((gus_sample->loop_fraction & 0x0f) << 4) | ((gus_sample->loop_fraction & 0xf0) >> 4);
 
2247
                }
 
2248
                for (i = 0; i < 6; i++) {
 
2249
                        if (gus_sample->modes & SAMPLE_ENVELOPE) {
 
2250
                                unsigned char env_rate = gus_patch[gus_ptr+37+i];
 
2251
                                if (sample_patch->env[i].set & 0x02) {
 
2252
                                        gus_sample->env_target[i] = 16448 * (unsigned long int)(255.0 * sample_patch->env[i].level);
 
2253
                                } else {
 
2254
                                        gus_sample->env_target[i] = 16448 * gus_patch[gus_ptr+43+i];
 
2255
                                }
 
2256
 
 
2257
                                if (sample_patch->env[i].set & 0x01) {
 
2258
                                        gus_sample->env_rate[i]  = (unsigned long int)(4194303.0 / ((float)WM_SampleRate * (sample_patch->env[i].time / 1000.0)));
 
2259
                                } else {
 
2260
                                        gus_sample->env_rate[i]  = (unsigned long int)(4194303.0 / ((float)WM_SampleRate * env_time_table[env_rate]));
 
2261
                                        if (gus_sample->env_rate[i] == 0) {
 
2262
                                                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]);
 
2263
                                                gus_sample->env_rate[i]  = (unsigned long int)(4194303.0 / ((float)WM_SampleRate * env_time_table[63]));
 
2264
                                        }
 
2265
                                }
 
2266
                        } else {
 
2267
                                gus_sample->env_target[i] = 4194303;
 
2268
                                gus_sample->env_rate[i]  = (unsigned long int)(4194303.0 / ((float)WM_SampleRate * env_time_table[63]));
 
2269
                        }
 
2270
                }
 
2271
                
 
2272
                gus_sample->env_target[6] = 0;
 
2273
                gus_sample->env_rate[6]  = (unsigned long int)(4194303.0 / ((float)WM_SampleRate * env_time_table[63]));
 
2274
        
 
2275
                if ((sample_patch->patchid == 47) && (!(gus_sample->modes & SAMPLE_LOOP))) {
 
2276
                        for (i = 3; i < 6; i++) {
 
2277
                                gus_sample->env_target[i] = gus_sample->env_target[2];
 
2278
                                gus_sample->env_rate[i] = gus_sample->env_rate[2];
 
2279
                        }
 
2280
                }
 
2281
                
 
2282
                gus_ptr += 96;
 
2283
                tmp_cnt = gus_sample->data_length;
 
2284
 
 
2285
/* convert to float */
 
2286
                gus_sample->min_peek = 0;
 
2287
                gus_sample->max_peek = 0;
 
2288
                
 
2289
                if (do_convert[(((gus_sample->modes & 0x18) >> 1)| (gus_sample->modes & 0x03))](&gus_patch[gus_ptr], gus_sample) == -1) {
 
2290
                        return -1;
 
2291
                        
 
2292
                };
 
2293
 
 
2294
                if (gus_sample->max_peek > (-gus_sample->min_peek)) {
 
2295
                        gus_sample->peek_adjust = 33553408 / gus_sample->max_peek;
 
2296
                } else {
 
2297
                        gus_sample->peek_adjust = 33554432 / (-gus_sample->min_peek);
 
2298
                }
 
2299
                gus_sample->peek_adjust = (gus_sample->peek_adjust * sample_patch->amp) >> 10;
 
2300
                
 
2301
                gus_ptr += tmp_cnt;
 
2302
                gus_sample->loop_start = (gus_sample->loop_start << 10) | (((gus_sample->loop_fraction & 0x0f) << 10) / 16);
 
2303
                gus_sample->loop_end = (gus_sample->loop_end << 10) | (((gus_sample->loop_fraction & 0xf0) << 6) / 16);
 
2304
                gus_sample->loop_size = gus_sample->loop_end - gus_sample->loop_start;
 
2305
                gus_sample->data_length = gus_sample->data_length << 10;
 
2306
                no_of_samples--;
 
2307
        }
 
2308
        free(gus_patch);
 
2309
        return 0;
 
2310
}
 
2311
 
 
2312
 
 
2313
struct _patch *
 
2314
get_patch_data(struct _mdi *mdi, unsigned short patchid) {
 
2315
        struct _patch *search_patch;
 
2316
        
 
2317
        WM_Lock(&patch_lock);
 
2318
                
 
2319
        search_patch = patch[patchid & 0x007F];
 
2320
 
 
2321
        if (search_patch == NULL) {
 
2322
                WM_Unlock(&patch_lock);
 
2323
                return NULL;
 
2324
        }
 
2325
 
 
2326
        while(search_patch != NULL) {
 
2327
                if (search_patch->patchid == patchid) {
 
2328
                        WM_Unlock(&patch_lock);
 
2329
                        return search_patch;
 
2330
                }
 
2331
                search_patch = search_patch->next;
 
2332
        }
 
2333
        if ((patchid >> 8) != 0) {
 
2334
                WM_Unlock(&patch_lock);
 
2335
                return (get_patch_data(mdi, patchid & 0x00FF));
 
2336
        }
 
2337
        WM_Unlock(&patch_lock);
 
2338
        return NULL;
 
2339
}
 
2340
 
 
2341
void
 
2342
load_patch (struct _mdi *mdi, unsigned short patchid) {
 
2343
        int i;
 
2344
        struct _patch *tmp_patch = NULL;
 
2345
 
 
2346
        for (i = 0; i < mdi->patch_count; i++) {
 
2347
                if (mdi->patches[i]->patchid == patchid) {
 
2348
                        return;
 
2349
                }
 
2350
        }
 
2351
                
 
2352
        tmp_patch = get_patch_data(mdi, patchid);
 
2353
        if (tmp_patch == NULL) {
 
2354
                return;
 
2355
        }
 
2356
        
 
2357
        WM_Lock(&patch_lock);
 
2358
        if (!tmp_patch->loaded) {
 
2359
                if (load_sample(tmp_patch) == -1) {
 
2360
                        WM_Unlock(&patch_lock);
 
2361
                        return;
 
2362
                }
 
2363
        }
 
2364
        
 
2365
        if (tmp_patch->first_sample == NULL) {
 
2366
                WM_Unlock(&patch_lock);
 
2367
                return;
 
2368
        }
 
2369
        
 
2370
        mdi->patch_count++;
 
2371
        mdi->patches = realloc(mdi->patches, (sizeof(struct _patch) * mdi->patch_count));
 
2372
        mdi->patches[mdi->patch_count -1] = tmp_patch;
 
2373
        tmp_patch->inuse_count++;
 
2374
        WM_Unlock(&patch_lock);
 
2375
        return;
 
2376
}
 
2377
 
 
2378
 
 
2379
struct _sample *
 
2380
get_sample_data (struct _patch *sample_patch, unsigned long int freq) {
 
2381
        struct _sample *last_sample = NULL;
 
2382
        struct _sample *return_sample = NULL;
 
2383
        
 
2384
        WM_Lock(&patch_lock);
 
2385
        if (sample_patch == NULL) {
 
2386
                WM_Unlock(&patch_lock);
 
2387
                return NULL;
 
2388
        }
 
2389
        if (sample_patch->first_sample == NULL) {
 
2390
                WM_Unlock(&patch_lock);
 
2391
                return NULL;
 
2392
        }
 
2393
        if (freq == 0) {
 
2394
                WM_Unlock(&patch_lock);
 
2395
                return sample_patch->first_sample;
 
2396
        }
 
2397
        
 
2398
        return_sample = sample_patch->first_sample;
 
2399
        last_sample = sample_patch->first_sample;
 
2400
        while (last_sample != NULL) {
 
2401
                if (freq > last_sample->freq_low) {
 
2402
                        if (freq < last_sample->freq_high) {
 
2403
                                WM_Unlock(&patch_lock);
 
2404
                                return last_sample;
 
2405
                        } else {
 
2406
                                return_sample = last_sample;
 
2407
                        }
 
2408
                }
 
2409
                last_sample = last_sample->next;
 
2410
        }
 
2411
        WM_Unlock(&patch_lock);
 
2412
        return return_sample;
 
2413
}
 
2414
 
 
2415
unsigned long int
 
2416
read_var_length (struct _mdi *mdi, struct _miditrack *track) {
 
2417
        unsigned long int var_data = 0;
 
2418
        if (mdi->data[track->ptr] > 0x7f) {
 
2419
                while(mdi->data[track->ptr] > 0x7f) {
 
2420
                        var_data |= mdi->data[track->ptr] & 0x7f;
 
2421
                        track->ptr++;
 
2422
                        if (track->ptr > mdi->size) {
 
2423
                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(too short)", 0);
 
2424
                                return 0xFFFFFFFF;
 
2425
                        }
 
2426
                        var_data = (var_data << 7);
 
2427
                }
 
2428
        }
 
2429
        var_data |= mdi->data[track->ptr] & 0x7f;
 
2430
        track->ptr++;
 
2431
        
 
2432
        if (track->ptr > mdi->size) {
 
2433
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(too short)", 0);
 
2434
                return 0xFFFFFFFF;
 
2435
        }
 
2436
        
 
2437
        return var_data;
 
2438
}
 
2439
 
 
2440
void
 
2441
do_note_off (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
 
2442
        struct _note *nte;
 
2443
 
 
2444
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
2445
 
 
2446
        nte = &mdi->note_table[0][ch][mdi->data[ptr]];
 
2447
        if (!nte->active)
 
2448
                nte = &mdi->note_table[1][ch][mdi->data[ptr]];
 
2449
        if (!nte->active) {
 
2450
                return;
 
2451
        }
 
2452
 
 
2453
        if ((ch == 9) && (!(nte->modes & SAMPLE_LOOP))) {
 
2454
                return;
 
2455
        }
 
2456
 
 
2457
        if (nte->hold) {
 
2458
                nte->hold |= HOLD_OFF;
 
2459
        } else {
 
2460
#if 0
 
2461
                if (nte->modes & SAMPLE_SUSTAIN) {
 
2462
                        nte->env = 3;
 
2463
                        if (nte->env_level > nte->sample->env_target[3]) {
 
2464
                                nte->env_inc = -nte->sample->env_rate[3];
 
2465
                        } else {
 
2466
                                nte->env_inc = nte->sample->env_rate[3];
 
2467
                        }
 
2468
                } else 
 
2469
#endif
 
2470
                {
 
2471
                        if (nte->env < 4) {
 
2472
                                nte->env = 4;
 
2473
                                if (nte->env_level > nte->sample->env_target[4]) {
 
2474
                                        nte->env_inc = -nte->sample->env_rate[4];
 
2475
                                } else {
 
2476
                                        nte->env_inc = nte->sample->env_rate[4];
 
2477
                                }                       
 
2478
                        }
 
2479
                }
 
2480
        }
 
2481
        return;
 
2482
}
 
2483
 
 
2484
inline unsigned long int
 
2485
get_inc (struct _mdi *mdi, struct _note *nte) {
 
2486
        int ch = nte->noteid >> 8;
 
2487
        signed long int note_f;
 
2488
        unsigned long int freq;
 
2489
        
 
2490
        if (__builtin_expect((nte->patch->note != 0),0)) {
 
2491
                note_f = nte->patch->note * 100;
 
2492
        } else {
 
2493
                note_f = (nte->noteid & 0x7f) * 100;
 
2494
        }
 
2495
        note_f += mdi->channel[ch].pitch_adjust;
 
2496
        if (__builtin_expect((note_f < 0), 0)) {
 
2497
                note_f = 0;
 
2498
        } else if (__builtin_expect((note_f > 12700), 0)) {
 
2499
                note_f = 12700;
 
2500
        }
 
2501
        freq = freq_table[(note_f % 1200)] >> (10 - (note_f / 1200));
 
2502
        return (((freq / ((WM_SampleRate * 100) / 1024)) * 1024 / nte->sample->inc_div));
 
2503
}
 
2504
 
 
2505
inline signed short int
 
2506
get_volume(struct _mdi *mdi, unsigned char ch, struct _note *nte) {
 
2507
        signed long int volume;
 
2508
        
 
2509
        if (mdi->info.mixer_options & WM_MO_LINEAR_VOLUME) {
 
2510
                volume = (lin_volume[mdi->channel[ch].volume] * 
 
2511
                        lin_volume[mdi->channel[ch].expression] *
 
2512
                        lin_volume[nte->velocity]) / 1048576;
 
2513
        } else {
 
2514
                volume = (sqr_volume[mdi->channel[ch].volume] *
 
2515
                        sqr_volume[mdi->channel[ch].expression] *
 
2516
                        sqr_volume[nte->velocity]) / 1048576;
 
2517
        }
 
2518
        return ((volume * nte->sample->peek_adjust) >> 10);
 
2519
}
 
2520
 
 
2521
void
 
2522
do_note_on (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
 
2523
        struct _note *nte;
 
2524
        unsigned long int freq = 0;
 
2525
        struct _patch *patch;
 
2526
        struct _sample *sample;
 
2527
        
 
2528
        if (mdi->data[ptr+1] == 0x00) {
 
2529
                do_note_off(ch, mdi, ptr);
 
2530
                return;
 
2531
        }
 
2532
        
 
2533
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
2534
 
 
2535
        if (ch != 9) {
 
2536
                patch = mdi->channel[ch].patch;
 
2537
                if (patch == NULL) {
 
2538
                        return;
 
2539
                }
 
2540
                freq = freq_table[(mdi->data[ptr] % 12) * 100] >> (10 -(mdi->data[ptr] / 12));
 
2541
        } else {
 
2542
                patch = get_patch_data(mdi, ((mdi->channel[ch].bank << 8) | mdi->data[ptr] | 0x80));
 
2543
                if (patch == NULL) {
 
2544
                        return;
 
2545
                }
 
2546
                if (patch->note) {
 
2547
                        freq = freq_table[(patch->note % 12) * 100] >> (10 -(patch->note / 12));
 
2548
                } else {
 
2549
                        freq = freq_table[(mdi->data[ptr] % 12) * 100] >> (10 -(mdi->data[ptr] / 12));
 
2550
                }
 
2551
        }
 
2552
 
 
2553
        sample = get_sample_data(patch, (freq / 100));
 
2554
 
 
2555
        if (sample == NULL) {
 
2556
                return;
 
2557
        }
 
2558
 
 
2559
        nte = &mdi->note_table[0][ch][mdi->data[ptr]];
 
2560
        
 
2561
        if (nte->active) {
 
2562
                if ((nte->modes & SAMPLE_ENVELOPE) && (nte->env < 3) && (!(nte->hold & HOLD_OFF)))
 
2563
                        return;
 
2564
                nte->next = &mdi->note_table[1][ch][mdi->data[ptr]];
 
2565
                nte->env = 6;
 
2566
                nte->env_inc = -nte->sample->env_rate[6];
 
2567
                nte = &mdi->note_table[1][ch][mdi->data[ptr]];
 
2568
        } else {
 
2569
                if (mdi->note_table[1][ch][mdi->data[ptr]].active) {
 
2570
                        if ((nte->modes & SAMPLE_ENVELOPE) && (nte->env < 3) && (!(nte->hold & HOLD_OFF)))
 
2571
                                return;
 
2572
                        mdi->note_table[1][ch][mdi->data[ptr]].next = nte;
 
2573
                        mdi->note_table[1][ch][mdi->data[ptr]].env = 6;
 
2574
                        mdi->note_table[1][ch][mdi->data[ptr]].env_inc = -mdi->note_table[1][ch][mdi->data[ptr]].sample->env_rate[6];
 
2575
                } else {
 
2576
                        *mdi->last_note = nte;
 
2577
                        mdi->last_note++;
 
2578
                        nte->active = 1;
 
2579
                }
 
2580
        }
 
2581
        nte->noteid = (ch << 8) | mdi->data[ptr];
 
2582
        nte->patch = patch;
 
2583
        nte->sample = sample;
 
2584
        nte->sample_pos = 0;
 
2585
        nte->sample_inc = get_inc (mdi, nte);
 
2586
        nte->velocity = mdi->data[ptr+1];
 
2587
        nte->env = 0;
 
2588
        nte->env_inc = nte->sample->env_rate[0];
 
2589
        nte->env_level = 0;
 
2590
        nte->modes = sample->modes;
 
2591
        nte->hold = mdi->channel[ch].hold;
 
2592
        nte->vol_lvl = get_volume(mdi, ch, nte);
 
2593
        nte->next = NULL;
 
2594
}
 
2595
 
 
2596
void
 
2597
do_aftertouch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
 
2598
        struct _note *nte;
 
2599
 
 
2600
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
2601
 
 
2602
        nte = &mdi->note_table[0][ch][mdi->data[ptr]];
 
2603
        if (!nte->active) {
 
2604
                nte = &mdi->note_table[1][ch][mdi->data[ptr]];
 
2605
                if (!nte->active) {
 
2606
                        return;
 
2607
                }
 
2608
        }
 
2609
        
 
2610
        nte->velocity = mdi->data[ptr+1];
 
2611
        nte->vol_lvl = get_volume(mdi, ch, nte);
 
2612
        
 
2613
        if (nte->next) {
 
2614
                nte->next->velocity = mdi->data[ptr+1];
 
2615
                nte->next->vol_lvl = get_volume(mdi, ch, nte->next);
 
2616
        }
 
2617
}
 
2618
 
 
2619
 
 
2620
void
 
2621
do_pan_adjust (struct _mdi *mdi, unsigned char ch) {
 
2622
        signed short int pan_adjust = mdi->channel[ch].balance + mdi->channel[ch].pan;
 
2623
        signed long int left, right;
 
2624
 
 
2625
        if (pan_adjust > 63) {
 
2626
                pan_adjust = 63;
 
2627
        } else if (pan_adjust < -64) {
 
2628
                pan_adjust = -64;
 
2629
        }
 
2630
 
 
2631
        pan_adjust += 64;
 
2632
 
 
2633
        if (mdi->info.mixer_options & WM_MO_LINEAR_VOLUME) {
 
2634
                left = (lin_volume[127 - pan_adjust] * WM_MasterVolume * mdi->amp) / 1048576;
 
2635
                right= (lin_volume[pan_adjust] * WM_MasterVolume * mdi->amp) / 1048576;
 
2636
        } else {
 
2637
                left =  (pan_volume[127 - pan_adjust] * WM_MasterVolume * mdi->amp) / 1048576;
 
2638
                right = (pan_volume[pan_adjust] * WM_MasterVolume * mdi->amp) / 1048576;
 
2639
        }
 
2640
 
 
2641
        mdi->channel[ch].left_adjust = left;
 
2642
        mdi->channel[ch].right_adjust = right;
 
2643
}
 
2644
 
 
2645
void
 
2646
do_control (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
 
2647
        struct _note **note_data = mdi->note;
 
2648
        
 
2649
        switch (mdi->data[ptr]) {
 
2650
                case 0: // Bank Select
 
2651
                        mdi->channel[ch].bank = mdi->data[ptr+1];
 
2652
                        break;
 
2653
                case 1:
 
2654
                case 2:
 
2655
                case 3:
 
2656
                case 4:
 
2657
                case 5:
 
2658
                        break;
 
2659
                case 6: // Data Entry Course
 
2660
                        if (mdi->channel[ch].reg_data == 0x0000) { // Pitch Bend Range
 
2661
                                int data_tmp;
 
2662
                                data_tmp = mdi->channel[ch].pitch_range % 100;
 
2663
                                mdi->channel[ch].pitch_range = mdi->data[ptr+1] * 100 + data_tmp;
 
2664
                        }
 
2665
                        break;
 
2666
                case 7: // Channel Volume
 
2667
                        mdi->channel[ch].volume = mdi->data[ptr+1];     
 
2668
        
 
2669
                        if (note_data != mdi->last_note) {
 
2670
                                do {
 
2671
                                        if (((*note_data)->noteid >> 8) == ch) {
 
2672
                                                (*note_data)->vol_lvl = get_volume(mdi, ch, *note_data);
 
2673
                                                if ((*note_data)->next)
 
2674
                                                        (*note_data)->next->vol_lvl = get_volume(mdi, ch, (*note_data)->next);
 
2675
                                        }
 
2676
                                        note_data++;
 
2677
                                } while (note_data != mdi->last_note);
 
2678
                        }
 
2679
                        break;
 
2680
                case 8: // Channel Balance
 
2681
                        mdi->channel[ch].balance = mdi->data[ptr+1] - 64;
 
2682
                        do_pan_adjust(mdi, ch);
 
2683
                        break;
 
2684
                case 9:
 
2685
                        break;
 
2686
                case 10: // Channel Pan
 
2687
                        mdi->channel[ch].pan = mdi->data[ptr+1] - 64;
 
2688
                        do_pan_adjust(mdi, ch);
 
2689
                        break;
 
2690
                case 11: // Channel Expression
 
2691
                        mdi->channel[ch].expression = mdi->data[ptr+1];
 
2692
        
 
2693
                        if (note_data != mdi->last_note) {
 
2694
                                do {
 
2695
                                        if (((*note_data)->noteid >> 8) == ch) {
 
2696
                                                (*note_data)->vol_lvl = get_volume(mdi, ch, *note_data);
 
2697
                                                if ((*note_data)->next)
 
2698
                                                        (*note_data)->next->vol_lvl = get_volume(mdi, ch, (*note_data)->next);
 
2699
                                        }
 
2700
                                        note_data++;
 
2701
                                } while (note_data != mdi->last_note);
 
2702
                        }
 
2703
                        break;
 
2704
                case 12:
 
2705
                case 13:
 
2706
                case 14:
 
2707
                case 15:
 
2708
                case 16:
 
2709
                case 17:
 
2710
                case 18:
 
2711
                case 19:
 
2712
                case 20:
 
2713
                case 21:
 
2714
                case 22:
 
2715
                case 23:
 
2716
                case 24:
 
2717
                case 25:
 
2718
                case 26:
 
2719
                case 27:
 
2720
                case 28:
 
2721
                case 29:
 
2722
                case 30:
 
2723
                case 31:
 
2724
                case 32:
 
2725
                case 33:
 
2726
                case 34:
 
2727
                case 35:
 
2728
                case 36:
 
2729
                case 37:
 
2730
                        break;
 
2731
                case 38: // Data Entry Fine
 
2732
                        if (mdi->channel[ch].reg_data == 0x0000) { // Pitch Bend Range
 
2733
                                int data_tmp;
 
2734
                                data_tmp = mdi->channel[ch].pitch_range / 100;
 
2735
                                mdi->channel[ch].pitch_range = (data_tmp * 100) + mdi->data[ptr+1];
 
2736
                        }
 
2737
                        break;
 
2738
                case 39:
 
2739
                case 40:
 
2740
                case 41:
 
2741
                case 42:
 
2742
                case 43:
 
2743
                        printf("\rController %i used\n",mdi->data[ptr]);
 
2744
                        break;
 
2745
                case 44:
 
2746
                case 45:
 
2747
                case 46:
 
2748
                case 47:
 
2749
                case 48:
 
2750
                case 49:
 
2751
                case 50:
 
2752
                case 51:
 
2753
                case 52:
 
2754
                case 53:
 
2755
                case 54:
 
2756
                case 55:
 
2757
                case 56:
 
2758
                case 57:
 
2759
                case 58:
 
2760
                case 59:
 
2761
                case 60:
 
2762
                case 61:
 
2763
                case 62:
 
2764
                case 63:
 
2765
                        break;
 
2766
                case 64: // Channel Hold
 
2767
                        if (mdi->data[ptr+1] > 63) {
 
2768
                                mdi->channel[ch].hold = 1;
 
2769
                        } else {
 
2770
                                mdi->channel[ch].hold = 0;
 
2771
                                if (note_data != mdi->last_note) {
 
2772
                                        do {
 
2773
                                                if (((*note_data)->noteid >> 8) == ch) {
 
2774
                                                        if ((*note_data)->hold & HOLD_OFF) {
 
2775
                                                                if ((*note_data)->modes & SAMPLE_ENVELOPE) {
 
2776
#if 0   
 
2777
                                                                        if ((*note_data)->modes & SAMPLE_SUSTAIN) {
 
2778
                                                                                (*note_data)->env = 3;
 
2779
                                                                                if ((*note_data)->env_level > (*note_data)->sample->env_target[3]) {
 
2780
                                                                                        (*note_data)->env_inc = -(*note_data)->sample->env_rate[3];
 
2781
                                                                                } else {
 
2782
                                                                                        (*note_data)->env_inc = (*note_data)->sample->env_rate[3];
 
2783
                                                                                }       
 
2784
                                                                        } else 
 
2785
#endif
 
2786
                                                                        {
 
2787
                                                                                if ((*note_data)->env < 4) {
 
2788
                                                                                        (*note_data)->env = 4;
 
2789
                                                                                        if ((*note_data)->env_level > (*note_data)->sample->env_target[4]) {
 
2790
                                                                                                (*note_data)->env_inc = -(*note_data)->sample->env_rate[4];
 
2791
                                                                                        } else {
 
2792
                                                                                                (*note_data)->env_inc = (*note_data)->sample->env_rate[4];
 
2793
                                                                                        }
 
2794
                                                                                }
 
2795
                                                                        }
 
2796
                                                                }
 
2797
                                                        }
 
2798
                                                        (*note_data)->hold = 0x00;
 
2799
                                                }
 
2800
                                                note_data++;
 
2801
                                        } while (note_data != mdi->last_note);
 
2802
                                }
 
2803
                        }
 
2804
                        break;
 
2805
                case 65:
 
2806
                case 66:
 
2807
                case 67:
 
2808
                case 68:
 
2809
                case 69:
 
2810
                case 70:
 
2811
                case 71:
 
2812
                case 72:
 
2813
                case 73:
 
2814
                case 74:
 
2815
                case 75:
 
2816
                case 76:
 
2817
                case 77:
 
2818
                case 78:
 
2819
                case 79:
 
2820
                case 80:
 
2821
                case 81:
 
2822
                case 82:
 
2823
                case 83:
 
2824
                case 84:
 
2825
                case 85:
 
2826
                case 86:
 
2827
                case 87:
 
2828
                case 88:
 
2829
                case 89:
 
2830
                case 90:
 
2831
                case 91:
 
2832
                case 92:
 
2833
                case 93:
 
2834
                case 94:
 
2835
                case 95:
 
2836
                case 96:
 
2837
                case 97:
 
2838
                case 98:
 
2839
                case 99:
 
2840
                        break;
 
2841
                case 100: // Registered Param Fine
 
2842
                        mdi->channel[ch].reg_data = (mdi->channel[ch].reg_data & 0xFF00) | mdi->data[ptr+1];
 
2843
                        break;
 
2844
                case 101: // Registered Param Course
 
2845
                        mdi->channel[ch].reg_data = (mdi->channel[ch].reg_data & 0xFF) | (mdi->data[ptr+1] << 8);
 
2846
                        break;
 
2847
                case 102:
 
2848
                case 103:
 
2849
                case 104:
 
2850
                case 105:
 
2851
                case 106:
 
2852
                case 107:
 
2853
                case 108:
 
2854
                case 109:
 
2855
                case 110:
 
2856
                case 111:
 
2857
                case 112:
 
2858
                case 113:
 
2859
                case 114:
 
2860
                case 115:
 
2861
                case 116:
 
2862
                case 117:
 
2863
                case 118:
 
2864
                case 119:
 
2865
                        break;
 
2866
                case 120: // All Channel Sound Off
 
2867
                        if (note_data != mdi->last_note) {
 
2868
                                do {
 
2869
                                        if (((*note_data)->noteid >> 8) == ch) {
 
2870
                                                (*note_data)->active = 0;
 
2871
                                                if ((*note_data)->next) {
 
2872
                                                        (*note_data)->next = NULL;
 
2873
                                                }
 
2874
                                        }
 
2875
                                        note_data++;
 
2876
                                } while (note_data != mdi->last_note);
 
2877
                                mdi->last_note = mdi->note;
 
2878
                        }
 
2879
                        break;
 
2880
                case 121: // All Controlers Off
 
2881
                        mdi->channel[ch].expression = 127;
 
2882
                        mdi->channel[ch].pressure = 0;
 
2883
                        mdi->channel[ch].volume = 100;
 
2884
                        mdi->channel[ch].pan = 0;
 
2885
                        mdi->channel[ch].balance = 0;
 
2886
                        mdi->channel[ch].reg_data = 0xffff;
 
2887
                        mdi->channel[ch].pitch_range = 200;
 
2888
                        mdi->channel[ch].pitch = 0;
 
2889
                        mdi->channel[ch].pitch_adjust = 0;
 
2890
                        mdi->channel[ch].hold = 0;
 
2891
                        do_pan_adjust(mdi, ch);
 
2892
                                                
 
2893
                        if (note_data != mdi->last_note) {
 
2894
                                do {
 
2895
                                        if (((*note_data)->noteid >> 8 ) == ch) {
 
2896
                                                (*note_data)->sample_inc = get_inc (mdi, *note_data);
 
2897
                                                (*note_data)->velocity = 0;
 
2898
                                                (*note_data)->vol_lvl = get_volume(mdi, ch, *note_data);
 
2899
                                                (*note_data)->hold = 0;
 
2900
                                                
 
2901
                                                if ((*note_data)->next) {
 
2902
                                                        (*note_data)->next->velocity = mdi->data[ptr];
 
2903
                                                        (*note_data)->next->vol_lvl = get_volume(mdi, ch, (*note_data)->next);
 
2904
                                                }
 
2905
                                                
 
2906
                                        }
 
2907
                                        note_data++;
 
2908
                                } while (note_data != mdi->last_note);
 
2909
                        }
 
2910
                        break;
 
2911
                case 122:
 
2912
                        break;
 
2913
                case 123: // All Channel Notes Off
 
2914
                        if (ch == 9)
 
2915
                                return;
 
2916
                        if (note_data != mdi->last_note) {
 
2917
                                do {
 
2918
                                        if (((*note_data)->noteid >> 8) == ch) { 
 
2919
                                                if (!(*note_data)->hold){
 
2920
                                                        if ((*note_data)->modes & SAMPLE_ENVELOPE) {
 
2921
                                                                if ((*note_data)->env < 5) {
 
2922
                                                                        if ((*note_data)->env_level > (*note_data)->sample->env_target[5]) {
 
2923
                                                                                (*note_data)->env_inc = -(*note_data)->sample->env_rate[5];
 
2924
                                                                        } else {
 
2925
                                                                                (*note_data)->env_inc = (*note_data)->sample->env_rate[5];
 
2926
                                                                        }
 
2927
                                                                        (*note_data)->env = 5;
 
2928
                                                                }
 
2929
                                                        }
 
2930
                                                } else {
 
2931
                                                        (*note_data)->hold |= HOLD_OFF;
 
2932
                                                }
 
2933
                                        }
 
2934
                                        note_data++;
 
2935
                                } while (note_data != mdi->last_note);
 
2936
                        }
 
2937
                        break;
 
2938
                case 124:
 
2939
                case 125:
 
2940
                case 126:
 
2941
                case 127:
 
2942
                        break;
 
2943
        }
 
2944
}
 
2945
 
 
2946
void
 
2947
do_patch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
 
2948
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
2949
        if (ch != 9) {
 
2950
                mdi->channel[ch].patch = get_patch_data(mdi, ((mdi->channel[ch].bank << 8) | mdi->data[ptr]));
 
2951
        } else {
 
2952
                mdi->channel[ch].bank = mdi->data[ptr];
 
2953
        }
 
2954
}
 
2955
 
 
2956
void
 
2957
do_channel_pressure (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
 
2958
        struct _note **note_data = mdi->note;
 
2959
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
2960
 
 
2961
        if (note_data != mdi->last_note) {
 
2962
                do {
 
2963
                        if (((*note_data)->noteid >> 8 ) == ch) {
 
2964
                                (*note_data)->velocity = mdi->data[ptr];
 
2965
                                (*note_data)->vol_lvl = get_volume(mdi, ch, *note_data);
 
2966
 
 
2967
                                if ((*note_data)->next) {
 
2968
                                        (*note_data)->next->velocity = mdi->data[ptr];
 
2969
                                        (*note_data)->next->vol_lvl = get_volume(mdi, ch, (*note_data)->next);
 
2970
                                }
 
2971
                        }
 
2972
                        note_data++;
 
2973
                } while (note_data != mdi->last_note);
 
2974
        }
 
2975
}
 
2976
 
 
2977
void
 
2978
do_pitch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
 
2979
        struct _note **note_data = mdi->note;
 
2980
 
 
2981
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
2982
        mdi->channel[ch].pitch = ((mdi->data[ptr+1] << 7) | mdi->data[ptr]) - 0x2000;
 
2983
        
 
2984
        if (mdi->channel[ch].pitch < 0) {
 
2985
                mdi->channel[ch].pitch_adjust = mdi->channel[ch].pitch_range * mdi->channel[ch].pitch / 8192;
 
2986
        } else {
 
2987
                mdi->channel[ch].pitch_adjust = mdi->channel[ch].pitch_range * mdi->channel[ch].pitch / 8191;
 
2988
        }
 
2989
 
 
2990
        if (note_data != mdi->last_note) {
 
2991
                do {
 
2992
                        if (((*note_data)->noteid >> 8 ) == ch) {
 
2993
                                (*note_data)->sample_inc = get_inc (mdi, *note_data);
 
2994
                        }
 
2995
                        note_data++;
 
2996
                } while (note_data != mdi->last_note);
 
2997
        }
 
2998
}
 
2999
 
 
3000
void
 
3001
do_message (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
 
3002
        unsigned char event_type = 0xF0 | ch;
 
3003
        static unsigned long int tempo = 500000;
 
3004
        
 
3005
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
3006
        if (event_type == 0xFF) {
 
3007
                if ((mdi->data[ptr] == 0x51) && (mdi->data[ptr+1] == 3)) {
 
3008
                        tempo = (mdi->data[ptr+2] << 16) | (mdi->data[ptr+3] << 8) | mdi->data[ptr+4];
 
3009
                        if (tempo == 0)
 
3010
                                mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
 
3011
                        else    
 
3012
                                mdi->samples_per_delta = (WM_SampleRate << 10) / ((1000000 * mdi->divisions) / tempo);
 
3013
                } 
 
3014
        } 
 
3015
}
 
3016
 
 
3017
 
 
3018
void
 
3019
do_null (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
 
3020
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
3021
};
 
3022
 
 
3023
 
 
3024
void
 
3025
WM_ResetToStart(midi * handle) {
 
3026
        struct _mdi *mdi = (struct _mdi *)handle;
 
3027
        int i;
 
3028
 
 
3029
        mdi->index_count = 0;
 
3030
        mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
 
3031
        mdi->samples_to_mix = 0;
 
3032
        mdi->info.current_sample= 0;    
 
3033
 
 
3034
        for (i=0; i<16; i++) {
 
3035
                mdi->channel[i].bank = 0;
 
3036
                mdi->channel[i].patch = NULL;
 
3037
                mdi->channel[i].hold = 0;
 
3038
                mdi->channel[i].volume = 100;
 
3039
                mdi->channel[i].pressure = 127;
 
3040
                mdi->channel[i].expression = 127;
 
3041
                mdi->channel[i].balance = 0;
 
3042
                mdi->channel[i].pan = 0;
 
3043
                mdi->channel[i].left_adjust = 1.0;
 
3044
                mdi->channel[i].right_adjust = 1.0;
 
3045
                mdi->channel[i].pitch = 0;
 
3046
                mdi->channel[i].pitch_range = 200;
 
3047
                mdi->channel[i].reg_data = 0xFFFF;
 
3048
        }
 
3049
}
 
3050
 
 
3051
void
 
3052
WM_RecalcSamples (midi * handle) {
 
3053
        struct _mdi *mdi = (struct _mdi *)handle;
 
3054
        struct _note **note_data = mdi->note;
 
3055
        unsigned long int total_samples = 0;
 
3056
        unsigned long int count_a;
 
3057
        unsigned long int count_b;
 
3058
        unsigned long int env_level;
 
3059
        
 
3060
        if (note_data != mdi->last_note) {
 
3061
                do {
 
3062
                        env_level = (*note_data)->env_level;
 
3063
                        count_a = 0;
 
3064
                        count_b = 0;
 
3065
                        if ((*note_data)->env < 4) {
 
3066
                                if (env_level > (*note_data)->sample->env_target[3]) {
 
3067
                                        count_a += (env_level - (*note_data)->sample->env_target[3] + (*note_data)->sample->env_rate[3] - 1) / (*note_data)->sample->env_rate[3];
 
3068
                                } else {
 
3069
                                        count_a += ((*note_data)->sample->env_target[3] - env_level + (*note_data)->sample->env_rate[3] - 1) / (*note_data)->sample->env_rate[3];
 
3070
                                }
 
3071
                                env_level = (*note_data)->sample->env_target[3];
 
3072
                        }
 
3073
                        if ((*note_data)->env < 5) {
 
3074
                                if (env_level > (*note_data)->sample->env_target[4]) {
 
3075
                                        count_a += (env_level - (*note_data)->sample->env_target[4] + (*note_data)->sample->env_rate[4] - 1) / (*note_data)->sample->env_rate[4];
 
3076
                                } else {
 
3077
                                        count_a += ((*note_data)->sample->env_target[4] - env_level + (*note_data)->sample->env_rate[4] - 1) / (*note_data)->sample->env_rate[4];
 
3078
                                }
 
3079
                                env_level = (*note_data)->sample->env_target[4];
 
3080
                        }
 
3081
                        if ((*note_data)->env < 6) {
 
3082
                                if (env_level > (*note_data)->sample->env_target[5]) {
 
3083
                                        count_a += (env_level - (*note_data)->sample->env_target[5] + (*note_data)->sample->env_rate[5] - 1) / (*note_data)->sample->env_rate[5];
 
3084
                                } else {
 
3085
                                        count_a += ((*note_data)->sample->env_target[5] - env_level + (*note_data)->sample->env_rate[5] - 1) / (*note_data)->sample->env_rate[5];
 
3086
                                }
 
3087
                                env_level = (*note_data)->sample->env_target[5];
 
3088
                        }
 
3089
                        if ((*note_data)->env == 6) {
 
3090
                                count_a = (env_level + (*note_data)->sample->env_rate[6] - 1) / (*note_data)->sample->env_rate[6];
 
3091
                                env_level = (*note_data)->sample->env_target[6];
 
3092
                        }
 
3093
                        if (env_level != 0) {
 
3094
                                if ((*note_data)->modes & SAMPLE_LOOP) {
 
3095
                                        unsigned long int smpl_pos = (*note_data)->sample_pos + (count_a * (*note_data)->sample_inc);
 
3096
                                        if (smpl_pos > ((*note_data)->sample->loop_end << 10)) {
 
3097
                                                while (smpl_pos > ((*note_data)->sample->loop_end << 10)) {
 
3098
                                                        smpl_pos -= ((*note_data)->sample->loop_end - (*note_data)->sample->loop_start) << 10;
 
3099
                                                }
 
3100
                                                count_a += (((*note_data)->sample->data_length << 10) - smpl_pos + (*note_data)->sample_inc - 1) / (*note_data)->sample_inc;
 
3101
                                        }
 
3102
                                        } else {
 
3103
                                        count_b = (((*note_data)->sample->data_length << 10) - (*note_data)->sample_pos + (*note_data)->sample_inc - 1) / (*note_data)->sample_inc;
 
3104
                                }
 
3105
                                if (count_b != 0) {
 
3106
                                        if (count_b < count_a) {
 
3107
                                                if (total_samples < count_b)
 
3108
                                                         total_samples = count_b;
 
3109
                                        } else {
 
3110
                                                if (total_samples < count_a)
 
3111
                                                        total_samples = count_a;
 
3112
                                        }
 
3113
                                } else {
 
3114
                                        if (total_samples < count_a)
 
3115
                                                        total_samples = count_a;
 
3116
                                }
 
3117
                        } else {
 
3118
                                if (!((*note_data)->modes & SAMPLE_LOOP)) {
 
3119
                                        count_b = (((*note_data)->sample->data_length << 10) - (*note_data)->sample_pos) / (*note_data)->sample_inc;
 
3120
                                        if (count_b < count_a) {
 
3121
                                                if (total_samples < count_b)
 
3122
                                                        total_samples = count_b;
 
3123
                                        } else {
 
3124
                                                if (total_samples < count_a)
 
3125
                                                        total_samples = count_a;
 
3126
                                                }
 
3127
                                } else {
 
3128
                                        if (total_samples < count_a)
 
3129
                                                total_samples = count_a;
 
3130
                                }
 
3131
                        }
 
3132
                note_data++;    
 
3133
                } while (note_data != mdi->last_note);
 
3134
        }
 
3135
        mdi->info.approx_total_samples += total_samples;
 
3136
        mdi->recalc_samples = 0;
 
3137
}
 
3138
 
 
3139
void
 
3140
do_amp_setup_note_off (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
 
3141
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
3142
 
 
3143
        mdi->lin_cur_vol -=  (lin_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] * 
 
3144
                lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
 
3145
        mdi->log_cur_vol -= (sqr_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] * 
 
3146
                log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
 
3147
        
 
3148
        mdi->note_vel[ch][mdi->data[track->ptr]] = 0;
 
3149
                
 
3150
        track->running_event = 0x80 | ch;
 
3151
        track->ptr += 2;
 
3152
        return;
 
3153
}
 
3154
 
 
3155
void
 
3156
do_amp_setup_note_on (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
 
3157
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
3158
        if (mdi->data[track->ptr+1] == 0x00) {
 
3159
                do_amp_setup_note_off(ch, mdi, track);
 
3160
                track->running_event = 0x90 | ch;
 
3161
                return;
 
3162
        }
 
3163
 
 
3164
        if (mdi->note_vel[ch][mdi->data[track->ptr]]) {
 
3165
                mdi->lin_cur_vol -= (lin_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] * 
 
3166
                        lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
 
3167
                mdi->log_cur_vol -= (sqr_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] * 
 
3168
                        log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
 
3169
        }
 
3170
        
 
3171
        mdi->note_vel[ch][mdi->data[track->ptr]] = mdi->data[track->ptr+1];
 
3172
        
 
3173
        mdi->lin_cur_vol += (lin_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] * 
 
3174
                lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
 
3175
        mdi->log_cur_vol += (sqr_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] * 
 
3176
                log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
 
3177
 
 
3178
        if (mdi->lin_cur_vol > mdi->lin_max_vol) {
 
3179
                mdi->lin_max_vol = mdi->lin_cur_vol;
 
3180
        }
 
3181
        if (mdi->log_cur_vol > mdi->log_max_vol) {
 
3182
                mdi->log_max_vol = mdi->log_cur_vol;
 
3183
        }
 
3184
        if (ch == 9) {
 
3185
                load_patch(mdi, ((mdi->channel[ch].bank << 8) | (mdi->data[track->ptr] | 0x80)));
 
3186
        }       
 
3187
        
 
3188
        track->running_event = 0x90 | ch;
 
3189
        track->ptr += 2;
 
3190
        return;
 
3191
}
 
3192
 
 
3193
void
 
3194
do_amp_setup_aftertouch (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
 
3195
        unsigned char pres = mdi->data[track->ptr+1];
 
3196
 
 
3197
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
3198
        
 
3199
        if (pres == 0)
 
3200
                pres = 1;
 
3201
 
 
3202
        if (mdi->note_vel[ch][mdi->data[track->ptr]] != 0) {
 
3203
                mdi->lin_cur_vol -= (lin_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] * 
 
3204
                        lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
 
3205
                mdi->log_cur_vol -= (sqr_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] * 
 
3206
                        log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
 
3207
                        
 
3208
                mdi->note_vel[ch][mdi->data[track->ptr]] = pres;
 
3209
                
 
3210
                mdi->lin_cur_vol += (lin_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] * 
 
3211
                        lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
 
3212
                mdi->log_cur_vol += (sqr_volume[mdi->note_vel[ch][mdi->data[track->ptr]]] * 
 
3213
                        log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
 
3214
 
 
3215
                if (mdi->lin_cur_vol > mdi->lin_max_vol) {
 
3216
                        mdi->lin_max_vol = mdi->lin_cur_vol;
 
3217
                }
 
3218
                if (mdi->log_cur_vol > mdi->log_max_vol) {
 
3219
                        mdi->log_max_vol = mdi->log_cur_vol;
 
3220
                }
 
3221
        }
 
3222
        track->running_event = 0xA0 | ch;
 
3223
        track->ptr += 2;
 
3224
        return;
 
3225
}
 
3226
 
 
3227
 
 
3228
void
 
3229
do_amp_setup_control (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
 
3230
        int i;
 
3231
 
 
3232
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
3233
        if (mdi->data[track->ptr] == 0x00) {
 
3234
                mdi->channel[ch].bank = mdi->data[track->ptr + 1];
 
3235
        } else if (mdi->data[track->ptr] == 0x07) {
 
3236
                for (i=0; i < 128; i++) {
 
3237
                        if (mdi->note_vel[ch][i] == 0)
 
3238
                                continue;
 
3239
                        mdi->lin_cur_vol -= (lin_volume[mdi->note_vel[ch][i]] * 
 
3240
                                lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
 
3241
                        mdi->log_cur_vol -= (sqr_volume[mdi->note_vel[ch][i]] * 
 
3242
                                log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
 
3243
        
 
3244
                        mdi->lin_cur_vol += (lin_volume[mdi->note_vel[ch][i]] * 
 
3245
                                lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->data[track->ptr + 1]]) / 1048576;
 
3246
                        mdi->log_cur_vol += (sqr_volume[mdi->note_vel[ch][i]] * 
 
3247
                                log_volume[mdi->ch_exp[ch]] * log_volume[mdi->data[track->ptr + 1]]) / 1048576;
 
3248
 
 
3249
                }
 
3250
                mdi->ch_vol[ch] = mdi->data[track->ptr + 1];
 
3251
                if (mdi->lin_cur_vol > mdi->lin_max_vol) {
 
3252
                        mdi->lin_max_vol = mdi->lin_cur_vol;
 
3253
                }
 
3254
                if (mdi->log_cur_vol > mdi->log_max_vol) {
 
3255
                        mdi->log_max_vol = mdi->log_cur_vol;
 
3256
                }
 
3257
        } else if (mdi->data[track->ptr] == 0x0B) {
 
3258
                for (i=0; i < 128; i++) {
 
3259
                        if (mdi->note_vel[ch][i] == 0)
 
3260
                                continue;
 
3261
                        mdi->lin_cur_vol -= (lin_volume[mdi->note_vel[ch][i]] * 
 
3262
                                lin_volume[mdi->ch_vol[ch]] * lin_volume[mdi->ch_exp[ch]]) / 1048576;
 
3263
                        mdi->log_cur_vol -= (sqr_volume[mdi->note_vel[ch][i]] * 
 
3264
                                log_volume[mdi->ch_vol[ch]] * log_volume[mdi->ch_exp[ch]]) / 1048576;
 
3265
        
 
3266
                        mdi->lin_cur_vol += (lin_volume[mdi->note_vel[ch][i]] * 
 
3267
                                lin_volume[mdi->ch_vol[ch]] * lin_volume[mdi->data[track->ptr + 1]]) / 1048576;
 
3268
                        mdi->log_cur_vol += (sqr_volume[mdi->note_vel[ch][i]] * 
 
3269
                                log_volume[mdi->ch_vol[ch]] * log_volume[mdi->data[track->ptr + 1]]) / 1048576;
 
3270
 
 
3271
                }
 
3272
                mdi->ch_exp[ch] = mdi->data[track->ptr + 1];
 
3273
                if (mdi->lin_cur_vol > mdi->lin_max_vol) {
 
3274
                        mdi->lin_max_vol = mdi->lin_cur_vol;
 
3275
                }
 
3276
                if (mdi->log_cur_vol > mdi->log_max_vol) {
 
3277
                        mdi->log_max_vol = mdi->log_cur_vol;
 
3278
                }
 
3279
        }
 
3280
 
 
3281
        track->running_event = 0xB0 | ch;
 
3282
        track->ptr += 2;
 
3283
        return;
 
3284
}
 
3285
 
 
3286
void
 
3287
do_amp_setup_patch (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
 
3288
        if (ch == 9) {
 
3289
                mdi->channel[ch].bank = mdi->data[track->ptr];
 
3290
        } else {
 
3291
                load_patch(mdi, ((mdi->channel[ch].bank << 8) | mdi->data[track->ptr]));        
 
3292
        }
 
3293
        track->running_event = 0xC0 | ch;
 
3294
        track->ptr++;
 
3295
        return;
 
3296
}
 
3297
 
 
3298
void
 
3299
do_amp_setup_channel_pressure (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
 
3300
        int i;
 
3301
        unsigned char pres = mdi->data[track->ptr];
 
3302
 
 
3303
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
3304
        
 
3305
        if (pres == 0)
 
3306
                pres = 1;
 
3307
 
 
3308
        for (i=0; i < 128; i++) {
 
3309
                if (mdi->note_vel[ch][i] == 0)
 
3310
                        continue;
 
3311
                mdi->lin_cur_vol -= (lin_volume[mdi->note_vel[ch][i]] * 
 
3312
                        lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
 
3313
                mdi->log_cur_vol -= (sqr_volume[mdi->note_vel[ch][i]] * 
 
3314
                        log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
 
3315
                        
 
3316
                mdi->note_vel[ch][i] = pres;
 
3317
                
 
3318
                mdi->lin_cur_vol += (lin_volume[mdi->note_vel[ch][i]] * 
 
3319
                        lin_volume[mdi->ch_exp[ch]] * lin_volume[mdi->ch_vol[ch]]) / 1048576;
 
3320
                mdi->log_cur_vol += (sqr_volume[mdi->note_vel[ch][i]] * 
 
3321
                        log_volume[mdi->ch_exp[ch]] * log_volume[mdi->ch_vol[ch]]) / 1048576;
 
3322
 
 
3323
        }
 
3324
        if (mdi->lin_cur_vol > mdi->lin_max_vol) {
 
3325
                mdi->lin_max_vol = mdi->lin_cur_vol;
 
3326
        }
 
3327
        if (mdi->log_cur_vol > mdi->log_max_vol) {
 
3328
                mdi->log_max_vol = mdi->log_cur_vol;
 
3329
        }
 
3330
        track->running_event = 0xD0 | ch;
 
3331
        track->ptr++;
 
3332
        return;
 
3333
}
 
3334
 
 
3335
void
 
3336
do_amp_setup_pitch (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
 
3337
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
3338
        track->running_event = 0xE0 | ch;
 
3339
        track->ptr += 2;
 
3340
        return;
 
3341
}
 
3342
 
 
3343
void
 
3344
do_amp_setup_message (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
 
3345
        unsigned long int event_length;
 
3346
        unsigned char event_type = 0xF0 | ch;
 
3347
        unsigned char event_data = mdi->data[track->ptr];
 
3348
        static unsigned long int tempo = 500000;
 
3349
        
 
3350
        MIDI_EVENT_DEBUG(__FUNCTION__,ch);
 
3351
        if (event_type == 0xF0) {
 
3352
                track->running_event = 0x00;
 
3353
                do {
 
3354
                        track->ptr++;
 
3355
                } while ( mdi->data[track->ptr] != 0xF7);
 
3356
                track->ptr++;
 
3357
        } else {
 
3358
                track->ptr += 1;
 
3359
                event_length = read_var_length(mdi, track);
 
3360
                if (event_length == 0xFFFFFFFF) {
 
3361
                        track->delta = 0xFFFFFFFF;
 
3362
                        return;
 
3363
                }
 
3364
                if (event_type == 0xFF) {
 
3365
                        if ((event_data == 0x2F) && (event_length == 0)) { // Track End
 
3366
                                track->EOT = 1;
 
3367
                                return;
 
3368
                        } else if ((event_data == 0x51) && (event_length == 3)) {  // Tempo Change
 
3369
                                tempo = (mdi->data[track->ptr] << 16) | (mdi->data[track->ptr+1] << 8) | mdi->data[track->ptr+2];
 
3370
                                if (tempo == 0)
 
3371
                                        mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
 
3372
                                else    
 
3373
                                        mdi->samples_per_delta = (WM_SampleRate << 10) / ((1000000 * mdi->divisions) / tempo);
 
3374
                        }
 
3375
                }
 
3376
                track->ptr += event_length;
 
3377
        }
 
3378
}
 
3379
 
 
3380
 
 
3381
struct _mdi *
 
3382
WM_ParseNewMidi(unsigned char *mididata, unsigned long int midisize ) {
 
3383
        int i;
 
3384
        unsigned char eot[] = { 0xff, 0x2f, 0x00}; 
 
3385
        unsigned long int index_count = 0;
 
3386
        unsigned long int temp_delta = 0xffffff00;
 
3387
        struct _mdi *mdi = NULL;
 
3388
        void (*do_event[])(unsigned char ch, struct _mdi *midifile, struct _miditrack *track) = {
 
3389
                *do_amp_setup_note_off,
 
3390
                *do_amp_setup_note_on,
 
3391
                *do_amp_setup_aftertouch,
 
3392
                *do_amp_setup_control,
 
3393
                *do_amp_setup_patch,
 
3394
                *do_amp_setup_channel_pressure,
 
3395
                *do_amp_setup_pitch,
 
3396
                *do_amp_setup_message
 
3397
        };
 
3398
        unsigned long int midiofs = 0;
 
3399
        unsigned long int midi_track_counter = 0;
 
3400
        unsigned long int last_delta;
 
3401
        unsigned long int minus_delta = 0;
 
3402
        unsigned char current_event = 0;
 
3403
        unsigned short int no_tracks;
 
3404
        unsigned long int EOT_count = 0;
 
3405
        struct _miditrack *tmp_trackdata;
 
3406
        struct _hndl *tmp_handle = NULL;
 
3407
 
 
3408
        mdi = malloc(sizeof(struct _mdi));
 
3409
        if (mdi == NULL) {
 
3410
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM," to parse midi data", errno);
 
3411
                free (mididata);
 
3412
                return NULL;
 
3413
        }
 
3414
        
 
3415
        // Initialize data
 
3416
        memset(mdi, 0, sizeof(struct _mdi));
 
3417
        mdi->lock = 0;
 
3418
        mdi->data = mididata;
 
3419
        mdi->size = midisize;
 
3420
        mdi->info.mixer_options = WM_MixerOptions;
 
3421
 
 
3422
        mdi->index = malloc(((midisize / 2) + 1)* sizeof(struct _mdi_index));
 
3423
        if (mdi->index == NULL) {
 
3424
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM," to parse midi data", errno);
 
3425
                free(mdi);
 
3426
                return NULL;            
 
3427
        }
 
3428
 
 
3429
        load_patch(mdi, 0x0000);
 
3430
        
 
3431
        for (i=0; i<16; i++) {
 
3432
                mdi->channel[i].volume = 100;
 
3433
                mdi->channel[i].pressure = 127;
 
3434
                mdi->channel[i].expression = 127;
 
3435
                mdi->channel[i].pitch_range = 200;
 
3436
                mdi->channel[i].reg_data = 0xFFFF;
 
3437
                mdi->ch_vol[i] = 100;
 
3438
                mdi->ch_exp[i] = 127;
 
3439
                mdi->channel[i].patch = get_patch_data(mdi, 0x0000);
 
3440
        }
 
3441
 
 
3442
        midiofs = 0;
 
3443
        if (strncmp(mididata,"RIFF",4) == 0)
 
3444
                midiofs = 20;
 
3445
                                                
 
3446
        if (strncmp(&mididata[midiofs],"MThd",4) != 0) {
 
3447
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID,"(not a midi file)", 0);
 
3448
                free(mdi->index);
 
3449
                free(mdi);
 
3450
                return NULL;
 
3451
        }
 
3452
 
 
3453
        if ((midiofs + 25) > midisize) {
 
3454
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT,"(too short)", 0);
 
3455
                free(mdi->index);
 
3456
                free(mdi);
 
3457
                return NULL;
 
3458
        }
 
3459
        
 
3460
        midiofs += 9;
 
3461
        if (mididata[midiofs] > 1) {
 
3462
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, NULL, 0);
 
3463
                free(mdi->index);
 
3464
                free(mdi);
 
3465
                return NULL;
 
3466
        }
 
3467
        midiofs++;
 
3468
 
 
3469
        no_tracks = mididata[midiofs] << 8 | mididata[midiofs+1];
 
3470
        midiofs += 2;
 
3471
 
 
3472
        mdi->divisions = mididata[midiofs] << 8 | mididata[midiofs+1];
 
3473
        mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
 
3474
        midiofs += 2;
 
3475
 
 
3476
        tmp_trackdata = calloc(no_tracks, sizeof(struct _miditrack));
 
3477
 
 
3478
        if (first_handle == NULL) {
 
3479
                first_handle = malloc(sizeof(struct _hndl));
 
3480
                if (first_handle == NULL) {
 
3481
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM," to parse midi data", errno);
 
3482
                        free(mdi->data);
 
3483
                        free(mdi);
 
3484
                        return NULL;
 
3485
                }
 
3486
                first_handle->handle = (void *)mdi;
 
3487
                first_handle->prev = NULL;
 
3488
                first_handle->next = NULL;
 
3489
        } else {
 
3490
                tmp_handle = first_handle;
 
3491
                        free(tmp_trackdata);
 
3492
                if (tmp_handle->next != NULL) {
 
3493
                        while (tmp_handle->next != NULL)
 
3494
                                tmp_handle = tmp_handle->next;
 
3495
                }
 
3496
                tmp_handle->next = malloc(sizeof(struct _hndl));
 
3497
                if (tmp_handle->next == NULL) {
 
3498
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM," to parse midi data", errno);
 
3499
                        free(mdi->data);
 
3500
                        free(mdi);
 
3501
                        return NULL;
 
3502
                }
 
3503
                tmp_handle->next->prev = tmp_handle;
 
3504
                tmp_handle = tmp_handle->next;
 
3505
                tmp_handle->next = NULL;
 
3506
                tmp_handle->handle = (void *)mdi;
 
3507
        }
 
3508
 
 
3509
 
 
3510
        // grab track offsets;
 
3511
 
 
3512
        midi_track_counter = 0;
 
3513
        while (midi_track_counter != no_tracks) {
 
3514
                if ((midiofs + 12) > midisize) {
 
3515
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(too short)", 0);
 
3516
                        WildMidi_Close(mdi);
 
3517
                        free(tmp_trackdata);
 
3518
                        return NULL;
 
3519
                }
 
3520
                if (strncmp(&mididata[midiofs],"MTrk",4) != 0) {
 
3521
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(Expected track header)", 0);
 
3522
                        WildMidi_Close(mdi);
 
3523
                        free(tmp_trackdata);
 
3524
                        return NULL;
 
3525
                }
 
3526
                midiofs += 4;
 
3527
                tmp_trackdata[midi_track_counter].length = mididata[midiofs] << 24 | mididata[midiofs+1] << 16 | mididata[midiofs+2] << 8 | mididata[midiofs+3];
 
3528
                midiofs += 4;
 
3529
 
 
3530
                if (midisize < (midiofs + tmp_trackdata[midi_track_counter].length)) {
 
3531
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(too short)", 0);
 
3532
                        WildMidi_Close(mdi);
 
3533
                        free(tmp_trackdata);
 
3534
                        return NULL;
 
3535
                }
 
3536
                tmp_trackdata[midi_track_counter].ptr = midiofs;
 
3537
                tmp_trackdata[midi_track_counter].EOT = 0;
 
3538
                tmp_trackdata[midi_track_counter].running_event = 0;
 
3539
                tmp_trackdata[midi_track_counter].delta = read_var_length(mdi, &tmp_trackdata[midi_track_counter]);
 
3540
                if (tmp_trackdata[midi_track_counter].delta == 0xFFFFFFFF) {
 
3541
                        WildMidi_Close(mdi);
 
3542
                        free(tmp_trackdata);
 
3543
                        return NULL;
 
3544
                }
 
3545
                midiofs += tmp_trackdata[midi_track_counter].length;
 
3546
 
 
3547
                if (memcmp(&mididata[midiofs-3], eot,3) != 0) {
 
3548
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(Expected EOT)", 0);
 
3549
                        WildMidi_Close(mdi);
 
3550
                        free(tmp_trackdata);
 
3551
                        return NULL;
 
3552
                }
 
3553
                if (tmp_trackdata[midi_track_counter].delta < temp_delta) {
 
3554
                        temp_delta = tmp_trackdata[midi_track_counter].delta;
 
3555
                }
 
3556
                midi_track_counter++;
 
3557
        }
 
3558
 
 
3559
// set midi info
 
3560
        mdi->index[0].offset = 0;
 
3561
        mdi->index[0].delta = temp_delta;
 
3562
        
 
3563
        while (EOT_count != no_tracks) {
 
3564
                last_delta = 0;
 
3565
                for (i = 0; i < no_tracks; i++) {
 
3566
                        if (tmp_trackdata[i].EOT) {
 
3567
                                continue;
 
3568
                        }
 
3569
                        if (tmp_trackdata[i].delta) {
 
3570
                                tmp_trackdata[i].delta -= minus_delta;
 
3571
                                if (tmp_trackdata[i].delta) {
 
3572
                                        if ((last_delta == 0) || (last_delta > tmp_trackdata[i].delta)) {
 
3573
                                                last_delta = tmp_trackdata[i].delta;
 
3574
                                        }
 
3575
                                        continue;
 
3576
                                }
 
3577
                        }
 
3578
                        do {
 
3579
                                if (mdi->data[tmp_trackdata[i].ptr] < 0x80) {
 
3580
                                        current_event = tmp_trackdata[i].running_event;
 
3581
                                        if (current_event < 0x80) {
 
3582
                                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(expected event)", 0);
 
3583
                                                WildMidi_Close(mdi);
 
3584
                                                free(tmp_trackdata);
 
3585
                                                return NULL;
 
3586
                                        }
 
3587
                                } else {
 
3588
                                        current_event = mdi->data[tmp_trackdata[i].ptr];
 
3589
                                        tmp_trackdata[i].ptr++;
 
3590
                                }
 
3591
                                
 
3592
 
 
3593
                                index_count++;
 
3594
                                mdi->index[index_count].offset = tmp_trackdata[i].ptr;
 
3595
                                mdi->index[index_count].delta = 0; 
 
3596
                                mdi->index[index_count].event = current_event;
 
3597
                                
 
3598
                                do_event[((current_event & 0xF0) >> 4) - 8]((current_event & 0x0F), mdi, &tmp_trackdata[i]);
 
3599
                                if (tmp_trackdata[i].delta == 0xFFFFFFFF) {
 
3600
                                        WildMidi_Close(mdi);
 
3601
                                        free(tmp_trackdata);
 
3602
                                        return NULL;
 
3603
                                }
 
3604
                                if (tmp_trackdata[i].EOT) {
 
3605
                                        EOT_count++;
 
3606
                                        break;
 
3607
                                }
 
3608
                                tmp_trackdata[i].delta = read_var_length(mdi, &tmp_trackdata[i]);
 
3609
                                if (tmp_trackdata[i].delta == 0xFFFFFFFF) {
 
3610
                                        WildMidi_Close(mdi);
 
3611
                                        free(tmp_trackdata);
 
3612
                                        return NULL;
 
3613
                                }
 
3614
                        } while (!(tmp_trackdata[i].delta));
 
3615
                        if ((last_delta == 0) || (last_delta > tmp_trackdata[i].delta)) {
 
3616
                                if (tmp_trackdata[i].delta != 0) {
 
3617
                                        last_delta = tmp_trackdata[i].delta;
 
3618
                                }
 
3619
                        }
 
3620
                }
 
3621
//              printf("\rLast Delta %lu\n",last_delta);
 
3622
                mdi->index[index_count].delta = last_delta;
 
3623
                mdi->samples_to_mix += last_delta * mdi->samples_per_delta;
 
3624
                mdi->sample_count += mdi->samples_to_mix >> 10;
 
3625
                mdi->samples_to_mix %= 1024;
 
3626
                minus_delta = last_delta;       
 
3627
        }
 
3628
        mdi->sample_count -= (mdi->index[index_count - 1].delta * mdi->samples_per_delta) >> 10;
 
3629
        mdi->index[index_count - 1].delta = 0;
 
3630
        mdi->index_size = index_count;
 
3631
        mdi->index = realloc(mdi->index, (sizeof(struct _mdi_index) * mdi->index_size));
 
3632
        if (mdi->index == NULL) {
 
3633
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
 
3634
                WildMidi_Close(mdi);
 
3635
                free(tmp_trackdata);
 
3636
                return NULL;            
 
3637
        }
 
3638
        mdi->info.approx_total_samples = mdi->sample_count + 1;
 
3639
        mdi->samples_to_mix = 0;
 
3640
        mdi->sample_count = 0;
 
3641
        mdi->info.current_sample = 0;
 
3642
        mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
 
3643
        mdi->recalc_samples = 1;
 
3644
        mdi->last_note = mdi->note;
 
3645
        if (mdi->info.mixer_options & WM_MO_LINEAR_VOLUME) {
 
3646
                mdi->amp = 281;
 
3647
        } else {
 
3648
                mdi->amp = 281 * mdi->lin_max_vol / mdi->log_max_vol;
 
3649
        }
 
3650
 
 
3651
        for (i = 0; i < 16; i++) {
 
3652
                mdi->channel[i].bank = 0;
 
3653
                do_pan_adjust(mdi, i);
 
3654
        }
 
3655
        
 
3656
        for (i = 0; i < 4; i++) {
 
3657
                mdi->filter.lowpass[i][0].in[0] = 0;
 
3658
                mdi->filter.lowpass[i][0].in[1] = 0;
 
3659
                mdi->filter.lowpass[i][1].in[0] = 0;
 
3660
                mdi->filter.lowpass[i][1].in[1] = 0;
 
3661
 
 
3662
                mdi->filter.lowpass[i][0].out[0] = 0;
 
3663
                mdi->filter.lowpass[i][0].out[1] = 0;
 
3664
                mdi->filter.lowpass[i][1].out[0] = 0;
 
3665
                mdi->filter.lowpass[i][1].out[1] = 0;
 
3666
                
 
3667
                mdi->filter.delay_pos[i][0] = 0;
 
3668
                mdi->filter.delay_pos[i][1] = 0;
 
3669
                
 
3670
                mdi->filter.delay[i][0] = malloc(delay_size[i][0] * sizeof(signed long int));
 
3671
                mdi->filter.delay[i][1] = malloc(delay_size[i][1] * sizeof(signed long int));
 
3672
                memset (mdi->filter.delay[i][0], 0, (delay_size[i][0] * sizeof(signed long int)));
 
3673
                memset (mdi->filter.delay[i][1], 0, (delay_size[i][1] * sizeof(signed long int)));
 
3674
 
 
3675
        }
 
3676
        mdi->filter.in[0][0] = 0;
 
3677
        mdi->filter.in[0][1] = 0;
 
3678
        mdi->filter.in[1][0] = 0;
 
3679
        mdi->filter.in[1][1] = 0;
 
3680
        mdi->filter.out[0][0] = 0;
 
3681
        mdi->filter.out[0][1] = 0;
 
3682
        mdi->filter.out[1][0] = 0;
 
3683
        mdi->filter.out[1][1] = 0;
 
3684
 
 
3685
        free(tmp_trackdata);
 
3686
        return (mdi);
 
3687
}
 
3688
 
 
3689
/*
 
3690
 * =========================
 
3691
 * External Functions
 
3692
 * =========================
 
3693
 */
 
3694
 
 
3695
const char * 
 
3696
WildMidi_GetString (unsigned short int info) {
 
3697
        switch (info) {
 
3698
                case WM_GS_VERSION:
 
3699
                        return WM_Version;
 
3700
        }
 
3701
        return NULL;
 
3702
}
 
3703
 
 
3704
int 
 
3705
WildMidi_Init (const char * config_file, unsigned short int rate, unsigned short int options) {
 
3706
        if (WM_Initialized) {
 
3707
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
3708
                return -1;
 
3709
        }
 
3710
 
 
3711
        if (config_file == NULL) {
 
3712
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL config file pointer)", 0);
 
3713
                return -1;
 
3714
        }
 
3715
        WM_InitPatches();
 
3716
        if (WM_LoadConfig(config_file) == -1) {
 
3717
                return -1;
 
3718
        }
 
3719
 
 
3720
        if (options & 0xFFD8) {
 
3721
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(invalid option)", 0);
 
3722
                WM_FreePatches();
 
3723
                return -1;
 
3724
        }
 
3725
        WM_MixerOptions = options;
 
3726
 
 
3727
        if ((rate < 11000) || (rate > 65000)) {
 
3728
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(rate out of bounds, range is 11000 - 65000)", 0);
 
3729
                WM_FreePatches();
 
3730
                return -1;
 
3731
        }
 
3732
        WM_SampleRate = rate;
 
3733
        WM_Initialized = 1;
 
3734
        patch_lock = 0;
 
3735
        
 
3736
        init_gauss();
 
3737
        init_lowpass();
 
3738
        return 0;
 
3739
}
 
3740
 
 
3741
int
 
3742
WildMidi_MasterVolume (unsigned char master_volume) {
 
3743
        struct _mdi *mdi = NULL;
 
3744
        struct _hndl * tmp_handle = first_handle;
 
3745
        int i = 0;
 
3746
 
 
3747
        if (!WM_Initialized) {
 
3748
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
3749
                return -1;
 
3750
        }
 
3751
        if (master_volume > 127) {
 
3752
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(master volume out of range, range is 0-127)", 0);
 
3753
                return -1;
 
3754
        }
 
3755
        
 
3756
        WM_MasterVolume = lin_volume[master_volume];
 
3757
 
 
3758
        if (tmp_handle != NULL) {
 
3759
                while(tmp_handle != NULL) {
 
3760
                        mdi = (struct _mdi *)tmp_handle->handle;
 
3761
                        for (i = 0; i < 16; i++) {
 
3762
                                do_pan_adjust(mdi, i);
 
3763
                        }
 
3764
                        tmp_handle = tmp_handle->next;
 
3765
                }
 
3766
        }
 
3767
        
 
3768
        return 0;
 
3769
}
 
3770
 
 
3771
int
 
3772
WildMidi_Close (midi * handle) {
 
3773
        struct _mdi *mdi = (struct _mdi *)handle;
 
3774
        struct _hndl * tmp_handle;
 
3775
        struct _sample *tmp_sample;
 
3776
        int i;
 
3777
 
 
3778
        if (!WM_Initialized) {
 
3779
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
3780
                return -1;
 
3781
        }
 
3782
        if (handle == NULL) {
 
3783
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
 
3784
                return -1;
 
3785
        }
 
3786
        if (first_handle == NULL) {
 
3787
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(no midi's open)", 0);
 
3788
                return -1;
 
3789
        }
 
3790
        WM_Lock(&mdi->lock);
 
3791
        if (first_handle->handle == handle) {
 
3792
                tmp_handle = first_handle->next;
 
3793
                free (first_handle);
 
3794
                first_handle = tmp_handle;
 
3795
                if (first_handle != NULL)
 
3796
                        first_handle->prev = NULL;
 
3797
        } else {
 
3798
                tmp_handle = first_handle;
 
3799
                while (tmp_handle->handle != handle) {
 
3800
                        tmp_handle = tmp_handle->next;
 
3801
                        if (tmp_handle == NULL) {
 
3802
                                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(handle does not exist)", 0);
 
3803
                                return -1;
 
3804
                        }
 
3805
                }
 
3806
                tmp_handle->prev->next = tmp_handle->next;
 
3807
                if (tmp_handle->next != NULL) {
 
3808
                        tmp_handle->next->prev = tmp_handle->prev;
 
3809
                }
 
3810
                free (tmp_handle);
 
3811
        }
 
3812
        
 
3813
        if (mdi->patch_count != 0) {
 
3814
                WM_Lock(&patch_lock);
 
3815
                for (i = 0; i < mdi->patch_count; i++) {
 
3816
                        mdi->patches[i]->inuse_count--;
 
3817
                        if (mdi->patches[i]->inuse_count == 0) {
 
3818
                                //free samples here
 
3819
                                if (mdi->patches[i]->first_sample != NULL) {
 
3820
                                        while (mdi->patches[i]->first_sample != NULL) {
 
3821
                                                tmp_sample = mdi->patches[i]->first_sample->next;
 
3822
                                                if (mdi->patches[i]->first_sample->data)
 
3823
                                                        free(mdi->patches[i]->first_sample->data);
 
3824
                                                free(mdi->patches[i]->first_sample);
 
3825
                                                mdi->patches[i]->first_sample = tmp_sample;
 
3826
                                        }
 
3827
                                        mdi->patches[i]->loaded = 0;
 
3828
                                }
 
3829
                        }
 
3830
                }
 
3831
                WM_Unlock(&patch_lock);
 
3832
                free (mdi->patches);
 
3833
        }
 
3834
 
 
3835
        for (i = 0; i < 4; i++) {
 
3836
                if (mdi->filter.delay[i][0] != NULL)
 
3837
                        free(mdi->filter.delay[i][0]);
 
3838
                if (mdi->filter.delay[i][1] != NULL)
 
3839
                        free(mdi->filter.delay[i][1]);
 
3840
        }
 
3841
 
 
3842
        if (mdi->data != NULL) {
 
3843
                free (mdi->data);
 
3844
        }
 
3845
        if (mdi->tmp_info != NULL) {
 
3846
                free (mdi->tmp_info);
 
3847
        }
 
3848
        if (mdi->index != NULL) 
 
3849
                free (mdi->index);
 
3850
        free (mdi);
 
3851
        // no need to unlock cause the struct containing the lock no-longer exists;
 
3852
        return 0;
 
3853
}
 
3854
 
 
3855
midi * 
 
3856
WildMidi_Open (const char *midifile) {
 
3857
        unsigned char *mididata = NULL;
 
3858
        unsigned long int midisize = 0;
 
3859
        
 
3860
        if (!WM_Initialized) {
 
3861
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
3862
                return NULL;
 
3863
        }
 
3864
        if (midifile == NULL) {
 
3865
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL filename)", 0);
 
3866
                return NULL;
 
3867
        }
 
3868
 
 
3869
        if ((mididata = WM_BufferFile(midifile, &midisize)) == NULL) {
 
3870
                return NULL;
 
3871
        }
 
3872
        
 
3873
        return (void *)WM_ParseNewMidi(mididata,midisize);
 
3874
}
 
3875
 
 
3876
midi *
 
3877
WildMidi_OpenBuffer (unsigned char *midibuffer, unsigned long int size) {
 
3878
        if (!WM_Initialized) {
 
3879
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
3880
                return NULL;
 
3881
        }
 
3882
        if (midibuffer == NULL) {
 
3883
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL midi data buffer)", 0);
 
3884
                return NULL;
 
3885
        }
 
3886
 
 
3887
        return (void *)WM_ParseNewMidi(midibuffer,size);
 
3888
}
 
3889
 
 
3890
int
 
3891
WildMidi_LoadSamples( midi * handle) {
 
3892
        return 0;
 
3893
}
 
3894
 
 
3895
int
 
3896
WildMidi_FastSeek ( midi * handle, unsigned long int *sample_pos) {
 
3897
        struct _mdi *mdi = (struct _mdi *)handle;
 
3898
        struct _note **note_data = mdi->note;
 
3899
        void (*do_event[])(unsigned char ch, struct _mdi *midifile, unsigned long int ptr) = {
 
3900
                *do_null,
 
3901
                *do_null,
 
3902
                *do_aftertouch,
 
3903
                *do_control,
 
3904
                *do_patch,
 
3905
                *do_channel_pressure,
 
3906
                *do_pitch,
 
3907
                *do_message
 
3908
        };
 
3909
        unsigned long int real_samples_to_mix = 0;
 
3910
 
 
3911
        if (!WM_Initialized) {
 
3912
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
3913
                return -1;
 
3914
        }
 
3915
        if (handle == NULL) {
 
3916
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
 
3917
                return -1;
 
3918
        }
 
3919
        WM_Lock(&mdi->lock);
 
3920
        if (sample_pos == NULL) {
 
3921
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL seek position pointer)", 0);                
 
3922
                WM_Unlock(&mdi->lock);
 
3923
                return -1;
 
3924
        }
 
3925
 
 
3926
        if (*sample_pos == mdi->info.current_sample) {
 
3927
                WM_Unlock(&mdi->lock);
 
3928
                return 0;
 
3929
        }
 
3930
 
 
3931
        if (*sample_pos > mdi->info.current_sample) {
 
3932
                if ((mdi->sample_count == 0) && (mdi->index_count == mdi->index_size) && (mdi->last_note == 0)) {
 
3933
                        *sample_pos = mdi->info.current_sample;
 
3934
                        WM_Unlock(&mdi->lock);
 
3935
                        return 0;
 
3936
                }
 
3937
        } else {
 
3938
                WM_ResetToStart(handle);
 
3939
        }
 
3940
 
 
3941
        //reset all notes
 
3942
        if (note_data != mdi->last_note) {
 
3943
                do {
 
3944
                        (*note_data)->active = 0;
 
3945
                        *note_data = NULL;
 
3946
                        note_data++;
 
3947
                } while (note_data != mdi->last_note);
 
3948
                mdi->last_note = mdi->note;
 
3949
        }               
 
3950
 
 
3951
        while (*sample_pos != mdi->info.current_sample) {
 
3952
                if (!mdi->sample_count) {
 
3953
                        if (mdi->index_count != mdi->index_size) {
 
3954
 
 
3955
                                do {
 
3956
                                        if (mdi->index_count == mdi->index_size) {
 
3957
                                                break;
 
3958
                                        }
 
3959
                                        
 
3960
                                        if (mdi->index_count != 0) {
 
3961
                                                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);
 
3962
                                        }
 
3963
                                } while (mdi->index[mdi->index_count++].delta == 0);
 
3964
                                
 
3965
                                mdi->samples_to_mix += mdi->index[mdi->index_count-1].delta * mdi->samples_per_delta;
 
3966
                                mdi->sample_count = mdi->samples_to_mix >> 10;
 
3967
                                mdi->samples_to_mix %= 1024;
 
3968
                        } else {
 
3969
                                mdi->sample_count = WM_SampleRate;
 
3970
                        }
 
3971
                }
 
3972
 
 
3973
                if (mdi->sample_count <= (*sample_pos - mdi->info.current_sample)) {
 
3974
                        real_samples_to_mix = mdi->sample_count;
 
3975
                        if (real_samples_to_mix == 0) {
 
3976
                                continue;
 
3977
                        }
 
3978
                } else {
 
3979
                        real_samples_to_mix = (*sample_pos - mdi->info.current_sample);
 
3980
                }
 
3981
                
 
3982
                mdi->info.current_sample += real_samples_to_mix;
 
3983
                mdi->sample_count -= real_samples_to_mix;
 
3984
                if ((mdi->index_count == mdi->index_size) && (mdi->last_note == 0)) {
 
3985
                        mdi->sample_count = 0;
 
3986
                        *sample_pos = mdi->info.current_sample;
 
3987
                        WM_Unlock(&mdi->lock);
 
3988
                        return 0;
 
3989
                }
 
3990
        }
 
3991
        WM_Unlock(&mdi->lock);
 
3992
        return 0;
 
3993
}
 
3994
 
 
3995
int
 
3996
WildMidi_SampledSeek ( midi * handle, unsigned long int *sample_pos) {
 
3997
        struct _mdi *mdi = (struct _mdi *)handle;
 
3998
        struct _note **note_data = mdi->note;
 
3999
        unsigned long int real_samples_to_mix = 0;
 
4000
        unsigned long int tmp_samples_to_mix = 0;
 
4001
 
 
4002
        if (!WM_Initialized) {
 
4003
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
4004
                return -1;
 
4005
        }
 
4006
        if (handle == NULL) {
 
4007
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
 
4008
                return -1;
 
4009
        }
 
4010
        WM_Lock(&mdi->lock);
 
4011
        if (sample_pos == NULL) {
 
4012
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL seek position pointer)", 0);                
 
4013
                WM_Unlock(&mdi->lock);
 
4014
                return -1;
 
4015
        }
 
4016
 
 
4017
        if (*sample_pos == mdi->info.current_sample) {
 
4018
                WM_Unlock(&mdi->lock);
 
4019
                return 0;
 
4020
        }
 
4021
 
 
4022
        if (*sample_pos > mdi->info.current_sample) {
 
4023
                if ((mdi->sample_count == 0) && (mdi->index_count == mdi->index_size) && (mdi->last_note == 0)) {
 
4024
                        *sample_pos = mdi->info.current_sample;
 
4025
                        WM_Unlock(&mdi->lock);
 
4026
                        return 0;
 
4027
                }
 
4028
        } else {
 
4029
                WM_ResetToStart(handle);
 
4030
                if (note_data != mdi->last_note) {
 
4031
                        do {
 
4032
                                (*note_data)->active = 0;
 
4033
                                *note_data = NULL;
 
4034
                                note_data++;
 
4035
                        } while (note_data != mdi->last_note);
 
4036
                        mdi->last_note = mdi->note;
 
4037
                }               
 
4038
        }
 
4039
        
 
4040
        while (*sample_pos != mdi->info.current_sample) {
 
4041
                if (!mdi->sample_count) {
 
4042
                        if (mdi->index_count != mdi->index_size) {
 
4043
 
 
4044
                                do {
 
4045
                                        if (mdi->index_count == mdi->index_size) {
 
4046
                                                break;
 
4047
                                        }
 
4048
                                        
 
4049
                                        if (mdi->index_count != 0) {
 
4050
                                                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);
 
4051
                                        }
 
4052
                                } while (mdi->index[mdi->index_count++].delta == 0);
 
4053
                                
 
4054
                                mdi->samples_to_mix += mdi->index[mdi->index_count-1].delta * mdi->samples_per_delta;
 
4055
                                mdi->sample_count = mdi->samples_to_mix >> 10;
 
4056
                                mdi->samples_to_mix %= 1024;
 
4057
                        } else {
 
4058
                                if (mdi->recalc_samples) {
 
4059
                                        WM_RecalcSamples(mdi);
 
4060
                                }
 
4061
                                mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
 
4062
                                if (mdi->sample_count == 0) {
 
4063
                                        WM_Unlock(&mdi->lock);
 
4064
                                        return 0;
 
4065
                                }
 
4066
                        }
 
4067
                }
 
4068
 
 
4069
                if (mdi->sample_count <= (*sample_pos - mdi->info.current_sample)) {
 
4070
                        real_samples_to_mix = mdi->sample_count;
 
4071
                        if (real_samples_to_mix == 0) {
 
4072
                                continue;
 
4073
                        }
 
4074
                } else {
 
4075
                        real_samples_to_mix = (*sample_pos - mdi->info.current_sample);
 
4076
                }
 
4077
                
 
4078
                // do mixing here
 
4079
                tmp_samples_to_mix = real_samples_to_mix;
 
4080
                do {
 
4081
 
 
4082
                        if (mdi->last_note != mdi->note) {
 
4083
                                note_data = mdi->note;
 
4084
                                while (note_data != mdi->last_note) {
 
4085
                                
 
4086
 
 
4087
/*
 
4088
 * ========================
 
4089
 * sample position checking
 
4090
 * ========================
 
4091
 */
 
4092
                                        (*note_data)->sample_pos += (*note_data)->sample_inc;
 
4093
                                        if (__builtin_expect(((*note_data)->sample_pos > (*note_data)->sample->loop_end), 0)) {
 
4094
                                                if ((*note_data)->modes & SAMPLE_LOOP) {
 
4095
                                                        (*note_data)->sample_pos = (*note_data)->sample->loop_start + (((*note_data)->sample_pos - (*note_data)->sample->loop_start) % (*note_data)->sample->loop_size);
 
4096
                                                } else if (__builtin_expect(((*note_data)->sample_pos >= (*note_data)->sample->data_length), 0)) {
 
4097
                                                        if (__builtin_expect(((*note_data)->next == NULL), 1)) {
 
4098
                                                                goto KILL_NOTE;
 
4099
                                                        }
 
4100
                                                        goto RESTART_NOTE;
 
4101
                                                }
 
4102
                                        }
 
4103
                                        if (__builtin_expect(((*note_data)->env_inc == 0), 0)) {
 
4104
                                                note_data++;
 
4105
                                                continue;
 
4106
                                        }
 
4107
                                        (*note_data)->env_level += (*note_data)->env_inc;
 
4108
                                        if (__builtin_expect(((*note_data)->env_level > 4194304), 0)) {
 
4109
                                                (*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
 
4110
                                        } 
 
4111
                                        if (__builtin_expect((((*note_data)->env_inc < 0) && 
 
4112
                                                        ((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env])) ||
 
4113
                                                        (((*note_data)->env_inc > 0) && 
 
4114
                                                        ((*note_data)->env_level < (*note_data)->sample->env_target[(*note_data)->env])), 1)) {
 
4115
                                                note_data++;
 
4116
                                                        continue;
 
4117
                                        }
 
4118
                                        (*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
 
4119
                                        switch ((*note_data)->env) {
 
4120
                                                case 0:
 
4121
                                                        if (!((*note_data)->modes & SAMPLE_ENVELOPE)) {
 
4122
                                                                (*note_data)->env_inc = 0;
 
4123
                                                                note_data++;
 
4124
                                                                continue;
 
4125
                                                        }
 
4126
                                                        break;
 
4127
                                                case 2:
 
4128
                                                        if ((*note_data)->modes & SAMPLE_SUSTAIN) {
 
4129
                                                        (*note_data)->env_inc = 0;
 
4130
                                                        note_data++;
 
4131
                                                        continue;
 
4132
                                                }
 
4133
                                                break;
 
4134
                                        case 5:
 
4135
                                                if (__builtin_expect(((*note_data)->env_level == 0), 1)) {
 
4136
                                                        goto KILL_NOTE;
 
4137
                                                }
 
4138
                                                // sample release
 
4139
                                                if ((*note_data)->modes & SAMPLE_LOOP)
 
4140
                                                        (*note_data)->modes ^= SAMPLE_LOOP;
 
4141
                                                (*note_data)->env_inc = 0;
 
4142
                                                note_data++;
 
4143
                                                continue;
 
4144
                                        case 6:
 
4145
                                                if (__builtin_expect(((*note_data)->next != NULL), 1)) {
 
4146
                                                        RESTART_NOTE:
 
4147
                                                        (*note_data)->active = 0;
 
4148
                                                        *note_data = (*note_data)->next;
 
4149
                                                        (*note_data)->active = 1;
 
4150
                                                        note_data++;
 
4151
                                                        
 
4152
                                                } else {
 
4153
                                                        KILL_NOTE:
 
4154
                                                        (*note_data)->active = 0;
 
4155
                                                        mdi->last_note--;
 
4156
                                                        if (note_data != mdi->last_note) {
 
4157
                                                                *note_data = *mdi->last_note;
 
4158
                                                        }
 
4159
                                                }
 
4160
                                                continue;
 
4161
                                        }
 
4162
                                        (*note_data)->env++;
 
4163
                                        if ((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env]) {
 
4164
                                                (*note_data)->env_inc = -(*note_data)->sample->env_rate[(*note_data)->env];
 
4165
                                        } else {
 
4166
                                        (*note_data)->env_inc = (*note_data)->sample->env_rate[(*note_data)->env];
 
4167
                                        }
 
4168
                                        note_data++;
 
4169
                                        continue;
 
4170
                                }
 
4171
                        } else {
 
4172
                                break;
 
4173
                        }
 
4174
                } while (--tmp_samples_to_mix);
 
4175
                mdi->info.current_sample += real_samples_to_mix;
 
4176
                mdi->sample_count -= real_samples_to_mix;
 
4177
                if (mdi->index_count == mdi->index_size) {
 
4178
                        if (mdi->last_note == 0) {
 
4179
                                mdi->sample_count = 0;
 
4180
                                *sample_pos = mdi->info.current_sample;
 
4181
                                WM_Unlock(&mdi->lock);
 
4182
                                return 0;
 
4183
                        }
 
4184
                }
 
4185
        }
 
4186
        WM_Unlock(&mdi->lock);
 
4187
        return 0;
 
4188
}
 
4189
 
 
4190
int
 
4191
WildMidi_GetOutput_Linear (midi * handle, char * buffer, unsigned long int size) {
 
4192
        unsigned long int buffer_used = 0;
 
4193
        struct _mdi *mdi = (struct _mdi *)handle;
 
4194
        unsigned long int real_samples_to_mix = 0;
 
4195
        unsigned long int data_pos;
 
4196
        signed long int premix, left_mix, right_mix;
 
4197
        signed long int vol_mul;
 
4198
        struct _note **note_data = NULL;
 
4199
        unsigned long int count;
 
4200
 
 
4201
        if (__builtin_expect((!WM_Initialized),0)) {
 
4202
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
4203
                return -1;
 
4204
        }
 
4205
        if (__builtin_expect((handle == NULL),0)) {
 
4206
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
 
4207
                return -1;
 
4208
        }
 
4209
        if (__builtin_expect((buffer == NULL),0)) {
 
4210
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL buffer pointer)", 0);
 
4211
                return -1;
 
4212
        }
 
4213
 
 
4214
        if (__builtin_expect((size == 0),0)) {
 
4215
                return 0;
 
4216
        }
 
4217
 
 
4218
        if (__builtin_expect((size % 4),0)) {
 
4219
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(size not a multiple of 4)", 0);
 
4220
                return -1;
 
4221
        }
 
4222
 
 
4223
        WM_Lock(&mdi->lock);
 
4224
        if (__builtin_expect(((mdi->index_count == mdi->index_size) && (mdi->last_note == 0)), 0)) {
 
4225
                WM_Unlock(&mdi->lock);
 
4226
                return 0;
 
4227
        }
 
4228
 
 
4229
        buffer_used = 0;
 
4230
        memset(buffer, 0, size);
 
4231
                
 
4232
        do {
 
4233
                if (__builtin_expect((!mdi->sample_count),0)) {
 
4234
                        if (__builtin_expect((mdi->index_count != mdi->index_size),1)) {
 
4235
                                do {
 
4236
                                        if (__builtin_expect((mdi->index_count == mdi->index_size), 0)) {
 
4237
                                                break;
 
4238
                                        }
 
4239
                                        
 
4240
                                        if (__builtin_expect((mdi->index_count != 0), 1)) {
 
4241
                                                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);
 
4242
                                        }
 
4243
                                } while (mdi->index[mdi->index_count++].delta == 0);
 
4244
                                
 
4245
                                mdi->samples_to_mix += mdi->index[mdi->index_count-1].delta * mdi->samples_per_delta;
 
4246
                                mdi->sample_count = mdi->samples_to_mix >> 10;
 
4247
                                mdi->samples_to_mix %= 1024;
 
4248
                        } else {
 
4249
                                if (mdi->recalc_samples) {
 
4250
                                        WM_RecalcSamples(mdi);
 
4251
                                }
 
4252
                                mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
 
4253
                                if (mdi->sample_count == 0) {
 
4254
                                        WM_Unlock(&mdi->lock);
 
4255
                                        return buffer_used;
 
4256
                                }
 
4257
                        }
 
4258
                }
 
4259
                if (__builtin_expect((mdi->sample_count > (size >> 2)),1)) {
 
4260
                        real_samples_to_mix = size >> 2;
 
4261
                } else {
 
4262
                        real_samples_to_mix = mdi->sample_count;
 
4263
                        if (real_samples_to_mix == 0) {
 
4264
                                continue;
 
4265
                        }
 
4266
                }
 
4267
                
 
4268
                // do mixing here
 
4269
                count = real_samples_to_mix;
 
4270
                do {
 
4271
                        note_data = mdi->note;
 
4272
                        left_mix = right_mix = 0;
 
4273
                        if (__builtin_expect((mdi->last_note != mdi->note),1)) {
 
4274
                                while (note_data != mdi->last_note) {
 
4275
/*
 
4276
 * ===================
 
4277
 * resample the sample
 
4278
 * ===================
 
4279
 */
 
4280
                                        data_pos = (*note_data)->sample_pos >> FPBITS;
 
4281
                                        vol_mul = (((*note_data)->vol_lvl * ((*note_data)->env_level >> 12)) >> FPBITS);
 
4282
                                        
 
4283
                                        premix = ((*note_data)->sample->data[data_pos] + 
 
4284
                                                (((*note_data)->sample->data[data_pos + 1]  - (*note_data)->sample->data[data_pos]) * 
 
4285
                                                (signed long int)((*note_data)->sample_pos & FPMASK) >> FPBITS)) * vol_mul / 1024;
 
4286
 
 
4287
                                        left_mix += premix * mdi->channel[(*note_data)->noteid >> 8].left_adjust;
 
4288
                                        right_mix += premix * mdi->channel[(*note_data)->noteid >> 8].right_adjust;
 
4289
 
 
4290
/*
 
4291
 * ========================
 
4292
 * sample position checking
 
4293
 * ========================
 
4294
 */
 
4295
                                        (*note_data)->sample_pos += (*note_data)->sample_inc;
 
4296
                                        if (__builtin_expect(((*note_data)->sample_pos > (*note_data)->sample->loop_end), 0)) {
 
4297
                                                if ((*note_data)->modes & SAMPLE_LOOP) {
 
4298
                                                        (*note_data)->sample_pos = (*note_data)->sample->loop_start + (((*note_data)->sample_pos - (*note_data)->sample->loop_start) % (*note_data)->sample->loop_size);
 
4299
                                                } else if (__builtin_expect(((*note_data)->sample_pos >= (*note_data)->sample->data_length), 0)) {
 
4300
                                                        if (__builtin_expect(((*note_data)->next == NULL), 1)) {
 
4301
                                                                goto KILL_NOTE;
 
4302
 
 
4303
                                                        }
 
4304
                                                        goto RESTART_NOTE;
 
4305
                                                }
 
4306
                                        }
 
4307
 
 
4308
                                        if (__builtin_expect(((*note_data)->env_inc == 0), 0)) {
 
4309
                                                note_data++;
 
4310
                                                continue;
 
4311
                                        }
 
4312
 
 
4313
                                        (*note_data)->env_level += (*note_data)->env_inc;
 
4314
                                        if (__builtin_expect(((*note_data)->env_level > 4194304), 0)) {
 
4315
                                                (*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
 
4316
                                        } 
 
4317
                                        if (__builtin_expect((((*note_data)->env_inc < 0) && 
 
4318
                                                        ((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env])) ||
 
4319
                                                        (((*note_data)->env_inc > 0) && 
 
4320
                                                        ((*note_data)->env_level < (*note_data)->sample->env_target[(*note_data)->env])), 1)) {
 
4321
                                                note_data++;
 
4322
                                                continue;
 
4323
                                        }
 
4324
 
 
4325
                                        (*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
 
4326
                                        switch ((*note_data)->env) {
 
4327
                                                case 0:
 
4328
                                                        if (!((*note_data)->modes & SAMPLE_ENVELOPE)) {
 
4329
                                                                (*note_data)->env_inc = 0;
 
4330
                                                                note_data++;
 
4331
                                                                continue;
 
4332
                                                        }
 
4333
                                                        break;
 
4334
                                                case 2:
 
4335
                                                        if ((*note_data)->modes & SAMPLE_SUSTAIN) {
 
4336
                                                                (*note_data)->env_inc = 0;
 
4337
                                                                note_data++;
 
4338
                                                                continue;
 
4339
                                                        }
 
4340
                                                        break;
 
4341
                                                case 5:
 
4342
                                                        if (__builtin_expect(((*note_data)->env_level == 0), 1)) {
 
4343
                                                                goto KILL_NOTE;
 
4344
                                                        }
 
4345
                                                        // sample release
 
4346
                                                        if ((*note_data)->modes & SAMPLE_LOOP)
 
4347
                                                                (*note_data)->modes ^= SAMPLE_LOOP;
 
4348
                                                        (*note_data)->env_inc = 0;
 
4349
                                                        note_data++;
 
4350
                                                        continue;
 
4351
                                                case 6:
 
4352
                                                        if (__builtin_expect(((*note_data)->next != NULL), 1)) {
 
4353
                                                                RESTART_NOTE:
 
4354
                                                                (*note_data)->active = 0;
 
4355
                                                                *note_data = (*note_data)->next;
 
4356
                                                                (*note_data)->active = 1;
 
4357
                                                                note_data++;
 
4358
                                                                
 
4359
                                                        } else {
 
4360
                                                                KILL_NOTE:
 
4361
                                                                (*note_data)->active = 0;
 
4362
                                                                mdi->last_note--;
 
4363
                                                                if (note_data != mdi->last_note) {
 
4364
                                                                        *note_data = *mdi->last_note;
 
4365
                                                                }
 
4366
                                                        }
 
4367
                                                        continue;
 
4368
                                        }
 
4369
                                        (*note_data)->env++;
 
4370
                                        if ((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env]) {
 
4371
                                                (*note_data)->env_inc = -(*note_data)->sample->env_rate[(*note_data)->env];
 
4372
                                        } else {
 
4373
                                                (*note_data)->env_inc = (*note_data)->sample->env_rate[(*note_data)->env];
 
4374
                                        }
 
4375
                                        note_data++;
 
4376
                                        continue;
 
4377
                                }
 
4378
/*
 
4379
 * =========================
 
4380
 * mix the channels together
 
4381
 * =========================
 
4382
 */
 
4383
                                
 
4384
                                left_mix /= 1024;
 
4385
                                right_mix /= 1024;
 
4386
                        }
 
4387
 
 
4388
#ifdef EXPERIMENT_626
 
4389
/*
 
4390
 * ==========================
 
4391
 * Experimental Reverb Engine
 
4392
 * ==========================
 
4393
 */
 
4394
 
 
4395
                        if (mdi->info.mixer_options & WM_MO_REVERB) {
 
4396
                                signed long int filteral = mdi->filter.delay[0][0][mdi->filter.delay_pos[0][0]];
 
4397
                                signed long int filterar = mdi->filter.delay[0][1][mdi->filter.delay_pos[0][1]];
 
4398
                                signed long int filterbl = mdi->filter.delay[1][0][mdi->filter.delay_pos[1][0]];
 
4399
                                signed long int filterbr = mdi->filter.delay[1][1][mdi->filter.delay_pos[1][1]];
 
4400
                                signed long int filtercl = mdi->filter.delay[2][0][mdi->filter.delay_pos[2][0]];
 
4401
                                signed long int filtercr = mdi->filter.delay[2][1][mdi->filter.delay_pos[2][1]];
 
4402
                                signed long int filterdl = mdi->filter.delay[3][0][mdi->filter.delay_pos[3][0]];
 
4403
                                signed long int filterdr = mdi->filter.delay[3][1][mdi->filter.delay_pos[3][1]];
 
4404
                                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;
 
4405
                                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;
 
4406
                                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;
 
4407
                                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;
 
4408
                                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;
 
4409
                                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;
 
4410
                                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;
 
4411
                                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;
 
4412
                                signed long int tfl, tflo;
 
4413
                                signed long int tfr, tfro;
 
4414
                                
 
4415
                                mdi->filter.lowpass[0][0].in[1] = mdi->filter.lowpass[0][0].in[0];
 
4416
                                mdi->filter.lowpass[0][0].in[0] = filteral;
 
4417
                                mdi->filter.lowpass[0][1].in[1] = mdi->filter.lowpass[0][1].in[0];
 
4418
                                mdi->filter.lowpass[0][1].in[0] = filterar;
 
4419
                                mdi->filter.lowpass[1][0].in[1] = mdi->filter.lowpass[1][0].in[0];
 
4420
                                mdi->filter.lowpass[1][0].in[0] = filterbl;
 
4421
                                mdi->filter.lowpass[1][1].in[1] = mdi->filter.lowpass[1][1].in[0];
 
4422
                                mdi->filter.lowpass[1][1].in[0] = filterbr;
 
4423
                                mdi->filter.lowpass[2][0].in[1] = mdi->filter.lowpass[2][0].in[0];
 
4424
                                mdi->filter.lowpass[2][0].in[0] = filtercl;
 
4425
                                mdi->filter.lowpass[2][1].in[1] = mdi->filter.lowpass[2][1].in[0];
 
4426
                                mdi->filter.lowpass[2][1].in[0] = filtercr;
 
4427
                                mdi->filter.lowpass[3][0].in[1] = mdi->filter.lowpass[3][0].in[0];
 
4428
                                mdi->filter.lowpass[3][0].in[0] = filterdl;
 
4429
                                mdi->filter.lowpass[3][1].in[1] = mdi->filter.lowpass[3][1].in[0];
 
4430
                                mdi->filter.lowpass[3][1].in[0] = filterdr;
 
4431
 
 
4432
                                mdi->filter.lowpass[0][0].out[1] = mdi->filter.lowpass[0][0].out[0];
 
4433
                                mdi->filter.lowpass[0][0].out[0] = tfal;
 
4434
                                mdi->filter.lowpass[0][1].out[1] = mdi->filter.lowpass[0][1].out[0];
 
4435
                                mdi->filter.lowpass[0][1].out[0] = tfar;
 
4436
                                mdi->filter.lowpass[1][0].out[1] = mdi->filter.lowpass[1][0].out[0];
 
4437
                                mdi->filter.lowpass[1][0].out[0] = tfbl;
 
4438
                                mdi->filter.lowpass[1][1].out[1] = mdi->filter.lowpass[1][1].out[0];
 
4439
                                mdi->filter.lowpass[1][1].out[0] = tfbr;
 
4440
                                mdi->filter.lowpass[2][0].out[1] = mdi->filter.lowpass[2][0].out[0];
 
4441
                                mdi->filter.lowpass[2][0].out[0] = tfcl;
 
4442
                                mdi->filter.lowpass[2][1].out[1] = mdi->filter.lowpass[2][1].out[0];
 
4443
                                mdi->filter.lowpass[2][1].out[0] = tfcr;
 
4444
                                mdi->filter.lowpass[3][0].out[1] = mdi->filter.lowpass[3][0].out[0];
 
4445
                                mdi->filter.lowpass[3][0].out[0] = tfdl;
 
4446
                                mdi->filter.lowpass[3][1].out[1] = mdi->filter.lowpass[3][1].out[0];
 
4447
                                mdi->filter.lowpass[3][1].out[0] = tfdr;
 
4448
                                
 
4449
                                mdi->filter.delay[0][0][mdi->filter.delay_pos[0][0]] = (tfbr * 405 + tfcr * 368) / 1024 + (left_mix * gain_in[0] / 1024);
 
4450
                                mdi->filter.delay[0][1][mdi->filter.delay_pos[0][1]] = (tfbl * 402 + tfcl * 370) / 1024 + (right_mix * gain_in[0] / 1024);
 
4451
                                mdi->filter.delay[1][0][mdi->filter.delay_pos[1][0]] = (tfar * -545 + tfdr * -364) / 1024 + (left_mix * gain_in[1] / 1024);
 
4452
                                mdi->filter.delay[1][1][mdi->filter.delay_pos[1][1]] = (tfal * -550 + tfdl * -362) / 1024 + (right_mix * gain_in[1] / 1024);
 
4453
                                mdi->filter.delay[2][0][mdi->filter.delay_pos[2][0]] = (tfar * 545 + tfdr * -364) / 1024 + (left_mix * gain_in[2] / 1024);
 
4454
                                mdi->filter.delay[2][1][mdi->filter.delay_pos[2][1]] = (tfal * 550 + tfdl * 362) / 1024 + (right_mix * gain_in[2] / 1024);
 
4455
                                mdi->filter.delay[3][0][mdi->filter.delay_pos[3][0]] = (tfbr * 405 + tfcr * -368) / 1024 + (left_mix * gain_in[3] / 1024);
 
4456
                                mdi->filter.delay[3][1][mdi->filter.delay_pos[3][1]] = (tfbl * 402 + tfcl * -370) / 1024 + (right_mix * gain_in[3] / 1024);
 
4457
                                
 
4458
                                tfl = ((tfal * gain_out[0] / 1024) + (tfbl * gain_out[1] / 1024) + (tfcl * gain_out[2] / 1024) + (tfdl * gain_out[3] / 1024));
 
4459
                                tfr = ((tfar * gain_out[0] / 1024) + (tfbr * gain_out[1] / 1024) + (tfcr * gain_out[2] / 1024) + (tfdr * gain_out[3] / 1024));
 
4460
                                
 
4461
                                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;
 
4462
                                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; 
 
4463
                                
 
4464
                                mdi->filter.in[1][0] =  mdi->filter.in[0][0];
 
4465
                                mdi->filter.in[0][0] = tfl;
 
4466
                                mdi->filter.in[1][1] =  mdi->filter.in[0][1];
 
4467
                                mdi->filter.in[0][1] = tfr;
 
4468
                                mdi->filter.out[1][0] =  mdi->filter.out[0][0];
 
4469
                                mdi->filter.out[0][0] = tflo;
 
4470
                                mdi->filter.out[1][1] =  mdi->filter.out[0][1];
 
4471
                                mdi->filter.out[0][1] = tfro;
 
4472
                                
 
4473
                                left_mix += tflo;
 
4474
                                right_mix += tfro;
 
4475
                                
 
4476
                                mdi->filter.delay_pos[0][0]++;
 
4477
                                mdi->filter.delay_pos[0][1]++;
 
4478
                                mdi->filter.delay_pos[1][0]++;
 
4479
                                mdi->filter.delay_pos[1][1]++;
 
4480
                                mdi->filter.delay_pos[2][0]++;
 
4481
                                mdi->filter.delay_pos[2][1]++;
 
4482
                                mdi->filter.delay_pos[3][0]++;
 
4483
                                mdi->filter.delay_pos[3][1]++;
 
4484
                                
 
4485
                                if (mdi->filter.delay_pos[0][0] == delay_size[0][0]) mdi->filter.delay_pos[0][0] = 0;
 
4486
                                if (mdi->filter.delay_pos[0][1] == delay_size[0][1]) mdi->filter.delay_pos[0][1] = 0;
 
4487
                                if (mdi->filter.delay_pos[1][0] == delay_size[1][0]) mdi->filter.delay_pos[1][0] = 0;
 
4488
                                if (mdi->filter.delay_pos[1][1] == delay_size[1][1]) mdi->filter.delay_pos[1][1] = 0;
 
4489
                                if (mdi->filter.delay_pos[2][0] == delay_size[2][0]) mdi->filter.delay_pos[2][0] = 0;
 
4490
                                if (mdi->filter.delay_pos[2][1] == delay_size[2][1]) mdi->filter.delay_pos[2][1] = 0;
 
4491
                                if (mdi->filter.delay_pos[3][0] == delay_size[3][0]) mdi->filter.delay_pos[3][0] = 0;
 
4492
                                if (mdi->filter.delay_pos[3][1] == delay_size[3][1]) mdi->filter.delay_pos[3][1] = 0;
 
4493
 
 
4494
                        }
 
4495
#endif                          
 
4496
                        if (left_mix > 32767) {
 
4497
                                left_mix = 32767;
 
4498
                        } else if (left_mix < -32768) {
 
4499
                                left_mix = -32768;
 
4500
                        }
 
4501
 
 
4502
                        if (right_mix > 32767) {
 
4503
                                right_mix = 32767;
 
4504
                        } else if (right_mix < -32768) {
 
4505
                                right_mix = -32768;
 
4506
                        }
 
4507
 
 
4508
 
 
4509
/*
 
4510
 * ===================
 
4511
 * Write to the buffer
 
4512
 * ===================
 
4513
 */
 
4514
                        (*buffer++) = left_mix & 0xff;
 
4515
                        (*buffer++) = (left_mix >> 8) & 0xff;
 
4516
                        (*buffer++) = right_mix & 0xff;
 
4517
                        (*buffer++) = (right_mix >> 8) & 0xff;
 
4518
                } while (--count);
 
4519
                
 
4520
                buffer_used += real_samples_to_mix * 4;
 
4521
                size -= (real_samples_to_mix << 2);
 
4522
                mdi->info.current_sample += real_samples_to_mix;
 
4523
                mdi->sample_count -= real_samples_to_mix;
 
4524
                if (mdi->index_count == mdi->index_size) {
 
4525
                        if (mdi->last_note == 0) {
 
4526
                                mdi->sample_count = 0;
 
4527
                                WM_Unlock(&mdi->lock);
 
4528
                                return buffer_used;
 
4529
                        }
 
4530
                }
 
4531
        } while (size);
 
4532
        
 
4533
        if ((mdi->index_count == mdi->index_size) && (mdi->recalc_samples)) {
 
4534
                WM_RecalcSamples(mdi);
 
4535
                mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
 
4536
        }
 
4537
        WM_Unlock(&mdi->lock);
 
4538
        return buffer_used;
 
4539
}
 
4540
 
 
4541
int
 
4542
WildMidi_GetOutput_Gauss (midi * handle, char * buffer, unsigned long int size) {
 
4543
        unsigned long int buffer_used = 0;
 
4544
        struct _mdi *mdi = (struct _mdi *)handle;
 
4545
        unsigned long int real_samples_to_mix = 0;
 
4546
        unsigned long int data_pos;
 
4547
        signed long int premix, left_mix, right_mix;
 
4548
        signed long int vol_mul;
 
4549
        struct _note **note_data = NULL;
 
4550
        unsigned long int count;
 
4551
        signed short int *sptr;
 
4552
        double y, xd;
 
4553
        float *gptr, *gend;
 
4554
        int left, right, temp_n;
 
4555
        int ii, jj;
 
4556
 
 
4557
 
 
4558
        if (__builtin_expect((!WM_Initialized),0)) {
 
4559
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
4560
                return -1;
 
4561
        }
 
4562
        if (__builtin_expect((handle == NULL),0)) {
 
4563
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
 
4564
                return -1;
 
4565
        }
 
4566
        if (__builtin_expect((buffer == NULL),0)) {
 
4567
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL buffer pointer)", 0);
 
4568
                return -1;
 
4569
        }
 
4570
 
 
4571
        if (__builtin_expect((size == 0),0)) {
 
4572
                return 0;
 
4573
        }
 
4574
 
 
4575
        if (__builtin_expect((size % 4),0)) {
 
4576
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(size not a multiple of 4)", 0);
 
4577
                return -1;
 
4578
        }
 
4579
 
 
4580
        WM_Lock(&mdi->lock);
 
4581
        if (__builtin_expect(((mdi->index_count == mdi->index_size) && (mdi->last_note == 0)), 0)) {
 
4582
                WM_Unlock(&mdi->lock);
 
4583
                return 0;
 
4584
        }
 
4585
 
 
4586
        buffer_used = 0;
 
4587
        memset(buffer, 0, size);
 
4588
                
 
4589
        do {
 
4590
                if (__builtin_expect((!mdi->sample_count),0)) {
 
4591
                        if (__builtin_expect((mdi->index_count != mdi->index_size),1)) {
 
4592
                                do {
 
4593
                                        if (__builtin_expect((mdi->index_count == mdi->index_size), 0)) {
 
4594
                                                break;
 
4595
                                        }
 
4596
                                        
 
4597
                                        if (__builtin_expect((mdi->index_count != 0), 1)) {
 
4598
                                                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);
 
4599
                                        }
 
4600
                                } while (mdi->index[mdi->index_count++].delta == 0);
 
4601
                                
 
4602
                                mdi->samples_to_mix += mdi->index[mdi->index_count-1].delta * mdi->samples_per_delta;
 
4603
                                mdi->sample_count = mdi->samples_to_mix >> 10;
 
4604
                                mdi->samples_to_mix %= 1024;
 
4605
                        } else {
 
4606
                                if (mdi->recalc_samples) {
 
4607
                                        WM_RecalcSamples(mdi);
 
4608
                                }
 
4609
                                mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
 
4610
                                if (mdi->sample_count == 0) {
 
4611
                                        WM_Unlock(&mdi->lock);
 
4612
                                        return buffer_used;
 
4613
                                }
 
4614
                        }
 
4615
                }
 
4616
                if (__builtin_expect((mdi->sample_count > (size >> 2)),1)) {
 
4617
                        real_samples_to_mix = size >> 2;
 
4618
                } else {
 
4619
                        real_samples_to_mix = mdi->sample_count;
 
4620
                        if (real_samples_to_mix == 0) {
 
4621
                                continue;
 
4622
                        }
 
4623
                }
 
4624
                
 
4625
                // do mixing here
 
4626
                count = real_samples_to_mix;
 
4627
                do {
 
4628
                        note_data = mdi->note;
 
4629
                        left_mix = right_mix = 0;
 
4630
                        if (__builtin_expect((mdi->last_note != mdi->note),1)) {
 
4631
                                while (note_data != mdi->last_note) {
 
4632
/*
 
4633
 * ===================
 
4634
 * resample the sample
 
4635
 * ===================
 
4636
 */
 
4637
                                        data_pos = (*note_data)->sample_pos >> FPBITS;
 
4638
                                        vol_mul = (((*note_data)->vol_lvl * ((*note_data)->env_level >> 12)) >> FPBITS);
 
4639
 
 
4640
                                        /* check to see if we're near one of the ends */
 
4641
                                        left = data_pos;
 
4642
                                        right = ((*note_data)->sample->data_length>>FPBITS)- left -1;
 
4643
                                        temp_n = (right<<1)-1;
 
4644
                                        if (temp_n <= 0)
 
4645
                                                temp_n = 1;
 
4646
                                        if (temp_n > (left<<1)+1)
 
4647
                                                temp_n = (left<<1)+1;
 
4648
 
 
4649
                                        /* use Newton if we can't fill the window */
 
4650
                                        if (temp_n < gauss_n) {
 
4651
                                                xd = (*note_data)->sample_pos & FPMASK;
 
4652
                                                xd /= (1L<<FPBITS);
 
4653
                                                xd += temp_n>>1;
 
4654
                                                y = 0;
 
4655
                                                sptr = (*note_data)->sample->data + ((*note_data)->sample_pos>>FPBITS) - (temp_n>>1);
 
4656
                                                for (ii = temp_n; ii;) {
 
4657
                                                        for (jj = 0; jj <= ii; jj++)
 
4658
                                                                y += sptr[jj] * newt_coeffs[ii][jj];
 
4659
                                                        y *= xd - --ii;
 
4660
                                                }
 
4661
                                                y += *sptr;
 
4662
                                        } else {                        /* otherwise, use Gauss as usual */
 
4663
                                                y = 0;
 
4664
                                                gptr = gauss_table[(*note_data)->sample_pos & FPMASK];
 
4665
                                                gend = gptr + gauss_n;
 
4666
                                                sptr = (*note_data)->sample->data + ((*note_data)->sample_pos >> FPBITS) - (gauss_n>>1);
 
4667
                                                do {
 
4668
                                                        y += *(sptr++) * *(gptr++);
 
4669
                                                } while (gptr <= gend);
 
4670
                                        }
 
4671
 
 
4672
                                        premix = y * vol_mul / 1024;
 
4673
 
 
4674
                                        left_mix += premix * mdi->channel[(*note_data)->noteid >> 8].left_adjust;
 
4675
                                        right_mix += premix * mdi->channel[(*note_data)->noteid >> 8].right_adjust;
 
4676
 
 
4677
/*
 
4678
 * ========================
 
4679
 * sample position checking
 
4680
 * ========================
 
4681
 */
 
4682
                                        (*note_data)->sample_pos += (*note_data)->sample_inc;
 
4683
                                        if (__builtin_expect(((*note_data)->sample_pos > (*note_data)->sample->loop_end), 0)) {
 
4684
                                                if ((*note_data)->modes & SAMPLE_LOOP) {
 
4685
                                                        (*note_data)->sample_pos = (*note_data)->sample->loop_start + (((*note_data)->sample_pos - (*note_data)->sample->loop_start) % (*note_data)->sample->loop_size);
 
4686
                                                } else if (__builtin_expect(((*note_data)->sample_pos >= (*note_data)->sample->data_length), 0)) {
 
4687
                                                        if (__builtin_expect(((*note_data)->next == NULL), 1)) {
 
4688
                                                                goto KILL_NOTE;
 
4689
 
 
4690
                                                        }
 
4691
                                                        goto RESTART_NOTE;
 
4692
                                                }
 
4693
                                        }
 
4694
 
 
4695
                                        if (__builtin_expect(((*note_data)->env_inc == 0), 0)) {
 
4696
                                                note_data++;
 
4697
                                                continue;
 
4698
                                        }
 
4699
 
 
4700
                                        (*note_data)->env_level += (*note_data)->env_inc;
 
4701
                                        if (__builtin_expect(((*note_data)->env_level > 4194304), 0)) {
 
4702
                                                (*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
 
4703
                                        } 
 
4704
                                        if (__builtin_expect((((*note_data)->env_inc < 0) && 
 
4705
                                                        ((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env])) ||
 
4706
                                                        (((*note_data)->env_inc > 0) && 
 
4707
                                                        ((*note_data)->env_level < (*note_data)->sample->env_target[(*note_data)->env])), 1)) {
 
4708
                                                note_data++;
 
4709
                                                continue;
 
4710
                                        }
 
4711
 
 
4712
                                        (*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
 
4713
                                        switch ((*note_data)->env) {
 
4714
                                                case 0:
 
4715
                                                        if (!((*note_data)->modes & SAMPLE_ENVELOPE)) {
 
4716
                                                                (*note_data)->env_inc = 0;
 
4717
                                                                note_data++;
 
4718
                                                                continue;
 
4719
                                                        }
 
4720
                                                        break;
 
4721
                                                case 2:
 
4722
                                                        if ((*note_data)->modes & SAMPLE_SUSTAIN) {
 
4723
                                                                (*note_data)->env_inc = 0;
 
4724
                                                                note_data++;
 
4725
                                                                continue;
 
4726
                                                        }
 
4727
                                                        break;
 
4728
                                                case 5:
 
4729
                                                        if (__builtin_expect(((*note_data)->env_level == 0), 1)) {
 
4730
                                                                goto KILL_NOTE;
 
4731
                                                        }
 
4732
                                                        // sample release
 
4733
                                                        if ((*note_data)->modes & SAMPLE_LOOP)
 
4734
                                                                (*note_data)->modes ^= SAMPLE_LOOP;
 
4735
                                                        (*note_data)->env_inc = 0;
 
4736
                                                        note_data++;
 
4737
                                                        continue;
 
4738
                                                case 6:
 
4739
                                                        if (__builtin_expect(((*note_data)->next != NULL), 1)) {
 
4740
                                                                RESTART_NOTE:
 
4741
                                                                (*note_data)->active = 0;
 
4742
                                                                *note_data = (*note_data)->next;
 
4743
                                                                (*note_data)->active = 1;
 
4744
                                                                note_data++;
 
4745
                                                                
 
4746
                                                        } else {
 
4747
                                                                KILL_NOTE:
 
4748
                                                                (*note_data)->active = 0;
 
4749
                                                                mdi->last_note--;
 
4750
                                                                if (note_data != mdi->last_note) {
 
4751
                                                                        *note_data = *mdi->last_note;
 
4752
                                                                }
 
4753
                                                        }
 
4754
                                                        continue;
 
4755
                                        }
 
4756
                                        (*note_data)->env++;
 
4757
                                        if ((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env]) {
 
4758
                                                (*note_data)->env_inc = -(*note_data)->sample->env_rate[(*note_data)->env];
 
4759
                                        } else {
 
4760
                                                (*note_data)->env_inc = (*note_data)->sample->env_rate[(*note_data)->env];
 
4761
                                        }
 
4762
                                        note_data++;
 
4763
                                        continue;
 
4764
                                }
 
4765
/*
 
4766
 * =========================
 
4767
 * mix the channels together
 
4768
 * =========================
 
4769
 */
 
4770
                                
 
4771
                                left_mix /= 1024;
 
4772
                                right_mix /= 1024;
 
4773
                        }
 
4774
 
 
4775
#ifdef EXPERIMENT_626
 
4776
/*
 
4777
 * ==========================
 
4778
 * Experimental Reverb Engine
 
4779
 * ==========================
 
4780
 */
 
4781
 
 
4782
                        if (mdi->info.mixer_options & WM_MO_REVERB) {
 
4783
                                signed long int filteral = mdi->filter.delay[0][0][mdi->filter.delay_pos[0][0]];
 
4784
                                signed long int filterar = mdi->filter.delay[0][1][mdi->filter.delay_pos[0][1]];
 
4785
                                signed long int filterbl = mdi->filter.delay[1][0][mdi->filter.delay_pos[1][0]];
 
4786
                                signed long int filterbr = mdi->filter.delay[1][1][mdi->filter.delay_pos[1][1]];
 
4787
                                signed long int filtercl = mdi->filter.delay[2][0][mdi->filter.delay_pos[2][0]];
 
4788
                                signed long int filtercr = mdi->filter.delay[2][1][mdi->filter.delay_pos[2][1]];
 
4789
                                signed long int filterdl = mdi->filter.delay[3][0][mdi->filter.delay_pos[3][0]];
 
4790
                                signed long int filterdr = mdi->filter.delay[3][1][mdi->filter.delay_pos[3][1]];
 
4791
                                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;
 
4792
                                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;
 
4793
                                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;
 
4794
                                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;
 
4795
                                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;
 
4796
                                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;
 
4797
                                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;
 
4798
                                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;
 
4799
                                signed long int tfl, tflo;
 
4800
                                signed long int tfr, tfro;
 
4801
                                
 
4802
                                mdi->filter.lowpass[0][0].in[1] = mdi->filter.lowpass[0][0].in[0];
 
4803
                                mdi->filter.lowpass[0][0].in[0] = filteral;
 
4804
                                mdi->filter.lowpass[0][1].in[1] = mdi->filter.lowpass[0][1].in[0];
 
4805
                                mdi->filter.lowpass[0][1].in[0] = filterar;
 
4806
                                mdi->filter.lowpass[1][0].in[1] = mdi->filter.lowpass[1][0].in[0];
 
4807
                                mdi->filter.lowpass[1][0].in[0] = filterbl;
 
4808
                                mdi->filter.lowpass[1][1].in[1] = mdi->filter.lowpass[1][1].in[0];
 
4809
                                mdi->filter.lowpass[1][1].in[0] = filterbr;
 
4810
                                mdi->filter.lowpass[2][0].in[1] = mdi->filter.lowpass[2][0].in[0];
 
4811
                                mdi->filter.lowpass[2][0].in[0] = filtercl;
 
4812
                                mdi->filter.lowpass[2][1].in[1] = mdi->filter.lowpass[2][1].in[0];
 
4813
                                mdi->filter.lowpass[2][1].in[0] = filtercr;
 
4814
                                mdi->filter.lowpass[3][0].in[1] = mdi->filter.lowpass[3][0].in[0];
 
4815
                                mdi->filter.lowpass[3][0].in[0] = filterdl;
 
4816
                                mdi->filter.lowpass[3][1].in[1] = mdi->filter.lowpass[3][1].in[0];
 
4817
                                mdi->filter.lowpass[3][1].in[0] = filterdr;
 
4818
 
 
4819
                                mdi->filter.lowpass[0][0].out[1] = mdi->filter.lowpass[0][0].out[0];
 
4820
                                mdi->filter.lowpass[0][0].out[0] = tfal;
 
4821
                                mdi->filter.lowpass[0][1].out[1] = mdi->filter.lowpass[0][1].out[0];
 
4822
                                mdi->filter.lowpass[0][1].out[0] = tfar;
 
4823
                                mdi->filter.lowpass[1][0].out[1] = mdi->filter.lowpass[1][0].out[0];
 
4824
                                mdi->filter.lowpass[1][0].out[0] = tfbl;
 
4825
                                mdi->filter.lowpass[1][1].out[1] = mdi->filter.lowpass[1][1].out[0];
 
4826
                                mdi->filter.lowpass[1][1].out[0] = tfbr;
 
4827
                                mdi->filter.lowpass[2][0].out[1] = mdi->filter.lowpass[2][0].out[0];
 
4828
                                mdi->filter.lowpass[2][0].out[0] = tfcl;
 
4829
                                mdi->filter.lowpass[2][1].out[1] = mdi->filter.lowpass[2][1].out[0];
 
4830
                                mdi->filter.lowpass[2][1].out[0] = tfcr;
 
4831
                                mdi->filter.lowpass[3][0].out[1] = mdi->filter.lowpass[3][0].out[0];
 
4832
                                mdi->filter.lowpass[3][0].out[0] = tfdl;
 
4833
                                mdi->filter.lowpass[3][1].out[1] = mdi->filter.lowpass[3][1].out[0];
 
4834
                                mdi->filter.lowpass[3][1].out[0] = tfdr;
 
4835
                                
 
4836
                                mdi->filter.delay[0][0][mdi->filter.delay_pos[0][0]] = (tfbr * 405 + tfcr * 368) / 1024 + (left_mix * gain_in[0] / 1024);
 
4837
                                mdi->filter.delay[0][1][mdi->filter.delay_pos[0][1]] = (tfbl * 402 + tfcl * 370) / 1024 + (right_mix * gain_in[0] / 1024);
 
4838
                                mdi->filter.delay[1][0][mdi->filter.delay_pos[1][0]] = (tfar * -545 + tfdr * -364) / 1024 + (left_mix * gain_in[1] / 1024);
 
4839
                                mdi->filter.delay[1][1][mdi->filter.delay_pos[1][1]] = (tfal * -550 + tfdl * -362) / 1024 + (right_mix * gain_in[1] / 1024);
 
4840
                                mdi->filter.delay[2][0][mdi->filter.delay_pos[2][0]] = (tfar * 545 + tfdr * -364) / 1024 + (left_mix * gain_in[2] / 1024);
 
4841
                                mdi->filter.delay[2][1][mdi->filter.delay_pos[2][1]] = (tfal * 550 + tfdl * 362) / 1024 + (right_mix * gain_in[2] / 1024);
 
4842
                                mdi->filter.delay[3][0][mdi->filter.delay_pos[3][0]] = (tfbr * 405 + tfcr * -368) / 1024 + (left_mix * gain_in[3] / 1024);
 
4843
                                mdi->filter.delay[3][1][mdi->filter.delay_pos[3][1]] = (tfbl * 402 + tfcl * -370) / 1024 + (right_mix * gain_in[3] / 1024);
 
4844
                                
 
4845
                                tfl = ((tfal * gain_out[0] / 1024) + (tfbl * gain_out[1] / 1024) + (tfcl * gain_out[2] / 1024) + (tfdl * gain_out[3] / 1024));
 
4846
                                tfr = ((tfar * gain_out[0] / 1024) + (tfbr * gain_out[1] / 1024) + (tfcr * gain_out[2] / 1024) + (tfdr * gain_out[3] / 1024));
 
4847
                                
 
4848
                                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;
 
4849
                                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; 
 
4850
                                
 
4851
                                mdi->filter.in[1][0] =  mdi->filter.in[0][0];
 
4852
                                mdi->filter.in[0][0] = tfl;
 
4853
                                mdi->filter.in[1][1] =  mdi->filter.in[0][1];
 
4854
                                mdi->filter.in[0][1] = tfr;
 
4855
                                mdi->filter.out[1][0] =  mdi->filter.out[0][0];
 
4856
                                mdi->filter.out[0][0] = tflo;
 
4857
                                mdi->filter.out[1][1] =  mdi->filter.out[0][1];
 
4858
                                mdi->filter.out[0][1] = tfro;
 
4859
                                
 
4860
                                left_mix += tflo;
 
4861
                                right_mix += tfro;
 
4862
                                
 
4863
                                mdi->filter.delay_pos[0][0]++;
 
4864
                                mdi->filter.delay_pos[0][1]++;
 
4865
                                mdi->filter.delay_pos[1][0]++;
 
4866
                                mdi->filter.delay_pos[1][1]++;
 
4867
                                mdi->filter.delay_pos[2][0]++;
 
4868
                                mdi->filter.delay_pos[2][1]++;
 
4869
                                mdi->filter.delay_pos[3][0]++;
 
4870
                                mdi->filter.delay_pos[3][1]++;
 
4871
                                
 
4872
                                if (mdi->filter.delay_pos[0][0] == delay_size[0][0]) mdi->filter.delay_pos[0][0] = 0;
 
4873
                                if (mdi->filter.delay_pos[0][1] == delay_size[0][1]) mdi->filter.delay_pos[0][1] = 0;
 
4874
                                if (mdi->filter.delay_pos[1][0] == delay_size[1][0]) mdi->filter.delay_pos[1][0] = 0;
 
4875
                                if (mdi->filter.delay_pos[1][1] == delay_size[1][1]) mdi->filter.delay_pos[1][1] = 0;
 
4876
                                if (mdi->filter.delay_pos[2][0] == delay_size[2][0]) mdi->filter.delay_pos[2][0] = 0;
 
4877
                                if (mdi->filter.delay_pos[2][1] == delay_size[2][1]) mdi->filter.delay_pos[2][1] = 0;
 
4878
                                if (mdi->filter.delay_pos[3][0] == delay_size[3][0]) mdi->filter.delay_pos[3][0] = 0;
 
4879
                                if (mdi->filter.delay_pos[3][1] == delay_size[3][1]) mdi->filter.delay_pos[3][1] = 0;
 
4880
 
 
4881
                        }
 
4882
#endif  
 
4883
                        if (left_mix > 32767) {
 
4884
                                left_mix = 32767;
 
4885
                        } else if (left_mix < -32768) {
 
4886
                                left_mix = -32768;
 
4887
                        }
 
4888
 
 
4889
                        if (right_mix > 32767) {
 
4890
                                right_mix = 32767;
 
4891
                        } else if (right_mix < -32768) {
 
4892
                                right_mix = -32768;
 
4893
                        }
 
4894
 
 
4895
 
 
4896
/*
 
4897
 * ===================
 
4898
 * Write to the buffer
 
4899
 * ===================
 
4900
 */
 
4901
                        (*buffer++) = left_mix & 0xff;
 
4902
                        (*buffer++) = (left_mix >> 8) & 0xff;
 
4903
                        (*buffer++) = right_mix & 0xff;
 
4904
                        (*buffer++) = (right_mix >> 8) & 0xff;
 
4905
                } while (--count);
 
4906
                
 
4907
                buffer_used += real_samples_to_mix * 4;
 
4908
                size -= (real_samples_to_mix << 2);
 
4909
                mdi->info.current_sample += real_samples_to_mix;
 
4910
                mdi->sample_count -= real_samples_to_mix;
 
4911
                if (mdi->index_count == mdi->index_size) {
 
4912
                        if (mdi->last_note == 0) {
 
4913
                                mdi->sample_count = 0;
 
4914
                                WM_Unlock(&mdi->lock);
 
4915
                                return buffer_used;
 
4916
                        }
 
4917
                }
 
4918
        } while (size);
 
4919
        
 
4920
        if ((mdi->index_count == mdi->index_size) && (mdi->recalc_samples)) {
 
4921
                WM_RecalcSamples(mdi);
 
4922
                mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
 
4923
        }
 
4924
        WM_Unlock(&mdi->lock);
 
4925
        return buffer_used;
 
4926
}
 
4927
 
 
4928
int
 
4929
WildMidi_GetOutput (midi * handle, char * buffer, unsigned long int size) {
 
4930
        struct _mdi *mdi = (struct _mdi *)handle;
 
4931
 
 
4932
        if (__builtin_expect((!WM_Initialized),0)) {
 
4933
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
4934
                return -1;
 
4935
        }
 
4936
        if (__builtin_expect((handle == NULL),0)) {
 
4937
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
 
4938
                return -1;
 
4939
        }
 
4940
        if (__builtin_expect((buffer == NULL),0)) {
 
4941
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL buffer pointer)", 0);
 
4942
                return -1;
 
4943
        }
 
4944
 
 
4945
        if (__builtin_expect((size == 0),0)) {
 
4946
                return 0;
 
4947
        }
 
4948
 
 
4949
        if (__builtin_expect((size % 4),0)) {
 
4950
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(size not a multiple of 4)", 0);
 
4951
                return -1;
 
4952
        }
 
4953
        if (mdi->info.mixer_options & WM_MO_EXPENSIVE_INTERPOLATION) {
 
4954
                return WildMidi_GetOutput_Gauss (handle, buffer,size); 
 
4955
        } else {
 
4956
                return WildMidi_GetOutput_Linear (handle, buffer, size); 
 
4957
        }
 
4958
}
 
4959
 
 
4960
int 
 
4961
WildMidi_SetOption (midi * handle, unsigned short int options, unsigned short int setting) {
 
4962
        struct _mdi *mdi = (struct _mdi *)handle;
 
4963
        struct _note **note_data = mdi->note;
 
4964
        int i;
 
4965
        
 
4966
        if (!WM_Initialized) {
 
4967
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
4968
                return -1;
 
4969
        }
 
4970
        if (handle == NULL) {
 
4971
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
 
4972
                return -1;
 
4973
        }
 
4974
        WM_Lock(&mdi->lock);
 
4975
        if ((!(options & 0x0007)) || (options & 0xFFF8)){
 
4976
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(invalid option)", 0);
 
4977
                WM_Unlock(&mdi->lock);
 
4978
                return -1;
 
4979
        }
 
4980
        if (setting & 0xFFF8) {
 
4981
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(invalid setting)", 0);
 
4982
                WM_Unlock(&mdi->lock);
 
4983
                return -1;
 
4984
        }
 
4985
 
 
4986
        mdi->info.mixer_options = ((mdi->info.mixer_options & (0x00FF ^ options)) | (options & setting));
 
4987
 
 
4988
        if (options & WM_MO_LINEAR_VOLUME) {
 
4989
                if (mdi->info.mixer_options & WM_MO_LINEAR_VOLUME) {
 
4990
                        mdi->amp = 281;
 
4991
                } else {
 
4992
                        mdi->amp = 281 * mdi->lin_max_vol / mdi->log_max_vol;
 
4993
                }
 
4994
                for (i = 0; i < 16; i++) {
 
4995
                        do_pan_adjust(mdi, i);
 
4996
                }
 
4997
                if (note_data != mdi->last_note) {
 
4998
                        do {
 
4999
                                (*note_data)->vol_lvl = get_volume(mdi, ((*note_data)->noteid >> 8), *note_data);
 
5000
                                if ((*note_data)->next)
 
5001
                                        (*note_data)->next->vol_lvl = get_volume(mdi, ((*note_data)->noteid >> 8), (*note_data)->next);
 
5002
                                note_data++;    
 
5003
                        } while (note_data != mdi->last_note);
 
5004
                }
 
5005
        }
 
5006
 
 
5007
        if (options & WM_MO_REVERB) {           
 
5008
                for (i = 0; i < 4; i++) {
 
5009
                        mdi->filter.lowpass[i][0].in[0] = 0;
 
5010
                        mdi->filter.lowpass[i][0].in[1] = 0;
 
5011
                        mdi->filter.lowpass[i][1].in[0] = 0;
 
5012
                        mdi->filter.lowpass[i][1].in[1] = 0;            
 
5013
 
 
5014
                        mdi->filter.lowpass[i][0].out[0] = 0;
 
5015
                        mdi->filter.lowpass[i][0].out[1] = 0;
 
5016
                        mdi->filter.lowpass[i][1].out[0] = 0;
 
5017
                        mdi->filter.lowpass[i][1].out[1] = 0;
 
5018
                
 
5019
                        mdi->filter.delay_pos[i][0] = 0;
 
5020
                        mdi->filter.delay_pos[i][1] = 0;
 
5021
                        
 
5022
                        memset (mdi->filter.delay[i][0], 0, (delay_size[i][0] * sizeof(signed long int)));
 
5023
                        memset (mdi->filter.delay[i][1], 0, (delay_size[i][1] * sizeof(signed long int)));
 
5024
                }
 
5025
        }
 
5026
        WM_Unlock(&mdi->lock);
 
5027
        return 0;
 
5028
}
 
5029
 
 
5030
struct _WM_Info * 
 
5031
WildMidi_GetInfo (midi * handle) {
 
5032
        struct _mdi *mdi = (struct _mdi *)handle;
 
5033
        if (!WM_Initialized) {
 
5034
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
5035
                return NULL;
 
5036
        }
 
5037
        if (handle == NULL) {
 
5038
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
 
5039
                return NULL;
 
5040
        }
 
5041
        WM_Lock(&mdi->lock);
 
5042
        if (mdi->tmp_info == NULL) {
 
5043
                mdi->tmp_info = malloc(sizeof(struct _WM_Info));
 
5044
                if (mdi->tmp_info == NULL) {
 
5045
                        WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to set info", 0);
 
5046
                        WM_Unlock(&mdi->lock);
 
5047
                        return NULL;
 
5048
                }
 
5049
        }
 
5050
        mdi->tmp_info->current_sample = mdi->info.current_sample;
 
5051
        mdi->tmp_info->approx_total_samples = mdi->info.approx_total_samples;
 
5052
        mdi->tmp_info->mixer_options = mdi->info.mixer_options;
 
5053
        WM_Unlock(&mdi->lock);
 
5054
        return mdi->tmp_info;
 
5055
}
 
5056
 
 
5057
int
 
5058
WildMidi_Shutdown ( void ) {
 
5059
        struct _hndl * tmp_hdle;
 
5060
 
 
5061
        if (!WM_Initialized) {
 
5062
                WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
 
5063
                return -1;
 
5064
        }
 
5065
        if (first_handle != NULL) {
 
5066
                while (first_handle != NULL) {
 
5067
                        tmp_hdle = first_handle->next;
 
5068
                        WildMidi_Close((struct _mdi *)first_handle->handle);
 
5069
                        free (first_handle);
 
5070
                        first_handle = tmp_hdle;                        
 
5071
                }
 
5072
        }
 
5073
        WM_FreePatches();
 
5074
        WM_Initialized = 0;
 
5075
        return 0;
 
5076
}
 
5077