Ejemplo n.º 1
0
    public void DetectWords(float[] wave)
    {
        float minScore = 0f;

        int closestIndex = 0;
        WordDetails closestWord = null;

        int size = wave.Length;
        int halfSize = size / 2;
        for (int wordIndex = FIRST_WORD_INDEX; wordIndex < word.Words.Count; ++wordIndex)
        {
            WordDetails details = word.Words[wordIndex];

            if (null == details)
            {
                continue;
            }

            if (word.WordsToIgnore.Contains(details.Label))
            {
                continue;
            }

            float[] spectrum = details.SpectrumReal;
            if (null == spectrum)
            {
                //Debug.LogError(string.Format("Word profile not set: {0}", details.Label));
                details.Score = -1;
                continue;
            }
            if (null == m_spectrumReal)
            {
                details.Score = -1;
                continue;
            }
            if (spectrum.Length != halfSize ||
                m_spectrumReal.Length != halfSize)
            {
                details.Score = -1;
                continue;
            }

            float score = word.Score(m_spectrumReal, spectrum);
            details.Score = score;

            #if USE_WORD_MIN_SCORE
            details.AddMinScore(score);
            score = details.GetMinScore(DateTime.Now - TimeSpan.FromSeconds(1));
            #endif

            if (wordIndex == FIRST_WORD_INDEX)
            {
                closestIndex = wordIndex;
                minScore = score;
                closestWord = details;
            }
            else if (score < minScore)
            {
                closestIndex = wordIndex;
                minScore = score;
                closestWord = details;
            }
        }

        //GameObject.FindGameObjectWithTag("dog").GetComponent<DogController>().DebugShow(string.Format("1111 minScore:{0}, closestIndex:{1}", minScore, closestIndex));
        if (ClosestIndex != closestIndex && minScore < word.DetectScoreThreshhold)
        {
            ClosestIndex = closestIndex;
            if (null != WordDetectEvent)
            {
                Word.WordEventArgs args = new Word.WordEventArgs();
                args.Details = closestWord;
                //Debug.Log(args.Details.Label);
                WordDetectEvent.Invoke(this, args);
                ClearMicData();

                GameObject.FindGameObjectWithTag("dog").GetComponent<DogController>().DebugShow(string.Format("2222 minScore:{0}, closestIndex:{1}", minScore, closestIndex));
            }
        }
    }
Ejemplo n.º 2
0
    void Update()
    {
        switch(state)
        {
        case State.Word:
        {
            if(Input.GetMouseButtonUp(0) || m_timerStart <= DateTime.Now)
            {
                GatherWave(false);

                //state = State.WordToNoise;
                //m_timerStart = DateTime.Now + TimeSpan.FromSeconds(word.RecordWordToNoiseInterval);
                state = State.Result;
                m_timerStart = DateTime.MinValue;
            }
        }
            break;

        case State.WordToNoise:
        {
            if(m_timerStart <= DateTime.Now)
            {
                state = State.Noise;
                m_startPosition = word.Mic.GetPosition();
                m_timerStart = DateTime.Now + TimeSpan.FromSeconds(word.Mic.CaptureTime);
            }
        }
            break;

        case State.Noise:
        {
            if(m_timerStart <= DateTime.Now)
            {
                GatherWave(true);

                state = State.Result;
                m_timerStart = DateTime.MinValue;
            }
        }
            break;

        case State.Result:
        {
            state = State.None;

            SetupNoiseProfile();
            WordDetails noiseDetails = word.GetWord(Word.WORD_NOISE);
            Word.WordEventArgs args = new Word.WordEventArgs();
            args.Details = word.GetWord(m_RecordingProfile);
            args.result = false;

            // success
            //if(word.Score(m_RecordingDetails.SpectrumReal, m_RecordingNoiseDetails.SpectrumReal) >= word.RecordScoreThreshhold)
            float score = word.Score(m_RecordingDetails.SpectrumReal, noiseDetails.SpectrumReal);
            GameObject.FindGameObjectWithTag("dog").GetComponent<DogController>().DebugShow(string.Format("{0}", score));
            if( score >= word.RecordScoreThreshhold)
            {
                args.result = true;
                SetupWordProfile(false);
                //SetupWordProfile(true);
                word.ProfileSave();
            }

            //Debug.Log(args.Details.Label);
            WordRecordEvent.Invoke(this, args);
        }
            break;
        }
    }