This commit is contained in:
2024-07-12 17:50:19 +08:00
parent c1c73d8f70
commit e249bf6cc7
2 changed files with 75 additions and 48 deletions

View File

@@ -0,0 +1,72 @@
<?php
/*
* description
* authorwh
* email
* createTime{2024/7/12} {17:41}
*/
namespace app\api\logic;
use app\api\controller\BaseWssApi;
use GatewayWorker\Lib\Gateway;
use IFlytek\Xfyun\Speech\LfasrClient;
use wanghua\general_utility_tools_php\tool\Tools;
class AudioRevertLogic extends BaseLogic
{
function revert($client_id,$audioData)
{
$xunfei_record_config = config('xunfei_record_config');
$appId = $xunfei_record_config['appid'];
$secretKey = $xunfei_record_config['secretKey'];
// 将音频数据转换为文件以便传递给iFLYTEK SDK
$tempFile = tempnam(sys_get_temp_dir(), 'audio');
Tools::log_to_write_txt(['tempFile:',$tempFile]);
file_put_contents($tempFile, $audioData);
//$appId = '5ca1e5**';// 此行代码需要更换为用户自己的appid
//$secretKey = '4e41a9b93846815****************';// 此行代码需要更换为用户自己的secretKey
$client = new LfasrClient($appId, $secretKey);
Tools::log_to_write_txt(['开始上传音频']);
//echo "开始上传音频...\n";
//$taskId = $client->combineUpload(__DIR__ . '/../resource/lfasrTest.wav');
$taskId = $client->combineUpload($tempFile);
Tools::log_to_write_txt(['音频上传成功']);
//echo "音频上传成功task_id: $taskId\n";
//do {
$progress = json_decode($client->getProgress($taskId)->getBody()->getContents(), true);
if ($progress['ok'] !== 0) {
//echo "获取任务进度失败\n";
$res_json = BaseWssApi::json_wss('error','获取任务进度失败');
Tools::log_to_write_txt($res_json);
Gateway::sendToClient($client_id, $res_json);
return;
}
if ($progress['err_no'] !== 0) {
$res_json = BaseWssApi::json_wss('error','获取任务进度失败');
Tools::log_to_write_txt($res_json);
Gateway::sendToClient($client_id, $res_json);
return;
}
$data = json_decode($progress['data'], true);
if ($data['status'] == 9) {
$result = $client->getResult($taskId)->getBody()->getContents();
$res_json = BaseWssApi::json_wss('wss_revert_audio','音频转写成功开始获取结果',$result);
Tools::log_to_write_txt($res_json);
Gateway::sendToClient($client_id, $res_json);
return;
} else {
$res_json = BaseWssApi::json_wss('error','持续转写中,错误:',$data);
Tools::log_to_write_txt($res_json);
Gateway::sendToClient($client_id, $res_json);
}
}
}

View File

@@ -3,6 +3,7 @@
namespace app\index\logic\events;
use app\api\controller\BaseWssApi;
use app\api\logic\AudioRevertLogic;
use app\api\logic\PartnerOfflineLogic;
use app\api\logic\UserLogic;
use app\api\logic\WssMessageLogic;
@@ -199,7 +200,7 @@ class Events extends \think\worker\Events
//$client = new VmsClient('YOUR_APPID', 'YOUR_SECRET_KEY');
// 音频数据接收处理
$audioData = $data;
//$audioData = $data;
//// 将音频数据转换为文件以便传递给iFLYTEK SDK
//$tempFile = tempnam(sys_get_temp_dir(), 'audio');
@@ -242,54 +243,8 @@ class Events extends \think\worker\Events
//// 清理临时文件
//unlink($tempFile);
$xunfei_record_config = config('xunfei_record_config');
$appId = $xunfei_record_config['appid'];
$secretKey = $xunfei_record_config['secretKey'];
// 将音频数据转换为文件以便传递给iFLYTEK SDK
$tempFile = tempnam(sys_get_temp_dir(), 'audio');
Tools::log_to_write_txt(['tempFile:',$tempFile]);
file_put_contents($tempFile, $audioData);
(new AudioRevertLogic())->revert($client_id,$data);
//$appId = '5ca1e5**';// 此行代码需要更换为用户自己的appid
//$secretKey = '4e41a9b93846815****************';// 此行代码需要更换为用户自己的secretKey
$client = new LfasrClient($appId, $secretKey);
Tools::log_to_write_txt(['开始上传音频']);
//echo "开始上传音频...\n";
//$taskId = $client->combineUpload(__DIR__ . '/../resource/lfasrTest.wav');
$taskId = $client->combineUpload($tempFile);
Tools::log_to_write_txt(['音频上传成功']);
//echo "音频上传成功task_id: $taskId\n";
//do {
$progress = json_decode($client->getProgress($taskId)->getBody()->getContents(), true);
if ($progress['ok'] !== 0) {
//echo "获取任务进度失败\n";
$res_json = BaseWssApi::json_wss('error','获取任务进度失败');
Tools::log_to_write_txt($res_json);
Gateway::sendToClient($client_id, $res_json);
return;
}
if ($progress['err_no'] !== 0) {
$res_json = BaseWssApi::json_wss('error','获取任务进度失败');
Tools::log_to_write_txt($res_json);
Gateway::sendToClient($client_id, $res_json);
return;
}
$data = json_decode($progress['data'], true);
if ($data['status'] == 9) {
$result = $client->getResult($taskId)->getBody()->getContents();
$res_json = BaseWssApi::json_wss('wss_revert_audio','音频转写成功开始获取结果',$result);
Tools::log_to_write_txt($res_json);
Gateway::sendToClient($client_id, $res_json);
return;
} else {
$res_json = BaseWssApi::json_wss('error','持续转写中,错误:',$data);
Tools::log_to_write_txt($res_json);
Gateway::sendToClient($client_id, $res_json);
}
//sleep(5);
//} while (true);