doushi7761 2014-05-10 17:29
浏览 161
已采纳

在Windows和Linux上的eSpeak到php中的mp3(在线文本到语音转换)

I want to implement simple text-to-speech script in my web application that would dynamically generate mp3's out of given texts.

It needs to run in both:

  • my local WAMP server on windows
  • and my online linux server

eSpeak doesn't offer the highest quality in sound but at least a strong support in languages, simple implementation and also it's free. So after a little digging i realized there are not much examples of integrating it into php. I concluded StackOverflow should contain a simple implementation of a php text to speech script that generates mp3 with eSpeak and lame.

  • 写回答

1条回答 默认 最新

  • doushen4719 2014-05-10 17:29
    关注

    First we need to setup path to espeak and lame. Make sure you have installed both. In my case it looks like this:

    I tought, someone might find this useful. I'm using this code to generate my command in local windows wamp server and online linux server:

    // 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');
    }
    

    Then, write your own command to be executed. I used %s spots to be replaced later with desired values. List of espeak commands can be found here.

    In case you don't need mp3 conversion and you are satisfied with .wav files, just remove the part after | (including this character) and replace argument --stdout with this two args -w desired_file_path. In that case make sure to correctly set %s variables later on.

    define('COMMAND', ESPEAK.' --stdout -v %s+m3 -p 60 -a 75 -s 130 "%s" | '.LAME.' --preset voice -q 9 --vbr-new - %s');
    

    and then execute the script like this:

    $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);
    

    As a last step, just output generated file:

    header('Content-Type: audio/mpeg');
    header('Content-Length: '.filesize($file_path));
    readfile($file_path);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题