Skip to content

Commit

Permalink
change event handler interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mohayonao committed Aug 18, 2014
1 parent b7e566c commit 1562286
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 107 deletions.
12 changes: 8 additions & 4 deletions src/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ compile[Syntax.End] = function() {
} else {
state.postMessage({
type: "end",
args: [ currentTime ]
});
when: currentTime
}, { bubble: true });
}
};
};
Expand All @@ -87,12 +87,16 @@ compile[Syntax.Note] = function(node) {
type: "sched",
when: currentTime + duration + (offset || 0),
callback: fn
});
}, { private: true });
}

state.postMessage({
type: "note",
args: [ currentTime, midi, duration, noteOff, index ]
when: currentTime,
midi: midi,
duration: duration,
noteOff: noteOff,
chordIndex: index
});
});

Expand Down
10 changes: 4 additions & 6 deletions src/emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ Emitter.prototype.addListener = Emitter.prototype.on;

Emitter.prototype.once = function(event, listener) {

function fn() {
function fn(arg) {
this.off(event, fn);
listener.apply(this, arguments);
listener.call(this, arg);
}

fn.listener = listener;
Expand Down Expand Up @@ -60,11 +60,9 @@ Emitter.prototype.removeListener = Emitter.prototype.off;

Emitter.prototype.removeAllListeners = Emitter.prototype.off;

Emitter.prototype.emit = function(event) {
var args = [].slice.call(arguments, 1);

Emitter.prototype.emit = function(event, arg) {
this.listeners(event).forEach(function(fn) {
fn.apply(this, args);
fn.call(this, arg);
}, this);
};

Expand Down
2 changes: 1 addition & 1 deletion src/sequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Sequencer.prototype.onmessage = function(message) {
if (message && message.type === "end") {
this._ended += 1;
if (this.tracks.length <= this._ended) {
this.emit("end", message.args[0]);
this.emit("end", message);
}
}
};
Expand Down
22 changes: 10 additions & 12 deletions src/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,17 @@ Track.prototype._process = function(currentTime) {
}
};

Track.prototype.onmessage = function(message) {
switch (message.type) {
case "sched":
Track.prototype.onmessage = function(message, opts) {
opts = opts || {};

if (message.type === "sched") {
this.sched(message.when, message.callback);
break;
case "end":
this.emit.apply(this, [ message.type ].concat(message.args));
if (this._parent && message.type === "end") {
this._parent.onmessage(message);
}
break;
default:
this.emit.apply(this, [ message.type ].concat(message.args));
}
if (!opts.private) {
this.emit(message.type, message);
}
if (opts.bubble && this._parent) {
this._parent.onmessage(message);
}
};

Expand Down
132 changes: 63 additions & 69 deletions test/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ var compile = require("../src/compile");
var parse = require("../src/parse");
var Emitter = require("../src/emitter");

function toComparable(args) {
return [].slice.call(args).map(function(elem) {
return typeof elem === "function" ? "<function>" : elem;
});
}

function duration(tempo, len, dot, quantize) {
var mul = 1;

Expand All @@ -23,97 +17,97 @@ function duration(tempo, len, dot, quantize) {
describe("compile", function() {
var testCase = {
"ceg": [
[ "note", 0, 72, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 1, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 2, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 0, 72, duration(120, 4, 0, 6), 0 ],
[ "note", 1, 76, duration(120, 4, 0, 6), 0 ],
[ "note", 2, 79, duration(120, 4, 0, 6), 0 ],
[ "end", 3 ],
],
"ce8g.": [
[ "note", 0, 72, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 1, 76, duration(120, 8, 0, 6), "<function>", 0 ],
[ "note", 2, 79, duration(120, 4, 1, 6), "<function>", 0 ],
[ "note", 0, 72, duration(120, 4, 0, 6), 0 ],
[ "note", 1, 76, duration(120, 8, 0, 6), 0 ],
[ "note", 2, 79, duration(120, 4, 1, 6), 0 ],
[ "end", 3 ],
],
"c^^ e8^^ g^^": [
[ "note", 0, 72, duration(120, 4, 0, 6) * 3, "<function>", 0 ],
[ "note", 1, 76, duration(120, 8, 0, 6) * 3, "<function>", 0 ],
[ "note", 2, 79, duration(120, 4, 0, 6) * 3, "<function>", 0 ],
[ "note", 0, 72, duration(120, 4, 0, 6) * 3, 0 ],
[ "note", 1, 76, duration(120, 8, 0, 6) * 3, 0 ],
[ "note", 2, 79, duration(120, 4, 0, 6) * 3, 0 ],
[ "end", 3 ],
],
"( ceg )": [
[ "note", 0, 72, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 0, 76, duration(120, 4, 0, 6), "<function>", 1 ],
[ "note", 0, 79, duration(120, 4, 0, 6), "<function>", 2 ],
[ "note", 0, 72, duration(120, 4, 0, 6), 0 ],
[ "note", 0, 76, duration(120, 4, 0, 6), 1 ],
[ "note", 0, 79, duration(120, 4, 0, 6), 2 ],
[ "end", 3 ],
],
"o4 ceg": [
[ "note", 0, 60, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 1, 64, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 2, 67, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 0, 60, duration(120, 4, 0, 6), 0 ],
[ "note", 1, 64, duration(120, 4, 0, 6), 0 ],
[ "note", 2, 67, duration(120, 4, 0, 6), 0 ],
[ "end", 3 ],
],
"c < e > g": [
[ "note", 0, 72, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 1, 88, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 2, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 0, 72, duration(120, 4, 0, 6), 0 ],
[ "note", 1, 88, duration(120, 4, 0, 6), 0 ],
[ "note", 2, 79, duration(120, 4, 0, 6), 0 ],
[ "end", 3 ],
],
"l16 ceg": [
[ "note", 0, 72, duration(120, 16, 0, 6), "<function>", 0 ],
[ "note", 1, 76, duration(120, 16, 0, 6), "<function>", 0 ],
[ "note", 2, 79, duration(120, 16, 0, 6), "<function>", 0 ],
[ "note", 0, 72, duration(120, 16, 0, 6), 0 ],
[ "note", 1, 76, duration(120, 16, 0, 6), 0 ],
[ "note", 2, 79, duration(120, 16, 0, 6), 0 ],
[ "end", 3 ],
],
"q2 ceg": [
[ "note", 0, 72, duration(120, 4, 0, 2), "<function>", 0 ],
[ "note", 1, 76, duration(120, 4, 0, 2), "<function>", 0 ],
[ "note", 2, 79, duration(120, 4, 0, 2), "<function>", 0 ],
[ "note", 0, 72, duration(120, 4, 0, 2), 0 ],
[ "note", 1, 76, duration(120, 4, 0, 2), 0 ],
[ "note", 2, 79, duration(120, 4, 0, 2), 0 ],
[ "end", 3 ],
],
"t80 ceg": [
[ "note", 0, 72, duration(80, 4, 0, 6), "<function>", 0 ],
[ "note", 1, 76, duration(80, 4, 0, 6), "<function>", 0 ],
[ "note", 2, 79, duration(80, 4, 0, 6), "<function>", 0 ],
[ "note", 0, 72, duration(80, 4, 0, 6), 0 ],
[ "note", 1, 76, duration(80, 4, 0, 6), 0 ],
[ "note", 2, 79, duration(80, 4, 0, 6), 0 ],
[ "end", 3 ],
],
"c $ eg": [
[ "note", 0, 72, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 1, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 2, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 3, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 4, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 5, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 6, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 7, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 8, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 9, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 10, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 11, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 12, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 13, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 14, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 15, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 0, 72, duration(120, 4, 0, 6), 0 ],
[ "note", 1, 76, duration(120, 4, 0, 6), 0 ],
[ "note", 2, 79, duration(120, 4, 0, 6), 0 ],
[ "note", 3, 76, duration(120, 4, 0, 6), 0 ],
[ "note", 4, 79, duration(120, 4, 0, 6), 0 ],
[ "note", 5, 76, duration(120, 4, 0, 6), 0 ],
[ "note", 6, 79, duration(120, 4, 0, 6), 0 ],
[ "note", 7, 76, duration(120, 4, 0, 6), 0 ],
[ "note", 8, 79, duration(120, 4, 0, 6), 0 ],
[ "note", 9, 76, duration(120, 4, 0, 6), 0 ],
[ "note", 10, 79, duration(120, 4, 0, 6), 0 ],
[ "note", 11, 76, duration(120, 4, 0, 6), 0 ],
[ "note", 12, 79, duration(120, 4, 0, 6), 0 ],
[ "note", 13, 76, duration(120, 4, 0, 6), 0 ],
[ "note", 14, 79, duration(120, 4, 0, 6), 0 ],
[ "note", 15, 76, duration(120, 4, 0, 6), 0 ],
],
"$": [
],
"[ ceg ]": [
[ "note", 0, 72, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 1, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 2, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 3, 72, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 4, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 5, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 0, 72, duration(120, 4, 0, 6), 0 ],
[ "note", 1, 76, duration(120, 4, 0, 6), 0 ],
[ "note", 2, 79, duration(120, 4, 0, 6), 0 ],
[ "note", 3, 72, duration(120, 4, 0, 6), 0 ],
[ "note", 4, 76, duration(120, 4, 0, 6), 0 ],
[ "note", 5, 79, duration(120, 4, 0, 6), 0 ],
[ "end", 6 ],
],
"[ ce|g ]3": [
[ "note", 0, 72, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 1, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 2, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 3, 72, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 4, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 5, 79, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 6, 72, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 7, 76, duration(120, 4, 0, 6), "<function>", 0 ],
[ "note", 0, 72, duration(120, 4, 0, 6), 0 ],
[ "note", 1, 76, duration(120, 4, 0, 6), 0 ],
[ "note", 2, 79, duration(120, 4, 0, 6), 0 ],
[ "note", 3, 72, duration(120, 4, 0, 6), 0 ],
[ "note", 4, 76, duration(120, 4, 0, 6), 0 ],
[ "note", 5, 79, duration(120, 4, 0, 6), 0 ],
[ "note", 6, 72, duration(120, 4, 0, 6), 0 ],
[ "note", 7, 76, duration(120, 4, 0, 6), 0 ],
[ "end", 8 ],
],
};
Expand All @@ -124,19 +118,19 @@ describe("compile", function() {
var obj = new Emitter();
var state = {
index: 0,
postMessage: function(message) {
obj.emit.apply(obj, [ message.type ].concat(message.args));
postMessage: function(e) {
obj.emit(e.type, e);
}
};
var when = 0;

obj.on("note", function() {
passed.push([ "note" ].concat(toComparable(arguments)));
obj.on("note", function(e) {
passed.push([ "note", e.when, e.midi, e.duration, e.chordIndex ]);
when += 1;
});

obj.on("end", function() {
passed.push([ "end" ].concat(toComparable(arguments)));
obj.on("end", function(e) {
passed.push([ "end", e.when ]);
});

var compiled = compile(parse(mml)[0]);
Expand Down
20 changes: 10 additions & 10 deletions test/sequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@ describe("sequencer", function() {

var timeline = [];

sequencer.tracks[0].on("note", function(when, midi) {
timeline.push([ when, "note(0)", midi ]);
}).on("end", function(when) {
timeline.push([ when, "end(0)" ]);
sequencer.tracks[0].on("note", function(e) {
timeline.push([ e.when, "note(0)", e.midi ]);
}).on("end", function(e) {
timeline.push([ e.when, "end(0)" ]);
});

sequencer.tracks[1].on("note", function(when, midi) {
timeline.push([ when, "note(1)", midi ]);
}).on("end", function(when) {
timeline.push([ when, "end(1)" ]);
sequencer.tracks[1].on("note", function(e) {
timeline.push([ e.when, "note(1)", e.midi ]);
}).on("end", function(e) {
timeline.push([ e.when, "end(1)" ]);
});

sequencer.on("end", function(when) {
timeline.push([ when, "end(*)" ]);
sequencer.on("end", function(e) {
timeline.push([ e.when, "end(*)" ]);
});

sequencer.start();
Expand Down
12 changes: 7 additions & 5 deletions test/track.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ describe("track", function() {
var timeline = [];

var mml = new Track(null, compile(parse("cd l8 efg(ab)")[0]))
.on("note", function(when, midi, duration, noteOff) {
noteOff(function(when) {
.on("note", function(e) {
var midi = e.midi;

e.noteOff(function(when) {
timeline.push([ when, "nOFF", midi ]);
});

timeline.push([ when, "note", midi ]);
timeline.push([ e.when, "note", e.midi ]);
})
.on("end", function(when) {
timeline.push([ when, "end" ]);
.on("end", function(e) {
timeline.push([ e.when, "end" ]);
});

var currentTime = 100.0;
Expand Down

0 comments on commit 1562286

Please sign in to comment.