Ejemplo n.º 1
0
    void SaveProfile(WordDetails details)
    {
        string path = string.Format("Assets/{0}_{1}.profile", GetType().Name, details.Label);

        if (!string.IsNullOrEmpty(path))
        {
            try
            {
                using (
                    FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write,
                                              FileShare.ReadWrite)
                    )
                {
                    using (BinaryWriter bw = new BinaryWriter(fs))
                    {
                        AudioWordDetection.SaveWord(bw, details);
                        //Debug.Log(string.Format("Save profile: {0}", details.Label));
                    }
                }
            }
            catch (Exception)
            {
                Debug.LogError(string.Format("Failed to save profile: {0}", details.Label));
            }
        }
    }
Ejemplo n.º 2
0
    /// <summary>
    /// GUI event
    /// </summary>
    protected override void OnGUI()
    {
        if (null == AudioWordDetection ||
            null == Mic ||
            string.IsNullOrEmpty(Mic.DeviceName))
        {
            return;
        }

        GUILayout.Space(40);

        GUILayout.Label(string.Format("Active Command: {0}", m_command));

        Color backgroundColor = GUI.backgroundColor;

        for (int wordIndex = 0; wordIndex < AudioWordDetection.Words.Count; ++wordIndex)
        {
            WordDetails details = AudioWordDetection.Words[wordIndex];

            if (null == details)
            {
                continue;
            }

            if (AudioWordDetection.ClosestIndex == wordIndex)
            {
                GUI.backgroundColor = Color.red;
            }
            else
            {
                GUI.backgroundColor = backgroundColor;
            }

            if (details != GetWord(WORD_NOISE))
            {
                GUI.enabled = (null != GetWord(WORD_NOISE) && null != GetWord(WORD_NOISE).SpectrumReal);
            }

            bool showRow = true;
            if (details == GetWord(WORD_NOISE))
            {
                GUILayout.Label("First: Record a noise sample");
            }
            else if (details == GetWord(WORD_PUSH_TO_TALK))
            {
                showRow = false;
            }

            if (showRow)
            {
                GUILayout.BeginHorizontal(GUILayout.Width(300));

                bool oldEnabled = GUI.enabled;
                GUI.enabled = null != details.Wave;
                if (GUILayout.Button("Play", GUILayout.Height(45)))
                {
                    if (null != details.Audio)
                    {
                        if (NormalizeWave)
                        {
                            GetComponent <AudioSource>().PlayOneShot(details.Audio, 0.1f);
                        }
                        else
                        {
                            GetComponent <AudioSource>().PlayOneShot(details.Audio);
                        }
                    }

                    // show profile
                    RefExample.OverrideSpectrumImag = true;
                    RefExample.SpectrumImag         = details.SpectrumReal;
                }
                GUI.enabled = oldEnabled;

                if (wordIndex == 0)
                {
                    GUILayout.Label(details.Label, GUILayout.Width(150), GUILayout.Height(45));
                }
                else
                {
                    details.Label = GUILayout.TextField(details.Label, GUILayout.Width(150), GUILayout.Height(45));
                }

                GUILayout.Button(string.Format("{0}",
                                               (null == details.SpectrumReal) ? "Rec" : "Re-Rec"), GUILayout.Height(45));
            }

            bool  rectAvailable = false;
            Rect  rect          = new Rect();
            Event e             = Event.current;
            if (details == GetWord(WORD_PUSH_TO_TALK))
            {
                if (AudioWordDetection.UsePushToTalk &&
                    null != e)
                {
                    rect          = new Rect(400, 250, 200, 200);
                    rectAvailable = true;
                    Color oldColor = GUI.backgroundColor;
                    GUI.backgroundColor = Color.green;
                    if (GUI.Button(rect, "Push To Talk\nHold Speak 1 Command"))
                    {
                    }
                    GUI.backgroundColor = oldColor;
                }
            }
            else
            {
                if (null != e)
                {
                    rect          = GUILayoutUtility.GetLastRect();
                    rectAvailable = true;
                }
            }

            if (rectAvailable)
            {
                bool overButton = rect.Contains(e.mousePosition);

                if (m_buttonIndex == -1 &&
                    m_timerStart == DateTime.MinValue &&
                    Input.GetMouseButton(0) &&
                    overButton)
                {
                    //Debug.Log("Initial button down");
                    m_buttonIndex   = wordIndex;
                    m_startPosition = Mic.GetPosition();
                    m_timerStart    = DateTime.Now + TimeSpan.FromSeconds(Mic.CaptureTime);
                }
                if (m_buttonIndex == wordIndex)
                {
                    bool buttonUp = Input.GetMouseButtonUp(0);
                    if (m_timerStart > DateTime.Now &&
                        !buttonUp)
                    {
                        //Debug.Log("Button still pressed");
                    }
                    else if (m_timerStart != DateTime.MinValue &&
                             m_timerStart < DateTime.Now)
                    {
                        //Debug.Log("Button timed out");
                        SetupWordProfile(false);
                        m_timerStart  = DateTime.MinValue;
                        m_buttonIndex = -1;
                    }
                    else if (m_timerStart != DateTime.MinValue &&
                             buttonUp &&
                             m_buttonIndex != -1)
                    {
                        //Debug.Log("Button is no longer pressed");
                        SetupWordProfile(true);
                        m_timerStart  = DateTime.MinValue;
                        m_buttonIndex = -1;
                    }
                }
            }

            if (showRow)
            {
#if UNITY_EDITOR
                GUI.enabled = null != details.Wave;
                if (GUILayout.Button("Save"))
                {
                    string defaultName = string.Format("{0}.profile", details.Label);
                    string path        = EditorUtility.SaveFilePanel("Save profile", "Assets/", defaultName, "profile");
                    if (!string.IsNullOrEmpty(path))
                    {
                        try
                        {
                            using (
                                FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write,
                                                          FileShare.ReadWrite)
                                )
                            {
                                using (BinaryWriter bw = new BinaryWriter(fs))
                                {
                                    AudioWordDetection.SaveWord(bw, details);
                                    Debug.Log("Saved word");
                                }
                            }
                        }
                        catch (Exception)
                        {
                            Debug.LogError("Failed to save word");
                        }
                    }
                }
                GUI.enabled = true;
                if (GUILayout.Button("Load"))
                {
                    string path = EditorUtility.OpenFilePanel("Load profile", "Assets/", "profile");
                    if (!string.IsNullOrEmpty(path))
                    {
                        try
                        {
                            using (
                                FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                                )
                            {
                                using (BinaryReader br = new BinaryReader(fs))
                                {
                                    string oldLabel = details.Label;
                                    AudioWordDetection.LoadWord(br, details);
                                    Debug.Log("Loaded word");
                                    details.Label = oldLabel;
                                    SetupWordProfile(details, false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Debug.LogError(string.Format("Failed to load word: {0}", ex));
                        }
                    }
                }
#endif

                GUILayout.EndHorizontal();

                if (details != GetWord(WORD_NOISE))
                {
                    GUI.enabled = true;
                }

                GUILayout.Space(10);
            }

            if (details == GetWord(WORD_NOISE))
            {
                GUILayout.Label("Next: Record the voice command");
            }
        }

        GUI.backgroundColor = backgroundColor;

        GUI.enabled = true;
    }