/[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 2229 - (hide annotations) (download) (as text)
Thu Aug 4 19:02:36 2011 UTC (12 years, 8 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 20958 byte(s)
* sfz engine: implemented opcodes ampeg_delayccN, ampeg_startccN,
  ampeg_attackccN, ampeg_holdccN, ampeg_decayccN, ampeg_sustainccN,
  ampeg_releaseccN, egN_timeX_onccY, egN_levelX_onccY
* sfz engine: lfoN_* and egN_* opcodes defined
  in group sections are now taken into account

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC