Text-to-Speech (TTS) programs like open source espeak, flite and festival and commercial solutions like cepstral, ispeech and voiceforge can be used to convert from written text to speech (recordings). PHP supports open source solutions like espeak, flite and festival to convert text to speech.

I have used festival with PHP for TTS program. Its very simple to use. If you are running linux, you can simply use “yum install festival” command to install festival package. This package is required for TTS program to work. Windows users can use cygwin to build festival package and place its .so file bin directory of PHP. Once you are done with installation of festival, you can move on to write PHP code that will convert your text to voice recording. Following is the code that creates recording from your text. I have written text into the variable that you can change:

<?php

//create a random number filename so that simultaneous users don't overwrite each other
$filename = 'test.txt';
$text = 'This text will be converted to audio recording';
$outputfile = basename($filename, ".txt");
$outputfile = $outputfile . '.wav';

if (!$handle = fopen($filename, "w"))
{
	//we cannot open the file handle!
	return false;
}
// Write $text to our opened file.
if (fwrite($handle, $text) === FALSE)
{
	//couldn't write the file...Check permissions
	return false;
}

fclose($handle);

//initialise and execute the festival C engine
//make sure that your environment is set to find the festival binaries!
$cmd = "text2wave $filename -o $outputfile";
//execute the command
exec($cmd);

unlink($filename);
echo "Recording is successful. \nRecording File: " . $outputfile;
//finally return the uptput file path and filename
return $outputfile;

?>

Note: After installing festival, don’t forget to restart your web server.

Tagged with: PHPProgrammingSource Code
 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.