Speech Module

New in version 0.1.1.

A collection of classes for handling speak input and output.

Classes:

XACommandDetector([command_function_map])

A command-based query detector.

XASpeech([message, voice, volume, rate])

XASpeechRecognizer([finish_conditions])

A rule-based query detector.

class PyXA.Additions.Speech.XACommandDetector(command_function_map: dict[str, Callable[[], Any]] | None = None)[source]

Bases: object

A command-based query detector.

New in version 0.0.9.

Attributes:

command_function_map

The dictionary of commands and corresponding functions to run upon detection

Methods:

listen()

Begins listening for the specified commands.

on_detect(command, function)

Adds or replaces a command to listen for upon calling listen(), and associates the given function with that command.

command_function_map

The dictionary of commands and corresponding functions to run upon detection

listen() Any[source]

Begins listening for the specified commands.

Returns:

The execution return value of the corresponding command function

Return type:

Any

Example:

>>> import PyXA
>>> PyXA.speak("What app do you want to open?")
>>> PyXA.XACommandDetector({
>>>     "safari": PyXA.Application("Safari").activate,
>>>     "messages": PyXA.Application("Messages").activate,
>>>     "shortcuts": PyXA.Application("Shortcuts").activate,
>>>     "mail": PyXA.Application("Mail").activate,
>>>     "calendar": PyXA.Application("Calendar").activate,
>>>     "notes": PyXA.Application("Notes").activate,
>>>     "music": PyXA.Application("Music").activate,
>>>     "tv": PyXA.Application("TV").activate,
>>>     "pages": PyXA.Application("Pages").activate,
>>>     "numbers": PyXA.Application("Numbers").activate,
>>>     "keynote": PyXA.Application("Keynote").activate,
>>> }).listen()

New in version 0.0.9.

on_detect(command: str, function: Callable[[], Any])[source]

Adds or replaces a command to listen for upon calling listen(), and associates the given function with that command.

Parameters:
  • command (str) – The command to listen for

  • function (Callable[[], Any]) – The function to call when the command is heard

Example:

>>> detector = PyXA.XACommandDetector()
>>> detector.on_detect("go to google", PyXA.XAURL("http://google.com").open)
>>> detector.listen()

New in version 0.0.9.

class PyXA.Additions.Speech.XASpeech(message: str = '', voice: str | None = None, volume: float = 0.5, rate: int = 200)[source]

Bases: object

Attributes:

message

The message to speak

rate

The speaking rate

voice

The voice that the message is spoken in

volume

The speaking volume

Methods:

speak([path])

Speaks the provided message using the desired voice, volume, and speaking rate.

voices()

Gets the list of voice names available on the system.

message: str

The message to speak

rate: int

The speaking rate

speak(path: str | XAPath | None | list[str] = None)[source]

Speaks the provided message using the desired voice, volume, and speaking rate.

Parameters:

path (Union[str, XAPath, None], optional) – The path to a .AIFF file to output sound to, defaults to None

Example 1:

Speak a message aloud

>>> import PyXA
>>> PyXA.XASpeech("This is a test").speak()
Example 2:

Output spoken message to an AIFF file

>>> import PyXA
>>> speaker = PyXA.XASpeech("Hello, world!")
>>> speaker.speak("/Users/steven/Downloads/Hello.AIFF")
Example 3:

Control the voice, volume, and speaking rate

>>> import PyXA
>>> speaker = PyXA.XASpeech(
>>>     message = "Hello, world!",
>>>     voice = "Alex",
>>>     volume = 1,
>>>     rate = 500
>>> )
>>> speaker.speak()

New in version 0.0.9.

voice: str | None

The voice that the message is spoken in

voices() list[str][source]

Gets the list of voice names available on the system.

Returns:

The list of voice names

Return type:

list[str]

Example:

>>> import PyXA
>>> speaker = PyXA.XASpeech()
>>> print(speaker.voices())
['Agnes', 'Alex', 'Alice', 'Allison',

New in version 0.0.9.

volume: float

The speaking volume

class PyXA.Additions.Speech.XASpeechRecognizer(finish_conditions: None | dict[Callable[[str], bool], Callable[[str], bool]] = None)[source]

Bases: object

A rule-based query detector.

New in version 0.0.9.

Attributes:

finish_conditions

A dictionary of rules and associated methods to call when a rule evaluates to true

spoken_query

The recognized spoken input

start_time

The time that the Speech Recognizer begins listening

time_elapsed

The amount of time passed since the start time

Methods:

listen()

Begins listening for a query until a rule returns True.

on_detect(rule, method)

Sets the given rule to call the specified method if a spoken query passes the rule.

finish_conditions: Callable[[str], bool]

A dictionary of rules and associated methods to call when a rule evaluates to true

listen() Any[source]

Begins listening for a query until a rule returns True.

Returns:

The value returned by the method invoked upon matching some rule

Return type:

Any

New in version 0.0.9.

on_detect(rule: Callable[[str], bool], method: Callable[[str], bool])[source]

Sets the given rule to call the specified method if a spoken query passes the rule.

Parameters:
  • rule (Callable[[str], bool]) – A function that takes the spoken query as a parameter and returns a boolean value depending on whether the query passes a desired rule

  • method (Callable[[str], bool]) – A function that takes the spoken query as a parameter and acts on it

New in version 0.0.9.

spoken_query: str

The recognized spoken input

start_time: datetime

The time that the Speech Recognizer begins listening

time_elapsed: timedelta

The amount of time passed since the start time