/[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 2216 - (hide annotations) (download) (as text)
Mon Jul 25 17:21:16 2011 UTC (12 years, 8 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 18235 byte(s)
* sfz: added support for sample offset (offset)

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC