Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
With the support of an on-device model, on-device text to speech (TTS) can convert text information into speech even when there is no Internet connection. The current version provides two types of male voice speakers and seven types of female voice speakers.
| Language | Code | Text Example | Speech Example | Speaker Name | Gender | On-device Algorithm Package (For details, please refer to Integrating the On-device Text to Speech SDK.) |
|---|---|---|---|---|---|---|
| Chinese | zh-Hans | 华为机器学习服务提供机器学习套件,为开发者使用机器学习能力开发各类应用,提供优质体验。 |
| zh-Hans-st-eagle-1 | Female | ml-computer-voice-tts-model-eagle |
|
| zh-Hans-st-eagle-2 | Male | ml-computer-voice-tts-model-eagle | |||
| English | en-US | The potential to go beyond the human mind now extends to app development. Use a full range of vision and language APIs to build your own AI apps, even if you are not a machine learning expert. |
| en-US-st-eagle-1 | Female | ml-computer-voice-tts-model-eagle |
|
| en-US-st-eagle-2 | Male | ml-computer-voice-tts-model-eagle | |||
|
| en-US-st-bee-1 | Female | ml-computer-voice-tts-model-bee | |||
| French | fr-FR | Le potentiel de dépassement de l'intelligence humaine s'étend désormais au développement d'applications. Utilisez une gamme complète d'API de vision numérique pour concevoir vos propres applications d'IA, même sans expertise en apprentissage automatique. |
| fr-FR-st-bee-1 | Female | ml-computer-voice-tts-model-bee |
| Spanish | es-ES | El potencial para ir más allá de la mente humana ahora llega al campo del desarrollo de aplicaciones. Utilice gran variedad de API de visión para desarrollar sus propias aplicaciones de inteligencia artificial, incluso si usted no es especialista en el campo del aprendizaje automático. |
| es-ES-st-bee-1 | Female | ml-computer-voice-tts-model-bee |
| German | de-DE | Das Potenzial, über den menschlichen Verstand hinauszuwachsen, erstreckt sich jetzt bis hin zur App-Entwicklung. Verwenden Sie eine Vielzahl von Vision-APIs, um Ihre eigenen KI-Apps zu erstellen, selbst wenn Sie kein Experte für maschinelles Lernen sind. |
| de-DE-st-bee-1 | Female | ml-computer-voice-tts-model-bee |
| Italian | it-IT | I servizi di apprendimento automatico offrono API di servizio avanzate per testo, parlato, lingua, immagini e volti umani per creare nuove esperienze di intelligenza artificiale e creare facilmente le tue applicazioni di intelligenza artificiale. |
| it-IT-st-bee-1 | Female | ml-computer-voice-tts-model-bee |
This service is widely used in daily life. For example, in a voice navigation app, a specific navigation voice may be synthesized offline based on navigation data, to make navigation more personalized and allow users to enjoy the travel. This service can also be used for 24-hour broadcasting in railway stations, greatly reducing labor costs.
Before app development, you need to make necessary development preparations, configure the Maven repository address for the HMS Core SDK, and integrate the on-device text to speech SDK.
// Use customized parameter settings to create a TTS engine.
// For details about the speaker names, please refer to the Timbres section.
MLTtsConfig mlTtsConfig = new MLTtsConfig()
// Set the text converted from speech to English.
.setLanguage(MLTtsConstants.TTS_EN_US)
// Set the speaker with the English female voice (bee).
.setPerson(MLTtsConstants.TTS_SPEAKER_OFFLINE_EN_US_FEMALE_BEE)
// Set the TTS mode to on-device mode. The default mode is real-time mode.
.setSynthesizeMode(MLTtsConstants.TTS_OFFLINE_MODE);
MLTtsEngine mlTtsEngine = new MLTtsEngine(mlTtsConfig);
// Update the configuration when the engine is running.
mlTtsEngine.updateConfig(mlTtsConfig);// Use customized parameter settings to create a TTS engine.
// For details about the speaker names, please refer to the Timbres section.
var mlTtsConfig = MLTtsConfig() // Set the text converted from speech to English.
.setLanguage(MLTtsConstants.TTS_EN_US) // Set the speaker with the English female voice (bee).
.setPerson(MLTtsConstants.TTS_SPEAKER_OFFLINE_EN_US_FEMALE_BEE) // Set the TTS mode to on-device mode. The default mode is real-time mode.
.setSynthesizeMode(MLTtsConstants.TTS_OFFLINE_MODE)
val mlTtsEngine = MLTtsEngine(mlTtsConfig)
// Update the configuration when the engine is running.
mlTtsEngine.updateConfig(mlTtsConfig)MLTtsCallback callback = new MLTtsCallback() {
@Override
public void onError(String taskId, MLTtsError err) {
// Processing logic for TTS failure.
}
@Override
public void onWarn(String taskId, MLTtsWarn warn) {
// Alarm handling without affecting service logic.
}
@Override
// Return the mapping between the currently played segment and text. start: start position of the audio segment in the input text; end (excluded): end position of the audio segment in the input text.
public void onRangeStart(String taskId, int start, int end) {
// Process the mapping between the currently played segment and text.
}
@Override
// taskId: ID of a TTS task corresponding to the audio.
// audioFragment: audio data.
// offset: offset of the audio segment to be transmitted in the queue. One TTS task corresponds to a TTS queue.
// range: text area where the audio segment to be transmitted is located; range.first (included): start position; range.second (excluded): end position.
public void onAudioAvailable(String taskId, MLTtsAudioFragment audioFragment, int offset,
Pair<Integer, Integer> range, Bundle bundle) {
// Audio stream callback API, which is used to return the synthesized audio data to the app.
}
@Override
public void onEvent(String taskId, int eventId, Bundle bundle) {
// Callback method of a TTS event. eventId indicates the event name.
boolean isInterrupted;
switch (eventId) {
case MLTtsConstants.EVENT_PLAY_START:
// Called when playback starts.
break;
case MLTtsConstants.EVENT_PLAY_STOP:
// Called when playback stops.
isInterrupted = bundle.getBoolean(MLTtsConstants.EVENT_PLAY_STOP_INTERRUPTED);
break;
case MLTtsConstants.EVENT_PLAY_RESUME:
// Called when playback resumes.
break;
case MLTtsConstants.EVENT_PLAY_PAUSE:
// Called when playback pauses.
break;
// Pay attention to the following callback events when you focus on only synthesized audio data but do not use the internal player for playback:
case MLTtsConstants.EVENT_SYNTHESIS_START:
// Called when TTS starts.
break;
case MLTtsConstants.EVENT_SYNTHESIS_END:
// Called when TTS ends.
break;
case MLTtsConstants.EVENT_SYNTHESIS_COMPLETE:
// TTS is complete. All synthesized audio streams are passed to the app.
isInterrupted = bundle.getBoolean(MLTtsConstants.EVENT_SYNTHESIS_INTERRUPTED);
break;
default:
break;
}
}
};val callback: MLTtsCallback = object : MLTtsCallback {
override fun onError(taskId: String, err: MLTtsError) {
// Processing logic for TTS failure.
}
override fun onWarn(taskId: String, warn: MLTtsWarn) {
// Alarm handling without affecting service logic.
}
// Return the mapping between the currently played segment and text. start: start position of the audio segment in the input text; end (excluded): end position of the audio segment in the input text.
override fun onRangeStart(taskId: String, start: Int, end: Int) {
// Process the mapping between the currently played segment and text.
}
// taskId: ID of an audio synthesis task corresponding to the audio.
// audioFragment: audio data.
// offset: offset of the audio segment to be transmitted in the queue. One TTS task corresponds to a TTS queue.
// range: text area where the audio segment to be transmitted is located; range.first (included): start position; range.second (excluded): end position.
override fun onAudioAvailable(taskId: String, audioFragment: MLTtsAudioFragment, offset: Int,
range: Pair<Int, Int>, bundle: Bundle) {
// Audio stream callback API, which is used to return the synthesized audio data to the app.
}
override fun onEvent(taskId: String, eventId: Int, bundle: Bundle) {
// Callback method of a TTS event. eventId indicates the event name.
val isInterrupted: Boolean
when (eventId) {
MLTtsConstants.EVENT_PLAY_START -> {
}
MLTtsConstants.EVENT_PLAY_STOP -> // Called when playback stops.
isInterrupted = bundle.getBoolean(MLTtsConstants.EVENT_PLAY_STOP_INTERRUPTED)
MLTtsConstants.EVENT_PLAY_RESUME -> {
}
MLTtsConstants.EVENT_PLAY_PAUSE -> {
}
MLTtsConstants.EVENT_SYNTHESIS_START -> {
}
MLTtsConstants.EVENT_SYNTHESIS_END -> {
}
MLTtsConstants.EVENT_SYNTHESIS_COMPLETE -> // TTS is complete. All synthesized audio streams are passed to the app.
isInterrupted = bundle.getBoolean(MLTtsConstants.EVENT_SYNTHESIS_INTERRUPTED)
else -> {
}
}
}
}mlTtsEngine.setTtsCallback(callback);
mlTtsEngine.setTtsCallback(callback)
// Create an on-device TTS model manager.
MLLocalModelManager localModelManager = MLLocalModelManager.getInstance();
// Create an MLTtsLocalModel instance to set the speaker so that the language model corresponding to the speaker can be downloaded through the model manager.
MLTtsLocalModel model = new MLTtsLocalModel.Factory(MLTtsConstants.TTS_SPEAKER_OFFLINE_EN_US_FEMALE_BEE).create();
localModelManager.isModelExist(model).addOnSuccessListener(new OnSuccessListener<Boolean>() {
@Override
public void onSuccess(Boolean aBoolean) {
// If the model is not downloaded, call the download API. Otherwise, call the TTS API of the on-device engine.
if (aBoolean) {
// Call the speak API to perform TTS. sourceText indicates the text to be synthesized. For details about the speak API, please refer to step 6.
mlTtsEngine.speak(sourceText, MLTtsEngine.QUEUE_APPEND | MLTtsEngine.OPEN_STREAM);
} else {
// Call the API for downloading the on-device TTS model in step 5.
downloadModel(MLTtsConstants.TTS_SPEAKER_OFFLINE_EN_US_FEMALE_BEE);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// Query error message.
}
});// Create an on-device TTS model manager.
val localModelManager = MLLocalModelManager.getInstance()
// Create an MLTtsLocalModel instance to set the speaker so that the language model corresponding to the speaker can be downloaded through the model manager.
val model = MLTtsLocalModel.Factory(MLTtsConstants.TTS_SPEAKER_OFFLINE_EN_US_FEMALE_BEE).create()
localModelManager.isModelExist(model).addOnSuccessListener { aBoolean ->
// If the model is not downloaded, call the download API. Otherwise, call the TTS API of the on-device engine.
if (aBoolean) {
// Call the speak API to perform TTS. sourceText indicates the text to be synthesized. For details about the speak API, please refer to step 6.
mlTtsEngine.speak(sourceText, MLTtsEngine.QUEUE_APPEND or MLTtsEngine.OPEN_STREAM)
} else {
// Call the API for downloading the on-device TTS model in step 5.
downloadModel(MLTtsConstants.TTS_SPEAKER_OFFLINE_EN_US_FEMALE_BEE)
}
}.addOnFailureListener {
// Query error message.
}private void downloadModel(String person) {
// Create an on-device TTS model manager.
MLLocalModelManager localModelManager = MLLocalModelManager.getInstance();
// Create an MLTtsLocalModel instance and pass the speaker (indicating by person) to download the language model corresponding to the speaker.
final MLTtsLocalModel model = new MLTtsLocalModel.Factory(person)
.create();
// Create a download policy configurator. You can set that when any of the following conditions is met, the model can be downloaded: 1. The device is charging; 2. Wi-Fi is connected; 3. The device is idle.
MLModelDownloadStrategy request = new MLModelDownloadStrategy.Factory().needWifi().create();
// Create a download progress listener for the on-device model.
MLModelDownloadListener modelDownloadListener = new MLModelDownloadListener() {
@Override
public void onProcess(long alreadyDownLength, long totalLength) {
// Display the download progress of the on-device language model.
}
};
// Call the download API of the on-device model manager.
localModelManager.downloadModel(model, request, modelDownloadListener)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// Download success.
// Call updateConfig to update the engine model configuration.
mlTtsEngine.updateConfig(mlTtsConfig);
// Call the speak API to perform TTS. sourceText indicates the text to be synthesized. For details about the speak API, please refer to step 6.
mlTtsEngine.speak(sourceText, MLTtsEngine.QUEUE_APPEND | MLTtsEngine.OPEN_STREAM);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
// Download failure.
}
});
}private fun downloadModel(person: String) {
// Create an on-device TTS model manager.
val localModelManager = MLLocalModelManager.getInstance()
// Create an MLTtsLocalModel instance and pass the speaker (indicating by person) to download the language model corresponding to the speaker.
val model = MLTtsLocalModel.Factory(person).create()
// Create a download policy configurator. You can set that when any of the following conditions is met, the model can be downloaded: 1. The device is charging; 2. Wi-Fi is connected; 3. The device is idle.
val request = MLModelDownloadStrategy.Factory().needWifi()
.create()
// Create a download progress listener for the on-device model.
val modelDownloadListener = MLModelDownloadListener { alreadyDownLength, totalLength ->
// Display the download progress of the on-device language model.
}
// Call the download API of the on-device model manager.
localModelManager.downloadModel(model, request, modelDownloadListener)
.addOnSuccessListener { // Download success.
// Call updateConfig to update the engine model configuration.
mlTtsEngine!!.updateConfig(mlTtsConfig)
// Call the speak API to perform TTS. sourceText indicates the text to be synthesized. For details about the speak API, please refer to step 6.
mlTtsEngine.speak(sourceText, MLTtsEngine.QUEUE_APPEND or MLTtsEngine.OPEN_STREAM)
}
.addOnFailureListener {
// Download failure.
}
}// sourceText indicates the text to be synthesized. mlTtsEngine.speak(sourceText, MLTtsEngine.QUEUE_APPEND | MLTtsEngine.OPEN_STREAM);
// sourceText indicates the text to be synthesized. mlTtsEngine!!.speak(sourceText, MLTtsEngine.QUEUE_APPEND or MLTtsEngine.OPEN_STREAM)
// Pause playback. mlTtsEngine.pause(); // Resume playback. mlTtsEngine.resume();
// Pause playback. mlTtsEngine.pause() // Resume playback. mlTtsEngine.resume()
mlTtsEngine.stop();
mlTtsEngine.stop()
if (mlTtsEngine != null) {
mlTtsEngine.shutdown();
}if (mlTtsEngine != null) {
mlTtsEngine.shutdown()
}Before downloading an on-device model, you need to understand the download principle and how to use the on-device model. For details, please refer to How to Download and Use an On-device Model.