-
Notifications
You must be signed in to change notification settings - Fork 14
/
Soundfont.c
449 lines (362 loc) · 8.78 KB
/
Soundfont.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h> // for NULL
#include <time.h>
#include "stdtype.h"
#include "Soundfont.h"
static const FOURCC MAIN_CHUNK_ORDER[] =
{
FCC_INFO, FCC_sdta, FCC_pdta, 0
};
static const FOURCC PDTA_CHUNK_ORDER[] =
{
FCC_phdr, FCC_pbag, FCC_pmod, FCC_pgen,
FCC_inst, FCC_ibag, FCC_imod, FCC_igen,
FCC_shdr,
0
};
typedef struct _sort_item
{
UINT32 idx;
void* data;
} SORT_ITEM;
SF2_DATA* CreateSF2Base(const char* SoundfontName)
{
SF2_DATA* SF2Data;
LIST_CHUNK* LstInfo;
LIST_CHUNK* LstSample;
LIST_CHUNK* LstPreset;
ITEM_CHUNK* CurItm;
sfVersionTag SFVer;
char TempStr[0x40];
time_t CurTime;
struct tm* TimeInfo;
SF2Data = (SF2_DATA*)malloc(sizeof(SF2_DATA));
if (SF2Data == NULL)
return NULL;
// generate Header
SF2Data->fccRIFF = FCC_RIFF;
SF2Data->RiffSize = 0x00;
SF2Data->RiffType = FCC_sfbk;
// Now generate the 3 LIST chunks.
LstInfo = List_MakeChunk(FCC_INFO);
LstSample = List_MakeChunk(FCC_sdta);
LstPreset = List_MakeChunk(FCC_pdta);
// Link them correctly and set the pointers in SF2Data.
LstInfo->next = LstSample;
LstSample->next = LstPreset;
SF2Data->Lists = LstInfo;
SF2Data->LastLst = LstPreset;
// make the 'INFO' Items
// Version (ifil)
SFVer.wMajor = 2;
SFVer.wMinor = 0;
CurItm = Item_MakeChunk(FCC_ifil, sizeof(sfVersionTag), &SFVer, 0x01);
List_AddItem(LstInfo, CurItm);
// Sound Engine (isng)
CurItm = Item_MakeChunk_String(FCC_isng, "EMU8000", 0x01);
List_AddItem(LstInfo, CurItm);
// Soundfont Name (INAM)
CurItm = Item_MakeChunk_String(FCC_INAM, SoundfontName, 0x01);
List_AddItem(LstInfo, CurItm);
// Creation Date (ICRD)
time(&CurTime);
TimeInfo = localtime(&CurTime);
strftime(TempStr, 0x40, "%#d %b %Y", TimeInfo);
CurItm = Item_MakeChunk_String(FCC_ICRD, TempStr, 0x01);
List_AddItem(LstInfo, CurItm);
return SF2Data;
}
void FreeSF2Data(SF2_DATA* SF2Data)
{
LIST_CHUNK* CurLst;
LIST_CHUNK* LastLst;
CurLst = SF2Data->Lists;
while(CurLst != NULL)
{
List_FreeListItems(CurLst);
LastLst = CurLst;
CurLst = CurLst->next;
free(LastLst);
}
free(SF2Data);
return;
}
void CalculateSF2BlockSizes(SF2_DATA* SF2Data)
{
UINT32 RIFFSize;
LIST_CHUNK* CurLst;
RIFFSize = 0x04; // RIFF Type
CurLst = SF2Data->Lists;
while(CurLst != NULL)
{
List_CalculateSize(CurLst);
RIFFSize += 0x08 + CurLst->ckSize;
CurLst = CurLst->next;
}
SF2Data->RiffSize = RIFFSize;
return;
}
UINT8 WriteSF2toFile(SF2_DATA* SF2Data, const char* FileName)
{
FILE* hFile;
LIST_CHUNK* CurLst;
hFile = fopen(FileName, "wb");
if (hFile == NULL)
return 0xFF;
CalculateSF2BlockSizes(SF2Data);
// write RIFF header
fwrite(&SF2Data->fccRIFF, 0x04, 0x01, hFile);
fwrite(&SF2Data->RiffSize, 0x04, 0x01, hFile);
fwrite(&SF2Data->RiffType, 0x04, 0x01, hFile);
CurLst = SF2Data->Lists;
while(CurLst != NULL)
{
List_WriteToFile(CurLst, hFile);
CurLst = CurLst->next;
}
fclose(hFile);
return 0x00;
}
static int SortCompare(const void* a, const void* b)
{
const SORT_ITEM* sortA = (const SORT_ITEM*)a;
const SORT_ITEM* sortB = (const SORT_ITEM*)b;
return sortA->idx - sortB->idx;
}
static UINT8 SortItemChunks(ITEM_CHUNK** chkBegin, ITEM_CHUNK** chkEnd, const FOURCC* order)
{
UINT32 chkCount;
UINT32 fccCount;
ITEM_CHUNK* curChk;
ITEM_CHUNK* lastChk;
const FOURCC* curFCC;
SORT_ITEM* sortItems;
UINT32 curItm;
UINT32 unkChkIdx; // index for unknown chunks
UINT8 needSort;
for (curChk = *chkBegin, chkCount = 0; curChk != NULL; curChk = curChk->next, chkCount ++)
;
for (curFCC = order, fccCount = 0; *curFCC != 0; curFCC ++, fccCount ++)
;
sortItems = (SORT_ITEM*)calloc(chkCount, sizeof(SORT_ITEM));
unkChkIdx = fccCount; // default to "after all known chunks"
needSort = 0;
for (curChk = *chkBegin, curItm = 0; curChk != NULL; curChk = curChk->next, curItm ++)
{
for (curFCC = order; *curFCC != 0; curFCC ++)
{
if (*curFCC == curChk->ckID)
break;
}
sortItems[curItm].data = curChk;
if (*curFCC != 0)
{
// put all known chunks in the listed order
sortItems[curItm].idx = curFCC - order;
}
else
{
// keep the order to all unknown chunks intact, but move them to the end
sortItems[curItm].idx = unkChkIdx;
unkChkIdx ++;
}
if (sortItems[curItm].idx != curItm)
needSort = 1;
}
if (! needSort)
{
free(sortItems);
return 0x00;
}
qsort(sortItems, chkCount, sizeof(SORT_ITEM), &SortCompare);
lastChk = (ITEM_CHUNK*)sortItems[0].data;
for (curItm = 1; curItm < chkCount; curItm ++)
{
curChk = (ITEM_CHUNK*)sortItems[curItm].data;
lastChk->next = curChk;
lastChk = curChk;
}
lastChk->next = NULL;
*chkEnd = lastChk;
*chkBegin = (ITEM_CHUNK*)sortItems[0].data;
free(sortItems);
return 0x01;
}
UINT8 SortSF2Chunks(SF2_DATA* SF2Data)
{
LIST_CHUNK* LstPreset;
UINT8 retVal = 0x00;
//retVal |= SortListChunks(&SF2Data->Lists, &SF2Data->LastLst, MAIN_CHUNK_ORDER);
LstPreset = List_GetChunk(SF2Data->Lists, FCC_pdta);
if (LstPreset != NULL)
retVal |= SortItemChunks(&LstPreset->Items, &LstPreset->LastItem, PDTA_CHUNK_ORDER);
return retVal; // 0 - no chunks reordered, 1 - some chunks were reordered
}
// --- List Chunk Handling ---
LIST_CHUNK* List_MakeChunk(const FOURCC fccID)
{
LIST_CHUNK* ListChk;
ListChk = (LIST_CHUNK*)malloc(sizeof(LIST_CHUNK));
if (ListChk == NULL)
return NULL;
ListChk->ckID = FCC_LIST;
ListChk->ckSize = 0x00;
ListChk->ckType = fccID;
ListChk->Items = NULL;
ListChk->LastItem = ListChk->Items;
ListChk->next = NULL;
return ListChk;
}
LIST_CHUNK* List_GetChunk(const LIST_CHUNK* FirstChk, const FOURCC fccID)
{
const LIST_CHUNK* CurChk;
CurChk = FirstChk;
while(CurChk != NULL)
{
if (CurChk->ckType == fccID)
return (LIST_CHUNK*)CurChk;
CurChk = CurChk->next;
}
return NULL;
}
void List_AddItem(LIST_CHUNK* Chunk, ITEM_CHUNK* Item)
{
if (Chunk->Items == NULL)
{
Chunk->Items = Item;
Chunk->LastItem = Item;
}
else
{
if (Chunk->LastItem == NULL) // this should really not happen
{
ITEM_CHUNK* CurChk;
CurChk = Chunk->Items;
while(CurChk->next != NULL)
CurChk = CurChk->next;
Chunk->LastItem = CurChk;
}
Chunk->LastItem->next = Item;
Chunk->LastItem = Item;
}
return;
}
void List_FreeListItems(LIST_CHUNK* Chunk)
{
ITEM_CHUNK* CurItm;
ITEM_CHUNK* LastItm;
CurItm = Chunk->Items;
while(CurItm != NULL)
{
Item_FreeItemData(CurItm);
LastItm = CurItm;
CurItm = CurItm->next;
free(LastItm);
}
Chunk->Items = NULL;
Chunk->LastItem = NULL;
return;
}
void List_CalculateSize(LIST_CHUNK* Chunk)
{
ITEM_CHUNK* CurItm;
UINT32 LstSize;
CurItm = Chunk->Items;
LstSize = 0x04; // List Type
while(CurItm != NULL)
{
LstSize += 0x08 + CurItm->ckSize;
CurItm = CurItm->next;
}
Chunk->ckSize = LstSize;
return;
}
void List_WriteToFile(LIST_CHUNK* Chunk, FILE* hFile)
{
ITEM_CHUNK* CurItm;
fwrite(&Chunk->ckID, 0x04, 0x01, hFile);
fwrite(&Chunk->ckSize, 0x04, 0x01, hFile);
fwrite(&Chunk->ckType, 0x04, 0x01, hFile);
CurItm = Chunk->Items;
while(CurItm != NULL)
{
Item_WriteToFile(CurItm, hFile);
CurItm = CurItm->next;
}
return;
}
// --- Item Chunk Handling ---
ITEM_CHUNK* Item_MakeChunk(const FOURCC fccID, DWORD DataSize, void* Data, UINT8 CopyData)
{
ITEM_CHUNK* ItemChk;
ItemChk = (ITEM_CHUNK*)malloc(sizeof(ITEM_CHUNK));
if (ItemChk == NULL)
return NULL;
ItemChk->ckID = fccID;
ItemChk->ckSize = DataSize;
if (! CopyData)
{
// the Data was already allocated with malloc
ItemChk->ckData = Data;
}
else
{
// We need to make copies of static structures (which are sometimes nicer to work with).
ItemChk->ckData = malloc(DataSize);
memcpy(ItemChk->ckData, Data, DataSize);
}
ItemChk->next = NULL;
return ItemChk;
}
ITEM_CHUNK* Item_MakeChunk_String(const FOURCC fccID, const char* Data, UINT8 CopyData)
{
ITEM_CHUNK* ItemChk;
UINT32 StrSize;
ItemChk = (ITEM_CHUNK*)malloc(sizeof(ITEM_CHUNK));
if (ItemChk == NULL)
return NULL;
StrSize = strlen(Data) + 1;
if (StrSize & 1)
StrSize ++; // pad to even size
ItemChk->ckID = fccID;
ItemChk->ckSize = StrSize;
if (! CopyData)
{
ItemChk->ckData = (void*)Data;
}
else
{
ItemChk->ckData = malloc(StrSize);
strncpy(ItemChk->ckData, Data, StrSize); // strncpy includes padding :)
}
ItemChk->next = NULL;
return ItemChk;
}
ITEM_CHUNK* Item_GetChunk(const ITEM_CHUNK* FirstChk, const FOURCC fccID)
{
const ITEM_CHUNK* CurChk;
CurChk = FirstChk;
while(CurChk != NULL)
{
if (CurChk->ckID == fccID)
return (ITEM_CHUNK*)CurChk;
CurChk = CurChk->next;
}
return NULL;
}
void Item_FreeItemData(ITEM_CHUNK* Chunk)
{
free(Chunk->ckData);
Chunk->ckData = NULL;
Chunk->ckSize = 0x00;
return;
}
void Item_WriteToFile(ITEM_CHUNK* Chunk, FILE* hFile)
{
fwrite(&Chunk->ckID, 0x04, 0x01, hFile);
fwrite(&Chunk->ckSize, 0x04, 0x01, hFile);
fwrite(Chunk->ckData, 0x01, Chunk->ckSize, hFile);
return;
}