My music program currently returns values for all notes and accidentals that equals the frequency of note A in that octave, but I cannot make the frequency output change for different notes or accidentals, i.e.:
A4=440, C4=440, D#5=880, A5=880 etc
Any input re how to amend my source code would be greatly appreciated!
// Calculates frequency (in Hz) of a note
int frequency(string note)
{
double octave;
double f_octave;
double f_sharpflat;
double frequency;
//getting octave as an int
{
if (note [1] == '#' || note [1] == 'b')
{
octave = atoi (¬e[2]);
}
else
{
octave = atoi (¬e[1]);
}
}
//putting frequency into right octave
{
if (octave < 4)
{
f_octave = round (A4 / (pow(2, (4 - octave))));
}
else if (octave > 4)
{
f_octave = round (A4 * (pow(2, (octave - 4))));
}
else
{
f_octave = A4;
}
}
//adding sharp or flat
{
if (note [1] == '#')
{
f_sharpflat = f_octave * (pow(2, 1/12));
}
else if (note [1] == 'b')
{
f_sharpflat = f_octave / (pow(2, 1/12));
}
else
{
f_sharpflat = f_octave;
}
}
//adding all notes
{
if (note [0] == 'C')
{
frequency = f_sharpflat / (pow(2, 9/12));
}
else if (note [0] == 'D')
{
frequency = f_sharpflat / (pow(2, 7/12));
}
else if (note [0] == 'E')
{
frequency = f_sharpflat / (pow(2, 5/12));
}
else if (note [0] == 'F')
{
frequency = f_sharpflat / (pow(2, 4/12));
}
else if (note [0] == 'G')
{
frequency = f_sharpflat / (pow(2, 2/12));
}
else if (note [0] == 'A')
{
frequency = f_sharpflat;
}
else if (note [0] == 'B')
{
frequency = f_sharpflat * (pow(2, 1/12));
}
else
{
return 0;
}
}
return round(frequency);
}