gpt4 book ai didi

c# - System.Beep 等效

转载 作者:行者123 更新时间:2023-11-30 15:23:56 27 4
gpt4 key购买 nike

是否有任何类可以通过不同的音调在扬声器中发出声音?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

这是爱德华·格里格的《山王》片段: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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com