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 modules, but they do not completely duplicate the modules' public APIs. Each module in use can be retrieved via its own getter, enabling use of its full API.
Any convenience methods called on a module that has been explicitly disabled
will result in a NullPointerException
.
This class is not threadsafe; public methods used to interact with Spokestack modules should be called from the same thread. The modules 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 modules.
|
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()
Release internal resources held by all registered Spokestack modules.
|
void |
deactivate()
Manually deactivate the speech pipeline, forcing the current ASR class to
stop recognizing speech.
|
void |
eventReceived(TTSEvent event)
Receive events from the TTS module.
|
FinalizedPrompt |
finalizePrompt(Prompt prompt)
Finalize a prompt, interpolating template strings using the current
conversation data store.
|
DialogueManager |
getDialogueManager() |
NLUManager |
getNlu() |
SpeechPipeline |
getSpeechPipeline() |
TTSManager |
getTts() |
void |
nluResult(NLUResult result)
Called when an NLU classification result is available if this class is
registered as a callback at classification time.
|
void |
onEvent(SpeechContext.Event event,
SpeechContext context)
Receive events from the speech pipeline.
|
void |
pause()
Pauses the speech pipeline, suspending passive listening.
|
void |
putConversationData(String key,
Object value)
Stores an object in the conversation data store for use in interpolating
system prompts.
|
void |
removeListener(SpokestackAdapter listener)
Remove a Spokestack event listener, allowing it to be garbage collected.
|
void |
resume()
Resumes a paused speech pipeline, returning it to a passive listening
state.
|
void |
setAndroidContext(android.content.Context context)
Update the Android application context used for on-device ASR and the TTS
manager.
|
void |
start()
Prepares all registered Spokestack modules for use and starts the
speech pipeline in passive listening mode.
|
void |
stop()
Stops the speech pipeline and releases internal resources held by all
registered Spokestack modules.
|
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, onDialogueEvent, onError, onTrace, speechEvent, trace, ttsEvent
public void setAndroidContext(android.content.Context context)
context
- The new Android context.public SpeechPipeline getSpeechPipeline()
public void start() throws Exception
Exception
- if there is an error configuring or starting a module.public void pause()
This method will implicitly deactivate the pipeline, canceling any in-flight ASR requests.
This method is called automatically when Spokestack is playing a TTS prompt if Spokestack is managing audio playback.
SpeechPipeline.pause()
public void resume()
This method is called automatically when Spokestack finishes playing a TTS prompt if Spokestack is managing audio playback.
SpeechPipeline.resume()
public void stop()
In order to support restarting Spokestack (calling start()
after
this method), this method does not clear registered
listeners. To do this, close then destroy the current Spokestack
instance and build a new one.
public void activate()
public void deactivate()
Note: This method currently has no effect on the Android speech recognizer this class uses by default.
public NLUManager 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 synthesize(SynthesisRequest request)
request
- The synthesis request data.public void stopPlayback()
public DialogueManager getDialogueManager()
public FinalizedPrompt finalizePrompt(Prompt prompt)
This method can only be used if a dialogue manager is active.
prompt
- The prompt to be finalized.DialogueManager.finalizePrompt(Prompt)
,
ConversationData
public void putConversationData(String key, Object value)
This method can only be used if a dialogue manager is active.
key
- The name of the object to store.value
- The object to store.ConversationData.set(String, Object)
public void addListener(SpokestackAdapter listener)
listener
- A listener that will receive events from all Spokestack
modules.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 nluResult(@NotNull NLUResult result)
SpokestackAdapter
Spokestack
class at build time are automatically registered for
all classifications.nluResult
in class SpokestackAdapter
result
- The NLU result.TensorflowNLU.classify(String)
,
AsyncResult
public void eventReceived(@NotNull TTSEvent event)
SpokestackAdapter
Clients should typically override SpokestackAdapter.ttsEvent(TTSEvent)
instead of
this method.
eventReceived
in interface TTSListener
eventReceived
in class SpokestackAdapter
event
- The event from the TTS module.public void close()
In order to support restarting Spokestack (calling start()
after
this method), this method does not clear registered
listeners. To do this, close then destroy the current Spokestack
instance and build a new one.
close
in interface AutoCloseable
Copyright © 2021. All rights reserved.