| 1 | class Bass64Job < ApplicationJob |
| 2 | queue_as :default |
| 3 | |
| 4 | def perform(audio_file_id) |
| 5 | |
| 6 | # Get the file to split. |
| 7 | file = AudioFile.find_by(id: audio_file_id) |
| 8 | |
| 9 | # Make sure the actual file is attached. |
| 10 | # Otherwise, we don't want to do the job. |
| 11 | return if file.nil? || !file.source_file.attached? |
| 12 | |
| 13 | file_path = ActiveStorage::Blob.service.send(:path_for, file.source_file.key) |
| 14 | |
| 15 | # Change to spleeter working directory. |
| 16 | # TODO: Tidy up. |
| 17 | if Rails.env.production? |
| 18 | Dir.chdir("/home/deploy/apps/spleeter/") |
| 19 | else |
| 20 | Dir.chdir("../spleeter/") |
| 21 | end |
| 22 | |
| 23 | # Spleet it! |
| 24 | # Always use the fullpath to the Spleeter. |
| 25 | # TODO: Tidy up. |
| 26 | if Rails.env.production? |
| 27 | system("/home/deploy/.local/bin/spleeter separate -p spleeter:4stems-16kHz -o ./output/ #{file_path} >> spleeter_log.txt") |
| 28 | else |
| 29 | system("spleeter separate -p spleeter:4stems-16kHz -o ./output/ #{file_path} >> spleeter_log.txt") |
| 30 | end |
| 31 | |
| 32 | file_output_directory = "output/#{file.source_file.blob.key}" |
| 33 | |
| 34 | # Compose results |
| 35 | result_bass_path = "#{file_output_directory}/bass.wav" |
| 36 | result_vocals_path = "#{file_output_directory}/vocals.wav" |
| 37 | result_drums_path = "#{file_output_directory}/drums.wav" |
| 38 | result_other_path = "#{file_output_directory}/other.wav" |
| 39 | |
| 40 | # Wait until the resilt files exist... |
| 41 | until |
| 42 | File.exist?(result_bass_path) && |
| 43 | File.exist?(result_vocals_path) && |
| 44 | File.exist?(result_drums_path) && |
| 45 | File.exist?(result_other_path) |
| 46 | |
| 47 | system("echo 'We are waiting for the result files...'") |
| 48 | sleep 5 |
| 49 | end |
| 50 | |
| 51 | |
| 52 | # Internal |
| 53 | # |
| 54 | # Save this as VocalFile and BassFile to train a speech recognition AI |
| 55 | # Only do so if we haven't had it in the database |
| 56 | unless !VocalFile.find_by(audio_file_id: file.id).nil? |
| 57 | vocal_file = File.open(result_vocals_path) |
| 58 | vocal_filename = "vocals-#{file.source_file.blob.filename}.wav" |
| 59 | vocal = VocalFile.new(audio_file_id: file.id) |
| 60 | |
| 61 | vocal.source_file.attach(io: vocal_file, filename: vocal_filename) |
| 62 | vocal.save |
| 63 | end |
| 64 | |
| 65 | unless !BassFile.find_by(audio_file_id: file.id).nil? |
| 66 | bass_file = File.open(result_bass_path) |
| 67 | bass_filename = "bass-#{file.source_file.blob.filename}.wav" |
| 68 | bass = BassFile.new(audio_file_id: file.id) |
| 69 | |
| 70 | bass.source_file.attach(io: bass_file, filename: bass_filename) |
| 71 | bass.save |
| 72 | end |
| 73 | |
| 74 | unless !DrumFile.find_by(audio_file_id: file.id).nil? |
| 75 | drum_file = File.open(result_drums_path) |
| 76 | drum_filename = "drums-#{file.source_file.blob.filename}.wav" |
| 77 | drum = DrumFile.new(audio_file_id: file.id) |
| 78 | |
| 79 | drum.source_file.attach(io: drum_file, filename: drum_filename) |
| 80 | drum.save |
| 81 | end |
| 82 | |
| 83 | unless !OtherFile.find_by(audio_file_id: file.id).nil? |
| 84 | other_file = File.open(result_other_path) |
| 85 | other_filename = "other-#{file.source_file.blob.filename}.wav" |
| 86 | other = OtherFile.new(audio_file_id: file.id) |
| 87 | |
| 88 | other.source_file.attach(io: other_file, filename: other_filename) |
| 89 | other.save |
| 90 | end |
| 91 | # |
| 92 | # END Internal |
| 93 | |
| 94 | result_bass_backing_track_filename = "bass-backing-track-#{file.source_file.blob.filename}.wav" |
| 95 | result_bass_backing_track_path = "#{file_output_directory}/#{result_bass_backing_track_filename}" |
| 96 | |
| 97 | system("ffmpeg -i #{result_drums_path} -i #{result_vocals_path} -i #{result_other_path} -filter_complex amix=inputs=3:duration=longest -y '#{result_bass_backing_track_path}'") |
| 98 | |
| 99 | until File.exist?(result_bass_backing_track_path) |
| 100 | system("echo 'We are waiting for #{result_bass_backing_track_path}...'") |
| 101 | sleep 60 |
| 102 | end |
| 103 | result_bass_backing_track_file = File.open(result_bass_backing_track_path) |
| 104 | |
| 105 | # We only save the result if user doesn't cancel the process |
| 106 | # Ideally, we cancel the running process immediately |
| 107 | if !file.audio_file_bass64_status.nil? && file.audio_file_bass64_status.cancelled? |
| 108 | system("rm -rf #{file_output_directory}") |
| 109 | return |
| 110 | end |
| 111 | |
| 112 | result_bass_backing_track = BassBackingTrack.new(audio_file_id: file.id) |
| 113 | result_bass_backing_track.source_file.attach(io: result_bass_backing_track_file, filename: result_bass_backing_track_filename) |
| 114 | result_bass_backing_track.save |
| 115 | |
| 116 | # Cleanup Spleeter output folder |
| 117 | system("rm -rf #{file_output_directory}") |
| 118 | |
| 119 | if file.audio_file_bass64_status.nil? |
| 120 | AudioFileBass64Status |
| 121 | .new(audio_file_id: audio_file_id, status: :done) |
| 122 | .save |
| 123 | elsif |
| 124 | file |
| 125 | .audio_file_bass64_status |
| 126 | .done! |
| 127 | end |
| 128 | |
| 129 | ActionCable.server.broadcast "Bass64Channel", { |
| 130 | audio_file_id: file.id, |
| 131 | message: MyController.render(partial: 'partial_bass64_row', locals: { audio_file: file} ) |
| 132 | } |
| 133 | |
| 134 | end |
| 135 | end |