コード例 #1
0
ファイル: NativeSpeechHelper.cs プロジェクト: ogazitt/zaplify
        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();
            }
        }
コード例 #2
0
ファイル: NativeSpeechHelper.cs プロジェクト: ogazitt/zaplify
        private static void MicBufferReady(object sender, EventArgs e)
        {
            // get the data from the mic
            int len = mic.GetData(speechBuffer);

            numBytes += len;

            // make a copy of the frame index and then increment it
            int frameIndex = frameCounter++;

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

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

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

            // send the chunk to the service
            try
            {
                // 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("Frame {0}: About to encode speech", frameIndex));
                        byte[] encodedBuf = EncodeSpeech(speechChunk, speechChunk.Length);
                        TraceHelper.AddMessage(String.Format("Frame {0}: Encoded down to {1} bytes", frameIndex, encodedBuf.Length));

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

                        // send the frame
                        TraceHelper.AddMessage(String.Format("Sending Frame {0}: {1} bytes", frameIndex, encodedBuf.Length));
                        NetworkHelper.SendSpeech(encodedBuf, encodedBuf.Length, null, new NetworkDelegate(NetworkCallback));

                        // set the current frame's mutex
                        bufferMutex.Set();
                    });
                }
                else
                {
                    // just send the frame
                    TraceHelper.AddMessage(String.Format("Sending Frame {0}: {1} bytes", frameIndex, speechChunk.Length));
                    NetworkHelper.SendSpeech(speechChunk, speechChunk.Length, null, new NetworkDelegate(NetworkCallback));
                }
            }
            catch (Exception ex)
            {
                // stop listening
                mic.Stop();

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

                // trace the exception
                TraceHelper.AddMessage(String.Format("Mic buffer ready: ex: {0}", ex.Message));

                speechOperationInProgress = false;
                return;
            }

#if DEBUG
            // signal the caller that the chunk has been sent
            speechStateDelegate.DynamicInvoke(SpeechHelper.SpeechState.Listening, numBytes.ToString());
#endif
        }