Last active
April 18, 2024 15:31
-
-
Save skittleson/94ce7ba1a13bfcba4b21b9ad5c7aa064 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wsl_shell | |
import simpleaudio as sa | |
import os | |
class GenerativeAudioService: | |
""" | |
Create and manage playback with text to speech audio files | |
""" | |
def __init__(self) -> None: | |
# todo. check if piper is installed | |
# piper --update-voices -m en_US-libritts-high | |
pass | |
def generative(self, text: str): | |
import time | |
import shlex | |
import os.path | |
temp_filename = 'generative.wav' | |
if os.path.isfile(temp_filename): | |
os.remove(temp_filename) | |
encoded_text = shlex.quote(text) | |
# en_US-lessac-medium | |
shell_command = f"echo {encoded_text} | piper --model en_US-libritts-high --output_file {temp_filename}" | |
# print(shell_command) | |
_, stderr, returncode = wsl_shell.shell(shell_command) | |
print(stderr) | |
if returncode == 0: | |
while not os.path.isfile(temp_filename): | |
time.sleep(1) | |
GenerativeAudioService.play(temp_filename) | |
@staticmethod | |
def play(filename: str, blocking: bool = True): | |
wave_obj = sa.WaveObject.from_wave_file(filename) | |
play_obj = wave_obj.play() | |
if blocking: | |
play_obj.wait_done() | |
@staticmethod | |
def ding(): | |
GenerativeAudioService.play('ding.wav', False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@staticmethod | |
def shell(command: str, distribution: str = "Ubuntu-22.04"): | |
""" | |
Command to start an interactive shell in WSL | |
""" | |
import subprocess | |
shell = ["wsl", "-d", distribution, "/bin/bash"] | |
p = subprocess.run(shell, input=command, capture_output=True, text=True) | |
stdout = p.stdout | |
stderr = p.stderr | |
returncode = p.returncode | |
return stdout, stderr, returncode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment