Class: Wavesync::TrackPadding

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

Constant Summary collapse

BEATS_PER_BAR =
4

Class Method Summary collapse

Class Method Details

.bar_counts(duration_seconds, bpm, bar_multiple) ⇒ Object

: ((Float | Integer)? duration_seconds, (Integer | Float | String)? bpm, (Integer | Float)? bar_multiple) -> [Integer?, Integer?]



21
22
23
24
25
26
27
28
# File 'lib/wavesync/track_padding.rb', line 21

def self.bar_counts(duration_seconds, bpm, bar_multiple)
  return [nil, nil] if bpm.nil? || bpm.to_f.zero? || duration_seconds.nil? || duration_seconds <= 0 || bar_multiple.nil?

  seconds_per_bar = BEATS_PER_BAR * 60.0 / bpm.to_f
  original_bars = (duration_seconds / seconds_per_bar).round
  target_bars = ((original_bars.to_f / bar_multiple).ceil * bar_multiple.to_f).to_i
  [original_bars, target_bars]
end

.compute(duration_seconds, bpm, bar_multiple) ⇒ Object

: ((Float | Integer)? duration_seconds, (Integer | Float | String)? bpm, (Integer | Float)? bar_multiple) -> (Float | Integer)



9
10
11
12
13
14
15
16
17
18
# File 'lib/wavesync/track_padding.rb', line 9

def self.compute(duration_seconds, bpm, bar_multiple)
  return 0 if bpm.nil? || bpm.to_f.zero? || duration_seconds.nil? || duration_seconds <= 0 || bar_multiple.nil?

  seconds_per_bar = BEATS_PER_BAR * 60.0 / bpm.to_f
  track_bars = (duration_seconds / seconds_per_bar).round(6)
  target_bars = (track_bars / bar_multiple.to_f).ceil * bar_multiple.to_f

  padding = (target_bars * seconds_per_bar) - duration_seconds
  padding < 0.001 ? 0 : padding
end