我需要在一定时间内播放某些频率。Console.beep 非常适合这个,但它在 win8 x64 上不受支持。SO上有一些帖子建议编写和播放WAV,但这并不理想。有没有更好更合适的方法?
问问题
394 次
1 回答
3
哔哔
public static void BeepBeep(int Amplitude, int Frequency, int Duration)
{
double A = ((Amplitude * (System.Math.Pow(2, 15))) / 1000) - 1;
double DeltaFT = 2 * Math.PI * Frequency / 44100.0;
int Samples = 441 * Duration / 10;
int Bytes = Samples * 4;
int[] Hdr = {0X46464952, 36 + Bytes, 0X45564157, 0X20746D66, 16, 0X20001, 44100, 176400, 0X100004, 0X61746164, Bytes};
using (MemoryStream MS = new MemoryStream(44 + Bytes))
{
using (BinaryWriter BW = new BinaryWriter(MS))
{
for (int I = 0; I < Hdr.Length; I++)
{
BW.Write(Hdr[I]);
}
for (int T = 0; T < Samples; T++)
{
short Sample = System.Convert.ToInt16(A * Math.Sin(DeltaFT * T));
BW.Write(Sample);
BW.Write(Sample);
}
BW.Flush();
MS.Seek(0, SeekOrigin.Begin);
using (SoundPlayer SP = new SoundPlayer(MS))
{
SP.PlaySync();
}
}
}
}
音符的频率
Octave--> 0 1 2 3 4 5 6 7
Note ---------------------------------------------
C 16 33 65 131 262 523 1046 2093
C# 17 35 69 139 277 554 1109 2217
D 18 37 73 147 294 587 1175 2349
D# 19 39 78 155 311 622 1244 2489
E 21 41 82 165 330 659 1328 2637
F 22 44 87 175 349 698 1397 2794
F# 23 46 92 185 370 740 1480 2960
G 24 49 98 196 392 784 1568 3136
G# 26 52 104 208 415 831 1661 3322
A 27 55 110 220 440 880 1760 3520
A# 29 58 116 233 466 932 1865 3729
B 31 62 123 245 494 988 1975 3951
于 2013-11-01T21:30:20.847 回答