Module: Sunniesnow::Charter::BeatSeries
- Included in:
- Sunniesnow::Charter, TimeDependent
- Defined in:
- lib/sscharter/charter/beat.rb
Overview
Including this module adds the ability to keep track of the current beat and set BPM changes at the current beat. It provides methods #beat and #beat! to navigate through the beats.
The examples shown in the documentation below assume that self is a Sunniesnow::Charter instance.
Instance Attribute Summary collapse
-
#bpm_changes ⇒ BpmChangeList?
readonly
It is
nilif the offset has not been set by #offset yet. -
#current_beat ⇒ Rational?
It is
nilif the offset has not been set by #offset yet.
DSL Methods collapse
-
#beat(delta_beat = 0) ⇒ Rational
(also: #b)
Increments the current beat by the given delta set by
delta_beat. -
#beat!(beat = @current_beat) ⇒ Rational
(also: #b!)
Sets the current beat to the given value.
-
#bpm(bpm) ⇒ BpmChangeList
Set the BPM starting at the current beat.
Instance Method Summary collapse
- #current_beat_state ⇒ BeatState
- #restore_beat_state(backup) ⇒ void
- #time_at(beat = @current_beat) ⇒ Float
Instance Attribute Details
#bpm_changes ⇒ BpmChangeList? (readonly)
It is nil if the offset has not been set by Sunniesnow::Charter#offset yet.
177 178 179 |
# File 'lib/sscharter/charter/beat.rb', line 177 def bpm_changes @bpm_changes end |
#current_beat ⇒ Rational?
It is nil if the offset has not been set by Sunniesnow::Charter#offset yet.
173 174 175 |
# File 'lib/sscharter/charter/beat.rb', line 173 def current_beat @current_beat end |
Instance Method Details
#beat(delta_beat = 0) ⇒ Rational Also known as: b
Increments the current beat by the given delta set by delta_beat.
It is recommended that delta_beat be a Rational or an Integer for accuracy.
Float will be converted to Rational, and a warning will be issued
when a Float is used.
This method is also useful for inspecting the current beat. If the method is called without an argument, it simply returns the current beat. For this purpose, this method is equivalent to #beat!.
This method must be called after Sunniesnow::Charter#offset.
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/sscharter/charter/beat.rb', line 220 def beat delta_beat = 0 raise OffsetError.new __method__ unless @current_beat case delta_beat when Integer, Rational @current_beat += delta_beat.to_r when Float warn 'float beat is not recommended' @current_beat += delta_beat.to_r else raise ArgumentError, 'invalid delta_beat' end end |
#beat!(beat = @current_beat) ⇒ Rational Also known as: b!
Sets the current beat to the given value.
It is recommended that beat be a Rational or an Integer for accuracy.
Float will be converted to Rational, and a warning will be issued.
When called without an argument, this method does nothing and returns the current beat. For this purpose, this method is equivalent to #beat.
This method must be called after Sunniesnow::Charter#offset.
252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/sscharter/charter/beat.rb', line 252 def beat! beat = @current_beat raise OffsetError.new __method__ unless @current_beat case beat when Integer, Rational @current_beat = beat.to_r when Float warn 'float beat is not recommended' @current_beat = beat.to_r else raise ArgumentError, 'invalid beat' end end |
#bpm(bpm) ⇒ BpmChangeList
Set the BPM starting at the current beat. This method must be called after Sunniesnow::Charter#offset. The method can be called multiple times, which is useful when the music changes its tempo from time to time.
Internally, this simply calls Sunniesnow::Charter::BpmChangeList#add on the BPM changes created by Sunniesnow::Charter#offset.
190 191 192 193 |
# File 'lib/sscharter/charter/beat.rb', line 190 def bpm bpm raise OffsetError.new __method__ unless @bpm_changes @bpm_changes.add @current_beat, bpm end |
#current_beat_state ⇒ BeatState
Internal API.
278 279 280 |
# File 'lib/sscharter/charter/beat.rb', line 278 def current_beat_state BeatState.new @current_beat, @bpm_changes end |
#restore_beat_state(backup) ⇒ void
Internal API.
This method returns an undefined value.
285 286 287 288 289 |
# File 'lib/sscharter/charter/beat.rb', line 285 def restore_beat_state backup @current_beat = backup.current_beat @bpm_changes = backup.bpm_changes nil end |
#time_at(beat = @current_beat) ⇒ Float
271 272 273 274 |
# File 'lib/sscharter/charter/beat.rb', line 271 def time_at beat = @current_beat raise OffsetError.new __method__ unless @bpm_changes @bpm_changes.time_at beat end |