In this article, you will learn how to convert text to speech. The text to speech conversion process improves the customer experience. Many people like to listen instead of reading, like news articles, courses and much more. It helps to interact the users in multiple languages and tones. The website with TTS technology attract most of the customer and extent the reach of your business. There are several APIs available to convert text to speech. In this article, we will use the Google Text-To-Speech or gTTS module.
The gTTS module produces very natural voice like human while some other APIs provides voice like robotic. It is freely available and easy to implement, as it simply converts specified text into audio which we can save as mp3 file. It has the capacity to read a long length of text with keeping proper intonation, abbreviations, decimals and more. This API also supports many languages including English, Hindi, French, German and much more.
First, create an 'index.php' file that we will call on the browser. In this file, we place a form element to collect text.
It has some predefined url attributes that accepts in GET requests, like - q - The targeted text to convert to audio, tl - Translation language, like en-IN for English India, fr for French, it for Italian and so on, ie - Input Encoding format, use the default UTF-8, oe - Output Encoding format.
Next, get the form value and find the special character if any in the form. After that, encode the text using rawurlencode(). Next, we will use the file_get_contents() to retrieve all the data from the gtts and store in a variable.