大佬原博客链接: Unity+C#,讯飞 文本转语音、语音转文本_讯飞在unity中实现离线语音转文字-优快云博客文章浏览阅读3.6k次,点赞32次,收藏42次。本文介绍在Unity3D中,使用C#编写讯飞语音转文本、文本转语音的功能。这两个功能相似,学会一个另一个就简单了。_讯飞在unity中实现离线语音转文字https://blog.youkuaiyun.com/qq_44284055/article/details/134596939在此基础上,针对语音转文本,出现大量重复的话术,做了一些改动,原因是结果中添加了大量的过程语句导致,可能和讯飞控制台,开通的多候选句级有关系,具体也没有测试
主要针对STTParseMessage方法的数据解析做了一些变动。有需要的佬,可以参考试试
/// <summary>
/// 讯飞语音转文本,接收消息
/// </summary>
/// <returns></returns>
private async Task<string> STTReceiveMessage()
{
//webSocket.
//状态值
int status = 0;
string finalText = string.Empty;
while (status != 2)
{
byte[] buffer = new byte[8 * 1024];
WebSocketReceiveResult webSocketReceiveResult = await webSocket.ReceiveAsync(buffer, CancellationToken.None);
string receivedMessage = Encoding.UTF8.GetString(buffer, 0, webSocketReceiveResult.Count);
//await Console.Out.WriteLineAsync("receivedMessage:" + receivedMessage);
//finalText += STTParseMessage(receivedMessage, out status);
//这里不重复叠加
finalText = STTParseMessage(receivedMessage, out status);
}
Debug.Log("讯飞语音转文本:" + finalText);
return finalText;
}
/// <summary>
/// 讯飞语音转文本,解析收到的Json消息 //这里参考官方java的demo改了数据解析
/// </summary>
/// <param name="message"></param>
/// <param name="status"></param>
/// <returns></returns>
private string STTParseMessage(string message, out int status)
{
ResponseData resp = JsonConvert.DeserializeObject<ResponseData>(message);
if (resp != null)
{
if(resp.getCode() != 0)
{
Debug.Log("code=>" + resp.getCode() + " error=>" + resp.getMessage() + " sid=" + resp.getSid());
Debug.Log("错误码查询链接:https://www.xfyun.cn/document/error-code");
}
else
{
if(resp.getData() != null)
{
if (resp.getData().getResult() != null)
{
Text te = resp.getData().getResult().getText();
try
{
Decoder.decode(te);
status = resp.getData().getStatus();
Debug.Log("中间识别结果 ==》" + Decoder.toString());
return Decoder.toString();
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
if (resp.getData().getStatus() == 2)
{
status = resp.getData().getStatus();
var txt = Decoder.toString();
Decoder.discard();
return txt;
}
}
}
}
status = -1;
return string.Empty;
}
[Serializable]
public class ResponseData
{
public int code;
public string message;
public string sid;
public Data data;
public int getCode()
{
return code;
}
public string getMessage()
{
return this.message;
}
public string getSid()
{
return sid;
}
public Data getData()
{
return data;
}
}
[Serializable]
public class Data
{
public int status;
public Result result;
public int getStatus()
{
return status;
}
public Result getResult()
{
return result;
}
}
[Serializable]
public class Result
{
public int bg;
public int ed;
public String pgs;
public int[] rg;
public int sn;
public Ws[] ws;
public bool ls;
public JObject vad;
public Text getText()
{
Text text = new Text();
StringBuilder sb = new StringBuilder();
foreach (Ws ws in this.ws)
{
sb.Append(ws.cw[0].w);
}
text.sn = this.sn;
text.text = sb.ToString();
text.sn = this.sn;
text.rg = this.rg;
text.pgs = this.pgs;
text.bg = this.bg;
text.ed = this.ed;
text.ls = this.ls;
text.vad = this.vad == null ? null : this.vad;
return text;
}
}
[Serializable]
public class Ws
{
public Cw[] cw;
public int bg;
public int ed;
}
[Serializable]
public class Cw
{
public int sc;
public string w;
}
[Serializable]
public class Text
{
public int sn;
public int bg;
public int ed;
public string text;
public string pgs;
public int[] rg;
public bool deleted;
public bool ls;
public JObject vad;
public string toString()
{
return "Text{" +
"bg=" + bg +
", ed=" + ed +
", ls=" + ls +
", sn=" + sn +
", text='" + text + '\'' +
", pgs=" + pgs +
", rg=" + rg.ToString() +
", deleted=" + deleted +
", vad=" + (vad == null ? "null" : vad["ws"].ToString()) +
'}';
}
}
//解析返回数据,仅供参考
public static class Decoder
{
private static Text[] texts;
private static int defc = 10;
static Decoder()
{
texts = new Text[defc];
}
public static void decode(Text text)
{
if (text.sn >= defc)
{
resize();
}
if ("rpl".Equals(text.pgs))
{
for (int i = text.rg[0]; i <= text.rg[1]; i++)
{
texts[i].deleted = true;
}
}
texts[text.sn] = text;
}
public static string toString()
{
StringBuilder sb = new StringBuilder();
foreach (Text t in texts)
{
if (t != null && !t.deleted)
{
sb.Append(t.text);
}
}
return sb.ToString();
}
public static void resize()
{
int oc = defc;
defc <<= 1;
Text[] old = texts;
texts = new Text[defc];
for (int i = 0; i < oc; i++)
{
texts[i] = old[i];
}
}
public static void discard()
{
for (int i = 0; i < texts.Length; i++)
{
texts[i] = null;
}
}
}