BogaudioModules

BogaudioModules for VCV Rack
Log | Files | Refs | README | LICENSE

slew_common.cpp (1698B)


      1 
      2 #include "slew_common.hpp"
      3 
      4 float RiseFallShapedSlewLimiter::timeMS(int c, Param& param, Input* input, float maxMS) {
      5 	float time = clamp(param.getValue(), 0.0f, 1.0f);
      6 	if (input && input->isConnected()) {
      7 		time *= clamp(input->getPolyVoltage(c) / 10.0f, 0.0f, 1.0f);
      8 	}
      9 	return time * time * maxMS;
     10 }
     11 
     12 float RiseFallShapedSlewLimiter::shape(int c, Param& param, bool invert, Input* cv, ShapeCVMode mode) {
     13 	float shape = param.getValue();
     14 	if (invert) {
     15 		shape *= -1.0f;
     16 	}
     17 	if (cv && mode != OFF_SCVM) {
     18 		float v = cv->getPolyVoltage(c) / 5.0f;
     19 		if (mode == INVERTED_SCVM) {
     20 			v = -v;
     21 		}
     22 		shape = clamp(shape + v, -1.0f, 1.0f);
     23 	}
     24 	if (shape < 0.0) {
     25 		shape = 1.0f + shape;
     26 		shape = _rise.minShape + shape * (1.0f - _rise.minShape);
     27 	}
     28 	else {
     29 		shape += 1.0f;
     30 	}
     31 	return shape;
     32 }
     33 
     34 void RiseFallShapedSlewLimiter::modulate(
     35 	float sampleRate,
     36 	Param& riseParam,
     37 	Input* riseInput,
     38 	float riseMaxMS,
     39 	Param& riseShapeParam,
     40 	Param& fallParam,
     41 	Input* fallInput,
     42 	float fallMaxMS,
     43 	Param& fallShapeParam,
     44 	int c,
     45 	bool invertRiseShape,
     46 	Input* shapeCV,
     47 	ShapeCVMode riseShapeMode,
     48 	ShapeCVMode fallShapeMode
     49 ) {
     50 	_rise.setParams(
     51 		sampleRate,
     52 		timeMS(c, riseParam, riseInput, riseMaxMS),
     53 		shape(c, riseShapeParam, invertRiseShape, shapeCV, riseShapeMode)
     54 	);
     55 	_fall.setParams(
     56 		sampleRate,
     57 		timeMS(c, fallParam, fallInput, fallMaxMS),
     58 		shape(c, fallShapeParam, false, shapeCV, fallShapeMode)
     59 	);
     60 }
     61 
     62 float RiseFallShapedSlewLimiter::next(float sample) {
     63 	if (sample > _last) {
     64 		if (!_rising) {
     65 			_rising = true;
     66 			_rise._last = _last;
     67 		}
     68 		return _last = _rise.next(sample);
     69 	}
     70 
     71 	if (_rising) {
     72 		_rising = false;
     73 		_fall._last = _last;
     74 	}
     75 	return _last = _fall.next(sample);
     76 }