Пример #1
0
        public static void playBasic(MidiDevice Device, string note, int trmbre, int channel)
        {
            Device.ChangeProgram(channel, trmbre, 0);
            int lenth = note.Length;
            int location;
            int speed;

            for (int i = 0; i < lenth - 2; i += 3)
            {
                location  = 60;
                location += 12 * (int)(note[i + 1] - 53);

                switch (note[i])
                {
                case 'C':
                case 'c': location += 0; break;

                case '!': location += 1; break;

                case 'D':
                case 'd': location += 2; break;

                case '#': location += 3; break;

                case 'E':
                case 'e': location += 4; break;

                case 'F':
                case 'f': location += 5; break;

                case '%': location += 6; break;

                case 'G':
                case 'g': location += 7; break;

                case '*': location += 8; break;

                case 'A':
                case 'a': location += 9; break;

                case 'B':
                case 'b': location += 11; break;

                default: location = -1; break;
                }

                //speed = 2 * int2Pow(note[i + 2] - 44);
                // speed = (int)(60000 / beat / 16 * (int)Math.Pow(2, (int)note[i + 2] - 48));
                speed = (int)(beat * int2Pow(note[i + 2] - 48));
                Device.Note_On(channel, location, 100);
                Thread.Sleep(speed);
                Device.Note_Off(channel, location, 100);
            }
        }
Пример #2
0
        /// <summary>
        /// 和弦播放单音的方法
        /// </summary>
        /// <param name="MIDI设备"></param>
        /// <param name="乐谱"></param>
        /// <param name="音色"></param>
        /// <param name="MIDI频道"></param>
        /// <param name="音量"></param>
        public static void playBasic_left(MidiDevice Device, string note, int trmbre, int channel, int playvolume)
        {
            // 1.  A3+31999 C4+41999 C4+41999 C4+41999 C3+51999 C5050999
            // 2.  8位一个音,1音名;2八度;3升降+-0;4音长;5附点1、0;6-8无意义的9占位;

            if (note != null)
            {
                //处理轨道音色问题
                Device.ChangeProgram(channel, trmbre, 0);

                //定义变量
                int    lenth = note.Length;
                int    location;
                double speed;

                for (int i = 0; i < lenth - 5; i += 8)
                {
                    //处理几个八度问题 第二位
                    location  = 60;
                    location += 12 * (int)(note[i + 1] - 53);
                    //处理八度以内问题 第一位
                    switch (note[i])
                    {
                    case 'C':
                    case 'c': location += 0; break;

                    case 'D':
                    case 'd': location += 2; break;

                    case 'E':
                    case 'e': location += 4; break;

                    case 'F':
                    case 'f': location += 5; break;

                    case 'G':
                    case 'g': location += 7; break;

                    case 'A':
                    case 'a': location += 9; break;

                    case 'B':
                    case 'b': location += 11; break;

                    default: location = -1; break;
                    }
                    //处理升降问题 + - 0 第三位
                    switch (note[i + 2])
                    {
                    case '+': location += 1; break;

                    case '-': location -= 1; break;

                    default: break;
                    }

                    //处理音符越界问题 第一二三位合起来
                    if (location != -1)
                    {
                        location += key_move;
                    }
                    if (location < -1)
                    {
                        location = -1;
                    }

                    //处理音符时长问题 第四位
                    speed = (60000 / beat / 16 * (int)Math.Pow(2, (int)note[i + 3] - 48));

                    //处理附点问题  第五位
                    if (note[i + 4] == '1')
                    {
                        speed *= 1.5;
                    }
                    Console.WriteLine("和弦轨播放:"
                                      + note[i] + note[i + 1] + note[i + 2] + note[i + 3]
                                      + note[i + 4] + note[i + 5] + note[i + 6] + note[i + 7]
                                      + "=" + location + "\t播放时间=" + speed);
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        MainWindow.InterfaceForMidi.keyDownColor_left(location);
                    }), null);
                    Device.Note_On(channel, location, (int)((double)volume / 100 * playvolume));
                    Thread.Sleep((int)speed);
                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        MainWindow.InterfaceForMidi.keyUpColor(location);
                    }), null);
                    Device.Note_Off(channel, location, (int)((double)volume / 100 * playvolume));
                }
            }
        }