Files
PiperTTS-scripts/Uptated-Prototype/TTS-Script-PiperTTS 2nd.py

57 lines
1.8 KiB
Python

# Theoretical Upgrade Test
from piper.voice import PiperVoice # Backbone of text to speech
import pyaudio
import os
import subprocess
import time
from piper.voice import SynthesisConfig
result = subprocess.run(["xsel"], capture_output=True, text=True) # In Theory grab selected text into Varible called TextToSpeak
#print(result) # In Theory grab selected text and print ot to terminal
texttospeak = str(result.stdout)
print(texttospeak)
# texttospeak = "Welcome to the world of speech synthesis! This is an example of text-to-speech using Piper TTS."
#voice = PiperVoice.load("~/en_US-amy-medium.onnx") # Load the model
voicedir = os.path.expanduser('~') #Where onnx model files are stored on my machine
model = voicedir+"/en_GB-alba-medium.onnx"
voice = PiperVoice.load(model)
syn_config = SynthesisConfig(
volume=0.5, # half as loud
length_scale=2.0, # twice as slow
noise_scale=0.0, # more audio variation
noise_w_scale=0.0, # more speaking variation
normalize_audio=False, # use raw audio from voice
)
chunks = voice.synthesize(texttospeak, syn_config=syn_config) # Set up Piper to stream audio from TextToSpeak
Lenght = str(len(chunks))
print(f"There are {Lenght} Chunks.") # Print the amount of chunks
first_chunk = next(chunks) # Get the first chunk to set up audio stream configuration
try:
print(first_chunk.sample_rate)
except:
print("Failed getting sample rate?")
p = pyaudio.PyAudio()
stream = p.open( # Open audio stream with correct settings
format=p.get_format_from_width(first_chunk.sample_width),
channels=first_chunk.sample_channels,
rate=first_chunk.sample_rate,
output=True,
)
stream.write(first_chunk.audio_int16_bytes) # Play the first chunk
for chunk in chunks: # Play subsequent chunks of audio
stream.write(chunk.audio_int16_bytes)
# Cleanup
stream.stop_stream()
stream.close()
p.terminate()