I really don't know what I am doing wrong. I think I've mad a mistake in frequency. Check50 says:
:( note C3 has frequency 131
expected "131", not "220"
:( note Bb5 has frequency 932
expected "932", not "831"
:( produces all correct notes for octaves 3-5
Incorrect frequency for C3, should be 131, not 220
I would really appreciate of someone would take a look at my code:
int frequency(string note)
{
char letter = atoi(¬e[0]);
int octave = atoi(¬e[1]);
int n = 0;
if (strlen(note) == 3)
{
octave = atoi(¬e[2]);
char accident = note[1];
if (accident == '#')
{
n += 1;
}
else if (accident == 'b')
{
n -= 1;
}
}
if (letter == 'C')
{
n-= 9;
}
else if (letter == 'D')
{
n-= 7;
}
else if (letter == 'E')
{
n-= 5;
}
else if (letter == 'F')
{
n-= 4;
}
else if (letter == 'G')
{
n-= 2;
}
else if (letter == 'A')
{
n-= 0;
}
else if (letter == 'B')
{
n+= 2;
}
n+= (octave - 4) * 12;
float x = 2.0;
float y = (n/12.0);
float freq = round(pow(x,y) * 440);
if (octave < 4)
{
for (int i = 0; i < 4 - octave; i++)
{
freq /= 2.0;
}
}
if (octave > 4)
{
for (int i = 0; i < octave - 4; i++)
{
freq *= 2.0;
}
}
int totalfreq = round(freq);
return totalfreq;
}
Many many many thanks!