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
2.9 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\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 音量大小,范围:[010]分别对应11个等级的音量默认为0代表正常音量。没有静音选项。输入除以上整数之外的其他参数不生效按默认值处理。
* @param float $speed 语速,范围:[-22],分别对应不同语速:-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默认mp3pcm
* @param string $region 公共参数
* @param integer $sample_rate 音频采样率 1600016k默认 80008k
* @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;
}
}
}