| 1 | # The SplitFire naming, i.e; camel-cased style, is only being used for branding. |
| 2 | # Inside the code, we refer this tool as Splitfire. |
| 3 | |
| 4 | class SplitfireJob < ApplicationJob |
| 5 | queue_as :default |
| 6 | |
| 7 | def perform(audio_file_id) |
| 8 | # Get the file to split. |
| 9 | @file = AudioFile.find_by(id: audio_file_id) |
| 10 | |
| 11 | # Make sure the actual file is attached. |
| 12 | # Otherwise, we don't want to do the job. |
| 13 | return if @file.nil? || !@file.source_file.attached? |
| 14 | |
| 15 | # Change working directory to the Spleeter output folder. |
| 16 | change_working_directory |
| 17 | process_split_file do |path| |
| 18 | process_results(path) if path.present? |
| 19 | end |
| 20 | end |
| 21 | |
| 22 | private |
| 23 | |
| 24 | def process_results(path) |
| 25 | Sidekiq.logger.info("Processing results in #{path} ...") |
| 26 | @file_output_directory = path |
| 27 | # Process results |
| 28 | process_vocals_file |
| 29 | process_drums_file |
| 30 | process_bass_file |
| 31 | process_other_file |
| 32 | |
| 33 | Sidekiq.logger.info('Finishing process ...') |
| 34 | system("rm -rf #{@file_output_directory}") |
| 35 | @file.update(progress: 100) |
| 36 | @file.done! |
| 37 | end |
| 38 | |
| 39 | def change_working_directory |
| 40 | Sidekiq.logger.info('Changing working directory...') |
| 41 | Dir.chdir(ENV['AUDIO_WORKING_DIRECTORY']) |
| 42 | system('echo $PWD') |
| 43 | end |
| 44 | |
| 45 | def process_split_file |
| 46 | Sidekiq.logger.info('Starting Splitfire processing...') |
| 47 | |
| 48 | @file.source_file.open do |file| |
| 49 | system("echo 'File path: #{file.path}'") |
| 50 | system("python3 -m demucs -n=htdemucs_ft -d cpu #{file.path} >> demucs_log.txt") |
| 51 | # get filename from path |
| 52 | filename = file.path.split('/').last |
| 53 | # If filename consist of dot, split it and get the first part |
| 54 | filename = filename.split('.').first if filename.include?('.') |
| 55 | file_output_directory = "separated/htdemucs_ft/#{filename}" |
| 56 | yield(file_output_directory) |
| 57 | end |
| 58 | end |
| 59 | |
| 60 | def process_vocals_file |
| 61 | Sidekiq.logger.info('Processing vocals file...') |
| 62 | # Compose results for vocals |
| 63 | result_vocals_path = "#{@file_output_directory}/vocals.wav" |
| 64 | result_vocals_file = File.open(result_vocals_path) |
| 65 | result_vocals_filename = "vocals-#{@file.source_file.blob.filename}.wav" |
| 66 | vocal = SplitfireResult.new(audio_file_id: @file.id) |
| 67 | vocal.source_file.attach(io: result_vocals_file, filename: result_vocals_filename) |
| 68 | vocal.save |
| 69 | save_vocals_file(result_vocals_file, result_vocals_filename) |
| 70 | end |
| 71 | |
| 72 | def save_vocals_file(result_vocals_path, result_vocals_filename) |
| 73 | Sidekiq.logger.info('Saving vocals file...') |
| 74 | result_vocals_file = File.open(result_vocals_path) |
| 75 | vocal = VocalFile.new(audio_file_id: @file.id) |
| 76 | vocal.source_file.attach(io: result_vocals_file, filename: result_vocals_filename) |
| 77 | vocal.save |
| 78 | end |
| 79 | |
| 80 | def process_drums_file |
| 81 | Sidekiq.logger.info('Processing drums file...') |
| 82 | # DRUMS |
| 83 | result_drums_path = "#{@file_output_directory}/drums.wav" |
| 84 | result_drums_file = File.open(result_drums_path) |
| 85 | result_drums_filename = "drums-#{@file.source_file.blob.filename}.wav" |
| 86 | result_drums = SplitfireResult.new(audio_file_id: @file.id) |
| 87 | result_drums.source_file.attach(io: result_drums_file, filename: result_drums_filename) |
| 88 | result_drums.save |
| 89 | end |
| 90 | |
| 91 | def process_bass_file |
| 92 | Sidekiq.logger.info('Processing bass file...') |
| 93 | # BASS |
| 94 | result_bass_path = "#{@file_output_directory}/bass.wav" |
| 95 | result_bass_file = File.open(result_bass_path) |
| 96 | result_bass_filename = "bass-#{@file.source_file.blob.filename}.wav" |
| 97 | result_bass = SplitfireResult.new(audio_file_id: @file.id) |
| 98 | result_bass.source_file.attach(io: result_bass_file, filename: result_bass_filename) |
| 99 | result_bass.save |
| 100 | end |
| 101 | |
| 102 | def process_other_file |
| 103 | Sidekiq.logger.info('Processing other file...') |
| 104 | # OTHER |
| 105 | result_other_path = "#{@file_output_directory}/other.wav" |
| 106 | result_other_file = File.open(result_other_path) |
| 107 | result_other_filename = "other-#{@file.source_file.blob.filename}.wav" |
| 108 | result_other = SplitfireResult.new(audio_file_id: @file.id) |
| 109 | result_other.source_file.attach(io: result_other_file, filename: result_other_filename) |
| 110 | result_other.save |
| 111 | end |
| 112 | |
| 113 | def broadcast_progress(progress) |
| 114 | @file.audio_file_split_status.update_attribute(:processing_progress, progress) |
| 115 | |
| 116 | ActionCable.server.broadcast 'SplitfireChannel', { |
| 117 | audio_file_id: @file.id, |
| 118 | partial_splitfire_row: MyController.render(partial: 'partial_splitfire_row', locals: { audio_file: @file }) |
| 119 | } |
| 120 | end |
| 121 | end |