/
instrum_dls.c
1255 lines (1143 loc) · 38.9 KB
/
instrum_dls.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
/*
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
This program is free software; you can redistribute it and/or modify
it under the terms of the Perl Artistic License, available in COPYING.
instrum.h
*/
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
#include "SDL_endian.h"
#include "SDL_rwops.h"
#include "SDL.h"
#include "timidity.h"
#include "options.h"
#include "instrum.h"
#include "tables.h"
#include "common.h"
/*-------------------------------------------------------------------------*/
/* * * * * * * * * * * * * * * * * load_riff.h * * * * * * * * * * * * * * */
/*-------------------------------------------------------------------------*/
typedef struct _RIFF_Chunk {
Uint32 magic;
Uint32 length;
Uint32 subtype;
Uint8 *data;
struct _RIFF_Chunk *child;
struct _RIFF_Chunk *next;
} RIFF_Chunk;
extern RIFF_Chunk* LoadRIFF(SDL_RWops *src);
extern void FreeRIFF(RIFF_Chunk *chunk);
extern void PrintRIFF(RIFF_Chunk *chunk, int level);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*-------------------------------------------------------------------------*/
/* * * * * * * * * * * * * * * * * load_riff.c * * * * * * * * * * * * * * */
/*-------------------------------------------------------------------------*/
#define RIFF 0x46464952 /* "RIFF" */
#define LIST 0x5453494c /* "LIST" */
static RIFF_Chunk *AllocRIFFChunk()
{
RIFF_Chunk *chunk = (RIFF_Chunk *)malloc(sizeof(*chunk));
if ( !chunk ) {
SDL_Error(SDL_ENOMEM);
return NULL;
}
memset(chunk, 0, sizeof(*chunk));
return chunk;
}
static void FreeRIFFChunk(RIFF_Chunk *chunk)
{
if ( chunk->child ) {
FreeRIFFChunk(chunk->child);
}
if ( chunk->next ) {
FreeRIFFChunk(chunk->next);
}
free(chunk);
}
static int ChunkHasSubType(Uint32 magic)
{
static Uint32 chunk_list[] = {
RIFF, LIST
};
int i;
for ( i = 0; i < SDL_TABLESIZE(chunk_list); ++i ) {
if ( magic == chunk_list[i] ) {
return 1;
}
}
return 0;
}
static int ChunkHasSubChunks(Uint32 magic)
{
static Uint32 chunk_list[] = {
RIFF, LIST
};
int i;
for ( i = 0; i < SDL_TABLESIZE(chunk_list); ++i ) {
if ( magic == chunk_list[i] ) {
return 1;
}
}
return 0;
}
static void LoadSubChunks(RIFF_Chunk *chunk, Uint8 *data, Uint32 left)
{
Uint8 *subchunkData;
Uint32 subchunkDataLen;
while ( left > 8 ) {
RIFF_Chunk *child = AllocRIFFChunk();
RIFF_Chunk *next, *prev = NULL;
for ( next = chunk->child; next; next = next->next ) {
prev = next;
}
if ( prev ) {
prev->next = child;
} else {
chunk->child = child;
}
child->magic = (data[0] << 0) |
(data[1] << 8) |
(data[2] << 16) |
(data[3] << 24);
data += 4;
left -= 4;
child->length = (data[0] << 0) |
(data[1] << 8) |
(data[2] << 16) |
(data[3] << 24);
data += 4;
left -= 4;
child->data = data;
if ( child->length > left ) {
child->length = left;
}
subchunkData = child->data;
subchunkDataLen = child->length;
if ( ChunkHasSubType(child->magic) && subchunkDataLen >= 4 ) {
child->subtype = (subchunkData[0] << 0) |
(subchunkData[1] << 8) |
(subchunkData[2] << 16) |
(subchunkData[3] << 24);
subchunkData += 4;
subchunkDataLen -= 4;
}
if ( ChunkHasSubChunks(child->magic) ) {
LoadSubChunks(child, subchunkData, subchunkDataLen);
}
data += child->length;
left -= child->length;
}
}
RIFF_Chunk *LoadRIFF(SDL_RWops *src)
{
RIFF_Chunk *chunk;
Uint8 *subchunkData;
Uint32 subchunkDataLen;
/* Allocate the chunk structure */
chunk = AllocRIFFChunk();
/* Make sure the file is in RIFF format */
chunk->magic = SDL_ReadLE32(src);
chunk->length = SDL_ReadLE32(src);
if ( chunk->magic != RIFF ) {
SDL_SetError("Not a RIFF file");
FreeRIFFChunk(chunk);
return NULL;
}
chunk->data = (Uint8 *)malloc(chunk->length);
if ( chunk->data == NULL ) {
SDL_Error(SDL_ENOMEM);
FreeRIFFChunk(chunk);
return NULL;
}
if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) {
SDL_Error(SDL_EFREAD);
FreeRIFF(chunk);
return NULL;
}
subchunkData = chunk->data;
subchunkDataLen = chunk->length;
if ( ChunkHasSubType(chunk->magic) && subchunkDataLen >= 4 ) {
chunk->subtype = (subchunkData[0] << 0) |
(subchunkData[1] << 8) |
(subchunkData[2] << 16) |
(subchunkData[3] << 24);
subchunkData += 4;
subchunkDataLen -= 4;
}
if ( ChunkHasSubChunks(chunk->magic) ) {
LoadSubChunks(chunk, subchunkData, subchunkDataLen);
}
return chunk;
}
void FreeRIFF(RIFF_Chunk *chunk)
{
free(chunk->data);
FreeRIFFChunk(chunk);
}
void PrintRIFF(RIFF_Chunk *chunk, int level)
{
static char prefix[128];
if ( level == sizeof(prefix)-1 ) {
return;
}
if ( level > 0 ) {
prefix[(level-1)*2] = ' ';
prefix[(level-1)*2+1] = ' ';
}
prefix[level*2] = '\0';
printf("%sChunk: %c%c%c%c (%d bytes)", prefix,
((chunk->magic >> 0) & 0xFF),
((chunk->magic >> 8) & 0xFF),
((chunk->magic >> 16) & 0xFF),
((chunk->magic >> 24) & 0xFF), chunk->length);
if ( chunk->subtype ) {
printf(" subtype: %c%c%c%c",
((chunk->subtype >> 0) & 0xFF),
((chunk->subtype >> 8) & 0xFF),
((chunk->subtype >> 16) & 0xFF),
((chunk->subtype >> 24) & 0xFF));
}
printf("\n");
if ( chunk->child ) {
printf("%s{\n", prefix);
PrintRIFF(chunk->child, level + 1);
printf("%s}\n", prefix);
}
if ( chunk->next ) {
PrintRIFF(chunk->next, level);
}
if ( level > 0 ) {
prefix[(level-1)*2] = '\0';
}
}
#ifdef TEST_MAIN_RIFF
main(int argc, char *argv[])
{
int i;
for ( i = 1; i < argc; ++i ) {
RIFF_Chunk *chunk;
SDL_RWops *src = SDL_RWFromFile(argv[i], "rb");
if ( !src ) {
fprintf(stderr, "Couldn't open %s: %s", argv[i], SDL_GetError());
continue;
}
chunk = LoadRIFF(src);
if ( chunk ) {
PrintRIFF(chunk, 0);
FreeRIFF(chunk);
} else {
fprintf(stderr, "Couldn't load %s: %s\n", argv[i], SDL_GetError());
}
SDL_RWclose(src);
}
}
#endif // TEST_MAIN
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*-------------------------------------------------------------------------*/
/* * * * * * * * * * * * * * * * * load_dls.h * * * * * * * * * * * * * * */
/*-------------------------------------------------------------------------*/
/* This code is based on the DLS spec version 1.1, available at:
http://www.midi.org/about-midi/dls/dlsspec.shtml
*/
/* Some typedefs so the public dls headers don't need to be modified */
#define FAR
typedef Uint8 BYTE;
typedef Sint16 SHORT;
typedef Uint16 USHORT;
typedef Uint16 WORD;
typedef Sint32 LONG;
typedef Uint32 ULONG;
typedef Uint32 DWORD;
#define mmioFOURCC(A, B, C, D) \
(((A) << 0) | ((B) << 8) | ((C) << 16) | ((D) << 24))
#define DEFINE_GUID(A, B, C, E, F, G, H, I, J, K, L, M)
#include "dls1.h"
#include "dls2.h"
typedef struct _WaveFMT {
WORD wFormatTag;
WORD wChannels;
DWORD dwSamplesPerSec;
DWORD dwAvgBytesPerSec;
WORD wBlockAlign;
WORD wBitsPerSample;
} WaveFMT;
typedef struct _DLS_Wave {
WaveFMT *format;
Uint8 *data;
Uint32 length;
WSMPL *wsmp;
WLOOP *wsmp_loop;
} DLS_Wave;
typedef struct _DLS_Region {
RGNHEADER *header;
WAVELINK *wlnk;
WSMPL *wsmp;
WLOOP *wsmp_loop;
CONNECTIONLIST *art;
CONNECTION *artList;
} DLS_Region;
typedef struct _DLS_Instrument {
const char *name;
INSTHEADER *header;
DLS_Region *regions;
CONNECTIONLIST *art;
CONNECTION *artList;
} DLS_Instrument;
typedef struct _DLS_Data {
struct _RIFF_Chunk *chunk;
Uint32 cInstruments;
DLS_Instrument *instruments;
POOLTABLE *ptbl;
POOLCUE *ptblList;
DLS_Wave *waveList;
const char *name;
const char *artist;
const char *copyright;
const char *comments;
} DLS_Data;
extern DLS_Data* LoadDLS(SDL_RWops *src);
extern void FreeDLS(DLS_Data *chunk);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*-------------------------------------------------------------------------*/
/* * * * * * * * * * * * * * * * * load_dls.c * * * * * * * * * * * * * * */
/*-------------------------------------------------------------------------*/
#define FOURCC_LIST 0x5453494c /* "LIST" */
#define FOURCC_FMT 0x20746D66 /* "fmt " */
#define FOURCC_DATA 0x61746164 /* "data" */
#define FOURCC_INFO mmioFOURCC('I','N','F','O')
#define FOURCC_IARL mmioFOURCC('I','A','R','L')
#define FOURCC_IART mmioFOURCC('I','A','R','T')
#define FOURCC_ICMS mmioFOURCC('I','C','M','S')
#define FOURCC_ICMT mmioFOURCC('I','C','M','T')
#define FOURCC_ICOP mmioFOURCC('I','C','O','P')
#define FOURCC_ICRD mmioFOURCC('I','C','R','D')
#define FOURCC_IENG mmioFOURCC('I','E','N','G')
#define FOURCC_IGNR mmioFOURCC('I','G','N','R')
#define FOURCC_IKEY mmioFOURCC('I','K','E','Y')
#define FOURCC_IMED mmioFOURCC('I','M','E','D')
#define FOURCC_INAM mmioFOURCC('I','N','A','M')
#define FOURCC_IPRD mmioFOURCC('I','P','R','D')
#define FOURCC_ISBJ mmioFOURCC('I','S','B','J')
#define FOURCC_ISFT mmioFOURCC('I','S','F','T')
#define FOURCC_ISRC mmioFOURCC('I','S','R','C')
#define FOURCC_ISRF mmioFOURCC('I','S','R','F')
#define FOURCC_ITCH mmioFOURCC('I','T','C','H')
static void FreeRegions(DLS_Instrument *instrument)
{
if ( instrument->regions ) {
free(instrument->regions);
}
}
static void AllocRegions(DLS_Instrument *instrument)
{
int datalen = (instrument->header->cRegions * sizeof(DLS_Region));
FreeRegions(instrument);
instrument->regions = (DLS_Region *)malloc(datalen);
if ( instrument->regions ) {
memset(instrument->regions, 0, datalen);
}
}
static void FreeInstruments(DLS_Data *data)
{
if ( data->instruments ) {
Uint32 i;
for ( i = 0; i < data->cInstruments; ++i ) {
FreeRegions(&data->instruments[i]);
}
free(data->instruments);
}
}
static void AllocInstruments(DLS_Data *data)
{
int datalen = (data->cInstruments * sizeof(DLS_Instrument));
FreeInstruments(data);
data->instruments = (DLS_Instrument *)malloc(datalen);
if ( data->instruments ) {
memset(data->instruments, 0, datalen);
}
}
static void FreeWaveList(DLS_Data *data)
{
if ( data->waveList ) {
free(data->waveList);
}
}
static void AllocWaveList(DLS_Data *data)
{
int datalen = (data->ptbl->cCues * sizeof(DLS_Wave));
FreeWaveList(data);
data->waveList = (DLS_Wave *)malloc(datalen);
if ( data->waveList ) {
memset(data->waveList, 0, datalen);
}
}
static void Parse_colh(DLS_Data *data, RIFF_Chunk *chunk)
{
data->cInstruments = SDL_SwapLE32(*(Uint32 *)chunk->data);
AllocInstruments(data);
}
static void Parse_insh(DLS_Data *data, RIFF_Chunk *chunk, DLS_Instrument *instrument)
{
INSTHEADER *header = (INSTHEADER *)chunk->data;
header->cRegions = SDL_SwapLE32(header->cRegions);
header->Locale.ulBank = SDL_SwapLE32(header->Locale.ulBank);
header->Locale.ulInstrument = SDL_SwapLE32(header->Locale.ulInstrument);
instrument->header = header;
AllocRegions(instrument);
}
static void Parse_rgnh(DLS_Data *data, RIFF_Chunk *chunk, DLS_Region *region)
{
RGNHEADER *header = (RGNHEADER *)chunk->data;
header->RangeKey.usLow = SDL_SwapLE16(header->RangeKey.usLow);
header->RangeKey.usHigh = SDL_SwapLE16(header->RangeKey.usHigh);
header->RangeVelocity.usLow = SDL_SwapLE16(header->RangeVelocity.usLow);
header->RangeVelocity.usHigh = SDL_SwapLE16(header->RangeVelocity.usHigh);
header->fusOptions = SDL_SwapLE16(header->fusOptions);
header->usKeyGroup = SDL_SwapLE16(header->usKeyGroup);
region->header = header;
}
static void Parse_wlnk(DLS_Data *data, RIFF_Chunk *chunk, DLS_Region *region)
{
WAVELINK *wlnk = (WAVELINK *)chunk->data;
wlnk->fusOptions = SDL_SwapLE16(wlnk->fusOptions);
wlnk->usPhaseGroup = SDL_SwapLE16(wlnk->usPhaseGroup);
wlnk->ulChannel = SDL_SwapLE16(wlnk->ulChannel);
wlnk->ulTableIndex = SDL_SwapLE16(wlnk->ulTableIndex);
region->wlnk = wlnk;
}
static void Parse_wsmp(DLS_Data *data, RIFF_Chunk *chunk, WSMPL **wsmp_ptr, WLOOP **wsmp_loop_ptr)
{
Uint32 i;
WSMPL *wsmp = (WSMPL *)chunk->data;
WLOOP *loop;
wsmp->cbSize = SDL_SwapLE32(wsmp->cbSize);
wsmp->usUnityNote = SDL_SwapLE16(wsmp->usUnityNote);
wsmp->sFineTune = SDL_SwapLE16(wsmp->sFineTune);
wsmp->lAttenuation = SDL_SwapLE32(wsmp->lAttenuation);
wsmp->fulOptions = SDL_SwapLE32(wsmp->fulOptions);
wsmp->cSampleLoops = SDL_SwapLE32(wsmp->cSampleLoops);
loop = (WLOOP *)((Uint8 *)chunk->data + wsmp->cbSize);
*wsmp_ptr = wsmp;
*wsmp_loop_ptr = loop;
for ( i = 0; i < wsmp->cSampleLoops; ++i ) {
loop->cbSize = SDL_SwapLE32(loop->cbSize);
loop->ulType = SDL_SwapLE32(loop->ulType);
loop->ulStart = SDL_SwapLE32(loop->ulStart);
loop->ulLength = SDL_SwapLE32(loop->ulLength);
++loop;
}
}
static void Parse_art(DLS_Data *data, RIFF_Chunk *chunk, CONNECTIONLIST **art_ptr, CONNECTION **artList_ptr)
{
Uint32 i;
CONNECTIONLIST *art = (CONNECTIONLIST *)chunk->data;
CONNECTION *artList;
art->cbSize = SDL_SwapLE32(art->cbSize);
art->cConnections = SDL_SwapLE32(art->cConnections);
artList = (CONNECTION *)((Uint8 *)chunk->data + art->cbSize);
*art_ptr = art;
*artList_ptr = artList;
for ( i = 0; i < art->cConnections; ++i ) {
artList->usSource = SDL_SwapLE16(artList->usSource);
artList->usControl = SDL_SwapLE16(artList->usControl);
artList->usDestination = SDL_SwapLE16(artList->usDestination);
artList->usTransform = SDL_SwapLE16(artList->usTransform);
artList->lScale = SDL_SwapLE32(artList->lScale);
++artList;
}
}
static void Parse_lart(DLS_Data *data, RIFF_Chunk *chunk, CONNECTIONLIST **conn_ptr, CONNECTION **connList_ptr)
{
/* FIXME: This only supports one set of connections */
for ( chunk = chunk->child; chunk; chunk = chunk->next ) {
Uint32 magic = (chunk->magic == FOURCC_LIST) ? chunk->subtype : chunk->magic;
switch(magic) {
case FOURCC_ART1:
case FOURCC_ART2:
Parse_art(data, chunk, conn_ptr, connList_ptr);
return;
}
}
}
static void Parse_rgn(DLS_Data *data, RIFF_Chunk *chunk, DLS_Region *region)
{
for ( chunk = chunk->child; chunk; chunk = chunk->next ) {
Uint32 magic = (chunk->magic == FOURCC_LIST) ? chunk->subtype : chunk->magic;
switch(magic) {
case FOURCC_RGNH:
Parse_rgnh(data, chunk, region);
break;
case FOURCC_WLNK:
Parse_wlnk(data, chunk, region);
break;
case FOURCC_WSMP:
Parse_wsmp(data, chunk, ®ion->wsmp, ®ion->wsmp_loop);
break;
case FOURCC_LART:
case FOURCC_LAR2:
Parse_lart(data, chunk, ®ion->art, ®ion->artList);
break;
}
}
}
static void Parse_lrgn(DLS_Data *data, RIFF_Chunk *chunk, DLS_Instrument *instrument)
{
Uint32 region = 0;
for ( chunk = chunk->child; chunk; chunk = chunk->next ) {
Uint32 magic = (chunk->magic == FOURCC_LIST) ? chunk->subtype : chunk->magic;
switch(magic) {
case FOURCC_RGN:
case FOURCC_RGN2:
if ( region < instrument->header->cRegions ) {
Parse_rgn(data, chunk, &instrument->regions[region++]);
}
break;
}
}
}
static void Parse_INFO_INS(DLS_Data *data, RIFF_Chunk *chunk, DLS_Instrument *instrument)
{
for ( chunk = chunk->child; chunk; chunk = chunk->next ) {
Uint32 magic = (chunk->magic == FOURCC_LIST) ? chunk->subtype : chunk->magic;
switch(magic) {
case FOURCC_INAM: /* Name */
instrument->name = (char *)chunk->data;
break;
}
}
}
static void Parse_ins(DLS_Data *data, RIFF_Chunk *chunk, DLS_Instrument *instrument)
{
for ( chunk = chunk->child; chunk; chunk = chunk->next ) {
Uint32 magic = (chunk->magic == FOURCC_LIST) ? chunk->subtype : chunk->magic;
switch(magic) {
case FOURCC_INSH:
Parse_insh(data, chunk, instrument);
break;
case FOURCC_LRGN:
Parse_lrgn(data, chunk, instrument);
break;
case FOURCC_LART:
case FOURCC_LAR2:
Parse_lart(data, chunk, &instrument->art, &instrument->artList);
break;
case FOURCC_INFO:
Parse_INFO_INS(data, chunk, instrument);
break;
}
}
}
static void Parse_lins(DLS_Data *data, RIFF_Chunk *chunk)
{
Uint32 instrument = 0;
for ( chunk = chunk->child; chunk; chunk = chunk->next ) {
Uint32 magic = (chunk->magic == FOURCC_LIST) ? chunk->subtype : chunk->magic;
switch(magic) {
case FOURCC_INS:
if ( instrument < data->cInstruments ) {
Parse_ins(data, chunk, &data->instruments[instrument++]);
}
break;
}
}
}
static void Parse_ptbl(DLS_Data *data, RIFF_Chunk *chunk)
{
Uint32 i;
POOLTABLE *ptbl = (POOLTABLE *)chunk->data;
ptbl->cbSize = SDL_SwapLE32(ptbl->cbSize);
ptbl->cCues = SDL_SwapLE32(ptbl->cCues);
data->ptbl = ptbl;
data->ptblList = (POOLCUE *)((Uint8 *)chunk->data + ptbl->cbSize);
for ( i = 0; i < ptbl->cCues; ++i ) {
data->ptblList[i].ulOffset = SDL_SwapLE32(data->ptblList[i].ulOffset);
}
AllocWaveList(data);
}
static void Parse_fmt(DLS_Data *data, RIFF_Chunk *chunk, DLS_Wave *wave)
{
WaveFMT *fmt = (WaveFMT *)chunk->data;
fmt->wFormatTag = SDL_SwapLE16(fmt->wFormatTag);
fmt->wChannels = SDL_SwapLE16(fmt->wChannels);
fmt->dwSamplesPerSec = SDL_SwapLE32(fmt->dwSamplesPerSec);
fmt->dwAvgBytesPerSec = SDL_SwapLE32(fmt->dwAvgBytesPerSec);
fmt->wBlockAlign = SDL_SwapLE16(fmt->wBlockAlign);
fmt->wBitsPerSample = SDL_SwapLE16(fmt->wBitsPerSample);
wave->format = fmt;
}
static void Parse_data(DLS_Data *data, RIFF_Chunk *chunk, DLS_Wave *wave)
{
wave->data = chunk->data;
wave->length = chunk->length;
}
static void Parse_wave(DLS_Data *data, RIFF_Chunk *chunk, DLS_Wave *wave)
{
for ( chunk = chunk->child; chunk; chunk = chunk->next ) {
Uint32 magic = (chunk->magic == FOURCC_LIST) ? chunk->subtype : chunk->magic;
switch(magic) {
case FOURCC_FMT:
Parse_fmt(data, chunk, wave);
break;
case FOURCC_DATA:
Parse_data(data, chunk, wave);
break;
case FOURCC_WSMP:
Parse_wsmp(data, chunk, &wave->wsmp, &wave->wsmp_loop);
break;
}
}
}
static void Parse_wvpl(DLS_Data *data, RIFF_Chunk *chunk)
{
Uint32 wave = 0;
for ( chunk = chunk->child; chunk; chunk = chunk->next ) {
Uint32 magic = (chunk->magic == FOURCC_LIST) ? chunk->subtype : chunk->magic;
switch(magic) {
case FOURCC_wave:
if ( wave < data->ptbl->cCues ) {
Parse_wave(data, chunk, &data->waveList[wave++]);
}
break;
}
}
}
static void Parse_INFO_DLS(DLS_Data *data, RIFF_Chunk *chunk)
{
for ( chunk = chunk->child; chunk; chunk = chunk->next ) {
Uint32 magic = (chunk->magic == FOURCC_LIST) ? chunk->subtype : chunk->magic;
switch(magic) {
case FOURCC_IARL: /* Archival Location */
break;
case FOURCC_IART: /* Artist */
data->artist = (char *)chunk->data;
break;
case FOURCC_ICMS: /* Commisioned */
break;
case FOURCC_ICMT: /* Comments */
data->comments = (char *)chunk->data;
break;
case FOURCC_ICOP: /* Copyright */
data->copyright = (char *)chunk->data;
break;
case FOURCC_ICRD: /* Creation Date */
break;
case FOURCC_IENG: /* Engineer */
break;
case FOURCC_IGNR: /* Genre */
break;
case FOURCC_IKEY: /* Keywords */
break;
case FOURCC_IMED: /* Medium */
break;
case FOURCC_INAM: /* Name */
data->name = (char *)chunk->data;
break;
case FOURCC_IPRD: /* Product */
break;
case FOURCC_ISBJ: /* Subject */
break;
case FOURCC_ISFT: /* Software */
break;
case FOURCC_ISRC: /* Source */
break;
case FOURCC_ISRF: /* Source Form */
break;
case FOURCC_ITCH: /* Technician */
break;
}
}
}
DLS_Data *LoadDLS(SDL_RWops *src)
{
RIFF_Chunk *chunk;
DLS_Data *data = (DLS_Data *)malloc(sizeof(*data));
if ( !data ) {
SDL_Error(SDL_ENOMEM);
return NULL;
}
memset(data, 0, sizeof(*data));
data->chunk = LoadRIFF(src);
if ( !data->chunk ) {
FreeDLS(data);
return NULL;
}
for ( chunk = data->chunk->child; chunk; chunk = chunk->next ) {
Uint32 magic = (chunk->magic == FOURCC_LIST) ? chunk->subtype : chunk->magic;
switch(magic) {
case FOURCC_COLH:
Parse_colh(data, chunk);
break;
case FOURCC_LINS:
Parse_lins(data, chunk);
break;
case FOURCC_PTBL:
Parse_ptbl(data, chunk);
break;
case FOURCC_WVPL:
Parse_wvpl(data, chunk);
break;
case FOURCC_INFO:
Parse_INFO_DLS(data, chunk);
break;
}
}
return data;
}
void FreeDLS(DLS_Data *data)
{
if ( data->chunk ) {
FreeRIFF(data->chunk);
}
FreeInstruments(data);
FreeWaveList(data);
free(data);
}
static const char *SourceToString(USHORT usSource)
{
switch(usSource) {
case CONN_SRC_NONE:
return "NONE";
case CONN_SRC_LFO:
return "LFO";
case CONN_SRC_KEYONVELOCITY:
return "KEYONVELOCITY";
case CONN_SRC_KEYNUMBER:
return "KEYNUMBER";
case CONN_SRC_EG1:
return "EG1";
case CONN_SRC_EG2:
return "EG2";
case CONN_SRC_PITCHWHEEL:
return "PITCHWHEEL";
case CONN_SRC_CC1:
return "CC1";
case CONN_SRC_CC7:
return "CC7";
case CONN_SRC_CC10:
return "CC10";
case CONN_SRC_CC11:
return "CC11";
case CONN_SRC_POLYPRESSURE:
return "POLYPRESSURE";
case CONN_SRC_CHANNELPRESSURE:
return "CHANNELPRESSURE";
case CONN_SRC_VIBRATO:
return "VIBRATO";
case CONN_SRC_MONOPRESSURE:
return "MONOPRESSURE";
case CONN_SRC_CC91:
return "CC91";
case CONN_SRC_CC93:
return "CC93";
default:
return "UNKNOWN";
}
}
static const char *TransformToString(USHORT usTransform)
{
switch (usTransform) {
case CONN_TRN_NONE:
return "NONE";
case CONN_TRN_CONCAVE:
return "CONCAVE";
case CONN_TRN_CONVEX:
return "CONVEX";
case CONN_TRN_SWITCH:
return "SWITCH";
default:
return "UNKNOWN";
}
}
static const char *DestinationToString(USHORT usDestination)
{
switch (usDestination) {
case CONN_DST_NONE:
return "NONE";
case CONN_DST_ATTENUATION:
return "ATTENUATION";
case CONN_DST_PITCH:
return "PITCH";
case CONN_DST_PAN:
return "PAN";
case CONN_DST_LFO_FREQUENCY:
return "LFO_FREQUENCY";
case CONN_DST_LFO_STARTDELAY:
return "LFO_STARTDELAY";
case CONN_DST_EG1_ATTACKTIME:
return "EG1_ATTACKTIME";
case CONN_DST_EG1_DECAYTIME:
return "EG1_DECAYTIME";
case CONN_DST_EG1_RELEASETIME:
return "EG1_RELEASETIME";
case CONN_DST_EG1_SUSTAINLEVEL:
return "EG1_SUSTAINLEVEL";
case CONN_DST_EG2_ATTACKTIME:
return "EG2_ATTACKTIME";
case CONN_DST_EG2_DECAYTIME:
return "EG2_DECAYTIME";
case CONN_DST_EG2_RELEASETIME:
return "EG2_RELEASETIME";
case CONN_DST_EG2_SUSTAINLEVEL:
return "EG2_SUSTAINLEVEL";
case CONN_DST_KEYNUMBER:
return "KEYNUMBER";
case CONN_DST_LEFT:
return "LEFT";
case CONN_DST_RIGHT:
return "RIGHT";
case CONN_DST_CENTER:
return "CENTER";
case CONN_DST_LEFTREAR:
return "LEFTREAR";
case CONN_DST_RIGHTREAR:
return "RIGHTREAR";
case CONN_DST_LFE_CHANNEL:
return "LFE_CHANNEL";
case CONN_DST_CHORUS:
return "CHORUS";
case CONN_DST_REVERB:
return "REVERB";
case CONN_DST_VIB_FREQUENCY:
return "VIB_FREQUENCY";
case CONN_DST_VIB_STARTDELAY:
return "VIB_STARTDELAY";
case CONN_DST_EG1_DELAYTIME:
return "EG1_DELAYTIME";
case CONN_DST_EG1_HOLDTIME:
return "EG1_HOLDTIME";
case CONN_DST_EG1_SHUTDOWNTIME:
return "EG1_SHUTDOWNTIME";
case CONN_DST_EG2_DELAYTIME:
return "EG2_DELAYTIME";
case CONN_DST_EG2_HOLDTIME:
return "EG2_HOLDTIME";
case CONN_DST_FILTER_CUTOFF:
return "FILTER_CUTOFF";
case CONN_DST_FILTER_Q:
return "FILTER_Q";
default:
return "UNKOWN";
}
}
static void PrintArt(const char *type, CONNECTIONLIST *art, CONNECTION *artList)
{
Uint32 i;
printf("%s Connections:\n", type);
for ( i = 0; i < art->cConnections; ++i ) {
printf(" Source: %s, Control: %s, Destination: %s, Transform: %s, Scale: %d\n",
SourceToString(artList[i].usSource),
SourceToString(artList[i].usControl),
DestinationToString(artList[i].usDestination),
TransformToString(artList[i].usTransform),
artList[i].lScale);
}
}
static void PrintWave(DLS_Wave *wave, Uint32 index)
{
WaveFMT *format = wave->format;
if ( format ) {
printf(" Wave %u: Format: %hu, %hu channels, %u Hz, %hu bits (length = %u)\n", index, format->wFormatTag, format->wChannels, format->dwSamplesPerSec, format->wBitsPerSample, wave->length);
}
if ( wave->wsmp ) {
Uint32 i;
printf(" wsmp->usUnityNote = %hu\n", wave->wsmp->usUnityNote);
printf(" wsmp->sFineTune = %hd\n", wave->wsmp->sFineTune);
printf(" wsmp->lAttenuation = %d\n", wave->wsmp->lAttenuation);
printf(" wsmp->fulOptions = 0x%8.8x\n", wave->wsmp->fulOptions);
printf(" wsmp->cSampleLoops = %u\n", wave->wsmp->cSampleLoops);
for ( i = 0; i < wave->wsmp->cSampleLoops; ++i ) {
WLOOP *loop = &wave->wsmp_loop[i];
printf(" Loop %u:\n", i);
printf(" ulStart = %u\n", loop->ulStart);
printf(" ulLength = %u\n", loop->ulLength);
}
}
}
static void PrintRegion(DLS_Region *region, Uint32 index)
{
printf(" Region %u:\n", index);
if ( region->header ) {
printf(" RangeKey = { %hu - %hu }\n", region->header->RangeKey.usLow, region->header->RangeKey.usHigh);
printf(" RangeVelocity = { %hu - %hu }\n", region->header->RangeVelocity.usLow, region->header->RangeVelocity.usHigh);
printf(" fusOptions = 0x%4.4hx\n", region->header->fusOptions);
printf(" usKeyGroup = %hu\n", region->header->usKeyGroup);
}
if ( region->wlnk ) {
printf(" wlnk->fusOptions = 0x%4.4hx\n", region->wlnk->fusOptions);
printf(" wlnk->usPhaseGroup = %hu\n", region->wlnk->usPhaseGroup);
printf(" wlnk->ulChannel = %u\n", region->wlnk->ulChannel);
printf(" wlnk->ulTableIndex = %u\n", region->wlnk->ulTableIndex);
}
if ( region->wsmp ) {
Uint32 i;
printf(" wsmp->usUnityNote = %hu\n", region->wsmp->usUnityNote);
printf(" wsmp->sFineTune = %hd\n", region->wsmp->sFineTune);
printf(" wsmp->lAttenuation = %d\n", region->wsmp->lAttenuation);
printf(" wsmp->fulOptions = 0x%8.8x\n", region->wsmp->fulOptions);
printf(" wsmp->cSampleLoops = %u\n", region->wsmp->cSampleLoops);
for ( i = 0; i < region->wsmp->cSampleLoops; ++i ) {
WLOOP *loop = ®ion->wsmp_loop[i];
printf(" Loop %u:\n", i);
printf(" ulStart = %u\n", loop->ulStart);
printf(" ulLength = %u\n", loop->ulLength);
}
}
if ( region->art && region->art->cConnections > 0 ) {
PrintArt("Region", region->art, region->artList);
}
}
static void PrintInstrument(DLS_Instrument *instrument, Uint32 index)
{
printf("Instrument %u:\n", index);
if ( instrument->name ) {
printf(" Name: %s\n", instrument->name);
}
if ( instrument->header ) {
Uint32 i;
printf(" ulBank = 0x%8.8x\n", instrument->header->Locale.ulBank);
printf(" ulInstrument = %u\n", instrument->header->Locale.ulInstrument);
printf(" Regions: %u\n", instrument->header->cRegions);
for ( i = 0; i < instrument->header->cRegions; ++i ) {
PrintRegion(&instrument->regions[i], i);
}
}
if ( instrument->art && instrument->art->cConnections > 0 ) {
PrintArt("Instrument", instrument->art, instrument->artList);
}
};
void PrintDLS(DLS_Data *data)
{
printf("DLS Data:\n");
printf("cInstruments = %u\n", data->cInstruments);
if ( data->instruments ) {
Uint32 i;
for ( i = 0; i < data->cInstruments; ++i ) {
PrintInstrument(&data->instruments[i], i);
}
}