Class SpeechSynthesizer
- java.lang.Object
-
- com.microsoft.cognitiveservices.speech.SpeechSynthesizer
-
- All Implemented Interfaces:
AutoCloseable
public class SpeechSynthesizer extends Object implements AutoCloseable
Performs speech synthesis to speaker, file, or other audio output streams, and gets synthesized audio as result. Note: close() must be called in order to release underlying resources held by the object. Updated in version 1.16.0
-
-
Field Summary
Fields Modifier and Type Field Description protected static StringAUTH_TOKEN_ERROR_PROPERTYProperty name for the last authentication token error.protected static StringAUTH_TOKEN_EXPIRY_PROPERTYProperty name for the token expiration time in milliseconds.protected static StringAUTH_TOKEN_REFRESH_INTERVAL_MS_PROPERTYProperty name for the token refresh interval in milliseconds.EventHandlerImpl<SpeechSynthesisBookmarkEventArgs>BookmarkReachedDefines event handler for bookmark reached event.EventHandlerImpl<SpeechSynthesisEventArgs>SynthesisCanceledDefines event handler for synthesis canceled event.EventHandlerImpl<SpeechSynthesisEventArgs>SynthesisCompletedDefines event handler for synthesis completed event.EventHandlerImpl<SpeechSynthesisEventArgs>SynthesisStartedDefines event handler for synthesis started event.EventHandlerImpl<SpeechSynthesisEventArgs>SynthesizingDefines event handler for synthesizing event.EventHandlerImpl<SpeechSynthesisVisemeEventArgs>VisemeReceivedDefines event handler for viseme received event.EventHandlerImpl<SpeechSynthesisWordBoundaryEventArgs>WordBoundaryDefines event handler for word boundary event.
-
Constructor Summary
Constructors Constructor Description SpeechSynthesizer(EmbeddedSpeechConfig embeddedSpeechConfig)Creates a new instance of Speech Synthesizer for embedded speech synthesis.SpeechSynthesizer(EmbeddedSpeechConfig embeddedSpeechConfig, AudioConfig audioConfig)Creates a new instance of Speech Synthesizer for embedded speech synthesis.SpeechSynthesizer(HybridSpeechConfig hybridSpeechConfig)Creates a new instance of Speech Synthesizer for hybrid speech synthesis.SpeechSynthesizer(HybridSpeechConfig hybridSpeechConfig, AudioConfig audioConfig)Creates a new instance of Speech Synthesizer for hybrid speech synthesis.SpeechSynthesizer(SpeechConfig speechConfig)Creates a new instance of Speech Synthesizer.SpeechSynthesizer(SpeechConfig speechConfig, AudioConfig audioConfig)Creates a new instance of Speech Synthesizer.SpeechSynthesizer(SpeechConfig speechConfig, AutoDetectSourceLanguageConfig autoDetectSourceLangConfig, AudioConfig audioConfig)Creates a new instance of Speech Synthesizer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Dispose of associated resources.StringgetAuthorizationToken()Gets the authorization token used to communicate with the service.SafeHandlegetImpl()INTERNALPropertyCollectiongetProperties()The collection of properties and their values defined for this SpeechSynthesizer.Future<SynthesisVoicesResult>getVoicesAsync()Get the available voices, asynchronously.Future<SynthesisVoicesResult>getVoicesAsync(String locale)Get the available voices, asynchronously.voidsetAuthorizationToken(String token)Sets the authorization token used to communicate with the service.protected voidsetUpTokenRefreshCallback(PropertyCollection property)Sets up a callback for the token refresh event.Future<SpeechSynthesisResult>SpeakRequestAsync(SpeechSynthesisRequest request)Execute the speech synthesis on request, asynchronously.SpeechSynthesisResultSpeakSsml(String ssml)Execute the speech synthesis on SSML, synchronously.Future<SpeechSynthesisResult>SpeakSsmlAsync(String ssml)Execute the speech synthesis on SSML, asynchronously.SpeechSynthesisResultSpeakText(String text)Execute the speech synthesis on plain text, synchronously.Future<SpeechSynthesisResult>SpeakTextAsync(String text)Execute the speech synthesis on plain text, asynchronously.Future<SpeechSynthesisResult>StartSpeakingRequestAsync(SpeechSynthesisRequest request)Queue the speech synthesis on request, as an asynchronous operation.SpeechSynthesisResultStartSpeakingSsml(String ssml)Start the speech synthesis on SSML, synchronously.Future<SpeechSynthesisResult>StartSpeakingSsmlAsync(String ssml)Start the speech synthesis on SSML, asynchronously.SpeechSynthesisResultStartSpeakingText(String text)Start the speech synthesis on plain text, synchronously.Future<SpeechSynthesisResult>StartSpeakingTextAsync(String text)Start the speech synthesis on plain text, asynchronously.Future<Void>StopSpeakingAsync()Stop the synthesis, asynchronously.protected voidupdateAuthToken(PropertyCollection property)Updates the authentication token in the given PropertyCollection.
-
-
-
Field Detail
-
AUTH_TOKEN_EXPIRY_PROPERTY
protected static final String AUTH_TOKEN_EXPIRY_PROPERTY
Property name for the token expiration time in milliseconds.- See Also:
- Constant Field Values
-
AUTH_TOKEN_ERROR_PROPERTY
protected static final String AUTH_TOKEN_ERROR_PROPERTY
Property name for the last authentication token error.- See Also:
- Constant Field Values
-
AUTH_TOKEN_REFRESH_INTERVAL_MS_PROPERTY
protected static final String AUTH_TOKEN_REFRESH_INTERVAL_MS_PROPERTY
Property name for the token refresh interval in milliseconds.- See Also:
- Constant Field Values
-
SynthesisStarted
public final EventHandlerImpl<SpeechSynthesisEventArgs> SynthesisStarted
Defines event handler for synthesis started event.
-
Synthesizing
public final EventHandlerImpl<SpeechSynthesisEventArgs> Synthesizing
Defines event handler for synthesizing event.
-
SynthesisCompleted
public final EventHandlerImpl<SpeechSynthesisEventArgs> SynthesisCompleted
Defines event handler for synthesis completed event.
-
SynthesisCanceled
public final EventHandlerImpl<SpeechSynthesisEventArgs> SynthesisCanceled
Defines event handler for synthesis canceled event.
-
WordBoundary
public final EventHandlerImpl<SpeechSynthesisWordBoundaryEventArgs> WordBoundary
Defines event handler for word boundary event.
-
VisemeReceived
public final EventHandlerImpl<SpeechSynthesisVisemeEventArgs> VisemeReceived
Defines event handler for viseme received event. Added in version 1.16.0
-
BookmarkReached
public final EventHandlerImpl<SpeechSynthesisBookmarkEventArgs> BookmarkReached
Defines event handler for bookmark reached event. Added in version 1.16.0
-
-
Constructor Detail
-
SpeechSynthesizer
public SpeechSynthesizer(SpeechConfig speechConfig)
Creates a new instance of Speech Synthesizer. Uses the default speaker on the system for audio output.- Parameters:
speechConfig- speech configuration.
-
SpeechSynthesizer
public SpeechSynthesizer(SpeechConfig speechConfig, AudioConfig audioConfig)
Creates a new instance of Speech Synthesizer. If audioConfig is null, there is no audio output.- Parameters:
speechConfig- speech configuration.audioConfig- audio configuration.
-
SpeechSynthesizer
public SpeechSynthesizer(SpeechConfig speechConfig, AutoDetectSourceLanguageConfig autoDetectSourceLangConfig, AudioConfig audioConfig)
Creates a new instance of Speech Synthesizer.- Parameters:
speechConfig- speech configuration.autoDetectSourceLangConfig- the configuration for auto detecting source languageaudioConfig- audio configuration.
-
SpeechSynthesizer
public SpeechSynthesizer(EmbeddedSpeechConfig embeddedSpeechConfig)
Creates a new instance of Speech Synthesizer for embedded speech synthesis. Uses the default speaker on the system for audio output. Added in version 1.19.0- Parameters:
embeddedSpeechConfig- embedded speech configuration.
-
SpeechSynthesizer
public SpeechSynthesizer(HybridSpeechConfig hybridSpeechConfig)
Creates a new instance of Speech Synthesizer for hybrid speech synthesis. Uses the default speaker on the system for audio output.- Parameters:
hybridSpeechConfig- hybrid speech configuration.
-
SpeechSynthesizer
public SpeechSynthesizer(EmbeddedSpeechConfig embeddedSpeechConfig, AudioConfig audioConfig)
Creates a new instance of Speech Synthesizer for embedded speech synthesis. If audioConfig is null, there is no audio output. Added in version 1.19.0- Parameters:
embeddedSpeechConfig- embedded speech configuration.audioConfig- audio configuration.
-
SpeechSynthesizer
public SpeechSynthesizer(HybridSpeechConfig hybridSpeechConfig, AudioConfig audioConfig)
Creates a new instance of Speech Synthesizer for hybrid speech synthesis. If audioConfig is null, there is no audio output.- Parameters:
hybridSpeechConfig- hybrid speech configuration.audioConfig- audio configuration.
-
-
Method Detail
-
SpeakText
public SpeechSynthesisResult SpeakText(String text)
Execute the speech synthesis on plain text, synchronously.- Parameters:
text- The plain text for synthesis.- Returns:
- The synthesis result.
-
SpeakSsml
public SpeechSynthesisResult SpeakSsml(String ssml)
Execute the speech synthesis on SSML, synchronously.- Parameters:
ssml- The SSML for synthesis.- Returns:
- The synthesis result.
-
SpeakTextAsync
public Future<SpeechSynthesisResult> SpeakTextAsync(String text)
Execute the speech synthesis on plain text, asynchronously.- Parameters:
text- The plain text for synthesis.- Returns:
- A task representing the synthesis operation. The task returns a value of SpeechSynthesisResult.
-
SpeakSsmlAsync
public Future<SpeechSynthesisResult> SpeakSsmlAsync(String ssml)
Execute the speech synthesis on SSML, asynchronously.- Parameters:
ssml- The SSML for synthesis.- Returns:
- A task representing the synthesis operation. The task returns a value of SpeechSynthesisResult.
-
SpeakRequestAsync
public Future<SpeechSynthesisResult> SpeakRequestAsync(SpeechSynthesisRequest request)
Execute the speech synthesis on request, asynchronously. This API could be used to synthesize speech from an input text stream, to reduce latency for text generation scenarios. Note: the feature is in preview and is subject to change.- Parameters:
request- The speech synthesis request.- Returns:
- A task representing the completed synthesis operation.
-
StartSpeakingText
public SpeechSynthesisResult StartSpeakingText(String text)
Start the speech synthesis on plain text, synchronously.- Parameters:
text- The plain text for synthesis.- Returns:
- The synthesis result.
-
StartSpeakingSsml
public SpeechSynthesisResult StartSpeakingSsml(String ssml)
Start the speech synthesis on SSML, synchronously.- Parameters:
ssml- The SSML for synthesis.- Returns:
- The synthesis result.
-
StartSpeakingTextAsync
public Future<SpeechSynthesisResult> StartSpeakingTextAsync(String text)
Start the speech synthesis on plain text, asynchronously.- Parameters:
text- The plain text for synthesis.- Returns:
- A task representing the synthesis operation. The task returns a value of SpeechSynthesisResult.
-
StartSpeakingSsmlAsync
public Future<SpeechSynthesisResult> StartSpeakingSsmlAsync(String ssml)
Start the speech synthesis on SSML, asynchronously.- Parameters:
ssml- The SSML for synthesis.- Returns:
- A task representing the synthesis operation. The task returns a value of SpeechSynthesisResult.
-
StartSpeakingRequestAsync
public Future<SpeechSynthesisResult> StartSpeakingRequestAsync(SpeechSynthesisRequest request)
Queue the speech synthesis on request, as an asynchronous operation. This API could be used to synthesize speech from an input text stream, to reduce latency for text generation scenarios. Note: the feature is in preview and is subject to change- Parameters:
request- The speech synthesis request.- Returns:
- A task representing the started synthesis operation.
-
StopSpeakingAsync
public Future<Void> StopSpeakingAsync()
Stop the synthesis, asynchronously. This method will stop the playback and clear the unread data inPullAudioOutputStream. Added in version 1.14.0- Returns:
- A task representing the asynchronous operation that stops the synthesis.
-
getVoicesAsync
public Future<SynthesisVoicesResult> getVoicesAsync(String locale)
Get the available voices, asynchronously. Added in version 1.16.0- Parameters:
locale- Specify the locale of voices, in BCP-47 format; or leave it empty to get all available voices.- Returns:
- A task representing the asynchronous operation that gets the voices.
-
getVoicesAsync
public Future<SynthesisVoicesResult> getVoicesAsync()
Get the available voices, asynchronously. Added in version 1.16.0- Returns:
- A task representing the asynchronous operation that gets the voices.
-
getProperties
public PropertyCollection getProperties()
The collection of properties and their values defined for this SpeechSynthesizer.- Returns:
- The collection of properties and their values defined for this SpeechSynthesizer.
-
setAuthorizationToken
public void setAuthorizationToken(String token)
Sets the authorization token used to communicate with the service. Note: The caller needs to ensure that the authorization token is valid. Before the authorization token expires, the caller needs to refresh it by calling this setter with a new valid token. Otherwise, the synthesizer will encounter errors while speech synthesis.- Parameters:
token- Authorization token.
-
getAuthorizationToken
public String getAuthorizationToken()
Gets the authorization token used to communicate with the service.- Returns:
- Authorization token.
-
close
public void close()
Dispose of associated resources.- Specified by:
closein interfaceAutoCloseable
-
getImpl
public SafeHandle getImpl()
INTERNAL- Returns:
- SafeHandle to
SpeechSynthesizer
-
setUpTokenRefreshCallback
protected void setUpTokenRefreshCallback(PropertyCollection property)
Sets up a callback for the token refresh event.- Parameters:
property- The property collection.
-
updateAuthToken
protected void updateAuthToken(PropertyCollection property)
Updates the authentication token in the given PropertyCollection.
-
-