4
Midi Wavetable Processing library
6
Copyright (C)2001-2004 Chris Ison
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.
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.
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
22
Email: cisos@bigpond.net.au
23
wildcode@users.sourceforge.net
25
$Id: wildmidi_lib.c,v 1.18 2004/01/28 05:45:09 wildcode Exp $
27
========================================
29
----------------------------------------
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
37
- added MIDI_EVENT_DEBUG to midi event functions
38
- fixed hold release killing off notes that hadn't been turned off
40
- sped up fake reverb by doing it outside the channel mixing loop
43
- removed note_table init from ParseNewMidi, entries are now fully reset
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,
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
58
- fixed volume, expression and preasure changes effecting all notes.
61
- spead up midi processing by using an event index
64
- spead up sampling and mixing by using a seperate function depending on fixed
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
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
76
- fixed expensive interpolation over-running the sample buffer
77
- changed stereo option so that changing it worked right away
80
- optimizations for and to the frequency calc code
81
- removal of wide stereo (it sucked anyway)
84
- fixed volume levels for panning when non-linear volumes are used
85
- removed fake reberb, it sucked on better sound systems.
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.
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.
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
111
- optimized sample conversions
112
- optimized note handling and sample/envelope position checks
115
- compile level optimizations
116
NOTE: gcc builtins used
120
- fixed sample inc calculation bug
121
- fixed panning volumes, now percieved volume of the sample is the same no matter
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
131
- Added all controllers off event
135
- added master sample data lock
136
- improved performance of the resampling algo
138
========================================
152
#include <sys/stat.h>
153
#include <sys/types.h>
156
# include <windows.h>
159
#include "wildmidi_lib.h"
162
* =========================
163
* Global Data and Data Structs
164
* =========================
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;
172
char WM_Version[] = "WildMidi Processing Library " WILDMIDILIB_VERSION;
175
signed long int in[2];
176
signed long int out[2];
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];
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;
204
unsigned long int env_rate[7];
205
unsigned long int env_target[7];
206
unsigned long int inc_div;
208
signed short max_peek;
209
signed short min_peek;
210
signed long int peek_adjust;
211
struct _sample *next;
215
unsigned short patchid;
216
unsigned char loaded;
218
signed short int amp;
220
unsigned char remove;
223
unsigned long int inuse_count;
224
struct _sample *first_sample;
228
struct _patch *patch[128];
234
struct _patch *patch;
236
unsigned char volume;
237
unsigned char pressure;
238
unsigned char expression;
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;
249
#define HOLD_OFF 0x02
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;
260
unsigned long int env_level;
263
unsigned char active;
265
signed short int vol_lvl;
269
unsigned long int length;
270
unsigned long int ptr;
271
unsigned long int delta;
272
unsigned char running_event;
276
struct _mdi_patches {
277
struct _patch *patch;
278
struct _mdi_patch *next;
282
unsigned long int offset;
284
unsigned long int delta;
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];
306
struct _patch **patches;
307
unsigned long int patch_count;
308
unsigned long int sample_count;
309
signed short int amp;
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;
317
unsigned char ch_vol[16];
318
unsigned char ch_exp[16];
319
unsigned char note_vel[16][128];
321
struct _filter filter;
324
/* Gauss Interpolation code adapted from code supplied by Eric. A. Welsh */
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 */
332
void init_gauss (void) {
333
/* init gauss table */
335
int m, i, k, n_half = (n>>1);
343
newt_coeffs[0][0] = 1;
345
for (i = 0; i <= n; i++) {
346
newt_coeffs[i][0] = 1;
347
newt_coeffs[i][i] = 1;
350
newt_coeffs[i][0] = newt_coeffs[i-1][0] / i;
351
newt_coeffs[i][i] = newt_coeffs[i-1][0] / i;
354
for (j = 1; j < i; j++) {
355
newt_coeffs[i][j] = newt_coeffs[i-1][j-1] + newt_coeffs[i-1][j];
357
newt_coeffs[i][j] /= i;
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;
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));
372
for (k = 0; k <= n; k++) {
375
for (i = 0; i <= n; i++) {
379
ck *= (sin(xz - z[i])) / (sin(z[k] - z[i]));
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];
392
void init_lowpass (void) {
395
float f[] = { 512.0, 1024.0, 2048.0, 4096.0 , 8192.0};
396
float aa, ab, ba, bb;
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);
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);
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;
435
struct _hndl * first_handle = NULL;
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 )
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 };
449
unsigned long int reverb_val = 92;
450
unsigned long int comb_size[8][2];
451
unsigned long int allpass_size[2][2];
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
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
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,
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
490
#define SAMPLE_CONVERT_DEBUG(dx) printf("\r%s\n",dx)
492
#define SAMPLE_CONVERT_DEBUG(dx)
496
#define MIDI_EVENT_DEBUG(dx,dy) printf("\r%s, %x\n",dx,dy)
498
#define MIDI_EVENT_DEBUG(dx,dy)
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
511
#define MAX_AUTO_AMP 2.0
514
#define FPMASK ((1L<<FPBITS)-1L)
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);
526
void (*do_event[])(unsigned char ch, struct _mdi *midifile, unsigned long int ptr) = {
532
*do_channel_pressure,
538
* =========================
540
* =========================
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",
552
"Invalid or Unsuported file format\0",
554
"Library not Initialized\0",
559
fprintf(stderr,"\rlibWildMidi(%s:%lu): ERROR %s %s (%s)\n",func, lne, errors[wmerno], wmfor, strerror(error));
561
fprintf(stderr,"\rlibWildMidi(%s:%lu): ERROR %s %s\n",func, lne, errors[wmerno], wmfor);
565
fprintf(stderr,"\rlibWildMidi(%s:%lu): ERROR %s (%s)\n",func, lne, errors[wmerno], strerror(error));
567
fprintf(stderr,"\rlibWildMidi(%s:%lu): ERROR %s\n",func, lne, errors[wmerno]);
574
WM_BufferFile (const char *filename, unsigned long int *size) {
577
struct stat buffer_stat;
580
struct passwd *pwd_ent;
581
char buffer_dir[1024];
584
char *buffer_file = malloc(strlen(filename) + 1);
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);
592
strcpy (buffer_file, filename);
594
if (strncmp(buffer_file,"~/",2) == 0) {
595
if ((pwd_ent = getpwuid (getuid ()))) {
596
home = pwd_ent->pw_dir;
598
home = getenv ("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);
608
memmove((buffer_file + strlen(home)), (buffer_file + 1), (strlen(buffer_file)));
609
strncpy (buffer_file, home,strlen(home));
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)] = '/';
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);
624
memmove((buffer_file + strlen(buffer_dir)), buffer_file, strlen(buffer_file)+1);
625
strncpy (buffer_file,buffer_dir,strlen(buffer_dir));
628
if (stat(buffer_file,&buffer_stat)) {
629
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_STAT, filename, errno);
633
*size = buffer_stat.st_size;
634
data = malloc(*size);
636
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, NULL, errno);
637
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_LOAD, filename, errno);
642
if ((buffer_fd = open(buffer_file,(O_RDONLY | O_BINARY))) == -1) {
644
if ((buffer_fd = open(buffer_file,O_RDONLY)) == -1) {
646
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_OPEN, filename, errno);
651
if (read(buffer_fd,data,*size) != buffer_stat.st_size) {
652
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_READ, filename, errno);
664
WM_Lock (int * wmlock) {
666
if (__builtin_expect(((*wmlock) == 0),1)) {
668
if (__builtin_expect(((*wmlock) == 1), 1)) {
682
WM_Unlock (int *wmlock) {
687
WM_InitPatches ( void ) {
689
for (i = 0; i < 128; i++) {
695
WM_FreePatches ( void ) {
697
struct _patch * tmp_patch;
698
struct _sample * tmp_sample;
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;
714
free (patch[i]->filename);
716
tmp_patch = patch[i]->next;
718
patch[i] = tmp_patch;
722
WM_Unlock(&patch_lock);
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;
740
if ((config_buffer = WM_BufferFile(config_file, &config_size)) == NULL) {
743
if (config_buffer == NULL) {
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);
755
free (config_buffer);
758
strncpy(config_dir, config_file, (dir_end - config_file + 1));
759
config_dir[dir_end - config_file + 1] = '\0';
763
while (config_ptr < config_size) {
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] = ' ';
772
if ((config_buffer[config_ptr] == ' ') && (config_ptr == line_start_ptr)) {
779
config_buffer[config_ptr] = '\0';
780
if (config_ptr == line_start_ptr) {
785
if (config_buffer[line_start_ptr] == '#') {
787
line_start_ptr = config_ptr;
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);
797
if (config_dir != NULL)
799
free (config_buffer);
802
strcpy(line_buffer, &config_buffer[line_start_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));
811
if ((chr_ptr = strchr(line_buffer, '#')) != NULL) {
814
if (line_buffer[strlen(line_buffer) -1] == ' ') {
815
while (line_buffer[strlen(line_buffer) -1] == ' ') {
816
line_buffer[strlen(line_buffer) -1] = '\0';
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);
829
free (config_buffer);
832
strcpy(config_dir, &line_buffer[4]);
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);
840
free (config_buffer);
843
strcpy(config_dir, &line_buffer[4]);
844
strcat(config_dir,"/");
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);
856
free (config_buffer);
859
strcpy(new_config,config_dir);
860
strcpy(&new_config[strlen(config_dir)], &line_buffer[7]);
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);
868
free (config_buffer);
871
strcpy(new_config, &line_buffer[7]);
873
if (WM_LoadConfig(new_config) == -1) {
876
free (config_buffer);
877
if (config_dir != NULL)
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);
888
if (config_dir != NULL)
891
free (config_buffer);
894
patchid = (atoi(&line_buffer[5]) & 0xFF ) << 8;
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);
901
if (config_dir != NULL)
904
free (config_buffer);
907
patchid = ((atoi(&line_buffer[8]) & 0xFF ) << 8) | 0x80;
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);
917
if (config_dir != NULL)
920
free (config_buffer);
923
tmp_patch = patch[(patchid & 0x7F)];
924
tmp_patch->patchid = patchid;
925
tmp_patch->filename = NULL;
926
tmp_patch->amp = 1024;
928
tmp_patch->next = NULL;
929
tmp_patch->first_sample = NULL;
930
tmp_patch->loaded = 0;
931
tmp_patch->inuse_count = 0;
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;
940
if (tmp_patch->next != NULL) {
941
while (tmp_patch->next != NULL) {
942
if (tmp_patch->next->patchid == patchid)
944
tmp_patch = tmp_patch->next;
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);
952
if (config_dir != NULL)
955
free (config_buffer);
958
tmp_patch = tmp_patch->next;
959
tmp_patch->patchid = patchid;
960
tmp_patch->filename = NULL;
961
tmp_patch->amp = 1024;
963
tmp_patch->next = NULL;
964
tmp_patch->first_sample = NULL;
965
tmp_patch->loaded = 0;
966
tmp_patch->inuse_count = 0;
968
tmp_patch = tmp_patch->next;
969
free (tmp_patch->filename);
970
tmp_patch->filename = NULL;
971
tmp_patch->amp = 1024;
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);
980
if (config_dir != NULL)
983
free (config_buffer);
986
tmp_patch = tmp_patch->next;
987
tmp_patch->patchid = patchid;
988
tmp_patch->filename = NULL;
989
tmp_patch->amp = 1024;
991
tmp_patch->next = NULL;
992
tmp_patch->first_sample = NULL;
993
tmp_patch->loaded = 0;
994
tmp_patch->inuse_count = 0;
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);
1004
if (config_dir != NULL)
1007
free (config_buffer);
1010
line_ptr = chr_ptr - line_buffer;
1011
chr_ptr = strchr(&line_buffer[line_ptr],' ');
1012
if (chr_ptr != NULL) {
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);
1022
if (config_dir != NULL)
1025
free (config_buffer);
1028
strcpy(tmp_patch->filename, config_dir);
1029
strcat(tmp_patch->filename, &line_buffer[line_ptr]);
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);
1037
free (config_buffer);
1040
strcpy(tmp_patch->filename, &line_buffer[line_ptr]);
1042
strcat(tmp_patch->filename, ".pat");
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);
1050
if (config_dir != NULL)
1053
free (config_buffer);
1056
strcpy(tmp_patch->filename, config_dir);
1057
strcat(tmp_patch->filename, &line_buffer[line_ptr]);
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);
1065
free (config_buffer);
1068
strcpy(tmp_patch->filename, &line_buffer[line_ptr]);
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;
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);
1088
tmp_patch->amp = ((atoi(&chr_ptr[4]) << 10) / 100);
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);
1096
tmp_patch->note = atoi(&chr_ptr[5]);
1099
chr_ptr = strstr(&line_buffer[line_ptr], "keep=loop");
1100
if (chr_ptr != NULL) {
1101
tmp_patch->keep |= SAMPLE_LOOP;
1103
chr_ptr = strstr(&line_buffer[line_ptr], "keep=env");
1104
if (chr_ptr != NULL) {
1105
tmp_patch->keep |= SAMPLE_ENVELOPE;
1107
chr_ptr = strstr(&line_buffer[line_ptr], "remove=sustain");
1108
if (chr_ptr != NULL) {
1109
tmp_patch->remove |= SAMPLE_SUSTAIN;
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);
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;
1121
tmp_patch->env[0].set |= 0x01;
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);
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;
1136
tmp_patch->env[1].set |= 0x01;
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);
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;
1150
tmp_patch->env[2].set |= 0x01;
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);
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;
1164
tmp_patch->env[3].set |= 0x01;
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);
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;
1178
tmp_patch->env[4].set |= 0x01;
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);
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;
1192
tmp_patch->env[5].set |= 0x01;
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);
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;
1206
tmp_patch->env[0].set |= 0x02;
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);
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;
1221
tmp_patch->env[1].set |= 0x02;
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);
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;
1235
tmp_patch->env[2].set |= 0x02;
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);
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;
1249
tmp_patch->env[3].set |= 0x02;
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);
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;
1263
tmp_patch->env[4].set |= 0x02;
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);
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;
1277
tmp_patch->env[5].set |= 0x02;
1286
if (line_buffer != NULL)
1289
free (config_buffer);
1291
if (config_dir != NULL)
1298
* sample data conversion functions
1299
* convert data to signed shorts
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;
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;
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;
1321
} while (read_data != read_end);
1325
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1329
/* 8bit signed ping pong */
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;
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;
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;
1353
} while (read_data != read_end);
1355
*write_data = (*read_data++ << 8);
1356
write_data_a = write_data + dloop_length;
1357
*write_data_a-- = *write_data;
1359
write_data_b = write_data + dloop_length;
1360
read_end = data + gus_sample->loop_end;
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;
1371
} while (read_data != read_end);
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)) {
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;
1385
} while (read_data != read_end);
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;
1394
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1398
/* 8bit signed reverse */
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;
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;
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;
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;
1426
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1431
/* 8bit signed reverse ping pong */
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;
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;
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;
1455
} while (read_data != read_end);
1457
*write_data = (*read_data-- << 8);
1458
write_data_a = write_data + dloop_length;
1459
*write_data_a-- = *write_data;
1461
write_data_b = write_data + dloop_length;
1462
read_end = data + gus_sample->loop_start;
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;
1473
} while (read_data != read_end);
1475
*write_data = (*read_data-- << 8);
1476
*write_data_b++ = *write_data;
1477
read_end = data - 1;
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;
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;
1494
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
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;
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;
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;
1517
} while (read_data != read_end);
1518
gus_sample->modes ^= SAMPLE_UNSIGNED;
1521
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1525
/* 8bit unsigned ping pong */
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;
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;
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;
1549
} while (read_data != read_end);
1551
*write_data = ((*read_data++) ^ 0x80) << 8;
1552
write_data_a = write_data + dloop_length;
1553
*write_data_a-- = *write_data;
1555
write_data_b = write_data + dloop_length;
1556
read_end = data + gus_sample->loop_end;
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;
1567
} while (read_data != read_end);
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)) {
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;
1581
} while (read_data != read_end);
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;
1590
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1594
/* 8bit unsigned reverse */
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;
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;
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;
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;
1622
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1626
/* 8bit unsigned reverse ping pong */
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;
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;
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;
1650
} while (read_data != read_end);
1652
*write_data = ((*read_data--) ^ 0x80) << 8;
1653
write_data_a = write_data + dloop_length;
1654
*write_data_a-- = *write_data;
1656
write_data_b = write_data + dloop_length;
1657
read_end = data + gus_sample->loop_start;
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;
1668
} while (read_data != read_end);
1670
*write_data = ((*read_data--) ^ 0x80) << 8;
1671
*write_data_b++ = *write_data;
1672
read_end = data - 1;
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;
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;
1689
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
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;
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;
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;
1713
} while (read_data < read_end);
1715
gus_sample->loop_start >>= 1;
1716
gus_sample->loop_end >>= 1;
1717
gus_sample->data_length >>= 1;
1720
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1724
/* 16bit signed ping pong */
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;
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;
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;
1749
} while (read_data < read_end);
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;
1756
write_data_b = write_data + (dloop_length >> 1);
1757
read_end = data + gus_sample->loop_end;
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;
1769
} while (read_data < read_end);
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)) {
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;
1785
} while (read_data < read_end);
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;
1797
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1801
/* 16bit signed reverse */
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;
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;
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;
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;
1833
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1838
/* 16bit signed reverse ping pong */
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;
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;
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;
1864
} while (read_data < read_end);
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;
1871
write_data_b = write_data + (dloop_length >> 1);
1872
read_end = data + gus_sample->loop_start;
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;
1884
} while (read_data < read_end);
1886
*write_data = ((*read_data--) << 8);
1887
*write_data |= *read_data--;
1888
*write_data_b++ = *write_data;
1889
read_end = data - 1;
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;
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;
1907
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1911
/* 16bit unsigned */
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;
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;
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;
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;
1938
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
1942
/* 16bit unsigned ping pong */
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;
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;
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;
1967
} while (read_data < read_end);
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;
1974
write_data_b = write_data + (dloop_length >> 1);
1975
read_end = data + gus_sample->loop_end;
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;
1987
} while (read_data < read_end);
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)) {
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;
2003
} while (read_data < read_end);
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;
2015
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
2019
/* 16bit unsigned reverse */
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;
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;
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;
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;
2051
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
2055
/* 16bit unsigned reverse ping pong */
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;
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;
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;
2080
} while (read_data < read_end);
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;
2087
write_data_b = write_data + (dloop_length >> 1);
2088
read_end = data + gus_sample->loop_start;
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;
2100
} while (read_data < read_end);
2102
*write_data = ((*read_data--) ^ 0x80) << 8;
2103
*write_data |= *read_data--;
2104
*write_data_b++ = *write_data;
2105
read_end = data - 1;
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;
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;
2123
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to parse sample", errno);
2128
/* sample loading */
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;
2139
int (*do_convert[])(unsigned char *data, struct _sample *gus_sample ) = {
2157
unsigned long int tmp_loop;
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) {
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);
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);
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);
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);
2190
no_of_samples = gus_patch[198];
2191
sample_patch->first_sample = NULL;
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;
2199
gus_sample->next = malloc(sizeof(struct _sample));
2200
gus_sample = gus_sample->next;
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);
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]);
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;
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]);
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;
2232
if (sample_patch->patchid & 0x0080) {
2233
if (!(sample_patch->keep & SAMPLE_LOOP)) {
2234
gus_sample->modes &= 0xFB;
2236
if (!(sample_patch->keep & SAMPLE_ENVELOPE)) {
2237
gus_sample->modes &= 0xBF;
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);
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);
2254
gus_sample->env_target[i] = 16448 * gus_patch[gus_ptr+43+i];
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)));
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]));
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]));
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]));
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];
2283
tmp_cnt = gus_sample->data_length;
2285
/* convert to float */
2286
gus_sample->min_peek = 0;
2287
gus_sample->max_peek = 0;
2289
if (do_convert[(((gus_sample->modes & 0x18) >> 1)| (gus_sample->modes & 0x03))](&gus_patch[gus_ptr], gus_sample) == -1) {
2294
if (gus_sample->max_peek > (-gus_sample->min_peek)) {
2295
gus_sample->peek_adjust = 33553408 / gus_sample->max_peek;
2297
gus_sample->peek_adjust = 33554432 / (-gus_sample->min_peek);
2299
gus_sample->peek_adjust = (gus_sample->peek_adjust * sample_patch->amp) >> 10;
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;
2314
get_patch_data(struct _mdi *mdi, unsigned short patchid) {
2315
struct _patch *search_patch;
2317
WM_Lock(&patch_lock);
2319
search_patch = patch[patchid & 0x007F];
2321
if (search_patch == NULL) {
2322
WM_Unlock(&patch_lock);
2326
while(search_patch != NULL) {
2327
if (search_patch->patchid == patchid) {
2328
WM_Unlock(&patch_lock);
2329
return search_patch;
2331
search_patch = search_patch->next;
2333
if ((patchid >> 8) != 0) {
2334
WM_Unlock(&patch_lock);
2335
return (get_patch_data(mdi, patchid & 0x00FF));
2337
WM_Unlock(&patch_lock);
2342
load_patch (struct _mdi *mdi, unsigned short patchid) {
2344
struct _patch *tmp_patch = NULL;
2346
for (i = 0; i < mdi->patch_count; i++) {
2347
if (mdi->patches[i]->patchid == patchid) {
2352
tmp_patch = get_patch_data(mdi, patchid);
2353
if (tmp_patch == NULL) {
2357
WM_Lock(&patch_lock);
2358
if (!tmp_patch->loaded) {
2359
if (load_sample(tmp_patch) == -1) {
2360
WM_Unlock(&patch_lock);
2365
if (tmp_patch->first_sample == NULL) {
2366
WM_Unlock(&patch_lock);
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);
2380
get_sample_data (struct _patch *sample_patch, unsigned long int freq) {
2381
struct _sample *last_sample = NULL;
2382
struct _sample *return_sample = NULL;
2384
WM_Lock(&patch_lock);
2385
if (sample_patch == NULL) {
2386
WM_Unlock(&patch_lock);
2389
if (sample_patch->first_sample == NULL) {
2390
WM_Unlock(&patch_lock);
2394
WM_Unlock(&patch_lock);
2395
return sample_patch->first_sample;
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);
2406
return_sample = last_sample;
2409
last_sample = last_sample->next;
2411
WM_Unlock(&patch_lock);
2412
return return_sample;
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;
2422
if (track->ptr > mdi->size) {
2423
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(too short)", 0);
2426
var_data = (var_data << 7);
2429
var_data |= mdi->data[track->ptr] & 0x7f;
2432
if (track->ptr > mdi->size) {
2433
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT, "(too short)", 0);
2441
do_note_off (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
2444
MIDI_EVENT_DEBUG(__FUNCTION__,ch);
2446
nte = &mdi->note_table[0][ch][mdi->data[ptr]];
2448
nte = &mdi->note_table[1][ch][mdi->data[ptr]];
2453
if ((ch == 9) && (!(nte->modes & SAMPLE_LOOP))) {
2458
nte->hold |= HOLD_OFF;
2461
if (nte->modes & SAMPLE_SUSTAIN) {
2463
if (nte->env_level > nte->sample->env_target[3]) {
2464
nte->env_inc = -nte->sample->env_rate[3];
2466
nte->env_inc = nte->sample->env_rate[3];
2473
if (nte->env_level > nte->sample->env_target[4]) {
2474
nte->env_inc = -nte->sample->env_rate[4];
2476
nte->env_inc = nte->sample->env_rate[4];
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;
2490
if (__builtin_expect((nte->patch->note != 0),0)) {
2491
note_f = nte->patch->note * 100;
2493
note_f = (nte->noteid & 0x7f) * 100;
2495
note_f += mdi->channel[ch].pitch_adjust;
2496
if (__builtin_expect((note_f < 0), 0)) {
2498
} else if (__builtin_expect((note_f > 12700), 0)) {
2501
freq = freq_table[(note_f % 1200)] >> (10 - (note_f / 1200));
2502
return (((freq / ((WM_SampleRate * 100) / 1024)) * 1024 / nte->sample->inc_div));
2505
inline signed short int
2506
get_volume(struct _mdi *mdi, unsigned char ch, struct _note *nte) {
2507
signed long int volume;
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;
2514
volume = (sqr_volume[mdi->channel[ch].volume] *
2515
sqr_volume[mdi->channel[ch].expression] *
2516
sqr_volume[nte->velocity]) / 1048576;
2518
return ((volume * nte->sample->peek_adjust) >> 10);
2522
do_note_on (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
2524
unsigned long int freq = 0;
2525
struct _patch *patch;
2526
struct _sample *sample;
2528
if (mdi->data[ptr+1] == 0x00) {
2529
do_note_off(ch, mdi, ptr);
2533
MIDI_EVENT_DEBUG(__FUNCTION__,ch);
2536
patch = mdi->channel[ch].patch;
2537
if (patch == NULL) {
2540
freq = freq_table[(mdi->data[ptr] % 12) * 100] >> (10 -(mdi->data[ptr] / 12));
2542
patch = get_patch_data(mdi, ((mdi->channel[ch].bank << 8) | mdi->data[ptr] | 0x80));
2543
if (patch == NULL) {
2547
freq = freq_table[(patch->note % 12) * 100] >> (10 -(patch->note / 12));
2549
freq = freq_table[(mdi->data[ptr] % 12) * 100] >> (10 -(mdi->data[ptr] / 12));
2553
sample = get_sample_data(patch, (freq / 100));
2555
if (sample == NULL) {
2559
nte = &mdi->note_table[0][ch][mdi->data[ptr]];
2562
if ((nte->modes & SAMPLE_ENVELOPE) && (nte->env < 3) && (!(nte->hold & HOLD_OFF)))
2564
nte->next = &mdi->note_table[1][ch][mdi->data[ptr]];
2566
nte->env_inc = -nte->sample->env_rate[6];
2567
nte = &mdi->note_table[1][ch][mdi->data[ptr]];
2569
if (mdi->note_table[1][ch][mdi->data[ptr]].active) {
2570
if ((nte->modes & SAMPLE_ENVELOPE) && (nte->env < 3) && (!(nte->hold & HOLD_OFF)))
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];
2576
*mdi->last_note = nte;
2581
nte->noteid = (ch << 8) | mdi->data[ptr];
2583
nte->sample = sample;
2584
nte->sample_pos = 0;
2585
nte->sample_inc = get_inc (mdi, nte);
2586
nte->velocity = mdi->data[ptr+1];
2588
nte->env_inc = nte->sample->env_rate[0];
2590
nte->modes = sample->modes;
2591
nte->hold = mdi->channel[ch].hold;
2592
nte->vol_lvl = get_volume(mdi, ch, nte);
2597
do_aftertouch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
2600
MIDI_EVENT_DEBUG(__FUNCTION__,ch);
2602
nte = &mdi->note_table[0][ch][mdi->data[ptr]];
2604
nte = &mdi->note_table[1][ch][mdi->data[ptr]];
2610
nte->velocity = mdi->data[ptr+1];
2611
nte->vol_lvl = get_volume(mdi, ch, nte);
2614
nte->next->velocity = mdi->data[ptr+1];
2615
nte->next->vol_lvl = get_volume(mdi, ch, nte->next);
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;
2625
if (pan_adjust > 63) {
2627
} else if (pan_adjust < -64) {
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;
2637
left = (pan_volume[127 - pan_adjust] * WM_MasterVolume * mdi->amp) / 1048576;
2638
right = (pan_volume[pan_adjust] * WM_MasterVolume * mdi->amp) / 1048576;
2641
mdi->channel[ch].left_adjust = left;
2642
mdi->channel[ch].right_adjust = right;
2646
do_control (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
2647
struct _note **note_data = mdi->note;
2649
switch (mdi->data[ptr]) {
2650
case 0: // Bank Select
2651
mdi->channel[ch].bank = mdi->data[ptr+1];
2659
case 6: // Data Entry Course
2660
if (mdi->channel[ch].reg_data == 0x0000) { // Pitch Bend Range
2662
data_tmp = mdi->channel[ch].pitch_range % 100;
2663
mdi->channel[ch].pitch_range = mdi->data[ptr+1] * 100 + data_tmp;
2666
case 7: // Channel Volume
2667
mdi->channel[ch].volume = mdi->data[ptr+1];
2669
if (note_data != mdi->last_note) {
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);
2677
} while (note_data != mdi->last_note);
2680
case 8: // Channel Balance
2681
mdi->channel[ch].balance = mdi->data[ptr+1] - 64;
2682
do_pan_adjust(mdi, ch);
2686
case 10: // Channel Pan
2687
mdi->channel[ch].pan = mdi->data[ptr+1] - 64;
2688
do_pan_adjust(mdi, ch);
2690
case 11: // Channel Expression
2691
mdi->channel[ch].expression = mdi->data[ptr+1];
2693
if (note_data != mdi->last_note) {
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);
2701
} while (note_data != mdi->last_note);
2731
case 38: // Data Entry Fine
2732
if (mdi->channel[ch].reg_data == 0x0000) { // Pitch Bend Range
2734
data_tmp = mdi->channel[ch].pitch_range / 100;
2735
mdi->channel[ch].pitch_range = (data_tmp * 100) + mdi->data[ptr+1];
2743
printf("\rController %i used\n",mdi->data[ptr]);
2766
case 64: // Channel Hold
2767
if (mdi->data[ptr+1] > 63) {
2768
mdi->channel[ch].hold = 1;
2770
mdi->channel[ch].hold = 0;
2771
if (note_data != mdi->last_note) {
2773
if (((*note_data)->noteid >> 8) == ch) {
2774
if ((*note_data)->hold & HOLD_OFF) {
2775
if ((*note_data)->modes & SAMPLE_ENVELOPE) {
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];
2782
(*note_data)->env_inc = (*note_data)->sample->env_rate[3];
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];
2792
(*note_data)->env_inc = (*note_data)->sample->env_rate[4];
2798
(*note_data)->hold = 0x00;
2801
} while (note_data != mdi->last_note);
2841
case 100: // Registered Param Fine
2842
mdi->channel[ch].reg_data = (mdi->channel[ch].reg_data & 0xFF00) | mdi->data[ptr+1];
2844
case 101: // Registered Param Course
2845
mdi->channel[ch].reg_data = (mdi->channel[ch].reg_data & 0xFF) | (mdi->data[ptr+1] << 8);
2866
case 120: // All Channel Sound Off
2867
if (note_data != mdi->last_note) {
2869
if (((*note_data)->noteid >> 8) == ch) {
2870
(*note_data)->active = 0;
2871
if ((*note_data)->next) {
2872
(*note_data)->next = NULL;
2876
} while (note_data != mdi->last_note);
2877
mdi->last_note = mdi->note;
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);
2893
if (note_data != mdi->last_note) {
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;
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);
2908
} while (note_data != mdi->last_note);
2913
case 123: // All Channel Notes Off
2916
if (note_data != mdi->last_note) {
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];
2925
(*note_data)->env_inc = (*note_data)->sample->env_rate[5];
2927
(*note_data)->env = 5;
2931
(*note_data)->hold |= HOLD_OFF;
2935
} while (note_data != mdi->last_note);
2947
do_patch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
2948
MIDI_EVENT_DEBUG(__FUNCTION__,ch);
2950
mdi->channel[ch].patch = get_patch_data(mdi, ((mdi->channel[ch].bank << 8) | mdi->data[ptr]));
2952
mdi->channel[ch].bank = mdi->data[ptr];
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);
2961
if (note_data != mdi->last_note) {
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);
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);
2973
} while (note_data != mdi->last_note);
2978
do_pitch (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
2979
struct _note **note_data = mdi->note;
2981
MIDI_EVENT_DEBUG(__FUNCTION__,ch);
2982
mdi->channel[ch].pitch = ((mdi->data[ptr+1] << 7) | mdi->data[ptr]) - 0x2000;
2984
if (mdi->channel[ch].pitch < 0) {
2985
mdi->channel[ch].pitch_adjust = mdi->channel[ch].pitch_range * mdi->channel[ch].pitch / 8192;
2987
mdi->channel[ch].pitch_adjust = mdi->channel[ch].pitch_range * mdi->channel[ch].pitch / 8191;
2990
if (note_data != mdi->last_note) {
2992
if (((*note_data)->noteid >> 8 ) == ch) {
2993
(*note_data)->sample_inc = get_inc (mdi, *note_data);
2996
} while (note_data != mdi->last_note);
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;
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];
3010
mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
3012
mdi->samples_per_delta = (WM_SampleRate << 10) / ((1000000 * mdi->divisions) / tempo);
3019
do_null (unsigned char ch, struct _mdi *mdi, unsigned long int ptr) {
3020
MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3025
WM_ResetToStart(midi * handle) {
3026
struct _mdi *mdi = (struct _mdi *)handle;
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;
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;
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;
3060
if (note_data != mdi->last_note) {
3062
env_level = (*note_data)->env_level;
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];
3069
count_a += ((*note_data)->sample->env_target[3] - env_level + (*note_data)->sample->env_rate[3] - 1) / (*note_data)->sample->env_rate[3];
3071
env_level = (*note_data)->sample->env_target[3];
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];
3077
count_a += ((*note_data)->sample->env_target[4] - env_level + (*note_data)->sample->env_rate[4] - 1) / (*note_data)->sample->env_rate[4];
3079
env_level = (*note_data)->sample->env_target[4];
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];
3085
count_a += ((*note_data)->sample->env_target[5] - env_level + (*note_data)->sample->env_rate[5] - 1) / (*note_data)->sample->env_rate[5];
3087
env_level = (*note_data)->sample->env_target[5];
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];
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;
3100
count_a += (((*note_data)->sample->data_length << 10) - smpl_pos + (*note_data)->sample_inc - 1) / (*note_data)->sample_inc;
3103
count_b = (((*note_data)->sample->data_length << 10) - (*note_data)->sample_pos + (*note_data)->sample_inc - 1) / (*note_data)->sample_inc;
3106
if (count_b < count_a) {
3107
if (total_samples < count_b)
3108
total_samples = count_b;
3110
if (total_samples < count_a)
3111
total_samples = count_a;
3114
if (total_samples < count_a)
3115
total_samples = count_a;
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;
3124
if (total_samples < count_a)
3125
total_samples = count_a;
3128
if (total_samples < count_a)
3129
total_samples = count_a;
3133
} while (note_data != mdi->last_note);
3135
mdi->info.approx_total_samples += total_samples;
3136
mdi->recalc_samples = 0;
3140
do_amp_setup_note_off (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
3141
MIDI_EVENT_DEBUG(__FUNCTION__,ch);
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;
3148
mdi->note_vel[ch][mdi->data[track->ptr]] = 0;
3150
track->running_event = 0x80 | ch;
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;
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;
3171
mdi->note_vel[ch][mdi->data[track->ptr]] = mdi->data[track->ptr+1];
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;
3178
if (mdi->lin_cur_vol > mdi->lin_max_vol) {
3179
mdi->lin_max_vol = mdi->lin_cur_vol;
3181
if (mdi->log_cur_vol > mdi->log_max_vol) {
3182
mdi->log_max_vol = mdi->log_cur_vol;
3185
load_patch(mdi, ((mdi->channel[ch].bank << 8) | (mdi->data[track->ptr] | 0x80)));
3188
track->running_event = 0x90 | ch;
3194
do_amp_setup_aftertouch (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
3195
unsigned char pres = mdi->data[track->ptr+1];
3197
MIDI_EVENT_DEBUG(__FUNCTION__,ch);
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;
3208
mdi->note_vel[ch][mdi->data[track->ptr]] = pres;
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;
3215
if (mdi->lin_cur_vol > mdi->lin_max_vol) {
3216
mdi->lin_max_vol = mdi->lin_cur_vol;
3218
if (mdi->log_cur_vol > mdi->log_max_vol) {
3219
mdi->log_max_vol = mdi->log_cur_vol;
3222
track->running_event = 0xA0 | ch;
3229
do_amp_setup_control (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
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)
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;
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;
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;
3254
if (mdi->log_cur_vol > mdi->log_max_vol) {
3255
mdi->log_max_vol = mdi->log_cur_vol;
3257
} else if (mdi->data[track->ptr] == 0x0B) {
3258
for (i=0; i < 128; i++) {
3259
if (mdi->note_vel[ch][i] == 0)
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;
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;
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;
3276
if (mdi->log_cur_vol > mdi->log_max_vol) {
3277
mdi->log_max_vol = mdi->log_cur_vol;
3281
track->running_event = 0xB0 | ch;
3287
do_amp_setup_patch (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
3289
mdi->channel[ch].bank = mdi->data[track->ptr];
3291
load_patch(mdi, ((mdi->channel[ch].bank << 8) | mdi->data[track->ptr]));
3293
track->running_event = 0xC0 | ch;
3299
do_amp_setup_channel_pressure (unsigned char ch, struct _mdi *mdi, struct _miditrack *track) {
3301
unsigned char pres = mdi->data[track->ptr];
3303
MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3308
for (i=0; i < 128; i++) {
3309
if (mdi->note_vel[ch][i] == 0)
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;
3316
mdi->note_vel[ch][i] = pres;
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;
3324
if (mdi->lin_cur_vol > mdi->lin_max_vol) {
3325
mdi->lin_max_vol = mdi->lin_cur_vol;
3327
if (mdi->log_cur_vol > mdi->log_max_vol) {
3328
mdi->log_max_vol = mdi->log_cur_vol;
3330
track->running_event = 0xD0 | ch;
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;
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;
3350
MIDI_EVENT_DEBUG(__FUNCTION__,ch);
3351
if (event_type == 0xF0) {
3352
track->running_event = 0x00;
3355
} while ( mdi->data[track->ptr] != 0xF7);
3359
event_length = read_var_length(mdi, track);
3360
if (event_length == 0xFFFFFFFF) {
3361
track->delta = 0xFFFFFFFF;
3364
if (event_type == 0xFF) {
3365
if ((event_data == 0x2F) && (event_length == 0)) { // Track End
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];
3371
mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
3373
mdi->samples_per_delta = (WM_SampleRate << 10) / ((1000000 * mdi->divisions) / tempo);
3376
track->ptr += event_length;
3382
WM_ParseNewMidi(unsigned char *mididata, unsigned long int midisize ) {
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
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;
3408
mdi = malloc(sizeof(struct _mdi));
3410
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM," to parse midi data", errno);
3416
memset(mdi, 0, sizeof(struct _mdi));
3418
mdi->data = mididata;
3419
mdi->size = midisize;
3420
mdi->info.mixer_options = WM_MixerOptions;
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);
3429
load_patch(mdi, 0x0000);
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);
3443
if (strncmp(mididata,"RIFF",4) == 0)
3446
if (strncmp(&mididata[midiofs],"MThd",4) != 0) {
3447
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID,"(not a midi file)", 0);
3453
if ((midiofs + 25) > midisize) {
3454
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_CORUPT,"(too short)", 0);
3461
if (mididata[midiofs] > 1) {
3462
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID, NULL, 0);
3469
no_tracks = mididata[midiofs] << 8 | mididata[midiofs+1];
3472
mdi->divisions = mididata[midiofs] << 8 | mididata[midiofs+1];
3473
mdi->samples_per_delta = (WM_SampleRate << 10) / (2 * mdi->divisions);
3476
tmp_trackdata = calloc(no_tracks, sizeof(struct _miditrack));
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);
3486
first_handle->handle = (void *)mdi;
3487
first_handle->prev = NULL;
3488
first_handle->next = NULL;
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;
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);
3503
tmp_handle->next->prev = tmp_handle;
3504
tmp_handle = tmp_handle->next;
3505
tmp_handle->next = NULL;
3506
tmp_handle->handle = (void *)mdi;
3510
// grab track offsets;
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);
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);
3527
tmp_trackdata[midi_track_counter].length = mididata[midiofs] << 24 | mididata[midiofs+1] << 16 | mididata[midiofs+2] << 8 | mididata[midiofs+3];
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);
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);
3545
midiofs += tmp_trackdata[midi_track_counter].length;
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);
3553
if (tmp_trackdata[midi_track_counter].delta < temp_delta) {
3554
temp_delta = tmp_trackdata[midi_track_counter].delta;
3556
midi_track_counter++;
3560
mdi->index[0].offset = 0;
3561
mdi->index[0].delta = temp_delta;
3563
while (EOT_count != no_tracks) {
3565
for (i = 0; i < no_tracks; i++) {
3566
if (tmp_trackdata[i].EOT) {
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;
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);
3588
current_event = mdi->data[tmp_trackdata[i].ptr];
3589
tmp_trackdata[i].ptr++;
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;
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);
3604
if (tmp_trackdata[i].EOT) {
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);
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;
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;
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);
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) {
3648
mdi->amp = 281 * mdi->lin_max_vol / mdi->log_max_vol;
3651
for (i = 0; i < 16; i++) {
3652
mdi->channel[i].bank = 0;
3653
do_pan_adjust(mdi, i);
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;
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;
3667
mdi->filter.delay_pos[i][0] = 0;
3668
mdi->filter.delay_pos[i][1] = 0;
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)));
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;
3685
free(tmp_trackdata);
3690
* =========================
3691
* External Functions
3692
* =========================
3696
WildMidi_GetString (unsigned short int info) {
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);
3711
if (config_file == NULL) {
3712
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL config file pointer)", 0);
3716
if (WM_LoadConfig(config_file) == -1) {
3720
if (options & 0xFFD8) {
3721
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(invalid option)", 0);
3725
WM_MixerOptions = options;
3727
if ((rate < 11000) || (rate > 65000)) {
3728
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(rate out of bounds, range is 11000 - 65000)", 0);
3732
WM_SampleRate = rate;
3742
WildMidi_MasterVolume (unsigned char master_volume) {
3743
struct _mdi *mdi = NULL;
3744
struct _hndl * tmp_handle = first_handle;
3747
if (!WM_Initialized) {
3748
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
3751
if (master_volume > 127) {
3752
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(master volume out of range, range is 0-127)", 0);
3756
WM_MasterVolume = lin_volume[master_volume];
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);
3764
tmp_handle = tmp_handle->next;
3772
WildMidi_Close (midi * handle) {
3773
struct _mdi *mdi = (struct _mdi *)handle;
3774
struct _hndl * tmp_handle;
3775
struct _sample *tmp_sample;
3778
if (!WM_Initialized) {
3779
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
3782
if (handle == NULL) {
3783
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
3786
if (first_handle == NULL) {
3787
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(no midi's open)", 0);
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;
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);
3806
tmp_handle->prev->next = tmp_handle->next;
3807
if (tmp_handle->next != NULL) {
3808
tmp_handle->next->prev = tmp_handle->prev;
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) {
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;
3827
mdi->patches[i]->loaded = 0;
3831
WM_Unlock(&patch_lock);
3832
free (mdi->patches);
3834
if (mdi->data != NULL) {
3837
if (mdi->tmp_info != NULL) {
3838
free (mdi->tmp_info);
3840
if (mdi->index != NULL)
3843
// no need to unlock cause the struct containing the lock no-longer exists;
3848
WildMidi_Open (const char *midifile) {
3849
unsigned char *mididata = NULL;
3850
unsigned long int midisize = 0;
3852
if (!WM_Initialized) {
3853
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
3856
if (midifile == NULL) {
3857
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL filename)", 0);
3861
if ((mididata = WM_BufferFile(midifile, &midisize)) == NULL) {
3865
return (void *)WM_ParseNewMidi(mididata,midisize);
3869
WildMidi_OpenBuffer (unsigned char *midibuffer, unsigned long int size) {
3870
if (!WM_Initialized) {
3871
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
3874
if (midibuffer == NULL) {
3875
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL midi data buffer)", 0);
3879
return (void *)WM_ParseNewMidi(midibuffer,size);
3883
WildMidi_LoadSamples( midi * handle) {
3888
WildMidi_FastSeek ( midi * handle, unsigned long int *sample_pos) {
3889
struct _mdi *mdi = (struct _mdi *)handle;
3890
struct _note **note_data = mdi->note;
3891
void (*do_event[])(unsigned char ch, struct _mdi *midifile, unsigned long int ptr) = {
3897
*do_channel_pressure,
3901
unsigned long int real_samples_to_mix = 0;
3903
if (!WM_Initialized) {
3904
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
3907
if (handle == NULL) {
3908
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
3911
WM_Lock(&mdi->lock);
3912
if (sample_pos == NULL) {
3913
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL seek position pointer)", 0);
3914
WM_Unlock(&mdi->lock);
3918
if (*sample_pos == mdi->info.current_sample) {
3919
WM_Unlock(&mdi->lock);
3923
if (*sample_pos > mdi->info.current_sample) {
3924
if ((mdi->sample_count == 0) && (mdi->index_count == mdi->index_size) && (mdi->last_note == 0)) {
3925
*sample_pos = mdi->info.current_sample;
3926
WM_Unlock(&mdi->lock);
3930
WM_ResetToStart(handle);
3934
if (note_data != mdi->last_note) {
3936
(*note_data)->active = 0;
3939
} while (note_data != mdi->last_note);
3940
mdi->last_note = mdi->note;
3943
while (*sample_pos != mdi->info.current_sample) {
3944
if (!mdi->sample_count) {
3945
if (mdi->index_count != mdi->index_size) {
3948
if (mdi->index_count == mdi->index_size) {
3952
if (mdi->index_count != 0) {
3953
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);
3955
} while (mdi->index[mdi->index_count++].delta == 0);
3957
mdi->samples_to_mix += mdi->index[mdi->index_count-1].delta * mdi->samples_per_delta;
3958
mdi->sample_count = mdi->samples_to_mix >> 10;
3959
mdi->samples_to_mix %= 1024;
3961
mdi->sample_count = WM_SampleRate;
3965
if (mdi->sample_count <= (*sample_pos - mdi->info.current_sample)) {
3966
real_samples_to_mix = mdi->sample_count;
3967
if (real_samples_to_mix == 0) {
3971
real_samples_to_mix = (*sample_pos - mdi->info.current_sample);
3974
mdi->info.current_sample += real_samples_to_mix;
3975
mdi->sample_count -= real_samples_to_mix;
3976
if ((mdi->index_count == mdi->index_size) && (mdi->last_note == 0)) {
3977
mdi->sample_count = 0;
3978
*sample_pos = mdi->info.current_sample;
3979
WM_Unlock(&mdi->lock);
3983
WM_Unlock(&mdi->lock);
3988
WildMidi_SampledSeek ( midi * handle, unsigned long int *sample_pos) {
3989
struct _mdi *mdi = (struct _mdi *)handle;
3990
struct _note **note_data = mdi->note;
3991
unsigned long int real_samples_to_mix = 0;
3992
unsigned long int tmp_samples_to_mix = 0;
3994
if (!WM_Initialized) {
3995
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
3998
if (handle == NULL) {
3999
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
4002
WM_Lock(&mdi->lock);
4003
if (sample_pos == NULL) {
4004
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL seek position pointer)", 0);
4005
WM_Unlock(&mdi->lock);
4009
if (*sample_pos == mdi->info.current_sample) {
4010
WM_Unlock(&mdi->lock);
4014
if (*sample_pos > mdi->info.current_sample) {
4015
if ((mdi->sample_count == 0) && (mdi->index_count == mdi->index_size) && (mdi->last_note == 0)) {
4016
*sample_pos = mdi->info.current_sample;
4017
WM_Unlock(&mdi->lock);
4021
WM_ResetToStart(handle);
4022
if (note_data != mdi->last_note) {
4024
(*note_data)->active = 0;
4027
} while (note_data != mdi->last_note);
4028
mdi->last_note = mdi->note;
4032
while (*sample_pos != mdi->info.current_sample) {
4033
if (!mdi->sample_count) {
4034
if (mdi->index_count != mdi->index_size) {
4037
if (mdi->index_count == mdi->index_size) {
4041
if (mdi->index_count != 0) {
4042
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);
4044
} while (mdi->index[mdi->index_count++].delta == 0);
4046
mdi->samples_to_mix += mdi->index[mdi->index_count-1].delta * mdi->samples_per_delta;
4047
mdi->sample_count = mdi->samples_to_mix >> 10;
4048
mdi->samples_to_mix %= 1024;
4050
if (mdi->recalc_samples) {
4051
WM_RecalcSamples(mdi);
4053
mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
4054
if (mdi->sample_count == 0) {
4055
WM_Unlock(&mdi->lock);
4061
if (mdi->sample_count <= (*sample_pos - mdi->info.current_sample)) {
4062
real_samples_to_mix = mdi->sample_count;
4063
if (real_samples_to_mix == 0) {
4067
real_samples_to_mix = (*sample_pos - mdi->info.current_sample);
4071
tmp_samples_to_mix = real_samples_to_mix;
4074
if (mdi->last_note != mdi->note) {
4075
note_data = mdi->note;
4076
while (note_data != mdi->last_note) {
4080
* ========================
4081
* sample position checking
4082
* ========================
4084
(*note_data)->sample_pos += (*note_data)->sample_inc;
4085
if (__builtin_expect(((*note_data)->sample_pos > (*note_data)->sample->loop_end), 0)) {
4086
if ((*note_data)->modes & SAMPLE_LOOP) {
4087
(*note_data)->sample_pos = (*note_data)->sample->loop_start + (((*note_data)->sample_pos - (*note_data)->sample->loop_start) % (*note_data)->sample->loop_size);
4088
} else if (__builtin_expect(((*note_data)->sample_pos >= (*note_data)->sample->data_length), 0)) {
4089
if (__builtin_expect(((*note_data)->next == NULL), 1)) {
4095
if (__builtin_expect(((*note_data)->env_inc == 0), 0)) {
4099
(*note_data)->env_level += (*note_data)->env_inc;
4100
if (__builtin_expect(((*note_data)->env_level > 4194304), 0)) {
4101
(*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
4103
if (__builtin_expect((((*note_data)->env_inc < 0) &&
4104
((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env])) ||
4105
(((*note_data)->env_inc > 0) &&
4106
((*note_data)->env_level < (*note_data)->sample->env_target[(*note_data)->env])), 1)) {
4110
(*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
4111
switch ((*note_data)->env) {
4113
if (!((*note_data)->modes & SAMPLE_ENVELOPE)) {
4114
(*note_data)->env_inc = 0;
4120
if ((*note_data)->modes & SAMPLE_SUSTAIN) {
4121
(*note_data)->env_inc = 0;
4127
if (__builtin_expect(((*note_data)->env_level == 0), 1)) {
4131
if ((*note_data)->modes & SAMPLE_LOOP)
4132
(*note_data)->modes ^= SAMPLE_LOOP;
4133
(*note_data)->env_inc = 0;
4137
if (__builtin_expect(((*note_data)->next != NULL), 1)) {
4139
(*note_data)->active = 0;
4140
*note_data = (*note_data)->next;
4141
(*note_data)->active = 1;
4146
(*note_data)->active = 0;
4148
if (note_data != mdi->last_note) {
4149
*note_data = *mdi->last_note;
4154
(*note_data)->env++;
4155
if ((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env]) {
4156
(*note_data)->env_inc = -(*note_data)->sample->env_rate[(*note_data)->env];
4158
(*note_data)->env_inc = (*note_data)->sample->env_rate[(*note_data)->env];
4166
} while (--tmp_samples_to_mix);
4167
mdi->info.current_sample += real_samples_to_mix;
4168
mdi->sample_count -= real_samples_to_mix;
4169
if (mdi->index_count == mdi->index_size) {
4170
if (mdi->last_note == 0) {
4171
mdi->sample_count = 0;
4172
*sample_pos = mdi->info.current_sample;
4173
WM_Unlock(&mdi->lock);
4178
WM_Unlock(&mdi->lock);
4183
WildMidi_GetOutput_Linear (midi * handle, char * buffer, unsigned long int size) {
4184
unsigned long int buffer_used = 0;
4185
struct _mdi *mdi = (struct _mdi *)handle;
4186
unsigned long int real_samples_to_mix = 0;
4187
unsigned long int data_pos;
4188
signed long int premix, left_mix, right_mix;
4189
signed long int vol_mul;
4190
struct _note **note_data = NULL;
4191
unsigned long int count;
4193
if (__builtin_expect((!WM_Initialized),0)) {
4194
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
4197
if (__builtin_expect((handle == NULL),0)) {
4198
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
4201
if (__builtin_expect((buffer == NULL),0)) {
4202
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL buffer pointer)", 0);
4206
if (__builtin_expect((size == 0),0)) {
4210
if (__builtin_expect((size % 4),0)) {
4211
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(size not a multiple of 4)", 0);
4215
WM_Lock(&mdi->lock);
4216
if (__builtin_expect(((mdi->index_count == mdi->index_size) && (mdi->last_note == 0)), 0)) {
4217
WM_Unlock(&mdi->lock);
4222
memset(buffer, 0, size);
4225
if (__builtin_expect((!mdi->sample_count),0)) {
4226
if (__builtin_expect((mdi->index_count != mdi->index_size),1)) {
4228
if (__builtin_expect((mdi->index_count == mdi->index_size), 0)) {
4232
if (__builtin_expect((mdi->index_count != 0), 1)) {
4233
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);
4235
} while (mdi->index[mdi->index_count++].delta == 0);
4237
mdi->samples_to_mix += mdi->index[mdi->index_count-1].delta * mdi->samples_per_delta;
4238
mdi->sample_count = mdi->samples_to_mix >> 10;
4239
mdi->samples_to_mix %= 1024;
4241
if (mdi->recalc_samples) {
4242
WM_RecalcSamples(mdi);
4244
mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
4245
if (mdi->sample_count == 0) {
4246
WM_Unlock(&mdi->lock);
4251
if (__builtin_expect((mdi->sample_count > (size >> 2)),1)) {
4252
real_samples_to_mix = size >> 2;
4254
real_samples_to_mix = mdi->sample_count;
4255
if (real_samples_to_mix == 0) {
4261
count = real_samples_to_mix;
4263
note_data = mdi->note;
4264
left_mix = right_mix = 0;
4265
if (__builtin_expect((mdi->last_note != mdi->note),1)) {
4266
while (note_data != mdi->last_note) {
4268
* ===================
4269
* resample the sample
4270
* ===================
4272
data_pos = (*note_data)->sample_pos >> FPBITS;
4273
vol_mul = (((*note_data)->vol_lvl * ((*note_data)->env_level >> 12)) >> FPBITS);
4275
premix = ((*note_data)->sample->data[data_pos] +
4276
(((*note_data)->sample->data[data_pos + 1] - (*note_data)->sample->data[data_pos]) *
4277
(signed long int)((*note_data)->sample_pos & FPMASK) >> FPBITS)) * vol_mul / 1024;
4279
left_mix += premix * mdi->channel[(*note_data)->noteid >> 8].left_adjust;
4280
right_mix += premix * mdi->channel[(*note_data)->noteid >> 8].right_adjust;
4283
* ========================
4284
* sample position checking
4285
* ========================
4287
(*note_data)->sample_pos += (*note_data)->sample_inc;
4288
if (__builtin_expect(((*note_data)->sample_pos > (*note_data)->sample->loop_end), 0)) {
4289
if ((*note_data)->modes & SAMPLE_LOOP) {
4290
(*note_data)->sample_pos = (*note_data)->sample->loop_start + (((*note_data)->sample_pos - (*note_data)->sample->loop_start) % (*note_data)->sample->loop_size);
4291
} else if (__builtin_expect(((*note_data)->sample_pos >= (*note_data)->sample->data_length), 0)) {
4292
if (__builtin_expect(((*note_data)->next == NULL), 1)) {
4300
if (__builtin_expect(((*note_data)->env_inc == 0), 0)) {
4305
(*note_data)->env_level += (*note_data)->env_inc;
4306
if (__builtin_expect(((*note_data)->env_level > 4194304), 0)) {
4307
(*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
4309
if (__builtin_expect((((*note_data)->env_inc < 0) &&
4310
((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env])) ||
4311
(((*note_data)->env_inc > 0) &&
4312
((*note_data)->env_level < (*note_data)->sample->env_target[(*note_data)->env])), 1)) {
4317
(*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
4318
switch ((*note_data)->env) {
4320
if (!((*note_data)->modes & SAMPLE_ENVELOPE)) {
4321
(*note_data)->env_inc = 0;
4327
if ((*note_data)->modes & SAMPLE_SUSTAIN) {
4328
(*note_data)->env_inc = 0;
4334
if (__builtin_expect(((*note_data)->env_level == 0), 1)) {
4338
if ((*note_data)->modes & SAMPLE_LOOP)
4339
(*note_data)->modes ^= SAMPLE_LOOP;
4340
(*note_data)->env_inc = 0;
4344
if (__builtin_expect(((*note_data)->next != NULL), 1)) {
4346
(*note_data)->active = 0;
4347
*note_data = (*note_data)->next;
4348
(*note_data)->active = 1;
4353
(*note_data)->active = 0;
4355
if (note_data != mdi->last_note) {
4356
*note_data = *mdi->last_note;
4361
(*note_data)->env++;
4362
if ((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env]) {
4363
(*note_data)->env_inc = -(*note_data)->sample->env_rate[(*note_data)->env];
4365
(*note_data)->env_inc = (*note_data)->sample->env_rate[(*note_data)->env];
4371
* =========================
4372
* mix the channels together
4373
* =========================
4380
#ifdef EXPERIMENT_626
4382
* ==========================
4383
* Experimental Reverb Engine
4384
* ==========================
4387
if (mdi->info.mixer_options & WM_MO_REVERB) {
4388
signed long int filteral = mdi->filter.delay[0][0][mdi->filter.delay_pos[0][0]];
4389
signed long int filterar = mdi->filter.delay[0][1][mdi->filter.delay_pos[0][1]];
4390
signed long int filterbl = mdi->filter.delay[1][0][mdi->filter.delay_pos[1][0]];
4391
signed long int filterbr = mdi->filter.delay[1][1][mdi->filter.delay_pos[1][1]];
4392
signed long int filtercl = mdi->filter.delay[2][0][mdi->filter.delay_pos[2][0]];
4393
signed long int filtercr = mdi->filter.delay[2][1][mdi->filter.delay_pos[2][1]];
4394
signed long int filterdl = mdi->filter.delay[3][0][mdi->filter.delay_pos[3][0]];
4395
signed long int filterdr = mdi->filter.delay[3][1][mdi->filter.delay_pos[3][1]];
4396
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;
4397
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;
4398
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;
4399
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;
4400
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;
4401
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;
4402
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;
4403
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;
4404
signed long int tfl, tflo;
4405
signed long int tfr, tfro;
4407
mdi->filter.lowpass[0][0].in[1] = mdi->filter.lowpass[0][0].in[0];
4408
mdi->filter.lowpass[0][0].in[0] = filteral;
4409
mdi->filter.lowpass[0][1].in[1] = mdi->filter.lowpass[0][1].in[0];
4410
mdi->filter.lowpass[0][1].in[0] = filterar;
4411
mdi->filter.lowpass[1][0].in[1] = mdi->filter.lowpass[1][0].in[0];
4412
mdi->filter.lowpass[1][0].in[0] = filterbl;
4413
mdi->filter.lowpass[1][1].in[1] = mdi->filter.lowpass[1][1].in[0];
4414
mdi->filter.lowpass[1][1].in[0] = filterbr;
4415
mdi->filter.lowpass[2][0].in[1] = mdi->filter.lowpass[2][0].in[0];
4416
mdi->filter.lowpass[2][0].in[0] = filtercl;
4417
mdi->filter.lowpass[2][1].in[1] = mdi->filter.lowpass[2][1].in[0];
4418
mdi->filter.lowpass[2][1].in[0] = filtercr;
4419
mdi->filter.lowpass[3][0].in[1] = mdi->filter.lowpass[3][0].in[0];
4420
mdi->filter.lowpass[3][0].in[0] = filterdl;
4421
mdi->filter.lowpass[3][1].in[1] = mdi->filter.lowpass[3][1].in[0];
4422
mdi->filter.lowpass[3][1].in[0] = filterdr;
4424
mdi->filter.lowpass[0][0].out[1] = mdi->filter.lowpass[0][0].out[0];
4425
mdi->filter.lowpass[0][0].out[0] = tfal;
4426
mdi->filter.lowpass[0][1].out[1] = mdi->filter.lowpass[0][1].out[0];
4427
mdi->filter.lowpass[0][1].out[0] = tfar;
4428
mdi->filter.lowpass[1][0].out[1] = mdi->filter.lowpass[1][0].out[0];
4429
mdi->filter.lowpass[1][0].out[0] = tfbl;
4430
mdi->filter.lowpass[1][1].out[1] = mdi->filter.lowpass[1][1].out[0];
4431
mdi->filter.lowpass[1][1].out[0] = tfbr;
4432
mdi->filter.lowpass[2][0].out[1] = mdi->filter.lowpass[2][0].out[0];
4433
mdi->filter.lowpass[2][0].out[0] = tfcl;
4434
mdi->filter.lowpass[2][1].out[1] = mdi->filter.lowpass[2][1].out[0];
4435
mdi->filter.lowpass[2][1].out[0] = tfcr;
4436
mdi->filter.lowpass[3][0].out[1] = mdi->filter.lowpass[3][0].out[0];
4437
mdi->filter.lowpass[3][0].out[0] = tfdl;
4438
mdi->filter.lowpass[3][1].out[1] = mdi->filter.lowpass[3][1].out[0];
4439
mdi->filter.lowpass[3][1].out[0] = tfdr;
4441
mdi->filter.delay[0][0][mdi->filter.delay_pos[0][0]] = (tfbr * 405 + tfcr * 368) / 1024 + (left_mix * gain_in[0] / 1024);
4442
mdi->filter.delay[0][1][mdi->filter.delay_pos[0][1]] = (tfbl * 402 + tfcl * 370) / 1024 + (right_mix * gain_in[0] / 1024);
4443
mdi->filter.delay[1][0][mdi->filter.delay_pos[1][0]] = (tfar * -545 + tfdr * -364) / 1024 + (left_mix * gain_in[1] / 1024);
4444
mdi->filter.delay[1][1][mdi->filter.delay_pos[1][1]] = (tfal * -550 + tfdl * -362) / 1024 + (right_mix * gain_in[1] / 1024);
4445
mdi->filter.delay[2][0][mdi->filter.delay_pos[2][0]] = (tfar * 545 + tfdr * -364) / 1024 + (left_mix * gain_in[2] / 1024);
4446
mdi->filter.delay[2][1][mdi->filter.delay_pos[2][1]] = (tfal * 550 + tfdl * 362) / 1024 + (right_mix * gain_in[2] / 1024);
4447
mdi->filter.delay[3][0][mdi->filter.delay_pos[3][0]] = (tfbr * 405 + tfcr * -368) / 1024 + (left_mix * gain_in[3] / 1024);
4448
mdi->filter.delay[3][1][mdi->filter.delay_pos[3][1]] = (tfbl * 402 + tfcl * -370) / 1024 + (right_mix * gain_in[3] / 1024);
4450
tfl = ((tfal * gain_out[0] / 1024) + (tfbl * gain_out[1] / 1024) + (tfcl * gain_out[2] / 1024) + (tfdl * gain_out[3] / 1024));
4451
tfr = ((tfar * gain_out[0] / 1024) + (tfbr * gain_out[1] / 1024) + (tfcr * gain_out[2] / 1024) + (tfdr * gain_out[3] / 1024));
4453
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;
4454
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;
4456
mdi->filter.in[1][0] = mdi->filter.in[0][0];
4457
mdi->filter.in[0][0] = tfl;
4458
mdi->filter.in[1][1] = mdi->filter.in[0][1];
4459
mdi->filter.in[0][1] = tfr;
4460
mdi->filter.out[1][0] = mdi->filter.out[0][0];
4461
mdi->filter.out[0][0] = tflo;
4462
mdi->filter.out[1][1] = mdi->filter.out[0][1];
4463
mdi->filter.out[0][1] = tfro;
4468
mdi->filter.delay_pos[0][0]++;
4469
mdi->filter.delay_pos[0][1]++;
4470
mdi->filter.delay_pos[1][0]++;
4471
mdi->filter.delay_pos[1][1]++;
4472
mdi->filter.delay_pos[2][0]++;
4473
mdi->filter.delay_pos[2][1]++;
4474
mdi->filter.delay_pos[3][0]++;
4475
mdi->filter.delay_pos[3][1]++;
4477
if (mdi->filter.delay_pos[0][0] == delay_size[0][0]) mdi->filter.delay_pos[0][0] = 0;
4478
if (mdi->filter.delay_pos[0][1] == delay_size[0][1]) mdi->filter.delay_pos[0][1] = 0;
4479
if (mdi->filter.delay_pos[1][0] == delay_size[1][0]) mdi->filter.delay_pos[1][0] = 0;
4480
if (mdi->filter.delay_pos[1][1] == delay_size[1][1]) mdi->filter.delay_pos[1][1] = 0;
4481
if (mdi->filter.delay_pos[2][0] == delay_size[2][0]) mdi->filter.delay_pos[2][0] = 0;
4482
if (mdi->filter.delay_pos[2][1] == delay_size[2][1]) mdi->filter.delay_pos[2][1] = 0;
4483
if (mdi->filter.delay_pos[3][0] == delay_size[3][0]) mdi->filter.delay_pos[3][0] = 0;
4484
if (mdi->filter.delay_pos[3][1] == delay_size[3][1]) mdi->filter.delay_pos[3][1] = 0;
4488
if (left_mix > 32767) {
4490
} else if (left_mix < -32768) {
4494
if (right_mix > 32767) {
4496
} else if (right_mix < -32768) {
4502
* ===================
4503
* Write to the buffer
4504
* ===================
4506
(*buffer++) = left_mix & 0xff;
4507
(*buffer++) = (left_mix >> 8) & 0xff;
4508
(*buffer++) = right_mix & 0xff;
4509
(*buffer++) = (right_mix >> 8) & 0xff;
4512
buffer_used += real_samples_to_mix * 4;
4513
size -= (real_samples_to_mix << 2);
4514
mdi->info.current_sample += real_samples_to_mix;
4515
mdi->sample_count -= real_samples_to_mix;
4516
if (mdi->index_count == mdi->index_size) {
4517
if (mdi->last_note == 0) {
4518
mdi->sample_count = 0;
4519
WM_Unlock(&mdi->lock);
4525
if ((mdi->index_count == mdi->index_size) && (mdi->recalc_samples)) {
4526
WM_RecalcSamples(mdi);
4527
mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
4529
WM_Unlock(&mdi->lock);
4534
WildMidi_GetOutput_Gauss (midi * handle, char * buffer, unsigned long int size) {
4535
unsigned long int buffer_used = 0;
4536
struct _mdi *mdi = (struct _mdi *)handle;
4537
unsigned long int real_samples_to_mix = 0;
4538
unsigned long int data_pos;
4539
signed long int premix, left_mix, right_mix;
4540
signed long int vol_mul;
4541
struct _note **note_data = NULL;
4542
unsigned long int count;
4543
signed short int *sptr;
4546
int left, right, temp_n;
4550
if (__builtin_expect((!WM_Initialized),0)) {
4551
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
4554
if (__builtin_expect((handle == NULL),0)) {
4555
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
4558
if (__builtin_expect((buffer == NULL),0)) {
4559
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL buffer pointer)", 0);
4563
if (__builtin_expect((size == 0),0)) {
4567
if (__builtin_expect((size % 4),0)) {
4568
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(size not a multiple of 4)", 0);
4572
WM_Lock(&mdi->lock);
4573
if (__builtin_expect(((mdi->index_count == mdi->index_size) && (mdi->last_note == 0)), 0)) {
4574
WM_Unlock(&mdi->lock);
4579
memset(buffer, 0, size);
4582
if (__builtin_expect((!mdi->sample_count),0)) {
4583
if (__builtin_expect((mdi->index_count != mdi->index_size),1)) {
4585
if (__builtin_expect((mdi->index_count == mdi->index_size), 0)) {
4589
if (__builtin_expect((mdi->index_count != 0), 1)) {
4590
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);
4592
} while (mdi->index[mdi->index_count++].delta == 0);
4594
mdi->samples_to_mix += mdi->index[mdi->index_count-1].delta * mdi->samples_per_delta;
4595
mdi->sample_count = mdi->samples_to_mix >> 10;
4596
mdi->samples_to_mix %= 1024;
4598
if (mdi->recalc_samples) {
4599
WM_RecalcSamples(mdi);
4601
mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
4602
if (mdi->sample_count == 0) {
4603
WM_Unlock(&mdi->lock);
4608
if (__builtin_expect((mdi->sample_count > (size >> 2)),1)) {
4609
real_samples_to_mix = size >> 2;
4611
real_samples_to_mix = mdi->sample_count;
4612
if (real_samples_to_mix == 0) {
4618
count = real_samples_to_mix;
4620
note_data = mdi->note;
4621
left_mix = right_mix = 0;
4622
if (__builtin_expect((mdi->last_note != mdi->note),1)) {
4623
while (note_data != mdi->last_note) {
4625
* ===================
4626
* resample the sample
4627
* ===================
4629
data_pos = (*note_data)->sample_pos >> FPBITS;
4630
vol_mul = (((*note_data)->vol_lvl * ((*note_data)->env_level >> 12)) >> FPBITS);
4632
/* check to see if we're near one of the ends */
4634
right = ((*note_data)->sample->data_length>>FPBITS)- left -1;
4635
temp_n = (right<<1)-1;
4638
if (temp_n > (left<<1)+1)
4639
temp_n = (left<<1)+1;
4641
/* use Newton if we can't fill the window */
4642
if (temp_n < gauss_n) {
4643
xd = (*note_data)->sample_pos & FPMASK;
4647
sptr = (*note_data)->sample->data + ((*note_data)->sample_pos>>FPBITS) - (temp_n>>1);
4648
for (ii = temp_n; ii;) {
4649
for (jj = 0; jj <= ii; jj++)
4650
y += sptr[jj] * newt_coeffs[ii][jj];
4654
} else { /* otherwise, use Gauss as usual */
4656
gptr = gauss_table[(*note_data)->sample_pos & FPMASK];
4657
gend = gptr + gauss_n;
4658
sptr = (*note_data)->sample->data + ((*note_data)->sample_pos >> FPBITS) - (gauss_n>>1);
4660
y += *(sptr++) * *(gptr++);
4661
} while (gptr <= gend);
4664
premix = y * vol_mul / 1024;
4666
left_mix += premix * mdi->channel[(*note_data)->noteid >> 8].left_adjust;
4667
right_mix += premix * mdi->channel[(*note_data)->noteid >> 8].right_adjust;
4670
* ========================
4671
* sample position checking
4672
* ========================
4674
(*note_data)->sample_pos += (*note_data)->sample_inc;
4675
if (__builtin_expect(((*note_data)->sample_pos > (*note_data)->sample->loop_end), 0)) {
4676
if ((*note_data)->modes & SAMPLE_LOOP) {
4677
(*note_data)->sample_pos = (*note_data)->sample->loop_start + (((*note_data)->sample_pos - (*note_data)->sample->loop_start) % (*note_data)->sample->loop_size);
4678
} else if (__builtin_expect(((*note_data)->sample_pos >= (*note_data)->sample->data_length), 0)) {
4679
if (__builtin_expect(((*note_data)->next == NULL), 1)) {
4687
if (__builtin_expect(((*note_data)->env_inc == 0), 0)) {
4692
(*note_data)->env_level += (*note_data)->env_inc;
4693
if (__builtin_expect(((*note_data)->env_level > 4194304), 0)) {
4694
(*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
4696
if (__builtin_expect((((*note_data)->env_inc < 0) &&
4697
((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env])) ||
4698
(((*note_data)->env_inc > 0) &&
4699
((*note_data)->env_level < (*note_data)->sample->env_target[(*note_data)->env])), 1)) {
4704
(*note_data)->env_level = (*note_data)->sample->env_target[(*note_data)->env];
4705
switch ((*note_data)->env) {
4707
if (!((*note_data)->modes & SAMPLE_ENVELOPE)) {
4708
(*note_data)->env_inc = 0;
4714
if ((*note_data)->modes & SAMPLE_SUSTAIN) {
4715
(*note_data)->env_inc = 0;
4721
if (__builtin_expect(((*note_data)->env_level == 0), 1)) {
4725
if ((*note_data)->modes & SAMPLE_LOOP)
4726
(*note_data)->modes ^= SAMPLE_LOOP;
4727
(*note_data)->env_inc = 0;
4731
if (__builtin_expect(((*note_data)->next != NULL), 1)) {
4733
(*note_data)->active = 0;
4734
*note_data = (*note_data)->next;
4735
(*note_data)->active = 1;
4740
(*note_data)->active = 0;
4742
if (note_data != mdi->last_note) {
4743
*note_data = *mdi->last_note;
4748
(*note_data)->env++;
4749
if ((*note_data)->env_level > (*note_data)->sample->env_target[(*note_data)->env]) {
4750
(*note_data)->env_inc = -(*note_data)->sample->env_rate[(*note_data)->env];
4752
(*note_data)->env_inc = (*note_data)->sample->env_rate[(*note_data)->env];
4758
* =========================
4759
* mix the channels together
4760
* =========================
4767
#ifdef EXPERIMENT_626
4769
* ==========================
4770
* Experimental Reverb Engine
4771
* ==========================
4774
if (mdi->info.mixer_options & WM_MO_REVERB) {
4775
signed long int filteral = mdi->filter.delay[0][0][mdi->filter.delay_pos[0][0]];
4776
signed long int filterar = mdi->filter.delay[0][1][mdi->filter.delay_pos[0][1]];
4777
signed long int filterbl = mdi->filter.delay[1][0][mdi->filter.delay_pos[1][0]];
4778
signed long int filterbr = mdi->filter.delay[1][1][mdi->filter.delay_pos[1][1]];
4779
signed long int filtercl = mdi->filter.delay[2][0][mdi->filter.delay_pos[2][0]];
4780
signed long int filtercr = mdi->filter.delay[2][1][mdi->filter.delay_pos[2][1]];
4781
signed long int filterdl = mdi->filter.delay[3][0][mdi->filter.delay_pos[3][0]];
4782
signed long int filterdr = mdi->filter.delay[3][1][mdi->filter.delay_pos[3][1]];
4783
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;
4784
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;
4785
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;
4786
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;
4787
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;
4788
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;
4789
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;
4790
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;
4791
signed long int tfl, tflo;
4792
signed long int tfr, tfro;
4794
mdi->filter.lowpass[0][0].in[1] = mdi->filter.lowpass[0][0].in[0];
4795
mdi->filter.lowpass[0][0].in[0] = filteral;
4796
mdi->filter.lowpass[0][1].in[1] = mdi->filter.lowpass[0][1].in[0];
4797
mdi->filter.lowpass[0][1].in[0] = filterar;
4798
mdi->filter.lowpass[1][0].in[1] = mdi->filter.lowpass[1][0].in[0];
4799
mdi->filter.lowpass[1][0].in[0] = filterbl;
4800
mdi->filter.lowpass[1][1].in[1] = mdi->filter.lowpass[1][1].in[0];
4801
mdi->filter.lowpass[1][1].in[0] = filterbr;
4802
mdi->filter.lowpass[2][0].in[1] = mdi->filter.lowpass[2][0].in[0];
4803
mdi->filter.lowpass[2][0].in[0] = filtercl;
4804
mdi->filter.lowpass[2][1].in[1] = mdi->filter.lowpass[2][1].in[0];
4805
mdi->filter.lowpass[2][1].in[0] = filtercr;
4806
mdi->filter.lowpass[3][0].in[1] = mdi->filter.lowpass[3][0].in[0];
4807
mdi->filter.lowpass[3][0].in[0] = filterdl;
4808
mdi->filter.lowpass[3][1].in[1] = mdi->filter.lowpass[3][1].in[0];
4809
mdi->filter.lowpass[3][1].in[0] = filterdr;
4811
mdi->filter.lowpass[0][0].out[1] = mdi->filter.lowpass[0][0].out[0];
4812
mdi->filter.lowpass[0][0].out[0] = tfal;
4813
mdi->filter.lowpass[0][1].out[1] = mdi->filter.lowpass[0][1].out[0];
4814
mdi->filter.lowpass[0][1].out[0] = tfar;
4815
mdi->filter.lowpass[1][0].out[1] = mdi->filter.lowpass[1][0].out[0];
4816
mdi->filter.lowpass[1][0].out[0] = tfbl;
4817
mdi->filter.lowpass[1][1].out[1] = mdi->filter.lowpass[1][1].out[0];
4818
mdi->filter.lowpass[1][1].out[0] = tfbr;
4819
mdi->filter.lowpass[2][0].out[1] = mdi->filter.lowpass[2][0].out[0];
4820
mdi->filter.lowpass[2][0].out[0] = tfcl;
4821
mdi->filter.lowpass[2][1].out[1] = mdi->filter.lowpass[2][1].out[0];
4822
mdi->filter.lowpass[2][1].out[0] = tfcr;
4823
mdi->filter.lowpass[3][0].out[1] = mdi->filter.lowpass[3][0].out[0];
4824
mdi->filter.lowpass[3][0].out[0] = tfdl;
4825
mdi->filter.lowpass[3][1].out[1] = mdi->filter.lowpass[3][1].out[0];
4826
mdi->filter.lowpass[3][1].out[0] = tfdr;
4828
mdi->filter.delay[0][0][mdi->filter.delay_pos[0][0]] = (tfbr * 405 + tfcr * 368) / 1024 + (left_mix * gain_in[0] / 1024);
4829
mdi->filter.delay[0][1][mdi->filter.delay_pos[0][1]] = (tfbl * 402 + tfcl * 370) / 1024 + (right_mix * gain_in[0] / 1024);
4830
mdi->filter.delay[1][0][mdi->filter.delay_pos[1][0]] = (tfar * -545 + tfdr * -364) / 1024 + (left_mix * gain_in[1] / 1024);
4831
mdi->filter.delay[1][1][mdi->filter.delay_pos[1][1]] = (tfal * -550 + tfdl * -362) / 1024 + (right_mix * gain_in[1] / 1024);
4832
mdi->filter.delay[2][0][mdi->filter.delay_pos[2][0]] = (tfar * 545 + tfdr * -364) / 1024 + (left_mix * gain_in[2] / 1024);
4833
mdi->filter.delay[2][1][mdi->filter.delay_pos[2][1]] = (tfal * 550 + tfdl * 362) / 1024 + (right_mix * gain_in[2] / 1024);
4834
mdi->filter.delay[3][0][mdi->filter.delay_pos[3][0]] = (tfbr * 405 + tfcr * -368) / 1024 + (left_mix * gain_in[3] / 1024);
4835
mdi->filter.delay[3][1][mdi->filter.delay_pos[3][1]] = (tfbl * 402 + tfcl * -370) / 1024 + (right_mix * gain_in[3] / 1024);
4837
tfl = ((tfal * gain_out[0] / 1024) + (tfbl * gain_out[1] / 1024) + (tfcl * gain_out[2] / 1024) + (tfdl * gain_out[3] / 1024));
4838
tfr = ((tfar * gain_out[0] / 1024) + (tfbr * gain_out[1] / 1024) + (tfcr * gain_out[2] / 1024) + (tfdr * gain_out[3] / 1024));
4840
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;
4841
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;
4843
mdi->filter.in[1][0] = mdi->filter.in[0][0];
4844
mdi->filter.in[0][0] = tfl;
4845
mdi->filter.in[1][1] = mdi->filter.in[0][1];
4846
mdi->filter.in[0][1] = tfr;
4847
mdi->filter.out[1][0] = mdi->filter.out[0][0];
4848
mdi->filter.out[0][0] = tflo;
4849
mdi->filter.out[1][1] = mdi->filter.out[0][1];
4850
mdi->filter.out[0][1] = tfro;
4855
mdi->filter.delay_pos[0][0]++;
4856
mdi->filter.delay_pos[0][1]++;
4857
mdi->filter.delay_pos[1][0]++;
4858
mdi->filter.delay_pos[1][1]++;
4859
mdi->filter.delay_pos[2][0]++;
4860
mdi->filter.delay_pos[2][1]++;
4861
mdi->filter.delay_pos[3][0]++;
4862
mdi->filter.delay_pos[3][1]++;
4864
if (mdi->filter.delay_pos[0][0] == delay_size[0][0]) mdi->filter.delay_pos[0][0] = 0;
4865
if (mdi->filter.delay_pos[0][1] == delay_size[0][1]) mdi->filter.delay_pos[0][1] = 0;
4866
if (mdi->filter.delay_pos[1][0] == delay_size[1][0]) mdi->filter.delay_pos[1][0] = 0;
4867
if (mdi->filter.delay_pos[1][1] == delay_size[1][1]) mdi->filter.delay_pos[1][1] = 0;
4868
if (mdi->filter.delay_pos[2][0] == delay_size[2][0]) mdi->filter.delay_pos[2][0] = 0;
4869
if (mdi->filter.delay_pos[2][1] == delay_size[2][1]) mdi->filter.delay_pos[2][1] = 0;
4870
if (mdi->filter.delay_pos[3][0] == delay_size[3][0]) mdi->filter.delay_pos[3][0] = 0;
4871
if (mdi->filter.delay_pos[3][1] == delay_size[3][1]) mdi->filter.delay_pos[3][1] = 0;
4875
if (left_mix > 32767) {
4877
} else if (left_mix < -32768) {
4881
if (right_mix > 32767) {
4883
} else if (right_mix < -32768) {
4889
* ===================
4890
* Write to the buffer
4891
* ===================
4893
(*buffer++) = left_mix & 0xff;
4894
(*buffer++) = (left_mix >> 8) & 0xff;
4895
(*buffer++) = right_mix & 0xff;
4896
(*buffer++) = (right_mix >> 8) & 0xff;
4899
buffer_used += real_samples_to_mix * 4;
4900
size -= (real_samples_to_mix << 2);
4901
mdi->info.current_sample += real_samples_to_mix;
4902
mdi->sample_count -= real_samples_to_mix;
4903
if (mdi->index_count == mdi->index_size) {
4904
if (mdi->last_note == 0) {
4905
mdi->sample_count = 0;
4906
WM_Unlock(&mdi->lock);
4912
if ((mdi->index_count == mdi->index_size) && (mdi->recalc_samples)) {
4913
WM_RecalcSamples(mdi);
4914
mdi->sample_count = mdi->info.approx_total_samples - mdi->info.current_sample;
4916
WM_Unlock(&mdi->lock);
4921
WildMidi_GetOutput (midi * handle, char * buffer, unsigned long int size) {
4922
struct _mdi *mdi = (struct _mdi *)handle;
4924
if (__builtin_expect((!WM_Initialized),0)) {
4925
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
4928
if (__builtin_expect((handle == NULL),0)) {
4929
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
4932
if (__builtin_expect((buffer == NULL),0)) {
4933
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL buffer pointer)", 0);
4937
if (__builtin_expect((size == 0),0)) {
4941
if (__builtin_expect((size % 4),0)) {
4942
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(size not a multiple of 4)", 0);
4945
if (mdi->info.mixer_options & WM_MO_EXPENSIVE_INTERPOLATION) {
4946
return WildMidi_GetOutput_Gauss (handle, buffer,size);
4948
return WildMidi_GetOutput_Linear (handle, buffer, size);
4953
WildMidi_SetOption (midi * handle, unsigned short int options, unsigned short int setting) {
4954
struct _mdi *mdi = (struct _mdi *)handle;
4955
struct _note **note_data = mdi->note;
4958
if (!WM_Initialized) {
4959
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
4962
if (handle == NULL) {
4963
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
4966
WM_Lock(&mdi->lock);
4967
if ((!(options & 0x0007)) || (options & 0xFFF8)){
4968
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(invalid option)", 0);
4969
WM_Unlock(&mdi->lock);
4972
if (setting & 0xFFF8) {
4973
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(invalid setting)", 0);
4974
WM_Unlock(&mdi->lock);
4978
mdi->info.mixer_options = ((mdi->info.mixer_options & (0x00FF ^ options)) | (options & setting));
4980
if (options & WM_MO_LINEAR_VOLUME) {
4981
if (mdi->info.mixer_options & WM_MO_LINEAR_VOLUME) {
4984
mdi->amp = 281 * mdi->lin_max_vol / mdi->log_max_vol;
4986
for (i = 0; i < 16; i++) {
4987
do_pan_adjust(mdi, i);
4989
if (note_data != mdi->last_note) {
4991
(*note_data)->vol_lvl = get_volume(mdi, ((*note_data)->noteid >> 8), *note_data);
4992
if ((*note_data)->next)
4993
(*note_data)->next->vol_lvl = get_volume(mdi, ((*note_data)->noteid >> 8), (*note_data)->next);
4995
} while (note_data != mdi->last_note);
4999
if (options & WM_MO_REVERB) {
5000
for (i = 0; i < 4; i++) {
5001
mdi->filter.lowpass[i][0].in[0] = 0;
5002
mdi->filter.lowpass[i][0].in[1] = 0;
5003
mdi->filter.lowpass[i][1].in[0] = 0;
5004
mdi->filter.lowpass[i][1].in[1] = 0;
5006
mdi->filter.lowpass[i][0].out[0] = 0;
5007
mdi->filter.lowpass[i][0].out[1] = 0;
5008
mdi->filter.lowpass[i][1].out[0] = 0;
5009
mdi->filter.lowpass[i][1].out[1] = 0;
5011
mdi->filter.delay_pos[i][0] = 0;
5012
mdi->filter.delay_pos[i][1] = 0;
5014
memset (mdi->filter.delay[i][0], 0, (delay_size[i][0] * sizeof(signed long int)));
5015
memset (mdi->filter.delay[i][1], 0, (delay_size[i][1] * sizeof(signed long int)));
5018
WM_Unlock(&mdi->lock);
5023
WildMidi_GetInfo (midi * handle) {
5024
struct _mdi *mdi = (struct _mdi *)handle;
5025
if (!WM_Initialized) {
5026
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
5029
if (handle == NULL) {
5030
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(NULL handle)", 0);
5033
WM_Lock(&mdi->lock);
5034
if (mdi->tmp_info == NULL) {
5035
mdi->tmp_info = malloc(sizeof(struct _WM_Info));
5036
if (mdi->tmp_info == NULL) {
5037
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, "to set info", 0);
5038
WM_Unlock(&mdi->lock);
5042
mdi->tmp_info->current_sample = mdi->info.current_sample;
5043
mdi->tmp_info->approx_total_samples = mdi->info.approx_total_samples;
5044
mdi->tmp_info->mixer_options = mdi->info.mixer_options;
5045
WM_Unlock(&mdi->lock);
5046
return mdi->tmp_info;
5050
WildMidi_Shutdown ( void ) {
5051
struct _hndl * tmp_hdle;
5053
if (!WM_Initialized) {
5054
WM_ERROR(__FUNCTION__, __LINE__, WM_ERR_NOT_INIT, NULL, 0);
5057
if (first_handle != NULL) {
5058
while (first_handle != NULL) {
5059
tmp_hdle = first_handle->next;
5060
WildMidi_Close((struct _mdi *)first_handle->handle);
5061
free (first_handle);
5062
first_handle = tmp_hdle;