Class: Sunniesnow::Charter::BpmChangeList

Inherits:
Object
  • Object
show all
Defined in:
lib/sscharter.rb

Defined Under Namespace

Classes: BpmChange

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(offset) ⇒ BpmChangeList

Returns a new instance of BpmChangeList.



49
50
51
52
# File 'lib/sscharter.rb', line 49

def initialize offset
  @offset = offset
  @list = []
end

Instance Attribute Details

#offsetObject

Returns the value of attribute offset.



47
48
49
# File 'lib/sscharter.rb', line 47

def offset
  @offset
end

Instance Method Details

#[](index) ⇒ Object



70
71
72
# File 'lib/sscharter.rb', line 70

def [] index
  @list[index]
end

#add(beat, bpm) ⇒ Object



54
55
56
57
58
# File 'lib/sscharter.rb', line 54

def add beat, bpm
  @list.push BpmChange.new beat, bpm
  @list.sort!
  self
end

#time_at(beat) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
# File 'lib/sscharter.rb', line 60

def time_at beat
  index = @list.bisect(right: true) { _1.beat <=> beat }
  raise ArgumentError, 'beat is before the first bpm change' if index < 0
  bpm = @list[index]
  (0...index).sum @offset + (beat - bpm.beat) / bpm.bps do |i|
    bpm = @list[i]
    (@list[i+1].beat - bpm.beat) / bpm.bps
  end
end