Module: Muze::Beat

Defined in:
lib/muze/beat/beat_track.rb

Overview

Beat tracking functions.

Class Method Summary collapse

Class Method Details

.beat_track(y: nil, sr: 22_050, onset_envelope: nil, hop_length: 512, start_bpm: 120.0, tightness: 100, min_bpm: 30.0, max_bpm: 240.0, bpm: nil, fill_missing: true, return_metadata: false) ⇒ Array(Float, Array<Integer>)

Returns estimated tempo and beat frames.

Parameters:

  • y (Numo::SFloat, Array<Float>, nil) (defaults to: nil)
  • sr (Integer) (defaults to: 22_050)
  • onset_envelope (Numo::SFloat, Array<Float>, nil) (defaults to: nil)
  • hop_length (Integer) (defaults to: 512)
  • start_bpm (Float) (defaults to: 120.0)
  • tightness (Integer) (defaults to: 100)

Returns:

  • (Array(Float, Array<Integer>))

    estimated tempo and beat frames



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/muze/beat/beat_track.rb', line 15

def beat_track(y: nil, sr: 22_050, onset_envelope: nil, hop_length: 512, start_bpm: 120.0, tightness: 100, min_bpm: 30.0, max_bpm: 240.0, bpm: nil, fill_missing: true, return_metadata: false)
  validate_beat_params!(
    sr:,
    hop_length:,
    start_bpm:,
    tightness:,
    min_bpm:,
    max_bpm:,
    bpm:,
    fill_missing:,
    return_metadata:
  )

  envelope = if onset_envelope
               onset_envelope.is_a?(Numo::NArray) ? onset_envelope.to_a : Array(onset_envelope)
             else
               Muze::Onset.onset_strength(y:, sr:, hop_length:).to_a
             end
  validate_finite_array!(envelope, "onset_envelope")

  if envelope.empty? || envelope.max.to_f <= 1.0e-12
    result = { tempo: nil, beats: [], confidence: 0.0 }
    return  ? result : [nil, []]
  end

  tempo = bpm || estimate_tempo(envelope, sr:, hop_length:, start_bpm:, min_bpm:, max_bpm:, tightness:)
  beats = track_beats(envelope, tempo:, sr:, hop_length:, tightness:, fill_missing:)
  confidence = beat_confidence(envelope, beats)
  return { tempo:, beats:, confidence: } if 

  [tempo, beats]
end

.tempo_frequencies(sr: 22_050, hop_length: 512, win_length: 384) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/muze/beat/beat_track.rb', line 60

def tempo_frequencies(sr: 22_050, hop_length: 512, win_length: 384)
  validate_positive_integer!(sr, "sr")
  validate_positive_integer!(hop_length, "hop_length")
  raise Muze::ParameterError, "win_length must be positive" unless win_length.positive?

  Numo::SFloat.cast(Array.new(win_length) do |lag|
    lag.zero? ? 0.0 : 60.0 * sr / (hop_length * lag)
  end)
end

.tempogram(y: nil, onset_envelope: nil, sr: 22_050, hop_length: 512, win_length: 384, normalize: false) ⇒ Numo::SFloat

Parameters:

  • y (Numo::SFloat, Array<Float>, nil) (defaults to: nil)
  • onset_envelope (Numo::SFloat, Array<Float>, nil) (defaults to: nil)
  • sr (Integer) (defaults to: 22_050)
  • hop_length (Integer) (defaults to: 512)
  • win_length (Integer) (defaults to: 384)

Returns:

  • (Numo::SFloat)


54
55
56
57
58
# File 'lib/muze/beat/beat_track.rb', line 54

def tempogram(y: nil, onset_envelope: nil, sr: 22_050, hop_length: 512, win_length: 384, normalize: false)
  validate_positive_integer!(sr, "sr")
  validate_positive_integer!(hop_length, "hop_length")
  Muze::Feature.tempogram(y:, onset_envelope:, sr:, hop_length:, win_length:, normalize:)
end