Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- **UPDATE: I dug around in the Surge source code and found the relevant bits here, I hope this helps**
- ```
- class ControllerModulationSource : public ModulationSource
- {
- public:
- // Smoothing and Shaping Behaviors
- enum SmoothingMode
- {
- LEGACY = -1, // This is (1) the exponential backoff and (2) not streamed.
- SLOW_EXP, // Legacy with a sigma clamp
- FAST_EXP, // Faster Legacy with a sigma clamp
- FAST_LINE, // Linearly move
- DIRECT // Apply the value directly
- } smoothingMode = LEGACY;
- ControllerModulationSource()
- {
- target = 0.f;
- output = 0.f;
- bipolar = false;
- changed = true;
- smoothingMode = LEGACY;
- }
- ControllerModulationSource(SmoothingMode mode) : ControllerModulationSource()
- {
- smoothingMode = mode;
- }
- virtual ~ControllerModulationSource() {}
- void set_target(float f)
- {
- target = f;
- startingpoint = output;
- changed = true;
- }
- void init(float f)
- {
- target = f;
- output = f;
- startingpoint = f;
- changed = true;
- }
- void set_target01(float f, bool updatechanged = true)
- {
- if (bipolar)
- target = 2.f * f - 1.f;
- else
- target = f;
- startingpoint = output;
- if (updatechanged)
- changed = true;
- }
- virtual float get_output01(int i) override
- {
- if (bipolar)
- return 0.5f + 0.5f * output;
- return output;
- }
- virtual float get_target01()
- {
- if (bipolar)
- return 0.5f + 0.5f * target;
- return target;
- }
- virtual bool has_changed(bool reset)
- {
- if (changed)
- {
- if (reset)
- changed = false;
- return true;
- }
- return false;
- }
- virtual void reset() override
- {
- target = 0.f;
- output = 0.f;
- bipolar = false;
- }
- inline void processSmoothing(SmoothingMode mode, float sigma)
- {
- if (mode == LEGACY || mode == SLOW_EXP || mode == FAST_EXP)
- {
- float b = fabs(target - output);
- if (b < sigma && mode != LEGACY)
- {
- output = target;
- }
- else
- {
- float a = (mode == FAST_EXP ? 0.99f : 0.9f) * 44100 * samplerate_inv * b;
- output = (1 - a) * output + a * target;
- }
- return;
- };
- if (mode == FAST_LINE)
- {
- /*
- * Apply a constant change until we get there.
- * Rate is set so we cover the entire range (0,1)
- * in 50 blocks at 44k
- */
- float sampf = samplerate / 44100;
- float da = (target - startingpoint) / (50 * sampf);
- float b = target - output;
- if (fabs(b) < fabs(da))
- {
- output = target;
- }
- else
- {
- output += da;
- }
- }
- if (mode == DIRECT)
- {
- output = target;
- }
- }
- virtual void process_block() override
- {
- processSmoothing(smoothingMode, smoothingMode == FAST_EXP ? 0.005f : 0.0025f);
- }
- virtual bool process_block_until_close(float sigma)
- {
- if (smoothingMode == LEGACY)
- processSmoothing(SLOW_EXP, sigma);
- else
- processSmoothing(smoothingMode, sigma);
- return (output != target); // continue
- }
- virtual bool is_bipolar() override { return bipolar; }
- virtual void set_bipolar(bool b) override { bipolar = b; }
- float target, startingpoint;
- int id; // can be used to assign the controller to a parameter id
- bool bipolar;
- bool changed;
- };
- ```
- **And then, elsewhere in the code, the smoothing is applied to these midi signals:**
- ```
- scene.modsources[ms_modwheel] = new ControllerModulationSource(storage.smoothingMode);
- scene.modsources[ms_breath] = new ControllerModulationSource(storage.smoothingMode);
- scene.modsources[ms_expression] = new ControllerModulationSource(storage.smoothingMode);
- scene.modsources[ms_sustain] = new ControllerModulationSource(storage.smoothingMode);
- scene.modsources[ms_aftertouch] = new ControllerModulationSource(storage.smoothingMode);
- scene.modsources[ms_pitchbend] = new ControllerModulationSource(storage.smoothingMode);
- scene.modsources[ms_lowest_key] = new ControllerModulationSource(storage.smoothingMode);
- scene.modsources[ms_highest_key] = new ControllerModulationSource(storage.smoothingMode);
- scene.modsources[ms_latest_key] = new ControllerModulationSource(storage.smoothingMode);
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement