From 817155c33f994014be5ca296b8c51747baf066e3 Mon Sep 17 00:00:00 2001 From: Sema Date: Mon, 18 Nov 2024 19:54:43 -0500 Subject: [PATCH] Added script --- test-scripts/mxl2xml.js | 7 ++----- test-scripts/xml2midi.js | 43 ++++++++++++++++++++++++++++++++++++++++ test-scripts/xml2mxl.js | 7 ++----- 3 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 test-scripts/xml2midi.js diff --git a/test-scripts/mxl2xml.js b/test-scripts/mxl2xml.js index 34af2c6..a44ce0b 100644 --- a/test-scripts/mxl2xml.js +++ b/test-scripts/mxl2xml.js @@ -21,12 +21,9 @@ catch (e) { } (async function() { - try { - data = await MXML.unzip(data); - } - catch (e) { + data = await MXML.unzip(data); + if (!data) { console.error('Cannot uncompress file:', input); - console.error(e.message); process.exit(1); } try { diff --git a/test-scripts/xml2midi.js b/test-scripts/xml2midi.js new file mode 100644 index 0000000..d82c877 --- /dev/null +++ b/test-scripts/xml2midi.js @@ -0,0 +1,43 @@ +#!/usr/bin/env node +const fs = require('fs'); +const MXML = require('../jazz-mxml'); + +if (process.argv.length < 3) { + console.log('Usage: ' + process.argv[1].split(/[\\/]/).slice(-1) + ' []'); + process.exit(0); +} + +const input = process.argv[2]; +const output = process.argv[3]; + +var data; +try { + data = fs.readFileSync(input); +} +catch (e) { + console.error('Cannot read file:', input); + console.error(e.message); + process.exit(1); +} + +(async function() { + var xml = new MXML(MXML.zipInfo(data) ? await MXML.unzip(data) : new TextDecoder().decode(data)); + if (!xml.isPartwise() && !xml.isTimewise()) { + console.error('Not a valid MusicXML file:', input); + process.exit(1); + } + data = xml.midi(); + if (!data) { + console.error('Cannot generate MIDI'); + process.exit(1); + } + try { + if (output) fs.writeFileSync(output, data.dump()); + else console.log(data.toString()); + } + catch (e) { + console.error('Cannot write file:', output); + console.error(e.message); + process.exit(1); + } +})(); diff --git a/test-scripts/xml2mxl.js b/test-scripts/xml2mxl.js index 0600d25..6481aca 100644 --- a/test-scripts/xml2mxl.js +++ b/test-scripts/xml2mxl.js @@ -27,12 +27,9 @@ if (!xml.isPartwise() && !xml.isTimewise() && !xml.isOpus()) { } (async function() { - try { - data = await MXML.zip(new TextEncoder().encode(data)); - } - catch (e) { + data = await MXML.zip(new TextEncoder().encode(data)); + if (!data) { console.error('Cannot uncompress file:', input); - console.error(e.message); process.exit(1); } try {