public void SpeechRecognition()
        {
            //初始化语音识别
            int    ret          = (int)ErrorCode.MSP_SUCCESS;
            string login_params = string.Format("appid = {0}, work_dir = {1}", ConfigurationManager.AppSettings["AppID"].ToString(), ConfigurationManager.AppSettings["WorkDir"].ToString());

            session_begin_params = ConfigurationManager.AppSettings["SessionBeginParams"].ToString();

            string Username = ConfigurationManager.AppSettings["Username"].ToString();
            string Password = ConfigurationManager.AppSettings["Password"].ToString();

            ret = MSCDLL.MSPLogin(Username, Password, login_params);

            if ((int)ErrorCode.MSP_SUCCESS != ret)
            {
                MessageBox.Show("MSPLogin failed,error code:{0}", ret.ToString());
                MSCDLL.MSPLogout();
            }

            TTS welcome = new TTS();

            welcome.CreateWAV("欢迎使用语音助手");
        }
Ejemplo n.º 2
0
        public static Task RunIAT(List <VoiceData> VoiceReady, string session_begin_params, ref SendDataPipe SendDataPipe)
        {
            IntPtr      session_id = IntPtr.Zero;
            string      rec_result = string.Empty;
            string      hints      = "正常结束";
            AudioStatus aud_stat   = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE;
            EpStatus    ep_stat    = EpStatus.ISR_EP_LOOKING_FOR_SPEECH;
            RecogStatus rec_stat   = RecogStatus.ISR_REC_STATUS_SUCCESS;
            int         errcode    = (int)ErrorCode.MSP_SUCCESS;

            session_id = MSCDLL.QISRSessionBegin(null, session_begin_params, ref errcode);
            if ((int)ErrorCode.MSP_SUCCESS != errcode)
            {
                Debug.WriteLine("\nQISRSessionBegin failed! error code:{0}\n", errcode);
                //return;
            }

            for (int i = 0; i < VoiceReady.Count(); i++)
            {
                aud_stat = AudioStatus.ISR_AUDIO_SAMPLE_CONTINUE;
                if (i == 0)
                {
                    aud_stat = AudioStatus.ISR_AUDIO_SAMPLE_FIRST;
                }
                errcode = MSCDLL.QISRAudioWrite(PtrToStr(session_id), VoiceReady[i].data, (uint)VoiceReady[i].data.Length, aud_stat, ref ep_stat, ref rec_stat);
                if ((int)ErrorCode.MSP_SUCCESS != errcode)
                {
                    MSCDLL.QISRSessionEnd(PtrToStr(session_id), null);
                }
            }

            errcode = MSCDLL.QISRAudioWrite(PtrToStr(session_id), null, 0, AudioStatus.ISR_AUDIO_SAMPLE_LAST, ref ep_stat, ref rec_stat);
            if ((int)ErrorCode.MSP_SUCCESS != errcode)
            {
                Debug.WriteLine("\nQISRAudioWrite failed! error code:{0} \n", errcode);
                //return;
            }

            while (RecogStatus.ISR_REC_STATUS_SPEECH_COMPLETE != rec_stat)
            {
                IntPtr rslt = MSCDLL.QISRGetResult(PtrToStr(session_id), ref rec_stat, 0, ref errcode);

                if ((int)ErrorCode.MSP_SUCCESS != errcode)
                {
                    Debug.WriteLine("\nQISRGetResult failed, error code: {0}\n", errcode);
                    break;
                }
                if (IntPtr.Zero != rslt)
                {
                    string tempRes = PtrToStr(rslt);

                    rec_result = rec_result + tempRes;
                    if (rec_result.Length >= BUFFER_SIZE)
                    {
                        Debug.WriteLine("\nno enough buffer for rec_result !\n");
                        break;
                    }
                }
            }

            //结果
            Debug.WriteLine(rec_result);

            SendDataPipe.Start(rec_result);

            int errorcode = MSCDLL.QISRSessionEnd(PtrToStr(session_id), hints);

            if ((int)ErrorCode.MSP_SUCCESS == errorcode)
            {
                Debug.WriteLine("\nQISRGetResult successfull, {0}\n", hints);
            }

            return(Task.FromResult(rec_result));
        }
Ejemplo n.º 3
0
        public void CreateWAV(string text)
        {
            try
            {
                if (string.IsNullOrEmpty(text))
                {
                    return;
                }

                string filename  = "Call.wav"; //合成的语音文件
                uint   audio_len = 0;

                SynthStatus synth_status = SynthStatus.MSP_TTS_FLAG_STILL_HAVE_DATA;

                string _params = ConfigurationManager.AppSettings["tts_putonghua"].ToString();

                session_ID = MSCDLL.QTTSSessionBegin(_params, ref ret);
                //QTTSSessionBegin方法返回失败
                if (ret != (int)ErrorCode.MSP_SUCCESS)
                {
                    return;
                }
                ret = MSCDLL.QTTSTextPut(Ptr2Str(session_ID), text, (uint)Encoding.Default.GetByteCount(text), string.Empty);
                //QTTSTextPut方法返回失败
                if (ret != (int)ErrorCode.MSP_SUCCESS)
                {
                    return;
                }

                MemoryStream memoryStream = new MemoryStream();
                memoryStream.Write(new byte[44], 0, 44);
                while (true)
                {
                    IntPtr source = MSCDLL.QTTSAudioGet(Ptr2Str(session_ID), ref audio_len, ref synth_status, ref ret);
                    byte[] array  = new byte[(int)audio_len];
                    if (audio_len > 0)
                    {
                        Marshal.Copy(source, array, 0, (int)audio_len);
                    }
                    memoryStream.Write(array, 0, array.Length);
                    Thread.Sleep(1000);
                    if (synth_status == SynthStatus.MSP_TTS_FLAG_DATA_END || ret != 0)
                    {
                        break;
                    }
                }
                WAVE_Header wave_Header = getWave_Header((int)memoryStream.Length - 44);
                byte[]      array2      = this.StructToBytes(wave_Header);
                memoryStream.Position = 0L;
                memoryStream.Write(array2, 0, array2.Length);
                memoryStream.Position = 0L;
                SoundPlayer soundPlayer = new SoundPlayer(memoryStream);
                soundPlayer.Stop();
                soundPlayer.Play();
                if (filename != null)
                {
                    FileStream fileStream = new FileStream(filename, FileMode.Create, FileAccess.Write);
                    memoryStream.WriteTo(fileStream);
                    memoryStream.Close();
                    fileStream.Close();
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                ret = MSCDLL.QTTSSessionEnd(Ptr2Str(session_ID), "");
            }
        }