January 4, 2016 9:16 am

Creating a Text-to-Speech API part 2

After we have set up all parameters, we create a variable that will hold the name of the audio file which will be a randomly generated name. Afterwards, we launch eSpeak in the command line with all possible user parameters. If the user has not specified a parameter, it will just be blank.Creating a Text-to-Speech API

Then, we check if eSpeak has returned some text to the command line (it will return text if the command failed) or if the expected file name has not been created and if one of these has happened, we will stream a generic error file to the user. We also create a $noDel variable which would indicate that this file should not be deleted after streaming has been done.

Afterwards, we add the necessary headers that would prevent the browsers from caching our script.

Now, we just stream our file by adding the required headers and reading the file

After the streaming has been done, we check if the $noDel variable has been set and if it has not been set we just remove the saved audio file from our server:

Finally, if the user has not provided a text or file parameter we suppose that he wants to read the API’s documentation and so we just display a static webpage with information about the API and a documentation:

How would a client use our Text-to-Speech API?

There are numerous ways such an API could be used but to exemplify: he could play different audios in his website depending on what his own users are doing using the HTML5 Audio API.

To exemplify, I have created different requests for our API. The user just loads an audio passing the parameters that he wants to our API and plays it to his users. The audio variable just speaks aloud some random text, the audio2 variable speaks aloud our APIs error audio file since it requests an unsupported language, the audio3 variable speaks aloud a random text file, the audio4 variable speaks aloud some text pronounced quite slowly and the audio5 variable speaks aloud some text in Bulgarian.

Tutorial Categories:

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter


Tutorial Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *