/[svn]/linuxsampler/trunk/src/engines/sfz/sfz.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/sfz/sfz.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2226 - (show annotations) (download) (as text)
Wed Aug 3 09:12:09 2011 UTC (12 years, 8 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 19992 byte(s)
* sfz engine: implemented opcodes pitchlfo_fade,
  fillfo_fade, amplfo_fade, lfoN_fade, lfoN_fade_onccX

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC