What are the best general algorithms to determine the correct notes (with sharp/flat) as they appear on a sheet with only input a MIDI-style data that has only the designation of a white or black key on keyboard?


  1. While knowing which software exists that perform the task is definitely is interesting, that question is about how this is done. The question is about programming (algorithms) and music, not how to get the work done for the end user in practice.
  2. Knowing no method is perfect nor can be guaranteed to work in all cases, the question is about the best methods.
  • 1
    What do you mean by algorithm? There are a lot of software applications that can display MIDI data on a staff. Is that what you're looking for? – Peter Jul 19 '19 at 2:09
  • @Peter I believe these programs always map one key on the keyboard to one note name. This is not what I'm looking for: I want a program capable of finding the sharps and the flats in general, as close as possible to the original sheet. – curiousguy Jul 19 '19 at 2:13
  • 1
    Normally a human figures out the key signature, but you could easily write something that looks at all the accidentals in a MIDI file and then consults a table of key signatures to find the best fit. It's not much of an algorithm, just a simple search of a very small data set (there are normally only 15 different key signatures used in music notation). – Todd Wilcox Jul 19 '19 at 3:39
  • 1
    I am assuming what OP means is for passages like D#-E-E♭ to be spelt "properly" (instead of D#-E-D# or E♭-E-E♭). Most professional applications do that, I think. Including MuseScore and many others. – Pyromonk Jul 19 '19 at 13:12
  • 1
    @ToddWilcox 13, really, since seven sharps is enharmonically equivalent to five flats, and seven flats is enharmonically equivalent to five sharps. And six sharps is the same as six flats, so you can actually eliminate one of those for a total of 12, unless you want to pick randomly. – phoog Jul 22 '19 at 18:56

This is not an easy problem.

Simple methods based on "knowing the key signature" only work for simple music. Even in straightforward common-practice harmony, secondary dominants and chromatic passing notes can quickly introduce more than 12 functionally correct spellings of the 12 notes in the chromatic scale within a single piece of music in a major key. For pieces in minor keys the problem is greater, since there is usually a greater diversity of major and minor chords with the same root note.

Simple methods don't work when confronted with music as harmonically straightforward as for example Mendelssohn's well known "Wedding march" - where the second chord of a piece in C major is actually B7 (i.e. B dominant seventh, not B diminished)

As a example of the ambiguities that have to be resolved, the note spellings F - A flat - B or F - A flat - C flat are both "correct" in C minor, in different situations. One is functionally a (partial) dominant chord with root G, the other is functionally part of a Bb chord with a flat 9th, which could be a secondary dominant of Eb.

A good source of evidence that this is a hard problem is the fact that many commercial notation programs make a complete mess of it, requiring much hand editing to fix the mistakes - or publishing a score which makes it obvious that its author doesn't understand music notation. The following example was recently put in front of a professional rehearsal pianist, playing for auditions for a music theatre production in the capital of a first-world country...

enter image description here

Within two bars of supposedly F major, we have a C chord spelled with B sharp instead of C, followed by an so-called A sharp major chord (but with D natural instead of C double sharp) instead of B flat! And that isn't an isolated mistake - the whole score is similar nonsense.

One of the best algorithms for doing this right is the so-called "CS13" by David Meredith. It works by identifying both the "local key" (which is not necessarily the same as the key signature) and the voice leading of adjacent notes. A full description and examples (50 pages long) is here.

| improve this answer | |
  • Fascinating, where do you believe the B# is coming from? – curiousguy Jul 20 '19 at 19:53
  • Awesome reference. – guidot Jul 22 '19 at 19:55

There are options, but you need a few things first.

  • Any DAW or MIDI editing software

You need to be able to edit the MIDI. In my case this is a DAW (Cubase). You need to delete all automations from the MIDI like Modulation, Pitchbend, Expression etc. because this is data the notation software doesn't need. It would receive all the information and would be confused with most automation or spam to whole score with trying to replicate the automation.

The next step is that you have to bring all the notes to the exact note value (length). They have to start and also have to end exactly on the beat you want. If you try to import a live played piano MIDI file into a notation program, it will create endless tied notes with 32th notes over the bars, because the notation software of course can't know that it's not intended. There are notation software where you can say that the maximum note value is 8th notes for example, but that doesn't always work... So the first mentioned way is the only safe option.

  • Any notation software with MIDI import

Once you've done all that, you can import the MIDI into a notation software (In my case it's Sibelius). There you'll have to set the instrument it is and of course double check or play the whole thing if everything got imported correctly. Then you have to add the dynamics and articulations manually (things like legato, staccato etc.).

Some notation software have also a plug-in included that respell enharmonic notes to the corresponding key signature. So if you are for example in any key signature with A#, but after the import it shows all notes in Bb, it would respell them to A# correctly.

... So yea, there is an option. But it's for sure not just a drag and drop style. It will always take you some time to do that and also the knowledge to check everything manually and do it correctly.

| improve this answer | |

is there a general reliable algorithm to determine the correct notes (with sharp/flat)

No - because there is no absolute, 100% agreed-upon definition of what 'correct' note spellings would be in every situation.

Don't get me wrong - there's a large degree of agreement (otherwise standard notation wouldn't be as useful, and usable, as it is). But there is sometimes disagreement as to what overall key signature would be best to use for a given piece, and then there are a lot of different considerations relating to how to use accidentals - take a look at Using the correct enharmonic equivalent, and the Wikipedia page for accidentals (which even opines that there are two different systems for using accidentals).

Even if someone made an algorithm that seemed very good and to cope with a lot of situations very well, there would be no way to prove it correct, as that definition of 'correct' doesn't exist. (If it did, it would be an expression of the algorithm!).

If your problem extends to every aspect of making 'proper sheet', we also haven't dealt with how to work out notated rhythms from timing values either; I suspect this is a much harder problem!

There's ultimately no 1:1 correspondence between score and performance, in either direction - just as a score could be interpreted into a performance in more than one way, the reverse is also true.

| improve this answer | |

Each key of a keyboard has a pitch number in midi data (1-128). Midi doesn’t know whether sharp or flat.The enharmonic definition has to be defined by the user. But usually a software has a feature or an option to find out the key or to choose the key by the user ( e.g. major minor and sharp or flat priority) for reconstructing the music according the defined key.

| improve this answer | |
  • 3
    Actually there appears to exist a key signature event in the midi spec. ie the spec is at least theoretically higher level than just 128 pitch numbers. Whether it is used much in practice, I don't know. – Rusi Jul 19 '19 at 10:34
  • @Rusi even so, the key signature event can't remove ambiguity. For example, is the pitch four whole steps above the tonic of a major key a raised fifth or a lowered sixth? Verdi's Requiem mass, starting at measure 52 of the Introit, has these pitches in the bass in a key signature of one flat (F major): B♭ B♮ C C♯ D D♭ C F. There are certainly good reasons for the first instance of midi note 49 being a C♯ while the second is a D♭, but the logic to make that choice correctly could easily be incorrect in other contexts. – phoog Jul 22 '19 at 19:22

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