/[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 2225 - (hide annotations) (download) (as text)
Tue Aug 2 13:44:57 2011 UTC (12 years, 8 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 19885 byte(s)
* sfz engine: implemented opcodes lfoN_phase, lfoN_phase_onccX,
  lfoN_pitch, lfoN_pitch_onccX

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC