/[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 2091 - (hide annotations) (download) (as text)
Sat May 15 09:02:31 2010 UTC (13 years, 10 months ago) by persson
File MIME type: text/x-c++hdr
File size: 17611 byte(s)
* sfz engine: reduced memory usage for sfz data

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     /////////////////////////////////////////////////////////////
408     // class Region
409    
410     /// Defines Region information of an Instrument
411     class Region :
412     public Definition
413     {
414     public:
415     Region();
416     virtual ~Region();
417    
418 iliev 2021 Sample* pSample;
419     Sample* GetSample(bool create = true);
420 iliev 2012 void DestroySampleIfNotUsed();
421    
422     Region* GetParent() { return this; }; // needed by EngineBase
423     Instrument* GetInstrument() { return pInstrument; }
424     void SetInstrument(Instrument* pInstrument) { this->pInstrument = pInstrument; }
425    
426 iliev 2021 bool HasLoop();
427     uint GetLoopStart();
428     uint GetLoopEnd();
429     uint GetLoopCount();
430    
431 iliev 2012 /// Return true if region is triggered by key
432     bool OnKey(uint8_t chan, uint8_t key, uint8_t vel,
433     int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft,
434     uint8_t prog, float rand, trigger_t trig, uint8_t* cc,
435 persson 2072 float timer, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key);
436 iliev 2012
437     /// Return true if region is triggered by control change
438     bool OnControl(uint8_t chan, uint8_t cont, uint8_t val,
439     int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft,
440     uint8_t prog, float rand, trigger_t trig, uint8_t* cc,
441 persson 2072 float timer, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key);
442 iliev 2012
443     /// Return an articulation for the current state
444     Articulation* GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc);
445    
446     // unique region id
447     int id;
448    
449     private:
450     Instrument* pInstrument;
451 persson 2072 int seq_counter;
452 iliev 2012 };
453    
454     /////////////////////////////////////////////////////////////
455     // class Instrument
456    
457     /// Provides all neccessary information for the synthesis of an Instrument
458     class Instrument : public SampleManager
459     {
460     public:
461     Instrument(std::string name = "Unknown", SampleManager* pSampleManager = NULL);
462     virtual ~Instrument();
463    
464     std::string GetName() { return name; }
465     SampleManager* GetSampleManager() { return pSampleManager; }
466    
467     /**
468     * @returns All regions that are triggered by key
469     */
470     std::vector<Region*> GetRegionsOnKey (
471     uint8_t chan, uint8_t key, uint8_t vel,
472     int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft,
473     uint8_t prog, float rand, trigger_t trig, uint8_t* cc,
474 persson 2072 float timer, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key
475 iliev 2012 );
476    
477     bool DestroyRegion(Region* pRegion);
478     bool HasKeyBinding(uint8_t key);
479     bool HasKeySwitchBinding(uint8_t key);
480    
481     /// List of Regions belonging to this Instrument
482     std::vector<Region*> regions;
483    
484     friend class sfz::File;
485    
486     private:
487     std::string name;
488     std::vector<bool> KeyBindings;
489     std::vector<bool> KeySwitchBindings;
490     SampleManager* pSampleManager;
491     };
492    
493     /////////////////////////////////////////////////////////////
494     // class Group
495    
496     /// A Group act just as a template containing Region default values
497     class Group :
498     public Definition
499     {
500     public:
501     Group();
502     virtual ~Group();
503    
504     /// Reset Group to default values
505     void Reset();
506    
507     /// Create a new Region
508     Region* RegionFactory();
509    
510     // id counter
511     int id;
512    
513     };
514    
515     /////////////////////////////////////////////////////////////
516     // class File
517    
518     /// Parses SFZ files and provides abstract access to the data
519     class File
520     {
521     public:
522     /// Load an existing SFZ file
523     File(std::string file, SampleManager* pSampleManager = NULL);
524     virtual ~File();
525    
526     /// Returns a pointer to the instrument object
527     Instrument* GetInstrument();
528    
529     private:
530     void push_header(std::string token);
531     void push_opcode(std::string token);
532 persson 2058 int parseKey(const std::string& value);
533 persson 2055 EG& eg(int x);
534     EGNode& egnode(int x, int y);
535 iliev 2012
536     std::string currentDir;
537     /// Pointer to the Instrument belonging to this file
538     Instrument* _instrument;
539    
540     // state variables
541     enum section_t { UNKNOWN, GROUP, REGION, CONTROL };
542     section_t _current_section;
543     Region* _current_region;
544     Group* _current_group;
545     Definition* pCurDef;
546    
547     // control header directives
548     std::string default_path;
549     int octave_offset;
550     int note_offset;
551     };
552    
553     } // !namespace sfz
554    
555     #endif // !LIBSFZ_SFZ_H

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC