Exemplo n.º 1
0
        /// <summary>
        /// 副界面的输入框允许用户输入文本。通过数据处理顺序以得到有效的答案来响应
        /// 若询问的文本内容无法得到解答, 回应一段语音
        /// </summary>
        /// <param name="text">输入的查询文本</param>

        /*
         * 数据模型调用顺序:
         * 函数迭代
         * 首先查询用户自建的本地数据(通过魔法训练中自定义的回答) ? None ->
         * 远程数据库 ? None ->
         * WolframAlpha ? None -> ...
         */
        public SecondaryInterSearchControl(string qText, System.Windows.Controls.TextBox inputBoxObj)
        {
            this.inputBoxObj = inputBoxObj;
            this.queryText   = qText;
            Console.WriteLine(this.queryText);
            string rst = QueryCustomAnswerXY(this.queryText);

            // 翻译至英文过程中失败
            if (rst == "TRANS_TO_EN_FAILED")
            {
                if (SecondaryInterSearchControl.speechTextDisplay.IsVisible)
                {
                    SecondaryInterSearchControl.speechTextDisplay.your_msg_display.Text = this.queryText;
                    SecondaryInterSearchControl.speechTextDisplay.xy_msg_display.Text   = "哎呀,出错了..";
                }
                else
                {
                    SecondaryInterSearchControl.speechTextDisplay = new page.other.SpeechTextDisplay(this.queryText, "哎呀,出错了..");
                    SecondaryInterSearchControl.speechTextDisplay.Show();
                }
                return;
            }

            /**
             * 显示响应的界面 注: 如果翻译至中文时发生错误, 代表之前的英文操作执行正常 显示英文的答案
             * 否则 中英文将一并显示
             * 如果异常 连接问题, 显示的是_connectFailed
             */
            Console.WriteLine("最终答案:" + this.answerText);
            if (SecondaryInterSearchControl.speechTextDisplay.IsVisible)
            {
                SecondaryInterSearchControl.speechTextDisplay.your_msg_display.Text = this.queryText;
                SecondaryInterSearchControl.speechTextDisplay.xy_msg_display.Text   = this.answerText;
            }
            else
            {
                SecondaryInterSearchControl.speechTextDisplay = new page.other.SpeechTextDisplay(this.queryText, this.answerText);
                SecondaryInterSearchControl.speechTextDisplay.Show();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 处理麦克风数据并响应
        /// </summary>
        private void HandelTheSpeechData()
        {
            // 启动ffmpeg进程, 编码指定格式的语音文件-pcm-采样率16k
            using (Process ffmpeg = new Process())
            {
                ffmpeg.StartInfo.CreateNoWindow         = true;
                ffmpeg.StartInfo.UseShellExecute        = false;
                ffmpeg.StartInfo.RedirectStandardOutput = true;
                ffmpeg.StartInfo.FileName  = "tools\\ffmpeg-4.2.2-win32-shared\\bin\\ffmpeg.exe";
                ffmpeg.StartInfo.Arguments = " -y -i " + AppDomain.CurrentDomain.BaseDirectory +
                                             "parameters\\save\\ONCE-SPEECH.wav -acodec pcm_s16le -f s16le -ac 1 -ar 16000 " +
                                             AppDomain.CurrentDomain.BaseDirectory + "parameters\\save\\ONCE-SPEECH.pcm";
                ffmpeg.Start();
                Console.WriteLine("wait ffmpeg");
                ffmpeg.WaitForExit(10000);
            }
            byte[] onceSpeechData = File.ReadAllBytes("parameters\\save\\ONCE-SPEECH.pcm");

            // 第一次与自身服务器通信? 获取密钥
            if (!_isGotKey)
            {
                Console.WriteLine("first get");
                // string ownerUri = "http://www.coollsx.com/";
                // string toUri = ownerUri + "api-station/direct/.../bd";
                // string queryUri = toUri + "?code=???&type=spd";
                // HttpWebRequest httpWebRequest = WebRequest.CreateHttp(queryUri);
                // httpWebRequest.Method = "GET";
                // httpWebRequest.Timeout = 10000;
                // httpWebRequest.AllowAutoRedirect = true;
                // StreamReader streamReader = new StreamReader(httpWebRequest.GetResponse().GetResponseStream(), Encoding.UTF8);
                // JObject jObject = JObject.Parse(streamReader.ReadToEnd());
                // streamReader.Close();
                // streamReader.Dispose();
                // SpaceKeySpeechControl.APP_ID = (string)jObject.GetValue("APP_ID");
                // SpaceKeySpeechControl.API_KEY = (string)jObject.GetValue("API_KEY");
                // SpaceKeySpeechControl.SECRET_KEY = (string)jObject.GetValue("SECRET_KEY");

                // 百度语音识别sdk需要的密钥,你需要获取,参见根目录README.md
                SpaceKeySpeechControl.APP_ID     = "你的APP ID";
                SpaceKeySpeechControl.API_KEY    = "你的API KEY";
                SpaceKeySpeechControl.SECRET_KEY = "你的SECRET KEY";
                _isGotKey = true;
            }
            // 初始化语音识别实例
            asrClient = new Baidu.Aip.Speech.Asr(APP_ID, API_KEY, SECRET_KEY);
            var options = new Dictionary <string, object>
            {
                { "dev_pid", 1537 }   // 中英文代号-识别中英文
            };

            asrClient.Timeout = 10000;
            JObject result;

            try
            {
                // 获取识别结果
                result = asrClient.Recognize(onceSpeechData, "pcm", 16000, options);
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("糟糕!网络连接错误..", "XiaoYue", MessageBoxButtons.OK);
                return;
            }

            bool   isHaveResult = result.ContainsKey("result");
            string yourText, XYFinalAnwser;

            if (isHaveResult)
            {
                Console.WriteLine(result);
                string speechText = (string)result["result"].First;
                Console.WriteLine(speechText);
                // 要么无result, 通常百度不会返回空结果。此处应属多余
                if (String.IsNullOrWhiteSpace(speechText))
                {
                    yourText      = "...";
                    XYFinalAnwser = "空空如也?老娘啥都听不到!";
                }
                else if (speechText == "我不知道。")  // 有声音内容但过于微弱 百度返回类似
                {
                    yourText      = "...";
                    XYFinalAnwser = "嗯?你似乎啥也没说..";
                }
                else
                {
                    // 执行数据模型, 以匹配有效的答案, 并显示应答窗口
                    ResultThinkTheSpeechControl thinkTheSpeechControl = new ResultThinkTheSpeechControl(speechText);
                    if (thinkTheSpeechControl.SpeechAnwser != null)
                    {
                        // 如果数据模型分析出可能是个任务操作命令 todo
                        if (thinkTheSpeechControl.SpeechAnwser == merged.Come.TASK)
                        {
                            //this._isTaskCommand = true;
                            //new common.SynthesizerOnceSpeak("好的,..."); // 语音回应, “好的。”?
                            //using (Process openAppProcess = new Process())
                            //{

                            //}
                        }
                        yourText      = speechText;
                        XYFinalAnwser = thinkTheSpeechControl.SpeechAnwser;
                    }
                    else
                    {
                        yourText      = speechText;
                        XYFinalAnwser = "抱歉,我暂时不明白..";
                    }
                }
            }
            else
            {
                yourText      = "...";
                XYFinalAnwser = "太快了吧,老娘啥都听不到!";
            }
            Action parentThreadAction = () =>
            {
                // 若是任务操作命令, 不必显示相应窗口
                if (!_isTaskCommand)
                {
                    // textDisplay必须重新生成, 因为当前的它在space按下时已经被关闭
                    SpaceKeySpeechControl.textDisplay = new page.other.SpeechTextDisplay(yourText, XYFinalAnwser);
                    SpaceKeySpeechControl.textDisplay.Show();       // 其在SpaceKeySpeechControl上生成, 非当前backDataAnalyze线程
                    new common.SynthesizerOnceSpeak(XYFinalAnwser); // 语言合成说出答案
                }
                progressRunning.Close();
                skin.Focus();
            };

            // 在其主线程上进行操作
            skin.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, parentThreadAction);
            return;
        }