The one TTS app to rule them all

The ultimate Text-to-Speech test app to experience ML Kit’s TTS capabilities.


Sponsor


Hello everybody, it’s me again. As you would guess from the kicker, I’m starting a series of articles covering Huawei ML Kit features. And this would be the first of many articles that would come in the future. At least, I hope to explain things and publish them in a continuous fashion 😄.

This article would be brief and self-explanatory. I would provide code samples about Text-to-Speech (TTS) as I guide you through the process. I wanted to explain the subtle details to make this guide as efficient as possible. You could think of this article as a TL;DR kind of article. So, I tried my best to be succinct and also gave you guys the nub of the story. Even until this point, you got bored and wanted to hop into the code. You would have to scroll your cursors to the bottom, which is not that beneath 😄. Also, before we start, I would like to mention that this guide and repository will be updated as the new version of the TTS releases. So, you might see different new capabilities, language supports, or even new API usage in your future readings.

TTS Preview
TTS Preview

## Text-to-Speech

Text to speech (TTS) can convert text information into audio output in real-time. Rich timbres are provided and the volume and speed can be adjusted, thereby natural voices can be produced. This service uses the deep neural network (DNN) synthesis mode and can be quickly integrated through the on-device SDK to generate audio data in real-time.

These above were straight from Huawei TTS docs. To be honest, we all know what Text-to-Speech (TTS) is or used in our mobile device in some parts of our daily life. I would like to list the capabilities of the Huawei ML Kit’s TTS;

### Capabilities

  • Supports the download of offline models
  • Provides two standard male voices and six standard female voices are available

Also, you would need to watch out for some limits;

### Limits

  • Only available on Huawei phones
  • A single request can contain a maximum of 500 characters and is encoded using UTF-8
  • TTS in French, Spanish, German, and Italian is deployed only in Asia, Africa, Latin America, and Europe
  • Depends on on-cloud APIs. But also, there is a way to use offline with limited scope. Keep in mind that you need to download some resources before using it.

After you read the limit list. You could think that why we stated only available on Huawei phones? Because ML Kit supports iOS as well. That makes ML Kit a multi-platform tool.

Multiple language support is one of the major points for Huawei’s TTS. It supports 6 languages which are Chinese, English, French, German, Italian, and Spanish. It not only supports multiple languages, but it also provides 2 different voice selections for Chinese and English for now. You can see the table below to see the language support list and its constants to use on the coding side.

Timbres
Timbres

Additionally, TTS offers us a rich number of callback methods that can be handled via its callback interface. This makes our life easier to do what we aim to do. These callbacks are explained in the reference section of MLTtsCallback.

The basic usage flow of TTS is like this;

  • Create configuration object via MLTtsConfig builder class
  • Initialize MLTtsEngine(...)‘s constructor with configured MLTtsConfig object as constructor parameter
  • Create your MLTtsCallback object to handle callback events
  • Invoke setTtsCallback(MLTtsCallback) to register your MLTtsCallback object

And that is pretty much it. After that, you just call MLTtsEngine’s speak(String, String). The first parameter is the text what we want it to say. And the second is the mode that we choose for it. There are 4 playback modes that can be used in different scenarios. You would see the original documentation below for the explanation.

Playback modes
Playback modes

And the last thing, you could do is changing configurations with updateConfig(…) that takes MLTtsConfig. This method would apply the new configuration selections that you provided. The next time, you invoke speak(…), you would hear the texts with the reflected changes.

You could see the gists below to understand how things work in the concrete code examples. Fair warning ⚠️, I did remove some repeated and long code blocks in gists. That way, we could only focus on what matters most.

Starting Point
Initial TTS Configuration
TTS Engine Initialization & Attaching TTS Callback to TTS Engine

Last but not least, don’t forget to use shutdown() method to release resources in your MLTtsEngine object when you don’t need it in your Activity/Fragment. That would prevent some one or two bugs in your project 😉.

## Test

Before you test it out, I would like you to remind you that you need to do some pre-work to run the test app. Because you would need to get some pretty tiny JSON file to sort things out to use the Huawei’s Kits on Huawei devices. You should read the following article about it.

⚠️ Each HMS Integration requires the same initial steps, to begin with. You could use this link to prepare your app before implementing features into it. Please, don’t skip this part. This is a mandatory phase. HMS Kits will not work as they should be without it.

After reading it, you should do one or two things to run the app. First, enable ML Kit under the Manage APIs tab on AppGallery Connect and should see the image below after enabling it.

Then, download the agconnect-services.json file that generated and place it under app directory. Finally learn your API Key under App Information section in General Information tab. And use it to initialize ML Kit like below in your Android Application class.

## Github Repository

ASR&TTS Codelabs GitHub Link

That is it for this article. You could search for any question that comes to your mind via Huawei Developer Forum. And lastly, you can find lengthy detailed videos on Huawei Developers YouTube channel. These resources diversify learning channels and make things easy to pick and learn from a huge knowledge pool. In short, there is something for everybody here 😄. Please comment if you’ve any questions on your mind. Stay tuned for more HMS Development resources. Thanks for reading. Be safe, folks.


© 2024 Yekta Sarioglu. All rights reserved.