/[svn]/linuxsampler/trunk/src/engines/sfz/sfz.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/sfz/sfz.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2027 - (show annotations) (download) (as text)
Tue Nov 3 19:27:42 2009 UTC (14 years, 4 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 16122 byte(s)
* sfz engine: support for exclusive groups
* sf2 engine: support for exclusive groups
* sf2 engine: manage presets only
* sf2 engine: preset regions are now taken into account

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2008-2009 Anders Dahnielson <anders@dahnielson.com> *
6 * Copyright (C) 2009 Grigor Iliev *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21 * MA 02111-1307 USA *
22 ***************************************************************************/
23
24 #ifndef LIBSFZ_SFZ_H
25 #define LIBSFZ_SFZ_H
26
27 #include <fstream>
28 #include <iostream>
29 #include <vector>
30 #include <string>
31 #include <stdexcept>
32
33 #include "../common/SampleFile.h"
34 #include "../common/SampleManager.h"
35
36 #define TRIGGER_ATTACK ((unsigned char) (1 << 0)) // 0x01
37 #define TRIGGER_RELEASE ((unsigned char) (1 << 1)) // 0x02
38 #define TRIGGER_FIRST ((unsigned char) (1 << 2)) // 0x04
39 #define TRIGGER_LEGATO ((unsigned char) (1 << 3)) // 0x08
40
41 namespace sfz
42 {
43 // Forward declarations
44 class Articulation;
45 class Region;
46 class Group;
47 class Instrument;
48 class File;
49
50 class Sample : public LinuxSampler::SampleFileBase<Region> {
51 public:
52 Sample(String File, bool DontClose = false) : LinuxSampler::SampleFileBase<Region>(File, DontClose) { }
53 virtual ~Sample() { }
54 };
55
56 // Enumerations
57 enum sw_vel_t { VEL_CURRENT, VEL_PREVIOUS };
58 enum off_mode_t { OFF_FAST, OFF_NORMAL };
59 enum loop_mode_t { NO_LOOP, ONE_SHOT, LOOP_CONTINUOUS, LOOP_SUSTAIN };
60 enum curve_t { GAIN, POWER };
61 enum filter_t { LPF_1P, HPF_1P, BPF_1P, BRF_1P, APF_1P,
62 LPF_2P, HPF_2P, BPF_2P, BRF_2P, PKF_2P,
63 LPF_4P, HPF_4P,
64 LPF_6P, HPF_6P };
65
66 typedef unsigned char trigger_t;
67 typedef unsigned char uint8_t;
68
69 class SampleManager : public LinuxSampler::SampleManager<Sample, Region> {
70 public:
71 Sample* FindSample(std::string samplePath);
72
73 protected:
74 virtual void OnSampleInUse(Sample* pSample) {
75 pSample->Open();
76 }
77
78 virtual void OnSampleInNotUse(Sample* pSample) {
79 pSample->Close();
80 }
81 };
82
83 /////////////////////////////////////////////////////////////
84 // class Exception
85
86 class Exception :
87 public std::runtime_error
88 {
89 public:
90 Exception(const std::string& msg) :
91 runtime_error(msg)
92 {
93 }
94
95 std::string Message()
96 {
97 return what();
98 }
99
100 void PrintMessage()
101 {
102 std::cerr << what() << std::endl << std::flush;
103 }
104 };
105
106 /////////////////////////////////////////////////////////////
107 // class optional
108
109 // Handy class nicked from LinuxSampler...
110 // Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck
111 // Copyright (C) 2005, 2006 Christian Schoenebeck
112
113 class optional_base
114 {
115 public:
116 class nothing_t { public: nothing_t() {} };
117 static const nothing_t nothing;
118 };
119
120 template<class T>
121 class optional :
122 public optional_base
123 {
124 public:
125 optional()
126 {
127 initialized = false;
128 }
129
130 optional(T data)
131 {
132 this->data = data;
133 initialized = true;
134 }
135
136 optional(nothing_t)
137 {
138 initialized = false;
139 }
140
141 template <class T_inner>
142 optional(T_inner data)
143 {
144 this->data = T(data);
145 initialized = true;
146 }
147
148 const T& get() const throw (Exception)
149 {
150 if (!initialized) throw Exception("optional variable not initialized");
151 return data;
152 }
153
154 T& get() throw (Exception)
155 {
156 if (!initialized) throw Exception("optional variable not initialized");
157 return data;
158 }
159
160 void unset()
161 {
162 initialized = false;
163 }
164
165 optional& operator =(const optional& arg) throw (Exception)
166 {
167 if (!arg.initialized) {
168 initialized = false;
169 } else {
170 this->data = arg.data;
171 initialized = true;
172 }
173 return *this;
174 }
175
176 optional& operator =(const T& arg)
177 {
178 this->data = arg;
179 initialized = true;
180 return *this;
181 }
182
183 const T& operator *() const throw (Exception) { return get(); }
184 T& operator *() throw (Exception) { return get(); }
185
186 const T* operator ->() const throw (Exception)
187 {
188 if (!initialized) throw Exception("optional variable not initialized");
189 return &data;
190 }
191
192 T* operator ->() throw (Exception)
193 {
194 if (!initialized) throw Exception("optional variable not initialized");
195 return &data;
196 }
197
198 operator bool() const { return initialized; }
199 bool operator !() const { return !initialized; }
200
201 protected:
202 T data;
203 bool initialized;
204 };
205
206 /////////////////////////////////////////////////////////////
207 // class Articulation
208
209 // Articulation containing all performance parameters for synthesis
210 class Articulation
211 {
212 public:
213 Articulation();
214 virtual ~Articulation();
215 };
216
217 /////////////////////////////////////////////////////////////
218 // class Definition
219
220 // Base definition used by groups and regions
221 class Definition
222 {
223 public:
224 Definition();
225 virtual ~Definition();
226
227 // sample definition
228 std::string sample;
229
230 // input controls
231 int lochan; int hichan;
232 int lokey; int hikey;
233 int lovel; int hivel;
234 std::vector<int> locc; std::vector<int> hicc;
235 int lobend; int hibend;
236 int lobpm; int hibpm;
237 int lochanaft; int hichanaft;
238 int lopolyaft; int hipolyaft;
239 int loprog; int hiprog;
240 float lorand; float hirand;
241 float lotimer; float hitimer;
242
243 int seq_length;
244 int seq_position;
245
246 std::vector<int> start_locc; std::vector<int> start_hicc;
247 std::vector<int> stop_locc; std::vector<int> stop_hicc;
248
249 int sw_lokey; int sw_hikey;
250 int sw_last;
251 int sw_down;
252 int sw_up;
253 int sw_previous;
254 sw_vel_t sw_vel;
255
256 trigger_t trigger;
257
258 uint group;
259 optional<int> off_by;
260 off_mode_t off_mode;
261
262 std::vector<int> on_locc; std::vector<int> on_hicc;
263
264 // sample player
265 optional<int> count;
266 optional<float> delay; optional<float> delay_random; std::vector<optional<float> > delay_oncc;
267 optional<int> delay_beats; optional<int> stop_beats;
268 optional<int> delay_samples; std::vector<optional<int> > delay_samples_oncc;
269 optional<int> end;
270 optional<float> loop_crossfade;
271 optional<int> offset; optional<int> offset_random; std::vector<optional<int> > offset_oncc;
272 loop_mode_t loop_mode;
273 optional<int> loop_start; optional<int> loop_end;
274 optional<int> sync_beats;
275 optional<int> sync_offset;
276
277 // amplifier
278 float volume;
279 float pan;
280 float width;
281 float position;
282 float amp_keytrack; int amp_keycenter; float amp_veltrack; std::vector<float> amp_velcurve_; float amp_random;
283 float rt_decay;
284 std::vector<float> gain_oncc;
285 int xfin_lokey; int xfin_hikey;
286 int xfout_lokey; int xfout_hikey;
287 curve_t xf_keycurve;
288 int xfin_lovel; int xfin_hivel;
289 int xfout_lovel; int xfout_hivel;
290 curve_t xf_velcurve;
291 std::vector<int> xfin_locc; std::vector<int> xfin_hicc;
292 std::vector<int> xfout_locc; std::vector<int> xfout_hicc;
293 curve_t xf_cccurve;
294
295 // pitch
296 int transpose;
297 int tune;
298 int pitch_keycenter; int pitch_keytrack; int pitch_veltrack; int pitch_random;
299 int bend_up; int bend_down; int bend_step;
300
301 // filter
302 filter_t fil_type; filter_t fil2_type;
303 optional<float> cutoff; optional<float> cutoff2;
304 std::vector<int> cutoff_oncc; std::vector<int> cutoff2_oncc;
305 std::vector<int> cutoff_smoothcc; std::vector<int> cutoff2_smoothcc;
306 std::vector<int> cutoff_stepcc; std::vector<int> cutoff2_stepcc;
307 std::vector<int> cutoff_curvecc; std::vector<int> cutoff2_curvecc;
308 int cutoff_chanaft; int cutoff2_chanaft;
309 int cutoff_polyaft; int cutoff2_polyaft;
310 float resonance; float resonance2;
311 std::vector<int> resonance_oncc; std::vector<int> resonance2_oncc;
312 std::vector<int> resonance_smoothcc; std::vector<int> resonance2_smoothcc;
313 std::vector<int> resonance_stepcc; std::vector<int> resonance2_stepcc;
314 std::vector<int> resonance_curvecc; std::vector<int> resonance2_curvecc;
315 int fil_keytrack; int fil2_keytrack;
316 int fil_keycenter; int fil2_keycenter;
317 int fil_veltrack; int fil2_veltrack;
318 int fil_random; int fil2_random;
319
320 // per voice equalizer
321 float eq1_freq; float eq2_freq; float eq3_freq;
322 std::vector<float> eq1_freq_oncc; std::vector<float> eq2_freq_oncc; std::vector<float> eq3_freq_oncc;
323 float eq1_vel2freq; float eq2_vel2freq; float eq3_vel2freq;
324 float eq1_bw; float eq2_bw; float eq3_bw;
325 std::vector<float> eq1_bw_oncc; std::vector<float> eq2_bw_oncc; std::vector<float> eq3_bw_oncc;
326 float eq1_gain; float eq2_gain; float eq3_gain;
327 std::vector<float> eq1_gain_oncc; std::vector<float> eq2_gain_oncc; std::vector<float> eq3_gain_oncc;
328 float eq1_vel2gain; float eq2_vel2gain; float eq3_vel2gain;
329
330 //Deprecated (from version 1)
331 float ampeg_delay, ampeg_start, ampeg_attack, ampeg_hold, ampeg_decay, ampeg_sustain, ampeg_release;
332 float fileg_delay, fileg_start, fileg_attack, fileg_hold, fileg_decay, fileg_sustain, fileg_release;
333 float pitcheg_delay, pitcheg_start, pitcheg_attack, pitcheg_hold, pitcheg_decay, pitcheg_sustain, pitcheg_release;
334 };
335
336 /////////////////////////////////////////////////////////////
337 // class Region
338
339 /// Defines Region information of an Instrument
340 class Region :
341 public Definition
342 {
343 public:
344 Region();
345 virtual ~Region();
346
347 Sample* pSample;
348 Sample* GetSample(bool create = true);
349 void DestroySampleIfNotUsed();
350
351 Region* GetParent() { return this; }; // needed by EngineBase
352 Instrument* GetInstrument() { return pInstrument; }
353 void SetInstrument(Instrument* pInstrument) { this->pInstrument = pInstrument; }
354
355 bool HasLoop();
356 uint GetLoopStart();
357 uint GetLoopEnd();
358 uint GetLoopCount();
359
360 /// Return true if region is triggered by key
361 bool OnKey(uint8_t chan, uint8_t key, uint8_t vel,
362 int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft,
363 uint8_t prog, float rand, trigger_t trig, uint8_t* cc,
364 float timer, uint8_t seq, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key);
365
366 /// Return true if region is triggered by control change
367 bool OnControl(uint8_t chan, uint8_t cont, uint8_t val,
368 int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft,
369 uint8_t prog, float rand, trigger_t trig, uint8_t* cc,
370 float timer, uint8_t seq, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key);
371
372 /// Return an articulation for the current state
373 Articulation* GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc);
374
375 // unique region id
376 int id;
377
378 private:
379 Instrument* pInstrument;
380 };
381
382 /////////////////////////////////////////////////////////////
383 // class Instrument
384
385 /// Provides all neccessary information for the synthesis of an Instrument
386 class Instrument : public SampleManager
387 {
388 public:
389 Instrument(std::string name = "Unknown", SampleManager* pSampleManager = NULL);
390 virtual ~Instrument();
391
392 std::string GetName() { return name; }
393 SampleManager* GetSampleManager() { return pSampleManager; }
394
395 /**
396 * @returns All regions that are triggered by key
397 */
398 std::vector<Region*> GetRegionsOnKey (
399 uint8_t chan, uint8_t key, uint8_t vel,
400 int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft,
401 uint8_t prog, float rand, trigger_t trig, uint8_t* cc,
402 float timer, uint8_t seq, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key
403 );
404
405 bool DestroyRegion(Region* pRegion);
406 bool HasKeyBinding(uint8_t key);
407 bool HasKeySwitchBinding(uint8_t key);
408
409 /// List of Regions belonging to this Instrument
410 std::vector<Region*> regions;
411
412 friend class sfz::File;
413
414 private:
415 std::string name;
416 std::vector<bool> KeyBindings;
417 std::vector<bool> KeySwitchBindings;
418 SampleManager* pSampleManager;
419 };
420
421 /////////////////////////////////////////////////////////////
422 // class Group
423
424 /// A Group act just as a template containing Region default values
425 class Group :
426 public Definition
427 {
428 public:
429 Group();
430 virtual ~Group();
431
432 /// Reset Group to default values
433 void Reset();
434
435 /// Create a new Region
436 Region* RegionFactory();
437
438 // id counter
439 int id;
440
441 };
442
443 /////////////////////////////////////////////////////////////
444 // class File
445
446 /// Parses SFZ files and provides abstract access to the data
447 class File
448 {
449 public:
450 /// Load an existing SFZ file
451 File(std::string file, SampleManager* pSampleManager = NULL);
452 virtual ~File();
453
454 /// Returns a pointer to the instrument object
455 Instrument* GetInstrument();
456
457 private:
458 void push_header(std::string token);
459 void push_opcode(std::string token);
460 int parseInt(std::string value);
461 float parseFloat(std::string value);
462
463 std::string currentDir;
464 /// Pointer to the Instrument belonging to this file
465 Instrument* _instrument;
466
467 // state variables
468 enum section_t { UNKNOWN, GROUP, REGION, CONTROL };
469 section_t _current_section;
470 Region* _current_region;
471 Group* _current_group;
472 Definition* pCurDef;
473
474 // control header directives
475 std::string default_path;
476 int octave_offset;
477 int note_offset;
478 };
479
480 } // !namespace sfz
481
482 #endif // !LIBSFZ_SFZ_H

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC