|
JSAPI 2.0 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface Recognizer
Provides access to speech recognition capabilities.
The Recognizer
interface extends the
Engine
interface and
so inherits the basic Engine
capabilities and provides additional
specialized capabilities.
The primary capabilities provided are Recognizer
control
and Result
handling.
An application is responsible for providing a Recognizer
with
Grammar
s through the GrammarManager
.
A Grammar
defines a set of words (technically known as tokens)
and defines acceptable patterns in which the tokens may be spoken.
When a Grammar
is active,
the Recognizer
listens for speech in the
incoming audio that matches the Grammar
.
When speech is detected, the Recognizer
produces a Result
.
The Result
object is passed to the application with information
on which words were heard.
The types of Grammar
s, mechanisms for creating, modifying and
managing Grammar
s, types of Result
s,
Result
handling and other Recognizer
functions are described in more detail below.
Recognizer
is created by a call to the
createEngine
method
of the EngineManager
class.
A Recognizer
for the default SpeechLocale
is created as follows:
Recognizer rec = (Recognizer)
EngineManager.createEngine(RecognizerMode.DEFAULT);
The recognition
package overview contains a
recognition example
.
Detailed descriptions of the procedures for locating,
selecting, creating and initializing a Recognizer are provided in the
documentation for the
EngineManager
.
GrammarManager
provides the functionality to create and
manipulate Grammar
s used by the Recognizer
.
The Recognizer
must be provided with one or more Grammar
s
that indicate what words and word sequences it should recognize.
The basic process for dealing with a Grammar follows.
pause
the Recognizer
(if not already PAUSED
)
Grammar
s according to
the application context.
Grammar
s for recognition as needed.
ResultListener
to either the Recognizer
or
Grammar
to receive ResultEvent
s.
resume
the Recognizer
to atomically commit
any Grammar
changes.
Grammar
s when
they are no longer needed.
Grammar
s (particularly large Grammar
s)
can be computationally expensive.
Loading a new Grammar
can take appreciable time.
Updates to a Grammar
may also be slow.
Therefore, applications should take precautions to build Grammar
s in
advance and to modify them only when necessary.
Furthermore, an application should minimize the number of Grammar
s
active at any point in time.
Recognizer
inherits two pairs of substates from the
ALLOCATED
state from the Engine
interface:
The initial states are DEFOCUSED
and PAUSED
.
An application should call
requestFocus
and
resume
after loading Grammar
s.
The Recognizer
interface adds two substates to the RESUMED
state:
LISTENING
and PROCESSING
RESUMED
, the initial state is LISTENING
while the
Recognizer
listens for speech.
Once speech is found, the Recognizer
remains in the
PROCESSING
state until it produces a Result
.
See the Table of Diagrams for state transition diagrams.
Recognizer
uses a two state substate-system with FOCUSED
and DEFOCUSED
to indicate whether this instance of the
Recognizer
currently has the recognition focus.
Focus is important in an environment with multiple applications
because more than one application can share an
underlying speech recognition engine, but the user gives recognition focus
to only one application at a time.
Since it is normal for an application to use only one Recognizer
,
recognition focus and application focus normally mean the same thing.
(Applications with multiple Recognizer
s cannot make this assumption.)
Focus is not usually relevant in telephony applications or some embedded applications in which there is a single input audio stream to a single application.
The focus status is a key factor in determining the activation
of Grammar
s and therefore in determining when Result
s will
and will not be generated.
The
activation conditions
for Grammar
s and the role of focus are
described in the Grammar
interface.
When focus is received, an ENGINE_FOCUSED
event
is issued to any RecognizerListener
s attached to this Engine
.
When focus is released or otherwise lost,
an ENGINE_DEFOCUSED
event is issued
to any RecognizerListener
s attached to this Engine
.
Applications can request focus for the Recognizer
by calling
the requestFocus
method.
This asynchronous method may return before focus is received.
To determine when focus is on,
applications can check for ENGINE_FOCUSED
events or
test the FOCUSED
bit of Engine
state.
Applications can release focus from the Recognizer
by calling
the releaseFocus
method.
This asynchronous method may return before focus is lost.
To determine when focus is off,
applications can check for ENGINE_DEFOCUSED
events or test
the DEFOCUSED
bit of Engine
state.
In GUI-based environments, it is normal (but not required)
for recognition focus to follow GUI focus.
Therefore, for example, it is common for the requestFocus
method
to be called
on AWT or Swing events such as FocusEvent
and WindowEvent
or in association with a MIDlet's startApp
method.
A well-behaved application only requests focus when it knows that it has the focus of the user (the user is talking to it and not to other applications). A well-behaved application will also release focus as soon as it finishes with it.
RESUMED
follows.
This assumes that Grammar
s have been defined as described above and
committed with a call to resume
.
The call to resume
transitions the Recognizer
from the
PAUSED
state to the RESUMED
state preceded by the events
CHANGES_COMMITTED
/CHANGES_REJECTED
,
ENGINE_RESUMED
, and
RECOGNIZER_LISTENING
.
Recognizer
starts in the LISTENING
state with a
certain set of
active
Grammar
s.
When incoming audio is detected that may match an active
Grammar
, the Recognizer
transitions to the
PROCESSING
state with a RECOGNIZER_PROCESSING
RecognizerEvent
.
The Recognizer
then creates a new Result
and issues a
RESULT_CREATED
ResultEvent
to provide the Result
to the application.
Recognizer
remains in the PROCESSING
state until
it completes recognition of the Result
.
Recognizer
indicates completion of recognition by issuing
an ENGINE_PAUSED
EngineEvent
to transition from the
PROCESSING
substate to the PAUSED
state.
Once in that state, it issues a Result
finalization
ResultEvent
to ResultListener
s
(RESULT_ACCEPTED
or RESULT_REJECTED
).
Recognizer
remains in the PAUSED
state until
processing of the Result
finalization ResultEvent
is completed.
Applications will usually make any necessary Grammar
changes
while the Recognizer
is PAUSED
.
The Recognizer
buffers incoming audio
in this scenario.
Buffering allows a user to continue speaking without loss of
speech data.
Once the Recognizer
returns to the RESUMED
state and
LISTENING
substate, any buffered audio
is processed to give the user the perception of real-time processing.
Recognizer
commits all Grammar
changes,
issues CHANGES_COMMITTED
/CHANGES_REJECTED
and
ENGINE_RESUMED
events to any RecognizerListener
s
and returns to the RESUMED
/LISTENING
state/substate.
It also issues
GRAMMAR_CHANGES_COMMITTED
/GRAMMAR_CHANGES_REJECTED
GrammarEvent
s to GrammarListener
s of
changed Grammar
s.
The Grammar
commit applies all Grammar
changes made
at any point up to the end of Result
finalization,
typically including changes made in the
Result
finalization ResultEvent
s.
Recognizer
listens in the LISTENING
state for speech
that matches the updated Grammar
s.
RECOGNIZER_PROCESSING
RecognizerEvent
is triggered by user action:
starting and stopping speaking.
The ENGINE_PAUSED
and ENGINE_RESUMED
EngineEvents are
triggered programmatically by the Recognizer
after issuing events to
application listeners during Result
finalization.
When a non-speech event occurs that changes the application state
or application data,
it is often necessary to update the Recognizer
's Grammar
s.
Furthermore, it is typically necessary to do this as if the change occurred
in real time - at exactly the point in time at which the event occurred.
The pause
and resume
methods of a Recognizer
may be used to handle non-speech asynchronous events.
The typical cycle for updating Grammar
s is as follows:
Recognizer
is in the LISTENING
state
(the user is not currently speaking).
As soon as the non-speech event is received, the application calls
pause(BUFFER_MODE)
to indicate that it is
about to change Grammar
s.
RECOGNIZER_PAUSED
RecognizerEvent
and transitions
from the LISTENING
substate to the PAUSED
state.
Grammar
s.
resume
method.
The Recognizer
commits the Grammar
updates atomically,
issues a CHANGES_COMMITTED
/CHANGES_REJECTED
RecognizerEvent
to transition from the PAUSED
state
back to the RESUMED
state and LISTENING
substate,
and issues
GRAMMAR_CHANGES_COMMITTED
/GRAMMAR_CHANGES_REJECTED
GrammarEvent
s to all changed Grammar
s.
Recognizer
resumes recognition of any buffered audio and
then live audio with the updated Grammar
s.
CHANGES_COMMITTED
/CHANGES_REJECTED
RecognizerEvent
occurs,
the audio is processed as if the updated Grammar
s were applied exactly at
the time of the asynchronous non-speech event.
The user has the perception of real-time processing.
Although audio is buffered in the PAUSED
state with
pause(BUFFER_MODE)
,
applications should makes changes and resume
as quickly as possible.
This minimizes the possibility of a buffer overrun (NOT_BUFFERING).
It also reduces delays in recognizing speech and responding to the user.
Consider the two cases in which speech and non-speech events interact:
Recognizer
is in either the
PROCESSING
substate or the PAUSED
state.
In both cases the event processing for the non-speech is no different
than normal.
The non-speech event handler calls pause(BUFFER_MODE)
to indicate it
is about to change Grammar
s, makes the Grammar
changes,
and then calls resume
to apply the commit the changes atomically.
The effect is that the
CHANGES_COMMITTED
/CHANGES_REJECTED
RecognizerEvent
that would occur in the
normal event cycle
may be delayed until the resume
method
is explicitly called and Result
finalization completes.
At that point, the Recognizer
applies changes made in response
to both the speech and non-speech events.
Recognizer
is in the PAUSED
state,
speech is buffered, and the speech event is actually delayed until
the Recognizer
returns to the LISTENING
substate.
Once the Recognizer
returns to the LISTENING
substate,
the incoming speech is processed with the
normal event cycle.
Engine
is in the ALLOCATED
state
and not DEALLOCATING_RESOURCES
.
The call blocks if the Engine
is in the
ALLOCATING_RESOURCES
state and completes when the Engine
reaches the ALLOCATED
state.
Otherwise, an exception is thrown for an Engine
in the DEALLOCATING_RESOURCES
or DEALLOCATED
states.
EngineManager
,
createEngine
,
RecognizerEvent
,
RecognizerListener
,
Grammar
,
RuleGrammar
,
GrammarManager
,
Result
,
FinalResult
Field Summary | |
---|---|
static int |
BUFFER_MODE
Flag used to indicate that audio buffering should continue while PAUSED . |
static long |
BUFFERING
Bit of state that is set when an ALLOCATED Recognizer
is buffering audio. |
static long |
LISTENING
Bit of state that is set when an ALLOCATED Recognizer
is listening to incoming audio for speech that may match an active
Grammar . |
static long |
NOT_BUFFERING
Bit of state that is set when an ALLOCATED Recognizer
is not buffering audio. |
static long |
PROCESSING
Bit of state that is set when an ALLOCATED Recognizer
is producing a Result for incoming speech that may match
an active Grammar . |
Fields inherited from interface Engine |
---|
ALLOCATED, ALLOCATING_RESOURCES, ASYNCHRONOUS_MODE, DEALLOCATED, DEALLOCATING_RESOURCES, DEFOCUSED, ERROR_OCCURRED, FOCUSED, IMMEDIATE_MODE, PAUSED, RESUMED |
Method Summary | |
---|---|
void |
addRecognizerListener(RecognizerListener listener)
Requests notifications of RecognizerEvent s related to
this Recognizer . |
void |
addResultListener(ResultListener listener)
Requests notifications of all ResultEvent s for all
Result s produced by this Recognizer . |
void |
allocate()
Allocates the resources required for this Engine and puts it into
the ALLOCATED state. |
void |
allocate(int mode)
Allocates the resources required for this Engine
using the mode specified
and puts it into the ALLOCATED state. |
void |
deallocate()
Deallocates the resources acquired for this Engine
and returns it to the DEALLOCATED state. |
void |
deallocate(int mode)
Deallocates the resources acquired for this Engine
using the mode specified
and returns it to the DEALLOCATED state. |
GrammarManager |
getGrammarManager()
Gets the GrammarManager for this Recognizer . |
RecognizerProperties |
getRecognizerProperties()
Returns the properties for this Recognizer . |
int |
getResultMask()
Gets the mask to filter ResultEvent s for added ResultListener s. |
SpeakerManager |
getSpeakerManager()
Returns the SpeakerManager for this Recognizer . |
void |
pause()
Pauses the audio processing for this Engine and
puts it into the PAUSED state. |
void |
pause(int mode)
Pauses a Recognizer with additional options. |
void |
processGrammars()
Informs this Recognizer that Grammar s have changed. |
void |
releaseFocus()
Releases the recognition focus from this Recognizer . |
void |
removeRecognizerListener(RecognizerListener listener)
Removes a RecognizerListener from this Recognizer . |
void |
removeResultListener(ResultListener listener)
Removes a ResultListener from this Recognizer . |
void |
requestFocus()
Requests the recognition focus for this Recognizer . |
boolean |
resume()
Resumes audio processing for this Engine by
entering the RESUMED state. |
void |
setResultMask(int mask)
Sets the mask to filter ResultEvent s for
added ResultListener s. |
Methods inherited from interface Engine |
---|
getAudioManager, getEngineMask, getEngineMode, getEngineState, getSpeechEventExecutor, getVocabularyManager, setEngineMask, setSpeechEventExecutor, testEngineState, waitEngineState, waitEngineState |
Field Detail |
---|
static final int BUFFER_MODE
PAUSED
.
The description of the pause(int)
method
includes more detail.
pause(int)
,
PAUSED
,
Constant Field Valuesstatic final long LISTENING
ALLOCATED
Recognizer
is listening to incoming audio for speech that may match an active
Grammar
.
A RECOGNIZER_LISTENING
event is issued to indicate
a transition into the LISTENING
substate of
the RESUMED
state.
A RECOGNIZER_PROCESSING
event is issued to indicate
a transition out of the LISTENING
substate and
into the PROCESSING
substate.
An ENGINE_PAUSED
event is issued to indicate a transition out
of the LISTENING
substate and into the PAUSED
state.
ALLOCATED
,
RESUMED
,
PROCESSING
,
PAUSED
,
pause
,
RECOGNIZER_LISTENING
,
RECOGNIZER_PROCESSING
,
ENGINE_PAUSED
,
getEngineState
,
waitEngineState
,
Constant Field Valuesstatic final long PROCESSING
ALLOCATED
Recognizer
is producing a Result
for incoming speech that may match
an active Grammar
.
A RECOGNIZER_PROCESSING
event is issued to indicate
a transition into the PROCESSING
substate
from the LISTENING
substate
when the start of a new Result
is detected.
An ENGINE_PAUSED
event is issued to indicate
a transition out of the PROCESSING
substate and
into the PAUSED
state when recognition
of a Result
is completed.
ALLOCATED
,
LISTENING
,
Engine.PAUSED
,
pause
,
RECOGNIZER_PROCESSING
,
ENGINE_PAUSED
,
getEngineState
,
waitEngineState
,
Result
,
Constant Field Valuesstatic final long BUFFERING
ALLOCATED
Recognizer
is buffering audio.
It is normal for a Recognizer
to buffer a limited amount of
audio so that it can later process the audio without missing speech.
A Recognizer
is first allocated
in the NOT_BUFFERING
state.
A call to resume
results in a transition
from the NOT_BUFFERING
to the BUFFERING
state
if not already there.
A Recognizer
will leave the BUFFERING
state
if buffer overflow occurs.
Recovery is described in the three states
PAUSED
, LISTENING
, and PROCESSING
.
An application can also leave the BUFFERING
state
by calling pause()
.
In this case, buffering resumes by calling resume
.
A RECOGNIZER_BUFFERING
event is issued to indicate
a transition into
the BUFFERING
state from the NOT_BUFFERING
state.
ALLOCATED
,
NOT_BUFFERING
,
PAUSED
,
LISTENING
,
PROCESSING
,
pause
,
resume
,
RECOGNIZER_BUFFERING
,
getEngineState
,
waitEngineState
,
Constant Field Valuesstatic final long NOT_BUFFERING
ALLOCATED
Recognizer
is not buffering audio.
It is normal for a Recognizer to buffer a limited amount of audio so
that it can later process the audio without missing speech.
A Recognizer
is first allocated
in the NOT_BUFFERING
state.
A call to resume
results in a transition
from the NOT_BUFFERING
to the BUFFERING
state
if not already there.
A Recognizer
will enter the NOT_BUFFERING
state
if buffer overflow occurs.
Recovery is described in the three states
PAUSED
, LISTENING
, and PROCESSING
.
An application can also enter the NOT_BUFFERING
state
by calling pause
.
In this case, buffering resumes by calling resume
.
A RECOGNIZER_NOT_BUFFERING
event is issued to indicate
a transition into
the NOT_BUFFERING
state from the BUFFERING
state.
ALLOCATED
,
BUFFERING
,
PAUSED
,
LISTENING
,
PROCESSING
,
pause
,
resume
,
RECOGNIZER_NOT_BUFFERING
,
getEngineState
,
waitEngineState
,
Constant Field ValuesMethod Detail |
---|
void addRecognizerListener(RecognizerListener listener)
RecognizerEvent
s related to
this Recognizer
.
An application can attach multiple RecognizerListener
s to
this Recognizer
.
A single RecognizerListener
can be attached to
multiple Recognizer
s.
A RecognizerListener
is removed with the
removeRecognizerListener
method.
A RecognizerListener
can be attached or removed in
any Recognizer
state.
Individual RecognizerEvent
s may be filtered with
setEngineMask
.
listener
- the RecognizerListener
to attach to
this Recognizer
RecognizerEvent
,
RecognizerListener
,
removeRecognizerListener
,
setEngineMask
void addResultListener(ResultListener listener)
ResultEvent
s for all
Result
s produced by this Recognizer
.
An application can attach multiple ResultListener
s to
this Recognizer
.
A single ResultListener
can be attached to
multiple Recognizer
s.
A ResultListener
is removed with the
removeResultListener
method.
ResultListener
s attached to a Recognizer
are
the only ResultListener
s to receive the
RESULT_CREATED
event in addition to
all subsequent ResultEvent
s.
ResultListener
objects can also be attached to
any Grammar
or to any Result
.
The addResultListener
methods for the respective classes
describe ResultEvent
s received.
A ResultListener
can be attached or removed in
any Recognizer
state.
Individual ResultEvent
s may be filtered with
setResultMask
.
listener
- the ResultListener
to attach to
this Recognizer
Grammar
,
Result
,
Grammar.addResultListener
,
Result.addResultListener
,
removeResultListener
,
ResultEvent
,
RESULT_CREATED
,
setResultMask
int getResultMask()
ResultEvent
s for added ResultListener
s.
See setResultMask
for details.
The event mask can be set and retrieved in any Recognizer
state.
ResultEvent
sResultEvent
,
ResultListener
,
setResultMask
void setResultMask(int mask)
ResultEvent
s for
added ResultListener
s.
It can improve efficiency to mask events that are not used.
Events of interest can be seen by using the logical OR of the
event constants defined in the ResultEvent
class.
ResultEvent.DEFAULT_MASK
is the initial value.
The event mask can be set and retrieved in any Recognizer
state.
This method affects filtering of all attached ResultListener
s.
mask
- the mask for ResultEvent
sResultEvent
,
ResultEvent.DEFAULT_MASK
,
ResultListener
,
getResultMask
GrammarManager getGrammarManager()
GrammarManager
for this Recognizer
.
This method never returns null
.
It may be called in any state of a Recognizer
.
GrammarManager
Grammar
,
RuleGrammar
RecognizerProperties getRecognizerProperties()
Recognizer
.
The RecognizerProperties
are available
in any state of this Recognizer
.
However, changes only take effect once a Recognizer
reaches
the ALLOCATED
state.
RecognizerProperties
object for
this Recognizer
SpeakerManager getSpeakerManager() throws SecurityException
SpeakerManager
for this Recognizer
.
This method may return null
if this Recognizer
does not store speaker data as indicated
as indicated by getSpeakerProfiles
.
A speaker-independent Recognizer
, for example, handles
all speakers the same.
This method may be called in any state of a Recognizer
.
The SpeakerManager
methods
listKnownSpeakers
and setCurrentSpeaker
operate in any Recognizer
state but only take effect in the
ALLOCATED
state.
This allows an application to set the initial SpeakerProfile
prior to calling allocate
for this Recognizer
.
SpeakerManager
for this Recognizer
SecurityException
- if the application does not have permissionallocate
,
SpeakerManager
,
listKnownSpeakers
,
setCurrentSpeaker
,
SpeakerProfile
,
getSpeakerProfiles
javax.speech.recognition.SpeakerManager
void processGrammars() throws EngineStateException
Recognizer
that Grammar
s have changed.
The GrammarManager
describes ways in which Grammar
s
may change.
Calling this method is not required.
Any Grammar
updates are atomically committed with
resume
.
This method may be helpful for applications that call pause
and
anticipate some time between the time Grammar
s are ready and
the next call to resume
.
Any problems with Grammar
s are reported with resume
.
Calling this method does not result in any state changes or events.
It is not an error to call processGrammars
when
no Grammar
s have been changed.
It is not an error to call processGrammars
multiple times.
The processGrammars
method operates as defined when the
Recognizer
is in the ALLOCATED
and PAUSED
states.
EngineStateException
- if this Recognizer
is
not ALLOCATED
and PAUSED
Grammar
,
GrammarManager
,
Engine.ALLOCATED
,
Engine.PAUSED
,
pause
,
resume
void releaseFocus() throws EngineStateException
Recognizer
.
This request is made asynchronously.
An ENGINE_DEFOCUSED
event is issued to attached
RecognizerListener
s once the focus
is released and the Recognizer
state changes
from FOCUSED
to DEFOCUSED
.
Since focus is a key factor in the activation policy
for Grammar
s, an ENGINE_DEFOCUSED
event is followed by
a GRAMMAR_DEACTIVATED
event to the GrammarListener
s of
each Grammar
that loses activation.
The Grammar
interface details the role of
focus and Grammar
activation conditions.
Since only one application may have recognition focus at any time, applications should release focus whenever it is not required. Focus is discussed in more detail with Recognizer focus states.
It is not an error for an application to release focus for
a Recognizer
that does not have focus.
Focus is implicitly released after calling deallocate
.
This method operates as defined when the standard Conditions for Operation apply.
EngineStateException
- when not in the standard Conditions for OperationrequestFocus
,
deallocate
,
FOCUSED
,
DEFOCUSED
,
ENGINE_DEFOCUSED
,
getEngineState
,
GRAMMAR_DEACTIVATED
,
Grammar
void removeRecognizerListener(RecognizerListener listener)
RecognizerListener
from this Recognizer
.
A RecognizerListener
can be removed
in any state of a Recognizer
.
listener
- the RecognizerListener to be removedaddRecognizerListener
void removeResultListener(ResultListener listener)
ResultListener
from this Recognizer
.
A ResultListener
can be attached or removed
in any state of a Recognizer
.
listener
- the ResultListener
to be removedaddResultListener
void requestFocus() throws EngineStateException
Recognizer
.
This request is made asynchronously.
When the focus is received, an ENGINE_FOCUSED
event is issued to
attached RecognizerListener
s and the Recognizer
changes state from DEFOCUSED
to FOCUSED
.
Since focus is a key factor in the activation policy
for Grammar
s, an ENGINE_FOCUSED
event is followed by
a GRAMMAR_ACTIVATED
event to the GrammarListener
s of
each Grammar
that is activated.
The Grammar
interface details the role of
focus and Grammar
activation conditions.
Since only one application may have recognition focus at any time, applications should only request focus when confident that the user is speaking to that application. Focus is discussed in more detail with Recognizer focus states.
It is not an error for an application to request focus for
a Recognizer
that already has focus.
Focus is implicitly released after calling deallocate
.
This method operates as defined when the standard Conditions for Operation apply.
EngineStateException
- when not in the standard Conditions for OperationreleaseFocus
,
deallocate()
,
FOCUSED
,
ENGINE_FOCUSED
,
getEngineState
,
GRAMMAR_ACTIVATED
,
Grammar
void allocate() throws AudioException, EngineException, EngineStateException, SecurityException
Engine
and puts it into
the ALLOCATED
state.
When this method returns successfully
the ALLOCATED
bit of Engine
state is set,
and the testEngineState(Engine.ALLOCATED)
method
returns true
.
During the processing of the method,
the Engine
is temporarily in the ALLOCATING_RESOURCES
state.
While this method is being processed,
events are issued to any EngineListener
s
attached to the Engine
to indicate state changes
(see setEngineMask
for event filtering).
First, as the Engine
changes from the DEALLOCATED
to
the ALLOCATING_RESOURCES
state,
an ENGINE_ALLOCATING_RESOURCES
event is issued.
As the allocation process completes, the Engine
moves from the
ALLOCATING_RESOURCES
state to the ALLOCATED
state and
an ENGINE_ALLOCATED
event is issued.
The allocate method should be called for an Engine
in
the DEALLOCATED
state.
The method has no effect for an Engine in either
the ALLOCATING_RESOURCES
or ALLOCATED
states.
The method throws an exception in the DEALLOCATING_RESOURCES
state.
If any problems are encountered during the allocation process so that
the Engine
cannot be allocated, the Engine
returns to
the DEALLOCATED
state
(with an ENGINE_DEALLOCATED
event),
and an EngineException
is thrown.
Allocating the resources for an Engine
may be fast
(less than a second)
or slow (several 10s of seconds) depending upon a range of factors.
Since the allocate
method does not return
until allocation is completed,
applications may use allocate(int)
with
a flag for asynchronous operation
When the Engine
reaches the ALLOCATED
state,
other Engine
substates are determined according to the type of
Engine
.
Each Engine
subclass provides more detail.
Recognizer
s throw SecurityException
if the application does not have permission.
allocate
in interface Engine
AudioException
- if any audio request fails
EngineException
- if an allocation error occurred or the
Engine
is not operational.
EngineStateException
- if called for an Engine
in the
DEALLOCATING_RESOURCES
state
SecurityException
- if the application does not have permission
for this Engine
allocate(int)
,
deallocate()
,
getEngineState
,
ALLOCATED
,
ALLOCATING_RESOURCES
,
DEALLOCATED
,
EngineEvent
,
ENGINE_ALLOCATED
,
ENGINE_DEALLOCATED
,
setEngineMask
javax.speech.recognition.Recognizer.allocate
void allocate(int mode) throws IllegalArgumentException, AudioException, EngineException, EngineStateException, SecurityException
Engine
using the mode
specified
and puts it into the ALLOCATED
state.
When the mode
is ASYNCHRONOUS_MODE
, this method
asynchronously allocates an Engine
.
In this case, the Engine
transitions to
ALLOCATING_RESOURCES
before this method returns.
When mode
is 0
, this method operates the same
as allocate()
.
waitEngineState
may be used to wait for the
ALLOCATED
state.
Engine engine;
engine = EngineManager.createEngine(RecognizerMode.DEFAULT);
engine.allocate(Engine.ASYNCHRONOUS_MODE);
// Do other stuff while allocation takes place
...
// Now wait until allocation is complete
engine.waitEngineState(Engine.ALLOCATED);
Recognizer
s throw SecurityException
if the application does not have permission.
allocate
in interface Engine
mode
- flag indicating the operational mode
IllegalArgumentException
- if called with an unexpected mode
value
AudioException
- if any audio access fails
EngineException
- if an allocation error occurred or
the Engine
is not operational.
EngineStateException
- if called for an Engine
in
the DEALLOCATING_RESOURCES
state
SecurityException
- if the application does not have permission
for this Engine
ASYNCHRONOUS_MODE
,
allocate()
,
deallocate()
,
getEngineState
,
ALLOCATED
,
ALLOCATING_RESOURCES
,
DEALLOCATED
,
EngineEvent
,
ENGINE_ALLOCATED
,
ENGINE_DEALLOCATED
,
setEngineMask
,
waitEngineState
javax.speech.recognition.Recognizer.allocate
void deallocate() throws AudioException, EngineException, EngineStateException
Engine
and returns it to the DEALLOCATED
state.
When this method returns the DEALLOCATED
bit of Engine
state is set
so the testEngineState(Engine.DEALLOCATED)
method returns true
.
During the processing of the method, the Engine
is temporarily
in the DEALLOCATING_RESOURCES
state.
A DEALLOCATED
Engine
can be restarted with a subsequent
call to allocate
.
Engine
s should normally clean up current activities
before becoming DEALLOCATED
.
If needed, deallocate
will wait for pending operations to complete
appropriate for the type of Engine
.
The deallocate(int)
method includes a flag
to stop pending operations immediately.
Deallocating resources for an Engine
is not always immediate.
Use the deallocate(int)
method with a flag
for asynchronous operation.
The documentation for the allocate(int)
method shows an example of asynchronous operation
While this method is being processed, events are issued to any
EngineListener
s attached to the Engine
to indicate state changes.
First, as the Engine
changes from the ALLOCATED
to the
DEALLOCATING_RESOURCES
state,
an ENGINE_DEALLOCATING_RESOURCES
event is issued.
As the deallocation process completes, the Engine
moves from the
DEALLOCATING_RESOURCES
state to the DEALLOCATED
state and an
ENGINE_DEALLOCATED
event is issued.
The deallocate
method should only be called for
an Engine
in the ALLOCATED
state.
The method has no effect for an Engine
in
either the DEALLOCATING_RESOURCES
or DEALLOCATED
states.
The method throws an exception in the ALLOCATING_RESOURCES
state.
deallocate
in interface Engine
AudioException
- if any audio request fails
EngineException
- if a deallocation error occurs
EngineStateException
- if called for an Engine
in
the ALLOCATING_RESOURCES
statedeallocate(int)
,
allocate()
,
allocate(int)
,
ASYNCHRONOUS_MODE
,
IMMEDIATE_MODE
,
DEALLOCATED
,
DEALLOCATING_RESOURCES
,
EngineEvent
,
ENGINE_DEALLOCATING_RESOURCES
,
ENGINE_DEALLOCATED
void deallocate(int mode) throws IllegalArgumentException, AudioException, EngineException, EngineStateException
Engine
using the mode
specified
and returns it to the DEALLOCATED
state.
When using ASYNCHRONOUS_MODE
, this method asynchronously
deallocates an Engine
.
In this case, the Engine
transitions to
DEALLOCATING_RESOURCES
before this method returns.
When using IMMEDIATE_MODE
, this method causes the Engine
to stop pending operations immediately before beginning the
deallocation process.
The mode
values may be combined.
Calling deallocate(ASYNCHRONOUS_MODE | IMMEDIATE_MODE)
combines the behavior of both mode
s.
When mode
is 0
, this method operates the same
as deallocate()
.
deallocate
in interface Engine
mode
- flag indicating the operational mode
IllegalArgumentException
- if called with an unexpected mode
value
AudioException
- if any audio request fails
EngineException
- if a deallocation error occurs
EngineStateException
- if called for an Engine
in the
ALLOCATING_RESOURCES
statedeallocate()
,
allocate()
,
ASYNCHRONOUS_MODE
,
IMMEDIATE_MODE
,
DEALLOCATED
,
DEALLOCATING_RESOURCES
,
EngineEvent
,
ENGINE_DEALLOCATING_RESOURCES
,
ENGINE_DEALLOCATED
,
waitEngineState
void pause() throws EngineStateException
Engine
and
puts it into the PAUSED
state.
Pausing an Engine
pauses the Engine
instance and
does not affect other Engine
instances
in the current application or other applications,
even though they may use the same underlying resources.
Applications may pause
an Engine
indefinitely.
When an Engine
moves from the RESUMED
state
to the PAUSED
state,
an ENGINE_PAUSED
event is issued
to each EngineListener
attached to the Engine
.
The PAUSED
bit of the Engine
state
is set to true
when paused,
and can be tested by the getEngineState
method and
other Engine
state methods.
The PAUSED
state is a substate of the ALLOCATED
state.
An ALLOCATED
Engine
is always in either
the PAUSED
or the RESUMED
state.
It is not an exception to pause
an Engine
that is already PAUSED
.
The pause
method does not always return immediately.
Some applications may want to execute pause
in
a separate Thread
.
Note that calls to pause
and resume
nest,
so that there must be a resume
for each pause
in order for resume
to operate as described.
This method operates as defined when the standard Conditions for Operation apply.
Operational detail depends on the particular Engine
subinterface.
For Engine
s that produce audio,
pausing is analogous to pressing the "pause" button on
a tape or CD player -
audio output is paused and any unplayed audio is heard
after resume
.
For Engine
s that consume audio,
pausing is analogous to pressing a "mute" button on
a microphone -
by default, audio input is ignored and permanently lost.
Audio processing does not begin again until after resume
.
For a Recognizer
,
pausing stops processing of the audio input stream as close
as possible to the time of the call to pause
.
By default, anything a user says while the Recognizer
is
PAUSED
will not be heard by the Recognizer
.
The pause(int)
method provides
more detail and options.
This method is equivalent to pause(0)
.
pause
in interface Engine
EngineStateException
- when not in the standard Conditions for Operationpause(int)
,
resume
,
PAUSED
,
RESUMED
,
getEngineState
,
ENGINE_PAUSED
void pause(int mode) throws IllegalArgumentException, EngineStateException
Recognizer
with additional options.
The pause()
method describes the basic functionality.
For a Recognizer
,
pausing stops processing of the audio input stream as close
as possible to the time of the call to pause
.
Pausing a Recognizer
allows an application to
modify Grammar
s and update activation.
By default (BUFFER_MODE
not set), anything a user says while the
Recognizer
is paused will not be heard by the Recognizer
.
The BUFFER_MODE
flag indicates that the Recognizer
should buffer audio while PAUSED
so that no audio will be lost
once it returns to the LISTENING
substate.
Pausing a Recognizer
during the middle of user speech
forces the Recognizer
to finalize processing of that incoming speech -
a recognition Result
cannot cross
a pause
/resume
boundary.
Most Recognizer
s have some amount of internal audio buffering.
This means that some Recognizer
processing may continue after
the call to pause
.
For example, Result
s can be created and finalized.
The IMMEDIATE_MODE
flag indicates that
pending recognition buffers should be cleared instead of processed.
A Recognizer
returns to the RESUMED
state
(and LISTENING
substate) with a call to resume
.
However, calls to pause
and resume
nest,
so that there must be a resume
for each pause
.
This avoids Grammar
update issues for multithreaded applications.
This method operates as defined when the standard Conditions for Operation apply.
mode
- modifier flags to use with this method
IllegalArgumentException
- when called with an unexpected mode
value
EngineStateException
- when not in the standard Conditions for Operationpause()
,
BUFFER_MODE
,
IMMEDIATE_MODE
,
resume
,
PAUSED
,
RESUMED
,
LISTENING
,
getEngineState
,
ENGINE_PAUSED
boolean resume() throws EngineStateException
Engine
by
entering the RESUMED
state.
Resuming an Engine
instance does not
affect other Engine
instances
that share the same underlying resources.
The pause
method describes how to pause an Engine
.
Note that calls to pause
and resume
nest,
so that there must be a resume
for each pause
in order for this method to operate as described.
When an Engine
moves from the PAUSED
state
to the RESUMED
state,
an ENGINE_RESUMED
event is issued to each EngineListener
attached to the Engine
.
The RESUMED
bit of the Engine
state is set
when the Engine
resumes,
and can be tested by the getEngineState
method and
other Engine
state methods.
The RESUMED
state is a substate of the ALLOCATED
state.
An ALLOCATED
Engine is always in either
the PAUSED
or the RESUMED
state.
It is not an exception to resume
an Engine
that
is already in the RESUMED
state.
The resume
method does not always return immediately.
Some applications may want to execute resume
in a separate Thread
.
The documentation for the Thread
class shows examples of this.
This method operates as defined when the standard Conditions for Operation apply.
For a Recognizer
,
this method resumes processing the audio input stream as close
as possible to the time of the call to resume
.
Recognition can only occur after calling resume
.
When this method causes the Recognizer
to enter the RESUMED
state,
any Grammar
updates are atomically committed
(see Committing Changes).
It checks that all the loaded Grammar
s are ready to use.
Any problems with the current definition
of any RuleGrammar
result in a CHANGES_REJECTED
event.
Problems might include an undefined RuleReference
,
illegal recursion and so on
(see the
SRGS definition for details).
Grammar
updates are committed asynchronously
(the changes have not necessarily been committed when the call returns).
When the changes have been committed,
a CHANGES_COMMITTED
or CHANGES_REJECTED
event is issued
to all RecognizerListener
s attached to this Recognizer
.
Corresponding to these events,
GrammarListener
s for any changed Grammar
s receive either
a GRAMMAR_CHANGES_COMMITTED
or GRAMMAR_CHANGES_REJECTED
event.
Note that calling pause
before resume
is important to
avoid Grammar
update issues that can occur with
ResultEvent
s and in multithreaded applications.
After any Grammar
updates,
the Recognizer
enters the RESUMED
state.
It waits in the LISTENING
substate until speech is found.
resume
in interface Engine
true
if the Engine
is in or is about to enter
the RESUMED
state; or false
if it will remain
PAUSED
due to nested calls to pause
.
EngineStateException
- when not in the standard Conditions for Operationpause
,
PAUSED
,
RESUMED
,
LISTENING
,
PROCESSING
,
getEngineState
,
ENGINE_RESUMED
,
CHANGES_COMMITTED
,
CHANGES_REJECTED
,
GRAMMAR_CHANGES_COMMITTED
,
GRAMMAR_CHANGES_REJECTED
,
RuleGrammar
,
RecognizerListener
,
GrammarListener
,
ResultEvent
getSupportsLetterToSound
when the vocabulary specified is not known,
results in a CHANGES_REJECTED
event otherwise
|
JSAPI 2.0 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
JavaTM Speech API 2.0,
Final Release v2.0.6.
© 2008, Conversay and Sun Microsystems.