Is there any class for making sound in speakers by different tones? System.Beep() is primitive, I can't send there frequency in double.
I want, let's say, play A sound or B# sound. I want to function call to be like:
double d = 425,545;
int duration = 500;
int volume = 0.8;
f(d, duration, volume)
f(ToneClass.A, duration, volume)//or like this
Integer part for frequency is enough. your ears wont even notice decimal part.
int frq = 425;
int duration = 500;
Console.Beep(frq, duration);
You can see piano key frequencies from here. https://en.wikipedia.org/wiki/Piano_key_frequencies
Some notes.
Use enum instead of ints. also use Task.Delay(duration).Wait();
for rest . (by rest i mean silence notes)
You can use some formulas to calculate piano key and then key frequency instead of using large enum or large number of hard coded ints. Also you should consider note lengths. You can see them here https://en.wikipedia.org/wiki/Note_value
Here is the piece of "Mountain King" by 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
}
}