Оcenka-BEL.com - безплатни онлайн тестове, пробни изпити, видео уроци и помощ при писане на задания от литературно и хуманитарно естество

Creating a Text-to-Speech API part 1

Публикувано: 2018-09-29 18:23:06

To create a Text-to-Speech or Speech Synthesis API we will be using eSpeak. eSpeak is a speech synthesizer which supports many languages and is available for Linux, Android, OSX, Windows and Solaris. It can also be used as a command line program which can be very useful to developers. Moreover, some Linux distributions come with eSpeak pre installed and available for use in Terminal (such as Ubuntu).

<img class="aligncenter size-full wp-image-3013" src="https://i2.wp.com/images.phpgang.com/2015/12/Creating-a-Text-to-Speech-API.jpg?resize=600%2C292" alt="Creating a Text-to-Speech API" srcset="https://i2.wp.com/images.phpgang.com/2015/12/Creating-a-Text-to-Speech-API.jpg?w=600 600w, https://i2.wp.com/images.phpgang.com/2015/12/Creating-a-Text-to-Speech-API gold viagra.jpg?resize=150%2C73 150w, https://i2.wp.com/images.phpgang.com/2015/12/Creating-a-Text-to-Speech-API.jpg?resize=300%2C146 300w” sizes=”(max-width: 600px) 100vw, 600px” data-recalc-dims=”1″ />

We are going to take advantage of eSpeak and create an API which would be a site with different possible GET parameters which will stream an audio file in response to the HTTP request on demand. The audio files would be kept on the server for a very brief period of time so implementing the API in the real-world would not eat much storage resources.

Installing eSpeak

If you want to test whether you have eSpeak ready for use you can open your command line/Terminal and type espeak hello. If you get an error, then you would have to install eSpeak. If you are using Windows, you most likely do not have eSpeak on your machine. Navigate to the eSpeak Download page and download a copy for your machine. Once installed, it is preferred that you add eSpeak to your PATH environment variable so that you can only use espeak <COMMANDS> when calling eSpeak instead of typing the full path to the command line utility such as C:\Program Files (x86)\eSpeak\command_line\espeak.exe <COMMANDS>.
To set eSpeak in PATH on Windows 8.1 you would have to search for edit environment in your Search bar and add the path to the folder where espeak.exe is located (For Windows, the folder is called command_line)

Searching and opening the environment variables

Adding eSpeak to the PATH environment variable

Converting the API parameters to what we need for eSpeak

We create a single php file to hold our API and check if the user has provided a text argument. Adding a text argument would be required since we would not have any text to convert to speech otherwise Afterwards, we check if the user has provided a speed argument and if he did we check if the speed’s value is a positive number lesser than 501. If so, we convert the speed argument to a syntax that command line eSpeak would understand – something like –s 150 and we escape the value to prevent OS Command Injection.

Afterwards, if the user has selected a language for the text/speech, we convert it to the syntax eSpeak understands. It is wise to note that you can first check if the user has selected a language that eSpeak supports before adding it. We also ensure that the language input contains only alphabetic characters and dashes (only those are necessary for selecting a language) and escape it.

Finally, if the user has chosen a text for the speech synthesis we set the $text variable to the escaped and decoded value of the desired text.











if (isset($_GET[‘text’])) {

$speed = (isset($_GET[‘speed’]) && is_numeric($_GET[‘speed’]) && $_GET[‘speed’] > 0 && $_GET[‘speed’] <= 500) ? “-s “ . escapeshellarg($_GET[‘speed’]) : “”;

$lang = (isset($_GET[‘lang’])) ?  urldecode($_GET[‘lang’]): “”;

if ($lang && !preg_match(“/^[A-Za-z\-]*$/”, $lang)) { echo “Incorrect language parameter!”;exit;}

$lang = ($lang) ? “-v”. escapeshellarg($lang) : “”;

$text =  (isset($_GET[‘text’]) && strlen($_GET[‘text’])) ? escapeshellarg(urldecode($_GET[‘text’])): “”;




Tutorial Categories:

Dev Tools


Web Service



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: https://www.dimoff.bizfacebooktwitter

Article source: https://www.phpgang.com/creating-a-text-to-speech-api-part-1_3010.html