Files
Mangio-RVC-Fork/my_utils.py

76 lines
3.1 KiB
Python
Raw Normal View History

import ffmpeg
import numpy as np
2023-07-23 03:47:53 +00:00
# import praatio
# import praatio.praat_scripts
2023-07-21 20:36:54 +07:00
import os
2023-07-23 03:47:53 +00:00
# from os.path import join
# praatEXE = join('.',os.path.abspath(os.getcwd()) + r"\Praat.exe")
2023-07-21 20:36:54 +07:00
def load_audio(file, sr, DoFormant, Quefrency, Timbre):
2023-03-31 17:54:38 +08:00
try:
# https://github.com/openai/whisper/blob/main/whisper/audio.py#L26
# This launches a subprocess to decode audio while down-mixing and resampling as necessary.
# Requires the ffmpeg CLI and `ffmpeg-python` package to be installed.
file = (
file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
) # 防止小白拷路径头尾带了空格和"和回车
2023-07-23 03:47:53 +00:00
file_formanted = file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
with open("formanting.txt", "r") as fvf:
2023-07-21 20:36:54 +07:00
content = fvf.readlines()
2023-07-23 03:47:53 +00:00
if "True" in content[0].split("\n")[0]:
# print("true")
2023-07-21 20:36:54 +07:00
DoFormant = True
2023-07-23 03:47:53 +00:00
Quefrency, Timbre = content[1].split("\n")[0], content[2].split("\n")[0]
2023-07-21 20:36:54 +07:00
else:
2023-07-23 03:47:53 +00:00
# print("not true")
2023-07-21 20:36:54 +07:00
DoFormant = False
2023-07-23 03:47:53 +00:00
2023-07-21 20:36:54 +07:00
if DoFormant:
2023-07-23 03:47:53 +00:00
# os.system(f"stftpitchshift -i {file} -q {Quefrency} -t {Timbre} -o {file_formanted}")
# print('stftpitchshift -i "%s" -p 1.0 --rms -w 128 -v 8 -q %s -t %s -o "%s"' % (file, Quefrency, Timbre, file_formanted))
if not file.endswith(".wav"):
converting = (
ffmpeg.input(file, threads = 0)
.output(f"{file_formanted}.wav")
.run(
cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True
)
)
2023-07-21 20:36:54 +07:00
print("formanting...")
2023-07-23 03:47:53 +00:00
os.system(
'runtime\Scripts\stftpitchshift.exe -i "%s" -q %s -t %s -o "%sFORMANTED"'
% (file_formanted, Quefrency, Timbre, file_formanted)
2023-07-23 03:47:53 +00:00
)
2023-07-21 20:36:54 +07:00
print("formanted!")
2023-07-23 03:47:53 +00:00
# filepraat = (os.path.abspath(os.getcwd()) + '\\' + file).replace('/','\\')
# file_formantedpraat = ('"' + os.path.abspath(os.getcwd()) + '/' + 'formanted'.join(file_formanted) + '"').replace('/','\\')
2023-07-21 20:36:54 +07:00
out, _ = (
2023-07-23 03:47:53 +00:00
ffmpeg.input("%sFORMANTED%s" % (file_formanted, ".wav"), threads=0)
2023-07-21 20:36:54 +07:00
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
2023-07-23 03:47:53 +00:00
.run(
cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True
)
2023-07-21 20:36:54 +07:00
)
2023-07-23 03:47:53 +00:00
os.remove("%sFORMANTED%s" % (file_formanted, ".wav"))
os.remove(f"{file_formanted}.wav")
2023-07-21 20:36:54 +07:00
else:
out, _ = (
ffmpeg.input(file, threads=0)
.output("-", format="f32le", acodec="pcm_f32le", ac=1, ar=sr)
2023-07-23 03:47:53 +00:00
.run(
cmd=["ffmpeg", "-nostdin"], capture_stdout=True, capture_stderr=True
)
2023-07-21 20:36:54 +07:00
)
except Exception as e:
raise RuntimeError(f"Failed to load audio: {e}")
2023-03-31 17:54:38 +08:00
return np.frombuffer(out, np.float32).flatten()