Finish up the basic Rigel TTS

This commit is contained in:
Sameer Rahmani 2022-04-15 16:45:21 +01:00
parent 8578d24c51
commit 6bd67afb1a
3 changed files with 52 additions and 4 deletions

View File

@ -1,2 +1,3 @@
tts==0.6
python-dotenv
simpleaudio

View File

@ -23,7 +23,12 @@ from tcp import start, synth
def main():
config = dotenv.dotenv_values(sys.argv[1])
asyncio.run(start(config, synth(config)))
try:
asyncio.run(start(config, synth(config)))
except KeyboardInterrupt:
sys.exit()
if __name__ == "__main__":
main()

View File

@ -14,13 +14,51 @@
# You should have received a copy of the GNU General Public License
import asyncio
import numpy as np
# pylint: disable=redefined-outer-name, unused-argument
from pathlib import Path
import simpleaudio as sa
from TTS.utils.manage import ModelManager
from TTS.utils.synthesizer import Synthesizer
def to_wav_data(wav):
wav_norm = np.array(wav) * (32767 / max(0.01, np.max(np.abs(wav))))
return wav_norm.astype(np.int16)
def play(wav):
#p = pyaudio.PyAudio()
try:
# open stream
# stream = p.open(format=p.get_format_from_width(wav.getsampwidth()),
# channels=wav.getnchannels(),
# rate=wav.getframerate(),
# output=True)
# stream = p.open(format=pyaudio.paInt16,
# channels=1,
# rate=22050,
# output=True)
# stream.write(to_wav_data(wav))
# # stop stream
# stream.stop_stream()
# stream.close()
# close PyAudio
# Start audio
play = sa.play_buffer(to_wav_data(wav), 1, 2, 22050)
# Wait for audio playback to finish before exiting
play.wait_done()
finally:
play.stop()
#p.terminate()
def synth(config):
path = Path(__file__).parent / ".models.json"
manager = ModelManager(path)
@ -51,12 +89,16 @@ def synth(config):
)
async def tcp_handler(reader, writer):
data = await reader.readuntil(b'\0')
# Read till EOF
data = await reader.read(-1)
message = data.decode()
#wav = synthesizer.tts(message, speaker_idx, "None", None)
wav = synthesizer.tts(message, speaker_idx, "None", None)
play(wav)
print(f"Received {message!r}")
writer.write("Ok")
writer.write(b"Ok")
await writer.drain()
writer.close()