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 Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1 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  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser 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_SFFILE_H
25 #define _FLUID_SFFILE_H
26 
27 
28 #include "fluid_gen.h"
29 #include "fluid_list.h"
30 #include "fluid_mod.h"
31 #include "fluidsynth.h"
32 #include "fluid_sys.h"
33 
34 
35 /* Sound Font structure defines */
36 
37 /* Forward declarations */
38 typedef union _SFGenAmount SFGenAmount;
39 typedef struct _SFVersion SFVersion;
40 typedef struct _SFMod SFMod;
41 typedef struct _SFGen SFGen;
42 typedef struct _SFZone SFZone;
43 typedef struct _SFSample SFSample;
44 typedef struct _SFInst SFInst;
45 typedef struct _SFPreset SFPreset;
46 typedef struct _SFData SFData;
47 typedef struct _SFChunk SFChunk;
48 
49 
50 struct _SFVersion
51 {
52     /* version structure */
53     unsigned short major;
54     unsigned short minor;
55 };
56 
57 struct _SFMod
58 {
59     /* Modulator structure */
60     unsigned short src; /* source modulator */
61     unsigned short dest; /* destination generator */
62     signed short amount; /* signed, degree of modulation */
63     unsigned short amtsrc; /* second source controls amnt of first */
64     unsigned short trans; /* transform applied to source */
65 };
66 
67 union _SFGenAmount   /* Generator amount structure */
68 {
69     signed short sword; /* signed 16 bit value */
70     unsigned short uword; /* unsigned 16 bit value */
71     struct
72     {
73         unsigned char lo; /* low value for ranges */
74         unsigned char hi; /* high value for ranges */
75     } range;
76 };
77 
78 struct _SFGen
79 {
80     /* Generator structure */
81     unsigned short id; /* generator ID */
82     SFGenAmount amount; /* generator value */
83 };
84 
85 struct _SFZone
86 {
87     /* Sample/instrument zone structure */
88     fluid_list_t *gen; /* list of generators */
89     fluid_list_t *mod; /* list of modulators */
90 };
91 
92 struct _SFSample
93 {
94     /* Sample structure */
95     char name[21]; /* Name of sample */
96     int idx; /* Index of this instrument in the Soundfont */
97     unsigned int start; /* Offset in sample area to start of sample */
98     unsigned int end; /* Offset from start to end of sample,
99              this is the last point of the
100              sample, the SF spec has this as the
101              1st point after, corrected on
102              load/save */
103     unsigned int loopstart; /* Offset from start to start of loop */
104     unsigned int loopend; /* Offset from start to end of loop,
105                  marks the first point after loop,
106                  whose sample value is ideally
107                  equivalent to loopstart */
108     unsigned int samplerate; /* Sample rate recorded at */
109     unsigned char origpitch; /* root midi key number */
110     signed char pitchadj; /* pitch correction in cents */
111     unsigned short sampletype; /* 1 mono,2 right,4 left,linked 8,0x8000=ROM */
112     fluid_sample_t *fluid_sample; /* Imported sample (fixed up in fluid_defsfont_load) */
113 };
114 
115 struct _SFInst
116 {
117     /* Instrument structure */
118     char name[21]; /* Name of instrument */
119     int idx; /* Index of this instrument in the Soundfont */
120     fluid_list_t *zone; /* list of instrument zones */
121 };
122 
123 struct _SFPreset
124 {
125     /* Preset structure */
126     char name[21]; /* preset name */
127     unsigned short prenum; /* preset number */
128     unsigned short bank; /* bank number */
129     fluid_list_t *zone; /* list of preset zones */
130 };
131 
132 /* NOTE: sffd is also used to determine if sound font is new (NULL) */
133 struct _SFData
134 {
135     /* Sound font data structure */
136     SFVersion version; /* sound font version */
137     SFVersion romver; /* ROM version */
138 
139     unsigned int filesize;
140 
141     unsigned int samplepos; /* position within sffd of the sample chunk */
142     unsigned int samplesize; /* length within sffd of the sample chunk */
143 
144     unsigned int sample24pos; /* position within sffd of the sm24 chunk, set to zero if no 24 bit
145                                  sample support */
146     unsigned int sample24size; /* length within sffd of the sm24 chunk */
147 
148     unsigned int hydrapos;
149     unsigned int hydrasize;
150 
151     char *fname; /* file name */
152     FILE *sffd; /* loaded sfont file descriptor */
153     const fluid_file_callbacks_t *fcbs; /* file callbacks used to read this file */
154 
155     fluid_rec_mutex_t mtx; /* this mutex can be used to synchronize calls to fcbs when using multiple threads (e.g. SF3 loading) */
156 
157     fluid_list_t *info; /* linked list of info strings (1st byte is ID) */
158     fluid_list_t *preset; /* linked list of preset info */
159     fluid_list_t *inst; /* linked list of instrument info */
160     fluid_list_t *sample; /* linked list of sample info */
161 };
162 
163 /* functions */
164 
165 
166 /*-----------------------------------sffile.h----------------------------*/
167 /*
168    File structures and routines (used to be in sffile.h)
169 */
170 
171 /* sfont file data structures */
172 struct _SFChunk
173 {
174     /* RIFF file chunk structure */
175     unsigned int id; /* chunk id */
176     unsigned int size; /* size of the following chunk */
177 };
178 
179 /* Public functions  */
180 SFData *fluid_sffile_open(const char *fname, const fluid_file_callbacks_t *fcbs);
181 void fluid_sffile_close(SFData *sf);
182 int fluid_sffile_parse_presets(SFData *sf);
183 int fluid_sffile_read_sample_data(SFData *sf, unsigned int sample_start, unsigned int sample_end,
184                                   int sample_type, short **data, char **data24);
185 
186 
187 /* extern only for unit test purposes */
188 int load_igen(SFData *sf, int size);
189 int load_pgen(SFData *sf, int size);
190 void delete_preset(SFPreset *preset);
191 void delete_inst(SFInst *inst);
192 void delete_zone(SFZone *zone);
193 
194 #endif /* _FLUID_SFFILE_H */
195