Skip to content

Commit

Permalink
fix(test): int16 rms overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Jul 10, 2024
1 parent b517b38 commit 580463c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
21 changes: 10 additions & 11 deletions tests/#521.py
Original file line number Diff line number Diff line change
Expand Up @@ -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流式处理
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions tools/audio/np.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 580463c

Please sign in to comment.