Ability to Transpose via CLI

• Apr 17, 2019 - 22:00

This is probably a bit on the ridiculous side of things, but I figured it wouldn't hurt to ask.

I'd like to be able to transpose a score and its parts via the command-line interface, and export the result to a separate file. So if I provided a score that was in like, Concert Bb, I could say:

musescore -i SongName.mscz -transpose-up D -o SongNameInConcertD.mscz

which would transpose the entire score up a third, or

musescore -i SongName.mscz -transpose-down D -o SongNameInConcertD.mscz

which would transpose the entire score down a minor 6th.


Comments

In reply to by Marr11317

If that repo is yours, then take another look at the git workflow.
You should never merge the upstream (MuseScore) master into your own master branch, but always rebase/fast-forward it.
If not, chances are any of your future PRs won't work because of a different history.

A command-line option was added to MuseScore for this purpose just last week. It was really only intended to be used by the server rather than actual users, as the output requires some extra processing, but with the help of a python script (attached below), anyone can make use of it.

On my system, I can use the following command to transpose a score named Canon_in_D.mscz to the key of E:

mscore Canon_in_D.mscz --score-transpose '{"mode": "by_key", "direction": "up", "targetKey": 4, "transposeKeySignatures": true}' | ./extract.py Canon_in_E.mscz Canon_in_E.pdf

This works for me because
- mscore has been aliased to the full path of the mscore executable.
- extract.py resides in the current directory, and has been given execute permissions.
- python is in /usr/bin.

The --score-transpose option to mscore takes one argument, which is a JSON string. Double quotes are required around all keys and string values. Do not use quotes around integer or boolean values. The entire JSON string is enclosed in single quotes in order for it to be interpreted as one string.

"mode" can be "by_key", "by_interval", or "diatonically". This may not be omitted, as there is no default value.

"direction" can be "up", "down", or "closest". This may not be omitted, as there is no default value.

If "mode" is "by_key", then you must specify a "targetKey". This is an integer from -7 (7 flats) to 7 (7 sharps).
-7 = C flat major / A flat minor
-6 = G flat major / E flat minor
-5 = D flat major / B flat minor
-4 = A flat major / F minor
-3 = E flat major / C minor
-2 = B flat major / G minor
-1 = F major / D minor
0 = C major / A minor
1 = G major / E minor
2 = D major / B minor
3 = A major / F sharp minor
4 = E major / C sharp minor
5 = B major / G sharp minor
6 = F sharp major
7 = C sharp major

If "mode" is "by_interval" or "diatonically", then you must specify a "transposeInterval". This is an integer from 0 to 25:
0 = Perfect Unison
1 = Augmented Unison
2 = Diminished Second
3 = Minor Second
4 = Major Second
5 = Augmented Second
6 = Diminished Third
7 = Minor Third
8 = Major Third
9 = Augmented Third
10 = Diminished Fourth
11 = Perfect Fourth
12 = Augmented Fourth
13 = Diminished Fifth
14 = Perfect Fifth
15 = Augmented Fifth
16 = Diminished Sixth
17 = Minor Sixth
18 = Major Sixth
19 = Augmented Sixth
20 = Diminished Seventh
21 = Minor Seventh
22 = Major Seventh
23 = Augmented Seventh
24 = Diminished Octave
25 = Perfect Octave

"transposeKeySignatures" may be true or false. If omitted, it defaults to false, which is probably not what you want if you are transposing the score to a new key.

There is also a "transposeChordNames" option, which may be true or false. If omitted, it defaults to false.

Finally, there is a "useDoubleSharpsFlats" option which may be true or false. If omitted, it defaults to false.

The extract.py script takes output from mscore --score-transpose and saves the transposed score to a file with the name that you specify as an argument. The score data is available in pdf and mscz formats. You can choose to save either the mscz or the pdf, or both.

Attachment Size
extract.py.txt 575 bytes

In reply to by mattmcclinch

If you use #!/usr/bin/env python in your python script, it would be independant of where python is living, as long as it is in PATH

I wonder why that addition to MuseScore hasn't been coded to allow for -o outputfile though, thus not needing external help.

This "by_key" reminded me to add this to my PR https://github.com/musescore/MuseScore/pull/5617 (to fix #299727: Change "Transpose by key" to "Transpose to key")

Do you still have an unanswered question? Please log in first to post your question.