Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import soundfile as sf
- import sounddevice as sd
- from openai import OpenAI
- from dotenv import load_dotenv
- import subprocess
- import threading
- import queue
- import config
- import tempfile
- import utils
- import requests
- import shutil
- ...
- ...
- def TTS_piper(self, text_to_speak, output_file):
- """
- Generate speech from text using the Piper TTS engine and save it to an output file.
- Args:
- text_to_speak (str): The text to be converted to speech.
- output_file (str): The file path where the audio will be saved.
- """
- # Sanitize the input text by removing unsuitable characters
- text_to_speak = utils.sanitize_text(text_to_speak)
- # If there is no text left after sanitization, return "failed"
- if not text_to_speak.strip():
- return "failed"
- try:
- # Define the API endpoint
- api_url = "http://127.0.0.1:7851/api/tts-generate"
- # Prepare the data payload for the POST request
- data = {
- "text_input": text_to_speak,
- "text_filtering": "none",
- "character_voice_gen": "female_03.wav",
- "narrator_enabled": "false",
- "narrator_voice_gen": "arnold.wav",
- "text_not_inside": "character",
- "language": "en",
- "output_file_name": "output",
- "output_file_timestamp": "true",
- "autoplay": "false",
- "autoplay_volume": "0.8"
- }
- response = requests.post(api_url, data=data)
- print(response.content)
- response.raise_for_status() # Raise an exception for HTTP error codes
- response_data = response.json()
- if response_data["status"] == "generate-success":
- # Use the local path of the generated audio file
- local_audio_path = response_data["output_file_path"]
- # Copy the file from the local path to the desired output file
- shutil.copyfile(local_audio_path, output_file)
- return "success"
- except requests.RequestException as e:
- print(f"Error calling TTS API: {e}")
- return "failed"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement