1.0
This commit is contained in:
1 parent
dbda6edc00
commit
122c47dd20
15 files changed
+3765
-73
No files matched your search
+226
-1
@@ -1,5 +1,10 @@
|
||||
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()
|
||||
@@ -9,7 +14,10 @@ def cli():
|
||||
|
||||
@cli.command()
|
||||
@click.option(
|
||||
"--backend", default="crispasr", help="The backend to use for speech recognition"
|
||||
"--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"
|
||||
@@ -42,11 +50,34 @@ def cli():
|
||||
"--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.argument("input_file")
|
||||
def say(
|
||||
backend: str,
|
||||
@@ -58,7 +89,11 @@ def say(
|
||||
executable: str,
|
||||
output_dir: str,
|
||||
codec_model: str,
|
||||
ws_uri: str,
|
||||
sample_rate: int,
|
||||
strict: bool,
|
||||
mode: str,
|
||||
split_length: int,
|
||||
):
|
||||
with open(input_file, "r", encoding="utf-8") as f:
|
||||
html = f.read()
|
||||
@@ -85,6 +120,28 @@ def say(
|
||||
voice=voice,
|
||||
text=text,
|
||||
instruct=instruct,
|
||||
split_length=split_length,
|
||||
output_file_prefix=f"{output_dir}/{voice}_{text[0:9]}",
|
||||
)
|
||||
case "qtr":
|
||||
from backend import QTRTTSBackend
|
||||
|
||||
tts_backend = QTRTTSBackend(ws_uri=ws_uri, sample_rate=sample_rate)
|
||||
from tqdm import tqdm
|
||||
|
||||
for voice, text in tqdm(
|
||||
result,
|
||||
desc="合成语音",
|
||||
unit="段",
|
||||
total=len(result),
|
||||
colour="green",
|
||||
):
|
||||
logger.info(f"正在合成: voice={voice}, text={text}", mode=mode)
|
||||
tts_backend.smart_say(
|
||||
voice=voice,
|
||||
text=text,
|
||||
instruct=instruct,
|
||||
split_length=split_length,
|
||||
output_file_prefix=f"{output_dir}/{voice}_{text[0:9]}",
|
||||
)
|
||||
case _:
|
||||
@@ -114,3 +171,171 @@ def transform(input_file: str, output_file: str):
|
||||
|
||||
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.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)
|
||||
Reference in new issue
Block a user