Skip to content

Commit 34e25e8

Browse files
committed
Fix pb element position in MEI (verovio)
1 parent 3b47fe0 commit 34e25e8

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

src/utils/ConvertMei.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,35 @@ import { uuidv4 } from './random';
22
import * as vkbeautify from 'vkbeautify';
33
import * as Notification from '../utils/Notification';
44

5+
// Note:
6+
// The file sent to Verovio has the following structure (ConvertToVerovio()):
7+
// <section>
8+
// <pb/>
9+
// <section type="neon-neume-line">
10+
// <staff/>
11+
// <layer/>
12+
// content
13+
// </section>
14+
// <section type="neon-neume-line">
15+
// <staff/>
16+
// <layer/>
17+
// content
18+
// </section>
19+
// ...
20+
// </section>
21+
//
22+
// The file received needs to be converted back to the following structure (ConvertToNeon()):
23+
// <section>
24+
// <staff/>
25+
// <layer/>
26+
// <pb/>
27+
// <sb/>
28+
// staff content <syllable> etc.
29+
// <sb/>
30+
// staff content <syllable> etc.
31+
// ...
32+
// </section>
33+
534
export function zip<T> (array1: Array<T>, array2: Array<T>): Array<T> {
635
const result = [];
736
for (let i = 0; i < (array1.length > array2.length ? array2.length : array1.length); i++) {
@@ -24,6 +53,10 @@ export function convertToNeon(staffBasedMei: string): string {
2453
let nCol = 0;
2554

2655
for (const section of mei.querySelectorAll('section:not([type="neon-neume-line"])')) {
56+
// Remove direct children pb elements within section
57+
const pbs = section.querySelectorAll('pb');
58+
pbs.forEach(pb => pb.remove());
59+
2760
const newStaff = meiDoc.createElementNS('http://www.music-encoding.org/ns/mei', 'staff');
2861
const newLayer = meiDoc.createElementNS('http://www.music-encoding.org/ns/mei', 'layer');
2962
newStaff.setAttribute('n', '1');
@@ -226,12 +259,6 @@ export function convertToVerovio(sbBasedMei: string): string {
226259
colLayout.parentNode.removeChild(colLayout);
227260
}
228261

229-
// Check if there are <pb> elements and remove them
230-
const pageBegins = Array.from(mei.getElementsByTagName('pb'));
231-
for (const pb of pageBegins) {
232-
pb.parentNode.removeChild(pb);
233-
}
234-
235262
// Check syllable without neume
236263
const syllables = Array.from(mei.getElementsByTagName('syllable'));
237264
let hasEmptySyllable = false;
@@ -279,6 +306,11 @@ export function convertToVerovio(sbBasedMei: string): string {
279306
// A separate array is necessary as the HTMLCollection will update!
280307
const originalStaves = Array.from(section.getElementsByTagName('staff'));
281308
for (const staff of originalStaves) {
309+
// Add a pb with a facs pointing to the surface
310+
const newPb = meiDoc.createElementNS('http://www.music-encoding.org/ns/mei', 'pb');
311+
newPb.setAttribute('facs', '#' + surface.getAttribute('xml:id'));
312+
section.insertBefore(newPb, staff);
313+
282314
const layer = staff.querySelector('layer');
283315
// First pass: get all sb elements as direct children of layer
284316
const sbArray = Array.from(layer.getElementsByTagName('sb'));

0 commit comments

Comments
 (0)