Files
fast_response/digital_doctor/application/api/logic/AudioRevertLogic.php
2025-03-17 10:56:09 +08:00

141 lines
5.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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'];
// 设置转写参数
$lfasrConfig = [
//'hasParticiple' => 'true'
//'verify_ssl' => false,
//...
];
// 这里的$app_id、$secret_key是在开放平台控制台获得
$client = new LfasrClient($appId, $secretKey, $lfasrConfig);
$filePath = Tools::get_root_path().'public/iseTest.mp3';
dump($filePath);
// $filePath为待转写的本地文件路径返回taskId作为后续查询进度、获取结果操作的参数
$taskId = $client->combineUpload($filePath);
dump($taskId);
// 查询进度json格式
$progress = $client->getProgress($taskId)->getBody()->getContents();
dump($progress);
// 获取结果json格式
$result = $client->getResult($taskId)->getBody()->getContents();
dump($result);
//// 将音频数据转换为文件以便传递给iFLYTEK SDK
//$tempFile = tempnam(sys_get_temp_dir(), 'audio');
//Tools::log_to_write_txt(['tempFile:',$tempFile]);
//file_put_contents($tempFile, $audioData);
//
//Tools::log_to_write_txt(['$xunfei_record_config'=>$xunfei_record_config]);
////$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);
//Tools::log_to_write_txt(['进度'=>$progress]);
//
//if ($progress['ok'] !== 0) {
// //echo "获取任务进度失败\n";
// $res_json = BaseWssApi::json_wss('error','获取任务进度失败',$progress);
// 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','获取任务进度失败',$progress);
// 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);
// return;
//}
}
function revert_test($client_id,$audioData){
$xunfei_record_config = config('xunfei_record_config');
$appId = $xunfei_record_config['appid'];
$secretKey = $xunfei_record_config['APIKey'];
dump($xunfei_record_config);
$client = new LfasrClient($appId, $secretKey);
echo "开始上传音频...\n";
$filePath = Tools::get_root_path().'public/iseTest.mp3';
$taskId = $client->combineUpload($filePath);
echo "音频上传成功task_id: $taskId\n";
do {
$progress = json_decode($client->getProgress($taskId)->getBody()->getContents(), true);
dump($progress);die;
if ($progress['ok'] !== 0) {
echo "获取任务进度失败\n";
exit;
}
if ($progress['err_no'] !== 0) {
echo "获取任务进度失败,错误码:" . $progress['err_no'] . "\n";
exit;
}
$data = json_decode($progress['data'], true);
if ($data['status'] == 9) {
echo "音频转写成功,开始获取结果...\n";
break;
} else {
echo "音频转写中...\n";
}
sleep(5);
} while (true);
$result = $client->getResult($taskId)->getBody()->getContents();
echo $result;
return $result;
}
}