Class BaseEngine
- java.lang.Object
-
- com.sun.speech.engine.BaseEngine
-
- All Implemented Interfaces:
SpeechEventDispatcher
- Direct Known Subclasses:
BaseSynthesizer
public abstract class BaseEngine extends java.lang.Object implements SpeechEventDispatcher
Supports the JSAPI 1.0Engine
interface. Actual JSAPI implementations might want to extend or modify this implementation.
-
-
Field Summary
Fields Modifier and Type Field Description protected AudioManager
audioManager
TheAudioManager
for thisEngine
.protected static long
CLEAR_ALL_STATE
Utility state for clearing theengineState
.protected java.util.Collection
engineListeners
List ofEngineListeners
registered forEngineEvents
on thisEngine
.protected EngineModeDesc
engineModeDesc
TheEngineModeDesc
for thisEngine
.protected EngineProperties
engineProperties
TheEngineProperties
for thisEngine
.protected long
engineState
A bitmask holding the current state of thisEngine
.protected java.lang.Object
engineStateLock
AnObject
used for synchronizing access toengineState
.
-
Constructor Summary
Constructors Constructor Description BaseEngine()
Creates a newEngine
in theDEALLOCATED
state.BaseEngine(EngineModeDesc desc)
Creates a newEngine
in theDEALLOCATED
state.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
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
createEngineProperties()
Factory constructor for EngineProperties object.void
deallocate()
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
getAudioManager()
Returns an object that provides management of the audio input or output of thisEngine
.EngineModeDesc
getEngineModeDesc()
Gets the current operating properties and mode of thisEngine
.EngineProperties
getEngineProperties()
Gets theEngineProperties
of thisEngine
.long
getEngineState()
Returns a or'ed set of flags indicating the current state of thisEngine
.VocabManager
getVocabManager()
Returns an object that provides management of the vocabulary for thisEngine
.protected abstract void
handleAllocate()
Called from theallocate
method.protected abstract void
handleDeallocate()
Called from thedeallocate
method.protected abstract void
handlePause()
Called from thepause
method.protected abstract void
handleResume()
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 java.lang.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.java.lang.String
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 Detail
-
engineState
protected long engineState
A bitmask holding the current state of thisEngine
.
-
engineStateLock
protected java.lang.Object engineStateLock
AnObject
used for synchronizing access toengineState
.- See Also:
engineState
-
engineListeners
protected java.util.Collection engineListeners
List ofEngineListeners
registered forEngineEvents
on thisEngine
.
-
audioManager
protected AudioManager audioManager
TheAudioManager
for thisEngine
.
-
engineModeDesc
protected EngineModeDesc engineModeDesc
TheEngineModeDesc
for thisEngine
.
-
engineProperties
protected EngineProperties engineProperties
TheEngineProperties
for thisEngine
.
-
CLEAR_ALL_STATE
protected static final long CLEAR_ALL_STATE
Utility state for clearing theengineState
.- See Also:
- Constant Field Values
-
-
Method Detail
-
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:
getEngineState()
,waitEngineState(long)
-
waitEngineState
public void waitEngineState(long state) throws java.lang.InterruptedException, java.lang.IllegalArgumentException
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:
java.lang.InterruptedException
- if another thread has interrupted this thread.java.lang.IllegalArgumentException
- if the specified state is unreachable- See Also:
testEngineState(long)
,getEngineState()
-
testEngineState
public boolean testEngineState(long state) throws java.lang.IllegalArgumentException
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:
java.lang.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, EngineStateError
Allocates 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:
deallocate()
-
handleAllocate
protected abstract void handleAllocate() throws EngineException
Called from theallocate
method. Override this in subclasses.- Throws:
EngineException
- if problems are encountered- See Also:
allocate()
-
deallocate
public void deallocate() throws EngineException, EngineStateError
Frees 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:
allocate()
-
handleDeallocate
protected abstract void handleDeallocate() throws EngineException
Called from thedeallocate
method. Override this in subclasses.- Throws:
EngineException
- if thisEngine
cannot be deallocated.
-
pause
public void pause() throws EngineStateError
Pauses 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, EngineStateError
Resumes 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 EngineStateError
Returns 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
public EngineModeDesc getEngineModeDesc() throws java.lang.SecurityException
Gets the current operating properties and mode of thisEngine
.- Returns:
- the operating mode of this
Engine
- Throws:
java.lang.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(EngineEvent)
,dispatchSpeechEvent(SpeechEvent)
-
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:
postEngineAllocated(long, long)
,dispatchSpeechEvent(SpeechEvent)
-
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(EngineEvent)
,dispatchSpeechEvent(SpeechEvent)
-
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:
postEngineAllocatingResources(long, long)
,dispatchSpeechEvent(SpeechEvent)
-
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(EngineEvent)
,dispatchSpeechEvent(SpeechEvent)
-
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:
postEngineDeallocated(long, long)
,dispatchSpeechEvent(SpeechEvent)
-
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(EngineEvent)
,dispatchSpeechEvent(SpeechEvent)
-
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:
postEngineDeallocatingResources(long, long)
,dispatchSpeechEvent(SpeechEvent)
-
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(EngineEvent)
,dispatchSpeechEvent(SpeechEvent)
-
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:
postEnginePaused(long, long)
,dispatchSpeechEvent(SpeechEvent)
-
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(EngineEvent)
,dispatchSpeechEvent(SpeechEvent)
-
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:
postEngineResumed(long, long)
,dispatchSpeechEvent(SpeechEvent)
-
createEngineProperties
protected abstract BaseEngineProperties 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
protected java.lang.String stateToString(long state)
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:
postEngineAllocatingResources(long, long)
,postEngineAllocated(long, long)
,postEngineDeallocatingResources(long, long)
,postEngineDeallocated(long, long)
,postEnginePaused(long, long)
,postEngineResumed(long, long)
-
toString
public java.lang.String toString()
Returns the engine name and mode for debug purposes.- Overrides:
toString
in classjava.lang.Object
- Returns:
- the engine name and mode.
-
-