Ai_Assistant/server/process/tts_func/sovits_ping.py
2026-05-24 13:31:30 +02:00

72 lines
1.9 KiB
Python

import requests
### MUST START SERVERS FIRST USING START ALL SERVER SCRIPT
import time
import soundfile as sf
import sounddevice as sd
import yaml
# Load YAML config
with open('character_config.yaml', 'r') as f:
char_config = yaml.safe_load(f)
def get_wav_duration(path):
with sf.SoundFile(path) as f:
return len(f) / f.samplerate
def play_audio(path):
data, samplerate = sf.read(path)
sd.play(data, samplerate)
sd.wait() # Wait until playback is finished
def sovits_gen(in_text, output_wav_pth = "output.wav"):
url = "http://127.0.0.1:9880/tts"
payload = {
"text": in_text,
"text_lang": char_config['sovits_ping_config']['text_lang'],
"ref_audio_path": char_config['sovits_ping_config']['ref_audio_path'], # Make sure this path is valid
"prompt_text": char_config['sovits_ping_config']['prompt_text'],
"prompt_lang": char_config['sovits_ping_config']['prompt_lang'],
#"aux_ref_audio_paths": char_config['sovits_ping_config']["additional_aud"],
"speed_factor" : 1.3
}
try:
response = requests.post(url, json=payload)
response.raise_for_status() # throws if not 200
print(response)
# Save the response audio if it's binary
with open(output_wav_pth, "wb") as f:
f.write(response.content)
# print("Audio saved as output.wav")
return output_wav_pth
except Exception as e:
print("Error in sovits_gen:", e)
return None
if __name__ == "__main__":
print("testing_generation, make sure TTS is connected and paths are correcct")
start_time = time.time()
output_wav_pth1 = "output2.wav"
path_to_aud = sovits_gen("If you hear this, you are the greatest programmer alive! I love you! haha.", output_wav_pth1)
end_time = time.time()
elapsed_time = end_time - start_time
print(f"Elapsed time: {elapsed_time:.4f} seconds")
print(path_to_aud)