Esempio n. 1
0
    /// <summary>
    /// 系统初始化
    /// </summary>
    public void init()
    {
        curMode         = 0;
        answer_time     = 0f;
        UserStartAnswer = false;
        isAnswer        = false;
        AnswerAnalysis  = false;
        flow_change     = false;
        FinishedAnswer  = false;
        canPlay         = false;
        isFinished      = false;
        isTransit       = false;
        voice_path      = Application.dataPath + "/Resources/Voice";
        record_path     = Application.dataPath + "/Resources/Question";
        if (!Directory.Exists(voice_path))
        {
            Directory.CreateDirectory(voice_path);
        }
        if (!Directory.Exists(record_path))
        {
            Directory.CreateDirectory(record_path);
        }
        if (!File.Exists(record_path + "/record.xml"))
        {
            File.Create(record_path + "/record.xml");
        }
        if (CharacterModel == null)
        {
            Debug.Log("加载人物模型失败");
            return;
        }
        UIObject u = Camera.main.GetComponent <UIObject>();

        if (u.M2P_Answer_Panel == null)
        {
            Debug.Log("加载沙勿略问我界面失败");
            return;
        }
        if (u.P2M_Ask_Panel == null)
        {
            Debug.Log("加载我问沙勿略界面失败");
            return;
        }
        u.HideM2PAnswerPanel();
        u.HideP2MAskPanel();
        u.M2P_Answer_Panel.transform.GetChild(0).transform.GetChild(0).gameObject.GetComponent <UILabel>().text = "";
        u.M2P_Answer_Panel.transform.GetChild(0).transform.GetChild(1).gameObject.GetComponent <UILabel>().text = "";
        u.M2P_Answer_Panel.transform.GetChild(0).transform.GetChild(2).gameObject.GetComponent <UILabel>().text = "";
        u.M2P_Answer_Panel.transform.GetChild(0).transform.GetChild(3).gameObject.GetComponent <UILabel>().text = "";
        u.M2P_Answer_Panel.transform.GetChild(1).gameObject.GetComponent <UILabel>().text = "";
        u.M2P_Answer_Panel.transform.GetChild(2).gameObject.GetComponent <UILabel>().text = "";
        u.P2M_Ask_Panel.transform.GetChild(0).transform.GetChild(1).gameObject.SetActive(false);
        u.P2M_Ask_Panel.transform.GetChild(0).transform.GetChild(1).gameObject.GetComponent <UILabel>().text = "";
        u.P2M_Ask_Panel.transform.GetChild(0).transform.GetChild(2).gameObject.SetActive(false);
        u.P2M_Ask_Panel.transform.GetChild(0).transform.GetChild(2).gameObject.GetComponent <UILabel>().text = "";
        CharacterModel.GetComponent <Animation>().Stop();
        FlowManage.EnterStandBy(CharacterModel);
    }
Esempio n. 2
0
 /// <summary>
 /// 语音唤醒回调函数
 /// </summary>
 /// <param name="sessionID"></param>
 /// <param name="msg"></param>
 /// <param name="param1"></param>
 /// <param name="param2"></param>
 /// <param name="info"></param>
 /// <param name="userData"></param>
 /// <returns></returns>
 static int registerCallback(string sessionID, msgProcCb msg, int param1, int param2, IntPtr info, IntPtr userData)
 {
     Debug.Log(string.Format("唤醒返回状态{0}", msg));
     if (msgProcCb.MSP_IVW_MSG_ERROR == msg) //唤醒出错消息
     {
         isWakeUp = false;
         Debug.Log(string.Format("{1} 唤醒失败({0})!", param1, DateTime.Now.Ticks));
     }
     else //唤醒成功消息
     {
         Debug.Log(string.Format("唤醒成功({0})!", info));
         FlowManage.M2PMode(1);
     }
     return(0);
 }
Esempio n. 3
0
    /// <summary>
    /// 语音识别方法
    /// </summary>
    /// <param name="sid"></param>
    public static void SpeechRecognition(List <VoiceData> VoiceBuffer)
    {
        audio_stat = audioStatus.MSP_AUDIO_SAMPLE_CONTINUE;
        ep_status  = epStatus.MSP_EP_LOOKING_FOR_SPEECH;
        recoStatus = RecogStatus.ISR_REC_STATUS_SUCCESS;
        sid        = Utils.Ptr2Str(MSC.QISRSessionBegin(string.Empty, speech_param, ref ret));
        Debug.Log(string.Format("-->开启一次语音识别[{0}]", sid));
        if (ret != (int)ErrorCode.MSP_SUCCESS)
        {
            Debug.Log("加载失败!"); return;
        }

        for (int i = 0; i < VoiceBuffer.Count(); i++)
        {
            audio_stat = audioStatus.MSP_AUDIO_SAMPLE_CONTINUE;
            if (i == 0)
            {
                audio_stat = audioStatus.MSP_AUDIO_SAMPLE_FIRST;
            }
            ret = MSC.QISRAudioWrite(sid, VoiceBuffer[i].data, (uint)VoiceBuffer[i].data.Length, audio_stat, ref ep_status, ref recoStatus);
            if ((int)ErrorCode.MSP_SUCCESS != ret)
            {
                MSC.QISRSessionEnd(sid, null);
            }
        }

        ret = MSC.QISRAudioWrite(sid, null, 0, audioStatus.MSP_AUDIO_SAMPLE_LAST, ref ep_status, ref recoStatus);
        if ((int)ErrorCode.MSP_SUCCESS != ret)
        {
            Debug.Log("\nQISRAudioWrite failed! error code:" + ret);
            return;
        }

        while (RecogStatus.ISR_REC_STATUS_SPEECH_COMPLETE != recoStatus)
        {
            IntPtr rslt = MSC.QISRGetResult(sid, ref recoStatus, 0, ref ret);
            if ((int)ErrorCode.MSP_SUCCESS != ret)
            {
                Debug.Log("\nQISRGetResult failed, error code: " + ret);
                break;
            }
            if (IntPtr.Zero != rslt)
            {
                string tempRes = Utils.Ptr2Str(rslt);

                ask_rec_result = ask_rec_result + tempRes;
                if (ask_rec_result.Length >= BUFFER_SIZE)
                {
                    Debug.Log("\nno enough buffer for rec_result !\n");
                    break;
                }
            }
        }
        int errorcode = MSC.QISRSessionEnd(sid, "正常结束");

        //语音识别结果
        if (ask_rec_result.Length != 0)
        {
            FlowManage.P2MMode(nar);
            Debug.Log("识别结果是:" + ask_rec_result);
            ask_rec_result = "";
        }
    }
Esempio n. 4
0
    // Update is called once per frame
    void Update()
    {
        /*if (FlowStart) //监听流程是否开始
         * {
         *  FlowStart = false;
         *  curMode = 1;
         *  FlowManage.PlayTransitVoice(1, "下面进入沙勿略问我环节。");
         * }*/

        if (canPlay) //语音合成完毕并生成音频后播放
        {
            canPlay = false;
            FlowManage.waveOutDevice = new WaveOutEvent();
            //waveOutDevice.PlaybackStopped += waveOutDevice_PlaybackStopped;
            FlowManage.audioFileReader = new AudioFileReader(voice_path + "/" + FlowManage.voicename + ".wav");
            FlowManage.waveOutDevice.Init(FlowManage.audioFileReader);
            FlowManage.waveOutDevice.Play();
            FlowManage.PlayModeAnimation();
            if (curMode == 1 && UserStartAnswer)
            {
                FlowManage.ShowQuestionInfo();
            }
        }
        if (FlowManage.waveOutDevice != null) //音频播放完毕后开始答题
        {
            if (UserStartAnswer)              //用户开始回答问题
            {
                if (FlowManage.waveOutDevice.PlaybackState == PlaybackState.Stopped)
                {
                    FlowManage.animName = AnimationControl.GetAnimationClipName(CharacterAction.Looking);
                    FlowManage.PlayModeAnimation();
                    UserStartAnswer = false;
                    Debug.Log("开始答题");
                    isAnswer = true;
                    if (FlowManage.waveOutDevice != null)
                    {
                        FlowManage.waveOutDevice.Dispose();
                        FlowManage.waveOutDevice = null;
                    }
                    if (FlowManage.audioFileReader != null)
                    {
                        FlowManage.audioFileReader.Close();
                        FlowManage.audioFileReader = null;
                    }
                    singleNar.StartRec();
                }
            }
            if (AnswerAnalysis) //沙勿略问我模式答题后给出答案解析
            {
                if (FlowManage.waveOutDevice.PlaybackState == PlaybackState.Stopped)
                {
                    if (FlowManage.waveOutDevice != null)
                    {
                        FlowManage.waveOutDevice.Dispose();
                        FlowManage.waveOutDevice = null;
                    }
                    if (FlowManage.audioFileReader != null)
                    {
                        FlowManage.audioFileReader.Close();
                        FlowManage.audioFileReader = null;
                    }
                    Debug.Log("完成答案解析,进入下一题");
                    AnswerAnalysis = false;
                    int questionNo = FlowManage.curNo;
                    questionNo++;
                    if (questionNo <= 3)
                    {
                        FlowManage.M2PMode(questionNo);
                    }
                    else
                    {
                        curMode = 2;
                        FlowManage.PlayTransitVoice(2, "下面进入我问沙勿略环节。");
                    }
                }
            }
            if (isTransit) //流程过渡播放声音
            {
                if (FlowManage.waveOutDevice != null)
                {
                    if (FlowManage.waveOutDevice.PlaybackState == PlaybackState.Stopped)
                    {
                        FlowManage.animName = AnimationControl.GetAnimationClipName(CharacterAction.Looking);
                        FlowManage.PlayModeAnimation();
                        if (FlowManage.waveOutDevice != null)
                        {
                            FlowManage.waveOutDevice.Dispose();
                            FlowManage.waveOutDevice = null;
                        }
                        if (FlowManage.audioFileReader != null)
                        {
                            FlowManage.audioFileReader.Close();
                            FlowManage.audioFileReader = null;
                        }
                        isTransit = false;
                        Debug.Log("完成过渡");
                        flow_change = true;
                    }
                }
            }
            if (FinishedAnswer) //我问沙勿略环节回答完毕
            {
                if (FlowManage.waveOutDevice.PlaybackState == PlaybackState.Stopped)
                {
                    if (FlowManage.waveOutDevice != null)
                    {
                        FlowManage.waveOutDevice.Dispose();
                        FlowManage.waveOutDevice = null;
                    }
                    if (FlowManage.audioFileReader != null)
                    {
                        FlowManage.audioFileReader.Close();
                        FlowManage.audioFileReader = null;
                    }
                    FinishedAnswer            = false;
                    FlowManage.canDistinguish = true;
                }
            }
            if (isFinished) //我问沙勿略环节结束
            {
                if (FlowManage.waveOutDevice.PlaybackState == PlaybackState.Stopped)
                {
                    if (FlowManage.waveOutDevice != null)
                    {
                        FlowManage.waveOutDevice.Dispose();
                        FlowManage.waveOutDevice = null;
                    }
                    if (FlowManage.audioFileReader != null)
                    {
                        FlowManage.audioFileReader.Close();
                        FlowManage.audioFileReader = null;
                    }
                    isFinished = false;
                    FlowManage.canDistinguish = true;
                    FlowStart = false;
                    init();
                }
            }
        }
        if (Input.GetMouseButtonDown(1))
        {
            if (!FlowStart)
            {
                FlowStart = true;
                curMode   = 1;
                FlowManage.PlayTransitVoice(1, "下面进入沙勿略问我环节。");
            }
        }

        if (isAnswer) //答题计时
        {
            answer_time += Time.deltaTime;
            if (answer_time >= 10)
            {
                isAnswer    = false;
                answer_time = 0f;
                singleNar.StopRec();
            }
        }
        if (curMode == 1 && successDistinguish)
        {
            FlowManage.StopAnswer();
            successDistinguish = false;
        }
        if (flow_change) //切换流程
        {
            flow_change = false;
            if (curMode == 1)
            {
                FlowManage.M2PMode(1);
            }
            else if (curMode == 2)
            {
                //AskMode = true;
                u.HideM2PAnswerPanel();
                u.ShowP2MAskPanel();
                FlowManage.canDistinguish = true;
                VoiceManage vm = new VoiceManage();
                vm.VoiceDistinguish();
                //nar.StartRec();
            }
        }
    }