I'm writing software that transposes sheet music between keys and I'm still new at music theory so I was hoping someone who knows more could let me know if my approach is correct. Here's my process for each note:
- Find the degree of the note's letter name in the current key's scale.
- Find the number of semitones raised or lowered the note is from the actual note in the scale. I'll call this the offset.
- Find the note in the new key's scale that is the same degree.
- Raise or lower the new note by the offset.
Here's an example. Say we are transposing the note E# from C major to D major. Then we would do the steps above as follows:
- E# would be third degree of the scale.
- The offset would be +1 because E is actually the third degree of the C major scale, and E# is one semitone above that.
- The third degree of the D major scale is F# so that's the new note.
- Adding our +1 offset to that would make it F##.
So F## is the result of transposing E# from C major to D major. I realize you wouldn't normally have an E# in the key of C major but I think it's possible and it works to explain the algorithm.
Is this algorithm correct? Should I be approaching this differently?
A secondary question: is it necessary to know the mode of the keys when executing this kind of algorithm? The results seem the same whether I'm using major or minor scales for the degree/offset calculations. Thanks in advance!
It sounds like the above algorithm is correct, but can be simplified. The piece of the puzzle I was missing was that you can determine the interval without referring to the scale. Here's the updated algorithm. Steps 1 and 2 find the interval between the current key and the new key. Steps 3 and 4 apply that same interval to the note we are transposing.
- Find the distance between the current key's letter name and the new key's letter name in the list of all letters (not including sharps or flats): A, B, C, D, E, F, G.
- Find the distance in semitones between the current key and the new key.
- The new note's letter will be the letter distance (Step 1) above the current note.
- Add sharps or flats to the new note's letter until it is the correct number of semitones (from Step 2) above the current note.
Repeating the original example we'll have:
- D is 1 letter above C
- D is 2 semitones above C
- The new letter will be 1 letter above E#, which is F
- The new note will be 2 semitones above E#, which is F##
Thank you to everyone who answered, and especially to @MattL and @Dom. All the answers and comments have been very enlightening!