Class: SynthBlocks::Mod::AdsrOld
- Inherits:
-
Object
- Object
- SynthBlocks::Mod::AdsrOld
- Defined in:
- lib/synth_blocks/mod/adsr_old.rb
Overview
Implementation of a linear ADSR envelope generator with a tracking value so that envelope restarts don’t click
Instance Attribute Summary collapse
-
#attack ⇒ Object
attack time in seconds.
-
#decay ⇒ Object
decay time in seconds.
-
#release ⇒ Object
release time in seconds.
-
#sustain ⇒ Object
sustain level (0.0-1.0).
Instance Method Summary collapse
-
#initialize(attack, decay, sustain, release) ⇒ AdsrOld
constructor
Creates new ADSR envelope.
-
#run(t, released) ⇒ Object
run the envelope.
Constructor Details
#initialize(attack, decay, sustain, release) ⇒ AdsrOld
Creates new ADSR envelope
attack, decay and release are times in seconds (as float)
sustain should be between 0 and 1
28 29 30 31 32 33 34 35 |
# File 'lib/synth_blocks/mod/adsr_old.rb', line 28 def initialize(attack, decay, sustain, release) @value = 0 @start_value = 0 @attack = attack @decay = decay @sustain = sustain @release = release end |
Instance Attribute Details
#attack ⇒ Object
attack time in seconds
9 10 11 |
# File 'lib/synth_blocks/mod/adsr_old.rb', line 9 def attack @attack end |
#decay ⇒ Object
decay time in seconds
13 14 15 |
# File 'lib/synth_blocks/mod/adsr_old.rb', line 13 def decay @decay end |
#release ⇒ Object
release time in seconds
20 21 22 |
# File 'lib/synth_blocks/mod/adsr_old.rb', line 20 def release @release end |
#sustain ⇒ Object
sustain level (0.0-1.0)
16 17 18 |
# File 'lib/synth_blocks/mod/adsr_old.rb', line 16 def sustain @sustain end |
Instance Method Details
#run(t, released) ⇒ Object
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
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/synth_blocks/mod/adsr_old.rb', line 42 def run(t, released) attack_decay = attack + decay if !released if t < 0.0001 # initialize start value (slightly hacky, but works) @start_value = @value return @start_value end if t <= attack # attack return @value = linear(@start_value, 1, attack, t) end if t > attack && t < attack_decay # decay return @value = linear(1.0, sustain, decay, t - attack) end if t >= attack + decay # sustain return @value = sustain end else # release if released <= attack # when released in attack phase attack_level = linear(@start_value, 1, attack, released) return [linear(attack_level, 0, release, t - released), 0].max end if released > attack && released <= attack_decay # when released in decay phase decay_level = linear(1.0, sustain, decay, released - attack) return @value = [linear(decay_level, 0, release, t - released), 0].max end if released > attack_decay # normal release return @value = [linear(sustain, 0, release, t - released), 0].max end end 0.0 end |