0

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; }

where A4_OCTAVE = 4, NUMBER_OF_SEMITONES = 12

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!

1 Answer 1

0

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.

2
  • 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
    Commented Sep 27, 2018 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
    Commented Sep 28, 2018 at 9:34

You must log in to answer this question.

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