Are there nice kernel32 beep sounds (onboard beep)?

雨燕双飞 提交于 2019-12-04 12:36:54

You can play around with the following simple programm to play melodies using 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();
        }
    }
}

For those that are interested: This used a Werckmeister temperament and calculates the frequencies based on the Cent values defined for this temperament.

The Beep function was wrapped by QBasic's Play command. Lots of Google hits on that one, this was the top selection. Writing the code to translate the Play string could make this a bit more interesting than copy-paste. The syntax is described here.

There is also System.Console.Beep.

Check Google for Monophonic Ring-tones then open the file with a hex editor and reverse engineer the songs. Bonus points if your program will read in the original file and translate it to the beeps on the fly.

There used to be a sound driver that would use the PC Speaker as a PCM audio device (sound card)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!