Class: Scruby::Env

Inherits:
Object show all
Defined in:
lib/scruby/env.rb

Constant Summary collapse

SHAPE_NAMES =
{
  :step        => 0,
  :lin         => 1,
  :linear      => 1,
  :exp         => 2,
  :exponential => 2,
  :sin         => 3,
  :sine        => 3,
  :wel         => 4,
  :welch       => 4,
  :sqr         => 6,
  :squared     => 6,
  :cub         => 7,
  :cubed       => 7
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(levels, times, curves = :lin, release_node = nil, loop_node = nil) ⇒ Env

Returns a new instance of Env.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
# File 'lib/scruby/env.rb', line 20

def initialize levels, times, curves = :lin, release_node = nil, loop_node = nil
  #times should be one less than levels size
  # raise( ArgumentError, 'levels and times must be array')
  @levels, @times, @curves, @release_node, @loop_node = levels, times, curves.to_array, release_node, loop_node
  raise ArgumentError, "levels and times should be array" unless levels.kind_of?(Array) and times.kind_of?(Array)
end

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



3
4
5
# File 'lib/scruby/env.rb', line 3

def array
  @array
end

#curvesObject

Returns the value of attribute curves.



3
4
5
# File 'lib/scruby/env.rb', line 3

def curves
  @curves
end

#levelsObject

Returns the value of attribute levels.



3
4
5
# File 'lib/scruby/env.rb', line 3

def levels
  @levels
end

#release_nodeObject

Returns the value of attribute release_node.



3
4
5
# File 'lib/scruby/env.rb', line 3

def release_node
  @release_node
end

#timesObject

Returns the value of attribute times.



3
4
5
# File 'lib/scruby/env.rb', line 3

def times
  @times
end

Class Method Details

.adsr(attackTime = 0.01, decayTime = 0.3, sustainLevel = 0.5, releaseTime = 1, peakLevel = 1, curve = -4,, bias = 0) ⇒ Object



54
55
56
# File 'lib/scruby/env.rb', line 54

def adsr attackTime = 0.01, decayTime = 0.3, sustainLevel = 0.5, releaseTime = 1, peakLevel = 1, curve = -4, bias = 0
  new [0, peakLevel, peakLevel * sustainLevel, 0].collect{ |e| e + bias }, [attackTime, decayTime, releaseTime], curve, 2
end

.asr(attackTime = 0.01, sustainLevel = 1, releaseTime = 1, curve = -4) ⇒ Object



58
59
60
# File 'lib/scruby/env.rb', line 58

def asr attackTime = 0.01, sustainLevel = 1, releaseTime = 1, curve = -4
  new [0, sustainLevel, 0], [attackTime, releaseTime], curve, 1
end

.cutoff(releaseTime = 0.1, level = 1, curve = :lin) ⇒ Object



46
47
48
# File 'lib/scruby/env.rb', line 46

def cutoff releaseTime = 0.1, level = 1, curve = :lin
  new [level, 0], [releaseTime], curve, 0
end

.dadsr(delayTime = 0.1, attackTime = 0.01, decayTime = 0.3, sustainLevel = 0.5, releaseTime = 1, peakLevel = 1, curve = -4,, bias = 0) ⇒ Object



50
51
52
# File 'lib/scruby/env.rb', line 50

def dadsr delayTime = 0.1, attackTime = 0.01, decayTime = 0.3, sustainLevel = 0.5, releaseTime = 1, peakLevel = 1, curve = -4, bias = 0
  new [0, 0, peakLevel, peakLevel * sustainLevel, 0].collect{ |e| e + bias }, [delayTime, attackTime, decayTime, releaseTime], curve, 3
end

.linen(attackTime = 0.01, sustainTime = 1, releaseTime = 1, level = 1, curve = :lin) ⇒ Object



42
43
44
# File 'lib/scruby/env.rb', line 42

def linen attackTime = 0.01, sustainTime = 1, releaseTime = 1, level = 1, curve = :lin
  new [0, level, level, 0], [attackTime, sustainTime, releaseTime], curve
end

.perc(attackTime = 0.01, releaseTime = 1, level = 1, curve = -4) ⇒ Object



38
39
40
# File 'lib/scruby/env.rb', line 38

def perc attackTime = 0.01, releaseTime = 1, level = 1, curve = -4
  new [0, level, 0], [attackTime, releaseTime], curve
end

.sine(dur = 1, level = 1) ⇒ Object



33
34
35
36
# File 'lib/scruby/env.rb', line 33

def sine dur = 1, level = 1
  dur = dur * 0.5
  new [0, level, 0], [dur, dur], :sine
end

.triangle(dur = 1, level = 1) ⇒ Object



28
29
30
31
# File 'lib/scruby/env.rb', line 28

def triangle dur = 1, level = 1
  dur = dur * 0.5
  new [0, level, 0], [dur, dur]
end

Instance Method Details

#collect_constantsObject

:nodoc:



90
91
# File 'lib/scruby/env.rb', line 90

def collect_constants #:nodoc:
end

#curve_valuesObject



76
77
78
79
80
# File 'lib/scruby/env.rb', line 76

def curve_values
  curves.collect do |curve|
    Ugens::Ugen.valid_input?( curve ) ? curve : 0
  end
end

#loop_nodeObject



86
87
88
# File 'lib/scruby/env.rb', line 86

def loop_node
  @loop_node ||= -99
end

#shape_numbersObject



70
71
72
73
74
# File 'lib/scruby/env.rb', line 70

def shape_numbers
  curves.collect do |curve|
    Ugens::Ugen.valid_input?( curve ) ? 5 : SHAPE_NAMES[curve]
  end
end

#to_arrayObject



65
66
67
68
# File 'lib/scruby/env.rb', line 65

def to_array
  contents = levels[0], times.size, release_node, loop_node
  contents + levels[1..-1].wrap_and_zip( times, shape_numbers, curve_values ).flatten
end