-
Notifications
You must be signed in to change notification settings - Fork 40
/
wildmidi_lib.c
2159 lines (1951 loc) · 82.6 KB
/
wildmidi_lib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* wildmidi_lib.c -- Midi Wavetable Processing library
*
* Copyright (C) WildMIDI Developers 2001-2016
*
* This file is part of WildMIDI.
*
* WildMIDI is free software: you can redistribute and/or modify the player
* under the terms of the GNU General Public License and you can redistribute
* and/or modify the library under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 3 of
* the licenses, or(at your option) any later version.
*
* WildMIDI is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License and
* the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the
* GNU Lesser General Public License along with WildMIDI. If not, see
* <http://www.gnu.org/licenses/>.
*/
#define _WILDMIDI_LIB_C
#include "config.h"
#include <stdint.h>
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wm_error.h"
#include "file_io.h"
#include "lock.h"
#include "reverb.h"
#include "gus_pat.h"
#include "common.h"
#include "wildmidi_lib.h"
#include "filenames.h"
#include "internal_midi.h"
#include "f_hmi.h"
#include "f_hmp.h"
#include "f_midi.h"
#include "f_mus.h"
#include "f_xmidi.h"
#include "patches.h"
#include "sample.h"
#include "mus2mid.h"
#include "xmi2mid.h"
/*
* =========================
* Global Data and Data Structs
* =========================
*/
static int WM_Initialized = 0;
uint16_t _WM_MixerOptions = 0;
uint16_t _WM_SampleRate;
int16_t _WM_MasterVolume;
/* when converting files to midi */
typedef struct _cvt_options {
int lock;
uint16_t xmi_convert_type;
uint16_t frequency;
} _cvt_options;
static _cvt_options WM_ConvertOptions = {0, 0, 0};
float _WM_reverb_room_width = 16.875f;
float _WM_reverb_room_length = 22.5f;
float _WM_reverb_listen_posx = 8.4375f;
float _WM_reverb_listen_posy = 16.875f;
int _WM_fix_release = 0;
int _WM_auto_amp = 0;
int _WM_auto_amp_with_amp = 0;
struct _miditrack {
uint32_t length;
uint32_t ptr;
uint32_t delta;
uint8_t running_event;
uint8_t EOT;
};
struct _mdi_patches {
struct _patch *patch;
struct _mdi_patch *next;
};
#define FPBITS 10
#define FPMASK ((1L<<FPBITS)-1L)
/* Gauss Interpolation code adapted from code supplied by Eric. A. Welsh */
static double newt_coeffs[58][58]; /* for start/end of samples */
#define MAX_GAUSS_ORDER 34 /* 34 is as high as we can go before errors crop up */
static double *gauss_table = NULL; /* *gauss_table[1<<FPBITS] */
static int gauss_n = MAX_GAUSS_ORDER;
static int gauss_lock;
static void init_gauss(void) {
/* init gauss table */
int n = gauss_n;
int m, i, k, n_half = (n >> 1);
int j;
int sign;
double ck;
double x, x_inc, xz;
double z[35];
double *gptr, *t;
_WM_Lock(&gauss_lock);
if (gauss_table) {
_WM_Unlock(&gauss_lock);
return;
}
newt_coeffs[0][0] = 1;
for (i = 0; i <= n; i++) {
newt_coeffs[i][0] = 1;
newt_coeffs[i][i] = 1;
if (i > 1) {
newt_coeffs[i][0] = newt_coeffs[i - 1][0] / i;
newt_coeffs[i][i] = newt_coeffs[i - 1][0] / i;
}
for (j = 1; j < i; j++) {
newt_coeffs[i][j] = newt_coeffs[i - 1][j - 1]
+ newt_coeffs[i - 1][j];
if (i > 1)
newt_coeffs[i][j] /= i;
}
z[i] = i / (4 * M_PI);
}
for (i = 0; i <= n; i++)
for (j = 0, sign = (int) pow(-1, i); j <= i; j++, sign *= -1)
newt_coeffs[i][j] *= sign;
t = (double *) malloc((1<<FPBITS) * (n + 1) * sizeof(double));
x_inc = 1.0 / (1<<FPBITS);
for (m = 0, x = 0.0; m < (1<<FPBITS); m++, x += x_inc) {
xz = (x + n_half) / (4 * M_PI);
gptr = &t[m * (n + 1)];
for (k = 0; k <= n; k++) {
ck = 1.0;
for (i = 0; i <= n; i++) {
if (i == k)
continue;
ck *= (sin(xz - z[i])) / (sin(z[k] - z[i]));
}
*gptr++ = ck;
}
}
gauss_table = t;
_WM_Unlock(&gauss_lock);
}
static void free_gauss(void) {
_WM_Lock(&gauss_lock);
free(gauss_table);
gauss_table = NULL;
_WM_Unlock(&gauss_lock);
}
struct _hndl {
void * handle;
struct _hndl *next;
struct _hndl *prev;
};
static struct _hndl * first_handle = NULL;
#define MAX_AUTO_AMP 2.0
/*
* =========================
* Internal Functions
* =========================
*/
void _cvt_reset_options (void) {
_WM_Lock(&WM_ConvertOptions.lock);
WM_ConvertOptions.xmi_convert_type = 0;
WM_ConvertOptions.frequency = 0;
_WM_Unlock(&WM_ConvertOptions.lock);
}
uint16_t _cvt_get_option (uint16_t tag) {
uint16_t r = 0;
_WM_Lock(&WM_ConvertOptions.lock);
switch (tag) {
case WM_CO_XMI_TYPE: r = WM_ConvertOptions.xmi_convert_type; break;
case WM_CO_FREQUENCY: r = WM_ConvertOptions.frequency; break;
}
_WM_Unlock(&WM_ConvertOptions.lock);
return r;
}
static void WM_InitPatches(void) {
int i;
for (i = 0; i < 128; i++) {
_WM_patch[i] = NULL;
}
}
static void WM_FreePatches(void) {
int i;
struct _patch * tmp_patch;
struct _sample * tmp_sample;
_WM_Lock(&_WM_patch_lock);
for (i = 0; i < 128; i++) {
while (_WM_patch[i]) {
while (_WM_patch[i]->first_sample) {
tmp_sample = _WM_patch[i]->first_sample->next;
free(_WM_patch[i]->first_sample->data);
free(_WM_patch[i]->first_sample);
_WM_patch[i]->first_sample = tmp_sample;
}
free(_WM_patch[i]->filename);
tmp_patch = _WM_patch[i]->next;
free(_WM_patch[i]);
_WM_patch[i] = tmp_patch;
}
}
_WM_Unlock(&_WM_patch_lock);
}
/* wm_strdup -- adds extra space for appending up to 4 chars */
static char *wm_strdup (const char *str) {
size_t l = strlen(str) + 5;
char *d = (char *) malloc(l * sizeof(char));
if (d) {
strcpy(d, str);
return (d);
}
return (NULL);
}
static inline int wm_isdigit(int c) {
return (c >= '0' && c <= '9');
}
static inline int wm_isupper(int c) {
return (c >= 'A' && c <= 'Z');
}
static inline int wm_tolower(int c) {
return ((wm_isupper(c)) ? (c | ('a' - 'A')) : c);
}
#if 0 /* clang whines that these aren't used. */
static inline int wm_islower(int c) {
return (c >= 'a' && c <= 'z');
}
static inline int wm_toupper(int c) {
return ((wm_islower(c)) ? (c & ~('a' - 'A')) : c);
}
#endif
static int wm_strcasecmp(const char *s1, const char * s2) {
const char * p1 = s1;
const char * p2 = s2;
char c1, c2;
if (p1 == p2) return 0;
do {
c1 = wm_tolower (*p1++);
c2 = wm_tolower (*p2++);
if (c1 == '\0') break;
} while (c1 == c2);
return (int)(c1 - c2);
}
static int wm_strncasecmp(const char *s1, const char *s2, size_t n) {
const char * p1 = s1;
const char * p2 = s2;
char c1, c2;
if (p1 == p2 || n == 0) return 0;
do {
c1 = wm_tolower (*p1++);
c2 = wm_tolower (*p2++);
if (c1 == '\0' || c1 != c2) break;
} while (--n > 0);
return (int)(c1 - c2);
}
#define TOKEN_CNT_INC 8
static char** WM_LC_Tokenize_Line(char *line_data) {
int line_length = (int) strlen(line_data);
int token_data_length = 0;
int line_ofs = 0;
int token_start = 0;
char **token_data = NULL;
int token_count = 0;
if (!line_length) return (NULL);
do {
/* ignore everything after # */
if (line_data[line_ofs] == '#') {
break;
}
if ((line_data[line_ofs] == ' ') || (line_data[line_ofs] == '\t')) {
/* whitespace means we aren't in a token */
if (token_start) {
token_start = 0;
line_data[line_ofs] = '\0';
}
} else {
if (!token_start) {
/* the start of a token in the line */
token_start = 1;
if (token_count >= token_data_length) {
token_data_length += TOKEN_CNT_INC;
token_data = (char **) realloc(token_data, token_data_length * sizeof(char *));
if (token_data == NULL) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM,"to parse config", errno);
return (NULL);
}
}
token_data[token_count] = &line_data[line_ofs];
token_count++;
}
}
line_ofs++;
} while (line_ofs != line_length);
/* if we have found some tokens then add a null token to the end */
if (token_count) {
if (token_count >= token_data_length) {
token_data = (char **) realloc(token_data, ((token_count + 1) * sizeof(char *)));
}
token_data[token_count] = NULL;
}
return (token_data);
}
static int load_config(const char *config_file, const char *conf_dir) {
uint32_t config_size = 0;
char *config_buffer = NULL;
const char *dir_end = NULL;
char *config_dir = NULL;
uint32_t config_ptr = 0;
uint32_t line_start_ptr = 0;
uint16_t patchid = 0;
struct _patch * tmp_patch;
char **line_tokens = NULL;
int token_count = 0;
config_buffer = (char *) _WM_BufferFile(config_file, &config_size);
if (!config_buffer) {
WM_FreePatches();
return (-1);
}
if (conf_dir) {
if (!(config_dir = wm_strdup(conf_dir))) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, config_file, errno);
WM_FreePatches();
free(config_buffer);
return (-1);
}
} else {
dir_end = FIND_LAST_DIRSEP(config_file);
if (dir_end) {
config_dir = (char *) malloc((dir_end - config_file + 2));
if (config_dir == NULL) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, config_file, errno);
WM_FreePatches();
free(config_buffer);
return (-1);
}
strncpy(config_dir, config_file, (dir_end - config_file + 1));
config_dir[dir_end - config_file + 1] = '\0';
}
}
config_ptr = 0;
line_start_ptr = 0;
/* handle files without a newline at the end: this relies on
* _WM_BufferFile() allocating the buffer with one extra byte */
config_buffer[config_size] = '\n';
while (config_ptr <= config_size) {
if (config_buffer[config_ptr] == '\r' ||
config_buffer[config_ptr] == '\n')
{
config_buffer[config_ptr] = '\0';
if (config_ptr != line_start_ptr) {
_WM_Global_ErrorI = 0; /* because WM_LC_Tokenize_Line() can legitimately return NULL */
line_tokens = WM_LC_Tokenize_Line(&config_buffer[line_start_ptr]);
if (line_tokens) {
if (wm_strcasecmp(line_tokens[0], "dir") == 0) {
free(config_dir);
if (!line_tokens[1]) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(missing name in dir line)", 0);
WM_FreePatches();
free(line_tokens);
free(config_buffer);
return (-1);
} else if (!(config_dir = wm_strdup(line_tokens[1]))) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, config_file, errno);
WM_FreePatches();
free(line_tokens);
free(config_buffer);
return (-1);
}
if (!IS_DIR_SEPARATOR(config_dir[strlen(config_dir) - 1])) {
config_dir[strlen(config_dir) + 1] = '\0';
config_dir[strlen(config_dir)] = DIR_SEPARATOR_CHAR;
}
} else if (wm_strcasecmp(line_tokens[0], "source") == 0) {
char *new_config = NULL;
if (!line_tokens[1]) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(missing name in source line)", 0);
WM_FreePatches();
free(line_tokens);
free(config_buffer);
return (-1);
} else if (!IS_ABSOLUTE_PATH(line_tokens[1]) && config_dir) {
new_config = (char *) malloc(strlen(config_dir) + strlen(line_tokens[1]) + 1);
if (new_config == NULL) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, config_file, errno);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
}
strcpy(new_config, config_dir);
strcpy(&new_config[strlen(config_dir)], line_tokens[1]);
} else {
if (!(new_config = wm_strdup(line_tokens[1]))) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, config_file, errno);
WM_FreePatches();
free(line_tokens);
free(config_buffer);
return (-1);
}
}
if (load_config(new_config, config_dir) == -1) {
free(new_config);
free(line_tokens);
free(config_buffer);
free(config_dir);
return (-1);
}
free(new_config);
} else if (wm_strcasecmp(line_tokens[0], "bank") == 0) {
if (!line_tokens[1] || !wm_isdigit(line_tokens[1][0])) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(syntax error in bank line)", 0);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
}
patchid = (atoi(line_tokens[1]) & 0xFF) << 8;
} else if (wm_strcasecmp(line_tokens[0], "drumset") == 0) {
if (!line_tokens[1] || !wm_isdigit(line_tokens[1][0])) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(syntax error in drumset line)", 0);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
}
patchid = ((atoi(line_tokens[1]) & 0xFF) << 8) | 0x80;
} else if (wm_strcasecmp(line_tokens[0], "reverb_room_width") == 0) {
if (!line_tokens[1] || !wm_isdigit(line_tokens[1][0])) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(syntax error in reverb_room_width line)", 0);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
}
_WM_reverb_room_width = (float) atof(line_tokens[1]);
if (_WM_reverb_room_width < 1.0f) {
_WM_DEBUG_MSG("%s: reverb_room_width < 1 meter, setting to minimum of 1 meter", config_file);
_WM_reverb_room_width = 1.0f;
} else if (_WM_reverb_room_width > 100.0f) {
_WM_DEBUG_MSG("%s: reverb_room_width > 100 meters, setting to maximum of 100 meters", config_file);
_WM_reverb_room_width = 100.0f;
}
} else if (wm_strcasecmp(line_tokens[0], "reverb_room_length") == 0) {
if (!line_tokens[1] || !wm_isdigit(line_tokens[1][0])) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(syntax error in reverb_room_length line)", 0);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
}
_WM_reverb_room_length = (float) atof(line_tokens[1]);
if (_WM_reverb_room_length < 1.0f) {
_WM_DEBUG_MSG("%s: reverb_room_length < 1 meter, setting to minimum of 1 meter", config_file);
_WM_reverb_room_length = 1.0f;
} else if (_WM_reverb_room_length > 100.0f) {
_WM_DEBUG_MSG("%s: reverb_room_length > 100 meters, setting to maximum of 100 meters", config_file);
_WM_reverb_room_length = 100.0f;
}
} else if (wm_strcasecmp(line_tokens[0], "reverb_listener_posx") == 0) {
if (!line_tokens[1] || !wm_isdigit(line_tokens[1][0])) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(syntax error in reverb_listen_posx line)", 0);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
}
_WM_reverb_listen_posx = (float) atof(line_tokens[1]);
if ((_WM_reverb_listen_posx > _WM_reverb_room_width)
|| (_WM_reverb_listen_posx < 0.0f)) {
_WM_DEBUG_MSG("%s: reverb_listen_posx set outside of room", config_file);
_WM_reverb_listen_posx = _WM_reverb_room_width / 2.0f;
}
} else if (wm_strcasecmp(line_tokens[0],
"reverb_listener_posy") == 0) {
if (!line_tokens[1] || !wm_isdigit(line_tokens[1][0])) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(syntax error in reverb_listen_posy line)", 0);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
}
_WM_reverb_listen_posy = (float) atof(line_tokens[1]);
if ((_WM_reverb_listen_posy > _WM_reverb_room_width)
|| (_WM_reverb_listen_posy < 0.0f)) {
_WM_DEBUG_MSG("%s: reverb_listen_posy set outside of room", config_file);
_WM_reverb_listen_posy = _WM_reverb_room_length * 0.75f;
}
} else if (wm_strcasecmp(line_tokens[0], "guspat_editor_author_cant_read_so_fix_release_time_for_me") == 0) {
_WM_fix_release = 1;
} else if (wm_strcasecmp(line_tokens[0], "auto_amp") == 0) {
_WM_auto_amp = 1;
} else if (wm_strcasecmp(line_tokens[0], "auto_amp_with_amp") == 0) {
_WM_auto_amp = 1;
_WM_auto_amp_with_amp = 1;
} else if (wm_isdigit(line_tokens[0][0])) {
patchid = (patchid & 0xFF80)
| (atoi(line_tokens[0]) & 0x7F);
if (_WM_patch[(patchid & 0x7F)] == NULL) {
_WM_patch[(patchid & 0x7F)] = (struct _patch *) malloc(sizeof(struct _patch));
if (_WM_patch[(patchid & 0x7F)] == NULL) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, config_file, errno);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
}
tmp_patch = _WM_patch[(patchid & 0x7F)];
tmp_patch->patchid = patchid;
tmp_patch->filename = NULL;
tmp_patch->amp = 1024;
tmp_patch->note = 0;
tmp_patch->next = NULL;
tmp_patch->first_sample = NULL;
tmp_patch->loaded = 0;
tmp_patch->inuse_count = 0;
} else {
tmp_patch = _WM_patch[(patchid & 0x7F)];
if (tmp_patch->patchid == patchid) {
free(tmp_patch->filename);
tmp_patch->filename = NULL;
tmp_patch->amp = 1024;
tmp_patch->note = 0;
} else {
if (tmp_patch->next) {
while (tmp_patch->next) {
if (tmp_patch->next->patchid == patchid)
break;
tmp_patch = tmp_patch->next;
}
if (tmp_patch->next == NULL) {
if ((tmp_patch->next = (struct _patch *) malloc(sizeof(struct _patch))) == NULL) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, config_file, 0);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
}
tmp_patch = tmp_patch->next;
tmp_patch->patchid = patchid;
tmp_patch->filename = NULL;
tmp_patch->amp = 1024;
tmp_patch->note = 0;
tmp_patch->next = NULL;
tmp_patch->first_sample = NULL;
tmp_patch->loaded = 0;
tmp_patch->inuse_count = 0;
} else {
tmp_patch = tmp_patch->next;
free(tmp_patch->filename);
tmp_patch->filename = NULL;
tmp_patch->amp = 1024;
tmp_patch->note = 0;
}
} else {
tmp_patch->next = (struct _patch *) malloc(sizeof(struct _patch));
if (tmp_patch->next == NULL) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, config_file, errno);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
}
tmp_patch = tmp_patch->next;
tmp_patch->patchid = patchid;
tmp_patch->filename = NULL;
tmp_patch->amp = 1024;
tmp_patch->note = 0;
tmp_patch->next = NULL;
tmp_patch->first_sample = NULL;
tmp_patch->loaded = 0;
tmp_patch->inuse_count = 0;
}
}
}
if (!line_tokens[1]) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_INVALID_ARG, "(missing name in patch line)", 0);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
} else if (!IS_ABSOLUTE_PATH(line_tokens[1]) && config_dir) {
tmp_patch->filename = (char *) malloc(strlen(config_dir) + strlen(line_tokens[1]) + 5);
if (tmp_patch->filename == NULL) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, config_file, 0);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
}
strcpy(tmp_patch->filename, config_dir);
strcat(tmp_patch->filename, line_tokens[1]);
} else {
if (!(tmp_patch->filename = wm_strdup(line_tokens[1]))) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, config_file, 0);
WM_FreePatches();
free(config_dir);
free(line_tokens);
free(config_buffer);
return (-1);
}
}
if (wm_strncasecmp(&tmp_patch->filename[strlen(tmp_patch->filename) - 4], ".pat", 4) != 0) {
strcat(tmp_patch->filename, ".pat");
}
tmp_patch->env[0].set = 0x00;
tmp_patch->env[1].set = 0x00;
tmp_patch->env[2].set = 0x00;
tmp_patch->env[3].set = 0x00;
tmp_patch->env[4].set = 0x00;
tmp_patch->env[5].set = 0x00;
tmp_patch->keep = 0;
tmp_patch->remove = 0;
token_count = 0;
while (line_tokens[token_count]) {
if (wm_strncasecmp(line_tokens[token_count], "amp=", 4) == 0) {
if (!wm_isdigit(line_tokens[token_count][4])) {
_WM_DEBUG_MSG("%s: syntax error in patch line for %s", config_file, "amp=");
} else {
tmp_patch->amp = (atoi(&line_tokens[token_count][4]) << 10) / 100;
}
} else if (wm_strncasecmp(line_tokens[token_count], "note=", 5) == 0) {
if (!wm_isdigit(line_tokens[token_count][5])) {
_WM_DEBUG_MSG("%s: syntax error in patch line for %s", config_file, "note=");
} else {
tmp_patch->note = atoi(&line_tokens[token_count][5]);
}
} else if (wm_strncasecmp(line_tokens[token_count], "env_time", 8) == 0) {
if ((!wm_isdigit(line_tokens[token_count][8])) ||
(!wm_isdigit(line_tokens[token_count][10])) ||
(line_tokens[token_count][9] != '=')) {
_WM_DEBUG_MSG("%s: syntax error in patch line for %s", config_file, "env_time");
} else {
uint32_t env_no = atoi(&line_tokens[token_count][8]);
if (env_no > 5) {
_WM_DEBUG_MSG("%s: syntax error in patch line for %s", config_file, "env_time");
} else {
tmp_patch->env[env_no].time = (float) atof(&line_tokens[token_count][10]);
if ((tmp_patch->env[env_no].time > 45000.0f) ||
(tmp_patch->env[env_no].time < 1.47f)) {
_WM_DEBUG_MSG("%s: range error in patch line %s", config_file, "env_time");
tmp_patch->env[env_no].set &= 0xFE;
} else {
tmp_patch->env[env_no].set |= 0x01;
}
}
}
} else if (wm_strncasecmp(line_tokens[token_count], "env_level", 9) == 0) {
if ((!wm_isdigit(line_tokens[token_count][9])) ||
(!wm_isdigit(line_tokens[token_count][11])) ||
(line_tokens[token_count][10] != '=')) {
_WM_DEBUG_MSG("%s: syntax error in patch line for %s", config_file, "env_level");
} else {
uint32_t env_no = atoi(&line_tokens[token_count][9]);
if (env_no > 5) {
_WM_DEBUG_MSG("%s: syntax error in patch line for %s", config_file, "env_level");
} else {
tmp_patch->env[env_no].level = (float) atof(&line_tokens[token_count][11]);
if ((tmp_patch->env[env_no].level > 1.0f) ||
(tmp_patch->env[env_no].level < 0.0f)) {
_WM_DEBUG_MSG("%s: range error in patch line for %s", config_file, "env_level");
tmp_patch->env[env_no].set &= 0xFD;
} else {
tmp_patch->env[env_no].set |= 0x02;
}
}
}
} else if (wm_strcasecmp(line_tokens[token_count], "keep=loop") == 0) {
tmp_patch->keep |= SAMPLE_LOOP;
} else if (wm_strcasecmp(line_tokens[token_count], "keep=env") == 0) {
tmp_patch->keep |= SAMPLE_ENVELOPE;
} else if (wm_strcasecmp(line_tokens[token_count], "remove=sustain") == 0) {
tmp_patch->remove |= SAMPLE_SUSTAIN;
} else if (wm_strcasecmp(line_tokens[token_count], "remove=clamped") == 0) {
tmp_patch->remove |= SAMPLE_CLAMPED;
}
token_count++;
}
}
}
else if (_WM_Global_ErrorI) { /* malloc() failure in WM_LC_Tokenize_Line() */
WM_FreePatches();
free(line_tokens);
free(config_buffer);
return (-1);
}
/* free up tokens */
free(line_tokens);
}
line_start_ptr = config_ptr + 1;
}
config_ptr++;
}
free(config_buffer);
free(config_dir);
return (0);
}
static int WM_LoadConfig(const char *config_file) {
return load_config(config_file, NULL);
}
static int add_handle(void * handle) {
struct _hndl *tmp_handle = NULL;
if (first_handle == NULL) {
first_handle = (struct _hndl *) malloc(sizeof(struct _hndl));
if (first_handle == NULL) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, " to get ram", errno);
return (-1);
}
first_handle->handle = handle;
first_handle->prev = NULL;
first_handle->next = NULL;
} else {
tmp_handle = first_handle;
if (tmp_handle->next) {
while (tmp_handle->next)
tmp_handle = tmp_handle->next;
}
tmp_handle->next = (struct _hndl *) malloc(sizeof(struct _hndl));
if (tmp_handle->next == NULL) {
_WM_GLOBAL_ERROR(__FUNCTION__, __LINE__, WM_ERR_MEM, " to get ram", errno);
return (-1);
}
tmp_handle->next->prev = tmp_handle;
tmp_handle = tmp_handle->next;
tmp_handle->next = NULL;
tmp_handle->handle = handle;
}
return (0);
}
//#define DEBUG_RESAMPLE
#ifdef DEBUG_RESAMPLE
#define RESAMPLE_DEBUGI(dx,dy) fprintf(stderr,"\r%s, %i\n",dx,dy)
#define RESAMPLE_DEBUGS(dx) fprintf(stderr,"\r%s\n",dx)
#else
#define RESAMPLE_DEBUGI(dx,dy)
#define RESAMPLE_DEBUGS(dx)
#endif
static int WM_GetOutput_Linear(midi * handle, int8_t *buffer, uint32_t size) {
uint32_t buffer_used = 0;
uint32_t i;
struct _mdi *mdi = (struct _mdi *) handle;
uint32_t real_samples_to_mix = 0;
uint32_t data_pos;
int32_t premix, left_mix, right_mix;
// int32_t vol_mul;
struct _note *note_data = NULL;
uint32_t count;
struct _event *event = mdi->current_event;
int32_t *tmp_buffer;
int32_t *out_buffer;
_WM_Lock(&mdi->lock);
buffer_used = 0;
memset(buffer, 0, size);
if ( (size / 2) > mdi->mix_buffer_size) {
if ( (size / 2) <= ( mdi->mix_buffer_size * 2 )) {
mdi->mix_buffer_size += MEM_CHUNK;
} else {
mdi->mix_buffer_size = size / 2;
}
mdi->mix_buffer = (int32_t *) realloc(mdi->mix_buffer, mdi->mix_buffer_size * sizeof(int32_t));
}
tmp_buffer = mdi->mix_buffer;
memset(tmp_buffer, 0, ((size / 2) * sizeof(int32_t)));
out_buffer = tmp_buffer;
do {
if (__builtin_expect((!mdi->samples_to_mix), 0)) {
while ((!mdi->samples_to_mix) && (event->do_event)) {
event->do_event(mdi, &event->event_data);
if ((mdi->extra_info.mixer_options & WM_MO_LOOP) && (event[0].do_event == *_WM_do_meta_endoftrack)) {
_WM_ResetToStart(mdi);
event = mdi->current_event;
} else {
mdi->samples_to_mix = event->samples_to_next;
event++;
mdi->current_event = event;
}
}
if (__builtin_expect((!mdi->samples_to_mix), 0)) {
if (mdi->extra_info.current_sample >= mdi->extra_info.approx_total_samples) {
break;
} else if ((mdi->extra_info.approx_total_samples
- mdi->extra_info.current_sample) > (size >> 2)) {
mdi->samples_to_mix = size >> 2;
} else {
mdi->samples_to_mix = mdi->extra_info.approx_total_samples
- mdi->extra_info.current_sample;
}
}
}
if (__builtin_expect((mdi->samples_to_mix > (size >> 2)), 1)) {
real_samples_to_mix = size >> 2;
} else {
real_samples_to_mix = mdi->samples_to_mix;
if (real_samples_to_mix == 0) {
continue;
}
}
/* do mixing here */
count = real_samples_to_mix;
do {
note_data = mdi->note;
left_mix = right_mix = 0;
RESAMPLE_DEBUGI("SAMPLES_TO_MIX",count);
if (__builtin_expect((note_data != NULL), 1)) {
RESAMPLE_DEBUGS("Processing Notes");
while (note_data) {
/*
* ===================
* resample the sample
* ===================
*/
data_pos = note_data->sample_pos >> FPBITS;
premix = ((note_data->sample->data[data_pos] + (((note_data->sample->data[data_pos + 1] - note_data->sample->data[data_pos]) * (int32_t)(note_data->sample_pos & FPMASK)) / 1024)) * (note_data->env_level >> 12)) / 1024;
left_mix += (premix * (int32_t)note_data->left_mix_volume) / 1024;
right_mix += (premix * (int32_t)note_data->right_mix_volume) / 1024;
/*
* ========================
* sample position checking
* ========================
*/
#ifdef DEBUG_RESAMPLE
fprintf(stderr,"\r\n%d -> INC %i, ENV %i, LEVEL %i, TARGET %d, RATE %i, SAMPLE POS %i, SAMPLE LENGTH %i, PREMIX %i (%i:%i)",
(uint32_t)note_data,
note_data->env_inc,
note_data->env, note_data->env_level,
note_data->sample->env_target[note_data->env],
note_data->sample->env_rate[note_data->env],
note_data->sample_pos,
note_data->sample->data_length,
premix, left_mix, right_mix);
if (note_data->modes & SAMPLE_LOOP)
fprintf(stderr,", LOOP %i + %i",
note_data->sample->loop_start,
note_data->sample->loop_size);
fprintf(stderr,"\r\n");
#endif
note_data->sample_pos += note_data->sample_inc;
if (__builtin_expect((note_data->modes & SAMPLE_LOOP), 1)) {
if (__builtin_expect(
(note_data->sample_pos > note_data->sample->loop_end),
0)) {
note_data->sample_pos = note_data->sample->loop_start
+ ((note_data->sample_pos
- note_data->sample->loop_start)
% note_data->sample->loop_size);
}
} else if (__builtin_expect(
(note_data->sample_pos
>= note_data->sample->data_length),
0)) {
goto _END_THIS_NOTE;
}
if (__builtin_expect((note_data->env_inc == 0), 0)) {
note_data = note_data->next;
RESAMPLE_DEBUGS("Next Note: 0 env_inc");
continue;
}
note_data->env_level += note_data->env_inc;
if (note_data->env_inc < 0) {
if (__builtin_expect((note_data->env_level
> note_data->sample->env_target[note_data->env]), 0)) {
note_data = note_data->next;
RESAMPLE_DEBUGS("Next Note: env_lvl > env_target");
continue;
}
} else if (note_data->env_inc > 0) {
if (__builtin_expect((note_data->env_level
< note_data->sample->env_target[note_data->env]), 0)) {
note_data = note_data->next;
RESAMPLE_DEBUGS("Next Note: env_lvl < env_target");
continue;
}
}
// Yes could have a condition here but
// it would crete another bottleneck
note_data->env_level =
note_data->sample->env_target[note_data->env];
switch (note_data->env) {
case 0:
if (!(note_data->modes & SAMPLE_ENVELOPE)) {
note_data->env_inc = 0;
note_data = note_data->next;
RESAMPLE_DEBUGS("Next Note: No Envelope");
continue;
}
break;
case 2:
if (note_data->modes & SAMPLE_SUSTAIN /*|| note_data->hold*/) {
note_data->env_inc = 0;
note_data = note_data->next;
RESAMPLE_DEBUGS("Next Note: SAMPLE_SUSTAIN");
continue;
} else if (note_data->modes & SAMPLE_CLAMPED) {
note_data->env = 5;
if (note_data->env_level
> note_data->sample->env_target[5]) {
note_data->env_inc =
-note_data->sample->env_rate[5];
} else {
note_data->env_inc =