|
JSAPI 2.0 | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface AudioManager
The AudioManager
is provided by subclasses of Engine
to allow an application to control audio input/output and to monitor audio-related events.
The getAudioManager
method for an Engine
returns its AudioManager
.
The following example shows how to redirect synthesizer output to a file. Redirection to a recognizer is similar, except that the recognizer reads audio from the file instead of writing to the file.
Synthesizer synth = ... // see Synthesizer for creation options
AudioManager am = synth.getAudioManager();
am.setMediaLocator("file:///user/smith/hello.wav"); // can throw AudioException or SecurityException
synth.allocate(); // after setMediaLocator to avoid error
synth.speak("hello world", null); // speak, redirecting audio to file
The media locator description is based on JSR-135. This JSR uses only the audio part.
Media locators are specified in URI syntax which is defined in the form:
<scheme>:<scheme-specific-part>}
The
<scheme>
part of the locator string identifies the name of the protocol being used to deliver the data.The locator syntax for (1) Live-media Capture, (2) RTP Streaming, and (3) Standard Schemes is defined as follows:
1. Locators for Live-media Capture
The locators for capturing live media are defined by the following syntax in Augmented BNF notations:
"capture://" device [ "?" params] "playback://" device [ "?" params] "stream://" device [ "?" params]The "stream" scheme supports directionless media locators associated with setMediaLocator and AudioSegments.A. Identifying the type or the specific name of the device:
device = "audio" / dev_name / logical_name dev_name = alphanumeric alphanumeric = 1*( ALPHA / DIGIT ) logical_name = "microphone" / "speaker" / "headset"B. Describing the media and capture parameters:
params = audio_params / custom_paramsC. Describing the audio media format:
audio_params = audio_param *( "&" audio_param ) audio_param = 1*( "encoding=" audio_enc / "rate=" rate / "bits=" bits / "channels=" channels / "endian=" endian / "signed=" signed ) audio_enc = "pcm" / "ulaw" / "alaw" / "gsm" rate = "96000" / "48000" / "44100" / "22050" / "16000" / "11025" / "8000" bits = "8" / "16" / "24" channels = pos_integer endian = "little" / "big" signed = "signed" / "unsigned" pos_integer = 1*DIGITD. Describing some custom media parameters:
Note: alaw is required for SSML, but is an extension of JSR-135.
Example: encoding=pcm&rate=11025&bits=16&channels=1
custom_params = param *( "&" param ) param = key "=" value key = alphanumeric value = alphanumeric
Examples: capture://audio (default input audio) playback://audio (default output audio, other capture examples apply) capture://audio?encoding=pcm (default audio audio in PCM format) capture://devmic0?encoding=pcm&rate=11025&bits=16&channels=1 (audio from a specific device--devmic0)
capture://mydev?myattr=123 (custom device with custom param)2. Locators for RTP Streaming
RTP is a public standard for streaming media. The locator syntax for specifying RTP sessions is:"rtp://" address [ ":" port ] [ "/" type ]
where:
address and port define the RTP session. The address and port usage is similar to the host and port usage as defined in the URI syntax.
type = "audio"3. Locators with Standard Schemes
Standard schemes may be used to specify media locators. The<scheme-specific-part>
in this case is defined by the standard URI syntax. Examples includeMedia locators may also be relative
"http://webserver/test.wav"
"file:///tmp/audio.wav"
The
"/org/jsapi2/examples/audiodemo/example.wav"
"example.wav"
EngineProperties.getBase
method is used while resolving relative locators. An exception is thrown if resolution fails.A special
"resource:"
scheme may be used to force resolution withgetResourceAsStream
. This avoids unintended redirection of references that begin with"/"
byEngineProperties.setBase
. See that method for details. The following example locator illustrates the"resource:"
scheme:
"resource:/org/jsapi2/examples/audiodemo/example.wav"
Engine
,
getAudioManager
,
getBase
,
setBase
,
AudioSegment
,
getResourceAsStream
Method Summary | |
---|---|
void |
addAudioListener(AudioListener listener)
Requests notifications of AudioEvent s to
an AudioListener . |
void |
audioStart()
Starts the audio for this AudioManager . |
void |
audioStop()
Stops the audio for this AudioManager . |
int |
getAudioMask()
Gets the mask to filter events for added AudioListener s. |
String |
getMediaLocator()
Returns the media locator for this AudioManager . |
String[] |
getSupportedMediaLocators(String mediaLocator)
Gets the set of supported audio media locator descriptions given a locator template. |
boolean |
isSameChannel(AudioManager audioManager)
Tests for equivalent audio sources or destinations. |
boolean |
isSupportedMediaLocator(String mediaLocator)
Tests a media locator to see if it is supported. |
void |
removeAudioListener(AudioListener listener)
Removes an AudioListener from this AudioManager . |
void |
setAudioMask(int mask)
Sets the mask to filter events for added AudioListener s. |
void |
setMediaLocator(String locator)
Sets the locator for this AudioManager . |
void |
setMediaLocator(String locator,
InputStream stream)
Sets the input audio source for this AudioManager . |
void |
setMediaLocator(String locator,
OutputStream stream)
Sets the output audio source for this AudioManager . |
Method Detail |
---|
int getAudioMask()
AudioListener
s.
See setAudioMask
for details.
The event mask can be set and retrieved in any Engine
state.
AudioEvent
sAudioEvent
,
AudioListener
,
setAudioMask
void setAudioMask(int mask)
AudioListener
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 AudioEvent
class.
AudioEvent.DEFAULT_MASK
is the initial value.
The event mask can be set and retrieved in any Engine
state.
This method affects filtering of all attached AudioListener
s.
mask
- the mask for AudioEvent
sAudioEvent
,
AudioEvent.DEFAULT_MASK
,
AudioListener
,
getAudioMask
String getMediaLocator()
AudioManager
.
This locator specifies characteristics about the current audio stream including the sample rate and encoding.
String
.setMediaLocator
String[] getSupportedMediaLocators(String mediaLocator) throws IllegalArgumentException
The locators are returned as String
s that identify which locators
can be used with an associated Engine
.
The locators indicate the scheme
and can indicate associated devices and media formats.
The argument provides a template that can specify a subset of
all possible schemes, devices, and audio media formats.
For example, if the given media locator is
"capture://audio?encoding=pcm"
, then the
supported media formats that can be used in an Engine
might be
"capture://audio?encoding=pcm&rate=11025&bits=16&channels=1" and
"capture://audio?encoding=pcm&rate=8000&bits=16&channels=1".
Some locator sources include the media format information internally.
For example, the audio source "file:///user/smith/audio.wav"
includes information such as the sample rate within the header.
If null
is passed in as the media locator,
all the supported protocols for this Engine
will be returned.
The returned array should be non-empty.
If the given media locator is unsupported, then an empty array will be returned.
mediaLocator
- a media locator description.
IllegalArgumentException
- if the given media locator is invalidisSupportedMediaLocator
boolean isSupportedMediaLocator(String mediaLocator) throws IllegalArgumentException
true
if a call to getSupportedMediaLocators
with the
same media locator would return a non-empty list.
This provides a convenient way to test for supported features without
constructing a potentially large list.
mediaLocator
- a media locator description.
true
if the media locator description is supported
IllegalArgumentException
- if the given media locator is invalidgetSupportedMediaLocators
void setMediaLocator(String locator) throws AudioException, IllegalStateException, IllegalArgumentException, SecurityException
AudioManager
.
Audio must be stopped when this method is called.
In some cases, the media content may contain its own format information (e.g., within a header). In this case, the format description may be ignored.
If null
is passed as the locator, the default locator will be used.
This includes the device and format.
If successful and the audio channel changes,
this method results in an AUDIO_CHANGED
event.
locator
- a media locator description.
AudioException
- if the locator
is not supported.
IllegalStateException
- if audio is not stopped.
IllegalArgumentException
- if the given media locator is invalid.
SecurityException
- if the application does not have permission.getMediaLocator
,
isSupportedMediaLocator(java.lang.String)
,
isSameChannel
,
audioStop
,
AUDIO_STARTED
,
AUDIO_CHANGED
javax.speech.AudioManager.control
void setMediaLocator(String locator, InputStream stream) throws AudioException, IllegalStateException, IllegalArgumentException, SecurityException
AudioManager
.
Audio must be stopped when this method is called.
An IllegalArgumentException
is thrown if the Engine
does not expect an InputStream
(i.e., it produces output audio).
The locator
scheme must be "stream"
or "capture"
and the parameters specify the encoding of the stream
.
For example:
"stream://audio?encoding=pcm&rate=8000&bits=16&channels=1"
For the "capture"
scheme,
the audio stream
is read continuously.
For the "stream"
scheme,
the audio stream
is only read in the the RESUMED
state.
These two schemes support live input and test input, respectively.
An AudioException
is thrown if the Engine
does not support the stream
with the specified locator
.
The isSupportedMediaLocator
can be used to test the
locator
in advance.
If successful and the audio channel changes,
this method results in an AUDIO_CHANGED
event.
locator
- a media locator descriptionstream
- an input audio source
AudioException
- if the locator
and stream
are not supported
IllegalStateException
- if audio is not stopped.
IllegalArgumentException
- if the given media locator is invalid,
InputStream
is not expected, or either argument is null
SecurityException
- if the application does not have permissiongetMediaLocator
,
isSupportedMediaLocator(java.lang.String)
,
isSameChannel
,
audioStop
,
AUDIO_STARTED
,
AUDIO_CHANGED
,
RESUMED
javax.speech.AudioManager.control
void setMediaLocator(String locator, OutputStream stream) throws AudioException, IllegalStateException, IllegalArgumentException, SecurityException
AudioManager
.
Audio must be stopped when this method is called.
An IllegalArgumentException
is thrown if the Engine
does not expect an OutputStream
(i.e., it consumes input audio).
The locator
scheme must be "stream"
or "playback"
and the parameters specify the encoding of the stream
.
For example:
"stream://audio?encoding=pcm&rate=8000&bits=16&channels=1"
For the "playback"
scheme,
data is written with considerations for real-time output.
For the "stream"
scheme,
data may be written as it becomes available.
These two schemes support live output and test output, respectively.
An AudioException
is thrown if the Engine
does not support the stream
with the specified locator
.
The isSupportedMediaLocator
can be used to test the
locator
in advance.
If successful and the audio channel changes,
this method results in an AUDIO_CHANGED
event.
locator
- a media locator descriptionstream
- an output audio sink
AudioException
- if the locator
and
stream
are not supported
IllegalStateException
- if audio is not stopped.
IllegalArgumentException
- if the given media locator is invalid,
OutputStream
is not expected,
or either argument is null
SecurityException
- if the application does not have permissiongetMediaLocator
,
isSupportedMediaLocator(java.lang.String)
,
isSameChannel
,
audioStop
,
AUDIO_STARTED
,
AUDIO_CHANGED
javax.speech.AudioManager.control
void addAudioListener(AudioListener listener)
AudioEvent
s to
an AudioListener
.
An application can attach multiple AudioListener
s to
an AudioManager
.
Individual events may be filtered with setAudioMask
.
listener
- a listener for AudioEvent
sAudioEvent
,
AudioListener
,
removeAudioListener
,
setAudioMask
void removeAudioListener(AudioListener listener)
AudioListener
from this AudioManager
.
listener
- a listener for AudioEvent
sAudioEvent
,
AudioListener
,
addAudioListener
void audioStart() throws SecurityException, AudioException, EngineStateException
AudioManager
.
Note that an Engine
will normally start and stop an audio channel.
This method provides additional control, especially when combined with
audioStop
.
If successful, this method results in an AUDIO_STARTED
event.
SecurityException
- if the application does not have permission
EngineStateException
- if the Engine
is not in the PAUSED
state
AudioException
- if the request failsaudioStop
,
AUDIO_STARTED
,
PAUSED
javax.speech.AudioManager.control
void audioStop() throws SecurityException, AudioException, EngineStateException
AudioManager
.
This method may be used to effectively mute the audio. For "live" audio, incoming audio is discarded while stopped. Likewise, for outgoing audio, no audio is transmitted.
Engine
s may buffer audio and include pending results or operations.
Applications should take appropriate action depending on the
type of Engine
and the processing goals.
If successful, this method results in an AUDIO_STOPPED
event.
SecurityException
- if the application does not have permission
EngineStateException
- if the Engine
is not in the PAUSED
state
AudioException
- if the request failsaudioStart
,
AUDIO_STOPPED
,
PAUSED
javax.speech.AudioManager.control
boolean isSameChannel(AudioManager audioManager)
This method returns true
if this AudioManager
represents
the same source or destination as the specified AudioManager
.
In addition to counting equal media locator String
s as equal,
AudioManager
s may represent the same channel if
AudioManager
is able to convert differences such as sample rate
for the same channel,
audioManager
- the AudioManager
to compare with this AudioManager
true
if the AudioManager
s represent the same source or destination
|
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.