Spaces:
Running
Running
File size: 6,482 Bytes
c8be32d |
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 216 217 218 219 220 221 222 223 224 225 226 |
"""
This module contains functions to manage audio files.
"""
import os
import shutil
from pathlib import PurePath
import gradio as gr
from backend.common import INTERMEDIATE_AUDIO_DIR, OUTPUT_AUDIO_DIR, display_progress
from backend.exceptions import InputMissingError, InvalidPathError, PathNotFoundError
from common import GRADIO_TEMP_DIR
def get_output_audio() -> list[tuple[str, str]]:
"""
Get the name and path of all output audio files.
Returns
-------
list[tuple[str, str]]
A list of tuples containing the name and path of each output audio file.
"""
if os.path.isdir(OUTPUT_AUDIO_DIR):
named_output_files = [
(file_name, os.path.join(OUTPUT_AUDIO_DIR, file_name))
for file_name in os.listdir(OUTPUT_AUDIO_DIR)
]
return sorted(named_output_files, key=lambda x: x[0])
return []
def delete_intermediate_audio(
song_dirs: list[str],
progress_bar: gr.Progress | None = None,
percentage: float = 0.0,
) -> str:
"""
Delete intermediate audio files in provided song directories.
Parameters
----------
song_dirs : list[str]
Paths of song directories to delete intermediate audio files for.
progress_bar : gr.Progress, optional
Gradio progress bar to update.
percentage : float, default=0.0
Percentage to display in the progress bar.
Returns
-------
str
Success message.
Raises
------
InputMissingError
If no song directories are provided.
PathNotFoundError
If a song directory does not exist.
InvalidPathError
If a song directory is not located in the root of the intermediate audio directory.
"""
if not song_dirs:
raise InputMissingError(
"Song directories missing! Please provide a non-empty list of song"
" directories."
)
display_progress(
"[~] Deleting intermediate audio files for selected songs...",
percentage,
progress_bar,
)
for song_dir in song_dirs:
if not os.path.isdir(song_dir):
raise PathNotFoundError(f"Song directory '{song_dir}' does not exist.")
if not PurePath(song_dir).parent == PurePath(INTERMEDIATE_AUDIO_DIR):
raise InvalidPathError(
f"Song directory '{song_dir}' is not located in the root of the"
" intermediate audio directory."
)
shutil.rmtree(song_dir)
return "[+] Successfully deleted intermediate audio files for selected songs!"
def delete_all_intermediate_audio(
progress_bar: gr.Progress | None = None, percentage: float = 0.0
) -> str:
"""
Delete all intermediate audio files.
Parameters
----------
progress_bar : gr.Progress, optional
Gradio progress bar to update.
percentage : float, default=0.0
Returns
-------
str
Success message.
"""
display_progress(
"[~] Deleting all intermediate audio files...", percentage, progress_bar
)
if os.path.isdir(INTERMEDIATE_AUDIO_DIR):
shutil.rmtree(INTERMEDIATE_AUDIO_DIR)
return "[+] All intermediate audio files successfully deleted!"
def delete_output_audio(
output_audio_files: list[str],
progress_bar: gr.Progress | None = None,
percentage: float = 0.0,
) -> str:
"""
Delete selected output audio files.
Parameters
----------
output_audio_files : list[str]
Paths of output audio files to delete.
progress_bar : gr.Progress, optional
Gradio progress bar to update.
percentage : float, default=0.0
Percentage to display in the progress bar.
Returns
-------
str
Success message.
Raises
------
InputMissingError
If no output audio files are provided.
PathNotFoundError
If an output audio file does not exist.
InvalidPathError
If an output audio file is not located in the root of the output audio directory.
"""
if not output_audio_files:
raise InputMissingError(
"Output audio files missing! Please provide a non-empty list of output"
" audio files."
)
display_progress(
"[~] Deleting selected output audio files...", percentage, progress_bar
)
for output_audio_file in output_audio_files:
if not os.path.isfile(output_audio_file):
raise PathNotFoundError(
f"Output audio file '{output_audio_file}' does not exist."
)
if not PurePath(output_audio_file).parent == PurePath(OUTPUT_AUDIO_DIR):
raise InvalidPathError(
f"Output audio file '{output_audio_file}' is not located in the root of"
" the output audio directory."
)
os.remove(output_audio_file)
return "[+] Successfully deleted selected output audio files!"
def delete_all_output_audio(
progress_bar: gr.Progress | None = None, percentage: float = 0.0
) -> str:
"""
Delete all output audio files.
Parameters
----------
progress_bar : gr.Progress, optional
Gradio progress bar to update.
percentage : float, default=0.0
Percentage to display in the progress bar.
Returns
-------
str
Success message.
"""
display_progress("[~] Deleting all output audio files...", percentage, progress_bar)
if os.path.isdir(OUTPUT_AUDIO_DIR):
shutil.rmtree(OUTPUT_AUDIO_DIR)
return "[+] All output audio files successfully deleted!"
def delete_all_audio(
progress_bar: gr.Progress | None = None, percentage: float = 0.0
) -> str:
"""
Delete all audio files.
Parameters
----------
progress_bar : gr.Progress, optional
Gradio progress bar to update.
percentage : float, default=0.0
Percentage to display in the progress bar.
Returns
-------
str
Success message.
"""
display_progress("[~] Deleting all audio files...", percentage, progress_bar)
if os.path.isdir(INTERMEDIATE_AUDIO_DIR):
shutil.rmtree(INTERMEDIATE_AUDIO_DIR)
if os.path.isdir(OUTPUT_AUDIO_DIR):
shutil.rmtree(OUTPUT_AUDIO_DIR)
return "[+] All audio files successfully deleted!"
def delete_gradio_temp_dir() -> None:
"""
Delete the directory where Gradio stores temporary files.
"""
if os.path.isdir(GRADIO_TEMP_DIR):
shutil.rmtree(GRADIO_TEMP_DIR)
|