1 /* FluidSynth - A Software Synthesizer
3 * Copyright (C) 2003 Peter Hanappe and others.
5 * SoundFont loading code borrowed from Smurf SoundFont Editor by Josh Green
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.
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.
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
24 #ifndef _FLUID_DEFSFONT_H
25 #define _FLUID_DEFSFONT_H
28 #include "fluidsynth.h"
29 #include "fluidsynth_priv.h"
30 #include "fluid_list.h"
34 /********************************************************************************/
35 /********************************************************************************/
36 /********************************************************************************/
37 /********************************************************************************/
38 /********************************************************************************/
40 /*-----------------------------------sfont.h----------------------------*/
42 #define SF_SAMPMODES_LOOP 1
43 #define SF_SAMPMODES_UNROLL 2
45 #define SF_MIN_SAMPLERATE 400
46 #define SF_MAX_SAMPLERATE 50000
48 #define SF_MIN_SAMPLE_LENGTH 32
50 /* Sound Font structure defines */
52 typedef struct _SFVersion
53 { /* version structure */
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 */
69 typedef union _SFGenAmount
70 { /* Generator amount structure */
71 signed short sword; /* signed 16 bit value */
72 unsigned short uword; /* unsigned 16 bit value */
75 unsigned char lo; /* low value for ranges */
76 unsigned char hi; /* high value for ranges */
83 { /* Generator structure */
84 unsigned short id; /* generator ID */
85 SFGenAmount amount; /* generator value */
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 */
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
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) */
120 typedef struct _SFInst
121 { /* Instrument structure */
122 char name[21]; /* Name of instrument */
123 fluid_list_t *zone; /* list of instrument zones */
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 */
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 */
155 /* sf file chunk IDs */
157 { UNKN_ID, RIFF_ID, LIST_ID, SFBK_ID,
158 INFO_ID, SDTA_ID, PDTA_ID, /* info/sample/preset */
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 */
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 */
170 /* generator types */
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,
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 */
197 /* generator unit type */
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 */
215 extern unsigned short badgen[]; /* list of bad generators */
216 extern unsigned short badpgen[]; /* list of bad preset generators */
219 void sfont_init_chunks (void);
221 void sfont_close (SFData * sf);
222 void sfont_free_zone (SFZone * zone);
223 int sfont_preset_compare_func (void* a, void* b);
225 void sfont_zone_delete (SFData * sf, fluid_list_t ** zlist, SFZone * zone);
227 fluid_list_t *gen_inlist (int gen, fluid_list_t * genlist);
228 int gen_valid (int gen);
229 int gen_validp (int gen);
232 /*-----------------------------------sffile.h----------------------------*/
234 File structures and routines (used to be in sffile.h)
237 #define CHNKIDSTR(id) &idlist[(id - 1) * 4]
239 /* sfont file chunk sizes */
240 #define SFPHDRSIZE 38
244 #define SFIHDRSIZE 22
245 #define SFSHDRSIZE 46
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 */
255 typedef struct _SFPhdr
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 */
267 typedef struct _SFBag
269 unsigned short genndx; /* index into generator list */
270 unsigned short modndx; /* index into modulator list */
274 typedef struct _SFIhdr
276 char name[20]; /* Name of instrument */
277 unsigned short ibagndx; /* Instrument bag index */
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 */
297 extern char idlist[];
300 SFData *sfload_file (const char * fname);
304 /********************************************************************************/
305 /********************************************************************************/
306 /********************************************************************************/
307 /********************************************************************************/
308 /********************************************************************************/
310 /* GLIB - Library of useful routines for C programming
311 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
332 /*-----------------------------------util.h----------------------------*/
334 Utility functions (formerly in util.h)
340 { ErrWarn, ErrFatal, ErrStatus, ErrCorr, ErrEof, ErrMem, Errno,
344 #define ErrMax ErrWrite
345 #define ErrnoStart Errno
346 #define ErrnoEnd ErrWrite
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);
354 /********************************************************************************/
355 /********************************************************************************/
356 /********************************************************************************/
357 /********************************************************************************/
358 /********************************************************************************/
362 /***************************************************************
364 * FORWARD DECLARATIONS
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;
378 fluid_sfloader_t* new_fluid_defsfloader(fluid_settings_t* settings);
379 int delete_fluid_defsfloader(fluid_sfloader_t* loader);
380 fluid_sfont_t* fluid_defsfloader_load(fluid_sfloader_t* loader, const char* filename);
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);
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);
400 struct _fluid_defsfont_t
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 int mlock; /* Should we try memlock (avoid swapping)? */
410 fluid_preset_t iter_preset; /* preset interface used in the iteration */
411 fluid_defpreset_t* iter_cur; /* the current preset in the iteration */
413 fluid_preset_t** preset_stack; /* List of presets that are available to use */
414 int preset_stack_capacity; /* Length of preset_stack array */
415 int preset_stack_size; /* Current number of items in the stack */
419 fluid_defsfont_t* new_fluid_defsfont(fluid_settings_t* settings);
420 int delete_fluid_defsfont(fluid_defsfont_t* sfont);
421 int fluid_defsfont_load(fluid_defsfont_t* sfont, const char* file);
422 char* fluid_defsfont_get_name(fluid_defsfont_t* sfont);
423 fluid_defpreset_t* fluid_defsfont_get_preset(fluid_defsfont_t* sfont, unsigned int bank, unsigned int prenum);
424 void fluid_defsfont_iteration_start(fluid_defsfont_t* sfont);
425 int fluid_defsfont_iteration_next(fluid_defsfont_t* sfont, fluid_preset_t* preset);
426 int fluid_defsfont_load_sampledata(fluid_defsfont_t* sfont);
427 int fluid_defsfont_add_sample(fluid_defsfont_t* sfont, fluid_sample_t* sample);
428 int fluid_defsfont_add_preset(fluid_defsfont_t* sfont, fluid_defpreset_t* preset);
434 struct _fluid_defpreset_t
436 fluid_defpreset_t* next;
437 fluid_defsfont_t* sfont; /* the soundfont this preset belongs to */
438 char name[21]; /* the name of the preset */
439 unsigned int bank; /* the bank number */
440 unsigned int num; /* the preset number */
441 fluid_preset_zone_t* global_zone; /* the global zone of the preset */
442 fluid_preset_zone_t* zone; /* the chained list of preset zones */
445 fluid_defpreset_t* new_fluid_defpreset(fluid_defsfont_t* sfont);
446 int delete_fluid_defpreset(fluid_defpreset_t* preset);
447 fluid_defpreset_t* fluid_defpreset_next(fluid_defpreset_t* preset);
448 int fluid_defpreset_import_sfont(fluid_defpreset_t* preset, SFPreset* sfpreset, fluid_defsfont_t* sfont);
449 int fluid_defpreset_set_global_zone(fluid_defpreset_t* preset, fluid_preset_zone_t* zone);
450 int fluid_defpreset_add_zone(fluid_defpreset_t* preset, fluid_preset_zone_t* zone);
451 fluid_preset_zone_t* fluid_defpreset_get_zone(fluid_defpreset_t* preset);
452 fluid_preset_zone_t* fluid_defpreset_get_global_zone(fluid_defpreset_t* preset);
453 int fluid_defpreset_get_banknum(fluid_defpreset_t* preset);
454 int fluid_defpreset_get_num(fluid_defpreset_t* preset);
455 char* fluid_defpreset_get_name(fluid_defpreset_t* preset);
456 int fluid_defpreset_noteon(fluid_defpreset_t* preset, fluid_synth_t* synth, int chan, int key, int vel);
461 struct _fluid_preset_zone_t
463 fluid_preset_zone_t* next;
470 fluid_gen_t gen[GEN_LAST];
471 fluid_mod_t * mod; /* List of modulators */
474 fluid_preset_zone_t* new_fluid_preset_zone(char* name);
475 int delete_fluid_preset_zone(fluid_preset_zone_t* zone);
476 fluid_preset_zone_t* fluid_preset_zone_next(fluid_preset_zone_t* preset);
477 int fluid_preset_zone_import_sfont(fluid_preset_zone_t* zone, SFZone* sfzone, fluid_defsfont_t* sfont);
478 int fluid_preset_zone_inside_range(fluid_preset_zone_t* zone, int key, int vel);
479 fluid_inst_t* fluid_preset_zone_get_inst(fluid_preset_zone_t* zone);
487 fluid_inst_zone_t* global_zone;
488 fluid_inst_zone_t* zone;
491 fluid_inst_t* new_fluid_inst(void);
492 int delete_fluid_inst(fluid_inst_t* inst);
493 int fluid_inst_import_sfont(fluid_inst_t* inst, SFInst *sfinst, fluid_defsfont_t* sfont);
494 int fluid_inst_set_global_zone(fluid_inst_t* inst, fluid_inst_zone_t* zone);
495 int fluid_inst_add_zone(fluid_inst_t* inst, fluid_inst_zone_t* zone);
496 fluid_inst_zone_t* fluid_inst_get_zone(fluid_inst_t* inst);
497 fluid_inst_zone_t* fluid_inst_get_global_zone(fluid_inst_t* inst);
502 struct _fluid_inst_zone_t
504 fluid_inst_zone_t* next;
506 fluid_sample_t* sample;
511 fluid_gen_t gen[GEN_LAST];
512 fluid_mod_t * mod; /* List of modulators */
515 fluid_inst_zone_t* new_fluid_inst_zone(char* name);
516 int delete_fluid_inst_zone(fluid_inst_zone_t* zone);
517 fluid_inst_zone_t* fluid_inst_zone_next(fluid_inst_zone_t* zone);
518 int fluid_inst_zone_import_sfont(fluid_inst_zone_t* zone, SFZone *sfzone, fluid_defsfont_t* sfont);
519 int fluid_inst_zone_inside_range(fluid_inst_zone_t* zone, int key, int vel);
520 fluid_sample_t* fluid_inst_zone_get_sample(fluid_inst_zone_t* zone);
524 fluid_sample_t* new_fluid_sample(void);
525 int delete_fluid_sample(fluid_sample_t* sample);
526 int fluid_sample_import_sfont(fluid_sample_t* sample, SFSample* sfsample, fluid_defsfont_t* sfont);
527 int fluid_sample_in_rom(fluid_sample_t* sample);
530 #endif /* _FLUID_SFONT_H */