1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
public void Audio2TxtAsync(string inFile, string outFile = null) { var dlt = new DltSpeek(Audio2Txt); dlt.BeginInvoke(inFile, outFile, null, null); }
public void Audio2Txt(string inFile, string outFile = null) { var ret = 0; var result = ""; try { if (!File.Exists(inFile)) throw new Exception("文件" + inFile + "不存在!"); if (inFile.Substring(inFile.Length - 3, 3).ToUpper() != "WAV" && inFile.Substring(inFile.Length - 3, 3).ToUpper() != "PCM") throw new Exception("音频文件格式不对!"); var fp = new FileStream(inFile, FileMode.Open); if (inFile.Substring(inFile.Length - 3, 3).ToUpper() == "WAV") fp.Position = 44; var buff = new byte[BufferNum]; var bp = Marshal.AllocHGlobal(BufferNum); int len; var status = AudioStatus.IsrAudioSampleContinue; var epStatus = EpStatus.IsrEpNull; var recStatus = RecogStatus.IsrRecNull; var rsltStatus = RecogStatus.IsrRecNull; while (fp.Position != fp.Length) { len = fp.Read(buff, 0, BufferNum); Marshal.Copy(buff, 0, bp, buff.Length); ret = AsrDll.QISRAudioWrite(_sessID, bp, (uint)len, status, ref epStatus, ref recStatus); if (ret != 0) { fp.Close(); throw new Exception("QISRAudioWrite err,errCode=" + ((ErrorCode)ret).ToString("G")); } if (recStatus == RecogStatus.IsrRecStatusSuccess) { var p = AsrDll.QISRGetResult(_sessID, ref rsltStatus, WaitTime, ref ret); if (p != IntPtr.Zero) { var tmp = FlyTts.Ptr2Str(p); DataArrived?.Invoke(this, new DataArrivedEventArgs(tmp)); result += tmp; Console.WriteLine(@"返回部分结果!:" + tmp); } } Thread.Sleep(500); } fp.Close();
status = AudioStatus.IsrAudioSampleLast;
ret = AsrDll.QISRAudioWrite(_sessID, bp, 1, status, ref epStatus, ref recStatus); if (ret != 0) throw new Exception("QISRAudioWrite write last audio err,errCode=" + ((ErrorCode)ret).ToString("G")); Marshal.FreeHGlobal(bp); var loopCount = 0; do { var p = AsrDll.QISRGetResult(_sessID, ref rsltStatus, WaitTime, ref ret); if (p != IntPtr.Zero) { var tmp = FlyTts.Ptr2Str(p); DataArrived?.Invoke(this, new DataArrivedEventArgs(tmp)); result += tmp; Console.WriteLine(@"传完音频后返回结果!:" + tmp); } if (ret != 0) throw new Exception("QISRGetResult err,errCode=" + ((ErrorCode)ret).ToString("G")); Thread.Sleep(200); } while (rsltStatus != RecogStatus.IsrRecStatusSpeechComplete && loopCount++ < 30); if (outFile != null) { var fout = new FileStream(outFile, FileMode.OpenOrCreate); fout.Write(Encoding.Default.GetBytes(result), 0, Encoding.Default.GetByteCount(result)); fout.Close(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { ret = AsrDll.QISRSessionEnd(_sessID, string.Empty); ret = AsrDll.MSPLogout(); IsrEnd?.Invoke(this, new EventArgs()); } }
|