/[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 2227 - (hide annotations) (download) (as text)
Wed Aug 3 17:11:40 2011 UTC (12 years, 7 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 20259 byte(s)
* sfz engine: implemented opcodes fillfo_freqccN,
  pitchlfo_freqccN, amplfo_freqccN, lfoN_freq_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 2226 float fade; // 0 to 100 seconds
270 iliev 2225 float phase; // 0 to 360 degrees
271 iliev 2218 uint wave; // 0 to 4294967296
272 iliev 2221 float volume; // -144 to 6 dB
273 iliev 2218 int pitch; // -9600 to 9600 cents
274     int cutoff; // -9600 to 9600 cents
275     float resonance; // 0 to 40 dB
276     float pan; // -100 to 100 %
277 iliev 2225
278 iliev 2227 LinuxSampler::ArrayList<CC> freq_oncc; // 0 to 20 Hz
279 iliev 2226 LinuxSampler::ArrayList<CC> fade_oncc; // 0 to 100 seconds
280 iliev 2225 LinuxSampler::ArrayList<CC> phase_oncc; // 0 to 360 degrees
281     LinuxSampler::ArrayList<CC> pitch_oncc;
282    
283 iliev 2218 LFO();
284     };
285    
286 persson 2091 // Fixed size array with copy-on-write semantics
287     template<class T>
288     class Array
289     {
290     private:
291     struct Rep {
292     int refcount;
293     T a[128];
294    
295     Rep() : refcount(1) { }
296     static void release(Rep* rep) {
297     if (!--rep->refcount) delete rep;
298     }
299     } *ptr;
300     public:
301     Array() : ptr(0) { }
302     ~Array() { Rep::release(ptr); }
303    
304     Array& operator=(const Array& array) {
305     if (this != &array) {
306     ptr = array.ptr;
307     if (ptr) ptr->refcount++;
308     }
309     return *this;
310     }
311    
312     const T& operator[](int i) const { return ptr->a[i]; }
313    
314     void set(int i, const T& v) {
315     if (!ptr) {
316     ptr = new Rep;
317     } else if (ptr->refcount > 1 && ptr->a[i] != v) {
318     Rep* newptr = new Rep(*ptr);
319     newptr->refcount = 1;
320     Rep::release(ptr);
321     ptr = newptr;
322     }
323     ptr->a[i] = v;
324     }
325     };
326    
327 iliev 2012 /////////////////////////////////////////////////////////////
328     // class Definition
329    
330     // Base definition used by groups and regions
331     class Definition
332     {
333     public:
334     Definition();
335     virtual ~Definition();
336    
337     // sample definition
338     std::string sample;
339    
340     // input controls
341     int lochan; int hichan;
342     int lokey; int hikey;
343     int lovel; int hivel;
344 persson 2091 Array<int> locc; Array<int> hicc;
345 iliev 2012 int lobend; int hibend;
346 persson 2106 float lobpm; float hibpm;
347 iliev 2012 int lochanaft; int hichanaft;
348     int lopolyaft; int hipolyaft;
349     int loprog; int hiprog;
350     float lorand; float hirand;
351     float lotimer; float hitimer;
352    
353     int seq_length;
354     int seq_position;
355    
356 persson 2091 Array<int> start_locc; Array<int> start_hicc;
357     Array<int> stop_locc; Array<int> stop_hicc;
358 iliev 2012
359     int sw_lokey; int sw_hikey;
360     int sw_last;
361     int sw_down;
362     int sw_up;
363     int sw_previous;
364     sw_vel_t sw_vel;
365    
366     trigger_t trigger;
367    
368 iliev 2027 uint group;
369 persson 2114 uint off_by;
370 iliev 2012 off_mode_t off_mode;
371    
372 persson 2091 Array<int> on_locc; Array<int> on_hicc;
373 iliev 2012
374     // sample player
375     optional<int> count;
376 persson 2091 optional<float> delay; optional<float> delay_random; Array<optional<float> > delay_oncc;
377 iliev 2012 optional<int> delay_beats; optional<int> stop_beats;
378 persson 2091 optional<int> delay_samples; Array<optional<int> > delay_samples_oncc;
379 iliev 2012 optional<int> end;
380     optional<float> loop_crossfade;
381 persson 2091 optional<int> offset; optional<int> offset_random; Array<optional<int> > offset_oncc;
382 iliev 2012 loop_mode_t loop_mode;
383     optional<int> loop_start; optional<int> loop_end;
384     optional<int> sync_beats;
385     optional<int> sync_offset;
386    
387     // amplifier
388     float volume;
389     float pan;
390     float width;
391     float position;
392 persson 2091 float amp_keytrack; int amp_keycenter; float amp_veltrack; Array<float> amp_velcurve; float amp_random;
393 iliev 2012 float rt_decay;
394 persson 2091 Array<float> gain_oncc;
395 iliev 2012 int xfin_lokey; int xfin_hikey;
396     int xfout_lokey; int xfout_hikey;
397     curve_t xf_keycurve;
398     int xfin_lovel; int xfin_hivel;
399     int xfout_lovel; int xfout_hivel;
400     curve_t xf_velcurve;
401 persson 2091 Array<int> xfin_locc; Array<int> xfin_hicc;
402     Array<int> xfout_locc; Array<int> xfout_hicc;
403 iliev 2012 curve_t xf_cccurve;
404    
405     // pitch
406     int transpose;
407     int tune;
408     int pitch_keycenter; int pitch_keytrack; int pitch_veltrack; int pitch_random;
409     int bend_up; int bend_down; int bend_step;
410    
411     // filter
412     filter_t fil_type; filter_t fil2_type;
413     optional<float> cutoff; optional<float> cutoff2;
414 persson 2091 Array<int> cutoff_oncc; Array<int> cutoff2_oncc;
415 persson 2175 int cutoff_cc; // TODO: this is just a temporary fix to avoid
416     // looping through the cutoff_oncc array
417 persson 2091 Array<int> cutoff_smoothcc; Array<int> cutoff2_smoothcc;
418     Array<int> cutoff_stepcc; Array<int> cutoff2_stepcc;
419     Array<int> cutoff_curvecc; Array<int> cutoff2_curvecc;
420 iliev 2012 int cutoff_chanaft; int cutoff2_chanaft;
421     int cutoff_polyaft; int cutoff2_polyaft;
422     float resonance; float resonance2;
423 persson 2091 Array<int> resonance_oncc; Array<int> resonance2_oncc;
424     Array<int> resonance_smoothcc; Array<int> resonance2_smoothcc;
425     Array<int> resonance_stepcc; Array<int> resonance2_stepcc;
426     Array<int> resonance_curvecc; Array<int> resonance2_curvecc;
427 iliev 2012 int fil_keytrack; int fil2_keytrack;
428     int fil_keycenter; int fil2_keycenter;
429     int fil_veltrack; int fil2_veltrack;
430     int fil_random; int fil2_random;
431    
432     // per voice equalizer
433     float eq1_freq; float eq2_freq; float eq3_freq;
434 persson 2091 Array<float> eq1_freq_oncc; Array<float> eq2_freq_oncc; Array<float> eq3_freq_oncc;
435 iliev 2012 float eq1_vel2freq; float eq2_vel2freq; float eq3_vel2freq;
436     float eq1_bw; float eq2_bw; float eq3_bw;
437 persson 2091 Array<float> eq1_bw_oncc; Array<float> eq2_bw_oncc; Array<float> eq3_bw_oncc;
438 iliev 2012 float eq1_gain; float eq2_gain; float eq3_gain;
439 persson 2091 Array<float> eq1_gain_oncc; Array<float> eq2_gain_oncc; Array<float> eq3_gain_oncc;
440 iliev 2012 float eq1_vel2gain; float eq2_vel2gain; float eq3_vel2gain;
441 iliev 2018
442     //Deprecated (from version 1)
443     float ampeg_delay, ampeg_start, ampeg_attack, ampeg_hold, ampeg_decay, ampeg_sustain, ampeg_release;
444 persson 2176 float ampeg_vel2delay, ampeg_vel2attack, ampeg_vel2hold, ampeg_vel2decay, ampeg_vel2sustain, ampeg_vel2release;
445 iliev 2018 float fileg_delay, fileg_start, fileg_attack, fileg_hold, fileg_decay, fileg_sustain, fileg_release;
446 iliev 2222 float fileg_vel2delay, fileg_vel2attack, fileg_vel2hold, fileg_vel2decay, fileg_vel2sustain, fileg_vel2release;
447 iliev 2018 float pitcheg_delay, pitcheg_start, pitcheg_attack, pitcheg_hold, pitcheg_decay, pitcheg_sustain, pitcheg_release;
448 iliev 2220 float pitcheg_vel2delay, pitcheg_vel2attack, pitcheg_vel2hold, pitcheg_vel2decay, pitcheg_vel2sustain, pitcheg_vel2release;
449 iliev 2222 int fileg_depth, pitcheg_depth;
450 persson 2072 float amplfo_delay, amplfo_fade, amplfo_freq, amplfo_depth;
451     float fillfo_delay, fillfo_fade, fillfo_freq, fillfo_depth;
452     float pitchlfo_delay, pitchlfo_fade, pitchlfo_freq;
453     int pitchlfo_depth;
454 iliev 2224 Array<int> pitchlfo_depthcc;
455 iliev 2227
456     LinuxSampler::ArrayList<CC> pitchlfo_freqcc; // 0 to 20 Hz
457     LinuxSampler::ArrayList<CC> fillfo_freqcc; // 0 to 20 Hz
458     LinuxSampler::ArrayList<CC> amplfo_freqcc; // 0 to 20 Hz
459 persson 2055
460     // envelope generators
461     LinuxSampler::ArrayList<EG> eg;
462 iliev 2218
463     // low frequency oscillators
464     LinuxSampler::ArrayList<LFO> lfos;
465 iliev 2012 };
466    
467 persson 2101 class Query {
468     public:
469     uint8_t chan; // MIDI channel
470 persson 2115 uint8_t key; // MIDI note
471 persson 2101 uint8_t vel; // MIDI velocity
472     int bend; // MIDI pitch bend
473     uint8_t bpm; // host BPM
474     uint8_t chanaft; // MIDI channel pressure
475     uint8_t polyaft; // MIDI polyphonic aftertouch
476     uint8_t prog; // MIDI program change
477     float rand; // generated random number
478     trigger_t trig; // how it was triggered
479     uint8_t* cc; // all 128 CC values
480     float timer; // time since previous region in the group was triggered
481     bool* sw; // state of region key switches, 128 possible values
482     uint8_t last_sw_key; // last key pressed in the key switch range
483     uint8_t prev_sw_key; // previous note value
484 persson 2106
485     void search(const Instrument* pInstrument);
486 persson 2115 void search(const Instrument* pInstrument, int triggercc);
487 persson 2106 Region* next();
488 persson 2101 private:
489 persson 2106 LinuxSampler::ArrayList<Region*>* pRegionList;
490     int regionIndex;
491 persson 2101 };
492    
493 iliev 2012 /////////////////////////////////////////////////////////////
494     // class Region
495    
496     /// Defines Region information of an Instrument
497     class Region :
498     public Definition
499     {
500     public:
501     Region();
502     virtual ~Region();
503    
504 iliev 2021 Sample* pSample;
505     Sample* GetSample(bool create = true);
506 iliev 2012 void DestroySampleIfNotUsed();
507    
508     Region* GetParent() { return this; }; // needed by EngineBase
509     Instrument* GetInstrument() { return pInstrument; }
510     void SetInstrument(Instrument* pInstrument) { this->pInstrument = pInstrument; }
511    
512 iliev 2021 bool HasLoop();
513     uint GetLoopStart();
514     uint GetLoopEnd();
515     uint GetLoopCount();
516    
517 persson 2106 /// Return true if region is triggered by key. Region is
518     /// assumed to come from a search in the lookup table.
519 persson 2101 bool OnKey(const Query& q);
520 iliev 2012
521     /// Return an articulation for the current state
522 persson 2101 Articulation* GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc);
523 iliev 2012
524     // unique region id
525     int id;
526    
527     private:
528     Instrument* pInstrument;
529 persson 2072 int seq_counter;
530 iliev 2012 };
531    
532     /////////////////////////////////////////////////////////////
533     // class Instrument
534    
535     /// Provides all neccessary information for the synthesis of an Instrument
536     class Instrument : public SampleManager
537     {
538     public:
539     Instrument(std::string name = "Unknown", SampleManager* pSampleManager = NULL);
540     virtual ~Instrument();
541    
542     std::string GetName() { return name; }
543     SampleManager* GetSampleManager() { return pSampleManager; }
544    
545     bool DestroyRegion(Region* pRegion);
546     bool HasKeyBinding(uint8_t key);
547     bool HasKeySwitchBinding(uint8_t key);
548    
549     /// List of Regions belonging to this Instrument
550     std::vector<Region*> regions;
551    
552 persson 2106 friend class File;
553     friend class Query;
554 iliev 2012
555     private:
556     std::string name;
557     std::vector<bool> KeyBindings;
558     std::vector<bool> KeySwitchBindings;
559     SampleManager* pSampleManager;
560 persson 2106 LookupTable* pLookupTable;
561 persson 2115 LookupTable* pLookupTableCC[128];
562 iliev 2012 };
563    
564     /////////////////////////////////////////////////////////////
565     // class Group
566    
567     /// A Group act just as a template containing Region default values
568     class Group :
569     public Definition
570     {
571     public:
572     Group();
573     virtual ~Group();
574    
575     /// Reset Group to default values
576     void Reset();
577    
578     /// Create a new Region
579     Region* RegionFactory();
580    
581     // id counter
582     int id;
583    
584     };
585    
586     /////////////////////////////////////////////////////////////
587     // class File
588    
589     /// Parses SFZ files and provides abstract access to the data
590     class File
591     {
592     public:
593     /// Load an existing SFZ file
594     File(std::string file, SampleManager* pSampleManager = NULL);
595     virtual ~File();
596    
597     /// Returns a pointer to the instrument object
598     Instrument* GetInstrument();
599    
600     private:
601     void push_header(std::string token);
602     void push_opcode(std::string token);
603 persson 2058 int parseKey(const std::string& value);
604 persson 2055 EG& eg(int x);
605     EGNode& egnode(int x, int y);
606 iliev 2218 LFO& lfo(int x);
607 iliev 2012
608     std::string currentDir;
609     /// Pointer to the Instrument belonging to this file
610     Instrument* _instrument;
611    
612     // state variables
613     enum section_t { UNKNOWN, GROUP, REGION, CONTROL };
614     section_t _current_section;
615     Region* _current_region;
616     Group* _current_group;
617     Definition* pCurDef;
618    
619     // control header directives
620     std::string default_path;
621     int octave_offset;
622     int note_offset;
623     };
624    
625     } // !namespace sfz
626    
627     #endif // !LIBSFZ_SFZ_H

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC