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