feature/share-libs
rb 48 lines 1.32 KB
Raw
1 # frozen_string_literal: true
2
3 class AudioAnalysisSplitfireResultJob < ApplicationJob
4 queue_as :default
5
6 def perform(splitfire_result_id)
7 # Get the file to split.
8 @file = SplitfireResult.find_by(id: splitfire_result_id)
9
10 # Make sure the actual file is attached.
11 # Otherwise, we don't want to do the job.
12 return if @file.nil? || !@file.source_file.attached?
13
14 Sidekiq.logger.debug("File: #{@file}")
15
16 # Change working directory to the output folder.
17 change_working_directory
18
19 length_and_onset do |length, onset|
20 @file.update(length: length, onset: onset)
21 Sidekiq.logger.debug("Length: #{length}, Onset: #{onset}")
22 end
23 end
24
25 private
26
27 def length_and_onset
28 Sidekiq.logger.info('Starting AudioAnalysisJob processing...')
29 @file.source_file.open do |file|
30 Sidekiq.logger.debug("File path: #{file.path}")
31 result = `python audio_length.py #{file.path}`
32 onset = `python onset.py #{file.path}`
33 # Process onset string into array.
34 onset = onset_string_to_array(onset)
35 yield(result, onset)
36 end
37 end
38
39 def onset_string_to_array(onset)
40 onset.delete('[]').split.map(&:to_f)
41 end
42
43 def change_working_directory
44 Sidekiq.logger.info('Changing working directory...')
45 Dir.chdir(ENV['AUDIO_ANALYSIS_WORKING_DIRECTORY'])
46 system('echo $PWD')
47 end
48 end