.NET SDK

最近更新时间:2024-07-16 17:00:51

我的收藏

安装依赖环境

.NET standard 2.0(.NET Framework 4.5+ 或者 .NET Core 2.1),.NetCore SDK 官方网站

安装 SDK

通过 nuget 安装 .NET SDK,可以选择安装所有依赖或本产品相关依赖。前往 Github 仓库 或者 Gitee 仓库 。
所有依赖
相关依赖
dotnet add package TencentCloudSDK
dotnet add package TencentCloudSDK.Soe
dotnet add package TencentCloudSDK.Sts
dotnet add package TencentCloudSDK.Common

使用 SDK

可通过 控制台 生成代码进行调用或者参考下方示例,密钥获取可参考 快速入门

本地音频文件评测

发音数据传输附带初始化:直接调用发音数据传输接口附带初始化过程完成一次性评测。
// dotnet run --property:StartupObject=TencentCloudExamples.TransmitOralProcessWithInit

using TencentCloud.Common;
using TencentCloud.Soe.V20180724;
using TencentCloud.Soe.V20180724.Models;


namespace TencentCloudExamples
{
class TransmitOralProcessWithInit
{
static void Main(string[] args)
{
try
{
string uuid = Guid.NewGuid().ToString();
string data = Convert.ToBase64String(File.ReadAllBytes("../SOEMUSIC/apple.mp3"));//本地音频文件
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
Credential cred = new Credential {
SecretId = "SecretId",
SecretKey = "SecretKey"
};
SoeClient client = new SoeClient(cred, "");

TransmitOralProcessWithInitRequest req = new TransmitOralProcessWithInitRequest();
req.SeqId = 1;
req.IsEnd = 1;
req.VoiceFileType = 3;
req.VoiceEncodeType = 1;
req.UserVoiceData = data;
req.SessionId = uuid;
req.RefText = "book";
req.WorkMode = 1;
req.EvalMode = 1;
req.ScoreCoeff = 1F;
TransmitOralProcessWithInitResponse resp = client.TransmitOralProcessWithInitSync(req);
Console.WriteLine(AbstractModel.ToJsonString(resp));
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.Read();
}
}
}

评测超时处理

如果本地文件过大,可能会导致评测超时。可以选用如下方案:
1. 流式分片传输:将发音数据进行分片处理,减少每次评测的时间。
2. 异步查询:使用异步功能将发音数据先上传,然后使用查询功能查询结构。
流式分片传输
异步查询
// dotnet run --property:StartupObject=TencentCloudExamples.OralevaluationStream

using TencentCloud.Common;
using TencentCloud.Soe.V20180724;
using TencentCloud.Soe.V20180724.Models;


namespace TencentCloudExamples
{
class OralevaluationStream
{

static void Main(string[] args)
{
try
{
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
Credential cred = new Credential {
SecretId = "SecretId",
SecretKey = "SecretKey"
};

SoeClient client = new SoeClient(cred, "");
string uuid = Guid.NewGuid().ToString();
byte[] content = File.ReadAllBytes("../SOEMUSIC/apple.mp3");
double content_len = content.Length;
int slice_num = 100 * 1024;
double pkg = content_len / slice_num;
double pkg_num = Math.Ceiling(pkg);

for (int j = 1; j <= pkg_num; j++)
{
int start_index = (j - 1) * slice_num;
int last_index = (int)(j == pkg_num ? content_len : j * slice_num);
int IsEnd = j == pkg_num ? 1 : 0;
byte[] sent_content = content[start_index..last_index];
string data = Convert.ToBase64String(sent_content);//本地音频文件

TransmitOralProcessWithInitRequest req = new TransmitOralProcessWithInitRequest();
req.SeqId = j;
req.IsEnd = IsEnd;
req.VoiceFileType = 3;
req.VoiceEncodeType = 1;
req.UserVoiceData = data;
req.SessionId = uuid;
req.RefText = "book";
req.WorkMode = 0;
req.EvalMode = 1;
req.ScoreCoeff = 1F;
TransmitOralProcessWithInitResponse resp = client.TransmitOralProcessWithInitSync(req);
Console.WriteLine(AbstractModel.ToJsonString(resp));
}

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.Read();
}
}
}
// dotnet run --property:StartupObject=TencentCloudExamples.OralevaluationAsync
using System;
using System.Threading.Tasks;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Soe.V20180724;
using TencentCloud.Soe.V20180724.Models;
using System.IO;
using System.Text.Json;
using Newtonsoft.Json;

namespace TencentCloudExamples
{
public class OralevaluationAsync
{
static void Main(string[] args)
{
try
{
string uuid = Guid.NewGuid().ToString();
string data = Convert.ToBase64String(File.ReadAllBytes("../SOEMUSIC/para.mp3"));//本地音频文件
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
Credential cred = new Credential {
SecretId = "SecretId",
SecretKey = "SecretKey"
};
ClientProfile clientProfile = new ClientProfile();
clientProfile.SignMethod = ClientProfile.SIGN_TC3SHA256;
SoeClient client = new SoeClient(cred, "", clientProfile);


for (int i = 1; i <= 60; i++)
{
TransmitOralProcessWithInitRequest req = new TransmitOralProcessWithInitRequest();
req.SessionId = uuid;
req.RefText = "book";
req.WorkMode = 1;
req.EvalMode = 2;
req.ScoreCoeff = 1F;
req.SeqId = 1;
req.IsEnd = 1;
req.VoiceFileType = 3;
req.VoiceEncodeType = 1;
req.UserVoiceData = data;
req.SessionId = uuid;
req.IsAsync = 1;
// 返回的resp是一个TransmitOralProcessResponse的实例,与请求对象对应
TransmitOralProcessWithInitResponse transresp = client.TransmitOralProcessWithInitSync(req);
// 输出json格式的字符串回包
string result = AbstractModel.ToJsonString(transresp);

dynamic res_obj = JsonConvert.DeserializeObject(result);
string status = res_obj.Status;
Console.WriteLine(result);
if (status == "Finished")
{
break;
}
else
{
req.IsQuery = 1;
}
Thread.Sleep(1000);
}

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.Read();
}
}
}


使用方法说明

1. SessionID:使用 uuid 来作为 SessionID,用于区分不同的音频。
2. base64 转换:读取本地音频文件,转换成 base64 数据进行评测。
3. 流式分片 base64 转换:读取本地音频文件,将音频进行分片,转换成 base64 数据进行评测。
4. 异步轮询:使用 json 反序列化,获取评测结果,判断是否查询到最终结果。
SessionID
base64转换
流式分片
异步轮询
string uuid = Guid.NewGuid().ToString();
string data = Convert.ToBase64String(File.ReadAllBytes(""));//本地音频文件
string uuid = Guid.NewGuid().ToString();
byte[] content = File.ReadAllBytes("../SOEMUSIC/apple.mp3");
double content_len = content.Length;
int slice_num = 1 * 1024;
double pkg = content_len / slice_num;
double pkg_num = Math.Ceiling(pkg);

for (int j = 1; j <= pkg_num; j++)
{
int start_index = (j - 1) * slice_num;
int last_index = (int)(j == pkg_num ? content_len : j * slice_num);
int IsEnd = j == pkg_num ? 1 : 0;
byte[] sent_content = content[start_index..last_index];
string data = Convert.ToBase64String(content);//本地音频文件
}
for (int i = 1; i <= 60; i++)
{
TransmitOralProcessWithInitRequest req = new TransmitOralProcessWithInitRequest();
req.SessionId = uuid;
req.RefText = "book";
req.WorkMode = 1;
req.EvalMode = 2;
req.ScoreCoeff = 1F;
req.SeqId = 1;
req.IsEnd = 1;
req.VoiceFileType = 3;
req.VoiceEncodeType = 1;
req.UserVoiceData = data;
req.SessionId = uuid;
req.IsAsync = 1;
// 返回的resp是一个TransmitOralProcessResponse的实例,与请求对象对应
TransmitOralProcessWithInitResponse transresp = client.TransmitOralProcessWithInitSync(req);
// 输出json格式的字符串回包
string result = AbstractModel.ToJsonString(transresp);

dynamic res_obj = JsonConvert.DeserializeObject(result);
string status = res_obj.Status;
Console.WriteLine(result);
if (status == "Finished")
{
break;
}
else
{
req.IsQuery = 1;
}
Thread.Sleep(1000);
}

临时授权凭证

客户端 SDK 需要使用 获取联合身份临时访问凭证 生成临时密钥,保障密钥安全性。
// dotnet run --property:StartupObject=TencentCloudExamples.GetFederationToken
using System;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Sts.V20180813;
using TencentCloud.Sts.V20180813.Models;
namespace TencentCloudExamples
{
class GetFederationToken
{
static void Main2(string[] args)
{
try
{
// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
Credential cred = new Credential {
SecretId = "SecretId",
SecretKey = "SecretKey"
};

StsClient client = new StsClient(cred, "ap-beijing");
GetFederationTokenRequest req = new GetFederationTokenRequest();
req.Name = "soe";
req.DurationSeconds = 1;

req.Policy = "{\\"version\\": \\"2.0\\", \\"statement\\": {\\"effect\\": \\"allow\\", \\"action\\": [\\"soe:TransmitOralProcessWithInit\\"],\\"resource\\": \\"*\\"}}";
GetFederationTokenResponse resp = client.GetFederationTokenSync(req);
Console.WriteLine(AbstractModel.ToJsonString(resp));
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.Read();
}
}
}