/[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 2101 - (hide annotations) (download) (as text)
Sun May 30 11:40:31 2010 UTC (13 years, 10 months ago) by persson
File MIME type: text/x-c++hdr
File size: 18049 byte(s)
* sfz/sf2 engines: RT-safeness: avoid malloc in audio thread
* sfz/sf2 engines: fixed a bug that could cause voice stealing to fail

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 2055
51 iliev 2021 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 iliev 2012
57     // Enumerations
58     enum sw_vel_t { VEL_CURRENT, VEL_PREVIOUS };
59     enum off_mode_t { OFF_FAST, OFF_NORMAL };
60 iliev 2021 enum loop_mode_t { NO_LOOP, ONE_SHOT, LOOP_CONTINUOUS, LOOP_SUSTAIN };
61 iliev 2012 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 iliev 2021 class SampleManager : public LinuxSampler::SampleManager<Sample, Region> {
71 iliev 2012 public:
72 iliev 2021 Sample* FindSample(std::string samplePath);
73 iliev 2012
74     protected:
75 iliev 2021 virtual void OnSampleInUse(Sample* pSample) {
76 iliev 2012 pSample->Open();
77     }
78    
79 iliev 2021 virtual void OnSampleInNotUse(Sample* pSample) {
80 iliev 2012 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 persson 2055 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 persson 2091 // Fixed size array with copy-on-write semantics
241     template<class T>
242     class Array
243     {
244     private:
245     struct Rep {
246     int refcount;
247     T a[128];
248    
249     Rep() : refcount(1) { }
250     static void release(Rep* rep) {
251     if (!--rep->refcount) delete rep;
252     }
253     } *ptr;
254     public:
255     Array() : ptr(0) { }
256     ~Array() { Rep::release(ptr); }
257    
258     Array& operator=(const Array& array) {
259     if (this != &array) {
260     ptr = array.ptr;
261     if (ptr) ptr->refcount++;
262     }
263     return *this;
264     }
265    
266     const T& operator[](int i) const { return ptr->a[i]; }
267    
268     void set(int i, const T& v) {
269     if (!ptr) {
270     ptr = new Rep;
271     } else if (ptr->refcount > 1 && ptr->a[i] != v) {
272     Rep* newptr = new Rep(*ptr);
273     newptr->refcount = 1;
274     Rep::release(ptr);
275     ptr = newptr;
276     }
277     ptr->a[i] = v;
278     }
279     };
280    
281 iliev 2012 /////////////////////////////////////////////////////////////
282     // class Definition
283    
284     // Base definition used by groups and regions
285     class Definition
286     {
287     public:
288     Definition();
289     virtual ~Definition();
290    
291     // sample definition
292     std::string sample;
293    
294     // input controls
295     int lochan; int hichan;
296     int lokey; int hikey;
297     int lovel; int hivel;
298 persson 2091 Array<int> locc; Array<int> hicc;
299 iliev 2012 int lobend; int hibend;
300     int lobpm; int hibpm;
301     int lochanaft; int hichanaft;
302     int lopolyaft; int hipolyaft;
303     int loprog; int hiprog;
304     float lorand; float hirand;
305     float lotimer; float hitimer;
306    
307     int seq_length;
308     int seq_position;
309    
310 persson 2091 Array<int> start_locc; Array<int> start_hicc;
311     Array<int> stop_locc; Array<int> stop_hicc;
312 iliev 2012
313     int sw_lokey; int sw_hikey;
314     int sw_last;
315     int sw_down;
316     int sw_up;
317     int sw_previous;
318     sw_vel_t sw_vel;
319    
320     trigger_t trigger;
321    
322 iliev 2027 uint group;
323 iliev 2012 optional<int> off_by;
324     off_mode_t off_mode;
325    
326 persson 2091 Array<int> on_locc; Array<int> on_hicc;
327 iliev 2012
328     // sample player
329     optional<int> count;
330 persson 2091 optional<float> delay; optional<float> delay_random; Array<optional<float> > delay_oncc;
331 iliev 2012 optional<int> delay_beats; optional<int> stop_beats;
332 persson 2091 optional<int> delay_samples; Array<optional<int> > delay_samples_oncc;
333 iliev 2012 optional<int> end;
334     optional<float> loop_crossfade;
335 persson 2091 optional<int> offset; optional<int> offset_random; Array<optional<int> > offset_oncc;
336 iliev 2012 loop_mode_t loop_mode;
337     optional<int> loop_start; optional<int> loop_end;
338     optional<int> sync_beats;
339     optional<int> sync_offset;
340    
341     // amplifier
342     float volume;
343     float pan;
344     float width;
345     float position;
346 persson 2091 float amp_keytrack; int amp_keycenter; float amp_veltrack; Array<float> amp_velcurve; float amp_random;
347 iliev 2012 float rt_decay;
348 persson 2091 Array<float> gain_oncc;
349 iliev 2012 int xfin_lokey; int xfin_hikey;
350     int xfout_lokey; int xfout_hikey;
351     curve_t xf_keycurve;
352     int xfin_lovel; int xfin_hivel;
353     int xfout_lovel; int xfout_hivel;
354     curve_t xf_velcurve;
355 persson 2091 Array<int> xfin_locc; Array<int> xfin_hicc;
356     Array<int> xfout_locc; Array<int> xfout_hicc;
357 iliev 2012 curve_t xf_cccurve;
358    
359     // pitch
360     int transpose;
361     int tune;
362     int pitch_keycenter; int pitch_keytrack; int pitch_veltrack; int pitch_random;
363     int bend_up; int bend_down; int bend_step;
364    
365     // filter
366     filter_t fil_type; filter_t fil2_type;
367     optional<float> cutoff; optional<float> cutoff2;
368 persson 2091 Array<int> cutoff_oncc; Array<int> cutoff2_oncc;
369     Array<int> cutoff_smoothcc; Array<int> cutoff2_smoothcc;
370     Array<int> cutoff_stepcc; Array<int> cutoff2_stepcc;
371     Array<int> cutoff_curvecc; Array<int> cutoff2_curvecc;
372 iliev 2012 int cutoff_chanaft; int cutoff2_chanaft;
373     int cutoff_polyaft; int cutoff2_polyaft;
374     float resonance; float resonance2;
375 persson 2091 Array<int> resonance_oncc; Array<int> resonance2_oncc;
376     Array<int> resonance_smoothcc; Array<int> resonance2_smoothcc;
377     Array<int> resonance_stepcc; Array<int> resonance2_stepcc;
378     Array<int> resonance_curvecc; Array<int> resonance2_curvecc;
379 iliev 2012 int fil_keytrack; int fil2_keytrack;
380     int fil_keycenter; int fil2_keycenter;
381     int fil_veltrack; int fil2_veltrack;
382     int fil_random; int fil2_random;
383    
384     // per voice equalizer
385     float eq1_freq; float eq2_freq; float eq3_freq;
386 persson 2091 Array<float> eq1_freq_oncc; Array<float> eq2_freq_oncc; Array<float> eq3_freq_oncc;
387 iliev 2012 float eq1_vel2freq; float eq2_vel2freq; float eq3_vel2freq;
388     float eq1_bw; float eq2_bw; float eq3_bw;
389 persson 2091 Array<float> eq1_bw_oncc; Array<float> eq2_bw_oncc; Array<float> eq3_bw_oncc;
390 iliev 2012 float eq1_gain; float eq2_gain; float eq3_gain;
391 persson 2091 Array<float> eq1_gain_oncc; Array<float> eq2_gain_oncc; Array<float> eq3_gain_oncc;
392 iliev 2012 float eq1_vel2gain; float eq2_vel2gain; float eq3_vel2gain;
393 iliev 2018
394     //Deprecated (from version 1)
395     float ampeg_delay, ampeg_start, ampeg_attack, ampeg_hold, ampeg_decay, ampeg_sustain, ampeg_release;
396     float fileg_delay, fileg_start, fileg_attack, fileg_hold, fileg_decay, fileg_sustain, fileg_release;
397     float pitcheg_delay, pitcheg_start, pitcheg_attack, pitcheg_hold, pitcheg_decay, pitcheg_sustain, pitcheg_release;
398 persson 2072 float amplfo_delay, amplfo_fade, amplfo_freq, amplfo_depth;
399     float fillfo_delay, fillfo_fade, fillfo_freq, fillfo_depth;
400     float pitchlfo_delay, pitchlfo_fade, pitchlfo_freq;
401     int pitchlfo_depth;
402 persson 2055
403     // envelope generators
404     LinuxSampler::ArrayList<EG> eg;
405 iliev 2012 };
406    
407 persson 2101 class Query {
408     public:
409     Query(const Instrument& instrument);
410     Region* next();
411     uint8_t chan; // MIDI channel
412     uint8_t key; // MIDI note TODO: or controller
413     uint8_t vel; // MIDI velocity
414     int bend; // MIDI pitch bend
415     uint8_t bpm; // host BPM
416     uint8_t chanaft; // MIDI channel pressure
417     uint8_t polyaft; // MIDI polyphonic aftertouch
418     uint8_t prog; // MIDI program change
419     float rand; // generated random number
420     trigger_t trig; // how it was triggered
421     uint8_t* cc; // all 128 CC values
422     float timer; // time since previous region in the group was triggered
423     bool* sw; // state of region key switches, 128 possible values
424     uint8_t last_sw_key; // last key pressed in the key switch range
425     uint8_t prev_sw_key; // previous note value
426     private:
427     std::vector<Region*>::const_iterator i;
428     std::vector<Region*>::const_iterator regions_end;
429     };
430    
431 iliev 2012 /////////////////////////////////////////////////////////////
432     // class Region
433    
434     /// Defines Region information of an Instrument
435     class Region :
436     public Definition
437     {
438     public:
439     Region();
440     virtual ~Region();
441    
442 iliev 2021 Sample* pSample;
443     Sample* GetSample(bool create = true);
444 iliev 2012 void DestroySampleIfNotUsed();
445    
446     Region* GetParent() { return this; }; // needed by EngineBase
447     Instrument* GetInstrument() { return pInstrument; }
448     void SetInstrument(Instrument* pInstrument) { this->pInstrument = pInstrument; }
449    
450 iliev 2021 bool HasLoop();
451     uint GetLoopStart();
452     uint GetLoopEnd();
453     uint GetLoopCount();
454    
455 iliev 2012 /// Return true if region is triggered by key
456 persson 2101 bool OnKey(const Query& q);
457 iliev 2012
458     /// Return true if region is triggered by control change
459     bool OnControl(uint8_t chan, uint8_t cont, uint8_t val,
460     int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft,
461     uint8_t prog, float rand, trigger_t trig, uint8_t* cc,
462 persson 2072 float timer, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key);
463 iliev 2012
464     /// Return an articulation for the current state
465 persson 2101 Articulation* GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc);
466 iliev 2012
467     // unique region id
468     int id;
469    
470     private:
471     Instrument* pInstrument;
472 persson 2072 int seq_counter;
473 iliev 2012 };
474    
475     /////////////////////////////////////////////////////////////
476     // class Instrument
477    
478     /// Provides all neccessary information for the synthesis of an Instrument
479     class Instrument : public SampleManager
480     {
481     public:
482     Instrument(std::string name = "Unknown", SampleManager* pSampleManager = NULL);
483     virtual ~Instrument();
484    
485     std::string GetName() { return name; }
486     SampleManager* GetSampleManager() { return pSampleManager; }
487    
488     bool DestroyRegion(Region* pRegion);
489     bool HasKeyBinding(uint8_t key);
490     bool HasKeySwitchBinding(uint8_t key);
491    
492     /// List of Regions belonging to this Instrument
493     std::vector<Region*> regions;
494    
495     friend class sfz::File;
496    
497     private:
498     std::string name;
499     std::vector<bool> KeyBindings;
500     std::vector<bool> KeySwitchBindings;
501     SampleManager* pSampleManager;
502     };
503    
504     /////////////////////////////////////////////////////////////
505     // class Group
506    
507     /// A Group act just as a template containing Region default values
508     class Group :
509     public Definition
510     {
511     public:
512     Group();
513     virtual ~Group();
514    
515     /// Reset Group to default values
516     void Reset();
517    
518     /// Create a new Region
519     Region* RegionFactory();
520    
521     // id counter
522     int id;
523    
524     };
525    
526     /////////////////////////////////////////////////////////////
527     // class File
528    
529     /// Parses SFZ files and provides abstract access to the data
530     class File
531     {
532     public:
533     /// Load an existing SFZ file
534     File(std::string file, SampleManager* pSampleManager = NULL);
535     virtual ~File();
536    
537     /// Returns a pointer to the instrument object
538     Instrument* GetInstrument();
539    
540     private:
541     void push_header(std::string token);
542     void push_opcode(std::string token);
543 persson 2058 int parseKey(const std::string& value);
544 persson 2055 EG& eg(int x);
545     EGNode& egnode(int x, int y);
546 iliev 2012
547     std::string currentDir;
548     /// Pointer to the Instrument belonging to this file
549     Instrument* _instrument;
550    
551     // state variables
552     enum section_t { UNKNOWN, GROUP, REGION, CONTROL };
553     section_t _current_section;
554     Region* _current_region;
555     Group* _current_group;
556     Definition* pCurDef;
557    
558     // control header directives
559     std::string default_path;
560     int octave_offset;
561     int note_offset;
562     };
563    
564     } // !namespace sfz
565    
566     #endif // !LIBSFZ_SFZ_H

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC