Skip to content

Instantly share code, notes, and snippets.

@Partoo
Created May 31, 2024 04:19
Show Gist options
  • Select an option

  • Save Partoo/ce1927ab961cfe6aa73190cb9ef0c657 to your computer and use it in GitHub Desktop.

Select an option

Save Partoo/ce1927ab961cfe6aa73190cb9ef0c657 to your computer and use it in GitHub Desktop.
ChatTTS app.py Demo
import os
import numpy as np
import platform
import subprocess
import wave
import torch
from ChatTTS import Chat
import argparse
def save_audio_to_wav(audio_data, file_path, sample_rate=24000):
# 将音频数据转换为16位PCM格式
audio_data = (audio_data / np.max(np.abs(audio_data)) * 32767).astype(np.int16)
with wave.open(file_path, "wb") as wf:
wf.setnchannels(1) # 单通道
wf.setsampwidth(2) # 16位
wf.setframerate(sample_rate)
wf.writeframes(audio_data.tobytes())
def open_folder(path):
"""
Open the folder at the specified path in the default file explorer.
"""
if platform.system() == "Windows":
# For Windows, use 'explorer'.
subprocess.run(['explorer', path], check=True)
elif platform.system() == "Darwin":
# For macOS, use 'open' to open Finder at the given path.
subprocess.run(['open', path], check=True)
else:
# Optionally, handle Linux systems with 'xdg-open'
subprocess.run(['xdg-open', path], check=True)
# Setup argument parser
parser = argparse.ArgumentParser(description="Generate TTS with specific speaker settings.")
parser.add_argument('--rand', action='store_true', help='Randomly generate speaker embedding.')
args = parser.parse_args()
# Initialize and load the TTS model
chat = Chat()
chat.load_models()
# Ensure output directory exists
output_dir = "./output"
os.makedirs(output_dir, exist_ok=True)
# Determine speaker embedding
speaker_embedding_path = './output/speaker_embedding.pt'
if args.rand or not os.path.exists(speaker_embedding_path):
# Generate random speaker
std, mean = torch.load('./ChatTTS/assets/spk_stat.pt').chunk(2)
rand_spk = torch.randn(768) * std + mean
torch.save(rand_spk, speaker_embedding_path)
print("Generated and saved new speaker embedding.")
else:
# Load existing speaker
rand_spk = torch.load(speaker_embedding_path)
print("Loaded existing speaker embedding.")
# Parameters for inference
params_infer_code = {
'spk_emb': rand_spk,
'temperature': 0.5,
'top_P': 0.8,
'top_K': 40
}
params_refine_text = {
'prompt': '[oral_2][laugh_0][break_4]' # 更自然的口语化,适度的笑声和较长的停顿
}
# Adjusted text content, combined with elements of Children's Day
texts = """
小朋友们,
儿童节快乐。[uv_break]
今天你们有没有收到很多礼物呢,
让我们永远保持童真,[uv_break]
永远年轻,永远夜里尿炕[laugh]。
""".replace('\n', '')
# Use the model for inference
audio_output = chat.infer(texts, params_infer_code=params_infer_code)
# Save the file
output_path = os.path.join(output_dir, "out.wav")
save_audio_to_wav(audio_output, output_path)
print(f"音频文件已保存到:{output_path}")
# Open the output directory
open_folder(output_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment