2010-07-23 83 views
3

我想知道是否有人發現一個很好的蜂鳴聲組合,實際上聽起來像音樂。有沒有不錯的kernel32蜂鳴聲(板載蜂鳴聲)?

這是如何調用該方法。

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

你打算如何使用它?可能有助於獲得一個體面的答案。 – Matt 2010-07-23 15:58:39

+1

這應該是社區維基,恕我直言,因爲音樂是一個相當主觀的話題,可能有很多「正確的」答案。 – stakx 2010-07-23 15:59:55

+0

只是爲了好玩。我知道我可以運行一個mp3或其他的東西,但我想知道互聯網上的任何人是否有時間「編寫」一些複雜的東西。 – citronas 2010-07-23 16:00:00

回答

10

你可以玩的下面這個簡單的PROGRAMM使用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值的頻率。

1

還有System.Console.Beep

2

Beep函數由QBasic中的Play命令包裹。大量的谷歌擊中那一個,這是top selection。編寫代碼來翻譯播放字符串可以使這比複製粘貼更有趣。語法是described here

1

檢查Google是否有Monophonic Ring-tones,然後用十六進制編輯器打開該文件並反向設計歌曲。如果您的程序將在原始文件中讀取並將其翻譯爲飛行中的嘟嘟聲,則可獲得獎勵積分。