0

My frequency function returns all 440. Been researching and trying possible solutions (turn ints to floats, round, power, etc) but still does it. What am I doing wrong? Thank you!

int frequency(string note)
{
    double f;
    float n; // number of semitones from note to A4
    // first get the number of semitones particular note is from A440
    // parse note into string
    int fullNote = strlen(note);
    int LetterNote = note[0];
    int octave = note[fullNote - 1] - '0';

        switch (LetterNote){

                case 'A':
                n = 0.0;
                break;

                case 'B':
                n = 2.0;
                break;

                case 'C':
                n = -9.0;
                break;

                case 'D':
                n = -7.0;
                break;

                case 'E':
                n = -5.0;
                break;

                case 'F':
                n = -4.0;
                break;

                case 'G':
                n = -2.0;
                break;

                default:
                return 0;
        }

        // if note has an accidental calculate either one semitone down or up
        if (fullNote == 3)

            if (note[1] == 'b')
            {
                n = n - 1.0;
            }

            if (note[1] == '#')
            {
                n = n + 1.0;
            }

        // calculate octave
        if (octave < 8 && octave > 0)
        {
            n = n + ((octave - 4.0) * 12.0);
        }
        if (octave > 8 || octave < 0)
        {
            return 0;
        }

        // calculate frequency with correct number of semitones

          f = round(pow(2.0, (n/12.0))) * 440.00;
          return f;
}
0

You are rounding the power of two, while you are meant to round the frequency.

Instead of

f = round(pow(2.0, (n/12.0))) * 440.00;

use

f = round(pow(2.0, (n/12.0)) * 440.00);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .