安装依赖环境
安装 SDK
npm install tencentcloud-sdk-nodejs --save
// 如使用此引入方式,代码中可将require("tencentcloud-sdk-nodejs")改为require("tencentcloud-sdk-nodejs-soe") 。npm install tencentcloud-sdk-nodejs-common tencentcloud-sdk-nodejs-soe tencentcloud-sdk-nodejs-sts --save
使用 SDK
本地音频文件评测
const tencentcloud = require("tencentcloud-sdk-nodejs");//导入对应产品模块的client models。const SoeClient = tencentcloud.soe.v20180724.Client;// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取const clientConfig = {credential: {secretId: "SecretId",secretKey: "SecretKey",},region: "",};// 实例化soe的client对象const client = new SoeClient(clientConfig);const fs = require('fs');let data = fs.readFileSync("SOEMUSIC/apple.mp3"); //本地音频文件data = new Buffer.from(data).toString('base64');var uuid = require('node-uuid');let SessionId = uuid.v1();const params = {"SeqId": 1,"IsEnd": 1,"VoiceFileType": 2,"VoiceEncodeType": 1,"UserVoiceData": data,"SessionId": SessionId,"RefText": "hello","WorkMode": 0,"EvalMode": 0,"ScoreCoeff": 1};// 通过client对象调用TransmitOralProcessWithInit方法发起请求client.TransmitOralProcessWithInit(params).then((data) => {console.log(data);},(err) => {console.error("error", err);});
评测超时处理
如果本地文件过大,可能会导致评测超时。可以选用如下方案:
1. 流式分片传输:将发音数据进行分片处理,减少每次评测的时间。
2. 异步查询:使用异步功能将发音数据先上传,然后使用查询功能查询结构。
const tencentcloud = require("tencentcloud-sdk-nodejs");const fs = require('fs');const uuid = require('node-uuid');//导入对应产品模块的client models。const SoeClient = tencentcloud.soe.v20180724.Client;const clientConfig = {// 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中credential: {secretId: process.env.TENCENTCLOUD_SECRET_ID,// 用户的SecretId,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参考https://cloud.tencent.com/document/product/598/37140secretKey: process.env.TENCENTCLOUD_SECRET_KEY,// 用户的SecretKey,建议使用子账号密钥,授权遵循最小权限指引,降低使用风险。子账号密钥获取可参考https://cloud.tencent.com/document/product/598/37140},region: "",};// 实例化要请求产品的client对象,clientProfile是可选的const client = new SoeClient(clientConfig);async function main(){// 获取base64数据const PKG_SIZE = 100 * 1024;const SessionId = uuid.v1();const data = fs.readFileSync('SOEMUSIC/apple.mp3');//本地音频文件console.log(typeof data);const pkgNum = Math.ceil(data.length / PKG_SIZE);for (let i = 1; i <= pkgNum; i++) {let lastIndex = i == pkgNum ? data.length : i * PKG_SIZElet IsEnd = i == pkgNum ? 1 : 0let buf = data.subarray((i - 1) * PKG_SIZE, lastIndex)base64_data = new Buffer.from(buf).toString('base64');const params = {"SeqId": i,"IsEnd": IsEnd,"VoiceFileType": 3,"VoiceEncodeType": 1,"UserVoiceData": base64_data,"SessionId": SessionId,"RefText": "hello","WorkMode": 0,"EvalMode": 2,"ScoreCoeff": 1};try {const response = await client.TransmitOralProcessWithInit(params);console.log(response);} catch (err) {console.error('error', err);}}}main()
const tencentcloud = require("tencentcloud-sdk-nodejs");//导入对应产品模块的client models。const SoeClient = tencentcloud.soe.v20180724.Client;// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取const clientConfig = {credential: {secretId: "SecretId",secretKey: "SecretKey",},region: "",};// 实例化soe的client对象const client = new SoeClient(clientConfig);// 请求参数赋值var uuid = require('node-uuid');let SessionId = uuid.v1();const fs = require('fs');let data = fs.readFileSync("SOEMUSIC/para.mp3"); //本地音频文件data = new Buffer.from(data).toString('base64');const params = {"SessionId": SessionId,"RefText": "hello","WorkMode": 1,"EvalMode": 2,"ScoreCoeff": 1,"IsAsync":1,"SeqId": 1,"IsEnd": 1,"VoiceFileType": 3,"VoiceEncodeType": 1,"UserVoiceData": data,"SessionId": SessionId,};// 请求查询var t1 = setInterval(check,1000)function check(){client.TransmitOralProcessWithInit(params).then((data) => {console.log(data);// var obj = JSON.parse(data)var status = data.Statusif (status == "Finished") {clearInterval(t1)}else {params["IsQuery"] = 1}},(err) => {console.error("error", err);clearInterval(t1)});}
使用方法说明
1. SessionID:使用 uuid 来作为 SessionID,用于区分不同的音频。
2. base64 转换:读取本地音频文件,转换成 base64 数据进行评测。
3. 流式分片 base64 转换:读取本地音频文件,将音频进行分片,转换成 base64 数据进行评测。
4. 异步轮询:使用 json 反序列化,获取评测结果,判断是否查询到最终结果。
var uuid = require('node-uuid');let SessionId = uuid.v1();
const fs = require('fs');const path = require('path');const mineType = require('mime-types');// 获取base64数据let filePath = path.resolve(''); //本地音频文件地址let data = fs.readFileSync(filePath);data = new Buffer.from(data).toString('base64');
let PKG_SIZE = 1 * 1024;const fs = require('fs');// 获取uuidvar uuid = require('node-uuid');let SessionId = uuid.v1();let data = fs.readFileSync('SOEMUSIC/apple.mp3');//本地音频文件let pkgNum = Math.ceil(data.length / PKG_SIZE);for (let i = 1; i <= pkgNum; i++) {let lastIndex = i == pkgNum ? data.length : i * PKG_SIZElet IsEnd = i == pkgNum ? 1 : 0let buf = data((i - 1) * PKG_SIZE, lastIndex)data = buf.toString('base64');
// 请求查询var t1 = setInterval(check,1000)function check(){client.TransmitOralProcessWithInit(params).then((data) => {console.log(data);// var obj = JSON.parse(data)var status = data.Statusif (status == "Finished") {clearInterval(t1)}else {params["IsQuery"] = 1}},(err) => {console.error("error", err);clearInterval(t1)});}
临时授权凭证
/// Depends on tencentcloud-sdk-nodejs version 4.0.3 or higherconst tencentcloud = require("tencentcloud-sdk-nodejs");const StsClient = tencentcloud.sts.v20180813.Client;// 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密// 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305// 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取const clientConfig = {credential: {secretId: "SecretId",secretKey: "SecretKey",},region: "",};// 实例化要请求产品的client对象,clientProfile是可选的const client = new StsClient(clientConfig);const policy = {version: '2.0',statement: {effect: 'allow',action: ['soe:TransmitOralProcessWithInit'],resource: '*'}};const params = {"Name": "soe","Policy": encodeURIComponent(JSON.stringify(policy))};client.GetFederationToken(params).then((data) => {console.log(data);},(err) => {console.error("error", err);});