From 580463ce220880c5229217dcb6b4a8e07e6d896b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 10 Jul 2024 15:39:49 +0900 Subject: [PATCH] fix(test): int16 rms overflow --- tests/#521.py | 21 ++++++++++----------- tools/audio/np.py | 9 ++++----- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/tests/#521.py b/tests/#521.py index 234743a19..a02827de9 100644 --- a/tests/#521.py +++ b/tests/#521.py @@ -24,26 +24,23 @@ # 计算rms # nan为噪声 !!! def calculate_rms(data): - # 数据清洗 方法1 - # data = data[~np.isnan(data)] - # 数据清洗 方法2 - data = np.nan_to_num(data, nan=0.0, posinf=0.0, neginf=0.0) - if len(data) == 0: - return np.nan # - # data = np.nan_to_num(data) - return np.sqrt(np.mean(np.square(data))) - + m = np.mean(np.square(data.astype(np.int32))) + if m < 0: + logger.warning("neg RM: %f", m) + else: + logger.info("RM: %f", m) + return np.sqrt(m) # 流式声音处理器 class AudioStreamer: # 流式写入 @staticmethod - def write(waveform): + def write(waveform: np.ndarray): global fail, logger rms = calculate_rms(waveform) if np.isnan(rms): fail = True - logger.warning("NAN RMS found") + logger.warning("NAN RMS found.") # ChatTTS流式处理 @@ -178,9 +175,11 @@ def start_writing(self, streamchat): spk_emb=rand_spk, # add sampled speaker temperature=0.0001, # using custom temperature prompt="[speed_0]", + show_tqdm=False, ) params_refine_text = ChatTTS.Chat.RefineTextParams( prompt="[oral_2][laugh_0][break_6]", + show_tqdm=False, ) # 获取ChatTTS 流式推理generator diff --git a/tools/audio/np.py b/tools/audio/np.py index b2cfb0fc9..7433f9e93 100644 --- a/tools/audio/np.py +++ b/tools/audio/np.py @@ -1,12 +1,11 @@ +import math + import numpy as np from numba import jit @jit def float_to_int16(audio: np.ndarray) -> np.ndarray: - """ - This function will destroy audio, use only once. - """ - am = np.abs(audio).max() * 32768 - am = 32767 * 32768 / am + am = int(math.ceil(float(np.abs(audio).max()))*32768) + am = 32767 * 32768 // am return np.multiply(audio, am).astype(np.int16)