Example #1
0
        public static void Start(User u, SpeechHelper.SpeechStateCallbackDelegate del, Delegate networkDel)
        {
            // trace the speech request
            TraceHelper.AddMessage("Starting Speech");

            // Start is not reentrant - make sure the caller didn't violate the contract
            if (speechOperationInProgress == true)
                return;

            // store the delegates passed in
            speechStateDelegate = del;
            networkDelegate = networkDel;

            // set the flag
            speechOperationInProgress = true;

            // initialize the connection
            if (SpeechkitInitialize() == false)
            {
                Cancel(networkDel);
                return;
            }

            // start a new thread that starts the dictation
            DictationStart(RecognizerRecognizerType.Dictation);
        }
Example #2
0
        public static void Start(User u, SpeechHelper.SpeechStateCallbackDelegate del, Delegate networkDel)
        {
            // StartStreamed is not reentrant - make sure the caller didn't violate the contract
            if (speechOperationInProgress == true)
                return;

            // set the flag
            speechOperationInProgress = true;

            // store the delegates passed in
            speechStateDelegate = del;
            networkDelegate = networkDel;
            user = u;

            // initialize the microphone information and speech buffer
            mic.BufferDuration = TimeSpan.FromSeconds(1);
            int length = mic.GetSampleSizeInBytes(mic.BufferDuration);
            speechBuffer = new byte[length];
            speechBufferList.Clear();
            bufferMutexList.Clear();
            numBytes = 0;

            // trace the speech request
            TraceHelper.AddMessage("Starting Speech");

            // initialize frame index
            frameCounter = 0;

            // callback when the mic gathered 1 sec worth of data
            if (initializedBufferReadyEvent == false)
            {
                mic.BufferReady += new EventHandler<EventArgs>(MicBufferReady);
                initializedBufferReadyEvent = true;
            }

            // connect to the web service, and once that completes successfully,
            // it will invoke the NetworkInterfaceCallback delegate to indicate the network quality
            // this delegate will then turn around and send the appropriate encoding in the SendPost call
            NetworkHelper.BeginSpeech(
                new NetworkInformationCallbackDelegate(NetworkInformationCallback),
                new NetworkDelegate(NetworkCallback));
        }
Example #3
0
 /// <summary>
 /// Set the UI based on the current state of the speech state machine
 /// </summary>
 /// <param name="state"></param>
 private void SpeechSetUIState(SpeechHelper.SpeechState state)
 {
     switch (state)
     {
         case SpeechHelper.SpeechState.Initializing:
             SpeechLabelText = "initializing...";
             SpeechButtonText = "done";
             SpeechButtonEnabled = false;
             SpeechCancelButtonText = "cancel";
             break;
         case SpeechHelper.SpeechState.Listening:
             SpeechLabelText = "listening...";
             SpeechButtonText = "done";
             SpeechButtonEnabled = true;
             SpeechCancelButtonText = "cancel";
             break;
         case SpeechHelper.SpeechState.Recognizing:
             SpeechLabelText = "recognizing...";
             SpeechButtonText = "try again";
             SpeechButtonEnabled = false;
             SpeechCancelButtonText = "cancel";
             break;
         case SpeechHelper.SpeechState.Finished:
             SpeechLabelText = "";
             SpeechButtonText = "try again";
             SpeechButtonEnabled = true;
             SpeechCancelButtonText = "ok";
             break;
     }
 }
Example #4
0
        private void SpeechPopup_SpeechStateCallback(SpeechHelper.SpeechState state, string message)
        {
            speechState = state;
            SpeechSetUIState(speechState);

            // store debug / timing info
            TimeSpan ts = DateTime.Now - speechStart;
            string stateString = SpeechHelper.SpeechStateString(state);
            string traceString = String.Format("New state: {0}; Time: {1}; Message: {2}", stateString, ts.TotalSeconds, message);
            TraceHelper.AddMessage(traceString);
            speechDebugString += traceString + "\n";
        }
Example #5
0
        public static void Stop(SpeechHelper.SpeechToTextCallbackDelegate del)
        {
            // get the last chunk of speech
            int len = mic.GetData(speechBuffer);

            // stop listening
            mic.Stop();

            // remove the mic eventhandler
            mic.BufferReady -= MicBufferReady;
            initializedBufferReadyEvent = false;

            // trace the operation
            TraceHelper.AddMessage(String.Format("Final Frame: {0} bytes of speech", len));

            // create a properly sized copy of the last buffer
            byte[] speechChunk = new byte[len];
            Array.Copy(speechBuffer, speechChunk, len);

            // add the last speech buffer to the folder
            speechBufferList.Add(speechChunk);

            // if the encode flag is set, encode the chunk before sending it
            if (encode)
            {
                // do this on a background thread because it is CPU-intensive
                ThreadPool.QueueUserWorkItem(delegate
                {
                    // create a new mutex object for this frame
                    AutoResetEvent bufferMutex = new AutoResetEvent(false);
                    bufferMutexList.Add(bufferMutex);

                    // encode the frame
                    TraceHelper.AddMessage(String.Format("Final Frame: About to encode speech"));
                    byte[] encodedBuf = EncodeSpeech(speechChunk, speechChunk.Length);
                    TraceHelper.AddMessage(String.Format("Final Frame: Encoded down to {0} bytes", encodedBuf.Length));

                    // wait until the previous frame has been sent
                    int frameIndex = bufferMutexList.Count - 1;
                    if (frameIndex > 0)
                        bufferMutexList[frameIndex - 1].WaitOne();

                    // send the last frame and retrieve the response
                    TraceHelper.AddMessage(String.Format("Sending Final Frame: {0} bytes", encodedBuf.Length));
                    NetworkHelper.EndSpeech(encodedBuf, encodedBuf.Length, del, new NetworkDelegate(NetworkCallback));

                    // repeat the sentence back to the user
                    PlaybackSpeech();
                });
            }
            else
            {
                // send the operation immediately
                TraceHelper.AddMessage(String.Format("Sending Final Frame: {0} bytes", speechChunk.Length));
                NetworkHelper.EndSpeech(speechChunk, speechChunk.Length, del, new NetworkDelegate(NetworkCallback));

                // play back the speech immediately
                PlaybackSpeech();
            }
        }
Example #6
0
        public static void Stop(SpeechHelper.SpeechToTextCallbackDelegate del)
        {
            // trace the operation
            TraceHelper.AddMessage("Stopping Speech");

            // save the delegate passed in
            speechToTextDelegate = del;

            // stop recording and start recognizing
            _recognizer.stopRecording();

            // update the speech state
            speechStateDelegate.DynamicInvoke(SpeechHelper.SpeechState.Recognizing, "Stopped recording");
        }