2 |
* * |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2008-2009 Anders Dahnielson <anders@dahnielson.com> * |
* Copyright (C) 2008 Anders Dahnielson <anders@dahnielson.com> * |
6 |
* Copyright (C) 2009 Grigor Iliev * |
* Copyright (C) 2009 - 2011 Anders Dahnielson and Grigor Iliev * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
32 |
|
|
33 |
#include "../common/SampleFile.h" |
#include "../common/SampleFile.h" |
34 |
#include "../common/SampleManager.h" |
#include "../common/SampleManager.h" |
35 |
|
#include "../../common/ArrayList.h" |
36 |
|
|
37 |
#define TRIGGER_ATTACK ((unsigned char) (1 << 0)) // 0x01 |
#define TRIGGER_ATTACK ((unsigned char) (1 << 0)) // 0x01 |
38 |
#define TRIGGER_RELEASE ((unsigned char) (1 << 1)) // 0x02 |
#define TRIGGER_RELEASE ((unsigned char) (1 << 1)) // 0x02 |
39 |
#define TRIGGER_FIRST ((unsigned char) (1 << 2)) // 0x04 |
#define TRIGGER_FIRST ((unsigned char) (1 << 2)) // 0x04 |
40 |
#define TRIGGER_LEGATO ((unsigned char) (1 << 3)) // 0x08 |
#define TRIGGER_LEGATO ((unsigned char) (1 << 3)) // 0x08 |
41 |
|
|
42 |
namespace sfz |
namespace sfz |
43 |
{ |
{ |
44 |
// Forward declarations |
// Forward declarations |
45 |
class Articulation; |
class Articulation; |
47 |
class Group; |
class Group; |
48 |
class Instrument; |
class Instrument; |
49 |
class File; |
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 |
// Enumerations |
61 |
enum sw_vel_t { VEL_CURRENT, VEL_PREVIOUS }; |
enum sw_vel_t { VEL_CURRENT, VEL_PREVIOUS }; |
62 |
enum off_mode_t { OFF_FAST, OFF_NORMAL }; |
enum off_mode_t { OFF_FAST, OFF_NORMAL }; |
63 |
enum loop_mode_t { NO_LOOP, ONE_SHOT, LOOP_CONTINOUS, LOOP_SUSTAIN }; |
enum loop_mode_t { NO_LOOP, ONE_SHOT, LOOP_CONTINUOUS, LOOP_SUSTAIN, LOOP_UNSET }; |
64 |
enum curve_t { GAIN, POWER }; |
enum curve_t { GAIN, POWER }; |
65 |
enum filter_t { LPF_1P, HPF_1P, BPF_1P, BRF_1P, APF_1P, |
enum filter_t { LPF_1P, HPF_1P, BPF_1P, BRF_1P, APF_1P, |
66 |
LPF_2P, HPF_2P, BPF_2P, BRF_2P, PKF_2P, |
LPF_2P, HPF_2P, BPF_2P, BRF_2P, PKF_2P, |
70 |
typedef unsigned char trigger_t; |
typedef unsigned char trigger_t; |
71 |
typedef unsigned char uint8_t; |
typedef unsigned char uint8_t; |
72 |
|
|
73 |
class SampleManager : public LinuxSampler::SampleManager<LinuxSampler::SampleFile, Region> { |
class SampleManager : public LinuxSampler::SampleManager<Sample, Region> { |
74 |
public: |
public: |
75 |
LinuxSampler::SampleFile* FindSample(std::string samplePath); |
Sample* FindSample(std::string samplePath, int offset); |
76 |
|
|
77 |
protected: |
protected: |
78 |
virtual void OnSampleInUse(LinuxSampler::SampleFile* pSample) { |
virtual void OnSampleInUse(Sample* pSample) { |
79 |
pSample->Open(); |
pSample->Open(); |
80 |
} |
} |
81 |
|
|
82 |
virtual void OnSampleInNotUse(LinuxSampler::SampleFile* pSample) { |
virtual void OnSampleInNotUse(Sample* pSample) { |
83 |
pSample->Close(); |
pSample->Close(); |
84 |
} |
} |
85 |
}; |
}; |
218 |
virtual ~Articulation(); |
virtual ~Articulation(); |
219 |
}; |
}; |
220 |
|
|
221 |
|
class EGNode |
222 |
|
{ |
223 |
|
public: |
224 |
|
float time; |
225 |
|
float level; |
226 |
|
float shape; |
227 |
|
float curve; |
228 |
|
EGNode(); |
229 |
|
}; |
230 |
|
|
231 |
|
class EG |
232 |
|
{ |
233 |
|
public: |
234 |
|
LinuxSampler::ArrayList<EGNode> node; |
235 |
|
int sustain; |
236 |
|
int loop; |
237 |
|
int loop_count; |
238 |
|
float amplitude; |
239 |
|
float cutoff; |
240 |
|
EG(); |
241 |
|
}; |
242 |
|
|
243 |
|
// Fixed size array with copy-on-write semantics |
244 |
|
template<class T> |
245 |
|
class Array |
246 |
|
{ |
247 |
|
private: |
248 |
|
struct Rep { |
249 |
|
int refcount; |
250 |
|
T a[128]; |
251 |
|
|
252 |
|
Rep() : refcount(1) { } |
253 |
|
static void release(Rep* rep) { |
254 |
|
if (!--rep->refcount) delete rep; |
255 |
|
} |
256 |
|
} *ptr; |
257 |
|
public: |
258 |
|
Array() : ptr(0) { } |
259 |
|
~Array() { Rep::release(ptr); } |
260 |
|
|
261 |
|
Array& operator=(const Array& array) { |
262 |
|
if (this != &array) { |
263 |
|
ptr = array.ptr; |
264 |
|
if (ptr) ptr->refcount++; |
265 |
|
} |
266 |
|
return *this; |
267 |
|
} |
268 |
|
|
269 |
|
const T& operator[](int i) const { return ptr->a[i]; } |
270 |
|
|
271 |
|
void set(int i, const T& v) { |
272 |
|
if (!ptr) { |
273 |
|
ptr = new Rep; |
274 |
|
} else if (ptr->refcount > 1 && ptr->a[i] != v) { |
275 |
|
Rep* newptr = new Rep(*ptr); |
276 |
|
newptr->refcount = 1; |
277 |
|
Rep::release(ptr); |
278 |
|
ptr = newptr; |
279 |
|
} |
280 |
|
ptr->a[i] = v; |
281 |
|
} |
282 |
|
}; |
283 |
|
|
284 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
285 |
// class Definition |
// class Definition |
286 |
|
|
298 |
int lochan; int hichan; |
int lochan; int hichan; |
299 |
int lokey; int hikey; |
int lokey; int hikey; |
300 |
int lovel; int hivel; |
int lovel; int hivel; |
301 |
std::vector<int> locc; std::vector<int> hicc; |
Array<int> locc; Array<int> hicc; |
302 |
int lobend; int hibend; |
int lobend; int hibend; |
303 |
int lobpm; int hibpm; |
float lobpm; float hibpm; |
304 |
int lochanaft; int hichanaft; |
int lochanaft; int hichanaft; |
305 |
int lopolyaft; int hipolyaft; |
int lopolyaft; int hipolyaft; |
306 |
int loprog; int hiprog; |
int loprog; int hiprog; |
310 |
int seq_length; |
int seq_length; |
311 |
int seq_position; |
int seq_position; |
312 |
|
|
313 |
std::vector<int> start_locc; std::vector<int> start_hicc; |
Array<int> start_locc; Array<int> start_hicc; |
314 |
std::vector<int> stop_locc; std::vector<int> stop_hicc; |
Array<int> stop_locc; Array<int> stop_hicc; |
315 |
|
|
316 |
int sw_lokey; int sw_hikey; |
int sw_lokey; int sw_hikey; |
317 |
int sw_last; |
int sw_last; |
322 |
|
|
323 |
trigger_t trigger; |
trigger_t trigger; |
324 |
|
|
325 |
optional<int> group; |
uint group; |
326 |
optional<int> off_by; |
uint off_by; |
327 |
off_mode_t off_mode; |
off_mode_t off_mode; |
328 |
|
|
329 |
std::vector<int> on_locc; std::vector<int> on_hicc; |
Array<int> on_locc; Array<int> on_hicc; |
330 |
|
|
331 |
// sample player |
// sample player |
332 |
optional<int> count; |
optional<int> count; |
333 |
optional<float> delay; optional<float> delay_random; std::vector<optional<float> > delay_oncc; |
optional<float> delay; optional<float> delay_random; Array<optional<float> > delay_oncc; |
334 |
optional<int> delay_beats; optional<int> stop_beats; |
optional<int> delay_beats; optional<int> stop_beats; |
335 |
optional<int> delay_samples; std::vector<optional<int> > delay_samples_oncc; |
optional<int> delay_samples; Array<optional<int> > delay_samples_oncc; |
336 |
optional<int> end; |
optional<int> end; |
337 |
optional<float> loop_crossfade; |
optional<float> loop_crossfade; |
338 |
optional<int> offset; optional<int> offset_random; std::vector<optional<int> > offset_oncc; |
optional<int> offset; optional<int> offset_random; Array<optional<int> > offset_oncc; |
339 |
loop_mode_t loop_mode; |
loop_mode_t loop_mode; |
340 |
optional<int> loop_start; optional<int> loop_end; |
optional<int> loop_start; optional<int> loop_end; |
341 |
optional<int> sync_beats; |
optional<int> sync_beats; |
346 |
float pan; |
float pan; |
347 |
float width; |
float width; |
348 |
float position; |
float position; |
349 |
float amp_keytrack; int amp_keycenter; float amp_veltrack; std::vector<float> amp_velcurve_; float amp_random; |
float amp_keytrack; int amp_keycenter; float amp_veltrack; Array<float> amp_velcurve; float amp_random; |
350 |
float rt_decay; |
float rt_decay; |
351 |
std::vector<float> gain_oncc; |
Array<float> gain_oncc; |
352 |
int xfin_lokey; int xfin_hikey; |
int xfin_lokey; int xfin_hikey; |
353 |
int xfout_lokey; int xfout_hikey; |
int xfout_lokey; int xfout_hikey; |
354 |
curve_t xf_keycurve; |
curve_t xf_keycurve; |
355 |
int xfin_lovel; int xfin_hivel; |
int xfin_lovel; int xfin_hivel; |
356 |
int xfout_lovel; int xfout_hivel; |
int xfout_lovel; int xfout_hivel; |
357 |
curve_t xf_velcurve; |
curve_t xf_velcurve; |
358 |
std::vector<int> xfin_locc; std::vector<int> xfin_hicc; |
Array<int> xfin_locc; Array<int> xfin_hicc; |
359 |
std::vector<int> xfout_locc; std::vector<int> xfout_hicc; |
Array<int> xfout_locc; Array<int> xfout_hicc; |
360 |
curve_t xf_cccurve; |
curve_t xf_cccurve; |
361 |
|
|
362 |
// pitch |
// pitch |
368 |
// filter |
// filter |
369 |
filter_t fil_type; filter_t fil2_type; |
filter_t fil_type; filter_t fil2_type; |
370 |
optional<float> cutoff; optional<float> cutoff2; |
optional<float> cutoff; optional<float> cutoff2; |
371 |
std::vector<int> cutoff_oncc; std::vector<int> cutoff2_oncc; |
Array<int> cutoff_oncc; Array<int> cutoff2_oncc; |
372 |
std::vector<int> cutoff_smoothcc; std::vector<int> cutoff2_smoothcc; |
int cutoff_cc; // TODO: this is just a temporary fix to avoid |
373 |
std::vector<int> cutoff_stepcc; std::vector<int> cutoff2_stepcc; |
// looping through the cutoff_oncc array |
374 |
std::vector<int> cutoff_curvecc; std::vector<int> cutoff2_curvecc; |
Array<int> cutoff_smoothcc; Array<int> cutoff2_smoothcc; |
375 |
|
Array<int> cutoff_stepcc; Array<int> cutoff2_stepcc; |
376 |
|
Array<int> cutoff_curvecc; Array<int> cutoff2_curvecc; |
377 |
int cutoff_chanaft; int cutoff2_chanaft; |
int cutoff_chanaft; int cutoff2_chanaft; |
378 |
int cutoff_polyaft; int cutoff2_polyaft; |
int cutoff_polyaft; int cutoff2_polyaft; |
379 |
float resonance; float resonance2; |
float resonance; float resonance2; |
380 |
std::vector<int> resonance_oncc; std::vector<int> resonance2_oncc; |
Array<int> resonance_oncc; Array<int> resonance2_oncc; |
381 |
std::vector<int> resonance_smoothcc; std::vector<int> resonance2_smoothcc; |
Array<int> resonance_smoothcc; Array<int> resonance2_smoothcc; |
382 |
std::vector<int> resonance_stepcc; std::vector<int> resonance2_stepcc; |
Array<int> resonance_stepcc; Array<int> resonance2_stepcc; |
383 |
std::vector<int> resonance_curvecc; std::vector<int> resonance2_curvecc; |
Array<int> resonance_curvecc; Array<int> resonance2_curvecc; |
384 |
int fil_keytrack; int fil2_keytrack; |
int fil_keytrack; int fil2_keytrack; |
385 |
int fil_keycenter; int fil2_keycenter; |
int fil_keycenter; int fil2_keycenter; |
386 |
int fil_veltrack; int fil2_veltrack; |
int fil_veltrack; int fil2_veltrack; |
388 |
|
|
389 |
// per voice equalizer |
// per voice equalizer |
390 |
float eq1_freq; float eq2_freq; float eq3_freq; |
float eq1_freq; float eq2_freq; float eq3_freq; |
391 |
std::vector<float> eq1_freq_oncc; std::vector<float> eq2_freq_oncc; std::vector<float> eq3_freq_oncc; |
Array<float> eq1_freq_oncc; Array<float> eq2_freq_oncc; Array<float> eq3_freq_oncc; |
392 |
float eq1_vel2freq; float eq2_vel2freq; float eq3_vel2freq; |
float eq1_vel2freq; float eq2_vel2freq; float eq3_vel2freq; |
393 |
float eq1_bw; float eq2_bw; float eq3_bw; |
float eq1_bw; float eq2_bw; float eq3_bw; |
394 |
std::vector<float> eq1_bw_oncc; std::vector<float> eq2_bw_oncc; std::vector<float> eq3_bw_oncc; |
Array<float> eq1_bw_oncc; Array<float> eq2_bw_oncc; Array<float> eq3_bw_oncc; |
395 |
float eq1_gain; float eq2_gain; float eq3_gain; |
float eq1_gain; float eq2_gain; float eq3_gain; |
396 |
std::vector<float> eq1_gain_oncc; std::vector<float> eq2_gain_oncc; std::vector<float> eq3_gain_oncc; |
Array<float> eq1_gain_oncc; Array<float> eq2_gain_oncc; Array<float> eq3_gain_oncc; |
397 |
float eq1_vel2gain; float eq2_vel2gain; float eq3_vel2gain; |
float eq1_vel2gain; float eq2_vel2gain; float eq3_vel2gain; |
398 |
|
|
399 |
|
//Deprecated (from version 1) |
400 |
|
float ampeg_delay, ampeg_start, ampeg_attack, ampeg_hold, ampeg_decay, ampeg_sustain, ampeg_release; |
401 |
|
float ampeg_vel2delay, ampeg_vel2attack, ampeg_vel2hold, ampeg_vel2decay, ampeg_vel2sustain, ampeg_vel2release; |
402 |
|
float fileg_delay, fileg_start, fileg_attack, fileg_hold, fileg_decay, fileg_sustain, fileg_release; |
403 |
|
float pitcheg_delay, pitcheg_start, pitcheg_attack, pitcheg_hold, pitcheg_decay, pitcheg_sustain, pitcheg_release; |
404 |
|
float amplfo_delay, amplfo_fade, amplfo_freq, amplfo_depth; |
405 |
|
float fillfo_delay, fillfo_fade, fillfo_freq, fillfo_depth; |
406 |
|
float pitchlfo_delay, pitchlfo_fade, pitchlfo_freq; |
407 |
|
int pitchlfo_depth; |
408 |
|
|
409 |
|
// envelope generators |
410 |
|
LinuxSampler::ArrayList<EG> eg; |
411 |
|
}; |
412 |
|
|
413 |
|
class Query { |
414 |
|
public: |
415 |
|
uint8_t chan; // MIDI channel |
416 |
|
uint8_t key; // MIDI note |
417 |
|
uint8_t vel; // MIDI velocity |
418 |
|
int bend; // MIDI pitch bend |
419 |
|
uint8_t bpm; // host BPM |
420 |
|
uint8_t chanaft; // MIDI channel pressure |
421 |
|
uint8_t polyaft; // MIDI polyphonic aftertouch |
422 |
|
uint8_t prog; // MIDI program change |
423 |
|
float rand; // generated random number |
424 |
|
trigger_t trig; // how it was triggered |
425 |
|
uint8_t* cc; // all 128 CC values |
426 |
|
float timer; // time since previous region in the group was triggered |
427 |
|
bool* sw; // state of region key switches, 128 possible values |
428 |
|
uint8_t last_sw_key; // last key pressed in the key switch range |
429 |
|
uint8_t prev_sw_key; // previous note value |
430 |
|
|
431 |
|
void search(const Instrument* pInstrument); |
432 |
|
void search(const Instrument* pInstrument, int triggercc); |
433 |
|
Region* next(); |
434 |
|
private: |
435 |
|
LinuxSampler::ArrayList<Region*>* pRegionList; |
436 |
|
int regionIndex; |
437 |
}; |
}; |
438 |
|
|
439 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
447 |
Region(); |
Region(); |
448 |
virtual ~Region(); |
virtual ~Region(); |
449 |
|
|
450 |
LinuxSampler::SampleFile* pSample; |
Sample* pSample; |
451 |
LinuxSampler::SampleFile* GetSample(bool create = true); |
Sample* GetSample(bool create = true); |
452 |
void DestroySampleIfNotUsed(); |
void DestroySampleIfNotUsed(); |
453 |
|
|
454 |
Region* GetParent() { return this; }; // needed by EngineBase |
Region* GetParent() { return this; }; // needed by EngineBase |
455 |
Instrument* GetInstrument() { return pInstrument; } |
Instrument* GetInstrument() { return pInstrument; } |
456 |
void SetInstrument(Instrument* pInstrument) { this->pInstrument = pInstrument; } |
void SetInstrument(Instrument* pInstrument) { this->pInstrument = pInstrument; } |
457 |
|
|
458 |
/// Return true if region is triggered by key |
bool HasLoop(); |
459 |
bool OnKey(uint8_t chan, uint8_t key, uint8_t vel, |
uint GetLoopStart(); |
460 |
int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, |
uint GetLoopEnd(); |
461 |
uint8_t prog, float rand, trigger_t trig, uint8_t* cc, |
uint GetLoopCount(); |
462 |
float timer, uint8_t seq, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key); |
|
463 |
|
/// Return true if region is triggered by key. Region is |
464 |
/// Return true if region is triggered by control change |
/// assumed to come from a search in the lookup table. |
465 |
bool OnControl(uint8_t chan, uint8_t cont, uint8_t val, |
bool OnKey(const Query& q); |
|
int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, |
|
|
uint8_t prog, float rand, trigger_t trig, uint8_t* cc, |
|
|
float timer, uint8_t seq, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key); |
|
466 |
|
|
467 |
/// Return an articulation for the current state |
/// Return an articulation for the current state |
468 |
Articulation* GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc); |
Articulation* GetArticulation(int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, uint8_t* cc); |
469 |
|
|
470 |
// unique region id |
// unique region id |
471 |
int id; |
int id; |
472 |
|
|
473 |
private: |
private: |
474 |
Instrument* pInstrument; |
Instrument* pInstrument; |
475 |
|
int seq_counter; |
476 |
}; |
}; |
477 |
|
|
478 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
488 |
std::string GetName() { return name; } |
std::string GetName() { return name; } |
489 |
SampleManager* GetSampleManager() { return pSampleManager; } |
SampleManager* GetSampleManager() { return pSampleManager; } |
490 |
|
|
|
/** |
|
|
* @returns All regions that are triggered by key |
|
|
*/ |
|
|
std::vector<Region*> GetRegionsOnKey ( |
|
|
uint8_t chan, uint8_t key, uint8_t vel, |
|
|
int bend, uint8_t bpm, uint8_t chanaft, uint8_t polyaft, |
|
|
uint8_t prog, float rand, trigger_t trig, uint8_t* cc, |
|
|
float timer, uint8_t seq, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key |
|
|
); |
|
|
|
|
491 |
bool DestroyRegion(Region* pRegion); |
bool DestroyRegion(Region* pRegion); |
492 |
bool HasKeyBinding(uint8_t key); |
bool HasKeyBinding(uint8_t key); |
493 |
bool HasKeySwitchBinding(uint8_t key); |
bool HasKeySwitchBinding(uint8_t key); |
495 |
/// List of Regions belonging to this Instrument |
/// List of Regions belonging to this Instrument |
496 |
std::vector<Region*> regions; |
std::vector<Region*> regions; |
497 |
|
|
498 |
friend class sfz::File; |
friend class File; |
499 |
|
friend class Query; |
500 |
|
|
501 |
private: |
private: |
502 |
std::string name; |
std::string name; |
503 |
std::vector<bool> KeyBindings; |
std::vector<bool> KeyBindings; |
504 |
std::vector<bool> KeySwitchBindings; |
std::vector<bool> KeySwitchBindings; |
505 |
SampleManager* pSampleManager; |
SampleManager* pSampleManager; |
506 |
|
LookupTable* pLookupTable; |
507 |
|
LookupTable* pLookupTableCC[128]; |
508 |
}; |
}; |
509 |
|
|
510 |
///////////////////////////////////////////////////////////// |
///////////////////////////////////////////////////////////// |
546 |
private: |
private: |
547 |
void push_header(std::string token); |
void push_header(std::string token); |
548 |
void push_opcode(std::string token); |
void push_opcode(std::string token); |
549 |
int parseInt(std::string value); |
int parseKey(const std::string& value); |
550 |
float parseFloat(std::string value); |
EG& eg(int x); |
551 |
|
EGNode& egnode(int x, int y); |
552 |
|
|
553 |
std::string currentDir; |
std::string currentDir; |
554 |
/// Pointer to the Instrument belonging to this file |
/// Pointer to the Instrument belonging to this file |