/[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 2176 - (show annotations) (download) (as text)
Sun May 1 15:19:38 2011 UTC (12 years, 11 months ago) by persson
File MIME type: text/x-c++hdr
File size: 18162 byte(s)
* sfz engine: added support for velocity effect on amplifier envelope
  time (ampeg_vel2attack, ampeg_vel2decay, ampeg_vel2sustain and
  ampeg_vel2release)

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC