--- linuxsampler/trunk/src/engines/sfz/sfz.h 2009/10/27 19:04:57 2018 +++ linuxsampler/trunk/src/engines/sfz/sfz.h 2010/08/10 12:05:19 2114 @@ -2,8 +2,8 @@ * * * LinuxSampler - modular, streaming capable sampler * * * - * Copyright (C) 2008-2009 Anders Dahnielson * - * Copyright (C) 2009 Grigor Iliev * + * Copyright (C) 2008 Anders Dahnielson * + * Copyright (C) 2009 - 2010 Anders Dahnielson and Grigor Iliev * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -32,13 +32,14 @@ #include "../common/SampleFile.h" #include "../common/SampleManager.h" +#include "../../common/ArrayList.h" #define TRIGGER_ATTACK ((unsigned char) (1 << 0)) // 0x01 #define TRIGGER_RELEASE ((unsigned char) (1 << 1)) // 0x02 #define TRIGGER_FIRST ((unsigned char) (1 << 2)) // 0x04 #define TRIGGER_LEGATO ((unsigned char) (1 << 3)) // 0x08 -namespace sfz +namespace sfz { // Forward declarations class Articulation; @@ -46,11 +47,18 @@ class Group; class Instrument; class File; + class LookupTable; + + class Sample : public LinuxSampler::SampleFileBase { + public: + Sample(String File, bool DontClose = false) : LinuxSampler::SampleFileBase(File, DontClose) { } + virtual ~Sample() { } + }; // Enumerations enum sw_vel_t { VEL_CURRENT, VEL_PREVIOUS }; enum off_mode_t { OFF_FAST, OFF_NORMAL }; - enum loop_mode_t { NO_LOOP, ONE_SHOT, LOOP_CONTINOUS, LOOP_SUSTAIN }; + enum loop_mode_t { NO_LOOP, ONE_SHOT, LOOP_CONTINUOUS, LOOP_SUSTAIN }; enum curve_t { GAIN, POWER }; enum filter_t { LPF_1P, HPF_1P, BPF_1P, BRF_1P, APF_1P, LPF_2P, HPF_2P, BPF_2P, BRF_2P, PKF_2P, @@ -60,16 +68,16 @@ typedef unsigned char trigger_t; typedef unsigned char uint8_t; - class SampleManager : public LinuxSampler::SampleManager { + class SampleManager : public LinuxSampler::SampleManager { public: - LinuxSampler::SampleFile* FindSample(std::string samplePath); + Sample* FindSample(std::string samplePath); protected: - virtual void OnSampleInUse(LinuxSampler::SampleFile* pSample) { + virtual void OnSampleInUse(Sample* pSample) { pSample->Open(); } - virtual void OnSampleInNotUse(LinuxSampler::SampleFile* pSample) { + virtual void OnSampleInNotUse(Sample* pSample) { pSample->Close(); } }; @@ -208,6 +216,69 @@ virtual ~Articulation(); }; + class EGNode + { + public: + float time; + float level; + float shape; + float curve; + EGNode(); + }; + + class EG + { + public: + LinuxSampler::ArrayList node; + int sustain; + int loop; + int loop_count; + float amplitude; + float cutoff; + EG(); + }; + + // Fixed size array with copy-on-write semantics + template + class Array + { + private: + struct Rep { + int refcount; + T a[128]; + + Rep() : refcount(1) { } + static void release(Rep* rep) { + if (!--rep->refcount) delete rep; + } + } *ptr; + public: + Array() : ptr(0) { } + ~Array() { Rep::release(ptr); } + + Array& operator=(const Array& array) { + if (this != &array) { + ptr = array.ptr; + if (ptr) ptr->refcount++; + } + return *this; + } + + const T& operator[](int i) const { return ptr->a[i]; } + + void set(int i, const T& v) { + if (!ptr) { + ptr = new Rep; + } else if (ptr->refcount > 1 && ptr->a[i] != v) { + Rep* newptr = new Rep(*ptr); + newptr->refcount = 1; + Rep::release(ptr); + ptr = newptr; + } + ptr->a[i] = v; + } + }; + ///////////////////////////////////////////////////////////// // class Definition @@ -225,9 +296,9 @@ int lochan; int hichan; int lokey; int hikey; int lovel; int hivel; - std::vector locc; std::vector hicc; + Array locc; Array hicc; int lobend; int hibend; - int lobpm; int hibpm; + float lobpm; float hibpm; int lochanaft; int hichanaft; int lopolyaft; int hipolyaft; int loprog; int hiprog; @@ -237,8 +308,8 @@ int seq_length; int seq_position; - std::vector start_locc; std::vector start_hicc; - std::vector stop_locc; std::vector stop_hicc; + Array start_locc; Array start_hicc; + Array stop_locc; Array stop_hicc; int sw_lokey; int sw_hikey; int sw_last; @@ -249,20 +320,20 @@ trigger_t trigger; - optional group; - optional off_by; + uint group; + uint off_by; off_mode_t off_mode; - std::vector on_locc; std::vector on_hicc; + Array on_locc; Array on_hicc; // sample player optional count; - optional delay; optional delay_random; std::vector > delay_oncc; + optional delay; optional delay_random; Array > delay_oncc; optional delay_beats; optional stop_beats; - optional delay_samples; std::vector > delay_samples_oncc; + optional delay_samples; Array > delay_samples_oncc; optional end; optional loop_crossfade; - optional offset; optional offset_random; std::vector > offset_oncc; + optional offset; optional offset_random; Array > offset_oncc; loop_mode_t loop_mode; optional loop_start; optional loop_end; optional sync_beats; @@ -273,17 +344,17 @@ float pan; float width; float position; - float amp_keytrack; int amp_keycenter; float amp_veltrack; std::vector amp_velcurve_; float amp_random; + float amp_keytrack; int amp_keycenter; float amp_veltrack; Array amp_velcurve; float amp_random; float rt_decay; - std::vector gain_oncc; + Array gain_oncc; int xfin_lokey; int xfin_hikey; int xfout_lokey; int xfout_hikey; curve_t xf_keycurve; int xfin_lovel; int xfin_hivel; int xfout_lovel; int xfout_hivel; curve_t xf_velcurve; - std::vector xfin_locc; std::vector xfin_hicc; - std::vector xfout_locc; std::vector xfout_hicc; + Array xfin_locc; Array xfin_hicc; + Array xfout_locc; Array xfout_hicc; curve_t xf_cccurve; // pitch @@ -295,17 +366,17 @@ // filter filter_t fil_type; filter_t fil2_type; optional cutoff; optional cutoff2; - std::vector cutoff_oncc; std::vector cutoff2_oncc; - std::vector cutoff_smoothcc; std::vector cutoff2_smoothcc; - std::vector cutoff_stepcc; std::vector cutoff2_stepcc; - std::vector cutoff_curvecc; std::vector cutoff2_curvecc; + Array cutoff_oncc; Array cutoff2_oncc; + Array cutoff_smoothcc; Array cutoff2_smoothcc; + Array cutoff_stepcc; Array cutoff2_stepcc; + Array cutoff_curvecc; Array cutoff2_curvecc; int cutoff_chanaft; int cutoff2_chanaft; int cutoff_polyaft; int cutoff2_polyaft; float resonance; float resonance2; - std::vector resonance_oncc; std::vector resonance2_oncc; - std::vector resonance_smoothcc; std::vector resonance2_smoothcc; - std::vector resonance_stepcc; std::vector resonance2_stepcc; - std::vector resonance_curvecc; std::vector resonance2_curvecc; + Array resonance_oncc; Array resonance2_oncc; + Array resonance_smoothcc; Array resonance2_smoothcc; + Array resonance_stepcc; Array resonance2_stepcc; + Array resonance_curvecc; Array resonance2_curvecc; int fil_keytrack; int fil2_keytrack; int fil_keycenter; int fil2_keycenter; int fil_veltrack; int fil2_veltrack; @@ -313,18 +384,50 @@ // per voice equalizer float eq1_freq; float eq2_freq; float eq3_freq; - std::vector eq1_freq_oncc; std::vector eq2_freq_oncc; std::vector eq3_freq_oncc; + Array eq1_freq_oncc; Array eq2_freq_oncc; Array eq3_freq_oncc; float eq1_vel2freq; float eq2_vel2freq; float eq3_vel2freq; float eq1_bw; float eq2_bw; float eq3_bw; - std::vector eq1_bw_oncc; std::vector eq2_bw_oncc; std::vector eq3_bw_oncc; + Array eq1_bw_oncc; Array eq2_bw_oncc; Array eq3_bw_oncc; float eq1_gain; float eq2_gain; float eq3_gain; - std::vector eq1_gain_oncc; std::vector eq2_gain_oncc; std::vector eq3_gain_oncc; + Array eq1_gain_oncc; Array eq2_gain_oncc; Array eq3_gain_oncc; float eq1_vel2gain; float eq2_vel2gain; float eq3_vel2gain; //Deprecated (from version 1) float ampeg_delay, ampeg_start, ampeg_attack, ampeg_hold, ampeg_decay, ampeg_sustain, ampeg_release; float fileg_delay, fileg_start, fileg_attack, fileg_hold, fileg_decay, fileg_sustain, fileg_release; float pitcheg_delay, pitcheg_start, pitcheg_attack, pitcheg_hold, pitcheg_decay, pitcheg_sustain, pitcheg_release; + float amplfo_delay, amplfo_fade, amplfo_freq, amplfo_depth; + float fillfo_delay, fillfo_fade, fillfo_freq, fillfo_depth; + float pitchlfo_delay, pitchlfo_fade, pitchlfo_freq; + int pitchlfo_depth; + + // envelope generators + LinuxSampler::ArrayList eg; + }; + + class Query { + public: + uint8_t chan; // MIDI channel + uint8_t key; // MIDI note TODO: or controller + uint8_t vel; // MIDI velocity + int bend; // MIDI pitch bend + uint8_t bpm; // host BPM + uint8_t chanaft; // MIDI channel pressure + uint8_t polyaft; // MIDI polyphonic aftertouch + uint8_t prog; // MIDI program change + float rand; // generated random number + trigger_t trig; // how it was triggered + uint8_t* cc; // all 128 CC values + float timer; // time since previous region in the group was triggered + bool* sw; // state of region key switches, 128 possible values + uint8_t last_sw_key; // last key pressed in the key switch range + uint8_t prev_sw_key; // previous note value + + void search(const Instrument* pInstrument); + Region* next(); + private: + LinuxSampler::ArrayList* pRegionList; + int regionIndex; }; ///////////////////////////////////////////////////////////// @@ -338,34 +441,38 @@ Region(); virtual ~Region(); - LinuxSampler::SampleFile* pSample; - LinuxSampler::SampleFile* GetSample(bool create = true); + Sample* pSample; + Sample* GetSample(bool create = true); void DestroySampleIfNotUsed(); Region* GetParent() { return this; }; // needed by EngineBase Instrument* GetInstrument() { return pInstrument; } void SetInstrument(Instrument* pInstrument) { this->pInstrument = pInstrument; } - /// Return true if region is triggered by key - bool OnKey(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); + bool HasLoop(); + uint GetLoopStart(); + uint GetLoopEnd(); + uint GetLoopCount(); + + /// Return true if region is triggered by key. Region is + /// assumed to come from a search in the lookup table. + bool OnKey(const Query& q); /// Return true if region is triggered by control change bool OnControl(uint8_t chan, uint8_t cont, uint8_t val, 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); + float timer, bool* sw, uint8_t last_sw_key, uint8_t prev_sw_key); /// Return an articulation for the current state - 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); // unique region id int id; private: Instrument* pInstrument; + int seq_counter; }; ///////////////////////////////////////////////////////////// @@ -381,16 +488,6 @@ std::string GetName() { return name; } SampleManager* GetSampleManager() { return pSampleManager; } - /** - * @returns All regions that are triggered by key - */ - std::vector 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 - ); - bool DestroyRegion(Region* pRegion); bool HasKeyBinding(uint8_t key); bool HasKeySwitchBinding(uint8_t key); @@ -398,13 +495,15 @@ /// List of Regions belonging to this Instrument std::vector regions; - friend class sfz::File; + friend class File; + friend class Query; private: std::string name; std::vector KeyBindings; std::vector KeySwitchBindings; SampleManager* pSampleManager; + LookupTable* pLookupTable; }; ///////////////////////////////////////////////////////////// @@ -446,8 +545,9 @@ private: void push_header(std::string token); void push_opcode(std::string token); - int parseInt(std::string value); - float parseFloat(std::string value); + int parseKey(const std::string& value); + EG& eg(int x); + EGNode& egnode(int x, int y); std::string currentDir; /// Pointer to the Instrument belonging to this file