MCPcopy Create free account
hub / github.com/SparkAudio/Spark-TTS / run_tts

Function run_tts

webui.py:51–91  ·  view source on GitHub ↗

Perform TTS inference and save the generated audio.

(
    text,
    model,
    prompt_text=None,
    prompt_speech=None,
    gender=None,
    pitch=None,
    speed=None,
    save_dir="example/results",
)

Source from the content-addressed store, hash-verified

49
50
51def run_tts(
52 text,
53 model,
54 prompt_text=None,
55 prompt_speech=None,
56 gender=None,
57 pitch=None,
58 speed=None,
59 save_dir="example/results",
60):
61 """Perform TTS inference and save the generated audio."""
62 logging.info(f"Saving audio to: {save_dir}")
63
64 if prompt_text is not None:
65 prompt_text = None if len(prompt_text) <= 1 else prompt_text
66
67 # Ensure the save directory exists
68 os.makedirs(save_dir, exist_ok=True)
69
70 # Generate unique filename using timestamp
71 timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
72 save_path = os.path.join(save_dir, f"{timestamp}.wav")
73
74 logging.info("Starting inference...")
75
76 # Perform inference and save the output audio
77 with torch.no_grad():
78 wav = model.inference(
79 text,
80 prompt_speech,
81 prompt_text,
82 gender,
83 pitch,
84 speed,
85 )
86
87 sf.write(save_path, wav, samplerate=16000)
88
89 logging.info(f"Audio saved at: {save_path}")
90
91 return save_path
92
93
94def build_ui(model_dir, device=0):

Callers 2

voice_cloneFunction · 0.70
voice_creationFunction · 0.70

Calls 1

inferenceMethod · 0.80

Tested by

no test coverage detected