class SynthBlocks::Mod::Adsr
Attributes
attack[RW]
attack time in seconds
decay[RW]
decay time in seconds
release[RW]
release time in seconds
sustain[RW]
sustain level (0.0-1.0)
Public Class Methods
new(attack, decay, sustain, release)
click to toggle source
Creates new ADSR envelope
attack, decay and release are times in seconds (as float)
sustain should be between 0 and 1
# File lib/synth_blocks/mod/adsr.rb, line 25 def initialize(attack, decay, sustain, release) @value = 0 @start_time = 0 @last_t = 0 @done = false @start_value = 0 @attack = attack @decay = decay @sustain = sustain @release = release end
Public Instance Methods
run(t, released)
click to toggle source
run the envelope.
if released is given (should be <= t), the envelope will enter the release stage returns the current value between 0 and 1
# File lib/synth_blocks/mod/adsr.rb, line 42 def run(t, released) delta = t - @last_t @last_t = t if released return 0 if @done @value += -(@sustain/@release) * (delta) if @value <= 0 @value = 0 @done = true end return @value else if t < 0.0001 # initialize start value (slightly hacky, but works) @start_time = t return @value end if t <= @attack return @value += 1/@attack * delta elsif t <= @attack + @decay return @value += -(1-@sustain)/@decay * delta else return @value = @sustain end end @a_s = 1 / @attack end