-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
pdx2sf2.c
162 lines (144 loc) · 5.26 KB
/
pdx2sf2.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
#include <string.h>
#include "pdx.h"
#include "adpcm.h"
#include "tools.h"
#include "Soundfont.h"
int main(int argc, char **argv) {
printf("ass %d\n", argc);
for(int i = 1; i < argc; i++) {
printf("balls %s\n", argv[i]);
size_t data_len = 0;
uint8_t *data = load_file(argv[1], &data_len);
if(!data) {
fprintf(stderr, "Could not load %s\n", argv[1]);
return 1;
}
struct pdx_file p;
pdx_file_load(&p, data, data_len);
SF2_DATA* s = CreateSF2Base(argv[i]);
int totalSamplesSize = 0;
int totalSamples = 0;
for(int j = 0; j < PDX_NUM_SAMPLES; j++) {
if(p.samples[j].len > 0) {
totalSamplesSize += p.samples[j].num_samples + 46;
totalSamples++;
}
}
int16_t *samples = (int16_t *)malloc(totalSamplesSize * 2);
memset(samples, 0, totalSamplesSize * 2);
int curPos = 0;
sfSample *sampleHeaders = (sfSample *)malloc((totalSamples + 1) * sizeof(sfSample));
int x = 0;
for(int j = 0; j < PDX_NUM_SAMPLES; j++) {
if(p.samples[j].len > 0) {
memcpy(samples + curPos, p.samples[j].decoded_data, p.samples[j].num_samples * sizeof(int16_t));
sfSample *h = sampleHeaders + x;
h->dwSampleRate = 15600;
h->byOriginalKey = 60;
h->chCorrection = 0;
h->wSampleLink = 0;
h->sfSampleType = monoSample;
snprintf(h->achSampleName, sizeof(h->achSampleName), "Sample %03X", x);
h->dwStart = curPos;
h->dwEnd = curPos + p.samples[j].num_samples + 46;
h->dwStartloop = curPos;
h->dwEndloop = h->dwEnd;
curPos += p.samples[j].num_samples + 46;
x++;
}
}
sfSample *h = sampleHeaders + x;
memset(h, 0, sizeof(sfSample));
strcpy(h->achSampleName, "EOS"); // write "End Of Samples" header
LIST_CHUNK *LstChk = List_GetChunk(s->Lists, FCC_sdta);
ITEM_CHUNK *ItmChk = Item_MakeChunk(FCC_smpl, totalSamplesSize * 2, samples, 0x00);
List_AddItem(LstChk, ItmChk);
LstChk = List_GetChunk(s->Lists, FCC_pdta);
ItmChk = Item_MakeChunk(FCC_shdr, (totalSamples + 1) * sizeof(sfSample), sampleHeaders, 0x00); // no free() needed either
List_AddItem(LstChk, ItmChk);
sfInst inst[2];
memset(inst, 0, sizeof(inst));
snprintf(inst[0].achInstName, sizeof(inst[0].achInstName), "Instrument %d", 1);
inst[0].wInstBagNdx = 0;
snprintf(inst[1].achInstName, sizeof(inst[1].achInstName), "EOI");
inst[1].wInstBagNdx = totalSamples;
ItmChk = Item_MakeChunk(FCC_inst, sizeof(inst), &inst, 0);
List_AddItem(LstChk, ItmChk);
int bagSize = (totalSamples + 1) * sizeof(sfInstBag);
sfInstBag *bags = (sfInstBag *)malloc(bagSize);
#define GENS_PER_ZONE 3
#define BASE_NOTE 36
for(int j = 0; j <= totalSamples; j++) {
bags[j].wInstGenNdx = j * GENS_PER_ZONE;
bags[j].wInstModNdx = 0;
}
ItmChk = Item_MakeChunk(FCC_ibag, bagSize, bags, 0);
List_AddItem(LstChk, ItmChk);
int genSize = ((totalSamples) * GENS_PER_ZONE + 1) * sizeof(sfInstGenList);
sfInstGenList *instGenLists = (sfInstGenList *)malloc(genSize);
memset(instGenLists, 0, genSize);
x = 0;
for(int j = 0; j < PDX_NUM_SAMPLES; j++) {
if(p.samples[j].num_samples > 0) {
int k = x * GENS_PER_ZONE;
instGenLists[k].sfGenOper = keyRange;
instGenLists[k].genAmount.ranges.byHi = BASE_NOTE + j;
instGenLists[k].genAmount.ranges.byLo = BASE_NOTE + j;
k++;
instGenLists[k].sfGenOper = overridingRootKey;
instGenLists[k].genAmount.wAmount = BASE_NOTE + j;
k++;
instGenLists[k].sfGenOper = sampleID;
instGenLists[k].genAmount.wAmount = x;
x++;
}
}
ItmChk = Item_MakeChunk(FCC_igen, genSize, instGenLists, 0);
List_AddItem(LstChk, ItmChk);
sfModList modList;
memset(&modList, 0, sizeof(modList));
ItmChk = Item_MakeChunk(FCC_imod, sizeof(modList), &modList, 0);
List_AddItem(LstChk, ItmChk);
sfPresetHeader presetHeaders[2];
snprintf(presetHeaders[0].achPresetName, sizeof(presetHeaders[0].achPresetName), "Preset 1");
presetHeaders[0].wPreset = 0;
presetHeaders[0].wBank = 0;
presetHeaders[0].wPresetBagNdx = 0;
presetHeaders[0].dwLibrary = 0;
presetHeaders[0].dwGenre = 0;
presetHeaders[0].dwMorphology = 0;
memset(presetHeaders + 1, 0, sizeof(sfPresetHeader));
snprintf(presetHeaders[1].achPresetName, sizeof(presetHeaders[1].achPresetName), "EOP");
presetHeaders[i].wPreset = 0xff;
presetHeaders[i].wBank = 0xff;
presetHeaders[i].wPresetBagNdx = 1;
ItmChk = Item_MakeChunk(FCC_phdr, sizeof(presetHeaders), presetHeaders, 0);
List_AddItem(LstChk, ItmChk);
sfPresetBag presetBags[2];
presetBags[0].wGenNdx = 0;
presetBags[0].wModNdx = 0;
presetBags[1].wGenNdx = 1;
presetBags[1].wModNdx = 0;
ItmChk = Item_MakeChunk(FCC_pbag, sizeof(presetBags), presetBags, 0);
List_AddItem(LstChk, ItmChk);
sfGenList presetGenOpers[2];
presetGenOpers[0].sfGenOper = instrument;
presetGenOpers[0].genAmount.wAmount = 0;
presetGenOpers[1].sfGenOper = 0;
presetGenOpers[1].genAmount.wAmount = 0;
ItmChk = Item_MakeChunk(FCC_pgen, sizeof(presetGenOpers), presetGenOpers, 0);
List_AddItem(LstChk, ItmChk);
sfModList presetModList;
memset(&presetModList, 0, sizeof(sfModList));
ItmChk = Item_MakeChunk(FCC_pmod, sizeof(presetModList), &presetModList, 0);
List_AddItem(LstChk, ItmChk);
char buf[256];
replace_ext(buf, sizeof(buf), argv[i], "sf2");
printf("Writing to file %s\n", buf);
WriteSF2toFile(s, buf);
free(samples);
free(sampleHeaders);
free(bags);
free(instGenLists);
}
}