是否有任何类(class)可以通过不同的音调在扬声器中发出声音?
System.Beep() 是原始的,我不能以双倍发送频率。
我想,比如说,播放 A 声音或 B# 声音。我想函数调用是这样的:
double d = 425,545;
int duration = 500;
int volume = 0.8;
f(d, duration, volume)
f(ToneClass.A, duration, volume)//or like this
最佳答案
频率的整数部分就足够了。你的耳朵甚至不会注意到小数部分。
int frq = 425;
int duration = 500;
Console.Beep(frq, duration);
您可以从此处查看钢琴键频率。 https://en.wikipedia.org/wiki/Piano_key_frequencies
一些笔记。
使用枚举而不是整数。也使用
Task.Delay(duration).Wait();
休息。 (休息我的意思是沉默笔记)您可以使用一些公式来计算钢琴键和键频率,而不是使用大量的枚举或大量的硬编码整数。您还应该考虑音符长度。你可以在这里看到它们 https://en.wikipedia.org/wiki/Note_value
这是爱德华·格里格 (Edvard Grieg) 的《山之王》:D
static int GetPianoKey(string note)
{
int key = -1;
switch (note[0])
{
case 'A': key = 1; break;
case 'B': key = 3; break;
case 'C': key = 4; break;
case 'D': key = 6; break;
case 'E': key = 8; break;
case 'F': key = 9; break;
case 'G': key = 11; break;
}
if (note.Length == 2)
{
return key + 12*(note[1] - '0');
}
if (note.Length == 3)
{
return key + 12*(note[2] - '0') + (note[1] == 'b' ? -1 : 1);
}
throw new ApplicationException("Wrong note.");
}
static int GetNoteFrequency(string note)
{
return (int) (Math.Pow(1.05946309436, GetPianoKey(note) - 49)*440);
}
static int GetTickDuration(int tempo)
{
return 60000/tempo;
}
private static void Main(string[] args)
{
int duration = GetTickDuration(120); // 120 bpm. duration for quarter note
for (int i = 0; i < 2; i++)
{
Console.Beep(GetNoteFrequency("A3"), duration / 2); // eighth note ==> duration/2
Console.Beep(GetNoteFrequency("B3"), duration / 2);
Console.Beep(GetNoteFrequency("C3"), duration / 2);
Console.Beep(GetNoteFrequency("D3"), duration / 2);
Console.Beep(GetNoteFrequency("E3"), duration / 2);
Console.Beep(GetNoteFrequency("C3"), duration / 2);
Console.Beep(GetNoteFrequency("E3"), duration / 2);
Task.Delay(duration/2).Wait(); // eighth rest ==> duration/2
Console.Beep(GetNoteFrequency("D#3"), duration / 2);
Console.Beep(GetNoteFrequency("B3"), duration / 2);
Console.Beep(GetNoteFrequency("D#3"), duration / 2);
Task.Delay(duration / 2).Wait();
Console.Beep(GetNoteFrequency("D3"), duration / 2);
Console.Beep(GetNoteFrequency("Bb3"), duration / 2);
Console.Beep(GetNoteFrequency("D3"), duration / 2);
Task.Delay(duration / 2).Wait();
Console.Beep(GetNoteFrequency("A3"), duration / 2);
Console.Beep(GetNoteFrequency("B3"), duration / 2);
Console.Beep(GetNoteFrequency("C3"), duration / 2);
Console.Beep(GetNoteFrequency("D3"), duration / 2);
Console.Beep(GetNoteFrequency("E3"), duration / 2);
Console.Beep(GetNoteFrequency("C3"), duration / 2);
Console.Beep(GetNoteFrequency("E3"), duration / 2);
Console.Beep(GetNoteFrequency("A4"), duration / 2);
Console.Beep(GetNoteFrequency("G3"), duration / 2);
Console.Beep(GetNoteFrequency("E3"), duration / 2);
Console.Beep(GetNoteFrequency("C3"), duration / 2);
Console.Beep(GetNoteFrequency("E3"), duration / 2);
Console.Beep(GetNoteFrequency("G3"), duration * 2); // half note ==> duration*2
}
}
关于c# - System.Beep 等效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33773075/