/[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 2058 - (show annotations) (download) (as text)
Sun Feb 14 11:40:49 2010 UTC (10 years, 1 month ago) by persson
File MIME type: text/x-c++hdr
File size: 16599 byte(s)
* sfz/sf2 engines: fixed memory leak and memory handling errors
* sfz engine: added support for sw_trigger=first, sw_trigger=legato
  and sw_previous
* sfz parser: allow non-numerical key values ("C#4" for example)
* sfz engine: "key" opcode now sets pitch_keycenter too
* sfz engine: fixed error when unloading instrument with same sample
  used by multiple regions
* sfz parser: added some opcode aliases, like loopmode for loop_mode,
  to be more compatible

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC