Class: Wavesync::PercivalBpmDetector

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

Constant Summary collapse

PYTHON_SCRIPT =
<<~PYTHON
  import essentia.standard as es, sys
  audio = es.MonoLoader(filename=sys.argv[1], sampleRate=44100)()
  bpm = es.PercivalBpmEstimator()(audio)
  print(round(float(bpm)))
PYTHON

Class Method Summary collapse

Class Method Details

.available?Boolean

: () -> bool?

Returns:

  • (Boolean)


17
18
19
# File 'lib/wavesync/percival_bpm_detector.rb', line 17

def self.available?
  PythonVenv.essentia_available?
end

.detect(file_path) ⇒ Object

: (String file_path) -> Integer?



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

def self.detect(file_path)
  output = PythonVenv.run_script(PYTHON_SCRIPT, file_path)
  bpm = output.strip.to_f
  bpm.positive? ? bpm.round : nil
rescue StandardError => e
  Logger.log_error(e, call_site: 'PercivalBpmDetector.detect', arguments: { file_path: })
  nil
end