码迷,mamicode.com
首页 > Web开发 > 详细

PHP调用语音合成接口

时间:2018-10-16 01:35:24      阅读:432      评论:0      收藏:0      [点我收藏+]

标签:应用   sse   eal   real   发音   com   获得   nap   res   

百度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;
        }
    }

PHP调用语音合成接口

标签:应用   sse   eal   real   发音   com   获得   nap   res   

原文地址:https://www.cnblogs.com/shanghailife/p/9795288.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!