/[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 2072 - (show annotations) (download) (as text)
Sat Mar 20 11:37:52 2010 UTC (14 years, 1 month ago) by persson
File MIME type: text/x-c++hdr
File size: 16809 byte(s)
* sfz engine: added support for random, seq_position, seq_length and
  volume
* sfz parser: added v1 LFO opcodes (no support in engine yet)

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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC