eGuideDog
free software for the blind

How to setup eSpeak TTS server for WebSpeech

(updated on May 2, 2013)

Step 1: Download and install eSpeak

Please get eSpeak from http://espeak.sourceforge.net/download.html. (You may need to access the page with a proxy or VPN if you are in China.)

Step 2: Install lame

Here is the website of lame. However, I didn't build lame from source. It can be installed with `apt-get` on Debian or Ubuntu, `yum` on Fedora, `zypper` on OpenSUSE or `port` on Mac.

Step 3: Write a Web script to handle requests

Take PHP for example:
<?php
// change following setting to a cache folder could increase performance
$base_dir = '/tmp';

// get input parameters
$text = $_GET['text'];

if (isset($_GET['voice'])) {
  $voice = $_GET['voice'];
} else {
  $voice = 'en';
}

if (isset($_GET['speedDelta'])) {
  $speed_delta = $_GET['speedDelta'];
} else {
  $speed_delta = 0;
}
$speed = (int)(175 + 175 * $speed_delta / 100);

if (isset($_GET['pitchDelta'])) {
  $pitch_delta = $_GET['pitchDelta'];
} else {
  $pitch_delta = 0;
}
$pitch = (int)(50 + $pitch_delta / 2);

if (isset($_GET['volumeDelta'])) {
  $volume_delta = $_GET['volumeDelta'];
} else {
  $volume_delta = 0;
}
$volume = (int)(100 + $volume_delta);

$filename = md5($text) . '.mp3';
$filepath = $base_dir . '/v' . $voice . 's' . $speed . 'p' . $pitch .
    'a' . $volume . 't' . $filename;

$text = escapeshellarg($text);
if (!file_exists($filepath)) {
  $cmd = "espeak -s $speed -p $pitch -a $volume --stdout $text |\
    lame --preset voice -q 9 --vbr-new - $filepath";
  exec($cmd);
}

header('Content-Type: audio/mpeg');
header('Content-Length: ' . filesize($filepath));
readfile($filepath);
?>

Tips: if you fail to execute system command in PHP code above, check whether espeak and lame are in PATH of web server. Or make a symbol link to /usr/bin