Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src.csharp/AlphaTab/Core/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static void Add<T>(this IList<T> list, IList<T> newItems)
}
}

public static T Find<T>(this IList<T> list, Func<T, bool> predicate)
{
return list.FirstOrDefault(predicate);
}

public static IList<T> Splice<T>(this IList<T> data, double start, double deleteCount)
{
var items = data.GetRange((int) start, (int) deleteCount);
Expand Down
64 changes: 63 additions & 1 deletion src/importer/MusicXmlImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { ModelUtils } from '@src/model/ModelUtils';
import { XmlDocument } from '@src/xml/XmlDocument';
import { XmlNode, XmlNodeType } from '@src/xml/XmlNode';
import { IOHelper } from '@src/io/IOHelper';
import { ZipReader } from '@src/zip/ZipReader';
import { ZipEntry } from '@src/zip/ZipEntry';

export class MusicXmlImporter extends ScoreImporter {
private _score!: Score;
Expand Down Expand Up @@ -54,7 +56,8 @@ export class MusicXmlImporter extends ScoreImporter {
this._tieStarts = [];
this._tieStartIds = new Map<number, boolean>();
this._slurStarts = new Map<string, Note>();
let xml: string = IOHelper.toString(this.data.readAll(), this.settings.importer.encoding);

let xml: string = this.extractMusicXml();
let dom: XmlDocument = new XmlDocument();
try {
dom.parse(xml);
Expand All @@ -73,6 +76,65 @@ export class MusicXmlImporter extends ScoreImporter {
return this._score;
}

extractMusicXml(): string {
const zip = new ZipReader(this.data);
let entries: ZipEntry[];
try {
entries = zip.read();
} catch (e) {
entries = [];
}

// no compressed MusicXML, try raw
if(entries.length === 0) {
this.data.reset();
return IOHelper.toString(this.data.readAll(), this.settings.importer.encoding);
}

const container = entries.find(e => e.fullName === 'META-INF/container.xml');
if (!container) {
throw new UnsupportedFormatError('No compressed MusicXML');
}

const containerDom = new XmlDocument();
try {
containerDom.parse(IOHelper.toString(container.data, this.settings.importer.encoding));
} catch (e) {
throw new UnsupportedFormatError('Malformed container.xml, could not parse as XML', e as Error);
}

let root: XmlNode | null = containerDom.firstElement;
if (!root || root.localName !== "container") {
throw new UnsupportedFormatError("Malformed container.xml, root element not 'container'");
}

const rootFiles = root.findChildElement("rootfiles");
if(!rootFiles) {
throw new UnsupportedFormatError("Malformed container.xml, 'container/rootfiles' not found");
}

let uncompressedFileFullPath:string = "";
for(const c of rootFiles.childNodes) {
if(c.nodeType == XmlNodeType.Element && c.localName === "rootfile") {
// The MusicXML root must be described in the first <rootfile> element.
// https://www.w3.org/2021/06/musicxml40/tutorial/compressed-mxl-files/
uncompressedFileFullPath = c.getAttribute("full-path");
break;
}
}

if(!uncompressedFileFullPath) {
throw new UnsupportedFormatError("Unsupported compressed MusicXML, missing rootfile");
}

const file = entries.find(e => e.fullName === uncompressedFileFullPath);
if(!file) {
throw new UnsupportedFormatError(`Malformed container.xml, '${uncompressedFileFullPath}' not contained in zip`);
}

return IOHelper.toString(file.data, this.settings.importer.encoding);
}

private mergePartGroups(): void {
let anyMerged: boolean = false;
for (const tracks of this._partGroups.values()) {
Expand Down
Binary file added test-data/musicxml3/compressed.mxl
Binary file not shown.
10 changes: 10 additions & 0 deletions test/importer/MusicXmlImporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,14 @@ describe('MusicXmlImporterTests', () => {
expect(score.tracks[0].staves[0].bars[0].voices[0].beats[0].chord!.strings[4]).to.equal(3);
expect(score.tracks[0].staves[0].bars[0].voices[0].beats[0].chord!.strings[5]).to.equal(-1);
});

it('compressed', async () => {
const score: Score = await MusicXmlImporterTestHelper.testReferenceFile(
'test-data/musicxml3/compressed.mxl'
);

expect(score.title).to.equal("Title");
expect(score.tracks.length).to.equal(1);
expect(score.masterBars.length).to.equal(1);
});
});