日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

小爱音响调用php接口_PHP调用语音合成接口

發(fā)布時(shí)間:2024/7/23 php 59 豆豆
生活随笔 收集整理的這篇文章主要介紹了 小爱音响调用php接口_PHP调用语音合成接口 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

百度TTS 語音合成

//百度文件轉(zhuǎn)換成語音

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

//發(fā)音人選擇, 0為普通女聲,1為普通男生,3為情感合成-度逍遙,4為情感合成-度丫丫,默認(rèn)為普通女聲

$per = 0;

//語速,取值0-15,默認(rèn)為5中語速

$spd = 5;

//音調(diào),取值0-15,默認(rèn)為5中語調(diào)

$pit = 5;

//音量,取值0-9,默認(rèn)為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); // 檢查證書中是否設(shè)置域名,0不驗(yàn)證

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']))) {

// 請(qǐng)至網(wǎng)頁上應(yīng)用內(nèi)開通語音合成權(quán)限

$obj["msg"]="DO NOT have tts permission";

return $obj;

}

$token = $response['access_token'];

/** 公共模塊獲取token結(jié)束 */

/** 拼接參數(shù)開始 **/

// 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', //固定參數(shù)

'ctp' => 1, // 固定參數(shù)

);

$paramsStr = http_build_query($params);

$url = 'http://tsn.baidu.com/text2audio';

/** 拼接參數(shù)結(jié)束 **/

$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/* 錯(cuò)誤返回類型為:application/json 則轉(zhuǎn)換失敗

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;

}

讀取音頻文件時(shí)長(zhǎng)

需要使用第三方庫:getID3-master,需要使用引用兩個(gè)文件夾: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(); //實(shí)例化類

$ThisFileInfo = $getID3->analyze($mp3_path); //分析文件,$path為音頻文件的地址

$fileDuration=$ThisFileInfo['playtime_seconds']; //這個(gè)獲得的便是音頻文件的時(shí)長(zhǎng)

$time = (int)ceil($fileDuration);

return $time;

}catch (Exception $e){

return 0;

}

}

總結(jié)

以上是生活随笔為你收集整理的小爱音响调用php接口_PHP调用语音合成接口的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。