|
|
<?php
|
|
|
|
|
|
namespace tencent\tts;
|
|
|
|
|
|
use TencentCloud\Common\Credential;
|
|
|
use TencentCloud\Common\Profile\ClientProfile;
|
|
|
use TencentCloud\Common\Profile\HttpProfile;
|
|
|
use TencentCloud\Common\Exception\TencentCloudSDKException;
|
|
|
use TencentCloud\Tts\V20190823\TtsClient;
|
|
|
use TencentCloud\Tts\V20190823\Models\TextToVoiceRequest;
|
|
|
|
|
|
class Voice extends Base
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 基础语音合成
|
|
|
* @param string $text 合成语音的源文本,按UTF-8编码统一计算。中文最大支持110个汉字(全角标点符号算一个汉字);英文最大支持350个字母(半角标点符号算一个字母)。
|
|
|
* @param string $session_id 一次请求对应一个SessionId,会原样返回,建议传入类似于uuid的字符串防止重复
|
|
|
* @param float $volume 音量大小,范围:[0,10],分别对应11个等级的音量,默认为0,代表正常音量。没有静音选项。输入除以上整数之外的其他参数不生效,按默认值处理。
|
|
|
* @param float $speed 语速,范围:[-2,2],分别对应不同语速:-2代表0.6倍 -1代表0.8倍 0代表1.0倍(默认)1代表1.2倍 2代表1.5倍 如果需要更细化的语速,可以保留小数点后一位,例如0.5 1.1 1.8等
|
|
|
* @param integer $voice_type 音色(又细分为普通音色和精品音色)
|
|
|
* @param integer $primary_language 主语言类型 1-中文(默认) 2-英文
|
|
|
* @param string $codec 返回音频格式,可取值:wav(默认),mp3,pcm
|
|
|
* @param string $region 公共参数
|
|
|
* @param integer $sample_rate 音频采样率 16000:16k(默认) 8000:8k
|
|
|
* @date 2021-02-18
|
|
|
*/
|
|
|
public function TextToVoice($text, $session_id, $volume = 0, $speed = 0, $voice_type = 0, $primary_language = 1, $codec = 'wav', $region = 'ap-beijing', $sample_rate = 16000)
|
|
|
{
|
|
|
try {
|
|
|
|
|
|
$cred = new Credential($this->config['secret_id'], $this->config['secret_key']);
|
|
|
$httpProfile = new HttpProfile();
|
|
|
$httpProfile->setEndpoint("tts.tencentcloudapi.com");
|
|
|
|
|
|
$clientProfile = new ClientProfile();
|
|
|
$clientProfile->setHttpProfile($httpProfile);
|
|
|
$client = new TtsClient($cred, $region, $clientProfile);
|
|
|
|
|
|
$req = new TextToVoiceRequest();
|
|
|
|
|
|
$params = array(
|
|
|
"Region" => $region,
|
|
|
"Text" => $text,
|
|
|
"SessionId" => $session_id,
|
|
|
"ModelType" => 1,
|
|
|
"Volume" => $volume,
|
|
|
"Speed" => $speed,
|
|
|
"VoiceType" => $voice_type,
|
|
|
"PrimaryLanguage" => $primary_language,
|
|
|
"SampleRate" => $sample_rate,
|
|
|
"Codec" => $codec,
|
|
|
);
|
|
|
$req->fromJsonString(json_encode($params));
|
|
|
|
|
|
$resp = $client->TextToVoice($req);
|
|
|
|
|
|
$resp = $resp->toJsonString();
|
|
|
return json_decode($resp, true);
|
|
|
} catch (TencentCloudSDKException $e) {
|
|
|
echo $e;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|