libgig  3.3.0.svn21
SF.h
Go to the documentation of this file.
1 /***************************************************************************
2  * *
3  * libsf2 - C++ cross-platform SF2 format file access library *
4  * *
5  * Copyright (C) 2009-2010 by Grigor Iliev <grigor@grigoriliev.com> *
6  * *
7  * This library is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This library is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this library; if not, write to the Free Software *
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20  * MA 02111-1307 USA *
21  ***************************************************************************/
22 
23 #ifndef __SF2_SF_H__
24 #define __SF2_SF_H__
25 
26 #include "RIFF.h"
27 
28 #include <vector>
29 
30 
31 #define RIFF_ID(x) (*((uint32_t*) x))
32 
33 
34 #define RIFF_TYPE_SF2 RIFF_ID("sfbk")
35 
36 // Level 0
37 #define LIST_TYPE_SDTA RIFF_ID("sdta")
38 #define LIST_TYPE_PDTA RIFF_ID("pdta")
39 
40 // Level 1
41 //<INFO-list>
42 #define CHUNK_ID_IFIL RIFF_ID("ifil")
43 #define CHUNK_ID_ISNG RIFF_ID("isng")
44 #define CHUNK_ID_IROM RIFF_ID("irom")
45 #define CHUNK_ID_IVER RIFF_ID("iver")
46 
47 //<sdta-list>
48 #define CHUNK_ID_SM24 RIFF_ID("sm24")
49 
50 //<pdta-list>
51 #define CHUNK_ID_PHDR RIFF_ID("phdr")
52 #define CHUNK_ID_PBAG RIFF_ID("pbag")
53 #define CHUNK_ID_PMOD RIFF_ID("pmod")
54 #define CHUNK_ID_PGEN RIFF_ID("pgen")
55 #define CHUNK_ID_INST RIFF_ID("inst")
56 #define CHUNK_ID_IBAG RIFF_ID("ibag")
57 #define CHUNK_ID_IMOD RIFF_ID("imod")
58 #define CHUNK_ID_IGEN RIFF_ID("igen")
59 #define CHUNK_ID_SHDR RIFF_ID("shdr")
60 
62 namespace sf2 {
63 
64  static uint NONE = 0x1ffffff;
65 
66  double ToSeconds(int Timecents);
67  double ToRatio(int Centibels);
68  double ToHz(int cents);
69 
70  typedef struct _PresetBag {
71  uint16_t GenNdx;
72  uint16_t ModNdx;
73  } PresetBag;
74 
75  typedef uint16_t SFModulator;
76  typedef uint16_t SFGenerator;
77  typedef uint16_t SFTransform;
78 
79  typedef struct _ModList {
80  SFModulator ModSrcOper;
81  SFGenerator ModDestOper;
82  uint16_t ModAmount;
83  SFModulator ModAmtSrcOper;
84  SFTransform ModTransOper;
85  } ModList;
86 
87  typedef struct _RangesType {
88  #if WORDS_BIGENDIAN
89  uint8_t byHi;
90  uint8_t byLo;
91  #else
92  uint8_t byLo;
93  uint8_t byHi;
94  #endif
95  } RangesType;
96 
97  typedef union _GenAmountType {
99  short shAmount;
100  uint16_t wAmount;
101  } GenAmountType;
102 
103  typedef struct _GenList {
104  SFGenerator GenOper;
106  } GenList;
107 
108  typedef struct _InstBag {
109  uint16_t InstGenNdx;
110  uint16_t InstModNdx;
111  } InstBag;
112 
113  typedef enum {
134  UNUSED4, //20
175  } SFGeneratorType;
176 
177  class Modulator {
178  public:
179 
184  enum {
185  NO_CONTROLLER = 0,
186  NOTE_ON_VELOCITY = 2,
187  NOTE_ON_KEY_NUMBER = 3,
188  POLY_PRESSURE = 10,
189  CHANNEL_PRESSURE = 13,
190  PITCH_WHEEL = 14,
191  PITCH_WHEEL_SENSITIVITY = 16,
192  LINK = 127
193  };
194 
198  enum {
199  LINEAR = 0,
202  SWITCH
203  };
204 
205  int Type;
207  bool Direction;
208  bool Polarity;
209  int Index;
210 
211  Modulator(SFModulator mod);
212  };
213 
215  public:
217  SFGenerator ModDestOper;
218  uint16_t ModAmount;
220  SFTransform ModTransOper;
221 
222  ModulatorItem(ModList& mod);
223  };
224 
225 
226  typedef std::string String;
227 
228  class Exception : public RIFF::Exception {
229  public: Exception(String Message) : RIFF::Exception(Message) { }
230  };
231 
232  class Version {
233  public:
234  int Major;
235  int Minor;
236 
237  Version(RIFF::Chunk* ck);
238  };
239 
240  class Info {
241  public:
243  String SoundEngine;
244  String BankName;
245  String RomName;
247  String CreationDate;
248  String Engineers;
249  String Product;
250  String Copyright;
251  String Comments;
252  String Software;
253 
254  Info(RIFF::List* list);
255  ~Info();
256  private:
257  static void LoadString(uint32_t ChunkID, RIFF::List* lstINFO, String& s);
258  };
259 
260  class Region;
261 
262  class Sample {
263  public:
264 
265  typedef enum {
266  MONO_SAMPLE = 1,
267  RIGHT_SAMPLE = 2,
268  LEFT_SAMPLE = 4,
269  LINKED_SAMPLE = 8,
270  ROM_MONO_SAMPLE = 0x8001,
271  ROM_RIGHT_SAMPLE = 0x8002,
272  ROM_LEFT_SAMPLE = 0x8004,
273  ROM_LINKED_SAMPLE = 0x8008
274  } Link;
275 
278  public:
279  unsigned long position;
280  bool reverse;
281  unsigned long loop_cycles_left;
282  };
283 
285  struct buffer_t {
286  void* pStart;
287  unsigned long Size;
288  unsigned long NullExtensionSize;
290  pStart = NULL;
291  Size = 0;
292  NullExtensionSize = 0;
293  }
294  };
295 
296  String Name;
297 
298  Sample(RIFF::Chunk* ck, RIFF::Chunk* pCkSmpl, RIFF::Chunk* pCkSm24);
299 
300  String GetName() { return Name; }
301  int GetChannelCount();
302  long GetTotalFrameCount();
303  int GetFrameSize();
304  bool HasLoops();
305  bool IsUnpitched() { return OriginalPitch == 255; }
306 
307  buffer_t LoadSampleData();
308  buffer_t LoadSampleData(unsigned long SampleCount);
309  buffer_t LoadSampleDataWithNullSamplesExtension(uint NullSamplesCount);
310  buffer_t LoadSampleDataWithNullSamplesExtension(unsigned long SampleCount, uint NullSamplesCount);
311  buffer_t GetCache();
312  void ReleaseSampleData();
313  unsigned long SetPos(unsigned long SampleCount);
314  unsigned long GetPos();
315  unsigned long Read(void* pBuffer, unsigned long SampleCount);
316 
317  unsigned long ReadAndLoop (
318  void* pBuffer,
319  unsigned long FrameCount,
320  PlaybackState* pPlaybackState,
321  Region* pRegion
322  );
323 
324  //protected:
328 
329  //private:
330  int ChannelCount; // 2 for left and right samples
331 
332  uint32_t Start; // in sample data points (frames) from the begining of the sample data field
333  uint32_t End; // in sample data points (frames) from the begining of the sample data field
334  uint32_t StartLoop; // in sample data points (frames) from the begining of the sample data field
335  uint32_t EndLoop; // in sample data points (frames) from the begining of the sample data field
336  uint32_t SampleRate;
337  uint8_t OriginalPitch;
339  uint16_t SampleLink; /* If sfSampleType indicates a left or right sample, the
340  * sample header index of the associated right or left stereo
341  * sample respectively; zero otherwise. */
342  uint16_t SampleType;
343  };
344 
345  class File;
346  class Instrument;
347 
351  class Region {
352  public:
353  int loKey, hiKey;
354  int minVel, maxVel;
355  int pan; // -64 - +63
356  int fineTune; // -99 - +99
357  int coarseTune; // TODO:
358  int overridingRootKey; // represents the MIDI key number at which the sample is to be played back at its original sample rate.
359  int startAddrsOffset, startAddrsCoarseOffset, endAddrsOffset, endAddrsCoarseOffset;
360  int startloopAddrsOffset, startloopAddrsCoarseOffset, endloopAddrsOffset, endloopAddrsCoarseOffset;
361 
362  int modEnvToPitch , modLfoToPitch, modEnvToFilterFc, modLfoToFilterFc; // in cents
363  int modLfoToVolume /* in centibels */, freqModLfo /* in absolute cents */;
364  int delayModLfo; // in absolute timecents
365  int vibLfoToPitch, freqVibLfo /* in absolute cents */;
366  int delayVibLfo; // in absolute timecents
367  int initialFilterFc /* in absolute cents */, initialFilterQ /* in centibels */;
368 
369  uint exclusiveClass; // exclusive group
370 
372  bool HasLoop;
373  uint LoopStart; // index (in frames) from the beginning of the sample
374  uint LoopEnd; // index (in frames) from the beginning of the sample
375  Instrument* pInstrument; // used when the region belongs to preset
376 
377  Region();
378  Sample* GetSample() { return pSample; }
379  Region* GetParent() { return this; }
380 
381  int GetUnityNote();
382 
387  Instrument* GetParentInstrument() { return pParentInstrument; }
388 
389  std::vector<ModulatorItem> modulators;
390 
391 
392  // Instrument can be referenced by more than one presets so we need to calculate values on the fly
393  int GetPan(Region* pPresetRegion = NULL); // -64 - +63
394  int GetFineTune(Region* pPresetRegion = NULL); // -99 - +99
395  int GetCoarseTune(Region* pPresetRegion = NULL); // -120 - +120
396  double GetEG1PreAttackDelay(Region* pPresetRegion = NULL); // in seconds
397  double GetEG1Attack(Region* pPresetRegion = NULL); // in seconds
398  double GetEG1Hold(Region* pPresetRegion = NULL); // in seconds
399  double GetEG1Decay(Region* pPresetRegion = NULL); // in seconds
400  int GetEG1Sustain(Region* pPresetRegion = NULL); // Sustain value of the sample amplitude EG (the decrease in level, expressed in centibels)
401  double GetEG1Release(Region* pPresetRegion = NULL); // in seconds
402 
403  double GetEG2PreAttackDelay(Region* pPresetRegion = NULL); // in seconds
404  double GetEG2Attack(Region* pPresetRegion = NULL); // in seconds
405  double GetEG2Hold(Region* pPresetRegion = NULL); // in seconds
406  double GetEG2Decay(Region* pPresetRegion = NULL); // in seconds
407  int GetEG2Sustain(Region* pPresetRegion = NULL); // Sustain value of the filter cutoff EG (in permilles)
408  double GetEG2Release(Region* pPresetRegion = NULL); // in seconds
409 
410  int GetModEnvToPitch(Region* pPresetRegion = NULL); // in cents
411  int GetModLfoToPitch(Region* pPresetRegion = NULL); // in cents
412  int GetModEnvToFilterFc(Region* pPresetRegion = NULL); // in cents
413  int GetModLfoToFilterFc(Region* pPresetRegion = NULL); // in cents
414  double GetModLfoToVolume(Region* pPresetRegion = NULL); // in centibels
415  double GetFreqModLfo(Region* pPresetRegion = NULL); // in Hz
416  double GetDelayModLfo(Region* pPresetRegion = NULL); // in seconds
417  int GetVibLfoToPitch(Region* pPresetRegion = NULL); // in cents
418  double GetFreqVibLfo(Region* pPresetRegion = NULL); // in Hz
419  double GetDelayVibLfo(Region* pPresetRegion = NULL); // in seconds
420  int GetInitialFilterFc(Region* pPresetRegion); // in absolute cents
421  int GetInitialFilterQ(Region* pPresetRegion); // in centibels
422 
423  friend class Instrument;
424  friend class Preset;
425 
426  private:
427  int EG1PreAttackDelay; // in timecents
428  int EG1Attack; // in timecents
429  int EG1Hold; // in timecents
430  int EG1Decay; // in timecents
431  int EG1Sustain; // Sustain value (the decrease in level, expressed in centibels)
432  int EG1Release; // in timecents
433 
434  int EG2PreAttackDelay; // in timecents
435  int EG2Attack; // in timecents
436  int EG2Hold; // in timecents
437  int EG2Decay; // in timecents
438  int EG2Sustain; // Sustain value of the filter cutoff EG (in permilles)
439  int EG2Release; // in timecents
440 
441  Instrument* pParentInstrument;
442 
443  void SetGenerator(sf2::File* pFile, GenList& Gen);
444  void SetModulator(sf2::File* pFile, ModList& Mod);
445  };
446 
448  public:
449  String Name;
451 
452  InstrumentBase(sf2::File* pFile);
453  virtual ~InstrumentBase();
454 
455  sf2::File* GetFile() { return pFile; }
456  String GetName() { return Name; }
457 
458  int GetRegionCount();
459  Region* GetRegion(int idx);
460 
461  protected:
462  std::vector<Region*> regions;
464  };
465 
466  class Query {
467  public:
468  int key;
469  uint8_t vel;
470 
471  Query(InstrumentBase& instrument);
472  Region* next();
473 
474  private:
475  InstrumentBase& instrument;
476  int i;
477  };
478 
479  class Instrument : public InstrumentBase {
480  public:
481  Instrument(sf2::File* pFile, RIFF::Chunk* ck);
482  ~Instrument();
483 
484  void DeleteRegion(Region* pRegion);
485  //private:
486  uint16_t InstBagNdx;
487 
491  void LoadRegions(int idx1, int idx2);
492 
493  Region* CreateRegion();
494  };
495 
496  class Preset : public InstrumentBase {
497  public:
498  uint16_t PresetNum;
499  uint16_t Bank;
500  uint32_t Library;
501  uint32_t Genre;
502  uint32_t Morphology;
503 
504  Preset(sf2::File* pFile, RIFF::Chunk* ck);
505  ~Preset();
506 
507  //private:
509  uint16_t PresetBagNdx;
510 
514  void LoadRegions(int idx1, int idx2);
515 
516  Region* CreateRegion();
517  };
518 
519  class File {
520  public:
522 
523  File(RIFF::File* pRIFF);
524  ~File();
525 
526  int GetPresetCount();
527  Preset* GetPreset(int idx);
528  int GetInstrumentCount();
529  Instrument* GetInstrument(int idx);
530  void DeleteInstrument(Instrument* pInstrument);
531  int GetSampleCount();
532  Sample* GetSample(int idx);
533  void DeleteSample(Sample* pSample);
534  bool HasSamples();
535 
536  friend class Region;
537  friend class Instrument;
538  friend class Preset;
539 
540  protected:
542  std::vector<PresetBag> PresetBags;
543  std::vector<ModList> PresetModLists;
544  std::vector<GenList> PresetGenLists;
545  std::vector<InstBag> InstBags;
546  std::vector<ModList> InstModLists;
547  std::vector<GenList> InstGenLists;
548 
549  private:
550  std::vector<Preset*> Presets;
551  std::vector<Instrument*> Instruments;
552  std::vector<Sample*> Samples;
553  };
554 
555  String libraryName();
556  String libraryVersion();
557 
558 } // namespace sf2
559 #endif // __SF2_SF_H__
int delayVibLfo
Definition: SF.h:366
double ToHz(int cents)
Definition: SF.cpp:48
Definition: SF.h:519
int Major
Definition: SF.h:234
SFGenerator GenOper
Definition: SF.h:104
uint16_t InstGenNdx
Definition: SF.h:109
String Software
[<ISFT-ck>] ; The SoundFont tools used to create and alter the bank
Definition: SF.h:252
Definition: SF.h:240
Info * pInfo
Definition: SF.h:521
SFTransform ModTransOper
Definition: SF.h:220
Region * pGlobalRegion
Definition: SF.h:450
String GetName()
Definition: SF.h:456
String Name
Definition: SF.h:296
sf2::File * pFile
Definition: SF.h:463
SFModulator ModSrcOper
Definition: SF.h:80
int key
Definition: SF.h:468
Definition: SF.h:466
uint32_t SampleRate
Definition: SF.h:336
struct sf2::_RangesType RangesType
String RomName
[<irom-ck>] ; Refers to the Sound ROM Name
Definition: SF.h:245
uint8_t byHi
Definition: SF.h:93
Instrument zone.
Definition: SF.h:351
String libraryVersion()
Returns version of this C++ library.
Definition: SF.cpp:1501
uint16_t ModNdx
Definition: SF.h:72
struct sf2::_InstBag InstBag
double ToRatio(int Centibels)
Definition: SF.cpp:42
uint16_t GenNdx
Definition: SF.h:71
uint16_t PresetNum
Definition: SF.h:498
String libraryName()
Returns the name of this C++ library.
Definition: SF.cpp:1494
uint16_t PresetBagNdx
Definition: SF.h:509
unsigned long position
Current position within the sample.
Definition: SF.h:279
std::string String
Definition: SF.h:226
int delayModLfo
Definition: SF.h:364
struct sf2::_ModList ModList
RangesType ranges
Definition: SF.h:98
int Type
Definition: SF.h:205
uint32_t Library
Definition: SF.h:500
int Index
Definition: SF.h:209
String Copyright
[<ICOP-ck>] ; Contains any Copyright message
Definition: SF.h:250
Region * GetParent()
Definition: SF.h:379
bool MidiPalete
Definition: SF.h:206
void LoadString(RIFF::Chunk *ck, std::string &s, int strLength)
Definition: SF.cpp:60
String Comments
[<ICMT-ck>] ; Contains any Comments on the Bank
Definition: SF.h:251
uint LoopEnd
Definition: SF.h:374
SoundFont specific classes and definitions.
Definition: SF.h:62
String Product
[<IPRD-ck>] ; Product for which the Bank was intended
Definition: SF.h:249
unsigned long NullExtensionSize
The buffer might be bigger than the actual data, if that&#39;s the case that unused space at the end of t...
Definition: SF.h:288
SFGenerator ModDestOper
Definition: SF.h:81
uint32_t Start
Definition: SF.h:332
uint32_t End
Definition: SF.h:333
std::vector< Region * > regions
Definition: SF.h:462
uint16_t ModAmount
Definition: SF.h:82
Reflects the current playback state for a sample.
Definition: SF.h:277
Sample * pSample
Definition: SF.h:371
int coarseTune
Definition: SF.h:357
RIFF List Chunk.
Definition: RIFF.h:280
unsigned long loop_cycles_left
How many times the loop has still to be passed, this value will be decremented with each loop cycle...
Definition: SF.h:281
RIFF::Chunk * pCkSmpl
Definition: SF.h:326
GenAmountType GenAmount
Definition: SF.h:105
uint32_t EndLoop
Definition: SF.h:335
SFGenerator ModDestOper
Definition: SF.h:217
bool Direction
Definition: SF.h:207
String GetName()
Definition: SF.h:300
Exception(String Message)
Definition: SF.h:229
int loKey
Definition: SF.h:353
Version * pRomVer
[<iver-ck>] ; Refers to the Sound ROM Version
Definition: SF.h:246
uint8_t PitchCorrection
Definition: SF.h:338
sf2::File * GetFile()
Definition: SF.h:455
SFGeneratorType
Definition: SF.h:113
uint exclusiveClass
Definition: SF.h:369
uint32_t Genre
Definition: SF.h:501
int fineTune
Definition: SF.h:356
sf2::File * pFile
Definition: SF.h:508
int startAddrsOffset
Definition: SF.h:359
union sf2::_GenAmountType GenAmountType
uint8_t byLo
Definition: SF.h:92
uint16_t InstModNdx
Definition: SF.h:110
std::vector< GenList > PresetGenLists
Definition: SF.h:544
std::vector< InstBag > InstBags
Definition: SF.h:545
std::vector< ModulatorItem > modulators
Definition: SF.h:389
bool IsUnpitched()
Definition: SF.h:305
Ordinary RIFF Chunk.
Definition: RIFF.h:183
std::vector< ModList > PresetModLists
Definition: SF.h:543
int minVel
Definition: SF.h:354
int vibLfoToPitch
Definition: SF.h:365
uint8_t OriginalPitch
Definition: SF.h:337
uint16_t ModAmount
Definition: SF.h:218
uint16_t SFTransform
Definition: SF.h:77
uint LoopStart
Definition: SF.h:373
String Name
Definition: SF.h:449
void * pStart
Points to the beginning of the buffer.
Definition: SF.h:286
std::vector< ModList > InstModLists
Definition: SF.h:546
uint8_t vel
Definition: SF.h:469
Version * pVer
<ifil-ck> ; Refers to the version of the Sound Font RIFF file
Definition: SF.h:242
uint16_t SFModulator
Definition: SF.h:75
uint16_t Bank
Definition: SF.h:499
struct sf2::_PresetBag PresetBag
buffer_t RAMCache
Buffers samples (already uncompressed) in RAM.
Definition: SF.h:325
int modLfoToPitch
Definition: SF.h:362
short shAmount
Definition: SF.h:99
int Minor
Definition: SF.h:235
uint32_t StartLoop
Definition: SF.h:334
Pointer address and size of a buffer.
Definition: SF.h:285
uint16_t SampleType
Definition: SF.h:342
uint16_t SampleLink
Definition: SF.h:339
String CreationDate
[<ICRD-ck>] ; Refers to the Date of Creation of the Bank
Definition: SF.h:247
uint16_t SFGenerator
Definition: SF.h:76
uint32_t Morphology
Definition: SF.h:502
RIFF File.
Definition: RIFF.h:328
struct sf2::_GenList GenList
RIFF specific classes and definitions.
Definition: RIFF.h:134
uint16_t wAmount
Definition: SF.h:100
Modulator ModSrcOper
Definition: SF.h:216
int pan
Definition: SF.h:355
Modulator ModAmtSrcOper
Definition: SF.h:219
double ToSeconds(int Timecents)
Definition: SF.cpp:35
bool HasLoop
Definition: SF.h:372
int ChannelCount
Definition: SF.h:330
Instrument * GetParentInstrument()
Definition: SF.h:387
SFModulator ModAmtSrcOper
Definition: SF.h:83
int overridingRootKey
Definition: SF.h:358
Will be thrown whenever an error occurs while handling a RIFF file.
Definition: RIFF.h:383
String SoundEngine
<isng-ck> ; Refers to the target Sound Engine
Definition: SF.h:243
String Engineers
[<IENG-ck>] ; Sound Designers and Engineers for the Bank
Definition: SF.h:248
std::vector< PresetBag > PresetBags
Definition: SF.h:542
int startloopAddrsOffset
Definition: SF.h:360
Sample * GetSample()
Definition: SF.h:378
Instrument * pInstrument
Definition: SF.h:375
Definition: SF.h:131
unsigned long Size
Size of the actual data in the buffer in bytes.
Definition: SF.h:287
SFTransform ModTransOper
Definition: SF.h:84
RIFF::Chunk * pCkSm24
Definition: SF.h:327
bool Polarity
Definition: SF.h:208
std::vector< GenList > InstGenLists
Definition: SF.h:547
bool reverse
If playback direction is currently backwards (in case there is a pingpong or reverse loop defined)...
Definition: SF.h:280
String BankName
<INAM-ck> ; Refers to the Sound Font Bank Name
Definition: SF.h:244
uint16_t InstBagNdx
Definition: SF.h:486
RIFF::File * pRIFF
Definition: SF.h:541