6

I'm new in music theory and also a junior web developer. I created a simple script that converts letter notes by semitone. I'm confused on how to transpose notes.

My current understanding is when you go up by one semitone, the notes became sharps. Go down by one semitone, the notes in that become flats. Is this right? If not, when can you tell if a certain note becomes sharp or flat?

Example scenario:

If the current note is A# and the user clicks the "+" icon, the result should be B. But if that user wants to go back by clicking the "-" button, the note becomes Bb?

5
  • Your understanding is correct. Sharp = up one semitone; Flat = down one semitone.
    – Aaron
    Jan 16, 2021 at 4:20
  • Thanks Aaron. I have one more question, if the current note is "A#" and the user click the "+" icon it should be B. But if he want to go back by clicking "-" button, the note became "Bb"?
    – enrique
    Jan 16, 2021 at 4:24
  • 2
    It is difficult to answer without understanding the purpose. Is there a reason not to stick to sharps only, or something like C# D# F# G# Bb, or displaying a pair A#/Bb?... Jan 16, 2021 at 4:59
  • Actually, my current script using only sharps. I'm just curious why some website uses flats. That's why I get confused in that part.
    – enrique
    Jan 16, 2021 at 5:04
  • 3
    Please could you tell us more about the context in which a user might do this? Is it to let a user who's written some music to select it and transpose it? Or to let the user write it in the first place? You're going to need to let the user enter flats as well as sharps, and transposing to the flat side (e.g. transposing music from A major to B flat major) will introduce flats. A model of pitch as number of semitones up from some reference point is not adequate for either purpose.
    – Rosie F
    Jan 16, 2021 at 7:27

9 Answers 9

11

In the 12-tone system, A sharp and B flat are "enharmonically equivalent," meaning that they are somewhere between being essentially the same and being literally the same. In any given context it may be more appropriate to use one name or another for that pitch. Without more context, as in the situation you describe, it doesn't much matter which name you use for that note. In that case, it is reasonable to call it A sharp the first time around and B flat the second time.

If the note is part of an actual piece of tonal music then the correct "spelling," as it is known, will depend on the key of the piece, and then it is likely to be more reasonable to use one spelling rather than the other. For example, if the A sharp is in a piece in the key of B major, then it will become a B when you transpose up a half step to C major and A sharp again when you transpose back down to B major. (The key of B major is preferred over C flat major because it has a simpler key signature.)

The one exception to that is the keys of F sharp major and G flat major, since their key signatures have the same complexity, so you could reasonably start with F sharp major, transpose up to G major, and transpose down to G flat major. I that case your A sharp would become a B flat after all.

1
  • Technically A sharp and B flat are only "enharmonically equivalent" on instruments tuned with equal temperament (pianos, fretted stringed instruments, etc...). In music theory they are different notes, would sound differently, and a violinist would play them on slightly different positions on his/her finger board. I gather because of the question's wording, he is talking about equal temperament because A sharp and B flat are being treated the same, so what you say is correct, but it's an important distinction. Jan 19, 2021 at 3:40
5

It's an interesting exercise, and you can learn a lot about programming, math and music theory.

I wrote some small scripts for harmonica/trumpet/guitar in Flash, Ruby and Python.

Some theory

  • Be sure to understand the circle of fifths.
  • Be sure to understand modular arithmetic. +5 semitones is the same as -7 (modulo 12), and applying +5 twice is the same as -2.
  • The most common keys are on top of the circle of fifths. C is the most common, then come F and G, then come B♭ and D, and so on.
  • C# isn't common at all (at the bottom of the circle of fifths), so you'll more often transpose by +5 or -5 semitones than by +1 or -1.
  • Each key is associated with 7 pitches. C major is simply "C, D, E, F, G, A, B". F is "F, G, A, B♭, C, D, E", G is "G, A, B, C, D, E, and F♯" and C# is "C♯, D♯, E♯, F♯, G♯, A♯, B♯".
  • Knowing the key of a piece will tell you which notation you should use: e.g. B♭ instead of A# for the key of F.
  • You can save your notes as a list of integers, representing the semitones. C can be represented as 0 (or 12 or 24...), D as 2 and so on.
  • You can try to determine the key of your melody by applying every offset between 0 and 11, and see if the notes fit inside the corresponding major scale:
    • If all your notes (modulo 12) are included in [0, 2, 4, 5, 7, 9, 11], then your melody can be written in C, without any # or b.
    • Transpose all your notes (modulo 12) by -5, and see if they are included in [0, 2, 4, 5, 7, 9, 11]. It would mean that the original key was F.
    • Transpose by 5, and check again. The key would be G.
    • Transpose by -10. The key would be B♭...
  • It's possible that no key fits exactly. In this case, you'd simply use the key with the least amount of extra # or ♭.
  • You could simply try to be lucky and use the notations for the most common keys : ['C', 'C#', 'D', 'Eb', 'E', 'F', 'F#', 'G', 'Ab', 'A', 'Bb', 'B'].
  • Be sure to keep the original list of notes and the desired transposition in separate variables. That way, +1 followed by -1 is simply a transposition of 0, and you don't need to change anything from the original notation.

Example

Let's say you have a list of notes, saved as

notes = [26,30,33,30,33,31,33,35,37,38,33,30,26,30,33,30,33,31,30,28,26]

without any other information.

Which key?

You can remove duplicates :

[26, 28, 30, 31, 33, 35, 37, 38]

Since we're working modulo 12, 26=38=2 and 28=4, and the unique, sorted notes are:

[1, 2, 4, 6, 7, 9, 11]
  • We already know that this melody isn't in C. (1 and 6 aren't in C major [0, 2, 4, 5, 7, 9, 11]).
  • If we transpose by -5 (or +7, which is the same), we get [8, 9, 11, 1, 2, 4, 6] or, when sorted, [1, 2, 4, 6, 8, 9, 11]. So this melody isn't in F.
  • If we transpose by +5, we get [0, 2, 4, 6, 7, 9, 11]. This melody isn't in G.
  • If we transpose by -10, we get [1, 3, 4, 6, 8, 9, 11]. This melody isn't in Bb.
  • If we transpose by +10, we get [0, 2, 4, 5, 7, 9, 11], the major scale! It means this melody can be written in D, without any accidental.

Writing notes in the original key

D major is written with those notes : D, E, F♯, G, A, B, and C♯.

It means we can write the melody as:

D F# A F# A G A B C# D A F# D F# A F# A G F# E D

or something like

D2 F#2 A2 F#2 A2 G2 A2 B2 C#3 D3 A2 F#2 D2 F#2 A2 F#2 A2 G2 F#2 E2 D2

if you want to show the corresponding octaves.

Transposing to another key

Let's say you want to add 3 semitones. You'd go from D to F.

If you simply add ### to every note, you'd get:

F A C A C A# C D E F C A F A C A C A# A G F

but it's not convenient to have both A and A#.

F major is written with F, G, A, B♭, C, D, and E. So your transposed melody would become:

F A C A C Bb C D E F C A F A C A C Bb A G F

Similarly, transposing by -2 semitones would transpose to C:

C E G E G F G A B C G E C E G E G F E D C

and +1 semitone would transpose to E♭ (note that the notation goes from two sharps to 3 flats):

Eb G Bb G Bb Ab Bb C D Eb Bb G Eb G Bb G Bb Ab G F Eb

Some code

You mention Javascript in the comments. You can define notes as an array:

var notes = [26,30,33,30,33,31,33,35,37,38,33,30,26,30,33,30,33,31,30,28,26];

To transpose, you can simply use map:

notes.map(x => x + 3);
# [29, 33, 36, ...]

modulo works too:

notes.map(x => x % 12);
# [ 2, 6, 9, 6, 9, 7, ...

And you can use a set in order to get unique notes:

new Set(notes.map(x => (x + 10) % 12));
# [ 0, 4, 7, 5, 9, 11, 2]

Have fun!

2
  • 1
    Thanks, Eric. I can't fully grasps most of the answers here because I'm lacking of knowledge in music theory. I'm literally a newbie. But I'll keep coming back here to learn. Appreciate all the links and knowledge you've shared. By the way, I'm just using jquery for my script.
    – enrique
    Jan 17, 2021 at 10:42
  • 1
    @enrique: My pleasure. You might need jquery for the frontend, but for your data and processing, vanilla javascript should do. You basically only need arrays, modulo, map and sets. Jan 17, 2021 at 10:59
3

Afraid it's back to the drawing board ! Generally speaking, yes, when notes are raised in pitch by a semitone, they are called sharp, and when dropped by a semitone, are called flat.

But since it's not merely individual notes that are being altered, doing that to them will not work too well.

In music - particularly tonal music, and especially when it's written down - as yours effectively will be - keys come into play, and each key will contain certain notes with certain names. Main reason is that each key will contain 7 diatonic notes, each having a separate letter name - ABCDEFG. That means for simplicity, each can have a line or space on the stave where it lives. With two A notes, for example - A and A♭ - accidentals would need to be used. Were that A♭ to be known as G♯, which is enharmonic, then each would have its own clearly defined place on the stave.

There's also the problem where two notes are only a semitone apart: B/C and E/F. Moving note B up a semitone far more often ends up with it being C, rather than B♯.

So in your program, it would need to know what the set of notes is called for any diatonic key.Otherwise, as explained above, inaccurate letter names would be produced.

If more clarification is needed, don't hesitate to ask.

3

If we use the circle of fifths to write out all of the notes in each major key we get:

I II III IV V VI VII
Gb Ab Bb Cb Db Eb F
Db Eb F Gb Ab Bb C
Ab Bb C Db Eb F G
Eb F G Ab Bb C D
Bb C D Eb F G A
F G A Bb C D E
C D E F G A B
G A B C D E F#
D E F# G A B C#
A B C# D E F# G#
E F# G# A B C# D#
B C# D# E F# G# A#

Let's put these in chromatic order:

row I II III IV V VI VII
0 Ab Bb C Db Eb F G
1 A B C# D E F# G#
2 Bb C D Eb F G A
3 B C# D# E F# G# A#
4 C D E F G A B
5 Db Eb F Gb Ab Bb C
6 D E F# G A B C#
7 Eb F G Ab Bb C D
8 E F# G# A B C# D#
9 F G A Bb C D E
10 Gb Ab Bb Cb Db Eb F
11 G A B C D E F#

To transpose, figure out what key your song is in, then transpose the key and use this table to map all of the notes.

For example, if you are in C and moving up a semitone, you would translate everything from row 4 above to row 5:

Old New
I C Db
II D Eb
III E F
IV F Gb
V G Ab
VI A Bb
VII B C

If you are in B and you want to move up by a fourth (5 semitones) translate row 3 to row (3+5)%12 (row 8):

Old New
I B E
II C# F#
III D# G#
IV E A
V F# B
VI G# C#
VII A# D#

Any alternative method of manually deciding whether a note should be sharp or flat gets tough. So while your question suggested that C should become C#, this isn't normal because no one works in the key of C#. It's possible, but it's a key with 7 sharps. A more extreme example would be moving from the key of E->F. If we tried to use a method where we:

  1. Add sharps
  2. Resolve double-sharps, B#, and E# to natural notes

We still end up with this:

Orig Add #s Reduce #s
I E E# F
II F# F## G
III G# G## A
IV A A# A# <-- Something is wrong here!
V B B# C
VI C# C## D
VII D# D## E

In this case adding a sharp resulted in a ton of doublesharps which is possible, but awkward. If this were written on a staff it'd have 11 sharps. No musician would look at you as a sane person. To make this more 'normal', we need resolve the double-sharps. E# is equal to F and B# is C, so we start with those. The double-sharps can be reduced. G## is an A, so we move those next. Finally we are left with A#. That may sound fine, but it leaves our scale with an A and an A#, then a gaping hole until we reach C. The written music would use accidentals whenever you played the fourth and would never put a note on the B-line. A# REALLY needs to be rewritten as Bb.

1
  • Good answer, with interesting tables! I've used a similar example (A & Bb to avoid A & A#) in mine. I dare say that the combination of both our answers should help OP. Jan 16, 2021 at 22:03
3

If the current note is A# and the user clicks the "+" icon, the result should be B. But if that user wants to go back by clicking the "-" button, the note becomes Bb?

With the best will in the world, this question shows that, at the moment your knowledge of music theory isn't sufficient even to understand most of the answers given so far. Some of them are actually quite misleading and at least one is wrong.

To illustrate what I mean, on English Language Learners Stack Exchange. Very often there will be a question like,

Which sentence is correct "I go" or I am going"?

What do you answer? The answer is of course that they are both right but which you should use depends on context.

Without being mean, your question is at a similar level. Do you understand that note letter names are not equally spaced in terms of semitones? Do you know that B# is enharmonically equivalent to C? When speaking of transposing, are you aware of the concept of key and how that affects the names of notes? It makes no sense to talk about transposing a single note unless you take into account the key.

I hope I don't come across as unfriendly - I don't mean to. However you really need to understand music theory a little more before you can talk about writing software to perform transposition.


Note: I have Degree-level qualifications in maths and computing and have taught classical guitar at a music college so I'm aware of what is needed for transposition software. The very bare minimum you need to know are

  1. What are the intervals in a major scale in terms of tones and semitones
  2. What it means to transpose from one key to another
  3. How musical keys are notated on the stave with sharps and flats
  4. How this relates to the physical keys on a piano keyboard and the alphabetic names of the notes.

As someone else said, this is a great opportunity to discover about several subjects at once so I encourage you to continue. One of the prime requirements of writing software is that you must be able to understand the theory of the subject you are writing it for as well as having the ability to code. If you know nothing about accounting or compound interest, then don't try to write a financial package!

5
  • Please give reasons for downvoting. Thank you. Jan 16, 2021 at 18:19
  • I kinda agree with your answer, and didn't downvote. Writing this script could be an excellent way for OP to learn a lot about music theory, notation, math and programming, though. "Practice makes perfect", and all that. I just hope OP's not working on Finale. Jan 16, 2021 at 18:35
  • @Eric Duminil - I know this is off-topic and not allowed as a question on Music SE but, do you have a recommendation for software that allows composition on the stave with good quality printing but with access to a sophisticated library of sampled instruments? I'm thinking of a DAW but one that is biased toward high quality stave input and display. Jan 16, 2021 at 20:27
  • 1
    Sorry, I don't know anything about DAWs. For printing staves, I've had good experience with lilypond.org and musescore.org/en, I suppose you know them already. Jan 16, 2021 at 21:00
  • @Eric Duminil - Thanks. I'll take a look at them. Jan 17, 2021 at 11:37
1

My current understanding is when you go up by one semitone, the notes became sharps. Go down by one semitone, the notes in that become flats.

This is correct:

semitone up => adding a sharp, (notes that are already augmented by a sharp become a double sharp = X)

But if you are e.g. in A#-minor and you want to go a semitone up you better transpose the key of the piece a semitone up => B-minor.

That's exactly the opposite function that happens if you transpose a semitone down:

Sharps will become naturals, natural tones become flattened, and already flattened become a double flat.

In 12 tone music everything is quite different, you can decide yourself how you want to define the black keys.

In the music program Finale you have 4 options of harmonic speling:

  • Use default spelling (according to the key of the piece)
  • Favor sharps (important when the piece is in minor or modulating in dominant regions)
  • Favor flats
  • Use spelling tables

So you can decide whether you want transpose from B-minor to Bb-minor or A#-minor.

Examples:

G => Gb (semitone down): all notes have on flat, F# becomes natural F (and vice versa.)

F => F# (semitone up): alle notes have a sharp, Bb becomes natural B (and vice versa)

F => Gb would be F => G (whole tone upe) and G => Gb (semitone down)

Mind that in the sharp keys (right side of the circle of fifths) sharpened notes will become double sharps when augmented, and in flat keys (left side of c.o.f.) flats become natural, not altered become sharps.

The same logic is applied when notes in the different kind of keys are flattened.

2
  • 1
    This is incorrect or at least inadequate. If I raise B, by a semitone, I get C. Yes it can also be called B#, at least on a piano, but you neglect to mention that note names differ by inconsistent intervals. Jan 16, 2021 at 17:32
  • Of course, but it I wouldn't make the things even more confusing. My exemple of A#-minor raising to B implies this case. You can of course theoretically transpose to B# by adding to to B major a double sharps, But this practically nonsense (like augmenting E-major to E# or F# to Fx). But for accidentals the rule is practicable. Jan 16, 2021 at 17:42
1

There are a lot of theory answers here and so I'm going to avoid that. I will discuss what you are doing in terms of web development. I built the site The Internet Chord Database where I do a ton of transposing and modulation. When doing so, one of the major things is getting note-names correct: should I display A# or B-flat or C-double-flat? It's a simple question with a complicated answer and an even more complicated way to implement when building software. Here's a simplified explanation.

You need to store, either in objects in memory or in a database (as the ICDb does) these musical constructs: First the basics:

  • Intervals. You have to store information on all of the intervals (M2, m2, M3, m3, P4, etc...)
  • Pitches. These do not have note names assigned to them. Instead, they are the hertz of each possible pitch played on an equal temperament instrument.

Next you need:

  • Scales. These are simply sequences of intervals so you need a relationship between those two tables. The ICDb supports Majors and the 3 types of Minors.
  • Keys. You need a Key record for each key signature you want to support. These are a Pitch record (the root of the key) and a Scale record (a sequence of intervals).

Now that you have a Key record, you know which pitches belong in its scale. Thus, you will know which note-name to display for a given pitch (hertz). If you go up a m2, then back down a m2, you know which is the destination pitch name.

Hope this helps.

0

According to the general naming convention, yes, starting from B, then clicking "-" would result in Bb. A# and Bb are known as "enharmonic equivalents", representing the same pitch.

In the context of the computer program described, the note could simply be named "A#/Bb", or the program could keep track of the user's most recent clicks and rename the note A#.

There is a further issue, which is that starting from A# and clicking "+" could also be correctly called Ax (A double-sharp). Theoretically there could be triple and quadruple sharps, but in practice, only double sharps (and, similarly, double flats) are used.

Notes are named according to the musical context in which they appear, so for the purposes of a computer program that just moves notes up and down, it would be best to pick a convention and stick to it.

My personal recommendation would be to use A A#/Bb B C C#/Db D D#/Eb etc.

2
  • The policy stated above is inadequate because the music notation needs to show an actual pitch, an A# or a Bb for example. The software needs to transpose correctly so that it picks the correct pitch. Displaying a Bb when the music calls for an A#, or vice versa, would be incorrect. "A#/Bb",is not a correct name, and does not correspond to any correct music notation.
    – Rosie F
    Jan 19, 2021 at 9:54
  • @RosieF All the OP is trying to do is write a simple script such that the user enters a letter name and clicks a + or - button, with the script returning an appropriate letter name. Frankly, I'm utterly baffled at the answers that seem to think OP is trying to write a transposition or notation program.
    – Aaron
    Jan 19, 2021 at 11:32
0

I'm confused on how to transpose notes...If the current note is A# and the user clicks the "+" icon, the result should be B...

You're going to run into a lot of trouble being new to music theory and trying to write a music notation program.

The scenario you describe - raising or lowering a pitch - in not transposition. That may seem nit-picky, but it isn't. If you had a minor chord, for example A minor, pitches A C E and you wanted to make it a major chord, you would raise the C to C#. That matches the scenario you describe, but the process is not transposing.

Transposing is when you move a set of pitches or the pitches of an entire composition. There are two types: diatonic and chromatic transposition. Transposition can be complicated when trying to preserve a sense of key. For example if you transposed down one half step, you would probably move A minor to G# minor where the letter of the tonic changes, but moving E major down a half step you would probably move it to Eb major where the letter doesn't change. You would want to understand the reason why that is so before trying to write a transposition program.

If you just want to have a function to raise of lower pitches, you could follow this common convention: when the pitches ascend use sharps, when they descend use flats. You could then set a limit of one sharp or flat and always use natural pitches when available. So, ascending from E would be E F F# G G#... and descending from G# would be G# G Gb F E.

If you really want to transpose, I suggest first trying a purely diatonic function. Set a palette of seven diatonic pitches, then make make a series of pitches with octave numbers, for example C4 G4 F4 E4 D4 E4, then transpose not by half steps but diatonic steps, for example transposing down one step would be B3 F4 E4 D4 C4 D4.

It's probably best to stick to theory you know well. Otherwise how can you evaluate the programming? You can't write a program if you don't know what the output is supposed to be. A programming exercise can be a good way to delve more deeply into a theory topic. But you will probably need to go to a textbook or scores to clarify some questions, so you might as well get a good handle on theory first then program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.