/[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 2018 - (hide annotations) (download) (as text)
Tue Oct 27 19:04:57 2009 UTC (14 years, 5 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 15885 byte(s)
* SFZ format engine: Implemented sfz version 1
   Filter EG, Amplifier EG and Pitch EG
* use SF2 file loader from libgig

1 iliev 2012 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2008-2009 Anders Dahnielson <anders@dahnielson.com> *
6     * Copyright (C) 2009 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    
36     #define TRIGGER_ATTACK ((unsigned char) (1 << 0)) // 0x01
37     #define TRIGGER_RELEASE ((unsigned char) (1 << 1)) // 0x02
38     #define TRIGGER_FIRST ((unsigned char) (1 << 2)) // 0x04
39     #define TRIGGER_LEGATO ((unsigned char) (1 << 3)) // 0x08
40    
41     namespace sfz
42     {
43     // Forward declarations
44     class Articulation;
45     class Region;
46     class Group;
47     class Instrument;
48     class File;
49    
50     // Enumerations
51     enum sw_vel_t { VEL_CURRENT, VEL_PREVIOUS };
52     enum off_mode_t { OFF_FAST, OFF_NORMAL };
53     enum loop_mode_t { NO_LOOP, ONE_SHOT, LOOP_CONTINOUS, LOOP_SUSTAIN };
54     enum curve_t { GAIN, POWER };
55     enum filter_t { LPF_1P, HPF_1P, BPF_1P, BRF_1P, APF_1P,
56     LPF_2P, HPF_2P, BPF_2P, BRF_2P, PKF_2P,
57     LPF_4P, HPF_4P,
58     LPF_6P, HPF_6P };
59    
60     typedef unsigned char trigger_t;
61     typedef unsigned char uint8_t;
62    
63     class SampleManager : public LinuxSampler::SampleManager<LinuxSampler::SampleFile, Region> {
64     public:
65     LinuxSampler::SampleFile* FindSample(std::string samplePath);
66    
67     protected:
68     virtual void OnSampleInUse(LinuxSampler::SampleFile* pSample) {
69     pSample->Open();
70     }
71    
72     virtual void OnSampleInNotUse(LinuxSampler::SampleFile* pSample) {
73     pSample->Close();
74     }
75     };
76    
77     /////////////////////////////////////////////////////////////
78     // class Exception
79    
80     class Exception :
81     public std::runtime_error
82     {
83     public:
84     Exception(const std::string& msg) :
85     runtime_error(msg)
86     {
87     }
88    
89     std::string Message()
90     {
91     return what();
92     }
93    
94     void PrintMessage()
95     {
96     std::cerr << what() << std::endl << std::flush;
97     }
98     };
99    
100     /////////////////////////////////////////////////////////////
101     // class optional
102    
103     // Handy class nicked from LinuxSampler...
104     // Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck
105     // Copyright (C) 2005, 2006 Christian Schoenebeck
106    
107     class optional_base
108     {
109     public:
110     class nothing_t { public: nothing_t() {} };
111     static const nothing_t nothing;
112     };
113    
114     template<class T>
115     class optional :
116     public optional_base
117     {
118     public:
119     optional()
120     {
121     initialized = false;
122     }
123    
124     optional(T data)
125     {
126     this->data = data;
127     initialized = true;
128     }
129    
130     optional(nothing_t)
131     {
132     initialized = false;
133     }
134    
135     template <class T_inner>
136     optional(T_inner data)
137     {
138     this->data = T(data);
139     initialized = true;
140     }
141    
142     const T& get() const throw (Exception)
143     {
144     if (!initialized) throw Exception("optional variable not initialized");
145     return data;
146     }
147    
148     T& get() throw (Exception)
149     {
150     if (!initialized) throw Exception("optional variable not initialized");
151     return data;
152     }
153    
154     void unset()
155     {
156     initialized = false;
157     }
158    
159     optional& operator =(const optional& arg) throw (Exception)
160     {
161     if (!arg.initialized) {
162     initialized = false;
163     } else {
164     this->data = arg.data;
165     initialized = true;
166     }
167     return *this;
168     }
169    
170     optional& operator =(const T& arg)
171     {
172     this->data = arg;
173     initialized = true;
174     return *this;
175     }
176    
177     const T& operator *() const throw (Exception) { return get(); }
178     T& operator *() throw (Exception) { return get(); }
179    
180     const T* operator ->() const throw (Exception)
181     {
182     if (!initialized) throw Exception("optional variable not initialized");
183     return &data;
184     }
185    
186     T* operator ->() throw (Exception)
187     {
188     if (!initialized) throw Exception("optional variable not initialized");
189     return &data;
190     }
191    
192     operator bool() const { return initialized; }
193     bool operator !() const { return !initialized; }
194    
195     protected:
196     T data;
197     bool initialized;
198     };
199    
200     /////////////////////////////////////////////////////////////
201     // class Articulation
202    
203     // Articulation containing all performance parameters for synthesis
204     class Articulation
205     {
206     public:
207     Articulation();
208     virtual ~Articulation();
209     };
210    
211     /////////////////////////////////////////////////////////////
212     // class Definition
213    
214     // Base definition used by groups and regions
215     class Definition
216     {
217     public:
218     Definition();
219     virtual ~Definition();
220    
221     // sample definition
222     std::string sample;
223    
224     // input controls
225     int lochan; int hichan;
226     int lokey; int hikey;
227     int lovel; int hivel;
228     std::vector<int> locc; std::vector<int> hicc;
229     int lobend; int hibend;
230     int lobpm; int hibpm;
231     int lochanaft; int hichanaft;
232     int lopolyaft; int hipolyaft;
233     int loprog; int hiprog;
234     float lorand; float hirand;
235     float lotimer; float hitimer;
236    
237     int seq_length;
238     int seq_position;
239    
240     std::vector<int> start_locc; std::vector<int> start_hicc;
241     std::vector<int> stop_locc; std::vector<int> stop_hicc;
242    
243     int sw_lokey; int sw_hikey;
244     int sw_last;
245     int sw_down;
246     int sw_up;
247     int sw_previous;
248     sw_vel_t sw_vel;
249    
250     trigger_t trigger;
251    
252     optional<int> group;
253     optional<int> off_by;
254     off_mode_t off_mode;
255    
256     std::vector<int> on_locc; std::vector<int> on_hicc;
257    
258     // sample player
259     optional<int> count;
260     optional<float> delay; optional<float> delay_random; std::vector<optional<float> > delay_oncc;
261     optional<int> delay_beats; optional<int> stop_beats;
262     optional<int> delay_samples; std::vector<optional<int> > delay_samples_oncc;
263     optional<int> end;
264     optional<float> loop_crossfade;
265     optional<int> offset; optional<int> offset_random; std::vector<optional<int> > offset_oncc;
266     loop_mode_t loop_mode;
267     optional<int> loop_start; optional<int> loop_end;
268     optional<int> sync_beats;
269     optional<int> sync_offset;
270    
271     // amplifier
272     float volume;
273     float pan;
274     float width;
275     float position;
276     float amp_keytrack; int amp_keycenter; float amp_veltrack; std::vector<float> amp_velcurve_; float amp_random;
277     float rt_decay;
278     std::vector<float> gain_oncc;
279     int xfin_lokey; int xfin_hikey;
280     int xfout_lokey; int xfout_hikey;
281     curve_t xf_keycurve;
282     int xfin_lovel; int xfin_hivel;
283     int xfout_lovel; int xfout_hivel;
284     curve_t xf_velcurve;
285     std::vector<int> xfin_locc; std::vector<int> xfin_hicc;
286     std::vector<int> xfout_locc; std::vector<int> xfout_hicc;
287     curve_t xf_cccurve;
288    
289     // pitch
290     int transpose;
291     int tune;
292     int pitch_keycenter; int pitch_keytrack; int pitch_veltrack; int pitch_random;
293     int bend_up; int bend_down; int bend_step;
294    
295     // filter
296     filter_t fil_type; filter_t fil2_type;
297     optional<float> cutoff; optional<float> cutoff2;
298     std::vector<int> cutoff_oncc; std::vector<int> cutoff2_oncc;
299     std::vector<int> cutoff_smoothcc; std::vector<int> cutoff2_smoothcc;
300     std::vector<int> cutoff_stepcc; std::vector<int> cutoff2_stepcc;
301     std::vector<int> cutoff_curvecc; std::vector<int> cutoff2_curvecc;
302     int cutoff_chanaft; int cutoff2_chanaft;
303     int cutoff_polyaft; int cutoff2_polyaft;
304     float resonance; float resonance2;
305     std::vector<int> resonance_oncc; std::vector<int> resonance2_oncc;
306     std::vector<int> resonance_smoothcc; std::vector<int> resonance2_smoothcc;
307     std::vector<int> resonance_stepcc; std::vector<int> resonance2_stepcc;
308     std::vector<int> resonance_curvecc; std::vector<int> resonance2_curvecc;
309     int fil_keytrack; int fil2_keytrack;
310     int fil_keycenter; int fil2_keycenter;
311     int fil_veltrack; int fil2_veltrack;
312     int fil_random; int fil2_random;
313    
314     // per voice equalizer
315     float eq1_freq; float eq2_freq; float eq3_freq;
316     std::vector<float> eq1_freq_oncc; std::vector<float> eq2_freq_oncc; std::vector<float> eq3_freq_oncc;
317     float eq1_vel2freq; float eq2_vel2freq; float eq3_vel2freq;
318     float eq1_bw; float eq2_bw; float eq3_bw;
319     std::vector<float> eq1_bw_oncc; std::vector<float> eq2_bw_oncc; std::vector<float> eq3_bw_oncc;
320     float eq1_gain; float eq2_gain; float eq3_gain;
321     std::vector<float> eq1_gain_oncc; std::vector<float> eq2_gain_oncc; std::vector<float> eq3_gain_oncc;
322     float eq1_vel2gain; float eq2_vel2gain; float eq3_vel2gain;
323 iliev 2018
324     //Deprecated (from version 1)
325     float ampeg_delay, ampeg_start, ampeg_attack, ampeg_hold, ampeg_decay, ampeg_sustain, ampeg_release;
326     float fileg_delay, fileg_start, fileg_attack, fileg_hold, fileg_decay, fileg_sustain, fileg_release;
327     float pitcheg_delay, pitcheg_start, pitcheg_attack, pitcheg_hold, pitcheg_decay, pitcheg_sustain, pitcheg_release;
328 iliev 2012 };
329    
330     /////////////////////////////////////////////////////////////
331     // class Region
332    
333     /// Defines Region information of an Instrument
334     class Region :
335     public Definition
336     {
337     public:
338     Region();
339     virtual ~Region();
340    
341     LinuxSampler::SampleFile* pSample;
342     LinuxSampler::SampleFile* GetSample(bool create = true);
343     void DestroySampleIfNotUsed();
344    
345     Region* GetParent() { return this; }; // needed by EngineBase
346     Instrument* GetInstrument() { return pInstrument; }
347     void SetInstrument(Instrument* pInstrument) { this->pInstrument = pInstrument; }
348    
349     /// Return true if region is triggered by key
350     bool OnKey(uint8_t chan, uint8_t key, uint8_t vel,
351     int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft,
352     uint8_t prog, float rand, trigger_t trig, uint8_t* cc,
353     float timer, uint8_t seq, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key);
354    
355     /// Return true if region is triggered by control change
356     bool OnControl(uint8_t chan, uint8_t cont, uint8_t val,
357     int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft,
358     uint8_t prog, float rand, trigger_t trig, uint8_t* cc,
359     float timer, uint8_t seq, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key);
360    
361     /// Return an articulation for the current state
362     Articulation* GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc);
363    
364     // unique region id
365     int id;
366    
367     private:
368     Instrument* pInstrument;
369     };
370    
371     /////////////////////////////////////////////////////////////
372     // class Instrument
373    
374     /// Provides all neccessary information for the synthesis of an Instrument
375     class Instrument : public SampleManager
376     {
377     public:
378     Instrument(std::string name = "Unknown", SampleManager* pSampleManager = NULL);
379     virtual ~Instrument();
380    
381     std::string GetName() { return name; }
382     SampleManager* GetSampleManager() { return pSampleManager; }
383    
384     /**
385     * @returns All regions that are triggered by key
386     */
387     std::vector<Region*> GetRegionsOnKey (
388     uint8_t chan, uint8_t key, uint8_t vel,
389     int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft,
390     uint8_t prog, float rand, trigger_t trig, uint8_t* cc,
391     float timer, uint8_t seq, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key
392     );
393    
394     bool DestroyRegion(Region* pRegion);
395     bool HasKeyBinding(uint8_t key);
396     bool HasKeySwitchBinding(uint8_t key);
397    
398     /// List of Regions belonging to this Instrument
399     std::vector<Region*> regions;
400    
401     friend class sfz::File;
402    
403     private:
404     std::string name;
405     std::vector<bool> KeyBindings;
406     std::vector<bool> KeySwitchBindings;
407     SampleManager* pSampleManager;
408     };
409    
410     /////////////////////////////////////////////////////////////
411     // class Group
412    
413     /// A Group act just as a template containing Region default values
414     class Group :
415     public Definition
416     {
417     public:
418     Group();
419     virtual ~Group();
420    
421     /// Reset Group to default values
422     void Reset();
423    
424     /// Create a new Region
425     Region* RegionFactory();
426    
427     // id counter
428     int id;
429    
430     };
431    
432     /////////////////////////////////////////////////////////////
433     // class File
434    
435     /// Parses SFZ files and provides abstract access to the data
436     class File
437     {
438     public:
439     /// Load an existing SFZ file
440     File(std::string file, SampleManager* pSampleManager = NULL);
441     virtual ~File();
442    
443     /// Returns a pointer to the instrument object
444     Instrument* GetInstrument();
445    
446     private:
447     void push_header(std::string token);
448     void push_opcode(std::string token);
449     int parseInt(std::string value);
450     float parseFloat(std::string value);
451    
452     std::string currentDir;
453     /// Pointer to the Instrument belonging to this file
454     Instrument* _instrument;
455    
456     // state variables
457     enum section_t { UNKNOWN, GROUP, REGION, CONTROL };
458     section_t _current_section;
459     Region* _current_region;
460     Group* _current_group;
461     Definition* pCurDef;
462    
463     // control header directives
464     std::string default_path;
465     int octave_offset;
466     int note_offset;
467     };
468    
469     } // !namespace sfz
470    
471     #endif // !LIBSFZ_SFZ_H

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC