/[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 2175 - (show annotations) (download) (as text)
Mon Apr 25 08:12:36 2011 UTC (8 years, 9 months ago) by persson
File MIME type: text/x-c++hdr
File size: 18042 byte(s)
* sfz engine: implemeted filters. Filter types: lowpass, bandpass,
  bandreject and highpass. 1, 2, 4 and 6 pole filters. Opcodes:
  fil_type, cutoff, resonance, fil_veltrack, fil_keytrack,
  fil_keycenter, cutoff_cc, cutoff_chanaft.
* sfz engine: bugfix: zero ampeg_sustain didn't work
* gig engine: bugfix: pitch LFO controller "internal+aftertouch" was broken
* gig engine: bugfix: filter keyboard tracking was broken
* gig engine: filter performance fix (an unnecessary copy was made of
  the filter parameters in each sub fragment)
* ASIO driver: fixes for newer gcc versions (fix from PortAudio)

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 fileg_delay, fileg_start, fileg_attack, fileg_hold, fileg_decay, fileg_sustain, fileg_release;
400 float pitcheg_delay, pitcheg_start, pitcheg_attack, pitcheg_hold, pitcheg_decay, pitcheg_sustain, pitcheg_release;
401 float amplfo_delay, amplfo_fade, amplfo_freq, amplfo_depth;
402 float fillfo_delay, fillfo_fade, fillfo_freq, fillfo_depth;
403 float pitchlfo_delay, pitchlfo_fade, pitchlfo_freq;
404 int pitchlfo_depth;
405
406 // envelope generators
407 LinuxSampler::ArrayList<EG> eg;
408 };
409
410 class Query {
411 public:
412 uint8_t chan; // MIDI channel
413 uint8_t key; // MIDI note
414 uint8_t vel; // MIDI velocity
415 int bend; // MIDI pitch bend
416 uint8_t bpm; // host BPM
417 uint8_t chanaft; // MIDI channel pressure
418 uint8_t polyaft; // MIDI polyphonic aftertouch
419 uint8_t prog; // MIDI program change
420 float rand; // generated random number
421 trigger_t trig; // how it was triggered
422 uint8_t* cc; // all 128 CC values
423 float timer; // time since previous region in the group was triggered
424 bool* sw; // state of region key switches, 128 possible values
425 uint8_t last_sw_key; // last key pressed in the key switch range
426 uint8_t prev_sw_key; // previous note value
427
428 void search(const Instrument* pInstrument);
429 void search(const Instrument* pInstrument, int triggercc);
430 Region* next();
431 private:
432 LinuxSampler::ArrayList<Region*>* pRegionList;
433 int regionIndex;
434 };
435
436 /////////////////////////////////////////////////////////////
437 // class Region
438
439 /// Defines Region information of an Instrument
440 class Region :
441 public Definition
442 {
443 public:
444 Region();
445 virtual ~Region();
446
447 Sample* pSample;
448 Sample* GetSample(bool create = true);
449 void DestroySampleIfNotUsed();
450
451 Region* GetParent() { return this; }; // needed by EngineBase
452 Instrument* GetInstrument() { return pInstrument; }
453 void SetInstrument(Instrument* pInstrument) { this->pInstrument = pInstrument; }
454
455 bool HasLoop();
456 uint GetLoopStart();
457 uint GetLoopEnd();
458 uint GetLoopCount();
459
460 /// Return true if region is triggered by key. Region is
461 /// assumed to come from a search in the lookup table.
462 bool OnKey(const Query& q);
463
464 /// Return an articulation for the current state
465 Articulation* GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc);
466
467 // unique region id
468 int id;
469
470 private:
471 Instrument* pInstrument;
472 int seq_counter;
473 };
474
475 /////////////////////////////////////////////////////////////
476 // class Instrument
477
478 /// Provides all neccessary information for the synthesis of an Instrument
479 class Instrument : public SampleManager
480 {
481 public:
482 Instrument(std::string name = "Unknown", SampleManager* pSampleManager = NULL);
483 virtual ~Instrument();
484
485 std::string GetName() { return name; }
486 SampleManager* GetSampleManager() { return pSampleManager; }
487
488 bool DestroyRegion(Region* pRegion);
489 bool HasKeyBinding(uint8_t key);
490 bool HasKeySwitchBinding(uint8_t key);
491
492 /// List of Regions belonging to this Instrument
493 std::vector<Region*> regions;
494
495 friend class File;
496 friend class Query;
497
498 private:
499 std::string name;
500 std::vector<bool> KeyBindings;
501 std::vector<bool> KeySwitchBindings;
502 SampleManager* pSampleManager;
503 LookupTable* pLookupTable;
504 LookupTable* pLookupTableCC[128];
505 };
506
507 /////////////////////////////////////////////////////////////
508 // class Group
509
510 /// A Group act just as a template containing Region default values
511 class Group :
512 public Definition
513 {
514 public:
515 Group();
516 virtual ~Group();
517
518 /// Reset Group to default values
519 void Reset();
520
521 /// Create a new Region
522 Region* RegionFactory();
523
524 // id counter
525 int id;
526
527 };
528
529 /////////////////////////////////////////////////////////////
530 // class File
531
532 /// Parses SFZ files and provides abstract access to the data
533 class File
534 {
535 public:
536 /// Load an existing SFZ file
537 File(std::string file, SampleManager* pSampleManager = NULL);
538 virtual ~File();
539
540 /// Returns a pointer to the instrument object
541 Instrument* GetInstrument();
542
543 private:
544 void push_header(std::string token);
545 void push_opcode(std::string token);
546 int parseKey(const std::string& value);
547 EG& eg(int x);
548 EGNode& egnode(int x, int y);
549
550 std::string currentDir;
551 /// Pointer to the Instrument belonging to this file
552 Instrument* _instrument;
553
554 // state variables
555 enum section_t { UNKNOWN, GROUP, REGION, CONTROL };
556 section_t _current_section;
557 Region* _current_region;
558 Group* _current_group;
559 Definition* pCurDef;
560
561 // control header directives
562 std::string default_path;
563 int octave_offset;
564 int note_offset;
565 };
566
567 } // !namespace sfz
568
569 #endif // !LIBSFZ_SFZ_H

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC