voice-changer/server/voice_changer/VoiceChanger.py

538 lines
21 KiB
Python
Raw Normal View History

2023-04-28 00:39:51 +03:00
from typing import Any, Union, cast
2023-05-06 22:18:18 +03:00
import socketio
2023-04-10 18:21:17 +03:00
from const import TMP_DIR, ModelType
2022-12-31 10:08:14 +03:00
import torch
2023-01-28 09:56:56 +03:00
import os
import traceback
2022-12-31 10:08:14 +03:00
import numpy as np
2023-04-10 18:21:17 +03:00
from dataclasses import dataclass, asdict, field
2023-02-18 14:53:15 +03:00
import resampy
2023-02-10 18:59:44 +03:00
from voice_changer.IORecorder import IORecorder
2023-05-06 22:18:18 +03:00
from voice_changer.Local.AudioDeviceList import ServerAudioDevice, list_audio_device
2023-04-28 00:39:51 +03:00
from voice_changer.utils.LoadModelParams import LoadModelParams
from voice_changer.utils.Timer import Timer
from voice_changer.utils.VoiceChangerModel import VoiceChangerModel, AudioInOut
2023-05-03 07:14:00 +03:00
from Exceptions import (
2023-05-04 11:15:53 +03:00
DeviceChangingException,
2023-05-03 07:14:00 +03:00
HalfPrecisionChangingException,
NoModeLoadedException,
2023-05-04 11:15:53 +03:00
NotEnoughDataExtimateF0,
2023-05-03 07:14:00 +03:00
ONNXInputArgumentException,
)
2023-04-27 17:38:25 +03:00
from voice_changer.utils.VoiceChangerParams import VoiceChangerParams
2023-05-06 22:18:18 +03:00
import pyaudio
import threading
import struct
import time
2023-04-10 03:28:00 +03:00
2023-04-27 17:38:25 +03:00
providers = [
"OpenVINOExecutionProvider",
"CUDAExecutionProvider",
"DmlExecutionProvider",
"CPUExecutionProvider",
]
2023-01-28 09:56:56 +03:00
STREAM_INPUT_FILE = os.path.join(TMP_DIR, "in.wav")
STREAM_OUTPUT_FILE = os.path.join(TMP_DIR, "out.wav")
2023-02-12 06:25:57 +03:00
2023-01-08 10:18:20 +03:00
@dataclass
2023-04-27 17:38:25 +03:00
class VoiceChangerSettings:
inputSampleRate: int = 48000 # 48000 or 24000
2023-01-28 09:56:56 +03:00
crossFadeOffsetRate: float = 0.1
crossFadeEndRate: float = 0.9
crossFadeOverlapSize: int = 4096
2023-02-20 01:14:05 +03:00
recordIO: int = 0 # 0:off, 1:on
2023-05-06 22:18:18 +03:00
serverAudioInputDevices: list[ServerAudioDevice] = field(default_factory=lambda: [])
serverAudioOutputDevices: list[ServerAudioDevice] = field(
default_factory=lambda: []
)
enableServerAudio: int = 0 # 0:off, 1:on
serverAudioStated: int = 0 # 0:off, 1:on
serverInputAudioSampleRate: int = 48000
serverOutputAudioSampleRate: int = 48000
serverInputAudioBufferSize: int = 1024 * 24
serverOutputAudioBufferSize: int = 1024 * 24
serverInputDeviceId: int = -1
serverOutputDeviceId: int = -1
serverReadChunkSize: int = 256
performance: list[int] = field(default_factory=lambda: [0, 0, 0, 0])
2023-02-10 18:59:44 +03:00
2023-01-08 10:18:20 +03:00
# ↓mutableな物だけ列挙
2023-04-10 18:21:17 +03:00
intData: list[str] = field(
2023-05-06 22:18:18 +03:00
default_factory=lambda: [
"inputSampleRate",
"crossFadeOverlapSize",
"recordIO",
"enableServerAudio",
"serverAudioStated",
"serverInputAudioSampleRate",
"serverOutputAudioSampleRate",
"serverInputAudioBufferSize",
"serverOutputAudioBufferSize",
"serverInputDeviceId",
"serverOutputDeviceId",
"serverReadChunkSize",
]
2023-04-10 18:21:17 +03:00
)
floatData: list[str] = field(
default_factory=lambda: ["crossFadeOffsetRate", "crossFadeEndRate"]
)
2023-04-27 17:38:25 +03:00
strData: list[str] = field(default_factory=lambda: [])
2023-01-08 10:18:20 +03:00
2023-01-28 09:56:56 +03:00
2023-05-06 22:18:18 +03:00
def serverLocal(_vc):
vc: VoiceChanger = _vc
audio = pyaudio.PyAudio()
def createAudioInput(deviceId: int, sampleRate: int, bufferSize: int):
audio_input_stream = audio.open(
format=pyaudio.paInt16,
channels=1,
rate=sampleRate,
# frames_per_buffer=32768,
frames_per_buffer=bufferSize,
input_device_index=deviceId,
input=True,
)
return audio_input_stream
def createAudioOutput(deviceId: int, sampleRate: int, bufferSize: int):
audio_output_stream = audio.open(
format=pyaudio.paInt16,
channels=1,
rate=sampleRate,
# frames_per_buffer=32768,
frames_per_buffer=bufferSize,
output_device_index=deviceId,
output=True,
)
return audio_output_stream
currentInputDeviceId = -1
currentInputSampleRate = -1
currentInputBufferSize = -1
currentOutputDeviceId = -1
currentOutputSampleRate = -1
currentOutputBufferSize = -1
audio_input_stream = None
audio_output_stream = None
2023-05-08 23:04:34 +03:00
showPerformanceTime = 0
2023-05-06 22:18:18 +03:00
while True:
if (
vc.settings.enableServerAudio == 0
or vc.settings.serverAudioStated == 0
or vc.settings.serverInputDeviceId == -1
or vc.settings.serverOutputDeviceId == -1
):
time.sleep(2)
else:
if (
currentInputDeviceId != vc.settings.serverInputDeviceId
or currentInputSampleRate != vc.settings.serverInputAudioSampleRate
or currentInputBufferSize != vc.settings.serverInputAudioBufferSize
):
currentInputDeviceId = vc.settings.serverInputDeviceId
currentInputSampleRate = vc.settings.serverInputAudioSampleRate
currentInputBufferSize = vc.settings.serverInputAudioBufferSize
if audio_input_stream is not None:
audio_input_stream.close()
audio_input_stream = createAudioInput(
currentInputDeviceId,
currentInputSampleRate,
currentInputBufferSize,
)
if (
currentOutputDeviceId != vc.settings.serverOutputDeviceId
or currentOutputSampleRate != vc.settings.serverOutputAudioSampleRate
or currentOutputBufferSize != vc.settings.serverOutputAudioBufferSize
):
currentOutputDeviceId = vc.settings.serverOutputDeviceId
currentOutputSampleRate = vc.settings.serverOutputAudioSampleRate
currentOutputBufferSize = vc.settings.serverOutputAudioBufferSize
if audio_output_stream is not None:
audio_output_stream.close()
audio_output_stream = createAudioOutput(
currentOutputDeviceId,
currentOutputSampleRate,
currentOutputBufferSize,
)
2023-05-08 23:04:34 +03:00
sampleNum = vc.settings.serverReadChunkSize * 128
in_wav = audio_input_stream.read(sampleNum, exception_on_overflow=False)
readNum = len(in_wav) // struct.calcsize("<h")
unpackedData = np.array(struct.unpack("<%sh" % readNum, in_wav)).astype(
np.int16
2023-05-06 22:18:18 +03:00
)
with Timer("all_inference_time") as t:
out_wav, times = vc.on_request(unpackedData)
all_inference_time = t.secs
performance = [all_inference_time] + times
2023-05-08 23:04:34 +03:00
performance = [round(x * 1000) for x in performance]
2023-05-06 22:18:18 +03:00
vc.settings.performance = performance
2023-05-08 23:04:34 +03:00
currentTime = time.time()
if currentTime - showPerformanceTime > 5:
print(sampleNum, readNum, performance)
showPerformanceTime = currentTime
2023-05-06 22:18:18 +03:00
audio_output_stream.write(out_wav.tobytes())
2023-04-27 17:38:25 +03:00
class VoiceChanger:
2023-04-10 02:18:14 +03:00
settings: VoiceChangerSettings
voiceChanger: VoiceChangerModel
2023-04-28 00:39:51 +03:00
ioRecorder: IORecorder
sola_buffer: AudioInOut
2023-05-06 22:18:18 +03:00
namespace: socketio.AsyncNamespace | None = None
2023-01-08 10:18:20 +03:00
2023-04-27 17:38:25 +03:00
def __init__(self, params: VoiceChangerParams):
2023-01-08 10:18:20 +03:00
# 初期化
2023-04-10 02:18:14 +03:00
self.settings = VoiceChangerSettings()
2023-01-10 16:49:16 +03:00
self.onnx_session = None
2023-04-28 00:39:51 +03:00
self.currentCrossFadeOffsetRate = 0.0
self.currentCrossFadeEndRate = 0.0
self.currentCrossFadeOverlapSize = 0 # setting
self.crossfadeSize = 0 # calculated
2023-01-28 09:56:56 +03:00
2023-04-10 18:21:17 +03:00
self.voiceChanger = None
2023-04-28 00:39:51 +03:00
self.modelType: ModelType | None = None
2023-04-10 18:21:17 +03:00
self.params = params
self.gpu_num = torch.cuda.device_count()
self.prev_audio = np.zeros(4096)
2023-04-27 17:38:25 +03:00
self.mps_enabled: bool = (
getattr(torch.backends, "mps", None) is not None
and torch.backends.mps.is_available()
)
2023-04-10 18:21:17 +03:00
2023-05-06 22:18:18 +03:00
audioinput, audiooutput = list_audio_device()
self.settings.serverAudioInputDevices = audioinput
self.settings.serverAudioOutputDevices = audiooutput
thread = threading.Thread(target=serverLocal, args=(self,))
thread.start()
2023-04-27 17:38:25 +03:00
print(
f"VoiceChanger Initialized (GPU_NUM:{self.gpu_num}, mps_enabled:{self.mps_enabled})"
)
2023-04-10 18:21:17 +03:00
def switchModelType(self, modelType: ModelType):
2023-04-28 00:39:51 +03:00
if hasattr(self, "voiceChanger") and self.voiceChanger is not None:
2023-04-10 18:21:17 +03:00
# return {"status": "ERROR", "msg": "vc is already selected. currently re-select is not implemented"}
del self.voiceChanger
self.voiceChanger = None
self.modelType = modelType
2023-03-13 15:07:35 +03:00
if self.modelType == "MMVCv15":
2023-03-08 03:48:50 +03:00
from voice_changer.MMVCv15.MMVCv15 import MMVCv15
2023-04-27 17:38:25 +03:00
2023-04-10 03:34:03 +03:00
self.voiceChanger = MMVCv15() # type: ignore
2023-03-13 15:07:35 +03:00
elif self.modelType == "MMVCv13":
2023-03-10 19:56:10 +03:00
from voice_changer.MMVCv13.MMVCv13 import MMVCv13
2023-04-27 17:38:25 +03:00
2023-03-10 19:56:10 +03:00
self.voiceChanger = MMVCv13()
2023-04-01 21:17:32 +03:00
elif self.modelType == "so-vits-svc-40v2":
2023-03-10 19:56:10 +03:00
from voice_changer.SoVitsSvc40v2.SoVitsSvc40v2 import SoVitsSvc40v2
2023-04-27 17:38:25 +03:00
2023-04-10 18:21:17 +03:00
self.voiceChanger = SoVitsSvc40v2(self.params)
2023-03-30 05:11:41 +03:00
elif self.modelType == "so-vits-svc-40" or self.modelType == "so-vits-svc-40_c":
2023-03-18 19:43:36 +03:00
from voice_changer.SoVitsSvc40.SoVitsSvc40 import SoVitsSvc40
2023-04-27 17:38:25 +03:00
2023-04-10 18:21:17 +03:00
self.voiceChanger = SoVitsSvc40(self.params)
2023-03-24 02:44:06 +03:00
elif self.modelType == "DDSP-SVC":
from voice_changer.DDSP_SVC.DDSP_SVC import DDSP_SVC
2023-04-27 17:38:25 +03:00
2023-04-10 18:21:17 +03:00
self.voiceChanger = DDSP_SVC(self.params)
2023-04-05 20:31:10 +03:00
elif self.modelType == "RVC":
from voice_changer.RVC.RVC import RVC
2023-04-27 17:38:25 +03:00
2023-04-10 18:21:17 +03:00
self.voiceChanger = RVC(self.params)
2023-03-07 19:46:08 +03:00
else:
2023-03-08 03:48:50 +03:00
from voice_changer.MMVCv13.MMVCv13 import MMVCv13
2023-04-27 17:38:25 +03:00
2023-03-07 19:46:08 +03:00
self.voiceChanger = MMVCv13()
2023-04-10 18:21:17 +03:00
return {"status": "OK", "msg": "vc is switched."}
2022-12-31 10:08:14 +03:00
2023-04-10 18:21:17 +03:00
def getModelType(self):
2023-04-28 00:39:51 +03:00
if self.modelType is not None:
2023-04-10 18:21:17 +03:00
return {"status": "OK", "vc": self.modelType}
else:
return {"status": "OK", "vc": "none"}
2023-01-04 20:28:36 +03:00
2023-04-28 00:39:51 +03:00
def loadModel(self, props: LoadModelParams):
2023-04-14 05:03:52 +03:00
try:
2023-04-16 03:56:12 +03:00
return self.voiceChanger.loadModel(props)
2023-04-14 05:03:52 +03:00
except Exception as e:
2023-04-28 07:49:40 +03:00
print(traceback.format_exc())
2023-04-14 05:03:52 +03:00
print("[Voice Changer] Model Load Error! Check your model is valid.", e)
return {"status": "NG"}
2022-12-31 10:08:14 +03:00
2023-01-07 18:25:21 +03:00
def get_info(self):
2023-01-08 10:18:20 +03:00
data = asdict(self.settings)
2023-04-10 18:21:17 +03:00
if hasattr(self, "voiceChanger"):
data.update(self.voiceChanger.get_info())
2023-01-08 10:18:20 +03:00
return data
2023-05-06 22:18:18 +03:00
def get_performance(self):
return self.settings.performance
2023-04-10 03:28:00 +03:00
def update_settings(self, key: str, val: Any):
if key in self.settings.intData:
2023-01-08 10:18:20 +03:00
setattr(self.settings, key, int(val))
2023-01-08 15:19:44 +03:00
if key == "crossFadeOffsetRate" or key == "crossFadeEndRate":
self.crossfadeSize = 0
2023-02-14 23:02:51 +03:00
if key == "recordIO" and val == 1:
if hasattr(self, "ioRecorder"):
self.ioRecorder.close()
2023-04-27 17:38:25 +03:00
self.ioRecorder = IORecorder(
STREAM_INPUT_FILE, STREAM_OUTPUT_FILE, self.settings.inputSampleRate
)
2023-02-15 01:18:05 +03:00
if key == "recordIO" and val == 0:
if hasattr(self, "ioRecorder"):
self.ioRecorder.close()
2023-02-16 21:03:21 +03:00
pass
if key == "recordIO" and val == 2:
if hasattr(self, "ioRecorder"):
self.ioRecorder.close()
2023-01-08 10:18:20 +03:00
elif key in self.settings.floatData:
setattr(self.settings, key, float(val))
elif key in self.settings.strData:
setattr(self.settings, key, str(val))
2023-01-08 03:45:58 +03:00
else:
2023-04-10 18:21:17 +03:00
if hasattr(self, "voiceChanger"):
ret = self.voiceChanger.update_settings(key, val)
2023-04-28 00:39:51 +03:00
if ret is False:
2023-04-10 18:21:17 +03:00
print(f"{key} is not mutable variable or unknown variable!")
else:
2023-04-28 00:39:51 +03:00
print("voice changer is not initialized!")
2023-01-10 18:59:09 +03:00
return self.get_info()
2023-01-08 10:18:20 +03:00
def _generate_strength(self, crossfadeSize: int):
2023-04-27 17:38:25 +03:00
if (
self.crossfadeSize != crossfadeSize
or self.currentCrossFadeOffsetRate != self.settings.crossFadeOffsetRate
or self.currentCrossFadeEndRate != self.settings.crossFadeEndRate
or self.currentCrossFadeOverlapSize != self.settings.crossFadeOverlapSize
):
self.crossfadeSize = crossfadeSize
2023-01-10 18:59:09 +03:00
self.currentCrossFadeOffsetRate = self.settings.crossFadeOffsetRate
self.currentCrossFadeEndRate = self.settings.crossFadeEndRate
self.currentCrossFadeOverlapSize = self.settings.crossFadeOverlapSize
2023-01-11 19:05:38 +03:00
cf_offset = int(crossfadeSize * self.settings.crossFadeOffsetRate)
cf_end = int(crossfadeSize * self.settings.crossFadeEndRate)
2023-01-04 20:28:36 +03:00
cf_range = cf_end - cf_offset
percent = np.arange(cf_range) / cf_range
2023-01-28 09:56:56 +03:00
np_prev_strength = np.cos(percent * 0.5 * np.pi) ** 2
np_cur_strength = np.cos((1 - percent) * 0.5 * np.pi) ** 2
2023-01-04 20:28:36 +03:00
2023-04-27 17:38:25 +03:00
self.np_prev_strength = np.concatenate(
[
np.ones(cf_offset),
np_prev_strength,
np.zeros(crossfadeSize - cf_offset - len(np_prev_strength)),
]
)
self.np_cur_strength = np.concatenate(
[
np.zeros(cf_offset),
np_cur_strength,
np.ones(crossfadeSize - cf_offset - len(np_cur_strength)),
]
)
print(
f"Generated Strengths: for prev:{self.np_prev_strength.shape}, for cur:{self.np_cur_strength.shape}"
)
2023-01-28 09:56:56 +03:00
2023-01-04 20:28:36 +03:00
# ひとつ前の結果とサイズが変わるため、記録は消去する。
2023-04-28 00:39:51 +03:00
if hasattr(self, "np_prev_audio1") is True:
delattr(self, "np_prev_audio1")
2023-04-28 00:39:51 +03:00
if hasattr(self, "sola_buffer") is True:
2023-04-14 05:03:52 +03:00
del self.sola_buffer
2023-04-14 03:18:34 +03:00
# receivedData: tuple of short
2023-04-27 17:38:25 +03:00
def on_request(
self, receivedData: AudioInOut
) -> tuple[AudioInOut, list[Union[int, float]]]:
2023-04-14 22:58:56 +03:00
return self.on_request_sola(receivedData)
2023-04-14 03:18:34 +03:00
2023-04-27 17:38:25 +03:00
def on_request_sola(
self, receivedData: AudioInOut
) -> tuple[AudioInOut, list[Union[int, float]]]:
2023-04-17 03:45:12 +03:00
try:
processing_sampling_rate = self.voiceChanger.get_processing_sampling_rate()
2023-04-17 03:45:12 +03:00
# 前処理
with Timer("pre-process") as t:
if self.settings.inputSampleRate != processing_sampling_rate:
2023-04-27 17:38:25 +03:00
newData = cast(
AudioInOut,
resampy.resample(
receivedData,
self.settings.inputSampleRate,
processing_sampling_rate,
),
)
2023-04-17 03:45:12 +03:00
else:
newData = receivedData
2023-04-14 03:18:34 +03:00
2023-04-17 03:45:12 +03:00
sola_search_frame = int(0.012 * processing_sampling_rate)
# sola_search_frame = 0
block_frame = newData.shape[0]
crossfade_frame = min(self.settings.crossFadeOverlapSize, block_frame)
self._generate_strength(crossfade_frame)
2023-04-14 03:18:34 +03:00
2023-04-27 17:38:25 +03:00
data = self.voiceChanger.generate_input(
newData, block_frame, crossfade_frame, sola_search_frame
)
2023-04-17 03:45:12 +03:00
preprocess_time = t.secs
2023-04-14 03:18:34 +03:00
2023-04-17 03:45:12 +03:00
# 変換処理
with Timer("main-process") as t:
2023-04-14 03:18:34 +03:00
# Inference
audio = self.voiceChanger.inference(data)
2023-04-28 00:39:51 +03:00
if hasattr(self, "sola_buffer") is True:
2023-04-14 03:18:34 +03:00
np.set_printoptions(threshold=10000)
2023-04-28 00:39:51 +03:00
audio_offset = -1 * (
sola_search_frame + crossfade_frame + block_frame
)
audio = audio[audio_offset:]
a = 0
audio = audio[a:]
2023-04-14 03:18:34 +03:00
# SOLA algorithm from https://github.com/yxlllc/DDSP-SVC, https://github.com/liujing04/Retrieval-based-Voice-Conversion-WebUI
2023-04-27 17:38:25 +03:00
cor_nom = np.convolve(
audio[: crossfade_frame + sola_search_frame],
np.flip(self.sola_buffer),
"valid",
)
cor_den = np.sqrt(
np.convolve(
audio[: crossfade_frame + sola_search_frame] ** 2,
np.ones(crossfade_frame),
"valid",
)
+ 1e-3
)
2023-04-28 00:39:51 +03:00
sola_offset = int(np.argmax(cor_nom / cor_den))
sola_end = sola_offset + block_frame
output_wav = audio[sola_offset:sola_end].astype(np.float64)
2023-04-14 03:18:34 +03:00
output_wav[:crossfade_frame] *= self.np_cur_strength
output_wav[:crossfade_frame] += self.sola_buffer[:]
result = output_wav
else:
2023-04-18 01:17:50 +03:00
print("[Voice Changer] no sola buffer. (You can ignore this.)")
2023-04-14 03:18:34 +03:00
result = np.zeros(4096).astype(np.int16)
2023-04-27 17:38:25 +03:00
if (
2023-04-28 00:39:51 +03:00
hasattr(self, "sola_buffer") is True
2023-04-27 17:38:25 +03:00
and sola_offset < sola_search_frame
):
2023-04-28 00:39:51 +03:00
offset = -1 * (sola_search_frame + crossfade_frame - sola_offset)
end = -1 * (sola_search_frame - sola_offset)
sola_buf_org = audio[offset:end]
2023-04-14 03:18:34 +03:00
self.sola_buffer = sola_buf_org * self.np_prev_strength
else:
2023-04-27 17:38:25 +03:00
self.sola_buffer = audio[-crossfade_frame:] * self.np_prev_strength
2023-04-14 03:18:34 +03:00
# self.sola_buffer = audio[- crossfade_frame:]
2023-04-17 03:45:12 +03:00
mainprocess_time = t.secs
2023-04-14 03:18:34 +03:00
2023-04-17 03:45:12 +03:00
# 後処理
with Timer("post-process") as t:
result = result.astype(np.int16)
if self.settings.inputSampleRate != processing_sampling_rate:
2023-04-27 17:38:25 +03:00
outputData = cast(
AudioInOut,
resampy.resample(
result,
processing_sampling_rate,
self.settings.inputSampleRate,
).astype(np.int16),
)
2023-04-17 03:45:12 +03:00
else:
outputData = result
2023-04-14 03:18:34 +03:00
2023-04-17 03:45:12 +03:00
print_convert_processing(
2023-04-27 17:38:25 +03:00
f" Output data size of {result.shape[0]}/{processing_sampling_rate}hz {outputData.shape[0]}/{self.settings.inputSampleRate}hz"
)
2023-04-14 03:18:34 +03:00
2023-04-17 03:45:12 +03:00
if self.settings.recordIO == 1:
self.ioRecorder.writeInput(receivedData)
self.ioRecorder.writeOutput(outputData.tobytes())
2023-04-14 03:18:34 +03:00
2023-04-17 03:45:12 +03:00
# if receivedData.shape[0] != outputData.shape[0]:
# print(f"Padding, in:{receivedData.shape[0]} out:{outputData.shape[0]}")
# outputData = pad_array(outputData, receivedData.shape[0])
# # print_convert_processing(
# # f" Padded!, Output data size of {result.shape[0]}/{processing_sampling_rate}hz {outputData.shape[0]}/{self.settings.inputSampleRate}hz")
postprocess_time = t.secs
2023-04-14 03:18:34 +03:00
2023-04-27 17:38:25 +03:00
print_convert_processing(
f" [fin] Input/Output size:{receivedData.shape[0]},{outputData.shape[0]}"
)
2023-04-17 03:45:12 +03:00
perf = [preprocess_time, mainprocess_time, postprocess_time]
return outputData, perf
except NoModeLoadedException as e:
print("[Voice Changer] [Exception]", e)
return np.zeros(1).astype(np.int16), [0, 0, 0]
2023-04-18 21:06:45 +03:00
except ONNXInputArgumentException as e:
print("[Voice Changer] [Exception]", e)
return np.zeros(1).astype(np.int16), [0, 0, 0]
2023-05-03 07:14:00 +03:00
except HalfPrecisionChangingException as e:
print("[Voice Changer] Switching model configuration....", e)
return np.zeros(1).astype(np.int16), [0, 0, 0]
2023-05-04 11:15:53 +03:00
except NotEnoughDataExtimateF0 as e:
print("[Voice Changer] not enough data", e)
return np.zeros(1).astype(np.int16), [0, 0, 0]
except DeviceChangingException as e:
print("[Voice Changer] embedder:", e)
return np.zeros(1).astype(np.int16), [0, 0, 0]
2023-04-17 03:45:12 +03:00
except Exception as e:
print("VC PROCESSING!!!! EXCEPTION!!!", e)
print(traceback.format_exc())
return np.zeros(1).astype(np.int16), [0, 0, 0]
2023-04-14 03:18:34 +03:00
2023-04-13 02:00:28 +03:00
def export2onnx(self):
return self.voiceChanger.export2onnx()
2023-02-20 22:07:43 +03:00
2023-04-13 02:00:28 +03:00
##############
2023-04-27 17:38:25 +03:00
2023-04-30 20:34:01 +03:00
def merge_models(self, request: str):
self.voiceChanger.merge_models(request)
return self.get_info()
2023-04-27 17:38:25 +03:00
2023-04-10 03:28:00 +03:00
PRINT_CONVERT_PROCESSING: bool = False
# PRINT_CONVERT_PROCESSING = True
def print_convert_processing(mess: str):
2023-04-28 00:39:51 +03:00
if PRINT_CONVERT_PROCESSING is True:
print(mess)
def pad_array(arr: AudioInOut, target_length: int):
2023-03-10 19:56:10 +03:00
current_length = arr.shape[0]
if current_length >= target_length:
return arr
else:
pad_width = target_length - current_length
pad_left = pad_width // 2
pad_right = pad_width - pad_left
2023-04-27 17:38:25 +03:00
padded_arr = np.pad(
arr, (pad_left, pad_right), "constant", constant_values=(0, 0)
)
2023-03-10 19:56:10 +03:00
return padded_arr