/[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 2233 - (show annotations) (download) (as text)
Mon Aug 8 18:46:19 2011 UTC (8 years, 6 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 23158 byte(s)
* sfz engine: implemented opcodes fillfo_depthccN, amplfo_depthccN,
  lfoN_volume, lfoN_volume_onccX, lfoN_volume_smoothccX,
  lfoN_freq_smoothccX, lfoN_pitch_smoothccX, lfoN_pan_onccX,
  lfoN_pan_smoothccX, lfoN_cutoff_onccX, lfoN_cutoff_smoothccX,
  lfoN_resonance_onccX, lfoN_resonance_smoothccX, lfoN_delay_onccX

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2008 Anders Dahnielson <anders@dahnielson.com> *
6 * Copyright (C) 2009 - 2011 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 class LookupTable;
51
52 class Sample : public LinuxSampler::SampleFileBase<Region> {
53 public:
54 Sample(String File, bool DontClose = false, uint offset = 0): LinuxSampler::SampleFileBase<Region>(File, DontClose) {
55 Offset = offset;
56 }
57 virtual ~Sample() { }
58 };
59
60 // Enumerations
61 enum sw_vel_t { VEL_CURRENT, VEL_PREVIOUS };
62 enum off_mode_t { OFF_FAST, OFF_NORMAL };
63 enum loop_mode_t { NO_LOOP, ONE_SHOT, LOOP_CONTINUOUS, LOOP_SUSTAIN, LOOP_UNSET };
64 enum curve_t { GAIN, POWER };
65 enum filter_t { LPF_1P, HPF_1P, BPF_1P, BRF_1P, APF_1P,
66 LPF_2P, HPF_2P, BPF_2P, BRF_2P, PKF_2P,
67 LPF_4P, HPF_4P,
68 LPF_6P, HPF_6P };
69
70 typedef unsigned char trigger_t;
71 typedef unsigned char uint8_t;
72
73 class SampleManager : public LinuxSampler::SampleManager<Sample, Region> {
74 public:
75 Sample* FindSample(std::string samplePath, int offset);
76
77 protected:
78 virtual void OnSampleInUse(Sample* pSample) {
79 pSample->Open();
80 }
81
82 virtual void OnSampleInNotUse(Sample* pSample) {
83 pSample->Close();
84 }
85 };
86
87 class CC {
88 public:
89 uint8_t Controller; ///< MIDI controller number.
90 short int Curve;
91 float Influence; ///< Controller Value.
92 float Smooth; ///< The speed of parameter change in milliseconds
93
94 CC(uint8_t Controller = 0, float Influence = 0.0f, short int Curve = -1, float Smooth = 0) {
95 this->Controller = Controller;
96 this->Influence = Influence;
97 this->Curve = Curve;
98 this->Smooth = Smooth;
99 }
100
101 CC(const CC& cc) { Copy(cc); }
102 void operator=(const CC& cc) { Copy(cc); }
103
104 void Copy(const CC& cc) {
105 Controller = cc.Controller;
106 Influence = cc.Influence;
107 Curve = cc.Curve;
108 Smooth = cc.Smooth;
109 }
110 };
111
112 /////////////////////////////////////////////////////////////
113 // class Exception
114
115 class Exception :
116 public std::runtime_error
117 {
118 public:
119 Exception(const std::string& msg) :
120 runtime_error(msg)
121 {
122 }
123
124 std::string Message()
125 {
126 return what();
127 }
128
129 void PrintMessage()
130 {
131 std::cerr << what() << std::endl << std::flush;
132 }
133 };
134
135 /////////////////////////////////////////////////////////////
136 // class optional
137
138 // Handy class nicked from LinuxSampler...
139 // Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck
140 // Copyright (C) 2005, 2006 Christian Schoenebeck
141
142 class optional_base
143 {
144 public:
145 class nothing_t { public: nothing_t() {} };
146 static const nothing_t nothing;
147 };
148
149 template<class T>
150 class optional :
151 public optional_base
152 {
153 public:
154 optional()
155 {
156 initialized = false;
157 }
158
159 optional(T data)
160 {
161 this->data = data;
162 initialized = true;
163 }
164
165 optional(nothing_t)
166 {
167 initialized = false;
168 }
169
170 template <class T_inner>
171 optional(T_inner data)
172 {
173 this->data = T(data);
174 initialized = true;
175 }
176
177 const T& get() const throw (Exception)
178 {
179 if (!initialized) throw Exception("optional variable not initialized");
180 return data;
181 }
182
183 T& get() throw (Exception)
184 {
185 if (!initialized) throw Exception("optional variable not initialized");
186 return data;
187 }
188
189 void unset()
190 {
191 initialized = false;
192 }
193
194 optional& operator =(const optional& arg) throw (Exception)
195 {
196 if (!arg.initialized) {
197 initialized = false;
198 } else {
199 this->data = arg.data;
200 initialized = true;
201 }
202 return *this;
203 }
204
205 optional& operator =(const T& arg)
206 {
207 this->data = arg;
208 initialized = true;
209 return *this;
210 }
211
212 const T& operator *() const throw (Exception) { return get(); }
213 T& operator *() throw (Exception) { return get(); }
214
215 const T* operator ->() const throw (Exception)
216 {
217 if (!initialized) throw Exception("optional variable not initialized");
218 return &data;
219 }
220
221 T* operator ->() throw (Exception)
222 {
223 if (!initialized) throw Exception("optional variable not initialized");
224 return &data;
225 }
226
227 operator bool() const { return initialized; }
228 bool operator !() const { return !initialized; }
229
230 protected:
231 T data;
232 bool initialized;
233 };
234
235 /////////////////////////////////////////////////////////////
236 // class Articulation
237
238 // Articulation containing all performance parameters for synthesis
239 class Articulation
240 {
241 public:
242 Articulation();
243 virtual ~Articulation();
244 };
245
246 class EGNode
247 {
248 public:
249 float time;
250 float level;
251 float shape;
252 float curve;
253 LinuxSampler::ArrayList<CC> time_oncc;
254 LinuxSampler::ArrayList<CC> level_oncc;
255
256 EGNode();
257 EGNode(const EGNode& egNode) { Copy(egNode); }
258 void operator=(const EGNode& egNode) { Copy(egNode); }
259 void Copy(const EGNode& egNode);
260 };
261
262 class EG
263 {
264 public:
265 LinuxSampler::ArrayList<EGNode> node;
266 int sustain;
267 int loop;
268 int loop_count;
269 float amplitude;
270 float cutoff;
271 EG();
272 EG(const EG& eg) { Copy(eg); }
273 void operator=(const EG& eg) { Copy(eg); }
274 void Copy(const EG& eg);
275 };
276
277 class LFO
278 {
279 public:
280 float delay; // 0 to 100 seconds
281 float freq; // 0 to 20 Hz
282 float fade; // 0 to 100 seconds
283 float phase; // 0 to 360 degrees
284 uint wave; // 0 to 4294967296
285 float volume; // -144 to 6 dB
286 int pitch; // -9600 to 9600 cents
287 int cutoff; // -9600 to 9600 cents
288 float resonance; // 0 to 40 dB
289 float pan; // -100 to 100 %
290
291 LinuxSampler::ArrayList<CC> delay_oncc; // 0 to 100 seconds
292 LinuxSampler::ArrayList<CC> freq_oncc; // 0 to 20 Hz
293 LinuxSampler::ArrayList<CC> freq_smoothcc; // 0 to ? milliseconds
294 LinuxSampler::ArrayList<CC> fade_oncc; // 0 to 100 seconds
295 LinuxSampler::ArrayList<CC> phase_oncc; // 0 to 360 degrees
296 LinuxSampler::ArrayList<CC> volume_oncc; // -144 to 6 dB
297 LinuxSampler::ArrayList<CC> volume_smoothcc; // 0 to ? milliseconds
298 LinuxSampler::ArrayList<CC> pitch_oncc;
299 LinuxSampler::ArrayList<CC> pitch_smoothcc; // 0 to ? milliseconds
300 LinuxSampler::ArrayList<CC> pan_oncc; // -100 to 100 %
301 LinuxSampler::ArrayList<CC> pan_smoothcc; // 0 to ? milliseconds
302 LinuxSampler::ArrayList<CC> cutoff_oncc; // -9600 to 9600 cents
303 LinuxSampler::ArrayList<CC> cutoff_smoothcc; // 0 to ? milliseconds
304 LinuxSampler::ArrayList<CC> resonance_oncc; // 0 to 40 dB
305 LinuxSampler::ArrayList<CC> resonance_smoothcc; // 0 to ? milliseconds
306
307 LFO();
308 LFO(const LFO& lfo) { Copy(lfo); }
309 void operator=(const LFO& lfo) { Copy(lfo); }
310 void Copy(const LFO& lfo);
311 };
312
313 // Fixed size array with copy-on-write semantics
314 template<class T>
315 class Array
316 {
317 private:
318 struct Rep {
319 int refcount;
320 T a[128];
321
322 Rep() : refcount(1) { }
323 static void release(Rep* rep) {
324 if (!--rep->refcount) delete rep;
325 }
326 } *ptr;
327 public:
328 Array() : ptr(0) { }
329 ~Array() { Rep::release(ptr); }
330
331 Array& operator=(const Array& array) {
332 if (this != &array) {
333 ptr = array.ptr;
334 if (ptr) ptr->refcount++;
335 }
336 return *this;
337 }
338
339 const T& operator[](int i) const { return ptr->a[i]; }
340
341 void set(int i, const T& v) {
342 if (!ptr) {
343 ptr = new Rep;
344 } else if (ptr->refcount > 1 && ptr->a[i] != v) {
345 Rep* newptr = new Rep(*ptr);
346 newptr->refcount = 1;
347 Rep::release(ptr);
348 ptr = newptr;
349 }
350 ptr->a[i] = v;
351 }
352 };
353
354 /////////////////////////////////////////////////////////////
355 // class Definition
356
357 // Base definition used by groups and regions
358 class Definition
359 {
360 public:
361 Definition();
362 virtual ~Definition();
363
364 // sample definition
365 std::string sample;
366
367 // input controls
368 int lochan; int hichan;
369 int lokey; int hikey;
370 int lovel; int hivel;
371 Array<int> locc; Array<int> hicc;
372 int lobend; int hibend;
373 float lobpm; float hibpm;
374 int lochanaft; int hichanaft;
375 int lopolyaft; int hipolyaft;
376 int loprog; int hiprog;
377 float lorand; float hirand;
378 float lotimer; float hitimer;
379
380 int seq_length;
381 int seq_position;
382
383 Array<int> start_locc; Array<int> start_hicc;
384 Array<int> stop_locc; Array<int> stop_hicc;
385
386 int sw_lokey; int sw_hikey;
387 int sw_last;
388 int sw_down;
389 int sw_up;
390 int sw_previous;
391 sw_vel_t sw_vel;
392
393 trigger_t trigger;
394
395 uint group;
396 uint off_by;
397 off_mode_t off_mode;
398
399 Array<int> on_locc; Array<int> on_hicc;
400
401 // sample player
402 optional<int> count;
403 optional<float> delay; optional<float> delay_random; Array<optional<float> > delay_oncc;
404 optional<int> delay_beats; optional<int> stop_beats;
405 optional<int> delay_samples; Array<optional<int> > delay_samples_oncc;
406 optional<int> end;
407 optional<float> loop_crossfade;
408 optional<int> offset; optional<int> offset_random; Array<optional<int> > offset_oncc;
409 loop_mode_t loop_mode;
410 optional<int> loop_start; optional<int> loop_end;
411 optional<int> sync_beats;
412 optional<int> sync_offset;
413
414 // amplifier
415 float volume;
416 float pan;
417 float width;
418 float position;
419 float amp_keytrack; int amp_keycenter; float amp_veltrack; Array<float> amp_velcurve; float amp_random;
420 float rt_decay;
421 Array<float> gain_oncc;
422 int xfin_lokey; int xfin_hikey;
423 int xfout_lokey; int xfout_hikey;
424 curve_t xf_keycurve;
425 int xfin_lovel; int xfin_hivel;
426 int xfout_lovel; int xfout_hivel;
427 curve_t xf_velcurve;
428 Array<int> xfin_locc; Array<int> xfin_hicc;
429 Array<int> xfout_locc; Array<int> xfout_hicc;
430 curve_t xf_cccurve;
431
432 // pitch
433 int transpose;
434 int tune;
435 int pitch_keycenter; int pitch_keytrack; int pitch_veltrack; int pitch_random;
436 int bend_up; int bend_down; int bend_step;
437
438 // filter
439 filter_t fil_type; filter_t fil2_type;
440 optional<float> cutoff; optional<float> cutoff2;
441 Array<int> cutoff_oncc; Array<int> cutoff2_oncc;
442 int cutoff_cc; // TODO: this is just a temporary fix to avoid
443 // looping through the cutoff_oncc array
444 Array<int> cutoff_smoothcc; Array<int> cutoff2_smoothcc;
445 Array<int> cutoff_stepcc; Array<int> cutoff2_stepcc;
446 Array<int> cutoff_curvecc; Array<int> cutoff2_curvecc;
447 int cutoff_chanaft; int cutoff2_chanaft;
448 int cutoff_polyaft; int cutoff2_polyaft;
449 float resonance; float resonance2;
450 Array<int> resonance_oncc; Array<int> resonance2_oncc;
451 Array<int> resonance_smoothcc; Array<int> resonance2_smoothcc;
452 Array<int> resonance_stepcc; Array<int> resonance2_stepcc;
453 Array<int> resonance_curvecc; Array<int> resonance2_curvecc;
454 int fil_keytrack; int fil2_keytrack;
455 int fil_keycenter; int fil2_keycenter;
456 int fil_veltrack; int fil2_veltrack;
457 int fil_random; int fil2_random;
458
459 // per voice equalizer
460 float eq1_freq; float eq2_freq; float eq3_freq;
461 Array<float> eq1_freq_oncc; Array<float> eq2_freq_oncc; Array<float> eq3_freq_oncc;
462 float eq1_vel2freq; float eq2_vel2freq; float eq3_vel2freq;
463 float eq1_bw; float eq2_bw; float eq3_bw;
464 Array<float> eq1_bw_oncc; Array<float> eq2_bw_oncc; Array<float> eq3_bw_oncc;
465 float eq1_gain; float eq2_gain; float eq3_gain;
466 Array<float> eq1_gain_oncc; Array<float> eq2_gain_oncc; Array<float> eq3_gain_oncc;
467 float eq1_vel2gain; float eq2_vel2gain; float eq3_vel2gain;
468
469 //Deprecated (from version 1)
470 float ampeg_delay, ampeg_start, ampeg_attack, ampeg_hold, ampeg_decay, ampeg_sustain, ampeg_release;
471 float ampeg_vel2delay, ampeg_vel2attack, ampeg_vel2hold, ampeg_vel2decay, ampeg_vel2sustain, ampeg_vel2release;
472 LinuxSampler::ArrayList<CC> ampeg_delaycc, ampeg_startcc, ampeg_attackcc, ampeg_holdcc;
473 LinuxSampler::ArrayList<CC> ampeg_decaycc, ampeg_sustaincc, ampeg_releasecc;
474 float fileg_delay, fileg_start, fileg_attack, fileg_hold, fileg_decay, fileg_sustain, fileg_release;
475 float fileg_vel2delay, fileg_vel2attack, fileg_vel2hold, fileg_vel2decay, fileg_vel2sustain, fileg_vel2release;
476 float pitcheg_delay, pitcheg_start, pitcheg_attack, pitcheg_hold, pitcheg_decay, pitcheg_sustain, pitcheg_release;
477 float pitcheg_vel2delay, pitcheg_vel2attack, pitcheg_vel2hold, pitcheg_vel2decay, pitcheg_vel2sustain, pitcheg_vel2release;
478 int fileg_depth, pitcheg_depth;
479 float amplfo_delay, amplfo_fade, amplfo_freq, amplfo_depth;
480 float fillfo_delay, fillfo_fade, fillfo_freq, fillfo_depth;
481 float pitchlfo_delay, pitchlfo_fade, pitchlfo_freq;
482 int pitchlfo_depth;
483 Array<int> pitchlfo_depthcc;
484
485 LinuxSampler::ArrayList<CC> pitchlfo_freqcc; // 0 to 20 Hz
486 LinuxSampler::ArrayList<CC> fillfo_depthcc; // -1200 to 1200 cents
487 LinuxSampler::ArrayList<CC> fillfo_freqcc; // 0 to 20 Hz
488 LinuxSampler::ArrayList<CC> amplfo_depthcc; // -10 to 10 dB
489 LinuxSampler::ArrayList<CC> amplfo_freqcc; // 0 to 20 Hz
490
491 // envelope generators
492 LinuxSampler::ArrayList<EG> eg;
493
494 // low frequency oscillators
495 LinuxSampler::ArrayList<LFO> lfos;
496
497 LinuxSampler::ArrayList<CC> volume_oncc;
498 LinuxSampler::ArrayList<CC> volume_curvecc; // used only as temporary buffer during the parsing - values are then moved to volume_oncc
499 LinuxSampler::ArrayList<CC> volume_smoothcc; // used only as temporary buffer during the parsing - values are then moved to volume_oncc
500 };
501
502 class Query {
503 public:
504 uint8_t chan; // MIDI channel
505 uint8_t key; // MIDI note
506 uint8_t vel; // MIDI velocity
507 int bend; // MIDI pitch bend
508 uint8_t bpm; // host BPM
509 uint8_t chanaft; // MIDI channel pressure
510 uint8_t polyaft; // MIDI polyphonic aftertouch
511 uint8_t prog; // MIDI program change
512 float rand; // generated random number
513 trigger_t trig; // how it was triggered
514 uint8_t* cc; // all 128 CC values
515 float timer; // time since previous region in the group was triggered
516 bool* sw; // state of region key switches, 128 possible values
517 uint8_t last_sw_key; // last key pressed in the key switch range
518 uint8_t prev_sw_key; // previous note value
519
520 void search(const Instrument* pInstrument);
521 void search(const Instrument* pInstrument, int triggercc);
522 Region* next();
523 private:
524 LinuxSampler::ArrayList<Region*>* pRegionList;
525 int regionIndex;
526 };
527
528 /////////////////////////////////////////////////////////////
529 // class Region
530
531 /// Defines Region information of an Instrument
532 class Region :
533 public Definition
534 {
535 public:
536 Region();
537 virtual ~Region();
538
539 Sample* pSample;
540 Sample* GetSample(bool create = true);
541 void DestroySampleIfNotUsed();
542
543 Region* GetParent() { return this; }; // needed by EngineBase
544 Instrument* GetInstrument() { return pInstrument; }
545 void SetInstrument(Instrument* pInstrument) { this->pInstrument = pInstrument; }
546
547 bool HasLoop();
548 uint GetLoopStart();
549 uint GetLoopEnd();
550 uint GetLoopCount();
551
552 /// Return true if region is triggered by key. Region is
553 /// assumed to come from a search in the lookup table.
554 bool OnKey(const Query& q);
555
556 /// Return an articulation for the current state
557 Articulation* GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc);
558
559 // unique region id
560 int id;
561
562 private:
563 Instrument* pInstrument;
564 int seq_counter;
565 };
566
567 class Curve {
568 public:
569 float v[128];
570 Curve() { for (int i = 0; i < 128; i++) v[i] = 0; }
571 Curve(const Curve& curve) { Copy(curve); }
572 void operator=(const Curve& curve) { Copy(curve); }
573 void Copy(const Curve& curve) { for (int i = 0; i < 128; i++) v[i] = curve.v[i]; }
574 };
575
576 /////////////////////////////////////////////////////////////
577 // class Instrument
578
579 /// Provides all neccessary information for the synthesis of an Instrument
580 class Instrument : public SampleManager
581 {
582 public:
583 Instrument(std::string name = "Unknown", SampleManager* pSampleManager = NULL);
584 virtual ~Instrument();
585
586 std::string GetName() { return name; }
587 SampleManager* GetSampleManager() { return pSampleManager; }
588
589 bool DestroyRegion(Region* pRegion);
590 bool HasKeyBinding(uint8_t key);
591 bool HasKeySwitchBinding(uint8_t key);
592
593 /// List of Regions belonging to this Instrument
594 std::vector<Region*> regions;
595 ::LinuxSampler::ArrayList<Curve> curves;
596
597 friend class File;
598 friend class Query;
599
600 private:
601 std::string name;
602 std::vector<bool> KeyBindings;
603 std::vector<bool> KeySwitchBindings;
604 SampleManager* pSampleManager;
605 LookupTable* pLookupTable;
606 LookupTable* pLookupTableCC[128];
607 };
608
609 /////////////////////////////////////////////////////////////
610 // class Group
611
612 /// A Group act just as a template containing Region default values
613 class Group :
614 public Definition
615 {
616 public:
617 Group();
618 virtual ~Group();
619
620 /// Reset Group to default values
621 void Reset();
622
623 /// Create a new Region
624 Region* RegionFactory();
625
626 // id counter
627 int id;
628
629 };
630
631 /////////////////////////////////////////////////////////////
632 // class File
633
634 /// Parses SFZ files and provides abstract access to the data
635 class File
636 {
637 public:
638 /// Load an existing SFZ file
639 File(std::string file, SampleManager* pSampleManager = NULL);
640 virtual ~File();
641
642 /// Returns a pointer to the instrument object
643 Instrument* GetInstrument();
644
645 private:
646 void push_header(std::string token);
647 void push_opcode(std::string token);
648 int parseKey(const std::string& value);
649 EG& eg(int x);
650 EGNode& egnode(int x, int y);
651 LFO& lfo(int x);
652 void copyCurves(LinuxSampler::ArrayList<CC>& curves, LinuxSampler::ArrayList<CC>& dest);
653 void copySmoothValues(LinuxSampler::ArrayList<CC>& smooths, LinuxSampler::ArrayList<CC>& dest);
654
655 std::string currentDir;
656 /// Pointer to the Instrument belonging to this file
657 Instrument* _instrument;
658
659 // state variables
660 enum section_t { UNKNOWN, GROUP, REGION, CONTROL, CURVE };
661 section_t _current_section;
662 Region* _current_region;
663 Group* _current_group;
664 Curve* _current_curve;
665 Definition* pCurDef;
666
667 // control header directives
668 std::string default_path;
669 int octave_offset;
670 int note_offset;
671 };
672
673 } // !namespace sfz
674
675 #endif // !LIBSFZ_SFZ_H

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC