1 /* FluidSynth - A Software Synthesizer
2  *
3  * Copyright (C) 2003  Peter Hanappe and others.
4  *
5  * SoundFont loading code borrowed from Smurf SoundFont Editor by Josh Green
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public License
9  * as published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA
21  */
22 
23 
24 #ifndef _FLUID_DEFSFONT_H
25 #define _FLUID_DEFSFONT_H
26 
27 
28 #include "fluidsynth.h"
29 #include "fluidsynth_priv.h"
30 #include "fluid_list.h"
31 
32 
33 
34 /********************************************************************************/
35 /********************************************************************************/
36 /********************************************************************************/
37 /********************************************************************************/
38 /********************************************************************************/
39 
40 /*-----------------------------------sfont.h----------------------------*/
41 
42 #define SF_SAMPMODES_LOOP	1
43 #define SF_SAMPMODES_UNROLL	2
44 
45 #define SF_MIN_SAMPLERATE	400
46 #define SF_MAX_SAMPLERATE	50000
47 
48 #define SF_MIN_SAMPLE_LENGTH	32
49 
50 /* Sound Font structure defines */
51 
52 typedef struct _SFVersion
53 {				/* version structure */
54   unsigned short major;
55   unsigned short minor;
56 }
57 SFVersion;
58 
59 typedef struct _SFMod
60 {				/* Modulator structure */
61   unsigned short src;			/* source modulator */
62   unsigned short dest;			/* destination generator */
63   signed short amount;		/* signed, degree of modulation */
64   unsigned short amtsrc;		/* second source controls amnt of first */
65   unsigned short trans;		/* transform applied to source */
66 }
67 SFMod;
68 
69 typedef union _SFGenAmount
70 {				/* Generator amount structure */
71   signed short sword;			/* signed 16 bit value */
72   unsigned short uword;		/* unsigned 16 bit value */
73   struct
74   {
75     unsigned char lo;			/* low value for ranges */
76     unsigned char hi;			/* high value for ranges */
77   }
78   range;
79 }
80 SFGenAmount;
81 
82 typedef struct _SFGen
83 {				/* Generator structure */
84   unsigned short id;			/* generator ID */
85   SFGenAmount amount;		/* generator value */
86 }
87 SFGen;
88 
89 typedef struct _SFZone
90 {				/* Sample/instrument zone structure */
91   fluid_list_t *instsamp;		/* instrument/sample pointer for zone */
92   fluid_list_t *gen;			/* list of generators */
93   fluid_list_t *mod;			/* list of modulators */
94 }
95 SFZone;
96 
97 typedef struct _SFSample
98 {				/* Sample structure */
99   char name[21];		/* Name of sample */
100   unsigned char samfile;		/* Loaded sfont/sample buffer = 0/1 */
101   unsigned int start;		/* Offset in sample area to start of sample */
102   unsigned int end;			/* Offset from start to end of sample,
103 				   this is the last point of the
104 				   sample, the SF spec has this as the
105 				   1st point after, corrected on
106 				   load/save */
107   unsigned int loopstart;		/* Offset from start to start of loop */
108   unsigned int loopend;		/* Offset from start to end of loop,
109 				   marks the first point after loop,
110 				   whose sample value is ideally
111 				   equivalent to loopstart */
112   unsigned int samplerate;		/* Sample rate recorded at */
113   unsigned char origpitch;		/* root midi key number */
114   signed char pitchadj;		/* pitch correction in cents */
115   unsigned short sampletype;		/* 1 mono,2 right,4 left,linked 8,0x8000=ROM */
116   fluid_sample_t *fluid_sample;	/* Imported sample (fixed up in fluid_defsfont_load) */
117 }
118 SFSample;
119 
120 typedef struct _SFInst
121 {				/* Instrument structure */
122   char name[21];		/* Name of instrument */
123   fluid_list_t *zone;			/* list of instrument zones */
124 }
125 SFInst;
126 
127 typedef struct _SFPreset
128 {				/* Preset structure */
129   char name[21];		/* preset name */
130   unsigned short prenum;		/* preset number */
131   unsigned short bank;			/* bank number */
132   unsigned int libr;			/* Not used (preserved) */
133   unsigned int genre;		/* Not used (preserved) */
134   unsigned int morph;		/* Not used (preserved) */
135   fluid_list_t *zone;			/* list of preset zones */
136 }
137 SFPreset;
138 
139 /* NOTE: sffd is also used to determine if sound font is new (NULL) */
140 typedef struct _SFData
141 {				/* Sound font data structure */
142   SFVersion version;		/* sound font version */
143   SFVersion romver;		/* ROM version */
144   unsigned int samplepos;		/* position within sffd of the sample chunk */
145   unsigned int samplesize;		/* length within sffd of the sample chunk */
146   char *fname;			/* file name */
147   FILE *sffd;			/* loaded sfont file descriptor */
148   fluid_list_t *info;		     /* linked list of info strings (1st byte is ID) */
149   fluid_list_t *preset;		/* linked list of preset info */
150   fluid_list_t *inst;			/* linked list of instrument info */
151   fluid_list_t *sample;		/* linked list of sample info */
152 }
153 SFData;
154 
155 /* sf file chunk IDs */
156 enum
157 { UNKN_ID, RIFF_ID, LIST_ID, SFBK_ID,
158   INFO_ID, SDTA_ID, PDTA_ID,	/* info/sample/preset */
159 
160   IFIL_ID, ISNG_ID, INAM_ID, IROM_ID, /* info ids (1st byte of info strings) */
161   IVER_ID, ICRD_ID, IENG_ID, IPRD_ID,	/* more info ids */
162   ICOP_ID, ICMT_ID, ISFT_ID,	/* and yet more info ids */
163 
164   SNAM_ID, SMPL_ID,		/* sample ids */
165   PHDR_ID, PBAG_ID, PMOD_ID, PGEN_ID,	/* preset ids */
166   IHDR_ID, IBAG_ID, IMOD_ID, IGEN_ID,	/* instrument ids */
167   SHDR_ID			/* sample info */
168 };
169 
170 /* generator types */
171 typedef enum
172 { Gen_StartAddrOfs, Gen_EndAddrOfs, Gen_StartLoopAddrOfs,
173   Gen_EndLoopAddrOfs, Gen_StartAddrCoarseOfs, Gen_ModLFO2Pitch,
174   Gen_VibLFO2Pitch, Gen_ModEnv2Pitch, Gen_FilterFc, Gen_FilterQ,
175   Gen_ModLFO2FilterFc, Gen_ModEnv2FilterFc, Gen_EndAddrCoarseOfs,
176   Gen_ModLFO2Vol, Gen_Unused1, Gen_ChorusSend, Gen_ReverbSend, Gen_Pan,
177   Gen_Unused2, Gen_Unused3, Gen_Unused4,
178   Gen_ModLFODelay, Gen_ModLFOFreq, Gen_VibLFODelay, Gen_VibLFOFreq,
179   Gen_ModEnvDelay, Gen_ModEnvAttack, Gen_ModEnvHold, Gen_ModEnvDecay,
180   Gen_ModEnvSustain, Gen_ModEnvRelease, Gen_Key2ModEnvHold,
181   Gen_Key2ModEnvDecay, Gen_VolEnvDelay, Gen_VolEnvAttack,
182   Gen_VolEnvHold, Gen_VolEnvDecay, Gen_VolEnvSustain, Gen_VolEnvRelease,
183   Gen_Key2VolEnvHold, Gen_Key2VolEnvDecay, Gen_Instrument,
184   Gen_Reserved1, Gen_KeyRange, Gen_VelRange,
185   Gen_StartLoopAddrCoarseOfs, Gen_Keynum, Gen_Velocity,
186   Gen_Attenuation, Gen_Reserved2, Gen_EndLoopAddrCoarseOfs,
187   Gen_CoarseTune, Gen_FineTune, Gen_SampleId, Gen_SampleModes,
188   Gen_Reserved3, Gen_ScaleTune, Gen_ExclusiveClass, Gen_OverrideRootKey,
189   Gen_Dummy
190 }
191 Gen_Type;
192 
193 #define Gen_MaxValid 	Gen_Dummy - 1	/* maximum valid generator */
194 #define Gen_Count	Gen_Dummy	/* count of generators */
195 #define GenArrSize sizeof(SFGenAmount)*Gen_Count	/* gen array size */
196 
197 /* generator unit type */
198 typedef enum
199 {
200   None,				/* No unit type */
201   Unit_Smpls,			/* in samples */
202   Unit_32kSmpls,		/* in 32k samples */
203   Unit_Cent,			/* in cents (1/100th of a semitone) */
204   Unit_HzCent,			/* in Hz Cents */
205   Unit_TCent,			/* in Time Cents */
206   Unit_cB,			/* in centibels (1/100th of a decibel) */
207   Unit_Percent,			/* in percentage */
208   Unit_Semitone,		/* in semitones */
209   Unit_Range			/* a range of values */
210 }
211 Gen_Unit;
212 
213 /* global data */
214 
215 extern unsigned short badgen[]; 	/* list of bad generators */
216 extern unsigned short badpgen[]; 	/* list of bad preset generators */
217 
218 /* functions */
219 void sfont_init_chunks (void);
220 
221 void sfont_close (SFData * sf);
222 void sfont_free_zone (SFZone * zone);
223 int sfont_preset_compare_func (void* a, void* b);
224 
225 void sfont_zone_delete (SFData * sf, fluid_list_t ** zlist, SFZone * zone);
226 
227 fluid_list_t *gen_inlist (int gen, fluid_list_t * genlist);
228 int gen_valid (int gen);
229 int gen_validp (int gen);
230 
231 
232 /*-----------------------------------sffile.h----------------------------*/
233 /*
234    File structures and routines (used to be in sffile.h)
235 */
236 
237 #define CHNKIDSTR(id)           &idlist[(id - 1) * 4]
238 
239 /* sfont file chunk sizes */
240 #define SFPHDRSIZE	38
241 #define SFBAGSIZE	4
242 #define SFMODSIZE	10
243 #define SFGENSIZE	4
244 #define SFIHDRSIZE	22
245 #define SFSHDRSIZE	46
246 
247 /* sfont file data structures */
248 typedef struct _SFChunk
249 {				/* RIFF file chunk structure */
250   unsigned int id;			/* chunk id */
251   unsigned int size;			/* size of the following chunk */
252 }
253 SFChunk;
254 
255 typedef struct _SFPhdr
256 {
257   unsigned char name[20];		/* preset name */
258   unsigned short preset;		/* preset number */
259   unsigned short bank;			/* bank number */
260   unsigned short pbagndx;		/* index into preset bag */
261   unsigned int library;		/* just for preserving them */
262   unsigned int genre;		/* Not used */
263   unsigned int morphology;		/* Not used */
264 }
265 SFPhdr;
266 
267 typedef struct _SFBag
268 {
269   unsigned short genndx;		/* index into generator list */
270   unsigned short modndx;		/* index into modulator list */
271 }
272 SFBag;
273 
274 typedef struct _SFIhdr
275 {
276   char name[20];		/* Name of instrument */
277   unsigned short ibagndx;		/* Instrument bag index */
278 }
279 SFIhdr;
280 
281 typedef struct _SFShdr
282 {				/* Sample header loading struct */
283   char name[20];		/* Sample name */
284   unsigned int start;		/* Offset to start of sample */
285   unsigned int end;			/* Offset to end of sample */
286   unsigned int loopstart;		/* Offset to start of loop */
287   unsigned int loopend;		/* Offset to end of loop */
288   unsigned int samplerate;		/* Sample rate recorded at */
289   unsigned char origpitch;		/* root midi key number */
290   signed char pitchadj;		/* pitch correction in cents */
291   unsigned short samplelink;		/* Not used */
292   unsigned short sampletype;		/* 1 mono,2 right,4 left,linked 8,0x8000=ROM */
293 }
294 SFShdr;
295 
296 /* data */
297 extern char idlist[];
298 
299 /* functions */
300 SFData *sfload_file (const char * fname);
301 
302 
303 
304 /********************************************************************************/
305 /********************************************************************************/
306 /********************************************************************************/
307 /********************************************************************************/
308 /********************************************************************************/
309 
310 /* GLIB - Library of useful routines for C programming
311  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
312  *
313  * This library is free software; you can redistribute it and/or
314  * modify it under the terms of the GNU Library General Public
315  * License as published by the Free Software Foundation; either
316  * version 2 of the License, or (at your option) any later version.
317  *
318  * This library is distributed in the hope that it will be useful,
319  * but WITHOUT ANY WARRANTY; without even the implied warranty of
320  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
321  * Library General Public License for more details.
322  *
323  * You should have received a copy of the GNU Library General Public
324  * License along with this library; if not, write to the
325  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
326  * Boston, MA 02110-1301, USA.
327  */
328 
329 #include <glib.h>
330 
331 
332 /*-----------------------------------util.h----------------------------*/
333 /*
334   Utility functions (formerly in util.h)
335  */
336 #define FAIL	0
337 #define OK	1
338 
339 enum
340 { ErrWarn, ErrFatal, ErrStatus, ErrCorr, ErrEof, ErrMem, Errno,
341   ErrRead, ErrWrite
342 };
343 
344 #define ErrMax		ErrWrite
345 #define ErrnoStart	Errno
346 #define ErrnoEnd	ErrWrite
347 
348 int gerr (int ev, char * fmt, ...);
349 int safe_fread (void *buf, int count, FILE * fd);
350 int safe_fwrite (void *buf, int count, FILE * fd);
351 int safe_fseek (FILE * fd, long ofs, int whence);
352 
353 
354 /********************************************************************************/
355 /********************************************************************************/
356 /********************************************************************************/
357 /********************************************************************************/
358 /********************************************************************************/
359 
360 
361 
362 /***************************************************************
363  *
364  *       FORWARD DECLARATIONS
365  */
366 typedef struct _fluid_defsfont_t fluid_defsfont_t;
367 typedef struct _fluid_defpreset_t fluid_defpreset_t;
368 typedef struct _fluid_preset_zone_t fluid_preset_zone_t;
369 typedef struct _fluid_inst_t fluid_inst_t;
370 typedef struct _fluid_inst_zone_t fluid_inst_zone_t;
371 
372 /*
373 
374   Public interface
375 
376  */
377 
378 fluid_sfloader_t* new_fluid_defsfloader(void);
379 int delete_fluid_defsfloader(fluid_sfloader_t* loader);
380 fluid_sfont_t* fluid_defsfloader_load(fluid_sfloader_t* loader, const char* filename);
381 
382 
383 int fluid_defsfont_sfont_delete(fluid_sfont_t* sfont);
384 char* fluid_defsfont_sfont_get_name(fluid_sfont_t* sfont);
385 fluid_preset_t* fluid_defsfont_sfont_get_preset(fluid_sfont_t* sfont, unsigned int bank, unsigned int prenum);
386 void fluid_defsfont_sfont_iteration_start(fluid_sfont_t* sfont);
387 int fluid_defsfont_sfont_iteration_next(fluid_sfont_t* sfont, fluid_preset_t* preset);
388 
389 
390 int fluid_defpreset_preset_delete(fluid_preset_t* preset);
391 char* fluid_defpreset_preset_get_name(fluid_preset_t* preset);
392 int fluid_defpreset_preset_get_banknum(fluid_preset_t* preset);
393 int fluid_defpreset_preset_get_num(fluid_preset_t* preset);
394 int fluid_defpreset_preset_noteon(fluid_preset_t* preset, fluid_synth_t* synth, int chan, int key, int vel);
395 
396 
397 /*
398  * fluid_defsfont_t
399  */
400 struct _fluid_defsfont_t
401 {
402   char* filename;           /* the filename of this soundfont */
403   unsigned int samplepos;   /* the position in the file at which the sample data starts */
404   unsigned int samplesize;  /* the size of the sample data */
405   short* sampledata;        /* the sample data, loaded in ram */
406   fluid_list_t* sample;      /* the samples in this soundfont */
407   fluid_defpreset_t* preset; /* the presets of this soundfont */
408 
409   fluid_preset_t iter_preset;        /* preset interface used in the iteration */
410   fluid_defpreset_t* iter_cur;       /* the current preset in the iteration */
411 };
412 
413 
414 fluid_defsfont_t* new_fluid_defsfont(void);
415 int delete_fluid_defsfont(fluid_defsfont_t* sfont);
416 int fluid_defsfont_load(fluid_defsfont_t* sfont, const char* file);
417 char* fluid_defsfont_get_name(fluid_defsfont_t* sfont);
418 fluid_defpreset_t* fluid_defsfont_get_preset(fluid_defsfont_t* sfont, unsigned int bank, unsigned int prenum);
419 void fluid_defsfont_iteration_start(fluid_defsfont_t* sfont);
420 int fluid_defsfont_iteration_next(fluid_defsfont_t* sfont, fluid_preset_t* preset);
421 int fluid_defsfont_load_sampledata(fluid_defsfont_t* sfont);
422 int fluid_defsfont_add_sample(fluid_defsfont_t* sfont, fluid_sample_t* sample);
423 int fluid_defsfont_add_preset(fluid_defsfont_t* sfont, fluid_defpreset_t* preset);
424 
425 
426 /*
427  * fluid_preset_t
428  */
429 struct _fluid_defpreset_t
430 {
431   fluid_defpreset_t* next;
432   fluid_defsfont_t* sfont;                  /* the soundfont this preset belongs to */
433   char name[21];                        /* the name of the preset */
434   unsigned int bank;                    /* the bank number */
435   unsigned int num;                     /* the preset number */
436   fluid_preset_zone_t* global_zone;        /* the global zone of the preset */
437   fluid_preset_zone_t* zone;               /* the chained list of preset zones */
438 };
439 
440 fluid_defpreset_t* new_fluid_defpreset(fluid_defsfont_t* sfont);
441 int delete_fluid_defpreset(fluid_defpreset_t* preset);
442 fluid_defpreset_t* fluid_defpreset_next(fluid_defpreset_t* preset);
443 int fluid_defpreset_import_sfont(fluid_defpreset_t* preset, SFPreset* sfpreset, fluid_defsfont_t* sfont);
444 int fluid_defpreset_set_global_zone(fluid_defpreset_t* preset, fluid_preset_zone_t* zone);
445 int fluid_defpreset_add_zone(fluid_defpreset_t* preset, fluid_preset_zone_t* zone);
446 fluid_preset_zone_t* fluid_defpreset_get_zone(fluid_defpreset_t* preset);
447 fluid_preset_zone_t* fluid_defpreset_get_global_zone(fluid_defpreset_t* preset);
448 int fluid_defpreset_get_banknum(fluid_defpreset_t* preset);
449 int fluid_defpreset_get_num(fluid_defpreset_t* preset);
450 char* fluid_defpreset_get_name(fluid_defpreset_t* preset);
451 int fluid_defpreset_noteon(fluid_defpreset_t* preset, fluid_synth_t* synth, int chan, int key, int vel);
452 
453 /*
454  * fluid_preset_zone
455  */
456 struct _fluid_preset_zone_t
457 {
458   fluid_preset_zone_t* next;
459   char* name;
460   fluid_inst_t* inst;
461   int keylo;
462   int keyhi;
463   int vello;
464   int velhi;
465   fluid_gen_t gen[GEN_LAST];
466   fluid_mod_t * mod; /* List of modulators */
467 };
468 
469 fluid_preset_zone_t* new_fluid_preset_zone(char* name);
470 int delete_fluid_preset_zone(fluid_preset_zone_t* zone);
471 fluid_preset_zone_t* fluid_preset_zone_next(fluid_preset_zone_t* preset);
472 int fluid_preset_zone_import_sfont(fluid_preset_zone_t* zone, SFZone* sfzone, fluid_defsfont_t* sfont);
473 int fluid_preset_zone_inside_range(fluid_preset_zone_t* zone, int key, int vel);
474 fluid_inst_t* fluid_preset_zone_get_inst(fluid_preset_zone_t* zone);
475 
476 /*
477  * fluid_inst_t
478  */
479 struct _fluid_inst_t
480 {
481   char name[21];
482   fluid_inst_zone_t* global_zone;
483   fluid_inst_zone_t* zone;
484 };
485 
486 fluid_inst_t* new_fluid_inst(void);
487 int delete_fluid_inst(fluid_inst_t* inst);
488 int fluid_inst_import_sfont(fluid_inst_t* inst, SFInst *sfinst, fluid_defsfont_t* sfont);
489 int fluid_inst_set_global_zone(fluid_inst_t* inst, fluid_inst_zone_t* zone);
490 int fluid_inst_add_zone(fluid_inst_t* inst, fluid_inst_zone_t* zone);
491 fluid_inst_zone_t* fluid_inst_get_zone(fluid_inst_t* inst);
492 fluid_inst_zone_t* fluid_inst_get_global_zone(fluid_inst_t* inst);
493 
494 /*
495  * fluid_inst_zone_t
496  */
497 struct _fluid_inst_zone_t
498 {
499   fluid_inst_zone_t* next;
500   char* name;
501   fluid_sample_t* sample;
502   int keylo;
503   int keyhi;
504   int vello;
505   int velhi;
506   fluid_gen_t gen[GEN_LAST];
507   fluid_mod_t * mod; /* List of modulators */
508 };
509 
510 fluid_inst_zone_t* new_fluid_inst_zone(char* name);
511 int delete_fluid_inst_zone(fluid_inst_zone_t* zone);
512 fluid_inst_zone_t* fluid_inst_zone_next(fluid_inst_zone_t* zone);
513 int fluid_inst_zone_import_sfont(fluid_inst_zone_t* zone, SFZone *sfzone, fluid_defsfont_t* sfont);
514 int fluid_inst_zone_inside_range(fluid_inst_zone_t* zone, int key, int vel);
515 fluid_sample_t* fluid_inst_zone_get_sample(fluid_inst_zone_t* zone);
516 
517 
518 
519 fluid_sample_t* new_fluid_sample(void);
520 int delete_fluid_sample(fluid_sample_t* sample);
521 int fluid_sample_import_sfont(fluid_sample_t* sample, SFSample* sfsample, fluid_defsfont_t* sfont);
522 int fluid_sample_in_rom(fluid_sample_t* sample);
523 
524 
525 #endif  /* _FLUID_SFONT_H */
526