Class: Musicality::Change::Gradual::Trimmed

Inherits:
Musicality::Change::Gradual show all
Defined in:
lib/musicality/notation/model/change.rb,
lib/musicality/notation/conversion/change_conversion.rb

Constant Summary

Constants inherited from Musicality::Change::Gradual

LINEAR, SIGMOID, TRANSITIONS

Constants included from Packable

Packable::PACKED_CLASS_KEY

Instance Attribute Summary collapse

Attributes inherited from Musicality::Change::Gradual

#duration, #start_value, #transition

Attributes inherited from Musicality::Change

#end_value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Musicality::Change::Gradual

#absolute?, #relative?, #to_trimmed, #trim, #trim_left, #trim_right

Methods included from Packable

#class_str, included, #init_params, #pack, pack_val, recover_class, unpack_val

Constructor Details

#initialize(end_value, duration, transition, start_value: nil, preceding: 0, remaining: 0) ⇒ Trimmed

Returns a new instance of Trimmed.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/musicality/notation/model/change.rb', line 86

def initialize end_value, duration, transition, start_value: nil, preceding: 0, remaining: 0
  if preceding < 0
    raise NegativeError, "preceding (#{preceding}) is < 0"
  end
  
  if remaining <= 0
    raise NonPositiveError, "remaining (#{remaining}) is <= 0"
  end
  
  @preceding, @remaining = preceding, remaining
  super(end_value, duration, transition, start_value: start_value)
end

Instance Attribute Details

#precedingObject (readonly)

Returns the value of attribute preceding.



74
75
76
# File 'lib/musicality/notation/model/change.rb', line 74

def preceding
  @preceding
end

#remainingObject (readonly)

Returns the value of attribute remaining.



74
75
76
# File 'lib/musicality/notation/model/change.rb', line 74

def remaining
  @remaining
end

Class Method Details

.linear(end_value, duration, start_value: nil, preceding: 0, remaining: 0) ⇒ Object



76
77
78
79
# File 'lib/musicality/notation/model/change.rb', line 76

def self.linear end_value, duration, start_value: nil, preceding: 0, remaining: 0
  Trimmed.new(end_value, duration, LINEAR, start_value: start_value,
              preceding: preceding, remaining: remaining)
end

.sigmoid(end_value, duration, start_value: nil, preceding: 0, remaining: 0) ⇒ Object



81
82
83
84
# File 'lib/musicality/notation/model/change.rb', line 81

def self.sigmoid end_value, duration, start_value: nil, preceding: 0, remaining: 0
  Trimmed.new(end_value, duration, SIGMOID, start_value: start_value,
              preceding: preceding, remaining: remaining)
end

Instance Method Details

#==(other) ⇒ Object



107
108
109
# File 'lib/musicality/notation/model/change.rb', line 107

def ==(other)
  super(other) && @preceding == other.preceding && @remaining == other.remaining
end

#cloneObject



111
112
113
114
115
116
# File 'lib/musicality/notation/model/change.rb', line 111

def clone
  ev = block_given? ? yield(@end_value) : @end_value
  sv = (block_given? && !@start_value.nil?) ? yield(@start_value) : @start_value
  Trimmed.new(ev, @duration, @transition, start_value: sv,
    preceding: @preceding, remaining: @remaining)
end

#offsets(base_offset) ⇒ Object



39
40
41
42
# File 'lib/musicality/notation/conversion/change_conversion.rb', line 39

def offsets base_offset
  origin = base_offset - @preceding
  [ origin, base_offset, base_offset + @remaining, origin + @duration ]
end

#remap(base_offset, map) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/musicality/notation/conversion/change_conversion.rb', line 44

def remap base_offset, map
  x0 = base_offset - @preceding
  y0 = map[x0]
  new_dur = map[x0 + @duration] - y0
  x1 = base_offset
  y1 = map[x1]
  Trimmed.new(@end_value, new_dur, @transition, preceding: y1 - y0,
              remaining: map[x1 + @remaining] - y1)
end

#to_transition(offset, value) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/musicality/notation/conversion/change_conversion.rb', line 54

def to_transition offset, value
  x1,x2,x3 = offset - @preceding, offset, offset + @remaining
  x4 = x1 + @duration
  func = case @transition
  when LINEAR
    Function::Linear.new(@start_value.nil? ? [x2,value] : [x1,@start_value],[x4, @end_value])
  when SIGMOID
    y1 = @start_value || Function::Sigmoid.find_y0(x1..x4, [x2, value], @end_value)
    Function::Sigmoid.new([x1,y1],[x4, @end_value])
  end
  Transition.new(func, x2..x3)
end

#trailingObject



99
100
101
# File 'lib/musicality/notation/model/change.rb', line 99

def trailing
  @duration - @preceding - @remaining
end

#untrimObject



103
104
105
# File 'lib/musicality/notation/model/change.rb', line 103

def untrim
  Gradual.new(@end_value, @duration, @transition, start_value: @start_value)
end