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.0Engineinterface. Actual JSAPI implementations might want to extend or modify this implementation.
-
-
Field Summary
Fields Modifier and Type Field Description protected AudioManageraudioManagerTheAudioManagerfor thisEngine.protected static longCLEAR_ALL_STATEUtility state for clearing theengineState.protected java.util.CollectionengineListenersList ofEngineListenersregistered forEngineEventson thisEngine.protected EngineModeDescengineModeDescTheEngineModeDescfor thisEngine.protected EnginePropertiesenginePropertiesTheEnginePropertiesfor thisEngine.protected longengineStateA bitmask holding the current state of thisEngine.protected java.lang.ObjectengineStateLockAnObjectused for synchronizing access toengineState.
-
Constructor Summary
Constructors Constructor Description BaseEngine()Creates a newEnginein theDEALLOCATEDstate.BaseEngine(EngineModeDesc desc)Creates a newEnginein theDEALLOCATEDstate.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddEngineListener(EngineListener listener)Requests notification ofEngineEventsfrom thisEngine.voidallocate()Allocates the resources required for thisEngineand puts it into theALLOCATEDstate.protected voidcheckEngineState(long state)Convenience method that throws anEngineStateErrorif any of the bits in the passed state are set in thestate.protected abstract BaseEnginePropertiescreateEngineProperties()Factory constructor for EngineProperties object.voiddeallocate()Frees the resources of thisEnginethat were acquired during allocation and during operation and return thisEngineto theDEALLOCATED.voiddispatchSpeechEvent(SpeechEvent event)Dispatches aSpeechEvent.voidfireEngineAllocated(EngineEvent event)Utility function that sends anENGINE_ALLOCATEDevent to allEngineListenersregistered with thisEngine.voidfireEngineAllocatingResources(EngineEvent event)Utility function that sends anENGINE_ALLOCATING_RESOURCESevent to allEngineListenersregistered with thisEngine.voidfireEngineDeallocated(EngineEvent event)Utility function that sends anENGINE_DEALLOCATEDevent to allEngineListenersregistered with thisEngine.voidfireEngineDeallocatingResources(EngineEvent event)Utility function that sends aENGINE_DEALLOCATING_RESOURCESevent to allEngineListenersregistered with thisEngine.voidfireEnginePaused(EngineEvent event)Utility function that sends anENGINE_PAUSEDevent to allEngineListenersregistered with thisEngine.voidfireEngineResumed(EngineEvent event)Utility function that sends anENGINE_RESUMEDevent to allEngineListenersregistered with thisEngine.AudioManagergetAudioManager()Returns an object that provides management of the audio input or output of thisEngine.EngineModeDescgetEngineModeDesc()Gets the current operating properties and mode of thisEngine.EnginePropertiesgetEngineProperties()Gets theEnginePropertiesof thisEngine.longgetEngineState()Returns a or'ed set of flags indicating the current state of thisEngine.VocabManagergetVocabManager()Returns an object that provides management of the vocabulary for thisEngine.protected abstract voidhandleAllocate()Called from theallocatemethod.protected abstract voidhandleDeallocate()Called from thedeallocatemethod.protected abstract voidhandlePause()Called from thepausemethod.protected abstract voidhandleResume()Called from theresumemethod.voidpause()Pauses the audio stream for thisEngineand put thisEngineinto thePAUSEDstate.protected voidpostEngineAllocated(long oldState, long newState)Utility function that generates anENGINE_ALLOCATEDevent and posts it to the event queue.protected voidpostEngineAllocatingResources(long oldState, long newState)Utility function that generates anENGINE_ALLOCATING_RESOURCESevent and posts it to the event queue.protected voidpostEngineDeallocated(long oldState, long newState)Utility function that generates anENGINE_DEALLOCATEDevent and posts it to the event queue.protected voidpostEngineDeallocatingResources(long oldState, long newState)Utility function that generatesENGINE_DEALLOCATING_RESOURCESevent and posts it to the event queue.protected voidpostEnginePaused(long oldState, long newState)Utility function that generates anENGINE_PAUSEDevent and posts it to the event queue.protected voidpostEngineResumed(long oldState, long newState)Utility function that generates anENGINE_RESUMEDevent and posts it to the event queue.voidremoveEngineListener(EngineListener listener)Removes anEngineListenerfrom the list ofEngineListeners.voidresume()Resumes the audio stream for thisEngineand put thisEngineinto theRESUMEDstate.protected voidsetEngineModeDesc(EngineModeDesc desc)Sets the current operating properties and mode of thisEngine.protected long[]setEngineState(long clear, long set)Updates thisEnginestate by clearing defined bits, then setting other specified bits.protected java.lang.StringstateToString(long state)Returns aStringof the names of all theEnginestates in the givenEnginestate.booleantestEngineState(long state)Returnstrueif this state of thisEnginematches the specified state.java.lang.StringtoString()Returns the engine name and mode for debug purposes.voidwaitEngineState(long state)Blocks the calling thread until thisEngineis in a specified state.
-
-
-
Field Detail
-
engineState
protected long engineState
A bitmask holding the current state of thisEngine.
-
engineStateLock
protected java.lang.Object engineStateLock
AnObjectused for synchronizing access toengineState.- See Also:
engineState
-
engineListeners
protected java.util.Collection engineListeners
List ofEngineListenersregistered forEngineEventson thisEngine.
-
audioManager
protected AudioManager audioManager
TheAudioManagerfor thisEngine.
-
engineModeDesc
protected EngineModeDesc engineModeDesc
TheEngineModeDescfor thisEngine.
-
engineProperties
protected EngineProperties engineProperties
TheEnginePropertiesfor 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
EngineEventis issued each time thisEnginechanges state.The
getEngineStatemethod can be called successfully in anyEnginestate.- Returns:
- the current state of this
Engine - See Also:
getEngineState(),waitEngineState(long)
-
waitEngineState
public void waitEngineState(long state) throws java.lang.InterruptedException, java.lang.IllegalArgumentExceptionBlocks the calling thread until thisEngineis in a specified state.All state bits specified in the
stateparameter must be set in order for the method to return, as defined for thetestEngineStatemethod. If thestateparameter defines an unreachable state (e.g.PAUSED | RESUMED) an exception is thrown.The
waitEngineStatemethod can be called successfully in anyEnginestate.- 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.IllegalArgumentExceptionReturnstrueif this state of thisEnginematches 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
Synthesizerqueue is empty, irrespective of the pause/resume and allocation states.if (synth.testEngineState(Synthesizer.QUEUE_EMPTY)) ...The
testEngineStatemethod is equivalent to:if ((engine.getEngineState() & state) == state)The
testEngineStatemethod can be called successfully in anyEnginestate.- Parameters:
state- a bitmask of the states to test for- Returns:
trueif thisEnginematchesstate; otherwisefalse- Throws:
java.lang.IllegalArgumentException- if the specified state is unreachable
-
setEngineState
protected long[] setEngineState(long clear, long set)Updates thisEnginestate 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 thisEngineand puts it into theALLOCATEDstate. When this method returns successfully theALLOCATEDbit of thisEnginestate is set, and thetestEngineState(Engine.ALLOCATED)method returnstrue.During the processing of the method, this
Engineis temporarily in theALLOCATING_RESOURCESstate.- Throws:
EngineException- if thisEnginecannot be allocatedEngineStateError- if thisEngineis in theDEALLOCATING_RESOURCESstate- See Also:
deallocate()
-
handleAllocate
protected abstract void handleAllocate() throws EngineExceptionCalled from theallocatemethod. Override this in subclasses.- Throws:
EngineException- if problems are encountered- See Also:
allocate()
-
deallocate
public void deallocate() throws EngineException, EngineStateErrorFrees the resources of thisEnginethat were acquired during allocation and during operation and return thisEngineto theDEALLOCATED. When this method returns theDEALLOCATEDbit of thisEnginestate is set so thetestEngineState(Engine.DEALLOCATED)method returnstrue.During the processing of the method, this
Engineis temporarily in theDEALLOCATING_RESOURCESstate.A deallocated engine can be re-started with a subsequent call to
allocate.- Throws:
EngineException- if thisEnginecannot be deallocatedEngineStateError- if thisEngineis in theALLOCATING_RESOURCESstate- See Also:
allocate()
-
handleDeallocate
protected abstract void handleDeallocate() throws EngineExceptionCalled from thedeallocatemethod. Override this in subclasses.- Throws:
EngineException- if thisEnginecannot be deallocated.
-
pause
public void pause() throws EngineStateErrorPauses the audio stream for thisEngineand put thisEngineinto thePAUSEDstate.- Throws:
EngineStateError- if thisEngineis in theDEALLOCATING_RESOURCESorDEALLOCATEDstate.
-
handlePause
protected abstract void handlePause()
Called from thepausemethod. Override this in subclasses.
-
resume
public void resume() throws AudioException, EngineStateErrorResumes the audio stream for thisEngineand put thisEngineinto theRESUMEDstate.- Throws:
AudioException- if unable to gain access to the audio channelEngineStateError- if thisEngineis in theDEALLOCATING_RESOURCESorDEALLOCATEDstate
-
handleResume
protected abstract void handleResume()
Called from theresumemethod. 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. Returnsnullif thisEnginedoes not support vocabulary management.- Returns:
- the vocabulary manager of this
Engine - Throws:
EngineStateError- if thisEnginein theDEALLOCATING_RESOURCESorDEALLOCATEDstate
-
getEngineProperties
public EngineProperties getEngineProperties()
Gets theEnginePropertiesof thisEngine. Must be set in subclasses.- Returns:
- the
EnginePropertiesof thisEngine.
-
getEngineModeDesc
public EngineModeDesc getEngineModeDesc() throws java.lang.SecurityExceptionGets 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 ofEngineEventsfrom thisEngine.- Parameters:
listener- the listener to add.
-
removeEngineListener
public void removeEngineListener(EngineListener listener)
Removes anEngineListenerfrom the list ofEngineListeners.- Parameters:
listener- the listener to remove.
-
postEngineAllocated
protected void postEngineAllocated(long oldState, long newState)Utility function that generates anENGINE_ALLOCATEDevent and posts it to the event queue. EventuallyfireEngineAllocatedwill be called by thedispatchSpeechEventas a result of this action.- Parameters:
oldState- the old state of thisEnginenewState- the new state of thisEngine- See Also:
fireEngineAllocated(EngineEvent),dispatchSpeechEvent(SpeechEvent)
-
fireEngineAllocated
public void fireEngineAllocated(EngineEvent event)
Utility function that sends anENGINE_ALLOCATEDevent to allEngineListenersregistered with thisEngine. Called bydispatchSpeechEvent.- Parameters:
event- theENGINE_ALLOCATEDevent- See Also:
postEngineAllocated(long, long),dispatchSpeechEvent(SpeechEvent)
-
postEngineAllocatingResources
protected void postEngineAllocatingResources(long oldState, long newState)Utility function that generates anENGINE_ALLOCATING_RESOURCESevent and posts it to the event queue. EventuallyfireEngineAllocatingResourceswill be called bydispatchSpeechEventas a result of this action.- Parameters:
oldState- the old state of thisEnginenewState- the new state of thisEngine- See Also:
fireEngineAllocatingResources(EngineEvent),dispatchSpeechEvent(SpeechEvent)
-
fireEngineAllocatingResources
public void fireEngineAllocatingResources(EngineEvent event)
Utility function that sends anENGINE_ALLOCATING_RESOURCESevent to allEngineListenersregistered with thisEngine. Called bydispatchSpeechEvent.- Parameters:
event- theENGINE_ALLOCATING_RESOURCESevent- See Also:
postEngineAllocatingResources(long, long),dispatchSpeechEvent(SpeechEvent)
-
postEngineDeallocated
protected void postEngineDeallocated(long oldState, long newState)Utility function that generates anENGINE_DEALLOCATEDevent and posts it to the event queue. EventuallyfireEngineDeallocatedwill be called bydispatchSpeechEventas a result of this action.- Parameters:
oldState- the old state of thisEnginenewState- the new state of thisEngine- See Also:
fireEngineDeallocated(EngineEvent),dispatchSpeechEvent(SpeechEvent)
-
fireEngineDeallocated
public void fireEngineDeallocated(EngineEvent event)
Utility function that sends anENGINE_DEALLOCATEDevent to allEngineListenersregistered with thisEngine. Called bydispatchSpeechEvent.- Parameters:
event- theENGINE_DEALLOCATEDevent- See Also:
postEngineDeallocated(long, long),dispatchSpeechEvent(SpeechEvent)
-
postEngineDeallocatingResources
protected void postEngineDeallocatingResources(long oldState, long newState)Utility function that generatesENGINE_DEALLOCATING_RESOURCESevent and posts it to the event queue. EventuallyfireEngineAllocatingResourceswill be called bydispatchSpeechEventas a result of this action.- Parameters:
oldState- the old state of thisEnginenewState- the new state of thisEngine- See Also:
fireEngineDeallocatingResources(EngineEvent),dispatchSpeechEvent(SpeechEvent)
-
fireEngineDeallocatingResources
public void fireEngineDeallocatingResources(EngineEvent event)
Utility function that sends aENGINE_DEALLOCATING_RESOURCESevent to allEngineListenersregistered with thisEngine. Called bydispatchSpeechEvent.- Parameters:
event- theENGINE_DEALLOCATING_RESOURCESevent- See Also:
postEngineDeallocatingResources(long, long),dispatchSpeechEvent(SpeechEvent)
-
postEnginePaused
protected void postEnginePaused(long oldState, long newState)Utility function that generates anENGINE_PAUSEDevent and posts it to the event queue. EventuallyfireEnginePausedwill be called bydispatchSpeechEventas a result of this action.- Parameters:
oldState- the old state of thisEnginenewState- the new state of thisEngine- See Also:
fireEnginePaused(EngineEvent),dispatchSpeechEvent(SpeechEvent)
-
fireEnginePaused
public void fireEnginePaused(EngineEvent event)
Utility function that sends anENGINE_PAUSEDevent to allEngineListenersregistered with thisEngine. Called bydispatchSpeechEvent.- Parameters:
event- theENGINE_PAUSEDevent- See Also:
postEnginePaused(long, long),dispatchSpeechEvent(SpeechEvent)
-
postEngineResumed
protected void postEngineResumed(long oldState, long newState)Utility function that generates anENGINE_RESUMEDevent and posts it to the event queue. EventuallyfireEngineResumedwill be called bydispatchSpeechEventas a result of this action.- Parameters:
oldState- the old state of thisEnginenewState- the new state of thisEngine- See Also:
fireEngineResumed(EngineEvent),dispatchSpeechEvent(SpeechEvent)
-
fireEngineResumed
public void fireEngineResumed(EngineEvent event)
Utility function that sends anENGINE_RESUMEDevent to allEngineListenersregistered with thisEngine. Called bydispatchSpeechEvent.- Parameters:
event- theENGINE_RESUMEDevent- See Also:
postEngineResumed(long, long),dispatchSpeechEvent(SpeechEvent)
-
createEngineProperties
protected abstract BaseEngineProperties createEngineProperties()
Factory constructor for EngineProperties object.- Returns:
- a
BaseEnginePropertiesobject specific to a subclass.
-
checkEngineState
protected void checkEngineState(long state) throws EngineStateErrorConvenience method that throws anEngineStateErrorif any of the bits in the passed state are set in thestate.- Parameters:
state- theEnginestate 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 aStringof the names of all theEnginestates in the givenEnginestate.- Parameters:
state- the bitmask of states- Returns:
- a
Stringcontaining the names of all the states set instate
-
dispatchSpeechEvent
public void dispatchSpeechEvent(SpeechEvent event)
Dispatches aSpeechEvent. The dispatcher should notify allEngineListenersfrom this method. TheSpeechEventwas added via the various post methods of this class.- Specified by:
dispatchSpeechEventin interfaceSpeechEventDispatcher- Parameters:
event- theSpeechEventto 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:
toStringin classjava.lang.Object- Returns:
- the engine name and mode.
-
-