-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
JZZ.gui.Karaoke.js
1 lines (1 loc) · 5.27 KB
/
JZZ.gui.Karaoke.js
1
!function(s){"object"==typeof exports&&"undefined"!=typeof module?module.exports=s.Karaoke=s:"function"==typeof define&&define.amd?define("JZZ.gui.Karaoke",["JZZ","JZZ.midi.SMF"],s):s(JZZ)}(function(v){var c;function e(s){if(!(this instanceof e))return new e(s);this.tracks=[];try{this.gui=document.createElement("div"),c=!0}catch(s){return this}if(this.gui.className="karaoke","string"==typeof s)try{return document.getElementById(s).appendChild(this.gui),this}catch(s){}try{return s.appendChild(this.gui),this}catch(s){}document.body.appendChild(this.gui)}function u(){this.verses=[]}v.gui||(v.gui={}),v.gui.Karaoke||(((e.prototype=new v.Widget).constructor=e).prototype.load=function(s){var e,t,i,n,r,o,h,a,l,p,d=0;if(c)for(;this.gui.firstChild;)this.gui.removeChild(this.gui.firstChild);for(this.tracks=[],e=this._tt=0;e<s.length;e++)if(s[e]instanceof v.MIDI.SMF.MTrk){for(o=void 0,t=p=0;t<s[e].length;t++)1==s[e][t].ff&&(r=s[e][t].tt,o||(o=new u,this.track||(this.track=o),c&&(o.dom=document.createElement("div"),this.gui.appendChild(o.dom)),this.tracks.push(o),h=void 0),"@"==(i=v.lib.fromUTF8(s[e][t].dd))[0]?((n={K:"k",V:"v",I:"i",L:"l",T:"t",W:"w"}[i[1]])&&(i=i.substring(2)),c&&(l=document.createElement("div"),n&&(l.className=n),l.appendChild(document.createTextNode(i)),o.dom.appendChild(l)),"t"==n&&(o.title=o.title?o.title+"\n"+i:i),h=void 0):("\\"==i[0]?(i=i.substring(1),h=void 0):"/"==i[0]&&(i=i.substring(1),a=void 0),h||(h={tt:r,lines:[]},c&&(h.dom=document.createElement("p"),o.dom.appendChild(h.dom)),o.verses.push(h),a=void 0),a||(a={tt:r,spans:[]},c&&(a.dom=document.createElement("div"),h.dom.appendChild(a.dom)),h.lines.push(a)),l={tt:r,txt:i},p++,c&&(l.dom=document.createElement("span"),l.dom.appendChild(document.createTextNode(i)),a.dom.appendChild(l.dom)),a.spans.push(l)));o&&d<p&&(d=p,this.track=o)}},e.prototype.reset=function(){for(var s=0;s<this.tracks.length;s++)this.tracks[s].reset()},e.prototype._receive=function(s){void 0!==s.tt&&this.jump(s.tt)},e.prototype.jump=function(s){if(this._tt>s&&this.reset(),this._tt=s,c)for(var e=0;e<this.tracks.length;e++)this.tracks[e].update(s);else this.track&&this.track.update(s)},e.prototype.toString=function(){for(var s=[],e=0;e<this.tracks.length;e++){var t=this.tracks[e];t.title&&(s.length&&s.push(""),s.push(t.title));for(var i=0;i<t.verses.length;i++){s.length&&s.push("");for(var n=0;n<t.verses[i].lines.length;n++){for(var r="",o=0;o<t.verses[i].lines[n].spans.length;o++)r+=t.verses[i].lines[n].spans[o].txt;s.push(r)}}}return s.join("\n")},u.prototype.reset=function(){var s,e,t;if(c)for(s=0;s<this.verses.length;s++)for(this.verses[s].dom.className="",e=0;e<this.verses[s].lines.length;e++)for(this.verses[s].lines[e].dom.className="",t=0;t<this.verses[s].lines[e].spans.length;t++)this.verses[s].lines[e].spans[t].dom.className="";this.verse=void 0},u.prototype.update=function(s){for(var e=this.verse||0;e<this.verses.length&&!(s<this.verses[e].tt);e++)e!=this.verse&&this.newVerse(e);if(void 0!==this.verse){for(e=this.line||0;e<this.verses[this.verse].lines.length&&!(s<this.verses[this.verse].lines[e].tt);e++)e!=this.line&&this.newLine(e);for(e=this.span||0;e<this.verses[this.verse].lines[this.line].spans.length&&!(s<this.verses[this.verse].lines[this.line].spans[e].tt);e++)e!=this.span&&this.newSpan(e)}},u.prototype.oldVerse=function(){var s,e;if(c)for(this.verses[this.verse].dom.className="past",s=0;s<this.verses[this.verse].lines.length;s++)for(this.verses[this.verse].lines[s].dom.className="past",e=0;e<this.verses[this.verse].lines[s].spans.length;e++)this.verses[this.verse].lines[s].spans[e].dom.className="past";else for(process.stdout.write("\r"),s=0;s<this.verses[this.verse].lines[this.line].spans.length;s++)process.stdout.write(this.verses[this.verse].lines[this.line].spans[s].txt)},u.prototype.newVerse=function(s){s&&this.oldVerse(),c?this.verses[s].dom.className="current":(process.stdout.write("\n"),!s&&this.title&&process.stdout.write(this.title+"\n")),this.verse=s,this.line=void 0},u.prototype.oldLine=function(){var s;if(c)for(this.verses[this.verse].lines[this.line].dom.className="past",s=0;s<this.verses[this.verse].lines[this.line].spans.length;s++)this.verses[this.verse].lines[this.line].spans[s].dom.className="past";else for(process.stdout.write("\r"),s=0;s<this.verses[this.verse].lines[this.line].spans.length;s++)process.stdout.write(this.verses[this.verse].lines[this.line].spans[s].txt)},u.prototype.newLine=function(s){if(s&&this.oldLine(),c)this.verses[this.verse].lines[s].dom.className="current";else{process.stdout.write("\n");for(var e=0;e<this.verses[this.verse].lines[s].spans.length;e++)process.stdout.write(this.verses[this.verse].lines[s].spans[e].txt)}this.line=s,this.span=void 0},u.prototype.oldSpan=function(){c&&(this.verses[this.verse].lines[this.line].spans[this.span].dom.className="past")},u.prototype.newSpan=function(s){if(s&&this.oldSpan(),c)this.verses[this.verse].lines[this.line].spans[s].dom.className="current";else{process.stdout.write("\r[1m");for(var e=0;e<=s;e++)process.stdout.write(this.verses[this.verse].lines[this.line].spans[e].txt);process.stdout.write("[0m")}var t,i,n,r;this.span=s,setTimeout((i=(t=this).verse,n=this.line,r=this.span,function(){i==t.verse&&n==t.line&&r==t.span&&(r==t.verses[i].lines[n].spans.length-1?n==t.verses[i].lines.length-1?t.oldVerse():t.oldLine():t.oldSpan())}),1e3)},v.gui.Karaoke=e)});