ScummVM API documentation
sf2file.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program 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 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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 program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 /*
22  * VGMTrans (c) 2002-2019
23  * Licensed under the zlib license,
24  * refer to the included VGMTrans_LICENSE.txt file
25  */
26 #ifndef AUDIO_SOUNDFONT_SF2FILE_H
27 #define AUDIO_SOUNDFONT_SF2FILE_H
28 
29 #include "common/endian.h"
30 #include "common/scummsys.h"
31 #include "common/array.h"
32 #include "common/str.h"
33 #include "rifffile.h"
34 
35 typedef enum {
36  // Oscillator
37  startAddrsOffset, // sample start address -4 (0 to 0xffffff) 0
38  endAddrsOffset,
39  startloopAddrsOffset, // loop start address -4 (0 to 0xffffff)
40  endloopAddrsOffset, // loop end address -3 (0 to 0xffffff)
41 
42  // Pitch
43  startAddrsCoarseOffset, // CHANGED FOR SF2
44  modLfoToPitch, // main fm: lfo1-> pitch 5
45  vibLfoToPitch, // aux fm: lfo2-> pitch
46  modEnvToPitch, // pitch env: env1(aux)-> pitch
47 
48  // Filter
49  initialFilterFc, // initial filter cutoff
50  initialFilterQ, // filter Q
51  modLfoToFilterFc, // filter modulation: lfo1 -> filter cutoff 10
52  modEnvToFilterFc, // filter env: env1(aux)-> filter cutoff
53 
54  // Amplifier
55  endAddrsCoarseOffset, // CHANGED FOR SF2
56  modLfoToVolume, // tremolo: lfo1-> volume
57  unused1,
58 
59  // Effects
60  chorusEffectsSend, // chorus 15
61  reverbEffectsSend, // reverb
62  pan,
63  unused2,
64  unused3,
65  unused4, // 20
66 
67  // Main lfo1
68  delayModLFO, // delay 0x8000-n*(725us)
69  freqModLFO, // frequency
70 
71  // Aux lfo2
72  delayVibLFO, // delay 0x8000-n*(725us)
73  freqVibLFO, // frequency
74 
75  // Env1(aux/value)
76  delayModEnv, // delay 0x8000 - n(725us) 25
77  attackModEnv, // attack
78  holdModEnv, // hold
79  decayModEnv, // decay
80  sustainModEnv, // sustain
81  releaseModEnv, // release 30
82  keynumToModEnvHold,
83  keynumToModEnvDecay,
84 
85  // Env2(ampl/vol)
86  delayVolEnv, // delay 0x8000 - n(725us)
87  attackVolEnv, // attack
88  holdVolEnv, // hold 35
89  decayVolEnv, // decay
90  sustainVolEnv, // sustain
91  releaseVolEnv, // release
92  keynumToVolEnvHold,
93  keynumToVolEnvDecay, // 40
94 
95  // Preset
96  instrument,
97  reserved1,
98  keyRange,
99  velRange,
100  startloopAddrCoarseOffset, // CHANGED FOR SF2 45
101  keynum,
102  velocity,
103  initialAttenuation, // CHANGED FOR SF2
104  reserved2,
105  endloopAddrsCoarseOffset, // CHANGED FOR SF2 50
106  coarseTune,
107  fineTune,
108  sampleID,
109  sampleModes, // CHANGED FOR SF2
110  reserved3, // 55
111  scaleTuning,
112  exclusiveClass,
113  overridingRootKey,
114  unused5,
115  endOper // 60
116 } SFGeneratorType;
117 
118 typedef uint16 SFGenerator;
119 
120 typedef enum {
121  /* Start of MIDI modulation operators */
122  cc1_Mod,
123  cc7_Vol,
124  cc10_Pan,
125  cc64_Sustain,
126  cc91_Reverb,
127  cc93_Chorus,
128 
129  ccPitchBend,
130  ccIndirectModX,
131  ccIndirectModY,
132 
133  endMod
134 } SFModulatorType;
135 
136 typedef uint16 SFModulator;
137 
138 typedef enum {
139  linear
140 } SFTransformType;
141 
142 typedef uint16 SFTransform;
143 /*
144 #define monoSample 0x0001
145 #define rightSample 0x0002
146 #define leftSample 0x0004
147 #define linkedSample 0x0008
148 
149 #define ROMSample 0x8000 //32768
150 #define ROMMonoSample 0x8001 //32769
151 #define ROMRightSample 0x8002 //32770
152 #define ROMLeftSample 0x8004 //32772
153 #define ROMLinkedSample 0x8008 //32776
154 */
155 
156 // enum scaleTuning
157 //{
158 // equalTemp,
159 // fiftyCents
160 //};
161 //
162 // enum SFSampleField //used by Sample Read Module
163 //{
164 // NAME_FIELD = 1,
165 // START_FIELD,
166 // END_FIELD,
167 // START_LOOP_FIELD,
168 // END_LOOP_FIELD,
169 // SMPL_RATE_FIELD,
170 // ORG_KEY_FIELD,
171 // CORRECTION_FIELD,
172 // SMPL_LINK_FIELD,
173 // SMPL_TYPE_FIELD
174 //};
175 //
176 // enum SFInfoChunkField //used by Bank Read Module
177 //{
178 // IFIL_FIELD = 1,
179 // IROM_FIELD,
180 // IVER_FIELD,
181 // ISNG_FIELD,
182 // INAM_FIELD,
183 // IPRD_FIELD,
184 // IENG_FIELD,
185 // ISFT_FIELD,
186 // ICRD_FIELD,
187 // ICMT_FIELD,
188 // ICOP_FIELD
189 //};
190 
191 static inline void uint16Write(uint8 **buffer, uint16 value) {
192  WRITE_LE_UINT16(*buffer, value);
193  *buffer += 2;
194 }
195 
196 static inline void uint32Write(uint8 **buffer, uint32 value) {
197  WRITE_LE_UINT32(*buffer, value);
198  *buffer += 4;
199 }
200 
201 static inline void writeBytes(uint8 **buffer, byte *bytes, uint32 size) {
202  memcpy(*buffer, bytes, size);
203  *buffer += size;
204 }
205 
206 #pragma pack(push) /* push current alignment to stack */
207 #pragma pack(2) /* set alignment to 2 byte boundary */
208 
209 struct sfVersionTag {
210  uint16 wMajor;
211  uint16 wMinor;
212 
213  uint8 *write(uint8 *buffer) {
214  uint16Write(&buffer, wMajor);
215  uint16Write(&buffer, wMinor);
216  return buffer;
217  }
218 };
219 
221  char achPresetName[20];
222  uint16 wPreset;
223  uint16 wBank;
224  uint16 wPresetBagNdx;
225  uint32 dwLibrary;
226  uint32 dwGenre;
227  uint32 dwMorphology;
228 
229  uint8 *write(uint8 *buffer) {
230  writeBytes(&buffer, (byte *)achPresetName, 20);
231  uint16Write(&buffer, wPreset);
232  uint16Write(&buffer, wBank);
233  uint16Write(&buffer, wPresetBagNdx);
234  uint32Write(&buffer, dwLibrary);
235  uint32Write(&buffer, dwGenre);
236  uint32Write(&buffer, dwMorphology);
237  return buffer;
238  }
239 };
240 
241 struct sfPresetBag {
242  uint16 wGenNdx;
243  uint16 wModNdx;
244 
245  uint8 *write(uint8 *buffer) {
246  uint16Write(&buffer, wGenNdx);
247  uint16Write(&buffer, wModNdx);
248  return buffer;
249  }
250 };
251 
252 struct sfModList {
253  SFModulator sfModSrcOper;
254  SFGenerator sfModDestOper;
255  int16 modAmount;
256  SFModulator sfModAmtSrcOper;
257  SFTransform sfModTransOper;
258 
259  uint8 *write(uint8 *buffer) {
260  uint16Write(&buffer, sfModSrcOper);
261  uint16Write(&buffer, sfModDestOper);
262  uint16Write(&buffer, modAmount);
263  uint16Write(&buffer, sfModAmtSrcOper);
264  uint16Write(&buffer, sfModTransOper);
265  return buffer;
266  }
267 };
268 
270  byte data[2];
271 
272  void setRangeLo(uint8 lo) { data[0] = lo; }
273  void setRangeHi(uint8 hi) { data[1] = hi; }
274  void setShAmount(int16 shAmount) { WRITE_LE_INT16(data, shAmount); }
275  void setwAmount(int16 wAmount) { WRITE_LE_UINT16(data, wAmount); }
276 
277  uint8 *write(uint8 *buffer) {
278  writeBytes(&buffer, (byte *)data, 2);
279  return buffer;
280  }
281 };
282 
283 struct sfGenList {
284  SFGenerator sfGenOper;
285  genAmountType genAmount;
286 
287  uint8 *write(uint8 *buffer) {
288  uint16Write(&buffer, sfGenOper);
289  return genAmount.write(buffer);
290  }
291 };
292 
294  SFGenerator sfGenOper;
295  genAmountType genAmount;
296 
297  uint8 *write(uint8 *buffer) {
298  uint16Write(&buffer, sfGenOper);
299  return genAmount.write(buffer);
300  }
301 };
302 
303 struct sfInst {
304  char achInstName[20];
305  uint16 wInstBagNdx;
306 
307  uint8 *write(uint8 *buffer) {
308  writeBytes(&buffer, (byte *)achInstName, 20);
309  uint16Write(&buffer, wInstBagNdx);
310  return buffer;
311  }
312 };
313 
314 struct sfInstBag {
315  uint16 wInstGenNdx;
316  uint16 wInstModNdx;
317 
318  uint8 *write(uint8 *buffer) {
319  uint16Write(&buffer, wInstGenNdx);
320  uint16Write(&buffer, wInstModNdx);
321  return buffer;
322  }
323 };
324 
325 typedef enum {
326  monoSample = 1,
327  rightSample = 2,
328  leftSample = 4,
329  linkedSample = 8,
330  RomMonoSample = 0x8001,
331  RomRightSample = 0x8002,
332  RomLeftSample = 0x8004,
333  RomLinkedSample = 0x8008
334 } SFSampleLinkType;
335 
336 typedef uint16 SFSampleLink;
337 
338 struct sfSample {
339  char achSampleName[20];
340  uint32 dwStart;
341  uint32 dwEnd;
342  uint32 dwStartloop;
343  uint32 dwEndloop;
344  uint32 dwSampleRate;
345  uint8 byOriginalKey;
346  char chCorrection;
347  uint16 wSampleLink;
348  SFSampleLink sfSampleType;
349 
350  uint8 *write(uint8 *buffer) {
351  writeBytes(&buffer, (byte *)achSampleName, 20);
352  uint32Write(&buffer, dwStart);
353  uint32Write(&buffer, dwEnd);
354  uint32Write(&buffer, dwStartloop);
355  uint32Write(&buffer, dwEndloop);
356  uint32Write(&buffer, dwSampleRate);
357  *buffer++ = byOriginalKey;
358  *buffer++ = chCorrection;
359  uint16Write(&buffer, wSampleLink);
360  uint16Write(&buffer, sfSampleType);
361  return buffer;
362  }
363 };
364 
365 #pragma pack(pop) /* restore original alignment from stack */
366 
367 class SF2StringChunk : public Chunk {
368 public:
369  SF2StringChunk(Common::String ckSig, Common::String info) : Chunk(ckSig) {
370  SetData(info.c_str(), (uint32) info.size());
371  }
372 };
373 
374 class SF2InfoListChunk : public LISTChunk {
375 public:
377 };
378 
379 class SF2sdtaChunk : public LISTChunk {
380 public:
381  SF2sdtaChunk();
382 };
383 
384 inline void WriteLIST(Common::Array<uint8> &buf, Common::String listName, uint32 listSize);
385 inline void AlignName(Common::String &name);
386 
387 class SynthFile;
388 
389 class SF2File : public RiffFile {
390 public:
391  SF2File(SynthFile *synthfile);
392  ~SF2File(void);
393 
394  const void *SaveToMem();
395 };
396 
397 #endif // AUDIO_SOUNDFONT_SF2FILE_H
Definition: str.h:59
Definition: sf2file.h:241
Definition: synthfile.h:48
Definition: rifffile.h:109
Definition: sf2file.h:209
Definition: sf2file.h:293
Definition: sf2file.h:269
Definition: sf2file.h:252
Definition: rifffile.h:37
Definition: sf2file.h:220
Definition: sf2file.h:389
Definition: sf2file.h:283
Definition: sf2file.h:367
Definition: sf2file.h:338
Definition: sf2file.h:303
Definition: sf2file.h:379
Definition: rifffile.h:101
Definition: sf2file.h:314
Definition: sf2file.h:374