From 603f66dc2a508d774c1ca3c707dfde7c10833dc4 Mon Sep 17 00:00:00 2001 From: Dilan Boskan Date: Sat, 16 Jan 2021 16:28:52 +0100 Subject: [PATCH] Refactoring Code; Fixed '_' being appended to the music file's name when Model Test Mode is not selected --- inference_v2.py | 74 ++++++++++++++++++++++++++++--------------------- inference_v4.py | 72 ++++++++++++++++++++++++++--------------------- 2 files changed, 84 insertions(+), 62 deletions(-) diff --git a/inference_v2.py b/inference_v2.py index 86758e1..a8c54f1 100644 --- a/inference_v2.py +++ b/inference_v2.py @@ -144,27 +144,55 @@ def determineExportPath(): Determine the path, where the music file is stored """ folder_path = data["export_path"] + file_add_on = '' if data['modelFolder']: # Model Test Mode selected - folder_name = '' # -Instrumental- if os.path.isfile(data['instrumentalModel']): - folder_name += os.path.splitext(os.path.basename(data['instrumentalModel']))[0] + file_add_on += os.path.splitext(os.path.basename(data['instrumentalModel']))[0] # -Vocal- elif os.path.isfile(data['vocalModel']): - folder_name += os.path.splitext(os.path.basename(data['vocalModel']))[0] + file_add_on += os.path.splitext(os.path.basename(data['vocalModel']))[0] # -Stack- if os.path.isfile(data['stackModel']): - folder_name += '-' + os.path.splitext(os.path.basename(data['stackModel']))[0] + file_add_on += '-' + os.path.splitext(os.path.basename(data['stackModel']))[0] # Add generated folder name to export Path - folder_path = os.path.join(folder_path, folder_name) + folder_path = os.path.join(folder_path, file_add_on) if not os.path.isdir(folder_path): # Folder does not exist os.mkdir(folder_path) + + file_add_on = f'_{file_add_on}' + + return folder_path, file_add_on + + +def getModelDeviceFile(models, devices, music_file, loop_num): + if not loop_num: + # First Iteration + if data['stackOnly']: + if os.path.isfile(data['stackModel']): + model_name = os.path.basename(data['stackModel']) + model = models['stack'] + device = devices['stack'] + else: + raise ValueError(f'Selected stack only model, however, stack model path file cannot be found\nPath: "{data["stackModel"]}"') # nopep8 + else: + model_name = os.path.basename(data[f'{data["useModel"]}Model']) + model = models[data['useModel']] + device = devices[data['useModel']] + else: + model_name = os.path.basename(data['stackModel']) + # Every other iteration + model = models['stack'] + device = devices['stack'] + # Reference new music file + music_file = 'temp.wav' + + return model, device, model_name, music_file - return folder_path def main(window: tk.Wm, text_widget: tk.Text, button_widget: tk.Button, progress_var: tk.Variable, **kwargs: dict): @@ -338,13 +366,13 @@ def main(window: tk.Wm, text_widget: tk.Text, button_widget: tk.Button, progress # Instrumental if instrumental_name is not None: instrumental_path = os.path.join(save_path, - f'{os.path.basename(base_name)}_{instrumental_name}_{modelFolderName}.wav') + f'{os.path.basename(base_name)}_{instrumental_name}{file_add_on}.wav') sf.write(instrumental_path, wav_instrument.T, sr) # Vocal if vocal_name is not None: vocal_path = os.path.join(save_path, - f'{os.path.basename(base_name)}_{vocal_name}_{modelFolderName}.wav') + f'{os.path.basename(base_name)}_{vocal_name}{file_add_on}.wav') sf.write(vocal_path, wav_vocals.T, sr) @@ -376,7 +404,7 @@ def main(window: tk.Wm, text_widget: tk.Text, button_widget: tk.Button, progress button_widget.configure(state=tk.DISABLED) # Disable Button models, devices = load_models() - folder_path = determineExportPath() + folder_path, file_add_on = determineExportPath() # Determine Loops total_loops = data['stackPasses'] @@ -389,33 +417,17 @@ def main(window: tk.Wm, text_widget: tk.Text, button_widget: tk.Button, progress f'{file_num}_{os.path.splitext(os.path.basename(music_file))[0]}') try: for loop_num in range(total_loops): - # -Determine which model will be used- - if not loop_num: - # First Iteration - if data['stackOnly']: - if os.path.isfile(data['stackModel']): - model_name = os.path.basename(data['stackModel']) - model = models['stack'] - device = devices['stack'] - else: - raise ValueError(f'Selected stack only model, however, stack model path file cannot be found\nPath: "{data["stackModel"]}"') # nopep8 - else: - model_name = os.path.basename(data[f'{data["useModel"]}Model']) - model = models[data['useModel']] - device = devices[data['useModel']] - else: - model_name = os.path.basename(data['stackModel']) - # Every other iteration - model = models['stack'] - device = devices['stack'] - # Reference new music file - music_file = 'temp.wav' - # -Get text and update progress- base_text = get_baseText(total_files=len(data['input_paths']), total_loops=total_loops, file_num=file_num, loop_num=loop_num) + # -Determine which model will be used- + model, device, model_name, music_file = getModelDeviceFile(models, + devices, + music_file, + loop_num) + progress_kwargs = {'progress_var': progress_var, 'total_files': len(data['input_paths']), 'total_loops': total_loops, diff --git a/inference_v4.py b/inference_v4.py index cee1ce6..fd167a6 100644 --- a/inference_v4.py +++ b/inference_v4.py @@ -264,27 +264,54 @@ def determineExportPath(): Determine the path, where the music file is stored """ folder_path = data["export_path"] + file_add_on = '' if data['modelFolder']: # Model Test Mode selected - folder_name = '' # -Instrumental- if os.path.isfile(data['instrumentalModel']): - folder_name += os.path.splitext(os.path.basename(data['instrumentalModel']))[0] + file_add_on += os.path.splitext(os.path.basename(data['instrumentalModel']))[0] # -Vocal- elif os.path.isfile(data['vocalModel']): - folder_name += os.path.splitext(os.path.basename(data['vocalModel']))[0] + file_add_on += os.path.splitext(os.path.basename(data['vocalModel']))[0] # -Stack- if os.path.isfile(data['stackModel']): - folder_name += '-' + os.path.splitext(os.path.basename(data['stackModel']))[0] + file_add_on += '-' + os.path.splitext(os.path.basename(data['stackModel']))[0] # Add generated folder name to export Path - folder_path = os.path.join(folder_path, folder_name) + folder_path = os.path.join(folder_path, file_add_on) if not os.path.isdir(folder_path): # Folder does not exist os.mkdir(folder_path) + + file_add_on = f'_{file_add_on}' - return folder_path + return folder_path, file_add_on + + +def getModelDeviceFile(vocal_remover, music_file, loop_num): + if not loop_num: + # First Iteration + if data['stackOnly']: + if os.path.isfile(data['stackModel']): + model_name = os.path.basename(data['stackModel']) + model = vocal_remover.models['stack'] + device = vocal_remover.devices['stack'] + else: + raise ValueError(f'Selected stack only model, however, stack model path file cannot be found\nPath: "{data["stackModel"]}"') # nopep8 + else: + model_name = os.path.basename(data[f'{data["useModel"]}Model']) + model = vocal_remover.models[data['useModel']] + device = vocal_remover.devices[data['useModel']] + else: + model_name = os.path.basename(data['stackModel']) + # Every other iteration + model = vocal_remover.models['stack'] + device = vocal_remover.devices['stack'] + # Reference new music file + music_file = 'temp.wav' + + return model, device, model_name, music_file def main(window: tk.Wm, text_widget: tk.Text, button_widget: tk.Button, progress_var: tk.Variable, @@ -326,8 +353,8 @@ def main(window: tk.Wm, text_widget: tk.Text, button_widget: tk.Button, progress # Reverse names vocal_name, instrumental_name = instrumental_name, vocal_name elif data['saveAllStacked']: - folder_name = os.path.basename(base_name) + ' Stacked Outputs' # nopep8 - save_path = os.path.join(save_path, folder_name) + stacked_folder_name = os.path.basename(base_name) + ' Stacked Outputs' # nopep8 + save_path = os.path.join(save_path, stacked_folder_name) if not os.path.isdir(save_path): os.mkdir(save_path) @@ -355,14 +382,14 @@ def main(window: tk.Wm, text_widget: tk.Text, button_widget: tk.Button, progress # Instrumental if instrumental_name is not None: instrumental_path = os.path.join(save_path, - f'{os.path.basename(base_name)}_{instrumental_name}_{modelFolderName}.wav') + f'{os.path.basename(base_name)}_{instrumental_name}{file_add_on}.wav') sf.write(instrumental_path, wav_instrument.T, sr) # Vocal if vocal_name is not None: vocal_path = os.path.join(save_path, - f'{os.path.basename(base_name)}_{vocal_name}_{modelFolderName}.wav') + f'{os.path.basename(base_name)}_{vocal_name}{file_add_on}.wav') sf.write(vocal_path, wav_vocals.T, sr) @@ -385,7 +412,7 @@ def main(window: tk.Wm, text_widget: tk.Text, button_widget: tk.Button, progress button_widget.configure(state=tk.DISABLED) # Disable Button vocal_remover = VocalRemover(data, text_widget) - folder_path = determineExportPath() + folder_path, file_add_on = determineExportPath() # Determine Loops total_loops = data['stackPasses'] @@ -399,26 +426,9 @@ def main(window: tk.Wm, text_widget: tk.Text, button_widget: tk.Button, progress # --Seperate Music Files-- for loop_num in range(total_loops): # -Determine which model will be used- - if not loop_num: - # First Iteration - if data['stackOnly']: - if os.path.isfile(data['stackModel']): - model_name = os.path.basename(data['stackModel']) - model = vocal_remover.models['stack'] - device = vocal_remover.devices['stack'] - else: - raise ValueError(f'Selected stack only model, however, stack model path file cannot be found\nPath: "{data["stackModel"]}"') # nopep8 - else: - model_name = os.path.basename(data[f'{data["useModel"]}Model']) - model = vocal_remover.models[data['useModel']] - device = vocal_remover.devices[data['useModel']] - else: - model_name = os.path.basename(data['stackModel']) - # Every other iteration - model = vocal_remover.models['stack'] - device = vocal_remover.devices['stack'] - # Reference new music file - music_file = 'temp.wav' + model, device, model_name, music_file = getModelDeviceFile(vocal_remover, + music_file, + loop_num) # -Get text and update progress- base_text = get_baseText(total_files=len(data['input_paths']),