Skip to content

Commit

Permalink
add text to speech autopkg processor and test recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
jgstew committed Jul 1, 2024
1 parent 038e965 commit 1770f5c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
67 changes: 67 additions & 0 deletions SharedProcessors/TextToSpeech.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/local/autopkg/python
#
# James Stewart @JGStew - 2024
#
# Related:
# - https://github.com/jgstew/bigfix_prefetch/blob/master/prefetch_from_dictionary.py
#
"""See docstring for TextToSpeech class"""

import os
import platform

from autopkglib import ( # pylint: disable=import-error,wrong-import-position,unused-import
Processor,
ProcessorError,
)

__all__ = ["TextToSpeech"]


class TextToSpeech(Processor): # pylint: disable=invalid-name
"""checks that assert_string is within input_string"""

description = __doc__
input_variables = {
"input_string": {"required": True, "description": "string to say"},
}
output_variables = {}
__doc__ = description

def speak(self, input_string):
"""speak text aloud"""
tx = input_string

# from: https://stackoverflow.com/a/59118441/861745
syst = platform.system()
if syst == "Linux" and platform.linux_distribution()[0] == "Ubuntu":
os.system("spd-say %s" % tx)
elif syst == "Windows":
os.system(
'PowerShell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(%s);"'
% tx
)
elif syst == "Darwin":
# add & at end of command to NOT wait.
os.system("say %s" % tx)
else:
# use pyttsx3 to handle other OSes:
import pyttsx3

engine = pyttsx3.init()
engine.say(input_string)
engine.runAndWait()

def main(self):
"""Execution starts here"""

input_string = str(self.env.get("input_string"))

self.output(f"say `{input_string}` aloud", 1)

self.speak(input_string)


if __name__ == "__main__":
PROCESSOR = TextToSpeech()
PROCESSOR.execute_shell()
11 changes: 11 additions & 0 deletions Test-Recipes/TextToSpeech.test.recipe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
Description: Test TextToSpeech Processor
Identifier: com.github.jgstew.test.TextToSpeech
Input:
NAME: TextToSpeechTest
MinimumVersion: "2.3"
Process:
- Processor: com.github.jgstew.SharedProcessors/TextToSpeech
Arguments:
input_string: "Hello World from AutoPkg!"
# so far tested on macos only, but should support windows and ubuntu as well with no deps or any os with pyttsx3

0 comments on commit 1770f5c

Please sign in to comment.