You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace tencent\asr;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Asr\V20190614\AsrClient;
use TencentCloud\Asr\V20190614\Models\SentenceRecognitionRequest;
class ShortRecord extends Base
{
/**
* 一句话识别 (支持wav、mp3格式 对60秒之内的短音频文件进行识别)
* @param string $source_type 语音数据来源。0语音 URL1语音数据post body
* @param string $content $source_type = 1语音的URL地址需要公网可下载。长度小于2048字节当 SourceType 值为 0 时须填写该字段,为 1 时不需要填写。注意请确保录音文件时长在5个小时之内否则可能识别失败。请保证文件的下载速度否则可能下载失败。
* $source_type = 2语音数据当SourceType 值为1时必须填写为0可不写。要base64编码(采用python语言时注意读取文件应该为string而不是byte以byte格式读取后要decode()。编码后的数据不可带有回车换行符)。音频数据要小于5MB
* @param string $user_audio_key 用户端对此任务的唯一标识,用户自助生成,用于用户查找识别结果。
* @param string $voice_format 识别音频的音频格式。mp3、wav。
* @param string $eng_ser_vice_type 引擎模型类型
* @param integer $data_length 数据长度,单位为字节。当 SourceType 值为1本地语音数据上传时必须填写当 SourceType 值为0语音 URL上传可不写此数据长度为数据未进行base64编码时的数据长度
* @date 2021-02-18
*/
public function SentenceRecognition($source_type, $content, $user_audio_key, $voice_format = 'mp3', $eng_ser_vice_type = '16k_zh', $data_length = 0)
{
try {
$cred = new Credential($this->config['secret_id'], $this->config['secret_key']);
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("asr.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new AsrClient($cred, "", $clientProfile);
$req = new SentenceRecognitionRequest();
$params = array(
"ProjectId" => 0,
"SubServiceType" => 2,
"EngSerViceType" => $eng_ser_vice_type,
"SourceType" => $source_type,
"VoiceFormat" => $voice_format,
"UsrAudioKey" => $user_audio_key,
);
if ($source_type == 0) {
$params['Url'] = $content;
} else if ($source_type == 1) {
$params['Data'] = $content;
$params['DataLen'] = $data_length;
}
$req->fromJsonString(json_encode($params));
$resp = $client->SentenceRecognition($req);
$resp = $resp->toJsonString();
return json_decode($resp, true);
} catch (TencentCloudSDKException $e) {
echo $e;
}
}
}