public final class Spokestack extends SpokestackAdapter implements AutoCloseable
Client applications may wish to establish event listeners for purposes such as forwarding trace events to a logging framework, but events necessary to complete user interactions (for example, sending ASR transcripts through NLU) are handled entirely by this class. This includes internal management of TTS playback, which requires the client application to declare additional media player dependencies; see the documentation for more details. This feature can be disabled via the builder if desired.
The default configuration of this class assumes that the client application wants to use all of Spokestack's features, regardless of their implied dependencies or required configuration, so an error will be thrown if any prerequisite is missing at build time. Individual features can be disabled via the builder.
Convenience methods are provided to interact with the most important features of individual subsystems, but they do not completely duplicate the subsystems' public APIs. Each subsystem in use can be retrieved via its own getter, enabling use of its full API.
Any convenience methods called on a subsystem that has been explicitly
disabled will result in a NullPointerException
.
This class is not threadsafe; public methods used to interact with Spokestack subsystems should be called from the same thread. The subsystems themselves use background threads where appropriate to perform intensive tasks.
SpeechPipeline
,
TensorflowNLU
,
TTSManager
Modifier and Type | Class and Description |
---|---|
static class |
Spokestack.Builder
Fluent builder interface for configuring Spokestack.
|
Modifier and Type | Method and Description |
---|---|
void |
activate()
Manually activate the speech pipeline, forcing the current ASR class to
begin recognizing speech.
|
void |
addListener(SpokestackAdapter listener)
Add a new listener to receive events from Spokestack subsystems.
|
AsyncResult<NLUResult> |
classify(String utterance)
Classify a user utterance, returning a wrapper that can either block
until the classification is complete or call a registered callback when
the result is ready.
|
void |
close() |
void |
deactivate()
Manually deactivate the speech pipeline, forcing the current ASR class to
stop recognizing speech.
|
NLUService |
getNlu() |
SpeechPipeline |
getSpeechPipeline() |
TTSManager |
getTts() |
void |
onEvent(SpeechContext.Event event,
SpeechContext context)
Receive events from the speech pipeline.
|
void |
prepareTts()
Dynamically constructs TTS component classes, allocating any resources
they control.
|
void |
releaseTts()
Stops activity in the TTS subsystem and releases any resources held by
its components.
|
void |
removeListener(SpokestackAdapter listener)
Remove a Spokestack event listener, allowing it to be garbage collected.
|
void |
start()
Starts the speech pipeline in order to process user input via the
microphone (or chosen input class).
|
void |
stop()
Stops the speech pipeline and releases all its internal resources.
|
void |
stopPlayback()
Stops playback of any playing or queued synthesis results.
|
void |
synthesize(SynthesisRequest request)
Synthesizes a piece of text or SSML, dispatching the result to any
registered listeners.
|
call, error, eventReceived, nluResult, onError, onTrace, speechEvent, trace, ttsEvent
public SpeechPipeline getSpeechPipeline()
public void start() throws Exception
Exception
- if there is an error configuring or starting the speech
pipeline.public void stop()
public void activate()
public void deactivate()
Note: This method currently has no effect on the Android speech recognizer this class uses by default.
public NLUService getNlu()
public AsyncResult<NLUResult> classify(String utterance)
This convenience method automatically registers all SpokestackAdapter
s added to this class at build time to receive the
classification result asynchronously.
utterance
- The utterance to classify.public TTSManager getTts()
public void prepareTts() throws Exception
releaseTts()
or close()
.Exception
- If there is an error constructing TTS components.public void releaseTts()
Once released, an explicit call to prepareTts()
is required to
reallocate TTS resources.
public void synthesize(SynthesisRequest request)
request
- The synthesis request data.public void stopPlayback()
public void addListener(SpokestackAdapter listener)
listener
- A listener that will receive events from all Spokestack
subsystems.public void removeListener(SpokestackAdapter listener)
listener
- The listener to be removed.public void onEvent(@NotNull SpeechContext.Event event, @NotNull SpeechContext context)
SpokestackAdapter
Clients should typically override SpokestackAdapter.speechEvent(SpeechContext.Event,
SpeechContext)
instead of this method.
onEvent
in interface OnSpeechEventListener
onEvent
in class SpokestackAdapter
event
- The name of the event that was raised.context
- The current speech context.public void close()
close
in interface AutoCloseable
Copyright © 2020. All rights reserved.