I'm having a bit of a hard time trying to calculate the value of n(the number of semitones from A4).

I'm currently using the following formula:

if (octave > A4_OCTAVE) { n = (octave - A4_OCTAVE) * NUMBER_OF_SEMITONES; } else if (octave < A4_OCTAVE) { n = (A4_OCTAVE - octave) * NUMBER_OF_SEMITONES; }


and octave is gotten from the last position of the note passed into the frequency function and then changed to an int using atoi().

Thanks for the help!


Why do you need an if/else? In my code, a negative number stands for octaves below, a positive for octaves above octave 4. This gets added to the number of semitones relative to A of the same octave (and possibly further modified by b or #). I don't mind the total going negative, as pow works fine with those.

  • Thanks for the response. Can you expand a bit more please? How do you then calculate for the number of semitones to get to let's say C4 from A4? – C_1992 Sep 27 '18 at 19:00
  • I have some table that maps note[0]-'A' (similar to caesar and vigenere) to a value, for example element for index 2 ('C'-'A') would be -9, as C4 is 9 semitones below A4. You could also use switch/case or an if/else chain. – Blauelf Sep 28 '18 at 9:34

You must log in to answer this question.

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