MuseScore  3.4
Music composition and notation
sfont.h
Go to the documentation of this file.
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  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307, USA
21  */
22 
23 #ifndef _FLUID_DEFSFONT_H
24 #define _FLUID_DEFSFONT_H
25 
26 #include "config.h"
27 #include "fluid.h"
28 
29 namespace FluidS {
30 
31 class Preset;
32 class Sample;
33 class Instrument;
34 struct SFGen;
35 struct SFMod;
36 
37 struct SFChunk;
38 
39 //---------------------------------------------------------
40 // SFVersion
41 //---------------------------------------------------------
42 
43 struct SFVersion { // version structure
44  unsigned short major, minor;
45  SFVersion();
46  };
47 
48 //---------------------------------------------------------
49 // SFont
50 //---------------------------------------------------------
51 
52 class SFont {
54  QFile f;
55  unsigned samplepos; // the position in the file at which the sample data starts
56  unsigned samplesize; // the size of the sample data
57 
58  QList<Instrument*> instruments;
59  QList<Preset*> presets;
60  QList<Sample*> sample;
61 
62  int _id;
64 
65  SFVersion _version; // sound font version
66  SFVersion romver; // ROM version
67  QString _fontName;
68  QList<unsigned char*> infos; // list of info strings (1st byte is ID)
69 
70  void read_listchunk(SFChunk* chunk);
71  void process_info(int size);
72  void process_sdta(unsigned int size);
73  void pdtahelper(unsigned int expid, unsigned int reclen, SFChunk* chunk, int* size);
74 
75  void process_pdta(int size);
76  void load_phdr(int size);
77  void load_pbag(int size);
78  void load_pmod(int size);
79  void load_pgen(int size);
80  void load_ihdr(int size);
81  void load_ibag(int size);
82  void load_imod(int size);
83  void load_igen(int size);
84  void load_shdr(int size);
85 
86  void fixup_pgen();
87  void fixup_igen();
88  void fixup_sample();
89 
90  void readchunk(SFChunk*);
91  unsigned short READW();
92  void READD(unsigned int& var);
93  void FSKIP(int size) { return safe_fseek(size); }
94  void FSKIPW();
95  unsigned char READB();
96  signed char READC();
97  void READSTR(char*);
98 
99  void safe_fread(void *buf, int count);
100  void safe_fseek(long ofs);
101  bool load();
102 
103  public:
104  SFont(Fluid* f);
105  virtual ~SFont();
106 
107  QString get_name() const { return f.fileName(); }
108  Preset* get_preset(int bank, int prenum);
109 
110  bool read(const QString& file);
111 
112  int load_sampledata();
113  unsigned int samplePos() const { return samplepos; }
114  int id() const { return _id; }
115  void setId(int i) { _id = i; }
116  void setSamplepos(unsigned v) { samplepos = v; }
117  void setSamplesize(unsigned v) { samplesize = v; }
118  unsigned getSamplesize() const { return samplesize; }
119  const QList<Preset*> getPresets() const { return presets; }
120  SFVersion version() const { return _version; }
121  int bankOffset() const { return _bankOffset; }
122  void setBankOffset(int val) { _bankOffset = val; }
123  QString fontName() const { return _fontName; }
124 
125  friend class Preset;
126  };
127 
128 //---------------------------------------------------------
129 // Sample
130 //---------------------------------------------------------
131 
132 class Sample {
133  bool _valid;
134 
135  public:
137  unsigned int start;
138  unsigned int end;
139  unsigned int loopstart;
140  unsigned int loopend;
141  unsigned int samplerate;
143  int pitchadj;
145 
146  short* data;
147 
151  /* Set this to zero, when submitting a new sample. */
152 
155 
156  Sample(SFont*);
157  ~Sample();
158 
159  bool inRom() const;
160  void optimize();
161  void load();
162  bool valid() const { return _valid; }
163  void setValid(bool v) { _valid = v; }
164 #ifdef SOUNDFONT3
165  bool decompressOggVorbis(char* p, int size);
166 #endif
167  };
168 
169 //---------------------------------------------------------
170 // Zone
171 //---------------------------------------------------------
172 
173 class Zone {
174  public:
175  union {
177  int sampIdx;
179  int instIdx;
180  };
181 
182  QList<SFGen*> gen;
183  QList<SFMod*> mod;
184 
185  int keylo, keyhi, vello, velhi;
186  Generator genlist[GEN_LAST];
187  QList<Mod*> modlist; // List of modulators
188 
189  public:
190  Zone();
191  ~Zone();
192  bool importZone();
193  bool inside_range(int key, int vel) const;
194  Instrument* get_inst() const { return instrument; }
195  Sample* get_sample() const { return sample; }
196  };
197 
198 //---------------------------------------------------------
199 // Instrument
200 //---------------------------------------------------------
201 
202 class Instrument {
203  public:
205  QList<Zone*> zones;
206 
207  public:
208  Instrument();
209  ~Instrument();
210  Zone* get_global_zone() const { return global_zone; }
211  QList<Zone*> get_zone() { return zones; }
212  bool import_sfont();
213  };
214 
215 //---------------------------------------------------------
216 // Preset
217 //---------------------------------------------------------
218 
219 class Preset {
220  public:
221  QString name; // the name of the preset
223  int bank; // the bank number
224  int num; // the preset number
225 
226  Zone* _global_zone; // the global zone of the preset
227  QList<Zone*> zones;
228 
229  public:
230  Preset(SFont* sfont);
231  ~Preset();
232 
233  QString get_name() const { return name; }
234  int get_banknum() const { return bank; }
235  int get_num() const { return num; }
236  bool noteon(Fluid*, unsigned id, int chan, int key, int vel, double nt);
237 
238  void setGlobalZone(Zone* z) { _global_zone = z; }
239  bool importSfont();
240 
241  Zone* global_zone() { return _global_zone; }
242  void loadSamples();
243  QList<Zone*> getZones() { return zones; }
244  };
245 
246 //---------------------------------------------------------
247 // SFChunk
248 //---------------------------------------------------------
249 
250 struct SFChunk { // RIFF file chunk structure
251  unsigned int id; // chunk id
252  unsigned int size; // size of the following chunk
253  };
254 
255 struct SFMod { /* Modulator structure */
256  unsigned short src; /* source modulator */
257  unsigned short dest; /* destination generator */
258  signed short amount; /* signed, degree of modulation */
259  unsigned short amtsrc; /* second source controls amnt of first */
260  unsigned short trans; /* transform applied to source */
261  };
262 
263 union SFGenAmount { /* Generator amount structure */
264  signed short sword; /* signed 16 bit value */
265  unsigned short uword; /* unsigned 16 bit value */
266  struct {
267  unsigned char lo; /* low value for ranges */
268  unsigned char hi; /* high value for ranges */
269  } range;
270  };
271 
272 struct SFGen { /* Generator structure */
273  unsigned short id; /* generator ID */
274  SFGenAmount amount; /* generator value */
275  };
276 
277 
278 /* NOTE: sffd is also used to determine if sound font is new (NULL) */
279 
280 /* sf file chunk IDs */
281 enum {
283  INFO_ID, SDTA_ID, PDTA_ID, /* info/sample/preset */
284 
285  IFIL_ID, ISNG_ID, INAM_ID, IROM_ID, /* info ids (1st byte of info strings) */
286  IVER_ID, ICRD_ID, IENG_ID, IPRD_ID, /* more info ids */
287  ICOP_ID, ICMT_ID, ISFT_ID, /* and yet more info ids */
288 
289  SNAM_ID, SMPL_ID, /* sample ids */
290  PHDR_ID, PBAG_ID, PMOD_ID, PGEN_ID, /* preset ids */
291  IHDR_ID, IBAG_ID, IMOD_ID, IGEN_ID, /* instrument ids */
292  SHDR_ID /* sample info */
293  };
294 
295 /* generator types */
296 enum Gen_Type {
315  };
316 
317 #define Gen_MaxValid Gen_Dummy - 1 /* maximum valid generator */
318 
319 /* generator unit type */
320 enum Gen_Unit {
321  None, /* No unit type */
322  Unit_Smpls, /* in samples */
323  Unit_32kSmpls, /* in 32k samples */
324  Unit_Cent, /* in cents (1/100th of a semitone) */
325  Unit_HzCent, /* in Hz Cents */
326  Unit_TCent, /* in Time Cents */
327  Unit_cB, /* in centibels (1/100th of a decibel) */
328  Unit_Percent, /* in percentage */
329  Unit_Semitone, /* in semitones */
330  Unit_Range /* a range of values */
331  };
332 
333 /* global data */
334 
335 #define CHNKIDSTR(id) &idlist[(id - 1) * 4]
336 
337 /* sfont file chunk sizes */
338 #define SFPHDRSIZE 38
339 #define SFBAGSIZE 4
340 #define SFMODSIZE 10
341 #define SFGENSIZE 4
342 #define SFIHDRSIZE 22
343 #define SFSHDRSIZE 46
344 
345 /* sfont file data structures */
346 
347 struct SFPhdr {
348  unsigned char name[20]; /* preset name */
349  unsigned short preset; /* preset number */
350  unsigned short bank; /* bank number */
351  unsigned short pbagndx; /* index into preset bag */
352  unsigned int library; /* just for preserving them */
353  unsigned int genre; /* Not used */
354  unsigned int morphology; /* Not used */
355  };
356 
357 struct SFBag {
358  unsigned short genndx; /* index into generator list */
359  unsigned short modndx; /* index into modulator list */
360  };
361 
362 struct SFIhdr {
363  char name[20]; /* Name of instrument */
364  unsigned short ibagndx; /* Instrument bag index */
365  };
366 
367 /* Basic bit swapping functions
368  */
369 #define GUINT16_SWAP_LE_BE_CONSTANT(val) ((unsigned short) ( \
370  (((unsigned short) (val) & (unsigned short) 0x00ffU) << 8) | \
371  (((unsigned short) (val) & (unsigned short) 0xff00U) >> 8)))
372 #define GUINT32_SWAP_LE_BE_CONSTANT(val) ((unsigned int) ( \
373  (((unsigned int) (val) & (unsigned int) 0x000000ffU) << 24) | \
374  (((unsigned int) (val) & (unsigned int) 0x0000ff00U) << 8) | \
375  (((unsigned int) (val) & (unsigned int) 0x00ff0000U) >> 8) | \
376  (((unsigned int) (val) & (unsigned int) 0xff000000U) >> 24)))
377 
378 #define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
379 #define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
380 
381 #define GINT16_TO_LE(val) ((signed short) (val))
382 #define GUINT16_TO_LE(val) ((unsigned short) (val))
383 #define GINT16_TO_BE(val) ((signed short) GUINT16_SWAP_LE_BE (val))
384 #define GUINT16_TO_BE(val) (GUINT16_SWAP_LE_BE (val))
385 #define GINT32_TO_LE(val) ((signed int) (val))
386 #define GUINT32_TO_LE(val) ((unsigned int) (val))
387 #define GINT32_TO_BE(val) ((signed int) GUINT32_SWAP_LE_BE (val))
388 #define GUINT32_TO_BE(val) (GUINT32_SWAP_LE_BE (val))
389 
390 /* The G*_TO_?E() macros are defined in glibconfig.h.
391  * The transformation is symmetric, so the FROM just maps to the TO.
392  */
393 #define GINT16_FROM_LE(val) (GINT16_TO_LE (val))
394 #define GUINT16_FROM_LE(val) (GUINT16_TO_LE (val))
395 #define GINT16_FROM_BE(val) (GINT16_TO_BE (val))
396 #define GUINT16_FROM_BE(val) (GUINT16_TO_BE (val))
397 #define GINT32_FROM_LE(val) (GINT32_TO_LE (val))
398 #define GUINT32_FROM_LE(val) (GUINT32_TO_LE (val))
399 #define GINT32_FROM_BE(val) (GINT32_TO_BE (val))
400 #define GUINT32_FROM_BE(val) (GUINT32_TO_BE (val))
401 }
402 
403 #endif /* _FLUID_SFONT_H */
Definition: sfont.h:305
Definition: sfont.h:132
Definition: sfont.h:289
unsigned short bank
Definition: sfont.h:350
Definition: sfont.h:283
const QList< Preset * > getPresets() const
Definition: sfont.h:119
Definition: sfont.h:272
Definition: sfont.h:287
Definition: sfont.h:304
unsigned short minor
Definition: sfont.h:44
unsigned short major
Definition: sfont.h:44
Definition: sfont.h:286
Definition: sfont.h:297
Definition: sfont.h:255
Definition: sfont.h:302
Definition: sfont.h:362
SFGenAmount amount
Definition: sfont.h:274
Instrument * get_inst() const
Definition: sfont.h:194
unsigned short id
Definition: sfont.h:273
unsigned short ibagndx
Definition: sfont.h:364
Definition: sfont.h:312
Definition: sfont.h:282
Definition: sfont.h:286
unsigned int size
Definition: sfont.h:252
Sample * get_sample() const
Definition: sfont.h:195
SFont * sfont
Definition: sfont.h:222
Zone * get_global_zone() const
Definition: sfont.h:210
Definition: sfont.h:312
Definition: sfont.h:291
Instrument * instrument
Definition: sfont.h:178
int sampletype
Definition: sfont.h:144
Definition: sfont.h:313
Definition: sfont.h:325
unsigned samplepos
Definition: sfont.h:55
Definition: sfont.h:298
int id() const
Definition: sfont.h:114
Definition: sfont.h:285
Definition: sfont.h:292
int pitchadj
Definition: sfont.h:143
void setId(int i)
Definition: sfont.h:115
Definition: sfont.h:301
Definition: sfont.h:250
unsigned short amtsrc
Definition: sfont.h:259
short * data
Definition: sfont.h:146
int bank
Definition: sfont.h:223
Definition: sfont.h:310
QString get_name() const
Definition: sfont.h:233
Definition: sfont.h:310
unsigned short modndx
Definition: sfont.h:359
Definition: sfont.h:285
unsigned short dest
Definition: sfont.h:257
Definition: sfont.h:290
Definition: sfont.h:286
int bankOffset() const
Definition: sfont.h:121
Definition: sfont.h:290
Definition: sfont.h:299
Definition: sfont.h:173
QString get_name() const
Definition: sfont.h:107
Definition: sfont.h:286
QList< unsigned char * > infos
Definition: sfont.h:68
unsigned int library
Definition: sfont.h:352
Definition: sfont.h:52
Definition: sfont.h:308
Definition: sfont.h:297
Definition: sfont.h:290
Definition: sfont.h:289
Definition: sfont.h:324
Definition: sfont.h:43
unsigned int morphology
Definition: sfont.h:354
Definition: sfont.h:302
signed short sword
Definition: sfont.h:264
Definition: sfont.h:308
signed short amount
Definition: sfont.h:258
Definition: sfont.h:357
unsigned samplesize
Definition: sfont.h:56
Definition: sfont.h:307
Definition: fluid.h:297
Definition: sfont.h:313
Definition: sfont.h:312
Definition: sfont.h:306
Definition: sfont.h:282
unsigned int samplerate
Definition: sfont.h:141
void setValid(bool v)
Definition: sfont.h:163
unsigned int genre
Definition: sfont.h:353
Definition: model.h:126
int get_banknum() const
Definition: sfont.h:234
Fluid * synth
Definition: sfont.h:53
Definition: sfont.h:306
Definition: sfont.h:312
bool _valid
Definition: sfont.h:133
QList< Mod * > modlist
Definition: sfont.h:187
Definition: sfont.h:330
Definition: sfont.h:282
unsigned short genndx
Definition: sfont.h:358
Definition: sfont.h:309
Definition: sfont.h:303
QList< SFMod * > mod
Definition: sfont.h:183
QList< Zone * > getZones()
Definition: sfont.h:243
int get_num() const
Definition: sfont.h:235
Definition: sfont.h:323
Definition: sfont.h:308
unsigned short uword
Definition: sfont.h:265
void setBankOffset(int val)
Definition: sfont.h:122
Definition: sfont.h:303
QString fontName() const
Definition: sfont.h:123
Definition: sfont.h:285
Value defines the count of generators (fluid_gen_type)
Definition: fluid.h:209
unsigned int end
Definition: sfont.h:138
QList< Sample * > sample
Definition: sfont.h:60
int _id
Definition: sfont.h:62
Definition: sfont.h:321
unsigned getSamplesize() const
Definition: sfont.h:118
Definition: sfont.h:287
Definition: sfont.h:300
Definition: sfont.h:290
Definition: sfont.h:307
unsigned int samplePos() const
Definition: sfont.h:113
unsigned int loopend
Definition: sfont.h:140
int num
Definition: sfont.h:224
Definition: sfont.h:311
Definition: sfont.h:328
Gen_Unit
Definition: sfont.h:320
unsigned int start
Definition: sfont.h:137
Zone * _global_zone
Definition: sfont.h:226
QString name
Definition: sfont.h:221
Definition: sfont.h:299
Definition: sfont.h:303
Definition: sfont.h:285
bool valid() const
Definition: sfont.h:162
Definition: sfont.h:304
Definition: sfont.h:300
Definition: sfont.h:309
void FSKIP(int size)
Definition: sfont.h:93
Definition: sfont.h:299
Definition: sfont.h:322
void setSamplesize(unsigned v)
Definition: sfont.h:117
QList< Zone * > get_zone()
Definition: sfont.h:211
Definition: sfont.h:263
SFVersion _version
Definition: sfont.h:65
Definition: sfont.h:301
QList< SFGen * > gen
Definition: sfont.h:182
Definition: sfont.h:347
Definition: sfont.h:298
double amplitude_that_reaches_noise_floor
Definition: sfont.h:154
int instIdx
Definition: sfont.h:179
void setGlobalZone(Zone *z)
Definition: sfont.h:238
Definition: sfont.h:313
Definition: sfont.h:326
int _bankOffset
Definition: sfont.h:63
bool amplitude_that_reaches_noise_floor_is_valid
The amplitude, that will lower the level of the sample&#39;s loop to the noise floor. ...
Definition: sfont.h:153
Definition: sfont.h:304
Definition: sfont.h:291
unsigned char lo
Definition: sfont.h:267
unsigned short preset
Definition: sfont.h:349
unsigned short pbagndx
Definition: sfont.h:351
QString _fontName
Definition: sfont.h:67
int vello
Definition: sfont.h:185
QFile f
Definition: sfont.h:54
Definition: sfont.h:283
Definition: sfont.h:314
Definition: sfont.h:301
unsigned short trans
Definition: sfont.h:260
Definition: sfont.h:297
Definition: sfont.h:298
Definition: sfont.h:305
SFVersion()
Definition: sfont.cpp:40
Definition: sfont.h:304
Definition: sfont.h:309
Definition: sfont.h:291
Definition: sfont.h:327
Definition: sfont.h:219
Definition: sfont.h:283
SoundFont generator structure.
Definition: fluid.h:498
Definition: sfont.h:307
Definition: sfont.h:300
Definition: sample.h:20
Definition: sfont.h:282
SFVersion romver
Definition: sfont.h:66
int sampIdx
Definition: sfont.h:177
QList< Zone * > zones
Definition: sfont.h:227
SFVersion version() const
Definition: sfont.h:120
int origpitch
Definition: sfont.h:142
Definition: chan.cpp:25
Definition: sfont.h:329
Definition: sfont.h:301
Gen_Type
Definition: sfont.h:296
Definition: sfont.h:307
void setSamplepos(unsigned v)
Definition: sfont.h:116
Zone * global_zone()
Definition: sfont.h:241
Definition: sfont.h:303
Definition: sfont.h:287
Definition: sfont.h:299
Definition: sfont.h:202
Definition: sfont.h:305
Definition: sfont.h:302
Sample * sample
Definition: sfont.h:176
Definition: sfont.h:311
QList< Instrument * > instruments
Definition: sfont.h:58
unsigned int loopstart
Definition: sfont.h:139
unsigned int id
Definition: sfont.h:251
QList< Preset * > presets
Definition: sfont.h:59
Definition: sfont.h:313
Definition: sfont.h:311
SFont * sf
Definition: sfont.h:136
unsigned char hi
Definition: sfont.h:268
Definition: sfont.h:306
QList< Zone * > zones
Definition: sfont.h:205
Zone * global_zone
Definition: sfont.h:204
Definition: sfont.h:310
Definition: sfont.h:301
unsigned short src
Definition: sfont.h:256
Definition: sfont.h:291