private void OnDestroy()
 {
     player.AppDataEvent -= appDataEventFunc;
     player.PlayingEvent -= playingEventFunc;
     player      = null;
     sampleTools = null;
     mmlBox      = null;
 }
        private void Awake()
        {
            syntheStation = GameObject.FindObjectOfType <MySyntheStation>();
            sampleTools   = GameObject.FindObjectOfType <SampleTools>();
            mmlBox        = sampleTools.GetComponent <MyMMLBox>();
            {
                Toggle extend = transform.Find("Panel/Extend").GetComponent <Toggle>();
                extend.onValueChanged.AddListener((value) =>
                {
                    param.Extended = value;
                    loadTone(param);
                });
            }
            {
                inputField = transform.Find("Panel/InputField").GetComponent <InputField>();
                inputField.onEndEdit.AddListener((v) =>
                {
                    var tone = new ToneParamPM8();
                    if (tone.LoadFromString(v))
                    {
                        loadTone(tone);
                    }
                });

                var presets = GetComponentsInChildren <PresetButton>();
                foreach (var pb in presets)
                {
                    var btn = pb.GetComponent <Button>();
                    var tp  = new ToneParamPM8(pb.ToneData);
                    btn.onClick.AddListener(() =>
                    {
                        loadTone(tp);
                        inputField.text = param.ToString();
                    });
                }
            }
            {
                var obj = transform.Find("Panel/FM").gameObject;
                setupSlider(obj, "Algorithm", "al:", (val) => { param.Al = (Byte)val; applyTone(); });
                setupSlider(obj, "Feedback", "fb:", (val) => { param.Fb = (Byte)val; applyTone(); });
                setupSlider(obj, "WaveForm", "lw:", (val) => { param.Lfo.WS = (Byte)val; applyTone(); });
                setupSlider(obj, "Frequency", "lf:", (val) => { param.Lfo.LF = (Byte)val; applyTone(); });
                setupSlider(obj, "PMPower", "lp:", (val) => { param.Lfo.LP = (Byte)val; applyTone(); });
                setupSlider(obj, "AMPower", "la:", (val) => { param.Lfo.LA = (Byte)val; applyTone(); });
                setupSlider(obj, "AttackRate", "ar:", (val) => { if (param.Extended)
                                                                 {
                                                                     param.Lfo.Env.ExAR = (Byte)val;
                                                                 }
                                                                 else
                                                                 {
                                                                     param.Lfo.Env.AR = (Byte)val;
                                                                 }; applyTone(); });
                setupSlider(obj, "DecayRate", "dr:", (val) => { if (param.Extended)
                                                                {
                                                                    param.Lfo.Env.ExDR = (Byte)val;
                                                                }
                                                                else
                                                                {
                                                                    param.Lfo.Env.DR = (Byte)val;
                                                                }; applyTone(); });
                setupSlider(obj, "SustainLevel", "sl:", (val) => { if (param.Extended)
                                                                   {
                                                                       param.Lfo.Env.ExSL = (Byte)val;
                                                                   }
                                                                   else
                                                                   {
                                                                       param.Lfo.Env.SL = (Byte)val;
                                                                   }; applyTone(); });
                setupSlider(obj, "SustainRate", "sr:", (val) => { if (param.Extended)
                                                                  {
                                                                      param.Lfo.Env.ExSR = (Byte)val;
                                                                  }
                                                                  else
                                                                  {
                                                                      param.Lfo.Env.SR = (Byte)val;
                                                                  }; applyTone(); });
                setupSlider(obj, "ReleaseRate", "rr:", (val) => { if (param.Extended)
                                                                  {
                                                                      param.Lfo.Env.ExRR = (Byte)val;
                                                                  }
                                                                  else
                                                                  {
                                                                      param.Lfo.Env.RR = (Byte)val;
                                                                  }; applyTone(); });
            }
            for (int i = 0; i < 4; i++)
            {
                int n   = i;
                var obj = transform.Find("Panel/OP" + (i + 1)).gameObject;
                setupSlider(obj, "WaveStyle", "ws:", (val) => { param.Op[n].WS = (Byte)val; applyTone(); });
                setupSlider(obj, "AMEnable", "ae:", (val) => { param.Op[n].AE = (Byte)val; applyTone(); });
                setupSlider(obj, "Multiple", "ml:", (val) => { param.Op[n].Ml = (Byte)val; applyTone(); });
                setupSlider(obj, "Detune", "dt:", (val) => { param.Op[n].Dt = (Byte)val; applyTone(); });
                setupSlider(obj, "KeyScale", "ks:", (val) => { param.Op[n].Env.KS = (Byte)val; applyTone(); });
                setupSlider(obj, "VelocitySense", "vs:", (val) => { param.Op[n].Env.VS = (Byte)val; applyTone(); });
                setupSlider(obj, "TotalLevel", "tl:", (val) => { param.Op[n].Env.TL = (Byte)val; applyTone(); });
                setupSlider(obj, "AttackRate", "ar:", (val) => { if (param.Extended)
                                                                 {
                                                                     param.Op[n].Env.ExAR = (Byte)val;
                                                                 }
                                                                 else
                                                                 {
                                                                     param.Op[n].Env.AR = (Byte)val;
                                                                 }; applyTone(); });
                setupSlider(obj, "DecayRate", "dr:", (val) => { if (param.Extended)
                                                                {
                                                                    param.Op[n].Env.ExDR = (Byte)val;
                                                                }
                                                                else
                                                                {
                                                                    param.Op[n].Env.DR = (Byte)val;
                                                                }; applyTone(); });
                setupSlider(obj, "SustainLevel", "sl:", (val) => { if (param.Extended)
                                                                   {
                                                                       param.Op[n].Env.ExSL = (Byte)val;
                                                                   }
                                                                   else
                                                                   {
                                                                       param.Op[n].Env.SL = (Byte)val;
                                                                   }; applyTone(); });
                setupSlider(obj, "SustainRate", "sr:", (val) => { if (param.Extended)
                                                                  {
                                                                      param.Op[n].Env.ExSR = (Byte)val;
                                                                  }
                                                                  else
                                                                  {
                                                                      param.Op[n].Env.SR = (Byte)val;
                                                                  }; applyTone(); });
                setupSlider(obj, "ReleaseRate", "rr:", (val) => { if (param.Extended)
                                                                  {
                                                                      param.Op[n].Env.ExRR = (Byte)val;
                                                                  }
                                                                  else
                                                                  {
                                                                      param.Op[n].Env.RR = (Byte)val;
                                                                  }; applyTone(); });
            }
            {
                var keyboard = transform.Find("Panel/Keyboard").GetComponent <Keyboard>();
                keyboard.PortNo = portNo;
                keyboard.ChNo   = chNo;
            }
            {
                var mmlEditorCanvas = transform.parent.Find("MMLPlayerCanvas");
                var button          = transform.Find("Panel/MMLEditorButton").GetComponent <Button>();
                button.onClick.AddListener(() =>
                {
                    mmlBox.Play("Click");
                    sampleTools.MoveCameraTo(mmlEditorCanvas.localPosition);
                    mmlEditorCanvas.GetComponent <CanvasGroup>().interactable = true;
                    this.GetComponent <CanvasGroup>().interactable            = false;
                });
            }
            {
                var button = transform.Find("Panel/Random").GetComponent <Button>();
                button.onClick.AddListener(() =>
                {
                    GenerateRandom(RandomMode.Normal);
                });
            }
            {
                var button = transform.Find("Panel/RandomEx").GetComponent <Button>();
                button.onClick.AddListener(() =>
                {
                    GenerateRandom(RandomMode.Ex);
                });
            }
            {
                var button = transform.Find("Panel/RandomTx").GetComponent <Button>();
                button.onClick.AddListener(() =>
                {
                    GenerateRandom(RandomMode.Tx);
                });
            }
        }
        private void Awake()
        {
            syntheStation = GameObject.FindObjectOfType <MySyntheStation>();
            sampleTools   = GameObject.FindObjectOfType <SampleTools>();
            mmlBox        = sampleTools.GetComponent <MyMMLBox>();
            {
                var extend = transform.Find("Panel/Extend").GetComponent <Button>();
                extend.onClick.AddListener(() =>
                {
                    param.Extended = !param.Extended;
                    updateParam();
                    loadTone(param);
                });
            }
            {
                inputField = transform.Find("Panel/InputField").GetComponent <InputField>();
                inputField.onEndEdit.AddListener((v) =>
                {
                    var tone = new ToneParamCT8();
                    if (tone.LoadFromString(v))
                    {
                        loadTone(tone);
                    }
                });

                var presets = GetComponentsInChildren <PresetButton>();
                foreach (var pb in presets)
                {
                    var btn = pb.GetComponent <Button>();
                    var tp  = new ToneParamCT8(pb.ToneData);
                    btn.onClick.AddListener(() =>
                    {
                        loadTone(tp);
                    });
                }
            }
            {
                var obj = transform.Find("Panel/LFO").gameObject;
                setupSlider(obj, "WaveForm", "lw:", (val) => { param.Lfo.WS = (Byte)val; updateParam(); });
                setupSlider(obj, "Frequency", "lf:", (val) => { param.Lfo.LF = (Byte)val; updateParam(); });
                setupSlider(obj, "PMPower", "lp:", (val) => { param.Lfo.LP = (Byte)val; updateParam(); });
                setupSlider(obj, "AMPower", "la:", (val) => { param.Lfo.LA = (Byte)val; updateParam(); });
                setupSlider(obj, "AttackRate", "ar:", (val) => { param.Lfo.Env.AR = (Byte)val; updateParam(); });
                setupSlider(obj, "DecayRate", "dr:", (val) => { param.Lfo.Env.DR = (Byte)val; updateParam(); });
                setupSlider(obj, "SustainLevel", "sl:", (val) => { param.Lfo.Env.SL = (Byte)val; updateParam(); });
                setupSlider(obj, "SustainRate", "sr:", (val) => { param.Lfo.Env.SR = (Byte)val; updateParam(); });
                setupSlider(obj, "ReleaseRate", "rr:", (val) => { param.Lfo.Env.RR = (Byte)val; updateParam(); });
            }
            {
                var obj = transform.Find("Panel/ENV").gameObject;
                setupSlider(obj, "WaveStyle", "ws:", (val) => { param.WS = (Byte)val; updateParam(); });
                setupSlider(obj, "KeyScale", "ks:", (val) => { param.Env.KS = (Byte)val; updateParam(); });
                setupSlider(obj, "VelocitySense", "vs:", (val) => { param.Env.VS = (Byte)val; updateParam(); });
                setupSlider(obj, "TotalLevel", "tl:", (val) => { param.Env.TL = (Byte)val; updateParam(); });
                setupSlider(obj, "AttackRate", "ar:", (val) => { param.Env.AR = (Byte)val; updateParam(); });
                setupSlider(obj, "DecayRate", "dr:", (val) => { param.Env.DR = (Byte)val; updateParam(); });
                setupSlider(obj, "SustainLevel", "sl:", (val) => { param.Env.SL = (Byte)val; updateParam(); });
                setupSlider(obj, "SustainRate", "sr:", (val) => { param.Env.SR = (Byte)val; updateParam(); });
                setupSlider(obj, "ReleaseRate", "rr:", (val) => { param.Env.RR = (Byte)val; updateParam(); });
            }
            {
                var wt = transform.Find("Panel/WaveTable").GetComponent <WaveTable>();
                wt.OnUpdateAction += (idx) =>
                {
                    param.WT[idx] = wt.WT[idx];
                    updateParam();
                };
            }
            {
                var keyboard = transform.Find("Panel/Keyboard").GetComponent <Keyboard>();
                keyboard.ChNo   = chNo;
                keyboard.PortNo = portNo;
            }
            {
                var mmlEditorCanvas = transform.parent.Find("MMLPlayerCanvas");
                var button          = transform.Find("Panel/MMLEditorButton").GetComponent <Button>();
                button.onClick.AddListener(() =>
                {
                    mmlBox.Play("Click");
                    sampleTools.MoveCameraTo(mmlEditorCanvas.localPosition);
                    mmlEditorCanvas.GetComponent <CanvasGroup>().interactable = true;
                    this.GetComponent <CanvasGroup>().interactable            = false;
                });
            }
        }
        private void Awake()
        {
            player               = GameObject.FindObjectOfType <MyMMLPlayer>();
            sampleTools          = GameObject.FindObjectOfType <SampleTools>();
            mmlBox               = sampleTools.GetComponent <MyMMLBox>();
            player.AppDataEvent += appDataEventFunc;
            player.PlayingEvent += playingEventFunc;
#if false
            {
                string    mml  = "$@(h_close5) = @pm8[4 7 op1[0 0 15 0 7 0 0 0 0 31 0 0 0 0] op2[0 0 0 0 7 0 0 1 0 21 18 5 15 13] op3[0 0 8 0 3 0 0 0 0 31 0 0 0 14] op4[0 0 15 0 3 0 1 1 5 31 17 15 13 9] lfo[0 127 0 0 31 0 0 0 0] ]\nt0=@(h_close5)a32c32";
                MyMMLClip clip = new MyMMLClip("Click", mml);
                clip.GenerateAudioClip = true;
                mmlBox.Add(clip);
            }
#endif
            invalid          = true;
            playing          = false;
            ToneEditorCanvas = transform.parent.Find("ToneEditorCanvas").gameObject;
            {
                var button = transform.Find("Panel/ToneEditorButton").GetComponent <Button>();
                button.onClick.AddListener(() =>
                {
                    mmlBox.Play("Click");
                    sampleTools.MoveCameraTo(ToneEditorCanvas.transform.localPosition);
                    ToneEditorCanvas.GetComponent <CanvasGroup>().interactable = true;
                    this.GetComponent <CanvasGroup>().interactable             = false;
                });
            }
            ToneEditorCT8Canvas = transform.parent.Find("ToneEditorCT8Canvas").gameObject;
            {
                var button = transform.Find("Panel/ToneEditorCT8Button").GetComponent <Button>();
                button.onClick.AddListener(() =>
                {
                    mmlBox.Play("Click");
                    sampleTools.MoveCameraTo(ToneEditorCT8Canvas.transform.localPosition);
                    ToneEditorCT8Canvas.GetComponent <CanvasGroup>().interactable = true;
                    this.GetComponent <CanvasGroup>().interactable = false;
                });
            }
            {
                var button = transform.Find("Panel/PlayButton").GetComponent <Button>();
                cursolPosText   = transform.Find("Panel/CursolPosText").GetComponent <Text>();
                mmlField        = transform.Find("Panel/MMLField").GetComponent <InputField>();
                consoleText     = transform.Find("Panel/ConsolePanel").GetComponentInChildren <Text>();
                instructionText = transform.Find("Panel/InstructionPanel").GetComponentInChildren <Text>();
                playButtonText  = button.GetComponentInChildren <Text>();
#if UNITY_5_3_OR_NEWER
                mmlField.onValueChanged.AddListener((str) =>
#else
                mmlField.onValueChange.AddListener((str) =>
#endif
                {
                    invalid = true;
                    if (player.Playing)
                    {
                        player.Stop();
                        playButtonText.text = "Play";
                        consoleText.text    = "";
                        playing             = false;
                    }
                    else
                    {
                        playButtonText.text = "Play";
                    }
                });
                button.onClick.AddListener(() =>
                {
                    if (invalid)
                    {
                        if (player.Playing && (player.Clip == mmlClip))
                        {
                            player.Stop();
                            playButtonText.text = "Play";
                            consoleText.text    = "";
                            playing             = false;
                        }
                        else
                        {
                            mmlClip.TextB = mmlField.text;
                            player.Play(mmlClip);
                            resetStb();
                            invalid             = false;
                            consoleText.text    = "Playing...";
                            playButtonText.text = "Pause";
                            playing             = true;
                        }
                    }
                    else
                    {
                        if (player.Playing)
                        {
                            player.Pause();
                            consoleText.text    = "";
                            playButtonText.text = "Continue";
                            playing             = false;
                        }
                        else
                        {
                            player.Continue();
                            consoleText.text    = "Playing...";
                            playButtonText.text = "Pause";
                            playing             = true;
                        }
                    }
                });
            }
        }