Class: Ruck::Generators::ADSR

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

Instance Attribute Summary collapse

Attributes included from UGen

#name

Instance Method Summary collapse

Methods included from Source

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

Methods included from Target

#add_source, #remove_source

Methods included from UGen

#to_s

Constructor Details

#initialize(attrs = {}) ⇒ ADSR

Returns a new instance of ADSR.



341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/ruck/ugen/general.rb', line 341

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.



336
337
338
# File 'lib/ruck/ugen/general.rb', line 336

def attack_gain
  @attack_gain
end

#attack_timeObject

Returns the value of attribute attack_time.



335
336
337
# File 'lib/ruck/ugen/general.rb', line 335

def attack_time
  @attack_time
end

#decay_timeObject

Returns the value of attribute decay_time.



337
338
339
# File 'lib/ruck/ugen/general.rb', line 337

def decay_time
  @decay_time
end

#release_timeObject

Returns the value of attribute release_time.



339
340
341
# File 'lib/ruck/ugen/general.rb', line 339

def release_time
  @release_time
end

#sustain_gainObject

Returns the value of attribute sustain_gain.



338
339
340
# File 'lib/ruck/ugen/general.rb', line 338

def sustain_gain
  @sustain_gain
end

Instance Method Details

#attr_namesObject



396
397
398
# File 'lib/ruck/ugen/general.rb', line 396

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

#next(now) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/ruck/ugen/general.rb', line 356

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



389
390
391
392
393
394
# File 'lib/ruck/ugen/general.rb', line 389

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

#onObject



382
383
384
385
386
387
# File 'lib/ruck/ugen/general.rb', line 382

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