Class: Wavesync::Analyzer

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

Constant Summary collapse

CONFIRM_MESSAGE =
'wavesync analyze will add bpm meta data to files in library. Continue? [y/N] '
SETUP_INSTRUCTIONS =
'brew install python@3.11 && python3.11 -m venv ~/.wavesync-venv && ~/.wavesync-venv/bin/pip install essentia'

Instance Method Summary collapse

Constructor Details

#initialize(library_path) ⇒ Analyzer

: (String library_path) -> void



10
11
12
13
14
# File 'lib/wavesync/analyzer.rb', line 10

def initialize(library_path)
  @library_path = File.expand_path(library_path) #: String
  @audio_files = find_audio_files #: Array[String]
  @ui = UI.new #: UI
end

Instance Method Details

#analyze(overwrite: false) ⇒ Object

: (?overwrite: bool) -> void



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wavesync/analyzer.rb', line 17

def analyze(overwrite: false)
  unless BpmDetector.available?
    puts "Error: essentia is not installed. Set up the Python venv with:\n  #{SETUP_INSTRUCTIONS}"
    exit 1
  end

  return unless @ui.confirm(CONFIRM_MESSAGE)

  @audio_files.each_with_index do |file, index|
    audio = Audio.new(file)

    if audio.bpm && !overwrite
      @ui.analyze_progress(index, @audio_files.size)
      @ui.analyze_skip(file, audio.bpm)
      next
    end

    bpm = BpmDetector.detect(file)
    audio.write_bpm(bpm) if bpm
    @ui.analyze_progress(index, @audio_files.size)
    @ui.analyze_result(file, bpm)
  end
end