Skip to content

Commit

Permalink
feat(gui): configurable output file (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
visidyn committed Apr 22, 2023
1 parent cbd3896 commit a714be1
Showing 1 changed file with 71 additions and 5 deletions.
76 changes: 71 additions & 5 deletions src/so_vits_svc_fork/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import multiprocessing
import os
from copy import copy
from logging import getLogger
from pathlib import Path
Expand Down Expand Up @@ -57,6 +58,54 @@ def delete_preset(name: str) -> dict:
return load_presets()


def get_output_path(input_path: Path) -> Path:
# Default output path
output_path = input_path.parent / f"{input_path.stem}.out{input_path.suffix}"

# Increment file number in path if output file already exists
file_num = 1
while output_path.exists():
output_path = (
input_path.parent / f"{input_path.stem}.out_{file_num}{input_path.suffix}"
)
file_num += 1
return output_path


def get_supported_file_types() -> tuple[tuple[str], ...]:
return tuple(
[
((extension, f".{extension.lower()}"))
for extension in sf.available_formats().keys()
]
)


def validate_output_file_type(output_path: Path) -> bool:
supported_file_types = sorted(
[f".{extension.lower()}" for extension in sf.available_formats().keys()]
)
if not output_path.suffix:
sg.popup_ok(
"Error: Output path missing file type extension, enter "
+ "one of the following manually:"
+ os.linesep
+ os.linesep
+ os.linesep.join(supported_file_types)
)
return False
if output_path.suffix.lower() not in supported_file_types:
sg.popup_ok(
f"Error: {output_path.suffix.lower()} is not a supported "
+ "extension; use one of the following:"
+ os.linesep
+ os.linesep
+ os.linesep.join(supported_file_types)
)
return False
return True


def get_devices(
update: bool = True,
) -> tuple[list[str], list[str], list[int], list[int]]:
Expand Down Expand Up @@ -255,10 +304,20 @@ def main():
[
sg.Text("Input audio path"),
sg.Push(),
sg.InputText(key="input_path"),
sg.InputText(key="input_path", enable_events=True),
sg.FileBrowse(initial_folder=".", key="input_path_browse"),
sg.Button("Play", key="play_input"),
],
[
sg.Text("Output audio path"),
sg.Push(),
sg.InputText(key="output_path"),
sg.FileSaveAs(
initial_folder=".",
key="output_path_browse",
file_types=get_supported_file_types(),
),
],
[sg.Checkbox(key="auto_play", text="Auto play", default=True)],
],
"Realtime": [
Expand Down Expand Up @@ -528,6 +587,10 @@ def apply_preset(name: str) -> None:
disabled=values["auto_predict_f0"],
visible=not values["auto_predict_f0"],
)

input_path = Path(values["input_path"])
output_path = Path(values["output_path"])

if event == "add_preset":
presets = add_preset(
values["preset_name"], {key: values[key] for key in PRESET_KEYS}
Expand All @@ -543,14 +606,17 @@ def apply_preset(name: str) -> None:
update_devices()
elif event == "config_path":
update_speaker()
elif event == "input_path":
# Don't change the output path if it's already set
if values["output_path"]:
continue
window.Element("output_path").Update(str(get_output_path(input_path)))
elif event == "infer":
input_path = Path(values["input_path"])
output_path = (
input_path.parent / f"{input_path.stem}.out{input_path.suffix}"
)
if not input_path.exists() or not input_path.is_file():
LOG.warning(f"Input path {input_path} does not exist.")
continue
if not validate_output_file_type(output_path):
continue

try:
from so_vits_svc_fork.inference.main import infer
Expand Down

0 comments on commit a714be1

Please sign in to comment.