【问题标题】:Are there nice kernel32 beep sounds (onboard beep)?是否有不错的 kernel32 哔声(板载哔声)?
【发布时间】:2011-03-20 04:43:19
【问题描述】:

我想知道是否有人找到了一个很好的哔哔声组合,这实际上听起来像音乐。

这是调用方法的方法。

[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);
        }            
    }

【问题讨论】:

  • 你打算用这个做什么?可能有助于获得一个体面的答案。
  • 这应该是社区维基,恕我直言,因为音乐是一个相当主观的话题,可能有很多“正确”的答案。
  • 只是为了好玩。我知道我可以运行 mp3 或其他东西,但我想知道互联网上是否有人有时间“创作”复杂的东西。
  • 作为提示... PC Speaker 曾经是唯一的声音设备。已经有很多尝试用内置的蜂鸣器制作音乐。

标签: c# windows audio beep


【解决方案1】:

您可以使用以下简单的程序来使用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, 0, 100),
            new Note(Note.C, 0, 100),
            new Note(Note.D, 0, 100),
            new Note(Note.E, 0, 100),
            new Note(Note.F, 0, 100),
            new Note(Note.G, 0, 100),
            new Note(Note.A, 0, 100),
            new Note(Note.B, 0, 100),
            new Note(Note.C, 1, 100),
            new Note(Note.D, 1, 100),
            new Note(Note.E, 1, 100),
            new Note(Note.F, 1, 100),
            new Note(Note.G, 1, 100),
            new Note(Note.A, 1, 100),
            new Note(Note.B, 1, 100),
            new Note(Note.C, 2, 100)
        };

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

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

【讨论】:

    【解决方案2】:

    Beep 函数由 QBasic 的 Play 命令包装。很多谷歌点击了那个,这是top selection。编写代码来翻译 Play 字符串可能比复制粘贴更有趣。语法是described here

    【讨论】:

      【解决方案3】:

      还有System.Console.Beep

      【讨论】:

        【解决方案4】:

        在 Google 上查找 Monophonic Ring-tones,然后使用十六进制编辑器打开文件并对歌曲进行逆向工程。如果您的程序将读取原始文件并将其即时转换为哔哔声,则可获得奖励积分。

        【讨论】:

          【解决方案5】:

          曾经有一个声音驱动程序将 PC 扬声器用作 PCM 音频设备(声卡)

          【讨论】:

            猜你喜欢
            • 2018-08-11
            • 1970-01-01
            • 1970-01-01
            • 2020-09-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多