3

誰かがビープ音の素晴らしい組み合わせを見つけたのではないかと思っていました。それは実際には音楽のように聞こえます。

これがメソッドの呼び出し方法です。

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

5 に答える 5

10

次の簡単なプログラムを試して、次のコマンドを使用してメロディーを再生できます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();
        }
    }
}

興味のある方へ:これはヴェルクマイスター音を使用し、この気質に定義されたセント値に基づいて頻度を計算します。

于 2010-07-23T16:39:18.330 に答える
2

Beep関数は、QBasicのPlayコマンドによってラップされました。多くのグーグルがその1つにヒットしました、これはトップセレクションでした。Play文字列を翻訳するコードを書くと、コピーアンドペーストよりも少し面白くなる可能性があります。構文はここで説明されています

于 2010-07-23T16:22:39.917 に答える
1

もありSystem.Console.Beepます。

于 2010-07-23T15:59:35.600 に答える
1

Googleでモノフォニック着信音を確認してから、16進エディタでファイルを開き、曲をリバースエンジニアリングします。プログラムが元のファイルを読み取り、その場でビープ音に変換する場合のボーナスポイント。

于 2010-07-23T16:29:46.053 に答える
0

PCスピーカーをPCMオーディオデバイス(サウンドカード)として使用するサウンドドライバーがありました。

于 2010-07-23T16:04:10.460 に答える