/
Source.java
402 lines (341 loc) · 12.9 KB
/
Source.java
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//------------------------------------------------------------------------------------------------//
// //
// S o u r c e //
// //
//------------------------------------------------------------------------------------------------//
// <editor-fold defaultstate="collapsed" desc="hdr">
//
// Copyright © Audiveris 2021. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify it under the terms of the
// GNU Affero General Public License as published by the Free Software Foundation, either version
// 3 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with this
// program. If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------------------------//
// </editor-fold>
package org.audiveris.omr.score;
import org.audiveris.omr.sheet.SystemInfo;
import org.audiveris.proxymusic.Identification;
import org.audiveris.proxymusic.Miscellaneous;
import org.audiveris.proxymusic.MiscellaneousField;
import org.audiveris.proxymusic.ObjectFactory;
import org.audiveris.proxymusic.ScorePartwise;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
/**
* Class <code>Source</code> precisely describes the source of a Score.
* <p>
* Within a given input file (or URI), perhaps composed of several sheet images, the relevant sheets
* are listed, each with the systems processed for the ScorePartwise instance at hand.
* <p>
* A sheet with no system processed (for example because it is made of text only), could explicitly
* appear but with an empty sequence of system numbers (this is recommended), or the sheet could not
* appear at all in this source structure.
* <p>
* In the following example, the Score spans the first 3 sheets of the input file, with
* sheet #2 being "empty", and only the first system in sheet #3.
* Another Score instance, typically representing a following movement, could start with the
* same sheet #3, but from system 2.
* <p>
* In MusicXML, such Source data is encoded using the miscellaneous element.
* <br>
* Using file:
* <pre>
* <miscellaneous>
* <miscellaneous-field name="source-file">D:\soft\scores\morphology\recordare\MozartTrio.png</miscellaneous-field>
* <miscellaneous-field name="source-offset">4</miscellaneous-field>
* <miscellaneous-field name="source-sheet-1">1 2</miscellaneous-field>
* <miscellaneous-field name="source-sheet-2"></miscellaneous-field>
* <miscellaneous-field name="source-sheet-3">1</miscellaneous-field>
* </miscellaneous>
* </pre>
* <p>
* Using uri:
* <pre>
* <miscellaneous>
* <miscellaneous-field name="source-uri">file:///MozartTrio.png</miscellaneous-field>
* <miscellaneous-field name="source-offset">4</miscellaneous-field>
* <miscellaneous-field name="source-sheet-1">1 2</miscellaneous-field>
* <miscellaneous-field name="source-sheet-2"></miscellaneous-field>
* <miscellaneous-field name="source-sheet-3">1</miscellaneous-field>
* </miscellaneous>
* </pre>
*
* @author Hervé Bitteur
*/
public class Source
{
//~ Static fields/initializers -----------------------------------------------------------------
private static final Logger logger = LoggerFactory.getLogger(Source.class);
private static final String SOURCE_PREFIX = "source-";
private static final String SHEET_PREFIX = "sheet-";
//~ Instance fields ----------------------------------------------------------------------------
/** Path to source image file, if any. */
private String file;
/** Source image URI, if any. */
private URI uri;
/** Systems processed in each image sheet. */
private final List<SheetSystems> sheets = new ArrayList<>();
//~ Methods ------------------------------------------------------------------------------------
//------------//
// encodePage //
//------------//
/**
* Encode provided page in target ScorePartwise.
*
* @param page input
* @param scorePartwise output
*/
public void encodePage (Page page,
ScorePartwise scorePartwise)
{
final ObjectFactory factory = new ObjectFactory();
Identification identification = scorePartwise.getIdentification();
if (identification == null) {
identification = factory.createIdentification();
scorePartwise.setIdentification(identification);
}
Miscellaneous misc = identification.getMiscellaneous();
if (misc == null) {
misc = factory.createMiscellaneous();
identification.setMiscellaneous(misc);
}
Source.SheetSystems sheetSystems = new Source.SheetSystems(
page.getSheet().getStub().getNumber());
sheets.add(sheetSystems);
for (SystemInfo system : page.getSystems()) {
sheetSystems.getSystems().add(system.getId());
}
MiscellaneousField field = factory.createMiscellaneousField();
misc.getMiscellaneousField().add(field);
field.setName(SOURCE_PREFIX + SHEET_PREFIX + sheetSystems.sheetNumber);
field.setValue(packInts(sheetSystems.getSystems()));
}
//-------------//
// encodeScore //
//-------------//
/**
* Encode score source, by filling the MusicXML Miscellaneous element.
*
* @param scorePartwise the ScorePartwise to encode.
*/
public void encodeScore (ScorePartwise scorePartwise)
{
final ObjectFactory factory = new ObjectFactory();
Identification identification = scorePartwise.getIdentification();
if (identification == null) {
identification = factory.createIdentification();
scorePartwise.setIdentification(identification);
}
Miscellaneous misc = identification.getMiscellaneous();
if (misc == null) {
misc = factory.createMiscellaneous();
identification.setMiscellaneous(misc);
}
MiscellaneousField field;
if (file != null) {
misc.getMiscellaneousField().add(field = factory.createMiscellaneousField());
field.setName(SOURCE_PREFIX + "file");
field.setValue(file);
} else if (uri != null) {
misc.getMiscellaneousField().add(field = factory.createMiscellaneousField());
field.setName(SOURCE_PREFIX + "uri");
field.setValue(uri.toString());
}
}
/**
* @return the file
*/
public String getFile ()
{
return file;
}
/**
* @param file the file to set
*/
public void setFile (String file)
{
this.file = file;
}
/**
* @return the sheetSystems
*/
public List<SheetSystems> getSheets ()
{
return sheets;
}
/**
* @return the uri
*/
public URI getUri ()
{
return uri;
}
/**
* @param uri the uri to set
*/
public void setUri (URI uri)
{
this.uri = uri;
}
@Override
public String toString ()
{
StringBuilder sb = new StringBuilder(getClass().getSimpleName());
sb.append("{");
if (file != null) {
sb.append("file='").append(file).append("'");
} else if (uri != null) {
sb.append("uri=").append(uri);
}
for (SheetSystems sheet : sheets) {
sb.append(" ").append(sheet);
}
sb.append("}");
return sb.toString();
}
//--------//
// decode //
//--------//
/**
* Decode the Source information from MusicXML Miscellaneous element.
*
* @param scorePartwise the ScorePartwise to process
* @return Source information or null if not found
*/
public static Source decode (ScorePartwise scorePartwise)
{
Identification identification = scorePartwise.getIdentification();
if (identification == null) {
return null;
}
Miscellaneous misc = identification.getMiscellaneous();
if (misc == null) {
return null;
}
Source source = new Source();
for (MiscellaneousField field : misc.getMiscellaneousField()) {
String name = field.getName();
String value = field.getValue().trim();
///logger.info("miscellaneous-field name:{} value:'{}'", name, value);
if (name.startsWith(SOURCE_PREFIX)) {
String tail = name.substring(SOURCE_PREFIX.length());
if (tail.equals("file")) {
source.file = value;
} else if (tail.equals("uri")) {
source.uri = URI.create(value);
} else if (tail.startsWith(SHEET_PREFIX)) {
String numStr = tail.substring(SHEET_PREFIX.length());
int num = Integer.decode(numStr);
SheetSystems sheet = new SheetSystems(num);
source.sheets.add(sheet);
sheet.getSystems().addAll(parseInts(value));
}
}
}
return source;
}
//----------//
// packInts //
//----------//
/**
* Return a single string made of provided ints separated by space.
*
* @param ints the provided integer values
* @return the resulting string
*/
private static String packInts (List<Integer> ints)
{
StringBuilder sb = new StringBuilder();
for (int val : ints) {
if (sb.length() > 0) {
sb.append(" ");
}
sb.append(val);
}
return sb.toString();
}
//-----------//
// parseInts //
//-----------//
/**
* Parse a string of integers, separated by space.
*
* @param str the string to parse
* @return the sequence of integers decoded
*/
private static List<Integer> parseInts (String str)
{
final List<Integer> intList = new ArrayList<>();
final String[] tokens = str.split("\\s+");
for (String token : tokens) {
try {
String trimmedToken = token.trim();
if (!trimmedToken.isEmpty()) {
intList.add(Integer.decode(trimmedToken));
}
} catch (NumberFormatException ex) {
logger.warn("Illegal integer", ex);
}
}
return intList;
}
//~ Inner Classes ------------------------------------------------------------------------------
//--------------//
// SheetSystems //
//--------------//
/**
* Describes which systems have been processed in this sheet.
*/
public static class SheetSystems
{
/** Sequence of systems processed, starting from 1. */
private final List<Integer> systems = new ArrayList<>();
/** Sheet number within source file, starting from 1. */
final int sheetNumber;
/**
* Create a SheetSystems object.
*
* @param sheetNumber starting sheet number in file
*/
public SheetSystems (int sheetNumber)
{
this.sheetNumber = sheetNumber;
}
/**
* Report the IDs of the processed systems
*
* @return the systems
*/
public List<Integer> getSystems ()
{
return systems;
}
@Override
public String toString ()
{
StringBuilder sb = new StringBuilder("sheet");
sb.append("#").append(sheetNumber);
sb.append("[");
boolean first = true;
for (Integer system : systems) {
if (!first) {
sb.append(" ");
}
sb.append(system);
first = false;
}
sb.append("]");
return sb.toString();
}
}
}