371 lines
10 KiB
Python
371 lines
10 KiB
Python
import click
|
|
from loguru import logger
|
|
import pydub
|
|
from tqdm import tqdm
|
|
|
|
logger.remove()
|
|
logger.add(lambda msg: tqdm.write(msg, end=""), colorize=True)
|
|
|
|
|
|
@click.group()
|
|
def cli():
|
|
pass
|
|
|
|
|
|
@cli.command()
|
|
@click.option(
|
|
"--backend",
|
|
default="crispasr",
|
|
type=click.Choice(["crispasr", "qtr"], case_sensitive=False),
|
|
help="The backend to use for speech synthesis",
|
|
)
|
|
@click.option(
|
|
"--model", default="./qwen3tts", help="The model to use for speech recognition"
|
|
)
|
|
@click.option(
|
|
"--output", default="./output.wav", help="The output file for the generated speech"
|
|
)
|
|
@click.option(
|
|
"--output-dir",
|
|
default="./outputs",
|
|
help="The output directory for the generated speech",
|
|
)
|
|
@click.option(
|
|
"--gpu-backend",
|
|
default="auto",
|
|
help="The GPU backend to use for speech recognition",
|
|
)
|
|
@click.option(
|
|
"--instruct",
|
|
default="用符合文本应读语气说",
|
|
help="The instruction to use for speech recognition",
|
|
)
|
|
@click.option(
|
|
"executable",
|
|
"--executable",
|
|
default="./crispasr",
|
|
help="The executable to use for speech recognition",
|
|
)
|
|
@click.option(
|
|
"--codec-model",
|
|
help="The codec model to use for speech recognition",
|
|
)
|
|
@click.option(
|
|
"--ws-uri",
|
|
default="ws://127.0.0.1:3001/api/tts/stream",
|
|
help="The websocket uri used by qtr backend",
|
|
)
|
|
@click.option(
|
|
"--sample-rate",
|
|
default=24000,
|
|
type=int,
|
|
help="Sample rate used by qtr backend when packaging wav",
|
|
)
|
|
@click.option(
|
|
"--strict",
|
|
is_flag=True,
|
|
help="Use strict parsing mode,which just keep Chinese, English and numbers",
|
|
)
|
|
@click.option(
|
|
"--split-length",
|
|
default=450,
|
|
type=int,
|
|
help="The max length of each text chunk sent to TTS backend",
|
|
)
|
|
@click.option(
|
|
"--mode",
|
|
default="mode",
|
|
type=click.Choice(["stream", "http"], case_sensitive=False),
|
|
help="The mode to use for speech synthesis",
|
|
)
|
|
@click.option(
|
|
"--form-chunk",
|
|
help="Form chunk to say, 1 for first chunk, 2 for second chunk, etc.",
|
|
)
|
|
@click.option(
|
|
"--to-chunk",
|
|
help="To chunk to say, 1 for first chunk, 2 for second chunk, etc.",
|
|
)
|
|
@click.argument("input_file")
|
|
def say(
|
|
backend: str,
|
|
model: str,
|
|
output: str,
|
|
input_file: str,
|
|
gpu_backend: str,
|
|
instruct: str,
|
|
executable: str,
|
|
output_dir: str,
|
|
codec_model: str,
|
|
ws_uri: str,
|
|
sample_rate: int,
|
|
strict: bool,
|
|
mode: str,
|
|
split_length: int,
|
|
form_chunk: int,
|
|
to_chunk: int,
|
|
):
|
|
with open(input_file, "r", encoding="utf-8") as f:
|
|
html = f.read()
|
|
from parser import TTSHTMLParser
|
|
|
|
logger.info("解析中...")
|
|
parser = TTSHTMLParser(html)
|
|
result = parser.parse(strict=strict)
|
|
logger.debug(f"解析结果: {result}")
|
|
match backend:
|
|
case "crispasr":
|
|
from backend import CrispASRCLIBackend
|
|
|
|
tts_backend = CrispASRCLIBackend(
|
|
model=model,
|
|
executable=executable,
|
|
gpu_backend=gpu_backend,
|
|
codec_model=codec_model,
|
|
)
|
|
|
|
case "qtr":
|
|
from backend import QTRTTSBackend
|
|
|
|
tts_backend = QTRTTSBackend(ws_uri=ws_uri, sample_rate=sample_rate)
|
|
from tqdm import tqdm
|
|
|
|
case _:
|
|
raise ValueError(f"Unsupported backend: {backend}")
|
|
|
|
for voice, text in tqdm(
|
|
result,
|
|
desc="合成语音",
|
|
unit="段",
|
|
total=len(result),
|
|
colour="green",
|
|
):
|
|
logger.info(f"正在合成: voice={voice}, text={text}", mode=mode)
|
|
u
|
|
tts_backend.smart_say(
|
|
from_chunk=form_chunk,
|
|
to_chunk=to_chunk,
|
|
voice=voice,
|
|
text=text,
|
|
instruct=instruct,
|
|
split_length=split_length,
|
|
output_file_prefix=f"{output_dir}/{voice}_{text[0:9]}",
|
|
)
|
|
|
|
|
|
@cli.command()
|
|
@click.argument("input_dir")
|
|
@click.argument("output_file")
|
|
@click.option(
|
|
"--add-silence",
|
|
default=0.5,
|
|
type=float,
|
|
help="Add silence between combined wav files in seconds",
|
|
)
|
|
@click.option("--format", default="wav", help="The output format, either wav or mp3")
|
|
def combine(input_dir: str, output_file: str, add_silence: float, format: str):
|
|
from files import FileHandler
|
|
|
|
handler = FileHandler(input_dir)
|
|
if format == "wav":
|
|
handler.combine_waves(output_file, add_silence=add_silence)
|
|
elif format == "mp3":
|
|
handler.combine_waves(output_file)
|
|
handler.wavs_to_mp3(output_file, output_file.replace(".wav", ".mp3"))
|
|
|
|
|
|
@cli.command()
|
|
@click.argument("input_file")
|
|
@click.argument("output_file")
|
|
def transform(input_file: str, output_file: str):
|
|
from files import FileHandler
|
|
|
|
handler = FileHandler(input_file)
|
|
handler.wavs_to_mp3(input_file, output_file)
|
|
|
|
|
|
@cli.command()
|
|
@click.argument("input_file")
|
|
@click.option(
|
|
"--bgm-dir",
|
|
default="./bgms",
|
|
help="The directory containing background music files",
|
|
)
|
|
@click.option(
|
|
"--bgm-volume",
|
|
default=-5,
|
|
type=int,
|
|
help="The volume of the background music in dB",
|
|
)
|
|
@click.option(
|
|
"--audio-refer",
|
|
default="input_file",
|
|
help="The audio file to use as reference for the final output",
|
|
)
|
|
@click.option(
|
|
"--mp3-bitrate",
|
|
default="64k",
|
|
help="Bitrate used when output file is mp3, e.g. 64k/96k/128k",
|
|
)
|
|
@click.option(
|
|
"--output-channels",
|
|
default="keep",
|
|
type=click.Choice(["keep", "mono", "stereo"], case_sensitive=False),
|
|
help="Output channels: keep original, force mono, or force stereo",
|
|
)
|
|
@click.option(
|
|
"--output-sample-rate",
|
|
default=0,
|
|
type=int,
|
|
help="Output sample rate in Hz, use 0 to keep original",
|
|
)
|
|
@click.argument("output_file")
|
|
def add_bgms(
|
|
input_file: str,
|
|
bgm_dir: str,
|
|
bgm_volume: int,
|
|
audio_refer: str,
|
|
mp3_bitrate: str,
|
|
output_channels: str,
|
|
output_sample_rate: int,
|
|
output_file: str,
|
|
):
|
|
from pathlib import Path
|
|
|
|
suffix = output_file.split(".")[-1]
|
|
if suffix not in ["wav", "mp3"]:
|
|
raise ValueError("Output file must have .wav or .mp3 suffix")
|
|
match suffix:
|
|
case "wav":
|
|
main_audio = pydub.AudioSegment.from_wav(input_file)
|
|
|
|
case "mp3":
|
|
main_audio = pydub.AudioSegment.from_mp3(input_file)
|
|
bgm_files = sorted(
|
|
[
|
|
f
|
|
for f in Path(bgm_dir).glob("*")
|
|
if f.suffix.lower() in [".wav", ".mp3", ".ogg"]
|
|
],
|
|
key=lambda f: f.stat().st_mtime,
|
|
)
|
|
if not bgm_files:
|
|
raise ValueError("No supported bgm files found in bgm directory")
|
|
for bgm_file in bgm_files:
|
|
bgm_suffix = bgm_file.suffix.lower()
|
|
match bgm_suffix:
|
|
case ".wav":
|
|
bgm_audio = pydub.AudioSegment.from_wav(bgm_file)
|
|
case ".mp3":
|
|
bgm_audio = pydub.AudioSegment.from_mp3(bgm_file)
|
|
case ".ogg":
|
|
bgm_audio = pydub.AudioSegment.from_ogg(bgm_file)
|
|
case _:
|
|
logger.warning(
|
|
f"Unsupported bgm format: {bgm_suffix}, skipping {bgm_file}"
|
|
)
|
|
continue
|
|
bgm_audio = bgm_audio - (20 - bgm_volume) # Adjust volume
|
|
if len(bgm_audio) < len(main_audio):
|
|
bgm_audio = bgm_audio * (len(main_audio) // len(bgm_audio) + 1)
|
|
bgm_audio = bgm_audio[: len(main_audio)]
|
|
match audio_refer:
|
|
case "input_file":
|
|
main_audio = main_audio.overlay(bgm_audio)
|
|
case "bgm":
|
|
main_audio = bgm_audio.overlay(main_audio)
|
|
case _:
|
|
raise ValueError(f"Unsupported audio refer: {audio_refer}")
|
|
|
|
match output_channels.lower():
|
|
case "mono":
|
|
main_audio = main_audio.set_channels(1)
|
|
case "stereo":
|
|
main_audio = main_audio.set_channels(2)
|
|
case "keep":
|
|
pass
|
|
case _:
|
|
raise ValueError(f"Unsupported output channels option: {output_channels}")
|
|
|
|
if output_sample_rate > 0:
|
|
main_audio = main_audio.set_frame_rate(output_sample_rate)
|
|
|
|
if suffix == "mp3":
|
|
main_audio.export(output_file, format="mp3", bitrate=mp3_bitrate)
|
|
else:
|
|
main_audio.export(output_file, format="wav")
|
|
|
|
|
|
@cli.command()
|
|
@click.option(
|
|
"--remove-silence",
|
|
default=0.3,
|
|
type=float,
|
|
help="Remove silence from the beginning and end of the audio",
|
|
)
|
|
@click.argument("input_file")
|
|
@click.argument("output_file")
|
|
def pretty(remove_silence: float, input_file: str, output_file: str):
|
|
if remove_silence > 0:
|
|
from pretty import remove_silence as remove_silence_func
|
|
|
|
remove_silence_func(
|
|
pydub.AudioSegment.from_wav(input_file), remove_silence
|
|
).export(output_file, format="wav")
|
|
|
|
|
|
@cli.command()
|
|
@click.argument("input_txt")
|
|
@click.argument("output_xml")
|
|
@click.option(
|
|
"--model",
|
|
default="qwen3:latest",
|
|
help="The model to use for text transformation, e.g. qwen3:4b or qwen3:latest",
|
|
)
|
|
@click.option(
|
|
"--context-window",
|
|
default=2048,
|
|
type=int,
|
|
help="The context window size in characters for the text transformation model",
|
|
)
|
|
@click.option(
|
|
"--prompt",
|
|
default=None,
|
|
help="The prompt to use for text transformation, if not provided, a default prompt will be used",
|
|
)
|
|
@click.option(
|
|
"--speakers",
|
|
default=None,
|
|
help="Comma-separated list of speakers to use for text transformation, if not provided, a default list will be used",
|
|
type=list,
|
|
)
|
|
@click.option(
|
|
"--transformer",
|
|
default="TXTTransformer",
|
|
type=click.Choice(["TXTTransformer", "OllamaTransformer"]),
|
|
)
|
|
def txt_to_xml(
|
|
input_txt: str,
|
|
output_xml: str,
|
|
model: str,
|
|
context_window: int,
|
|
prompt: str,
|
|
speakers: list[str],
|
|
transformer: str,
|
|
):
|
|
from transform import TXTTransformer, OllamaTransformer, AutoTransformer
|
|
|
|
with open(input_txt, "r", encoding="utf-8") as f:
|
|
text = f.read()
|
|
if transformer == "OllamaTransformer":
|
|
transformer = OllamaTransformer(
|
|
model=model, context_window=context_window, prompt=prompt, speakers=speakers
|
|
)
|
|
elif transformer == "AutoTransformer":
|
|
transformer = AutoTransformer(speakers=speakers)
|
|
else:
|
|
transformer = TXTTransformer()
|
|
xml = transformer.transform(text)
|
|
with open(output_xml, "w", encoding="utf-8") as f:
|
|
f.write(xml)
|