Create a Synthesizes Natural Sounding Speech From Text Tool
Tags: python, jupyter, ai, ibmwatson
Published: 2021-08-11 12:36:41

You've probably used whatever text to speech tool at least once. So in this post, We'll create your own text-to-speech tool with an audio exporting feature using Python.

Basically, We'll use IBM Watson Text to Speech Machine learning model. IBM Watson helping enterprises put AI to work and helps organizations predict future outcomes, automate complex processes, and optimize employees’ time.


## Register with IBM Cloud
To Getting started with the Text to Speech model, You have to register with IBM Cloud. Go to [IBM Cloud](https://cloud.ibm.com/registration) and create a new free account.

After that, you have to create lite plan instances of the model. To create that, go to the [Text to Speech model](https://cloud.ibm.com/catalog/services/text-to-speech) page and then create a free instance by clicking Create button.

Afterward, you'll see the getting started page. Go to the **Manage** page to get model credentials which are API key and URL. Now registration process is completed.

![Model Credential Page](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vir59nc5vr0w9quj6drd.png)

## Usage
First, have to install the *ibm_watson* on your computer.
```
pip install ibm_watson
```
If you are using **Jupyter Notebook**, add an exclamation mark before the command to act as if it is executed in the terminal.
```
!pip install ibm_watson
```

### Authenticate
Import *TextToSpeech* model, *Watson authenticator* and authenticate with API key and the URL.
```python
from ibm_watson import TextToSpeechV1
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
```
Specify the API Key and URL
```python
url = '<your-api-url>'
apiKey = '<your-api-key>'
```
```python
authenticator = IAMAuthenticator(apiKey)
tts = TextToSpeechV1(authenticator=authenticator)
tts.set_service_url(url)
```

### Setup Text to Speech
In this step, we'll look at how to speak a text from string and text files.

#### From String
```python
with open('./speech.mp3', 'wb') as audio_file:
    res = tts.synthesize('Hello World! I\'m Thirasha', accept='audio/mp3', voice='en-US_AllisonV3Voice').get_result()
    audio_file.write(res.content)
```
In a while, it will generate that string to an audio file and export it as `speech.mp3` at the root directory.


#### From Text File
```python
with open('SpeechText.txt', 'r') as f:
    text = f.readlines()
```
Remove line breaks
```python
text = [line.replace('\n', '') for line in text]
text = ''.join(str(line) for line in text)
```
Export audio file
```python
with open('./speech.mp3', 'wb') as audio_file:
    res = tts.synthesize(text, accept='audio/mp3', voice='en-US_AllisonV3Voice').get_result()
    audio_file.write(res.content)
```

## Change Language and Voice (Optional)

If you want to change the voice or language, refer to this [IBM Languages and Voices](https://cloud.ibm.com/docs/text-to-speech?topic=text-to-speech-voices) documentation.

For example, If I have chosen the German female voice `de-DE_BirgitV3Voice`, that code should be change like this.

```python
with open('./germanspeech.mp3', 'wb') as audio_file:
    res = tts.synthesize('Hallo Welt! Ich bin Thirasha', accept='audio/mp3', voice='de-DE_BirgitV3Voice').get_result()
    audio_file.write(res.content)
```

Eventually, You have created your own Speech-To-Text generating tool!🎉