Class: Sunniesnow::Charter::BpmChangeList
- Inherits:
-
Object
- Object
- Sunniesnow::Charter::BpmChangeList
- Includes:
- Enumerable
- Defined in:
- lib/sscharter/charter/beat.rb
Defined Under Namespace
Classes: BpmChange
Instance Attribute Summary collapse
Instance Method Summary collapse
- #[](index) ⇒ BpmChange?
-
#add(beat, bpm) ⇒ BpmChangeList
self. - #bps_after(beat) ⇒ Float
- #bps_before(beat) ⇒ Float
- #each {|| ... } ⇒ Object
-
#initialize(offset) ⇒ BpmChangeList
constructor
A new instance of BpmChangeList.
- #time_at(beat) ⇒ Float
Constructor Details
#initialize(offset) ⇒ BpmChangeList
Returns a new instance of BpmChangeList.
55 56 57 58 |
# File 'lib/sscharter/charter/beat.rb', line 55 def initialize offset @offset = offset @list = [] end |
Instance Attribute Details
#offset ⇒ Float
52 53 54 |
# File 'lib/sscharter/charter/beat.rb', line 52 def offset @offset end |
Instance Method Details
#[](index) ⇒ BpmChange?
101 102 103 |
# File 'lib/sscharter/charter/beat.rb', line 101 def [] index @list[index] end |
#add(beat, bpm) ⇒ BpmChangeList
Returns self.
63 64 65 66 67 68 69 70 71 |
# File 'lib/sscharter/charter/beat.rb', line 63 def add beat, bpm if index = @list.bsearch_index { beat <=> _1.beat } @list[index].bpm = bpm else @list.push BpmChange.new beat, bpm @list.sort! end self end |
#bps_after(beat) ⇒ Float
94 95 96 97 |
# File 'lib/sscharter/charter/beat.rb', line 94 def bps_after beat raise ArgumentError, 'beat is before the first bpm change' if beat < @list.first.beat @list[@list.bisect(right: true) { _1.beat <=> beat }].bps end |
#bps_before(beat) ⇒ Float
87 88 89 90 |
# File 'lib/sscharter/charter/beat.rb', line 87 def bps_before beat raise ArgumentError, 'beat is before or at the first bpm change' if beat <= @list.first.beat @list[@list.bisect(right: false) { _1.beat <=> beat } - 1].bps end |
#each {|| ... } ⇒ Object
106 107 108 109 |
# File 'lib/sscharter/charter/beat.rb', line 106 def each(...) @list.each(...) self end |
#time_at(beat) ⇒ Float
75 76 77 78 79 80 81 82 83 |
# File 'lib/sscharter/charter/beat.rb', line 75 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 |