Пример #1
0
        public void SendUpoad(string url, string path)
        {
            if (url == null)
            {
                return;
            }
            if (path == null)
            {
                SendRequest(url); return;
            }

            if (working)
            {
                return;
            }
            working = true;

            url = CleanURL(url);
            WSRConfig.GetInstance().logInfo("HTTP", "Build UploadRequest: " + url);

            WebClient client = new WebClient();

            client.Headers.Add("user-agent", "S.A.R.A.H. (Self Actuated Residential Automated Habitat)");

            try {
                byte[] responseArray = client.UploadFile(url, path);
                String response      = System.Text.Encoding.ASCII.GetString(responseArray);
                WSRSpeakerManager.GetInstance().Speak(response, false);
            }
            catch (Exception ex) {
                WSRConfig.GetInstance().logInfo("HTTP", "Exception: " + ex.Message);
            }
            working = false;
        }
Пример #2
0
 public static WSRSpeakerManager GetInstance()
 {
     if (manager == null)
     {
         manager = new WSRSpeakerManager();
     }
     return(manager);
 }
Пример #3
0
        protected void HandlePlay(XPathNavigator xnav)
        {
            XPathNavigator play = xnav.SelectSingleNode("/SML/action/@play");

            if (play != null)
            {
                WSRSpeakerManager.GetInstance().Play(play.Value, false);
            }
        }
Пример #4
0
        protected String HandleHeight(XPathNavigator xnav, String path)
        {
            XPathNavigator height = xnav.SelectSingleNode("/SML/action/@height");

            if (height != null)
            {
                double h = WSRProfileManager.GetInstance().Heigth;
                WSRSpeakerManager.GetInstance().Speak(h + " mètres", false);
            }
            return(path);
        }
Пример #5
0
        // ==========================================
        //  RECOGNIZE
        // ==========================================

        protected void SpeechRecognized(RecognitionResult rr)
        {
            // 1. Prevent while speaking
            if (WSRSpeakerManager.GetInstance().Speaking)
            {
                cfg.logWarning("ENGINE - " + Name, "REJECTED Speech while speaking : " + rr.Confidence + " Text: " + rr.Text);
                return;
            }

            // 2. Prevent while working
            XPathNavigator xnav = HandleSpeech(rr);

            if (xnav == null)
            {
                return;
            }

            // 3. Reset context timeout
            if (rr.Grammar.Name != "Dyn")
            {
                WSRSpeechManager.GetInstance().ResetContextTimeout();
            }
            else
            {
                cfg.logInfo("ENGINE - " + Name, "DYN reset to default context");
                WSRSpeechManager.GetInstance().SetContext("default");
                WSRSpeechManager.GetInstance().ForwardContext();
            }

            // 4. Track Audio Pitch
            TrackPitch(rr);

            // 5. Reset Speech Buffer
            WSRSpeakerManager.GetInstance().SpeechBuffer = "";

            // 6. Hook
            String path = cfg.WSR.HandleCustomAttributes(xnav);

            // 7. Parse Result's URL
            String url = GetURL(xnav, rr.Confidence);

            // 8. Parse Result's Dication
            url = HandleWildcard(rr, url);
            if (url == null)
            {
                return;
            }

            // 9. Send the request
            HandleRequest(url, path);
        }
Пример #6
0
        // Perform cleanup on application exit
        public virtual void Dispose()
        {
            // Stop RTP Client
            DisposeRTPClient();

            // Stop HttpServer
            WSRHttpManager.GetInstance().Dispose();

            // Stop Speech Manager
            WSRSpeechManager.GetInstance().Dispose();

            // Stop Speaker
            WSRSpeakerManager.GetInstance().Dispose();
        }
Пример #7
0
        protected void HandleTTS(XPathNavigator xnav)
        {
            XPathNavigator tts = xnav.SelectSingleNode("/SML/action/@tts");

            if (tts != null)
            {
                WSRSpeakerManager.GetInstance().Speak(tts.Value, false);
            }

            XPathNavigator notts = xnav.SelectSingleNode("/SML/action/@notts");

            if (notts != null)
            {
                WSRSpeakerManager.GetInstance().ShutUp();
            }
        }
Пример #8
0
        public void SendRequest(string url)
        {
            if (url == null)
            {
                return;
            }
            if (working)
            {
                return;
            }

            working = true;

            // Clean URL
            url = CleanURL(url);

            // Append ClientId
            url = AppendURL(url, "client", WSRConfig.GetInstance().Id);

            // Append UserId
            var profile = WSRProfileManager.GetInstance().Current;

            if (null != profile)
            {
                url = AppendURL(url, "profile", profile.Name);
            }

            // Append Directory Path
            url = AppendURL(url, "directory", WSRSpeechManager.GetInstance().GetGrammarPath());

            WSRConfig.GetInstance().logInfo("HTTP", "Build HttpRequest: " + url);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            req.Method = "GET";

            WSRConfig.GetInstance().logInfo("HTTP", "Send HttpRequest: " + req.Address);
            try {
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8)) {
                    WSRSpeakerManager.GetInstance().Speak(sr.ReadToEnd(), false);
                }
            }
            catch (WebException ex) {
                WSRConfig.GetInstance().logError("HTTP", ex);
            }
            working = false;
        }
Пример #9
0
        // ==========================================
        //  CONSTRUCTOR
        // ==========================================

        public virtual void Init()
        {
            // Start RTP Client
            StartRTPClient();

            // Start HttpServer
            WSRHttpManager.GetInstance().StartHttpServer();

            // Start Speech Manager
            WSRSpeechManager.GetInstance().Init();
            InitSpeechEngine();

            // Start Speaker Manager
            WSRSpeakerManager.GetInstance();

            // Start Timeout
            RestartTimeout();
        }
Пример #10
0
        protected void recognizer_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
        {
            var url = WSRSpeechManager.GetInstance().ProcessRejected();

            if (url == null)
            {
                return;
            }

            // 1. Prevent while speaking
            if (WSRSpeakerManager.GetInstance().Speaking)
            {
                cfg.logWarning("ENGINE - " + Name, "REJECTED Speech while speaking (in SpeechRecognitionRejected)");
                return;
            }

            // 2. Process Audio
            cfg.logInfo("ENGINE - " + Name, "DYN process speech2text: ...");
            String speech2text = ProcessAudioStream(e.Result, WSRConfig.GetInstance().language);


            if (String.IsNullOrEmpty(speech2text))
            {
                cfg.logInfo("ENGINE - " + Name, "DYN wrong speech2text: " + speech2text);
                return;
            }

            // 3. Reset Dynamic
            cfg.logInfo("ENGINE - " + Name, "DYN reset to default context (in SpeechRecognitionRejected)");
            WSRSpeechManager.GetInstance().SetContext("default");
            WSRSpeechManager.GetInstance().ForwardContext();

            // 4. Send back request
            cfg.logInfo("ENGINE - " + Name, "DYN callback: " + speech2text);
            url += "?dictation=" + HttpUtility.UrlEncode(speech2text);
            HandleRequest(url, null);
        }
Пример #11
0
 public static WSRSpeakerManager GetInstance() {
   if (manager == null) {
     manager = new WSRSpeakerManager();
   }
   return manager;
 }
Пример #12
0
        // ==========================================
        //  HANDLE HTTPSERVER
        // ==========================================

        public virtual bool HandleCustomRequest(NHttp.HttpRequestEventArgs e, StreamWriter writer)
        {
            // Status
            String status = e.Request.Params.Get("status");

            if (status != null)
            {
                if (WSRSpeakerManager.GetInstance().Speaking)
                {
                    writer.Write("speaking");
                }
            }

            // Askme
            var values = System.Web.HttpUtility.ParseQueryString(e.Request.Url.Query);
            var mgr    = WSRSpeechManager.GetInstance();

            String[] grammar = values.GetValues("grammar");
            String[] tags    = values.GetValues("tags");
            WSRSpeechManager.GetInstance().DynamicGrammar(grammar, tags);

            // Stop Music
            String pause = e.Request.Params.Get("pause");

            if (pause != null)
            {
                WSRSpeakerManager.GetInstance().Stop(pause);
            }

            // Play Music
            String mp3 = e.Request.Params.Get("play");

            if (mp3 != null)
            {
                WSRSpeakerManager.GetInstance().Play(mp3, e.Request.Params.Get("sync") == null);
            }

            // Recognize
            String audio = e.Request.Params.Get("recognize");

            if (audio != null)
            {
                WSRSpeechManager.GetInstance().RecognizeFile(audio);
            }

            // Listening
            String listen = e.Request.Params.Get("listen");

            if (listen != null)
            {
                WSRSpeechManager.GetInstance().Listening = bool.Parse(listen);
            }

            // Recognize File
            NHttp.HttpPostedFile file = e.Request.Files.Get("recognize");
            if (file != null)
            {
                byte[] data = null;
                using (var reader = new BinaryReader(file.InputStream)) {
                    data = reader.ReadBytes(file.ContentLength);
                }
                var path = WSRConfig.GetInstance().audio + "/" + file.FileName;
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                File.WriteAllBytes(path, data);
            }

            // Text To Speech
            String tts = e.Request.Params.Get("tts");

            if (tts != null)
            {
                tts = e.Server.HtmlDecode(tts);
                WSRSpeakerManager.GetInstance().Speak(tts, e.Request.Params.Get("sync") == null);
            }

            // Text To Speech - Stop
            String notts = e.Request.Params.Get("notts");

            if (notts != null)
            {
                WSRSpeakerManager.GetInstance().ShutUp();
            }

            // Text To Speech - Stop
            String restart = e.Request.Params.Get("restart");

            if (restart != null)
            {
                Restart();
            }

            // Set Context
            String ctxt = e.Request.Params.Get("context");

            if (ctxt != null)
            {
                WSRSpeechManager.GetInstance().SetContext(new List <string>(ctxt.Split(',')));
                WSRSpeechManager.GetInstance().SetContextTimeout();
                WSRSpeechManager.GetInstance().ForwardContext();
            }

            // Process
            String activate = e.Request.Params.Get("activate");

            if (activate != null)
            {
                WSRKeyboard.GetInstance().ActivateApp(activate);
            }

            String run   = e.Request.Params.Get("run");
            String param = e.Request.Params.Get("runp");

            if (run != null)
            {
                WSRKeyboard.GetInstance().RunApp(run, param);
            }

            // Keyboard
            String keyMod = e.Request.Params.Get("keyMod");
            String key    = e.Request.Params.Get("keyPress");

            if (key != null)
            {
                WSRKeyboard.GetInstance().SimulateKey(key, 0, keyMod);
            }

            key = e.Request.Params.Get("keyDown");
            if (key != null)
            {
                WSRKeyboard.GetInstance().SimulateKey(key, 1, keyMod);
            }

            key = e.Request.Params.Get("keyUp");
            if (key != null)
            {
                WSRKeyboard.GetInstance().SimulateKey(key, 2, keyMod);
            }

            String keyText = e.Request.Params.Get("keyText");

            if (keyText != null)
            {
                WSRKeyboard.GetInstance().SimulateTextEntry(keyText);
            }

            // Recognize String
            String emulate = e.Request.Params.Get("emulate");

            if (emulate != null)
            {
                WSRSpeechManager.GetInstance().RecognizeString(emulate);
                writer.Write(WSRSpeakerManager.GetInstance().SpeechBuffer);
            }
            return(false);
        }
Пример #13
0
        // ==========================================
        //  HANDLE HTTPSERVER
        // ==========================================

        public override bool HandleCustomRequest(NHttp.HttpRequestEventArgs e, StreamWriter writer)
        {
            if (base.HandleCustomRequest(e, writer))
            {
                return(true);
            }

            // Parse Result's Photo
            if (e.Request.Params.Get("picture") != null)
            {
                String path = ActiveSensor().TakePicture("medias/");
                e.Response.ContentType = "image/jpeg";
                Bitmap bmp = (Bitmap)Bitmap.FromFile(path);
                bmp.Save(e.Response.OutputStream, ImageFormat.Jpeg);

                return(true);
            }

            // Return Last Height Active Sensor
            var height = e.Request.Params.Get("height");

            if (height != null)
            {
                double h = WSRProfileManager.GetInstance().Heigth;
                if (height == "tts")
                {
                    WSRSpeakerManager.GetInstance().Speak(h + " mètres", false);
                }
                else
                {
                    writer.Write("" + h);
                }
                return(true);
            }

            // Face recognition
            String facereco = e.Request.Params.Get("face");

            if (facereco != null)
            {
                facereco = e.Server.HtmlDecode(facereco);
                if ("start".Equals(facereco))
                {
                    WSRConfig.GetInstance().StandByFace = false;
                }
                else if ("stop".Equals(facereco))
                {
                    WSRConfig.GetInstance().StandByFace = true;
                }
            }

            // Gesture recognition
            String gesture = e.Request.Params.Get("gesture");

            if (gesture != null)
            {
                gesture = e.Server.HtmlDecode(gesture);
                if ("start".Equals(gesture))
                {
                    WSRConfig.GetInstance().StandByGesture = false;
                }
                else if ("stop".Equals(gesture))
                {
                    WSRConfig.GetInstance().StandByGesture = true;
                }
            }
            return(false);
        }