1
0
mirror of https://github.com/fumiama/Retrieval-based-Voice-Conversion-WebUI.git synced 2026-06-10 04:50:26 +08:00

refactor(audio): store all of the audio related functions in the infer.lib.audio

refactors previous commit to have singular functions for each task, all located in `infer.lib.audio`
This commit is contained in:
Alex Murkoff
2024-06-11 12:24:03 +07:00
parent 15cf2e067e
commit 6425f2091e
4 changed files with 74 additions and 74 deletions

View File

@@ -6,7 +6,7 @@ logger = logging.getLogger(__name__)
import librosa
import numpy as np
import soundfile as sf
import av
from infer.lib.audio import downsample_audio
import torch
from infer.lib.uvr5_pack.lib_v5 import nets_123821KB as Nets
@@ -147,7 +147,7 @@ class AudioPre:
)
if os.path.exists(path):
opt_format_path = path[:-4] + ".%s" % format
process_audio(path, opt_format_path, format)
downsample_audio(path, opt_format_path, format)
if vocal_root is not None:
if is_hp3 == True:
head = "instrument_"
@@ -182,37 +182,7 @@ class AudioPre:
self.mp.param["sr"],
)
opt_format_path = path[:-4] + ".%s" % format
process_audio(path, opt_format_path, format)
def process_audio(input_path: str, output_path: str, format: str) -> None:
if not os.path.exists(input_path): return
input_container = av.open(input_path)
output_container = av.open(output_path, 'w')
# Create a stream in the output container
input_stream = input_container.streams.audio[0]
output_stream = output_container.add_stream(format)
output_stream.bit_rate = 128_000 # 128kb/s (equivalent to -q:a 2)
# Copy packets from the input file to the output file
for packet in input_container.demux(input_stream):
for frame in packet.decode():
for out_packet in output_stream.encode(frame):
output_container.mux(out_packet)
for packet in output_stream.encode():
output_container.mux(packet)
# Close the containers
input_container.close()
output_container.close()
try: # Remove the original file
os.remove(input_path)
except Exception as e:
print(f"Failed to remove the original file: {e}")
downsample_audio(path, opt_format_path, format)
class AudioPreDeEcho:
def __init__(self, agg, model_path, device, is_half, tta=False):
@@ -342,7 +312,7 @@ class AudioPreDeEcho:
)
if os.path.exists(path):
opt_format_path = path[:-4] + ".%s" % format
process_audio(path, opt_format_path, format)
downsample_audio(path, opt_format_path, format)
if vocal_root is not None:
if self.data["high_end_process"].startswith("mirroring"):
input_high_end_ = spec_utils.mirroring(
@@ -374,4 +344,4 @@ class AudioPreDeEcho:
)
if os.path.exists(path):
opt_format_path = path[:-4] + ".%s" % format
process_audio(path, opt_format_path, format)
downsample_audio(path, opt_format_path, format)