Class BaseEngine
- All Implemented Interfaces:
SpeechEventDispatcher
- Direct Known Subclasses:
BaseSynthesizer
Engine
interface.
Actual JSAPI implementations might want to extend or modify this
implementation.-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected AudioManager
TheAudioManager
for thisEngine
.protected static final long
Utility state for clearing theengineState
.protected Collection
List ofEngineListeners
registered forEngineEvents
on thisEngine
.protected EngineModeDesc
TheEngineModeDesc
for thisEngine
.protected EngineProperties
TheEngineProperties
for thisEngine
.protected long
A bitmask holding the current state of thisEngine
.protected Object
AnObject
used for synchronizing access toengineState
. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a newEngine
in theDEALLOCATED
state.BaseEngine
(EngineModeDesc desc) Creates a newEngine
in theDEALLOCATED
state. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addEngineListener
(EngineListener listener) Requests notification ofEngineEvents
from thisEngine
.void
allocate()
Allocates the resources required for thisEngine
and puts it into theALLOCATED
state.protected void
checkEngineState
(long state) Convenience method that throws anEngineStateError
if any of the bits in the passed state are set in thestate
.protected abstract BaseEngineProperties
Factory constructor for EngineProperties object.void
Frees the resources of thisEngine
that were acquired during allocation and during operation and return thisEngine
to theDEALLOCATED
.void
dispatchSpeechEvent
(SpeechEvent event) Dispatches aSpeechEvent
.void
fireEngineAllocated
(EngineEvent event) Utility function that sends anENGINE_ALLOCATED
event to allEngineListeners
registered with thisEngine
.void
fireEngineAllocatingResources
(EngineEvent event) Utility function that sends anENGINE_ALLOCATING_RESOURCES
event to allEngineListeners
registered with thisEngine
.void
fireEngineDeallocated
(EngineEvent event) Utility function that sends anENGINE_DEALLOCATED
event to allEngineListeners
registered with thisEngine
.void
fireEngineDeallocatingResources
(EngineEvent event) Utility function that sends aENGINE_DEALLOCATING_RESOURCES
event to allEngineListeners
registered with thisEngine
.void
fireEnginePaused
(EngineEvent event) Utility function that sends anENGINE_PAUSED
event to allEngineListeners
registered with thisEngine
.void
fireEngineResumed
(EngineEvent event) Utility function that sends anENGINE_RESUMED
event to allEngineListeners
registered with thisEngine
.AudioManager
Returns an object that provides management of the audio input or output of thisEngine
.EngineModeDesc
Gets the current operating properties and mode of thisEngine
.EngineProperties
Gets theEngineProperties
of thisEngine
.long
Returns a or'ed set of flags indicating the current state of thisEngine
.VocabManager
Returns an object that provides management of the vocabulary for thisEngine
.protected abstract void
Called from theallocate
method.protected abstract void
Called from thedeallocate
method.protected abstract void
Called from thepause
method.protected abstract void
Called from theresume
method.void
pause()
Pauses the audio stream for thisEngine
and put thisEngine
into thePAUSED
state.protected void
postEngineAllocated
(long oldState, long newState) Utility function that generates anENGINE_ALLOCATED
event and posts it to the event queue.protected void
postEngineAllocatingResources
(long oldState, long newState) Utility function that generates anENGINE_ALLOCATING_RESOURCES
event and posts it to the event queue.protected void
postEngineDeallocated
(long oldState, long newState) Utility function that generates anENGINE_DEALLOCATED
event and posts it to the event queue.protected void
postEngineDeallocatingResources
(long oldState, long newState) Utility function that generatesENGINE_DEALLOCATING_RESOURCES
event and posts it to the event queue.protected void
postEnginePaused
(long oldState, long newState) Utility function that generates anENGINE_PAUSED
event and posts it to the event queue.protected void
postEngineResumed
(long oldState, long newState) Utility function that generates anENGINE_RESUMED
event and posts it to the event queue.void
removeEngineListener
(EngineListener listener) Removes anEngineListener
from the list ofEngineListeners
.void
resume()
Resumes the audio stream for thisEngine
and put thisEngine
into theRESUMED
state.protected void
setEngineModeDesc
(EngineModeDesc desc) Sets the current operating properties and mode of thisEngine
.protected long[]
setEngineState
(long clear, long set) Updates thisEngine
state by clearing defined bits, then setting other specified bits.protected String
stateToString
(long state) Returns aString
of the names of all theEngine
states in the givenEngine
state.boolean
testEngineState
(long state) Returnstrue
if this state of thisEngine
matches the specified state.toString()
Returns the engine name and mode for debug purposes.void
waitEngineState
(long state) Blocks the calling thread until thisEngine
is in a specified state.
-
Field Details
-
engineState
protected long engineStateA bitmask holding the current state of thisEngine
. -
engineStateLock
AnObject
used for synchronizing access toengineState
.- See Also:
-
engineListeners
List ofEngineListeners
registered forEngineEvents
on thisEngine
. -
audioManager
protected AudioManager audioManagerTheAudioManager
for thisEngine
. -
engineModeDesc
protected EngineModeDesc engineModeDescTheEngineModeDesc
for thisEngine
. -
engineProperties
protected EngineProperties enginePropertiesTheEngineProperties
for thisEngine
. -
CLEAR_ALL_STATE
protected static final long CLEAR_ALL_STATEUtility state for clearing theengineState
.- See Also:
-
-
Constructor Details
-
BaseEngine
public BaseEngine()Creates a newEngine
in theDEALLOCATED
state. -
BaseEngine
public BaseEngine(EngineModeDesc desc) Creates a newEngine
in theDEALLOCATED
state.- Parameters:
desc
- the operating mode of thisEngine
-
-
Method Details
-
getEngineState
public long getEngineState()Returns a or'ed set of flags indicating the current state of thisEngine
.An
EngineEvent
is issued each time thisEngine
changes state.The
getEngineState
method can be called successfully in anyEngine
state.- Returns:
- the current state of this
Engine
- See Also:
-
waitEngineState
Blocks the calling thread until thisEngine
is in a specified state.All state bits specified in the
state
parameter must be set in order for the method to return, as defined for thetestEngineState
method. If thestate
parameter defines an unreachable state (e.g.PAUSED | RESUMED
) an exception is thrown.The
waitEngineState
method can be called successfully in anyEngine
state.- Parameters:
state
- a bitmask of the state to wait for- Throws:
InterruptedException
- if another thread has interrupted this thread.IllegalArgumentException
- if the specified state is unreachable- See Also:
-
testEngineState
Returnstrue
if this state of thisEngine
matches the specified state.The test performed is not an exact match to the current state. Only the specified states are tested. For example the following returns true only if the
Synthesizer
queue is empty, irrespective of the pause/resume and allocation states.if (synth.testEngineState(Synthesizer.QUEUE_EMPTY)) ...
The
testEngineState
method is equivalent to:if ((engine.getEngineState() & state) == state)
The
testEngineState
method can be called successfully in anyEngine
state.- Parameters:
state
- a bitmask of the states to test for- Returns:
true
if thisEngine
matchesstate
; otherwisefalse
- Throws:
IllegalArgumentException
- if the specified state is unreachable
-
setEngineState
protected long[] setEngineState(long clear, long set) Updates thisEngine
state by clearing defined bits, then setting other specified bits.- Returns:
- a length-2 array with old and new state values.
-
allocate
public void allocate() throws EngineException, EngineStateErrorAllocates the resources required for thisEngine
and puts it into theALLOCATED
state. When this method returns successfully theALLOCATED
bit of thisEngine
state is set, and thetestEngineState(Engine.ALLOCATED)
method returnstrue
.During the processing of the method, this
Engine
is temporarily in theALLOCATING_RESOURCES
state.- Throws:
EngineException
- if thisEngine
cannot be allocatedEngineStateError
- if thisEngine
is in theDEALLOCATING_RESOURCES
state- See Also:
-
handleAllocate
protected abstract void handleAllocate() throws EngineExceptionCalled from theallocate
method. Override this in subclasses.- Throws:
EngineException
- if problems are encountered- See Also:
-
deallocate
public void deallocate() throws EngineException, EngineStateErrorFrees the resources of thisEngine
that were acquired during allocation and during operation and return thisEngine
to theDEALLOCATED
. When this method returns theDEALLOCATED
bit of thisEngine
state is set so thetestEngineState(Engine.DEALLOCATED)
method returnstrue
.During the processing of the method, this
Engine
is temporarily in theDEALLOCATING_RESOURCES
state.A deallocated engine can be re-started with a subsequent call to
allocate
.- Throws:
EngineException
- if thisEngine
cannot be deallocatedEngineStateError
- if thisEngine
is in theALLOCATING_RESOURCES
state- See Also:
-
handleDeallocate
protected abstract void handleDeallocate() throws EngineExceptionCalled from thedeallocate
method. Override this in subclasses.- Throws:
EngineException
- if thisEngine
cannot be deallocated.
-
pause
public void pause() throws EngineStateErrorPauses the audio stream for thisEngine
and put thisEngine
into thePAUSED
state.- Throws:
EngineStateError
- if thisEngine
is in theDEALLOCATING_RESOURCES
orDEALLOCATED
state.
-
handlePause
protected abstract void handlePause()Called from thepause
method. Override this in subclasses. -
resume
public void resume() throws AudioException, EngineStateErrorResumes the audio stream for thisEngine
and put thisEngine
into theRESUMED
state.- Throws:
AudioException
- if unable to gain access to the audio channelEngineStateError
- if thisEngine
is in theDEALLOCATING_RESOURCES
orDEALLOCATED
state
-
handleResume
protected abstract void handleResume()Called from theresume
method. Override in subclasses. -
getAudioManager
public AudioManager getAudioManager()Returns an object that provides management of the audio input or output of thisEngine
.- Returns:
- the audio manader for this
Engine
-
getVocabManager
public VocabManager getVocabManager() throws EngineStateErrorReturns an object that provides management of the vocabulary for thisEngine
. Returnsnull
if thisEngine
does not support vocabulary management.- Returns:
- the vocabulary manager of this
Engine
- Throws:
EngineStateError
- if thisEngine
in theDEALLOCATING_RESOURCES
orDEALLOCATED
state
-
getEngineProperties
public EngineProperties getEngineProperties()Gets theEngineProperties
of thisEngine
. Must be set in subclasses.- Returns:
- the
EngineProperties
of thisEngine
.
-
getEngineModeDesc
Gets the current operating properties and mode of thisEngine
.- Returns:
- the operating mode of this
Engine
- Throws:
SecurityException
-
setEngineModeDesc
protected void setEngineModeDesc(EngineModeDesc desc) Sets the current operating properties and mode of thisEngine
.- Parameters:
desc
- the new operating mode of thisEngine
-
addEngineListener
public void addEngineListener(EngineListener listener) Requests notification ofEngineEvents
from thisEngine
.- Parameters:
listener
- the listener to add.
-
removeEngineListener
public void removeEngineListener(EngineListener listener) Removes anEngineListener
from the list ofEngineListeners
.- Parameters:
listener
- the listener to remove.
-
postEngineAllocated
protected void postEngineAllocated(long oldState, long newState) Utility function that generates anENGINE_ALLOCATED
event and posts it to the event queue. EventuallyfireEngineAllocated
will be called by thedispatchSpeechEvent
as a result of this action.- Parameters:
oldState
- the old state of thisEngine
newState
- the new state of thisEngine
- See Also:
-
fireEngineAllocated
public void fireEngineAllocated(EngineEvent event) Utility function that sends anENGINE_ALLOCATED
event to allEngineListeners
registered with thisEngine
. Called bydispatchSpeechEvent
.- Parameters:
event
- theENGINE_ALLOCATED
event- See Also:
-
postEngineAllocatingResources
protected void postEngineAllocatingResources(long oldState, long newState) Utility function that generates anENGINE_ALLOCATING_RESOURCES
event and posts it to the event queue. EventuallyfireEngineAllocatingResources
will be called bydispatchSpeechEvent
as a result of this action.- Parameters:
oldState
- the old state of thisEngine
newState
- the new state of thisEngine
- See Also:
-
fireEngineAllocatingResources
public void fireEngineAllocatingResources(EngineEvent event) Utility function that sends anENGINE_ALLOCATING_RESOURCES
event to allEngineListeners
registered with thisEngine
. Called bydispatchSpeechEvent
.- Parameters:
event
- theENGINE_ALLOCATING_RESOURCES
event- See Also:
-
postEngineDeallocated
protected void postEngineDeallocated(long oldState, long newState) Utility function that generates anENGINE_DEALLOCATED
event and posts it to the event queue. EventuallyfireEngineDeallocated
will be called bydispatchSpeechEvent
as a result of this action.- Parameters:
oldState
- the old state of thisEngine
newState
- the new state of thisEngine
- See Also:
-
fireEngineDeallocated
public void fireEngineDeallocated(EngineEvent event) Utility function that sends anENGINE_DEALLOCATED
event to allEngineListeners
registered with thisEngine
. Called bydispatchSpeechEvent
.- Parameters:
event
- theENGINE_DEALLOCATED
event- See Also:
-
postEngineDeallocatingResources
protected void postEngineDeallocatingResources(long oldState, long newState) Utility function that generatesENGINE_DEALLOCATING_RESOURCES
event and posts it to the event queue. EventuallyfireEngineAllocatingResources
will be called bydispatchSpeechEvent
as a result of this action.- Parameters:
oldState
- the old state of thisEngine
newState
- the new state of thisEngine
- See Also:
-
fireEngineDeallocatingResources
public void fireEngineDeallocatingResources(EngineEvent event) Utility function that sends aENGINE_DEALLOCATING_RESOURCES
event to allEngineListeners
registered with thisEngine
. Called bydispatchSpeechEvent
.- Parameters:
event
- theENGINE_DEALLOCATING_RESOURCES
event- See Also:
-
postEnginePaused
protected void postEnginePaused(long oldState, long newState) Utility function that generates anENGINE_PAUSED
event and posts it to the event queue. EventuallyfireEnginePaused
will be called bydispatchSpeechEvent
as a result of this action.- Parameters:
oldState
- the old state of thisEngine
newState
- the new state of thisEngine
- See Also:
-
fireEnginePaused
public void fireEnginePaused(EngineEvent event) Utility function that sends anENGINE_PAUSED
event to allEngineListeners
registered with thisEngine
. Called bydispatchSpeechEvent
.- Parameters:
event
- theENGINE_PAUSED
event- See Also:
-
postEngineResumed
protected void postEngineResumed(long oldState, long newState) Utility function that generates anENGINE_RESUMED
event and posts it to the event queue. EventuallyfireEngineResumed
will be called bydispatchSpeechEvent
as a result of this action.- Parameters:
oldState
- the old state of thisEngine
newState
- the new state of thisEngine
- See Also:
-
fireEngineResumed
public void fireEngineResumed(EngineEvent event) Utility function that sends anENGINE_RESUMED
event to allEngineListeners
registered with thisEngine
. Called bydispatchSpeechEvent
.- Parameters:
event
- theENGINE_RESUMED
event- See Also:
-
createEngineProperties
Factory constructor for EngineProperties object.- Returns:
- a
BaseEngineProperties
object specific to a subclass.
-
checkEngineState
protected void checkEngineState(long state) throws EngineStateError Convenience method that throws anEngineStateError
if any of the bits in the passed state are set in thestate
.- Parameters:
state
- theEngine
state to check- Throws:
EngineStateError
- if any of the bits in the passed state are set in thestate
-
stateToString
Returns aString
of the names of all theEngine
states in the givenEngine
state.- Parameters:
state
- the bitmask of states- Returns:
- a
String
containing the names of all the states set instate
-
dispatchSpeechEvent
public void dispatchSpeechEvent(SpeechEvent event) Dispatches aSpeechEvent
. The dispatcher should notify allEngineListeners
from this method. TheSpeechEvent
was added via the various post methods of this class.- Specified by:
dispatchSpeechEvent
in interfaceSpeechEventDispatcher
- Parameters:
event
- theSpeechEvent
to dispatch- See Also:
-
toString
Returns the engine name and mode for debug purposes.
-