⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 大话设计模式源码bigtalkdesignpattern_src
💻 CS
📖 第 1 页 / 共 2 页
字号:

            }

            return result;
        }
    }
    /// <summary>
    /// 表达式
    /// </summary>
    abstract class Expression
    {
        protected MediaPlayer p;
        
        public Expression(MediaPlayer p)
        {
            this.p = p;
        }

        public void Interpret(PlayContext context)
        {
            if (context.PlayText.Length == 0)
            {
                return;
            }
            else
            {
                string playKey = context.PlayText.Substring(0, 1);
                context.PlayText = context.PlayText.Substring(2);
                double playValue = Convert.ToDouble(context.PlayText.Substring(0, context.PlayText.IndexOf(" ")));
                context.PlayText = context.PlayText.Substring(context.PlayText.IndexOf(" ") + 1);
                
                Excute(playKey, playValue);

            }
        }

        public abstract void Excute(string key,double value);
    }
    //音符
    class Note : Expression
    {
        public Note(MediaPlayer p):base(p)
        {}
        public override void Excute(string key, double value)
        {
            p.Note = key;
            p.test += value;
            //System.Console.WriteLine("test{0}:{1}",p.GetType().FullName, p.test);

            p.Interval = Convert.ToInt32(value * p.Speed);//音长为获得的值乘以音速,也就是timer控件的单次interval值
            p.Play();
        }
    }
    //音阶
    class Scale : Expression
    {
        public Scale(MediaPlayer p)
            : base(p)
        { }

        public override void Excute(string key, double value)
        {
            p.Scale=Convert.ToInt32(value);
            p.Interval = 1;//在设置音阶时,timer不需要时间间隔,所以为1表示1毫秒
        }
    }
    //音符
    class Speed : Expression
    {
        public Speed(MediaPlayer p)
            : base(p)
        { }

        public override void Excute(string key, double value)
        {
            p.Speed = Convert.ToInt32(value);
            p.Interval = 1;//在设置音速时,timer不需要时间间隔,所以为1表示1毫秒
        }
    }

    //播放器
    class MediaPlayer
    {
        public double test = 0;

        private AxWindowsMediaPlayer[] awm;
        private string path;
        public MediaPlayer(string path, params AxWindowsMediaPlayer[] arr)
        {
            awm = arr;
            this.path = path;
        }

        /// <summary>
        /// 给具体的MediaPlayer控件设置声音文件
        /// </summary>
        /// <param name="index"></param>
        /// <param name="num"></param>
        public void GetSound(int index, int num)
        {
            awm[index].URL = path + num.ToString() + ".mp3";
        }

        //音阶
        private int scale=2;
        public int Scale
        {
            get { return scale; }
            set { scale = value; }
        }
        //音速
        private int speed=500;
        public int Speed
        {
            get { return speed; }
            set { speed = value; }
        }
        //音符
        private string note;
        public string Note
        {
            get { return note; }
            set { note = value; }
        }
        //音长
        private int interval=1;
        public int Interval
        {
            get { return interval; }
            set { interval = value; }
        }
        //播放
        public void Play()
        {
            int index = 0;
            //选择音阶
            switch (scale)
            {
                case 0:
                    index = 0;
                    break;
                case 1:
                    index = 7;
                    break;
                case 2:
                    index = 14;
                    break;
                case 3:
                    index = 21;
                    break;
                case 4:
                    index = 28;
                    break;
                case 5:
                    index = 35;
                    break;

            }
            //选择音符
            switch (note)
            {
                case "C":
                    index += 0;
                    break;
                case "D":
                    index += 1;
                    break;
                case "E":
                    index += 2;
                    break;
                case "F":
                    index += 3;
                    break;
                case "G":
                    index += 4;
                    break;
                case "A":
                    index += 5;
                    break;
                case "B":
                    index += 6;
                    break;
                case "P":
                    return;
            }

            //停止原来播放的声音。相当于钢琴某个键按过后还在余音,此时再此按到此键,则原有余音结束
            awm[index].Ctlcontrols.stop();
            //播放
            awm[index].Ctlcontrols.play();
        }

        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -