forked from gregchapman-dev/musicdiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
254 lines (231 loc) · 8.54 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# ------------------------------------------------------------------------------
# Purpose: __main__.py is a music file comparison tool built on musicdiff.
# musicdiff is a package for comparing music scores using music21.
# Usage:
# python3 -m musicdiff filePath1 filePath2
#
# Authors: Greg Chapman <[email protected]>
# musicdiff is derived from:
# https://github.com/fosfrancesco/music-score-diff.git
# by Francesco Foscarin <[email protected]>
#
# Copyright: (c) 2022-2025 Francesco Foscarin, Greg Chapman
# License: MIT, see LICENSE
# ------------------------------------------------------------------------------
import sys
import argparse
from musicdiff import DetailLevel
from musicdiff import diff
# ------------------------------------------------------------------------------
'''
main entry point (parse arguments and do conversion)
'''
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog='python3 -m musicdiff',
description='Music score notation diff (MusicXML, MEI, Humdrum, etc)'
)
parser.add_argument(
"file1",
help="first music score file to compare (any format music21 can parse)"
)
parser.add_argument(
"file2",
help="second music score file to compare (any format music21 can parse)"
)
parser.add_argument(
"-i",
"--include",
default=["allobjects"],
nargs="*",
choices=[
"decoratednotesandrests",
"otherobjects",
"allobjects",
"style",
"metadata",
"voicing",
"notesandrests",
"beams",
"tremolos",
"ornaments",
"articulations",
"ties",
"slurs",
"signatures",
"directions",
"barlines",
"staffdetails",
"chordsymbols",
"ottavas",
"arpeggios",
"lyrics"],
help="included details (can include multiple details)"
)
parser.add_argument(
"-x",
"--exclude",
default=[],
nargs="*",
choices=[
"decoratednotesandrests",
"otherobjects",
"allobjects",
"style",
"metadata",
"voicing",
"notesandrests",
"beams",
"tremolos",
"ornaments",
"articulations",
"ties",
"slurs",
"signatures",
"directions",
"barlines",
"staffdetails",
"chordsymbols",
"ottavas",
"arpeggios",
"lyrics"],
help="excluded details (can exclude multiple details)"
)
parser.add_argument(
"-o",
"--output",
default=["visual"],
nargs="*",
choices=["visual", "v", "text", "t", "ser", "s"],
help="'visual'/'v' is marked up scores, rendered to PDFs;"
+ " 'text'/'t' is diff-like, written to stdout;"
+ " 'ser'/'s is the symbolic error rate (symbol errors/total symbols),"
+ " written to stdout."
+ " Any, all, or none of these can be requested."
)
parser.add_argument(
"--fix_first_file_syntax",
action='store_true',
help="If set, syntax errors in the first input file will be fixed"
+ " (if possible) so the diff can continue. Any fixes will be"
+ " added to the returned cost in symbol errors). Note that errors"
+ " in the second file (assumed to be the ground truth) are never"
+ " corrected. Note also that this currently only works for Humdrum"
+ " **kern files."
)
args = parser.parse_args()
detail: int = DetailLevel.Default
if args.include:
detail = 0
for det in args.include:
# combos
if det == "decoratednotesandrests":
detail |= DetailLevel.DecoratedNotesAndRests
elif det == "otherobjects":
detail |= DetailLevel.OtherObjects
elif det == "allobjects":
detail |= DetailLevel.AllObjects
# bits not in any combo
elif det == "style":
detail |= DetailLevel.Style
elif det == "voicing":
detail |= DetailLevel.Voicing
elif det == "metadata":
detail |= DetailLevel.Metadata
# bits in the DecoratedNotesAndRests combo
elif det == "notesandrests":
detail |= DetailLevel.NotesAndRests
elif det == "beams":
detail |= DetailLevel.Beams
elif det == "tremolos":
detail |= DetailLevel.Tremolos
elif det == "ornaments":
detail |= DetailLevel.Ornaments
elif det == "articulations":
detail |= DetailLevel.Articulations
elif det == "ties":
detail |= DetailLevel.Ties
elif det == "slurs":
detail |= DetailLevel.Slurs
# bits in the OtherObjects combo
elif det == "signatures":
detail |= DetailLevel.Signatures
elif det == "directions":
detail |= DetailLevel.Directions
elif det == "barlines":
detail |= DetailLevel.Barlines
elif det == "staffdetails":
detail |= DetailLevel.StaffDetails
elif det == "chordsymbols":
detail |= DetailLevel.ChordSymbols
elif det == "ottavas":
detail |= DetailLevel.Ottavas
elif det == "arpeggios":
detail |= DetailLevel.Arpeggios
elif det == "lyrics":
detail |= DetailLevel.Lyrics
if detail != 0 and args.exclude:
for det in args.exclude:
# combos
if det == "decoratednotesandrests":
detail &= ~DetailLevel.DecoratedNotesAndRests
elif det == "otherobjects":
detail &= ~DetailLevel.OtherObjects
elif det == "allobjects":
detail &= ~DetailLevel.AllObjects
# bits not in any combo
elif det == "style":
detail &= ~DetailLevel.Style
elif det == "voicing":
detail &= ~DetailLevel.Voicing
elif det == "metadata":
detail &= ~DetailLevel.Metadata
# bits in the DecoratedNotesAndRests combo
elif det == "notesandrests":
detail &= ~DetailLevel.NotesAndRests
elif det == "beams":
detail &= ~DetailLevel.Beams
elif det == "tremolos":
detail &= ~DetailLevel.Tremolos
elif det == "ornaments":
detail &= ~DetailLevel.Ornaments
elif det == "articulations":
detail &= ~DetailLevel.Articulations
elif det == "ties":
detail &= ~DetailLevel.Ties
elif det == "slurs":
detail &= ~DetailLevel.Slurs
# bits in the OtherObjects combo
elif det == "signatures":
detail &= ~DetailLevel.Signatures
elif det == "directions":
detail &= ~DetailLevel.Directions
elif det == "barlines":
detail &= ~DetailLevel.Barlines
elif det == "staffdetails":
detail &= ~DetailLevel.StaffDetails
elif det == "chordsymbols":
detail &= ~DetailLevel.ChordSymbols
elif det == "ottavas":
detail &= ~DetailLevel.Ottavas
elif det == "arpeggios":
detail &= ~DetailLevel.Arpeggios
elif det == "lyrics":
detail &= ~DetailLevel.Lyrics
visualize_diffs: bool = "visual" in args.output or "v" in args.output
print_text_output: bool = "text" in args.output or "t" in args.output
print_ser_output: bool = "ser" in args.output or "s" in args.output
fix_first_file_syntax: bool = args.fix_first_file_syntax is True
cost: int | None = diff(
args.file1,
args.file2,
detail=detail,
visualize_diffs=visualize_diffs,
print_text_output=print_text_output,
print_ser_output=print_ser_output,
fix_first_file_syntax=fix_first_file_syntax,
)
if cost is None:
print('musicdiff failed.', file=sys.stderr)
elif cost == 0:
print(f'Scores in {args.file1} and {args.file2} are identical.', file=sys.stderr)