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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2114 - (hide annotations) (download) (as text)
Tue Aug 10 12:05:19 2010 UTC (13 years, 7 months ago) by persson
File MIME type: text/x-c++hdr
File size: 18177 byte(s)
* sfz engine: improved support for exclusive groups (group, off_by and
  off_mode)
* minor valgrind fixes

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC