更新流式语音
This commit is contained in:
parent
4778c87763
commit
7967a6f010
@ -122,19 +122,115 @@ class XunFeiController extends BaseApiController
|
|||||||
public function iat()
|
public function iat()
|
||||||
{
|
{
|
||||||
header('X-Accel-Buffering: no');
|
header('X-Accel-Buffering: no');
|
||||||
$parmas=$this->request->param('content');
|
$st = microtime(true);
|
||||||
|
|
||||||
$appid = "fa185cd6";
|
// $this->app_id = "fa185cd6";
|
||||||
$apiSecret = "1474397d85f34828194622aab80f1e51";
|
// $this->api_secret = "1474397d85f34828194622aab80f1e51";
|
||||||
$apiKey = "ZjQOYjhjMmE2NmMzYzhiMjU30GE1NjJl";
|
// $this->api_key = "ZjQOYjhjMmE2NmMzYzhiMjU30GE1NjJl";
|
||||||
|
|
||||||
$hostUrl = "wss://iat-api.xfyun.cn/v2/iat";
|
$hostUrl = "wss://iat-api.xfyun.cn/v2/iat";
|
||||||
|
|
||||||
// $iat=new IatClient($appid,$apiKey,$apiSecret);
|
// $iat=new IatClient($appid,$apiKey,$apiSecret);
|
||||||
$iat = new IatClient($this->app_id,$this->api_key,$this->api_secret);
|
$iat = new IatClient($this->app_id,$this->api_key,$this->api_secret);
|
||||||
halt($iat->assembleAuthUrl($hostUrl));
|
// halt($iat->assembleAuthUrl($hostUrl));
|
||||||
$client = new Client($iat->assembleAuthUrl($hostUrl));
|
$client = new Client($iat->assembleAuthUrl($hostUrl));
|
||||||
usleep(1); //等待数据接收完成
|
if ($client) {
|
||||||
|
// $file = app()->getRuntimePath() . 'iat_mp3_16k.mp3';
|
||||||
|
$file = "https://lihai001.oss-cn-chengdu.aliyuncs.com/media/iat_mp3_16k.mp3";
|
||||||
|
$audioFile = fopen($file, 'rb');
|
||||||
|
$frameSize = 1280; //每一帧的音频大小
|
||||||
|
$intervel = 40 * 1000; //发送音频间隔
|
||||||
|
$status = 0;
|
||||||
|
while (true) {
|
||||||
|
$len = fread($audioFile, $frameSize);
|
||||||
|
if ($len === false) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if ($len === '') { //文件读取完了
|
||||||
|
$status = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($status) {
|
||||||
|
case 0: //发送第一帧音频,带business 参数
|
||||||
|
$frameData = array(
|
||||||
|
'common' => array(
|
||||||
|
'app_id' => $this->app_id //appid 必须带上,只需第一帧发送
|
||||||
|
),
|
||||||
|
'business' => array( //business 参数,只需一帧发送
|
||||||
|
'language' => 'zh_cn',
|
||||||
|
'domain' => 'iat',
|
||||||
|
'accent' => 'mandarin'
|
||||||
|
),
|
||||||
|
'data' => array(
|
||||||
|
'status' => 0,
|
||||||
|
'format' => 'audio/L16;rate=16000',
|
||||||
|
'audio' => base64_encode($len),
|
||||||
|
'encoding' => 'lame'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$client->send(json_encode($frameData));
|
||||||
|
$status = 1;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
$frameData = array(
|
||||||
|
'data' => array(
|
||||||
|
'status' => 1,
|
||||||
|
'format' => 'audio/L16;rate=16000',
|
||||||
|
'audio' => base64_encode($len),
|
||||||
|
'encoding' => 'raw'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$client->send(json_encode($frameData));
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
$frameData = array(
|
||||||
|
'data' => array(
|
||||||
|
'status' => 2,
|
||||||
|
'format' => 'audio/L16;rate=16000',
|
||||||
|
'audio' => base64_encode($len),
|
||||||
|
'encoding' => 'raw'
|
||||||
|
)
|
||||||
|
);
|
||||||
|
$client->send(json_encode($frameData));
|
||||||
|
break 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
//模拟音频采样间隔
|
||||||
|
usleep($intervel);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
$response = $client->receive();
|
||||||
|
if ($response === null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$resp = json_decode($response,true);
|
||||||
|
$code = $resp['code'];
|
||||||
|
$message = $resp['message'];
|
||||||
|
$data = $resp['data'];
|
||||||
|
$result = $data['result'];
|
||||||
|
$status = $data['status'];
|
||||||
|
$sid = $resp['sid'];
|
||||||
|
|
||||||
|
if ($code != 0) {
|
||||||
|
echo sprintf('%d %s %f', $code, $message, microtime(true) - $st);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var_dump($resp, $result);
|
||||||
|
|
||||||
|
if ($status === 2) {
|
||||||
|
echo sprintf('%d %s %f', $code, $message, microtime(true) - $st);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this->success('success');
|
||||||
|
} else {
|
||||||
|
return $this->fail('无法连接到 WebSocket 服务器');
|
||||||
|
}
|
||||||
|
fclose($audioFile);
|
||||||
|
usleep(3); //等待数据
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user