Skip to content

Commit

Permalink
Revert from using SQL back to storing data on disk directly (for stab…
Browse files Browse the repository at this point in the history
…ility)
  • Loading branch information
kalomaze committed Jul 28, 2023
2 parents 5d611da + 177e9a8 commit e2b3aa0
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 142 deletions.
Empty file removed TEMP/db
Empty file.
93 changes: 22 additions & 71 deletions infer-web.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from lib.infer_pack.models_onnx import SynthesizerTrnMsNSFsidM
from infer_uvr5 import _audio_pre_, _audio_pre_new
from MDXNet import MDXNetDereverb
from my_utils import load_audio
from my_utils import load_audio, CSVutil
from train.process_ckpt import change_info, extract_small_model, merge, show_info
from vc_infer_pipeline import VC
from sklearn.cluster import MiniBatchKMeans
Expand All @@ -56,63 +56,26 @@
warnings.filterwarnings("ignore")
torch.manual_seed(114514)

import sqlite3

def clear_sql(signal, frame):
cursor.execute("DELETE FROM formant_data")
cursor.execute("DELETE FROM stop_train")
conn.commit()
conn.close()
print("Clearing SQL database...")
sys.exit(0)

if sys.platform == 'win32':
signal.signal(signal.SIGBREAK, clear_sql)

signal.signal(signal.SIGINT, clear_sql)
signal.signal(signal.SIGTERM, clear_sql)



logging.getLogger("numba").setLevel(logging.WARNING)

conn = sqlite3.connect('TEMP/db:cachedb?mode=memory&cache=shared', check_same_thread=False)
cursor = conn.cursor()
import csv



cursor.execute("""
CREATE TABLE IF NOT EXISTS formant_data (
Quefrency FLOAT,
Timbre FLOAT,
DoFormant INTEGER
)
""")

cursor.execute("""
CREATE TABLE IF NOT EXISTS stop_train (
stop BOOL
)
""")
if not os.path.isdir('csvdb/'):
os.makedirs('csvdb')
frmnt, stp = open("csvdb/formanting.csv", 'w'), open("csvdb/stop.csv", 'w')
frmnt.close()
stp.close()

global DoFormant, Quefrency, Timbre

try:
cursor.execute("SELECT Quefrency, Timbre, DoFormant FROM formant_data")
row = cursor.fetchone()
if row is not None:
Quefrency, Timbre, DoFormant = row
else:
raise ValueError("No data")

except (ValueError, TypeError):
Quefrency = 8.0
Timbre = 1.2
DoFormant = False
cursor.execute("DELETE FROM formant_data")
cursor.execute("DELETE FROM stop_train")
cursor.execute("INSERT INTO formant_data (Quefrency, Timbre, DoFormant) VALUES (?, ?, ?)", (Quefrency, Timbre, 0))
conn.commit()
DoFormant, Quefrency, Timbre = CSVutil('csvdb/formanting.csv', 'r', 'formanting')
DoFormant = (
lambda DoFormant: True if DoFormant.lower() == 'true' else (False if DoFormant.lower() == 'false' else DoFormant)
)(DoFormant)
except (ValueError, TypeError, IndexError):
DoFormant, Quefrency, Timbre = False, 1.0, 1.0
CSVutil('csvdb/formanting.csv', 'w+', 'formanting', DoFormant, Quefrency, Timbre)

config = Config()
i18n = I18nAuto()
Expand Down Expand Up @@ -653,9 +616,7 @@ def formant_enabled(cbox, qfrency, tmbre, frmntapply, formantpreset, formant_ref
if (cbox):

DoFormant = True
cursor.execute("DELETE FROM formant_data")
cursor.execute("INSERT INTO formant_data (Quefrency, Timbre, DoFormant) VALUES (?, ?, ?)", (qfrency, tmbre, 1))
conn.commit()
CSVutil('csvdb/formanting.csv', 'w+', 'formanting', DoFormant, qfrency, tmbre)

#print(f"is checked? - {cbox}\ngot {DoFormant}")

Expand All @@ -672,9 +633,7 @@ def formant_enabled(cbox, qfrency, tmbre, frmntapply, formantpreset, formant_ref
else:

DoFormant = False
cursor.execute("DELETE FROM formant_data")
cursor.execute("INSERT INTO formant_data (Quefrency, Timbre, DoFormant) VALUES (?, ?, ?)", (qfrency, tmbre, int(DoFormant)))
conn.commit()
CSVutil('csvdb/formanting.csv', 'w+', 'formanting', DoFormant, qfrency, tmbre)

#print(f"is checked? - {cbox}\ngot {DoFormant}")
return (
Expand All @@ -692,9 +651,7 @@ def formant_apply(qfrency, tmbre):
Quefrency = qfrency
Timbre = tmbre
DoFormant = True
cursor.execute("DELETE FROM formant_data")
cursor.execute("INSERT INTO formant_data (Quefrency, Timbre, DoFormant) VALUES (?, ?, ?)", (qfrency, tmbre, 1))
conn.commit()
CSVutil('csvdb/formanting.csv', 'w+', 'formanting', DoFormant, qfrency, tmbre)

return ({"value": Quefrency, "__type__": "update"}, {"value": Timbre, "__type__": "update"})

Expand Down Expand Up @@ -993,8 +950,7 @@ def click_train(
if_save_every_weights18,
version19,
):
cursor.execute("DELETE FROM stop_train")
conn.commit()
CSVutil('csvdb/stop.csv', 'w+', 'formanting', False)
# 生成filelist
exp_dir = "%s/logs/%s" % (now_dir, exp_dir1)
os.makedirs(exp_dir, exist_ok=True)
Expand Down Expand Up @@ -1576,18 +1532,14 @@ def cli_infer(com):
DoFormant = False
Quefrency = 0.0
Timbre = 0.0
cursor.execute("DELETE FROM formant_data")
cursor.execute("INSERT INTO formant_data (Quefrency, Timbre, DoFormant) VALUES (?, ?, ?)", (Quefrency, Timbre, 0))
conn.commit()
CSVutil('csvdb/formanting.csv', 'w+', 'formanting', DoFormant, Quefrency, Timbre)

else:
DoFormant = True
Quefrency = float(com[15])
Timbre = float(com[16])
cursor.execute("DELETE FROM formant_data")
cursor.execute("INSERT INTO formant_data (Quefrency, Timbre, DoFormant) VALUES (?, ?, ?)", (Quefrency, Timbre, 1))
conn.commit()

CSVutil('csvdb/formanting.csv', 'w+', 'formanting', DoFormant, Quefrency, Timbre)

print("Mangio-RVC-Fork Infer-CLI: Starting the inference...")
vc_data = get_vc(model_name, protection_amnt, protect1)
print(vc_data)
Expand Down Expand Up @@ -1945,8 +1897,7 @@ def match_index(sid0):
def stoptraining(mim):
if int(mim) == 1:

cursor.execute("INSERT INTO stop_train (stop) VALUES (?)", (True,))
conn.commit()
CSVutil('csvdb/stop.csv', 'w+', 'stop', 'True')
#p.terminate()
#p.kill()
try:
Expand Down
42 changes: 30 additions & 12 deletions my_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import random

import sqlite3
import csv

platform_stft_mapping = {
'linux': 'stftpitchshift',
Expand All @@ -19,23 +19,45 @@
stft = platform_stft_mapping.get(sys.platform)
# praatEXE = join('.',os.path.abspath(os.getcwd()) + r"\Praat.exe")

def CSVutil(file, rw, type, *args):
if type == 'formanting':
if rw == 'r':
with open(file) as fileCSVread:
csv_reader = list(csv.reader(fileCSVread))
return (
csv_reader[0][0], csv_reader[0][1], csv_reader[0][2]
) if csv_reader is not None else (lambda: exec('raise ValueError("No data")'))()
else:
if args:
doformnt = args[0]
else:
doformnt = False
qfr = args[1] if len(args) > 1 else 1.0
tmb = args[2] if len(args) > 2 else 1.0
with open(file, rw, newline='') as fileCSVwrite:
csv_writer = csv.writer(fileCSVwrite, delimiter=',')
csv_writer.writerow([doformnt, qfr, tmb])
elif type == 'stop':
stop = args[0] if args else False
with open(file, rw, newline='') as fileCSVwrite:
csv_writer = csv.writer(fileCSVwrite, delimiter=',')
csv_writer.writerow([stop])

def load_audio(file, sr, DoFormant, Quefrency, Timbre):
converted = False
DoFormant, Quefrency, Timbre = CSVutil('csvdb/formanting.csv', 'r', 'formanting')
try:
conn = sqlite3.connect('TEMP/db:cachedb?mode=memory&cache=shared', check_same_thread=False)
cursor = conn.cursor()
# 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(" ")
) # 防止小白拷路径头尾带了空格和"和回车
file_formanted = file.strip(" ").strip('"').strip("\n").strip('"').strip(" ")
cursor.execute("SELECT Quefrency, Timbre, DoFormant FROM formant_data")
Quefrency, Timbre, DoFormant = cursor.fetchone()

#print(f"dofor={bool(DoFormant)} timbr={Timbre} quef={Quefrency}\n")
if bool(DoFormant):

if (lambda DoFormant: True if DoFormant.lower() == 'true' else (False if DoFormant.lower() == 'false' else DoFormant))(DoFormant):
numerator = round(random.uniform(1,4), 4)
# 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))
Expand Down Expand Up @@ -89,7 +111,8 @@ def load_audio(file, sr, DoFormant, Quefrency, Timbre):
)
)


try: os.remove("%sFORMANTED_%s.wav" % (file_formanted, str(numerator)))
except Exception: pass; print("couldn't remove formanted type of file")

else:
out, _ = (
Expand All @@ -102,14 +125,9 @@ def load_audio(file, sr, DoFormant, Quefrency, Timbre):
except Exception as e:
raise RuntimeError(f"Failed to load audio: {e}")

if DoFormant:
try: os.remove("%sFORMANTED_%s.wav" % (file_formanted, str(numerator)))
except Exception: pass; print("couldn't remove formanted type of file")

if converted:
try: os.remove(file_formanted)
except Exception: pass; print("couldn't remove converted type of file")
converted = False

conn.close()
return np.frombuffer(out, np.float32).flatten()
Loading

0 comments on commit e2b3aa0

Please sign in to comment.