Spaces:
Sleeping
Sleeping
helloWorld199
commited on
Commit
•
13f9cfc
1
Parent(s):
6172477
Upload main.py
Browse files
main.py
ADDED
@@ -0,0 +1,438 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import gc
|
3 |
+
import hashlib
|
4 |
+
import json
|
5 |
+
import os
|
6 |
+
import shlex
|
7 |
+
import subprocess
|
8 |
+
from contextlib import suppress
|
9 |
+
from urllib.parse import urlparse, parse_qs
|
10 |
+
import time
|
11 |
+
|
12 |
+
import gradio as gr
|
13 |
+
import librosa
|
14 |
+
import numpy as np
|
15 |
+
import soundfile as sf
|
16 |
+
import sox
|
17 |
+
import yt_dlp
|
18 |
+
from pedalboard import Pedalboard, Reverb, Compressor, HighpassFilter
|
19 |
+
from pedalboard.io import AudioFile
|
20 |
+
from pydub import AudioSegment
|
21 |
+
from my_utils import add_stem_name
|
22 |
+
|
23 |
+
from mdx import run_mdx
|
24 |
+
from rvc import Config, load_hubert, get_vc, rvc_infer
|
25 |
+
|
26 |
+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
27 |
+
|
28 |
+
mdxnet_models_dir = os.path.join(BASE_DIR, 'mdxnet_models')
|
29 |
+
rvc_models_dir = os.path.join(BASE_DIR, 'rvc_models')
|
30 |
+
output_dir = os.path.join(BASE_DIR, 'song_output')
|
31 |
+
|
32 |
+
|
33 |
+
def get_youtube_video_id(url, ignore_playlist=True):
|
34 |
+
"""
|
35 |
+
Examples:
|
36 |
+
http://youtu.be/SA2iWivDJiE
|
37 |
+
http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu
|
38 |
+
http://www.youtube.com/embed/SA2iWivDJiE
|
39 |
+
http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US
|
40 |
+
"""
|
41 |
+
query = urlparse(url)
|
42 |
+
if query.hostname == 'youtu.be':
|
43 |
+
if query.path[1:] == 'watch':
|
44 |
+
return query.query[2:]
|
45 |
+
return query.path[1:]
|
46 |
+
|
47 |
+
if query.hostname in {'www.youtube.com', 'youtube.com', 'music.youtube.com'}:
|
48 |
+
if not ignore_playlist:
|
49 |
+
# use case: get playlist id not current video in playlist
|
50 |
+
with suppress(KeyError):
|
51 |
+
return parse_qs(query.query)['list'][0]
|
52 |
+
if query.path == '/watch':
|
53 |
+
return parse_qs(query.query)['v'][0]
|
54 |
+
if query.path[:7] == '/watch/':
|
55 |
+
return query.path.split('/')[1]
|
56 |
+
if query.path[:7] == '/embed/':
|
57 |
+
return query.path.split('/')[2]
|
58 |
+
if query.path[:3] == '/v/':
|
59 |
+
return query.path.split('/')[2]
|
60 |
+
|
61 |
+
# returns None for invalid YouTube url
|
62 |
+
return None
|
63 |
+
|
64 |
+
|
65 |
+
def yt_download(link):
|
66 |
+
ydl_opts = {
|
67 |
+
'format': 'bestaudio',
|
68 |
+
'outtmpl': '%(title)s',
|
69 |
+
'nocheckcertificate': True,
|
70 |
+
'ignoreerrors': True,
|
71 |
+
'no_warnings': True,
|
72 |
+
'quiet': True,
|
73 |
+
'extractaudio': True,
|
74 |
+
'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3'}],
|
75 |
+
}
|
76 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
77 |
+
result = ydl.extract_info(link, download=True)
|
78 |
+
download_path = ydl.prepare_filename(result, outtmpl='%(title)s.mp3')
|
79 |
+
|
80 |
+
return download_path
|
81 |
+
|
82 |
+
|
83 |
+
def raise_exception(error_msg, is_webui):
|
84 |
+
if is_webui:
|
85 |
+
raise gr.Error(error_msg)
|
86 |
+
else:
|
87 |
+
raise Exception(error_msg)
|
88 |
+
|
89 |
+
|
90 |
+
def get_rvc_model(voice_model, is_webui):
|
91 |
+
rvc_model_filename, rvc_index_filename = None, None
|
92 |
+
model_dir = os.path.join(rvc_models_dir, voice_model)
|
93 |
+
for file in os.listdir(model_dir):
|
94 |
+
ext = os.path.splitext(file)[1]
|
95 |
+
if ext == '.pth':
|
96 |
+
rvc_model_filename = file
|
97 |
+
if ext == '.index':
|
98 |
+
rvc_index_filename = file
|
99 |
+
|
100 |
+
if rvc_model_filename is None:
|
101 |
+
error_msg = f'No model file exists in {model_dir}.'
|
102 |
+
raise_exception(error_msg, is_webui)
|
103 |
+
|
104 |
+
return os.path.join(model_dir, rvc_model_filename), os.path.join(model_dir, rvc_index_filename) if rvc_index_filename else ''
|
105 |
+
|
106 |
+
|
107 |
+
def get_audio_paths(song_dir):
|
108 |
+
orig_song_path = None
|
109 |
+
instrumentals_path = None
|
110 |
+
main_vocals_dereverb_path = None
|
111 |
+
backup_vocals_path = None
|
112 |
+
|
113 |
+
for file in os.listdir(song_dir):
|
114 |
+
if file.endswith('_Instrumental.wav'):
|
115 |
+
instrumentals_path = os.path.join(song_dir, file)
|
116 |
+
orig_song_path = instrumentals_path.replace('_Instrumental', '')
|
117 |
+
|
118 |
+
elif file.endswith('_Vocals_Main_DeReverb.wav'):
|
119 |
+
main_vocals_dereverb_path = os.path.join(song_dir, file)
|
120 |
+
|
121 |
+
elif file.endswith('_Vocals_Backup.wav'):
|
122 |
+
backup_vocals_path = os.path.join(song_dir, file)
|
123 |
+
|
124 |
+
return orig_song_path, instrumentals_path, main_vocals_dereverb_path, backup_vocals_path
|
125 |
+
|
126 |
+
|
127 |
+
def convert_to_stereo(audio_path):
|
128 |
+
wave, sr = librosa.load(audio_path, mono=False, sr=44100)
|
129 |
+
|
130 |
+
# check if mono
|
131 |
+
if type(wave[0]) != np.ndarray:
|
132 |
+
stereo_path = f'{os.path.splitext(audio_path)[0]}_stereo.wav'
|
133 |
+
command = shlex.split(f'ffmpeg -y -loglevel error -i "{audio_path}" -ac 2 -f wav "{stereo_path}"')
|
134 |
+
subprocess.run(command)
|
135 |
+
return stereo_path
|
136 |
+
else:
|
137 |
+
return audio_path
|
138 |
+
|
139 |
+
|
140 |
+
def pitch_shift(audio_path, pitch_change):
|
141 |
+
output_path = f'{os.path.splitext(audio_path)[0]}_p{pitch_change}.wav'
|
142 |
+
if not os.path.exists(output_path):
|
143 |
+
y, sr = sf.read(audio_path)
|
144 |
+
tfm = sox.Transformer()
|
145 |
+
tfm.pitch(pitch_change)
|
146 |
+
y_shifted = tfm.build_array(input_array=y, sample_rate_in=sr)
|
147 |
+
sf.write(output_path, y_shifted, sr)
|
148 |
+
|
149 |
+
return output_path
|
150 |
+
|
151 |
+
|
152 |
+
def get_hash(filepath):
|
153 |
+
with open(filepath, 'rb') as f:
|
154 |
+
file_hash = hashlib.blake2b()
|
155 |
+
while chunk := f.read(8192):
|
156 |
+
file_hash.update(chunk)
|
157 |
+
|
158 |
+
return file_hash.hexdigest()[:11]
|
159 |
+
|
160 |
+
|
161 |
+
def display_progress(message, percent, is_webui, progress=None):
|
162 |
+
if is_webui:
|
163 |
+
progress(percent, desc=message)
|
164 |
+
else:
|
165 |
+
print(message)
|
166 |
+
|
167 |
+
|
168 |
+
def preprocess_song(song_input, mdx_model_params, song_id, is_webui, input_type, progress=None):
|
169 |
+
keep_orig = False
|
170 |
+
if input_type == 'yt':
|
171 |
+
display_progress('[~] Downloading song...', 0, is_webui, progress)
|
172 |
+
song_link = song_input.split('&')[0]
|
173 |
+
orig_song_path = yt_download(song_link)
|
174 |
+
elif input_type == 'local':
|
175 |
+
orig_song_path = song_input
|
176 |
+
keep_orig = True
|
177 |
+
else:
|
178 |
+
orig_song_path = None
|
179 |
+
|
180 |
+
song_output_dir = os.path.join(output_dir, song_id)
|
181 |
+
orig_song_path = convert_to_stereo(orig_song_path)
|
182 |
+
|
183 |
+
display_progress('[~] Separating Vocals from Instrumental...', 0.1, is_webui, progress)
|
184 |
+
vocals_path, instrumentals_path = run_mdx(mdx_model_params, song_output_dir, os.path.join(mdxnet_models_dir, 'UVR-MDX-NET-Voc_FT.onnx'), orig_song_path, denoise=True, keep_orig=keep_orig, _stemname1 = "_origvocals", _stemname2="_originstr")
|
185 |
+
|
186 |
+
display_progress('[~] Separating Main Vocals from Backup Vocals...', 0.2, is_webui, progress)
|
187 |
+
backup_vocals_path, main_vocals_path = run_mdx(mdx_model_params, song_output_dir, os.path.join(mdxnet_models_dir, 'UVR_MDXNET_KARA_2.onnx'), vocals_path, suffix='Backup', invert_suffix='Main', denoise=True)
|
188 |
+
|
189 |
+
display_progress('[~] Applying DeReverb to Vocals...', 0.3, is_webui, progress)
|
190 |
+
_, main_vocals_dereverb_path = run_mdx(mdx_model_params, song_output_dir, os.path.join(mdxnet_models_dir, 'Reverb_HQ_By_FoxJoy.onnx'), main_vocals_path, invert_suffix='DeReverb', exclude_main=True, denoise=True)
|
191 |
+
|
192 |
+
return orig_song_path, vocals_path, instrumentals_path, main_vocals_path, backup_vocals_path, main_vocals_dereverb_path
|
193 |
+
|
194 |
+
# Function to preprocess vocals only, i.e. apply just dereverb process.
|
195 |
+
def preprocess_vocals_only(song_input, mdx_model_params, song_id, is_webui, input_type, progress=None):
|
196 |
+
orig_song_path = song_input
|
197 |
+
|
198 |
+
song_output_dir = os.path.join(output_dir, song_id)
|
199 |
+
orig_song_path = convert_to_stereo(orig_song_path)
|
200 |
+
|
201 |
+
display_progress('[~] Applying DeReverb to Vocals...', 0.3, is_webui, progress)
|
202 |
+
_, vocals_path = run_mdx(mdx_model_params, song_output_dir, os.path.join(mdxnet_models_dir, 'Reverb_HQ_By_FoxJoy.onnx'), orig_song_path, invert_suffix='DeReverb', exclude_main=True, denoise=True)
|
203 |
+
|
204 |
+
return orig_song_path, vocals_path
|
205 |
+
|
206 |
+
|
207 |
+
def voice_change(voice_model, vocals_path, output_path, pitch_change, f0_method, index_rate, filter_radius, rms_mix_rate, protect, crepe_hop_length, is_webui):
|
208 |
+
rvc_model_path, rvc_index_path = get_rvc_model(voice_model, is_webui)
|
209 |
+
device = 'cpu'
|
210 |
+
config = Config(device, True)
|
211 |
+
hubert_model = load_hubert(device, config.is_half, os.path.join(rvc_models_dir, 'hubert_base.pt'))
|
212 |
+
cpt, version, net_g, tgt_sr, vc = get_vc(device, config.is_half, config, rvc_model_path)
|
213 |
+
|
214 |
+
# convert main vocals
|
215 |
+
rvc_infer(rvc_index_path, index_rate, vocals_path, output_path, pitch_change, f0_method, cpt, version, net_g, filter_radius, tgt_sr, rms_mix_rate, protect, crepe_hop_length, vc, hubert_model)
|
216 |
+
del hubert_model, cpt
|
217 |
+
gc.collect()
|
218 |
+
|
219 |
+
|
220 |
+
def add_audio_effects(audio_path, reverb_rm_size, reverb_wet, reverb_dry, reverb_damping):
|
221 |
+
# Added _covervocals at the end of filename
|
222 |
+
output_path = f'{os.path.splitext(audio_path)[0]}_mixed_covervocals.wav'
|
223 |
+
|
224 |
+
# Initialize audio effects plugins
|
225 |
+
board = Pedalboard(
|
226 |
+
[
|
227 |
+
HighpassFilter(),
|
228 |
+
Compressor(ratio=4, threshold_db=-15),
|
229 |
+
Reverb(room_size=reverb_rm_size, dry_level=reverb_dry, wet_level=reverb_wet, damping=reverb_damping)
|
230 |
+
]
|
231 |
+
)
|
232 |
+
|
233 |
+
with AudioFile(audio_path) as f:
|
234 |
+
with AudioFile(output_path, 'w', f.samplerate, f.num_channels) as o:
|
235 |
+
# Read one second of audio at a time, until the file is empty:
|
236 |
+
while f.tell() < f.frames:
|
237 |
+
chunk = f.read(int(f.samplerate))
|
238 |
+
effected = board(chunk, f.samplerate, reset=False)
|
239 |
+
o.write(effected)
|
240 |
+
|
241 |
+
return output_path
|
242 |
+
|
243 |
+
|
244 |
+
def combine_audio(audio_paths, output_path, main_gain, backup_gain, inst_gain, output_format):
|
245 |
+
main_vocal_audio = AudioSegment.from_wav(audio_paths[0]) - 4 + main_gain
|
246 |
+
backup_vocal_audio = AudioSegment.from_wav(audio_paths[1]) - 6 + backup_gain
|
247 |
+
instrumental_audio = AudioSegment.from_wav(audio_paths[2]) - 7 + inst_gain
|
248 |
+
main_vocal_audio.overlay(backup_vocal_audio).overlay(instrumental_audio).export(output_path, format=output_format)
|
249 |
+
|
250 |
+
# Function defining the pipeline to create the AI cover of an audio containing only voice information.
|
251 |
+
# Returns the path of the cover vocal.
|
252 |
+
def vocal_only_pipeline(song_input, voice_model, pitch_change, is_webui=0,index_rate=0.5, filter_radius=3,
|
253 |
+
rms_mix_rate=0.25, f0_method='rmvpe', crepe_hop_length=128, protect=0.33, pitch_change_all=0,
|
254 |
+
reverb_rm_size=0.15, reverb_wet=0.2, reverb_dry=0.8, reverb_damping=0.7, output_format='mp3',progress=gr.Progress()):
|
255 |
+
try:
|
256 |
+
#Load mdx parameters
|
257 |
+
with open(os.path.join(mdxnet_models_dir, 'model_data.json')) as infile:
|
258 |
+
mdx_model_params = json.load(infile)
|
259 |
+
|
260 |
+
# Get path of loaded vocal file
|
261 |
+
input_type = 'local'
|
262 |
+
song_input = song_input.strip('\"')
|
263 |
+
if os.path.exists(song_input):
|
264 |
+
song_id = get_hash(song_input)
|
265 |
+
else:
|
266 |
+
error_msg = f'{song_input} does not exist.'
|
267 |
+
song_id = None
|
268 |
+
raise_exception(error_msg, is_webui)
|
269 |
+
|
270 |
+
song_dir = os.path.join(output_dir, song_id)
|
271 |
+
os.makedirs(song_dir)
|
272 |
+
|
273 |
+
if not os.path.exists(song_dir):
|
274 |
+
os.makedirs(song_dir)
|
275 |
+
orig_song_path, vocals_path = preprocess_vocals_only(song_input, mdx_model_params, song_id, is_webui, input_type, progress)
|
276 |
+
else:
|
277 |
+
vocals_path = None
|
278 |
+
for file in os.listdir(song_dir):
|
279 |
+
vocals_path = os.path.join(song_dir, file)
|
280 |
+
return vocals_path
|
281 |
+
|
282 |
+
# if any of the audio files aren't available or keep intermediate files, rerun preprocess
|
283 |
+
#if any(path is None for path in paths):
|
284 |
+
# orig_song_path, vocals_path= preprocess_vocals_only(song_input, mdx_model_params, song_id, is_webui, input_type, progress)
|
285 |
+
#else:
|
286 |
+
# orig_song_path = paths
|
287 |
+
#orig_song_path, vocals_path = preprocess_vocals_only(song_input, mdx_model_params, song_id, is_webui, input_type, progress)
|
288 |
+
|
289 |
+
pitch_change = pitch_change * 12 + pitch_change_all
|
290 |
+
ai_vocals_path = os.path.join(song_dir, f'{os.path.splitext(os.path.basename(orig_song_path))[0]}_{voice_model}_p{pitch_change}_i{index_rate}_fr{filter_radius}_rms{rms_mix_rate}_pro{protect}_{f0_method}{"" if f0_method != "mangio-crepe" else f"_{crepe_hop_length}"}.wav')
|
291 |
+
display_progress(f'[~] Post reverb {ai_vocals_path}', 0.5, is_webui, progress)
|
292 |
+
time.sleep(10)
|
293 |
+
|
294 |
+
display_progress('[~] Converting voice using RVC...', 0.5, is_webui, progress)
|
295 |
+
voice_change(voice_model, vocals_path, ai_vocals_path, pitch_change, f0_method, index_rate, filter_radius, rms_mix_rate, protect, crepe_hop_length, is_webui)
|
296 |
+
display_progress('[~] Applying audio effects to Vocals...', 0.8, is_webui, progress)
|
297 |
+
ai_vocals_mixed_path = add_audio_effects(ai_vocals_path, reverb_rm_size, reverb_wet, reverb_dry, reverb_damping)
|
298 |
+
|
299 |
+
return ai_vocals_mixed_path
|
300 |
+
except Exception as e:
|
301 |
+
raise_exception(str(e), is_webui)
|
302 |
+
|
303 |
+
|
304 |
+
|
305 |
+
def song_cover_pipeline(song_input, voice_model, pitch_change, keep_files,
|
306 |
+
is_webui=0, main_gain=0, backup_gain=0, inst_gain=0, index_rate=0.5, filter_radius=3,
|
307 |
+
rms_mix_rate=0.25, f0_method='rmvpe', crepe_hop_length=128, protect=0.33, pitch_change_all=0,
|
308 |
+
reverb_rm_size=0.15, reverb_wet=0.2, reverb_dry=0.8, reverb_damping=0.7, output_format='mp3',
|
309 |
+
progress=gr.Progress()):
|
310 |
+
|
311 |
+
try:
|
312 |
+
if not song_input or not voice_model:
|
313 |
+
raise_exception('Ensure that the song input field and voice model field is filled.', is_webui)
|
314 |
+
|
315 |
+
display_progress('[~] Starting AI Cover Generation Pipeline...', 0, is_webui, progress)
|
316 |
+
|
317 |
+
with open(os.path.join(mdxnet_models_dir, 'model_data.json')) as infile:
|
318 |
+
mdx_model_params = json.load(infile)
|
319 |
+
|
320 |
+
# if youtube url
|
321 |
+
if urlparse(song_input).scheme == 'https':
|
322 |
+
input_type = 'yt'
|
323 |
+
song_id = get_youtube_video_id(song_input)
|
324 |
+
if song_id is None:
|
325 |
+
error_msg = 'Invalid YouTube url.'
|
326 |
+
raise_exception(error_msg, is_webui)
|
327 |
+
|
328 |
+
# local audio file
|
329 |
+
else:
|
330 |
+
input_type = 'local'
|
331 |
+
song_input = song_input.strip('\"')
|
332 |
+
if os.path.exists(song_input):
|
333 |
+
song_id = get_hash(song_input)
|
334 |
+
else:
|
335 |
+
error_msg = f'{song_input} does not exist.'
|
336 |
+
song_id = None
|
337 |
+
raise_exception(error_msg, is_webui)
|
338 |
+
|
339 |
+
song_dir = os.path.join(output_dir, song_id)
|
340 |
+
|
341 |
+
if not os.path.exists(song_dir):
|
342 |
+
os.makedirs(song_dir)
|
343 |
+
orig_song_path, vocals_path, instrumentals_path, main_vocals_path, backup_vocals_path, main_vocals_dereverb_path = preprocess_song(song_input, mdx_model_params, song_id, is_webui, input_type, progress)
|
344 |
+
|
345 |
+
else:
|
346 |
+
vocals_path, main_vocals_path = None, None
|
347 |
+
paths = get_audio_paths(song_dir)
|
348 |
+
|
349 |
+
# if any of the audio files aren't available or keep intermediate files, rerun preprocess
|
350 |
+
if any(path is None for path in paths) or keep_files:
|
351 |
+
orig_song_path, vocals_path, instrumentals_path, main_vocals_path, backup_vocals_path, main_vocals_dereverb_path = preprocess_song(song_input, mdx_model_params, song_id, is_webui, input_type, progress)
|
352 |
+
else:
|
353 |
+
orig_song_path, instrumentals_path, main_vocals_dereverb_path, backup_vocals_path = paths
|
354 |
+
|
355 |
+
pitch_change = pitch_change * 12 + pitch_change_all
|
356 |
+
ai_vocals_path = os.path.join(song_dir, f'{os.path.splitext(os.path.basename(orig_song_path))[0]}_{voice_model}_p{pitch_change}_i{index_rate}_fr{filter_radius}_rms{rms_mix_rate}_pro{protect}_{f0_method}{"" if f0_method != "mangio-crepe" else f"_{crepe_hop_length}"}.wav')
|
357 |
+
# Added _cover at the end of filename
|
358 |
+
ai_cover_path = os.path.join(song_dir, f'{os.path.splitext(os.path.basename(orig_song_path))[0]} ({voice_model} Ver)_cover.{output_format}')
|
359 |
+
|
360 |
+
if not os.path.exists(ai_vocals_path):
|
361 |
+
display_progress('[~] Converting voice using RVC...', 0.5, is_webui, progress)
|
362 |
+
voice_change(voice_model, main_vocals_dereverb_path, ai_vocals_path, pitch_change, f0_method, index_rate, filter_radius, rms_mix_rate, protect, crepe_hop_length, is_webui)
|
363 |
+
|
364 |
+
display_progress('[~] Applying audio effects to Vocals...', 0.8, is_webui, progress)
|
365 |
+
ai_vocals_mixed_path = add_audio_effects(ai_vocals_path, reverb_rm_size, reverb_wet, reverb_dry, reverb_damping)
|
366 |
+
|
367 |
+
if pitch_change_all != 0:
|
368 |
+
display_progress('[~] Applying overall pitch change', 0.85, is_webui, progress)
|
369 |
+
instrumentals_path = pitch_shift(instrumentals_path, pitch_change_all)
|
370 |
+
backup_vocals_path = pitch_shift(backup_vocals_path, pitch_change_all)
|
371 |
+
|
372 |
+
display_progress('[~] Combining AI Vocals and Instrumentals...', 0.9, is_webui, progress)
|
373 |
+
combine_audio([ai_vocals_mixed_path, backup_vocals_path, instrumentals_path], ai_cover_path, main_gain, backup_gain, inst_gain, output_format)
|
374 |
+
|
375 |
+
if not keep_files:
|
376 |
+
display_progress('[~] Removing intermediate audio files...', 0.95, is_webui, progress)
|
377 |
+
intermediate_files = [vocals_path, main_vocals_path, ai_vocals_mixed_path]
|
378 |
+
if pitch_change_all != 0:
|
379 |
+
intermediate_files += [instrumentals_path, backup_vocals_path]
|
380 |
+
for file in intermediate_files:
|
381 |
+
if file and os.path.exists(file):
|
382 |
+
os.remove(file)
|
383 |
+
|
384 |
+
|
385 |
+
# Returning the stems: AI cover, original vocal, original instrumental, AI generated vocal
|
386 |
+
|
387 |
+
return ai_cover_path, vocals_path, instrumentals_path, ai_vocals_mixed_path
|
388 |
+
|
389 |
+
except Exception as e:
|
390 |
+
raise_exception(str(e), is_webui)
|
391 |
+
|
392 |
+
|
393 |
+
if __name__ == '__main__':
|
394 |
+
parser = argparse.ArgumentParser(description='Generate a AI cover song in the song_output/id directory.', add_help=True)
|
395 |
+
parser.add_argument('-i', '--song-input', type=str, required=True, help='Link to a YouTube video or the filepath to a local mp3/wav file to create an AI cover of')
|
396 |
+
parser.add_argument('-dir', '--rvc-dirname', type=str, required=True, help='Name of the folder in the rvc_models directory containing the RVC model file and optional index file to use')
|
397 |
+
parser.add_argument('-p', '--pitch-change', type=int, required=True, help='Change the pitch of AI Vocals only. Generally, use 1 for male to female and -1 for vice-versa. (Octaves)')
|
398 |
+
parser.add_argument('-k', '--keep-files', action=argparse.BooleanOptionalAction, help='Whether to keep all intermediate audio files generated in the song_output/id directory, e.g. Isolated Vocals/Instrumentals')
|
399 |
+
parser.add_argument('-ir', '--index-rate', type=float, default=0.5, help='A decimal number e.g. 0.5, used to reduce/resolve the timbre leakage problem. If set to 1, more biased towards the timbre quality of the training dataset')
|
400 |
+
parser.add_argument('-fr', '--filter-radius', type=int, default=3, help='A number between 0 and 7. If >=3: apply median filtering to the harvested pitch results. The value represents the filter radius and can reduce breathiness.')
|
401 |
+
parser.add_argument('-rms', '--rms-mix-rate', type=float, default=0.25, help="A decimal number e.g. 0.25. Control how much to use the original vocal's loudness (0) or a fixed loudness (1).")
|
402 |
+
parser.add_argument('-palgo', '--pitch-detection-algo', type=str, default='rmvpe', help='Best option is rmvpe (clarity in vocals), then mangio-crepe (smoother vocals).')
|
403 |
+
parser.add_argument('-hop', '--crepe-hop-length', type=int, default=128, help='If pitch detection algo is mangio-crepe, controls how often it checks for pitch changes in milliseconds. The higher the value, the faster the conversion and less risk of voice cracks, but there is less pitch accuracy. Recommended: 128.')
|
404 |
+
parser.add_argument('-pro', '--protect', type=float, default=0.33, help='A decimal number e.g. 0.33. Protect voiceless consonants and breath sounds to prevent artifacts such as tearing in electronic music. Set to 0.5 to disable. Decrease the value to increase protection, but it may reduce indexing accuracy.')
|
405 |
+
parser.add_argument('-mv', '--main-vol', type=int, default=0, help='Volume change for AI main vocals in decibels. Use -3 to decrease by 3 decibels and 3 to increase by 3 decibels')
|
406 |
+
parser.add_argument('-bv', '--backup-vol', type=int, default=0, help='Volume change for backup vocals in decibels')
|
407 |
+
parser.add_argument('-iv', '--inst-vol', type=int, default=0, help='Volume change for instrumentals in decibels')
|
408 |
+
parser.add_argument('-pall', '--pitch-change-all', type=int, default=0, help='Change the pitch/key of vocals and instrumentals. Changing this slightly reduces sound quality')
|
409 |
+
parser.add_argument('-rsize', '--reverb-size', type=float, default=0.15, help='Reverb room size between 0 and 1')
|
410 |
+
parser.add_argument('-rwet', '--reverb-wetness', type=float, default=0.2, help='Reverb wet level between 0 and 1')
|
411 |
+
parser.add_argument('-rdry', '--reverb-dryness', type=float, default=0.8, help='Reverb dry level between 0 and 1')
|
412 |
+
parser.add_argument('-rdamp', '--reverb-damping', type=float, default=0.7, help='Reverb damping between 0 and 1')
|
413 |
+
parser.add_argument('-oformat', '--output-format', type=str, default='mp3', help='Output format of audio file. mp3 for smaller file size, wav for best quality')
|
414 |
+
args = parser.parse_args()
|
415 |
+
|
416 |
+
rvc_dirname = args.rvc_dirname
|
417 |
+
if not os.path.exists(os.path.join(rvc_models_dir, rvc_dirname)):
|
418 |
+
raise Exception(f'The folder {os.path.join(rvc_models_dir, rvc_dirname)} does not exist.')
|
419 |
+
|
420 |
+
cover_path, original_vocals, original_instrumentals, ai_vocals= song_cover_pipeline(args.song_input, rvc_dirname, args.pitch_change, args.keep_files,
|
421 |
+
#cover_path, original_vocals = song_cover_pipeline(args.song_input, rvc_dirname, args.pitch_change, args.keep_files,
|
422 |
+
main_gain=args.main_vol, backup_gain=args.backup_vol, inst_gain=args.inst_vol,
|
423 |
+
index_rate=args.index_rate, filter_radius=args.filter_radius,
|
424 |
+
rms_mix_rate=args.rms_mix_rate, f0_method=args.pitch_detection_algo,
|
425 |
+
crepe_hop_length=args.crepe_hop_length, protect=args.protect,
|
426 |
+
pitch_change_all=args.pitch_change_all,
|
427 |
+
reverb_rm_size=args.reverb_size, reverb_wet=args.reverb_wetness,
|
428 |
+
reverb_dry=args.reverb_dryness, reverb_damping=args.reverb_damping,
|
429 |
+
output_format=args.output_format, vocal_only=False)
|
430 |
+
|
431 |
+
|
432 |
+
|
433 |
+
print(f'[+] Cover generated at {cover_path}')
|
434 |
+
print(f'[+] Original vocals at {original_vocals}')
|
435 |
+
print(f'[+] Original instrumentals at {original_instrumentals}')
|
436 |
+
print(f'[+] AI vocals at {ai_vocals}')
|
437 |
+
|
438 |
+
|