TAB to MIDI is unique, MIDI to TAB is not!
Your mapping from midi note to tablature is correct in itself, but it lacks a critical point: Tablature, as a representation of fretting, is not unique, when the input is staff notation or midi.
On 12-tone-equal-temperament (12TET) instruments, when the number of frets on a string exceeds the interval in semitones between the root notes (open strings) of this string and the next one, fretting is not unique.
The same notes can be produced on different strings.
The mandolin's strings are tuned to intervals of fifths; i.e. the open strings are 7 semitones apart.
Therefore the open A-string gives the same note as the 7th fret on the D-string.
This is noted in How to find equivalent notes on different strings of a mandolin? .
Many notes can be played in two or three different ways on the mandolin and these differences are not captured by midi encoding or staff notation, but they are represented in tablature.
This is the actual strength of tablature: The choice of where to fret a note is already made for you.
This is why tablature is not a redundant ornament in combination with staff notation.
In addition, on 12TET instruments, tablature screens you from enharmonic change (whether that be good or bad).
Only some of the notes in the ambitus of a 12TET string instrument are uniquely mapped to tablature, since they can only be played on one string respectively.
The notes on the lowest string, including the lowest open string, up to excluding the root note of the second lowest string are unique.
And the ones on the highest string which exceed the range of the second highest string are also unique.
Whenever these notes occur in the midi source their unique representation in tablature must be the result.
On the mandolin, the low uniquely-tablatable notes are g through cis', in the 0th through 6th fret on the G-string; d' in the 7th fret is doubled by the D-string, so it is not unique.
The high unique ones depend on how many frets the A- and E-string have. This varies from one instrument to another.
Above the 20th or 24th fret, the notes are hardly playable or musically usable (or enjoyable). But some instruments have functional (practically decorative) frets even above that range.
Good and bad tablature
When transcribing from midi to tablature you have to fix a degree of freedom.
As written before: Tablature notates the choice of where to fret a note. And this choice should be made wisely!
Whether a certain fretting of a note is good or bad depends strongly on the context of the surrounding music.
Aspects of aesthetic, practicality, and realizaibility come into play.
The timbre of open strings is noticeably different from the timbre of fretted notes.
Hence open strings may be avoided for aesthetics.
At the same time the timbre of different strings can differ a lot, too, and legato is harder to achieve when strings are crossed ("crossing strings" means going from one string to another).
Hence open strings may be seeked for aesthetics.
Consequentially open strings are sometimes avoided, sometimes not.
Avoiding string crossings and achieving a different timbre is one reason for playing in other positions than the 1st position.
Playing in positions means covering a certain range of the fretboard with your left-hand fingers, see https://music.stackexchange.com/a/56209/54823 .
The mapping from midi-notes to tablature you provided in your question is in the so called 1st position.
Precicely 1st position with open strings and without the 7th fret.
In 2nd position, the left hand placed differently on the neck.
The fingers stop the strings in different frets, which means the mapping from notes to tablature is different.
In 2nd position the map is:
const mandolin_2nd_pos = {
//'G4': { position: 0, string: 4 },
//'G#4': { position: 1, string: 4 },
//'A4': { position: 2, string: 4 },
'A#4': { position: 3, string: 4 },
'B4': { position: 4, string: 4 },
'C5': { position: 5, string: 4 },
'C#5': { position: 6, string: 4 },
'D5': { position: 7, string: 4 },
'D#5': { position: 8, string: 4 },
'E5': { position: 9, string: 4 },
'F5': { position: 3, string: 3 },
'F#5': { position: 4, string: 3 },
'G5': { position: 5, string: 3 },
'G#5': { position: 6, string: 3 },
'A5': { position: 7, string: 3 },
'A#5': { position: 8, string: 3 },
'B5': { position: 9, string: 3 },
'C6': { position: 3, string: 2 },
'C#6': { position: 4, string: 2 },
'D6': { position: 5, string: 2 },
'D#6': { position: 6, string: 2 },
'E6': { position: 7, string: 2 },
'F6': { position: 8, string: 2 },
'F#6': { position: 9, string: 2 },
'G6': { position: 3, string: 1 },
'G#6': { position: 4, string: 1 },
'A6': { position: 5, string: 1 },
'A#6': { position: 6, string: 1 },
'B6': { position: 7, string: 1 },
'C7': { position: 8, string: 1 },
'C#7': { position: 9, string: 1 },
}
The lines with 'G4'
, 'G#4'
, 'A4'
, are still the same.
Those notes have unique tablature so they never change.
I have commented them out because they cannot be fingered in 2nd position, but this does not affect the tablature.
[ A note on fingering:
In contrast to tablature, fingering notates which finger is used for fretting.
In general this does not prescribe which string is fretted.
Hence tablature and fingering indications are independent to a certain degree and not fully redundant.
But often the combinatorics are manageable and some combinations are much more reasonable than others.
]
Additionally, D5
, D#5
, E5
that are on string: 3
in 1st position are in higher frets on string: 4
in 2nd position.
The ranges covered on the other strings are shifted accordingly.
The minimum fret in 2nd position is the 3rd fret, i.e. all strings in the map start with position: 3
.
Note that there are yet two more 'positions' between 1st and 2nd position;
they are not given names (some might call them 'half position'), but they are completely legit to use and they contribute to tablaure.
Another reason to leave the 1st position is ease of playing, especially in fast or high passages.
The argument, that an open string needs to be muted, is important, yet an open string can buy you a split second of time to move your left hand when changing position.
Hence clever usage of open strings invalidates the rule that fret numbers stay above a certain minimum in certain left-hand positions.
To avoid open strings entirely is certainly bad advice.
When several notes sound simultaneously to form chords, the tablature has to be at least physically possible to play.
A string can only produce one note at a time and tablature must respect this fact.
[
Split-string technique is a rather advanced method in which the two courses of a mandolin string are fretted independently.
This way, a string can produce two notes. Theoretically a chord of 8 notes could be played; 4 of them are g d' a' e''.
The technique is quite tricky on instruments where the strings are tightly spaced and
the effect is rather underwhelming, considering the effort it takes.
]
When it comes to automated generation of tablature from midi, I see two options:
Either you need to have previous knowledge about the tunes you are going to process.
For example you know that they will all be exclusively in first position.
This is often the case for tunes in tutor books on beginner's level.
Yet, even in theese books the fluent use of both, open strings and the 7th fret, is usually encouraged early on.
Some books even elaborate on heuristics of when to use open strings and when the 7th fret.
Otherwise, if the automation scheme needs to get along without previous knowlege, you need to incorporate a lot of complexity into your automation scheme.
You could try to gather some heuristics into an algorithm, with the aim of automatedly generating tablatures, which are mostly acceptable.
This is not as bad as it sounds. Much like fingering indications, tablature is often mere technical advice, when it is combined with staff notation.
For many medium-difficulty tunes 'mostly acceptable' should be identical to 'optimal'.
If you have access to a very large corpus of well thought-through tablature, you could also try to use machine learning.
As Todd Wilcox pointed out: Alternative tunings and the use of capodasters are both rare on the mandolin. Both would change the tablature.
However, the real complexity of tablature lies not in figuring out how notes are mapped to 1st-position frets, but in aesthetically and practicably choosing which note to play on which string.
A complex example
Here is an example of some of the complexities one might encounter in autogeneration of tablature:
It is an excerpt from measure 9 of the first voice of Al paño fino with tablature, as I play it on the Mandolin.
The piece was arranged for two guitars by H. J. Teschner and published by Acoustic Music GmbH & Co. KG, Osnabrück.

Since the accented eighth notes (quavers, c b a gis) are required to ring on, while the sixteenth notes (always b) are played,
the eighths and sixteenths cannot be played on the same string. Therefore nontrivial fretting is required.
Measure 9 could theoretically also be played 10–2 instead of 3–9, but that would be too much of a stretch for my left hand.
In measure 10 the alternatives 2–9 and 9–2 may work equally well; I choose to continue the right-hand picking pattern from measure 9, thus fretting 2–9.
This way, the fretting and picking in measure 9 helps preparing measure 10, which requires a wider stretch of the left hand, but no further change.
In view of how I fret the following measures, 9–2 would be reasonable as well;
but stretching my left hand this far and switching the pattern in the right hand at the same time would be too much effort.
In measure 11 one might play 0-9, keeping the small finger in the 9th fret of the D-string and sticking to the picking pattern.
Instead I play 7-2, keeping the index finger in the 2nd fret of the A-string and switching the picking pattern.
I prefer 7–2 over 0–9 because the sound is more uniform, when the open string (0) is avoided.
If I would choose 0–9 here, the choice of 2–9 in measure 10 would be reinforced and 9–2 would become absurd there.
Finally, the fretting in measure 12 is without reasonable alternative.
This also dictates that picking has to change eventually, legitimating the earlier change in measure 11.
My tablature is not a definitive one and I do not claim that it is optimal by any regard.
I have only tried to demonstrate which amount of thought can go into tablature with a real-world example.
I am courious about wheter someone has already tried to automate the generation of tablature at this level of complexity.