Adjust speaking rate(SSML) via cURL POST
Tags: shortposts, codenewbie, tts, speakingrate
Published: 2020-11-09 12:04:30

Stackoverflow is an ocean for learning and exploring. How? Try answering a question and you will understand :) 

A few weeks ago, I saw this question on StackOverflow - 

> How to adjust the speaking rate in Watson Text-to-Speech using cURL POST?

I coded an [Android chatbot](https://www.youtube.com/watch?v=hLETr8t7_bU) that uses Speech-to-Text and Text-to-speech as add-ons. But, I never bothered about the speaking rate and Speech Synthesis Markup Language (SSML). 

![Courtesy: unDraw](https://dev-to-uploads.s3.amazonaws.com/i/zjkkpp9jsmenq41gtk74.png)

## So, what is this speaking rate and SSML?

According to [Virtualspeech](https://virtualspeech.com/blog/average-speaking-rate-words-per-minute),
> Speaking rate is often expressed in words per minute (wpm). To calculate this value, you’ll need to record yourself talking for a few minutes and then add up the number of words in your speech. Divide the total number of words by the number of minutes your speech took.

> Speaking rate (wpm) = total words / number of minutes

and according to [IBM Cloud docs](https://cloud.ibm.com/docs/text-to-speech?topic=text-to-speech-ssml)

> The Speech Synthesis Markup Language (SSML) is an XML-based markup language that provides annotations of text for speech-synthesis applications. It is a recommendation of the W3C Voice-Browser Working Group that has been adopted as the standard markup language for speech synthesis by the VoiceXML 2.0 specification. SSML provides developers of speech applications with a standard way to control aspects of the synthesis process by enabling them to specify pronunciation, volume, pitch, speed, and other attributes via markup.

OK, I understood the terms. 

### What's the answer to the Stackoverflow question

Here's a working example with the `POST` call,

```sh
curl -X POST -u "apikey:{API_KEY}" \
--header "Accept: audio/wav" \
--header "Content-Type: application/json" \
--data '{"text": "<p>
<prosody rate=\"+50%\">This is the first sentence of the paragraph.</prosody>

Here is another sentence.

Finally, this is the last sentence.
</p>"}' \
--output result.wav \
"{URL}/v1/synthesize" -v
```

on a Windows command prompt(cmd), create a JSON file `input.json` with the below command

```sh
echo {"text": "<p>
<prosody rate='+50%'>This is the first sentence of the paragraph.</prosody>

Here is another sentence.

Finally, this is the last sentence.
</p>"} > input.json
```
and then cURL to see result.wav file
```sh
curl -X POST -u "apikey:{API_KEY}" ^
--header "Accept: audio/wav" ^
--header "Content-Type: application/json" ^
--data @input.json ^
--output result.wav ^
"{URL}/v1/synthesize" -v
```

For the sentence in the actual [question](https://stackoverflow.com/questions/64185972/ibm-cloud-how-to-adjust-speaking-rate-in-watson-tts-using-curl-post), replace the JSON above with yours

```sh
{"text":"<prosody rate='fast'>Adult capybaras are one meter long.</prosody>"}
```

Here are some useful links I followed to create the above code sample that will help you in understanding the SSML attributes. Also, check the limitations of `<prosody>` in the links below

- https://cloud.ibm.com/docs/text-to-speech?topic=text-to-speech-usingHTTP#usingHTTP
- https://cloud.ibm.com/docs/text-to-speech?topic=text-to-speech-elements#break_element
- https://text-to-speech-demo.ng.bluemix.net