加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 有没有很棒的kernel32哔声(板上发出哔哔声)?

发布时间:2020-12-15 19:36:41 所属栏目:百科 来源:网络整理
导读:我想知道是否有人发现了一个很好的哔哔声组合,实际上听起来像音乐. 这是如何调用该方法. [DllImport("Kernel32.dll")]public static extern bool Beep(UInt32 frequency,UInt32 duration);// ... // callBeep(2000,400); 我的第一次尝试: for (int j = 1; j
我想知道是否有人发现了一个很好的哔哔声组合,实际上听起来像音乐.

这是如何调用该方法.

[DllImport("Kernel32.dll")]
public static extern bool Beep(UInt32 frequency,UInt32 duration);
// ... 
// call
Beep(2000,400);

我的第一次尝试:

for (int j = 1; j < 20; j++)
    {
        for (int i = 1; i <= 10; i++)
        {
            Console.Beep(300 * i,200);
        }            
    }

解决方法

您可以使用以下简单的程序来播放使用Beep的旋律:

using System;
using System.Runtime.InteropServices;

class MelodyPlayer
{
    const double ConcertPitch = 440.0;

    class Note
    {
        [DllImport("Kernel32.dll")]
        public static extern bool Beep(UInt32 frequency,UInt32 duration);

        public const int C = -888;
        public const int CSharp = -798;
        public const int DFlat = CSharp;
        public const int D = -696;
        public const int DSharp = -594;
        public const int EFlat = DSharp;
        public const int E = -498;
        public const int F = -390;
        public const int FSharp = -300;
        public const int GFlat = FSharp;
        public const int G = -192;
        public const int GSharp = -96;
        public const int AFlat = GSharp;
        public const int A = 0;
        public const int ASharp = 108;
        public const int BFlat = ASharp;
        public const int B = 204;

        public int Key { get; set; }
        public int Octave { get; set; }
        public uint Duration { get; set; }

        public Note(int key,int octave,uint duration)
        {
            this.Key = key;
            this.Octave = octave;
            this.Duration = duration;
        }

        public uint Frequency
        {
            get
            {
                double factor = Math.Pow(2.0,1.0 / 1200.0);
                return ((uint)(MelodyPlayer.ConcertPitch * Math.Pow(factor,this.Key + this.Octave * 1200.0)));
            }
        }

        public void Play()
        {
            Beep(this.Frequency,this.Duration);
        }
    }

    static void Main(string[] args)
    {
        Note[] melody = new Note[] {
            new Note(Note.C,100),new Note(Note.C,new Note(Note.D,new Note(Note.E,new Note(Note.F,new Note(Note.G,new Note(Note.A,new Note(Note.B,1,2,100)
        };

        foreach (var note in melody)
        {
            note.Play();
        }
    }
}

对于那些感兴趣的人:这使用了Werckmeister temperament并根据为这种气质定义的Cent值计算频率.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读