Class: Topaz::TempoCalculator
- Inherits:
-
Object
- Object
- Topaz::TempoCalculator
- Defined in:
- lib/topaz/tempo_calculator.rb
Instance Attribute Summary collapse
-
#tempo ⇒ Object
readonly
Returns the value of attribute tempo.
-
#timestamps ⇒ Object
readonly
Returns the value of attribute timestamps.
Instance Method Summary collapse
-
#find_tempo ⇒ Object
analyse the tempo based on the last 6 ticks.
-
#initialize(tempo = nil) ⇒ TempoCalculator
constructor
A new instance of TempoCalculator.
Constructor Details
#initialize(tempo = nil) ⇒ TempoCalculator
Returns a new instance of TempoCalculator.
8 9 10 11 12 |
# File 'lib/topaz/tempo_calculator.rb', line 8 def initialize(tempo = nil) @tempo = tempo @timestamps = [] @counter = 0 end |
Instance Attribute Details
#tempo ⇒ Object (readonly)
Returns the value of attribute tempo.
6 7 8 |
# File 'lib/topaz/tempo_calculator.rb', line 6 def tempo @tempo end |
#timestamps ⇒ Object (readonly)
Returns the value of attribute timestamps.
6 7 8 |
# File 'lib/topaz/tempo_calculator.rb', line 6 def @timestamps end |
Instance Method Details
#find_tempo ⇒ Object
analyse the tempo based on the last 6 ticks
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/topaz/tempo_calculator.rb', line 15 def find_tempo tempo = nil diffs = [] @timestamps.shift while @timestamps.length > 6 @timestamps.each_with_index { |n, i| (diffs << (@timestamps[i+1] - n)) unless @timestamps[i+1].nil? } unless diffs.empty? avg = (diffs.inject { |a, b| a + b }.to_f / diffs.length.to_f) tempo = ppq24_millis_to_bpm(avg) end @tempo = tempo end |