假设已有 Base64 编码的音频文件(16kHz, s16le, pcm)
1、获取websocket url
import * as CryptoJS from 'crypto-js';
/**
* 获取websocket url
*/
const getWebSocketUrl = () => {
const config = {
// 请求地址
hostUrl: "wss://iat-api.xfyun.cn/v2/iat",
host: "iat-api.xfyun.cn",
//在控制台-我的应用-语音听写(流式版)获取
appid: '',
apiSecret: '', // 替换为你的 APISecret
apiKey: '', // 替换为你的 APIKey
file: "./16k_10.pcm", //请填写您的音频文件路径
uri: "/v2/iat",
highWaterMark: 1280
}
const date = new Date().toUTCString()
// 鉴权签名
let signatureOrigin = `host: ${config.host}\ndate: ${date}\nGET ${config.uri} HTTP/1.1`
let signatureSha = CryptoJS.HmacSHA256(signatureOrigin, config.apiSecret);
let signature = CryptoJS.enc.Base64.stringify(signatureSha)
let authorizationOrigin = `api_key="${config.apiKey}", algorithm="hmac-sha256", headers="host date request-line", signature="${signature}"`
let authStr = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(authorizationOrigin))
let wssUrl = `${config.hostUrl}?authorization=${authStr}&date=${encodeURIComponent(date).replace(/%2C/g, ',').replace(/%3A/g, ':')}&host=${config.host}`
return wssUrl
}
2、建立连接并发送数据
需要注意的是如果 base64 数据很大不能一次发送完成,就需要将 base64 重新转为二进制数据后按需要的大小切割,然后将切割后的二进制片段再重新转为 base64 编码后发送,否则服务端无法正确读取数据。
import { Buffer } from 'buffer'; // 导入 Buffer
// 如果有socket地址
const url = getWebSocketUrl();
const ws = new WebSocket(url);
ws.onopen = () => {
console.log("🚀 ~ 连接成功")
const params = {
common: { app_id: '' },//appId
business: {
language: "zh_cn",
domain: "iat",
accent: "mandarin",
vad_eos: 5000,
dwa: "wpgs",
},
data: {
status: 0,
format: "audio/L16;rate=16000",
encoding: "raw",
},
};
ws.send(JSON.stringify(WebApiSocketStore.params));
try {
// 解码 Base64 字符串为二进制数据
const audioPcmData = Buffer.from(data.base64Pcm, 'base64');
let offset = 0;
while (offset < audioPcmData.length) {
const subString = audioPcmData.slice(offset, offset + 1280)
offset += 1280
const isEnd = offset >= audioPcmData.length;
const btoa = Buffer.from(subString, 'utf-8').toString('base64');
const dataAudio = JSON.stringify({
data: {
status: isEnd ? 2 : 1,
format: "audio/L16;rate=16000",
encoding: "raw",
audio: `${btoa}`, // 将二进制字符串转换为 Base64
},
})
ws?.send(dataAudio);
}
} catch (error) {
console.log('🚀 ~ Error reading file:', error);
}
};
ws.onmessage = (e) => {
// a message was received
console.log("🚀 ~ e.data:", e.data)
const jsonData = JSON.parse(e?.data || {});
// 处理返回结果
renderResult(e?.data || {});
if (jsonData.code === 0 && jsonData.data.status === 2) {
setVoiceText(resultText.current);
ws.close();
}
if (jsonData.code !== 0) {
ws.close();
}
};
ws.onerror = (e) => {
// an error occurred
console.log("🚀 ~ e.error", e.message)
};
ws.onclose = (e) => {
// connection closed
console.log("🚀 ~ e.code:", e.code, e.reason)
};
3、处理语音返回结果
const resultText = React.useRef<string>('');
const resultTextTemp = React.useRef<string>('');
// 识别结束 处理返回结果
const renderResult = (resultData: string) => {
let jsonData = JSON.parse(resultData);
if (jsonData.data && jsonData.data.result) {
let data = jsonData.data.result;
let str = "";
let { ws } = data;
for (let i = 0; i < ws.length; i++) {
str = str + ws[i].cw[0].w;
}
// 开启wpgs会有此字段(前提:在控制台开通动态修正功能)
// 取值为 "apd"时表示该片结果是追加到前面的最终结果;取值为"rpl" 时表示替换前面的部分结果,替换范围为rg字段
if (data.pgs) {
if (data.pgs === "apd") {
// 将resultTextTemp同步给resultText
resultText.current = resultTextTemp.current;
}
// 将结果存储在resultTextTemp中
resultTextTemp.current = resultText.current + str;
} else {
resultText.current = resultText.current + str;
}
}
}