voicebox.voiceboxes package
Submodules
voicebox.voiceboxes.base module
voicebox.voiceboxes.parallel module
- class voicebox.voiceboxes.parallel.ParallelVoicebox(tts: TTS = None, effects: List[Effect] = None, sink: Sink = None, text_splitter: Splitter = None, start: bool = True, queue_get_timeout: float = 1.0, daemon: bool = True)[source]
Bases:
VoiceboxWithTextSplitterHandles speech on a separate thread so the main thread is not blocked waiting for speech to complete.
Also eliminates loading time between messages by loading the audio for the next message while the current message is playing.
Example
>>> voicebox = ParallelVoicebox(...) >>> voicebox.say('Hello, world!') # Does not block; speech handled by thread >>> voicebox.say('How are you?') # Does not block >>> # Do stuff in main thread while speech is happening... >>> voicebox.wait_until_done() # Call before program end to prevent cutoff >>> >>> # Can be used as context manager >>> with ParallelVoicebox(...) as voicebox: >>> ... >>> # Voicebox threads are stopped after exiting `with` block
- Parameters:
tts – The
voicebox.tts.TTSengine to use.effects – Sequence of
voicebox.effects.Effectinstances to apply to the audio before playing it.sink – The
voicebox.sinks.Sinkto use to play the audio.text_splitter – The
voicebox.voiceboxes.splitter.Splitterto use to split the text into chunks to be spoken. Defaults to no splitting.start – Whether to start the threads.
queue_get_timeout – Seconds to wait for text to appear in the queue of things to say between checks of the stop flag.
daemon – Whether the thread is daemonic (i.e. dies when the main thread exits).
- join(timeout: float = None) None[source]
Wait until the threads terminate.
- Parameters:
timeout – Wait up to this many seconds.
Nonewaits forever.
- stop(wait: bool = False, timeout: float = None) None[source]
Notify the threads to stop running.
- Parameters:
wait – Whether to wait for the threads to stop.
timeout – If waiting, then wait up to this many seconds.
Nonewaits forever.
- wait_until_done(timeout: float = None) None[source]
Wait until all speech is done.
Useful to run before program end to prevent speech from being cut off.
- Raises:
NotFinished – If
timeoutis notNoneand the timeout is reached before all speech is done.
voicebox.voiceboxes.queue module
- exception voicebox.voiceboxes.queue.NotFinished[source]
Bases:
ExceptionException raised by Queue.join() when it times out.
- class voicebox.voiceboxes.queue.Queue(maxsize=0)[source]
Bases:
Queue- join(timeout: float = None) None[source]
Blocks until all items in the Queue have been gotten and processed, or until
timeoutseconds have elapsed.The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls task_done() to indicate the item was retrieved and all work on it is complete.
When the count of unfinished tasks drops to zero, join() unblocks.
- Parameters:
timeout – An optional timeout in seconds to wait for unfinished tasks. If
None(default), then there is no limit to the wait time.- Raises:
NotFinished – If
timeoutis notNoneand the timeout is reached before all tasks are finished.
voicebox.voiceboxes.simple module
- class voicebox.voiceboxes.simple.SimpleVoicebox(tts: TTS = None, effects: List[Effect] = None, sink: Sink = None, text_splitter: Splitter = None)[source]
Bases:
VoiceboxWithTextSplitterUses a TTS engine to convert text to audio, applies a series of effects to the audio, and then plays the audio.
- Parameters:
tts – The
voicebox.tts.TTSengine to use.effects – Sequence of
voicebox.effects.Effectinstances to apply to the audio before playing it.sink – The
voicebox.sinks.Sinkto use to play the audio.text_splitter – The
voicebox.voiceboxes.splitter.Splitterto use to split the text into chunks to be spoken. Defaults to no splitting.
voicebox.voiceboxes.splitter module
- class voicebox.voiceboxes.splitter.NltkTokenizerSplitter(tokenizer: TokenizerI)[source]
Bases:
SplitterUses an NLTK tokenizer to split text.
- split(text: str | SSML) Iterable[str | SSML][source]
Splits the given text into chunks, unless it is a
voicebox.SSMLinstance, in which case it is returned as-is, i.e.[text].
- tokenizer: TokenizerI
- class voicebox.voiceboxes.splitter.PunktSentenceSplitter(language: str = 'english')[source]
Bases:
NltkTokenizerSplitterUses the Punkt sentence tokenizer from NLTK to split text into sentences more intelligently than a simple pattern-based splitter. It can handle instances of mid-sentence punctuation very well; e.g. “Mr. Jones went to see Dr. Sherman” would be correctly “split” into only one sentence.
This requires that the Punkt NLTK resources be located on disk, e.g. by downloading via one of these methods:
>>> PunktSentenceSplitter.download_resources()
or
>>> import nltk; nltk.download('punkt_tab')
or
$ python -m nltk.downloader punkt_tab
See here for all NLTK Data installation methods: https://www.nltk.org/data.html
- class voicebox.voiceboxes.splitter.RegexSplitter(pattern: str | Pattern, join_split_group: bool = True)[source]
Bases:
SplitterSplits text on regex pattern.
- join_split_group: bool
- pattern: Pattern
- class voicebox.voiceboxes.splitter.SimpleSentenceSplitter[source]
Bases:
RegexSplitterSplits text on sentence punctuation ‘.’, ‘!’, and ‘?’.