Class: Ruck::UGen::Generators::ADSR

Inherits:
Object
  • Object
show all
Includes:
Source, Target, UGenBase
Defined in:
lib/ruck/ugen/basic.rb

Instance Attribute Summary collapse

Attributes included from UGenBase

#name

Instance Method Summary collapse

Methods included from Source

#<<, #>>, #last, #out, #out_channels

Methods included from Target

#add_source, #remove_source

Methods included from UGenBase

#to_s

Constructor Details

#initialize(attrs = {}) ⇒ ADSR

Returns a new instance of ADSR.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ruck/ugen/basic.rb', line 171

def initialize(attrs = {})
  parse_attrs({ :attack_time => 50.ms,
                :attack_gain => 1.0,
                :decay_time => 50.ms,
                :sustain_gain => 0.5,
                :release_time => 500.ms }.merge(attrs))

  @ramp = Ramp.new

  @ins = []
  @last = 0.0
  @gain = 0.0
  @state = :idle
end

Instance Attribute Details

#attack_gainObject

Returns the value of attribute attack_gain.



166
167
168
# File 'lib/ruck/ugen/basic.rb', line 166

def attack_gain
  @attack_gain
end

#attack_timeObject

Returns the value of attribute attack_time.



165
166
167
# File 'lib/ruck/ugen/basic.rb', line 165

def attack_time
  @attack_time
end

#decay_timeObject

Returns the value of attribute decay_time.



167
168
169
# File 'lib/ruck/ugen/basic.rb', line 167

def decay_time
  @decay_time
end

#release_timeObject

Returns the value of attribute release_time.



169
170
171
# File 'lib/ruck/ugen/basic.rb', line 169

def release_time
  @release_time
end

#sustain_gainObject

Returns the value of attribute sustain_gain.



168
169
170
# File 'lib/ruck/ugen/basic.rb', line 168

def sustain_gain
  @sustain_gain
end

Instance Method Details

#attr_namesObject



226
227
228
# File 'lib/ruck/ugen/basic.rb', line 226

def attr_names
  [:attack_time, :attack_gain, :decay_time, :sustain_gain, :release_time]
end

#next(now) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/ruck/ugen/basic.rb', line 186

def next(now)
  return @last if @now == now
  @now = now
  @gain = case @state
          when :idle
            0
          when :attack
            if @ramp.finished?
              @ramp.reset
              @ramp.from, @ramp.to = @ramp.last, @sustain_gain
              @ramp.duration = @decay_time
              @state = :decay
            end
            @ramp.next(now)
          when :decay
            @state = :sustain if @ramp.finished?
            @ramp.next(now)
          when :sustain
            @sustain_gain
          when :release
            @state = :idle if @ramp.finished?
            @ramp.next(now)
          end
  @last = @ins.inject(0) { |samp, ugen| samp += ugen.next(now) } * @gain
end

#offObject



219
220
221
222
223
224
# File 'lib/ruck/ugen/basic.rb', line 219

def off
  @ramp.reset
  @ramp.from, @ramp.to = @gain, 0
  @ramp.duration = @release_time
  @state = :release
end

#onObject



212
213
214
215
216
217
# File 'lib/ruck/ugen/basic.rb', line 212

def on
  @ramp.reset
  @ramp.from, @ramp.to = @gain, @attack_gain
  @ramp.duration = @attack_time
  @state = :attack
end