Was this page helpful?

AGI:multi-tts.agi

    multi-tts.agi 是一個用 PHP 撰寫的 AGI for Asterisk,可以應用在 FreePBX 或純 Asterisk 系統上。這個 AGI 可以整合多個不同的 TTS engine,包含 AWS Polly TTS,國內工研院的中文 TTS,以及其他不同的 TTS。

    使用方法

    複製 multi-tts.agi 至 /var/lib/asterisk/agi-bin

    cp multi-tts.agi /var/lib/asterisk/agi-bin
    chown asterisk.asterisk /var/lib/asterisk/agi-bin/multi-tts.agi
    chmod 0755 /var/lib/asterisk/agi-bin/multi-tts.agi
    

    注意:程式碼以下段落是用來執行不同 TTS engine 的指令
    以 ITRI TTS 為例

    ...
            case 'itritts':
                exec("/usr/local/bin/itri-tts.php '$text' $audiofile.wav");
                break;
    ... 
    

    multi-tts.agi

    #!/usr/bin/php -q 
    <?php
    /********************************************
    Author: A-Lang alang[DOT]hsu[AT]gmail[DOT]com
    Created: 2018/1/24
    ********************************************/
     
    ob_implicit_flush(false); 
    error_reporting(0); 
    set_time_limit(300); 
    
    $ttsdir = "/tmp/tts";
    
    //------------------------ PHP-AGI ------------------------------
    include("phpagi.php");
    $AGI = new AGI();
    
    // Check arguments
    if ($argc < 2) {
        debug("Abort: Missing some parameters.",1);
        return 1;
    }
    
    $text = $argv[1];
    $hash = md5($text);
    $engine = $argv[2];
    
    // Make the directory if not found
    if (!is_dir($ttsdir)) {
        mkdir($ttsdir, 0755, true);
    }
    $audiofile = $ttsdir."/".$engine."-".$hash;
    $textfile = $ttsdir."/".$engine."-".$hash.".txt";
    
    if ( !file_exists("$audiofile.wav") ) {
        debug("Text to speech wave file doesnt exist, lets create it.", 1);
        if ( false === ($fh = fopen($textfile, "w")) ) {
            debug("ERROR: Cannot open the file: $textfile", 1);
            return 1;
        }
        if ( false === fwrite($fh, $text) ) {
            debug("ERROR: Cannot write to file: $textfile", 1);
            return 1;
        }
        fclose($fh);
    
        debug("Executing $engine ", 1);
        switch ($engine) {
            case 'awstts':
                exec("/usr/local/n/versions/node/0.12.7/bin/node /opt/aws-nodejs-sample/script.js --mp3=$audiofile.mp3 --text='$text' --wav=$audiofile.wav");
                break;
            case 'itritts':
                exec("/usr/local/bin/itri-tts.php '$text' $audiofile.wav");
                break;
            default:
                debug("$engine is not a valid engine!", 1);
                break;
        }
    }
    
    // Play the audio file
    if ( file_exists("$audiofile.wav") ) {
        // Adding a wait because the first time the wave file is generated, it was not played
        $AGI->wait_for_digit(1000);
        debug("Streaming the generated wave.", 1);
        $AGI->stream_file($audiofile,'#');
    } else {
        debug("File was not created!", 1);
    }
    debug("TTS AGI end", 1);
    
    
    //-------------------- Functions --------------------------
    function debug($string, $level=3) {
        global $AGI;
        $AGI->verbose($string, $level);
    }
    ?>
    設定 Asterisk

    編輯 /etc/asterisk/extensions_custom.conf

    [multi-tts-demo]
    exten => s,1,Answer()
     same => n,NoOp(Multi-TTS Demo)
    ; same => n,AGI(multi-tts.agi,"Welcome to my small interactive voice response menu.",awstts)
     same => n,AGI(multi-tts.agi,"你好,這是工研院文字轉語音展示系統",itritts)
     same => n,Hangup()
    

    NOTE:

    - 以上的 dialplan 還需要搭配 FreePBX GUI 上的其他設定後才能使用。

    - AGI 格式:multi-tts.agi,<Text>,<TTS-engine>
    NOTE: <TTS-engine> 必須與 multi-tts.agi 內的程式碼相同。

    Was this page helpful?
    標籤 (Edit tags)
    • No tags
    您必須 登入 才能發佈評論。
    Powered by MindTouch Core