Module: Deftones::Music::UnitHelpers

Defined in:
lib/deftones/music/unit_helpers.rb

Constant Summary collapse

NOTATION_CANDIDATES =
%w[1m 1n 2n. 2n 4n. 4n 8n. 8n 8t 16n. 16n 16t 32n 64n 128n].freeze

Class Method Summary collapse

Class Method Details

.closest_notation(seconds, transport:) ⇒ Object



9
10
11
12
13
14
# File 'lib/deftones/music/unit_helpers.rb', line 9

def closest_notation(seconds, transport:)
  NOTATION_CANDIDATES.min_by do |notation|
    duration = Time.parse(notation, bpm: transport.bpm, time_signature: transport.time_signature, ppq: transport.ppq)
    (duration - seconds).abs
  end
end

.quantize_seconds(seconds, subdivision, transport:, percent: 1.0) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/deftones/music/unit_helpers.rb', line 16

def quantize_seconds(seconds, subdivision, transport:, percent: 1.0)
  quantum = Time.parse(subdivision, bpm: transport.bpm, time_signature: transport.time_signature, ppq: transport.ppq)
  return seconds unless quantum.positive?

  target = (seconds / quantum).round * quantum
  seconds + ((target - seconds) * percent.to_f.clamp(0.0, 1.0))
end

.samples_for_seconds(seconds, sample_rate) ⇒ Object



24
25
26
# File 'lib/deftones/music/unit_helpers.rb', line 24

def samples_for_seconds(seconds, sample_rate)
  (seconds * sample_rate.to_f).round
end