Skip to content

Commit

Permalink
MIDI - in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
jazz-soft committed Nov 22, 2024
1 parent 52ff4c1 commit 3ef57a8
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 17 deletions.
81 changes: 64 additions & 17 deletions jazz-mxml.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ MXML.prototype.format = function () { return builder.build(this.xml); };
MXML.validate = function(s) { return FXP.XMLValidator.validate(s); };
MXML.prototype.validate = function () { return MXML.validate(this.txt); };
function for_tag(a, t, f) { for (var x of a) if (x[t]) f(x, x[t]); }
function get_tag(a, t) {
var r = [];
for (var x of a) if (x[t]) r.push(x[t]);
return r;
}
function copy_mxl_headers(src, dst) {
function push(x) { dst.push(x); }
for (var tag of ['work', 'movement-number', 'movement-title', 'identification', 'defaults', 'credit', 'part-list']) for_tag(src, tag, push);
Expand Down Expand Up @@ -91,18 +86,6 @@ MXML.prototype.time2part = function() {
return [XML_prolog, XML_partwise, builder.build([pw]).trim()].join('\n');
};

MXML.prototype.midi = function() {
var M = new DOM({'/': this.xml});

return toSMF();
}
function toSMF(x) {
var smf = new JZZ.MIDI.SMF();
var trk = new JZZ.MIDI.SMF.MTrk();
smf.push(trk);
return smf;
}

async function inflate(b) {
const ds = new DecompressionStream('deflate-raw');
const writer = ds.writable.getWriter();
Expand Down Expand Up @@ -347,6 +330,59 @@ function crc(B) {
return ~C;
}

MXML.prototype.midi = function() {
var DOC = new DOM({'/': this.xml});
var SC, parts;
var p, P, PP = [], PPP = {};
var m, M, MM = [], MMM = {};
var score = {};
if (this.isPartwise()) {
SC = DOC.get('score-partwise')[0];
for (P of SC.get('part')) {
p = P.attr('id');
if (!PPP[p]) {
PP.push(p);
PPP[p] = P;
}
for (M of P.get('measure')) {
m = M.attr('number');
if (!MMM[m]) {
MM.push(m);
MMM[m] = M;
score[m] = {};
}
score[m][p] = M;
}
}
}
else if (this.isTimewise()) {
SC = DOC.get('score-timewise')[0];
for (M of SC.get('measure')) {
m = M.attr('number');
if (!MMM[m]) {
MM.push(m);
MMM[m] = M;
score[m] = {};
}
for (P of M.get('part')) {
p = P.attr('id');
if (!PPP[p]) {
PP.push(p);
PPP[p] = P;
}
score[m][p] = P;
}
}
}
return toSMF();
}
function toSMF(x) {
var smf = new JZZ.MIDI.SMF();
var trk = new JZZ.MIDI.SMF.MTrk();
smf.push(trk);
return smf;
}

function DOM(obj, sup) {
if (typeof obj['#text'] != 'undefined') {
this.tag = '#';
Expand Down Expand Up @@ -376,5 +412,16 @@ DOM.prototype.get = function(...args) {
for (var x of this.sub) if (x.tag == t) a = a.concat(x.get(...args));
return a;
}
DOM.prototype.attr = function(...args) {
if (!args.length) return;
var t = args.pop();
var a = this.get(...args);
if (a.length == 1 && a[0].atr) return a[0].atr[t];
}
DOM.prototype.value = function(...args) {
args.push('#');
var a = this.get(...args);
if (a.length == 1) return a[0].val;
}

module.exports = MXML;
11 changes: 11 additions & 0 deletions test/mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ describe('files', function() {
});
});

describe('MIDI', function() {
it('partwise', function() {
var X = new MXML(partwise);
var smf = X.midi();
});
it('timewise', function() {
var X = new MXML(timewise);
var smf = X.midi();
});
});

describe('utils', function() {
it('validate', function() {
var X = new MXML(partwise);
Expand Down

0 comments on commit 3ef57a8

Please sign in to comment.