Beispiel #1
0
        /// <summary>
        /// This function POSTs the given audio clip the recognize function and convert speech into text. This function should be used
        /// only on AudioClips under 4MB once they have been converted into WAV format. Use the StartListening() for continuous
        /// recognition of text.
        /// </summary>
        /// <param name="clip">The AudioClip object.</param>
        /// <param name="callback">A callback to invoke with the results.</param>
        /// <returns></returns>
        public bool Recognize(AudioClip clip, OnRecognize callback)
        {
            if (clip == null)
            {
                throw new ArgumentNullException("clip");
            }
            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, "/v1/recognize");

            if (connector == null)
            {
                return(false);
            }

            RecognizeRequest req = new RecognizeRequest();

            req.Clip     = clip;
            req.Callback = callback;

            req.Headers["Content-Type"] = "audio/wav";
            req.Send = WaveFile.CreateWAV(clip);
            if (req.Send.Length > MAX_RECOGNIZE_CLIP_SIZE)
            {
                Log.Error("SpeechToText", "AudioClip is too large for Recognize().");
                return(false);
            }
            req.Parameters["model"]            = m_RecognizeModel;
            req.Parameters["continuous"]       = "false";
            req.Parameters["max_alternatives"] = m_MaxAlternatives.ToString();
            req.Parameters["timestamps"]       = m_Timestamps ? "true" : "false";
            req.Parameters["word_confidence"]  = m_WordConfidence ? "true" : "false";
            req.OnResponse = OnRecognizeResponse;

            return(connector.Send(req));
        }