科大讯飞语音接口-iat语音听写
Demo下载链接:
https://download.csdn.net/download/jinglell/11566402

首先,来看科大讯飞给的msc.dll API文档,
可以看到给出了语音识别详细的接口说明,
http://mscdoc.xfyun.cn/windows/api/iFlytekMSCReferenceManual/qisr_8h.html
然后我们看另一个文档
http://mscdoc.xfyun.cn/windows/api/iFlytekMSCReferenceManual/msp__cmn_8h.html
这里面就包含了MSC的初始化登入与最后的登出。
接下来就是引入MSC里面的接口,使用DLLImport(),我将DLL的import单独放在了一个文件里。
之后就是编写audio2text.ashx请求页,
首先要对appID进行修改,修改你自己的,并将msc.dll替换为你自己从官网下载来的,以免版本不同出现问题(若是遇到找不到某个msc.dll里的方法,请先到上面的MSCAPI文档里找原因)
private static string audio_iat(string audio_path, string session_begin_params)
{
if (audio_path == null || audio_path == "") return "";
IntPtr session_id;
StringBuilder result = new StringBuilder();//存储最终识别的结果
var aud_stat = AudioStatus.MSP_AUDIO_SAMPLE_CONTINUE;//音频状态
var ep_stat = EpStatus.MSP_EP_LOOKING_FOR_SPEECH;//端点状态
var rec_stat = RecogStatus.MSP_REC_STATUS_SUCCESS;//识别状态
int errcode = (int)Errors.MSP_SUCCESS;
byte[] audio_content; //用来存储音频文件的二进制数据
int totalLength = 0;//用来记录总的识别后的结果的长度,判断是否超过缓存最大值
try
{
audio_content = File.ReadAllBytes(audio_path);
//SoundPlayer player = new SoundPlayer(audio_path);
//player.Play();
}
catch (Exception e)
{
//Console.WriteLine(e);
System.Diagnostics.Debug.WriteLine(e.Message);
audio_content = null;
}
try
{
if (audio_content == null)
throw new Exception("没有读取到任何内容");
//Console.WriteLine("开始进行语音听写.......");
session_id = mscDLL.QISRSessionBegin(null, session_begin_params, ref errcode);
if (errcode != (int)Errors.MSP_SUCCESS)
throw new Exception("开始一次语音识别失败!");
int res = mscDLL.QISRAudioWrite(session_id, audio_content, (uint)audio_content.Length, aud_stat, ref ep_stat, ref rec_stat);
if (res != (int)Errors.MSP_SUCCESS)
throw new Exception("写入识别的音频失败!" + res);
res = mscDLL.QISRAudioWrite(session_id, null, 0, AudioStatus.MSP_AUDIO_SAMPLE_LAST, ref ep_stat, ref rec_stat);
if (res != (int)Errors.MSP_SUCCESS)
new Exception("写入音频失败!" + res);
while (RecogStatus.MSP_REC_STATUS_COMPLETE != rec_stat)
{
IntPtr now_result = mscDLL.QISRGetResult(session_id, ref rec_stat, 0, ref errcode);
if (errcode != (int)Errors.MSP_SUCCESS)
throw new Exception("获取结果失败:" + errcode);
if (now_result != null)
{
int length = now_result.ToString().Length;
totalLength += length;
if (totalLength > 4096)
throw new Exception("缓存空间不够" + totalLength);
result.Append(Marshal.PtrToStringAnsi(now_result));
}
Thread.Sleep(150);//防止频繁占用cpu
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
finally
{
//Console.WriteLine("语音听写结束");
//Console.WriteLine("听写结果: ");
//Console.WriteLine(result);
//int res = mscDLL.MSPLogout();//用户名,密码,登陆信息,前两个均为空
}
return result.ToString();
}
而这时其实已经可以对官方提供的.wav测试样例进行识别了,但是我们平时若是有其他系统对接我们的接口时,比如我这次的微信小程序,会发现通过录音得来的是.mp3音频格式,科大讯飞要求的却是.wav/.pcm格式,该怎么转换音频格式?
见下一篇文章:
微信小程序+.NET(五) 音频格式转换:
https://blog.csdn.net/jinglell/article/details/99677371