コード例 #1
0
    /// <summary>
    /// Initialize the example
    /// </summary>
    protected override void Start()
    {
        if (null == AudioWordDetection ||
            null == Mic)
        {
            Debug.LogError("Missing meta references");
            return;
        }

        Dictionary <Commands, WordDetails> words = new Dictionary <Commands, WordDetails>();

        // prepopulate words
        foreach (string val in Enum.GetNames(typeof(Commands)))
        {
            //Debug.Log(val);
            Commands    command = (Commands)Enum.Parse(typeof(Commands), val);
            WordDetails details = new WordDetails()
            {
                Label = val
            };
            words[command] = details;
            try
            {
                string path = string.Format("Assets/{0}_{1}.profile", GetType().Name, val);
                if (File.Exists(path))
                {
                    using (
                        FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                        )
                    {
                        using (BinaryReader br = new BinaryReader(fs))
                        {
                            AudioWordDetection.LoadWord(br, details);
                            //Debug.Log(string.Format("Loaded profile: {0}", path));
                            details.Label = val;
                        }
                    }
                }
                else
                {
                    Debug.Log(string.Format("Profile not available for: {0}", path));
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(string.Format("Failed to load word: {0}", ex));
            }
        }

        // set 1
        m_set1.Add(words[Commands.Noise]);
        m_set1.Add(words[Commands.Go]);

        // set 2
        m_set2.Add(words[Commands.Noise]);
        m_set2.Add(words[Commands.Back]);
        m_set2.Add(words[Commands.Action]);
        m_set2.Add(words[Commands.Misc]);

        // set 3
        m_set3.Add(words[Commands.Noise]);
        m_set3.Add(words[Commands.Back]);
        m_set3.Add(words[Commands.Charge]);
        m_set3.Add(words[Commands.Elbow]);
        m_set3.Add(words[Commands.Head]);
        m_set3.Add(words[Commands.Punch]);

        // set 4
        m_set4.Add(words[Commands.Noise]);
        m_set4.Add(words[Commands.Back]);
        m_set4.Add(words[Commands.Dance]);
        m_set4.Add(words[Commands.Fall]);
        m_set4.Add(words[Commands.Guitar]);
        m_set4.Add(words[Commands.Run]);

        AudioWordDetection.Words = m_set1;
        m_mode = Modes.Set1;

        //subscribe detection event
        AudioWordDetection.WordDetectedEvent += WordDetectedHandler;
    }
コード例 #2
0
    /// <summary>
    /// Initialize the example
    /// </summary>
    protected override void Start()
    {
        if (null == AudioWordDetection ||
            null == Mic)
        {
            Debug.LogError("Missing meta references");
            return;
        }

        Dictionary <String, WordDetails> words = new Dictionary <String, WordDetails>();

        WordDetails noise = new WordDetails()
        {
            Label = "Noise"
        };

        AudioWordDetection.Words.Add(noise);

        string[] navigation =
        {
            WordDetectionInput.KEY_JUMP,
            WordDetectionInput.KEY_FORWARD,
            WordDetectionInput.KEY_BACKWARD,
            WordDetectionInput.KEY_LEFT,
            WordDetectionInput.KEY_RIGHT,
        };

        // prepopulate words
        foreach (string command in navigation)
        {
            //Debug.Log(command);
            if (command == "idle")
            {
                continue;
            }
            WordDetails details = new WordDetails()
            {
                Label = command
            };
            AudioWordDetection.Words.Add(details);
            words[command] = details;
#if !UNITY_WEBPLAYER
            try
            {
                string path = string.Format("Assets/{0}_{1}.profile", GetType().Name, command);
                if (File.Exists(path))
                {
                    using (
                        FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                        )
                    {
                        using (BinaryReader br = new BinaryReader(fs))
                        {
                            AudioWordDetection.LoadWord(br, details);
                            //Debug.Log(string.Format("Loaded profile: {0}", path));
                            details.Label = command;
                        }
                    }
                }
                else
                {
                    Debug.Log(string.Format("Profile not available for: {0}", path));
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(string.Format("Failed to load word: {0}", ex));
            }
#endif
        }

        //subscribe detection event
        AudioWordDetection.WordDetectedEvent += WordDetectedHandler;
    }
コード例 #3
0
ファイル: Example14.cs プロジェクト: evagyomrey/YellingVR
    /// <summary>
    /// Initialize the example
    /// </summary>
    protected override void Start()
    {
        if (null == AudioWordDetection ||
            null == Mic)
        {
            Debug.LogError("Missing meta references");
            return;
        }

        if (null == Head)
        {
            Debug.LogError("Missing Head");
            return;
        }

        if (null == Head.sharedMesh)
        {
            Debug.LogError("Missing Head Shared Mesh");
            return;
        }

        Mesh mesh = Head.sharedMesh;

        for (int index = 0; index < Head.sharedMesh.blendShapeCount; ++index)
        {
            m_blendShapes.Add(mesh.GetBlendShapeName(index));
        }

        Dictionary <String, WordDetails> words = new Dictionary <String, WordDetails>();

        WordDetails noise = new WordDetails()
        {
            Label = "Noise"
        };

        AudioWordDetection.Words.Add(noise);

        // prepopulate words
        foreach (string command in m_blendShapes)
        {
            //Debug.Log(command);
            if (command == "idle")
            {
                continue;
            }
            WordDetails details = new WordDetails()
            {
                Label = command
            };
            AudioWordDetection.Words.Add(details);
            words[command] = details;
#if !UNITY_WEBPLAYER
            try
            {
                string path = string.Format("Assets/{0}_{1}.profile", GetType().Name, command);
                if (File.Exists(path))
                {
                    using (
                        FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                        )
                    {
                        using (BinaryReader br = new BinaryReader(fs))
                        {
                            AudioWordDetection.LoadWord(br, details);
                            //Debug.Log(string.Format("Loaded profile: {0}", path));
                            details.Label = command;
                        }
                    }
                }
                else
                {
                    Debug.Log(string.Format("Profile not available for: {0}", path));
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(string.Format("Failed to load word: {0}", ex));
            }
#endif
        }

        //subscribe detection event
        AudioWordDetection.WordDetectedEvent += WordDetectedHandler;
    }
コード例 #4
0
ファイル: Example16.cs プロジェクト: evagyomrey/YellingVR
    /// <summary>
    /// Initialize the example
    /// </summary>
    protected override void Start()
    {
        if (null == AudioWordDetection ||
            null == Mic)
        {
            Debug.LogError("Missing meta references");
            return;
        }

        if (null == VideoStage)
        {
            Debug.LogError("Missing Video Stage");
            return;
        }

        m_map.Add(Commands.Noise, new Mapping()
        {
            m_material = MaterialIdle, m_audioClip = null
        });
        m_map.Add(Commands.One, new Mapping()
        {
            m_material = MaterialVideo1, m_audioClip = Audio1
        });
        m_map.Add(Commands.Two, new Mapping()
        {
            m_material = MaterialVideo2, m_audioClip = Audio2
        });
        m_map.Add(Commands.Three, new Mapping()
        {
            m_material = MaterialVideo3, m_audioClip = Audio3
        });
        m_map.Add(Commands.Four, new Mapping()
        {
            m_material = MaterialVideo4, m_audioClip = Audio4
        });
        m_map.Add(Commands.Five, new Mapping()
        {
            m_material = MaterialVideo5, m_audioClip = Audio5
        });

        // prepopulate words
        foreach (string val in Enum.GetNames(typeof(Commands)))
        {
            //Debug.Log(val);
            WordDetails details = new WordDetails()
            {
                Label = val
            };
            AudioWordDetection.Words.Add(details);

#if !UNITY_WEBPLAYER
            try
            {
                string path = string.Format("Assets/{0}_{1}.profile", GetType().Name, val);
                if (File.Exists(path))
                {
                    using (
                        FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                        )
                    {
                        using (BinaryReader br = new BinaryReader(fs))
                        {
                            AudioWordDetection.LoadWord(br, details);
                            //Debug.Log(string.Format("Loaded profile: {0}", path));
                            details.Label = val;
                        }
                    }
                }
                else
                {
                    Debug.Log(string.Format("Profile not available for: {0}", path));
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(string.Format("Failed to load word: {0}", ex));
            }
#endif
        }

        //subscribe detection event
        AudioWordDetection.WordDetectedEvent += WordDetectedHandler;
    }
コード例 #5
0
ファイル: Example8.cs プロジェクト: evagyomrey/YellingVR
    /// <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;
    }
コード例 #6
0
    /// <summary>
    /// Initialize the example
    /// </summary>
    protected override void Start()
    {
        if (null == AudioWordDetection ||
            null == Mic)
        {
            Debug.LogError("Missing meta references");
            return;
        }

        m_Noise = Duplicate(m_PrefabIdle);
        for (int index = 0; index < m_expressions.Length; ++index)
        {
            m_expressions[index] = Duplicate(m_expressions[index]);
        }

        Dictionary <String, WordDetails> words = new Dictionary <String, WordDetails>();

        WordDetails noise = new WordDetails()
        {
            Label = "Noise"
        };

        AudioWordDetection.Words.Add(noise);

        // prepopulate words
        foreach (MeshRenderer expression in m_expressions)
        {
            //Debug.Log(val);
            String      command = expression.name;
            WordDetails details = new WordDetails()
            {
                Label = command
            };
            AudioWordDetection.Words.Add(details);
            words[command] = details;
#if !UNITY_WEBPLAYER
            try
            {
                string path = string.Format("Assets/{0}_{1}.profile", GetType().Name, command);
                if (File.Exists(path))
                {
                    using (
                        FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
                        )
                    {
                        using (BinaryReader br = new BinaryReader(fs))
                        {
                            AudioWordDetection.LoadWord(br, details);
                            //Debug.Log(string.Format("Loaded profile: {0}", path));
                            details.Label = command;
                        }
                    }
                }
                else
                {
                    Debug.Log(string.Format("Profile not available for: {0}", path));
                }
            }
            catch (Exception ex)
            {
                Debug.LogError(string.Format("Failed to load word: {0}", ex));
            }
#endif
        }

        //subscribe detection event
        AudioWordDetection.WordDetectedEvent += WordDetectedHandler;
    }