Example #1
0
    /// <summary>
    /// 解析键盘码,获取midiSigle类型的对象
    /// </summary>
    /// <param name="cmd">按下1或抬起按键0</param>
    /// <param name="keyId">按下键的id</param>
    /// <returns>midiSigle</returns>
    MidiKeyCode analysisKeySigle(int cmd, String keyId)
    {
        keyCode = new MidiKeyCode();
        long time_ticks;
        int  strength = 0;

        TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);

        time_ticks = Convert.ToInt64(ts.TotalSeconds);

        keyCode.Time_ticks = time_ticks;
        Console.WriteLine("MM" + keyId);
        keyCode.KeyCode = Convert.ToInt32(keyId[0]) - 65;
        Console.WriteLine("WW" + keyCode.KeyCode);
        keyCode.Strength = strength;
        if (cmd == 1)
        {
            keyCode.IsRrelease = false;
        }
        else if (cmd == 0)
        {
            keyCode.IsRrelease = true;
        }
        return(keyCode);
    }
Example #2
0
    // Use this for initialization
    void Start()
    {
        scoreUtil.init();
        string xmlPath = Application.streamingAssetsPath + "/" + PlayerPrefs.GetString("selectedLevel") + ".xml";

        midi = Midi.getMidi(xmlPath);
        midiPlayer.StopAll();
        midiPlayer.Play(PlayerPrefs.GetString("selectedLevel") + ".mid");

        analysisSignal = new AnalysisSignal();
        analysisSignal.midiStart(0);
        midikeycode   = new MidiKeyCode();
        midichordcode = new ChordKeycode();
    }
Example #3
0
    public void analysisMidiKey(string sigleStr)
    {
        MidiKeyCode recentKeyCode = new MidiKeyCode();

        continueCode = isMidiSignal(sigleStr);
        sigles       = continueCode.Split('d');


        for (int i = 0; i < sigles.Length - 1; i++)
        {
            sigles[i] += "d";
        }
        for (int j = 0; j < sigles.Length - 1; j++)
        {
            recentKeyCode = analysisMidiKeyCode(sigles[j]);


            if (!recentKeyCode.IsRrelease)
            {
                midiKeyCodeList.Add(recentKeyCode);
                isChordKeyCodeList.Add(recentKeyCode);

                keyCode = recentKeyCode;
                if (chordUtil.isChord(isChordKeyCodeList))
                {
                    isChord = true;
                    chord   = chordUtil.chord;
                    initIsChordKeyCodeList();
                }
                else
                {
                    isChord = false;
                    chord   = null;
                }
                //}
            }
            else
            {
                foreach (MidiKeyCode i in midiKeyCodeList)
                {
                    if (i.KeyCode == recentKeyCode.KeyCode)
                    {
                        midiKeyCodeList.Remove(i);
                        break;
                    }
                }
            }
        }
    }
Example #4
0
    /// <summary>
    /// 解析MIDI码,获取midiSigle类型的对象
    /// </summary>
    /// <param name="sigleStr">成为焦点的控件获取到的键盘码,string类型</param>
    /// <returns>midiSigle</returns>
    MidiKeyCode analysisMidiKeyCode(string sigleStr)
    {
        keyCode = new MidiKeyCode();
        long time_ticks;
        int  midi_cmd = 144;
        int  key;
        int  strength = 0;

        string[] sArray = sigleStr.Split('a');
        time_ticks = Convert.ToInt64(sArray[0]);
        sigleStr   = sArray[1];

        string[] sArray1 = sigleStr.Split('b');
        midi_cmd = Convert.ToInt32(sArray1[0]);
        sigleStr = sArray1[1];

        string[] sArray2 = sigleStr.Split('c');
        key      = Convert.ToInt32(sArray2[0]);
        sigleStr = sArray2[1];

        string[] sArray3 = sigleStr.Split('d');
        strength = Convert.ToInt32(sArray3[0]);


        keyCode.Time_ticks = time_ticks;
        keyCode.KeyCode    = key;
        keyCode.Strength   = strength;
        if (midi_cmd == 144)
        {
            keyCode.IsRrelease = false;
        }
        else
        {
            keyCode.IsRrelease = true;
        }


        return(keyCode);
    }
Example #5
0
    public bool isChord(List <MidiKeyCode> midiKeyCodeList)
    {
        if (midiKeyCodeList.Count < 3)
        {
            return(false);
        }


        List <int> allKey  = new List <int>();
        List <int> rootKey = new List <int>();                       //中央c右侧的音认为是根音
        //bool flag1 = false;
        List <int> chordKey = new List <int>();                      //中央c侧的音认为是

        MidiKeyCode        midirootKey  = new MidiKeyCode();
        List <MidiKeyCode> midichordKey = new List <MidiKeyCode>();

        midichordKey = midiKeyCodeList;

        for (int i = 0; i < midiKeyCodeList.Count; i++)
        {
            allKey.Add(midiKeyCodeList[i].KeyCode);
            if (midiKeyCodeList[i].KeyCode >= centerC)
            {
                rootKey.Add(midiKeyCodeList[i].KeyCode);
                if (midichordKey.Count > 0)
                {
                    midichordKey.Remove(midiKeyCodeList[i]);
                }
            }
        }

        if (midichordKey.Count < 3)
        {
            return(false);
        }

        chordKey = allKey.Except(rootKey).ToList();


        bool flag = true;

        foreach (ChordKeycode i in chordCodeList)
        {
            //MessageBox.Show("十si个和铉!");
            //MessageBox.Show(tmp.Count.ToString());
            if (i.ChordskeysCode.Count != chordKey.Count)
            {
                //MessageBox.Show("Count!=");
                flag = false;
            }
            else
            {
                flag = true;
                for (int k = 0; k < i.ChordskeysCode.Count; k++)
                {
                    if (i.ChordskeysCode[k] >= 12)
                    {
                        i.ChordskeysCode[k] = i.ChordskeysCode[k] % 12;
                    }
                }
                for (int j = 0; j < i.ChordskeysCode.Count; j++)
                {
                    //MessageBox.Show(i.ChordskeysCode[j].ToString()+"OOOOO"+chordKey[j]);
                    if (!i.ChordskeysCode.Contains(chordKey[j] % 12))
                    {
                        flag = false;
                        break;
                    }
                }
            }

            if (flag)
            {
                chord                = i;
                chord.Chordskeys     = midichordKey;
                chord.ChordskeysCode = chordKey;
                //MessageBox.Show(midichordKey[0].KeyCode.ToString());
                return(flag);
            }
        }

        return(flag);
    }
Example #6
0
    // Update is called once per frame
    void Update()
    {
        //此处判断游戏关卡结束条件,关卡对象总数暂时写100个,后续根据midi文件生成
        //if(Time.time>midi.TotalLength/1000+5)
        //{
        //	Application.LoadLevel ("end");
        //	Destroy(this);
        //	analysisSignal.midiClose();
        //}
        content  = "实时得分:" + scoreUtil.PerScore.ToString() + "\r\n";
        content += "力度匹配程度:" + scoreUtil.ForceRation.ToString() + "\r\n";
        content += "总得分:" + scoreUtil.Score.ToString() + "\r\n";
        content += "当前正确率:" + scoreUtil.Correct + "/" + count + "\r\n";

        if (count < midi.Count)
        {
            example(midi.NoteDelta[count] / 1000f);
        }
        foreach (int index in nullList)
        {
            goList.RemoveAt(index);
        }
        nullList = new ArrayList();

        /*if (Input.GetKeyDown (KeyCode.W)) {
         *      foreach(GameObject go in goList){
         *              if(go==null){
         *                      nullList.Add(goList.IndexOf(go));
         *              }else{
         *                      go.BroadcastMessage ("ReceiveBroadcastMessage","C5");
         *              }
         *      }
         * }*/
        if (Input.anyKey)
        {
            if (Input.GetKeyDown(KeyCode.W))
            {
                foreach (GameObject go in goList)
                {
                    if (go == null)
                    {
                        nullList.Add(goList.IndexOf(go));
                    }
                    else
                    {
                        go.BroadcastMessage("ReceiveBroadcastMessage", "C5");
                    }
                }
            }
            if (!analysisSignal.isEquelPerString(Input.inputString) && Input.inputString != "")
            {
                analysisSignal.analysisMidiKey(Input.inputString);
                midikeycode = analysisSignal.KeyCode;
                if (midikeycode != null && midikeycode.KeyCode != null && Midi.getNote(midikeycode.KeyCode) != null)
                {
                    foreach (GameObject go in goList)
                    {
                        if (go == null)
                        {
                            nullList.Add(goList.IndexOf(go));
                        }
                        else
                        {
                            go.BroadcastMessage("ReceiveBroadcastMessage", Midi.getNote(midikeycode.KeyCode));
                        }
                    }
                }
                Debug.Log("键值:" + midikeycode.KeyCode);
                //Debug.Log("键是否为释放状态:"+midikeycode.IsRrelease);
                //Debug.Log("按键力度:"+midikeycode.Strength);
                //Debug.Log("按键时间戳:"+midikeycode.Time_ticks);
            }
        }
    }