安装依赖环境
安装 SDK
dotnet add package TencentCloudSDK
dotnet add package TencentCloudSDK.Soedotnet add package TencentCloudSDK.Stsdotnet add package TencentCloudSDK.Common
使用 SDK
本地音频文件评测
// dotnet run --property:StartupObject=TencentCloudExamples.TransmitOralProcessWithInitusing 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.OralevaluationStreamusing 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.OralevaluationAsyncusing 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 反序列化,获取评测结果,判断是否查询到最终结果。
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);}
临时授权凭证
// dotnet run --property:StartupObject=TencentCloudExamples.GetFederationTokenusing 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();}}}