/[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 2230 - (show annotations) (download) (as text)
Fri Aug 5 17:59:10 2011 UTC (12 years, 8 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 21787 byte(s)
* sfz engine: implemented curves
* sfz engine: implemented opcodes volume_onccN, volume_curveccN

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC