首先我們需要設置espeak和lame的路徑。確保你已經安裝了兩者。在我的情況下,它看起來像這樣:
我付出了,有人可能會覺得這很有用。我使用這個代碼在本地Windows WAMP服務器和在線Linux服務器生成我的命令:
// APPLICATION PATHS AND CONFIG
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
//This is a server using Windows!
define('ESPEAK', '..\application\libraries\espeak-win\command_line\espeak');
define('LAME', '..\application\libraries\espeak-win\command_line\lame');
}
else {
//This is a server not using Windows!
define('ESPEAK', '/usr/bin/espeak');
define('LAME', '/usr/bin/lame');
}
然後,寫你自己要執行的命令。我使用%s斑點以後用所需的值替換。 List of espeak commands can be found here。
如果你不需要MP3轉換和您滿意.wav文件,只是|
(包括該字符)後取出一部分,並與這種有兩個參數-w desired_file_path
替換參數--stdout
。在這種情況下,請確保稍後正確設置%s變量。
define('COMMAND', ESPEAK.' --stdout -v %s+m3 -p 60 -a 75 -s 130 "%s" | '.LAME.' --preset voice -q 9 --vbr-new - %s');
,然後執行類似這樣的腳本:
$lang_voice = 'en';
$input_text = 'some input text to read';
$file_path = 'voice-cache/output.mp3'
$exe_path = sprintf(COMMAND, $lang_voice, $input_text, $file_path); // fills %s spots
exec($exe_path);
最後一步,只輸出生成的文件:
header('Content-Type: audio/mpeg');
header('Content-Length: '.filesize($file_path));
readfile($file_path);