若使用C# ,请用CS转VBConverter 工具转换
Imports System.Runtime.InteropServices
Imports System.IO
#Const _Debug = 1
#Const _WriteWaveFile = 0
Public Class SpeechRecogWav
Dim _appid As String
Public Sub New(appid As String)
_appid = appid
End Sub
Const xfAppId = "appid = 599dXXXX" '***************科大讯飞的appid
Dim login_params As String = xfAppId & ",engine_start = ivw,ivw_res_path =fo|res/ivw/wakeupresource.jet, work_dir = ." '//使用唤醒需要在此设置engine_start = ivw,ivw_res_path =fo|xxx/xx 启动唤醒引擎
Dim session_begin_params As String = "sub = iat, domain = iat, language = zh_cn, accent = mandarin, sample_rate = 16000, result_type = plain, result_encoding = gb2312"
' Dim grammar_id As String = vbNullString
Dim Sessionid As String
Public Sub Logoin() '第一步 登录
Dim ret As Integer = 0
ret = MSPLogin(vbNullString, vbNullString, login_params) '第一个参数是用户名,第二个参数是密码,均传NULL即可,第三个参数是登录参数
If (MSP_SUCCESS <> ret) Then 'vbNullString 与vbNull 不同,见
' debug.print(ret)
Debug.Print("speechrecog login error" & ret)
End If
End Sub
#Region "iat 可上传 也可不上传语法 ASR需上传语法进行判断"
'''是否上传语法文件,用户词表
Public Function UpdataGrammar(abnfFile As String) As String '第二步上传语法文件
'// 获取语法ID
Dim fs As New IO.FileStream(abnfFile, IO.FileMode.Open)
Dim ret As Integer = 0
Dim flen As Integer = fs.Length
Dim grammar_len As Integer = fs.Length
Dim buff As Byte()
ReDim buff(flen)
fs.Read(buff, 0, flen)
fs.Close()
ret = -1
Dim ret_id As IntPtr = MSPUploadData("usergram", buff, grammar_len, "dtt = abnf, sub = asr", ret)
Dim grammar_id As String = Marshal.PtrToStringAnsi(ret_id)
Return grammar_id
End Function
'上传用户词表
Public Function UpdataUserWordFile(useWordFile As String) As Integer
Dim fs As New IO.FileStream(useWordFile, IO.FileMode.Open)
Dim ret As Integer = 0
Dim flen As Integer = fs.Length
Dim buff As Byte()
ReDim buff(flen)
fs.Read(buff, 0, flen)
fs.Close()
ret = -1
' 用户词表上传不需要获取GRAMMAR_ID
MSPUploadData("userwords", buff, flen, "sub = uup, dtt = userword", ret) ' //上传用户词表
Return ret
End Function
'上传用户词表
Public Function UpdataUserWord(useWord As String) As Integer
Dim ret As Integer = -1
' 用户词表上传不需要获取GRAMMAR_ID
Dim Bt() As Byte = System.Text.Encoding.ASCII.GetBytes(useWord)
' Bt(useWord.Length - 1) = 0
MSPUploadData("userwords", Bt, Bt.Length, "sub = uup, dtt = userword", ret) ' //上传用户词表
#If _Debug Then
Debug.Print("update userword:" & ret.ToString)
#End If
Return ret
End Function
#End Region
Dim status As AudioStatus
Dim ep_status As EpStatus
Dim rec_status As RecogStatus
Dim rslt_status As RecogStatus
Dim waveCount As Integer = 0
Dim isOnSession As Boolean = False
'开始录音
Public Sub SessionBegin() '第二步 开始对话
If (isOnSession = True) Then
SessionEnd() '结束上次会话
End If
isOnSession = True
status = AudioStatus.ISR_AUDIO_SAMPLE_FIRST '第一次时使用此
ep_status = EpStatus.ISR_EP_LOOKING_FOR_SPEECH
rec_status = RecogStatus.ISR_REC_STATUS_SUCCESS
rslt_status = RecogStatus.ISR_REC_STATUS_SUCCESS
Dim ret As Integer = 0
'开始对话
Dim errorcode2 As Integer = 0
Sessionid = Marshal.PtrToStringAnsi(QISRSessionBegin(vbNullString, session_begin_params, errorcode2)) 'SESSION_BEGIN_PARAMS 而不是 LOGIN
If (errorcode2 <> 0) Then
Debug.Print("**session begin fail: " & errorcode2)
' Sessionid = Marshal.PtrToStringAnsi(QISRSessionBegin(vbNullString, session_begin_params, errorcode2)) 'SESSION_BEGIN_PARAMS 而不是 LOGIN
End If
End Sub
'上传录音数据
Public Function UpdateWave(lpdata As Byte(), datalen As Integer) As Boolean
#If _WriteWaveFile Then
Wave.WaveWriteFile(lpdata, datalen)
#End If
Dim ret As Integer = 0
status = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE
If (datalen <= 0) Then
Return True
End If
'写入【待识别】语音
ret = QISRAudioWrite(Sessionid, lpdata, datalen, status, ep_status, rec_status)
If (MSP_SUCCESS <> ret) Then
Debug.Print("//audio write error")
isOnSession = False
QISRSessionEnd(Sessionid, String.Empty)
Return False
End If
'检测到结束端点 返回FALSE
If (ep_status = EpStatus.ISR_EP_AFTER_SPEECH) Then
Debug.Print("UPDATAWAVE ISE EP AFTER SPEECH")
Return False
End If
Dim waittime As Integer = 1000
Dim errcode As Integer
'【获取】识别内容
If (rec_status = RecogStatus.ISR_REC_STATUS_SUCCESS) Then
Dim p As IntPtr = QISRGetResult(Sessionid, rslt_status, waittime, errcode)
If (p <> IntPtr.Zero) Then
Dim tmp As String = Marshal.PtrToStringAnsi(p) '识别的内容
'If (Not waveEventCallBack Is Nothing) Then
' waveEventCallBack(1, tmp)
'End If
System.Console.WriteLine("updateWave返回部分结果!:" + tmp)
End If
End If
Threading.Thread.Sleep(200) '模拟人说话时间间隙,10帧的音频长度为200ms
If (ret <> 0) Then
Debug.Print("qisrupdateaudio fail")
Return False
End If
Return True
End Function
'Public Delegate Sub DelgWaveEventCallBackFun(type As Integer, txt As String)
'Public waveEventCallBack As DelgWaveEventCallBackFun
'Public Sub AddWaveEventCallBack(WCB As DelgWaveEventCallBackFun)
' waveEventCallBack = WCB
'End Sub
'第-2步,结束对话
Public Sub SessionEnd()
isOnSession = False
#If _WriteWaveFile Then
Wave.CloseWaveFile()
#End If
' wav.StopRecord()
'写入上传结束标志
status = AudioStatus.ISR_AUDIO_SAMPLE_LAST
Dim errcode As Integer
errcode = QISRAudioWrite(Sessionid, Nothing, 0, status, ep_status, rec_status)
If (errcode <> 0) Then
Debug.Print("**write end fail")
QISRSessionEnd(Sessionid, String.Empty)
Return
End If
Dim waittime As Integer = 1000
'获取识别内容
If (rec_status = RecogStatus.ISR_REC_STATUS_SUCCESS) Then
Dim p As IntPtr = QISRGetResult(Sessionid, rslt_status, waittime, errcode)
If (p <> IntPtr.Zero) Then
Dim tmp As String = Marshal.PtrToStringAnsi(p)
'If (Not waveEventCallBack Is Nothing) Then
' waveEventCallBack(1, tmp)
'End If
' RaiseEvent WaveTxtEvent(tmp) '******事件
System.Console.WriteLine("结束会话返回部分结果!:" + tmp)
End If
End If
Dim loop_count = 0
While (rslt_status <> RecogStatus.ISR_REC_STATUS_SPEECH_COMPLETE And loop_count < 300)
Dim p As IntPtr = QISRGetResult(Sessionid, rslt_status, waittime, errcode)
If (p <> IntPtr.Zero) Then
Dim tmp As String = Marshal.PtrToStringAnsi(p)
'If (Not waveEventCallBack Is Nothing) Then
' If (tmp <> "。") Then
' waveEventCallBack(1, tmp)
' End If
'End If
' RaiseEvent WaveTxtEvent(tmp) '******事件
System.Console.WriteLine("传完音频后返回结果!:" + tmp)
End If
If (errcode <> 0) Then
Debug.Print("end session qisrgetresult " & errcode.ToString)
Exit While
End If
Threading.Thread.Sleep(150)
loop_count = loop_count + 1
End While
'结束对话
QISRSessionEnd(Sessionid, String.Empty)
End Sub
'最后一步 登出
Public Sub Logout()
'登出
MSPLogout()
End Sub
End Class
若要使用语音识别,请将APPID ,跟DLL 文件换成你自己的
资源地址:https://download.csdn.net/download/qq_39239990/10384854 开源