PHP調用科大訊飛Voice Messaging Service

來源:互聯網
上載者:User

本篇文章給大家分享的內容是關於PHP調用科大訊飛Voice Messaging Service ,有著一定的參考價值,有需要的朋友可以參考一下

PHP調用科大訊飛Voice Messaging Service

最近在做小程式,需要做語音辨識,選擇了國內很有名的訊飛語音。
我的後台是PHP,在接入過程中走了一些坑,在這裡分享出來希望可以協助需要的朋友


準備工作

  1. 申請訊飛帳號http://www.xfyun.cn/

  2. 添加IP白名單(5-10分鐘生效)

  3. 準備一個音頻檔案(wav或pcm格式)

  4. 擷取APPID和APPKEY(每個服務的APPKEY不同

const APP_ID = 'xxxx';const APP_KEY_IAT = 'xxxx'; //聽寫APPKEYconst APP_KEY_ISE = 'xxxx'; //語音評測APPKEYconst APP_KEY_TTS = 'xxxx'; //語音合成APPKEY

聽寫

public function voiceIat($file_path){    $param = [        'engine_type' => 'sms16k',        'aue' => 'raw'    ];    $cur_time = (string)time();    $x_param = base64_encode(json_encode($param));    $header_data = [        'X-Appid:'.self::APP_ID,        'X-CurTime:'.$cur_time,        'X-Param:'.$x_param,        'X-CheckSum:'.md5(self::APP_KEY_IAT.$cur_time.$x_param),        'Content-Type:application/x-www-form-urlencoded; charset=utf-8'    ];    //Body    $file_path = $file_path;    $file_content = file_get_contents($file_path);    $body_data = 'audio='.urlencode(base64_encode($file_content));    //Request    $url = "http://api.xfyun.cn/v1/service/v1/iat";    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_HEADER, 0);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);    curl_setopt($ch, CURLOPT_POST, TRUE);    curl_setopt($ch, CURLOPT_HTTPHEADER, $header_data);    curl_setopt($ch, CURLOPT_POSTFIELDS, $body_data);    $result = curl_exec($ch);    curl_close($ch);    return $result;}

聽寫樣本:

voiceIat('a.wav');

語音評測

public function voiceIse($file_path, $content){    $param = [        'language' => 'cn',        'aue' => 'raw',        'category' => 'read_sentence'    ];    $cur_time = (string)time();    $x_param = base64_encode(json_encode($param));    $header_data = [        'X-Appid:'.self::APP_ID,        'X-CurTime:'.$cur_time,        'X-Param:'.$x_param,        'X-CheckSum:'.md5(self::APP_KEY_ISE.$cur_time.$x_param),        'Content-Type:application/x-www-form-urlencoded; charset=utf-8'    ];    //Body    $file_path = $file_path;    $file_content = file_get_contents($file_path);    $body_data = 'audio='.urlencode(base64_encode($file_content)).'&text='.urlencode($content);    $url = "http://api.xfyun.cn/v1/service/v1/ise";    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_HEADER, 0);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);    curl_setopt($ch, CURLOPT_POST, TRUE);    curl_setopt($ch, CURLOPT_HTTPHEADER, $header_data);    curl_setopt($ch, CURLOPT_POSTFIELDS, $body_data);    $result = curl_exec($ch);    curl_close($ch);    return $result;}

語音評測樣本:

echo voiceIse('a.wav', '科大訊飛真給力');

語音合成

public function voiceTts($content, $output_path){    $param = [        'engine_type' => 'intp65',        'auf' => 'audio/L16;rate=16000',        'aue' => 'raw',        'voice_name' => 'xiaoyan',        'speed' => '0'    ];    $cur_time = (string)time();    $x_param = base64_encode(json_encode($param));    $header_data = [        'X-Appid:'.self::APP_ID,        'X-CurTime:'.$cur_time,        'X-Param:'.$x_param,        'X-CheckSum:'.md5(self::APP_KEY_TTS.$cur_time.$x_param),        'Content-Type:application/x-www-form-urlencoded; charset=utf-8'    ];    //Body    $body_data = 'text='.urlencode($content);    //Request    $url = "http://api.xfyun.cn/v1/service/v1/tts";    $ch = curl_init();    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_HEADER, TRUE);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);    curl_setopt($ch, CURLOPT_POST, TRUE);    curl_setopt($ch, CURLOPT_HTTPHEADER, $header_data);    curl_setopt($ch, CURLOPT_POSTFIELDS, $body_data);    $result = curl_exec($ch);    $res_header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);    $res_header = substr($result, 0, $res_header_size);    curl_close($ch);    if(stripos($res_header, 'Content-Type: audio/mpeg') === FALSE){ //合成錯誤        return substr($result, $res_header_size);    }else{        file_put_contents($output_path, substr($result, $res_header_size));        return '語音合成成功,請查看檔案!';    }}

語音合成樣本:

echo voiceTts('科大訊飛真給力', 'a.wav');

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.