0

I've been trying to figure out the frequency part of this problem, and all of it seems to be running fine as far as I can tell, except for one part: my octave variable is not giving me anything close to what I intend. When I debug I can see that my octave's value is 52, but I have no idea where it's coming from. When I manually set octave to a specific number it works fine. Maybe there's just something stupid I'm missing? Here's the main portion of my code.

 int octave = note[strlen(note) - 1];
float freq = 0.0;

switch(note[0])
{
    case 'C':
        freq = 440.0 / pow(2.0, (9.0/12.0));
        break;

    case 'D':
        freq = 440.0 / pow(2.0, (7.0/12.0));
        break;

    case 'E':
        freq = 440.0 / pow(2, (5.0/12.0));
        break;

    case 'F':
        freq = 440.0 / pow(2.0, (4.0/12.0));
        break;

    case 'G':
        freq = 440.0 / pow(2.0, (2.0/12.0));
        break;

    case 'A':
        freq = 440.0;
        break;

    case 'B':
        freq = 440.0 * pow(2.0, (2.0/12.0));
        break;
}
1

note[strlen(note) - 1] is the ASCII value of the digit. You would need to subtract the character '0' (that's same as subtracting 48) to find the value you expect.

So you could use note[strlen(note) - 1] - '0'. If you want, you could subtract '4' instead to find the octaves relative to A4.

1
  • Thank you! That cleared everything up so much. I forgot that numbers are characters and have their own ASCII values. Everything seems to be working properly now!
    – DylieWylie
    Mar 7 '18 at 17:26

You must log in to answer this question.

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