diff --git a/.eslintrc.js b/.eslintrc.js index 6adc78e..38ef412 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -28,7 +28,8 @@ module.exports = { "rules": { "no-console" : "off", "no-constant-condition" : ["error", { "checkLoops": false }], - "no-empty" : ["warn", { "allowEmptyCatch": true }] + "no-empty" : ["warn", { "allowEmptyCatch": true }], + "no-prototype-builtins" : "off" } } ] diff --git a/.npmignore b/.npmignore index 84c0a6d..b5e44e7 100644 --- a/.npmignore +++ b/.npmignore @@ -1,7 +1,6 @@ minified -test +test* .* bower.json Gruntfile.js *.html -test.* \ No newline at end of file diff --git a/bower.json b/bower.json index 84218f8..66f6955 100644 --- a/bower.json +++ b/bower.json @@ -26,6 +26,6 @@ "Gruntfile.js" ], "dependencies": { - "jzz": "^0.8.0" + "jzz": "^0.8.5" } } diff --git a/javascript/JZZ.midi.SMF.js b/javascript/JZZ.midi.SMF.js index f24349c..601e338 100644 --- a/javascript/JZZ.midi.SMF.js +++ b/javascript/JZZ.midi.SMF.js @@ -12,7 +12,7 @@ if (JZZ.MIDI.SMF) return; - var _ver = '1.2.4'; + var _ver = '1.2.5'; var _now = JZZ.lib.now; function _error(s) { throw new Error(s); } @@ -239,6 +239,7 @@ pl.ppqn = this.ppqn; pl.fps = this.fps; pl.ppf = this.ppf; + pl.ppf = this.ppf; var i; var j; var tt = []; @@ -678,6 +679,21 @@ Player.prototype.sndOff = function() { for (var c = 0; c < 16; c++) this._emit(JZZ.MIDI.allSoundOff(c)); }; + function _filter(e) { this._receive(e); } + Player.prototype._filter = _filter; + Player.prototype.filter = function(f) { + this._filter = f instanceof Function ? f : _filter; + }; + function _div(s) { return (s.charCodeAt(0) << 16) + (s.charCodeAt(1) << 8) + s.charCodeAt(2); } + Player.prototype._receive = function(e) { + if (e.ff == 0x51 && this.ppqn && (this._type != 1 || e.track == 0)) { + var t = _now(); + this._mul = this.ppqn * 1000.0 / _div(e.dd); + this.mul = this._mul / this._speed; + this._p0 = this._pos - (t - this._t0) * this.mul; + } + this._emit(e); + }; Player.prototype.tick = function() { var t = _now(); var e; @@ -685,11 +701,7 @@ for(; this._ptr < this._data.length; this._ptr++) { e = this._data[this._ptr]; if (e.tt > this._pos) break; - if (e.ff == 0x51 && this.ppqn && (this._type != 1 || e.track == 0)) { - this.mul = this.ppqn * 1000.0 / ((e.dd.charCodeAt(0) << 16) + (e.dd.charCodeAt(1) << 8) + e.dd.charCodeAt(2)); - this._p0 = this._pos - (t - this._t0) * this.mul; - } - this._emit(e); + this._filter(e); } if (this._ptr >= this._data.length) { if (this._loop && this._loop != -1) this._loop--; @@ -744,8 +756,8 @@ this._duration = this._data[this._data.length - 1].tt; this._ttt = []; if (this.ppqn) { - this.mul = this.ppqn / 500.0; // 120 bpm - m = this.mul; + this._mul = this.ppqn / 500.0; // 120 bpm + m = this._mul; t = 0; this._durationMS = 0; this._ttt.push({ t: 0, m: m, ms: 0 }); @@ -754,20 +766,28 @@ if (e.ff == 0x51 && (this.type != 1 || e.track == 0)) { this._durationMS += (e.tt - t) / m; t = e.tt; - m = this.ppqn * 1000.0 / ((e.dd.charCodeAt(0) << 16) + (e.dd.charCodeAt(1) << 8) + e.dd.charCodeAt(2)); + m = this.ppqn * 1000.0 / _div(e.dd); this._ttt.push({ t: t, m: m, ms: this._durationMS }); } } this._durationMS += (this._duration - t) / m; } else { - this.mul = this.fps * this.ppf / 1000.0; // 1s = fps*ppf ticks - this._ttt.push({ t: 0, m: this.mul, ms: 0 }); - this._durationMS = this._duration / this.mul; + this._mul = this.fps * this.ppf / 1000.0; // 1s = fps*ppf ticks + this._ttt.push({ t: 0, m: this._mul, ms: 0 }); + this._durationMS = this._duration / this._mul; } + this._coeff = 1; + this.mul = this._mul; this._ttt.push({ t: this._duration, m: 0, ms: this._durationMS }); if (!this._durationMS) this._durationMS = 1; }; + Player.prototype.speed = function(x) { + if (isNaN(parseFloat(x)) || x <= 0) x = 1; + this._speed = x; + this.mul = this._mul / this._speed; + this._p0 = this._pos - (_now() - this._t0) * this.mul; + }; Player.prototype.type = function() { return this._type; }; Player.prototype.tracks = function() { return this._tracks; }; Player.prototype.duration = function() { return this._duration; }; @@ -815,10 +835,12 @@ var e = this._data[this._ptr]; if (e.tt >= this._pos) break; if (e.ff == 0x51 && this.ppqn) { - this.mul = this.ppqn * 1000.0 / ((e.dd.charCodeAt(0) << 16) + (e.dd.charCodeAt(1) << 8) + e.dd.charCodeAt(2)); - this._p0 = this._pos - (_now() - this._t0) * this.mul; + this._mul = this.ppqn * 1000.0 / _div(e.dd); + this._p0 = this._pos - (_now() - this._t0) * this._mul; } } +//console.log('speed:', this._speed); + //this.mul = this._mul / this._speed; }; Player.prototype.tick2ms = function(t) { if (isNaN(parseFloat(t))) _error('Not a number: ' + t); diff --git a/minified/JZZ.midi.SMF.js b/minified/JZZ.midi.SMF.js index efaf810..3faee25 100644 --- a/minified/JZZ.midi.SMF.js +++ b/minified/JZZ.midi.SMF.js @@ -1 +1 @@ -!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?module.exports=i:"function"==typeof define&&define.amd?define("JZZ.midi.SMF",["JZZ"],i):i(JZZ)}(0,function(p){if(!p.MIDI.SMF){var n="1.2.4",r=p.lib.now;a.version=function(){return n},((a.prototype=[]).constructor=a).prototype.copy=function(){var t=new a;t.type=this.type,t.ppqn=this.ppqn,t.fps=this.fps,t.ppf=this.ppf,t.rmi=this.rmi,t.ntrk=this.ntrk;for(var i=0;it.length&&this._complain(i+t.length,"Incomplete data",e-t.length)},a.prototype.validate=function(){var t,i,r=[];if(this._warn)for(t=0;t=e[t].length||(a&&(o=e[t][h[t]].tt),a=!1,o>e[t][h[t]].tt&&(o=e[t][h[t]].tt))}if(s=o,a)break}}return r._type=this.type,r._tracks=e.length,r._timing(),r},(((a.Chunk=l).prototype=[]).constructor=l).prototype.copy=function(){return new l(this.type,this.data)},l.prototype.sub={MTrk:function(t,i,r){return new C(i,r)}},l.prototype.dump=function(){return this.type+s(this.data.length)+this.data},l.prototype.toString=function(){return this.type+": "+this.data.length+" bytes"},(((a.MTrk=C).prototype=[]).constructor=C).prototype.copy=function(){for(var t=new C,i=t.length=0;it)&&!(this[r].tt==t&&I(this[r])>n);r++);return this.splice(r,0,i),this},C.prototype.send=function(t){this._orig.add(this._tick,t)},C.prototype.tick=function(t){if(t!=parseInt(t)||t<0)throw RangeError("Bad tick value: "+t);if(!t)return this;var i=function(){};i.prototype=this._orig;var r=new i;return r._tick=this._tick+t,r},C.prototype.note=function(t,i,r,n){return this.noteOn(t,i,r),0this._pos);this._ptr++)81!=t.ff||!this.ppqn||1==this._type&&0!=t.track||(this.mul=1e3*this.ppqn/((t.dd.charCodeAt(0)<<16)+(t.dd.charCodeAt(1)<<8)+t.dd.charCodeAt(2)),this._p0=this._pos-(i-this._t0)*this.mul),this._emit(t);this._ptr>=this._data.length&&(this._loop&&-1!=this._loop&&this._loop--,this._loop?(this._ptr=0,this._p0=0,this._t0=i,this._st=i,this._ms=0):this.stop(),this.onEnd()),"stop"==this.event&&(this.playing=!1,this.paused=!1,this._pos=0,this._ms=0,this._ptr=0,this.sndOff(),this.event=void 0),"pause"==this.event&&(this.playing=!1,this.paused=!0,this._ms+=r()-this._st,this._pos>=this._duration&&(this._pos=this._duration-1),this._p0=this._pos,this.sndOff(),this.event=void 0),this.playing&&p.lib.schedule(this._tick)},k.prototype.trim=function(){var t,i,r,n=[],e=0;for(t=i=0;t=this._duration&&(t=this._duration-1),this._goto(t,this._t2ms(t))},k.prototype.jumpMS=function(t){isNaN(parseFloat(t))&&d("Not a number: "+t),t<0&&(t=0),t>=this._durationMS&&(t=this._durationMS-1),this._goto(this._ms2t(t),t)},k.prototype._t2ms=function(t){if(!t)return 0;var i;for(i=0;this._ttt[i].t=this._pos)break;81==t.ff&&this.ppqn&&(this.mul=1e3*this.ppqn/((t.dd.charCodeAt(0)<<16)+(t.dd.charCodeAt(1)<<8)+t.dd.charCodeAt(2)),this._p0=this._pos-(r()-this._t0)*this.mul)}},k.prototype.tick2ms=function(t){return isNaN(parseFloat(t))&&d("Not a number: "+t),t<=0?0:t>=this._duration?this._durationMS:this._t2ms(t)},k.prototype.ms2tick=function(t){return isNaN(parseFloat(t))&&d("Not a number: "+t),t<=0?0:t>=this._durationMS?this._duration:this._ms2t(t)},p.MIDI.SMF=a}function d(t){throw new Error(t)}function o(t){var i="";return 2097151>21&127))),16383>14&127))),127>7&127))),i+=String.fromCharCode(127&t)}function e(t){return String.fromCharCode(t>>8)+String.fromCharCode(255&t)}function s(t){return String.fromCharCode(t>>24&255)+String.fromCharCode(t>>16&255)+String.fromCharCode(t>>8&255)+String.fromCharCode(255&t)}function h(t){return String.fromCharCode(255&t)+String.fromCharCode(t>>8&255)+String.fromCharCode(t>>16&255)+String.fromCharCode(t>>24&255)}function a(){var t,i,r=this instanceof a?this:r=new a,n=1,e=96;if(1==arguments.length){if(arguments[0]instanceof a)return arguments[0].copy();if("string"==typeof arguments[0]&&"0"!=arguments[0]&&"1"!=arguments[0]&&"2"!=arguments[0])return r.load(arguments[0]),r;n=parseInt(arguments[0])}else 2==arguments.length?(n=parseInt(arguments[0]),e=parseInt(arguments[1])):3==arguments.length?(n=parseInt(arguments[0]),t=parseInt(arguments[1]),i=parseInt(arguments[2])):arguments.length&&d("Invalid parameters");return(isNaN(n)||n<0||2r?u(t._off,"Invalid "+i+" meta event: data too long",t.toString(),t.tt):void 0}function v(t){var i;if(void 0!==t.ff){if(127>4;if(void 0!==(i={8:10,15:11,11:12,12:13,10:15,13:15,14:15}[r]))return i;if(9==r)return t[1]?14:10}return 18}function i(t,i,r){this._orig=t,this._chan=i,this._tick=r}function A(t,i,r,n){var e;if(255==i.charCodeAt(0))e=p.MIDI.smf(i.charCodeAt(1),r);else{for(var o=[i.charCodeAt(0)],s=0;st.length&&this._complain(i+t.length,"Incomplete data",e-t.length)},a.prototype.validate=function(){var t,i,r=[];if(this._warn)for(t=0;t=e[t].length||(a&&(s=e[t][h[t]].tt),a=!1,s>e[t][h[t]].tt&&(s=e[t][h[t]].tt))}if(o=s,a)break}}return r._type=this.type,r._tracks=e.length,r._timing(),r},(((a.Chunk=l).prototype=[]).constructor=l).prototype.copy=function(){return new l(this.type,this.data)},l.prototype.sub={MTrk:function(t,i,r){return new C(i,r)}},l.prototype.dump=function(){return this.type+o(this.data.length)+this.data},l.prototype.toString=function(){return this.type+": "+this.data.length+" bytes"},(((a.MTrk=C).prototype=[]).constructor=C).prototype.copy=function(){for(var t=new C,i=t.length=0;it)&&!(this[r].tt==t&&I(this[r])>n);r++);return this.splice(r,0,i),this},C.prototype.send=function(t){this._orig.add(this._tick,t)},C.prototype.tick=function(t){if(t!=parseInt(t)||t<0)throw RangeError("Bad tick value: "+t);if(!t)return this;var i=function(){};i.prototype=this._orig;var r=new i;return r._tick=this._tick+t,r},C.prototype.note=function(t,i,r,n){return this.noteOn(t,i,r),0this._pos);this._ptr++)this._filter(t);this._ptr>=this._data.length&&(this._loop&&-1!=this._loop&&this._loop--,this._loop?(this._ptr=0,this._p0=0,this._t0=i,this._st=i,this._ms=0):this.stop(),this.onEnd()),"stop"==this.event&&(this.playing=!1,this.paused=!1,this._pos=0,this._ms=0,this._ptr=0,this.sndOff(),this.event=void 0),"pause"==this.event&&(this.playing=!1,this.paused=!0,this._ms+=r()-this._st,this._pos>=this._duration&&(this._pos=this._duration-1),this._p0=this._pos,this.sndOff(),this.event=void 0),this.playing&&p.lib.schedule(this._tick)},S.prototype.trim=function(){var t,i,r,n=[],e=0;for(t=i=0;t=this._duration&&(t=this._duration-1),this._goto(t,this._t2ms(t))},S.prototype.jumpMS=function(t){isNaN(parseFloat(t))&&u("Not a number: "+t),t<0&&(t=0),t>=this._durationMS&&(t=this._durationMS-1),this._goto(this._ms2t(t),t)},S.prototype._t2ms=function(t){if(!t)return 0;var i;for(i=0;this._ttt[i].t=this._pos)break;81==t.ff&&this.ppqn&&(this._mul=1e3*this.ppqn/M(t.dd),this.mul=this._mul/this._speed,this._p0=this._pos-(r()-this._t0)*this.mul)}},S.prototype.tick2ms=function(t){return isNaN(parseFloat(t))&&u("Not a number: "+t),t<=0?0:t>=this._duration?this._durationMS:this._t2ms(t)},S.prototype.ms2tick=function(t){return isNaN(parseFloat(t))&&u("Not a number: "+t),t<=0?0:t>=this._durationMS?this._duration:this._ms2t(t)},p.MIDI.SMF=a}function u(t){throw new Error(t)}function s(t){var i="";return 2097151>21&127))),16383>14&127))),127>7&127))),i+=String.fromCharCode(127&t)}function e(t){return String.fromCharCode(t>>8)+String.fromCharCode(255&t)}function o(t){return String.fromCharCode(t>>24&255)+String.fromCharCode(t>>16&255)+String.fromCharCode(t>>8&255)+String.fromCharCode(255&t)}function h(t){return String.fromCharCode(255&t)+String.fromCharCode(t>>8&255)+String.fromCharCode(t>>16&255)+String.fromCharCode(t>>24&255)}function a(){var t,i,r=this instanceof a?this:r=new a,n=1,e=96;if(1==arguments.length){if(arguments[0]instanceof a)return arguments[0].copy();if("string"==typeof arguments[0]&&"0"!=arguments[0]&&"1"!=arguments[0]&&"2"!=arguments[0])return r.load(arguments[0]),r;n=parseInt(arguments[0])}else 2==arguments.length?(n=parseInt(arguments[0]),e=parseInt(arguments[1])):3==arguments.length?(n=parseInt(arguments[0]),t=parseInt(arguments[1]),i=parseInt(arguments[2])):arguments.length&&u("Invalid parameters");return(isNaN(n)||n<0||2r?d(t._off,"Invalid "+i+" meta event: data too long",t.toString(),t.tt):void 0}function v(t){var i;if(void 0!==t.ff){if(127>4;if(void 0!==(i={8:10,15:11,11:12,12:13,10:15,13:15,14:15}[r]))return i;if(9==r)return t[1]?14:10}return 18}function i(t,i,r){this._orig=t,this._chan=i,this._tick=r}function k(t,i,r,n){var e;if(255==i.charCodeAt(0))e=p.MIDI.smf(i.charCodeAt(1),r);else{for(var s=[i.charCodeAt(0)],o=0;o