rADSR

Attack/decay/sustain/release envelope with retrigger and hard-reset inputs
Author: Remco van der Most
License: BSD
Github: sss/env/rADSR.axo

Inlets

bool32.risingfalling gate

bool32 retrig

bool32 reset

Outlets

frac32.positive envelope output

Parameters

frac32.s.map.kdecaytime.exp d

frac32.s.map.kdecaytime.exp r

frac32.u.map s

frac32.s.map modrate

frac32.s.map mod

frac32.s.map.klineartime.exp2 a

Declaration
int8_t stage;
int ntrig;
int32_t val;
bool rtrg;
bool rst;
uint32_t cnt;
Init
stage = 0;
ntrig = 0;
val = 0;
Control Rate
int32_t rate;
MTOF(param_modrate, rate)
rate = rate >> 3;
int32_t base;
MTOF(0, base)
float32_t ratio;
int32_t mod;
MTOF(___SMMUL(-param_mod, cnt), mod)
ratio = (float32_t)base / mod;
if (inlet_gate > 0) {
  cnt = __USAT(cnt + rate, 30);
}
bool retrig = inlet_retrig && inlet_gate;
if ((retrig > 0) && !rtrg) {
  rtrg = 1;
  stage = 1;
  cnt = 0;
} else if (retrig < 1) {
  rtrg = 0;
}
if ((inlet_reset > 0) && !rst) {
  rst = 1;
  val = 0;
  cnt = 0;
  stage = 1;
} else if (inlet_reset < 1) {
  rst = 0;
}
if ((inlet_gate > 0) && !ntrig) {
  stage = 1;
  ntrig = 1;
  cnt = 0;
}
if (!(inlet_gate > 0) && ntrig) {
  stage = 0;
  ntrig = 0;
}

if (stage == 0) {
  val = ___SMMUL(val, param_r * ratio) << 1;
} else if (stage == 1) {
  val = val + param_a;
  if (val < 0) {
    val = 0x7FFFFFFF;
    stage = 2;
  }
} else if (stage == 2) {
  val = (param_s << 4) + (___SMMUL(val - (param_s << 4), param_d) << 1);
}

outlet_env = val >> 4;

Privacy

© 2024 Zrna Research