Class: Topaz::TempoCalculator
- Inherits:
-
Object
- Object
- Topaz::TempoCalculator
- Defined in:
- lib/topaz/tempo_calculator.rb
Overview
Calculate tempo given timestamps
Constant Summary collapse
- THRESHOLD =
Optimal number of ticks to analyze
6
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
-
#calculate ⇒ Float?
Analyze the tempo based on the threshold.
-
#initialize ⇒ TempoCalculator
constructor
A new instance of TempoCalculator.
Constructor Details
#initialize ⇒ TempoCalculator
Returns a new instance of TempoCalculator.
10 11 12 13 |
# File 'lib/topaz/tempo_calculator.rb', line 10 def initialize @tempo = nil @timestamps = [] end |
Instance Attribute Details
#tempo ⇒ Object (readonly)
Returns the value of attribute tempo.
8 9 10 |
# File 'lib/topaz/tempo_calculator.rb', line 8 def tempo @tempo end |
#timestamps ⇒ Object (readonly)
Returns the value of attribute timestamps.
8 9 10 |
# File 'lib/topaz/tempo_calculator.rb', line 8 def @timestamps end |
Instance Method Details
#calculate ⇒ Float?
Analyze the tempo based on the threshold
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/topaz/tempo_calculator.rb', line 17 def calculate tempo = nil if @timestamps.count >= 2 deltas = get_deltas sum = deltas.inject(&:+) average = sum.to_f / deltas.count bpm = ppq24_millis_to_bpm(average) @tempo = bpm end end |