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 "fluidsynth_priv.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 typedef struct _SFPhdr SFPhdr;
49 typedef struct _SFBag SFBag;
50 typedef struct _SFIhdr SFIhdr;
51 typedef struct _SFShdr SFShdr;
52 
53 
54 struct _SFVersion
55 {
56     /* version structure */
57     unsigned short major;
58     unsigned short minor;
59 };
60 
61 struct _SFMod
62 {
63     /* Modulator structure */
64     unsigned short src; /* source modulator */
65     unsigned short dest; /* destination generator */
66     signed short amount; /* signed, degree of modulation */
67     unsigned short amtsrc; /* second source controls amnt of first */
68     unsigned short trans; /* transform applied to source */
69 };
70 
71 union _SFGenAmount   /* Generator amount structure */
72 {
73     signed short sword; /* signed 16 bit value */
74     unsigned short uword; /* unsigned 16 bit value */
75     struct
76     {
77         unsigned char lo; /* low value for ranges */
78         unsigned char hi; /* high value for ranges */
79     } range;
80 };
81 
82 struct _SFGen
83 {
84     /* Generator structure */
85     unsigned short id; /* generator ID */
86     SFGenAmount amount; /* generator value */
87 };
88 
89 struct _SFZone
90 {
91     /* Sample/instrument zone structure */
92     fluid_list_t *instsamp; /* instrument/sample pointer for zone */
93     fluid_list_t *gen; /* list of generators */
94     fluid_list_t *mod; /* list of modulators */
95 };
96 
97 struct _SFSample
98 {
99     /* Sample structure */
100     char name[21]; /* Name of sample */
101     unsigned char samfile; /* Loaded sfont/sample buffer = 0/1 */
102     unsigned int start; /* Offset in sample area to start of sample */
103     unsigned int end; /* Offset from start to end of sample,
104              this is the last point of the
105              sample, the SF spec has this as the
106              1st point after, corrected on
107              load/save */
108     unsigned int loopstart; /* Offset from start to start of loop */
109     unsigned int loopend; /* Offset from start to end of loop,
110                  marks the first point after loop,
111                  whose sample value is ideally
112                  equivalent to loopstart */
113     unsigned int samplerate; /* Sample rate recorded at */
114     unsigned char origpitch; /* root midi key number */
115     signed char pitchadj; /* pitch correction in cents */
116     unsigned short sampletype; /* 1 mono,2 right,4 left,linked 8,0x8000=ROM */
117     fluid_sample_t *fluid_sample; /* Imported sample (fixed up in fluid_defsfont_load) */
118 };
119 
120 struct _SFInst
121 {
122     /* Instrument structure */
123     char name[21]; /* Name of instrument */
124     int idx; /* Index of this instrument in the Soundfont */
125     fluid_list_t *zone; /* list of instrument zones */
126 };
127 
128 struct _SFPreset
129 {
130     /* Preset structure */
131     char name[21]; /* preset name */
132     unsigned short prenum; /* preset number */
133     unsigned short bank; /* bank number */
134     unsigned int libr; /* Not used (preserved) */
135     unsigned int genre; /* Not used (preserved) */
136     unsigned int morph; /* Not used (preserved) */
137     fluid_list_t *zone; /* list of preset zones */
138 };
139 
140 /* NOTE: sffd is also used to determine if sound font is new (NULL) */
141 struct _SFData
142 {
143     /* Sound font data structure */
144     SFVersion version; /* sound font version */
145     SFVersion romver; /* ROM version */
146 
147     unsigned int filesize;
148 
149     unsigned int samplepos; /* position within sffd of the sample chunk */
150     unsigned int samplesize; /* length within sffd of the sample chunk */
151 
152     unsigned int sample24pos; /* position within sffd of the sm24 chunk, set to zero if no 24 bit
153                                  sample support */
154     unsigned int sample24size; /* length within sffd of the sm24 chunk */
155 
156     unsigned int hydrapos;
157     unsigned int hydrasize;
158 
159     char *fname; /* file name */
160     FILE *sffd; /* loaded sfont file descriptor */
161     const fluid_file_callbacks_t *fcbs; /* file callbacks used to read this file */
162 
163     fluid_list_t *info; /* linked list of info strings (1st byte is ID) */
164     fluid_list_t *preset; /* linked list of preset info */
165     fluid_list_t *inst; /* linked list of instrument info */
166     fluid_list_t *sample; /* linked list of sample info */
167 };
168 
169 /* functions */
170 
171 
172 /*-----------------------------------sffile.h----------------------------*/
173 /*
174    File structures and routines (used to be in sffile.h)
175 */
176 
177 /* sfont file data structures */
178 struct _SFChunk
179 {
180     /* RIFF file chunk structure */
181     unsigned int id; /* chunk id */
182     unsigned int size; /* size of the following chunk */
183 };
184 
185 struct _SFPhdr
186 {
187     unsigned char name[20]; /* preset name */
188     unsigned short preset; /* preset number */
189     unsigned short bank; /* bank number */
190     unsigned short pbagndx; /* index into preset bag */
191     unsigned int library; /* just for preserving them */
192     unsigned int genre; /* Not used */
193     unsigned int morphology; /* Not used */
194 };
195 
196 struct _SFBag
197 {
198     unsigned short genndx; /* index into generator list */
199     unsigned short modndx; /* index into modulator list */
200 };
201 
202 struct _SFIhdr
203 {
204     char name[20]; /* Name of instrument */
205     unsigned short ibagndx; /* Instrument bag index */
206 };
207 
208 struct _SFShdr
209 {
210     /* Sample header loading struct */
211     char name[20]; /* Sample name */
212     unsigned int start; /* Offset to start of sample */
213     unsigned int end; /* Offset to end of sample */
214     unsigned int loopstart; /* Offset to start of loop */
215     unsigned int loopend; /* Offset to end of loop */
216     unsigned int samplerate; /* Sample rate recorded at */
217     unsigned char origpitch; /* root midi key number */
218     signed char pitchadj; /* pitch correction in cents */
219     unsigned short samplelink; /* Not used */
220     unsigned short sampletype; /* 1 mono,2 right,4 left,linked 8,0x8000=ROM */
221 };
222 
223 /* Public functions  */
224 SFData *fluid_sffile_open(const char *fname, const fluid_file_callbacks_t *fcbs);
225 void fluid_sffile_close(SFData *sf);
226 int fluid_sffile_parse_presets(SFData *sf);
227 int fluid_sffile_read_sample_data(SFData *sf, unsigned int sample_start, unsigned int sample_end,
228                                   int sample_type, short **data, char **data24);
229 
230 #endif /* _FLUID_SFFILE_H */
231