Delete main
Browse filesFiles changed (41)
- main/app/app.pyDeleted
- main/app/clean.pyDeleted-40
- main/app/run_tensorboard.pyDeleted-30
- main/app/sync.pyDeleted-80
- main/configs/config.jsonDeleted-229
- main/configs/config.pyDeleted-120
- main/configs/v1/32000.jsonDeleted-82
- main/configs/v1/40000.jsonDeleted-80
- main/configs/v1/48000.jsonDeleted-82
- main/configs/v2/32000.jsonDeleted-76
- main/configs/v2/40000.jsonDeleted-76
- main/configs/v2/48000.jsonDeleted-76
- main/inference/audio_effects.pyDeleted-170
- main/inference/convert.pyDeleted-1060
- main/inference/create_dataset.pyDeleted-370
- main/inference/create_index.pyDeleted-120
- main/inference/extract.pyDeleted-450
- main/inference/preprocess.pyDeleted-360
- main/inference/separator_music.pyDeleted-400
- main/inference/train.pyDeleted-1600
- main/library/algorithm/commons.pyDeleted-100
- main/library/algorithm/modules.pyDeleted-80
- main/library/algorithm/residuals.pyDeleted-170
- main/library/algorithm/separator.pyDeleted-420
- main/library/algorithm/synthesizers.pyDeleted-590
- main/library/architectures/demucs_separator.pyDeleted-340
- main/library/architectures/mdx_separator.pyDeleted-370
- main/library/predictors/FCPE.pyDeleted-600
- main/library/predictors/RMVPE.pyDeleted-270
- main/library/uvr5_separator/common_separator.pyDeleted-270
- main/library/uvr5_separator/demucs/apply.pyDeleted-280
- main/library/uvr5_separator/demucs/demucs.pyDeleted-340
- main/library/uvr5_separator/demucs/hdemucs.pyDeleted-850
- main/library/uvr5_separator/demucs/htdemucs.pyDeleted-690
- main/library/uvr5_separator/demucs/states.pyDeleted-70
- main/library/uvr5_separator/demucs/utils.pyDeleted-10
- main/library/uvr5_separator/spec_utils.pyDeleted-1100
- main/tools/gdown.pyDeleted-230
- main/tools/mediafire.pyDeleted-30
- main/tools/meganz.pyDeleted-180
- main/tools/pixeldrain.pyDeleted-19
12345678910111213141516171819202122232425262728293031323334353637383940import time
import threading
from googleapiclient.discovery import build
class Clean:
def __init__(self, every=300):
self.service = build('drive', 'v3')
self.every = every
self.trash_cleanup_thread = None
def delete(self):
page_token = None
while 1:
response = self.service.files().list(q="trashed=true", spaces='drive', fields="nextPageToken, files(id, name)", pageToken=page_token).execute()
for file in response.get('files', []):
if file['name'].startswith("G_") or file['name'].startswith("D_"):
try:
self.service.files().delete(fileId=file['id']).execute()
except Exception as e:
raise RuntimeError(e)
page_token = response.get('nextPageToken', None)
if page_token is None: break
def clean(self):
while 1:
self.delete()
time.sleep(self.every)
def start(self):
self.trash_cleanup_thread = threading.Thread(target=self.clean)
self.trash_cleanup_thread.daemon = True
self.trash_cleanup_thread.start()
def stop(self):
if self.trash_cleanup_thread: self.trash_cleanup_thread.join()123456789101112131415161718192021222324252627282930import os
import sys
import json
import logging
import webbrowser
from tensorboard import program
sys.path.append(os.getcwd())
from main.configs.config import Config
translations = Config().translations
with open(os.path.join("main", "configs", "config.json"), "r") as f:
configs = json.load(f)
def launch_tensorboard_pipeline():
logging.getLogger("root").setLevel(logging.ERROR)
logging.getLogger("tensorboard").setLevel(logging.ERROR)
tb = program.TensorBoard()
tb.configure(argv=[None, "--logdir", "assets/logs", f"--port={configs["tensorboard_port"]}"])
url = tb.launch()
print(f"{translations['tensorboard_url']}: {url}")
webbrowser.open(url)
return f"{translations['tensorboard_url']}: {url}"
if __name__ == "__main__": launch_tensorboard_pipeline()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import timeimport threadingimport subprocess
from typing import List, Union
class Channel: def __init__(self, source, destination, sync_deletions=False, every=60, exclude: Union[str, List, None] = None): self.source = source self.destination = destination self.event = threading.Event() self.syncing_thread = threading.Thread(target=self._sync, args=()) self.sync_deletions = sync_deletions self.every = every
if not exclude: exclude = [] if isinstance(exclude,str): exclude = [exclude]
self.exclude = exclude self.command = ['rsync', '-aP']
def alive(self): if self.syncing_thread.is_alive(): return True else: return False
def _sync(self): command = self.command
for exclusion in self.exclude: command.append(f'--exclude={exclusion}')
command.extend([f'{self.source}/', f'{self.destination}/'])
if self.sync_deletions: command.append('--delete')
while not self.event.is_set(): subprocess.run(command) time.sleep(self.every)
def copy(self): command = self.command
for exclusion in self.exclude: command.append(f'--exclude={exclusion}')
command.extend([f'{self.source}/', f'{self.destination}/'])
if self.sync_deletions: command.append('--delete') subprocess.run(command)
return True
def start(self): if self.syncing_thread.is_alive(): self.event.set() self.syncing_thread.join()
if self.event.is_set(): self.event.clear() if self.syncing_thread._started.is_set(): self.syncing_thread = threading.Thread(target=self._sync, args=())
self.syncing_thread.start()
return self.alive()
def stop(self): if self.alive(): self.event.set() self.syncing_thread.join()
while self.alive(): if not self.alive(): break
return not self.alive()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229{ "language": "vi-VN", "support_language": [ "en-US", "vi-VN" ],
"theme": "NoCrypt/miku", "themes": [ "NoCrypt/miku", "gstaff/xkcd", "JohnSmith9982/small_and_pretty", "ParityError/Interstellar", "earneleh/paris", "shivi/calm_seafoam", "Hev832/Applio", "YTheme/Minecraft", "gstaff/sketch", "SebastianBravo/simci_css", "allenai/gradio-theme", "Nymbo/Nymbo_Theme_5", "lone17/kotaemon", "Zarkel/IBM_Carbon_Theme", "SherlockRamos/Feliz", "freddyaboulton/dracula_revamped", "freddyaboulton/bad-theme-space", "gradio/dracula_revamped", "abidlabs/dracula_revamped", "gradio/dracula_test", "gradio/seafoam", "gradio/glass", "gradio/monochrome", "gradio/soft", "gradio/default", "gradio/base", "abidlabs/pakistan", "dawood/microsoft_windows", "ysharma/steampunk", "ysharma/huggingface", "abidlabs/Lime", "freddyaboulton/this-theme-does-not-exist-2", "aliabid94/new-theme", "aliabid94/test2", "aliabid94/test3", "aliabid94/test4", "abidlabs/banana", "freddyaboulton/test-blue", "gstaff/whiteboard", "ysharma/llamas", "abidlabs/font-test", "YenLai/Superhuman", "bethecloud/storj_theme", "sudeepshouche/minimalist", "knotdgaf/gradiotest", "ParityError/Anime", "Ajaxon6255/Emerald_Isle", "ParityError/LimeFace", "finlaymacklon/smooth_slate", "finlaymacklon/boxy_violet", "derekzen/stardust", "EveryPizza/Cartoony-Gradio-Theme", "Ifeanyi/Cyanister", "Tshackelton/IBMPlex-DenseReadable", "snehilsanyal/scikit-learn", "Himhimhim/xkcd", "nota-ai/theme", "rawrsor1/Everforest", "rottenlittlecreature/Moon_Goblin", "abidlabs/test-yellow", "abidlabs/test-yellow3", "idspicQstitho/dracula_revamped", "kfahn/AnimalPose", "HaleyCH/HaleyCH_Theme", "simulKitke/dracula_test", "braintacles/CrimsonNight", "wentaohe/whiteboardv2", "reilnuud/polite", "remilia/Ghostly", "Franklisi/darkmode", "coding-alt/soft", "xiaobaiyuan/theme_land", "step-3-profit/Midnight-Deep", "xiaobaiyuan/theme_demo", "Taithrah/Minimal", "Insuz/SimpleIndigo", "zkunn/Alipay_Gradio_theme", "Insuz/Mocha", "xiaobaiyuan/theme_brief", "Ama434/434-base-Barlow", "Ama434/def_barlow", "Ama434/neutral-barlow", "dawood/dracula_test", "nuttea/Softblue", "BlueDancer/Alien_Diffusion", "naughtondale/monochrome", "Dagfinn1962/standard", "default" ],
"tts_voice": [ "af-ZA-AdriNeural", "af-ZA-WillemNeural", "sq-AL-AnilaNeural", "sq-AL-IlirNeural", "am-ET-AmehaNeural", "am-ET-MekdesNeural", "ar-DZ-AminaNeural", "ar-DZ-IsmaelNeural", "ar-BH-AliNeural", "ar-BH-LailaNeural", "ar-EG-SalmaNeural", "ar-EG-ShakirNeural", "ar-IQ-BasselNeural", "ar-IQ-RanaNeural", "ar-JO-SanaNeural", "ar-JO-TaimNeural", "ar-KW-FahedNeural", "ar-KW-NouraNeural", "ar-LB-LaylaNeural", "ar-LB-RamiNeural", "ar-LY-ImanNeural", "ar-LY-OmarNeural", "ar-MA-JamalNeural", "ar-MA-MounaNeural", "ar-OM-AbdullahNeural", "ar-OM-AyshaNeural", "ar-QA-AmalNeural", "ar-QA-MoazNeural", "ar-SA-HamedNeural", "ar-SA-ZariyahNeural", "ar-SY-AmanyNeural", "ar-SY-LaithNeural", "ar-TN-HediNeural", "ar-TN-ReemNeural", "ar-AE-FatimaNeural", "ar-AE-HamdanNeural", "ar-YE-MaryamNeural", "ar-YE-SalehNeural", "az-AZ-BabekNeural", "az-AZ-BanuNeural", "bn-BD-NabanitaNeural", "bn-BD-PradeepNeural", "bn-IN-BashkarNeural", "bn-IN-TanishaaNeural", "bs-BA-GoranNeural", "bs-BA-VesnaNeural", "bg-BG-BorislavNeural", "bg-BG-KalinaNeural", "my-MM-NilarNeural", "my-MM-ThihaNeural", "ca-ES-EnricNeural", "ca-ES-JoanaNeural", "zh-HK-HiuGaaiNeural", "zh-HK-HiuMaanNeural", "zh-HK-WanLungNeural", "zh-CN-XiaoxiaoNeural", "zh-CN-XiaoyiNeural", "zh-CN-YunjianNeural", "zh-CN-YunxiNeural", "zh-CN-YunxiaNeural", "zh-CN-YunyangNeural", "zh-CN-liaoning-XiaobeiNeural", "zh-TW-HsiaoChenNeural", "zh-TW-YunJheNeural", "zh-TW-HsiaoYuNeural", "zh-CN-shaanxi-XiaoniNeural", "hr-HR-GabrijelaNeural", "hr-HR-SreckoNeural", "cs-CZ-AntoninNeural", "cs-CZ-VlastaNeural", "da-DK-ChristelNeural", "da-DK-JeppeNeural", "nl-BE-ArnaudNeural", "nl-BE-DenaNeural", "nl-NL-ColetteNeural", "nl-NL-FennaNeural", "nl-NL-MaartenNeural", "en-AU-NatashaNeural", "en-AU-WilliamNeural", "en-CA-ClaraNeural", "en-CA-LiamNeural", "en-HK-SamNeural", "en-HK-YanNeural", "en-IN-NeerjaExpressiveNeural", "en-IN-NeerjaNeural", "en-IN-PrabhatNeural", "en-IE-ConnorNeural", "en-IE-EmilyNeural", "en-KE-AsiliaNeural", "en-KE-ChilembaNeural", "en-NZ-MitchellNeural", "en-NZ-MollyNeural", "en-NG-AbeoNeural", "en-NG-EzinneNeural", "en-PH-JamesNeural", "en-PH-RosaNeural", "en-SG-LunaNeural", "en-SG-WayneNeural", "en-ZA-LeahNeural", "en-ZA-LukeNeural", "en-TZ-ElimuNeural", "en-TZ-ImaniNeural", "en-GB-LibbyNeural", "en-GB-MaisieNeural", "en-GB-RyanNeural", "en-GB-SoniaNeural", "en-GB-ThomasNeural", "en-US-AvaMultilingualNeural", "en-US-AndrewMultilingualNeural", "en-US-EmmaMultilingualNeural", "en-US-BrianMultilingualNeural", "en-US-AvaNeural", "en-US-AndrewNeural", "en-US-EmmaNeural", "en-US-BrianNeural", "en-US-AnaNeural", "en-US-AriaNeural", "en-US-ChristopherNeural", "en-US-EricNeural", "en-US-GuyNeural", "en-US-JennyNeural", "en-US-MichelleNeural", "en-US-RogerNeural", "en-US-SteffanNeural", "et-EE-AnuNeural", "et-EE-KertNeural", "fil-PH-AngeloNeural", "fil-PH-BlessicaNeural", "fi-FI-HarriNeural", "fi-FI-NooraNeural", "fr-BE-CharlineNeural", "fr-BE-GerardNeural", "fr-CA-ThierryNeural", "fr-CA-AntoineNeural", "fr-CA-JeanNeural", "fr-CA-SylvieNeural", "fr-FR-VivienneMultilingualNeural", "fr-FR-RemyMultilingualNeural", "fr-FR-DeniseNeural", "fr-FR-EloiseNeural", "fr-FR-HenriNeural", "fr-CH-ArianeNeural", "fr-CH-FabriceNeural", "gl-ES-RoiNeural", "gl-ES-SabelaNeural", "ka-GE-EkaNeural", "ka-GE-GiorgiNeural", "de-AT-IngridNeural", "de-AT-JonasNeural", "de-DE-SeraphinaMultilingualNeural", "de-DE-FlorianMultilingualNeural", "de-DE-AmalaNeural", "de-DE-ConradNeural", "de-DE-KatjaNeural", "de-DE-KillianNeural", "de-CH-JanNeural", "de-CH-LeniNeural", "el-GR-AthinaNeural", "el-GR-NestorasNeural", "gu-IN-DhwaniNeural", "gu-IN-NiranjanNeural", "he-IL-AvriNeural", "he-IL-HilaNeural", "hi-IN-MadhurNeural", "hi-IN-SwaraNeural", "hu-HU-NoemiNeural", "hu-HU-TamasNeural", "is-IS-GudrunNeural", "is-IS-GunnarNeural", "id-ID-ArdiNeural", "id-ID-GadisNeural", "ga-IE-ColmNeural", "ga-IE-OrlaNeural", "it-IT-GiuseppeNeural", "it-IT-DiegoNeural", "it-IT-ElsaNeural", "it-IT-IsabellaNeural", "ja-JP-KeitaNeural", "ja-JP-NanamiNeural", "jv-ID-DimasNeural", "jv-ID-SitiNeural", "kn-IN-GaganNeural", "kn-IN-SapnaNeural", "kk-KZ-AigulNeural", "kk-KZ-DauletNeural", "km-KH-PisethNeural", "km-KH-SreymomNeural", "ko-KR-HyunsuNeural", "ko-KR-InJoonNeural", "ko-KR-SunHiNeural", "lo-LA-ChanthavongNeural", "lo-LA-KeomanyNeural", "lv-LV-EveritaNeural", "lv-LV-NilsNeural", "lt-LT-LeonasNeural", "lt-LT-OnaNeural", "mk-MK-AleksandarNeural", "mk-MK-MarijaNeural", "ms-MY-OsmanNeural", "ms-MY-YasminNeural", "ml-IN-MidhunNeural", "ml-IN-SobhanaNeural", "mt-MT-GraceNeural", "mt-MT-JosephNeural", "mr-IN-AarohiNeural", "mr-IN-ManoharNeural", "mn-MN-BataaNeural", "mn-MN-YesuiNeural", "ne-NP-HemkalaNeural", "ne-NP-SagarNeural", "nb-NO-FinnNeural", "nb-NO-PernilleNeural", "ps-AF-GulNawazNeural", "ps-AF-LatifaNeural", "fa-IR-DilaraNeural", "fa-IR-FaridNeural", "pl-PL-MarekNeural", "pl-PL-ZofiaNeural", "pt-BR-ThalitaNeural", "pt-BR-AntonioNeural", "pt-BR-FranciscaNeural", "pt-PT-DuarteNeural", "pt-PT-RaquelNeural", "ro-RO-AlinaNeural", "ro-RO-EmilNeural", "ru-RU-DmitryNeural", "ru-RU-SvetlanaNeural", "sr-RS-NicholasNeural", "sr-RS-SophieNeural", "si-LK-SameeraNeural", "si-LK-ThiliniNeural", "sk-SK-LukasNeural", "sk-SK-ViktoriaNeural", "sl-SI-PetraNeural", "sl-SI-RokNeural", "so-SO-MuuseNeural", "so-SO-UbaxNeural", "es-AR-ElenaNeural", "es-AR-TomasNeural", "es-BO-MarceloNeural", "es-BO-SofiaNeural", "es-CL-CatalinaNeural", "es-CL-LorenzoNeural", "es-ES-XimenaNeural", "es-CO-GonzaloNeural", "es-CO-SalomeNeural", "es-CR-JuanNeural", "es-CR-MariaNeural", "es-CU-BelkysNeural", "es-CU-ManuelNeural", "es-DO-EmilioNeural", "es-DO-RamonaNeural", "es-EC-AndreaNeural", "es-EC-LuisNeural", "es-SV-LorenaNeural", "es-SV-RodrigoNeural", "es-GQ-JavierNeural", "es-GQ-TeresaNeural", "es-GT-AndresNeural", "es-GT-MartaNeural", "es-HN-CarlosNeural", "es-HN-KarlaNeural", "es-MX-DaliaNeural", "es-MX-JorgeNeural", "es-NI-FedericoNeural", "es-NI-YolandaNeural", "es-PA-MargaritaNeural", "es-PA-RobertoNeural", "es-PY-MarioNeural", "es-PY-TaniaNeural", "es-PE-AlexNeural", "es-PE-CamilaNeural", "es-PR-KarinaNeural", "es-PR-VictorNeural", "es-ES-AlvaroNeural", "es-ES-ElviraNeural", "es-US-AlonsoNeural", "es-US-PalomaNeural", "es-UY-MateoNeural", "es-UY-ValentinaNeural", "es-VE-PaolaNeural", "es-VE-SebastianNeural", "su-ID-JajangNeural", "su-ID-TutiNeural", "sw-KE-RafikiNeural", "sw-KE-ZuriNeural", "sw-TZ-DaudiNeural", "sw-TZ-RehemaNeural", "sv-SE-MattiasNeural", "sv-SE-SofieNeural", "ta-IN-PallaviNeural", "ta-IN-ValluvarNeural", "ta-MY-KaniNeural", "ta-MY-SuryaNeural", "ta-SG-AnbuNeural", "ta-SG-VenbaNeural", "ta-LK-KumarNeural", "ta-LK-SaranyaNeural", "te-IN-MohanNeural", "te-IN-ShrutiNeural", "th-TH-NiwatNeural", "th-TH-PremwadeeNeural", "tr-TR-AhmetNeural", "tr-TR-EmelNeural", "uk-UA-OstapNeural", "uk-UA-PolinaNeural", "ur-IN-GulNeural", "ur-IN-SalmanNeural", "ur-PK-AsadNeural", "ur-PK-UzmaNeural", "uz-UZ-MadinaNeural", "uz-UZ-SardorNeural", "vi-VN-HoaiMyNeural", "vi-VN-NamMinhNeural", "cy-GB-AledNeural", "cy-GB-NiaNeural", "zu-ZA-ThandoNeural", "zu-ZA-ThembaNeural" ],
"separator_tab": false, "convert_tab": true, "tts_tab": true, "effects_tab": true, "create_dataset_tab": false, "training_tab": false, "fushion_tab": false, "read_tab": false, "downloads_tab": true, "settings_tab": false,
"app_port": 7860, "tensorboard_port": 6870, "num_of_restart": 5, "server_name": "0.0.0.0", "app_show_error": true, "share": false}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120import osimport jsonimport torch
version_config_paths = [ os.path.join("v1", "32000.json"), os.path.join("v1", "40000.json"), os.path.join("v1", "48000.json"), os.path.join("v2", "48000.json"), os.path.join("v2", "40000.json"), os.path.join("v2", "32000.json"),]
def singleton(cls): instances = {}
def get_instance(*args, **kwargs): if cls not in instances: instances[cls] = cls(*args, **kwargs)
return instances[cls] return get_instance
@singletonclass Config: def __init__(self): self.device = "cuda:0" if torch.cuda.is_available() else "cpu" self.is_half = self.device != "cpu" self.gpu_name = (torch.cuda.get_device_name(int(self.device.split(":")[-1])) if self.device.startswith("cuda") else None) self.json_config = self.load_config_json() self.translations = self.multi_language() self.gpu_mem = None self.x_pad, self.x_query, self.x_center, self.x_max = self.device_config()
def load_config_json(self) -> dict: configs = {} for config_file in version_config_paths: config_path = os.path.join("main", "configs", config_file)
with open(config_path, "r") as f: configs[config_file] = json.load(f)
return configs def multi_language(self): with open(os.path.join("main", "configs", "config.json"), "r") as f: configs = json.load(f)
lang = configs["language"] if len([l for l in os.listdir(os.path.join("assets", "languages")) if l.endswith(".json")]) < 1: raise FileNotFoundError("Không tìm thấy bất cứ gói ngôn ngữ nào(No package languages found)") if not lang: lang = "vi-VN" if lang not in configs["support_language"]: raise ValueError("Ngôn ngữ không được hỗ trợ(Language not supported)")
lang_path = os.path.join("assets", "languages", f"{lang}.json") if not os.path.exists(lang_path): lang_path = os.path.join("assets", "languages", f"vi-VN.json")
with open(lang_path, encoding="utf-8") as f: translations = json.load(f) return translations
def set_precision(self, precision): if precision not in ["fp32", "fp16"]: raise ValueError("Loại chính xác không hợp lệ. Phải là 'fp32' hoặc 'fp16'(Invalid precision type. Must be 'fp32' or 'fp16').")
fp16_run_value = precision == "fp16"
for config_path in version_config_paths: full_config_path = os.path.join("main", "configs", config_path)
try: with open(full_config_path, "r") as f: config = json.load(f)
config["train"]["fp16_run"] = fp16_run_value
with open(full_config_path, "w") as f: json.dump(config, f, indent=4) except FileNotFoundError: print(self.translations["not_found"].format(name=full_config_path))
return self.translations["set_precision"].format(precision=precision)
def device_config(self) -> tuple: if self.device.startswith("cuda"): self.set_cuda_config() elif self.has_mps(): self.device = "mps" self.is_half = False self.set_precision("fp32") else: self.device = "cpu" self.is_half = False self.set_precision("fp32")
x_pad, x_query, x_center, x_max = ((3, 10, 60, 65) if self.is_half else (1, 6, 38, 41)) if self.gpu_mem is not None and self.gpu_mem <= 4: x_pad, x_query, x_center, x_max = (1, 5, 30, 32)
return x_pad, x_query, x_center, x_max
def set_cuda_config(self): i_device = int(self.device.split(":")[-1]) self.gpu_name = torch.cuda.get_device_name(i_device) low_end_gpus = ["16", "P40", "P10", "1060", "1070", "1080"]
if (any(gpu in self.gpu_name for gpu in low_end_gpus) and "V100" not in self.gpu_name.upper()): self.is_half = False self.set_precision("fp32")
self.gpu_mem = torch.cuda.get_device_properties(i_device).total_memory // (1024**3)
def has_mps(self) -> bool: return torch.backends.mps.is_available()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182{
"train": {
"log_interval": 200,
"seed": 1234,
"epochs": 20000,
"learning_rate": 0.0001,
"betas": [
0.8,
0.99
],
"eps": 1e-09,
"batch_size": 4,
"fp16_run": false,
"lr_decay": 0.999875,
"segment_size": 12800,
"init_lr_ratio": 1,
"warmup_epochs": 0,
"c_mel": 45,
"c_kl": 1.0
},
"data": {
"max_wav_value": 32768.0,
"sample_rate": 32000,
"filter_length": 1024,
"hop_length": 320,
"win_length": 1024,
"n_mel_channels": 80,
"mel_fmin": 0.0,
"mel_fmax": null
},
"model": {
"inter_channels": 192,
"hidden_channels": 192,
"filter_channels": 768,
"text_enc_hidden_dim": 256,
"n_heads": 2,
"n_layers": 6,
"kernel_size": 3,
"p_dropout": 0,
"resblock": "1",
"resblock_kernel_sizes": [
3,
7,
11
],
"resblock_dilation_sizes": [
[
1,
3,
5
],
[
1,
3,
5
],
[
1,
3,
5
]
],
"upsample_rates": [
10,
4,
2,
2,
2
],
"upsample_initial_channel": 512,
"upsample_kernel_sizes": [
16,
16,
4,
4,
4
],
"use_spectral_norm": false,
"gin_channels": 256,
"spk_embed_dim": 109
}
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980{
"train": {
"log_interval": 200,
"seed": 1234,
"epochs": 20000,
"learning_rate": 0.0001,
"betas": [
0.8,
0.99
],
"eps": 1e-09,
"batch_size": 4,
"fp16_run": false,
"lr_decay": 0.999875,
"segment_size": 12800,
"init_lr_ratio": 1,
"warmup_epochs": 0,
"c_mel": 45,
"c_kl": 1.0
},
"data": {
"max_wav_value": 32768.0,
"sample_rate": 40000,
"filter_length": 2048,
"hop_length": 400,
"win_length": 2048,
"n_mel_channels": 125,
"mel_fmin": 0.0,
"mel_fmax": null
},
"model": {
"inter_channels": 192,
"hidden_channels": 192,
"filter_channels": 768,
"text_enc_hidden_dim": 256,
"n_heads": 2,
"n_layers": 6,
"kernel_size": 3,
"p_dropout": 0,
"resblock": "1",
"resblock_kernel_sizes": [
3,
7,
11
],
"resblock_dilation_sizes": [
[
1,
3,
5
],
[
1,
3,
5
],
[
1,
3,
5
]
],
"upsample_rates": [
10,
10,
2,
2
],
"upsample_initial_channel": 512,
"upsample_kernel_sizes": [
16,
16,
4,
4
],
"use_spectral_norm": false,
"gin_channels": 256,
"spk_embed_dim": 109
}
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182{
"train": {
"log_interval": 200,
"seed": 1234,
"epochs": 20000,
"learning_rate": 0.0001,
"betas": [
0.8,
0.99
],
"eps": 1e-09,
"batch_size": 4,
"fp16_run": false,
"lr_decay": 0.999875,
"segment_size": 11520,
"init_lr_ratio": 1,
"warmup_epochs": 0,
"c_mel": 45,
"c_kl": 1.0
},
"data": {
"max_wav_value": 32768.0,
"sample_rate": 48000,
"filter_length": 2048,
"hop_length": 480,
"win_length": 2048,
"n_mel_channels": 128,
"mel_fmin": 0.0,
"mel_fmax": null
},
"model": {
"inter_channels": 192,
"hidden_channels": 192,
"filter_channels": 768,
"text_enc_hidden_dim": 256,
"n_heads": 2,
"n_layers": 6,
"kernel_size": 3,
"p_dropout": 0,
"resblock": "1",
"resblock_kernel_sizes": [
3,
7,
11
],
"resblock_dilation_sizes": [
[
1,
3,
5
],
[
1,
3,
5
],
[
1,
3,
5
]
],
"upsample_rates": [
10,
6,
2,
2,
2
],
"upsample_initial_channel": 512,
"upsample_kernel_sizes": [
16,
16,
4,
4,
4
],
"use_spectral_norm": false,
"gin_channels": 256,
"spk_embed_dim": 109
}
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576{
"train": {
"log_interval": 200,
"seed": 1234,
"learning_rate": 0.0001,
"betas": [
0.8,
0.99
],
"eps": 1e-09,
"fp16_run": false,
"lr_decay": 0.999875,
"segment_size": 12800,
"c_mel": 45,
"c_kl": 1.0
},
"data": {
"max_wav_value": 32768.0,
"sample_rate": 32000,
"filter_length": 1024,
"hop_length": 320,
"win_length": 1024,
"n_mel_channels": 80,
"mel_fmin": 0.0,
"mel_fmax": null
},
"model": {
"inter_channels": 192,
"hidden_channels": 192,
"filter_channels": 768,
"text_enc_hidden_dim": 768,
"n_heads": 2,
"n_layers": 6,
"kernel_size": 3,
"p_dropout": 0,
"resblock": "1",
"resblock_kernel_sizes": [
3,
7,
11
],
"resblock_dilation_sizes": [
[
1,
3,
5
],
[
1,
3,
5
],
[
1,
3,
5
]
],
"upsample_rates": [
10,
8,
2,
2
],
"upsample_initial_channel": 512,
"upsample_kernel_sizes": [
20,
16,
4,
4
],
"use_spectral_norm": false,
"gin_channels": 256,
"spk_embed_dim": 109
}
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576{
"train": {
"log_interval": 200,
"seed": 1234,
"learning_rate": 0.0001,
"betas": [
0.8,
0.99
],
"eps": 1e-09,
"fp16_run": false,
"lr_decay": 0.999875,
"segment_size": 12800,
"c_mel": 45,
"c_kl": 1.0
},
"data": {
"max_wav_value": 32768.0,
"sample_rate": 40000,
"filter_length": 2048,
"hop_length": 400,
"win_length": 2048,
"n_mel_channels": 125,
"mel_fmin": 0.0,
"mel_fmax": null
},
"model": {
"inter_channels": 192,
"hidden_channels": 192,
"filter_channels": 768,
"text_enc_hidden_dim": 768,
"n_heads": 2,
"n_layers": 6,
"kernel_size": 3,
"p_dropout": 0,
"resblock": "1",
"resblock_kernel_sizes": [
3,
7,
11
],
"resblock_dilation_sizes": [
[
1,
3,
5
],
[
1,
3,
5
],
[
1,
3,
5
]
],
"upsample_rates": [
10,
10,
2,
2
],
"upsample_initial_channel": 512,
"upsample_kernel_sizes": [
16,
16,
4,
4
],
"use_spectral_norm": false,
"gin_channels": 256,
"spk_embed_dim": 109
}
}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576{
"train": {
"log_interval": 200,
"seed": 1234,
"learning_rate": 0.0001,
"betas": [
0.8,
0.99
],
"eps": 1e-09,
"fp16_run": false,
"lr_decay": 0.999875,
"segment_size": 17280,
"c_mel": 45,
"c_kl": 1.0
},
"data": {
"max_wav_value": 32768.0,
"sample_rate": 48000,
"filter_length": 2048,
"hop_length": 480,
"win_length": 2048,
"n_mel_channels": 128,
"mel_fmin": 0.0,
"mel_fmax": null
},
"model": {
"inter_channels": 192,
"hidden_channels": 192,
"filter_channels": 768,
"text_enc_hidden_dim": 768,
"n_heads": 2,
"n_layers": 6,
"kernel_size": 3,
"p_dropout": 0,
"resblock": "1",
"resblock_kernel_sizes": [
3,
7,
11
],
"resblock_dilation_sizes": [
[
1,
3,
5
],
[
1,
3,
5
],
[
1,
3,
5
]
],
"upsample_rates": [
12,
10,
2,
2
],
"upsample_initial_channel": 512,
"upsample_kernel_sizes": [
24,
20,
4,
4
],
"use_spectral_norm": false,
"gin_channels": 256,
"spk_embed_dim": 109
}
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170import os
import sys
import librosa
import argparse
import numpy as np
import soundfile as sf
from distutils.util import strtobool
from scipy.signal import butter, filtfilt
from pedalboard import Pedalboard, Chorus, Distortion, Reverb, PitchShift, Delay, Limiter, Gain, Bitcrush, Clipping, Compressor, Phaser
now_dir = os.getcwd()
sys.path.append(now_dir)
from main.configs.config import Config
translations = Config().translations
def parse_arguments() -> tuple:
parser = argparse.ArgumentParser()
parser.add_argument("--input_path", type=str, required=True)
parser.add_argument("--output_path", type=str, default="./audios/apply_effects.wav")
parser.add_argument("--resample", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--resample_sr", type=int, default=0)
parser.add_argument("--chorus", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--chorus_depth", type=float, default=0.5)
parser.add_argument("--chorus_rate", type=float, default=1.5)
parser.add_argument("--chorus_mix", type=float, default=0.5)
parser.add_argument("--chorus_delay", type=int, default=10)
parser.add_argument("--chorus_feedback", type=float, default=0)
parser.add_argument("--distortion", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--drive_db", type=int, default=20)
parser.add_argument("--reverb", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--reverb_room_size", type=float, default=0.5)
parser.add_argument("--reverb_damping", type=float, default=0.5)
parser.add_argument("--reverb_wet_level", type=float, default=0.33)
parser.add_argument("--reverb_dry_level", type=float, default=0.67)
parser.add_argument("--reverb_width", type=float, default=1)
parser.add_argument("--reverb_freeze_mode", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--pitchshift", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--pitch_shift", type=int, default=0)
parser.add_argument("--delay", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--delay_seconds", type=float, default=0.5)
parser.add_argument("--delay_feedback", type=float, default=0.5)
parser.add_argument("--delay_mix", type=float, default=0.5)
parser.add_argument("--compressor", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--compressor_threshold", type=int, default=-20)
parser.add_argument("--compressor_ratio", type=float, default=4)
parser.add_argument("--compressor_attack_ms", type=float, default=10)
parser.add_argument("--compressor_release_ms", type=int, default=200)
parser.add_argument("--limiter", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--limiter_threshold", type=int, default=0)
parser.add_argument("--limiter_release", type=int, default=100)
parser.add_argument("--gain", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--gain_db", type=int, default=0)
parser.add_argument("--bitcrush", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--bitcrush_bit_depth", type=int, default=16)
parser.add_argument("--clipping", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--clipping_threshold", type=int, default=-10)
parser.add_argument("--phaser", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--phaser_rate_hz", type=float, default=0.5)
parser.add_argument("--phaser_depth", type=float, default=0.5)
parser.add_argument("--phaser_centre_frequency_hz", type=int, default=1000)
parser.add_argument("--phaser_feedback", type=float, default=0)
parser.add_argument("--phaser_mix", type=float, default=0.5)
parser.add_argument("--treble_bass_boost", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--bass_boost_db", type=int, default=0)
parser.add_argument("--bass_boost_frequency", type=int, default=100)
parser.add_argument("--treble_boost_db", type=int, default=0)
parser.add_argument("--treble_boost_frequency", type=int, default=3000)
parser.add_argument("--fade_in_out", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--fade_in_duration", type=float, default=2000)
parser.add_argument("--fade_out_duration", type=float, default=2000)
parser.add_argument("--export_format", type=str, default="wav")
args = parser.parse_args()
return args
def main():
args = parse_arguments()
process_audio(input_path=args.input_path, output_path=args.output_path, resample=args.resample, resample_sr=args.resample_sr, chorus_depth=args.chorus_depth, chorus_rate=args.chorus_rate, chorus_mix=args.chorus_mix, chorus_delay=args.chorus_delay, chorus_feedback=args.chorus_feedback, distortion_drive=args.drive_db, reverb_room_size=args.reverb_room_size, reverb_damping=args.reverb_damping, reverb_wet_level=args.reverb_wet_level, reverb_dry_level=args.reverb_dry_level, reverb_width=args.reverb_width, reverb_freeze_mode=args.reverb_freeze_mode, pitch_shift=args.pitch_shift, delay_seconds=args.delay_seconds, delay_feedback=args.delay_feedback, delay_mix=args.delay_mix, compressor_threshold=args.compressor_threshold, compressor_ratio=args.compressor_ratio, compressor_attack_ms=args.compressor_attack_ms, compressor_release_ms=args.compressor_release_ms, limiter_threshold=args.limiter_threshold, limiter_release=args.limiter_release, gain_db=args.gain_db, bitcrush_bit_depth=args.bitcrush_bit_depth, clipping_threshold=args.clipping_threshold, phaser_rate_hz=args.phaser_rate_hz, phaser_depth=args.phaser_depth, phaser_centre_frequency_hz=args.phaser_centre_frequency_hz, phaser_feedback=args.phaser_feedback, phaser_mix=args.phaser_mix, bass_boost_db=args.bass_boost_db, bass_boost_frequency=args.bass_boost_frequency, treble_boost_db=args.treble_boost_db, treble_boost_frequency=args.treble_boost_frequency, fade_in_duration=args.fade_in_duration, fade_out_duration=args.fade_out_duration, export_format=args.export_format, chorus=args.chorus, distortion=args.distortion, reverb=args.reverb, pitchshift=args.pitchshift, delay=args.delay, compressor=args.compressor, limiter=args.limiter, gain=args.gain, bitcrush=args.bitcrush, clipping=args.clipping, phaser=args.phaser, treble_bass_boost=args.treble_bass_boost, fade_in_out=args.fade_in_out)
def process_audio(input_path, output_path, resample, resample_sr, chorus_depth, chorus_rate, chorus_mix, chorus_delay, chorus_feedback, distortion_drive, reverb_room_size, reverb_damping, reverb_wet_level, reverb_dry_level, reverb_width, reverb_freeze_mode, pitch_shift, delay_seconds, delay_feedback, delay_mix, compressor_threshold, compressor_ratio, compressor_attack_ms, compressor_release_ms, limiter_threshold, limiter_release, gain_db, bitcrush_bit_depth, clipping_threshold, phaser_rate_hz, phaser_depth, phaser_centre_frequency_hz, phaser_feedback, phaser_mix, bass_boost_db, bass_boost_frequency, treble_boost_db, treble_boost_frequency, fade_in_duration, fade_out_duration, export_format, chorus, distortion, reverb, pitchshift, delay, compressor, limiter, gain, bitcrush, clipping, phaser, treble_bass_boost, fade_in_out):
def bass_boost(audio, gain_db, frequency, sample_rate):
if gain_db >= 1:
b, a = butter(4, frequency / (0.5 * sample_rate), btype='low')
return filtfilt(b, a, audio) * 10 ** (gain_db / 20)
else: return audio
def treble_boost(audio, gain_db, frequency, sample_rate):
if gain_db >=1:
b, a = butter(4, frequency / (0.5 * sample_rate), btype='high')
return filtfilt(b, a, audio) * 10 ** (gain_db / 20)
else: return audio
def fade_out_effect(audio, sr, duration=3.0):
length = int(duration * sr)
end = audio.shape[0]
if length > end: length = end
start = end - length
audio[start:end] = audio[start:end] * np.linspace(1.0, 0.0, length)
return audio
def fade_in_effect(audio, sr, duration=3.0):
length = int(duration * sr)
start = 0
if length > audio.shape[0]: length = audio.shape[0]
end = length
audio[start:end] = audio[start:end] * np.linspace(0.0, 1.0, length)
return audio
if not input_path or not os.path.exists(input_path):
print(translations["input_not_valid"])
sys.exit(1)
if not output_path:
print(translations["output_not_valid"])
sys.exit(1)
if os.path.exists(output_path): os.remove(output_path)
try:
audio, sample_rate = sf.read(input_path)
except Exception as e:
raise RuntimeError(translations["errors_loading_audio"].format(e=e))
try:
board = Pedalboard()
if chorus: board.append(Chorus(depth=chorus_depth, rate_hz=chorus_rate, mix=chorus_mix, centre_delay_ms=chorus_delay, feedback=chorus_feedback))
if distortion: board.append(Distortion(drive_db=distortion_drive))
if reverb: board.append(Reverb(room_size=reverb_room_size, damping=reverb_damping, wet_level=reverb_wet_level, dry_level=reverb_dry_level, width=reverb_width, freeze_mode=1 if reverb_freeze_mode else 0))
if pitchshift: board.append(PitchShift(semitones=pitch_shift))
if delay: board.append(Delay(delay_seconds=delay_seconds, feedback=delay_feedback, mix=delay_mix))
if compressor: board.append(Compressor(threshold_db=compressor_threshold, ratio=compressor_ratio, attack_ms=compressor_attack_ms, release_ms=compressor_release_ms))
if limiter: board.append(Limiter(threshold_db=limiter_threshold, release_ms=limiter_release))
if gain: board.append(Gain(gain_db=gain_db))
if bitcrush: board.append(Bitcrush(bit_depth=bitcrush_bit_depth))
if clipping: board.append(Clipping(threshold_db=clipping_threshold))
if phaser: board.append(Phaser(rate_hz=phaser_rate_hz, depth=phaser_depth, centre_frequency_hz=phaser_centre_frequency_hz, feedback=phaser_feedback, mix=phaser_mix))
processed_audio = board(audio, sample_rate)
if treble_bass_boost:
processed_audio = bass_boost(processed_audio, bass_boost_db, bass_boost_frequency, sample_rate)
processed_audio = treble_boost(processed_audio, treble_boost_db, treble_boost_frequency, sample_rate)
if fade_in_out:
processed_audio = fade_in_effect(processed_audio, sample_rate, fade_in_duration)
processed_audio = fade_out_effect(processed_audio, sample_rate, fade_out_duration)
if resample_sr != sample_rate and resample_sr > 0 and resample:
processed_audio = librosa.resample(processed_audio, orig_sr=sample_rate, target_sr=resample_sr)
sample_rate = resample_sr
except Exception as e:
raise RuntimeError(translations["apply_error"].format(e=e))
sf.write(output_path.replace(".wav", f".{export_format}"), processed_audio, sample_rate, format=export_format)
return output_path
if __name__ == "__main__": main()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060import gc
import re
import os
import sys
import time
import torch
import faiss
import shutil
import codecs
import pyworld
import librosa
import logging
import argparse
import warnings
import traceback
import torchcrepe
import subprocess
import parselmouth
import logging.handlers
import numpy as np
import soundfile as sf
import noisereduce as nr
import torch.nn.functional as F
import torch.multiprocessing as mp
from tqdm import tqdm
from scipy import signal
from torch import Tensor
from scipy.io import wavfile
from audio_upscaler import upscale
from distutils.util import strtobool
from fairseq import checkpoint_utils
from pydub import AudioSegment, silence
now_dir = os.getcwd()
sys.path.append(now_dir)
from main.configs.config import Config
from main.library.predictors.FCPE import FCPE
from main.library.predictors.RMVPE import RMVPE
from main.library.algorithm.synthesizers import Synthesizer
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=UserWarning)
logging.getLogger("wget").setLevel(logging.ERROR)
logging.getLogger("torch").setLevel(logging.ERROR)
logging.getLogger("faiss").setLevel(logging.ERROR)
logging.getLogger("httpx").setLevel(logging.ERROR)
logging.getLogger("fairseq").setLevel(logging.ERROR)
logging.getLogger("httpcore").setLevel(logging.ERROR)
logging.getLogger("faiss.loader").setLevel(logging.ERROR)
FILTER_ORDER = 5
CUTOFF_FREQUENCY = 48
SAMPLE_RATE = 16000
bh, ah = signal.butter(N=FILTER_ORDER, Wn=CUTOFF_FREQUENCY, btype="high", fs=SAMPLE_RATE)
input_audio_path2wav = {}
log_file = os.path.join("assets", "logs", "convert.log")
logger = logging.getLogger(__name__)
logger.propagate = False
translations = Config().translations
if logger.hasHandlers(): logger.handlers.clear()
else:
console_handler = logging.StreamHandler()
console_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
console_handler.setFormatter(console_formatter)
console_handler.setLevel(logging.INFO)
file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=5*1024*1024, backupCount=3, encoding='utf-8')
file_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
file_handler.setFormatter(file_formatter)
file_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler)
logger.addHandler(file_handler)
logger.setLevel(logging.DEBUG)
def parse_arguments() -> tuple:
parser = argparse.ArgumentParser()
parser.add_argument("--pitch", type=int, default=0)
parser.add_argument("--filter_radius", type=int, default=3)
parser.add_argument("--index_rate", type=float, default=0.5)
parser.add_argument("--volume_envelope", type=float, default=1)
parser.add_argument("--protect", type=float, default=0.33)
parser.add_argument("--hop_length", type=int, default=64)
parser.add_argument( "--f0_method", type=str, default="rmvpe")
parser.add_argument("--input_path", type=str, required=True)
parser.add_argument("--output_path", type=str, default="./audios/output.wav")
parser.add_argument("--pth_path", type=str, required=True)
parser.add_argument("--index_path", type=str, required=True)
parser.add_argument("--f0_autotune", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--f0_autotune_strength", type=float, default=1)
parser.add_argument("--clean_audio", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--clean_strength", type=float, default=0.7)
parser.add_argument("--export_format", type=str, default="wav")
parser.add_argument("--embedder_model", type=str, default="contentvec_base")
parser.add_argument("--upscale_audio", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--resample_sr", type=int, default=0)
parser.add_argument("--batch_process", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--batch_size", type=int, default=2)
parser.add_argument("--split_audio", type=lambda x: bool(strtobool(x)), default=False)
args = parser.parse_args()
return args
def main():
args = parse_arguments()
pitch = args.pitch
filter_radius = args.filter_radius
index_rate = args.index_rate
volume_envelope = args.volume_envelope
protect = args.protect
hop_length = args.hop_length
f0_method = args.f0_method
input_path = args.input_path
output_path = args.output_path
pth_path = args.pth_path
index_path = args.index_path
f0_autotune = args.f0_autotune
f0_autotune_strength = args.f0_autotune_strength
clean_audio = args.clean_audio
clean_strength = args.clean_strength
export_format = args.export_format
embedder_model = args.embedder_model
upscale_audio = args.upscale_audio
resample_sr = args.resample_sr
batch_process = args.batch_process
batch_size = args.batch_size
split_audio = args.split_audio
logger.debug(f"{translations['pitch']}: {pitch}")
logger.debug(f"{translations['filter_radius']}: {filter_radius}")
logger.debug(f"{translations['index_strength']} {index_rate}")
logger.debug(f"{translations['volume_envelope']}: {volume_envelope}")
logger.debug(f"{translations['protect']}: {protect}")
if f0_method == "crepe" or f0_method == "crepe-tiny": logger.debug(f"Hop length: {hop_length}")
logger.debug(f"{translations['f0_method']}: {f0_method}")
logger.debug(f"f0_method: {input_path}")
logger.debug(f"{translations['audio_path']}: {input_path}")
logger.debug(f"{translations['output_path']}: {output_path.replace('.wav', f'.{export_format}')}")
logger.debug(f"{translations['model_path']}: {pth_path}")
logger.debug(f"{translations['indexpath']}: {index_path}")
logger.debug(f"{translations['autotune']}: {f0_autotune}")
logger.debug(f"{translations['clear_audio']}: {clean_audio}")
if clean_audio: logger.debug(f"{translations['clean_strength']}: {clean_strength}")
logger.debug(f"{translations['export_format']}: {export_format}")
logger.debug(f"{translations['hubert_model']}: {embedder_model}")
logger.debug(f"{translations['upscale_audio']}: {upscale_audio}")
if resample_sr != 0: logger.debug(f"{translations['sample_rate']}: {resample_sr}")
if split_audio: logger.debug(f"{translations['batch_process']}: {batch_process}")
if batch_process and split_audio: logger.debug(f"{translations['batch_size']}: {batch_size}")
logger.debug(f"{translations['split_audio']}: {split_audio}")
if f0_autotune: logger.debug(f"{translations['autotune_rate_info']}: {f0_autotune_strength}")
check_rmvpe_fcpe(f0_method)
check_hubert(embedder_model)
run_convert_script(pitch=pitch, filter_radius=filter_radius, index_rate=index_rate, volume_envelope=volume_envelope, protect=protect, hop_length=hop_length, f0_method=f0_method, input_path=input_path, output_path=output_path, pth_path=pth_path, index_path=index_path, f0_autotune=f0_autotune, f0_autotune_strength=f0_autotune_strength, clean_audio=clean_audio, clean_strength=clean_strength, export_format=export_format, embedder_model=embedder_model, upscale_audio=upscale_audio, resample_sr=resample_sr, batch_process=batch_process, batch_size=batch_size, split_audio=split_audio)
def check_rmvpe_fcpe(method):
def download_rmvpe():
if not os.path.exists(os.path.join("assets", "model", "predictors", "rmvpe.pt")): subprocess.run(["wget", "-q", "--show-progress", "--no-check-certificate", codecs.decode("uggcf://uhttvatsnpr.pb/NauC/Pbyno_EIP_Cebwrpg_2/erfbyir/znva/", "rot13") + "rmvpe.pt", "-P", os.path.join("assets", "model", "predictors")], check=True)
def download_fcpe():
if not os.path.exists(os.path.join("assets", "model", "predictors", "fcpe.pt")): subprocess.run(["wget", "-q", "--show-progress", "--no-check-certificate", codecs.decode("uggcf://uhttvatsnpr.pb/NauC/Pbyno_EIP_Cebwrpg_2/erfbyir/znva/", "rot13") + "fcpe.pt", "-P", os.path.join("assets", "model", "predictors")], check=True)
if method == "rmvpe": download_rmvpe()
elif method == "fcpe": download_fcpe()
elif "hybrid" in method:
methods_str = re.search("hybrid\[(.+)\]", method)
if methods_str: methods = [method.strip() for method in methods_str.group(1).split("+")]
for method in methods:
if method == "rmvpe": download_rmvpe()
elif method == "fcpe": download_fcpe()
def check_hubert(hubert):
if hubert == "contentvec_base" or hubert == "hubert_base" or hubert == "japanese_hubert_base" or hubert == "korean_hubert_base" or hubert == "chinese_hubert_base":
model_path = os.path.join(now_dir, "assets", "model", "embedders", hubert + '.pt')
if not os.path.exists(model_path): subprocess.run(["wget", "-q", "--show-progress", "--no-check-certificate", codecs.decode("uggcf://uhttvatsnpr.pb/NauC/Pbyno_EIP_Cebwrpg_2/erfbyir/znva/", "rot13") + f"{hubert}.pt", "-P", os.path.join("assets", "model", "embedders")], check=True)
def load_audio_infer(file, sample_rate):
try:
file = file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
if not os.path.isfile(file): raise FileNotFoundError(translations["not_found"].format(name=file))
audio, sr = sf.read(file)
if len(audio.shape) > 1: audio = librosa.to_mono(audio.T)
if sr != sample_rate: audio = librosa.resample(audio, orig_sr=sr, target_sr=sample_rate)
except Exception as e:
raise RuntimeError(f"{translations['errors_loading_audio']}: {e}")
return audio.flatten()
def process_audio(file_path, output_path):
try:
song = AudioSegment.from_file(file_path)
nonsilent_parts = silence.detect_nonsilent(song, min_silence_len=750, silence_thresh=-70)
cut_files = []
time_stamps = []
min_chunk_duration = 30
for i, (start_i, end_i) in enumerate(nonsilent_parts):
chunk = song[start_i:end_i]
if len(chunk) >= min_chunk_duration:
chunk_file_path = os.path.join(output_path, f"chunk{i}.wav")
if os.path.exists(chunk_file_path): os.remove(chunk_file_path)
chunk.export(chunk_file_path, format="wav")
cut_files.append(chunk_file_path)
time_stamps.append((start_i, end_i))
else: logger.debug(translations["skip_file"].format(i=i, chunk=len(chunk)))
logger.info(f"{translations['split_total']}: {len(cut_files)}")
return cut_files, time_stamps
except Exception as e:
raise RuntimeError(f"{translations['process_audio_error']}: {e}")
def merge_audio(files_list, time_stamps, original_file_path, output_path, format):
try:
def extract_number(filename):
match = re.search(r'_(\d+)', filename)
return int(match.group(1)) if match else 0
files_list = sorted(files_list, key=extract_number)
total_duration = len(AudioSegment.from_file(original_file_path))
combined = AudioSegment.empty()
current_position = 0
for file, (start_i, end_i) in zip(files_list, time_stamps):
if start_i > current_position:
silence_duration = start_i - current_position
combined += AudioSegment.silent(duration=silence_duration)
combined += AudioSegment.from_file(file)
current_position = end_i
if current_position < total_duration: combined += AudioSegment.silent(duration=total_duration - current_position)
combined.export(output_path, format=format)
return output_path
except Exception as e:
raise RuntimeError(f"{translations['merge_error']}: {e}")
def run_batch_convert(params):
cvt = VoiceConverter()
path = params["path"]
audio_temp = params["audio_temp"]
export_format = params["export_format"]
cut_files = params["cut_files"]
pitch = params["pitch"]
filter_radius = params["filter_radius"]
index_rate = params["index_rate"]
volume_envelope = params["volume_envelope"]
protect = params["protect"]
hop_length = params["hop_length"]
f0_method = params["f0_method"]
pth_path = params["pth_path"]
index_path = params["index_path"]
f0_autotune = params["f0_autotune"]
f0_autotune_strength = params["f0_autotune_strength"]
clean_audio = params["clean_audio"]
clean_strength = params["clean_strength"]
upscale_audio = params["upscale_audio"]
embedder_model = params["embedder_model"]
resample_sr = params["resample_sr"]
segment_output_path = os.path.join(audio_temp, f"output_{cut_files.index(path)}.{export_format}")
if os.path.exists(segment_output_path): os.remove(segment_output_path)
cvt.convert_audio(pitch=pitch, filter_radius=filter_radius, index_rate=index_rate, volume_envelope=volume_envelope, protect=protect, hop_length=hop_length, f0_method=f0_method, audio_input_path=path, audio_output_path=segment_output_path, model_path=pth_path, index_path=index_path, f0_autotune=f0_autotune, f0_autotune_strength=f0_autotune_strength, clean_audio=clean_audio, clean_strength=clean_strength, export_format=export_format, upscale_audio=upscale_audio, embedder_model=embedder_model, resample_sr=resample_sr)
os.remove(path)
if os.path.exists(segment_output_path): return segment_output_path
else:
logger.warning(f"{translations['not_found_convert_file']}: {segment_output_path}")
sys.exit(1)
def run_convert_script(pitch, filter_radius, index_rate, volume_envelope, protect, hop_length, f0_method, input_path, output_path, pth_path, index_path, f0_autotune, f0_autotune_strength, clean_audio, clean_strength, export_format, upscale_audio, embedder_model, resample_sr, batch_process, batch_size, split_audio):
cvt = VoiceConverter()
start_time = time.time()
if not pth_path or not os.path.exists(pth_path) or os.path.isdir(pth_path) or not pth_path.endswith(".pth"):
logger.warning(translations["provide_file"].format(filename=translations["model"]))
sys.exit(1)
if not index_path or not os.path.exists(index_path) or os.path.isdir(index_path) or not index_path.endswith(".index"):
logger.warning(translations["provide_file"].format(filename=translations["index"]))
sys.exit(1)
output_dir = os.path.dirname(output_path)
output_dir = output_path if not output_dir else output_dir
if output_dir is None: output_dir = "audios"
if not os.path.exists(output_dir): os.makedirs(output_dir, exist_ok=True)
audio_temp = os.path.join("audios_temp")
if not os.path.exists(audio_temp) and split_audio: os.makedirs(audio_temp, exist_ok=True)
processed_segments = []
if os.path.isdir(input_path):
try:
logger.info(translations["convert_batch"])
audio_files = [f for f in os.listdir(input_path) if f.endswith(("wav", "mp3", "flac", "ogg", "opus", "m4a", "mp4", "aac", "alac", "wma", "aiff", "webm", "ac3"))]
if not audio_files:
logger.warning(translations["not_found_audio"])
sys.exit(1)
logger.info(translations["found_audio"].format(audio_files=len(audio_files)))
for audio in audio_files:
audio_path = os.path.join(input_path, audio)
output_audio = os.path.join(input_path, os.path.splitext(audio)[0] + f"_output.{export_format}")
if split_audio:
try:
cut_files, time_stamps = process_audio(audio_path, audio_temp)
num_threads = min(batch_size, len(cut_files))
params_list = [
{
"path": path,
"audio_temp": audio_temp,
"export_format": export_format,
"cut_files": cut_files,
"pitch": pitch,
"filter_radius": filter_radius,
"index_rate": index_rate,
"volume_envelope": volume_envelope,
"protect": protect,
"hop_length": hop_length,
"f0_method": f0_method,
"pth_path": pth_path,
"index_path": index_path,
"f0_autotune": f0_autotune,
"f0_autotune_strength": f0_autotune_strength,
"clean_audio": clean_audio,
"clean_strength": clean_strength,
"upscale_audio": upscale_audio,
"embedder_model": embedder_model,
"resample_sr": resample_sr
}
for path in cut_files
]
if batch_process:
with mp.Pool(processes=num_threads) as pool:
with tqdm(total=len(params_list), desc=translations["convert_audio"]) as pbar:
for results in pool.imap_unordered(run_batch_convert, params_list):
processed_segments.append(results)
pbar.update(1)
else:
for params in tqdm(params_list, desc=translations["convert_audio"]):
run_batch_convert(params)
merge_audio(processed_segments, time_stamps, audio_path, output_audio, export_format)
except Exception as e:
logger.error(translations["error_convert_batch"].format(e=e))
finally:
if os.path.exists(audio_temp): shutil.rmtree(audio_temp, ignore_errors=True)
else:
try:
logger.info(f"{translations['convert_audio']} '{audio_path}'...")
if os.path.exists(output_audio): os.remove(output_audio)
with tqdm(total=1, desc=translations["convert_audio"]) as pbar:
cvt.convert_audio(pitch=pitch, filter_radius=filter_radius, index_rate=index_rate, volume_envelope=volume_envelope, protect=protect, hop_length=hop_length, f0_method=f0_method, audio_input_path=audio_path, audio_output_path=output_audio, model_path=pth_path, index_path=index_path, f0_autotune=f0_autotune, f0_autotune_strength=f0_autotune_strength, clean_audio=clean_audio, clean_strength=clean_strength, export_format=export_format, upscale_audio=upscale_audio, embedder_model=embedder_model, resample_sr=resample_sr)
pbar.update(1)
except Exception as e:
logger.error(translations["error_convert"].format(e=e))
elapsed_time = time.time() - start_time
logger.info(translations["convert_batch_success"].format(elapsed_time=f"{elapsed_time:.2f}", output_path=output_path.replace('.wav', f'.{export_format}')))
except Exception as e:
logger.error(translations["error_convert_batch_2"].format(e=e))
else:
logger.info(f"{translations['convert_audio']} '{input_path}'...")
if not os.path.exists(input_path):
logger.warning(translations["not_found_audio"])
sys.exit(1)
if os.path.isdir(output_path): output_path = os.path.join(output_path, f"output.{export_format}")
if os.path.exists(output_path): os.remove(output_path)
if split_audio:
try:
cut_files, time_stamps = process_audio(input_path, audio_temp)
num_threads = min(batch_size, len(cut_files))
params_list = [
{
"path": path,
"audio_temp": audio_temp,
"export_format": export_format,
"cut_files": cut_files,
"pitch": pitch,
"filter_radius": filter_radius,
"index_rate": index_rate,
"volume_envelope": volume_envelope,
"protect": protect,
"hop_length": hop_length,
"f0_method": f0_method,
"pth_path": pth_path,
"index_path": index_path,
"f0_autotune": f0_autotune,
"f0_autotune_strength": f0_autotune_strength,
"clean_audio": clean_audio,
"clean_strength": clean_strength,
"upscale_audio": upscale_audio,
"embedder_model": embedder_model,
"resample_sr": resample_sr
}
for path in cut_files
]
if batch_process:
with mp.Pool(processes=num_threads) as pool:
with tqdm(total=len(params_list), desc=translations["convert_audio"]) as pbar:
for results in pool.imap_unordered(run_batch_convert, params_list):
processed_segments.append(results)
pbar.update(1)
else:
for params in tqdm(params_list, desc=translations["convert_audio"]):
run_batch_convert(params)
merge_audio(processed_segments, time_stamps, input_path, output_path.replace(".wav", f".{export_format}"), export_format)
except Exception as e:
logger.error(translations["error_convert_batch"].format(e=e))
finally:
if os.path.exists(audio_temp): shutil.rmtree(audio_temp, ignore_errors=True)
else:
try:
with tqdm(total=1, desc=translations["convert_audio"]) as pbar:
cvt.convert_audio(pitch=pitch, filter_radius=filter_radius, index_rate=index_rate, volume_envelope=volume_envelope, protect=protect, hop_length=hop_length, f0_method=f0_method, audio_input_path=input_path, audio_output_path=output_path, model_path=pth_path, index_path=index_path, f0_autotune=f0_autotune, f0_autotune_strength=f0_autotune_strength, clean_audio=clean_audio, clean_strength=clean_strength, export_format=export_format, upscale_audio=upscale_audio, embedder_model=embedder_model, resample_sr=resample_sr)
pbar.update(1)
except Exception as e:
logger.error(translations["error_convert"].format(e=e))
elapsed_time = time.time() - start_time
logger.info(translations["convert_audio_success"].format(input_path=input_path, elapsed_time=f"{elapsed_time:.2f}", output_path=output_path.replace('.wav', f'.{export_format}')))
def change_rms(source_audio: np.ndarray, source_rate: int, target_audio: np.ndarray, target_rate: int, rate: float) -> np.ndarray:
rms1 = librosa.feature.rms(
y=source_audio,
frame_length=source_rate // 2 * 2,
hop_length=source_rate // 2,
)
rms2 = librosa.feature.rms(
y=target_audio,
frame_length=target_rate // 2 * 2,
hop_length=target_rate // 2,
)
rms1 = F.interpolate(
torch.from_numpy(rms1).float().unsqueeze(0),
size=target_audio.shape[0],
mode="linear",
).squeeze()
rms2 = F.interpolate(
torch.from_numpy(rms2).float().unsqueeze(0),
size=target_audio.shape[0],
mode="linear",
).squeeze()
rms2 = torch.maximum(rms2, torch.zeros_like(rms2) + 1e-6)
adjusted_audio = (target_audio * (torch.pow(rms1, 1 - rate) * torch.pow(rms2, rate - 1)).numpy())
return adjusted_audio
class Autotune:
def __init__(self, ref_freqs):
self.ref_freqs = ref_freqs
self.note_dict = self.ref_freqs
def autotune_f0(self, f0, f0_autotune_strength):
autotuned_f0 = np.zeros_like(f0)
for i, freq in enumerate(f0):
closest_note = min(self.note_dict, key=lambda x: abs(x - freq))
autotuned_f0[i] = freq + (closest_note - freq) * f0_autotune_strength
return autotuned_f0
class VC:
def __init__(self, tgt_sr, config):
self.x_pad = config.x_pad
self.x_query = config.x_query
self.x_center = config.x_center
self.x_max = config.x_max
self.is_half = config.is_half
self.sample_rate = 16000
self.window = 160
self.t_pad = self.sample_rate * self.x_pad
self.t_pad_tgt = tgt_sr * self.x_pad
self.t_pad2 = self.t_pad * 2
self.t_query = self.sample_rate * self.x_query
self.t_center = self.sample_rate * self.x_center
self.t_max = self.sample_rate * self.x_max
self.time_step = self.window / self.sample_rate * 1000
self.f0_min = 50
self.f0_max = 1100
self.f0_mel_min = 1127 * np.log(1 + self.f0_min / 700)
self.f0_mel_max = 1127 * np.log(1 + self.f0_max / 700)
self.device = config.device
self.ref_freqs = [
49.00,
51.91,
55.00,
58.27,
61.74,
65.41,
69.30,
73.42,
77.78,
82.41,
87.31,
92.50,
98.00,
103.83,
110.00,
116.54,
123.47,
130.81,
138.59,
146.83,
155.56,
164.81,
174.61,
185.00,
196.00,
207.65,
220.00,
233.08,
246.94,
261.63,
277.18,
293.66,
311.13,
329.63,
349.23,
369.99,
392.00,
415.30,
440.00,
466.16,
493.88,
523.25,
554.37,
587.33,
622.25,
659.25,
698.46,
739.99,
783.99,
830.61,
880.00,
932.33,
987.77,
1046.50
]
self.autotune = Autotune(self.ref_freqs)
self.note_dict = self.autotune.note_dict
def get_f0_crepe(self, x, f0_min, f0_max, p_len, hop_length, model="full"):
x = x.astype(np.float32)
x /= np.quantile(np.abs(x), 0.999)
audio = torch.from_numpy(x).to(self.device, copy=True)
audio = torch.unsqueeze(audio, dim=0)
if audio.ndim == 2 and audio.shape[0] > 1: audio = torch.mean(audio, dim=0, keepdim=True).detach()
audio = audio.detach()
pitch: Tensor = torchcrepe.predict(audio, self.sample_rate, hop_length, f0_min, f0_max, model, batch_size=hop_length * 2, device=self.device, pad=True)
p_len = p_len or x.shape[0] // hop_length
source = np.array(pitch.squeeze(0).cpu().float().numpy())
source[source < 0.001] = np.nan
target = np.interp(
np.arange(0, len(source) * p_len, len(source)) / p_len,
np.arange(0, len(source)),
source,
)
f0 = np.nan_to_num(target)
return f0
def get_f0_hybrid(self, methods_str, x, f0_min, f0_max, p_len, hop_length, filter_radius):
methods_str = re.search("hybrid\[(.+)\]", methods_str)
if methods_str: methods = [method.strip() for method in methods_str.group(1).split("+")]
f0_computation_stack = []
logger.debug(translations["hybrid_methods"].format(methods=methods))
x = x.astype(np.float32)
x /= np.quantile(np.abs(x), 0.999)
for method in methods:
f0 = None
if method == "pm":
f0 = (parselmouth.Sound(x, self.sample_rate).to_pitch_ac(time_step=self.window / self.sample_rate * 1000 / 1000, voicing_threshold=0.6, pitch_floor=self.f0_min, pitch_ceiling=self.f0_max).selected_array["frequency"])
pad_size = (p_len - len(f0) + 1) // 2
if pad_size > 0 or p_len - len(f0) - pad_size > 0: f0 = np.pad(f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant")
elif method == 'dio':
f0, t = pyworld.dio(x.astype(np.double), fs=self.sample_rate, f0_ceil=self.f0_max, f0_floor=self.f0_min, frame_period=10)
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.sample_rate)
f0 = signal.medfilt(f0, 3)
elif method == "crepe-tiny":
f0 = self.get_f0_crepe(x, self.f0_min, self.f0_max, p_len, int(hop_length), "tiny")
elif method == "crepe":
f0 = self.get_f0_crepe(x, f0_min, f0_max, p_len, int(hop_length))
elif method == "fcpe":
self.model_fcpe = FCPE(os.path.join("assets", "model", "predictors", "fcpe.pt"), hop_length=int(hop_length), f0_min=int(f0_min), f0_max=int(f0_max), dtype=torch.float32, device=self.device, sample_rate=self.sample_rate, threshold=0.03)
f0 = self.model_fcpe.compute_f0(x, p_len=p_len)
del self.model_fcpe
gc.collect()
elif method == "rmvpe":
f0 = RMVPE(os.path.join("assets", "model", "predictors", "rmvpe.pt"), is_half=self.is_half, device=self.device).infer_from_audio(x, thred=0.03)
f0 = f0[1:]
elif method == "harvest":
f0, t = pyworld.harvest(x.astype(np.double), fs=self.sample_rate, f0_ceil=self.f0_max, f0_floor=self.f0_min, frame_period=10)
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.sample_rate)
if filter_radius > 2: f0 = signal.medfilt(f0, 3)
else: raise ValueError(translations["method_not_valid"])
f0_computation_stack.append(f0)
resampled_stack = []
for f0 in f0_computation_stack:
resampled_f0 = np.interp(np.linspace(0, len(f0), p_len), np.arange(len(f0)), f0)
resampled_stack.append(resampled_f0)
f0_median_hybrid = resampled_stack[0] if len(resampled_stack) == 1 else np.nanmedian(np.vstack(resampled_stack), axis=0)
return f0_median_hybrid
def get_f0(self, input_audio_path, x, p_len, pitch, f0_method, filter_radius, hop_length, f0_autotune, f0_autotune_strength):
global input_audio_path2wav
if f0_method == "pm":
f0 = (parselmouth.Sound(x, self.sample_rate).to_pitch_ac(time_step=self.window / self.sample_rate * 1000 / 1000, voicing_threshold=0.6, pitch_floor=self.f0_min, pitch_ceiling=self.f0_max).selected_array["frequency"])
pad_size = (p_len - len(f0) + 1) // 2
if pad_size > 0 or p_len - len(f0) - pad_size > 0: f0 = np.pad(f0, [[pad_size, p_len - len(f0) - pad_size]], mode="constant")
elif f0_method == "dio":
f0, t = pyworld.dio(x.astype(np.double), fs=self.sample_rate, f0_ceil=self.f0_max, f0_floor=self.f0_min, frame_period=10)
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.sample_rate)
f0 = signal.medfilt(f0, 3)
elif f0_method == "crepe-tiny":
f0 = self.get_f0_crepe(x, self.f0_min, self.f0_max, p_len, int(hop_length), "tiny")
elif f0_method == "crepe":
f0 = self.get_f0_crepe(x, self.f0_min, self.f0_max, p_len, int(hop_length))
elif f0_method == "fcpe":
self.model_fcpe = FCPE(os.path.join("assets", "model", "predictors", "fcpe.pt"), hop_length=int(hop_length), f0_min=int(self.f0_min), f0_max=int(self.f0_max), dtype=torch.float32, device=self.device, sample_rate=self.sample_rate, threshold=0.03)
f0 = self.model_fcpe.compute_f0(x, p_len=p_len)
del self.model_fcpe
gc.collect()
elif f0_method == "rmvpe":
f0 = RMVPE(os.path.join("assets", "model", "predictors", "rmvpe.pt"), is_half=self.is_half, device=self.device).infer_from_audio(x, thred=0.03)
elif f0_method == "harvest":
f0, t = pyworld.harvest(x.astype(np.double), fs=self.sample_rate, f0_ceil=self.f0_max, f0_floor=self.f0_min, frame_period=10)
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.sample_rate)
if filter_radius > 2: f0 = signal.medfilt(f0, 3)
elif "hybrid" in f0_method:
input_audio_path2wav[input_audio_path] = x.astype(np.double)
f0 = self.get_f0_hybrid(f0_method, x, self.f0_min, self.f0_max, p_len, hop_length, filter_radius)
else: raise ValueError(translations["method_not_valid"])
if f0_autotune: f0 = Autotune.autotune_f0(self, f0, f0_autotune_strength)
f0 *= pow(2, pitch / 12)
f0bak = f0.copy()
f0_mel = 1127 * np.log(1 + f0 / 700)
f0_mel[f0_mel > 0] = (f0_mel[f0_mel > 0] - self.f0_mel_min) * 254 / (self.f0_mel_max - self.f0_mel_min) + 1
f0_mel[f0_mel <= 1] = 1
f0_mel[f0_mel > 255] = 255
f0_coarse = np.rint(f0_mel).astype(np.int32)
return f0_coarse, f0bak
def voice_conversion(self, model, net_g, sid, audio0, pitch, pitchf, index, big_npy, index_rate, version, protect):
pitch_guidance = pitch != None and pitchf != None
feats = (torch.from_numpy(audio0).half() if self.is_half else torch.from_numpy(audio0).float())
if feats.dim() == 2: feats = feats.mean(-1)
assert feats.dim() == 1, feats.dim()
feats = feats.view(1, -1)
padding_mask = torch.BoolTensor(feats.shape).to(self.device).fill_(False)
inputs = {
"source": feats.to(self.device),
"padding_mask": padding_mask,
"output_layer": 9 if version == "v1" else 12,
}
with torch.no_grad():
logits = model.extract_features(**inputs)
feats = model.final_proj(logits[0]) if version == "v1" else logits[0]
if protect < 0.5 and pitch_guidance: feats0 = feats.clone()
if (not isinstance(index, type(None)) and not isinstance(big_npy, type(None)) and index_rate != 0):
npy = feats[0].cpu().numpy()
if self.is_half: npy = npy.astype("float32")
score, ix = index.search(npy, k=8)
weight = np.square(1 / score)
weight /= weight.sum(axis=1, keepdims=True)
npy = np.sum(big_npy[ix] * np.expand_dims(weight, axis=2), axis=1)
if self.is_half: npy = npy.astype("float16")
feats = (torch.from_numpy(npy).unsqueeze(0).to(self.device) * index_rate + (1 - index_rate) * feats)
feats = F.interpolate(feats.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
if protect < 0.5 and pitch_guidance: feats0 = F.interpolate(feats0.permute(0, 2, 1), scale_factor=2).permute(0, 2, 1)
p_len = audio0.shape[0] // self.window
if feats.shape[1] < p_len:
p_len = feats.shape[1]
if pitch_guidance:
pitch = pitch[:, :p_len]
pitchf = pitchf[:, :p_len]
if protect < 0.5 and pitch_guidance:
pitchff = pitchf.clone()
pitchff[pitchf > 0] = 1
pitchff[pitchf < 1] = protect
pitchff = pitchff.unsqueeze(-1)
feats = feats * pitchff + feats0 * (1 - pitchff)
feats = feats.to(feats0.dtype)
p_len = torch.tensor([p_len], device=self.device).long()
with torch.no_grad():
audio1 = ((net_g.infer(feats, p_len, pitch, pitchf, sid)[0][0, 0]).data.cpu().float().numpy()) if pitch_guidance else ((net_g.infer(feats, p_len, sid)[0][0, 0]).data.cpu().float().numpy())
del feats, p_len, padding_mask
if torch.cuda.is_available(): torch.cuda.empty_cache()
return audio1
def pipeline(self, model, net_g, sid, audio, input_audio_path, pitch, f0_method, file_index, index_rate, pitch_guidance, filter_radius, tgt_sr, resample_sr, volume_envelope, version, protect, hop_length, f0_autotune, f0_autotune_strength):
if file_index != "" and os.path.exists(file_index) and index_rate != 0:
try:
index = faiss.read_index(file_index)
big_npy = index.reconstruct_n(0, index.ntotal)
except Exception as e:
logger.error(translations["read_faiss_index_error"].format(e=e))
index = big_npy = None
else: index = big_npy = None
audio = signal.filtfilt(bh, ah, audio)
audio_pad = np.pad(audio, (self.window // 2, self.window // 2), mode="reflect")
opt_ts = []
if audio_pad.shape[0] > self.t_max:
audio_sum = np.zeros_like(audio)
for i in range(self.window):
audio_sum += audio_pad[i : i - self.window]
for t in range(self.t_center, audio.shape[0], self.t_center):
opt_ts.append(t - self.t_query + np.where(np.abs(audio_sum[t - self.t_query : t + self.t_query]) == np.abs(audio_sum[t - self.t_query : t + self.t_query]).min())[0][0])
s = 0
audio_opt = []
t = None
audio_pad = np.pad(audio, (self.t_pad, self.t_pad), mode="reflect")
p_len = audio_pad.shape[0] // self.window
sid = torch.tensor(sid, device=self.device).unsqueeze(0).long()
if pitch_guidance:
pitch, pitchf = self.get_f0(input_audio_path, audio_pad, p_len, pitch, f0_method, filter_radius, hop_length, f0_autotune, f0_autotune_strength)
pitch = pitch[:p_len]
pitchf = pitchf[:p_len]
if self.device == "mps": pitchf = pitchf.astype(np.float32)
pitch = torch.tensor(pitch, device=self.device).unsqueeze(0).long()
pitchf = torch.tensor(pitchf, device=self.device).unsqueeze(0).float()
for t in opt_ts:
t = t // self.window * self.window
if pitch_guidance: audio_opt.append(self.voice_conversion(model, net_g, sid, audio_pad[s : t + self.t_pad2 + self.window], pitch[:, s // self.window : (t + self.t_pad2) // self.window], pitchf[:, s // self.window : (t + self.t_pad2) // self.window], index, big_npy, index_rate, version, protect)[self.t_pad_tgt : -self.t_pad_tgt])
else: audio_opt.append(self.voice_conversion(model, net_g, sid, audio_pad[s : t + self.t_pad2 + self.window], None, None, index, big_npy, index_rate, version, protect)[self.t_pad_tgt : -self.t_pad_tgt])
s = t
if pitch_guidance: audio_opt.append(self.voice_conversion(model, net_g, sid, audio_pad[t:], pitch[:, t // self.window :] if t is not None else pitch, pitchf[:, t // self.window :] if t is not None else pitchf, index, big_npy, index_rate, version, protect)[self.t_pad_tgt : -self.t_pad_tgt])
else: audio_opt.append(self.voice_conversion(model, net_g, sid, audio_pad[t:], None, None, index, big_npy, index_rate, version, protect)[self.t_pad_tgt : -self.t_pad_tgt])
audio_opt = np.concatenate(audio_opt)
if volume_envelope != 1: audio_opt = change_rms(audio, self.sample_rate, audio_opt, tgt_sr, volume_envelope)
if resample_sr >= self.sample_rate and tgt_sr != resample_sr: audio_opt = librosa.resample(audio_opt, orig_sr=tgt_sr, target_sr=resample_sr)
audio_max = np.abs(audio_opt).max() / 0.99
max_int16 = 32768
if audio_max > 1: max_int16 /= audio_max
audio_opt = (audio_opt * max_int16).astype(np.int16)
if pitch_guidance: del pitch, pitchf
del sid
if torch.cuda.is_available(): torch.cuda.empty_cache()
return audio_opt
class VoiceConverter:
def __init__(self):
self.config = Config()
self.hubert_model = (None)
self.tgt_sr = None
self.net_g = None
self.vc = None
self.cpt = None
self.version = None
self.n_spk = None
self.use_f0 = None
self.loaded_model = None
def load_hubert(self, embedder_model):
try:
models, _, _ = checkpoint_utils.load_model_ensemble_and_task([os.path.join(now_dir, "assets", "model", "embedders", embedder_model + '.pt')], suffix="")
except Exception as e:
raise ImportError(translations["read_model_error"].format(e=e))
self.hubert_model = models[0].to(self.config.device)
self.hubert_model = (self.hubert_model.half() if self.config.is_half else self.hubert_model.float())
self.hubert_model.eval()
@staticmethod
def remove_audio_noise(input_audio_path, reduction_strength=0.7):
try:
rate, data = wavfile.read(input_audio_path)
reduced_noise = nr.reduce_noise(y=data, sr=rate, prop_decrease=reduction_strength)
return reduced_noise
except Exception as e:
logger.error(translations["denoise_error"].format(e=e))
return None
@staticmethod
def convert_audio_format(input_path, output_path, output_format):
try:
if output_format != "wav":
logger.debug(translations["change_format"].format(output_format=output_format))
audio, sample_rate = sf.read(input_path)
common_sample_rates = [
8000,
11025,
12000,
16000,
22050,
24000,
32000,
44100,
48000
]
target_sr = min(common_sample_rates, key=lambda x: abs(x - sample_rate))
audio = librosa.resample(audio, orig_sr=sample_rate, target_sr=target_sr)
sf.write(output_path, audio, target_sr, format=output_format)
return output_path
except Exception as e:
raise RuntimeError(translations["change_format_error"].format(e=e))
def convert_audio(self, audio_input_path, audio_output_path, model_path, index_path, embedder_model, pitch, f0_method, index_rate, volume_envelope, protect, hop_length, f0_autotune, f0_autotune_strength, filter_radius, clean_audio, clean_strength, export_format, upscale_audio, resample_sr = 0, sid = 0):
self.get_vc(model_path, sid)
try:
if upscale_audio: upscale(audio_input_path, audio_input_path)
audio = load_audio_infer(audio_input_path, 16000)
audio_max = np.abs(audio).max() / 0.95
if audio_max > 1: audio /= audio_max
if not self.hubert_model:
if not os.path.exists(os.path.join(now_dir, "assets", "model", "embedders", embedder_model + '.pt')): raise FileNotFoundError(f"Không tìm thấy mô hình: {embedder_model}")
self.load_hubert(embedder_model)
if self.tgt_sr != resample_sr >= 16000: self.tgt_sr = resample_sr
file_index = (index_path.strip().strip('"').strip("\n").strip('"').strip().replace("trained", "added"))
audio_opt = self.vc.pipeline(model=self.hubert_model, net_g=self.net_g, sid=sid, audio=audio, input_audio_path=audio_input_path, pitch=pitch, f0_method=f0_method, file_index=file_index, index_rate=index_rate, pitch_guidance=self.use_f0, filter_radius=filter_radius, tgt_sr=self.tgt_sr, resample_sr=resample_sr, volume_envelope=volume_envelope, version=self.version, protect=protect, hop_length=hop_length, f0_autotune=f0_autotune, f0_autotune_strength=f0_autotune_strength)
if audio_output_path: sf.write(audio_output_path, audio_opt, self.tgt_sr, format="wav")
if clean_audio:
cleaned_audio = self.remove_audio_noise(audio_output_path, clean_strength)
if cleaned_audio is not None: sf.write(audio_output_path, cleaned_audio, self.tgt_sr, format="wav")
output_path_format = audio_output_path.replace(".wav", f".{export_format}")
audio_output_path = self.convert_audio_format(audio_output_path, output_path_format, export_format)
except Exception as e:
logger.error(translations["error_convert"].format(e=e))
logger.error(traceback.format_exc())
def get_vc(self, weight_root, sid):
if sid == "" or sid == []:
self.cleanup_model()
if torch.cuda.is_available(): torch.cuda.empty_cache()
if not self.loaded_model or self.loaded_model != weight_root:
self.load_model(weight_root)
if self.cpt is not None:
self.setup_network()
self.setup_vc_instance()
self.loaded_model = weight_root
def cleanup_model(self):
if self.hubert_model is not None:
del self.net_g, self.n_spk, self.vc, self.hubert_model, self.tgt_sr
self.hubert_model = self.net_g = self.n_spk = self.vc = self.tgt_sr = None
if torch.cuda.is_available(): torch.cuda.empty_cache()
del self.net_g, self.cpt
if torch.cuda.is_available(): torch.cuda.empty_cache()
self.cpt = None
def load_model(self, weight_root):
self.cpt = (torch.load(weight_root, map_location="cpu") if os.path.isfile(weight_root) else None)
def setup_network(self):
if self.cpt is not None:
self.tgt_sr = self.cpt["config"][-1]
self.cpt["config"][-3] = self.cpt["weight"]["emb_g.weight"].shape[0]
self.use_f0 = self.cpt.get("f0", 1)
self.version = self.cpt.get("version", "v1")
self.text_enc_hidden_dim = 768 if self.version == "v2" else 256
self.net_g = Synthesizer(*self.cpt["config"], use_f0=self.use_f0, text_enc_hidden_dim=self.text_enc_hidden_dim, is_half=self.config.is_half)
del self.net_g.enc_q
self.net_g.load_state_dict(self.cpt["weight"], strict=False)
self.net_g.eval().to(self.config.device)
self.net_g = (self.net_g.half() if self.config.is_half else self.net_g.float())
def setup_vc_instance(self):
if self.cpt is not None:
self.vc = VC(self.tgt_sr, self.config)
self.n_spk = self.cpt["config"][-3]
if __name__ == "__main__":
mp.set_start_method("spawn", force=True)
main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370import os
import re
import sys
import time
import yt_dlp
import shutil
import librosa
import logging
import argparse
import warnings
import logging.handlers
import soundfile as sf
import noisereduce as nr
from distutils.util import strtobool
from pydub import AudioSegment, silence
now_dir = os.getcwd()
sys.path.append(now_dir)
from main.configs.config import Config
from main.library.algorithm.separator import Separator
translations = Config().translations
log_file = os.path.join("assets", "logs", "create_dataset.log")
logger = logging.getLogger(__name__)
if logger.hasHandlers(): logger.handlers.clear()
else:
console_handler = logging.StreamHandler()
console_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
console_handler.setFormatter(console_formatter)
console_handler.setLevel(logging.INFO)
file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=5*1024*1024, backupCount=3, encoding='utf-8')
file_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
file_handler.setFormatter(file_formatter)
file_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler)
logger.addHandler(file_handler)
logger.setLevel(logging.DEBUG)
def parse_arguments() -> tuple:
parser = argparse.ArgumentParser()
parser.add_argument("--input_audio", type=str, required=True)
parser.add_argument("--output_dataset", type=str, default="./dataset")
parser.add_argument("--resample", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--resample_sr", type=int, default=44100)
parser.add_argument("--clean_dataset", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--clean_strength", type=float, default=0.7)
parser.add_argument("--separator_music", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--separator_reverb", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--kim_vocal_version", type=int, default=2)
parser.add_argument("--overlap", type=float, default=0.25)
parser.add_argument("--segments_size", type=int, default=256)
parser.add_argument("--mdx_hop_length", type=int, default=1024)
parser.add_argument("--mdx_batch_size", type=int, default=1)
parser.add_argument("--denoise_mdx", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--skip", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--skip_start_audios", type=str, default="0")
parser.add_argument("--skip_end_audios", type=str, default="0")
args = parser.parse_args()
return args
dataset_temp = os.path.join("dataset_temp")
def main():
args = parse_arguments()
input_audio = args.input_audio
output_dataset = args.output_dataset
resample = args.resample
resample_sr = args.resample_sr
clean_dataset = args.clean_dataset
clean_strength = args.clean_strength
separator_music = args.separator_music
separator_reverb = args.separator_reverb
kim_vocal_version = args.kim_vocal_version
overlap = args.overlap
segments_size = args.segments_size
hop_length = args.mdx_hop_length
batch_size = args.mdx_batch_size
denoise_mdx = args.denoise_mdx
skip = args.skip
skip_start_audios = args.skip_start_audios
skip_end_audios = args.skip_end_audios
logger.debug(f"{translations['audio_path']}: {input_audio}")
logger.debug(f"{translations['output_path']}: {output_dataset}")
logger.debug(f"{translations['resample']}: {resample}")
if resample: logger.debug(f"{translations['sample_rate']}: {resample_sr}")
logger.debug(f"{translations['clear_dataset']}: {clean_dataset}")
if clean_dataset: logger.debug(f"{translations['clean_strength']}: {clean_strength}")
logger.debug(f"{translations['separator_music']}: {separator_music}")
logger.debug(f"{translations['dereveb_audio']}: {separator_reverb}")
if separator_music: logger.debug(f"{translations['training_version']}: {kim_vocal_version}")
logger.debug(f"{translations['segments_size']}: {segments_size}")
logger.debug(f"{translations['overlap']}: {overlap}")
logger.debug(f"Hop length: {hop_length}")
logger.debug(f"{translations['batch_size']}: {batch_size}")
logger.debug(f"{translations['denoise_mdx']}: {denoise_mdx}")
logger.debug(f"{translations['skip']}: {skip}")
if skip: logger.debug(f"{translations['skip_start']}: {skip_start_audios}")
if skip: logger.debug(f"{translations['skip_end']}: {skip_end_audios}")
if kim_vocal_version != 1 and kim_vocal_version != 2: raise ValueError(translations["version_not_valid"])
if separator_reverb and not separator_music: raise ValueError(translations["create_dataset_value_not_valid"])
start_time = time.time()
try:
paths = []
if not os.path.exists(dataset_temp): os.makedirs(dataset_temp, exist_ok=True)
urls = input_audio.replace(", ", ",").split(",")
for url in urls:
path = downloader(url, urls.index(url))
paths.append(path)
if skip:
skip_start_audios = skip_start_audios.replace(", ", ",").split(",")
skip_end_audios = skip_end_audios.replace(", ", ",").split(",")
if len(skip_start_audios) < len(paths) or len(skip_end_audios) < len(paths):
logger.warning(translations["skip<audio"])
sys.exit(1)
elif len(skip_start_audios) > len(paths) or len(skip_end_audios) > len(paths):
logger.warning(translations["skip>audio"])
sys.exit(1)
else:
for audio, skip_start_audio, skip_end_audio in zip(paths, skip_start_audios, skip_end_audios):
skip_start(audio, skip_start_audio)
skip_end(audio, skip_end_audio)
if separator_music:
separator_paths = []
for audio in paths:
vocals = separator_music_main(audio, dataset_temp, segments_size, overlap, denoise_mdx, kim_vocal_version, hop_length, batch_size)
if separator_reverb: vocals = separator_reverb_audio(vocals, dataset_temp, segments_size, overlap, denoise_mdx, hop_length, batch_size)
separator_paths.append(vocals)
paths = separator_paths
processed_paths = []
for audio in paths:
output = process_audio(audio)
processed_paths.append(output)
paths = processed_paths
for audio_path in paths:
data, sample_rate = sf.read(audio_path)
if resample_sr != sample_rate and resample_sr > 0 and resample:
data = librosa.resample(data, orig_sr=sample_rate, target_sr=resample_sr)
sample_rate = resample_sr
if clean_dataset: data = nr.reduce_noise(y=data, prop_decrease=clean_strength)
sf.write(audio_path, data, sample_rate)
except Exception as e:
raise RuntimeError(f"{translations['create_dataset_error']}: {e}")
finally:
for audio in paths:
shutil.move(audio, output_dataset)
if os.path.exists(dataset_temp): shutil.rmtree(dataset_temp, ignore_errors=True)
elapsed_time = time.time() - start_time
logger.info(translations["create_dataset_success"].format(elapsed_time=f"{elapsed_time:.2f}"))
def downloader(url, name):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': os.path.join(dataset_temp, f"{name}"),
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'wav',
'preferredquality': '192',
}],
'noplaylist': True,
'verbose': False,
}
logger.info(f"{translations['starting_download']}: {url}...")
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.extract_info(url)
logger.info(f"{translations['download_success']}: {url}")
return os.path.join(dataset_temp, f"{name}" + ".wav")
def skip_start(input_file, seconds):
data, sr = sf.read(input_file)
total_duration = len(data) / sr
if seconds <= 0: logger.warning(translations["=<0"])
elif seconds >= total_duration: logger.warning(translations["skip_warning"].format(seconds=seconds, total_duration=f"{total_duration:.2f}"))
else:
logger.info(f"{translations['skip_start']}: {input_file}...")
sf.write(input_file, data[int(seconds * sr):], sr)
logger.info(translations["skip_start_audio"].format(input_file=input_file))
def skip_end(input_file, seconds):
data, sr = sf.read(input_file)
total_duration = len(data) / sr
if seconds <= 0: logger.warning(translations["=<0"])
elif seconds > total_duration: logger.warning(translations["skip_warning"].format(seconds=seconds, total_duration=f"{total_duration:.2f}"))
else:
logger.info(f"{translations['skip_end']}: {input_file}...")
sf.write(input_file, data[:-int(seconds * sr)], sr)
logger.info(translations["skip_end_audio"].format(input_file=input_file))
def process_audio(file_path):
try:
song = AudioSegment.from_file(file_path)
nonsilent_parts = silence.detect_nonsilent(song, min_silence_len=750, silence_thresh=-70)
cut_files = []
for i, (start_i, end_i) in enumerate(nonsilent_parts):
chunk = song[start_i:end_i]
if len(chunk) >= 30:
chunk_file_path = os.path.join(os.path.dirname(file_path), f"chunk{i}.wav")
if os.path.exists(chunk_file_path): os.remove(chunk_file_path)
chunk.export(chunk_file_path, format="wav")
cut_files.append(chunk_file_path)
else: logger.warning(translations["skip_file"].format(i=i, chunk=len(chunk)))
logger.info(f"{translations['split_total']}: {len(cut_files)}")
def extract_number(filename):
match = re.search(r'_(\d+)', filename)
return int(match.group(1)) if match else 0
cut_files = sorted(cut_files, key=extract_number)
combined = AudioSegment.empty()
for file in cut_files:
combined += AudioSegment.from_file(file)
output_path = os.path.splitext(file_path)[0] + "_processed" + ".wav"
logger.info(translations["merge_audio"])
combined.export(output_path, format="wav")
return output_path
except Exception as e:
raise RuntimeError(f"{translations['process_audio_error']}: {e}")
def separator_music_main(input, output, segments_size, overlap, denoise, version, hop_length, batch_size):
if not os.path.exists(input):
logger.warning(translations["input_not_valid"])
return None
if not os.path.exists(output):
logger.warning(translations["output_not_valid"])
return None
model = f"Kim_Vocal_{version}.onnx"
logger.info(translations["separator_process"].format(input=input))
output_separator = separator_main(audio_file=input, model_filename=model, output_format="wav", output_dir=output, mdx_segment_size=segments_size, mdx_overlap=overlap, mdx_batch_size=batch_size, mdx_hop_length=hop_length, mdx_enable_denoise=denoise)
for f in output_separator:
path = os.path.join(output, f)
if not os.path.exists(path): logger.error(translations["not_found"].format(name=path))
if '_(Instrumental)_' in f: os.rename(path, os.path.splitext(path)[0].replace("(", "").replace(")", "") + ".wav")
elif '_(Vocals)_' in f:
rename_file = os.path.splitext(path)[0].replace("(", "").replace(")", "") + ".wav"
os.rename(path, rename_file)
logger.info(f": {rename_file}")
return rename_file
def separator_reverb_audio(input, output, segments_size, overlap, denoise, hop_length, batch_size):
reverb_models = "Reverb_HQ_By_FoxJoy.onnx"
if not os.path.exists(input):
logger.warning(translations["input_not_valid"])
return None
if not os.path.exists(output):
logger.warning(translations["output_not_valid"])
return None
logger.info(f"{translations['dereverb']}: {input}...")
output_dereverb = separator_main(audio_file=input, model_filename=reverb_models, output_format="wav", output_dir=output, mdx_segment_size=segments_size, mdx_overlap=overlap, mdx_batch_size=hop_length, mdx_hop_length=batch_size, mdx_enable_denoise=denoise)
for f in output_dereverb:
path = os.path.join(output, f)
if not os.path.exists(path): logger.error(translations["not_found"].format(name=path))
if '_(Reverb)_' in f: os.rename(path, os.path.splitext(path)[0].replace("(", "").replace(")", "") + ".wav")
elif '_(No Reverb)_' in f:
rename_file = os.path.splitext(path)[0].replace("(", "").replace(")", "") + ".wav"
os.rename(path, rename_file)
logger.info(f"{translations['dereverb_success']}: {rename_file}")
return rename_file
def separator_main(audio_file=None, model_filename="Kim_Vocal_1.onnx", output_format="wav", output_dir=".", mdx_segment_size=256, mdx_overlap=0.25, mdx_batch_size=1, mdx_hop_length=1024, mdx_enable_denoise=True):
separator = Separator(
log_formatter=file_formatter,
log_level=logging.INFO,
output_dir=output_dir,
output_format=output_format,
output_bitrate=None,
normalization_threshold=0.9,
output_single_stem=None,
invert_using_spec=False,
sample_rate=44100,
mdx_params={
"hop_length": mdx_hop_length,
"segment_size": mdx_segment_size,
"overlap": mdx_overlap,
"batch_size": mdx_batch_size,
"enable_denoise": mdx_enable_denoise,
},
)
separator.load_model(model_filename=model_filename)
return separator.separate(audio_file)
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120import osimport sysimport faissimport loggingimport argparseimport logging.handlers
import numpy as np
from multiprocessing import cpu_countfrom sklearn.cluster import MiniBatchKMeans
now_dir = os.getcwd()sys.path.append(now_dir)
from main.configs.config import Config
translations = Config().translations
def parse_arguments() -> tuple: parser = argparse.ArgumentParser() parser.add_argument("--model_name", type=str, required=True) parser.add_argument("--rvc_version", type=str, default="v2") parser.add_argument("--index_algorithm", type=str, default="Auto")
args = parser.parse_args() return args
def main(): args = parse_arguments()
exp_dir = os.path.join("assets", "logs", args.model_name) version = args.rvc_version index_algorithm = args.index_algorithm
log_file = os.path.join(exp_dir, "create_index.log") logger = logging.getLogger(__name__)
if logger.hasHandlers(): logger.handlers.clear() else: console_handler = logging.StreamHandler() console_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
console_handler.setFormatter(console_formatter) console_handler.setLevel(logging.INFO)
file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=5*1024*1024, backupCount=3, encoding='utf-8') file_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
file_handler.setFormatter(file_formatter) file_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler) logger.addHandler(file_handler) logger.setLevel(logging.DEBUG)
logger.debug(f"{translations['modelname']}: {args.model_name}") logger.debug(f"{translations['model_path']}: {exp_dir}") logger.debug(f"{translations['training_version']}: {version}") logger.debug(f"{translations['index_algorithm_info']}: {index_algorithm}")
try: feature_dir = os.path.join(exp_dir, f"{version}_extracted") model_name = os.path.basename(exp_dir)
npys = [] listdir_res = sorted(os.listdir(feature_dir))
for name in listdir_res: file_path = os.path.join(feature_dir, name) phone = np.load(file_path) npys.append(phone)
big_npy = np.concatenate(npys, axis=0) big_npy_idx = np.arange(big_npy.shape[0])
np.random.shuffle(big_npy_idx) big_npy = big_npy[big_npy_idx]
if big_npy.shape[0] > 2e5 and (index_algorithm == "Auto" or index_algorithm == "KMeans"): big_npy = (MiniBatchKMeans(n_clusters=10000, verbose=True, batch_size=256 * cpu_count(), compute_labels=False, init="random").fit(big_npy).cluster_centers_)
np.save(os.path.join(exp_dir, "total_fea.npy"), big_npy)
n_ivf = min(int(16 * np.sqrt(big_npy.shape[0])), big_npy.shape[0] // 39)
index_trained = faiss.index_factory(256 if version == "v1" else 768, f"IVF{n_ivf},Flat")
index_ivf_trained = faiss.extract_index_ivf(index_trained) index_ivf_trained.nprobe = 1
index_trained.train(big_npy)
faiss.write_index(index_trained, os.path.join(exp_dir, f"trained_IVF{n_ivf}_Flat_nprobe_{index_ivf_trained.nprobe}_{model_name}_{version}.index"))
index_added = faiss.index_factory(256 if version == "v1" else 768, f"IVF{n_ivf},Flat") index_ivf_added = faiss.extract_index_ivf(index_added)
index_ivf_added.nprobe = 1 index_added.train(big_npy)
batch_size_add = 8192 for i in range(0, big_npy.shape[0], batch_size_add): index_added.add(big_npy[i : i + batch_size_add]) index_filepath_added = os.path.join(exp_dir, f"added_IVF{n_ivf}_Flat_nprobe_{index_ivf_added.nprobe}_{model_name}_{version}.index")
faiss.write_index(index_added, index_filepath_added)
logger.info(f"{translations['save_index']} '{index_filepath_added}'") except Exception as e: logger.error(f"{translations['create_index_error']}: {e}")
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450import os
import gc
import sys
import time
import tqdm
import torch
import shutil
import codecs
import pyworld
import librosa
import logging
import argparse
import warnings
import subprocess
import torchcrepe
import parselmouth
import logging.handlers
import numpy as np
import soundfile as sf
import torch.nn.functional as F
from random import shuffle
from functools import partial
from multiprocessing import Pool
from distutils.util import strtobool
from fairseq import checkpoint_utils
from concurrent.futures import ThreadPoolExecutor, as_completed
now_dir = os.getcwd()
sys.path.append(now_dir)
from main.configs.config import Config
from main.library.predictors.FCPE import FCPE
from main.library.predictors.RMVPE import RMVPE
logging.getLogger("wget").setLevel(logging.ERROR)
warnings.filterwarnings("ignore", category=FutureWarning)
warnings.filterwarnings("ignore", category=UserWarning)
logger = logging.getLogger(__name__)
logger.propagate = False
config = Config()
translations = config.translations
def parse_arguments() -> tuple:
parser = argparse.ArgumentParser()
parser.add_argument("--model_name", type=str, required=True)
parser.add_argument("--rvc_version", type=str, default="v2")
parser.add_argument("--f0_method", type=str, default="rmvpe")
parser.add_argument("--pitch_guidance", type=lambda x: bool(strtobool(x)), default=True)
parser.add_argument("--hop_length", type=int, default=128)
parser.add_argument("--cpu_cores", type=int, default=2)
parser.add_argument("--gpu", type=str, default="-")
parser.add_argument("--sample_rate", type=int, required=True)
parser.add_argument("--embedder_model", type=str, default="contentvec_base")
args = parser.parse_args()
return args
def load_audio(file, sample_rate):
try:
file = file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
audio, sr = sf.read(file)
if len(audio.shape) > 1: audio = librosa.to_mono(audio.T)
if sr != sample_rate: audio = librosa.resample(audio, orig_sr=sr, target_sr=sample_rate)
except Exception as e:
raise RuntimeError(f"{translations['errors_loading_audio']}: {e}")
return audio.flatten()
def check_rmvpe_fcpe(method):
if method == "rmvpe" and not os.path.exists(os.path.join("assets", "model", "predictors", "rmvpe.pt")): subprocess.run(["wget", "-q", "--show-progress", "--no-check-certificate", codecs.decode("uggcf://uhttvatsnpr.pb/NauC/Pbyno_EIP_Cebwrpg_2/erfbyir/znva/", "rot13") + "rmvpe.pt", "-P", os.path.join("assets", "model", "predictors")], check=True)
elif method == "fcpe" and not os.path.exists(os.path.join("assets", "model", "predictors", "fcpe.pt")): subprocess.run(["wget", "-q", "--show-progress", "--no-check-certificate", codecs.decode("uggcf://uhttvatsnpr.pb/NauC/Pbyno_EIP_Cebwrpg_2/erfbyir/znva/", "rot13") + "fcpe.pt", "-P", os.path.join("assets", "model", "predictors")], check=True)
def check_hubert(hubert):
if hubert == "contentvec_base" or hubert == "hubert_base" or hubert == "japanese_hubert_base" or hubert == "korean_hubert_base" or hubert == "chinese_hubert_base":
model_path = os.path.join(now_dir, "assets", "model", "embedders", hubert + '.pt')
if not os.path.exists(model_path): subprocess.run(["wget", "-q", "--show-progress", "--no-check-certificate", codecs.decode("uggcf://uhttvatsnpr.pb/NauC/Pbyno_EIP_Cebwrpg_2/erfbyir/znva/", "rot13") + f"{hubert}.pt", "-P", os.path.join("assets", "model", "embedders")], check=True)
def generate_config(rvc_version, sample_rate, model_path):
config_path = os.path.join("main", "configs", rvc_version, f"{sample_rate}.json")
config_save_path = os.path.join(model_path, "config.json")
if not os.path.exists(config_save_path): shutil.copy(config_path, config_save_path)
def generate_filelist(pitch_guidance, model_path, rvc_version, sample_rate):
gt_wavs_dir = os.path.join(model_path, "sliced_audios")
feature_dir = os.path.join(model_path, f"{rvc_version}_extracted")
f0_dir, f0nsf_dir = None, None
if pitch_guidance:
f0_dir = os.path.join(model_path, "f0")
f0nsf_dir = os.path.join(model_path, "f0_voiced")
gt_wavs_files = set(name.split(".")[0] for name in os.listdir(gt_wavs_dir))
feature_files = set(name.split(".")[0] for name in os.listdir(feature_dir))
if pitch_guidance:
f0_files = set(name.split(".")[0] for name in os.listdir(f0_dir))
f0nsf_files = set(name.split(".")[0] for name in os.listdir(f0nsf_dir))
names = gt_wavs_files & feature_files & f0_files & f0nsf_files
else: names = gt_wavs_files & feature_files
options = []
mute_base_path = os.path.join(now_dir, "assets", "logs", "mute")
for name in names:
if pitch_guidance: options.append(f"{gt_wavs_dir}/{name}.wav|{feature_dir}/{name}.npy|{f0_dir}/{name}.wav.npy|{f0nsf_dir}/{name}.wav.npy|0")
else: options.append(f"{gt_wavs_dir}/{name}.wav|{feature_dir}/{name}.npy|0")
mute_audio_path = os.path.join(mute_base_path, "sliced_audios", f"mute{sample_rate}.wav")
mute_feature_path = os.path.join(mute_base_path, f"{rvc_version}_extracted", "mute.npy")
for _ in range(2):
if pitch_guidance:
mute_f0_path = os.path.join(mute_base_path, "f0", "mute.wav.npy")
mute_f0nsf_path = os.path.join(mute_base_path, "f0_voiced", "mute.wav.npy")
options.append(f"{mute_audio_path}|{mute_feature_path}|{mute_f0_path}|{mute_f0nsf_path}|0")
else: options.append(f"{mute_audio_path}|{mute_feature_path}|0")
shuffle(options)
with open(os.path.join(model_path, "filelist.txt"), "w") as f:
f.write("\n".join(options))
def setup_paths(exp_dir, version = None):
wav_path = os.path.join(exp_dir, "sliced_audios_16k")
if version:
out_path = os.path.join(exp_dir, "v1_extracted" if version == "v1" else "v2_extracted")
os.makedirs(out_path, exist_ok=True)
return wav_path, out_path
else:
output_root1 = os.path.join(exp_dir, "f0")
output_root2 = os.path.join(exp_dir, "f0_voiced")
os.makedirs(output_root1, exist_ok=True)
os.makedirs(output_root2, exist_ok=True)
return wav_path, output_root1, output_root2
def read_wave(wav_path, normalize = False):
wav, sr = sf.read(wav_path)
assert sr == 16000, translations["sr_not_16000"]
feats = torch.from_numpy(wav).float()
if config.is_half: feats = feats.half()
if feats.dim() == 2: feats = feats.mean(-1)
feats = feats.view(1, -1)
if normalize: feats = F.layer_norm(feats, feats.shape)
return feats
def get_device(gpu_index):
if gpu_index == "cpu": return "cpu"
try:
index = int(gpu_index)
if index < torch.cuda.device_count(): return f"cuda:{index}"
else: logger.warning(translations["gpu_not_valid"])
except ValueError:
logger.warning(translations["gpu_not_valid"])
return "cpu"
class FeatureInput:
def __init__(self, sample_rate=16000, hop_size=160, device="cpu"):
self.fs = sample_rate
self.hop = hop_size
self.f0_bin = 256
self.f0_max = 1100.0
self.f0_min = 50.0
self.f0_mel_min = 1127 * np.log(1 + self.f0_min / 700)
self.f0_mel_max = 1127 * np.log(1 + self.f0_max / 700)
self.device = device
def compute_f0(self, np_arr, f0_method, hop_length):
if f0_method == "pm": return self.get_pm(np_arr)
elif f0_method == 'dio': return self.get_dio(np_arr)
elif f0_method == "crepe": return self.get_crepe(np_arr, int(hop_length))
elif f0_method == "crepe-tiny": return self.get_crepe(np_arr, int(hop_length), "tiny")
elif f0_method == "fcpe": return self.get_fcpe(np_arr, int(hop_length))
elif f0_method == "rmvpe": return self.get_rmvpe(np_arr)
elif f0_method == "harvest": return self.get_harvest(np_arr)
else: raise ValueError(translations["method_not_valid"])
def get_pm(self, x):
time_step = 160 / 16000 * 1000
f0 = (parselmouth.Sound(x, self.fs).to_pitch_ac(time_step=time_step / 1000, voicing_threshold=0.6, pitch_floor=50, pitch_ceiling=1100).selected_array["frequency"])
pad_size = ((x.size // self.hop) - len(f0) + 1) // 2
if pad_size > 0 or (x.size // self.hop) - len(f0) - pad_size > 0: f0 = np.pad(f0, [[pad_size, (x.size // self.hop) - len(f0) - pad_size]], mode="constant")
return f0
def get_dio(self, x):
f0, t = pyworld.dio(x.astype(np.double), fs=self.fs, f0_ceil=self.f0_max, f0_floor=self.f0_min, frame_period=1000 * self.hop / self.fs)
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.fs)
return f0
def get_crepe(self, x, hop_length, model="full"):
audio = torch.from_numpy(x.astype(np.float32)).to(self.device)
audio /= torch.quantile(torch.abs(audio), 0.999)
audio = audio.unsqueeze(0)
pitch = torchcrepe.predict(audio, self.fs, hop_length, self.f0_min, self.f0_max, model=model, batch_size=hop_length * 2, device=self.device, pad=True)
source = pitch.squeeze(0).cpu().float().numpy()
source[source < 0.001] = np.nan
target = np.interp(np.arange(0, len(source) * (x.size // self.hop), len(source)) / (x.size // self.hop), np.arange(0, len(source)), source)
return np.nan_to_num(target)
def get_fcpe(self, x, hop_length):
self.model_fcpe = FCPE(os.path.join("assets", "model", "predictors", "fcpe.pt"), hop_length=int(hop_length), f0_min=self.f0_min, f0_max=self.f0_max, dtype=torch.float32, device=self.device, sample_rate=self.fs, threshold=0.03)
f0 = self.model_fcpe.compute_f0(x, p_len=(x.size // self.hop))
del self.model_fcpe
gc.collect()
return f0
def get_rmvpe(self, x):
self.model_rmvpe = RMVPE(os.path.join("assets", "model", "predictors", "rmvpe.pt"), is_half=False, device=self.device)
return self.model_rmvpe.infer_from_audio(x, thred=0.03)
def get_harvest(self, x):
f0, t = pyworld.harvest(x.astype(np.double), fs=self.fs, f0_ceil=self.f0_max, f0_floor=self.f0_min, frame_period=1000 * self.hop / self.fs)
f0 = pyworld.stonemask(x.astype(np.double), f0, t, self.fs)
return f0
def coarse_f0(self, f0):
f0_mel = 1127 * np.log(1 + f0 / 700)
f0_mel = np.clip((f0_mel - self.f0_mel_min) * (self.f0_bin - 2) / (self.f0_mel_max - self.f0_mel_min) + 1, 1, self.f0_bin - 1)
return np.rint(f0_mel).astype(int)
def process_file(self, file_info, f0_method, hop_length):
inp_path, opt_path1, opt_path2, np_arr = file_info
if os.path.exists(opt_path1 + ".npy") and os.path.exists(opt_path2 + ".npy"): return
try:
feature_pit = self.compute_f0(np_arr, f0_method, hop_length)
np.save(opt_path2, feature_pit, allow_pickle=False)
coarse_pit = self.coarse_f0(feature_pit)
np.save(opt_path1, coarse_pit, allow_pickle=False)
except Exception as e:
raise RuntimeError(f"{translations['extract_file_error']} {inp_path}: {e}")
def process_files(self, files, f0_method, hop_length, pbar):
for file_info in files:
self.process_file(file_info, f0_method, hop_length)
pbar.update()
def run_pitch_extraction(exp_dir, f0_method, hop_length, num_processes, gpus):
input_root, *output_roots = setup_paths(exp_dir)
if len(output_roots) == 2: output_root1, output_root2 = output_roots
else:
output_root1 = output_roots[0]
output_root2 = None
paths = [
(
os.path.join(input_root, name),
os.path.join(output_root1, name) if output_root1 else None,
os.path.join(output_root2, name) if output_root2 else None,
load_audio(os.path.join(input_root, name), 16000),
)
for name in sorted(os.listdir(input_root))
if "spec" not in name
]
logger.info(translations["extract_f0_method"].format(num_processes=num_processes, f0_method=f0_method))
start_time = time.time()
if gpus != "-":
gpus = gpus.split("-")
num_gpus = len(gpus)
process_partials = []
pbar = tqdm.tqdm(total=len(paths), desc=translations["extract_f0"])
for idx, gpu in enumerate(gpus):
device = get_device(gpu)
feature_input = FeatureInput(device=device)
part_paths = paths[idx::num_gpus]
process_partials.append((feature_input, part_paths))
with ThreadPoolExecutor() as executor:
futures = [executor.submit(FeatureInput.process_files, feature_input, part_paths, f0_method, hop_length, pbar) for feature_input, part_paths in process_partials]
for future in as_completed(futures):
pbar.update(1)
future.result()
pbar.close()
else:
feature_input = FeatureInput(device="cpu")
with tqdm.tqdm(total=len(paths), desc=translations["extract_f0"]) as pbar:
with Pool(processes=num_processes) as pool:
process_file_partial = partial(feature_input.process_file, f0_method=f0_method, hop_length=hop_length)
for _ in pool.imap_unordered(process_file_partial, paths):
pbar.update(1)
elapsed_time = time.time() - start_time
logger.info(translations["extract_f0_success"].format(elapsed_time=f"{elapsed_time:.2f}"))
def process_file_embedding(file, wav_path, out_path, model, device, version, saved_cfg):
wav_file_path = os.path.join(wav_path, file)
out_file_path = os.path.join(out_path, file.replace("wav", "npy"))
if os.path.exists(out_file_path): return
feats = read_wave(wav_file_path, normalize=saved_cfg.task.normalize)
dtype = torch.float16 if device.startswith("cuda") else torch.float32
feats = feats.to(dtype).to(device)
padding_mask = torch.BoolTensor(feats.shape).fill_(False).to(dtype).to(device)
inputs = {
"source": feats,
"padding_mask": padding_mask,
"output_layer": 9 if version == "v1" else 12,
}
with torch.no_grad():
model = model.to(device).to(dtype)
logits = model.extract_features(**inputs)
feats = model.final_proj(logits[0]) if version == "v1" else logits[0]
feats = feats.squeeze(0).float().cpu().numpy()
if not np.isnan(feats).any(): np.save(out_file_path, feats, allow_pickle=False)
else: logger.warning(f"{file} {translations['NaN']}")
def run_embedding_extraction(exp_dir, version, gpus, embedder_model):
wav_path, out_path = setup_paths(exp_dir, version)
logger.info(translations["start_extract_hubert"])
start_time = time.time()
try:
models, saved_cfg, _ = checkpoint_utils.load_model_ensemble_and_task([os.path.join(now_dir, "assets", "model", "embedders", embedder_model + '.pt')], suffix="")
except Exception as e:
raise ImportError(translations["read_model_error"].format(e=e))
model = models[0]
devices = [get_device(gpu) for gpu in (gpus.split("-") if gpus != "-" else ["cpu"])]
paths = sorted([file for file in os.listdir(wav_path) if file.endswith(".wav")])
if not paths:
logger.warning(translations["not_found_audio_file"])
sys.exit(1)
pbar = tqdm.tqdm(total=len(paths) * len(devices), desc=translations["extract_hubert"])
tasks = [(file, wav_path, out_path, model, device, version, saved_cfg) for file in paths for device in devices]
for task in tasks:
try:
process_file_embedding(*task)
except Exception as e:
raise RuntimeError(f"{translations['process_error']} {task[0]}: {e}")
pbar.update(1)
pbar.close()
elapsed_time = time.time() - start_time
logger.info(translations["extract_hubert_success"].format(elapsed_time=f"{elapsed_time:.2f}"))
if __name__ == "__main__":
args = parse_arguments()
exp_dir = os.path.join("assets", "logs", args.model_name)
f0_method = args.f0_method
hop_length = args.hop_length
num_processes = args.cpu_cores
gpus = args.gpu
version = args.rvc_version
pitch_guidance = args.pitch_guidance
sample_rate = args.sample_rate
embedder_model = args.embedder_model
check_rmvpe_fcpe(f0_method)
check_hubert(embedder_model)
if len([f for f in os.listdir(os.path.join(exp_dir, "sliced_audios")) if os.path.isfile(os.path.join(exp_dir, "sliced_audios", f))]) < 1 or len([f for f in os.listdir(os.path.join(exp_dir, "sliced_audios_16k")) if os.path.isfile(os.path.join(exp_dir, "sliced_audios_16k", f))]) < 1: raise FileNotFoundError("Không tìm thấy dữ liệu được xử lý, vui lòng xử lý lại âm thanh")
log_file = os.path.join(exp_dir, "extract.log")
if logger.hasHandlers(): logger.handlers.clear()
else:
console_handler = logging.StreamHandler()
console_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
console_handler.setFormatter(console_formatter)
console_handler.setLevel(logging.INFO)
file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=5*1024*1024, backupCount=3, encoding='utf-8')
file_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
file_handler.setFormatter(file_formatter)
file_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler)
logger.addHandler(file_handler)
logger.setLevel(logging.DEBUG)
logger.debug(f"{translations['modelname']}: {args.model_name}")
logger.debug(f"{translations['export_process']}: {exp_dir}")
logger.debug(f"{translations['f0_method']}: {f0_method}")
logger.debug(f"{translations['pretrain_sr']}: {sample_rate}")
logger.debug(f"{translations['cpu_core']}: {num_processes}")
logger.debug(f"Gpu: {gpus}")
if f0_method == "crepe" or f0_method == "crepe-tiny" or f0_method == "fcpe": logger.debug(f"Hop length: {hop_length}")
logger.debug(f"{translations['training_version']}: {version}")
logger.debug(f"{translations['extract_f0']}: {pitch_guidance}")
logger.debug(f"{translations['hubert_model']}: {embedder_model}")
try:
run_pitch_extraction(exp_dir, f0_method, hop_length, num_processes, gpus)
run_embedding_extraction(exp_dir, version, gpus, embedder_model)
generate_config(version, sample_rate, exp_dir)
generate_filelist(pitch_guidance, exp_dir, version, sample_rate)
except Exception as e:
logger.error(f"{translations['extract_error']}: {e}")
logger.info(f"{translations['extract_success']} {args.model_name}.")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360import osimport sysimport timeimport torchimport loggingimport librosaimport argparseimport logging.handlers
import numpy as npimport soundfile as sfimport multiprocessingimport noisereduce as nr
from tqdm import tqdmfrom scipy import signalfrom scipy.io import wavfilefrom distutils.util import strtoboolfrom concurrent.futures import ProcessPoolExecutor, as_completed
now_directory = os.getcwd()sys.path.append(now_directory)
from main.configs.config import Config
logger = logging.getLogger(__name__)
logging.getLogger("numba.core.byteflow").setLevel(logging.ERROR)logging.getLogger("numba.core.ssa").setLevel(logging.ERROR)logging.getLogger("numba.core.interpreter").setLevel(logging.ERROR)
OVERLAP = 0.3MAX_AMPLITUDE = 0.9ALPHA = 0.75HIGH_PASS_CUTOFF = 48SAMPLE_RATE_16K = 16000
config = Config()per = 3.0 if config.is_half else 3.7translations = config.translations
def parse_arguments() -> tuple: parser = argparse.ArgumentParser() parser.add_argument("--model_name", type=str, required=True) parser.add_argument("--dataset_path", type=str, default="./dataset") parser.add_argument("--sample_rate", type=int, required=True) parser.add_argument("--cpu_cores", type=int, default=2) parser.add_argument("--cut_preprocess", type=lambda x: bool(strtobool(x)), default=True) parser.add_argument("--process_effects", type=lambda x: bool(strtobool(x)), default=False) parser.add_argument("--clean_dataset", type=lambda x: bool(strtobool(x)), default=False) parser.add_argument("--clean_strength", type=float, default=0.7)
args = parser.parse_args() return args
def load_audio(file, sample_rate): try: file = file.strip(" ").strip('"').strip("\n").strip('"').strip(" ") audio, sr = sf.read(file)
if len(audio.shape) > 1: audio = librosa.to_mono(audio.T) if sr != sample_rate: audio = librosa.resample(audio, orig_sr=sr, target_sr=sample_rate) except Exception as e: raise RuntimeError(f"{translations['errors_loading_audio']}: {e}")
return audio.flatten()
class Slicer: def __init__(self, sr, threshold = -40.0, min_length = 5000, min_interval = 300, hop_size = 20, max_sil_kept = 5000): if not min_length >= min_interval >= hop_size: raise ValueError(translations["min_length>=min_interval>=hop_size"]) if not max_sil_kept >= hop_size: raise ValueError(translations["max_sil_kept>=hop_size"])
min_interval = sr * min_interval / 1000 self.threshold = 10 ** (threshold / 20.0) self.hop_size = round(sr * hop_size / 1000) self.win_size = min(round(min_interval), 4 * self.hop_size) self.min_length = round(sr * min_length / 1000 / self.hop_size) self.min_interval = round(min_interval / self.hop_size) self.max_sil_kept = round(sr * max_sil_kept / 1000 / self.hop_size)
def _apply_slice(self, waveform, begin, end): start_idx = begin * self.hop_size
if len(waveform.shape) > 1: end_idx = min(waveform.shape[1], end * self.hop_size) return waveform[:, start_idx:end_idx] else: end_idx = min(waveform.shape[0], end * self.hop_size) return waveform[start_idx:end_idx]
def slice(self, waveform): samples = waveform.mean(axis=0) if len(waveform.shape) > 1 else waveform if samples.shape[0] <= self.min_length: return [waveform]
rms_list = get_rms(y=samples, frame_length=self.win_size, hop_length=self.hop_size).squeeze(0)
sil_tags = [] silence_start, clip_start = None, 0
for i, rms in enumerate(rms_list): if rms < self.threshold: if silence_start is None: silence_start = i continue
if silence_start is None: continue
is_leading_silence = silence_start == 0 and i > self.max_sil_kept need_slice_middle = (i - silence_start >= self.min_interval and i - clip_start >= self.min_length)
if not is_leading_silence and not need_slice_middle: silence_start = None continue
if i - silence_start <= self.max_sil_kept: pos = rms_list[silence_start : i + 1].argmin() + silence_start if silence_start == 0: sil_tags.append((0, pos)) else: sil_tags.append((pos, pos))
clip_start = pos
elif i - silence_start <= self.max_sil_kept * 2: pos = rms_list[i - self.max_sil_kept : silence_start + self.max_sil_kept + 1].argmin()
pos += i - self.max_sil_kept
pos_l = (rms_list[silence_start : silence_start + self.max_sil_kept + 1].argmin() + silence_start) pos_r = (rms_list[i - self.max_sil_kept : i + 1].argmin() + i - self.max_sil_kept)
if silence_start == 0: sil_tags.append((0, pos_r)) clip_start = pos_r else: sil_tags.append((min(pos_l, pos), max(pos_r, pos))) clip_start = max(pos_r, pos) else: pos_l = (rms_list[silence_start : silence_start + self.max_sil_kept + 1].argmin() + silence_start) pos_r = (rms_list[i - self.max_sil_kept : i + 1].argmin() + i - self.max_sil_kept)
if silence_start == 0: sil_tags.append((0, pos_r)) else: sil_tags.append((pos_l, pos_r))
clip_start = pos_r silence_start = None
total_frames = rms_list.shape[0]
if (silence_start is not None and total_frames - silence_start >= self.min_interval): silence_end = min(total_frames, silence_start + self.max_sil_kept) pos = rms_list[silence_start : silence_end + 1].argmin() + silence_start sil_tags.append((pos, total_frames + 1))
if not sil_tags: return [waveform] else: chunks = []
if sil_tags[0][0] > 0: chunks.append(self._apply_slice(waveform, 0, sil_tags[0][0]))
for i in range(len(sil_tags) - 1): chunks.append(self._apply_slice(waveform, sil_tags[i][1], sil_tags[i + 1][0]))
if sil_tags[-1][1] < total_frames: chunks.append(self._apply_slice(waveform, sil_tags[-1][1], total_frames))
return chunks
def get_rms(y, frame_length=2048, hop_length=512, pad_mode="constant"): padding = (int(frame_length // 2), int(frame_length // 2)) y = np.pad(y, padding, mode=pad_mode)
axis = -1 out_strides = y.strides + tuple([y.strides[axis]]) x_shape_trimmed = list(y.shape) x_shape_trimmed[axis] -= frame_length - 1 out_shape = tuple(x_shape_trimmed) + tuple([frame_length]) xw = np.lib.stride_tricks.as_strided(y, shape=out_shape, strides=out_strides)
target_axis = axis - 1 if axis < 0 else axis + 1
xw = np.moveaxis(xw, -1, target_axis) slices = [slice(None)] * xw.ndim slices[axis] = slice(0, None, hop_length) x = xw[tuple(slices)]
power = np.mean(np.abs(x) ** 2, axis=-2, keepdims=True) return np.sqrt(power)
class PreProcess: def __init__(self, sr, exp_dir, per): self.slicer = Slicer(sr=sr, threshold=-42, min_length=1500, min_interval=400, hop_size=15, max_sil_kept=500) self.sr = sr self.b_high, self.a_high = signal.butter(N=5, Wn=HIGH_PASS_CUTOFF, btype="high", fs=self.sr) self.per = per self.exp_dir = exp_dir self.device = "cpu" self.gt_wavs_dir = os.path.join(exp_dir, "sliced_audios") self.wavs16k_dir = os.path.join(exp_dir, "sliced_audios_16k")
os.makedirs(self.gt_wavs_dir, exist_ok=True) os.makedirs(self.wavs16k_dir, exist_ok=True)
def _normalize_audio(self, audio: torch.Tensor): tmp_max = torch.abs(audio).max() if tmp_max > 2.5: return None return (audio / tmp_max * (MAX_AMPLITUDE * ALPHA)) + (1 - ALPHA) * audio
def process_audio_segment(self, normalized_audio: np.ndarray, sid, idx0, idx1): if normalized_audio is None: logs(f"{sid}-{idx0}-{idx1}-filtered") return wavfile.write(os.path.join(self.gt_wavs_dir, f"{sid}_{idx0}_{idx1}.wav"), self.sr, normalized_audio.astype(np.float32)) audio_16k = librosa.resample(normalized_audio, orig_sr=self.sr, target_sr=SAMPLE_RATE_16K) wavfile.write(os.path.join(self.wavs16k_dir, f"{sid}_{idx0}_{idx1}.wav"), SAMPLE_RATE_16K, audio_16k.astype(np.float32))
def process_audio(self, path, idx0, sid, cut_preprocess, process_effects, clean_dataset, clean_strength): try: audio = load_audio(path, self.sr)
if process_effects: audio = signal.lfilter(self.b_high, self.a_high, audio) audio = self._normalize_audio(audio)
if clean_dataset: audio = nr.reduce_noise(y=audio, sr=self.sr, prop_decrease=clean_strength)
idx1 = 0
if cut_preprocess: for audio_segment in self.slicer.slice(audio): i = 0
while 1: start = int(self.sr * (self.per - OVERLAP) * i) i += 1
if len(audio_segment[start:]) > (self.per + OVERLAP) * self.sr: tmp_audio = audio_segment[start : start + int(self.per * self.sr)] self.process_audio_segment(tmp_audio, sid, idx0, idx1) idx1 += 1 else: tmp_audio = audio_segment[start:] self.process_audio_segment(tmp_audio, sid, idx0, idx1) idx1 += 1 break else: self.process_audio_segment(audio, sid, idx0, idx1) except Exception as e: raise RuntimeError(f"{translations['process_audio_error']}: {e}")
def process_file(args): pp, file, cut_preprocess, process_effects, clean_dataset, clean_strength = (args) file_path, idx0, sid = file
pp.process_audio(file_path, idx0, sid, cut_preprocess, process_effects, clean_dataset, clean_strength)
def preprocess_training_set(input_root, sr, num_processes, exp_dir, per, cut_preprocess, process_effects, clean_dataset, clean_strength): start_time = time.time()
pp = PreProcess(sr, exp_dir, per) logger.info(translations["start_preprocess"].format(num_processes=num_processes))
files = [] idx = 0
for root, _, filenames in os.walk(input_root): try: sid = 0 if root == input_root else int(os.path.basename(root)) for f in filenames: if f.lower().endswith((".wav", ".mp3", ".flac", ".ogg")): files.append((os.path.join(root, f), idx, sid)) idx += 1 except ValueError: raise ValueError(f"{translations['not_integer']} '{os.path.basename(root)}'.")
with tqdm(total=len(files), desc=translations["preprocess"]) as pbar: with ProcessPoolExecutor(max_workers=num_processes) as executor: futures = [ executor.submit( process_file, ( pp, file, cut_preprocess, process_effects, clean_dataset, clean_strength, ), ) for file in files ] for future in as_completed(futures): try: future.result() except Exception as e: raise RuntimeError(f"{translations['process_error']}: {e}")
pbar.update(1)
elapsed_time = time.time() - start_time logger.info(translations["preprocess_success"].format(elapsed_time=f"{elapsed_time:.2f}"))
if __name__ == "__main__": args = parse_arguments()
experiment_directory = os.path.join("assets", "logs", args.model_name) num_processes = args.cpu_cores num_processes = multiprocessing.cpu_count() if num_processes is None else int(num_processes) dataset = args.dataset_path sample_rate = args.sample_rate cut_preprocess = args.cut_preprocess preprocess_effects = args.process_effects clean_dataset = args.clean_dataset clean_strength = args.clean_strength
os.makedirs(experiment_directory, exist_ok=True)
if len([f for f in os.listdir(os.path.join(dataset)) if os.path.isfile(os.path.join(dataset, f)) and f.lower().endswith((".wav", ".mp3", ".flac", ".ogg"))]) < 1: raise FileNotFoundError("Không tìm thấy dữ liệu")
log_file = os.path.join(experiment_directory, "preprocess.log")
if logger.hasHandlers(): logger.handlers.clear() else: console_handler = logging.StreamHandler() console_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
console_handler.setFormatter(console_formatter) console_handler.setLevel(logging.INFO)
file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=5*1024*1024, backupCount=3, encoding='utf-8') file_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
file_handler.setFormatter(file_formatter) file_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler) logger.addHandler(file_handler) logger.setLevel(logging.DEBUG)
logger.debug(f"{translations['modelname']}: {args.model_name}") logger.debug(f"{translations['export_process']}: {experiment_directory}") logger.debug(f"{translations['dataset_folder']}: {dataset}") logger.debug(f"{translations['pretrain_sr']}: {sample_rate}") logger.debug(f"{translations['cpu_core']}: {num_processes}") logger.debug(f"{translations['split_audio']}: {cut_preprocess}") logger.debug(f"{translations['preprocess_effect']}: {preprocess_effects}") logger.debug(f"{translations['clear_audio']}: {clean_dataset}") if clean_dataset: logger.debug(f"{translations['clean_strength']}: {clean_strength}") try: preprocess_training_set(dataset, sample_rate, num_processes, experiment_directory, per, cut_preprocess, preprocess_effects, clean_dataset, clean_strength) except Exception as e: logger.error(f"{translations['process_audio_error']} {e}")
logger.info(f"{translations['preprocess_model_success']} {args.model_name}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400import os
import sys
import time
import logging
import argparse
import logging.handlers
import soundfile as sf
import noisereduce as nr
from pydub import AudioSegment
from distutils.util import strtobool
now_dir = os.getcwd()
sys.path.append(now_dir)
from main.configs.config import Config
from main.library.algorithm.separator import Separator
translations = Config().translations
log_file = os.path.join("assets", "logs", "separator.log")
logger = logging.getLogger(__name__)
if logger.hasHandlers(): logger.handlers.clear()
else:
console_handler = logging.StreamHandler()
console_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
console_handler.setFormatter(console_formatter)
console_handler.setLevel(logging.INFO)
file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=5*1024*1024, backupCount=3, encoding='utf-8')
file_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
file_handler.setFormatter(file_formatter)
file_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler)
logger.addHandler(file_handler)
logger.setLevel(logging.DEBUG)
demucs_models = {
"HT-Tuned": "htdemucs_ft.yaml",
"HT-Normal": "htdemucs.yaml",
"HD_MMI": "hdemucs_mmi.yaml",
"HT_6S": "htdemucs_6s.yaml"
}
mdx_models = {
"Main_340": "UVR-MDX-NET_Main_340.onnx",
"Main_390": "UVR-MDX-NET_Main_390.onnx",
"Main_406": "UVR-MDX-NET_Main_406.onnx",
"Main_427": "UVR-MDX-NET_Main_427.onnx",
"Main_438": "UVR-MDX-NET_Main_438.onnx",
"Inst_full_292": "UVR-MDX-NET-Inst_full_292.onnx",
"Inst_HQ_1": "UVR-MDX-NET_Inst_HQ_1.onnx",
"Inst_HQ_2": "UVR-MDX-NET_Inst_HQ_2.onnx",
"Inst_HQ_3": "UVR-MDX-NET_Inst_HQ_3.onnx",
"Inst_HQ_4": "UVR-MDX-NET-Inst_HQ_4.onnx",
"Kim_Vocal_1": "Kim_Vocal_1.onnx",
"Kim_Vocal_2": "Kim_Vocal_2.onnx",
"Kim_Inst": "Kim_Inst.onnx",
"Inst_187_beta": "UVR-MDX-NET_Inst_187_beta.onnx",
"Inst_82_beta": "UVR-MDX-NET_Inst_82_beta.onnx",
"Inst_90_beta": "UVR-MDX-NET_Inst_90_beta.onnx",
"Voc_FT": "UVR-MDX-NET-Voc_FT.onnx",
"Crowd_HQ": "UVR-MDX-NET_Crowd_HQ_1.onnx",
"MDXNET_9482": "UVR_MDXNET_9482.onnx",
"Inst_1": "UVR-MDX-NET-Inst_1.onnx",
"Inst_2": "UVR-MDX-NET-Inst_2.onnx",
"Inst_3": "UVR-MDX-NET-Inst_3.onnx",
"MDXNET_1_9703": "UVR_MDXNET_1_9703.onnx",
"MDXNET_2_9682": "UVR_MDXNET_2_9682.onnx",
"MDXNET_3_9662": "UVR_MDXNET_3_9662.onnx",
"Inst_Main": "UVR-MDX-NET-Inst_Main.onnx",
"MDXNET_Main": "UVR_MDXNET_Main.onnx"
}
kara_models = {
"Version-1": "UVR_MDXNET_KARA.onnx",
"Version-2": "UVR_MDXNET_KARA_2.onnx"
}
def parse_arguments() -> tuple:
parser = argparse.ArgumentParser()
parser.add_argument("--input_path", type=str, required=True)
parser.add_argument("--output_path", type=str, default="./audios")
parser.add_argument("--format", type=str, default="wav")
parser.add_argument("--shifts", type=int, default=10)
parser.add_argument("--segments_size", type=int, default=256)
parser.add_argument("--overlap", type=float, default=0.25)
parser.add_argument("--mdx_hop_length", type=int, default=1024)
parser.add_argument("--mdx_batch_size", type=int, default=1)
parser.add_argument("--clean_audio", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--clean_strength", type=float, default=0.7)
parser.add_argument("--backing_denoise", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--demucs_model", type=str, default="HT-Normal")
parser.add_argument("--kara_model", type=str, default="Version-1")
parser.add_argument("--mdx_model", type=str, default="Main_340")
parser.add_argument("--backing", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--mdx", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--mdx_denoise", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--reverb", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--reverb_denoise", type=lambda x: bool(strtobool(x)), default=False)
parser.add_argument("--backing_reverb", type=lambda x: bool(strtobool(x)), default=False)
args = parser.parse_args()
return args
def main():
start_time = time.time()
try:
args = parse_arguments()
input_path = args.input_path
output_path = args.output_path
export_format = args.format
shifts = args.shifts
segments_size = args.segments_size
overlap = args.overlap
hop_length = args.mdx_hop_length
batch_size = args.mdx_batch_size
clean_audio = args.clean_audio
clean_strength = args.clean_strength
backing_denoise = args.backing_denoise
demucs_model = args.demucs_model
kara_model = args.kara_model
backing = args.backing
mdx = args.mdx
mdx_model = args.mdx_model
mdx_denoise = args.mdx_denoise
reverb = args.reverb
reverb_denoise = args.reverb_denoise
backing_reverb = args.backing_reverb
if backing_reverb and not reverb:
logger.warning(translations["turn_on_dereverb"])
sys.exit(1)
if backing_reverb and not backing:
logger.warning(translations["turn_on_separator_backing"])
sys.exit(1)
logger.debug(f"{translations['audio_path']}: {input_path}")
logger.debug(f"{translations['output_path']}: {output_path}")
logger.debug(f"{translations['export_format']}: {export_format}")
if not mdx: logger.debug(f"{translations['shift']}: {shifts}")
logger.debug(f"{translations['segments_size']}: {segments_size}")
logger.debug(f"{translations['overlap']}: {overlap}")
if clean_audio: logger.debug(f"{translations['clear_audio']}: {clean_audio}")
if clean_audio: logger.debug(f"{translations['clean_strength']}: {clean_strength}")
if not mdx: logger.debug(f"{translations['demucs_model']}: {demucs_model}")
if backing: logger.debug(f"{translations['denoise_backing']}: {backing_denoise}")
if backing: logger.debug(f"{translations['backing_model_ver']}: {kara_model}")
if backing: logger.debug(f"{translations['separator_backing']}: {backing}")
if mdx: logger.debug(f"{translations['use_mdx']}: {mdx}")
if mdx: logger.debug(f"{translations['mdx_model']}: {mdx_model}")
if mdx: logger.debug(f"{translations['denoise_mdx']}: {mdx_denoise}")
if mdx or backing or reverb: logger.debug(f"Hop length: {hop_length}")
if mdx or backing or reverb: logger.debug(f"{translations['batch_size']}: {batch_size}")
if reverb: logger.debug(f"{translations['dereveb_audio']}: {reverb}")
if reverb: logger.debug(f"{translations['denoise_dereveb']}: {reverb_denoise}")
if reverb: logger.debug(f"{translations['dereveb_backing']}: {backing_reverb}")
if not mdx: vocals, instruments = separator_music_demucs(input_path, output_path, export_format, shifts, overlap, segments_size, demucs_model)
else: vocals, instruments = separator_music_mdx(input_path, output_path, export_format, segments_size, overlap, mdx_denoise, mdx_model, hop_length, batch_size)
if backing: main_vocals, backing_vocals = separator_backing(vocals, output_path, export_format, segments_size, overlap, backing_denoise, kara_model, hop_length, batch_size)
if reverb: vocals_no_reverb, main_vocals_no_reverb, backing_vocals_no_reverb = separator_reverb(output_path, export_format, segments_size, overlap, reverb_denoise, reverb, backing, backing_reverb, hop_length, batch_size)
original_output = os.path.join(output_path, f"Original_Vocals_No_Reverb.{export_format}") if reverb else os.path.join(output_path, f"Original_Vocals.{export_format}")
main_output = os.path.join(output_path, f"Main_Vocals_No_Reverb.{export_format}") if reverb and backing else os.path.join(output_path, f"Main_Vocals.{export_format}")
backing_output = os.path.join(output_path, f"Backing_Vocals_No_Reverb.{export_format}") if reverb and backing_reverb else os.path.join(output_path, f"Backing_Vocals.{export_format}")
if clean_audio:
logger.info(f"{translations['clear_audio']}...")
vocal_data, vocal_sr = sf.read(vocals_no_reverb if reverb else vocals)
main_data, main_sr = sf.read(main_vocals_no_reverb if reverb and backing else main_vocals)
backing_data, backing_sr = sf.read(backing_vocals_no_reverb if reverb and backing_reverb else backing_vocals)
vocals_clean = nr.reduce_noise(y=vocal_data, prop_decrease=clean_strength)
sf.write(original_output, vocals_clean, vocal_sr, format=export_format)
if backing:
mains_clean = nr.reduce_noise(y=main_data, sr=main_sr, prop_decrease=clean_strength)
backing_clean = nr.reduce_noise(y=backing_data, sr=backing_sr, prop_decrease=clean_strength)
sf.write(main_output, mains_clean, main_sr, format=export_format)
sf.write(backing_output, backing_clean, backing_sr, format=export_format)
logger.info(translations["clean_audio_success"])
return original_output, instruments, main_output, backing_output
except Exception as e:
logger.error(f"{translations['separator_error']}: {e}")
return None, None, None, None
elapsed_time = time.time() - start_time
logger.info(translations["separator_success"].format(elapsed_time=f"{elapsed_time:.2f}"))
def separator_music_demucs(input, output, format, shifts, overlap, segments_size, demucs_model):
if not os.path.exists(input):
logger.warning(translations["input_not_valid"])
sys.exit(1)
if not os.path.exists(output):
logger.warning(translations["output_not_valid"])
sys.exit(1)
for i in [f"Original_Vocals.{format}", f"Instruments.{format}"]:
if os.path.exists(os.path.join(output, i)): os.remove(os.path.join(output, i))
model = demucs_models.get(demucs_model)
segment_size = segments_size / 2
logger.info(f"{translations['separator_process_2']}...")
demucs_output = separator_main(audio_file=input, model_filename=model, output_format=format, output_dir=output, demucs_segment_size=segment_size, demucs_shifts=shifts, demucs_overlap=overlap)
for f in demucs_output:
path = os.path.join(output, f)
if not os.path.exists(path): logger.error(translations["not_found"].format(name=path))
if '_(Drums)_' in f: drums = path
elif '_(Bass)_' in f: bass = path
elif '_(Other)_' in f: other = path
elif '_(Vocals)_' in f: os.rename(path, os.path.join(output, f"Original_Vocals.{format}"))
AudioSegment.from_file(drums).overlay(AudioSegment.from_file(bass)).overlay(AudioSegment.from_file(other)).export(os.path.join(output, f"Instruments.{format}"), format=format)
for f in [drums, bass, other]:
if os.path.exists(f): os.remove(f)
logger.info(translations["separator_success_2"])
return os.path.join(output, f"Original_Vocals.{format}"), os.path.join(output, f"Instruments.{format}")
def separator_backing(input, output, format, segments_size, overlap, denoise, kara_model, hop_length, batch_size):
if not os.path.exists(input):
logger.warning(translations["input_not_valid"])
sys.exit(1)
if not os.path.exists(output):
logger.warning(translations["output_not_valid"])
sys.exit(1)
for f in [f"Main_Vocals.{format}", f"Backing_Vocals.{format}"]:
if os.path.exists(os.path.join(output, f)): os.remove(os.path.join(output, f))
model_2 = kara_models.get(kara_model)
logger.info(f"{translations['separator_process_backing']}...")
backing_outputs = separator_main(audio_file=input, model_filename=model_2, output_format=format, output_dir=output, mdx_segment_size=segments_size, mdx_overlap=overlap, mdx_batch_size=batch_size, mdx_hop_length=hop_length, mdx_enable_denoise=denoise)
main_output = os.path.join(output, f"Main_Vocals.{format}")
backing_output = os.path.join(output, f"Backing_Vocals.{format}")
for f in backing_outputs:
path = os.path.join(output, f)
if not os.path.exists(path): logger.error(translations["not_found"].format(name=path))
if '_(Instrumental)_' in f: os.rename(path, backing_output)
elif '_(Vocals)_' in f: os.rename(path, main_output)
logger.info(translations["separator_process_backing_success"])
return main_output, backing_output
def separator_music_mdx(input, output, format, segments_size, overlap, denoise, mdx_model, hop_length, batch_size):
if not os.path.exists(input):
logger.warning(translations["input_not_valid"])
sys.exit(1)
if not os.path.exists(output):
logger.warning(translations["output_not_valid"])
sys.exit(1)
for i in [f"Original_Vocals.{format}", f"Instruments.{format}"]:
if os.path.exists(os.path.join(output, i)): os.remove(os.path.join(output, i))
model_3 = mdx_models.get(mdx_model)
logger.info(f"{translations['separator_process_2']}...")
output_music = separator_main(audio_file=input, model_filename=model_3, output_format=format, output_dir=output, mdx_segment_size=segments_size, mdx_overlap=overlap, mdx_batch_size=batch_size, mdx_hop_length=hop_length, mdx_enable_denoise=denoise)
original_output = os.path.join(output, f"Original_Vocals.{format}")
instruments_output = os.path.join(output, f"Instruments.{format}")
for f in output_music:
path = os.path.join(output, f)
if not os.path.exists(path): logger.error(translations["not_found"].format(name=path))
if '_(Instrumental)_' in f: os.rename(path, instruments_output)
elif '_(Vocals)_' in f: os.rename(path, original_output)
logger.info(translations["separator_process_backing_success"])
return original_output, instruments_output
def separator_reverb(output, format, segments_size, overlap, denoise, original, main, backing_reverb, hop_length, batch_size):
if not os.path.exists(output):
logger.warning(translations["output_not_valid"])
sys.exit(1)
for i in [f"Original_Vocals_Reverb.{format}", f"Main_Vocals_Reverb.{format}", f"Original_Vocals_No_Reverb.{format}", f"Main_Vocals_No_Reverb.{format}"]:
if os.path.exists(os.path.join(output, i)): os.remove(os.path.join(output, i))
dereveb_path = []
if original:
try:
dereveb_path.append(os.path.join(output, [f for f in os.listdir(output) if 'Original_Vocals' in f][0]))
except IndexError:
logger.warning(translations["not_found_original_vocal"])
sys.exit(1)
if main:
try:
dereveb_path.append(os.path.join(output, [f for f in os.listdir(output) if 'Main_Vocals' in f][0]))
except IndexError:
logger.warning(translations["not_found_main_vocal"])
sys.exit(1)
if backing_reverb:
try:
dereveb_path.append(os.path.join(output, [f for f in os.listdir(output) if 'Backing_Vocals' in f][0]))
except IndexError:
logger.warning(translations["not_found_backing_vocal"])
sys.exit(1)
for path in dereveb_path:
if not os.path.exists(path):
logger.warning(translations["not_found"].format(name=path))
sys.exit(1)
if "Original_Vocals" in path:
reverb_path = os.path.join(output, f"Original_Vocals_Reverb.{format}")
no_reverb_path = os.path.join(output, f"Original_Vocals_No_Reverb.{format}")
start_title = translations["process_original"]
end_title = translations["process_original_success"]
elif "Main_Vocals" in path:
reverb_path = os.path.join(output, f"Main_Vocals_Reverb.{format}")
no_reverb_path = os.path.join(output, f"Main_Vocals_No_Reverb.{format}")
start_title = translations["process_main"]
end_title = translations["process_main_success"]
elif "Backing_Vocals" in path:
reverb_path = os.path.join(output, f"Backing_Vocals_Reverb.{format}")
no_reverb_path = os.path.join(output, f"Backing_Vocals_No_Reverb.{format}")
start_title = translations["process_backing"]
end_title = translations["process_backing_success"]
logger.info(start_title)
output_dereveb = separator_main(audio_file=path, model_filename="Reverb_HQ_By_FoxJoy.onnx", output_format=format, output_dir=output, mdx_segment_size=segments_size, mdx_overlap=overlap, mdx_batch_size=batch_size, mdx_hop_length=hop_length, mdx_enable_denoise=denoise)
for f in output_dereveb:
path = os.path.join(output, f)
if not os.path.exists(path): logger.error(translations["not_found"].format(name=path))
if '_(Reverb)_' in f: os.rename(path, reverb_path)
elif '_(No Reverb)_' in f: os.rename(path, no_reverb_path)
logger.info(end_title)
return (os.path.join(output, f"Original_Vocals_No_Reverb.{format}") if original else None), (os.path.join(output, f"Main_Vocals_No_Reverb.{format}") if main else None), (os.path.join(output, f"Backing_Vocals_No_Reverb.{format}") if backing_reverb else None)
def separator_main(audio_file=None, model_filename="UVR-MDX-NET_Main_340.onnx", output_format="wav", output_dir=".", mdx_segment_size=256, mdx_overlap=0.25, mdx_batch_size=1, mdx_hop_length=1024, mdx_enable_denoise=True, demucs_segment_size=256, demucs_shifts=2, demucs_overlap=0.25):
separator = Separator(
log_formatter=file_formatter,
log_level=logging.INFO,
output_dir=output_dir,
output_format=output_format,
output_bitrate=None,
normalization_threshold=0.9,
output_single_stem=None,
invert_using_spec=False,
sample_rate=44100,
mdx_params={
"hop_length": mdx_hop_length,
"segment_size": mdx_segment_size,
"overlap": mdx_overlap,
"batch_size": mdx_batch_size,
"enable_denoise": mdx_enable_denoise,
},
demucs_params={
"segment_size": demucs_segment_size,
"shifts": demucs_shifts,
"overlap": demucs_overlap,
"segments_enabled": True,
}
)
separator.load_model(model_filename=model_filename)
return separator.separate(audio_file)
if __name__ == "__main__": main()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600import osimport reimport sysimport globimport jsonimport torchimport loggingimport hashlibimport argparseimport datetimeimport warningsimport logging.handlers
import numpy as npimport torch.utils.dataimport matplotlib.pyplot as pltimport torch.distributed as distimport torch.multiprocessing as mp
from tqdm import tqdmfrom time import time as ttimefrom scipy.io.wavfile import readfrom collections import OrderedDictfrom random import randint, shuffle from torch.nn import functional as Ffrom distutils.util import strtoboolfrom torch.utils.data import DataLoaderfrom torch.cuda.amp import GradScaler, autocastfrom torch.utils.tensorboard import SummaryWriterfrom librosa.filters import mel as librosa_mel_fnfrom torch.nn.parallel import DistributedDataParallel as DDPfrom torch.nn.utils.parametrizations import spectral_norm, weight_norm
current_dir = os.getcwd()sys.path.append(current_dir)
from main.configs.config import Configfrom main.library.algorithm.residuals import LRELU_SLOPEfrom main.library.algorithm.synthesizers import Synthesizerfrom main.library.algorithm.commons import get_padding, slice_segments, clip_grad_value
warnings.filterwarnings("ignore", category=FutureWarning)warnings.filterwarnings("ignore", category=UserWarning)
logging.getLogger("torch").setLevel(logging.ERROR)
MATPLOTLIB_FLAG = False
translations = Config().translations
class HParams: def __init__(self, **kwargs): for k, v in kwargs.items(): self[k] = HParams(**v) if isinstance(v, dict) else v
def keys(self): return self.__dict__.keys()
def items(self): return self.__dict__.items()
def values(self): return self.__dict__.values()
def __len__(self): return len(self.__dict__)
def __getitem__(self, key): return self.__dict__[key]
def __setitem__(self, key, value): self.__dict__[key] = value
def __contains__(self, key): return key in self.__dict__
def __repr__(self): return repr(self.__dict__)
def parse_arguments() -> tuple: parser = argparse.ArgumentParser() parser.add_argument("--model_name", type=str, required=True) parser.add_argument("--rvc_version", type=str, default="v2") parser.add_argument("--save_every_epoch", type=int, required=True) parser.add_argument("--save_only_latest", type=lambda x: bool(strtobool(x)), default=True) parser.add_argument("--save_every_weights", type=lambda x: bool(strtobool(x)), default=True) parser.add_argument("--total_epoch", type=int, default=300) parser.add_argument("--sample_rate", type=int, required=True) parser.add_argument("--batch_size", type=int, default=8) parser.add_argument("--gpu", type=str, default="0") parser.add_argument("--pitch_guidance", type=lambda x: bool(strtobool(x)), default=True) parser.add_argument("--g_pretrained_path", type=str, default="") parser.add_argument("--d_pretrained_path", type=str, default="") parser.add_argument("--overtraining_detector", type=lambda x: bool(strtobool(x)), default=False) parser.add_argument("--overtraining_threshold", type=int, default=50) parser.add_argument("--sync_graph", type=lambda x: bool(strtobool(x)), default=False) parser.add_argument("--cache_data_in_gpu", type=lambda x: bool(strtobool(x)), default=False) parser.add_argument("--model_author", type=str)
args = parser.parse_args() return args
args = parse_arguments()
model_name = args.model_namesave_every_epoch = args.save_every_epochtotal_epoch = args.total_epochpretrainG = args.g_pretrained_pathpretrainD = args.d_pretrained_pathversion = args.rvc_versiongpus = args.gpubatch_size = args.batch_sizesample_rate = args.sample_ratepitch_guidance = args.pitch_guidancesave_only_latest = args.save_only_latestsave_every_weights = args.save_every_weightscache_data_in_gpu = args.cache_data_in_gpuovertraining_detector = args.overtraining_detectorovertraining_threshold = args.overtraining_thresholdsync_graph = args.sync_graphmodel_author = args.model_author
experiment_dir = os.path.join(current_dir, "assets", "logs", model_name)config_save_path = os.path.join(experiment_dir, "config.json")
os.environ["CUDA_VISIBLE_DEVICES"] = gpus.replace("-", ",")n_gpus = len(gpus.split("-"))
torch.backends.cudnn.deterministic = Falsetorch.backends.cudnn.benchmark = False
global_step = 0last_loss_gen_all = 0overtrain_save_epoch = 0
loss_gen_history = []smoothed_loss_gen_history = []loss_disc_history = []smoothed_loss_disc_history = []
lowest_value = {"step": 0, "value": float("inf"), "epoch": 0}training_file_path = os.path.join(experiment_dir, "training_data.json")
with open(config_save_path, "r") as f: config = json.load(f)
config = HParams(**config)config.data.training_files = os.path.join(experiment_dir, "filelist.txt")
log_file = os.path.join(experiment_dir, "train.log")
logger = logging.getLogger(__name__)
if logger.hasHandlers(): logger.handlers.clear()else: console_handler = logging.StreamHandler() console_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
console_handler.setFormatter(console_formatter) console_handler.setLevel(logging.INFO)
file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=5*1024*1024, backupCount=3, encoding='utf-8') file_formatter = logging.Formatter(fmt="\n%(asctime)s.%(msecs)03d | %(levelname)s | %(module)s | %(message)s", datefmt="%Y-%m-%d %H:%M:%S")
file_handler.setFormatter(file_formatter) file_handler.setLevel(logging.DEBUG)
logger.addHandler(console_handler) logger.addHandler(file_handler) logger.setLevel(logging.DEBUG)
logger.debug(f"{translations['modelname']}: {model_name}")logger.debug(translations["save_every_epoch"].format(save_every_epoch=save_every_epoch))logger.debug(translations["total_e"].format(total_epoch=total_epoch))logger.debug(translations["dorg"].format(pretrainG=pretrainG, pretrainD=pretrainD))logger.debug(f"{translations['training_version']}: {version}")logger.debug(f"Gpu: {gpus}")logger.debug(f"{translations['batch_size']}: {batch_size}")logger.debug(f"{translations['pretrain_sr']}: {sample_rate}")logger.debug(f"{translations['training_f0']}: {pitch_guidance}")logger.debug(f"{translations['save_only_latest']}: {save_only_latest}")logger.debug(f"{translations['save_every_weights']}: {save_every_weights}")logger.debug(f"{translations['cache_in_gpu']}: {cache_data_in_gpu}")logger.debug(f"{translations['overtraining_detector']}: {overtraining_detector}")logger.debug(f"{translations['threshold']}: {overtraining_threshold}")logger.debug(f"{translations['sync_graph']}: {sync_graph}")if not model_author: logger.debug(translations["model_author"].format(model_author=model_author))
def main(): global training_file_path, last_loss_gen_all, smoothed_loss_gen_history, loss_gen_history, loss_disc_history, smoothed_loss_disc_history, overtrain_save_epoch, model_author os.environ["MASTER_ADDR"] = "localhost" os.environ["MASTER_PORT"] = str(randint(20000, 55555))
if torch.cuda.is_available(): device = torch.device("cuda") n_gpus = torch.cuda.device_count() elif torch.backends.mps.is_available(): device = torch.device("mps") n_gpus = 1 else: device = torch.device("cpu") n_gpus = 1
def start(): children = []
for i in range(n_gpus): subproc = mp.Process(target=run, args=(i, n_gpus, experiment_dir, pretrainG, pretrainD, pitch_guidance, custom_total_epoch, custom_save_every_weights, config, device, model_author)) children.append(subproc) subproc.start()
for i in range(n_gpus): children[i].join()
def load_from_json(file_path): if os.path.exists(file_path): with open(file_path, "r") as f: data = json.load(f)
return ( data.get("loss_disc_history", []), data.get("smoothed_loss_disc_history", []), data.get("loss_gen_history", []), data.get("smoothed_loss_gen_history", []), ) return [], [], [], []
def continue_overtrain_detector(training_file_path): if overtraining_detector: if os.path.exists(training_file_path): ( loss_disc_history, smoothed_loss_disc_history, loss_gen_history, smoothed_loss_gen_history, ) = load_from_json(training_file_path)
n_gpus = torch.cuda.device_count()
if not torch.cuda.is_available() and torch.backends.mps.is_available(): n_gpus = 1
if n_gpus < 1: logger.warning(translations["not_gpu"]) n_gpus = 1
if sync_graph: logger.debug(translations["sync"]) custom_total_epoch = 1 custom_save_every_weights = True
start()
model_config_file = os.path.join(experiment_dir, "config.json") rvc_config_file = os.path.join(current_dir, "main", "configs", version, str(sample_rate) + ".json")
if not os.path.exists(rvc_config_file): rvc_config_file = os.path.join(current_dir, "main", "configs", "v1", str(sample_rate) + ".json")
pattern = rf"{os.path.basename(model_name)}_(\d+)e_(\d+)s\.pth"
for filename in os.listdir(os.path.join("assets", "weights")): match = re.match(pattern, filename)
if match: steps = int(match.group(2))
def edit_config(config_file): with open(config_file, "r", encoding="utf8") as json_file: config_data = json.load(json_file)
config_data["train"]["log_interval"] = steps
with open(config_file, "w", encoding="utf8") as json_file: json.dump(config_data, json_file, indent=2, separators=(",", ": "), ensure_ascii=False)
edit_config(model_config_file) edit_config(rvc_config_file)
for root, dirs, files in os.walk(experiment_dir, topdown=False): for name in files: file_path = os.path.join(root, name) _, file_extension = os.path.splitext(name)
if file_extension == ".0": os.remove(file_path) elif ("D" in name or "G" in name) and file_extension == ".pth": os.remove(file_path) elif ("added" in name or "trained" in name) and file_extension == ".index": os.remove(file_path)
for name in dirs: if name == "eval": folder_path = os.path.join(root, name)
for item in os.listdir(folder_path): item_path = os.path.join(folder_path, item) if os.path.isfile(item_path): os.remove(item_path)
os.rmdir(folder_path)
logger.info(translations["sync_success"]) custom_total_epoch = total_epoch custom_save_every_weights = save_every_weights
continue_overtrain_detector(training_file_path) start() else: custom_total_epoch = total_epoch custom_save_every_weights = save_every_weights
continue_overtrain_detector(training_file_path) start()
def plot_spectrogram_to_numpy(spectrogram): global MATPLOTLIB_FLAG
if not MATPLOTLIB_FLAG: plt.switch_backend("Agg") MATPLOTLIB_FLAG = True
fig, ax = plt.subplots(figsize=(10, 2)) im = ax.imshow(spectrogram, aspect="auto", origin="lower", interpolation="none")
plt.colorbar(im, ax=ax) plt.xlabel("Frames") plt.ylabel("Channels") plt.tight_layout()
fig.canvas.draw() data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8) data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,)) plt.close(fig) return data
def summarize(writer, global_step, scalars={}, histograms={}, images={}, audios={}, audio_sample_rate=22050): for k, v in scalars.items(): writer.add_scalar(k, v, global_step)
for k, v in histograms.items(): writer.add_histogram(k, v, global_step)
for k, v in images.items(): writer.add_image(k, v, global_step, dataformats="HWC")
for k, v in audios.items(): writer.add_audio(k, v, global_step, audio_sample_rate)
def load_checkpoint(checkpoint_path, model, optimizer=None, load_opt=1): assert os.path.isfile(checkpoint_path), translations["not_found_checkpoint"].format(checkpoint_path=checkpoint_path)
checkpoint_dict = torch.load(checkpoint_path, map_location="cpu") checkpoint_dict = replace_keys_in_dict(replace_keys_in_dict(checkpoint_dict, ".weight_v", ".parametrizations.weight.original1"), ".weight_g", ".parametrizations.weight.original0")
model_state_dict = (model.module.state_dict() if hasattr(model, "module") else model.state_dict()) new_state_dict = {k: checkpoint_dict["model"].get(k, v) for k, v in model_state_dict.items()}
if hasattr(model, "module"): model.module.load_state_dict(new_state_dict, strict=False) else: model.load_state_dict(new_state_dict, strict=False)
if optimizer and load_opt == 1: optimizer.load_state_dict(checkpoint_dict.get("optimizer", {}))
logger.debug(translations["save_checkpoint"].format(checkpoint_path=checkpoint_path, checkpoint_dict=checkpoint_dict['iteration']))
return ( model, optimizer, checkpoint_dict.get("learning_rate", 0), checkpoint_dict["iteration"], )
def save_checkpoint(model, optimizer, learning_rate, iteration, checkpoint_path): state_dict = (model.module.state_dict() if hasattr(model, "module") else model.state_dict())
checkpoint_data = { "model": state_dict, "iteration": iteration, "optimizer": optimizer.state_dict(), "learning_rate": learning_rate, }
torch.save(checkpoint_data, checkpoint_path)
old_version_path = checkpoint_path.replace(".pth", "_old_version.pth") checkpoint_data = replace_keys_in_dict(replace_keys_in_dict(checkpoint_data, ".parametrizations.weight.original1", ".weight_v"), ".parametrizations.weight.original0", ".weight_g")
torch.save(checkpoint_data, old_version_path)
os.replace(old_version_path, checkpoint_path) logger.info(translations["save_model"].format(checkpoint_path=checkpoint_path, iteration=iteration))
def latest_checkpoint_path(dir_path, regex="G_*.pth"): checkpoints = sorted(glob.glob(os.path.join(dir_path, regex)), key=lambda f: int("".join(filter(str.isdigit, f))))
return checkpoints[-1] if checkpoints else None
def load_wav_to_torch(full_path): sample_rate, data = read(full_path) return torch.FloatTensor(data.astype(np.float32)), sample_rate
def load_filepaths_and_text(filename, split="|"): with open(filename, encoding="utf-8") as f: return [line.strip().split(split) for line in f]
def feature_loss(fmap_r, fmap_g): loss = 0
for dr, dg in zip(fmap_r, fmap_g): for rl, gl in zip(dr, dg): rl = rl.float().detach() gl = gl.float() loss += torch.mean(torch.abs(rl - gl))
return loss * 2
def discriminator_loss(disc_real_outputs, disc_generated_outputs): loss = 0 r_losses = [] g_losses = []
for dr, dg in zip(disc_real_outputs, disc_generated_outputs): dr = dr.float() dg = dg.float() r_loss = torch.mean((1 - dr) ** 2) g_loss = torch.mean(dg**2) loss += r_loss + g_loss r_losses.append(r_loss.item()) g_losses.append(g_loss.item())
return loss, r_losses, g_losses
def generator_loss(disc_outputs): loss = 0 gen_losses = []
for dg in disc_outputs: dg = dg.float() l = torch.mean((1 - dg) ** 2) gen_losses.append(l) loss += l
return loss, gen_losses
def kl_loss(z_p, logs_q, m_p, logs_p, z_mask): z_p = z_p.float() logs_q = logs_q.float() m_p = m_p.float() logs_p = logs_p.float() z_mask = z_mask.float()
kl = logs_p - logs_q - 0.5 kl += 0.5 * ((z_p - m_p) ** 2) * torch.exp(-2.0 * logs_p) kl = torch.sum(kl * z_mask) l = kl / torch.sum(z_mask)
return l
class TextAudioLoaderMultiNSFsid(torch.utils.data.Dataset): def __init__(self, hparams): self.audiopaths_and_text = load_filepaths_and_text(hparams.training_files) self.max_wav_value = hparams.max_wav_value self.sample_rate = hparams.sample_rate self.filter_length = hparams.filter_length self.hop_length = hparams.hop_length self.win_length = hparams.win_length self.sample_rate = hparams.sample_rate self.min_text_len = getattr(hparams, "min_text_len", 1) self.max_text_len = getattr(hparams, "max_text_len", 5000) self._filter()
def _filter(self): audiopaths_and_text_new = [] lengths = []
for audiopath, text, pitch, pitchf, dv in self.audiopaths_and_text: if self.min_text_len <= len(text) and len(text) <= self.max_text_len: audiopaths_and_text_new.append([audiopath, text, pitch, pitchf, dv]) lengths.append(os.path.getsize(audiopath) // (3 * self.hop_length))
self.audiopaths_and_text = audiopaths_and_text_new self.lengths = lengths
def get_sid(self, sid): try: sid = torch.LongTensor([int(sid)]) except ValueError as e: logger.error(translations["sid_error"].format(sid=sid, e=e)) sid = torch.LongTensor([0])
return sid
def get_audio_text_pair(self, audiopath_and_text): file = audiopath_and_text[0] phone = audiopath_and_text[1] pitch = audiopath_and_text[2] pitchf = audiopath_and_text[3] dv = audiopath_and_text[4]
phone, pitch, pitchf = self.get_labels(phone, pitch, pitchf) spec, wav = self.get_audio(file) dv = self.get_sid(dv)
len_phone = phone.size()[0] len_spec = spec.size()[-1] if len_phone != len_spec: len_min = min(len_phone, len_spec) len_wav = len_min * self.hop_length
spec = spec[:, :len_min] wav = wav[:, :len_wav]
phone = phone[:len_min, :] pitch = pitch[:len_min] pitchf = pitchf[:len_min]
return (spec, wav, phone, pitch, pitchf, dv)
def get_labels(self, phone, pitch, pitchf): phone = np.load(phone) phone = np.repeat(phone, 2, axis=0)
pitch = np.load(pitch) pitchf = np.load(pitchf)
n_num = min(phone.shape[0], 900) phone = phone[:n_num, :]
pitch = pitch[:n_num] pitchf = pitchf[:n_num]
phone = torch.FloatTensor(phone)
pitch = torch.LongTensor(pitch) pitchf = torch.FloatTensor(pitchf)
return phone, pitch, pitchf
def get_audio(self, filename): audio, sample_rate = load_wav_to_torch(filename)
if sample_rate != self.sample_rate: raise ValueError(translations["sr_does_not_match"].format(sample_rate=sample_rate, sample_rate2=self.sample_rate))
audio_norm = audio audio_norm = audio_norm.unsqueeze(0) spec_filename = filename.replace(".wav", ".spec.pt")
if os.path.exists(spec_filename): try: spec = torch.load(spec_filename) except Exception as e: logger.error(translations["spec_error"].format(spec_filename=spec_filename, e=e)) spec = spectrogram_torch( audio_norm, self.filter_length, self.hop_length, self.win_length, center=False, ) spec = torch.squeeze(spec, 0)
torch.save(spec, spec_filename, _use_new_zipfile_serialization=False) else: spec = spectrogram_torch( audio_norm, self.filter_length, self.hop_length, self.win_length, center=False, ) spec = torch.squeeze(spec, 0)
torch.save(spec, spec_filename, _use_new_zipfile_serialization=False) return spec, audio_norm
def __getitem__(self, index): return self.get_audio_text_pair(self.audiopaths_and_text[index])
def __len__(self): return len(self.audiopaths_and_text)
class TextAudioCollateMultiNSFsid: def __init__(self, return_ids=False): self.return_ids = return_ids
def __call__(self, batch): _, ids_sorted_decreasing = torch.sort(torch.LongTensor([x[0].size(1) for x in batch]), dim=0, descending=True)
max_spec_len = max([x[0].size(1) for x in batch]) max_wave_len = max([x[1].size(1) for x in batch])
spec_lengths = torch.LongTensor(len(batch)) wave_lengths = torch.LongTensor(len(batch)) spec_padded = torch.FloatTensor(len(batch), batch[0][0].size(0), max_spec_len) wave_padded = torch.FloatTensor(len(batch), 1, max_wave_len)
spec_padded.zero_() wave_padded.zero_()
max_phone_len = max([x[2].size(0) for x in batch])
phone_lengths = torch.LongTensor(len(batch)) phone_padded = torch.FloatTensor(len(batch), max_phone_len, batch[0][2].shape[1]) pitch_padded = torch.LongTensor(len(batch), max_phone_len) pitchf_padded = torch.FloatTensor(len(batch), max_phone_len)
phone_padded.zero_() pitch_padded.zero_() pitchf_padded.zero_() sid = torch.LongTensor(len(batch))
for i in range(len(ids_sorted_decreasing)): row = batch[ids_sorted_decreasing[i]]
spec = row[0] spec_padded[i, :, : spec.size(1)] = spec spec_lengths[i] = spec.size(1)
wave = row[1] wave_padded[i, :, : wave.size(1)] = wave wave_lengths[i] = wave.size(1)
phone = row[2] phone_padded[i, : phone.size(0), :] = phone phone_lengths[i] = phone.size(0)
pitch = row[3] pitch_padded[i, : pitch.size(0)] = pitch pitchf = row[4] pitchf_padded[i, : pitchf.size(0)] = pitchf
sid[i] = row[5]
return ( phone_padded, phone_lengths, pitch_padded, pitchf_padded, spec_padded, spec_lengths, wave_padded, wave_lengths, sid, )
class TextAudioLoader(torch.utils.data.Dataset): def __init__(self, hparams): self.audiopaths_and_text = load_filepaths_and_text(hparams.training_files) self.max_wav_value = hparams.max_wav_value self.sample_rate = hparams.sample_rate self.filter_length = hparams.filter_length self.hop_length = hparams.hop_length self.win_length = hparams.win_length self.sample_rate = hparams.sample_rate self.min_text_len = getattr(hparams, "min_text_len", 1) self.max_text_len = getattr(hparams, "max_text_len", 5000) self._filter()
def _filter(self): audiopaths_and_text_new = [] lengths = []
for entry in self.audiopaths_and_text: if len(entry) >= 3: audiopath, text, dv = entry[:3]
if self.min_text_len <= len(text) and len(text) <= self.max_text_len: audiopaths_and_text_new.append([audiopath, text, dv]) lengths.append(os.path.getsize(audiopath) // (3 * self.hop_length))
self.audiopaths_and_text = audiopaths_and_text_new self.lengths = lengths
def get_sid(self, sid): try: sid = torch.LongTensor([int(sid)]) except ValueError as e: logger.error(translations["sid_error"].format(sid=sid, e=e)) sid = torch.LongTensor([0])
return sid
def get_audio_text_pair(self, audiopath_and_text): file = audiopath_and_text[0] phone = audiopath_and_text[1] dv = audiopath_and_text[2]
phone = self.get_labels(phone) spec, wav = self.get_audio(file) dv = self.get_sid(dv)
len_phone = phone.size()[0] len_spec = spec.size()[-1]
if len_phone != len_spec: len_min = min(len_phone, len_spec) len_wav = len_min * self.hop_length spec = spec[:, :len_min] wav = wav[:, :len_wav] phone = phone[:len_min, :]
return (spec, wav, phone, dv)
def get_labels(self, phone): phone = np.load(phone) phone = np.repeat(phone, 2, axis=0) n_num = min(phone.shape[0], 900) phone = phone[:n_num, :] phone = torch.FloatTensor(phone) return phone
def get_audio(self, filename): audio, sample_rate = load_wav_to_torch(filename)
if sample_rate != self.sample_rate: raise ValueError(translations["sr_does_not_match"].format(sample_rate=sample_rate, sample_rate2=self.sample_rate))
audio_norm = audio audio_norm = audio_norm.unsqueeze(0)
spec_filename = filename.replace(".wav", ".spec.pt")
if os.path.exists(spec_filename): try: spec = torch.load(spec_filename) except Exception as e: logger.error(translations["spec_error"].format(spec_filename=spec_filename, e=e)) spec = spectrogram_torch( audio_norm, self.filter_length, self.hop_length, self.win_length, center=False, ) spec = torch.squeeze(spec, 0)
torch.save(spec, spec_filename, _use_new_zipfile_serialization=False) else: spec = spectrogram_torch( audio_norm, self.filter_length, self.hop_length, self.win_length, center=False, ) spec = torch.squeeze(spec, 0)
torch.save(spec, spec_filename, _use_new_zipfile_serialization=False) return spec, audio_norm
def __getitem__(self, index): return self.get_audio_text_pair(self.audiopaths_and_text[index])
def __len__(self): return len(self.audiopaths_and_text)
class TextAudioCollate: def __init__(self, return_ids=False): self.return_ids = return_ids
def __call__(self, batch): _, ids_sorted_decreasing = torch.sort(torch.LongTensor([x[0].size(1) for x in batch]), dim=0, descending=True)
max_spec_len = max([x[0].size(1) for x in batch]) max_wave_len = max([x[1].size(1) for x in batch])
spec_lengths = torch.LongTensor(len(batch)) wave_lengths = torch.LongTensor(len(batch))
spec_padded = torch.FloatTensor(len(batch), batch[0][0].size(0), max_spec_len) wave_padded = torch.FloatTensor(len(batch), 1, max_wave_len)
spec_padded.zero_() wave_padded.zero_()
max_phone_len = max([x[2].size(0) for x in batch])
phone_lengths = torch.LongTensor(len(batch)) phone_padded = torch.FloatTensor(len(batch), max_phone_len, batch[0][2].shape[1])
phone_padded.zero_() sid = torch.LongTensor(len(batch))
for i in range(len(ids_sorted_decreasing)): row = batch[ids_sorted_decreasing[i]]
spec = row[0] spec_padded[i, :, : spec.size(1)] = spec spec_lengths[i] = spec.size(1)
wave = row[1] wave_padded[i, :, : wave.size(1)] = wave wave_lengths[i] = wave.size(1)
phone = row[2] phone_padded[i, : phone.size(0), :] = phone phone_lengths[i] = phone.size(0)
sid[i] = row[3]
return ( phone_padded, phone_lengths, spec_padded, spec_lengths, wave_padded, wave_lengths, sid, )
class DistributedBucketSampler(torch.utils.data.distributed.DistributedSampler): def __init__(self, dataset, batch_size, boundaries, num_replicas=None, rank=None, shuffle=True): super().__init__(dataset, num_replicas=num_replicas, rank=rank, shuffle=shuffle) self.lengths = dataset.lengths self.batch_size = batch_size self.boundaries = boundaries self.buckets, self.num_samples_per_bucket = self._create_buckets() self.total_size = sum(self.num_samples_per_bucket) self.num_samples = self.total_size // self.num_replicas
def _create_buckets(self): buckets = [[] for _ in range(len(self.boundaries) - 1)]
for i in range(len(self.lengths)): length = self.lengths[i] idx_bucket = self._bisect(length) if idx_bucket != -1: buckets[idx_bucket].append(i)
for i in range(len(buckets) - 1, -1, -1): if len(buckets[i]) == 0: buckets.pop(i) self.boundaries.pop(i + 1)
num_samples_per_bucket = []
for i in range(len(buckets)): len_bucket = len(buckets[i]) total_batch_size = self.num_replicas * self.batch_size rem = (total_batch_size - (len_bucket % total_batch_size)) % total_batch_size num_samples_per_bucket.append(len_bucket + rem)
return buckets, num_samples_per_bucket
def __iter__(self): g = torch.Generator() g.manual_seed(self.epoch)
indices = []
if self.shuffle: for bucket in self.buckets: indices.append(torch.randperm(len(bucket), generator=g).tolist()) else: for bucket in self.buckets: indices.append(list(range(len(bucket))))
batches = []
for i in range(len(self.buckets)): bucket = self.buckets[i] len_bucket = len(bucket) ids_bucket = indices[i] num_samples_bucket = self.num_samples_per_bucket[i]
rem = num_samples_bucket - len_bucket
ids_bucket = (ids_bucket + ids_bucket * (rem // len_bucket) + ids_bucket[: (rem % len_bucket)]) ids_bucket = ids_bucket[self.rank :: self.num_replicas]
for j in range(len(ids_bucket) // self.batch_size): batch = [bucket[idx] for idx in ids_bucket[j * self.batch_size : (j + 1) * self.batch_size]] batches.append(batch)
if self.shuffle: batch_ids = torch.randperm(len(batches), generator=g).tolist() batches = [batches[i] for i in batch_ids]
self.batches = batches
assert len(self.batches) * self.batch_size == self.num_samples return iter(self.batches)
def _bisect(self, x, lo=0, hi=None): if hi is None: hi = len(self.boundaries) - 1
if hi > lo: mid = (hi + lo) // 2
if self.boundaries[mid] < x and x <= self.boundaries[mid + 1]: return mid elif x <= self.boundaries[mid]: return self._bisect(x, lo, mid) else: return self._bisect(x, mid + 1, hi)
else: return -1
def __len__(self): return self.num_samples // self.batch_size
class MultiPeriodDiscriminator(torch.nn.Module): def __init__(self, use_spectral_norm=False): super(MultiPeriodDiscriminator, self).__init__() periods = [2, 3, 5, 7, 11, 17] self.discriminators = torch.nn.ModuleList([DiscriminatorS(use_spectral_norm=use_spectral_norm)] + [DiscriminatorP(p, use_spectral_norm=use_spectral_norm) for p in periods])
def forward(self, y, y_hat): y_d_rs, y_d_gs, fmap_rs, fmap_gs = [], [], [], []
for d in self.discriminators: y_d_r, fmap_r = d(y) y_d_g, fmap_g = d(y_hat) y_d_rs.append(y_d_r) y_d_gs.append(y_d_g) fmap_rs.append(fmap_r) fmap_gs.append(fmap_g)
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
class MultiPeriodDiscriminatorV2(torch.nn.Module): def __init__(self, use_spectral_norm=False): super(MultiPeriodDiscriminatorV2, self).__init__() periods = [2, 3, 5, 7, 11, 17, 23, 37] self.discriminators = torch.nn.ModuleList([DiscriminatorS(use_spectral_norm=use_spectral_norm)] + [DiscriminatorP(p, use_spectral_norm=use_spectral_norm) for p in periods])
def forward(self, y, y_hat): y_d_rs, y_d_gs, fmap_rs, fmap_gs = [], [], [], []
for d in self.discriminators: y_d_r, fmap_r = d(y) y_d_g, fmap_g = d(y_hat) y_d_rs.append(y_d_r) y_d_gs.append(y_d_g) fmap_rs.append(fmap_r) fmap_gs.append(fmap_g)
return y_d_rs, y_d_gs, fmap_rs, fmap_gs
class DiscriminatorS(torch.nn.Module): def __init__(self, use_spectral_norm=False): super(DiscriminatorS, self).__init__() norm_f = spectral_norm if use_spectral_norm else weight_norm
self.convs = torch.nn.ModuleList([norm_f(torch.nn.Conv1d(1, 16, 15, 1, padding=7)), norm_f(torch.nn.Conv1d(16, 64, 41, 4, groups=4, padding=20)), norm_f(torch.nn.Conv1d(64, 256, 41, 4, groups=16, padding=20)), norm_f(torch.nn.Conv1d(256, 1024, 41, 4, groups=64, padding=20)), norm_f(torch.nn.Conv1d(1024, 1024, 41, 4, groups=256, padding=20)), norm_f(torch.nn.Conv1d(1024, 1024, 5, 1, padding=2))]) self.conv_post = norm_f(torch.nn.Conv1d(1024, 1, 3, 1, padding=1)) self.lrelu = torch.nn.LeakyReLU(LRELU_SLOPE)
def forward(self, x): fmap = []
for conv in self.convs: x = self.lrelu(conv(x)) fmap.append(x)
x = self.conv_post(x) fmap.append(x) x = torch.flatten(x, 1, -1)
return x, fmap
class DiscriminatorP(torch.nn.Module): def __init__(self, period, kernel_size=5, stride=3, use_spectral_norm=False): super(DiscriminatorP, self).__init__() self.period = period norm_f = spectral_norm if use_spectral_norm else weight_norm
in_channels = [1, 32, 128, 512, 1024] out_channels = [32, 128, 512, 1024, 1024]
self.convs = torch.nn.ModuleList( [ norm_f( torch.nn.Conv2d( in_ch, out_ch, (kernel_size, 1), (stride, 1), padding=(get_padding(kernel_size, 1), 0), ) ) for in_ch, out_ch in zip(in_channels, out_channels) ] )
self.conv_post = norm_f(torch.nn.Conv2d(1024, 1, (3, 1), 1, padding=(1, 0))) self.lrelu = torch.nn.LeakyReLU(LRELU_SLOPE)
def forward(self, x): fmap = [] b, c, t = x.shape
if t % self.period != 0: n_pad = self.period - (t % self.period) x = torch.nn.functional.pad(x, (0, n_pad), "reflect")
x = x.view(b, c, -1, self.period)
for conv in self.convs: x = self.lrelu(conv(x)) fmap.append(x)
x = self.conv_post(x) fmap.append(x) x = torch.flatten(x, 1, -1) return x, fmap
class EpochRecorder: def __init__(self): self.last_time = ttime()
def record(self): now_time = ttime() elapsed_time = now_time - self.last_time self.last_time = now_time elapsed_time = round(elapsed_time, 1) elapsed_time_str = str(datetime.timedelta(seconds=int(elapsed_time))) current_time = datetime.datetime.now().strftime("%H:%M:%S") return translations["time_or_speed_training"].format(current_time=current_time, elapsed_time_str=elapsed_time_str)
def dynamic_range_compression_torch(x, C=1, clip_val=1e-5): return torch.log(torch.clamp(x, min=clip_val) * C)
def dynamic_range_decompression_torch(x, C=1): return torch.exp(x) / C
def spectral_normalize_torch(magnitudes): return dynamic_range_compression_torch(magnitudes)
def spectral_de_normalize_torch(magnitudes): return dynamic_range_decompression_torch(magnitudes)
mel_basis = {}hann_window = {}
def spectrogram_torch(y, n_fft, hop_size, win_size, center=False): global hann_window dtype_device = str(y.dtype) + "_" + str(y.device) wnsize_dtype_device = str(win_size) + "_" + dtype_device if wnsize_dtype_device not in hann_window: hann_window[wnsize_dtype_device] = torch.hann_window(win_size).to(dtype=y.dtype, device=y.device)
y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft - hop_size) / 2), int((n_fft - hop_size) / 2)), mode="reflect")
y = y.squeeze(1)
spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[wnsize_dtype_device], center=center, pad_mode="reflect", normalized=False, onesided=True, return_complex=True)
spec = torch.sqrt(spec.real.pow(2) + spec.imag.pow(2) + 1e-6) return spec
def spec_to_mel_torch(spec, n_fft, num_mels, sample_rate, fmin, fmax): global mel_basis dtype_device = str(spec.dtype) + "_" + str(spec.device) fmax_dtype_device = str(fmax) + "_" + dtype_device if fmax_dtype_device not in mel_basis: mel = librosa_mel_fn(sr=sample_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax) mel_basis[fmax_dtype_device] = torch.from_numpy(mel).to(dtype=spec.dtype, device=spec.device)
melspec = torch.matmul(mel_basis[fmax_dtype_device], spec) melspec = spectral_normalize_torch(melspec) return melspec
def mel_spectrogram_torch(y, n_fft, num_mels, sample_rate, hop_size, win_size, fmin, fmax, center=False): spec = spectrogram_torch(y, n_fft, hop_size, win_size, center)
melspec = spec_to_mel_torch(spec, n_fft, num_mels, sample_rate, fmin, fmax)
return melspec
def replace_keys_in_dict(d, old_key_part, new_key_part): updated_dict = OrderedDict() if isinstance(d, OrderedDict) else {}
for key, value in d.items(): new_key = (key.replace(old_key_part, new_key_part) if isinstance(key, str) else key) updated_dict[new_key] = (replace_keys_in_dict(value, old_key_part, new_key_part) if isinstance(value, dict) else value) return updated_dict
def extract_model(ckpt, sr, pitch_guidance, name, model_dir, epoch, step, version, hps, model_author): try: logger.info(translations["savemodel"].format(model_dir=model_dir, epoch=epoch, step=step))
model_dir_path = os.path.join("assets", "weights")
if "best_epoch" in model_dir: pth_file = f"{name}_{epoch}e_{step}s_best_epoch.pth" else: pth_file = f"{name}_{epoch}e_{step}s.pth"
pth_file_old_version_path = os.path.join(model_dir_path, f"{pth_file}_old_version.pth")
opt = OrderedDict(weight={key: value.half() for key, value in ckpt.items() if "enc_q" not in key})
opt["config"] = [ hps.data.filter_length // 2 + 1, 32, hps.model.inter_channels, hps.model.hidden_channels, hps.model.filter_channels, hps.model.n_heads, hps.model.n_layers, hps.model.kernel_size, hps.model.p_dropout, hps.model.resblock, hps.model.resblock_kernel_sizes, hps.model.resblock_dilation_sizes, hps.model.upsample_rates, hps.model.upsample_initial_channel, hps.model.upsample_kernel_sizes, hps.model.spk_embed_dim, hps.model.gin_channels, hps.data.sample_rate, ]
opt["epoch"] = f"{epoch}epoch" opt["step"] = step opt["sr"] = sr opt["f0"] = int(pitch_guidance) opt["version"] = version opt["creation_date"] = datetime.datetime.now().isoformat()
hash_input = f"{str(ckpt)} {epoch} {step} {datetime.datetime.now().isoformat()}" model_hash = hashlib.sha256(hash_input.encode()).hexdigest() opt["model_hash"] = model_hash opt["model_name"] = name opt["author"] = model_author
torch.save(opt, os.path.join(model_dir_path, pth_file))
model = torch.load(model_dir, map_location=torch.device("cpu")) torch.save(replace_keys_in_dict(replace_keys_in_dict(model, ".parametrizations.weight.original1", ".weight_v"), ".parametrizations.weight.original0", ".weight_g"), pth_file_old_version_path)
os.remove(model_dir) os.rename(pth_file_old_version_path, model_dir)
except Exception as e: logger.error(f"{translations['extract_model_error']}: {e}")
def run(rank, n_gpus, experiment_dir, pretrainG, pretrainD, pitch_guidance, custom_total_epoch, custom_save_every_weights, config, device, model_author): global global_step
if rank == 0: writer = SummaryWriter(log_dir=experiment_dir) writer_eval = SummaryWriter(log_dir=os.path.join(experiment_dir, "eval"))
dist.init_process_group(backend="gloo", init_method="env://", world_size=n_gpus, rank=rank) torch.manual_seed(config.train.seed)
if torch.cuda.is_available(): torch.cuda.set_device(rank)
train_dataset = TextAudioLoaderMultiNSFsid(config.data)
train_sampler = DistributedBucketSampler(train_dataset, batch_size * n_gpus, [100, 200, 300, 400, 500, 600, 700, 800, 900], num_replicas=n_gpus, rank=rank, shuffle=True)
collate_fn = TextAudioCollateMultiNSFsid()
train_loader = DataLoader(train_dataset, num_workers=4, shuffle=False, pin_memory=True, collate_fn=collate_fn, batch_sampler=train_sampler, persistent_workers=True, prefetch_factor=8)
net_g = Synthesizer(config.data.filter_length // 2 + 1, config.train.segment_size // config.data.hop_length, **config.model, use_f0=pitch_guidance == True, is_half=config.train.fp16_run and device.type == "cuda", sr=sample_rate).to(device)
if torch.cuda.is_available(): net_g = net_g.cuda(rank)
if version == "v1": net_d = MultiPeriodDiscriminator(config.model.use_spectral_norm) else: net_d = MultiPeriodDiscriminatorV2(config.model.use_spectral_norm)
if torch.cuda.is_available(): net_d = net_d.cuda(rank)
optim_g = torch.optim.AdamW(net_g.parameters(), config.train.learning_rate, betas=config.train.betas, eps=config.train.eps) optim_d = torch.optim.AdamW(net_d.parameters(), config.train.learning_rate, betas=config.train.betas, eps=config.train.eps)
if torch.cuda.is_available(): net_g = DDP(net_g, device_ids=[rank]) net_d = DDP(net_d, device_ids=[rank]) else: net_g = DDP(net_g) net_d = DDP(net_d)
try: logger.info(translations["start_training"])
_, _, _, epoch_str = load_checkpoint(latest_checkpoint_path(experiment_dir, "D_*.pth"), net_d, optim_d) _, _, _, epoch_str = load_checkpoint(latest_checkpoint_path(experiment_dir, "G_*.pth"), net_g, optim_g)
epoch_str += 1 global_step = (epoch_str - 1) * len(train_loader)
except: epoch_str = 1 global_step = 0
if pretrainG != "": if rank == 0: logger.info(translations["import_pretrain"].format(dg="G", pretrain=pretrainG))
if hasattr(net_g, "module"): net_g.module.load_state_dict(torch.load(pretrainG, map_location="cpu")["model"]) else: net_g.load_state_dict(torch.load(pretrainG, map_location="cpu")["model"]) else: logger.warning(translations["not_using_pretrain"].format(dg="G"))
if pretrainD != "": if rank == 0: logger.info(translations["import_pretrain"].format(dg="D", pretrain=pretrainD))
if hasattr(net_d, "module"): net_d.module.load_state_dict(torch.load(pretrainD, map_location="cpu")["model"]) else: net_d.load_state_dict(torch.load(pretrainD, map_location="cpu")["model"]) else: logger.warning(translations["not_using_pretrain"].format(dg="D"))
scheduler_g = torch.optim.lr_scheduler.ExponentialLR(optim_g, gamma=config.train.lr_decay, last_epoch=epoch_str - 2) scheduler_d = torch.optim.lr_scheduler.ExponentialLR(optim_d, gamma=config.train.lr_decay, last_epoch=epoch_str - 2)
optim_d.step() optim_g.step()
scaler = GradScaler(enabled=config.train.fp16_run)
cache = []
for info in train_loader: phone, phone_lengths, pitch, pitchf, _, _, _, _, sid = info reference = ( phone.to(device), phone_lengths.to(device), pitch.to(device) if pitch_guidance else None, pitchf.to(device) if pitch_guidance else None, sid.to(device), ) break
for epoch in range(epoch_str, total_epoch + 1): if rank == 0: train_and_evaluate(rank, epoch, config, [net_g, net_d], [optim_g, optim_d], scaler, [train_loader, None], [writer, writer_eval], cache, custom_save_every_weights, custom_total_epoch, device, reference, model_author) else: train_and_evaluate(rank, epoch, config, [net_g, net_d], [optim_g, optim_d], scaler, [train_loader, None], None, cache, custom_save_every_weights, custom_total_epoch, device, reference, model_author)
scheduler_g.step() scheduler_d.step()
def train_and_evaluate(rank, epoch, hps, nets, optims, scaler, loaders, writers, cache, custom_save_every_weights, custom_total_epoch, device, reference, model_author): global global_step, lowest_value, loss_disc, consecutive_increases_gen, consecutive_increases_disc
if epoch == 1: lowest_value = {"step": 0, "value": float("inf"), "epoch": 0} last_loss_gen_all = 0.0 consecutive_increases_gen = 0 consecutive_increases_disc = 0
net_g, net_d = nets optim_g, optim_d = optims train_loader = loaders[0] if loaders is not None else None
if writers is not None: writer = writers[0]
train_loader.batch_sampler.set_epoch(epoch)
net_g.train() net_d.train()
if device.type == "cuda" and cache_data_in_gpu: data_iterator = cache
if cache == []: for batch_idx, info in enumerate(train_loader): ( phone, phone_lengths, pitch, pitchf, spec, spec_lengths, wave, wave_lengths, sid, ) = info cache.append( (batch_idx, ( phone.cuda(rank, non_blocking=True), phone_lengths.cuda(rank, non_blocking=True), (pitch.cuda(rank, non_blocking=True) if pitch_guidance else None), (pitchf.cuda(rank, non_blocking=True) if pitch_guidance else None), spec.cuda(rank, non_blocking=True), spec_lengths.cuda(rank, non_blocking=True), wave.cuda(rank, non_blocking=True), wave_lengths.cuda(rank, non_blocking=True), sid.cuda(rank, non_blocking=True), ), )) else: shuffle(cache) else: data_iterator = enumerate(train_loader)
epoch_recorder = EpochRecorder()
with tqdm(total=len(train_loader), leave=False) as pbar: for batch_idx, info in data_iterator: ( phone, phone_lengths, pitch, pitchf, spec, spec_lengths, wave, wave_lengths, sid, ) = info if device.type == "cuda" and not cache_data_in_gpu: phone = phone.cuda(rank, non_blocking=True) phone_lengths = phone_lengths.cuda(rank, non_blocking=True) pitch = pitch.cuda(rank, non_blocking=True) if pitch_guidance else None pitchf = (pitchf.cuda(rank, non_blocking=True) if pitch_guidance else None) sid = sid.cuda(rank, non_blocking=True) spec = spec.cuda(rank, non_blocking=True) spec_lengths = spec_lengths.cuda(rank, non_blocking=True) wave = wave.cuda(rank, non_blocking=True) wave_lengths = wave_lengths.cuda(rank, non_blocking=True) else: phone = phone.to(device) phone_lengths = phone_lengths.to(device) pitch = pitch.to(device) if pitch_guidance else None pitchf = pitchf.to(device) if pitch_guidance else None sid = sid.to(device) spec = spec.to(device) spec_lengths = spec_lengths.to(device) wave = wave.to(device) wave_lengths = wave_lengths.to(device)
use_amp = config.train.fp16_run and device.type == "cuda"
with autocast(enabled=use_amp): ( y_hat, ids_slice, x_mask, z_mask, (z, z_p, m_p, logs_p, m_q, logs_q), ) = net_g(phone, phone_lengths, pitch, pitchf, spec, spec_lengths, sid) mel = spec_to_mel_torch( spec, config.data.filter_length, config.data.n_mel_channels, config.data.sample_rate, config.data.mel_fmin, config.data.mel_fmax, ) y_mel = slice_segments(mel, ids_slice, config.train.segment_size // config.data.hop_length, dim=3) with autocast(enabled=False): y_hat_mel = mel_spectrogram_torch( y_hat.float().squeeze(1), config.data.filter_length, config.data.n_mel_channels, config.data.sample_rate, config.data.hop_length, config.data.win_length, config.data.mel_fmin, config.data.mel_fmax, ) if use_amp: y_hat_mel = y_hat_mel.half()
wave = slice_segments(wave, ids_slice * config.data.hop_length, config.train.segment_size, dim=3) y_d_hat_r, y_d_hat_g, _, _ = net_d(wave, y_hat.detach())
with autocast(enabled=False): loss_disc, losses_disc_r, losses_disc_g = discriminator_loss(y_d_hat_r, y_d_hat_g)
optim_d.zero_grad() scaler.scale(loss_disc).backward() scaler.unscale_(optim_d) grad_norm_d = clip_grad_value(net_d.parameters(), None) scaler.step(optim_d)
with autocast(enabled=use_amp): y_d_hat_r, y_d_hat_g, fmap_r, fmap_g = net_d(wave, y_hat) with autocast(enabled=False): loss_mel = F.l1_loss(y_mel, y_hat_mel) * config.train.c_mel loss_kl = (kl_loss(z_p, logs_q, m_p, logs_p, z_mask) * config.train.c_kl) loss_fm = feature_loss(fmap_r, fmap_g) loss_gen, losses_gen = generator_loss(y_d_hat_g) loss_gen_all = loss_gen + loss_fm + loss_mel + loss_kl
if loss_gen_all < lowest_value["value"]: lowest_value["value"] = loss_gen_all lowest_value["step"] = global_step lowest_value["epoch"] = epoch if epoch > lowest_value["epoch"]: logger.warning(translations["training_warning"])
optim_g.zero_grad() scaler.scale(loss_gen_all).backward() scaler.unscale_(optim_g) grad_norm_g = clip_grad_value(net_g.parameters(), None) scaler.step(optim_g) scaler.update()
if rank == 0: if global_step % config.train.log_interval == 0: lr = optim_g.param_groups[0]["lr"]
if loss_mel > 75: loss_mel = 75 if loss_kl > 9: loss_kl = 9
scalar_dict = { "loss/g/total": loss_gen_all, "loss/d/total": loss_disc, "learning_rate": lr, "grad_norm_d": grad_norm_d, "grad_norm_g": grad_norm_g, } scalar_dict.update( { "loss/g/fm": loss_fm, "loss/g/mel": loss_mel, "loss/g/kl": loss_kl, } ) scalar_dict.update( {f"loss/g/{i}": v for i, v in enumerate(losses_gen)} ) scalar_dict.update( {f"loss/d_r/{i}": v for i, v in enumerate(losses_disc_r)} ) scalar_dict.update( {f"loss/d_g/{i}": v for i, v in enumerate(losses_disc_g)} ) image_dict = { "slice/mel_org": plot_spectrogram_to_numpy( y_mel[0].data.cpu().numpy() ), "slice/mel_gen": plot_spectrogram_to_numpy( y_hat_mel[0].data.cpu().numpy() ), "all/mel": plot_spectrogram_to_numpy(mel[0].data.cpu().numpy()), }
with torch.no_grad(): if hasattr(net_g, "module"): o, *_ = net_g.module.infer(*reference) else: o, *_ = net_g.infer(*reference)
audio_dict = {f"gen/audio_{global_step:07d}": o[0, :, :]}
summarize( writer=writer, global_step=global_step, images=image_dict, scalars=scalar_dict, audios=audio_dict, audio_sample_rate=config.data.sample_rate, )
global_step += 1 pbar.update(1)
def check_overtraining(smoothed_loss_history, threshold, epsilon=0.004): if len(smoothed_loss_history) < threshold + 1: return False
for i in range(-threshold, -1): if smoothed_loss_history[i + 1] > smoothed_loss_history[i]: return True if abs(smoothed_loss_history[i + 1] - smoothed_loss_history[i]) >= epsilon: return False
return True
def update_exponential_moving_average(smoothed_loss_history, new_value, smoothing=0.987): smoothed_value = new_value if not smoothed_loss_history else (smoothing * smoothed_loss_history[-1] + (1 - smoothing) * new_value) smoothed_loss_history.append(smoothed_value) return smoothed_value
def save_to_json(file_path, loss_disc_history, smoothed_loss_disc_history, loss_gen_history, smoothed_loss_gen_history): data = { "loss_disc_history": loss_disc_history, "smoothed_loss_disc_history": smoothed_loss_disc_history, "loss_gen_history": loss_gen_history, "smoothed_loss_gen_history": smoothed_loss_gen_history, }
with open(file_path, "w") as f: json.dump(data, f) model_add = [] model_del = [] done = False if rank == 0: if epoch % save_every_epoch == False: checkpoint_suffix = f"{2333333 if save_only_latest else global_step}.pth"
save_checkpoint(net_g, optim_g, config.train.learning_rate, epoch, os.path.join(experiment_dir, "G_" + checkpoint_suffix)) save_checkpoint(net_d, optim_d, config.train.learning_rate, epoch, os.path.join(experiment_dir, "D_" + checkpoint_suffix))
if custom_save_every_weights: model_add.append(os.path.join("assets", "weights", f"{model_name}_{epoch}e_{global_step}s.pth"))
if overtraining_detector and epoch > 1: current_loss_disc = float(loss_disc) loss_disc_history.append(current_loss_disc)
smoothed_value_disc = update_exponential_moving_average(smoothed_loss_disc_history, current_loss_disc) is_overtraining_disc = check_overtraining(smoothed_loss_disc_history, overtraining_threshold * 2)
if is_overtraining_disc: consecutive_increases_disc += 1 else: consecutive_increases_disc = 0
current_loss_gen = float(lowest_value["value"]) loss_gen_history.append(current_loss_gen)
smoothed_value_gen = update_exponential_moving_average(smoothed_loss_gen_history, current_loss_gen) is_overtraining_gen = check_overtraining(smoothed_loss_gen_history, overtraining_threshold, 0.01)
if is_overtraining_gen: consecutive_increases_gen += 1 else: consecutive_increases_gen = 0
if epoch % save_every_epoch == 0: save_to_json(training_file_path, loss_disc_history, smoothed_loss_disc_history, loss_gen_history, smoothed_loss_gen_history)
if (is_overtraining_gen and consecutive_increases_gen == overtraining_threshold or is_overtraining_disc and consecutive_increases_disc == (overtraining_threshold * 2)): logger.info(translations["overtraining_find"].format(epoch=epoch, smoothed_value_gen=f"{smoothed_value_gen:.3f}", smoothed_value_disc=f"{smoothed_value_disc:.3f}")) done = True else: logger.info(translations["best_epoch"].format(epoch=epoch, smoothed_value_gen=f"{smoothed_value_gen:.3f}", smoothed_value_disc=f"{smoothed_value_disc:.3f}"))
old_model_files = glob.glob(os.path.join("assets", "weights", f"{model_name}_*e_*s_best_epoch.pth"))
for file in old_model_files: model_del.append(file)
model_add.append(os.path.join("assets", "weights", f"{model_name}_{epoch}e_{global_step}s_best_epoch.pth")) if epoch >= custom_total_epoch: lowest_value_rounded = float(lowest_value["value"]) lowest_value_rounded = round(lowest_value_rounded, 3)
logger.info(translations["success_training"].format(epoch=epoch, global_step=global_step, loss_gen_all=round(loss_gen_all.item(), 3))) logger.info(translations["training_info"].format(lowest_value_rounded=lowest_value_rounded, lowest_value_epoch=lowest_value['epoch'], lowest_value_step=lowest_value['step']))
pid_file_path = os.path.join(experiment_dir, "config.json")
with open(pid_file_path, "r") as pid_file: pid_data = json.load(pid_file) with open(pid_file_path, "w") as pid_file: pid_data.pop("process_pids", None) json.dump(pid_data, pid_file, indent=4)
model_add.append(os.path.join("assets", "weights", f"{model_name}_{epoch}e_{global_step}s.pth")) done = True if model_add: ckpt = (net_g.module.state_dict() if hasattr(net_g, "module") else net_g.state_dict())
for m in model_add: if not os.path.exists(m): extract_model(ckpt=ckpt, sr=sample_rate, pitch_guidance=pitch_guidance == True, name=model_name, model_dir=m, epoch=epoch, step=global_step, version=version, hps=hps, model_author=model_author)
for m in model_del: os.remove(m)
lowest_value_rounded = float(lowest_value["value"]) lowest_value_rounded = round(lowest_value_rounded, 3)
if epoch > 1 and overtraining_detector: remaining_epochs_gen = overtraining_threshold - consecutive_increases_gen remaining_epochs_disc = (overtraining_threshold * 2) - consecutive_increases_disc
logger.info(translations["model_training_info"].format(model_name=model_name, epoch=epoch, global_step=global_step, epoch_recorder=epoch_recorder.record(), lowest_value_rounded=lowest_value_rounded, lowest_value_epoch=lowest_value['epoch'], lowest_value_step=lowest_value['step'], remaining_epochs_gen=remaining_epochs_gen, remaining_epochs_disc=remaining_epochs_disc, smoothed_value_gen=f"{smoothed_value_gen:.3f}", smoothed_value_disc=f"{smoothed_value_disc:.3f}")) elif epoch > 1 and overtraining_detector == False: logger.info(translations["model_training_info_2"].format(model_name=model_name, epoch=epoch, global_step=global_step, epoch_recorder=epoch_recorder.record(), lowest_value_rounded=lowest_value_rounded, lowest_value_epoch=lowest_value['epoch'], lowest_value_step=lowest_value['step'])) else: logger.info(translations["model_training_info_3"].format(model_name=model_name, epoch=epoch, global_step=global_step, epoch_recorder=epoch_recorder.record()))
last_loss_gen_all = loss_gen_all
if done: os._exit(2333333)
if __name__ == "__main__": torch.multiprocessing.set_start_method("spawn")
try: main() except Exception as e: logger.error(f"{translations['training_error']} {e}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100import mathimport torch
from typing import List, Optional
def init_weights(m, mean=0.0, std=0.01): classname = m.__class__.__name__ if classname.find("Conv") != -1: m.weight.data.normal_(mean, std)
def get_padding(kernel_size, dilation=1): return int((kernel_size * dilation - dilation) / 2)
def convert_pad_shape(pad_shape): l = pad_shape[::-1] pad_shape = [item for sublist in l for item in sublist] return pad_shape
def kl_divergence(m_p, logs_p, m_q, logs_q): kl = (logs_q - logs_p) - 0.5 kl += (0.5 * (torch.exp(2.0 * logs_p) + ((m_p - m_q) ** 2)) * torch.exp(-2.0 * logs_q)) return kl
def slice_segments(x: torch.Tensor, ids_str: torch.Tensor, segment_size: int = 4, dim: int = 2): if dim == 2: ret = torch.zeros_like(x[:, :segment_size]) elif dim == 3: ret = torch.zeros_like(x[:, :, :segment_size])
for i in range(x.size(0)): idx_str = ids_str[i].item() idx_end = idx_str + segment_size
if dim == 2: ret[i] = x[i, idx_str:idx_end] else: ret[i] = x[i, :, idx_str:idx_end]
return ret
def rand_slice_segments(x, x_lengths=None, segment_size=4): b, d, t = x.size()
if x_lengths is None: x_lengths = t
ids_str_max = x_lengths - segment_size + 1 ids_str = (torch.rand([b]).to(device=x.device) * ids_str_max).to(dtype=torch.long) ret = slice_segments(x, ids_str, segment_size, dim=3) return ret, ids_str
def get_timing_signal_1d(length, channels, min_timescale=1.0, max_timescale=1.0e4): position = torch.arange(length, dtype=torch.float) num_timescales = channels // 2 log_timescale_increment = math.log(float(max_timescale) / float(min_timescale)) / (num_timescales - 1) inv_timescales = min_timescale * torch.exp(torch.arange(num_timescales, dtype=torch.float) * -log_timescale_increment) scaled_time = position.unsqueeze(0) * inv_timescales.unsqueeze(1) signal = torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], 0) signal = torch.nn.functional.pad(signal, [0, 0, 0, channels % 2]) signal = signal.view(1, channels, length) return signal
def subsequent_mask(length): mask = torch.tril(torch.ones(length, length)).unsqueeze(0).unsqueeze(0) return mask
@torch.jit.scriptdef fused_add_tanh_sigmoid_multiply(input_a, input_b, n_channels): n_channels_int = n_channels[0] in_act = input_a + input_b t_act = torch.tanh(in_act[:, :n_channels_int, :]) s_act = torch.sigmoid(in_act[:, n_channels_int:, :]) acts = t_act * s_act return acts
def convert_pad_shape(pad_shape: List[List[int]]) -> List[int]: return torch.tensor(pad_shape).flip(0).reshape(-1).int().tolist()
def sequence_mask(length: torch.Tensor, max_length: Optional[int] = None): if max_length is None: max_length = length.max() x = torch.arange(max_length, dtype=length.dtype, device=length.device) return x.unsqueeze(0) < length.unsqueeze(1)
def clip_grad_value(parameters, clip_value, norm_type=2): if isinstance(parameters, torch.Tensor): parameters = [parameters]
parameters = list(filter(lambda p: p.grad is not None, parameters)) norm_type = float(norm_type)
if clip_value is not None: clip_value = float(clip_value)
total_norm = 0
for p in parameters: param_norm = p.grad.data.norm(norm_type) total_norm += param_norm.item() ** norm_type if clip_value is not None: p.grad.data.clamp_(min=-clip_value, max=clip_value)
total_norm = total_norm ** (1.0 / norm_type) return total_norm1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import osimport sysimport torch
now_dir = os.getcwd()sys.path.append(now_dir)
from .commons import fused_add_tanh_sigmoid_multiply
class WaveNet(torch.nn.Module): def __init__(self, hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=0, p_dropout=0): super(WaveNet, self).__init__() assert kernel_size % 2 == 1
self.hidden_channels = hidden_channels self.kernel_size = (kernel_size,) self.dilation_rate = dilation_rate
self.n_layers = n_layers self.gin_channels = gin_channels self.p_dropout = p_dropout
self.in_layers = torch.nn.ModuleList() self.res_skip_layers = torch.nn.ModuleList() self.drop = torch.nn.Dropout(p_dropout)
if gin_channels != 0: cond_layer = torch.nn.Conv1d(gin_channels, 2 * hidden_channels * n_layers, 1) self.cond_layer = torch.nn.utils.parametrizations.weight_norm(cond_layer, name="weight")
dilations = [dilation_rate**i for i in range(n_layers)] paddings = [(kernel_size * d - d) // 2 for d in dilations]
for i in range(n_layers): in_layer = torch.nn.Conv1d(hidden_channels, 2 * hidden_channels, kernel_size, dilation=dilations[i], padding=paddings[i]) in_layer = torch.nn.utils.parametrizations.weight_norm(in_layer, name="weight")
self.in_layers.append(in_layer)
res_skip_channels = (hidden_channels if i == n_layers - 1 else 2 * hidden_channels) res_skip_layer = torch.nn.Conv1d(hidden_channels, res_skip_channels, 1) res_skip_layer = torch.nn.utils.parametrizations.weight_norm(res_skip_layer, name="weight") self.res_skip_layers.append(res_skip_layer)
def forward(self, x, x_mask, g=None, **kwargs): output = torch.zeros_like(x) n_channels_tensor = torch.IntTensor([self.hidden_channels])
if g is not None: g = self.cond_layer(g)
for i in range(self.n_layers): x_in = self.in_layers[i](x)
if g is not None: cond_offset = i * 2 * self.hidden_channels g_l = g[:, cond_offset : cond_offset + 2 * self.hidden_channels, :] else: g_l = torch.zeros_like(x_in)
acts = fused_add_tanh_sigmoid_multiply(x_in, g_l, n_channels_tensor) acts = self.drop(acts)
res_skip_acts = self.res_skip_layers[i](acts)
if i < self.n_layers - 1: res_acts = res_skip_acts[:, : self.hidden_channels, :] x = (x + res_acts) * x_mask output = output + res_skip_acts[:, self.hidden_channels :, :] else: output = output + res_skip_acts
return output * x_mask
def remove_weight_norm(self): if self.gin_channels != 0: torch.nn.utils.remove_weight_norm(self.cond_layer)
for l in self.in_layers: torch.nn.utils.remove_weight_norm(l)
for l in self.res_skip_layers: torch.nn.utils.remove_weight_norm(l)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170import osimport sysimport torch
from typing import Optional
from torch.nn.utils import remove_weight_normfrom torch.nn.utils.parametrizations import weight_norm
now_dir = os.getcwd()sys.path.append(now_dir)
from .modules import WaveNetfrom .commons import get_padding, init_weights
LRELU_SLOPE = 0.1
def create_conv1d_layer(channels, kernel_size, dilation): return weight_norm(torch.nn.Conv1d(channels, channels, kernel_size, 1, dilation=dilation, padding=get_padding(kernel_size, dilation)))
def apply_mask(tensor, mask): return tensor * mask if mask is not None else tensor
class ResBlockBase(torch.nn.Module): def __init__(self, channels, kernel_size, dilations): super(ResBlockBase, self).__init__() self.convs1 = torch.nn.ModuleList([create_conv1d_layer(channels, kernel_size, d) for d in dilations]) self.convs1.apply(init_weights)
self.convs2 = torch.nn.ModuleList([create_conv1d_layer(channels, kernel_size, 1) for _ in dilations]) self.convs2.apply(init_weights)
def forward(self, x, x_mask=None): for c1, c2 in zip(self.convs1, self.convs2): xt = torch.nn.functional.leaky_relu(x, LRELU_SLOPE) xt = apply_mask(xt, x_mask) xt = torch.nn.functional.leaky_relu(c1(xt), LRELU_SLOPE) xt = apply_mask(xt, x_mask) xt = c2(xt) x = xt + x return apply_mask(x, x_mask)
def remove_weight_norm(self): for conv in self.convs1 + self.convs2: remove_weight_norm(conv)
class ResBlock1(ResBlockBase): def __init__(self, channels, kernel_size=3, dilation=(1, 3, 5)): super(ResBlock1, self).__init__(channels, kernel_size, dilation)
class ResBlock2(ResBlockBase): def __init__(self, channels, kernel_size=3, dilation=(1, 3)): super(ResBlock2, self).__init__(channels, kernel_size, dilation)
class Log(torch.nn.Module): def forward(self, x, x_mask, reverse=False, **kwargs): if not reverse: y = torch.log(torch.clamp_min(x, 1e-5)) * x_mask logdet = torch.sum(-y, [1, 2]) return y, logdet else: x = torch.exp(x) * x_mask return x
class Flip(torch.nn.Module): def forward(self, x, *args, reverse=False, **kwargs): x = torch.flip(x, [1]) if not reverse: logdet = torch.zeros(x.size(0)).to(dtype=x.dtype, device=x.device) return x, logdet else: return x
class ElementwiseAffine(torch.nn.Module): def __init__(self, channels): super().__init__() self.channels = channels self.m = torch.nn.Parameter(torch.zeros(channels, 1)) self.logs = torch.nn.Parameter(torch.zeros(channels, 1))
def forward(self, x, x_mask, reverse=False, **kwargs): if not reverse: y = self.m + torch.exp(self.logs) * x y = y * x_mask logdet = torch.sum(self.logs * x_mask, [1, 2]) return y, logdet else: x = (x - self.m) * torch.exp(-self.logs) * x_mask return x
class ResidualCouplingBlock(torch.nn.Module): def __init__(self, channels, hidden_channels, kernel_size, dilation_rate, n_layers, n_flows=4, gin_channels=0): super(ResidualCouplingBlock, self).__init__() self.channels = channels self.hidden_channels = hidden_channels self.kernel_size = kernel_size self.dilation_rate = dilation_rate self.n_layers = n_layers self.n_flows = n_flows self.gin_channels = gin_channels
self.flows = torch.nn.ModuleList() for i in range(n_flows): self.flows.append(ResidualCouplingLayer(channels, hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=gin_channels, mean_only=True)) self.flows.append(Flip())
def forward(self, x: torch.Tensor, x_mask: torch.Tensor, g: Optional[torch.Tensor] = None, reverse = False): if not reverse: for flow in self.flows: x, _ = flow(x, x_mask, g=g, reverse=reverse) else: for flow in reversed(self.flows): x = flow.forward(x, x_mask, g=g, reverse=reverse)
return x
def remove_weight_norm(self): for i in range(self.n_flows): self.flows[i * 2].remove_weight_norm()
def __prepare_scriptable__(self): for i in range(self.n_flows): for hook in self.flows[i * 2]._forward_pre_hooks.values(): if (hook.__module__ == "torch.nn.utils.parametrizations.weight_norm" and hook.__class__.__name__ == "WeightNorm"): torch.nn.utils.remove_weight_norm(self.flows[i * 2]) return self
class ResidualCouplingLayer(torch.nn.Module): def __init__(self, channels, hidden_channels, kernel_size, dilation_rate, n_layers, p_dropout=0, gin_channels=0, mean_only=False): assert channels % 2 == 0, "Channels/2" super().__init__() self.channels = channels self.hidden_channels = hidden_channels self.kernel_size = kernel_size self.dilation_rate = dilation_rate self.n_layers = n_layers self.half_channels = channels // 2 self.mean_only = mean_only
self.pre = torch.nn.Conv1d(self.half_channels, hidden_channels, 1) self.enc = WaveNet(hidden_channels, kernel_size, dilation_rate, n_layers, p_dropout=p_dropout, gin_channels=gin_channels) self.post = torch.nn.Conv1d(hidden_channels, self.half_channels * (2 - mean_only), 1) self.post.weight.data.zero_() self.post.bias.data.zero_()
def forward(self, x, x_mask, g=None, reverse=False): x0, x1 = torch.split(x, [self.half_channels] * 2, 1) h = self.pre(x0) * x_mask h = self.enc(h, x_mask, g=g) stats = self.post(h) * x_mask
if not self.mean_only: m, logs = torch.split(stats, [self.half_channels] * 2, 1) else: m = stats logs = torch.zeros_like(m)
if not reverse: x1 = m + x1 * torch.exp(logs) * x_mask x = torch.cat([x0, x1], 1) logdet = torch.sum(logs, [1, 2]) return x, logdet else: x1 = (x1 - m) * torch.exp(-logs) * x_mask x = torch.cat([x0, x1], 1) return x
def remove_weight_norm(self): self.enc.remove_weight_norm()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420import osimport sysimport timeimport jsonimport yamlimport torchimport codecsimport hashlibimport loggingimport platformimport warningsimport requestsimport subprocess
import onnxruntime as ort
from tqdm import tqdmfrom importlib import metadata, import_module
now_dir = os.getcwd()sys.path.append(now_dir)
from main.configs.config import Configtranslations = Config().translations
class Separator: def __init__(self, log_level=logging.INFO, log_formatter=None, model_file_dir="assets/model/uvr5", output_dir=None, output_format="wav", output_bitrate=None, normalization_threshold=0.9, output_single_stem=None, invert_using_spec=False, sample_rate=44100, mdx_params={"hop_length": 1024, "segment_size": 256, "overlap": 0.25, "batch_size": 1, "enable_denoise": False}, demucs_params={"segment_size": "Default", "shifts": 2, "overlap": 0.25, "segments_enabled": True}): self.logger = logging.getLogger(__name__) self.logger.setLevel(log_level) self.log_level = log_level self.log_formatter = log_formatter self.log_handler = logging.StreamHandler()
if self.log_formatter is None: self.log_formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(module)s - %(message)s")
self.log_handler.setFormatter(self.log_formatter)
if not self.logger.hasHandlers(): self.logger.addHandler(self.log_handler) if log_level > logging.DEBUG: warnings.filterwarnings("ignore")
self.logger.info(translations["separator_info"].format(output_dir=output_dir, output_format=output_format))
self.model_file_dir = model_file_dir
if output_dir is None: output_dir = os.getcwd() self.logger.info(translations["output_dir_is_none"])
self.output_dir = output_dir
os.makedirs(self.model_file_dir, exist_ok=True) os.makedirs(self.output_dir, exist_ok=True)
self.output_format = output_format self.output_bitrate = output_bitrate
if self.output_format is None: self.output_format = "wav"
self.normalization_threshold = normalization_threshold
if normalization_threshold <= 0 or normalization_threshold > 1: raise ValueError(translations[">0or=1"])
self.output_single_stem = output_single_stem
if output_single_stem is not None: self.logger.debug(translations["output_single"].format(output_single_stem=output_single_stem))
self.invert_using_spec = invert_using_spec if self.invert_using_spec: self.logger.debug(translations["step2"])
try: self.sample_rate = int(sample_rate) if self.sample_rate <= 0: raise ValueError(translations["other_than_zero"].format(sample_rate=self.sample_rate)) if self.sample_rate > 12800000: raise ValueError(translations["too_high"].format(sample_rate=self.sample_rate)) except ValueError: raise ValueError(translations["sr_not_valid"])
self.arch_specific_params = {"MDX": mdx_params, "Demucs": demucs_params} self.torch_device = None self.torch_device_cpu = None self.torch_device_mps = None self.onnx_execution_provider = None self.model_instance = None self.model_is_uvr_vip = False self.model_friendly_name = None
self.setup_accelerated_inferencing_device()
def setup_accelerated_inferencing_device(self): system_info = self.get_system_info() self.check_ffmpeg_installed() self.log_onnxruntime_packages() self.setup_torch_device(system_info)
def get_system_info(self): os_name = platform.system() os_version = platform.version()
self.logger.info(f"{translations['os']}: {os_name} {os_version}")
system_info = platform.uname() self.logger.info(translations["platform_info"].format(system_info=system_info, node=system_info.node, release=system_info.release, machine=system_info.machine, processor=system_info.processor))
python_version = platform.python_version() self.logger.info(f"{translations['name_ver'].format(name='python')}: {python_version}")
pytorch_version = torch.__version__ self.logger.info(f"{translations['name_ver'].format(name='pytorch')}: {pytorch_version}")
return system_info
def check_ffmpeg_installed(self): try: ffmpeg_version_output = subprocess.check_output(["ffmpeg", "-version"], text=True) first_line = ffmpeg_version_output.splitlines()[0] self.logger.info(f"{translations['install_ffmpeg']}: {first_line}") except FileNotFoundError: self.logger.error(translations["none_ffmpeg"]) if "PYTEST_CURRENT_TEST" not in os.environ: raise
def log_onnxruntime_packages(self): onnxruntime_gpu_package = self.get_package_distribution("onnxruntime-gpu") onnxruntime_cpu_package = self.get_package_distribution("onnxruntime")
if onnxruntime_gpu_package is not None: self.logger.info(f"{translations['install_onnx'].format(pu='GPU')}: {onnxruntime_gpu_package.version}") if onnxruntime_cpu_package is not None: self.logger.info(f"{translations['install_onnx'].format(pu='CPU')}: {onnxruntime_cpu_package.version}")
def setup_torch_device(self, system_info): hardware_acceleration_enabled = False ort_providers = ort.get_available_providers()
self.torch_device_cpu = torch.device("cpu")
if torch.cuda.is_available(): self.configure_cuda(ort_providers) hardware_acceleration_enabled = True elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available() and system_info.processor == "arm": self.configure_mps(ort_providers) hardware_acceleration_enabled = True
if not hardware_acceleration_enabled: self.logger.info(translations["running_in_cpu"]) self.torch_device = self.torch_device_cpu self.onnx_execution_provider = ["CPUExecutionProvider"]
def configure_cuda(self, ort_providers): self.logger.info(translations["running_in_cuda"]) self.torch_device = torch.device("cuda")
if "CUDAExecutionProvider" in ort_providers: self.logger.info(translations["onnx_have"].format(have='CUDAExecutionProvider')) self.onnx_execution_provider = ["CUDAExecutionProvider"] else: self.logger.warning(translations["onnx_not_have"].format(have='CUDAExecutionProvider'))
def configure_mps(self, ort_providers): self.logger.info("Cài đặt thiết bị Torch thành MPS") self.torch_device_mps = torch.device("mps") self.torch_device = self.torch_device_mps
if "CoreMLExecutionProvider" in ort_providers: self.logger.info(translations["onnx_have"].format(have='CoreMLExecutionProvider')) self.onnx_execution_provider = ["CoreMLExecutionProvider"] else: self.logger.warning(translations["onnx_not_have"].format(have='CoreMLExecutionProvider'))
def get_package_distribution(self, package_name): try: return metadata.distribution(package_name) except metadata.PackageNotFoundError: self.logger.debug(translations["python_not_install"].format(package_name=package_name)) return None
def get_model_hash(self, model_path): self.logger.debug(translations["hash"].format(model_path=model_path))
try: with open(model_path, "rb") as f: f.seek(-10000 * 1024, 2) return hashlib.md5(f.read()).hexdigest() except IOError as e: self.logger.error(translations["ioerror"].format(e=e))
return hashlib.md5(open(model_path, "rb").read()).hexdigest()
def download_file_if_not_exists(self, url, output_path): if os.path.isfile(output_path): self.logger.debug(translations["cancel_download"].format(output_path=output_path)) return
self.logger.debug(translations["download_model"].format(url=url, output_path=output_path)) response = requests.get(url, stream=True, timeout=300)
if response.status_code == 200: total_size_in_bytes = int(response.headers.get("content-length", 0)) progress_bar = tqdm(total=total_size_in_bytes)
with open(output_path, "wb") as f: for chunk in response.iter_content(chunk_size=8192): progress_bar.update(len(chunk)) f.write(chunk)
progress_bar.close() else: raise RuntimeError(translations["download_error"].format(url=url, status_code=response.status_code))
def print_uvr_vip_message(self): if self.model_is_uvr_vip: self.logger.warning(translations["vip_model"].format(model_friendly_name=self.model_friendly_name)) self.logger.warning(translations["vip_print"])
def list_supported_model_files(self): download_checks_path = os.path.join(self.model_file_dir, "download_checks.json")
model_downloads_list = json.load(open(download_checks_path, encoding="utf-8")) self.logger.debug(translations["load_download_json"])
filtered_demucs_v4 = {key: value for key, value in model_downloads_list["demucs_download_list"].items() if key.startswith("Demucs v4")}
model_files_grouped_by_type = {"MDX": {**model_downloads_list["mdx_download_list"], **model_downloads_list["mdx_download_vip_list"]}, "Demucs": filtered_demucs_v4} return model_files_grouped_by_type def download_model_files(self, model_filename): model_path = os.path.join(self.model_file_dir, f"{model_filename}")
supported_model_files_grouped = self.list_supported_model_files() public_model_repo_url_prefix = codecs.decode("uggcf://tvguho.pbz/GEiyie/zbqry_ercb/eryrnfrf/qbjaybnq/nyy_choyvp_hie_zbqryf", "rot13") vip_model_repo_url_prefix = codecs.decode("uggcf://tvguho.pbz/Nawbx0109/nv_zntvp/eryrnfrf/qbjaybnq/i5", "rot13")
audio_separator_models_repo_url_prefix = codecs.decode("uggcf://tvguho.pbz/abznqxnenbxr/clguba-nhqvb-frcnengbe/eryrnfrf/qbjaybnq/zbqry-pbasvtf", "rot13")
yaml_config_filename = None
self.logger.debug(translations["search_model"].format(model_filename=model_filename))
for model_type, model_list in supported_model_files_grouped.items(): for model_friendly_name, model_download_list in model_list.items(): self.model_is_uvr_vip = "VIP" in model_friendly_name model_repo_url_prefix = vip_model_repo_url_prefix if self.model_is_uvr_vip else public_model_repo_url_prefix
if isinstance(model_download_list, str) and model_download_list == model_filename: self.logger.debug(translations["single_model"].format(model_friendly_name=model_friendly_name)) self.model_friendly_name = model_friendly_name
try: self.download_file_if_not_exists(f"{model_repo_url_prefix}/{model_filename}", model_path) except RuntimeError: self.logger.debug(translations["not_found_model"]) self.download_file_if_not_exists(f"{audio_separator_models_repo_url_prefix}/{model_filename}", model_path)
self.print_uvr_vip_message()
self.logger.debug(translations["single_model_path"].format(model_path=model_path))
return model_filename, model_type, model_friendly_name, model_path, yaml_config_filename elif isinstance(model_download_list, dict): this_model_matches_input_filename = False
for file_name, file_url in model_download_list.items(): if file_name == model_filename or file_url == model_filename: self.logger.debug(translations["find_model"].format(model_filename=model_filename, model_friendly_name=model_friendly_name)) this_model_matches_input_filename = True
if this_model_matches_input_filename: self.logger.debug(translations["find_models"].format(model_friendly_name=model_friendly_name)) self.model_friendly_name = model_friendly_name self.print_uvr_vip_message()
for config_key, config_value in model_download_list.items(): self.logger.debug(f"{translations['find_path']}: {config_key} -> {config_value}")
if config_value.startswith("http"): self.download_file_if_not_exists(config_value, os.path.join(self.model_file_dir, config_key)) elif config_key.endswith(".ckpt"): try: download_url = f"{model_repo_url_prefix}/{config_key}" self.download_file_if_not_exists(download_url, os.path.join(self.model_file_dir, config_key)) except RuntimeError: self.logger.debug(translations["not_found_model_warehouse"]) download_url = f"{audio_separator_models_repo_url_prefix}/{config_key}" self.download_file_if_not_exists(download_url, os.path.join(self.model_file_dir, config_key))
if model_filename.endswith(".yaml"): self.logger.warning(translations["yaml_warning"].format(model_filename=model_filename)) self.logger.warning(translations["yaml_warning_2"].format(config_key=config_key)) self.logger.warning(translations["yaml_warning_3"])
model_filename = config_key model_path = os.path.join(self.model_file_dir, f"{model_filename}")
yaml_config_filename = config_value yaml_config_filepath = os.path.join(self.model_file_dir, yaml_config_filename)
try: url = codecs.decode("uggcf://enj.tvguhohfrepbagrag.pbz/GEiyie/nccyvpngvba_qngn/znva/zqk_zbqry_qngn/zqk_p_pbasvtf", "rot13") yaml_config_url = f"{url}/{yaml_config_filename}" self.download_file_if_not_exists(f"{yaml_config_url}", yaml_config_filepath) except RuntimeError: self.logger.debug(translations["yaml_debug"]) yaml_config_url = f"{audio_separator_models_repo_url_prefix}/{yaml_config_filename}" self.download_file_if_not_exists(f"{yaml_config_url}", yaml_config_filepath) else: download_url = f"{model_repo_url_prefix}/{config_value}" self.download_file_if_not_exists(download_url, os.path.join(self.model_file_dir, config_value))
self.logger.debug(translations["download_model_friendly"].format(model_friendly_name=model_friendly_name, model_path=model_path))
return model_filename, model_type, model_friendly_name, model_path, yaml_config_filename
raise ValueError(translations["not_found_model_2"].format(model_filename=model_filename))
def load_model_data_from_yaml(self, yaml_config_filename): model_data_yaml_filepath = os.path.join(self.model_file_dir, yaml_config_filename) if not os.path.exists(yaml_config_filename) else yaml_config_filename
self.logger.debug(translations["load_yaml"].format(model_data_yaml_filepath=model_data_yaml_filepath))
model_data = yaml.load(open(model_data_yaml_filepath, encoding="utf-8"), Loader=yaml.FullLoader) self.logger.debug(translations["load_yaml_2"].format(model_data=model_data))
if "roformer" in model_data_yaml_filepath: model_data["is_roformer"] = True
return model_data
def load_model_data_using_hash(self, model_path): mdx_model_data_url = codecs.decode("uggcf://enj.tvguhohfrepbagrag.pbz/GEiyie/nccyvpngvba_qngn/znva/zqk_zbqry_qngn/zbqry_qngn_arj.wfba", "rot13")
self.logger.debug(translations["hash_md5"]) model_hash = self.get_model_hash(model_path) self.logger.debug(translations["model_hash"].format(model_path=model_path, model_hash=model_hash))
mdx_model_data_path = os.path.join(self.model_file_dir, "mdx_model_data.json") self.logger.debug(translations["mdx_data"].format(mdx_model_data_path=mdx_model_data_path)) self.download_file_if_not_exists(mdx_model_data_url, mdx_model_data_path)
self.logger.debug(translations["load_mdx"]) mdx_model_data_object = json.load(open(mdx_model_data_path, encoding="utf-8"))
if model_hash in mdx_model_data_object: model_data = mdx_model_data_object[model_hash] else: raise ValueError(translations["model_not_support"].format(model_hash=model_hash))
self.logger.debug(translations["uvr_json"].format(model_hash=model_hash, model_data=model_data))
return model_data
def load_model(self, model_filename): self.logger.info(translations["loading_model"].format(model_filename=model_filename))
load_model_start_time = time.perf_counter()
model_filename, model_type, model_friendly_name, model_path, yaml_config_filename = self.download_model_files(model_filename) model_name = model_filename.split(".")[0] self.logger.debug(translations["download_model_friendly_2"].format(model_friendly_name=model_friendly_name, model_path=model_path))
if model_path.lower().endswith(".yaml"): yaml_config_filename = model_path
model_data = self.load_model_data_from_yaml(yaml_config_filename) if yaml_config_filename is not None else self.load_model_data_using_hash(model_path)
common_params = { "logger": self.logger, "log_level": self.log_level, "torch_device": self.torch_device, "torch_device_cpu": self.torch_device_cpu, "torch_device_mps": self.torch_device_mps, "onnx_execution_provider": self.onnx_execution_provider, "model_name": model_name, "model_path": model_path, "model_data": model_data, "output_format": self.output_format, "output_bitrate": self.output_bitrate, "output_dir": self.output_dir, "normalization_threshold": self.normalization_threshold, "output_single_stem": self.output_single_stem, "invert_using_spec": self.invert_using_spec, "sample_rate": self.sample_rate, }
separator_classes = {"MDX": "mdx_separator.MDXSeparator", "Demucs": "demucs_separator.DemucsSeparator"}
if model_type not in self.arch_specific_params or model_type not in separator_classes: raise ValueError(translations["model_type_not_support"].format(model_type=model_type)) if model_type == "Demucs" and sys.version_info < (3, 10): raise Exception(translations["demucs_not_support_python<3.10"])
self.logger.debug(f"{translations['import_module']} {model_type}: {separator_classes[model_type]}")
module_name, class_name = separator_classes[model_type].split(".") module = import_module(f"main.library.architectures.{module_name}") separator_class = getattr(module, class_name)
self.logger.debug(f"{translations['initialization']} {model_type}: {separator_class}") self.model_instance = separator_class(common_config=common_params, arch_config=self.arch_specific_params[model_type])
self.logger.debug(translations["loading_model_success"]) self.logger.info(f"{translations['loading_model_duration']}: {time.strftime('%H:%M:%S', time.gmtime(int(time.perf_counter() - load_model_start_time)))}")
def separate(self, audio_file_path): self.logger.info(f"{translations['starting_separator']}: {audio_file_path}") separate_start_time = time.perf_counter()
self.logger.debug(translations["normalization"].format(normalization_threshold=self.normalization_threshold))
output_files = self.model_instance.separate(audio_file_path)
self.model_instance.clear_gpu_cache()
self.model_instance.clear_file_specific_paths()
self.print_uvr_vip_message()
self.logger.debug(translations["separator_success_3"]) self.logger.info(f"{translations['separator_duration']}: {time.strftime('%H:%M:%S', time.gmtime(int(time.perf_counter() - separate_start_time)))}")
return output_files
def download_model_and_data(self, model_filename): self.logger.info(translations["loading_separator_model"].format(model_filename=model_filename))
model_filename, model_type, model_friendly_name, model_path, yaml_config_filename = self.download_model_files(model_filename)
if model_path.lower().endswith(".yaml"): yaml_config_filename = model_path
model_data = self.load_model_data_from_yaml(yaml_config_filename) if yaml_config_filename is not None else self.load_model_data_using_hash(model_path)
model_data_dict_size = len(model_data)
self.logger.info(translations["downloading_model"].format(model_type=model_type, model_friendly_name=model_friendly_name, model_path=model_path, model_data_dict_size=model_data_dict_size))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590import osimport sysimport mathimport torch
from typing import Optionalfrom torch.nn.utils import remove_weight_normfrom torch.nn.utils.parametrizations import weight_norm
now_dir = os.getcwd()sys.path.append(now_dir)
from .modules import WaveNetfrom .residuals import ResidualCouplingBlock, ResBlock1, ResBlock2, LRELU_SLOPEfrom .commons import init_weights, slice_segments, rand_slice_segments, sequence_mask, convert_pad_shape
class Generator(torch.nn.Module): def __init__(self, initial_channel, resblock, resblock_kernel_sizes, resblock_dilation_sizes, upsample_rates, upsample_initial_channel, upsample_kernel_sizes, gin_channels=0): super(Generator, self).__init__()
self.num_kernels = len(resblock_kernel_sizes) self.num_upsamples = len(upsample_rates) self.conv_pre = torch.nn.Conv1d(initial_channel, upsample_initial_channel, 7, 1, padding=3) resblock = ResBlock1 if resblock == "1" else ResBlock2
self.ups_and_resblocks = torch.nn.ModuleList()
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)): self.ups_and_resblocks.append(weight_norm(torch.nn.ConvTranspose1d(upsample_initial_channel // (2**i), upsample_initial_channel // (2 ** (i + 1)), k, u, padding=(k - u) // 2)))
ch = upsample_initial_channel // (2 ** (i + 1))
for j, (k, d) in enumerate(zip(resblock_kernel_sizes, resblock_dilation_sizes)): self.ups_and_resblocks.append(resblock(ch, k, d))
self.conv_post = torch.nn.Conv1d(ch, 1, 7, 1, padding=3, bias=False) self.ups_and_resblocks.apply(init_weights)
if gin_channels != 0: self.cond = torch.nn.Conv1d(gin_channels, upsample_initial_channel, 1)
def forward(self, x: torch.Tensor, g: Optional[torch.Tensor] = None): x = self.conv_pre(x) if g is not None: x = x + self.cond(g)
resblock_idx = 0
for _ in range(self.num_upsamples): x = torch.nn.functional.leaky_relu(x, LRELU_SLOPE) x = self.ups_and_resblocks[resblock_idx](x) resblock_idx += 1 xs = 0
for _ in range(self.num_kernels): xs += self.ups_and_resblocks[resblock_idx](x) resblock_idx += 1
x = xs / self.num_kernels
x = torch.nn.functional.leaky_relu(x) x = self.conv_post(x) x = torch.tanh(x)
return x
def __prepare_scriptable__(self): for l in self.ups_and_resblocks: for hook in l._forward_pre_hooks.values(): if (hook.__module__ == "torch.nn.utils.parametrizations.weight_norm" and hook.__class__.__name__ == "WeightNorm"): torch.nn.utils.remove_weight_norm(l)
return self def remove_weight_norm(self): for l in self.ups_and_resblocks: remove_weight_norm(l)
class SineGen(torch.nn.Module): def __init__(self, samp_rate, harmonic_num=0, sine_amp=0.1, noise_std=0.003, voiced_threshold=0, flag_for_pulse=False): super(SineGen, self).__init__() self.sine_amp = sine_amp self.noise_std = noise_std self.harmonic_num = harmonic_num self.dim = self.harmonic_num + 1 self.sample_rate = samp_rate self.voiced_threshold = voiced_threshold
def _f02uv(self, f0): uv = torch.ones_like(f0) uv = uv * (f0 > self.voiced_threshold) return uv
def forward(self, f0: torch.Tensor, upp: int): with torch.no_grad(): f0 = f0[:, None].transpose(1, 2) f0_buf = torch.zeros(f0.shape[0], f0.shape[1], self.dim, device=f0.device) f0_buf[:, :, 0] = f0[:, :, 0] f0_buf[:, :, 1:] = (f0_buf[:, :, 0:1] * torch.arange(2, self.harmonic_num + 2, device=f0.device)[None, None, :]) rad_values = (f0_buf / float(self.sample_rate)) % 1 rand_ini = torch.rand(f0_buf.shape[0], f0_buf.shape[2], device=f0_buf.device) rand_ini[:, 0] = 0 rad_values[:, 0, :] = rad_values[:, 0, :] + rand_ini tmp_over_one = torch.cumsum(rad_values, 1) tmp_over_one *= upp tmp_over_one = torch.nn.functional.interpolate(tmp_over_one.transpose(2, 1), scale_factor=float(upp), mode="linear", align_corners=True).transpose(2, 1) rad_values = torch.nn.functional.interpolate(rad_values.transpose(2, 1), scale_factor=float(upp), mode="nearest").transpose(2, 1) tmp_over_one %= 1 tmp_over_one_idx = (tmp_over_one[:, 1:, :] - tmp_over_one[:, :-1, :]) < 0 cumsum_shift = torch.zeros_like(rad_values) cumsum_shift[:, 1:, :] = tmp_over_one_idx * -1.0 sine_waves = torch.sin(torch.cumsum(rad_values + cumsum_shift, dim=1) * 2 * torch.pi) sine_waves = sine_waves * self.sine_amp uv = self._f02uv(f0) uv = torch.nn.functional.interpolate(uv.transpose(2, 1), scale_factor=float(upp), mode="nearest").transpose(2, 1) noise_amp = uv * self.noise_std + (1 - uv) * self.sine_amp / 3 noise = noise_amp * torch.randn_like(sine_waves) sine_waves = sine_waves * uv + noise return sine_waves, uv, noise
class SourceModuleHnNSF(torch.nn.Module): def __init__(self, sample_rate, harmonic_num=0, sine_amp=0.1, add_noise_std=0.003, voiced_threshod=0, is_half=True): super(SourceModuleHnNSF, self).__init__()
self.sine_amp = sine_amp self.noise_std = add_noise_std self.is_half = is_half
self.l_sin_gen = SineGen(sample_rate, harmonic_num, sine_amp, add_noise_std, voiced_threshod) self.l_linear = torch.nn.Linear(harmonic_num + 1, 1) self.l_tanh = torch.nn.Tanh()
def forward(self, x: torch.Tensor, upsample_factor: int = 1): sine_wavs, uv, _ = self.l_sin_gen(x, upsample_factor) sine_wavs = sine_wavs.to(dtype=self.l_linear.weight.dtype) sine_merge = self.l_tanh(self.l_linear(sine_wavs)) return sine_merge, None, None
class GeneratorNSF(torch.nn.Module): def __init__(self, initial_channel, resblock, resblock_kernel_sizes, resblock_dilation_sizes, upsample_rates, upsample_initial_channel, upsample_kernel_sizes, gin_channels, sr, is_half=False): super(GeneratorNSF, self).__init__()
self.num_kernels = len(resblock_kernel_sizes) self.num_upsamples = len(upsample_rates) self.f0_upsamp = torch.nn.Upsample(scale_factor=math.prod(upsample_rates)) self.m_source = SourceModuleHnNSF(sample_rate=sr, harmonic_num=0, is_half=is_half)
self.conv_pre = torch.nn.Conv1d(initial_channel, upsample_initial_channel, 7, 1, padding=3) resblock_cls = ResBlock1 if resblock == "1" else ResBlock2
self.ups = torch.nn.ModuleList() self.noise_convs = torch.nn.ModuleList()
channels = [upsample_initial_channel // (2 ** (i + 1)) for i in range(len(upsample_rates))] stride_f0s = [math.prod(upsample_rates[i + 1 :]) if i + 1 < len(upsample_rates) else 1 for i in range(len(upsample_rates))]
for i, (u, k) in enumerate(zip(upsample_rates, upsample_kernel_sizes)): self.ups.append(weight_norm(torch.nn.ConvTranspose1d(upsample_initial_channel // (2**i), channels[i], k, u, padding=(k - u) // 2))) self.noise_convs.append(torch.nn.Conv1d(1, channels[i], kernel_size=(stride_f0s[i] * 2 if stride_f0s[i] > 1 else 1), stride=stride_f0s[i], padding=(stride_f0s[i] // 2 if stride_f0s[i] > 1 else 0)))
self.resblocks = torch.nn.ModuleList([resblock_cls(channels[i], k, d) for i in range(len(self.ups)) for k, d in zip(resblock_kernel_sizes, resblock_dilation_sizes)])
self.conv_post = torch.nn.Conv1d(channels[-1], 1, 7, 1, padding=3, bias=False) self.ups.apply(init_weights)
if gin_channels != 0: self.cond = torch.nn.Conv1d(gin_channels, upsample_initial_channel, 1)
self.upp = math.prod(upsample_rates) self.lrelu_slope = LRELU_SLOPE
def forward(self, x, f0, g: Optional[torch.Tensor] = None): har_source, _, _ = self.m_source(f0, self.upp) har_source = har_source.transpose(1, 2) x = self.conv_pre(x)
if g is not None: x = x + self.cond(g)
for i, (ups, noise_convs) in enumerate(zip(self.ups, self.noise_convs)): x = torch.nn.functional.leaky_relu(x, self.lrelu_slope) x = ups(x) x = x + noise_convs(har_source)
xs = sum([resblock(x) for j, resblock in enumerate(self.resblocks) if j in range(i * self.num_kernels, (i + 1) * self.num_kernels)]) x = xs / self.num_kernels
x = torch.nn.functional.leaky_relu(x) x = torch.tanh(self.conv_post(x)) return x
def remove_weight_norm(self): for l in self.ups: remove_weight_norm(l) for l in self.resblocks: l.remove_weight_norm()
def __prepare_scriptable__(self): for l in self.ups: for hook in l._forward_pre_hooks.values(): if (hook.__module__ == "torch.nn.utils.parametrizations.weight_norm" and hook.__class__.__name__ == "WeightNorm"): remove_weight_norm(l)
for l in self.resblocks: for hook in l._forward_pre_hooks.values(): if (hook.__module__ == "torch.nn.utils.parametrizations.weight_norm" and hook.__class__.__name__ == "WeightNorm"): remove_weight_norm(l) return self
class LayerNorm(torch.nn.Module): def __init__(self, channels, eps=1e-5): super().__init__() self.eps = eps self.gamma = torch.nn.Parameter(torch.ones(channels)) self.beta = torch.nn.Parameter(torch.zeros(channels))
def forward(self, x): x = x.transpose(1, -1) x = torch.nn.functional.layer_norm(x, (x.size(-1),), self.gamma, self.beta, self.eps) return x.transpose(1, -1)
class MultiHeadAttention(torch.nn.Module): def __init__(self, channels, out_channels, n_heads, p_dropout=0.0, window_size=None, heads_share=True, block_length=None, proximal_bias=False, proximal_init=False): super().__init__() assert channels % n_heads == 0
self.channels = channels self.out_channels = out_channels self.n_heads = n_heads self.p_dropout = p_dropout self.window_size = window_size self.heads_share = heads_share self.block_length = block_length self.proximal_bias = proximal_bias self.proximal_init = proximal_init self.attn = None self.k_channels = channels // n_heads self.conv_q = torch.nn.Conv1d(channels, channels, 1) self.conv_k = torch.nn.Conv1d(channels, channels, 1) self.conv_v = torch.nn.Conv1d(channels, channels, 1) self.conv_o = torch.nn.Conv1d(channels, out_channels, 1) self.drop = torch.nn.Dropout(p_dropout)
if window_size is not None: n_heads_rel = 1 if heads_share else n_heads rel_stddev = self.k_channels**-0.5
self.emb_rel_k = torch.nn.Parameter(torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels) * rel_stddev) self.emb_rel_v = torch.nn.Parameter(torch.randn(n_heads_rel, window_size * 2 + 1, self.k_channels) * rel_stddev)
torch.nn.init.xavier_uniform_(self.conv_q.weight) torch.nn.init.xavier_uniform_(self.conv_k.weight) torch.nn.init.xavier_uniform_(self.conv_v.weight)
if proximal_init: with torch.no_grad(): self.conv_k.weight.copy_(self.conv_q.weight) self.conv_k.bias.copy_(self.conv_q.bias)
def forward(self, x, c, attn_mask=None): q = self.conv_q(x) k = self.conv_k(c) v = self.conv_v(c)
x, self.attn = self.attention(q, k, v, mask=attn_mask)
x = self.conv_o(x) return x
def attention(self, query, key, value, mask=None): b, d, t_s, t_t = (*key.size(), query.size(2)) query = query.view(b, self.n_heads, self.k_channels, t_t).transpose(2, 3) key = key.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3) value = value.view(b, self.n_heads, self.k_channels, t_s).transpose(2, 3)
scores = torch.matmul(query / math.sqrt(self.k_channels), key.transpose(-2, -1)) if self.window_size is not None: assert (t_s == t_t), "(t_s == t_t)" key_relative_embeddings = self._get_relative_embeddings(self.emb_rel_k, t_s) rel_logits = self._matmul_with_relative_keys(query / math.sqrt(self.k_channels), key_relative_embeddings) scores_local = self._relative_position_to_absolute_position(rel_logits) scores = scores + scores_local
if self.proximal_bias: assert t_s == t_t, "t_s == t_t" scores = scores + self._attention_bias_proximal(t_s).to(device=scores.device, dtype=scores.dtype)
if mask is not None: scores = scores.masked_fill(mask == 0, -1e4) if self.block_length is not None: assert (t_s == t_t), "(t_s == t_t)"
block_mask = (torch.ones_like(scores).triu(-self.block_length).tril(self.block_length)) scores = scores.masked_fill(block_mask == 0, -1e4)
p_attn = torch.nn.functional.softmax(scores, dim=-1) p_attn = self.drop(p_attn) output = torch.matmul(p_attn, value)
if self.window_size is not None: relative_weights = self._absolute_position_to_relative_position(p_attn) value_relative_embeddings = self._get_relative_embeddings(self.emb_rel_v, t_s) output = output + self._matmul_with_relative_values(relative_weights, value_relative_embeddings)
output = (output.transpose(2, 3).contiguous().view(b, d, t_t)) return output, p_attn
def _matmul_with_relative_values(self, x, y): ret = torch.matmul(x, y.unsqueeze(0)) return ret
def _matmul_with_relative_keys(self, x, y): ret = torch.matmul(x, y.unsqueeze(0).transpose(-2, -1)) return ret
def _get_relative_embeddings(self, relative_embeddings, length): pad_length = max(length - (self.window_size + 1), 0) slice_start_position = max((self.window_size + 1) - length, 0) slice_end_position = slice_start_position + 2 * length - 1
if pad_length > 0: padded_relative_embeddings = torch.nn.functional.pad(relative_embeddings, convert_pad_shape([[0, 0], [pad_length, pad_length], [0, 0]])) else: padded_relative_embeddings = relative_embeddings
used_relative_embeddings = padded_relative_embeddings[:, slice_start_position:slice_end_position] return used_relative_embeddings
def _relative_position_to_absolute_position(self, x): batch, heads, length, _ = x.size()
x = torch.nn.functional.pad(x, convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, 1]]))
x_flat = x.view([batch, heads, length * 2 * length]) x_flat = torch.nn.functional.pad(x_flat, convert_pad_shape([[0, 0], [0, 0], [0, length - 1]]))
x_final = x_flat.view([batch, heads, length + 1, 2 * length - 1])[:, :, :length, length - 1 :]
return x_final
def _absolute_position_to_relative_position(self, x): batch, heads, length, _ = x.size() x = torch.nn.functional.pad(x, convert_pad_shape([[0, 0], [0, 0], [0, 0], [0, length - 1]])) x_flat = x.view([batch, heads, length**2 + length * (length - 1)]) x_flat = torch.nn.functional.pad(x_flat, convert_pad_shape([[0, 0], [0, 0], [length, 0]])) x_final = x_flat.view([batch, heads, length, 2 * length])[:, :, :, 1:] return x_final
def _attention_bias_proximal(self, length): r = torch.arange(length, dtype=torch.float32) diff = torch.unsqueeze(r, 0) - torch.unsqueeze(r, 1) return torch.unsqueeze(torch.unsqueeze(-torch.log1p(torch.abs(diff)), 0), 0)
class FFN(torch.nn.Module): def __init__(self, in_channels, out_channels, filter_channels, kernel_size, p_dropout=0.0, activation=None, causal=False): super().__init__() self.in_channels = in_channels self.out_channels = out_channels self.filter_channels = filter_channels self.kernel_size = kernel_size self.p_dropout = p_dropout self.activation = activation self.causal = causal
if causal: self.padding = self._causal_padding else: self.padding = self._same_padding
self.conv_1 = torch.nn.Conv1d(in_channels, filter_channels, kernel_size) self.conv_2 = torch.nn.Conv1d(filter_channels, out_channels, kernel_size) self.drop = torch.nn.Dropout(p_dropout)
def forward(self, x, x_mask): x = self.conv_1(self.padding(x * x_mask))
if self.activation == "gelu": x = x * torch.sigmoid(1.702 * x) else: x = torch.relu(x)
x = self.drop(x) x = self.conv_2(self.padding(x * x_mask)) return x * x_mask
def _causal_padding(self, x): if self.kernel_size == 1: return x
pad_l = self.kernel_size - 1 pad_r = 0 padding = [[0, 0], [0, 0], [pad_l, pad_r]] x = torch.nn.functional.pad(x, convert_pad_shape(padding)) return x
def _same_padding(self, x): if self.kernel_size == 1: return x pad_l = (self.kernel_size - 1) // 2 pad_r = self.kernel_size // 2
padding = [[0, 0], [0, 0], [pad_l, pad_r]] x = torch.nn.functional.pad(x, convert_pad_shape(padding))
return x
class Encoder(torch.nn.Module): def __init__(self, hidden_channels, filter_channels, n_heads, n_layers, kernel_size=1, p_dropout=0.0, window_size=10, **kwargs): super().__init__() self.hidden_channels = hidden_channels self.filter_channels = filter_channels self.n_heads = n_heads self.n_layers = n_layers self.kernel_size = kernel_size self.p_dropout = p_dropout self.window_size = window_size
self.drop = torch.nn.Dropout(p_dropout) self.attn_layers = torch.nn.ModuleList() self.norm_layers_1 = torch.nn.ModuleList() self.ffn_layers = torch.nn.ModuleList() self.norm_layers_2 = torch.nn.ModuleList()
for _ in range(self.n_layers): self.attn_layers.append(MultiHeadAttention(hidden_channels, hidden_channels, n_heads, p_dropout=p_dropout, window_size=window_size)) self.norm_layers_1.append(LayerNorm(hidden_channels)) self.ffn_layers.append(FFN(hidden_channels, hidden_channels, filter_channels, kernel_size, p_dropout=p_dropout)) self.norm_layers_2.append(LayerNorm(hidden_channels))
def forward(self, x, x_mask): attn_mask = x_mask.unsqueeze(2) * x_mask.unsqueeze(-1) x = x * x_mask
for i in range(self.n_layers): y = self.attn_layers[i](x, x, attn_mask) y = self.drop(y) x = self.norm_layers_1[i](x + y)
y = self.ffn_layers[i](x, x_mask) y = self.drop(y) x = self.norm_layers_2[i](x + y)
x = x * x_mask return x
class TextEncoder(torch.nn.Module): def __init__(self, out_channels, hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout, embedding_dim, f0=True): super(TextEncoder, self).__init__() self.out_channels = out_channels self.hidden_channels = hidden_channels self.filter_channels = filter_channels self.n_heads = n_heads self.n_layers = n_layers self.kernel_size = kernel_size self.p_dropout = float(p_dropout) self.emb_phone = torch.nn.Linear(embedding_dim, hidden_channels) self.lrelu = torch.nn.LeakyReLU(0.1, inplace=True)
if f0: self.emb_pitch = torch.nn.Embedding(256, hidden_channels)
self.encoder = Encoder(hidden_channels, filter_channels, n_heads, n_layers, kernel_size, float(p_dropout)) self.proj = torch.nn.Conv1d(hidden_channels, out_channels * 2, 1)
def forward(self, phone: torch.Tensor, pitch: Optional[torch.Tensor], lengths: torch.Tensor): if pitch is None: x = self.emb_phone(phone) else: x = self.emb_phone(phone) + self.emb_pitch(pitch)
x = x * math.sqrt(self.hidden_channels) x = self.lrelu(x) x = torch.transpose(x, 1, -1) x_mask = torch.unsqueeze(sequence_mask(lengths, x.size(2)), 1).to(x.dtype) x = self.encoder(x * x_mask, x_mask) stats = self.proj(x) * x_mask
m, logs = torch.split(stats, self.out_channels, dim=1) return m, logs, x_mask
class PosteriorEncoder(torch.nn.Module): def __init__(self, in_channels, out_channels, hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=0): super(PosteriorEncoder, self).__init__() self.in_channels = in_channels self.out_channels = out_channels self.hidden_channels = hidden_channels self.kernel_size = kernel_size self.dilation_rate = dilation_rate self.n_layers = n_layers self.gin_channels = gin_channels
self.pre = torch.nn.Conv1d(in_channels, hidden_channels, 1) self.enc = WaveNet(hidden_channels, kernel_size, dilation_rate, n_layers, gin_channels=gin_channels) self.proj = torch.nn.Conv1d(hidden_channels, out_channels * 2, 1)
def forward(self, x: torch.Tensor, x_lengths: torch.Tensor, g: Optional[torch.Tensor] = None): x_mask = torch.unsqueeze(sequence_mask(x_lengths, x.size(2)), 1).to(x.dtype) x = self.pre(x) * x_mask x = self.enc(x, x_mask, g=g) stats = self.proj(x) * x_mask m, logs = torch.split(stats, self.out_channels, dim=1) z = (m + torch.randn_like(m) * torch.exp(logs)) * x_mask return z, m, logs, x_mask
def remove_weight_norm(self): self.enc.remove_weight_norm()
def __prepare_scriptable__(self): for hook in self.enc._forward_pre_hooks.values(): if (hook.__module__ == "torch.nn.utils.parametrizations.weight_norm" and hook.__class__.__name__ == "WeightNorm"): torch.nn.utils.remove_weight_norm(self.enc)
return self
class Synthesizer(torch.nn.Module): def __init__(self, spec_channels, segment_size, inter_channels, hidden_channels, filter_channels, n_heads, n_layers, kernel_size, p_dropout, resblock, resblock_kernel_sizes, resblock_dilation_sizes, upsample_rates, upsample_initial_channel, upsample_kernel_sizes, spk_embed_dim, gin_channels, sr, use_f0, text_enc_hidden_dim=768, **kwargs): super(Synthesizer, self).__init__() self.spec_channels = spec_channels self.inter_channels = inter_channels self.hidden_channels = hidden_channels self.filter_channels = filter_channels self.n_heads = n_heads self.n_layers = n_layers self.kernel_size = kernel_size self.p_dropout = float(p_dropout) self.resblock = resblock self.resblock_kernel_sizes = resblock_kernel_sizes self.resblock_dilation_sizes = resblock_dilation_sizes self.upsample_rates = upsample_rates self.upsample_initial_channel = upsample_initial_channel self.upsample_kernel_sizes = upsample_kernel_sizes self.segment_size = segment_size self.gin_channels = gin_channels self.spk_embed_dim = spk_embed_dim self.use_f0 = use_f0
self.enc_p = TextEncoder(inter_channels, hidden_channels, filter_channels, n_heads, n_layers, kernel_size, float(p_dropout), text_enc_hidden_dim, f0=use_f0)
if use_f0: self.dec = GeneratorNSF(inter_channels, resblock, resblock_kernel_sizes, resblock_dilation_sizes, upsample_rates, upsample_initial_channel, upsample_kernel_sizes, gin_channels=gin_channels, sr=sr, is_half=kwargs["is_half"]) else: self.dec = Generator(inter_channels, resblock, resblock_kernel_sizes, resblock_dilation_sizes, upsample_rates, upsample_initial_channel, upsample_kernel_sizes, gin_channels=gin_channels)
self.enc_q = PosteriorEncoder(spec_channels, inter_channels, hidden_channels, 5, 1, 16, gin_channels=gin_channels) self.flow = ResidualCouplingBlock(inter_channels, hidden_channels, 5, 1, 3, gin_channels=gin_channels) self.emb_g = torch.nn.Embedding(self.spk_embed_dim, gin_channels)
def remove_weight_norm(self): self.dec.remove_weight_norm() self.flow.remove_weight_norm() self.enc_q.remove_weight_norm()
def __prepare_scriptable__(self): for hook in self.dec._forward_pre_hooks.values(): if (hook.__module__ == "torch.nn.utils.parametrizations.weight_norm" and hook.__class__.__name__ == "WeightNorm"): torch.nn.utils.remove_weight_norm(self.dec)
for hook in self.flow._forward_pre_hooks.values(): if (hook.__module__ == "torch.nn.utils.parametrizations.weight_norm" and hook.__class__.__name__ == "WeightNorm"): torch.nn.utils.remove_weight_norm(self.flow)
if hasattr(self, "enc_q"): for hook in self.enc_q._forward_pre_hooks.values(): if (hook.__module__ == "torch.nn.utils.parametrizations.weight_norm" and hook.__class__.__name__ == "WeightNorm"): torch.nn.utils.remove_weight_norm(self.enc_q)
return self
@torch.jit.ignore def forward(self, phone: torch.Tensor, phone_lengths: torch.Tensor, pitch: Optional[torch.Tensor] = None, pitchf: Optional[torch.Tensor] = None, y: torch.Tensor = None, y_lengths: torch.Tensor = None, ds: Optional[torch.Tensor] = None): g = self.emb_g(ds).unsqueeze(-1) m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths)
if y is not None: z, m_q, logs_q, y_mask = self.enc_q(y, y_lengths, g=g) z_p = self.flow(z, y_mask, g=g) z_slice, ids_slice = rand_slice_segments(z, y_lengths, self.segment_size)
if self.use_f0: pitchf = slice_segments(pitchf, ids_slice, self.segment_size, 2) o = self.dec(z_slice, pitchf, g=g) else: o = self.dec(z_slice, g=g)
return o, ids_slice, x_mask, y_mask, (z, z_p, m_p, logs_p, m_q, logs_q) else: return None, None, x_mask, None, (None, None, m_p, logs_p, None, None)
@torch.jit.export def infer(self, phone: torch.Tensor, phone_lengths: torch.Tensor, pitch: Optional[torch.Tensor] = None, nsff0: Optional[torch.Tensor] = None, sid: torch.Tensor = None, rate: Optional[torch.Tensor] = None): g = self.emb_g(sid).unsqueeze(-1) m_p, logs_p, x_mask = self.enc_p(phone, pitch, phone_lengths) z_p = (m_p + torch.exp(logs_p) * torch.randn_like(m_p) * 0.66666) * x_mask
if rate is not None: assert isinstance(rate, torch.Tensor) head = int(z_p.shape[2] * (1.0 - rate.item())) z_p = z_p[:, :, head:] x_mask = x_mask[:, :, head:]
if self.use_f0: nsff0 = nsff0[:, head:]
if self.use_f0: z = self.flow(z_p, x_mask, g=g, reverse=True) o = self.dec(z * x_mask, nsff0, g=g) else: z = self.flow(z_p, x_mask, g=g, reverse=True) o = self.dec(z * x_mask, g=g)
return o, x_mask, (z, z_p, m_p, logs_p)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340import osimport sysimport yamlimport torch
import numpy as npimport typing as tp
from pathlib import Pathfrom hashlib import sha256
now_dir = os.getcwd()sys.path.append(now_dir)
from main.configs.config import Configfrom main.library.uvr5_separator import spec_utilsfrom main.library.uvr5_separator.demucs.hdemucs import HDemucsfrom main.library.uvr5_separator.demucs.states import load_modelfrom main.library.uvr5_separator.demucs.apply import BagOfModels, Modelfrom main.library.uvr5_separator.common_separator import CommonSeparatorfrom main.library.uvr5_separator.demucs.apply import apply_model, demucs_segments
translations = Config().translations
DEMUCS_4_SOURCE = ["drums", "bass", "other", "vocals"]
DEMUCS_2_SOURCE_MAPPER = { CommonSeparator.INST_STEM: 0, CommonSeparator.VOCAL_STEM: 1}
DEMUCS_4_SOURCE_MAPPER = { CommonSeparator.BASS_STEM: 0, CommonSeparator.DRUM_STEM: 1, CommonSeparator.OTHER_STEM: 2, CommonSeparator.VOCAL_STEM: 3}
DEMUCS_6_SOURCE_MAPPER = { CommonSeparator.BASS_STEM: 0, CommonSeparator.DRUM_STEM: 1, CommonSeparator.OTHER_STEM: 2, CommonSeparator.VOCAL_STEM: 3, CommonSeparator.GUITAR_STEM: 4, CommonSeparator.PIANO_STEM: 5,}
REMOTE_ROOT = Path(__file__).parent / "remote"
PRETRAINED_MODELS = { "demucs": "e07c671f", "demucs48_hq": "28a1282c", "demucs_extra": "3646af93", "demucs_quantized": "07afea75", "tasnet": "beb46fac", "tasnet_extra": "df3777b2", "demucs_unittest": "09ebc15f",}
sys.path.insert(0, os.path.join(os.getcwd(), "main", "library", "uvr5_separator"))
AnyModel = tp.Union[Model, BagOfModels]
class DemucsSeparator(CommonSeparator): def __init__(self, common_config, arch_config): super().__init__(config=common_config)
self.segment_size = arch_config.get("segment_size", "Default") self.shifts = arch_config.get("shifts", 2) self.overlap = arch_config.get("overlap", 0.25) self.segments_enabled = arch_config.get("segments_enabled", True)
self.logger.debug(translations["demucs_info"].format(segment_size=self.segment_size, segments_enabled=self.segments_enabled)) self.logger.debug(translations["demucs_info_2"].format(shifts=self.shifts, overlap=self.overlap))
self.demucs_source_map = DEMUCS_4_SOURCE_MAPPER
self.audio_file_path = None self.audio_file_base = None self.demucs_model_instance = None
self.logger.info(translations["start_demucs"])
def separate(self, audio_file_path): self.logger.debug(translations["start_separator"]) source = None stem_source = None
inst_source = {}
self.audio_file_path = audio_file_path self.audio_file_base = os.path.splitext(os.path.basename(audio_file_path))[0]
self.logger.debug(translations["prepare_mix"]) mix = self.prepare_mix(self.audio_file_path)
self.logger.debug(translations["demix"].format(shape=mix.shape))
self.logger.debug(translations["cancel_mix"])
self.demucs_model_instance = HDemucs(sources=DEMUCS_4_SOURCE) self.demucs_model_instance = get_demucs_model(name=os.path.splitext(os.path.basename(self.model_path))[0], repo=Path(os.path.dirname(self.model_path))) self.demucs_model_instance = demucs_segments(self.segment_size, self.demucs_model_instance) self.demucs_model_instance.to(self.torch_device) self.demucs_model_instance.eval()
self.logger.debug(translations["model_review"])
source = self.demix_demucs(mix)
del self.demucs_model_instance self.clear_gpu_cache() self.logger.debug(translations["del_gpu_cache_after_demix"])
output_files = [] self.logger.debug(translations["process_output_file"])
if isinstance(inst_source, np.ndarray): self.logger.debug(translations["process_ver"]) source_reshape = spec_utils.reshape_sources(inst_source[self.demucs_source_map[CommonSeparator.VOCAL_STEM]], source[self.demucs_source_map[CommonSeparator.VOCAL_STEM]]) inst_source[self.demucs_source_map[CommonSeparator.VOCAL_STEM]] = source_reshape source = inst_source
if isinstance(source, np.ndarray): source_length = len(source) self.logger.debug(translations["source_length"].format(source_length=source_length))
match source_length: case 2: self.logger.debug(translations["set_map"].format(part="2")) self.demucs_source_map = DEMUCS_2_SOURCE_MAPPER case 6: self.logger.debug(translations["set_map"].format(part="6")) self.demucs_source_map = DEMUCS_6_SOURCE_MAPPER case _: self.logger.debug(translations["set_map"].format(part="2")) self.demucs_source_map = DEMUCS_4_SOURCE_MAPPER
self.logger.debug(translations["process_all_part"])
for stem_name, stem_value in self.demucs_source_map.items(): if self.output_single_stem is not None: if stem_name.lower() != self.output_single_stem.lower(): self.logger.debug(translations["skip_part"].format(stem_name=stem_name, output_single_stem=self.output_single_stem)) continue
stem_path = os.path.join(f"{self.audio_file_base}_({stem_name})_{self.model_name}.{self.output_format.lower()}") stem_source = source[stem_value].T
self.final_process(stem_path, stem_source, stem_name) output_files.append(stem_path)
return output_files
def demix_demucs(self, mix): self.logger.debug(translations["starting_demix_demucs"])
processed = {} mix = torch.tensor(mix, dtype=torch.float32) ref = mix.mean(0) mix = (mix - ref.mean()) / ref.std() mix_infer = mix
with torch.no_grad(): self.logger.debug(translations["model_infer"]) sources = apply_model(model=self.demucs_model_instance, mix=mix_infer[None], shifts=self.shifts, split=self.segments_enabled, overlap=self.overlap, static_shifts=1 if self.shifts == 0 else self.shifts, set_progress_bar=None, device=self.torch_device, progress=True)[0]
sources = (sources * ref.std() + ref.mean()).cpu().numpy() sources[[0, 1]] = sources[[1, 0]]
processed[mix] = sources[:, :, 0:None].copy()
sources = list(processed.values()) sources = [s[:, :, 0:None] for s in sources] sources = np.concatenate(sources, axis=-1) return sources
class ModelOnlyRepo: def has_model(self, sig: str) -> bool: raise NotImplementedError()
def get_model(self, sig: str) -> Model: raise NotImplementedError()
class RemoteRepo(ModelOnlyRepo): def __init__(self, models: tp.Dict[str, str]): self._models = models
def has_model(self, sig: str) -> bool: return sig in self._models
def get_model(self, sig: str) -> Model: try: url = self._models[sig] except KeyError: raise RuntimeError(translations["not_found_model_signature"].format(sig=sig)) pkg = torch.hub.load_state_dict_from_url(url, map_location="cpu", check_hash=True) return load_model(pkg)
class LocalRepo(ModelOnlyRepo): def __init__(self, root: Path): self.root = root self.scan()
def scan(self): self._models = {} self._checksums = {}
for file in self.root.iterdir(): if file.suffix == ".th": if "-" in file.stem: xp_sig, checksum = file.stem.split("-") self._checksums[xp_sig] = checksum else: xp_sig = file.stem
if xp_sig in self._models: raise RuntimeError(translations["del_all_but_one"].format(xp_sig=xp_sig)) self._models[xp_sig] = file
def has_model(self, sig: str) -> bool: return sig in self._models
def get_model(self, sig: str) -> Model: try: file = self._models[sig] except KeyError: raise RuntimeError(translations["not_found_model_signature"].format(sig=sig)) if sig in self._checksums: check_checksum(file, self._checksums[sig])
return load_model(file)
class BagOnlyRepo: def __init__(self, root: Path, model_repo: ModelOnlyRepo): self.root = root self.model_repo = model_repo self.scan()
def scan(self): self._bags = {}
for file in self.root.iterdir(): if file.suffix == ".yaml": self._bags[file.stem] = file
def has_model(self, name: str) -> bool: return name in self._bags
def get_model(self, name: str) -> BagOfModels: try: yaml_file = self._bags[name] except KeyError: raise RuntimeError(translations["name_not_pretrained"].format(name=name)) bag = yaml.safe_load(open(yaml_file)) signatures = bag["models"] models = [self.model_repo.get_model(sig) for sig in signatures]
weights = bag.get("weights") segment = bag.get("segment")
return BagOfModels(models, weights, segment)
class AnyModelRepo: def __init__(self, model_repo: ModelOnlyRepo, bag_repo: BagOnlyRepo): self.model_repo = model_repo self.bag_repo = bag_repo
def has_model(self, name_or_sig: str) -> bool: return self.model_repo.has_model(name_or_sig) or self.bag_repo.has_model(name_or_sig)
def get_model(self, name_or_sig: str) -> AnyModel: if self.model_repo.has_model(name_or_sig): return self.model_repo.get_model(name_or_sig) else: return self.bag_repo.get_model(name_or_sig)
def check_checksum(path: Path, checksum: str): sha = sha256()
with open(path, "rb") as file: while 1: buf = file.read(2**20) if not buf: break
sha.update(buf)
actual_checksum = sha.hexdigest()[: len(checksum)]
if actual_checksum != checksum: raise RuntimeError(translations["invalid_checksum"].format(path=path, checksum=checksum, actual_checksum=actual_checksum))
def _parse_remote_files(remote_file_list) -> tp.Dict[str, str]: root: str = "" models: tp.Dict[str, str] = {}
for line in remote_file_list.read_text().split("\n"): line = line.strip()
if line.startswith("#"): continue elif line.startswith("root:"): root = line.split(":", 1)[1].strip() else: sig = line.split("-", 1)[0] assert sig not in models
models[sig] = "https://dl.fbaipublicfiles.com/demucs/mdx_final/" + root + line
return models
def get_demucs_model(name: str, repo: tp.Optional[Path] = None): if name == "demucs_unittest": return HDemucs(channels=4, sources=DEMUCS_4_SOURCE)
model_repo: ModelOnlyRepo
if repo is None: models = _parse_remote_files(REMOTE_ROOT / "files.txt") model_repo = RemoteRepo(models) bag_repo = BagOnlyRepo(REMOTE_ROOT, model_repo) else: if not repo.is_dir(): print(translations["repo_must_be_folder"].format(repo=repo))
model_repo = LocalRepo(repo) bag_repo = BagOnlyRepo(repo, model_repo)
any_repo = AnyModelRepo(model_repo, bag_repo)
model = any_repo.get_model(name) model.eval()
return model123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370import osimport sysimport onnximport torchimport platformimport onnx2torch
import numpy as npimport onnxruntime as ort
from tqdm import tqdm
now_dir = os.getcwd()sys.path.append(now_dir)
from main.configs.config import Configfrom main.library.uvr5_separator import spec_utilsfrom main.library.uvr5_separator.common_separator import CommonSeparator
translations = Config().translations
class MDXSeparator(CommonSeparator): def __init__(self, common_config, arch_config): super().__init__(config=common_config)
self.segment_size = arch_config.get("segment_size") self.overlap = arch_config.get("overlap") self.batch_size = arch_config.get("batch_size", 1) self.hop_length = arch_config.get("hop_length") self.enable_denoise = arch_config.get("enable_denoise") self.logger.debug(translations["mdx_info"].format(batch_size=self.batch_size, segment_size=self.segment_size)) self.logger.debug(translations["mdx_info_2"].format(overlap=self.overlap, hop_length=self.hop_length, enable_denoise=self.enable_denoise)) self.compensate = self.model_data["compensate"] self.dim_f = self.model_data["mdx_dim_f_set"] self.dim_t = 2 ** self.model_data["mdx_dim_t_set"] self.n_fft = self.model_data["mdx_n_fft_scale_set"] self.config_yaml = self.model_data.get("config_yaml", None) self.logger.debug(f"{translations['mdx_info_3']}: compensate = {self.compensate}, dim_f = {self.dim_f}, dim_t = {self.dim_t}, n_fft = {self.n_fft}") self.logger.debug(f"{translations['mdx_info_3']}: config_yaml = {self.config_yaml}") self.load_model() self.n_bins = 0 self.trim = 0 self.chunk_size = 0 self.gen_size = 0 self.stft = None self.primary_source = None self.secondary_source = None self.audio_file_path = None self.audio_file_base = None
def load_model(self): self.logger.debug(translations["load_model_onnx"])
if self.segment_size == self.dim_t: ort_session_options = ort.SessionOptions()
if self.log_level > 10: ort_session_options.log_severity_level = 3 else: ort_session_options.log_severity_level = 0
ort_inference_session = ort.InferenceSession(self.model_path, providers=self.onnx_execution_provider, sess_options=ort_session_options) self.model_run = lambda spek: ort_inference_session.run(None, {"input": spek.cpu().numpy()})[0] self.logger.debug(translations["load_model_onnx_success"]) else: if platform.system() == 'Windows': onnx_model = onnx.load(self.model_path) self.model_run = onnx2torch.convert(onnx_model) else: self.model_run = onnx2torch.convert(self.model_path) self.model_run.to(self.torch_device).eval() self.logger.warning(translations["onnx_to_pytorch"])
def separate(self, audio_file_path): self.audio_file_path = audio_file_path self.audio_file_base = os.path.splitext(os.path.basename(audio_file_path))[0]
self.logger.debug(translations["mix"].format(audio_file_path=self.audio_file_path)) mix = self.prepare_mix(self.audio_file_path)
self.logger.debug(translations["normalization_demix"]) mix = spec_utils.normalize(wave=mix, max_peak=self.normalization_threshold)
source = self.demix(mix) self.logger.debug(translations["mix_success"])
output_files = [] self.logger.debug(translations["process_output_file"])
if not isinstance(self.primary_source, np.ndarray): self.logger.debug(translations["primary_source"]) self.primary_source = spec_utils.normalize(wave=source, max_peak=self.normalization_threshold).T if not isinstance(self.secondary_source, np.ndarray): self.logger.debug(translations["secondary_source"]) raw_mix = self.demix(mix, is_match_mix=True)
if self.invert_using_spec: self.logger.debug(translations["invert_using_spec"]) self.secondary_source = spec_utils.invert_stem(raw_mix, source) else: self.logger.debug(translations["invert_using_spec_2"]) self.secondary_source = mix.T - source.T
if not self.output_single_stem or self.output_single_stem.lower() == self.secondary_stem_name.lower(): self.secondary_stem_output_path = os.path.join(f"{self.audio_file_base}_({self.secondary_stem_name})_{self.model_name}.{self.output_format.lower()}") self.logger.info(translations["save_secondary_stem_output_path"].format(stem_name=self.secondary_stem_name, stem_output_path=self.secondary_stem_output_path)) self.final_process(self.secondary_stem_output_path, self.secondary_source, self.secondary_stem_name) output_files.append(self.secondary_stem_output_path)
if not self.output_single_stem or self.output_single_stem.lower() == self.primary_stem_name.lower(): self.primary_stem_output_path = os.path.join(f"{self.audio_file_base}_({self.primary_stem_name})_{self.model_name}.{self.output_format.lower()}")
if not isinstance(self.primary_source, np.ndarray): self.primary_source = source.T
self.logger.info(translations["save_secondary_stem_output_path"].format(stem_name=self.primary_stem_name, stem_output_path=self.primary_stem_output_path)) self.final_process(self.primary_stem_output_path, self.primary_source, self.primary_stem_name) output_files.append(self.primary_stem_output_path)
return output_files
def initialize_model_settings(self): self.logger.debug(translations["starting_model"])
self.n_bins = self.n_fft // 2 + 1 self.trim = self.n_fft // 2
self.chunk_size = self.hop_length * (self.segment_size - 1) self.gen_size = self.chunk_size - 2 * self.trim
self.stft = STFT(self.logger, self.n_fft, self.hop_length, self.dim_f, self.torch_device)
self.logger.debug(f"{translations['input_info']}: n_fft = {self.n_fft} hop_length = {self.hop_length} dim_f = {self.dim_f}") self.logger.debug(f"{translations['model_settings']}: n_bins = {self.n_bins}, Trim = {self.trim}, chunk_size = {self.chunk_size}, gen_size = {self.gen_size}")
def initialize_mix(self, mix, is_ckpt=False): self.logger.debug(translations["initialize_mix"].format(is_ckpt=is_ckpt, shape=mix.shape))
if mix.shape[0] != 2: error_message = translations["!=2"].format(shape=mix.shape[0]) self.logger.error(error_message) raise ValueError(error_message)
if is_ckpt: self.logger.debug(translations["process_check"]) pad = self.gen_size + self.trim - (mix.shape[-1] % self.gen_size) self.logger.debug(f"{translations['cache']}: {pad}")
mixture = np.concatenate((np.zeros((2, self.trim), dtype="float32"), mix, np.zeros((2, pad), dtype="float32")), 1)
num_chunks = mixture.shape[-1] // self.gen_size self.logger.debug(translations["shape"].format(shape=mixture.shape, num_chunks=num_chunks))
mix_waves = [mixture[:, i * self.gen_size : i * self.gen_size + self.chunk_size] for i in range(num_chunks)] else: self.logger.debug(translations["process_no_check"]) mix_waves = [] n_sample = mix.shape[1]
pad = self.gen_size - n_sample % self.gen_size self.logger.debug(translations["n_sample_or_pad"].format(n_sample=n_sample, pad=pad))
mix_p = np.concatenate((np.zeros((2, self.trim)), mix, np.zeros((2, pad)), np.zeros((2, self.trim))), 1) self.logger.debug(f"{translations['shape_2']}: {mix_p.shape}")
i = 0 while i < n_sample + pad: waves = np.array(mix_p[:, i : i + self.chunk_size]) mix_waves.append(waves)
self.logger.debug(translations["process_part"].format(mix_waves=len(mix_waves), i=i, ii=i + self.chunk_size)) i += self.gen_size
mix_waves_tensor = torch.tensor(mix_waves, dtype=torch.float32).to(self.torch_device) self.logger.debug(translations["mix_waves_to_tensor"].format(shape=mix_waves_tensor.shape))
return mix_waves_tensor, pad
def demix(self, mix, is_match_mix=False): self.logger.debug(f"{translations['demix_is_match_mix']}: {is_match_mix}...") self.initialize_model_settings()
org_mix = mix self.logger.debug(f"{translations['mix_shape']}: {org_mix.shape}")
tar_waves_ = []
if is_match_mix: chunk_size = self.hop_length * (self.segment_size - 1) overlap = 0.02 self.logger.debug(translations["chunk_size_or_overlap"].format(chunk_size=chunk_size, overlap=overlap)) else: chunk_size = self.chunk_size overlap = self.overlap self.logger.debug(translations["chunk_size_or_overlap_standard"].format(chunk_size=chunk_size, overlap=overlap))
gen_size = chunk_size - 2 * self.trim self.logger.debug(f"{translations['calc_size']}: {gen_size}")
pad = gen_size + self.trim - ((mix.shape[-1]) % gen_size)
mixture = np.concatenate((np.zeros((2, self.trim), dtype="float32"), mix, np.zeros((2, pad), dtype="float32")), 1) self.logger.debug(f"{translations['mix_cache']}: {mixture.shape}")
step = int((1 - overlap) * chunk_size) self.logger.debug(translations["step_or_overlap"].format(step=step, overlap=overlap))
result = np.zeros((1, 2, mixture.shape[-1]), dtype=np.float32) divider = np.zeros((1, 2, mixture.shape[-1]), dtype=np.float32)
total = 0 total_chunks = (mixture.shape[-1] + step - 1) // step self.logger.debug(f"{translations['all_process_part']}: {total_chunks}")
for i in tqdm(range(0, mixture.shape[-1], step)): total += 1 start = i end = min(i + chunk_size, mixture.shape[-1]) self.logger.debug(translations["process_part_2"].format(total=total, total_chunks=total_chunks, start=start, end=end))
chunk_size_actual = end - start window = None
if overlap != 0: window = np.hanning(chunk_size_actual) window = np.tile(window[None, None, :], (1, 2, 1)) self.logger.debug(translations["window"])
mix_part_ = mixture[:, start:end] if end != i + chunk_size: pad_size = (i + chunk_size) - end mix_part_ = np.concatenate((mix_part_, np.zeros((2, pad_size), dtype="float32")), axis=-1)
mix_part = torch.tensor([mix_part_], dtype=torch.float32).to(self.torch_device)
mix_waves = mix_part.split(self.batch_size) total_batches = len(mix_waves) self.logger.debug(f"{translations['mix_or_batch']}: {total_batches}")
with torch.no_grad(): batches_processed = 0 for mix_wave in mix_waves: batches_processed += 1 self.logger.debug(f"{translations['mix_wave']} {batches_processed}/{total_batches}")
tar_waves = self.run_model(mix_wave, is_match_mix=is_match_mix)
if window is not None: tar_waves[..., :chunk_size_actual] *= window divider[..., start:end] += window else: divider[..., start:end] += 1
result[..., start:end] += tar_waves[..., : end - start]
self.logger.debug(translations["normalization_2"]) tar_waves = result / divider tar_waves_.append(tar_waves)
tar_waves_ = np.vstack(tar_waves_)[:, :, self.trim : -self.trim] tar_waves = np.concatenate(tar_waves_, axis=-1)[:, : mix.shape[-1]]
source = tar_waves[:, 0:None] self.logger.debug(f"{translations['tar_waves']}: {tar_waves.shape}")
if not is_match_mix: source *= self.compensate self.logger.debug(translations["mix_match"])
self.logger.debug(translations["mix_success"]) return source
def run_model(self, mix, is_match_mix=False): spek = self.stft(mix.to(self.torch_device)) self.logger.debug(translations["stft_2"].format(shape=spek.shape))
spek[:, :, :3, :] *= 0
if is_match_mix: spec_pred = spek.cpu().numpy() self.logger.debug(translations["is_match_mix"]) else: if self.enable_denoise: spec_pred_neg = self.model_run(-spek) spec_pred_pos = self.model_run(spek) spec_pred = (spec_pred_neg * -0.5) + (spec_pred_pos * 0.5) self.logger.debug(translations["enable_denoise"]) else: spec_pred = self.model_run(spek) self.logger.debug(translations["no_denoise"])
result = self.stft.inverse(torch.tensor(spec_pred).to(self.torch_device)).cpu().detach().numpy() self.logger.debug(f"{translations['stft']}: {result.shape}")
return result
class STFT: def __init__(self, logger, n_fft, hop_length, dim_f, device): self.logger = logger self.n_fft = n_fft self.hop_length = hop_length self.dim_f = dim_f self.device = device self.hann_window = torch.hann_window(window_length=self.n_fft, periodic=True)
def __call__(self, input_tensor): is_non_standard_device = not input_tensor.device.type in ["cuda", "cpu"]
if is_non_standard_device: input_tensor = input_tensor.cpu()
stft_window = self.hann_window.to(input_tensor.device)
batch_dimensions = input_tensor.shape[:-2] channel_dim, time_dim = input_tensor.shape[-2:]
reshaped_tensor = input_tensor.reshape([-1, time_dim]) stft_output = torch.stft(reshaped_tensor, n_fft=self.n_fft, hop_length=self.hop_length, window=stft_window, center=True, return_complex=False)
permuted_stft_output = stft_output.permute([0, 3, 1, 2])
final_output = permuted_stft_output.reshape([*batch_dimensions, channel_dim, 2, -1, permuted_stft_output.shape[-1]]).reshape([*batch_dimensions, channel_dim * 2, -1, permuted_stft_output.shape[-1]])
if is_non_standard_device: final_output = final_output.to(self.device)
return final_output[..., : self.dim_f, :]
def pad_frequency_dimension(self, input_tensor, batch_dimensions, channel_dim, freq_dim, time_dim, num_freq_bins): freq_padding = torch.zeros([*batch_dimensions, channel_dim, num_freq_bins - freq_dim, time_dim]).to(input_tensor.device) padded_tensor = torch.cat([input_tensor, freq_padding], -2)
return padded_tensor
def calculate_inverse_dimensions(self, input_tensor): batch_dimensions = input_tensor.shape[:-3] channel_dim, freq_dim, time_dim = input_tensor.shape[-3:]
num_freq_bins = self.n_fft // 2 + 1
return batch_dimensions, channel_dim, freq_dim, time_dim, num_freq_bins
def prepare_for_istft(self, padded_tensor, batch_dimensions, channel_dim, num_freq_bins, time_dim): reshaped_tensor = padded_tensor.reshape([*batch_dimensions, channel_dim // 2, 2, num_freq_bins, time_dim]) flattened_tensor = reshaped_tensor.reshape([-1, 2, num_freq_bins, time_dim]) permuted_tensor = flattened_tensor.permute([0, 2, 3, 1]) complex_tensor = permuted_tensor[..., 0] + permuted_tensor[..., 1] * 1.0j
return complex_tensor
def inverse(self, input_tensor): is_non_standard_device = not input_tensor.device.type in ["cuda", "cpu"]
if is_non_standard_device: input_tensor = input_tensor.cpu()
stft_window = self.hann_window.to(input_tensor.device)
batch_dimensions, channel_dim, freq_dim, time_dim, num_freq_bins = self.calculate_inverse_dimensions(input_tensor)
padded_tensor = self.pad_frequency_dimension(input_tensor, batch_dimensions, channel_dim, freq_dim, time_dim, num_freq_bins)
complex_tensor = self.prepare_for_istft(padded_tensor, batch_dimensions, channel_dim, num_freq_bins, time_dim)
istft_result = torch.istft(complex_tensor, n_fft=self.n_fft, hop_length=self.hop_length, window=stft_window, center=True)
final_output = istft_result.reshape([*batch_dimensions, 2, -1])
if is_non_standard_device: final_output = final_output.to(self.device)
return final_output123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600import osimport mathimport torchimport librosaimport numpy as npimport torch.nn as nnimport soundfile as sfimport torch.utils.dataimport torch.nn.functional as F
from torch import nnfrom typing import Unionfrom functools import partialfrom einops import rearrange, repeatfrom torchaudio.transforms import Resamplefrom local_attention import LocalAttentionfrom librosa.filters import mel as librosa_mel_fnfrom torch.nn.utils.parametrizations import weight_norm
os.environ["LRU_CACHE_CAPACITY"] = "3"
def load_wav_to_torch(full_path, target_sr=None, return_empty_on_exception=False): try: data, sample_rate = sf.read(full_path, always_2d=True) except Exception as e: print(f"{full_path}: {e}") if return_empty_on_exception: return [], sample_rate or target_sr or 48000 else: raise
data = data[:, 0] if len(data.shape) > 1 else data assert len(data) > 2
max_mag = (-np.iinfo(data.dtype).min if np.issubdtype(data.dtype, np.integer) else max(np.amax(data), -np.amin(data))) max_mag = ((2**31) + 1 if max_mag > (2**15) else ((2**15) + 1 if max_mag > 1.01 else 1.0)) data = torch.FloatTensor(data.astype(np.float32)) / max_mag
if (torch.isinf(data) | torch.isnan(data)).any() and return_empty_on_exception: return [], sample_rate or target_sr or 48000 if target_sr is not None and sample_rate != target_sr: data = torch.from_numpy(librosa.core.resample(data.numpy(), orig_sr=sample_rate, target_sr=target_sr)) sample_rate = target_sr
return data, sample_rate
def dynamic_range_compression(x, C=1, clip_val=1e-5): return np.log(np.clip(x, a_min=clip_val, a_max=None) * C)def dynamic_range_decompression(x, C=1): return np.exp(x) / Cdef dynamic_range_compression_torch(x, C=1, clip_val=1e-5): return torch.log(torch.clamp(x, min=clip_val) * C)def dynamic_range_decompression_torch(x, C=1): return torch.exp(x) / C
class STFT: def __init__(self, sr=22050, n_mels=80, n_fft=1024, win_size=1024, hop_length=256, fmin=20, fmax=11025, clip_val=1e-5): self.target_sr = sr self.n_mels = n_mels self.n_fft = n_fft self.win_size = win_size self.hop_length = hop_length self.fmin = fmin self.fmax = fmax self.clip_val = clip_val self.mel_basis = {} self.hann_window = {}
def get_mel(self, y, keyshift=0, speed=1, center=False, train=False): sample_rate = self.target_sr n_mels = self.n_mels n_fft = self.n_fft win_size = self.win_size hop_length = self.hop_length fmin = self.fmin fmax = self.fmax clip_val = self.clip_val
factor = 2 ** (keyshift / 12) n_fft_new = int(np.round(n_fft * factor)) win_size_new = int(np.round(win_size * factor)) hop_length_new = int(np.round(hop_length * speed))
mel_basis = self.mel_basis if not train else {} hann_window = self.hann_window if not train else {} mel_basis_key = str(fmax) + "_" + str(y.device)
if mel_basis_key not in mel_basis: mel = librosa_mel_fn(sr=sample_rate, n_fft=n_fft, n_mels=n_mels, fmin=fmin, fmax=fmax) mel_basis[mel_basis_key] = torch.from_numpy(mel).float().to(y.device)
keyshift_key = str(keyshift) + "_" + str(y.device)
if keyshift_key not in hann_window: hann_window[keyshift_key] = torch.hann_window(win_size_new).to(y.device)
pad_left = (win_size_new - hop_length_new) // 2 pad_right = max((win_size_new - hop_length_new + 1) // 2, win_size_new - y.size(-1) - pad_left) mode = "reflect" if pad_right < y.size(-1) else "constant" y = torch.nn.functional.pad(y.unsqueeze(1), (pad_left, pad_right), mode=mode) y = y.squeeze(1)
spec = torch.stft(y, n_fft_new, hop_length=hop_length_new, win_length=win_size_new, window=hann_window[keyshift_key], center=center, pad_mode="reflect", normalized=False, onesided=True, return_complex=True) spec = torch.sqrt(spec.real.pow(2) + spec.imag.pow(2) + (1e-9))
if keyshift != 0: size = n_fft // 2 + 1 resize = spec.size(1) spec = (F.pad(spec, (0, 0, 0, size - resize)) if resize < size else spec[:, :size, :]) spec = spec * win_size / win_size_new spec = torch.matmul(mel_basis[mel_basis_key], spec) spec = dynamic_range_compression_torch(spec, clip_val=clip_val) return spec
def __call__(self, audiopath): audio, sr = load_wav_to_torch(audiopath, target_sr=self.target_sr) spect = self.get_mel(audio.unsqueeze(0)).squeeze(0) return spect
stft = STFT()
def softmax_kernel(data, *, projection_matrix, is_query, normalize_data=True, eps=1e-4, device=None): b, h, *_ = data.shape
data_normalizer = (data.shape[-1] ** -0.25) if normalize_data else 1.0
ratio = projection_matrix.shape[0] ** -0.5 projection = repeat(projection_matrix, "j d -> b h j d", b=b, h=h) projection = projection.type_as(data) data_dash = torch.einsum("...id,...jd->...ij", (data_normalizer * data), projection)
diag_data = data**2 diag_data = torch.sum(diag_data, dim=-1) diag_data = (diag_data / 2.0) * (data_normalizer**2) diag_data = diag_data.unsqueeze(dim=-1)
if is_query: data_dash = ratio * (torch.exp(data_dash - diag_data - torch.max(data_dash, dim=-1, keepdim=True).values) + eps) else: data_dash = ratio * (torch.exp(data_dash - diag_data + eps))
return data_dash.type_as(data)
def orthogonal_matrix_chunk(cols, qr_uniform_q=False, device=None): unstructured_block = torch.randn((cols, cols), device=device) q, r = torch.linalg.qr(unstructured_block.cpu(), mode="reduced") q, r = map(lambda t: t.to(device), (q, r))
if qr_uniform_q: d = torch.diag(r, 0) q *= d.sign()
return q.t()
def exists(val): return val is not Nonedef empty(tensor): return tensor.numel() == 0def default(val, d): return val if exists(val) else ddef cast_tuple(val): return (val,) if not isinstance(val, tuple) else val
class PCmer(nn.Module): def __init__(self, num_layers, num_heads, dim_model, dim_keys, dim_values, residual_dropout, attention_dropout): super().__init__() self.num_layers = num_layers self.num_heads = num_heads self.dim_model = dim_model self.dim_values = dim_values self.dim_keys = dim_keys self.residual_dropout = residual_dropout self.attention_dropout = attention_dropout
self._layers = nn.ModuleList([_EncoderLayer(self) for _ in range(num_layers)])
def forward(self, phone, mask=None): for layer in self._layers: phone = layer(phone, mask)
return phone
class _EncoderLayer(nn.Module): def __init__(self, parent: PCmer): super().__init__() self.conformer = ConformerConvModule(parent.dim_model) self.norm = nn.LayerNorm(parent.dim_model) self.dropout = nn.Dropout(parent.residual_dropout) self.attn = SelfAttention(dim=parent.dim_model, heads=parent.num_heads, causal=False)
def forward(self, phone, mask=None): phone = phone + (self.attn(self.norm(phone), mask=mask)) phone = phone + (self.conformer(phone)) return phone
def calc_same_padding(kernel_size): pad = kernel_size // 2 return (pad, pad - (kernel_size + 1) % 2)
class Swish(nn.Module): def forward(self, x): return x * x.sigmoid()
class Transpose(nn.Module): def __init__(self, dims): super().__init__() assert len(dims) == 2, "dims == 2" self.dims = dims
def forward(self, x): return x.transpose(*self.dims)
class GLU(nn.Module): def __init__(self, dim): super().__init__() self.dim = dim
def forward(self, x): out, gate = x.chunk(2, dim=self.dim) return out * gate.sigmoid()
class DepthWiseConv1d(nn.Module): def __init__(self, chan_in, chan_out, kernel_size, padding): super().__init__() self.padding = padding self.conv = nn.Conv1d(chan_in, chan_out, kernel_size, groups=chan_in)
def forward(self, x): x = F.pad(x, self.padding) return self.conv(x)
class ConformerConvModule(nn.Module): def __init__(self, dim, causal=False, expansion_factor=2, kernel_size=31, dropout=0.0): super().__init__()
inner_dim = dim * expansion_factor padding = calc_same_padding(kernel_size) if not causal else (kernel_size - 1, 0)
self.net = nn.Sequential(nn.LayerNorm(dim), Transpose((1, 2)), nn.Conv1d(dim, inner_dim * 2, 1), GLU(dim=1), DepthWiseConv1d(inner_dim, inner_dim, kernel_size=kernel_size, padding=padding), Swish(), nn.Conv1d(inner_dim, dim, 1), Transpose((1, 2)), nn.Dropout(dropout))
def forward(self, x): return self.net(x)
def linear_attention(q, k, v): if v is None: out = torch.einsum("...ed,...nd->...ne", k, q) return out else: k_cumsum = k.sum(dim=-2) D_inv = 1.0 / (torch.einsum("...nd,...d->...n", q, k_cumsum.type_as(q)) + 1e-8) context = torch.einsum("...nd,...ne->...de", k, v) out = torch.einsum("...de,...nd,...n->...ne", context, q, D_inv) return out
def gaussian_orthogonal_random_matrix(nb_rows, nb_columns, scaling=0, qr_uniform_q=False, device=None): nb_full_blocks = int(nb_rows / nb_columns) block_list = []
for _ in range(nb_full_blocks): q = orthogonal_matrix_chunk(nb_columns, qr_uniform_q=qr_uniform_q, device=device) block_list.append(q)
remaining_rows = nb_rows - nb_full_blocks * nb_columns
if remaining_rows > 0: q = orthogonal_matrix_chunk(nb_columns, qr_uniform_q=qr_uniform_q, device=device) block_list.append(q[:remaining_rows])
final_matrix = torch.cat(block_list)
if scaling == 0: multiplier = torch.randn((nb_rows, nb_columns), device=device).norm(dim=1) elif scaling == 1: multiplier = math.sqrt((float(nb_columns))) * torch.ones((nb_rows,), device=device) else: raise ValueError(f"Chia không hợp lệ {scaling}")
return torch.diag(multiplier) @ final_matrix
class FastAttention(nn.Module): def __init__(self, dim_heads, nb_features=None, ortho_scaling=0, causal=False, generalized_attention=False, kernel_fn=nn.ReLU(), qr_uniform_q=False, no_projection=False): super().__init__() nb_features = default(nb_features, int(dim_heads * math.log(dim_heads)))
self.dim_heads = dim_heads self.nb_features = nb_features self.ortho_scaling = ortho_scaling
self.create_projection = partial(gaussian_orthogonal_random_matrix, nb_rows=self.nb_features, nb_columns=dim_heads, scaling=ortho_scaling, qr_uniform_q=qr_uniform_q) projection_matrix = self.create_projection() self.register_buffer("projection_matrix", projection_matrix)
self.generalized_attention = generalized_attention self.kernel_fn = kernel_fn self.no_projection = no_projection self.causal = causal
@torch.no_grad() def redraw_projection_matrix(self): projections = self.create_projection() self.projection_matrix.copy_(projections) del projections
def forward(self, q, k, v): device = q.device
if self.no_projection: q = q.softmax(dim=-1) k = torch.exp(k) if self.causal else k.softmax(dim=-2) else: create_kernel = partial(softmax_kernel, projection_matrix=self.projection_matrix, device=device)
q = create_kernel(q, is_query=True) k = create_kernel(k, is_query=False)
attn_fn = linear_attention if not self.causal else self.causal_linear_fn
if v is None: out = attn_fn(q, k, None) return out else: out = attn_fn(q, k, v) return out
class SelfAttention(nn.Module): def __init__(self, dim, causal=False, heads=8, dim_head=64, local_heads=0, local_window_size=256, nb_features=None, feature_redraw_interval=1000, generalized_attention=False, kernel_fn=nn.ReLU(), qr_uniform_q=False, dropout=0.0, no_projection=False): super().__init__() assert dim % heads == 0 dim_head = default(dim_head, dim // heads) inner_dim = dim_head * heads self.fast_attention = FastAttention(dim_head, nb_features, causal=causal, generalized_attention=generalized_attention, kernel_fn=kernel_fn, qr_uniform_q=qr_uniform_q, no_projection=no_projection) self.heads = heads self.global_heads = heads - local_heads self.local_attn = (LocalAttention(window_size=local_window_size, causal=causal, autopad=True, dropout=dropout, look_forward=int(not causal), rel_pos_emb_config=(dim_head, local_heads)) if local_heads > 0 else None) self.to_q = nn.Linear(dim, inner_dim) self.to_k = nn.Linear(dim, inner_dim) self.to_v = nn.Linear(dim, inner_dim) self.to_out = nn.Linear(inner_dim, dim) self.dropout = nn.Dropout(dropout)
@torch.no_grad() def redraw_projection_matrix(self): self.fast_attention.redraw_projection_matrix()
def forward(self, x, context=None, mask=None, context_mask=None, name=None, inference=False, **kwargs): _, _, _, h, gh = *x.shape, self.heads, self.global_heads
cross_attend = exists(context) context = default(context, x) context_mask = default(context_mask, mask) if not cross_attend else context_mask q, k, v = self.to_q(x), self.to_k(context), self.to_v(context)
q, k, v = map(lambda t: rearrange(t, "b n (h d) -> b h n d", h=h), (q, k, v)) (q, lq), (k, lk), (v, lv) = map(lambda t: (t[:, :gh], t[:, gh:]), (q, k, v))
attn_outs = []
if not empty(q): if exists(context_mask): global_mask = context_mask[:, None, :, None] v.masked_fill_(~global_mask, 0.0)
if cross_attend: pass else: out = self.fast_attention(q, k, v)
attn_outs.append(out)
if not empty(lq): assert (not cross_attend), "not cross_attend" out = self.local_attn(lq, lk, lv, input_mask=mask) attn_outs.append(out)
out = torch.cat(attn_outs, dim=1) out = rearrange(out, "b h n d -> b n (h d)") out = self.to_out(out) return self.dropout(out)
def l2_regularization(model, l2_alpha): l2_loss = [] for module in model.modules(): if type(module) is nn.Conv2d: l2_loss.append((module.weight**2).sum() / 2.0)
return l2_alpha * sum(l2_loss)
class _FCPE(nn.Module): def __init__(self, input_channel=128, out_dims=360, n_layers=12, n_chans=512, use_siren=False, use_full=False, loss_mse_scale=10, loss_l2_regularization=False, loss_l2_regularization_scale=1, loss_grad1_mse=False, loss_grad1_mse_scale=1, f0_max=1975.5, f0_min=32.70, confidence=False, threshold=0.05, use_input_conv=True): super().__init__() if use_siren: raise ValueError("Siren not support") if use_full: raise ValueError("Model full not support")
self.loss_mse_scale = loss_mse_scale if (loss_mse_scale is not None) else 10 self.loss_l2_regularization = (loss_l2_regularization if (loss_l2_regularization is not None) else False) self.loss_l2_regularization_scale = (loss_l2_regularization_scale if (loss_l2_regularization_scale is not None) else 1) self.loss_grad1_mse = loss_grad1_mse if (loss_grad1_mse is not None) else False self.loss_grad1_mse_scale = (loss_grad1_mse_scale if (loss_grad1_mse_scale is not None) else 1) self.f0_max = f0_max if (f0_max is not None) else 1975.5 self.f0_min = f0_min if (f0_min is not None) else 32.70 self.confidence = confidence if (confidence is not None) else False self.threshold = threshold if (threshold is not None) else 0.05 self.use_input_conv = use_input_conv if (use_input_conv is not None) else True self.cent_table_b = torch.Tensor(np.linspace(self.f0_to_cent(torch.Tensor([f0_min]))[0], self.f0_to_cent(torch.Tensor([f0_max]))[0], out_dims)) self.register_buffer("cent_table", self.cent_table_b)
_leaky = nn.LeakyReLU()
self.stack = nn.Sequential(nn.Conv1d(input_channel, n_chans, 3, 1, 1), nn.GroupNorm(4, n_chans), _leaky, nn.Conv1d(n_chans, n_chans, 3, 1, 1)) self.decoder = PCmer(num_layers=n_layers, num_heads=8, dim_model=n_chans, dim_keys=n_chans, dim_values=n_chans, residual_dropout=0.1, attention_dropout=0.1) self.norm = nn.LayerNorm(n_chans) self.n_out = out_dims self.dense_out = weight_norm(nn.Linear(n_chans, self.n_out))
def forward(self, mel, infer=True, gt_f0=None, return_hz_f0=False, cdecoder="local_argmax"): if cdecoder == "argmax": self.cdecoder = self.cents_decoder elif cdecoder == "local_argmax": self.cdecoder = self.cents_local_decoder
x = (self.stack(mel.transpose(1, 2)).transpose(1, 2) if self.use_input_conv else mel) x = self.decoder(x) x = self.norm(x) x = self.dense_out(x) x = torch.sigmoid(x)
if not infer: gt_cent_f0 = self.f0_to_cent(gt_f0) gt_cent_f0 = self.gaussian_blurred_cent(gt_cent_f0) loss_all = self.loss_mse_scale * F.binary_cross_entropy(x, gt_cent_f0)
if self.loss_l2_regularization: loss_all = loss_all + l2_regularization(model=self, l2_alpha=self.loss_l2_regularization_scale)
x = loss_all
if infer: x = self.cdecoder(x) x = self.cent_to_f0(x) x = (1 + x / 700).log() if not return_hz_f0 else x
return x
def cents_decoder(self, y, mask=True): B, N, _ = y.size() ci = self.cent_table[None, None, :].expand(B, N, -1) rtn = torch.sum(ci * y, dim=-1, keepdim=True) / torch.sum(y, dim=-1, keepdim=True) if mask: confident = torch.max(y, dim=-1, keepdim=True)[0] confident_mask = torch.ones_like(confident) confident_mask[confident <= self.threshold] = float("-INF") rtn = rtn * confident_mask
return (rtn, confident) if self.confidence else rtn
def cents_local_decoder(self, y, mask=True): B, N, _ = y.size() ci = self.cent_table[None, None, :].expand(B, N, -1) confident, max_index = torch.max(y, dim=-1, keepdim=True) local_argmax_index = torch.arange(0, 9).to(max_index.device) + (max_index - 4) local_argmax_index = torch.clamp(local_argmax_index, 0, self.n_out - 1) ci_l = torch.gather(ci, -1, local_argmax_index) y_l = torch.gather(y, -1, local_argmax_index) rtn = torch.sum(ci_l * y_l, dim=-1, keepdim=True) / torch.sum(y_l, dim=-1, keepdim=True)
if mask: confident_mask = torch.ones_like(confident) confident_mask[confident <= self.threshold] = float("-INF") rtn = rtn * confident_mask
return (rtn, confident) if self.confidence else rtn
def cent_to_f0(self, cent): return 10.0 * 2 ** (cent / 1200.0)
def f0_to_cent(self, f0): return 1200.0 * torch.log2(f0 / 10.0)
def gaussian_blurred_cent(self, cents): mask = (cents > 0.1) & (cents < (1200.0 * np.log2(self.f0_max / 10.0))) B, N, _ = cents.size() ci = self.cent_table[None, None, :].expand(B, N, -1) return torch.exp(-torch.square(ci - cents) / 1250) * mask.float()
class FCPEInfer: def __init__(self, model_path, device=None, dtype=torch.float32): if device is None: device = "cuda" if torch.cuda.is_available() else "cpu"
self.device = device ckpt = torch.load(model_path, map_location=torch.device(self.device)) self.args = DotDict(ckpt["config"]) self.dtype = dtype model = _FCPE(input_channel=self.args.model.input_channel, out_dims=self.args.model.out_dims, n_layers=self.args.model.n_layers, n_chans=self.args.model.n_chans, use_siren=self.args.model.use_siren, use_full=self.args.model.use_full, loss_mse_scale=self.args.loss.loss_mse_scale, loss_l2_regularization=self.args.loss.loss_l2_regularization, loss_l2_regularization_scale=self.args.loss.loss_l2_regularization_scale, loss_grad1_mse=self.args.loss.loss_grad1_mse, loss_grad1_mse_scale=self.args.loss.loss_grad1_mse_scale, f0_max=self.args.model.f0_max, f0_min=self.args.model.f0_min, confidence=self.args.model.confidence) model.to(self.device).to(self.dtype) model.load_state_dict(ckpt["model"]) model.eval() self.model = model self.wav2mel = Wav2Mel(self.args, dtype=self.dtype, device=self.device)
@torch.no_grad() def __call__(self, audio, sr, threshold=0.05): self.model.threshold = threshold audio = audio[None, :] mel = self.wav2mel(audio=audio, sample_rate=sr).to(self.dtype) f0 = self.model(mel=mel, infer=True, return_hz_f0=True) return f0
class Wav2Mel: def __init__(self, args, device=None, dtype=torch.float32): self.sample_rate = args.mel.sampling_rate self.hop_size = args.mel.hop_size
if device is None: device = "cuda" if torch.cuda.is_available() else "cpu"
self.device = device self.dtype = dtype self.stft = STFT(args.mel.sampling_rate, args.mel.num_mels, args.mel.n_fft, args.mel.win_size, args.mel.hop_size, args.mel.fmin, args.mel.fmax) self.resample_kernel = {}
def extract_nvstft(self, audio, keyshift=0, train=False): mel = self.stft.get_mel(audio, keyshift=keyshift, train=train).transpose(1, 2) return mel
def extract_mel(self, audio, sample_rate, keyshift=0, train=False): audio = audio.to(self.dtype).to(self.device)
if sample_rate == self.sample_rate: audio_res = audio else: key_str = str(sample_rate)
if key_str not in self.resample_kernel: self.resample_kernel[key_str] = Resample(sample_rate, self.sample_rate, lowpass_filter_width=128)
self.resample_kernel[key_str] = (self.resample_kernel[key_str].to(self.dtype).to(self.device)) audio_res = self.resample_kernel[key_str](audio)
mel = self.extract_nvstft(audio_res, keyshift=keyshift, train=train) n_frames = int(audio.shape[1] // self.hop_size) + 1 mel = (torch.cat((mel, mel[:, -1:, :]), 1) if n_frames > int(mel.shape[1]) else mel) mel = mel[:, :n_frames, :] if n_frames < int(mel.shape[1]) else mel return mel
def __call__(self, audio, sample_rate, keyshift=0, train=False): return self.extract_mel(audio, sample_rate, keyshift=keyshift, train=train)
class DotDict(dict): def __getattr__(*args): val = dict.get(*args) return DotDict(val) if type(val) is dict else val
__setattr__ = dict.__setitem__ __delattr__ = dict.__delitem__
class F0Predictor(object): def compute_f0(self, wav, p_len): pass def compute_f0_uv(self, wav, p_len): pass
class FCPE(F0Predictor): def __init__(self, model_path, hop_length=512, f0_min=50, f0_max=1100, dtype=torch.float32, device=None, sample_rate=44100, threshold=0.05): self.fcpe = FCPEInfer(model_path, device=device, dtype=dtype) self.hop_length = hop_length self.f0_min = f0_min self.f0_max = f0_max self.device = device or ("cuda" if torch.cuda.is_available() else "cpu") self.threshold = threshold self.sample_rate = sample_rate self.dtype = dtype self.name = "fcpe"
def repeat_expand(self, content: Union[torch.Tensor, np.ndarray], target_len, mode = "nearest"): ndim = content.ndim content = (content[None, None] if ndim == 1 else content[None] if ndim == 2 else content) assert content.ndim == 3 is_np = isinstance(content, np.ndarray) content = torch.from_numpy(content) if is_np else content results = torch.nn.functional.interpolate(content, size=target_len, mode=mode) results = results.numpy() if is_np else results return results[0, 0] if ndim == 1 else results[0] if ndim == 2 else results
def post_process(self, x, sample_rate, f0, pad_to): f0 = (torch.from_numpy(f0).float().to(x.device) if isinstance(f0, np.ndarray) else f0) f0 = self.repeat_expand(f0, pad_to) if pad_to is not None else f0
vuv_vector = torch.zeros_like(f0) vuv_vector[f0 > 0.0] = 1.0 vuv_vector[f0 <= 0.0] = 0.0
nzindex = torch.nonzero(f0).squeeze() f0 = torch.index_select(f0, dim=0, index=nzindex).cpu().numpy() time_org = self.hop_length / sample_rate * nzindex.cpu().numpy() time_frame = np.arange(pad_to) * self.hop_length / sample_rate
vuv_vector = F.interpolate(vuv_vector[None, None, :], size=pad_to)[0][0]
if f0.shape[0] <= 0: return np.zeros(pad_to), vuv_vector.cpu().numpy() if f0.shape[0] == 1: return np.ones(pad_to) * f0[0], vuv_vector.cpu().numpy()
f0 = np.interp(time_frame, time_org, f0, left=f0[0], right=f0[-1]) return f0, vuv_vector.cpu().numpy()
def compute_f0(self, wav, p_len=None): x = torch.FloatTensor(wav).to(self.dtype).to(self.device) p_len = x.shape[0] // self.hop_length if p_len is None else p_len f0 = self.fcpe(x, sr=self.sample_rate, threshold=self.threshold)[0, :, 0]
if torch.all(f0 == 0): return f0.cpu().numpy() if p_len is None else np.zeros(p_len), (f0.cpu().numpy() if p_len is None else np.zeros(p_len))
return self.post_process(x, self.sample_rate, f0, p_len)[0] def compute_f0_uv(self, wav, p_len=None): x = torch.FloatTensor(wav).to(self.dtype).to(self.device) p_len = x.shape[0] // self.hop_length if p_len is None else p_len f0 = self.fcpe(x, sr=self.sample_rate, threshold=self.threshold)[0, :, 0]
if torch.all(f0 == 0): return f0.cpu().numpy() if p_len is None else np.zeros(p_len), (f0.cpu().numpy() if p_len is None else np.zeros(p_len)) return self.post_process(x, self.sample_rate, f0, p_len)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270import torchimport numpy as npimport torch.nn as nnimport torch.nn.functional as F
from typing import Listfrom librosa.filters import mel
N_MELS = 128N_CLASS = 360
class ConvBlockRes(nn.Module): def __init__(self, in_channels, out_channels, momentum=0.01): super(ConvBlockRes, self).__init__() self.conv = nn.Sequential(nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False), nn.BatchNorm2d(out_channels, momentum=momentum), nn.ReLU(), nn.Conv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False), nn.BatchNorm2d(out_channels, momentum=momentum), nn.ReLU())
if in_channels != out_channels: self.shortcut = nn.Conv2d(in_channels, out_channels, (1, 1)) self.is_shortcut = True else: self.is_shortcut = False
def forward(self, x): if self.is_shortcut: return self.conv(x) + self.shortcut(x) else: return self.conv(x) + x
class ResEncoderBlock(nn.Module): def __init__(self, in_channels, out_channels, kernel_size, n_blocks=1, momentum=0.01): super(ResEncoderBlock, self).__init__() self.n_blocks = n_blocks self.conv = nn.ModuleList() self.conv.append(ConvBlockRes(in_channels, out_channels, momentum))
for _ in range(n_blocks - 1): self.conv.append(ConvBlockRes(out_channels, out_channels, momentum))
self.kernel_size = kernel_size
if self.kernel_size is not None: self.pool = nn.AvgPool2d(kernel_size=kernel_size)
def forward(self, x): for i in range(self.n_blocks): x = self.conv[i](x)
if self.kernel_size is not None: return x, self.pool(x) else: return x
class Encoder(nn.Module): def __init__(self, in_channels, in_size, n_encoders, kernel_size, n_blocks, out_channels=16, momentum=0.01): super(Encoder, self).__init__() self.n_encoders = n_encoders self.bn = nn.BatchNorm2d(in_channels, momentum=momentum) self.layers = nn.ModuleList() self.latent_channels = []
for i in range(self.n_encoders): self.layers.append(ResEncoderBlock(in_channels, out_channels, kernel_size, n_blocks, momentum=momentum)) self.latent_channels.append([out_channels, in_size]) in_channels = out_channels out_channels *= 2 in_size //= 2 self.out_size = in_size self.out_channel = out_channels
def forward(self, x: torch.Tensor): concat_tensors: List[torch.Tensor] = [] x = self.bn(x)
for i in range(self.n_encoders): t, x = self.layers[i](x) concat_tensors.append(t)
return x, concat_tensors
class Intermediate(nn.Module): def __init__(self, in_channels, out_channels, n_inters, n_blocks, momentum=0.01): super(Intermediate, self).__init__() self.n_inters = n_inters self.layers = nn.ModuleList() self.layers.append(ResEncoderBlock(in_channels, out_channels, None, n_blocks, momentum))
for _ in range(self.n_inters - 1): self.layers.append(ResEncoderBlock(out_channels, out_channels, None, n_blocks, momentum))
def forward(self, x): for i in range(self.n_inters): x = self.layers[i](x) return x
class ResDecoderBlock(nn.Module): def __init__(self, in_channels, out_channels, stride, n_blocks=1, momentum=0.01): super(ResDecoderBlock, self).__init__() out_padding = (0, 1) if stride == (1, 2) else (1, 1) self.n_blocks = n_blocks self.conv1 = nn.Sequential(nn.ConvTranspose2d(in_channels=in_channels, out_channels=out_channels, kernel_size=(3, 3), stride=stride, padding=(1, 1), output_padding=out_padding, bias=False), nn.BatchNorm2d(out_channels, momentum=momentum), nn.ReLU()) self.conv2 = nn.ModuleList() self.conv2.append(ConvBlockRes(out_channels * 2, out_channels, momentum)) for _ in range(n_blocks - 1): self.conv2.append(ConvBlockRes(out_channels, out_channels, momentum))
def forward(self, x, concat_tensor): x = self.conv1(x) x = torch.cat((x, concat_tensor), dim=1) for i in range(self.n_blocks): x = self.conv2[i](x)
return x
class Decoder(nn.Module): def __init__(self, in_channels, n_decoders, stride, n_blocks, momentum=0.01): super(Decoder, self).__init__() self.layers = nn.ModuleList() self.n_decoders = n_decoders
for _ in range(self.n_decoders): out_channels = in_channels // 2 self.layers.append(ResDecoderBlock(in_channels, out_channels, stride, n_blocks, momentum)) in_channels = out_channels
def forward(self, x, concat_tensors): for i in range(self.n_decoders): x = self.layers[i](x, concat_tensors[-1 - i])
return x
class DeepUnet(nn.Module): def __init__(self, kernel_size, n_blocks, en_de_layers=5, inter_layers=4, in_channels=1, en_out_channels=16): super(DeepUnet, self).__init__() self.encoder = Encoder(in_channels, 128, en_de_layers, kernel_size, n_blocks, en_out_channels) self.intermediate = Intermediate(self.encoder.out_channel // 2, self.encoder.out_channel, inter_layers, n_blocks) self.decoder = Decoder(self.encoder.out_channel, en_de_layers, kernel_size, n_blocks)
def forward(self, x): x, concat_tensors = self.encoder(x) x = self.intermediate(x) x = self.decoder(x, concat_tensors) return x
class E2E(nn.Module): def __init__(self, n_blocks, n_gru, kernel_size, en_de_layers=5, inter_layers=4, in_channels=1, en_out_channels=16): super(E2E, self).__init__() self.unet = DeepUnet(kernel_size, n_blocks, en_de_layers, inter_layers, in_channels, en_out_channels) self.cnn = nn.Conv2d(en_out_channels, 3, (3, 3), padding=(1, 1))
if n_gru: self.fc = nn.Sequential(BiGRU(3 * 128, 256, n_gru), nn.Linear(512, N_CLASS), nn.Dropout(0.25), nn.Sigmoid()) else: self.fc = nn.Sequential(nn.Linear(3 * N_MELS, N_CLASS), nn.Dropout(0.25), nn.Sigmoid())
def forward(self, mel): mel = mel.transpose(-1, -2).unsqueeze(1) x = self.cnn(self.unet(mel)).transpose(1, 2).flatten(-2) x = self.fc(x) return x
class MelSpectrogram(torch.nn.Module): def __init__(self, is_half, n_mel_channels, sample_rate, win_length, hop_length, n_fft=None, mel_fmin=0, mel_fmax=None, clamp=1e-5): super().__init__() n_fft = win_length if n_fft is None else n_fft self.hann_window = {} mel_basis = mel(sr=sample_rate, n_fft=n_fft, n_mels=n_mel_channels, fmin=mel_fmin, fmax=mel_fmax, htk=True) mel_basis = torch.from_numpy(mel_basis).float() self.register_buffer("mel_basis", mel_basis) self.n_fft = win_length if n_fft is None else n_fft self.hop_length = hop_length self.win_length = win_length self.sample_rate = sample_rate self.n_mel_channels = n_mel_channels self.clamp = clamp self.is_half = is_half
def forward(self, audio, keyshift=0, speed=1, center=True): factor = 2 ** (keyshift / 12) n_fft_new = int(np.round(self.n_fft * factor)) win_length_new = int(np.round(self.win_length * factor)) hop_length_new = int(np.round(self.hop_length * speed)) keyshift_key = str(keyshift) + "_" + str(audio.device)
if keyshift_key not in self.hann_window: self.hann_window[keyshift_key] = torch.hann_window(win_length_new).to(audio.device)
fft = torch.stft(audio, n_fft=n_fft_new, hop_length=hop_length_new, win_length=win_length_new, window=self.hann_window[keyshift_key], center=center, return_complex=True) magnitude = torch.sqrt(fft.real.pow(2) + fft.imag.pow(2))
if keyshift != 0: size = self.n_fft // 2 + 1 resize = magnitude.size(1)
if resize < size: magnitude = F.pad(magnitude, (0, 0, 0, size - resize))
magnitude = magnitude[:, :size, :] * self.win_length / win_length_new
mel_output = torch.matmul(self.mel_basis, magnitude)
if self.is_half: mel_output = mel_output.half()
log_mel_spec = torch.log(torch.clamp(mel_output, min=self.clamp)) return log_mel_spec
class RMVPE: def __init__(self, model_path, is_half, device=None): self.resample_kernel = {} model = E2E(4, 1, (2, 2)) ckpt = torch.load(model_path, map_location="cpu") model.load_state_dict(ckpt) model.eval()
if is_half: model = model.half()
self.model = model self.resample_kernel = {} self.is_half = is_half self.device = device self.mel_extractor = MelSpectrogram(is_half, N_MELS, 16000, 1024, 160, None, 30, 8000).to(device) self.model = self.model.to(device) cents_mapping = 20 * np.arange(N_CLASS) + 1997.3794084376191 self.cents_mapping = np.pad(cents_mapping, (4, 4))
def mel2hidden(self, mel): with torch.no_grad(): n_frames = mel.shape[-1] mel = F.pad(mel, (0, 32 * ((n_frames - 1) // 32 + 1) - n_frames), mode="reflect") hidden = self.model(mel) return hidden[:, :n_frames]
def decode(self, hidden, thred=0.03): cents_pred = self.to_local_average_cents(hidden, thred=thred) f0 = 10 * (2 ** (cents_pred / 1200)) f0[f0 == 10] = 0 return f0
def infer_from_audio(self, audio, thred=0.03): audio = torch.from_numpy(audio).float().to(self.device).unsqueeze(0) mel = self.mel_extractor(audio, center=True) hidden = self.mel2hidden(mel) hidden = hidden.squeeze(0).cpu().numpy()
if self.is_half: hidden = hidden.astype("float32")
f0 = self.decode(hidden, thred=thred) return f0
def to_local_average_cents(self, salience, thred=0.05): center = np.argmax(salience, axis=1) salience = np.pad(salience, ((0, 0), (4, 4))) center += 4 todo_salience = [] todo_cents_mapping = [] starts = center - 4 ends = center + 5
for idx in range(salience.shape[0]): todo_salience.append(salience[:, starts[idx] : ends[idx]][idx]) todo_cents_mapping.append(self.cents_mapping[starts[idx] : ends[idx]])
todo_salience = np.array(todo_salience) todo_cents_mapping = np.array(todo_cents_mapping) product_sum = np.sum(todo_salience * todo_cents_mapping, 1) weight_sum = np.sum(todo_salience, 1) devided = product_sum / weight_sum maxx = np.max(salience, axis=1) devided[maxx <= thred] = 0 return devided
class BiGRU(nn.Module): def __init__(self, input_features, hidden_features, num_layers): super(BiGRU, self).__init__() self.gru = nn.GRU(input_features, hidden_features, num_layers=num_layers, batch_first=True, bidirectional=True)
def forward(self, x): return self.gru(x)[0]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270import osimport gcimport sysimport torchimport librosa
import numpy as npimport soundfile as sf
from logging import Loggerfrom pydub import AudioSegment
now_dir = os.getcwd()sys.path.append(now_dir)
from . import spec_utilsfrom main.configs.config import Config
translations = Config().translations
class CommonSeparator: ALL_STEMS = "All Stems" VOCAL_STEM = "Vocals" INST_STEM = "Instrumental" OTHER_STEM = "Other" BASS_STEM = "Bass" DRUM_STEM = "Drums" GUITAR_STEM = "Guitar" PIANO_STEM = "Piano" SYNTH_STEM = "Synthesizer" STRINGS_STEM = "Strings" WOODWINDS_STEM = "Woodwinds" BRASS_STEM = "Brass" WIND_INST_STEM = "Wind Inst" NO_OTHER_STEM = "No Other" NO_BASS_STEM = "No Bass" NO_DRUM_STEM = "No Drums" NO_GUITAR_STEM = "No Guitar" NO_PIANO_STEM = "No Piano" NO_SYNTH_STEM = "No Synthesizer" NO_STRINGS_STEM = "No Strings" NO_WOODWINDS_STEM = "No Woodwinds" NO_WIND_INST_STEM = "No Wind Inst" NO_BRASS_STEM = "No Brass" PRIMARY_STEM = "Primary Stem" SECONDARY_STEM = "Secondary Stem" LEAD_VOCAL_STEM = "lead_only" BV_VOCAL_STEM = "backing_only" LEAD_VOCAL_STEM_I = "with_lead_vocals" BV_VOCAL_STEM_I = "with_backing_vocals" LEAD_VOCAL_STEM_LABEL = "Lead Vocals" BV_VOCAL_STEM_LABEL = "Backing Vocals" NO_STEM = "No "
STEM_PAIR_MAPPER = {VOCAL_STEM: INST_STEM, INST_STEM: VOCAL_STEM, LEAD_VOCAL_STEM: BV_VOCAL_STEM, BV_VOCAL_STEM: LEAD_VOCAL_STEM, PRIMARY_STEM: SECONDARY_STEM}
NON_ACCOM_STEMS = (VOCAL_STEM, OTHER_STEM, BASS_STEM, DRUM_STEM, GUITAR_STEM, PIANO_STEM, SYNTH_STEM, STRINGS_STEM, WOODWINDS_STEM, BRASS_STEM, WIND_INST_STEM)
def __init__(self, config): self.logger: Logger = config.get("logger") self.log_level: int = config.get("log_level") self.torch_device = config.get("torch_device") self.torch_device_cpu = config.get("torch_device_cpu") self.torch_device_mps = config.get("torch_device_mps") self.onnx_execution_provider = config.get("onnx_execution_provider") self.model_name = config.get("model_name") self.model_path = config.get("model_path") self.model_data = config.get("model_data") self.output_dir = config.get("output_dir") self.output_format = config.get("output_format") self.output_bitrate = config.get("output_bitrate") self.normalization_threshold = config.get("normalization_threshold") self.enable_denoise = config.get("enable_denoise") self.output_single_stem = config.get("output_single_stem") self.invert_using_spec = config.get("invert_using_spec") self.sample_rate = config.get("sample_rate")
self.primary_stem_name = None self.secondary_stem_name = None
if "training" in self.model_data and "instruments" in self.model_data["training"]: instruments = self.model_data["training"]["instruments"]
if instruments: self.primary_stem_name = instruments[0] self.secondary_stem_name = instruments[1] if len(instruments) > 1 else self.secondary_stem(self.primary_stem_name)
if self.primary_stem_name is None: self.primary_stem_name = self.model_data.get("primary_stem", "Vocals") self.secondary_stem_name = self.secondary_stem(self.primary_stem_name)
self.is_karaoke = self.model_data.get("is_karaoke", False) self.is_bv_model = self.model_data.get("is_bv_model", False) self.bv_model_rebalance = self.model_data.get("is_bv_model_rebalanced", 0)
self.logger.debug(translations["info"].format(model_name=self.model_name, model_path=self.model_path)) self.logger.debug(translations["info_2"].format(output_dir=self.output_dir, output_format=self.output_format)) self.logger.debug(translations["info_3"].format(normalization_threshold=self.normalization_threshold)) self.logger.debug(translations["info_4"].format(enable_denoise=self.enable_denoise, output_single_stem=self.output_single_stem)) self.logger.debug(translations["info_5"].format(invert_using_spec=self.invert_using_spec, sample_rate=self.sample_rate)) self.logger.debug(translations["info_6"].format(primary_stem_name=self.primary_stem_name, secondary_stem_name=self.secondary_stem_name)) self.logger.debug(translations["info_7"].format(is_karaoke=self.is_karaoke, is_bv_model=self.is_bv_model, bv_model_rebalance=self.bv_model_rebalance))
self.audio_file_path = None self.audio_file_base = None self.primary_source = None self.secondary_source = None self.primary_stem_output_path = None self.secondary_stem_output_path = None self.cached_sources_map = {}
def secondary_stem(self, primary_stem: str): primary_stem = primary_stem if primary_stem else self.NO_STEM
return self.STEM_PAIR_MAPPER[primary_stem] if primary_stem in self.STEM_PAIR_MAPPER else primary_stem.replace(self.NO_STEM, "") if self.NO_STEM in primary_stem else f"{self.NO_STEM}{primary_stem}"
def separate(self, audio_file_path): pass
def final_process(self, stem_path, source, stem_name): self.logger.debug(translations["success_process"].format(stem_name=stem_name)) self.write_audio(stem_path, source)
return {stem_name: source}
def cached_sources_clear(self): self.cached_sources_map = {}
def cached_source_callback(self, model_architecture, model_name=None): model, sources = None, None mapper = self.cached_sources_map[model_architecture]
for key, value in mapper.items(): if model_name in key: model = key sources = value
return model, sources
def cached_model_source_holder(self, model_architecture, sources, model_name=None): self.cached_sources_map[model_architecture] = {**self.cached_sources_map.get(model_architecture, {}), **{model_name: sources}}
def prepare_mix(self, mix): audio_path = mix
if not isinstance(mix, np.ndarray): self.logger.debug(f"{translations['load_audio']}: {mix}") mix, sr = librosa.load(mix, mono=False, sr=self.sample_rate) self.logger.debug(translations["load_audio_success"].format(sr=sr, shape=mix.shape)) else: self.logger.debug(translations["convert_mix"]) mix = mix.T self.logger.debug(translations["convert_shape"].format(shape=mix.shape))
if isinstance(audio_path, str): if not np.any(mix): error_msg = translations["audio_not_valid"].format(audio_path=audio_path) self.logger.error(error_msg) raise ValueError(error_msg) else: self.logger.debug(translations["audio_valid"])
if mix.ndim == 1: self.logger.debug(translations["mix_single"]) mix = np.asfortranarray([mix, mix]) self.logger.debug(translations["convert_mix_audio"])
self.logger.debug(translations["mix_success_2"]) return mix
def write_audio(self, stem_path: str, stem_source): duration_seconds = librosa.get_duration(filename=self.audio_file_path) duration_hours = duration_seconds / 3600 self.logger.info(translations["duration"].format(duration_hours=f"{duration_hours:.2f}", duration_seconds=f"{duration_seconds:.2f}"))
if duration_hours >= 1: self.logger.warning(translations["write"].format(name="soundfile")) self.write_audio_soundfile(stem_path, stem_source) else: self.logger.info(translations["write"].format(name="pydub")) self.write_audio_pydub(stem_path, stem_source)
def write_audio_pydub(self, stem_path: str, stem_source): self.logger.debug(f"{translations['write_audio'].format(name='write_audio_pydub')} {stem_path}")
stem_source = spec_utils.normalize(wave=stem_source, max_peak=self.normalization_threshold)
if np.max(np.abs(stem_source)) < 1e-6: self.logger.warning(translations["original_not_valid"]) return
if self.output_dir: os.makedirs(self.output_dir, exist_ok=True) stem_path = os.path.join(self.output_dir, stem_path)
self.logger.debug(f"{translations['shape_audio']}: {stem_source.shape}") self.logger.debug(f"{translations['convert_data']}: {stem_source.dtype}")
if stem_source.dtype != np.int16: stem_source = (stem_source * 32767).astype(np.int16) self.logger.debug(translations["original_source_to_int16"])
stem_source_interleaved = np.empty((2 * stem_source.shape[0],), dtype=np.int16) stem_source_interleaved[0::2] = stem_source[:, 0] stem_source_interleaved[1::2] = stem_source[:, 1]
self.logger.debug(f"{translations['shape_audio_2']}: {stem_source_interleaved.shape}")
try: audio_segment = AudioSegment(stem_source_interleaved.tobytes(), frame_rate=self.sample_rate, sample_width=stem_source.dtype.itemsize, channels=2) self.logger.debug(translations["create_audiosegment"]) except (IOError, ValueError) as e: self.logger.error(f"{translations['create_audiosegment_error']}: {e}") return
file_format = stem_path.lower().split(".")[-1]
if file_format == "m4a": file_format = "mp4" elif file_format == "mka": file_format = "matroska"
bitrate = "320k" if file_format == "mp3" and self.output_bitrate is None else self.output_bitrate
try: audio_segment.export(stem_path, format=file_format, bitrate=bitrate) self.logger.debug(f"{translations['export_success']} {stem_path}") except (IOError, ValueError) as e: self.logger.error(f"{translations['export_error']}: {e}")
def write_audio_soundfile(self, stem_path: str, stem_source): self.logger.debug(f"{translations['write_audio'].format(name='write_audio_soundfile')}: {stem_path}")
if stem_source.shape[1] == 2: if stem_source.flags["F_CONTIGUOUS"]: stem_source = np.ascontiguousarray(stem_source) else: stereo_interleaved = np.empty((2 * stem_source.shape[0],), dtype=np.int16) stereo_interleaved[0::2] = stem_source[:, 0]
stereo_interleaved[1::2] = stem_source[:, 1] stem_source = stereo_interleaved
self.logger.debug(f"{translations['shape_audio_2']}: {stem_source.shape}")
try: sf.write(stem_path, stem_source, self.sample_rate) self.logger.debug(f"{translations['export_success']} {stem_path}") except Exception as e: self.logger.error(f"{translations['export_error']}: {e}")
def clear_gpu_cache(self): self.logger.debug(translations["clean"]) gc.collect()
if self.torch_device == torch.device("mps"): self.logger.debug(translations["clean_cache"].format(name="MPS")) torch.mps.empty_cache()
if self.torch_device == torch.device("cuda"): self.logger.debug(translations["clean_cache"].format(name="CUDA")) torch.cuda.empty_cache()
def clear_file_specific_paths(self): self.logger.info(translations["del_path"]) self.audio_file_path = None self.audio_file_base = None self.primary_source = None self.secondary_source = None
self.primary_stem_output_path = None self.secondary_stem_output_path = None123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280import tqdmimport torchimport random
import typing as tp
from torch import nnfrom torch.nn import functional as Ffrom concurrent.futures import ThreadPoolExecutor
from .demucs import Demucsfrom .hdemucs import HDemucsfrom .htdemucs import HTDemucsfrom .utils import center_trim
Model = tp.Union[Demucs, HDemucs, HTDemucs]
class DummyPoolExecutor: class DummyResult: def __init__(self, func, *args, **kwargs): self.func = func self.args = args self.kwargs = kwargs
def result(self): return self.func(*self.args, **self.kwargs)
def __init__(self, workers=0): pass
def submit(self, func, *args, **kwargs): return DummyPoolExecutor.DummyResult(func, *args, **kwargs)
def __enter__(self): return self
def __exit__(self, exc_type, exc_value, exc_tb): return
class BagOfModels(nn.Module): def __init__(self, models: tp.List[Model], weights: tp.Optional[tp.List[tp.List[float]]] = None, segment: tp.Optional[float] = None): super().__init__() assert len(models) > 0 first = models[0]
for other in models: assert other.sources == first.sources assert other.samplerate == first.samplerate assert other.audio_channels == first.audio_channels
if segment is not None: other.segment = segment
self.audio_channels = first.audio_channels self.samplerate = first.samplerate self.sources = first.sources self.models = nn.ModuleList(models)
if weights is None: weights = [[1.0 for _ in first.sources] for _ in models] else: assert len(weights) == len(models)
for weight in weights: assert len(weight) == len(first.sources)
self.weights = weights
def forward(self, x): raise NotImplementedError("`apply_model`")
class TensorChunk: def __init__(self, tensor, offset=0, length=None): total_length = tensor.shape[-1] assert offset >= 0 assert offset < total_length
length = total_length - offset if length is None else min(total_length - offset, length)
if isinstance(tensor, TensorChunk): self.tensor = tensor.tensor self.offset = offset + tensor.offset else: self.tensor = tensor self.offset = offset
self.length = length self.device = tensor.device
@property def shape(self): shape = list(self.tensor.shape) shape[-1] = self.length
return shape
def padded(self, target_length): delta = target_length - self.length total_length = self.tensor.shape[-1] assert delta >= 0
start = self.offset - delta // 2 end = start + target_length
correct_start = max(0, start) correct_end = min(total_length, end)
pad_left = correct_start - start pad_right = end - correct_end
out = F.pad(self.tensor[..., correct_start:correct_end], (pad_left, pad_right))
assert out.shape[-1] == target_length
return out
def tensor_chunk(tensor_or_chunk): if isinstance(tensor_or_chunk, TensorChunk): return tensor_or_chunk else: assert isinstance(tensor_or_chunk, torch.Tensor)
return TensorChunk(tensor_or_chunk)
def apply_model(model, mix, shifts=1, split=True, overlap=0.25, transition_power=1.0, static_shifts=1, set_progress_bar=None, device=None, progress=False, num_workers=0, pool=None): global fut_length global bag_num global prog_bar
device = mix.device if device is None else torch.device(device)
if pool is None: pool = ThreadPoolExecutor(num_workers) if num_workers > 0 and device.type == "cpu" else DummyPoolExecutor()
kwargs = { "shifts": shifts, "split": split, "overlap": overlap, "transition_power": transition_power, "progress": progress, "device": device, "pool": pool, "set_progress_bar": set_progress_bar, "static_shifts": static_shifts, }
if isinstance(model, BagOfModels): estimates = 0 totals = [0] * len(model.sources) bag_num = len(model.models) fut_length = 0 prog_bar = 0 current_model = 0
for sub_model, weight in zip(model.models, model.weights): original_model_device = next(iter(sub_model.parameters())).device sub_model.to(device) fut_length += fut_length current_model += 1
out = apply_model(sub_model, mix, **kwargs) sub_model.to(original_model_device)
for k, inst_weight in enumerate(weight): out[:, k, :, :] *= inst_weight totals[k] += inst_weight
estimates += out del out
for k in range(estimates.shape[1]): estimates[:, k, :, :] /= totals[k]
return estimates
model.to(device) model.eval()
assert transition_power >= 1
batch, channels, length = mix.shape
if shifts: kwargs["shifts"] = 0 max_shift = int(0.5 * model.samplerate) mix = tensor_chunk(mix) padded_mix = mix.padded(length + 2 * max_shift) out = 0
for _ in range(shifts): offset = random.randint(0, max_shift) shifted = TensorChunk(padded_mix, offset, length + max_shift - offset) shifted_out = apply_model(model, shifted, **kwargs) out += shifted_out[..., max_shift - offset :]
out /= shifts
return out elif split: kwargs["split"] = False out = torch.zeros(batch, len(model.sources), channels, length, device=mix.device) sum_weight = torch.zeros(length, device=mix.device) segment = int(model.samplerate * model.segment) stride = int((1 - overlap) * segment) offsets = range(0, length, stride)
weight = torch.cat([torch.arange(1, segment // 2 + 1, device=device), torch.arange(segment - segment // 2, 0, -1, device=device)]) assert len(weight) == segment
weight = (weight / weight.max()) ** transition_power futures = []
for offset in offsets: chunk = TensorChunk(mix, offset, segment) future = pool.submit(apply_model, model, chunk, **kwargs) futures.append((future, offset)) offset += segment
if progress: futures = tqdm.tqdm(futures)
for future, offset in futures: if set_progress_bar: fut_length = len(futures) * bag_num * static_shifts prog_bar += 1 set_progress_bar(0.1, (0.8 / fut_length * prog_bar))
chunk_out = future.result() chunk_length = chunk_out.shape[-1]
out[..., offset : offset + segment] += (weight[:chunk_length] * chunk_out).to(mix.device) sum_weight[offset : offset + segment] += weight[:chunk_length].to(mix.device)
assert sum_weight.min() > 0
out /= sum_weight
return out else: valid_length = model.valid_length(length) if hasattr(model, "valid_length") else length
mix = tensor_chunk(mix) padded_mix = mix.padded(valid_length).to(device)
with torch.no_grad(): out = model(padded_mix)
return center_trim(out, length)
def demucs_segments(demucs_segment, demucs_model): if demucs_segment == "Default": segment = None
if isinstance(demucs_model, BagOfModels): if segment is not None: for sub in demucs_model.models: sub.segment = segment else: if segment is not None: sub.segment = segment else: try: segment = int(demucs_segment)
if isinstance(demucs_model, BagOfModels): if segment is not None: for sub in demucs_model.models: sub.segment = segment else: if segment is not None: sub.segment = segment except: segment = None
if isinstance(demucs_model, BagOfModels): if segment is not None: for sub in demucs_model.models: sub.segment = segment else: if segment is not None: sub.segment = segment
return demucs_model123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340import mathimport torchimport julius
import typing as tp
from torch import nn
from torch.nn import functional as F
from .utils import center_trimfrom .states import capture_init
def unfold(a, kernel_size, stride): *shape, length = a.shape n_frames = math.ceil(length / stride) tgt_length = (n_frames - 1) * stride + kernel_size a = F.pad(a, (0, tgt_length - length)) strides = list(a.stride())
assert strides[-1] == 1
strides = strides[:-1] + [stride, 1]
return a.as_strided([*shape, n_frames, kernel_size], strides)
def rescale_conv(conv, reference): scale = (conv.weight.std().detach() / reference) ** 0.5 conv.weight.data /= scale
if conv.bias is not None: conv.bias.data /= scale
def rescale_module(module, reference): for sub in module.modules(): if isinstance(sub, (nn.Conv1d, nn.ConvTranspose1d, nn.Conv2d, nn.ConvTranspose2d)): rescale_conv(sub, reference)
class BLSTM(nn.Module): def __init__(self, dim, layers=1, max_steps=None, skip=False): super().__init__() assert max_steps is None or max_steps % 4 == 0 self.max_steps = max_steps self.lstm = nn.LSTM(bidirectional=True, num_layers=layers, hidden_size=dim, input_size=dim) self.linear = nn.Linear(2 * dim, dim) self.skip = skip
def forward(self, x): B, C, T = x.shape y = x framed = False
if self.max_steps is not None and T > self.max_steps: width = self.max_steps stride = width // 2 frames = unfold(x, width, stride) nframes = frames.shape[2] framed = True x = frames.permute(0, 2, 1, 3).reshape(-1, C, width)
x = x.permute(2, 0, 1)
x = self.lstm(x)[0] x = self.linear(x) x = x.permute(1, 2, 0)
if framed: out = [] frames = x.reshape(B, -1, C, width) limit = stride // 2
for k in range(nframes): if k == 0: out.append(frames[:, k, :, :-limit]) elif k == nframes - 1: out.append(frames[:, k, :, limit:]) else: out.append(frames[:, k, :, limit:-limit])
out = torch.cat(out, -1) out = out[..., :T] x = out
if self.skip: x = x + y
return x
class LayerScale(nn.Module): def __init__(self, channels: int, init: float = 0): super().__init__() self.scale = nn.Parameter(torch.zeros(channels, requires_grad=True)) self.scale.data[:] = init
def forward(self, x): return self.scale[:, None] * x
class DConv(nn.Module): def __init__(self, channels: int, compress: float = 4, depth: int = 2, init: float = 1e-4, norm=True, attn=False, heads=4, ndecay=4, lstm=False, gelu=True, kernel=3, dilate=True): super().__init__() assert kernel % 2 == 1 self.channels = channels self.compress = compress self.depth = abs(depth) dilate = depth > 0
norm_fn: tp.Callable[[int], nn.Module] norm_fn = lambda d: nn.Identity()
if norm: norm_fn = lambda d: nn.GroupNorm(1, d)
hidden = int(channels / compress)
act: tp.Type[nn.Module] act = nn.GELU if gelu else nn.ReLU self.layers = nn.ModuleList([])
for d in range(self.depth): dilation = 2**d if dilate else 1 padding = dilation * (kernel // 2)
mods = [ nn.Conv1d(channels, hidden, kernel, dilation=dilation, padding=padding), norm_fn(hidden), act(), nn.Conv1d(hidden, 2 * channels, 1), norm_fn(2 * channels), nn.GLU(1), LayerScale(channels, init), ]
if attn: mods.insert(3, LocalState(hidden, heads=heads, ndecay=ndecay)) if lstm: mods.insert(3, BLSTM(hidden, layers=2, max_steps=200, skip=True))
layer = nn.Sequential(*mods) self.layers.append(layer)
def forward(self, x): for layer in self.layers: x = x + layer(x)
return x
class LocalState(nn.Module): def __init__(self, channels: int, heads: int = 4, nfreqs: int = 0, ndecay: int = 4): super().__init__()
assert channels % heads == 0, (channels, heads)
self.heads = heads self.nfreqs = nfreqs self.ndecay = ndecay self.content = nn.Conv1d(channels, channels, 1) self.query = nn.Conv1d(channels, channels, 1) self.key = nn.Conv1d(channels, channels, 1)
if nfreqs: self.query_freqs = nn.Conv1d(channels, heads * nfreqs, 1)
if ndecay: self.query_decay = nn.Conv1d(channels, heads * ndecay, 1) self.query_decay.weight.data *= 0.01
assert self.query_decay.bias is not None
self.query_decay.bias.data[:] = -2
self.proj = nn.Conv1d(channels + heads * nfreqs, channels, 1)
def forward(self, x): B, C, T = x.shape heads = self.heads indexes = torch.arange(T, device=x.device, dtype=x.dtype) delta = indexes[:, None] - indexes[None, :]
queries = self.query(x).view(B, heads, -1, T) keys = self.key(x).view(B, heads, -1, T)
dots = torch.einsum("bhct,bhcs->bhts", keys, queries) dots /= keys.shape[2] ** 0.5
if self.nfreqs: periods = torch.arange(1, self.nfreqs + 1, device=x.device, dtype=x.dtype) freq_kernel = torch.cos(2 * math.pi * delta / periods.view(-1, 1, 1)) freq_q = self.query_freqs(x).view(B, heads, -1, T) / self.nfreqs**0.5 dots += torch.einsum("fts,bhfs->bhts", freq_kernel, freq_q)
if self.ndecay: decays = torch.arange(1, self.ndecay + 1, device=x.device, dtype=x.dtype) decay_q = self.query_decay(x).view(B, heads, -1, T) decay_q = torch.sigmoid(decay_q) / 2 decay_kernel = -decays.view(-1, 1, 1) * delta.abs() / self.ndecay**0.5 dots += torch.einsum("fts,bhfs->bhts", decay_kernel, decay_q)
dots.masked_fill_(torch.eye(T, device=dots.device, dtype=torch.bool), -100) weights = torch.softmax(dots, dim=2)
content = self.content(x).view(B, heads, -1, T) result = torch.einsum("bhts,bhct->bhcs", weights, content)
if self.nfreqs: time_sig = torch.einsum("bhts,fts->bhfs", weights, freq_kernel) result = torch.cat([result, time_sig], 2)
result = result.reshape(B, -1, T) return x + self.proj(result)
class Demucs(nn.Module): @capture_init def __init__(self, sources, audio_channels=2, channels=64, growth=2.0, depth=6, rewrite=True, lstm_layers=0, kernel_size=8, stride=4, context=1, gelu=True, glu=True, norm_starts=4, norm_groups=4, dconv_mode=1, dconv_depth=2, dconv_comp=4, dconv_attn=4, dconv_lstm=4, dconv_init=1e-4, normalize=True, resample=True, rescale=0.1, samplerate=44100, segment=4 * 10): super().__init__() self.audio_channels = audio_channels self.sources = sources self.kernel_size = kernel_size self.context = context self.stride = stride self.depth = depth self.resample = resample self.channels = channels self.normalize = normalize self.samplerate = samplerate self.segment = segment
self.encoder = nn.ModuleList() self.decoder = nn.ModuleList() self.skip_scales = nn.ModuleList()
if glu: activation = nn.GLU(dim=1) ch_scale = 2 else: activation = nn.ReLU() ch_scale = 1
act2 = nn.GELU if gelu else nn.ReLU
in_channels = audio_channels padding = 0
for index in range(depth): norm_fn = lambda d: nn.Identity()
if index >= norm_starts: norm_fn = lambda d: nn.GroupNorm(norm_groups, d)
encode = [] encode += [nn.Conv1d(in_channels, channels, kernel_size, stride), norm_fn(channels), act2()]
attn = index >= dconv_attn lstm = index >= dconv_lstm
if dconv_mode & 1: encode += [DConv(channels, depth=dconv_depth, init=dconv_init, compress=dconv_comp, attn=attn, lstm=lstm)] if rewrite: encode += [nn.Conv1d(channels, ch_scale * channels, 1), norm_fn(ch_scale * channels), activation]
self.encoder.append(nn.Sequential(*encode))
decode = []
out_channels = in_channels if index > 0 else len(self.sources) * audio_channels if rewrite: decode += [nn.Conv1d(channels, ch_scale * channels, 2 * context + 1, padding=context), norm_fn(ch_scale * channels), activation] if dconv_mode & 2: decode += [DConv(channels, depth=dconv_depth, init=dconv_init, compress=dconv_comp, attn=attn, lstm=lstm)]
decode += [nn.ConvTranspose1d(channels, out_channels, kernel_size, stride, padding=padding)]
if index > 0: decode += [norm_fn(out_channels), act2()]
self.decoder.insert(0, nn.Sequential(*decode)) in_channels = channels channels = int(growth * channels)
channels = in_channels
self.lstm = BLSTM(channels, lstm_layers) if lstm_layers else None
if rescale: rescale_module(self, reference=rescale)
def valid_length(self, length): if self.resample: length *= 2
for _ in range(self.depth): length = math.ceil((length - self.kernel_size) / self.stride) + 1 length = max(1, length)
for _ in range(self.depth): length = (length - 1) * self.stride + self.kernel_size
if self.resample: length = math.ceil(length / 2)
return int(length)
def forward(self, mix): x = mix length = x.shape[-1]
if self.normalize: mono = mix.mean(dim=1, keepdim=True) mean = mono.mean(dim=-1, keepdim=True) std = mono.std(dim=-1, keepdim=True) x = (x - mean) / (1e-5 + std) else: mean = 0 std = 1
delta = self.valid_length(length) - length x = F.pad(x, (delta // 2, delta - delta // 2))
if self.resample: x = julius.resample_frac(x, 1, 2)
saved = []
for encode in self.encoder: x = encode(x) saved.append(x)
if self.lstm: x = self.lstm(x)
for decode in self.decoder: skip = saved.pop(-1) skip = center_trim(skip, x) x = decode(x + skip)
if self.resample: x = julius.resample_frac(x, 2, 1)
x = x * std + mean x = center_trim(x, length) x = x.view(x.size(0), len(self.sources), self.audio_channels, x.size(-1))
return x
def load_state_dict(self, state, strict=True): for idx in range(self.depth): for a in ["encoder", "decoder"]: for b in ["bias", "weight"]: new = f"{a}.{idx}.3.{b}" old = f"{a}.{idx}.2.{b}"
if old in state and new not in state: state[new] = state.pop(old) super().load_state_dict(state, strict=strict)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850import mathimport torch
import typing as tp
from torch import nnfrom copy import deepcopyfrom typing import Optional
from torch.nn import functional as F
from .states import capture_initfrom .demucs import DConv, rescale_module
def spectro(x, n_fft=512, hop_length=None, pad=0): *other, length = x.shape x = x.reshape(-1, length) device_type = x.device.type is_other_gpu = not device_type in ["cuda", "cpu"]
if is_other_gpu: x = x.cpu()
z = torch.stft(x, n_fft * (1 + pad), hop_length or n_fft // 4, window=torch.hann_window(n_fft).to(x), win_length=n_fft, normalized=True, center=True, return_complex=True, pad_mode="reflect") _, freqs, frame = z.shape
return z.view(*other, freqs, frame)
def ispectro(z, hop_length=None, length=None, pad=0): *other, freqs, frames = z.shape n_fft = 2 * freqs - 2 z = z.view(-1, freqs, frames) win_length = n_fft // (1 + pad) device_type = z.device.type is_other_gpu = not device_type in ["cuda", "cpu"]
if is_other_gpu: z = z.cpu()
x = torch.istft(z, n_fft, hop_length, window=torch.hann_window(win_length).to(z.real), win_length=win_length, normalized=True, length=length, center=True) _, length = x.shape
return x.view(*other, length)
def atan2(y, x): pi = 2 * torch.asin(torch.tensor(1.0)) x += ((x == 0) & (y == 0)) * 1.0
out = torch.atan(y / x) out += ((y >= 0) & (x < 0)) * pi out -= ((y < 0) & (x < 0)) * pi out *= 1 - ((y > 0) & (x == 0)) * 1.0 out += ((y > 0) & (x == 0)) * (pi / 2) out *= 1 - ((y < 0) & (x == 0)) * 1.0 out += ((y < 0) & (x == 0)) * (-pi / 2)
return out
def _norm(x: torch.Tensor) -> torch.Tensor: return torch.abs(x[..., 0]) ** 2 + torch.abs(x[..., 1]) ** 2
def _mul_add(a: torch.Tensor, b: torch.Tensor, out: Optional[torch.Tensor] = None) -> torch.Tensor: target_shape = torch.Size([max(sa, sb) for (sa, sb) in zip(a.shape, b.shape)])
if out is None or out.shape != target_shape: out = torch.zeros(target_shape, dtype=a.dtype, device=a.device)
if out is a: real_a = a[..., 0] out[..., 0] = out[..., 0] + (real_a * b[..., 0] - a[..., 1] * b[..., 1]) out[..., 1] = out[..., 1] + (real_a * b[..., 1] + a[..., 1] * b[..., 0]) else: out[..., 0] = out[..., 0] + (a[..., 0] * b[..., 0] - a[..., 1] * b[..., 1]) out[..., 1] = out[..., 1] + (a[..., 0] * b[..., 1] + a[..., 1] * b[..., 0])
return out
def _mul(a: torch.Tensor, b: torch.Tensor, out: Optional[torch.Tensor] = None) -> torch.Tensor: target_shape = torch.Size([max(sa, sb) for (sa, sb) in zip(a.shape, b.shape)])
if out is None or out.shape != target_shape: out = torch.zeros(target_shape, dtype=a.dtype, device=a.device)
if out is a: real_a = a[..., 0] out[..., 0] = real_a * b[..., 0] - a[..., 1] * b[..., 1] out[..., 1] = real_a * b[..., 1] + a[..., 1] * b[..., 0] else: out[..., 0] = a[..., 0] * b[..., 0] - a[..., 1] * b[..., 1] out[..., 1] = a[..., 0] * b[..., 1] + a[..., 1] * b[..., 0]
return out
def _inv(z: torch.Tensor, out: Optional[torch.Tensor] = None) -> torch.Tensor: ez = _norm(z)
if out is None or out.shape != z.shape: out = torch.zeros_like(z)
out[..., 0] = z[..., 0] / ez out[..., 1] = -z[..., 1] / ez
return out
def _conj(z, out: Optional[torch.Tensor] = None) -> torch.Tensor: if out is None or out.shape != z.shape: out = torch.zeros_like(z)
out[..., 0] = z[..., 0] out[..., 1] = -z[..., 1]
return out
def _invert(M: torch.Tensor, out: Optional[torch.Tensor] = None) -> torch.Tensor: nb_channels = M.shape[-2]
if out is None or out.shape != M.shape: out = torch.empty_like(M)
if nb_channels == 1: out = _inv(M, out) elif nb_channels == 2: det = _mul(M[..., 0, 0, :], M[..., 1, 1, :]) det = det - _mul(M[..., 0, 1, :], M[..., 1, 0, :]) invDet = _inv(det)
out[..., 0, 0, :] = _mul(invDet, M[..., 1, 1, :], out[..., 0, 0, :]) out[..., 1, 0, :] = _mul(-invDet, M[..., 1, 0, :], out[..., 1, 0, :]) out[..., 0, 1, :] = _mul(-invDet, M[..., 0, 1, :], out[..., 0, 1, :]) out[..., 1, 1, :] = _mul(invDet, M[..., 0, 0, :], out[..., 1, 1, :]) else: raise Exception("Torch == 2 Channels") return out
def expectation_maximization(y: torch.Tensor, x: torch.Tensor, iterations: int = 2, eps: float = 1e-10, batch_size: int = 200): (nb_frames, nb_bins, nb_channels) = x.shape[:-1] nb_sources = y.shape[-1]
regularization = torch.cat((torch.eye(nb_channels, dtype=x.dtype, device=x.device)[..., None], torch.zeros((nb_channels, nb_channels, 1), dtype=x.dtype, device=x.device)), dim=2) regularization = torch.sqrt(torch.as_tensor(eps)) * (regularization[None, None, ...].expand((-1, nb_bins, -1, -1, -1)))
R = [torch.zeros((nb_bins, nb_channels, nb_channels, 2), dtype=x.dtype, device=x.device) for j in range(nb_sources)] weight: torch.Tensor = torch.zeros((nb_bins,), dtype=x.dtype, device=x.device)
v: torch.Tensor = torch.zeros((nb_frames, nb_bins, nb_sources), dtype=x.dtype, device=x.device)
for _ in range(iterations): v = torch.mean(torch.abs(y[..., 0, :]) ** 2 + torch.abs(y[..., 1, :]) ** 2, dim=-2)
for j in range(nb_sources): R[j] = torch.tensor(0.0, device=x.device)
weight = torch.tensor(eps, device=x.device) pos: int = 0
batch_size = batch_size if batch_size else nb_frames
while pos < nb_frames: t = torch.arange(pos, min(nb_frames, pos + batch_size)) pos = int(t[-1]) + 1
R[j] = R[j] + torch.sum(_covariance(y[t, ..., j]), dim=0) weight = weight + torch.sum(v[t, ..., j], dim=0)
R[j] = R[j] / weight[..., None, None, None] weight = torch.zeros_like(weight)
if y.requires_grad: y = y.clone()
pos = 0
while pos < nb_frames: t = torch.arange(pos, min(nb_frames, pos + batch_size)) pos = int(t[-1]) + 1
y[t, ...] = torch.tensor(0.0, device=x.device, dtype=x.dtype)
Cxx = regularization
for j in range(nb_sources): Cxx = Cxx + (v[t, ..., j, None, None, None] * R[j][None, ...].clone())
inv_Cxx = _invert(Cxx)
for j in range(nb_sources): gain = torch.zeros_like(inv_Cxx)
indices = torch.cartesian_prod(torch.arange(nb_channels), torch.arange(nb_channels), torch.arange(nb_channels))
for index in indices: gain[:, :, index[0], index[1], :] = _mul_add(R[j][None, :, index[0], index[2], :].clone(), inv_Cxx[:, :, index[2], index[1], :], gain[:, :, index[0], index[1], :])
gain = gain * v[t, ..., None, None, None, j]
for i in range(nb_channels): y[t, ..., j] = _mul_add(gain[..., i, :], x[t, ..., i, None, :], y[t, ..., j])
return y, v, R
def wiener(targets_spectrograms: torch.Tensor, mix_stft: torch.Tensor, iterations: int = 1, softmask: bool = False, residual: bool = False, scale_factor: float = 10.0, eps: float = 1e-10): if softmask: y = mix_stft[..., None] * (targets_spectrograms / (eps + torch.sum(targets_spectrograms, dim=-1, keepdim=True).to(mix_stft.dtype)))[..., None, :] else: angle = atan2(mix_stft[..., 1], mix_stft[..., 0])[..., None] nb_sources = targets_spectrograms.shape[-1] y = torch.zeros(mix_stft.shape + (nb_sources,), dtype=mix_stft.dtype, device=mix_stft.device) y[..., 0, :] = targets_spectrograms * torch.cos(angle) y[..., 1, :] = targets_spectrograms * torch.sin(angle)
if residual: y = torch.cat([y, mix_stft[..., None] - y.sum(dim=-1, keepdim=True)], dim=-1) if iterations == 0: return y
max_abs = torch.max(torch.as_tensor(1.0, dtype=mix_stft.dtype, device=mix_stft.device), torch.sqrt(_norm(mix_stft)).max() / scale_factor)
mix_stft = mix_stft / max_abs y = y / max_abs
y = expectation_maximization(y, mix_stft, iterations, eps=eps)[0]
y = y * max_abs return y
def _covariance(y_j): (nb_frames, nb_bins, nb_channels) = y_j.shape[:-1]
Cj = torch.zeros((nb_frames, nb_bins, nb_channels, nb_channels, 2), dtype=y_j.dtype, device=y_j.device) indices = torch.cartesian_prod(torch.arange(nb_channels), torch.arange(nb_channels))
for index in indices: Cj[:, :, index[0], index[1], :] = _mul_add(y_j[:, :, index[0], :], _conj(y_j[:, :, index[1], :]), Cj[:, :, index[0], index[1], :])
return Cj
def pad1d(x: torch.Tensor, paddings: tp.Tuple[int, int], mode: str = "constant", value: float = 0.0): x0 = x length = x.shape[-1] padding_left, padding_right = paddings
if mode == "reflect": max_pad = max(padding_left, padding_right)
if length <= max_pad: extra_pad = max_pad - length + 1 extra_pad_right = min(padding_right, extra_pad) extra_pad_left = extra_pad - extra_pad_right paddings = (padding_left - extra_pad_left, padding_right - extra_pad_right) x = F.pad(x, (extra_pad_left, extra_pad_right))
out = F.pad(x, paddings, mode, value)
assert out.shape[-1] == length + padding_left + padding_right assert (out[..., padding_left : padding_left + length] == x0).all() return out
class ScaledEmbedding(nn.Module): def __init__(self, num_embeddings: int, embedding_dim: int, scale: float = 10.0, smooth=False): super().__init__()
self.embedding = nn.Embedding(num_embeddings, embedding_dim)
if smooth: weight = torch.cumsum(self.embedding.weight.data, dim=0) weight = weight / torch.arange(1, num_embeddings + 1).to(weight).sqrt()[:, None]
self.embedding.weight.data[:] = weight
self.embedding.weight.data /= scale self.scale = scale
@property def weight(self): return self.embedding.weight * self.scale
def forward(self, x): return self.embedding(x) * self.scale
class HEncLayer(nn.Module): def __init__(self, chin, chout, kernel_size=8, stride=4, norm_groups=1, empty=False, freq=True, dconv=True, norm=True, context=0, dconv_kw={}, pad=True, rewrite=True): super().__init__() norm_fn = lambda d: nn.Identity()
if norm: norm_fn = lambda d: nn.GroupNorm(norm_groups, d)
pad = kernel_size // 4 if pad else 0
klass = nn.Conv1d self.freq = freq self.kernel_size = kernel_size self.stride = stride self.empty = empty self.norm = norm self.pad = pad
if freq: kernel_size = [kernel_size, 1] stride = [stride, 1] pad = [pad, 0] klass = nn.Conv2d self.conv = klass(chin, chout, kernel_size, stride, pad)
if self.empty: return self.norm1 = norm_fn(chout) self.rewrite = None
if rewrite: self.rewrite = klass(chout, 2 * chout, 1 + 2 * context, 1, context) self.norm2 = norm_fn(2 * chout)
self.dconv = None
if dconv: self.dconv = DConv(chout, **dconv_kw)
def forward(self, x, inject=None): if not self.freq and x.dim() == 4: B, C, Fr, T = x.shape x = x.view(B, -1, T)
if not self.freq: le = x.shape[-1]
if not le % self.stride == 0: x = F.pad(x, (0, self.stride - (le % self.stride)))
y = self.conv(x)
if self.empty: return y if inject is not None: assert inject.shape[-1] == y.shape[-1], (inject.shape, y.shape)
if inject.dim() == 3 and y.dim() == 4: inject = inject[:, :, None]
y = y + inject y = F.gelu(self.norm1(y))
if self.dconv: if self.freq: B, C, Fr, T = y.shape y = y.permute(0, 2, 1, 3).reshape(-1, C, T)
y = self.dconv(y)
if self.freq: y = y.view(B, Fr, C, T).permute(0, 2, 1, 3)
if self.rewrite: z = self.norm2(self.rewrite(y)) z = F.glu(z, dim=1) else: z = y
return z
class MultiWrap(nn.Module): def __init__(self, layer, split_ratios): super().__init__()
self.split_ratios = split_ratios self.layers = nn.ModuleList() self.conv = isinstance(layer, HEncLayer)
assert not layer.norm assert layer.freq assert layer.pad
if not self.conv: assert not layer.context_freq
for _ in range(len(split_ratios) + 1): lay = deepcopy(layer)
if self.conv: lay.conv.padding = (0, 0) else: lay.pad = False
for m in lay.modules(): if hasattr(m, "reset_parameters"): m.reset_parameters()
self.layers.append(lay)
def forward(self, x, skip=None, length=None): B, C, Fr, T = x.shape
ratios = list(self.split_ratios) + [1] start = 0 outs = []
for ratio, layer in zip(ratios, self.layers): if self.conv: pad = layer.kernel_size // 4
if ratio == 1: limit = Fr frames = -1 else: limit = int(round(Fr * ratio)) le = limit - start
if start == 0: le += pad frames = round((le - layer.kernel_size) / layer.stride + 1) limit = start + (frames - 1) * layer.stride + layer.kernel_size if start == 0: limit -= pad
assert limit - start > 0, (limit, start) assert limit <= Fr, (limit, Fr)
y = x[:, :, start:limit, :]
if start == 0: y = F.pad(y, (0, 0, pad, 0)) if ratio == 1: y = F.pad(y, (0, 0, 0, pad))
outs.append(layer(y)) start = limit - layer.kernel_size + layer.stride else: limit = Fr if ratio == 1 else int(round(Fr * ratio))
last = layer.last layer.last = True
y = x[:, :, start:limit] s = skip[:, :, start:limit] out, _ = layer(y, s, None)
if outs: outs[-1][:, :, -layer.stride :] += out[:, :, : layer.stride] - layer.conv_tr.bias.view(1, -1, 1, 1) out = out[:, :, layer.stride :]
if ratio == 1: out = out[:, :, : -layer.stride // 2, :] if start == 0: out = out[:, :, layer.stride // 2 :, :]
outs.append(out) layer.last = last start = limit
out = torch.cat(outs, dim=2)
if not self.conv and not last: out = F.gelu(out)
if self.conv: return out else: return out, None
class HDecLayer(nn.Module): def __init__(self, chin, chout, last=False, kernel_size=8, stride=4, norm_groups=1, empty=False, freq=True, dconv=True, norm=True, context=1, dconv_kw={}, pad=True, context_freq=True, rewrite=True): super().__init__() norm_fn = lambda d: nn.Identity()
if norm: norm_fn = lambda d: nn.GroupNorm(norm_groups, d)
pad = kernel_size // 4 if pad else 0 self.pad = pad self.last = last self.freq = freq self.chin = chin self.empty = empty self.stride = stride self.kernel_size = kernel_size self.norm = norm self.context_freq = context_freq klass = nn.Conv1d klass_tr = nn.ConvTranspose1d
if freq: kernel_size = [kernel_size, 1] stride = [stride, 1] klass = nn.Conv2d klass_tr = nn.ConvTranspose2d
self.conv_tr = klass_tr(chin, chout, kernel_size, stride) self.norm2 = norm_fn(chout)
if self.empty: return self.rewrite = None
if rewrite: if context_freq: self.rewrite = klass(chin, 2 * chin, 1 + 2 * context, 1, context) else: self.rewrite = klass(chin, 2 * chin, [1, 1 + 2 * context], 1, [0, context])
self.norm1 = norm_fn(2 * chin)
self.dconv = None
if dconv: self.dconv = DConv(chin, **dconv_kw)
def forward(self, x, skip, length): if self.freq and x.dim() == 3: B, C, T = x.shape x = x.view(B, self.chin, -1, T)
if not self.empty: x = x + skip
y = F.glu(self.norm1(self.rewrite(x)), dim=1) if self.rewrite else x if self.dconv: if self.freq: B, C, Fr, T = y.shape y = y.permute(0, 2, 1, 3).reshape(-1, C, T)
y = self.dconv(y)
if self.freq: y = y.view(B, Fr, C, T).permute(0, 2, 1, 3) else: y = x assert skip is None
z = self.norm2(self.conv_tr(y))
if self.freq: if self.pad: z = z[..., self.pad : -self.pad, :] else: z = z[..., self.pad : self.pad + length] assert z.shape[-1] == length, (z.shape[-1], length)
if not self.last: z = F.gelu(z)
return z, y
class HDemucs(nn.Module): @capture_init def __init__(self, sources, audio_channels=2, channels=48, channels_time=None, growth=2, nfft=4096, wiener_iters=0, end_iters=0, wiener_residual=False, cac=True, depth=6, rewrite=True, hybrid=True, hybrid_old=False, multi_freqs=None, multi_freqs_depth=2, freq_emb=0.2, emb_scale=10, emb_smooth=True, kernel_size=8, time_stride=2, stride=4, context=1, context_enc=0, norm_starts=4, norm_groups=4, dconv_mode=1, dconv_depth=2, dconv_comp=4, dconv_attn=4, dconv_lstm=4, dconv_init=1e-4, rescale=0.1, samplerate=44100, segment=4 * 10): super().__init__()
self.cac = cac self.wiener_residual = wiener_residual self.audio_channels = audio_channels self.sources = sources self.kernel_size = kernel_size self.context = context self.stride = stride self.depth = depth self.channels = channels self.samplerate = samplerate self.segment = segment self.nfft = nfft self.hop_length = nfft // 4 self.wiener_iters = wiener_iters self.end_iters = end_iters self.freq_emb = None self.hybrid = hybrid self.hybrid_old = hybrid_old
if hybrid_old: assert hybrid if hybrid: assert wiener_iters == end_iters
self.encoder = nn.ModuleList() self.decoder = nn.ModuleList()
if hybrid: self.tencoder = nn.ModuleList() self.tdecoder = nn.ModuleList()
chin = audio_channels chin_z = chin
if self.cac: chin_z *= 2
chout = channels_time or channels chout_z = channels freqs = nfft // 2
for index in range(depth): lstm = index >= dconv_lstm attn = index >= dconv_attn norm = index >= norm_starts freq = freqs > 1 stri = stride ker = kernel_size
if not freq: assert freqs == 1
ker = time_stride * 2 stri = time_stride
pad = True last_freq = False
if freq and freqs <= kernel_size: ker = freqs pad = False last_freq = True
kw = { "kernel_size": ker, "stride": stri, "freq": freq, "pad": pad, "norm": norm, "rewrite": rewrite, "norm_groups": norm_groups, "dconv_kw": {"lstm": lstm, "attn": attn, "depth": dconv_depth, "compress": dconv_comp, "init": dconv_init, "gelu": True}, }
kwt = dict(kw) kwt["freq"] = 0 kwt["kernel_size"] = kernel_size kwt["stride"] = stride kwt["pad"] = True kw_dec = dict(kw)
multi = False
if multi_freqs and index < multi_freqs_depth: multi = True kw_dec["context_freq"] = False
if last_freq: chout_z = max(chout, chout_z) chout = chout_z
enc = HEncLayer(chin_z, chout_z, dconv=dconv_mode & 1, context=context_enc, **kw)
if hybrid and freq: tenc = HEncLayer(chin, chout, dconv=dconv_mode & 1, context=context_enc, empty=last_freq, **kwt) self.tencoder.append(tenc)
if multi: enc = MultiWrap(enc, multi_freqs)
self.encoder.append(enc)
if index == 0: chin = self.audio_channels * len(self.sources) chin_z = chin
if self.cac: chin_z *= 2
dec = HDecLayer(chout_z, chin_z, dconv=dconv_mode & 2, last=index == 0, context=context, **kw_dec)
if multi: dec = MultiWrap(dec, multi_freqs)
if hybrid and freq: tdec = HDecLayer(chout, chin, dconv=dconv_mode & 2, empty=last_freq, last=index == 0, context=context, **kwt) self.tdecoder.insert(0, tdec)
self.decoder.insert(0, dec)
chin = chout chin_z = chout_z
chout = int(growth * chout) chout_z = int(growth * chout_z)
if freq: if freqs <= kernel_size: freqs = 1 else: freqs //= stride
if index == 0 and freq_emb: self.freq_emb = ScaledEmbedding(freqs, chin_z, smooth=emb_smooth, scale=emb_scale) self.freq_emb_scale = freq_emb
if rescale: rescale_module(self, reference=rescale)
def _spec(self, x): hl = self.hop_length nfft = self.nfft
if self.hybrid: assert hl == nfft // 4
le = int(math.ceil(x.shape[-1] / hl)) pad = hl // 2 * 3
x = pad1d(x, (pad, pad + le * hl - x.shape[-1]), mode="reflect") if not self.hybrid_old else pad1d(x, (pad, pad + le * hl - x.shape[-1]))
z = spectro(x, nfft, hl)[..., :-1, :]
if self.hybrid: assert z.shape[-1] == le + 4, (z.shape, x.shape, le) z = z[..., 2 : 2 + le]
return z
def _ispec(self, z, length=None, scale=0): hl = self.hop_length // (4**scale) z = F.pad(z, (0, 0, 0, 1))
if self.hybrid: z = F.pad(z, (2, 2)) pad = hl // 2 * 3 le = hl * int(math.ceil(length / hl)) + 2 * pad if not self.hybrid_old else hl * int(math.ceil(length / hl))
x = ispectro(z, hl, length=le) x = x[..., pad : pad + length] if not self.hybrid_old else x[..., :length] else: x = ispectro(z, hl, length)
return x
def _magnitude(self, z): if self.cac: B, C, Fr, T = z.shape m = torch.view_as_real(z).permute(0, 1, 4, 2, 3) m = m.reshape(B, C * 2, Fr, T) else: m = z.abs()
return m
def _mask(self, z, m): niters = self.wiener_iters
if self.cac: B, S, C, Fr, T = m.shape out = m.view(B, S, -1, 2, Fr, T).permute(0, 1, 2, 4, 5, 3) out = torch.view_as_complex(out.contiguous()) return out if self.training: niters = self.end_iters
if niters < 0: z = z[:, None] return z / (1e-8 + z.abs()) * m else: return self._wiener(m, z, niters)
def _wiener(self, mag_out, mix_stft, niters): init = mix_stft.dtype wiener_win_len = 300 residual = self.wiener_residual
B, S, C, Fq, T = mag_out.shape mag_out = mag_out.permute(0, 4, 3, 2, 1) mix_stft = torch.view_as_real(mix_stft.permute(0, 3, 2, 1))
outs = []
for sample in range(B): pos = 0 out = []
for pos in range(0, T, wiener_win_len): frame = slice(pos, pos + wiener_win_len) z_out = wiener(mag_out[sample, frame], mix_stft[sample, frame], niters, residual=residual) out.append(z_out.transpose(-1, -2))
outs.append(torch.cat(out, dim=0))
out = torch.view_as_complex(torch.stack(outs, 0)) out = out.permute(0, 4, 3, 2, 1).contiguous()
if residual: out = out[:, :-1]
assert list(out.shape) == [B, S, C, Fq, T] return out.to(init)
def forward(self, mix): x = mix length = x.shape[-1]
z = self._spec(mix) mag = self._magnitude(z).to(mix.device) x = mag
B, C, Fq, T = x.shape
mean = x.mean(dim=(1, 2, 3), keepdim=True) std = x.std(dim=(1, 2, 3), keepdim=True) x = (x - mean) / (1e-5 + std)
if self.hybrid: xt = mix meant = xt.mean(dim=(1, 2), keepdim=True) stdt = xt.std(dim=(1, 2), keepdim=True) xt = (xt - meant) / (1e-5 + stdt)
saved = [] saved_t = [] lengths = [] lengths_t = []
for idx, encode in enumerate(self.encoder): lengths.append(x.shape[-1]) inject = None
if self.hybrid and idx < len(self.tencoder): lengths_t.append(xt.shape[-1]) tenc = self.tencoder[idx] xt = tenc(xt)
if not tenc.empty: saved_t.append(xt) else: inject = xt
x = encode(x, inject)
if idx == 0 and self.freq_emb is not None: frs = torch.arange(x.shape[-2], device=x.device) emb = self.freq_emb(frs).t()[None, :, :, None].expand_as(x) x = x + self.freq_emb_scale * emb
saved.append(x)
x = torch.zeros_like(x)
if self.hybrid: xt = torch.zeros_like(x)
for idx, decode in enumerate(self.decoder): skip = saved.pop(-1) x, pre = decode(x, skip, lengths.pop(-1))
if self.hybrid: offset = self.depth - len(self.tdecoder)
if self.hybrid and idx >= offset: tdec = self.tdecoder[idx - offset] length_t = lengths_t.pop(-1)
if tdec.empty: assert pre.shape[2] == 1, pre.shape
pre = pre[:, :, 0] xt, _ = tdec(pre, None, length_t) else: skip = saved_t.pop(-1) xt, _ = tdec(xt, skip, length_t)
assert len(saved) == 0 assert len(lengths_t) == 0 assert len(saved_t) == 0
S = len(self.sources) x = x.view(B, S, -1, Fq, T) x = x * std[:, None] + mean[:, None]
device_type = x.device.type device_load = f"{device_type}:{x.device.index}" if not device_type == "mps" else device_type x_is_other_gpu = not device_type in ["cuda", "cpu"]
if x_is_other_gpu: x = x.cpu()
zout = self._mask(z, x) x = self._ispec(zout, length)
if x_is_other_gpu: x = x.to(device_load)
if self.hybrid: xt = xt.view(B, S, -1, length) xt = xt * stdt[:, None] + meant[:, None] x = xt + x
return x123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690import osimport sysimport mathimport torchimport random
import numpy as npimport typing as tp
from torch import nnfrom einops import rearrangefrom fractions import Fractionfrom torch.nn import functional as F
now_dir = os.getcwd()sys.path.append(now_dir)
from .states import capture_initfrom .demucs import rescale_modulefrom main.configs.config import Configfrom .hdemucs import pad1d, spectro, ispectro, wiener, ScaledEmbedding, HEncLayer, MultiWrap, HDecLayer
translations = Config().translations
def create_sin_embedding(length: int, dim: int, shift: int = 0, device="cpu", max_period=10000): assert dim % 2 == 0
pos = shift + torch.arange(length, device=device).view(-1, 1, 1) half_dim = dim // 2
adim = torch.arange(dim // 2, device=device).view(1, 1, -1) phase = pos / (max_period ** (adim / (half_dim - 1)))
return torch.cat([torch.cos(phase), torch.sin(phase)], dim=-1)
def create_2d_sin_embedding(d_model, height, width, device="cpu", max_period=10000): if d_model % 4 != 0: raise ValueError(translations["dims"].format(dims=d_model))
pe = torch.zeros(d_model, height, width)
d_model = int(d_model / 2)
div_term = torch.exp(torch.arange(0.0, d_model, 2) * -(math.log(max_period) / d_model))
pos_w = torch.arange(0.0, width).unsqueeze(1) pos_h = torch.arange(0.0, height).unsqueeze(1)
pe[0:d_model:2, :, :] = torch.sin(pos_w * div_term).transpose(0, 1).unsqueeze(1).repeat(1, height, 1) pe[1:d_model:2, :, :] = torch.cos(pos_w * div_term).transpose(0, 1).unsqueeze(1).repeat(1, height, 1) pe[d_model::2, :, :] = torch.sin(pos_h * div_term).transpose(0, 1).unsqueeze(2).repeat(1, 1, width) pe[d_model + 1 :: 2, :, :] = torch.cos(pos_h * div_term).transpose(0, 1).unsqueeze(2).repeat(1, 1, width)
return pe[None, :].to(device)
def create_sin_embedding_cape( length: int, dim: int, batch_size: int, mean_normalize: bool, augment: bool, max_global_shift: float = 0.0, max_local_shift: float = 0.0, max_scale: float = 1.0, device: str = "cpu", max_period: float = 10000.0): assert dim % 2 == 0
pos = 1.0 * torch.arange(length).view(-1, 1, 1) pos = pos.repeat(1, batch_size, 1)
if mean_normalize: pos -= torch.nanmean(pos, dim=0, keepdim=True)
if augment: delta = np.random.uniform(-max_global_shift, +max_global_shift, size=[1, batch_size, 1]) delta_local = np.random.uniform(-max_local_shift, +max_local_shift, size=[length, batch_size, 1])
log_lambdas = np.random.uniform(-np.log(max_scale), +np.log(max_scale), size=[1, batch_size, 1]) pos = (pos + delta + delta_local) * np.exp(log_lambdas)
pos = pos.to(device)
half_dim = dim // 2 adim = torch.arange(dim // 2, device=device).view(1, 1, -1) phase = pos / (max_period ** (adim / (half_dim - 1))) return torch.cat([torch.cos(phase), torch.sin(phase)], dim=-1).float()
class MyGroupNorm(nn.GroupNorm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs)
def forward(self, x): x = x.transpose(1, 2) return super().forward(x).transpose(1, 2) class LayerScale(nn.Module): def __init__(self, channels: int, init: float = 0, channel_last=False): super().__init__() self.channel_last = channel_last self.scale = nn.Parameter(torch.zeros(channels, requires_grad=True)) self.scale.data[:] = init
def forward(self, x): if self.channel_last: return self.scale * x else: return self.scale[:, None] * x
class MyTransformerEncoderLayer(nn.TransformerEncoderLayer): def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1, activation=F.relu, group_norm=0, norm_first=False, norm_out=False, layer_norm_eps=1e-5, layer_scale=False, init_values=1e-4, device=None, dtype=None, sparse=False, mask_type="diag", mask_random_seed=42, sparse_attn_window=500, global_window=50, auto_sparsity=False, sparsity=0.95, batch_first=False): factory_kwargs = {"device": device, "dtype": dtype} super().__init__(d_model=d_model, nhead=nhead, dim_feedforward=dim_feedforward, dropout=dropout, activation=activation, layer_norm_eps=layer_norm_eps, batch_first=batch_first, norm_first=norm_first, device=device, dtype=dtype)
self.auto_sparsity = auto_sparsity
if group_norm: self.norm1 = MyGroupNorm(int(group_norm), d_model, eps=layer_norm_eps, **factory_kwargs) self.norm2 = MyGroupNorm(int(group_norm), d_model, eps=layer_norm_eps, **factory_kwargs)
self.norm_out = None
if self.norm_first & norm_out: self.norm_out = MyGroupNorm(num_groups=int(norm_out), num_channels=d_model)
self.gamma_1 = LayerScale(d_model, init_values, True) if layer_scale else nn.Identity() self.gamma_2 = LayerScale(d_model, init_values, True) if layer_scale else nn.Identity()
def forward(self, src, src_mask=None, src_key_padding_mask=None): x = src T, B, C = x.shape
if self.norm_first: x = x + self.gamma_1(self._sa_block(self.norm1(x), src_mask, src_key_padding_mask)) x = x + self.gamma_2(self._ff_block(self.norm2(x)))
if self.norm_out: x = self.norm_out(x) else: x = self.norm1(x + self.gamma_1(self._sa_block(x, src_mask, src_key_padding_mask))) x = self.norm2(x + self.gamma_2(self._ff_block(x)))
return x
class CrossTransformerEncoder(nn.Module): def __init__(self, dim: int, emb: str = "sin", hidden_scale: float = 4.0, num_heads: int = 8, num_layers: int = 6, cross_first: bool = False, dropout: float = 0.0, max_positions: int = 1000, norm_in: bool = True, norm_in_group: bool = False, group_norm: int = False, norm_first: bool = False, norm_out: bool = False, max_period: float = 10000.0, weight_decay: float = 0.0, lr: tp.Optional[float] = None, layer_scale: bool = False, gelu: bool = True, sin_random_shift: int = 0, weight_pos_embed: float = 1.0, cape_mean_normalize: bool = True, cape_augment: bool = True, cape_glob_loc_scale: list = [5000.0, 1.0, 1.4], sparse_self_attn: bool = False, sparse_cross_attn: bool = False, mask_type: str = "diag", mask_random_seed: int = 42, sparse_attn_window: int = 500, global_window: int = 50, auto_sparsity: bool = False, sparsity: float = 0.95): super().__init__() assert dim % num_heads == 0
hidden_dim = int(dim * hidden_scale)
self.num_layers = num_layers self.classic_parity = 1 if cross_first else 0 self.emb = emb self.max_period = max_period self.weight_decay = weight_decay self.weight_pos_embed = weight_pos_embed self.sin_random_shift = sin_random_shift
if emb == "cape": self.cape_mean_normalize = cape_mean_normalize self.cape_augment = cape_augment self.cape_glob_loc_scale = cape_glob_loc_scale
if emb == "scaled": self.position_embeddings = ScaledEmbedding(max_positions, dim, scale=0.2)
self.lr = lr
activation: tp.Any = F.gelu if gelu else F.relu
self.norm_in: nn.Module self.norm_in_t: nn.Module
if norm_in: self.norm_in = nn.LayerNorm(dim) self.norm_in_t = nn.LayerNorm(dim) elif norm_in_group: self.norm_in = MyGroupNorm(int(norm_in_group), dim) self.norm_in_t = MyGroupNorm(int(norm_in_group), dim) else: self.norm_in = nn.Identity() self.norm_in_t = nn.Identity()
self.layers = nn.ModuleList() self.layers_t = nn.ModuleList()
kwargs_common = { "d_model": dim, "nhead": num_heads, "dim_feedforward": hidden_dim, "dropout": dropout, "activation": activation, "group_norm": group_norm, "norm_first": norm_first, "norm_out": norm_out, "layer_scale": layer_scale, "mask_type": mask_type, "mask_random_seed": mask_random_seed, "sparse_attn_window": sparse_attn_window, "global_window": global_window, "sparsity": sparsity, "auto_sparsity": auto_sparsity, "batch_first": True, }
kwargs_classic_encoder = dict(kwargs_common) kwargs_classic_encoder.update({"sparse": sparse_self_attn}) kwargs_cross_encoder = dict(kwargs_common) kwargs_cross_encoder.update({"sparse": sparse_cross_attn})
for idx in range(num_layers): if idx % 2 == self.classic_parity: self.layers.append(MyTransformerEncoderLayer(**kwargs_classic_encoder)) self.layers_t.append(MyTransformerEncoderLayer(**kwargs_classic_encoder)) else: self.layers.append(CrossTransformerEncoderLayer(**kwargs_cross_encoder)) self.layers_t.append(CrossTransformerEncoderLayer(**kwargs_cross_encoder))
def forward(self, x, xt): B, C, Fr, T1 = x.shape
pos_emb_2d = create_2d_sin_embedding(C, Fr, T1, x.device, self.max_period) pos_emb_2d = rearrange(pos_emb_2d, "b c fr t1 -> b (t1 fr) c")
x = rearrange(x, "b c fr t1 -> b (t1 fr) c") x = self.norm_in(x) x = x + self.weight_pos_embed * pos_emb_2d
B, C, T2 = xt.shape xt = rearrange(xt, "b c t2 -> b t2 c")
pos_emb = self._get_pos_embedding(T2, B, C, x.device) pos_emb = rearrange(pos_emb, "t2 b c -> b t2 c")
xt = self.norm_in_t(xt) xt = xt + self.weight_pos_embed * pos_emb
for idx in range(self.num_layers): if idx % 2 == self.classic_parity: x = self.layers[idx](x) xt = self.layers_t[idx](xt) else: old_x = x x = self.layers[idx](x, xt) xt = self.layers_t[idx](xt, old_x)
x = rearrange(x, "b (t1 fr) c -> b c fr t1", t1=T1) xt = rearrange(xt, "b t2 c -> b c t2")
return x, xt
def _get_pos_embedding(self, T, B, C, device): if self.emb == "sin": shift = random.randrange(self.sin_random_shift + 1) pos_emb = create_sin_embedding(T, C, shift=shift, device=device, max_period=self.max_period) elif self.emb == "cape": if self.training: pos_emb = create_sin_embedding_cape(T, C, B, device=device, max_period=self.max_period, mean_normalize=self.cape_mean_normalize, augment=self.cape_augment, max_global_shift=self.cape_glob_loc_scale[0], max_local_shift=self.cape_glob_loc_scale[1], max_scale=self.cape_glob_loc_scale[2]) else: pos_emb = create_sin_embedding_cape(T, C, B, device=device, max_period=self.max_period, mean_normalize=self.cape_mean_normalize, augment=False)
elif self.emb == "scaled": pos = torch.arange(T, device=device) pos_emb = self.position_embeddings(pos)[:, None]
return pos_emb
def make_optim_group(self): group = {"params": list(self.parameters()), "weight_decay": self.weight_decay} if self.lr is not None: group["lr"] = self.lr
return group
class CrossTransformerEncoderLayer(nn.Module): def __init__(self, d_model: int, nhead: int, dim_feedforward: int = 2048, dropout: float = 0.1, activation=F.relu, layer_norm_eps: float = 1e-5, layer_scale: bool = False, init_values: float = 1e-4, norm_first: bool = False, group_norm: bool = False, norm_out: bool = False, sparse=False, mask_type="diag", mask_random_seed=42, sparse_attn_window=500, global_window=50, sparsity=0.95, auto_sparsity=None, device=None, dtype=None, batch_first=False): factory_kwargs = {"device": device, "dtype": dtype} super().__init__()
self.auto_sparsity = auto_sparsity
self.cross_attn: nn.Module self.cross_attn = nn.MultiheadAttention(d_model, nhead, dropout=dropout, batch_first=batch_first)
self.linear1 = nn.Linear(d_model, dim_feedforward, **factory_kwargs) self.dropout = nn.Dropout(dropout) self.linear2 = nn.Linear(dim_feedforward, d_model, **factory_kwargs)
self.norm_first = norm_first
self.norm1: nn.Module self.norm2: nn.Module self.norm3: nn.Module
if group_norm: self.norm1 = MyGroupNorm(int(group_norm), d_model, eps=layer_norm_eps, **factory_kwargs) self.norm2 = MyGroupNorm(int(group_norm), d_model, eps=layer_norm_eps, **factory_kwargs) self.norm3 = MyGroupNorm(int(group_norm), d_model, eps=layer_norm_eps, **factory_kwargs) else: self.norm1 = nn.LayerNorm(d_model, eps=layer_norm_eps, **factory_kwargs) self.norm2 = nn.LayerNorm(d_model, eps=layer_norm_eps, **factory_kwargs) self.norm3 = nn.LayerNorm(d_model, eps=layer_norm_eps, **factory_kwargs)
self.norm_out = None if self.norm_first & norm_out: self.norm_out = MyGroupNorm(num_groups=int(norm_out), num_channels=d_model)
self.gamma_1 = LayerScale(d_model, init_values, True) if layer_scale else nn.Identity() self.gamma_2 = LayerScale(d_model, init_values, True) if layer_scale else nn.Identity()
self.dropout1 = nn.Dropout(dropout) self.dropout2 = nn.Dropout(dropout)
if isinstance(activation, str): self.activation = self._get_activation_fn(activation) else: self.activation = activation
def forward(self, q, k, mask=None): if self.norm_first: x = q + self.gamma_1(self._ca_block(self.norm1(q), self.norm2(k), mask)) x = x + self.gamma_2(self._ff_block(self.norm3(x)))
if self.norm_out: x = self.norm_out(x) else: x = self.norm1(q + self.gamma_1(self._ca_block(q, k, mask))) x = self.norm2(x + self.gamma_2(self._ff_block(x)))
return x
def _ca_block(self, q, k, attn_mask=None): x = self.cross_attn(q, k, k, attn_mask=attn_mask, need_weights=False)[0] return self.dropout1(x)
def _ff_block(self, x): x = self.linear2(self.dropout(self.activation(self.linear1(x)))) return self.dropout2(x)
def _get_activation_fn(self, activation): if activation == "relu": return F.relu elif activation == "gelu": return F.gelu
raise RuntimeError(translations["activation"].format(activation=activation))
class HTDemucs(nn.Module): @capture_init def __init__(self, sources, audio_channels=2, channels=48, channels_time=None, growth=2, nfft=4096, wiener_iters=0, end_iters=0, wiener_residual=False, cac=True, depth=4, rewrite=True, multi_freqs=None, multi_freqs_depth=3, freq_emb=0.2, emb_scale=10, emb_smooth=True, kernel_size=8, time_stride=2, stride=4, context=1, context_enc=0, norm_starts=4, norm_groups=4, dconv_mode=1, dconv_depth=2, dconv_comp=8, dconv_init=1e-3, bottom_channels=0, t_layers=5, t_emb="sin", t_hidden_scale=4.0, t_heads=8, t_dropout=0.0, t_max_positions=10000, t_norm_in=True, t_norm_in_group=False, t_group_norm=False, t_norm_first=True, t_norm_out=True, t_max_period=10000.0, t_weight_decay=0.0, t_lr=None, t_layer_scale=True, t_gelu=True, t_weight_pos_embed=1.0, t_sin_random_shift=0, t_cape_mean_normalize=True, t_cape_augment=True, t_cape_glob_loc_scale=[5000.0, 1.0, 1.4], t_sparse_self_attn=False, t_sparse_cross_attn=False, t_mask_type="diag", t_mask_random_seed=42, t_sparse_attn_window=500, t_global_window=100, t_sparsity=0.95, t_auto_sparsity=False, t_cross_first=False, rescale=0.1, samplerate=44100, segment=4 * 10, use_train_segment=True): super().__init__()
self.cac = cac self.wiener_residual = wiener_residual self.audio_channels = audio_channels self.sources = sources self.kernel_size = kernel_size self.context = context self.stride = stride self.depth = depth self.bottom_channels = bottom_channels self.channels = channels self.samplerate = samplerate self.segment = segment self.use_train_segment = use_train_segment self.nfft = nfft self.hop_length = nfft // 4 self.wiener_iters = wiener_iters self.end_iters = end_iters self.freq_emb = None
assert wiener_iters == end_iters
self.encoder = nn.ModuleList() self.decoder = nn.ModuleList() self.tencoder = nn.ModuleList() self.tdecoder = nn.ModuleList()
chin = audio_channels chin_z = chin
if self.cac: chin_z *= 2
chout = channels_time or channels chout_z = channels freqs = nfft // 2
for index in range(depth): norm = index >= norm_starts freq = freqs > 1 stri = stride ker = kernel_size
if not freq: assert freqs == 1
ker = time_stride * 2 stri = time_stride
pad = True last_freq = False
if freq and freqs <= kernel_size: ker = freqs pad = False last_freq = True
kw = { "kernel_size": ker, "stride": stri, "freq": freq, "pad": pad, "norm": norm, "rewrite": rewrite, "norm_groups": norm_groups, "dconv_kw": {"depth": dconv_depth, "compress": dconv_comp, "init": dconv_init, "gelu": True}, }
kwt = dict(kw) kwt["freq"] = 0 kwt["kernel_size"] = kernel_size kwt["stride"] = stride kwt["pad"] = True kw_dec = dict(kw)
multi = False
if multi_freqs and index < multi_freqs_depth: multi = True kw_dec["context_freq"] = False
if last_freq: chout_z = max(chout, chout_z) chout = chout_z
enc = HEncLayer(chin_z, chout_z, dconv=dconv_mode & 1, context=context_enc, **kw)
if freq: tenc = HEncLayer(chin, chout, dconv=dconv_mode & 1, context=context_enc, empty=last_freq, **kwt) self.tencoder.append(tenc)
if multi: enc = MultiWrap(enc, multi_freqs)
self.encoder.append(enc)
if index == 0: chin = self.audio_channels * len(self.sources) chin_z = chin
if self.cac: chin_z *= 2
dec = HDecLayer(chout_z, chin_z, dconv=dconv_mode & 2, last=index == 0, context=context, **kw_dec)
if multi: dec = MultiWrap(dec, multi_freqs)
if freq: tdec = HDecLayer(chout, chin, dconv=dconv_mode & 2, empty=last_freq, last=index == 0, context=context, **kwt) self.tdecoder.insert(0, tdec)
self.decoder.insert(0, dec)
chin = chout chin_z = chout_z
chout = int(growth * chout) chout_z = int(growth * chout_z)
if freq: if freqs <= kernel_size: freqs = 1 else: freqs //= stride
if index == 0 and freq_emb: self.freq_emb = ScaledEmbedding(freqs, chin_z, smooth=emb_smooth, scale=emb_scale) self.freq_emb_scale = freq_emb
if rescale: rescale_module(self, reference=rescale)
transformer_channels = channels * growth ** (depth - 1)
if bottom_channels: self.channel_upsampler = nn.Conv1d(transformer_channels, bottom_channels, 1) self.channel_downsampler = nn.Conv1d(bottom_channels, transformer_channels, 1) self.channel_upsampler_t = nn.Conv1d(transformer_channels, bottom_channels, 1) self.channel_downsampler_t = nn.Conv1d(bottom_channels, transformer_channels, 1)
transformer_channels = bottom_channels
if t_layers > 0: self.crosstransformer = CrossTransformerEncoder(dim=transformer_channels, emb=t_emb, hidden_scale=t_hidden_scale, num_heads=t_heads, num_layers=t_layers, cross_first=t_cross_first, dropout=t_dropout, max_positions=t_max_positions, norm_in=t_norm_in, norm_in_group=t_norm_in_group, group_norm=t_group_norm, norm_first=t_norm_first, norm_out=t_norm_out, max_period=t_max_period, weight_decay=t_weight_decay, lr=t_lr, layer_scale=t_layer_scale, gelu=t_gelu, sin_random_shift=t_sin_random_shift, weight_pos_embed=t_weight_pos_embed, cape_mean_normalize=t_cape_mean_normalize, cape_augment=t_cape_augment, cape_glob_loc_scale=t_cape_glob_loc_scale, sparse_self_attn=t_sparse_self_attn, sparse_cross_attn=t_sparse_cross_attn, mask_type=t_mask_type, mask_random_seed=t_mask_random_seed, sparse_attn_window=t_sparse_attn_window, global_window=t_global_window, sparsity=t_sparsity, auto_sparsity=t_auto_sparsity) else: self.crosstransformer = None
def _spec(self, x): hl = self.hop_length nfft = self.nfft
assert hl == nfft // 4
le = int(math.ceil(x.shape[-1] / hl)) pad = hl // 2 * 3
x = pad1d(x, (pad, pad + le * hl - x.shape[-1]), mode="reflect")
z = spectro(x, nfft, hl)[..., :-1, :]
assert z.shape[-1] == le + 4, (z.shape, x.shape, le)
z = z[..., 2 : 2 + le]
return z
def _ispec(self, z, length=None, scale=0): hl = self.hop_length // (4**scale) z = F.pad(z, (0, 0, 0, 1)) z = F.pad(z, (2, 2))
pad = hl // 2 * 3 le = hl * int(math.ceil(length / hl)) + 2 * pad
x = ispectro(z, hl, length=le) x = x[..., pad : pad + length]
return x
def _magnitude(self, z): if self.cac: B, C, Fr, T = z.shape m = torch.view_as_real(z).permute(0, 1, 4, 2, 3) m = m.reshape(B, C * 2, Fr, T) else: m = z.abs()
return m
def _mask(self, z, m): niters = self.wiener_iters if self.cac: B, S, C, Fr, T = m.shape out = m.view(B, S, -1, 2, Fr, T).permute(0, 1, 2, 4, 5, 3) out = torch.view_as_complex(out.contiguous()) return out if self.training: niters = self.end_iters
if niters < 0: z = z[:, None] return z / (1e-8 + z.abs()) * m else: return self._wiener(m, z, niters)
def _wiener(self, mag_out, mix_stft, niters): init = mix_stft.dtype wiener_win_len = 300 residual = self.wiener_residual
B, S, C, Fq, T = mag_out.shape mag_out = mag_out.permute(0, 4, 3, 2, 1) mix_stft = torch.view_as_real(mix_stft.permute(0, 3, 2, 1))
outs = []
for sample in range(B): pos = 0 out = []
for pos in range(0, T, wiener_win_len): frame = slice(pos, pos + wiener_win_len) z_out = wiener(mag_out[sample, frame], mix_stft[sample, frame], niters, residual=residual) out.append(z_out.transpose(-1, -2))
outs.append(torch.cat(out, dim=0))
out = torch.view_as_complex(torch.stack(outs, 0)) out = out.permute(0, 4, 3, 2, 1).contiguous()
if residual: out = out[:, :-1]
assert list(out.shape) == [B, S, C, Fq, T]
return out.to(init)
def valid_length(self, length: int): if not self.use_train_segment: return length training_length = int(self.segment * self.samplerate) if training_length < length: raise ValueError(translations["length_or_training_length"].format(length=length, training_length=training_length)) return training_length
def forward(self, mix): length = mix.shape[-1] length_pre_pad = None
if self.use_train_segment: if self.training: self.segment = Fraction(mix.shape[-1], self.samplerate) else: training_length = int(self.segment * self.samplerate)
if mix.shape[-1] < training_length: length_pre_pad = mix.shape[-1] mix = F.pad(mix, (0, training_length - length_pre_pad))
z = self._spec(mix) mag = self._magnitude(z).to(mix.device) x = mag
B, C, Fq, T = x.shape
mean = x.mean(dim=(1, 2, 3), keepdim=True) std = x.std(dim=(1, 2, 3), keepdim=True) x = (x - mean) / (1e-5 + std)
xt = mix meant = xt.mean(dim=(1, 2), keepdim=True) stdt = xt.std(dim=(1, 2), keepdim=True) xt = (xt - meant) / (1e-5 + stdt)
saved = [] saved_t = [] lengths = [] lengths_t = []
for idx, encode in enumerate(self.encoder): lengths.append(x.shape[-1]) inject = None
if idx < len(self.tencoder): lengths_t.append(xt.shape[-1]) tenc = self.tencoder[idx] xt = tenc(xt)
if not tenc.empty: saved_t.append(xt) else: inject = xt
x = encode(x, inject)
if idx == 0 and self.freq_emb is not None: frs = torch.arange(x.shape[-2], device=x.device) emb = self.freq_emb(frs).t()[None, :, :, None].expand_as(x) x = x + self.freq_emb_scale * emb
saved.append(x)
if self.crosstransformer: if self.bottom_channels: b, c, f, t = x.shape x = rearrange(x, "b c f t-> b c (f t)") x = self.channel_upsampler(x) x = rearrange(x, "b c (f t)-> b c f t", f=f)
xt = self.channel_upsampler_t(xt)
x, xt = self.crosstransformer(x, xt)
if self.bottom_channels: x = rearrange(x, "b c f t-> b c (f t)") x = self.channel_downsampler(x) x = rearrange(x, "b c (f t)-> b c f t", f=f)
xt = self.channel_downsampler_t(xt)
for idx, decode in enumerate(self.decoder): skip = saved.pop(-1) x, pre = decode(x, skip, lengths.pop(-1))
offset = self.depth - len(self.tdecoder)
if idx >= offset: tdec = self.tdecoder[idx - offset] length_t = lengths_t.pop(-1)
if tdec.empty: assert pre.shape[2] == 1, pre.shape pre = pre[:, :, 0] xt, _ = tdec(pre, None, length_t) else: skip = saved_t.pop(-1) xt, _ = tdec(xt, skip, length_t)
assert len(saved) == 0 assert len(lengths_t) == 0 assert len(saved_t) == 0
S = len(self.sources) x = x.view(B, S, -1, Fq, T) x = x * std[:, None] + mean[:, None]
device_type = x.device.type device_load = f"{device_type}:{x.device.index}" if not device_type == "mps" else device_type x_is_other_gpu = not device_type in ["cuda", "cpu"]
if x_is_other_gpu: x = x.cpu()
zout = self._mask(z, x)
if self.use_train_segment: x = self._ispec(zout, length) if self.training else self._ispec(zout, training_length) else: x = self._ispec(zout, length)
if x_is_other_gpu: x = x.to(device_load)
if self.use_train_segment: xt = xt.view(B, S, -1, length) if self.training else xt.view(B, S, -1, training_length) else: xt = xt.view(B, S, -1, length)
xt = xt * stdt[:, None] + meant[:, None] x = xt + x
if length_pre_pad: x = x[..., :length_pre_pad]
return x12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import osimport sysimport torchimport inspectimport warningsimport functools
from pathlib import Pathfrom diffq import restore_quantized_state
now_dir = os.getcwd()sys.path.append(now_dir)
from main.configs.config import Config
translations = Config().translations
def load_model(path_or_package, strict=False): if isinstance(path_or_package, dict): package = path_or_package elif isinstance(path_or_package, (str, Path)): with warnings.catch_warnings(): warnings.simplefilter("ignore")
path = path_or_package package = torch.load(path, map_location="cpu") else: raise ValueError(f"{translations['type_not_valid']} {path_or_package}.")
klass = package["klass"] args = package["args"] kwargs = package["kwargs"]
if strict: model = klass(*args, **kwargs) else: sig = inspect.signature(klass)
for key in list(kwargs): if key not in sig.parameters: warnings.warn(translations["del_parameter"] + key) del kwargs[key]
model = klass(*args, **kwargs)
state = package["state"]
set_state(model, state)
return model
def set_state(model, state, quantizer=None): if state.get("__quantized"): if quantizer is not None: quantizer.restore_quantized_state(model, state["quantized"]) else: restore_quantized_state(model, state) else: model.load_state_dict(state)
return state
def capture_init(init): @functools.wraps(init)
def __init__(self, *args, **kwargs): self._init_args_kwargs = (args, kwargs) init(self, *args, **kwargs)
return __init__12345678910import torchimport typing as tp
def center_trim(tensor: torch.Tensor, reference: tp.Union[torch.Tensor, int]): ref_size: int ref_size = reference.size(-1) if isinstance(reference, torch.Tensor) else reference delta = tensor.size(-1) - ref_size if delta < 0: raise ValueError(f"tensor > parameter: {delta}.") if delta: tensor = tensor[..., delta // 2 : -(delta - delta // 2)] return tensor12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100import io
import os
import six
import sys
import math
import librosa
import tempfile
import platform
import traceback
import audioread
import subprocess
import numpy as np
import soundfile as sf
from scipy.signal import correlate, hilbert
now_dir = os.getcwd()
sys.path.append(now_dir)
from main.configs.config import Config
translations = Config().translations
OPERATING_SYSTEM = platform.system()
SYSTEM_ARCH = platform.platform()
SYSTEM_PROC = platform.processor()
ARM = "arm"
AUTO_PHASE = "Automatic"
POSITIVE_PHASE = "Positive Phase"
NEGATIVE_PHASE = "Negative Phase"
NONE_P = ("None",)
LOW_P = ("Shifts: Low",)
MED_P = ("Shifts: Medium",)
HIGH_P = ("Shifts: High",)
VHIGH_P = "Shifts: Very High"
MAXIMUM_P = "Shifts: Maximum"
BASE_PATH_RUB = sys._MEIPASS if getattr(sys, 'frozen', False) else os.path.dirname(os.path.abspath(__file__))
DEVNULL = open(os.devnull, 'w') if six.PY2 else subprocess.DEVNULL
MAX_SPEC = "Max Spec"
MIN_SPEC = "Min Spec"
LIN_ENSE = "Linear Ensemble"
MAX_WAV = MAX_SPEC
MIN_WAV = MIN_SPEC
AVERAGE = "Average"
progress_value = 0
last_update_time = 0
is_macos = False
if OPERATING_SYSTEM == "Darwin":
wav_resolution = "polyphase" if SYSTEM_PROC == ARM or ARM in SYSTEM_ARCH else "sinc_fastest"
wav_resolution_float_resampling = "kaiser_best" if SYSTEM_PROC == ARM or ARM in SYSTEM_ARCH else wav_resolution
is_macos = True
else:
wav_resolution = "sinc_fastest"
wav_resolution_float_resampling = wav_resolution
def crop_center(h1, h2):
h1_shape = h1.size()
h2_shape = h2.size()
if h1_shape[3] == h2_shape[3]: return h1
elif h1_shape[3] < h2_shape[3]: raise ValueError("h1_shape[3] > h2_shape[3]")
s_time = (h1_shape[3] - h2_shape[3]) // 2
e_time = s_time + h2_shape[3]
h1 = h1[:, :, :, s_time:e_time]
return h1
def preprocess(X_spec):
return np.abs(X_spec), np.angle(X_spec)
def make_padding(width, cropsize, offset):
left = offset
roi_size = cropsize - offset * 2
if roi_size == 0: roi_size = cropsize
right = roi_size - (width % roi_size) + left
return left, right, roi_size
def normalize(wave, max_peak=1.0):
maxv = np.abs(wave).max()
if maxv > max_peak: wave *= max_peak / maxv
return wave
def auto_transpose(audio_array: np.ndarray):
if audio_array.shape[1] == 2: return audio_array.T
return audio_array
def write_array_to_mem(audio_data, subtype):
if isinstance(audio_data, np.ndarray):
audio_buffer = io.BytesIO()
sf.write(audio_buffer, audio_data, 44100, subtype=subtype, format="WAV")
audio_buffer.seek(0)
return audio_buffer
else: return audio_data
def spectrogram_to_image(spec, mode="magnitude"):
if mode == "magnitude":
y = np.abs(spec) if np.iscomplexobj(spec) else spec
y = np.log10(y**2 + 1e-8)
elif mode == "phase":
y = np.angle(spec) if np.iscomplexobj(spec) else spec
y -= y.min()
y *= 255 / y.max()
img = np.uint8(y)
if y.ndim == 3:
img = img.transpose(1, 2, 0)
img = np.concatenate([np.max(img, axis=2, keepdims=True), img], axis=2)
return img
def reduce_vocal_aggressively(X, y, softmask):
v = X - y
y_mag_tmp = np.abs(y)
v_mag_tmp = np.abs(v)
v_mask = v_mag_tmp > y_mag_tmp
y_mag = np.clip(y_mag_tmp - v_mag_tmp * v_mask * softmask, 0, np.inf)
return y_mag * np.exp(1.0j * np.angle(y))
def merge_artifacts(y_mask, thres=0.01, min_range=64, fade_size=32):
mask = y_mask
try:
if min_range < fade_size * 2: raise ValueError("min_range >= fade_size * 2")
idx = np.where(y_mask.min(axis=(0, 1)) > thres)[0]
start_idx = np.insert(idx[np.where(np.diff(idx) != 1)[0] + 1], 0, idx[0])
end_idx = np.append(idx[np.where(np.diff(idx) != 1)[0]], idx[-1])
artifact_idx = np.where(end_idx - start_idx > min_range)[0]
weight = np.zeros_like(y_mask)
if len(artifact_idx) > 0:
start_idx = start_idx[artifact_idx]
end_idx = end_idx[artifact_idx]
old_e = None
for s, e in zip(start_idx, end_idx):
if old_e is not None and s - old_e < fade_size: s = old_e - fade_size * 2
if s != 0: weight[:, :, s : s + fade_size] = np.linspace(0, 1, fade_size)
else: s -= fade_size
if e != y_mask.shape[2]: weight[:, :, e - fade_size : e] = np.linspace(1, 0, fade_size)
else: e += fade_size
weight[:, :, s + fade_size : e - fade_size] = 1
old_e = e
v_mask = 1 - y_mask
y_mask += weight * v_mask
mask = y_mask
except Exception as e:
error_name = f"{type(e).__name__}"
traceback_text = "".join(traceback.format_tb(e.__traceback__))
message = f'{error_name}: "{e}"\n{traceback_text}"'
print(translations["not_success"], message)
return mask
def align_wave_head_and_tail(a, b):
l = min([a[0].size, b[0].size])
return a[:l, :l], b[:l, :l]
def convert_channels(spec, mp, band):
cc = mp.param["band"][band].get("convert_channels")
if "mid_side_c" == cc:
spec_left = np.add(spec[0], spec[1] * 0.25)
spec_right = np.subtract(spec[1], spec[0] * 0.25)
elif "mid_side" == cc:
spec_left = np.add(spec[0], spec[1]) / 2
spec_right = np.subtract(spec[0], spec[1])
elif "stereo_n" == cc:
spec_left = np.add(spec[0], spec[1] * 0.25) / 0.9375
spec_right = np.add(spec[1], spec[0] * 0.25) / 0.9375
else: return spec
return np.asfortranarray([spec_left, spec_right])
def combine_spectrograms(specs, mp, is_v51_model=False):
l = min([specs[i].shape[2] for i in specs])
spec_c = np.zeros(shape=(2, mp.param["bins"] + 1, l), dtype=np.complex64)
offset = 0
bands_n = len(mp.param["band"])
for d in range(1, bands_n + 1):
h = mp.param["band"][d]["crop_stop"] - mp.param["band"][d]["crop_start"]
spec_c[:, offset : offset + h, :l] = specs[d][:, mp.param["band"][d]["crop_start"] : mp.param["band"][d]["crop_stop"], :l]
offset += h
if offset > mp.param["bins"]: raise ValueError("Quá nhiều thùng")
if mp.param["pre_filter_start"] > 0:
if is_v51_model: spec_c *= get_lp_filter_mask(spec_c.shape[1], mp.param["pre_filter_start"], mp.param["pre_filter_stop"])
else:
if bands_n == 1: spec_c = fft_lp_filter(spec_c, mp.param["pre_filter_start"], mp.param["pre_filter_stop"])
else:
gp = 1
for b in range(mp.param["pre_filter_start"] + 1, mp.param["pre_filter_stop"]):
g = math.pow(10, -(b - mp.param["pre_filter_start"]) * (3.5 - gp) / 20.0)
gp = g
spec_c[:, b, :] *= g
return np.asfortranarray(spec_c)
def wave_to_spectrogram(wave, hop_length, n_fft, mp, band, is_v51_model=False):
if wave.ndim == 1: wave = np.asfortranarray([wave, wave])
if not is_v51_model:
if mp.param["reverse"]:
wave_left = np.flip(np.asfortranarray(wave[0]))
wave_right = np.flip(np.asfortranarray(wave[1]))
elif mp.param["mid_side"]:
wave_left = np.asfortranarray(np.add(wave[0], wave[1]) / 2)
wave_right = np.asfortranarray(np.subtract(wave[0], wave[1]))
elif mp.param["mid_side_b2"]:
wave_left = np.asfortranarray(np.add(wave[1], wave[0] * 0.5))
wave_right = np.asfortranarray(np.subtract(wave[0], wave[1] * 0.5))
else:
wave_left = np.asfortranarray(wave[0])
wave_right = np.asfortranarray(wave[1])
else:
wave_left = np.asfortranarray(wave[0])
wave_right = np.asfortranarray(wave[1])
spec_left = librosa.stft(wave_left, n_fft=n_fft, hop_length=hop_length)
spec_right = librosa.stft(wave_right, n_fft=n_fft, hop_length=hop_length)
spec = np.asfortranarray([spec_left, spec_right])
if is_v51_model: spec = convert_channels(spec, mp, band)
return spec
def spectrogram_to_wave(spec, hop_length=1024, mp={}, band=0, is_v51_model=True):
spec_left = np.asfortranarray(spec[0])
spec_right = np.asfortranarray(spec[1])
wave_left = librosa.istft(spec_left, hop_length=hop_length)
wave_right = librosa.istft(spec_right, hop_length=hop_length)
if is_v51_model:
cc = mp.param["band"][band].get("convert_channels")
if "mid_side_c" == cc: return np.asfortranarray([np.subtract(wave_left / 1.0625, wave_right / 4.25), np.add(wave_right / 1.0625, wave_left / 4.25)])
elif "mid_side" == cc: return np.asfortranarray([np.add(wave_left, wave_right / 2), np.subtract(wave_left, wave_right / 2)])
elif "stereo_n" == cc: return np.asfortranarray([np.subtract(wave_left, wave_right * 0.25), np.subtract(wave_right, wave_left * 0.25)])
else:
if mp.param["reverse"]: return np.asfortranarray([np.flip(wave_left), np.flip(wave_right)])
elif mp.param["mid_side"]: return np.asfortranarray([np.add(wave_left, wave_right / 2), np.subtract(wave_left, wave_right / 2)])
elif mp.param["mid_side_b2"]: return np.asfortranarray([np.add(wave_right / 1.25, 0.4 * wave_left), np.subtract(wave_left / 1.25, 0.4 * wave_right)])
return np.asfortranarray([wave_left, wave_right])
def cmb_spectrogram_to_wave(spec_m, mp, extra_bins_h=None, extra_bins=None, is_v51_model=False):
bands_n = len(mp.param["band"])
offset = 0
for d in range(1, bands_n + 1):
bp = mp.param["band"][d]
spec_s = np.zeros(shape=(2, bp["n_fft"] // 2 + 1, spec_m.shape[2]), dtype=complex)
h = bp["crop_stop"] - bp["crop_start"]
spec_s[:, bp["crop_start"] : bp["crop_stop"], :] = spec_m[:, offset : offset + h, :]
offset += h
if d == bands_n:
if extra_bins_h:
max_bin = bp["n_fft"] // 2
spec_s[:, max_bin - extra_bins_h : max_bin, :] = extra_bins[:, :extra_bins_h, :]
if bp["hpf_start"] > 0:
if is_v51_model: spec_s *= get_hp_filter_mask(spec_s.shape[1], bp["hpf_start"], bp["hpf_stop"] - 1)
else: spec_s = fft_hp_filter(spec_s, bp["hpf_start"], bp["hpf_stop"] - 1)
if bands_n == 1: wave = spectrogram_to_wave(spec_s, bp["hl"], mp, d, is_v51_model)
else: wave = np.add(wave, spectrogram_to_wave(spec_s, bp["hl"], mp, d, is_v51_model))
else:
sr = mp.param["band"][d + 1]["sr"]
if d == 1:
if is_v51_model: spec_s *= get_lp_filter_mask(spec_s.shape[1], bp["lpf_start"], bp["lpf_stop"])
else: spec_s = fft_lp_filter(spec_s, bp["lpf_start"], bp["lpf_stop"])
try:
wave = librosa.resample(spectrogram_to_wave(spec_s, bp["hl"], mp, d, is_v51_model), orig_sr=bp["sr"], target_sr=sr, res_type=wav_resolution)
except ValueError as e:
print(f"{translations['resample_error']}: {e}")
print(f"{translations['shapes']} Spec_s: {spec_s.shape}, SR: {sr}, {translations['wav_resolution']}: {wav_resolution}")
else:
if is_v51_model:
spec_s *= get_hp_filter_mask(spec_s.shape[1], bp["hpf_start"], bp["hpf_stop"] - 1)
spec_s *= get_lp_filter_mask(spec_s.shape[1], bp["lpf_start"], bp["lpf_stop"])
else:
spec_s = fft_hp_filter(spec_s, bp["hpf_start"], bp["hpf_stop"] - 1)
spec_s = fft_lp_filter(spec_s, bp["lpf_start"], bp["lpf_stop"])
wave2 = np.add(wave, spectrogram_to_wave(spec_s, bp["hl"], mp, d, is_v51_model))
try:
wave = librosa.resample(wave2, orig_sr=bp["sr"], target_sr=sr, res_type=wav_resolution)
except ValueError as e:
print(f"{translations['resample_error']}: {e}")
print(f"{translations['shapes']} Spec_s: {spec_s.shape}, SR: {sr}, {translations['wav_resolution']}: {wav_resolution}")
return wave
def get_lp_filter_mask(n_bins, bin_start, bin_stop):
return np.concatenate([np.ones((bin_start - 1, 1)), np.linspace(1, 0, bin_stop - bin_start + 1)[:, None], np.zeros((n_bins - bin_stop, 1))], axis=0)
def get_hp_filter_mask(n_bins, bin_start, bin_stop):
return np.concatenate([np.zeros((bin_stop + 1, 1)), np.linspace(0, 1, 1 + bin_start - bin_stop)[:, None], np.ones((n_bins - bin_start - 2, 1))], axis=0)
def fft_lp_filter(spec, bin_start, bin_stop):
g = 1.0
for b in range(bin_start, bin_stop):
g -= 1 / (bin_stop - bin_start)
spec[:, b, :] = g * spec[:, b, :]
spec[:, bin_stop:, :] *= 0
return spec
def fft_hp_filter(spec, bin_start, bin_stop):
g = 1.0
for b in range(bin_start, bin_stop, -1):
g -= 1 / (bin_start - bin_stop)
spec[:, b, :] = g * spec[:, b, :]
spec[:, 0 : bin_stop + 1, :] *= 0
return spec
def spectrogram_to_wave_old(spec, hop_length=1024):
if spec.ndim == 2: wave = librosa.istft(spec, hop_length=hop_length)
elif spec.ndim == 3:
spec_left = np.asfortranarray(spec[0])
spec_right = np.asfortranarray(spec[1])
wave_left = librosa.istft(spec_left, hop_length=hop_length)
wave_right = librosa.istft(spec_right, hop_length=hop_length)
wave = np.asfortranarray([wave_left, wave_right])
return wave
def wave_to_spectrogram_old(wave, hop_length, n_fft):
wave_left = np.asfortranarray(wave[0])
wave_right = np.asfortranarray(wave[1])
spec_left = librosa.stft(wave_left, n_fft=n_fft, hop_length=hop_length)
spec_right = librosa.stft(wave_right, n_fft=n_fft, hop_length=hop_length)
return np.asfortranarray([spec_left, spec_right])
def mirroring(a, spec_m, input_high_end, mp):
if "mirroring" == a:
mirror = np.flip(np.abs(spec_m[:, mp.param["pre_filter_start"] - 10 - input_high_end.shape[1] : mp.param["pre_filter_start"] - 10, :]), 1)
mirror = mirror * np.exp(1.0j * np.angle(input_high_end))
return np.where(np.abs(input_high_end) <= np.abs(mirror), input_high_end, mirror)
if "mirroring2" == a:
mirror = np.flip(np.abs(spec_m[:, mp.param["pre_filter_start"] - 10 - input_high_end.shape[1] : mp.param["pre_filter_start"] - 10, :]), 1)
mi = np.multiply(mirror, input_high_end * 1.7)
return np.where(np.abs(input_high_end) <= np.abs(mi), input_high_end, mi)
def adjust_aggr(mask, is_non_accom_stem, aggressiveness):
aggr = aggressiveness["value"] * 2
if aggr != 0:
if is_non_accom_stem:
aggr = 1 - aggr
if np.any(aggr > 10) or np.any(aggr < -10): print(f"{translations['warnings']}: {aggr}")
aggr = [aggr, aggr]
if aggressiveness["aggr_correction"] is not None:
aggr[0] += aggressiveness["aggr_correction"]["left"]
aggr[1] += aggressiveness["aggr_correction"]["right"]
for ch in range(2):
mask[ch, : aggressiveness["split_bin"]] = np.power(mask[ch, : aggressiveness["split_bin"]], 1 + aggr[ch] / 3)
mask[ch, aggressiveness["split_bin"] :] = np.power(mask[ch, aggressiveness["split_bin"] :], 1 + aggr[ch])
return mask
def stft(wave, nfft, hl):
wave_left = np.asfortranarray(wave[0])
wave_right = np.asfortranarray(wave[1])
spec_left = librosa.stft(wave_left, n_fft=nfft, hop_length=hl)
spec_right = librosa.stft(wave_right, n_fft=nfft, hop_length=hl)
spec = np.asfortranarray([spec_left, spec_right])
return spec
def istft(spec, hl):
spec_left = np.asfortranarray(spec[0])
spec_right = np.asfortranarray(spec[1])
wave_left = librosa.istft(spec_left, hop_length=hl)
wave_right = librosa.istft(spec_right, hop_length=hl)
wave = np.asfortranarray([wave_left, wave_right])
return wave
def spec_effects(wave, algorithm="Default", value=None):
if np.isnan(wave).any() or np.isinf(wave).any(): print(f"{translations['warnings_2']}: {wave.shape}")
spec = [stft(wave[0], 2048, 1024), stft(wave[1], 2048, 1024)]
if algorithm == "Min_Mag":
v_spec_m = np.where(np.abs(spec[1]) <= np.abs(spec[0]), spec[1], spec[0])
wave = istft(v_spec_m, 1024)
elif algorithm == "Max_Mag":
v_spec_m = np.where(np.abs(spec[1]) >= np.abs(spec[0]), spec[1], spec[0])
wave = istft(v_spec_m, 1024)
elif algorithm == "Default": wave = (wave[1] * value) + (wave[0] * (1 - value))
elif algorithm == "Invert_p":
X_mag = np.abs(spec[0])
y_mag = np.abs(spec[1])
max_mag = np.where(X_mag >= y_mag, X_mag, y_mag)
v_spec = spec[1] - max_mag * np.exp(1.0j * np.angle(spec[0]))
wave = istft(v_spec, 1024)
return wave
def spectrogram_to_wave_no_mp(spec, n_fft=2048, hop_length=1024):
wave = librosa.istft(spec, n_fft=n_fft, hop_length=hop_length)
if wave.ndim == 1: wave = np.asfortranarray([wave, wave])
return wave
def wave_to_spectrogram_no_mp(wave):
spec = librosa.stft(wave, n_fft=2048, hop_length=1024)
if spec.ndim == 1: spec = np.asfortranarray([spec, spec])
return spec
def invert_audio(specs, invert_p=True):
ln = min([specs[0].shape[2], specs[1].shape[2]])
specs[0] = specs[0][:, :, :ln]
specs[1] = specs[1][:, :, :ln]
if invert_p:
X_mag = np.abs(specs[0])
y_mag = np.abs(specs[1])
max_mag = np.where(X_mag >= y_mag, X_mag, y_mag)
v_spec = specs[1] - max_mag * np.exp(1.0j * np.angle(specs[0]))
else:
specs[1] = reduce_vocal_aggressively(specs[0], specs[1], 0.2)
v_spec = specs[0] - specs[1]
return v_spec
def invert_stem(mixture, stem):
mixture = wave_to_spectrogram_no_mp(mixture)
stem = wave_to_spectrogram_no_mp(stem)
output = spectrogram_to_wave_no_mp(invert_audio([mixture, stem]))
return -output.T
def ensembling(a, inputs, is_wavs=False):
for i in range(1, len(inputs)):
if i == 1: input = inputs[0]
if is_wavs:
ln = min([input.shape[1], inputs[i].shape[1]])
input = input[:, :ln]
inputs[i] = inputs[i][:, :ln]
else:
ln = min([input.shape[2], inputs[i].shape[2]])
input = input[:, :, :ln]
inputs[i] = inputs[i][:, :, :ln]
if MIN_SPEC == a: input = np.where(np.abs(inputs[i]) <= np.abs(input), inputs[i], input)
if MAX_SPEC == a: input = np.where(np.abs(inputs[i]) >= np.abs(input), inputs[i], input)
return input
def ensemble_for_align(waves):
specs = []
for wav in waves:
spec = wave_to_spectrogram_no_mp(wav.T)
specs.append(spec)
wav_aligned = spectrogram_to_wave_no_mp(ensembling(MIN_SPEC, specs)).T
wav_aligned = match_array_shapes(wav_aligned, waves[1], is_swap=True)
return wav_aligned
def ensemble_inputs(audio_input, algorithm, is_normalization, wav_type_set, save_path, is_wave=False, is_array=False):
wavs_ = []
if algorithm == AVERAGE:
output = average_audio(audio_input)
samplerate = 44100
else:
specs = []
for i in range(len(audio_input)):
wave, samplerate = librosa.load(audio_input[i], mono=False, sr=44100)
wavs_.append(wave)
spec = wave if is_wave else wave_to_spectrogram_no_mp(wave)
specs.append(spec)
wave_shapes = [w.shape[1] for w in wavs_]
target_shape = wavs_[wave_shapes.index(max(wave_shapes))]
output = ensembling(algorithm, specs, is_wavs=True) if is_wave else spectrogram_to_wave_no_mp(ensembling(algorithm, specs))
output = to_shape(output, target_shape.shape)
sf.write(save_path, normalize(output.T, is_normalization), samplerate, subtype=wav_type_set)
def to_shape(x, target_shape):
padding_list = []
for x_dim, target_dim in zip(x.shape, target_shape):
pad_value = target_dim - x_dim
pad_tuple = (0, pad_value)
padding_list.append(pad_tuple)
return np.pad(x, tuple(padding_list), mode="constant")
def to_shape_minimize(x: np.ndarray, target_shape):
padding_list = []
for x_dim, target_dim in zip(x.shape, target_shape):
pad_value = target_dim - x_dim
pad_tuple = (0, pad_value)
padding_list.append(pad_tuple)
return np.pad(x, tuple(padding_list), mode="constant")
def detect_leading_silence(audio, sr, silence_threshold=0.007, frame_length=1024):
if len(audio.shape) == 2:
channel = np.argmax(np.sum(np.abs(audio), axis=1))
audio = audio[channel]
for i in range(0, len(audio), frame_length):
if np.max(np.abs(audio[i : i + frame_length])) > silence_threshold: return (i / sr) * 1000
return (len(audio) / sr) * 1000
def adjust_leading_silence(target_audio, reference_audio, silence_threshold=0.01, frame_length=1024):
def find_silence_end(audio):
if len(audio.shape) == 2:
channel = np.argmax(np.sum(np.abs(audio), axis=1))
audio_mono = audio[channel]
else: audio_mono = audio
for i in range(0, len(audio_mono), frame_length):
if np.max(np.abs(audio_mono[i : i + frame_length])) > silence_threshold: return i
return len(audio_mono)
ref_silence_end = find_silence_end(reference_audio)
target_silence_end = find_silence_end(target_audio)
silence_difference = ref_silence_end - target_silence_end
try:
ref_silence_end_p = (ref_silence_end / 44100) * 1000
target_silence_end_p = (target_silence_end / 44100) * 1000
silence_difference_p = ref_silence_end_p - target_silence_end_p
print("im lặng khác biệt: ", silence_difference_p)
except Exception as e:
pass
if silence_difference > 0:
silence_to_add = np.zeros((target_audio.shape[0], silence_difference))if len(target_audio.shape) == 2 else np.zeros(silence_difference)
return np.hstack((silence_to_add, target_audio))
elif silence_difference < 0:
if len(target_audio.shape) == 2: return target_audio[:, -silence_difference:]
else: return target_audio[-silence_difference:]
else: return target_audio
def match_array_shapes(array_1: np.ndarray, array_2: np.ndarray, is_swap=False):
if is_swap: array_1, array_2 = array_1.T, array_2.T
if array_1.shape[1] > array_2.shape[1]: array_1 = array_1[:, : array_2.shape[1]]
elif array_1.shape[1] < array_2.shape[1]:
padding = array_2.shape[1] - array_1.shape[1]
array_1 = np.pad(array_1, ((0, 0), (0, padding)), "constant", constant_values=0)
if is_swap: array_1, array_2 = array_1.T, array_2.T
return array_1
def match_mono_array_shapes(array_1: np.ndarray, array_2: np.ndarray):
if len(array_1) > len(array_2): array_1 = array_1[: len(array_2)]
elif len(array_1) < len(array_2):
padding = len(array_2) - len(array_1)
array_1 = np.pad(array_1, (0, padding), "constant", constant_values=0)
return array_1
def change_pitch_semitones(y, sr, semitone_shift):
factor = 2 ** (semitone_shift / 12)
y_pitch_tuned = []
for y_channel in y:
y_pitch_tuned.append(librosa.resample(y_channel, orig_sr=sr, target_sr=sr * factor, res_type=wav_resolution_float_resampling))
y_pitch_tuned = np.array(y_pitch_tuned)
new_sr = sr * factor
return y_pitch_tuned, new_sr
def augment_audio(export_path, audio_file, rate, is_normalization, wav_type_set, save_format=None, is_pitch=False, is_time_correction=True):
wav, sr = librosa.load(audio_file, sr=44100, mono=False)
if wav.ndim == 1: wav = np.asfortranarray([wav, wav])
if not is_time_correction: wav_mix = change_pitch_semitones(wav, 44100, semitone_shift=-rate)[0]
else:
if is_pitch:
wav_1 = pitch_shift(wav[0], sr, rate, rbargs=None)
wav_2 = pitch_shift(wav[1], sr, rate, rbargs=None)
else:
wav_1 = time_stretch(wav[0], sr, rate, rbargs=None)
wav_2 = time_stretch(wav[1], sr, rate, rbargs=None)
if wav_1.shape > wav_2.shape: wav_2 = to_shape(wav_2, wav_1.shape)
if wav_1.shape < wav_2.shape: wav_1 = to_shape(wav_1, wav_2.shape)
wav_mix = np.asfortranarray([wav_1, wav_2])
sf.write(export_path, normalize(wav_mix.T, is_normalization), sr, subtype=wav_type_set)
save_format(export_path)
def average_audio(audio):
waves = []
wave_shapes = []
final_waves = []
for i in range(len(audio)):
wave = librosa.load(audio[i], sr=44100, mono=False)
waves.append(wave[0])
wave_shapes.append(wave[0].shape[1])
wave_shapes_index = wave_shapes.index(max(wave_shapes))
target_shape = waves[wave_shapes_index]
waves.pop(wave_shapes_index)
final_waves.append(target_shape)
for n_array in waves:
wav_target = to_shape(n_array, target_shape.shape)
final_waves.append(wav_target)
waves = sum(final_waves)
waves = waves / len(audio)
return waves
def average_dual_sources(wav_1, wav_2, value):
if wav_1.shape > wav_2.shape: wav_2 = to_shape(wav_2, wav_1.shape)
if wav_1.shape < wav_2.shape: wav_1 = to_shape(wav_1, wav_2.shape)
wave = (wav_1 * value) + (wav_2 * (1 - value))
return wave
def reshape_sources(wav_1: np.ndarray, wav_2: np.ndarray):
if wav_1.shape > wav_2.shape: wav_2 = to_shape(wav_2, wav_1.shape)
if wav_1.shape < wav_2.shape:
ln = min([wav_1.shape[1], wav_2.shape[1]])
wav_2 = wav_2[:, :ln]
ln = min([wav_1.shape[1], wav_2.shape[1]])
wav_1 = wav_1[:, :ln]
wav_2 = wav_2[:, :ln]
return wav_2
def reshape_sources_ref(wav_1_shape, wav_2: np.ndarray):
if wav_1_shape > wav_2.shape: wav_2 = to_shape(wav_2, wav_1_shape)
return wav_2
def combine_arrarys(audio_sources, is_swap=False):
source = np.zeros_like(max(audio_sources, key=np.size))
for v in audio_sources:
v = match_array_shapes(v, source, is_swap=is_swap)
source += v
return source
def combine_audio(paths: list, audio_file_base=None, wav_type_set="FLOAT", save_format=None):
source = combine_arrarys([load_audio(i) for i in paths])
save_path = f"{audio_file_base}_combined.wav"
sf.write(save_path, source.T, 44100, subtype=wav_type_set)
save_format(save_path)
def reduce_mix_bv(inst_source, voc_source, reduction_rate=0.9):
inst_source = inst_source * (1 - reduction_rate)
mix_reduced = combine_arrarys([inst_source, voc_source], is_swap=True)
return mix_reduced
def organize_inputs(inputs):
input_list = {"target": None, "reference": None, "reverb": None, "inst": None}
for i in inputs:
if i.endswith("_(Vocals).wav"): input_list["reference"] = i
elif "_RVC_" in i: input_list["target"] = i
elif i.endswith("reverbed_stem.wav"): input_list["reverb"] = i
elif i.endswith("_(Instrumental).wav"): input_list["inst"] = i
return input_list
def check_if_phase_inverted(wav1, wav2, is_mono=False):
if not is_mono:
wav1 = np.mean(wav1, axis=0)
wav2 = np.mean(wav2, axis=0)
correlation = np.corrcoef(wav1[:1000], wav2[:1000])
return correlation[0, 1] < 0
def align_audio(file1, file2, file2_aligned, file_subtracted, wav_type_set, is_save_aligned, command_Text, save_format, align_window: list, align_intro_val: list, db_analysis: tuple, set_progress_bar, phase_option, phase_shifts, is_match_silence, is_spec_match):
global progress_value
progress_value = 0
is_mono = False
def get_diff(a, b):
corr = np.correlate(a, b, "full")
diff = corr.argmax() - (b.shape[0] - 1)
return diff
def progress_bar(length):
global progress_value
progress_value += 1
if (0.90 / length * progress_value) >= 0.9: length = progress_value + 1
set_progress_bar(0.1, (0.9 / length * progress_value))
if file1.endswith(".mp3") and is_macos:
length1 = rerun_mp3(file1)
wav1, sr1 = librosa.load(file1, duration=length1, sr=44100, mono=False)
else: wav1, sr1 = librosa.load(file1, sr=44100, mono=False)
if file2.endswith(".mp3") and is_macos:
length2 = rerun_mp3(file2)
wav2, sr2 = librosa.load(file2, duration=length2, sr=44100, mono=False)
else: wav2, sr2 = librosa.load(file2, sr=44100, mono=False)
if wav1.ndim == 1 and wav2.ndim == 1: is_mono = True
elif wav1.ndim == 1: wav1 = np.asfortranarray([wav1, wav1])
elif wav2.ndim == 1: wav2 = np.asfortranarray([wav2, wav2])
if phase_option == AUTO_PHASE:
if check_if_phase_inverted(wav1, wav2, is_mono=is_mono): wav2 = -wav2
elif phase_option == POSITIVE_PHASE: wav2 = +wav2
elif phase_option == NEGATIVE_PHASE: wav2 = -wav2
if is_match_silence: wav2 = adjust_leading_silence(wav2, wav1)
wav1_length = int(librosa.get_duration(y=wav1, sr=44100))
wav2_length = int(librosa.get_duration(y=wav2, sr=44100))
if not is_mono:
wav1 = wav1.transpose()
wav2 = wav2.transpose()
wav2_org = wav2.copy()
command_Text(translations["process_file"])
seconds_length = min(wav1_length, wav2_length)
wav2_aligned_sources = []
for sec_len in align_intro_val:
sec_seg = 1 if sec_len == 1 else int(seconds_length // sec_len)
index = sr1 * sec_seg
if is_mono:
samp1, samp2 = wav1[index : index + sr1], wav2[index : index + sr1]
diff = get_diff(samp1, samp2)
else:
index = sr1 * sec_seg
samp1, samp2 = wav1[index : index + sr1, 0], wav2[index : index + sr1, 0]
samp1_r, samp2_r = wav1[index : index + sr1, 1], wav2[index : index + sr1, 1]
diff, diff_r = get_diff(samp1, samp2), get_diff(samp1_r, samp2_r)
if diff > 0:
zeros_to_append = np.zeros(diff) if is_mono else np.zeros((diff, 2))
wav2_aligned = np.append(zeros_to_append, wav2_org, axis=0)
elif diff < 0: wav2_aligned = wav2_org[-diff:]
else: wav2_aligned = wav2_org
if not any(np.array_equal(wav2_aligned, source) for source in wav2_aligned_sources): wav2_aligned_sources.append(wav2_aligned)
unique_sources = len(wav2_aligned_sources)
sub_mapper_big_mapper = {}
for s in wav2_aligned_sources:
wav2_aligned = match_mono_array_shapes(s, wav1) if is_mono else match_array_shapes(s, wav1, is_swap=True)
if align_window:
wav_sub = time_correction(wav1, wav2_aligned, seconds_length, align_window=align_window, db_analysis=db_analysis, progress_bar=progress_bar, unique_sources=unique_sources, phase_shifts=phase_shifts)
wav_sub_size = np.abs(wav_sub).mean()
sub_mapper_big_mapper = {**sub_mapper_big_mapper, **{wav_sub_size: wav_sub}}
else:
wav2_aligned = wav2_aligned * np.power(10, db_analysis[0] / 20)
db_range = db_analysis[1]
for db_adjustment in db_range:
s_adjusted = wav2_aligned * (10 ** (db_adjustment / 20))
wav_sub = wav1 - s_adjusted
wav_sub_size = np.abs(wav_sub).mean()
sub_mapper_big_mapper = {**sub_mapper_big_mapper, **{wav_sub_size: wav_sub}}
sub_mapper_value_list = list(sub_mapper_big_mapper.values())
wav_sub = ensemble_for_align(list(sub_mapper_big_mapper.values())) if is_spec_match and len(sub_mapper_value_list) >= 2 else ensemble_wav(list(sub_mapper_big_mapper.values()))
wav_sub = np.clip(wav_sub, -1, +1)
command_Text(translations["save_instruments"])
if is_save_aligned or is_spec_match:
wav1 = match_mono_array_shapes(wav1, wav_sub) if is_mono else match_array_shapes(wav1, wav_sub, is_swap=True)
wav2_aligned = wav1 - wav_sub
if is_spec_match:
if wav1.ndim == 1 and wav2.ndim == 1:
wav2_aligned = np.asfortranarray([wav2_aligned, wav2_aligned]).T
wav1 = np.asfortranarray([wav1, wav1]).T
wav2_aligned = ensemble_for_align([wav2_aligned, wav1])
wav_sub = wav1 - wav2_aligned
if is_save_aligned:
sf.write(file2_aligned, wav2_aligned, sr1, subtype=wav_type_set)
save_format(file2_aligned)
sf.write(file_subtracted, wav_sub, sr1, subtype=wav_type_set)
save_format(file_subtracted)
def phase_shift_hilbert(signal, degree):
analytic_signal = hilbert(signal)
return np.cos(np.radians(degree)) * analytic_signal.real - np.sin(np.radians(degree)) * analytic_signal.imag
def get_phase_shifted_tracks(track, phase_shift):
if phase_shift == 180: return [track, -track]
step = phase_shift
end = 180 - (180 % step) if 180 % step == 0 else 181
phase_range = range(step, end, step)
flipped_list = [track, -track]
for i in phase_range:
flipped_list.extend([phase_shift_hilbert(track, i), phase_shift_hilbert(track, -i)])
return flipped_list
def time_correction(mix: np.ndarray, instrumental: np.ndarray, seconds_length, align_window, db_analysis, sr=44100, progress_bar=None, unique_sources=None, phase_shifts=NONE_P):
def align_tracks(track1, track2):
shifted_tracks = {}
track2 = track2 * np.power(10, db_analysis[0] / 20)
db_range = db_analysis[1]
track2_flipped = [track2] if phase_shifts == 190 else get_phase_shifted_tracks(track2, phase_shifts)
for db_adjustment in db_range:
for t in track2_flipped:
track2_adjusted = t * (10 ** (db_adjustment / 20))
corr = correlate(track1, track2_adjusted)
delay = np.argmax(np.abs(corr)) - (len(track1) - 1)
track2_shifted = np.roll(track2_adjusted, shift=delay)
track2_shifted_sub = track1 - track2_shifted
mean_abs_value = np.abs(track2_shifted_sub).mean()
shifted_tracks[mean_abs_value] = track2_shifted
return shifted_tracks[min(shifted_tracks.keys())]
assert mix.shape == instrumental.shape, translations["assert"].format(mixshape=mix.shape, instrumentalshape=instrumental.shape)
seconds_length = seconds_length // 2
sub_mapper = {}
progress_update_interval = 120
total_iterations = 0
if len(align_window) > 2: progress_update_interval = 320
for secs in align_window:
step = secs / 2
window_size = int(sr * secs)
step_size = int(sr * step)
if len(mix.shape) == 1:
total_mono = (len(range(0, len(mix) - window_size, step_size)) // progress_update_interval) * unique_sources
total_iterations += total_mono
else:
total_stereo_ = len(range(0, len(mix[:, 0]) - window_size, step_size)) * 2
total_stereo = (total_stereo_ // progress_update_interval) * unique_sources
total_iterations += total_stereo
for secs in align_window:
sub = np.zeros_like(mix)
divider = np.zeros_like(mix)
step = secs / 2
window_size = int(sr * secs)
step_size = int(sr * step)
window = np.hanning(window_size)
if len(mix.shape) == 1:
counter = 0
for i in range(0, len(mix) - window_size, step_size):
counter += 1
if counter % progress_update_interval == 0: progress_bar(total_iterations)
window_mix = mix[i : i + window_size] * window
window_instrumental = instrumental[i : i + window_size] * window
window_instrumental_aligned = align_tracks(window_mix, window_instrumental)
sub[i : i + window_size] += window_mix - window_instrumental_aligned
divider[i : i + window_size] += window
else:
counter = 0
for ch in range(mix.shape[1]):
for i in range(0, len(mix[:, ch]) - window_size, step_size):
counter += 1
if counter % progress_update_interval == 0: progress_bar(total_iterations)
window_mix = mix[i : i + window_size, ch] * window
window_instrumental = instrumental[i : i + window_size, ch] * window
window_instrumental_aligned = align_tracks(window_mix, window_instrumental)
sub[i : i + window_size, ch] += window_mix - window_instrumental_aligned
divider[i : i + window_size, ch] += window
sub = np.where(divider > 1e-6, sub / divider, sub)
sub_size = np.abs(sub).mean()
sub_mapper = {**sub_mapper, **{sub_size: sub}}
sub = ensemble_wav(list(sub_mapper.values()), split_size=12)
return sub
def ensemble_wav(waveforms, split_size=240):
waveform_thirds = {i: np.array_split(waveform, split_size) for i, waveform in enumerate(waveforms)}
final_waveform = []
for third_idx in range(split_size):
means = [np.abs(waveform_thirds[i][third_idx]).mean() for i in range(len(waveforms))]
min_index = np.argmin(means)
final_waveform.append(waveform_thirds[min_index][third_idx])
final_waveform = np.concatenate(final_waveform)
return final_waveform
def ensemble_wav_min(waveforms):
for i in range(1, len(waveforms)):
if i == 1: wave = waveforms[0]
ln = min(len(wave), len(waveforms[i]))
wave = wave[:ln]
waveforms[i] = waveforms[i][:ln]
wave = np.where(np.abs(waveforms[i]) <= np.abs(wave), waveforms[i], wave)
return wave
def align_audio_test(wav1, wav2, sr1=44100):
def get_diff(a, b):
corr = np.correlate(a, b, "full")
diff = corr.argmax() - (b.shape[0] - 1)
return diff
wav1 = wav1.transpose()
wav2 = wav2.transpose()
wav2_org = wav2.copy()
index = sr1
samp1 = wav1[index : index + sr1, 0]
samp2 = wav2[index : index + sr1, 0]
diff = get_diff(samp1, samp2)
if diff > 0: wav2_aligned = np.append(np.zeros((diff, 1)), wav2_org, axis=0)
elif diff < 0: wav2_aligned = wav2_org[-diff:]
else: wav2_aligned = wav2_org
return wav2_aligned
def load_audio(audio_file):
wav, sr = librosa.load(audio_file, sr=44100, mono=False)
if wav.ndim == 1: wav = np.asfortranarray([wav, wav])
return wav
def rerun_mp3(audio_file):
with audioread.audio_open(audio_file) as f:
track_length = int(f.duration)
return track_length
def __rubberband(y, sr, **kwargs):
assert sr > 0
fd, infile = tempfile.mkstemp(suffix='.wav')
os.close(fd)
fd, outfile = tempfile.mkstemp(suffix='.wav')
os.close(fd)
sf.write(infile, y, sr)
try:
arguments = [os.path.join(BASE_PATH_RUB, 'rubberband'), '-q']
for key, value in six.iteritems(kwargs):
arguments.append(str(key))
arguments.append(str(value))
arguments.extend([infile, outfile])
subprocess.check_call(arguments, stdout=DEVNULL, stderr=DEVNULL)
y_out, _ = sf.read(outfile, always_2d=True)
if y.ndim == 1: y_out = np.squeeze(y_out)
except OSError as exc:
six.raise_from(RuntimeError(translations["rubberband"]), exc)
finally:
os.unlink(infile)
os.unlink(outfile)
return y_out
def time_stretch(y, sr, rate, rbargs=None):
if rate <= 0: raise ValueError(translations["rate"])
if rate == 1.0: return y
if rbargs is None: rbargs = dict()
rbargs.setdefault('--tempo', rate)
return __rubberband(y, sr, **rbargs)
def pitch_shift(y, sr, n_steps, rbargs=None):
if n_steps == 0: return y
if rbargs is None: rbargs = dict()
rbargs.setdefault('--pitch', n_steps)
return __rubberband(y, sr, **rbargs)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230import os
import re
import sys
import six
import json
import tqdm
import shutil
import tempfile
import requests
import warnings
import textwrap
from time import sleep, time
from urllib.parse import urlparse, parse_qs, unquote
now_dir = os.getcwd()
sys.path.append(now_dir)
from main.configs.config import Config
translations = Config().translations
CHUNK_SIZE = 512 * 1024
HOME = os.path.expanduser("~")
def indent(text, prefix):
return "".join((prefix + line if line.strip() else line) for line in text.splitlines(True))
def parse_url(url, warning=True):
parsed = urlparse(url)
is_download_link = parsed.path.endswith("/uc")
if not parsed.hostname in ("drive.google.com", "docs.google.com"): return None, is_download_link
file_id = parse_qs(parsed.query).get("id", [None])[0]
if file_id is None:
for pattern in (r"^/file/d/(.*?)/(edit|view)$", r"^/file/u/[0-9]+/d/(.*?)/(edit|view)$", r"^/document/d/(.*?)/(edit|htmlview|view)$", r"^/document/u/[0-9]+/d/(.*?)/(edit|htmlview|view)$", r"^/presentation/d/(.*?)/(edit|htmlview|view)$", r"^/presentation/u/[0-9]+/d/(.*?)/(edit|htmlview|view)$", r"^/spreadsheets/d/(.*?)/(edit|htmlview|view)$", r"^/spreadsheets/u/[0-9]+/d/(.*?)/(edit|htmlview|view)$"):
match = re.match(pattern, parsed.path)
if match:
file_id = match.group(1)
break
if warning and not is_download_link:
warnings.warn(translations["gdown_warning"].format(file_id=file_id))
return file_id, is_download_link
def get_url_from_gdrive_confirmation(contents):
for pattern in (r'href="(\/uc\?export=download[^"]+)', r'href="/open\?id=([^"]+)"', r'"downloadUrl":"([^"]+)'):
match = re.search(pattern, contents)
if match:
url = match.group(1)
if pattern == r'href="/open\?id=([^"]+)"': url = ("https://drive.usercontent.google.com/download?id=" + url + "&confirm=t&uuid=" + re.search(r'<input\s+type="hidden"\s+name="uuid"\s+value="([^"]+)"', contents).group(1))
elif pattern == r'"downloadUrl":"([^"]+)': url = url.replace("\\u003d", "=").replace("\\u0026", "&")
else: url = "https://docs.google.com" + url.replace("&", "&")
return url
match = re.search(r'<p class="uc-error-subcaption">(.*)</p>', contents)
if match:
error = match.group(1)
raise Exception(error)
raise Exception(translations["gdown_error"])
def _get_session(proxy, use_cookies, return_cookies_file=False):
sess = requests.session()
sess.headers.update({"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)"})
if proxy is not None:
sess.proxies = {"http": proxy, "https": proxy}
print("Using proxy:", proxy, file=sys.stderr)
cookies_file = os.path.join(HOME, ".cache/gdown/cookies.json")
if os.path.exists(cookies_file) and use_cookies:
with open(cookies_file) as f:
cookies = json.load(f)
for k, v in cookies:
sess.cookies[k] = v
return (sess, cookies_file) if return_cookies_file else sess
def gdown_download(url=None, output=None, output_dir=None, quiet=False, proxy=None, speed=None, use_cookies=True, verify=True, id=None, fuzzy=True, resume=False, format=None):
if not (id is None) ^ (url is None): raise ValueError(translations["gdown_value_error"])
if id is not None: url = f"https://drive.google.com/uc?id={id}"
url_origin = url
sess, cookies_file = _get_session(proxy=proxy, use_cookies=use_cookies, return_cookies_file=True)
gdrive_file_id, is_gdrive_download_link = parse_url(url, warning=not fuzzy)
if fuzzy and gdrive_file_id:
url = f"https://drive.google.com/uc?id={gdrive_file_id}"
url_origin = url
is_gdrive_download_link = True
while 1:
res = sess.get(url, stream=True, verify=verify)
if url == url_origin and res.status_code == 500:
url = f"https://drive.google.com/open?id={gdrive_file_id}"
continue
if res.headers["Content-Type"].startswith("text/html"):
title = re.search("<title>(.+)</title>", res.text)
if title:
title = title.group(1)
if title.endswith(" - Google Docs"):
url = f"https://docs.google.com/document/d/{gdrive_file_id}/export?format={'docx' if format is None else format}"
continue
if title.endswith(" - Google Sheets"):
url = f"https://docs.google.com/spreadsheets/d/{gdrive_file_id}/export?format={'xlsx' if format is None else format}"
continue
if title.endswith(" - Google Slides"):
url = f"https://docs.google.com/presentation/d/{gdrive_file_id}/export?format={'pptx' if format is None else format}"
continue
elif ("Content-Disposition" in res.headers and res.headers["Content-Disposition"].endswith("pptx") and format not in (None, "pptx")):
url = f"https://docs.google.com/presentation/d/{gdrive_file_id}/export?format={'pptx' if format is None else format}"
continue
if use_cookies:
os.makedirs(os.path.dirname(cookies_file), exist_ok=True)
with open(cookies_file, "w") as f:
cookies = [(k, v) for k, v in sess.cookies.items() if not k.startswith("download_warning_")]
json.dump(cookies, f, indent=2)
if "Content-Disposition" in res.headers: break
if not (gdrive_file_id and is_gdrive_download_link): break
try:
url = get_url_from_gdrive_confirmation(res.text)
except Exception as e:
error = indent("\n".join(textwrap.wrap(str(e))), prefix="\t")
raise Exception(translations["gdown_error_2"].format(error=error, url_origin=url_origin))
if gdrive_file_id and is_gdrive_download_link:
content_disposition = unquote(res.headers["Content-Disposition"])
filename_from_url = (re.search(r"filename\*=UTF-8''(.*)", content_disposition) or re.search(r'filename=["\']?(.*?)["\']?$', content_disposition)).group(1)
filename_from_url = filename_from_url.replace(os.path.sep, "_")
else: filename_from_url = os.path.basename(url)
output = output or filename_from_url
output_is_path = isinstance(output, six.string_types)
if output_is_path and output.endswith(os.path.sep):
os.makedirs(output, exist_ok=True)
output = os.path.join(output, filename_from_url)
if output_is_path:
temp_dir = os.path.dirname(output) or "."
prefix = os.path.basename(output)
existing_tmp_files = [os.path.join(temp_dir, file) for file in os.listdir(temp_dir) if file.startswith(prefix)]
if resume and existing_tmp_files:
if len(existing_tmp_files) > 1:
print(translations["temps"], file=sys.stderr)
for file in existing_tmp_files:
print(f"\t{file}", file=sys.stderr)
print(translations["del_all_temps"], file=sys.stderr)
return
tmp_file = existing_tmp_files[0]
else:
resume = False
tmp_file = tempfile.mktemp(suffix=tempfile.template, prefix=prefix, dir=temp_dir)
f = open(tmp_file, "ab")
else:
tmp_file = None
f = output
if tmp_file is not None and f.tell() != 0: res = sess.get(url, headers={"Range": f"bytes={f.tell()}-"}, stream=True, verify=verify)
if not quiet:
if resume: print(translations["continue"], tmp_file, file=sys.stderr)
print(translations["to"], os.path.abspath(output) if output_is_path else output, file=sys.stderr)
try:
if not quiet: pbar = tqdm.tqdm(total=int(res.headers.get("Content-Length", 0)))
t_start = time()
for chunk in res.iter_content(chunk_size=CHUNK_SIZE):
f.write(chunk)
if not quiet: pbar.update(len(chunk))
if speed is not None:
elapsed_time_expected = 1.0 * pbar.n / speed
elapsed_time = time() - t_start
if elapsed_time < elapsed_time_expected: sleep(elapsed_time_expected - elapsed_time)
if not quiet: pbar.close()
if tmp_file:
f.close()
if output_dir is not None:
output_file = os.path.join(output_dir, output)
if os.path.exists(output_file): os.remove(output_file)
shutil.move(tmp_file, output_file)
else:
if os.path.exists(output): os.remove(output)
shutil.move(tmp_file, output)
finally:
sess.close()
return output123456789101112131415161718192021222324252627282930import os
import sys
import requests
from bs4 import BeautifulSoup
def Mediafire_Download(url, output=None, filename=None):
if not filename: filename = url.split('/')[-2]
if not output: output = os.path.dirname(os.path.realpath(__file__))
output_file = os.path.join(output, filename)
sess = requests.session()
sess.headers.update({"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6)"})
try:
with requests.get(BeautifulSoup(sess.get(url).content, "html.parser").find(id="downloadButton").get("href"), stream=True) as r:
r.raise_for_status()
with open(output_file, "wb") as f:
total_length = r.headers.get('content-length')
total_length = int(total_length)
download_progress = 0
for chunk in r.iter_content(chunk_size=1024):
download_progress += len(chunk)
f.write(chunk)
sys.stdout.write(f"\r[{filename}]: {int(100 * download_progress/total_length)}% ({round(download_progress/1024/1024, 2)}mb/{round(total_length/1024/1024, 2)}mb)")
sys.stdout.flush()
sys.stdout.write("\n")
return output_file
except Exception as e:
raise RuntimeError(e)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180import os
import re
import sys
import json
import tqdm
import codecs
import random
import base64
import struct
import shutil
import requests
import tempfile
from Crypto.Cipher import AES
from Crypto.Util import Counter
from tenacity import retry, wait_exponential, retry_if_exception_type
now_dir = os.getcwd()
sys.path.append(now_dir)
from main.configs.config import Config
translations = Config().translations
def makebyte(x):
return codecs.latin_1_encode(x)[0]
def a32_to_str(a):
return struct.pack('>%dI' % len(a), *a)
def get_chunks(size):
p = 0
s = 0x20000
while p + s < size:
yield (p, s)
p += s
if s < 0x100000: s += 0x20000
yield (p, size - p)
def decrypt_attr(attr, key):
attr = AES.new(a32_to_str(key), AES.MODE_CBC, makebyte('\0' * 16)).decrypt(attr)
attr = codecs.latin_1_decode(attr)[0]
attr = attr.rstrip('\0')
return json.loads(attr[4:]) if attr[:6] == 'MEGA{"' else False
@retry(retry=retry_if_exception_type(RuntimeError), wait=wait_exponential(multiplier=2, min=2, max=60))
def _api_request(data):
sequence_num = random.randint(0, 0xFFFFFFFF)
params = {'id': sequence_num}
sequence_num += 1
if not isinstance(data, list): data = [data]
json_resp = json.loads(requests.post(f'https://g.api.mega.co.nz/cs', params=params, data=json.dumps(data), timeout=160).text)
try:
if isinstance(json_resp, list): int_resp = json_resp[0] if isinstance(json_resp[0], int) else None
elif isinstance(json_resp, int): int_resp = json_resp
except IndexError:
int_resp = None
if int_resp is not None:
if int_resp == 0: return int_resp
if int_resp == -3: raise RuntimeError('int_resp==-3')
raise Exception(int_resp)
return json_resp[0]
def base64_url_decode(data):
data += '=='[(2 - len(data) * 3) % 4:]
for search, replace in (('-', '+'), ('_', '/'), (',', '')):
data = data.replace(search, replace)
return base64.b64decode(data)
def str_to_a32(b):
if isinstance(b, str): b = makebyte(b)
if len(b) % 4: b += b'\0' * (4 - len(b) % 4)
return struct.unpack('>%dI' % (len(b) / 4), b)
def mega_download_file(file_handle, file_key, dest_path=None, dest_filename=None, file=None):
if file is None:
file_key = str_to_a32(base64_url_decode(file_key))
file_data = _api_request({'a': 'g', 'g': 1, 'p': file_handle})
k = (file_key[0] ^ file_key[4], file_key[1] ^ file_key[5], file_key[2] ^ file_key[6], file_key[3] ^ file_key[7])
iv = file_key[4:6] + (0, 0)
meta_mac = file_key[6:8]
else:
file_data = _api_request({'a': 'g', 'g': 1, 'n': file['h']})
k = file['k']
iv = file['iv']
meta_mac = file['meta_mac']
if 'g' not in file_data: raise Exception(translations["file_not_access"])
file_size = file_data['s']
attribs = base64_url_decode(file_data['at'])
attribs = decrypt_attr(attribs, k)
file_name = dest_filename if dest_filename is not None else attribs['n']
input_file = requests.get(file_data['g'], stream=True).raw
if dest_path is None: dest_path = ''
else: dest_path += '/'
temp_output_file = tempfile.NamedTemporaryFile(mode='w+b', prefix='megapy_', delete=False)
k_str = a32_to_str(k)
counter = Counter.new(128, initial_value=((iv[0] << 32) + iv[1]) << 64)
aes = AES.new(k_str, AES.MODE_CTR, counter=counter)
mac_str = b'\0' * 16
mac_encryptor = AES.new(k_str, AES.MODE_CBC, mac_str)
iv_str = a32_to_str([iv[0], iv[1], iv[0], iv[1]])
pbar = tqdm.tqdm(total=file_size)
for _, chunk_size in get_chunks(file_size):
chunk = input_file.read(chunk_size)
chunk = aes.decrypt(chunk)
temp_output_file.write(chunk)
pbar.update(len(chunk))
encryptor = AES.new(k_str, AES.MODE_CBC, iv_str)
for i in range(0, len(chunk)-16, 16):
block = chunk[i:i + 16]
encryptor.encrypt(block)
if file_size > 16: i += 16
else: i = 0
block = chunk[i:i + 16]
if len(block) % 16: block += b'\0' * (16 - (len(block) % 16))
mac_str = mac_encryptor.encrypt(encryptor.encrypt(block))
file_mac = str_to_a32(mac_str)
temp_output_file.close()
if (file_mac[0] ^ file_mac[1], file_mac[2] ^ file_mac[3]) != meta_mac: raise ValueError(translations["mac_not_match"])
file_path = os.path.join(dest_path, file_name)
if os.path.exists(file_path): os.remove(file_path)
shutil.move(temp_output_file.name, file_path)
def mega_download_url(url, dest_path=None, dest_filename=None):
if '/file/' in url:
url = url.replace(' ', '')
file_id = re.findall(r'\W\w\w\w\w\w\w\w\w\W', url)[0][1:-1]
path = f'{file_id}!{url[re.search(file_id, url).end() + 1:]}'.split('!')
elif '!' in url: path = re.findall(r'/#!(.*)', url)[0].split('!')
else: raise Exception(translations["missing_url"])
return mega_download_file(file_handle=path[0], file_key=path[1], dest_path=dest_path, dest_filename=dest_filename)12345678910111213141516171819import os
import requests
def pixeldrain(url, output_dir):
try:
file_id = url.split("pixeldrain.com/u/")[1]
response = requests.get(f"https://pixeldrain.com/api/file/{file_id}")
if response.status_code == 200:
file_name = (response.headers.get("Content-Disposition").split("filename=")[-1].strip('";'))
file_path = os.path.join(output_dir, file_name)
with open(file_path, "wb") as newfile:
newfile.write(response.content)
return file_path
else: return None
except Exception as e:
raise RuntimeError(e)main/app/app.pyDeleted
main/app/clean.pyDeleted-40
main/app/run_tensorboard.pyDeleted-30
main/app/sync.pyDeleted-80
main/configs/config.jsonDeleted-229
main/configs/config.pyDeleted-120
main/configs/v1/32000.jsonDeleted-82
main/configs/v1/40000.jsonDeleted-80
main/configs/v1/48000.jsonDeleted-82
main/configs/v2/32000.jsonDeleted-76
main/configs/v2/40000.jsonDeleted-76
main/configs/v2/48000.jsonDeleted-76
main/inference/audio_effects.pyDeleted-170
main/inference/convert.pyDeleted-1060
main/inference/create_dataset.pyDeleted-370
main/inference/create_index.pyDeleted-120
main/inference/extract.pyDeleted-450
main/inference/preprocess.pyDeleted-360
main/inference/separator_music.pyDeleted-400
main/inference/train.pyDeleted-1600
main/library/algorithm/commons.pyDeleted-100
main/library/algorithm/modules.pyDeleted-80
main/library/algorithm/residuals.pyDeleted-170
main/library/algorithm/separator.pyDeleted-420
main/library/algorithm/synthesizers.pyDeleted-590
main/library/architectures/demucs_separator.pyDeleted-340
main/library/architectures/mdx_separator.pyDeleted-370
main/library/predictors/FCPE.pyDeleted-600
main/library/predictors/RMVPE.pyDeleted-270
main/library/uvr5_separator/common_separator.pyDeleted-270
main/library/uvr5_separator/demucs/apply.pyDeleted-280
main/library/uvr5_separator/demucs/demucs.pyDeleted-340
main/library/uvr5_separator/demucs/hdemucs.pyDeleted-850
main/library/uvr5_separator/demucs/htdemucs.pyDeleted-690
main/library/uvr5_separator/demucs/states.pyDeleted-70
main/library/uvr5_separator/demucs/utils.pyDeleted-10
main/library/uvr5_separator/spec_utils.pyDeleted-1100
main/tools/gdown.pyDeleted-230
main/tools/mediafire.pyDeleted-30
main/tools/meganz.pyDeleted-180
main/tools/pixeldrain.pyDeleted-19