微信小程序+.NET(四) 科大讯飞语音接口-iat语音听写

  科大讯飞语音接口-iat语音听写

  Demo下载链接:https://download.csdn.net/download/jinglell/11566402
语音转文本 (iat/语音听写)
  首先,来看科大讯飞给的msc.dll API文档,
可以看到给出了语音识别详细的接口说明,

http://mscdoc.xfyun.cn/windows/api/iFlytekMSCReferenceManual/qisr_8h.html

qisr.h
  然后我们看另一个文档

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值