Spaces:
Runtime error
Runtime error
File size: 5,918 Bytes
1a942eb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
"""Module which defines functions to manage audio files."""
import operator
import shutil
from collections.abc import Sequence
from pathlib import Path
import gradio as gr
from ultimate_rvc.core.common import (
INTERMEDIATE_AUDIO_BASE_DIR,
OUTPUT_AUDIO_DIR,
display_progress,
)
from ultimate_rvc.core.exceptions import (
Entity,
InvalidLocationError,
Location,
NotFoundError,
NotProvidedError,
UIMessage,
)
from ultimate_rvc.typing_extra import StrPath
def get_saved_output_audio() -> list[tuple[str, str]]:
"""
Get the name and path of all output audio files.
Returns
-------
list[tuple[str, Path]]
A list of tuples containing the name and path of each output
audio file.
"""
if OUTPUT_AUDIO_DIR.is_dir():
named_output_files = [
(file_path.name, str(file_path)) for file_path in OUTPUT_AUDIO_DIR.iterdir()
]
return sorted(named_output_files, key=operator.itemgetter(0))
return []
def delete_intermediate_audio(
directories: Sequence[StrPath],
progress_bar: gr.Progress | None = None,
percentage: float = 0.5,
) -> None:
"""
Delete provided directories containing intermediate audio files.
The provided directories must be located in the root of the
intermediate audio base directory.
Parameters
----------
directories : Sequence[StrPath]
Paths to directories containing intermediate audio files to
delete.
progress_bar : gr.Progress, optional
Gradio progress bar to update.
percentage : float, default=0.5
Percentage to display in the progress bar.
Raises
------
NotProvidedError
If no paths are provided.
NotFoundError
if a provided path does not point to an existing directory.
InvalidLocationError
If a provided path does not point to a location in the root of
the intermediate audio base directory.
"""
if not directories:
raise NotProvidedError(entity=Entity.DIRECTORIES, ui_msg=UIMessage.NO_SONG_DIRS)
display_progress(
"[~] Deleting directories ...",
percentage,
progress_bar,
)
for directory in directories:
dir_path = Path(directory)
if not dir_path.is_dir():
raise NotFoundError(entity=Entity.DIRECTORY, location=dir_path)
if dir_path.parent != INTERMEDIATE_AUDIO_BASE_DIR:
raise InvalidLocationError(
entity=Entity.DIRECTORY,
location=Location.INTERMEDIATE_AUDIO_ROOT,
path=dir_path,
)
shutil.rmtree(dir_path)
def delete_all_intermediate_audio(
progress_bar: gr.Progress | None = None,
percentage: float = 0.5,
) -> None:
"""
Delete all intermediate audio files.
Parameters
----------
progress_bar : gr.Progress, optional
Gradio progress bar to update.
percentage : float, default=0.5
Percentage to display in the progress bar.
"""
display_progress(
"[~] Deleting all intermediate audio files...",
percentage,
progress_bar,
)
if INTERMEDIATE_AUDIO_BASE_DIR.is_dir():
shutil.rmtree(INTERMEDIATE_AUDIO_BASE_DIR)
def delete_output_audio(
files: Sequence[StrPath],
progress_bar: gr.Progress | None = None,
percentage: float = 0.5,
) -> None:
"""
Delete provided output audio files.
The provided files must be located in the root of the output audio
directory.
Parameters
----------
files : Sequence[StrPath]
Paths to the output audio files to delete.
progress_bar : gr.Progress, optional
Gradio progress bar to update.
percentage : float, default=0.5
Percentage to display in the progress bar.
Raises
------
NotProvidedError
If no paths are provided.
NotFoundError
If a provided path does not point to an existing file.
InvalidLocationError
If a provided path does not point to a location in the root of
the output audio directory.
"""
if not files:
raise NotProvidedError(
entity=Entity.FILES,
ui_msg=UIMessage.NO_OUTPUT_AUDIO_FILES,
)
display_progress(
"[~] Deleting output audio files...",
percentage,
progress_bar,
)
for file in files:
file_path = Path(file)
if not file_path.is_file():
raise NotFoundError(entity=Entity.FILE, location=file_path)
if file_path.parent != OUTPUT_AUDIO_DIR:
raise InvalidLocationError(
entity=Entity.FILE,
location=Location.OUTPUT_AUDIO_ROOT,
path=file_path,
)
file_path.unlink()
def delete_all_output_audio(
progress_bar: gr.Progress | None = None,
percentage: float = 0.5,
) -> None:
"""
Delete all output audio files.
Parameters
----------
progress_bar : gr.Progress, optional
Gradio progress bar to update.
percentage : float, default=0.5
Percentage to display in the progress bar.
"""
display_progress("[~] Deleting all output audio files...", percentage, progress_bar)
if OUTPUT_AUDIO_DIR.is_dir():
shutil.rmtree(OUTPUT_AUDIO_DIR)
def delete_all_audio(
progress_bar: gr.Progress | None = None,
percentage: float = 0.5,
) -> None:
"""
Delete all audio files.
Parameters
----------
progress_bar : gr.Progress, optional
Gradio progress bar to update.
percentage : float, default=0.5
Percentage to display in the progress bar.
"""
display_progress("[~] Deleting all audio files...", percentage, progress_bar)
if INTERMEDIATE_AUDIO_BASE_DIR.is_dir():
shutil.rmtree(INTERMEDIATE_AUDIO_BASE_DIR)
if OUTPUT_AUDIO_DIR.is_dir():
shutil.rmtree(OUTPUT_AUDIO_DIR)
|