PHP調用語音合成接口

百度TTS 語音合成

 //百度文件轉換成語音
    private function toSpeech($text)
    {
        define('DEMO_CURL_VERBOSE', false);
        $obj=["status"=>0,"msg"=>"","file_name"=>""];
        //獲取祕鑰
        $apiKey = Config::get('apiKey');
        $secretKey = Config::get('secretKey');
        $cuid = Config::get('cuid');

        //發音人選擇, 0爲普通女聲,1爲普通男生,3爲情感合成-度逍遙,4爲情感合成-度丫丫,默認爲普通女聲
        $per = 0;
        //語速,取值0-15,默認爲5中語速
        $spd = 5;
        //音調,取值0-15,默認爲5中語調
        $pit = 5;
        //音量,取值0-9,默認爲5中音量
        $vol = 5;
        // 下載的文件格式, 3:mp3(default) 4: pcm-16k 5: pcm-8k 6. wav
        $aue = 3;
        $formats = array(3 => 'mp3', 4 => 'pcm', 5 => 'pcm', 6 => 'wav');
        $format = $formats[$aue];

        /** 公共模塊獲取token開始 */
        $auth_url = "https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=" . $apiKey . "&client_secret=" . $secretKey;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $auth_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //信任任何證書
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // 檢查證書中是否設置域名,0不驗證
        curl_setopt($ch, CURLOPT_VERBOSE, DEMO_CURL_VERBOSE);
        $res = curl_exec($ch);
        if (curl_errno($ch)) {
            $obj["msg"] = curl_error($ch);
            return $obj;
        }
        curl_close($ch);
        $response = json_decode($res, true);

        if (!isset($response['access_token'])) {
            $obj["msg"]="ERROR TO OBTAIN TOKEN";
            return $obj;
        }
        if (!isset($response['scope'])) {
            $obj["msg"]="ERROR TO OBTAIN scopes";
            return $obj;
        }
        if (!in_array('audio_tts_post', explode(" ", $response['scope']))) {
            // 請至網頁上應用內開通語音合成權限
            $obj["msg"]="DO NOT have tts permission";
            return $obj;
        }
        $token = $response['access_token'];
        /** 公共模塊獲取token結束 */

        /** 拼接參數開始 **/
        // tex=$text&lan=zh&ctp=1&cuid=$cuid&tok=$token&per=$per&spd=$spd&pit=$pit&vol=$vol
        $params = array(
            'tex' => urlencode($text), // 爲避免+等特殊字符沒有編碼,此處需要2次urlencode。
            'per' => $per,
            'spd' => $spd,
            'pit' => $pit,
            'vol' => $vol,
            'aue' => $aue,
            'cuid' => $cuid,
            'tok' => $token,
            'lan' => 'zh', //固定參數
            'ctp' => 1, // 固定參數
        );
        $paramsStr = http_build_query($params);
        $url = 'http://tsn.baidu.com/text2audio';
        /** 拼接參數結束 **/

        $g_has_error = false;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $paramsStr);
        $data = curl_exec($ch);
        $res = curl_getinfo($ch);
        // 正常返回:audio/*   錯誤返回類型爲:application/json 則轉換失敗
        if ($res['content_type'] == 'application/json') {
            $g_has_error = true;
            $obj["msg"]=json_decode($data);
            return $obj;
        }
        if (curl_errno($ch)) {
            $obj["msg"]=curl_error($ch);
            return $obj;
        }
        curl_close($ch);
        //拼接文件名字
        if (!$g_has_error) {
            $path = "uploads/voices/". strtotime(date("Y-m-dH:i:s")). "_" . rand(100000, 999999) .".".$format;
            file_put_contents($path, $data);
            $obj["status"]=1;
            $obj["msg"]="操作成功!";
            $obj["file_name"]=$path;
        }else{
            $obj["msg"]="操作失敗!";
        }
        return $obj;
    }

讀取音頻文件時長

需要使用第三方庫:getID3-master,需要使用引用兩個文件夾:getid3,helperapps

private function voiceTime($img){
        //包含文件
        $path =__DIR__.'/../libs/getid3/getid3.php';
        $fileName =realpath($path);
        if (!file_exists($fileName) || !include_once($fileName)) {
            return 0;
        }
        include_once($fileName);

        try{
            $mp3_path=__DIR__.'/../../../public/'.$img;
            $getID3 = new \getID3();  //實例化類
            $ThisFileInfo = $getID3->analyze($mp3_path); //分析文件,$path爲音頻文件的地址
            $fileDuration=$ThisFileInfo['playtime_seconds']; //這個獲得的便是音頻文件的時長
            $time = (int)ceil($fileDuration);
            return $time;
        }catch (Exception $e){
            return 0;
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章