更新iat语音
This commit is contained in:
parent
49e0be2e73
commit
87f2f22a32
@ -139,103 +139,120 @@ class XunFeiController extends BaseApiController
|
|||||||
if (!file_exists($file)) {
|
if (!file_exists($file)) {
|
||||||
return $this->fail('未上传音频文件');
|
return $this->fail('未上传音频文件');
|
||||||
}
|
}
|
||||||
|
$filesize = filesize($file);
|
||||||
|
if ($filesize > 1 * 1024 * 1024) {
|
||||||
|
return $this->fail('录音文件太长');
|
||||||
|
}
|
||||||
|
$ext = pathinfo($file, PATHINFO_EXTENSION);
|
||||||
|
if ($ext != 'mp3') {
|
||||||
|
return $this->fail('录音格式错误');
|
||||||
|
}
|
||||||
// $file = "https://lihai001.oss-cn-chengdu.aliyuncs.com/media/iat_mp3_16k.mp3";
|
// $file = "https://lihai001.oss-cn-chengdu.aliyuncs.com/media/iat_mp3_16k.mp3";
|
||||||
$audioFile = fopen($file, 'rb');
|
$audioFile = fopen($file, 'rb');
|
||||||
if ($audioFile === false) {
|
if ($audioFile === false) {
|
||||||
return $this->fail('音频文件异常');
|
return $this->fail('音频文件异常');
|
||||||
}
|
}
|
||||||
$words = '';
|
|
||||||
$iatHostUrl = "wss://iat-api.xfyun.cn/v2/iat";
|
try {
|
||||||
$iat = new IatClient($this->app_id, $this->api_key, $this->api_secret);
|
$words = '';
|
||||||
$client = new Client($iat->assembleAuthUrl($iatHostUrl));
|
$iatHostUrl = "wss://iat-api.xfyun.cn/v2/iat";
|
||||||
if ($client) {
|
$iat = new IatClient($this->app_id, $this->api_key, $this->api_secret);
|
||||||
$frameSize = 1280; //每一帧的音频大小
|
$client = new Client($iat->assembleAuthUrl($iatHostUrl));
|
||||||
$intervel = 20 * 1000; //发送音频间隔
|
if ($client) {
|
||||||
$status = 0;
|
$frameSize = 1280; //每一帧的音频大小
|
||||||
while (true) {
|
$intervel = 40 * 1000; //发送音频间隔
|
||||||
$len = fread($audioFile, $frameSize);
|
$status = 0;
|
||||||
if ($len === false) {
|
while (true) {
|
||||||
break;
|
$len = fread($audioFile, $frameSize);
|
||||||
}
|
if ($len === false) {
|
||||||
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;
|
break;
|
||||||
case 1:
|
}
|
||||||
$frameData = array(
|
if ($len === '') { //文件读取完了
|
||||||
'data' => array(
|
$status = 2;
|
||||||
'status' => 1,
|
}
|
||||||
'format' => 'audio/L16;rate=16000',
|
switch ($status) {
|
||||||
'audio' => base64_encode($len),
|
case 0: //发送第一帧音频,带business 参数
|
||||||
'encoding' => 'raw'
|
$frameData = array(
|
||||||
)
|
'common' => array(
|
||||||
);
|
'app_id' => $this->app_id //appid 必须带上,只需第一帧发送
|
||||||
$client->send(json_encode($frameData));
|
),
|
||||||
|
'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;
|
break;
|
||||||
case 2:
|
}
|
||||||
$frameData = array(
|
$resp = json_decode($response,true);
|
||||||
'data' => array(
|
$code = $resp['code'];
|
||||||
'status' => 2,
|
$message = $resp['message'];
|
||||||
'format' => 'audio/L16;rate=16000',
|
$data = $resp['data'];
|
||||||
'audio' => base64_encode($len),
|
$result = $data['result'];
|
||||||
'encoding' => 'raw'
|
$status = $data['status'];
|
||||||
)
|
foreach($result['ws'] as $v) {
|
||||||
);
|
$words .= $v['cw'][0]['w'] ?? '';
|
||||||
$client->send(json_encode($frameData));
|
}
|
||||||
break 2;
|
if ($code != 0 || $status === 2) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//模拟音频采样间隔
|
} else {
|
||||||
usleep($intervel);
|
// 删除临时音频文件
|
||||||
|
if (file_exists($file)) {
|
||||||
|
//unlink($file);
|
||||||
|
}
|
||||||
|
return $this->fail('无法连接到 WebSocket 服务器');
|
||||||
}
|
}
|
||||||
while (true) {
|
fclose($audioFile);
|
||||||
$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'];
|
|
||||||
foreach($result['ws'] as $v) {
|
|
||||||
$words .= $v['cw'][0]['w'] ?? '';
|
|
||||||
}
|
|
||||||
if ($code != 0 || $status === 2) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 删除临时音频文件
|
// 删除临时音频文件
|
||||||
if (file_exists($file)) {
|
if (file_exists($file)) {
|
||||||
//unlink($file);
|
//unlink($file);
|
||||||
}
|
}
|
||||||
return $this->fail('无法连接到 WebSocket 服务器');
|
} catch (\Exception $e) {
|
||||||
}
|
if (file_exists($file)) {
|
||||||
fclose($audioFile);
|
// 删除临时文件
|
||||||
// 删除临时音频文件
|
unlink($file);
|
||||||
if (file_exists($file)) {
|
}
|
||||||
//unlink($file);
|
return $this->fail($e->getMessage());
|
||||||
}
|
}
|
||||||
return $this->data(['words' => $words]);
|
return $this->data(['words' => $words]);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user