[Huawei Developers] Jack Wu Shenzhen:
Introduction

Have you ever met a situation like this before? A novel is too long to read, it will spare you much time if the app can automatically read the novel for you. So a tool to transfer text into speech is urgently required. HUAWEI ML kit (machine learning) has the function of TTS, which can help your app quickly achieve text to speech function.

Text to Speech can convert text into human voice. This can be achieved by default methods also but they don’t provide natural or realistic sounds.

This service uses Deep Neural Networks in order to process the text and create a natural sound, rich timbres are also supported in order to enhance the result. This service is available globally.

As this service uses cloud service hence there is a limit of 500 characters. These characters are encoded using UTF-8

Below are the format supported currently.

· English – Male voice

· English – Female voice

· Mandarin Chinese – Male voice

· Mandarin Chinese – Female voice

· English + Chinese – Male voice

· English + Chinese – Female voice

Article Takeaway

Below is the final result which we will be going to achieve after implementing this Kit.

Steps to Integrate

Step 1: Create a new project in Android Studio

Step 2: Add the below dependencies into app.gradle file

implementation 'com.huawei.hms:ml-computer-voice-tts:1.0.4.300'

Step 3: Add agc plugin in the top of app.gradle file

apply plugin: 'com.huawei.agconnect'

Step 4: Create a callback in your activity

var callback: MLTtsCallback = object : MLTtsCallback {

override fun onError(taskId: String, err: MLTtsError) {

}

override fun onWarn(taskId: String, warn: MLTtsWarn) {

}

override fun onRangeStart(taskId: String, start: Int, end: Int) {

}

override fun onEvent(taskId: String, eventName: Int, bundle: Bundle?) {

if (eventName == MLTtsConstants.EVENT_PLAY_STOP) {

val isStop = bundle?.getBoolean(MLTtsConstants.EVENT_PLAY_STOP_INTERRUPTED)

}

}

}

Let us discuss this in detail.

4 callback methods are provided. Below are the details.

· OnError() - In case of any error the control will flow here, you can use this to notify user what error occurred and send the analytics data by HMS Analytics to console for further verification.

· OnWarn() - In case of warning like insufficient bandwidth the callback comes here.

· OnRangeStart() - It returns the mapping between the currently played segment and text

· OnEvent() - Whenever a new event occurs this method is called, example – In case of audio paused we will get EVENT_PLAY_STOP_INTERRUPTED parameter in bundle.

o If MLTtsConstants.EVENT_PLAY_STOP is false then whole audio is played without issue.

o If MLTtsConstants.EVENT_PLAY_STOP is true then there is some interruption.

Step 5: Object Initialization

mlConfigs = MLTtsConfig()

.setLanguage(MLTtsConstants.TTS_EN_US)

.setPerson(MLTtsConstants.TTS_SPEAKER_FEMALE_EN)

.setSpeed(1.0f)

.setVolume(1.0f)

mlTtsEngine = MLTtsEngine(mlConfigs)

mlTtsEngine.setTtsCallback(callback)

Let us discuss this in detail.

There are 2 ways by which we can create TTS Engine.

We will be using custom TTSEnigne by MLTtsConfig object.

· Language set to English by MLTtsConstants.TTS_EN_US

o You can set language by MLTtsConstants.TTS_ZH_HANS for Chinese.

· Set person voice by MLTtsConstants.TTS_SPEAKER_FEMALE_EN

o English Male – MLTtsConstants .TTS_SPEAKER_MALE_EN

o Chinese Female – MLTtsConstants .TTS_SPEAKER_FEMALE_ZH

o Chinese Male – MLTtsConstants .TTS_SPEAKER_MALE_ZH

· Set the speech speed. Range: 0.2–1.8. 1.0 indicates 1x speed.

· Set the volume. Range: 0.2–1.8. 1.0 indicates 1x volume.

· Create the object of MLTtsEngine and provide above MLTtsConfig object.

· Set the above created callback object into MLTtsEngine

Step 6: Add the below method in your activity and call it on click of a button

private fun startTtsService() {

val id = mlTtsEngine.speak(sourceText,MLTtsEngine.QUEUE_APPEND)

}

Let us discuss this in detail.

sourceText is the text entered by user.

· MLTtsEngine.QUEUE_APPENDED is used when we want queue system. Once first operation of TTS will complete then next will start.

· In case you want a functionality where you want to only process current operation then use MLTtsEngine. QUEUE_FLUSH

· In onPause() you can stop the MLTtsEngine by

override fun onPause() {

super.onPause()

mlTtsEngine.stop()

}

In onDestroy() you can release the resources occupied by MLTtsEngine.

override fun onDestroy() {

super.onDestroy()

mlTtsEngine.shutdown()

}

FAQ

Q: Is TTS only available on huawei devices?

A: Yes.

Q: Do you need internet access in order to use TTS service?

A: Yes, this is a cloud based service hence internet is required.

Conclusion

We have merely scratched the surface. The text-to-speech function provided by HUAWEI ML Kit is also applicable to the following scenarios: News reading, audio novels, stock information broadcast, voice navigation, and video dubbing.

Above are some areas which needs TTS as a main service. I hope you liked this article. I would love to hear your ideas on how you can use this kit in your Applications.

Below is the Github link where you can download the source code.

Github Link: https://github.com/HMS-Core/hms-ml-demo/tree/master/TTSSampleKotlin