text: //#javascript
  // abc2svg - ABC to SVG translator
  // @source: http://moinejf.free.fr/js/abc2svg.tar.gz.php
  // Copyright (C) 2014-2017 Jean-Francois Moine
  // This program is free software; you can redistribute it and/or modify
  // it under the terms of the GNU General Public License version 2 as
  // published by the Free Software Foundation.
  const abc2svg={version:"1.12.3",vdate:"2017-04-09"}
  function Abc(user){var wpsobj,svgobj;this.user=user;const BAR=0,CLEF=1,CUSTOS=2,GRACE=4,KEY=5,METER=6,MREST=7,NOTE=8,PART=9,REST=10,SPACE=11,STAVES=12,STBRK=13,TEMPO=14,BLOCK=16,REMARK=17,FULL=0,EMPTY=1,OVAL=2,OVALBARS=3,SQUARE=4,SL_ABOVE=1,SL_BELOW=2,SL_AUTO=3,SL_HIDDEN=4,SL_DOTTED=8,OPEN_BRACE=1,CLOSE_BRACE=2,OPEN_BRACKET=4,CLOSE_BRACKET=8,OPEN_PARENTH=16,CLOSE_PARENTH=32,STOP_BAR=64,FL_VOICE=128,OPEN_BRACE2=256,CLOSE_BRACE2=512,OPEN_BRACKET2=1024,CLOSE_BRACKET2=2048,MASTER_VOICE=4096,BASE_LEN=1536,IN=96,CM=37.8,YSTEP=128;var glovar={meter:{type:METER,wmeasure:1,a_meter:[]}},info={},parse={ctx:{},prefix:"%",state:0};function clone(obj){if(!obj)return obj;var tmp=new obj.constructor;for(var k in obj)tmp[k]=obj[k];return tmp}function errbld(sev,txt,fn,idx){var i,j,l,c,h;if(user.errbld){switch(sev){case 0:sev="warn";break;case 1:sev="error";break;default:sev="fatal";break}user.errbld(sev,txt,fn,idx);return}if(idx!=undefined&&idx>=0){l=0;i=-1;while(1){j=parse.file.indexOf("\n",i+1);if(j<0||j>idx)break;l++;i=j}c=idx-i}h="";if(fn){h=fn;if(l)h+=":"+(l+1)+":"+(c+1);h+=" "}switch(sev){case 0:h+="Warning: ";break;case 1:h+="Error: ";break;default:h+="Internal bug: ";break}user.errmsg(h+clean_txt(txt),l,c)}function error(sev,s,msg,a1,a2,a3,a4){var i,j,regex,tmp;if(user.textrans){tmp=user.textrans[msg];if(tmp)msg=tmp}if(arguments.length>3)msg=msg.replace(/\$./g,function(a){switch(a){case"$1":return a1;case"$2":return a2;case"$3":return a3;default:return a4}});if(s&&s.ctx)errbld(sev,msg,s.ctx.fname,s.istart);else errbld(sev,msg)}function scanBuf(){this.index=0;this.char=function(){return this.buffer[this.index]};this.next_char=function(){return this.buffer[++this.index]};this.advance=function(){this.index++};this.get_int=function(){var val=0,c=this.buffer[this.index];while(c>="0"&&c<="9"){val=val*10+Number(c);c=this.next_char()}return val};this.get_float=function(){var txt="",c;while(1){c=this.buffer[this.index];if("0123456789.-".indexOf(c)<0)break;txt+=c;this.index++}return parseFloat(txt)};this.error=function(msg,a1,a2,a3,a4){var i,regex,tmp;if(user.textrans){tmp=user.textrans[msg];if(tmp)msg=tmp}if(arguments.length>1)msg=msg.replace(/\$./g,function(a){switch(a){case"$1":return a1;case"$2":return a2;case"$3":return a3;default:return a4}});errbld(1,msg,parse.ctx.fname,this.index+parse.bol-1)}}function syntax(sev,msg,a1,a2,a3,a4){var i,j,regex,tmp;if(user.textrans){tmp=user.textrans[msg];if(tmp)msg=tmp}if(arguments.length>2)msg=msg.replace(/\$./g,function(a){switch(a){case"$1":return a1;case"$2":return a2;case"$3":return a3;default:return a4}});errbld(sev,msg,parse.ctx.fname,parse.istart)}function empty_function(){}function abc2svg_init(){font_init();if(!user.anno_start)anno_start=empty_function;if(!user.anno_stop)anno_stop=empty_function;init_tune()}var dd_tb={},a_de=[];const std_deco={dot:"0 stc 5 1 1",tenuto:"0 emb 5 2 2",slide:"1 sld 3 7 0",arpeggio:"2 arp 12 10 0",roll:"3 roll 7 6 6",fermata:"3 hld 12 7 7",emphasis:"3 accent 7 4 4",lowermordent:"3 lmrd 10 2 2",coda:"3 coda 24 10 10",uppermordent:"3 umrd 10 2 2",segno:"3 sgno 20 4 4",trill:"3 trl 14 4 4",upbow:"3 upb 10 5 5",downbow:"3 dnb 9 5 5",gmark:"3 grm 6 5 5",wedge:"3 wedge 8 1 1",turnx:"3 turnx 10 0 5",breath:"3 brth 0 1 20",longphrase:"3 lphr 0 1 1",mediumphrase:"3 mphr 0 1 1",shortphrase:"3 sphr 0 1 1",invertedfermata:"3 hld 12 7 7",invertedturn:"3 turn 10 0 5",invertedturnx:"3 turnx 10 0 5",0:"3 fng 8 3 3 0",1:"3 fng 8 3 3 1",2:"3 fng 8 3 3 2",3:"3 fng 8 3 3 3",4:"3 fng 8 3 3 4",5:"3 fng 8 3 3 5",plus:"3 dplus 7 3 3","+":"3 dplus 7 3 3",accent:"3 accent 6 4 4",">":"3 accent 6 4 4",marcato:"3 marcato 9 3 3","^":"3 marcato 9 3 3",mordent:"3 lmrd 10 2 2",open:"3 opend 10 2 2",snap:"3 snap 14 3 3",thumb:"3 thumb 14 2 2","D.C.":"3 dacs 16 10 10 D.C.","D.S.":"3 dacs 16 10 10 D.S.",fine:"3 dacs 16 10 10 FINE",turn:"3 turn 10 0 5","trill(":"3 ltr 8 0 0","trill)":"3 ltr 8 0 0",f:"6 pf 18 1 7",ff:"6 pf 18 2 10",fff:"6 pf 18 4 13",ffff:"6 pf 18 6 16",mf:"6 pf 18 6 13",mp:"6 pf 18 6 16",p:"6 pf 18 2 8",pp:"6 pf 18 5 14",ppp:"6 pf 18 8 20",pppp:"6 pf 18 10 25",pralltriller:"3 umrd 10 2 2",sfz:'6 sfz 18 4 10 ""',ped:"4 ped 20 0 0","ped-up":"4 pedoff 20 0 0","crescendo(":"7 cresc 18 0 0","crescendo)":"7 cresc 18 0 0","<(":"7 cresc 18 0 0","<)":"7 cresc 18 0 0","diminuendo(":"7 dim 18 0 0","diminuendo)":"7 dim 18 0 0",">(":"7 dim 18 0 0",">)":"7 dim 18 0 0","-(":"8 gliss 0 0 0","-)":"8 gliss 0 0 0","~(":"8 glisq 0 0 0","~)":"8 glisq 0 0 0","8va(":"3 8va 10 0 0","8va)":"3 8va 10 0 0","8vb(":"4 8vb 10 0 0","8vb)":"4 8vb 10 0 0","15ma(":"3 15ma 10 0 0","15ma)":"3 15ma 10 0 0","15mb(":"4 15mb 10 0 0","15mb)":"4 15mb 10 0 0",invisible:"32 0 0 0 0",beamon:"33 0 0 0 0",trem1:"34 0 0 0 0",trem2:"34 0 0 0 0",trem3:"34 0 0 0 0",trem4:"34 0 0 0 0",xstem:"35 0 0 0 0",beambr1:"36 0 0 0 0",beambr2:"36 0 0 0 0",rbstop:"37 0 0 0 0","/":"38 0 0 6 6","//":"38 0 0 6 6","///":"38 0 0 6 6","beam-accel":"39 0 0 0 0","beam-rall":"39 0 0 0 0",stemless:"40 0 0 0 0",rbend:"41 0 0 0 0"},f_near=[true,true,true],f_note=[false,false,false,true,true,true,false,false,true],f_staff=[false,false,false,false,false,false,true,true];var user_deco={};function y_get(st,up,x,w){var y,p_staff=staff_tb[st],i=x/realwidth*YSTEP|0,j=(x+w)/realwidth*YSTEP|0;if(i<0)i=0;if(j>=YSTEP){j=YSTEP-1;if(i>j)i=j}if(up){y=p_staff.top[i++];while(i<=j){if(y<p_staff.top[i])y=p_staff.top[i];i++}}else{y=p_staff.bot[i++];while(i<=j){if(y>p_staff.bot[i])y=p_staff.bot[i];i++}}return y}function y_set(st,up,x,w,y){var p_staff=staff_tb[st],i=x/realwidth*YSTEP|0,j=(x+w)/realwidth*YSTEP|0;if(i<0)i=0;if(j>=YSTEP){j=YSTEP-1;if(i>j)i=j}if(up){while(i<=j){if(p_staff.top[i]<y)p_staff.top[i]=y;i++}}else{while(i<=j){if(p_staff.bot[i]>y)p_staff.bot[i]=y;i++}}}function up_p(s,pos){switch(pos){case SL_ABOVE:return true;case SL_BELOW:return false}if(s.multi&&s.multi!=0)return s.multi>0;if(!s.p_v.have_ly)return false;return s.pos.voc!=SL_ABOVE}function d_arp(de){var m,h,dx,s=de.s,dd=de.dd,xc=0;for(m=0;m<=s.nhd;m++){if(s.notes[m].acc){dx=5+s.notes[m].shac}else{dx=6-s.notes[m].shhd;switch(s.head){case SQUARE:dx+=3.5;break;case OVALBARS:case OVAL:dx+=2;break}}if(dx>xc)xc=dx}h=3*(s.notes[s.nhd].pit-s.notes[0].pit)+4;m=dd.h;if(h<m)h=m;de.has_val=true;de.val=h;de.x-=xc;de.y=3*(s.notes[0].pit-18)-3}function d_cresc(de){if(de.ldst)return;var s,dd,dd2,up,x,dx,x2,i,s2=de.s,de2=de.start,de2_prev;s=de2.s;x=s.x+3;i=de2.ix;if(i>0)de2_prev=a_de[i-1];de.st=s2.st;de.lden=false;de.has_val=true;up=up_p(s2,s2.pos.dyn);if(up)de.up=true;if(de2_prev&&de2_prev.s==s&&(de.up&&!de2_prev.up||!de.up&&de2_prev.up)){dd2=de2_prev.dd;if(f_staff[dd2.func]){x2=de2_prev.x+de2_prev.val+4;if(x2>x)x=x2}}if(de.defl.noen){dx=de.x-x;if(dx<20){x=de.x-20-3;dx=20}}else{x2=s2.x;if(de.next&&de.next.s==s&&(de.up&&!de.next.up||!de.up&&de.next.up)){dd2=de.next.dd;if(f_staff[dd2.func])x2-=5}dx=x2-x-4;if(dx<20){x-=(20-dx)*.5;dx=20}}de.val=dx;de.x=x;de.y=y_get(de.st,up,x,dx);if(!up){dd=de.dd;de.y-=dd.h}}function d_near(de){var y,up,s=de.s,dd=de.dd;if(dd.str){return}if(s.multi)up=s.multi>0;else up=s.stem<0;if(up)y=s.ymx|0;else y=s.ymn-dd.h|0;if(y>-6&&y<24){if(up)y+=3;y=((y+6)/6|0)*6-6}if(up)s.ymx=y+dd.h;else s.ymn=y;de.y=y;de.x=s.x+s.notes[s.stem>=0?0:s.nhd].shhd;if(dd.name[0]=="d"&&s.nflags>=-1){if(up){if(s.stem>0)de.x+=3.5}else{if(s.stem<0)de.x-=3.5}}}function d_pf(de){var dd2,x2,str,x,up,s=de.s,dd=de.dd,de_prev;de.val=dd.wl+dd.wr;up=up_p(s,s.pos.vol);if(up)de.up=true;x=s.x-dd.wl;if(de.ix>0){de_prev=a_de[de.ix-1];if(de_prev.s==s&&(de.up&&!de_prev.up||!de.up&&de_prev.up)){dd2=de_prev.dd;if(f_staff[dd2.func]){x2=de_prev.x+de_prev.val+4;if(x2>x)x=x2}}}de.x=x;de.y=y_get(s.st,up,x,de.val);if(!up)de.y-=dd.h}function d_slide(de){var m,dx,s=de.s,yc=s.notes[0].pit,xc=5;for(m=0;m<=s.nhd;m++){if(s.notes[m].acc){dx=4+s.notes[m].shac}else{dx=5-s.notes[m].shhd;switch(s.head){case SQUARE:dx+=3.5;break;case OVALBARS:case OVAL:dx+=2;break}}if(s.notes[m].pit<=yc+3&&dx>xc)xc=dx}de.x-=xc;de.y=3*(yc-18)}function d_trill(de){if(de.ldst)return;var dd,up,y,w,tmp,s2=de.s,st=s2.st,s=de.start.s,x=s.x;if(s.type==NOTE&&s.a_dd&&s.a_dd.length>1)x+=10;de.st=st;if(de.dd.func!=4)up=s2.multi>=0;if(de.defl.noen){w=de.x-x;if(w<20){x=de.x-20-3;w=20}}else{w=s2.x-x-6;if(s2.type==NOTE)w-=6;if(w<20){x-=(20-w)*.5;w=20}}dd=de.dd;y=y_get(st,up,x,w);if(up){tmp=staff_tb[s.st].topbar+2;if(y<tmp)y=tmp}else{y-=dd.h;tmp=staff_tb[s.st].botbar-2;if(y>tmp)y=tmp}de.lden=false;de.has_val=true;de.val=w;de.x=x;de.y=y;if(up)y+=dd.h;y_set(st,up,x,w,y);if(up)s.ymx=s2.ymx=y;else s.ymn=s2.ymn=y}function d_upstaff(de){if(de.ldst)return;if(de.start){d_trill(de);return}var yc,up,inv,s=de.s,dd=de.dd,x=s.x,w=dd.wl+dd.wr,stafft=staff_tb[s.st].topbar+2,staffb=staff_tb[s.st].botbar-2;if(s.nhd)x+=s.notes[s.stem>=0?0:s.nhd].shhd;up=-1;if(dd.func==4){up=0}else if(s.pos){switch(s.pos.orn){case SL_ABOVE:up=1;break;case SL_BELOW:up=0;break}}switch(dd.glyph){case"accent":case"roll":if(!up||up<0&&(s.multi<0||!s.multi&&s.stem>0)){yc=y_get(s.st,false,s.x-dd.wl,w);if(yc>staffb)yc=staffb;yc-=dd.h;y_set(s.st,false,s.x,0,yc);inv=true;s.ymn=yc}else{yc=y_get(s.st,true,s.x,0);if(yc<stafft)yc=stafft;y_set(s.st,true,s.x-dd.wl,w,yc+dd.h);s.ymx=yc+dd.h}break;case"brth":case"lphr":case"mphr":case"sphr":yc=stafft+1;for(s=s.ts_next;s;s=s.ts_next)if(s.shrink)break;x+=((s?s.x:realwidth)-x)*.4;break;default:if(dd.name.indexOf("invert")==0)inv=true;if(dd.name!="invertedfermata"&&(up>0||up<0&&s.multi>=0)){yc=y_get(s.st,true,s.x-dd.wl,w);if(yc<stafft)yc=stafft;y_set(s.st,true,s.x-dd.wl,w,yc+dd.h);s.ymx=yc+dd.h}else{yc=y_get(s.st,false,s.x-dd.wl,w);if(yc>staffb)yc=staffb;yc-=dd.h;y_set(s.st,false,s.x-dd.wl,w,yc);if(dd.name=="fermata")inv=true;s.ymn=yc}break}if(inv){yc+=dd.h;de.inv=true}de.x=x;de.y=yc}const func_tb=[d_near,d_slide,d_arp,d_upstaff,d_upstaff,d_trill,d_pf,d_cresc];function deco_add(param){var dv=param.match(/(\S*)\s+(.*)/);user_deco[dv[1]]=dv[2]}function deco_build(nm,text){var dd,dd2,c,i,elts,str;a=text.match(/(\d+)\s+(.+?)\s+([0-9.]+)\s+([0-9.]+)\s+([0-9.]+)/);if(!a){error(1,null,"Invalid decoration '$1'",nm);return}var c_func=Number(a[1]),h=parseFloat(a[3]),wl=parseFloat(a[4]),wr=parseFloat(a[5]);if(isNaN(c_func)){error(1,null,"%%deco: bad C function value '$1'",a[1]);return}if((c_func<0||c_func>10)&&(c_func<32||c_func>41)){error(1,null,"%%deco: bad C function index '$1'",c_func);return}if(h<0||wl<0||wr<0){error(1,null,"%%deco: cannot have a negative value '$1'",text);return}if(h>50||wl>80||wr>80){error(1,null,"%%deco: abnormal h/wl/wr value '$1'",text);return}dd=dd_tb[nm];if(!dd){dd={name:nm};dd_tb[nm]=dd}dd.func=dd.name.indexOf("head-")==0?9:c_func;dd.glyph=a[2];dd.h=h;dd.wl=wl;dd.wr=wr;str=text.replace(a[0],"").trim();if(str){if(str[0]=='"')str=str.slice(1,-1);dd.str=str}if(dd.func==6&&dd.str==undefined)dd.str=dd.name;c=dd.name.slice(-1);if(c=="("||c==")"&&dd.name.indexOf("(")<0){name2=dd.name.slice(0,-1)+(c=="("?")":"(");dd2=dd_tb[name2];if(dd2){if(c=="("){dd.dd_en=dd2;dd2.dd_st=dd}else{dd.dd_st=dd2;dd2.dd_en=dd}}else{dd2=deco_def(name2);if(!dd2)return}}return dd}function deco_cnv(a_dcn,s,prev){var i,j,dd,dcn,note,n=a_dcn.length;for(i=0;i<n;i++){dcn=a_dcn[i];dd=dd_tb[dcn];if(!dd){dd=deco_def(dcn);if(!dd)continue}switch(dd.func){case 0:if(s.type==BAR&&dd.name=="dot"){s.bar_dotted=true;break}case 1:case 2:if(!s.notes){error(1,s,"!$1! must be on a note or a rest",dd.name);continue}break;case 8:if(s.type!=NOTE){error(1,s,"!$1! must be on a note",dd.name);continue}note=s.notes[s.nhd];if(!note.a_dcn)note.a_dcn=[];note.a_dcn.push(dd.name);continue;case 9:if(!s.notes){error(1,s,"!$1! must be on a note or rest",dd.name);continue}for(j=0;j<=s.nhd;j++){note=s.notes[j];if(!note.a_dcn)note.a_dcn=[];note.a_dcn.push(dd.name)}continue;default:break;case 10:if(s.notes){for(j=0;j<=s.nhd;j++)s.notes[j].color=dd.name}else{s.color=dd.name}continue;case 32:s.invis=true;continue;case 33:if(s.type!=BAR){error(1,s,"!beamon! must be on a bar");continue}s.beam_on=true;continue;case 34:if(s.type!=NOTE||!prev||prev.type!=NOTE){error(1,s,"!$1! must be on the last of a couple of notes",dd.name);continue}s.trem2=true;s.beam_end=true;prev.trem2=true;prev.beam_st=true;s.ntrem=prev.ntrem=Number(dd.name[4]);s.nflags--;prev.nflags--;if(s.nflags>0){s.nflags+=s.ntrem;prev.nflags+=s.ntrem}else{if(s.nflags<=-2){s.stemless=true;prev.stemless=true}s.nflags=s.ntrem;prev.nflags=s.ntrem}for(j=0;j<=s.nhd;j++)s.notes[j].dur*=2;for(j=0;j<=prev.nhd;j++)prev.notes[j].dur*=2;continue;case 35:if(s.type!=NOTE){error(1,s,"!xstem! must be on a note");continue}s.xstem=true;s.nflags=0;continue;case 36:if(s.type!=NOTE){error(1,s,"!$1! must be on a note",dd.name);continue}if(dd.name[6]=="1")s.beam_br1=true;else s.beam_br2=true;continue;case 37:s.rbstop=1;continue;case 38:if(s.type!=NOTE){error(1,s,"!$1! must be on a note",dd.name);continue}s.trem1=true;s.ntrem=dd.name.length;if(s.nflags>0)s.nflags+=s.ntrem;else s.nflags=s.ntrem;continue;case 39:if(s.type!=NOTE){error(1,s,"!$1! must be on a note",dd.name);continue}s.feathered_beam=dd.name[5]=="a"?1:-1;continue;case 40:s.stemless=true;continue;case 41:s.rbstop=2;continue}if(!s.a_dd)s.a_dd=[];s.a_dd.push(dd)}}function deco_def(nm){if(user_deco&&user_deco[nm])return deco_build(nm,user_deco[nm]);if(std_deco[nm])return deco_build(nm,std_deco[nm]);if(cfmt.decoerr)error(1,null,"Unknown decoration '$1'",nm);return}function deco_update(s,dx){var i,de,n=a_de.length;for(i=0;i<n;i++){de=a_de[i];if(de.s==s)de.x+=dx}}function deco_width(s){var dd,i,wl=0,a_dd=s.a_dd,n=a_dd.length;for(i=i=0;i<n;i++){dd=a_dd[i];switch(dd.func){case 1:if(wl<7)wl=7;break;case 2:if(wl<14)wl=14;break}}if(wl!=0&&s.prev&&s.prev.type==BAR)wl-=3;return wl}function draw_all_deco(){if(a_de.length==0)return;var de,de2,dd,s,note,f,st,x,y,y2,ym,uf,i,str,ymid=[];if(!cfmt.dynalign){st=nstaff;y=staff_tb[st].y;while(--st>=0){y2=staff_tb[st].y;ymid[st]=(y+24+y2)*.5;y=y2}}while(1){de=a_de.shift();if(!de)break;dd=de.dd;if(!dd)continue;if(dd.dd_en)continue;s=de.s;f=dd.glyph;i=f.indexOf("/");if(i>0){if(s.stem>=0)f=f.slice(0,i);else f=f.slice(i+1)}if(f_staff[dd.func])set_sscale(-1);else set_scale(s);st=de.st;x=de.x;y=de.y+staff_tb[st].y;if(de.m!=undefined){note=s.notes[de.m];x+=note.shhd*stv_g.scale}else if(f_staff[dd.func]&&!cfmt.dynalign&&(de.up&&st>0||!de.up&&st<nstaff)){if(de.up)ym=ymid[--st];else ym=ymid[st++];ym-=dd.h*.5;if(de.up&&y<ym||!de.up&&y>ym){y2=y_get(st,!de.up,de.x,de.val)+staff_tb[st].y;if(de.up)y2-=dd.h;if(de.up&&y2>ym||!de.up&&y2<ym){y=ym}}}uf=user[f];if(uf&&typeof uf=="function"){uf(x,y,de);continue}if(psdeco(f,x,y,de))continue;anno_start(s,"deco");if(de.inv){g_open(x,y,0,1,-1);x=y=0}if(de.has_val){if(dd.func!=2||stv_g.st<0)out_deco_val(x,y,f,de.val/stv_g.scale,de.defl);else out_deco_val(x,y,f,de.val,de.defl)}else if(dd.str!=undefined&&dd.str!="sfz"){str=dd.str;if(str[0]=="@"){a=str.match(/^@([0-9.-]+),([0-9.-]+);?/);x+=Number(a[1]);y+=Number(a[2]);str=str.replace(a[0],"")}out_deco_str(x,y,f,str)}else if(de.lden){out_deco_long(x,y,de)}else{xygl(x,y,f)}if(stv_g.g)g_close();anno_stop(s,"deco")}}function draw_deco_near(){var s,g,dd,first,m;function create_deco(s){var dd,k,l,pos,de,n=s.a_dd.length;for(k=0;k<n;k++){dd=s.a_dd[k];switch(dd.func){default:pos=0;break;case 3:case 4:case 5:pos=s.pos.orn;break;case 6:pos=s.pos.vol;break;case 7:pos=s.pos.dyn;break}if(pos==SL_HIDDEN)continue;de={s:s,dd:dd,st:s.st,ix:a_de.length,defl:{},x:s.x,y:s.y};a_de.push(de);if(dd.dd_en){de.ldst=true}else if(dd.dd_st){de.lden=true;de.defl.nost=true}if(!f_near[dd.func])continue;func_tb[dd.func](de)}}function create_all(s){var m;if(s.a_dd)create_deco(s);if(s.notes){for(m=0;m<s.notes.length;m++){if(s.notes[m].a_dcn)create_dh(s,m)}}}function create_dh(s,m){var f,str,de,uf,dcn,dd,note=s.notes[m];for(k=0;k<note.a_dcn.length;k++){dcn=note.a_dcn[k];dd=dd_tb[dcn];if(!dd){dd=deco_def(dcn);if(!dd)continue}switch(dd.func){case 2:case 5:case 7:error(1,null,"Cannot have !$1! on a head",dd.name);continue;case 9:note.invis=true;break;case 10:note.color=dd.name;continue;case 32:note.invis=true;continue}de={s:s,dd:dd,st:s.st,m:m,ix:0,defl:{},x:s.x,y:3*(note.pit-18)};a_de.push(de);if(dd.dd_en){de.ldst=true}else if(dd.dd_st){de.lden=true;de.defl.nost=true}}}function ll_deco(){var i,j,de,dd,dd2,v,s,st,n_de=a_de.length;for(i=0;i<n_de;i++){de=a_de[i];if(!de.ldst)continue;dd=de.dd;dd2=dd.dd_en;s=de.s;v=s.v;for(j=i+1;j<n_de;j++){de2=a_de[j];if(!de2.start&&de2.dd==dd2&&de2.s.v==v)break}if(j==n_de){st=s.st;for(j=i+1;j<n_de;j++){de2=a_de[j];if(!de2.start&&de2.dd==dd2&&de2.s.st==st)break}}if(j==n_de){de2={s:de.s,st:de.st,dd:dd2,ix:a_de.length-1,x:realwidth-6,y:de.s.y,lden:true,defl:{noen:true}};if(de2.x<s.x+10)de2.x=s.x+10;if(de.m!=undefined)de2.m=de.m;a_de.push(de2)}de2.start=de;de2.defl.nost=false}for(i=0;i<n_de;i++){de2=a_de[i];if(!de2.lden||de2.start)continue;s=de2.s;de={s:prev_scut(s),st:de2.st,dd:de2.dd.dd_st,ix:a_de.length-1,y:s.y,ldst:true};de.x=de.s.x;if(de2.m!=undefined)de.m=de2.m;a_de.push(de);de2.start=de}}for(s=tsfirst;s;s=s.ts_next){switch(s.type){case BAR:case MREST:break;case NOTE:case REST:case SPACE:if(!first)first=s;break;case GRACE:for(g=s.extra;g;g=g.next)create_all(g);default:continue}create_all(s)}ll_deco()}function draw_deco_note(){var i,de,dd,f,n_de=a_de.length;for(i=0;i<n_de;i++){de=a_de[i];dd=de.dd;f=dd.func;if(f_note[f]&&de.m==undefined)func_tb[f](de)}}function draw_deco_staff(){var s,first_gchord,p_voice,x,y,w,i,n,v,de,dd,gch,gch2,ix,minmax=new Array(nstaff);function draw_repbra(p_voice){var s,s1,y,y2,i,p,w,first_repeat;y=staff_tb[p_voice.st].topbar+6+20;for(s=p_voice.sym;s;s=s.next){if(s.type!=BAR)continue;if(!s.rbstart||s.norepbra)continue;if(!s.next)break;if(!first_repeat)first_repeat=s;s1=s;for(;;){if(!s.next)break;s=s.next;if(s.rbstop)break}y2=y_get(p_voice.st,true,s1.x,s.x-s1.x);if(y<y2)y=y2;if(s1.text){w=strw(s1.text);y2=y_get(p_voice.st,true,s1.x+4,w);y2+=gene.curfont.size+2;if(y<y2)y=y2}if(s.rbstart)s=s.prev}s=first_repeat;if(!s)return;set_dscale(p_voice.st,true);set_font("repeat");y2=y*staff_tb[p_voice.st].staffscale;for(;s;s=s.next){if(!s.rbstart||s.norepbra)continue;s1=s;while(1){if(!s.next)break;s=s.next;if(s.rbstop)break}if(s1==s)break;x=s1.x;if(s.type!=BAR){w=s.rbstop?0:s.x-realwidth+4}else if(s.bar_type.length>1&&s.bar_type!="[]"||s.bar_type=="]"){if(s1.st>0&&!(cur_sy.staves[s1.st-1].flags&STOP_BAR)){w=s.wl}else if(s.bar_type.slice(-1)==":"){w=12}else if(s.bar_type[0]!=":"||s.bar_type=="]"){w=0}else{w=8}}else{w=8}w=s.x-x-w;if(!s.next&&!s.rbstop&&!p_voice.bar_start){p_voice.bar_start=clone(s);p_voice.bar_start.bar_type="[";p_voice.bar_start.text=undefined;p_voice.bar_start.rbstart=1;delete p_voice.bar_start.a_gch}if(s1.text)xy_str(x+4,y2-gene.curfont.size*.9,s1.text);xypath(x,y2);if(s1.rbstart==2)output.push("m0 20v-20");output.push("h");output.push(w.toFixed(2));if(s.rbstop==2)output.push("v20");output.push('"/>\n');y_set(s1.st,true,x,w,y+2);if(s.rbstart)s=s.prev}}for(i=0;i<=nstaff;i++)minmax[i]={ymin:0,ymax:24};for(s=tsfirst;s;s=s.ts_next){if(!s.a_gch)continue;if(!first_gchord)first_gchord=s;gch2=null;n=s.a_gch.length;for(ix=0;ix<n;ix++){gch=s.a_gch[ix];if(gch.type!="g")continue;gch2=gch;if(gch.y<0)break}if(gch2){w=gch2.w;if(gch2.y>=0){y=y_get(s.st,true,s.x,w);if(y>minmax[s.st].ymax)minmax[s.st].ymax=y}else{y=y_get(s.st,false,s.x,w);if(y<minmax[s.st].ymin)minmax[s.st].ymin=y}}}if(first_gchord){for(i=0;i<=nstaff;i++){var bot=staff_tb[i].botbar;minmax[i].ymin-=3;if(minmax[i].ymin>bot-10)minmax[i].ymin=bot-10;var top=staff_tb[i].topbar;minmax[i].ymax+=3;if(minmax[i].ymax<top+10)minmax[i].ymax=top+10}set_sscale(-1);for(s=first_gchord;s;s=s.ts_next){if(!s.a_gch)continue;draw_gchord(s,minmax[s.st].ymin,minmax[s.st].ymax)}}n=voice_tb.length;for(v=0;v<n;v++){p_voice=voice_tb[v];if(p_voice.second||!p_voice.sym)continue;draw_repbra(p_voice)}for(i=0;i<=nstaff;i++)minmax[i]={ymin:0,ymax:0};n=a_de.length;for(i=0;i<n;i++){de=a_de[i];dd=de.dd;if(!dd)continue;if(!f_staff[dd.func]||de.m!=undefined)continue;func_tb[dd.func](de);if(dd.dd_en)continue;if(cfmt.dynalign){if(de.up){if(de.y>minmax[de.st].ymax)minmax[de.st].ymax=de.y}else{if(de.y<minmax[de.st].ymin)minmax[de.st].ymin=de.y}}}for(i=0;i<n;i++){de=a_de[i];dd=de.dd;if(!dd)continue;if(dd.dd_en||!f_staff[dd.func])continue;if(cfmt.dynalign){if(de.up)y=minmax[de.st].ymax;else y=minmax[de.st].ymin;de.y=y}else{y=de.y}if(de.up)y+=dd.h;y_set(de.st,de.up,de.x,de.val,y)}}function draw_measnb(){var s,st,bar_num,x,y,w,any_nb,font_size,sy=cur_sy;for(st=0;st<=nstaff;st++){if(!sy.staves[st].empty)break}if(st>nstaff)return;set_dscale(st);if(staff_tb[st].staffscale!=1){font_size=get_font("measure").size;param_set_font("measurefont","* "+(font_size/staff_tb[st].staffscale).toString())}set_font("measure");s=tsfirst;bar_num=gene.nbar;if(bar_num>1){if(cfmt.measurenb==0){any_nb=true;y=y_get(st,true,0,20);if(y<staff_tb[st].topbar+14)y=staff_tb[st].topbar+14;xy_str(0,y,bar_num.toString());y_set(st,true,0,20,y+gene.curfont.size+2)}else if(bar_num%cfmt.measurenb==0){for(;;s=s.ts_next){switch(s.type){case METER:case CLEF:case KEY:case STBRK:continue}break}while(s.st!=st)s=s.ts_next;if(s.prev.type!=CLEF)s=s.prev;x=s.x-s.wl;any_nb=true;w=cwid("0")*gene.curfont.swfac;if(bar_num>=10)w*=bar_num>=100?3:2;if(cfmt.measurebox)w+=4;y=y_get(st,true,x,w);if(y<staff_tb[st].topbar+6)y=staff_tb[st].topbar+6;y+=2;if(cfmt.measurebox){xy_str_b(x,y,bar_num.toString());w+=3}else{xy_str(x,y,bar_num.toString())}y+=gene.curfont.size;y_set(st,true,x,w,y);s.ymx=y}}for(;s;s=s.ts_next){switch(s.type){case STAVES:sy=sy.next;for(st=0;st<nstaff;st++){if(!sy.staves[st].empty)break}set_sscale(st);continue;default:continue;case BAR:if(!s.bar_num)continue;break}bar_num=s.bar_num;if(cfmt.measurenb==0||bar_num%cfmt.measurenb!=0||!s.next)continue;if(!any_nb)any_nb=true;w=cwid("0")*gene.curfont.swfac;if(bar_num>=10)w*=bar_num>=100?3:2;if(cfmt.measurebox)w+=4;x=s.x-w*.4;y=y_get(st,true,x,w);if(y<staff_tb[st].topbar+6)y=staff_tb[st].topbar+6;if(s.next.type==NOTE){if(s.next.stem>0){if(y<s.next.ys-gene.curfont.size)y=s.next.ys-gene.curfont.size}else{if(y<s.next.y)y=s.next.y}}y+=2;if(cfmt.measurebox){xy_str_b(x,y,bar_num.toString());w+=3}else{xy_str(x,y,bar_num.toString())}y+=gene.curfont.size;y_set(st,true,x,w,y);s.ymx=y}gene.nbar=bar_num;if(font_size)param_set_font("measurefont","* "+font_size.toString())}function draw_notempo(s,x,y,dur,sc){var dx,p,dotx,elts=identify_note(s,dur),head=elts[0],dots=elts[1],nflags=elts[2];if(stv_g.started){output.push("</g>\n");stv_g.started=false}out_XYAB('<g transform="translate(X,Y) scale(F)">\n',x+8,y+3,sc);switch(head){case OVAL:p="HD";break;case EMPTY:p="Hd";break;default:p="hd";break}xygl(-posx,posy,p);dx=4;if(dots){dotx=8;if(nflags>0)dotx+=4;switch(head){case SQUARE:dotx+=3;break;case OVALBARS:case OVAL:dotx+=2;break;case EMPTY:dotx+=1;break}dx=dotx*dots;dotx-=posx;while(--dots>=0){xygl(dotx,posy,"dot");dotx+=3.5}}if(dur<BASE_LEN){if(nflags<=0){out_stem(-posx,posy,21)}else{out_stem(-posx,posy,21,false,nflags);if(dx<6)dx=6}}output.push("\n</g>\n");return(dx+15)*sc}function tempo_width(s){var w=0;set_font("tempo");if(s.tempo_str1)w=strw(s.tempo_str1);if(s.tempo_ca)w+=strw(s.tempo_ca);if(s.tempo_notes)w+=10*s.tempo_notes.length+6+cwid(" ")*gene.curfont.swfac*6+10;if(s.tempo_str2)w+=strw(s.tempo_str2);return w}function write_tempo(s,x,y){var j,dx,sc=.5*gene.curfont.size/15;set_font("tempo");if(s.tempo_str1){xy_str(x,y,s.tempo_str1);x+=strw(s.tempo_str1)+6}if(s.tempo_notes){for(j=0;j<s.tempo_notes.length;j++)x+=draw_notempo(s,x,y,s.tempo_notes[j],sc);x+=5;xy_str(x,y,"=");x+=strw("= ")+5;if(s.tempo_ca){xy_str(x,y,s.tempo_ca);x+=strw(s.tempo_ca)+5}if(s.tempo){xy_str(x,y,s.tempo.toString());dx=cwid("0")*gene.curfont.swfac;x+=dx+5;if(s.tempo>=10){x+=dx;if(s.tempo>=100)x+=dx}}else{x+=draw_notempo(s,x,y,s.new_beat,sc)}}if(s.tempo_str2)xy_str(x,y,s.tempo_str2);s.del=true}function draw_partempo(st,top){var s,some_part,some_tempo,h,w,y,dy=0,ht=0;var ymin=staff_tb[st].topbar+12,dosh=0,shift=1,x=0;for(s=tsfirst;s;s=s.ts_next){if(s.type!=TEMPO||s.del)continue;if(!some_tempo)some_tempo=s;w=tempo_width(s);y=y_get(st,true,s.x-5,w)+2;if(y>ymin)ymin=y;if(x>=s.x-5&&!(dosh&shift>>1))dosh|=shift;shift<<=1;x=s.x-5+w}if(some_tempo){set_font("tempo");ht=gene.curfont.size+2+2;y=2-ht;h=y-ht;if(dosh!=0)ht*=2;if(top<ymin+ht)dy=ymin+ht-top;for(s=some_tempo;s;s=s.ts_next){if(s.type!=TEMPO||s.del)continue;if(user.anno_start||user.anno_stop){s.wl=5;s.wr=40;s.ymn=dosh&1?h:y;s.ymx=s.ymn+14;anno_start(s)}write_tempo(s,s.x-5,dosh&1?h:y);anno_stop(s);dosh>>=1}}ymin=staff_tb[st].topbar+14;for(s=tsfirst;s;s=s.ts_next){if(s.type!=PART)continue;if(!some_part){some_part=s;set_font("parts");h=gene.curfont.size+2+2}w=strw(s.text);y=y_get(st,true,s.x-10,w+15)+5;if(ymin<y)ymin=y}if(some_part){if(top<ymin+h+ht)dy=ymin+h+ht-top;for(s=some_part;s;s=s.ts_next){if(s.type!=PART)continue;s.x-=10;if(user.anno_start||user.anno_stop){w=strw(s.text);s.wl=0;s.wr=w;s.ymn=-ht-h;s.ymx=s.ymn+h;anno_start(s)}if(cfmt.partsbox)xy_str_b(s.x,2-ht-h,s.text);else xy_str(s.x,2-ht-h,s.text);anno_stop(s)}}return dy}const STEM_MIN=16,STEM_MIN2=14,STEM_MIN3=12,STEM_MIN4=10,STEM_CH_MIN=14,STEM_CH_MIN2=10,STEM_CH_MIN3=9,STEM_CH_MIN4=9,BEAM_DEPTH=3.2,BEAM_OFFSET=.25,BEAM_SHIFT=5,BEAM_FLATFAC=.6,BEAM_THRESH=.06,BEAM_SLOPE=.8,BEAM_STUB=8,SLUR_SLOPE=.5,GSTEM=15,GSTEM_XOFF=2.3;function b_pos(grace,stem,nflags,b){var top,bot,d1,d2,shift=!grace?BEAM_SHIFT:3.5,depth=!grace?BEAM_DEPTH:1.8;function rnd6(y){var iy=Math.round((y+12)/6)*6-12;return iy-y}if(stem>0){bot=b-(nflags-1)*shift-depth;if(bot>26)return 0;top=b}else{top=b+(nflags-1)*shift+depth;if(top<-2)return 0;bot=b}d1=rnd6(top-BEAM_OFFSET);d2=rnd6(bot+BEAM_OFFSET);if(d1*d1>d2*d2)return d2;return d1}function sym_dup(s_orig){var m,note,s=clone(s_orig);s.invis=true;delete s.text;delete s.a_gch;delete s.a_ly;delete s.a_dd;s.notes=clone(s_orig.notes);for(m=0;m<=s.nhd;m++){note=s.notes[m]=clone(s_orig.notes[m]);delete note.a_dcn}return s}const min_tb=[[STEM_MIN,STEM_MIN,STEM_MIN2,STEM_MIN3,STEM_MIN4,STEM_MIN4],[STEM_CH_MIN,STEM_CH_MIN,STEM_CH_MIN2,STEM_CH_MIN3,STEM_CH_MIN4,STEM_CH_MIN4]];function calculate_beam(bm,s1){var s,s2,notes,nflags,st,v,two_staves,two_dir,hh,x,y,ys,a,b,stem_err,max_stem_err,sx,sy,sxx,sxy,syy,a0,stem_xoff,scale,visible;if(!s1.beam_st){s=sym_dup(s1);s.prev=s1.prev;if(s.prev)s.prev.next=s;else s.p_v.sym=s;s1.prev=s;s.next=s1;s1.ts_prev.ts_next=s;s.ts_prev=s1.ts_prev;s1.ts_prev=s;s.ts_next=s1;s.x-=12;if(s.x>s1.prev.x+12)s.x=s1.prev.x+12;s.beam_st=true;delete s.beam_end;s.tmp=true;s.slur_start=0;delete s.slur_end;s1=s}notes=nflags=0;two_staves=two_dir=false;st=s1.st;v=s1.v;stem_xoff=s1.grace?GSTEM_XOFF:3.5;for(s2=s1;;s2=s2.next){if(s2.type==NOTE){if(s2.nflags>nflags)nflags=s2.nflags;notes++;if(s2.st!=st)two_staves=true;if(s2.stem!=s1.stem)two_dir=true;if(!visible&&!s2.invis&&!s2.stemless)visible=true;if(s2.beam_end)break}if(!s2.next){for(;;s2=s2.prev){if(s2.type==NOTE)break}s=sym_dup(s2);s.next=s2.next;if(s.next)s.next.prev=s;s2.next=s;s.prev=s2;s.ts_next=s2.ts_next;if(s.ts_next)s.ts_next.ts_prev=s;s2.ts_next=s;s.ts_prev=s2;delete s.beam_st;s.beam_end=true;s.tmp=true;delete s.slur_start;delete s.slur_end;s.x+=12;if(s.x<realwidth-12)s.x=realwidth-12;s2=s;notes++;break}}if(!visible)return false;bm.s2=s2;if(staff_tb[st].y==0){if(two_staves)return false}else{if(!two_staves){bm.s1=s1;bm.a=(s1.ys-s2.ys)/(s1.xs-s2.xs);bm.b=s1.ys-s1.xs*bm.a+staff_tb[st].y;bm.nflags=nflags;return true}}sx=sy=sxx=sxy=syy=0;for(s=s1;;s=s.next){if(s.type!=NOTE)continue;if((scale=s.p_v.scale)==1)scale=staff_tb[s.st].staffscale;if(s.stem>=0)x=stem_xoff+s.notes[0].shhd;else x=-stem_xoff+s.notes[s.nhd].shhd;x*=scale;x+=s.x;s.xs=x;y=s.ys+staff_tb[s.st].y;sx+=x;sy+=y;sxx+=x*x;sxy+=x*y;syy+=y*y;if(s==s2)break}a=(sxy*notes-sx*sy)/(sxx*notes-sx*sx);b=(sy-a*sx)/notes;if(notes>=3){hh=syy-a*sxy-b*sy;if(hh>0&&hh/(notes-2)>.5)a*=BEAM_FLATFAC}if(a>=0)a=BEAM_SLOPE*a/(BEAM_SLOPE+a);else a=BEAM_SLOPE*a/(BEAM_SLOPE-a);a0=a*(s2.xs-s1.xs)/(20*(notes-1));if(a0*a0<BEAM_THRESH*BEAM_THRESH)a=0;b=(sy-a*sx)/notes;if(cfmt.flatbeams){if(!s1.grace)b=-11+staff_tb[st].y;else b=35+staff_tb[st].y;a=0}max_stem_err=0;s=s1;if(two_dir){if(!s1.grace)ys=BEAM_SHIFT;else ys=3.5;ys*=nflags-1;ys+=BEAM_DEPTH;ys*=.5;if(s1.stem!=s2.stem&&s1.nflags<s2.nflags)ys*=s2.stem;else ys*=s1.stem;b+=ys}else if(!s1.grace){var beam_h=BEAM_DEPTH+BEAM_SHIFT*(nflags-1);while(s.ts_prev&&s.ts_prev.type==NOTE&&s.ts_prev.time==s.time&&s.ts_prev.x>s1.xs)s=s.ts_prev;for(;s&&s.time<=s2.time;s=s.ts_next){if(s.type!=NOTE||s.invis||s.st!=st&&s.v!=v){continue}x=s.v==v?s.xs:s.x;ys=a*x+b-staff_tb[s.st].y;if(s.v==v){if(s.nhd==0)stem_err=min_tb[0][s.nflags];else stem_err=min_tb[1][s.nflags];if(s.stem>0){if(s.notes[s.nhd].pit>26){stem_err-=2;if(s.notes[s.nhd].pit>28)stem_err-=2}stem_err-=ys-3*(s.notes[s.nhd].pit-18)}else{if(s.notes[0].pit<18){stem_err-=2;if(s.notes[0].pit<16)stem_err-=2}stem_err-=3*(s.notes[0].pit-18)-ys}stem_err+=BEAM_DEPTH+BEAM_SHIFT*(s.nflags-1)}else{if(s1.stem>0){if(s.stem>0){if(s.ymn>ys+4||s.ymx<ys-beam_h-2)continue;if(s.v>v)stem_err=s.ymx-ys;else stem_err=s.ymn+8-ys}else{stem_err=s.ymx-ys}}else{if(s.stem<0){if(s.ymx<ys-4||s.ymn>ys-beam_h-2)continue;if(s.v<v)stem_err=ys-s.ymn;else stem_err=ys-s.ymx+8}else{stem_err=ys-s.ymn}}stem_err+=2+beam_h}if(stem_err>max_stem_err)max_stem_err=stem_err}}else{for(;;s=s.next){ys=a*s.xs+b-staff_tb[s.st].y;stem_err=GSTEM-2;if(s.stem>0)stem_err-=ys-3*(s.notes[s.nhd].pit-18);else stem_err+=ys-3*(s.notes[0].pit-18);stem_err+=3*(s.nflags-1);if(stem_err>max_stem_err)max_stem_err=stem_err;if(s==s2)break}}if(max_stem_err>0)b+=s1.stem*max_stem_err;if(!two_staves&&!two_dir)for(s=s1.next;;s=s.next){var g;switch(s.type){case REST:g=s.ts_next;if(!g||g.st!=st||g.type!=NOTE&&g.type!=REST)break;case BAR:if(s.invis)break;case CLEF:y=a*s.x+b;if(s1.stem>0){y=s.ymx-y+BEAM_DEPTH+BEAM_SHIFT*(nflags-1)+2;if(y>0)b+=y}else{y=s.ymn-y-BEAM_DEPTH-BEAM_SHIFT*(nflags-1)-2;if(y<0)b+=y}break;case GRACE:for(g=s.extra;g;g=g.next){y=a*g.x+b;if(s1.stem>0){y=g.ymx-y+BEAM_DEPTH+BEAM_SHIFT*(nflags-1)+2;if(y>0)b+=y}else{y=g.ymn-y-BEAM_DEPTH-BEAM_SHIFT*(nflags-1)-2;if(y<0)b+=y}}break}if(s==s2)break}if(a==0)b+=b_pos(s1.grace,s1.stem,nflags,b-staff_tb[st].y);for(s=s1;;s=s.next){var dy;switch(s.type){case NOTE:s.ys=a*s.xs+b-staff_tb[s.st].y;if(s.stem>0){s.ymx=s.ys+2.5;if(s.ts_prev&&s.ts_prev.stem>0&&s.ts_prev.st==s.st&&s.ts_prev.ymn<s.ymx&&s.ts_prev.x==s.x&&s.notes[0].shhd==0){s.ts_prev.x-=5;s.ts_prev.xs-=5}}else{s.ymn=s.ys-2.5}break;case REST:y=a*s.x+b-staff_tb[s.st].y;dy=BEAM_DEPTH+BEAM_SHIFT*(nflags-1)+(s.head!=FULL?4:9);if(s1.stem>0){y-=dy;if(s1.multi==0&&y>12)y=12;if(s.y<=y)break}else{y+=dy;if(s1.multi==0&&y<12)y=12;if(s.y>=y)break}if(s.head!=FULL)y=((y+3+12)/6|0)*6-12;s.y=y;break}if(s==s2)break}if(staff_tb[st].y==0)return false;bm.s1=s1;bm.a=a;bm.b=b;bm.nflags=nflags;return true}function draw_beams(bm){var s,i,beam_dir,shift,bshift,bstub,bh,da,k,k1,k2,x1,s1=bm.s1,s2=bm.s2;function draw_beam(x1,x2,dy,h,bm,n){var y1,dy2,s=bm.s1,nflags=s.nflags;if(s.ntrem)nflags-=s.ntrem;if(s.trem2&&n>nflags){if(s.dur>=BASE_LEN/2){x1=s.x+6;x2=bm.s2.x-6}else if(s.dur<BASE_LEN/4){x1+=5;x2-=6}}y1=bm.a*x1+bm.b-dy;x2-=x1;x2/=stv_g.scale;dy2=bm.a*x2*stv_g.scale;xypath(x1,y1,true);output.push("l"+x2.toFixed(2)+" "+(-dy2).toFixed(2)+"v"+h.toFixed(2)+"l"+(-x2).toFixed(2)+" "+dy2.toFixed(2)+'"/>\n')}anno_start(s1,"beam");if(!s1.grace){bshift=BEAM_SHIFT;bstub=BEAM_STUB;shift=.34;bh=BEAM_DEPTH}else{bshift=3.5;bstub=3.2;shift=.29;bh=1.8}beam_dir=s1.stem;if(s1.stem!=s2.stem&&s1.nflags<s2.nflags)beam_dir=s2.stem;if(beam_dir<0)bh=-bh;draw_beam(s1.xs-shift,s2.xs+shift,0,bh,bm,1);da=0;for(s=s1;;s=s.next){if(s.type==NOTE&&s.stem!=beam_dir)s.ys=bm.a*s.xs+bm.b-staff_tb[s.st].y+bshift*(s.nflags-1)*s.stem-bh;if(s==s2)break}if(s1.feathered_beam){da=bshift/(s2.xs-s1.xs);if(s1.feathered_beam>0){da=-da;bshift=da*s1.xs}else{bshift=da*s2.xs}da=da*beam_dir}shift=0;for(i=2;i<=bm.nflags;i++){shift+=bshift;if(da!=0)bm.a+=da;for(s=s1;;s=s.next){if(s.type!=NOTE||s.nflags<i){if(s==s2)break;continue}if(s.trem1&&i>s.nflags-s.ntrem){if(s.dur>=BASE_LEN/2)x1=s.x;else x1=s.xs;draw_beam(x1-5,x1+5,(shift+2.5)*beam_dir,bh,bm,i);if(s==s2)break;continue}k1=s;while(1){if(s==s2)break;
  k=s.next;if(k.type==NOTE||k.type==REST){if(k.trem1){if(k.nflags-k.ntrem<i)break}else if(k.nflags<i){break}}if(k.beam_br1||k.beam_br2&&i>2)break;s=k}k2=s;while(k2.type!=NOTE)k2=k2.prev;x1=k1.xs;if(k1==k2){if(k1==s1){x1+=bstub}else if(k1==s2){x1-=bstub}else if(k1.beam_br1||k1.beam_br2&&i>2){x1+=bstub}else{k=k1.next;while(k.type!=NOTE)k=k.next;if(k.beam_br1||k.beam_br2&&i>2){x1-=bstub}else{k1=k1.prev;while(k1.type!=NOTE)k1=k1.prev;if(k1.nflags<k.nflags||k1.nflags==k.nflags&&k1.dots<k.dots)x1+=bstub;else x1-=bstub}}}draw_beam(x1,k2.xs,shift*beam_dir,bh,bm,i);if(s==s2)break}}if(s1.tmp)unlksym(s1);else if(s2.tmp)unlksym(s2);anno_stop(s1,"beam")}function draw_lstaff(x){var i,j,yb,h,nst=cur_sy.nstaff,l=0;function draw_sysbra(x,st,flag){var i,st_end,yt,yb;while(cur_sy.staves[st].empty){if(cur_sy.staves[st].flags&flag)return;st++}i=st_end=st;while(1){if(!cur_sy.staves[i].empty)st_end=i;if(cur_sy.staves[i].flags&flag)break;i++}yt=staff_tb[st].y+staff_tb[st].topbar*staff_tb[st].staffscale;yb=staff_tb[st_end].y+staff_tb[st_end].botbar*staff_tb[st_end].staffscale;if(flag&(CLOSE_BRACE|CLOSE_BRACE2))out_brace(x,yb,yt-yb);else out_bracket(x,yt,yt-yb)}for(i=0;;i++){if(cur_sy.staves[i].flags&(OPEN_BRACE|OPEN_BRACKET))l++;if(!cur_sy.staves[i].empty)break;if(cur_sy.staves[i].flags&(CLOSE_BRACE|CLOSE_BRACKET))l--;if(i==nst)break}for(j=nst;j>i;j--){if(!cur_sy.staves[j].empty)break}if(i==j&&l==0)return;yb=staff_tb[j].y+staff_tb[j].botbar*staff_tb[j].staffscale;h=staff_tb[i].y+staff_tb[i].topbar*staff_tb[i].staffscale-yb;xypath(x,yb);output.push("v"+(-h).toFixed(2)+'"/>\n');for(i=0;i<=nst;i++){if(cur_sy.staves[i].flags&OPEN_BRACE)draw_sysbra(x,i,CLOSE_BRACE);if(cur_sy.staves[i].flags&OPEN_BRACKET)draw_sysbra(x,i,CLOSE_BRACKET);if(cur_sy.staves[i].flags&OPEN_BRACE2)draw_sysbra(x-6,i,CLOSE_BRACE2);if(cur_sy.staves[i].flags&OPEN_BRACKET2)draw_sysbra(x-6,i,CLOSE_BRACKET2)}}function draw_meter(x,s){if(!s.a_meter)return;var dx,i,st=s.st,y=staff_tb[st].y;x-=s.wl;for(i=0;i<s.a_meter.length;i++){var f,meter=s.a_meter[i];if(meter.top=="C|")dx=13;else dx=13*meter.top.length;if(meter.bot){if(meter.bot.length>meter.top.length)dx=13*meter.bot.length;out_XYAB('<g style="font:bold 16px serif"\n\ttransform="translate(X,Y) scale(1.2,1)">\n\t<text y="-12" text-anchor="middle">A</text>\n\t<text text-anchor="middle">B</text>\n</g>\n',x+dx*.5,y,meter.top,meter.bot)}else{switch(meter.top[0]){case"C":f=meter.top[1]!="|"?"csig":"ctsig";x-=5;y+=12;break;case"c":f=meter.top[1]!="."?"imsig":"iMsig";break;case"o":f=meter.top[1]!="."?"pmsig":"pMsig";break;default:out_XYAB('<g style="font:bold 18px serif"\n\ttransform="translate(X,Y) scale(1.2,1)">\n\t<text y="-6" text-anchor="middle">A</text>\n</g>\n',x+dx*.5,y,meter.top);break}}if(f)xygl(x+dx*.5,y,f);x+=dx}}function draw_acc(x,y,acc,micro_n,micro_d){if(micro_n){if(micro_n==micro_d){if(acc==-1)acc=-2;else acc=2}else if(micro_n*2!=micro_d){xygl(x,y,"acc"+acc+"_"+micro_n+"_"+micro_d);return}}xygl(x,y,"acc"+acc)}function draw_hl(x,yl,yu,st,hltype){var i,p_staff=staff_tb[st],staffb=p_staff.y,top=(p_staff.stafflines.length-1)*6;yl=Math.ceil(yl/6)*6;for(;yl<p_staff.botline;yl+=6)xygl(x,staffb+yl,hltype);yu-=yu%6;for(;yu>top;yu-=6)xygl(x,staffb+yu,hltype)}const sharp_cl=[24,9,15,21,6,12,18],flat_cl=[12,18,24,9,15,21,6],sharp1=[-9,12,-9,-9,12,-9],sharp2=[12,-9,12,-9,12,-9],flat1=[9,-12,9,-12,9,-12],flat2=[-12,9,-12,9,-12,9];function draw_keysig(p_voice,x,s){if(s.k_none)return;var old_sf=s.k_old_sf,st=p_voice.st,staffb=staff_tb[st].y,i,shift,p_seq,clef_ix=s.k_y_clef;if(clef_ix&1)clef_ix+=7;clef_ix/=2;while(clef_ix<0)clef_ix+=7;clef_ix%=7;if(!s.k_a_acc){if(cfmt.cancelkey||s.k_sf==0){if(s.k_sf==0||old_sf*s.k_sf<0){shift=sharp_cl[clef_ix];p_seq=shift>9?sharp1:sharp2;for(i=0;i<old_sf;i++){xygl(x,staffb+shift,"acc3");shift+=p_seq[i];x+=5.5}shift=flat_cl[clef_ix];p_seq=shift<18?flat1:flat2;for(i=0;i>old_sf;i--){xygl(x,staffb+shift,"acc3");shift+=p_seq[-i];x+=5.5}if(s.k_sf!=0)x+=3}}if(s.k_sf>0){shift=sharp_cl[clef_ix];p_seq=shift>9?sharp1:sharp2;for(i=0;i<s.k_sf;i++){xygl(x,staffb+shift,"acc1");shift+=p_seq[i];x+=5.5}if(cfmt.cancelkey&&i<old_sf){x+=2;for(;i<old_sf;i++){xygl(x,staffb+shift,"acc3");shift+=p_seq[i];x+=5.5}}}if(s.k_sf<0){shift=flat_cl[clef_ix];p_seq=shift<18?flat1:flat2;for(i=0;i>s.k_sf;i--){xygl(x,staffb+shift,"acc-1");shift+=p_seq[-i];x+=5.5}if(cfmt.cancelkey&&i>old_sf){x+=2;for(;i>old_sf;i--){xygl(x,staffb+shift,"acc3");shift+=p_seq[-i];x+=5.5}}}}else{var acc,last_acc=s.k_a_acc[0].acc,last_shift=100;for(i=0;i<s.k_a_acc.length;i++){acc=s.k_a_acc[i];shift=(s.k_y_clef+acc.pit-18)*3;if(i!=0&&(shift>last_shift+18||shift<last_shift-18))x-=5.5;else if(acc.acc!=last_acc)x+=3;last_acc=acc.acc;draw_hl(x,shift,shift,st,"hl");last_shift=shift;draw_acc(x,staffb+shift,acc.acc,acc.micro_n,acc.micro_d);x+=5.5}}}function bar_cnv(bar_type){switch(bar_type){case"[":case"[]":return"";case"|:":case"|::":case"|:::":return"["+bar_type;case":|":case"::|":case":::|":return bar_type+"]";case"::":return cfmt.dblrepbar;case"||:":return"[|:"}return bar_type}function draw_bar(s,bot,h){var i,yb,bar_type,st=s.st,x=s.x;if(s.bar_mrep){yb=staff_tb[st].y+12;set_scale(s);if(s.bar_mrep==1){for(var s2=s.prev;s2.type!=REST;s2=s2.prev);xygl(s2.x,yb,"mrep")}else{xygl(x,yb,"mrep2");if(s.v==cur_sy.top_voice){set_font("annotation");xy_str(x,yb+staff_tb[st].topbar-9,s.bar_mrep.toString(),"c")}}}if(st!=0&&s.ts_prev&&s.ts_prev.type!=BAR)h=staff_tb[st].topbar*staff_tb[st].staffscale;bar_type=bar_cnv(s.bar_type);if(!bar_type)return;for(i=bar_type.length;--i>=0;){switch(bar_type[i]){case"|":set_sscale(-1);if(s.bar_dotted)out_dotbar(x,bot,h);else out_bar(x,bot,h);break;default:x-=3;set_sscale(-1);out_thbar(x,bot,h);break;case":":x-=2;set_sscale(st);xygl(x+1,staff_tb[st].y,"rdots");break}x-=3}}const rest_tb=["r128","r64","r32","r16","r8","r4","r2","r1","r0","r00"];function draw_rest(s){var s2,i,j,x,y,dotx,staffb,yb,yt,head,p_staff=staff_tb[s.st];if(p_staff.empty)return;if(s.dur==s.p_v.meter.wmeasure||s.rep_nb&&s.rep_nb>=0){s2=s.ts_next;while(s2&&s2.time!=s.time+s.dur)s2=s2.ts_next;x=s2?s2.x:realwidth;s2=s;while(!s2.seqst)s2=s2.ts_prev;s2=s2.ts_prev;x=(x+s2.x)/2;if(s.a_dd)deco_update(s,x-s.x);s.x=x}else{x=s.x;if(s.notes[0].shhd)x+=s.notes[0].shhd*stv_g.scale}if(s.invis)return;staffb=p_staff.y;if(s.rep_nb){staffb+=12;if(s.rep_nb<0){xygl(x,staffb,"srep")}else{xygl(x,staffb,"mrep");if(s.rep_nb>2&&s.v==cur_sy.top_voice){set_font("annotation");xy_str(x,staffb+p_staff.topbar-9,s.rep_nb.toString(),"c")}}return}y=s.y;i=5-s.nflags;if(i==7&&y==12&&p_staff.stafflines.length<=2)y-=6;xygl(x,y+staffb,s.notes[0].head?s.notes[0].head:rest_tb[i]);if(i>=6){j=y/6;switch(i){default:if(p_staff.stafflines[j+1]!="|")xygl(x,y+6+staffb,"hl1");if(i==9){y-=6;j--}break;case 7:y+=6;j++;case 6:break}if(p_staff.stafflines[j]!="|")xygl(x,y+staffb,"hl1")}x+=8;y+=staffb+3;for(i=0;i<s.dots;i++){xygl(x,y,"dot");x+=3.5}}function draw_gracenotes(s){var yy,x0,y0,x1,y1,x2,y2,x3,y3,bet1,bet2,dy1,dy2,g,last,note,bm={};for(g=s.extra;g;g=g.next){if(g.beam_st&&!g.beam_end){if(calculate_beam(bm,g))draw_beams(bm)}anno_start(g);draw_note(g,!bm.s2);if(g==bm.s2)bm.s2=null;if(g.sappo){if(!g.next){x1=9;y1=g.stem>0?5:-5}else{x1=(g.next.x-g.x)*.5+4;y1=(g.ys+g.next.ys)*.5-g.y;if(g.stem>0)y1-=1;else y1+=1}note=g.notes[g.stem<0?0:g.nhd];out_acciac(x_head(g,note),y_head(g,note),x1,y1,g.stem>0)}anno_stop(g);if(!g.next)break}if(s.p_v.key.k_bagpipe||!cfmt.graceslurs||s.slur_start||!s.next||s.next.type!=NOTE)return;last=g;if(last.stem>=0){yy=127;for(g=s.extra;g;g=g.next){if(g.y<yy){yy=g.y;last=g}}x0=last.x;y0=last.y-5;if(s.extra!=last){x0-=4;y0+=1}s=s.next;x3=s.x-1;if(s.stem<0)x3-=4;y3=3*(s.notes[0].pit-18)-5;dy1=(x3-x0)*.4;if(dy1>3)dy1=3;dy2=dy1;bet1=.2;bet2=.8;if(y0>y3+7){x0=last.x-1;y0+=.5;y3+=6.5;x3=s.x-5.5;dy1=(y0-y3)*.8;dy2=(y0-y3)*.2;bet1=0}else if(y3>y0+4){y3=y0+4;x0=last.x+2;y0=last.y-4}}else{yy=-127;for(g=s.extra;g;g=g.next){if(g.y>yy){yy=g.y;last=g}}x0=last.x;y0=last.y+5;if(s.extra!=last){x0-=4;y0-=1}s=s.next;x3=s.x-1;if(s.stem>=0)x3-=2;y3=3*(s.notes[s.nhd].pit-18)+5;dy1=(x0-x3)*.4;if(dy1<-3)dy1=-3;dy2=dy1;bet1=.2;bet2=.8;if(y0<y3-7){x0=last.x-1;y0-=.5;y3-=6.5;x3=s.x-5.5;dy1=(y0-y3)*.8;dy2=(y0-y3)*.2;bet1=0}else if(y3<y0-4){y3=y0-4;x0=last.x+2;y0=last.y+4}}x1=bet1*x3+(1-bet1)*x0-x0;y1=bet1*y3+(1-bet1)*y0-dy1-y0;x2=bet2*x3+(1-bet2)*x0-x0;y2=bet2*y3+(1-bet2)*y0-dy2-y0;anno_start(s,"slur");xypath(x0,y0+staff_tb[s.st].y);output.push("c"+x1.toFixed(2)+" "+(-y1).toFixed(2)+" "+x2.toFixed(2)+" "+(-y2).toFixed(2)+" "+(x3-x0).toFixed(2)+" "+(-y3+y0).toFixed(2)+'"/>\n');anno_stop(s,"slur")}function setdoty(s,y_tb){var m,m1,y;for(m=0;m<=s.nhd;m++){y=3*(s.notes[m].pit-18);if(y%6==0){if(s.dot_low)y-=3;else y+=3}y_tb[m]=y}for(m=0;m<s.nhd;m++){if(y_tb[m+1]>y_tb[m])continue;m1=m;while(m1>0){if(y_tb[m1]>y_tb[m1-1]+6)break;m1--}if(3*(s.notes[m1].pit-18)-y_tb[m1]<y_tb[m+1]-3*(s.notes[m+1].pit-18)){while(m1<=m)y_tb[m1++]-=6}else{y_tb[m+1]=y_tb[m]+6}}}function x_head(s,note){return s.x+note.shhd}function y_head(s,note){return staff_tb[s.st].y+3*(note.pit-18)}function draw_basic_note(x,s,m,y_tb){var i,k,y,p,yy,dotx,doty,old_color=false,note=s.notes[m],staffb=staff_tb[s.st].y,y=3*(note.pit-18),shhd=note.shhd*stv_g.scale,x_note=x+shhd,y_note=y+staffb;if(s.invis)return;var elts=identify_note(s,note.dur),head=elts[0],dots=elts[1],nflags=elts[2];if(y%6==0&&shhd!=(s.stem>0?s.notes[0].shhd:s.notes[s.nhd].shhd)){yy=0;if(y>=30){yy=y;if(yy%6)yy-=3}else if(y<=-6){yy=y;if(yy%6)yy+=3}if(yy)xygl(x_note,yy+staffb,"hl")}if(note.invis){}else if(note.head){p=note.head}else if(s.grace){p="ghd";x_note-=4.5*stv_g.scale}else if(note.map&&note.map[0]){i=-s.nflags;if(i<0)i=0;p=note.map[0][i];if(!p)p=note.map[0][note.map[0].length-1];i=p.indexOf("/");if(i>=0){if(s.stem>=0)p=p.slice(0,i);else p=p.slice(i+1)}}else if(s.type==CUSTOS){p="custos"}else{switch(head){case OVAL:p="HD";break;case OVALBARS:if(s.head!=SQUARE){p="HDD";break}case SQUARE:p=note.dur<BASE_LEN*4?"breve":"longa";if(!tsnext&&s.next&&s.next.type==BAR&&!s.next.next)dots=0;break;case EMPTY:p="Hd";break;default:p="hd";break}}if(note.color)old_color=set_color(note.color);else if(note.map&&note.map[2])old_color=set_color(note.map[2]);if(p){if(!psxygl(x_note,y_note,p))xygl(x_note,y_note,p)}if(dots){dotx=x+(7.7+s.xmx)*stv_g.scale;if(y_tb[m]==undefined){y_tb[m]=3*(s.notes[m].pit-18);if((s.notes[m].pit&1)==0)y_tb[m]+=3}doty=y_tb[m]+staffb;while(--dots>=0){xygl(dotx,doty,"dot");dotx+=3.5}}if(note.acc){x-=note.shac*stv_g.scale;if(!s.grace){draw_acc(x,y+staffb,note.acc,note.micro_n,note.micro_d)}else{g_open(x,y+staffb,0,.75);draw_acc(0,0,note.acc,note.micro_n,note.micro_d);g_close()}}if(old_color!=false)set_color(old_color)}function draw_note(s,fl){var s2,i,m,y,staffb,slen,c,hltype,nflags,x,y,note,y_tb=new Array(s.nhd+1);if(s.dots)setdoty(s,y_tb);note=s.notes[s.stem<0?s.nhd:0];x=x_head(s,note);staffb=staff_tb[s.st].y;if(!s.invis){if(s.grace){hltype="ghl"}else{switch(s.head){default:hltype="hl";break;case OVAL:case OVALBARS:hltype="hl1";break;case SQUARE:hltype="hl2";break}}draw_hl(x,3*(s.notes[0].pit-18),3*(s.notes[s.nhd].pit-18),s.st,hltype)}y=y_head(s,note);if(!s.invis&&!s.stemless){slen=s.ys-s.y;nflags=s.nflags;if(s.ntrem)nflags-=s.ntrem;if(!fl||nflags<=0){if(s.nflags>0){if(s.stem>=0)slen-=1;else slen+=1}out_stem(x,y,slen,s.grace)}else{out_stem(x,y,slen,s.grace,nflags,cfmt.straightflags)}}else if(s.xstem){s2=s.ts_prev;slen=(s2.stem>0?s2.y:s2.ys)-s.y;slen+=staff_tb[s2.st].y-staffb;slen/=s.p_v.scale;out_stem(x,y,slen)}if(!s.invis&&fl&&s.trem1){var ntrem=s.ntrem||0,x1=x;slen=3*(s.notes[s.stem>0?s.nhd:0].pit-18);if(s.head==FULL||s.head==EMPTY){x1+=(s.grace?GSTEM_XOFF:3.5)*s.stem;if(s.stem>0)slen+=6+5.4*ntrem;else slen-=6+5.4}else{if(s.stem>0)slen+=5+5.4*ntrem;else slen-=5+5.4}slen/=s.p_v.scale;out_trem(x1,staffb+slen,ntrem)}x=s.x;for(m=0;m<=s.nhd;m++)draw_basic_note(x,s,m,y_tb)}function next_scut(s){var prev=s;for(s=s.next;s;s=s.next){if(s.rbstop)return s;prev=s}return prev}function prev_scut(s){while(s.prev){s=s.prev;if(s.rbstart)return s}s=s.p_v.sym;while(s.type!=CLEF)s=s.ts_prev;if(s.next&&s.next.type==KEY)s=s.next;if(s.next&&s.next.type==METER)return s.next;return s}function slur_direction(k1,k2){var s,some_upstem,low;if(k1.grace&&k1.stem>0)return-1;for(s=k1;;s=s.next){if(s.type==NOTE){if(!s.stemless){if(s.stem<0)return 1;some_upstem=true}if(s.notes[0].pit<22)low=true}if(s==k2)break}if(!some_upstem&&!low)return 1;return-1}function slur_out(x1,y1,x2,y2,dir,height,dotted){var dx,dy,dz,alfa=.3,beta=.45;dy=y2-y1;if(dy<0)dy=-dy;dx=x2-x1;if(dx>40&&dy/dx<.7){alfa=.3+.002*(dx-40);if(alfa>.7)alfa=.7}var mx=.5*(x1+x2),my=.5*(y1+y2),xx1=mx+alfa*(x1-mx),yy1=my+alfa*(y1-my)+height;xx1=x1+beta*(xx1-x1);yy1=y1+beta*(yy1-y1);var xx2=mx+alfa*(x2-mx),yy2=my+alfa*(y2-my)+height;xx2=x2+beta*(xx2-x2);yy2=y2+beta*(yy2-y2);dx=.03*(x2-x1);dy=2*dir;dz=.2+.001*(x2-x1);if(dz>.6)dz=.6;dz*=dir;var scale_y=stv_g.v?stv_g.scale:1;if(!dotted)output.push('<path class="fill" d="M');else output.push('<path class="stroke" stroke-dasharray="5,5" d="M');out_sxsy(x1," ",y1);output.push("c"+((xx1-x1)/stv_g.scale).toFixed(2)+" "+((y1-yy1)/scale_y).toFixed(2)+" "+((xx2-x1)/stv_g.scale).toFixed(2)+" "+((y1-yy2)/scale_y).toFixed(2)+" "+((x2-x1)/stv_g.scale).toFixed(2)+" "+((y1-y2)/scale_y).toFixed(2));if(!dotted)output.push("\n\tv"+(-dz).toFixed(2)+"c"+((xx2-dx-x2)/stv_g.scale).toFixed(2)+" "+((y2+dz-yy2-dy)/scale_y).toFixed(2)+" "+((xx1+dx-x2)/stv_g.scale).toFixed(2)+" "+((y2+dz-yy1-dy)/scale_y).toFixed(2)+" "+((x1-x2)/stv_g.scale).toFixed(2)+" "+((y2+dz-y1)/scale_y).toFixed(2));output.push('"/>\n')}function slur_multi(k1,k2){while(1){if(k1.multi)return k1.multi;if(k1==k2)break;k1=k1.next}return 0}function draw_slur(k1_o,k2,m1,m2,slur_type){var k1=k1_o,k,g,x1,y1,x2,y2,height,addy,a,y,z,h,dx,dy,dir;while(k1.v!=k2.v)k1=k1.ts_next;switch(slur_type&7){case SL_ABOVE:dir=1;break;case SL_BELOW:dir=-1;break;default:dir=slur_multi(k1,k2);if(!dir)dir=slur_direction(k1,k2);break}var nn=1,upstaff=k1.st,two_staves=false;if(k1!=k2){k=k1.next;while(1){if(k.type==NOTE||k.type==REST){nn++;if(k.st!=upstaff){two_staves=true;if(k.st<upstaff)upstaff=k.st}}if(k==k2)break;k=k.next}}if(two_staves)error(2,k1,"*** multi-staves slurs not treated yet");x1=k1_o.x;if(k1_o.notes&&k1_o.notes[0].shhd)x1+=k1_o.notes[0].shhd;if(k1_o!=k2){x2=k2.x;if(k2.notes)x2+=k2.notes[0].shhd}else{for(k=k2.ts_next;k;k=k.ts_next)if(k.type==STAVES)break;if(!k)x2=realwidth;else x2=k.x}if(m1>=0){y1=3*(k1.notes[m1].pit-18)+5*dir}else{y1=dir>0?k1.ymx+2:k1.ymn-2;if(k1.type==NOTE){if(dir>0){if(k1.stem>0){x1+=5;if(k1.beam_end&&k1.nflags>=-1&&!k1.in_tuplet){if(k1.nflags>0){x1+=2;y1=k1.ys-3}else{y1=k1.ys-6}}}}else{if(k1.stem<0){x1-=1;if(k2.grace){y1=k1.y-8}else if(k1.beam_end&&k1.nflags>=-1&&(!k1.in_tuplet||k1.ys<y1+3)){if(k1.nflags>0){x1+=2;y1=k1.ys+3}else{y1=k1.ys+6}}}}}}if(m2>=0){y2=3*(k2.notes[m2].pit-18)+5*dir}else{y2=dir>0?k2.ymx+2:k2.ymn-2;if(k2.type==NOTE){if(dir>0){if(k2.stem>0){x2+=1;if(k2.beam_st&&k2.nflags>=-1&&!k2.in_tuplet)y2=k2.ys-6}}else{if(k2.stem<0){x2-=5;if(k2.beam_st&&k2.nflags>=-1&&!k2.in_tuplet)y2=k2.ys+6}}}}if(k1.type!=NOTE){y1=y2+1.2*dir;x1=k1.x+k1.wr*.5;if(x1>x2-12)x1=x2-12}if(k2.type!=NOTE){if(k1.type==NOTE)y2=y1+1.2*dir;else y2=y1;if(k1!=k2)x2=k2.x-k2.wl*.3}if(nn>=3){if(k1.next.type!=BAR&&k1.next.x<x1+48){if(dir>0){y=k1.next.ymx-2;if(y1<y)y1=y}else{y=k1.next.ymn+2;if(y1>y)y1=y}}if(k2.prev&&k2.prev.type!=BAR&&k2.prev.x>x2-48){if(dir>0){y=k2.prev.ymx-2;if(y2<y)y2=y}else{y=k2.prev.ymn+2;if(y2>y)y2=y}}}a=(y2-y1)/(x2-x1);if(a>SLUR_SLOPE||a<-SLUR_SLOPE){a=a>SLUR_SLOPE?SLUR_SLOPE:-SLUR_SLOPE;if(a*dir>0)y1=y2-a*(x2-x1);else y2=y1+a*(x2-x1)}y=y2-y1;if(y>8)y=8;else if(y<-8)y=-8;z=y;if(z<0)z=-z;dx=.5*z;dy=.3*y;if(y*dir>0){x2-=dx;y2-=dy}else{x1+=dx;y1+=dy}if(k1.grace)x1=k1.x-GSTEM_XOFF*.5;if(k2.grace)x2=k2.x+GSTEM_XOFF*1.5;h=0;a=(y2-y1)/(x2-x1);if(k1!=k2&&k1.v==k2.v){addy=y1-a*x1;for(k=k1.next;k!=k2;k=k.next){if(k.st!=upstaff)continue;switch(k.type){case NOTE:case REST:if(dir>0){y=3*(k.notes[k.nhd].pit-18)+6;if(y<k.ymx)y=k.ymx;y-=a*k.x+addy;if(y>h)h=y}else{y=3*(k.notes[0].pit-18)-6;if(y>k.ymn)y=k.ymn;y-=a*k.x+addy;if(y<h)h=y}break;case GRACE:for(g=k.extra;g;g=g.next){if(dir>0){y=3*(g.notes[g.nhd].pit-18)+6;if(y<g.ymx)y=g.ymx;y-=a*g.x+addy;if(y>h)h=y}else{y=3*(g.notes[0].pit-18)-6;if(y>g.ymn)y=g.ymn;y-=a*g.x+addy;if(y<h)h=y}}break}}y1+=.45*h;y2+=.45*h;h*=.65}if(nn>3)height=(.08*(x2-x1)+12)*dir;else height=(.03*(x2-x1)+8)*dir;if(dir>0){if(height<3*h)height=3*h;if(height>40)height=40}else{if(height>3*h)height=3*h;if(height<-40)height=-40}y=y2-y1;if(y<0)y=-y;if(dir>0){if(height<.8*y)height=.8*y}else{if(height>-.8*y)height=-.8*y}height*=cfmt.slurheight;slur_out(x1,y1,x2,y2,dir,height,slur_type&SL_DOTTED);dx=x2-x1;a=(y2-y1)/dx;addy=y1-a*x1+.4*height;if(k1.v==k2.v)for(k=k1;k!=k2;k=k.next){if(k.st!=upstaff)continue;y=a*k.x+addy;if(k.ymx<y)k.ymx=y;else if(k.ymn>y)k.ymn=y;if(k.next==k2){dx=x2;if(k2.sl1)dx-=5}else{dx=k.next.x}if(k!=k1)x1=k.x;dx-=x1;y_set(upstaff,dir>0,x1,dx,y)}return(dir>0?SL_ABOVE:SL_BELOW)|slur_type&SL_DOTTED}function draw_slurs(first,last){var s1,k,gr1,gr2,i,m1,m2,slur_type,cont,s=first;while(1){if(!s||s==last){if(!gr1||!(s=gr1.next)||s==last)break;gr1=null}if(s.type==GRACE){gr1=s;s=s.extra;continue}if(s.type!=NOTE&&s.type!=REST&&s.type!=SPACE||!s.slur_start&&!s.sl1){s=s.next;continue}k=null;s1=s.next;var gr1_out=false;while(1){if(!s1){if(gr2){s1=gr2.next;gr2=null;continue}if(!gr1||gr1_out)break;s1=gr1.next;gr1_out=true;continue}if(s1.type==GRACE){gr2=s1;s1=s1.extra;continue}if(s1.type==BAR&&(s1.bar_type[0]==":"||s1.bar_type=="|]"||s1.bar_type=="[|"||s1.text&&s1.text[0]!="1")){k=s1;break}if(s1.type!=NOTE&&s1.type!=REST&&s1.type!=SPACE){s1=s1.next;continue}if(s1.slur_end||s1.sl2){k=s1;break}if(s1.slur_start||s1.sl1){if(gr2){for(k=s1;k.next;k=k.next);k.next=gr2.next;if(gr2.next)gr2.next.prev=k;k=null}draw_slurs(s1,last);if(gr2&&gr2.next){gr2.next.prev.next=null;gr2.next.prev=gr2}}if(s1==last)break;s1=s1.next}if(!s1){k=next_scut(s)}else if(!k){s=s1;if(s==last)break;continue}if(gr1){for(s1=s;s1.next;s1=s1.next);s1.next=gr1.next;if(gr1.next)gr1.next.prev=s1;gr1.slur_start=SL_AUTO}if(gr2){gr2.prev.next=gr2.extra;gr2.extra.prev=gr2.prev;gr2.slur_start=SL_AUTO}if(s.slur_start){slur_type=s.slur_start&15;s.slur_start>>=4;m1=-1}else{for(m1=0;m1<=s.nhd;m1++)if(s.notes[m1].sl1)break;slur_type=s.notes[m1].sl1&15;s.notes[m1].sl1>>=4;s.sl1--}m2=-1;cont=0;if((k.type==NOTE||k.type==REST||k.type==SPACE)&&(k.slur_end||k.sl2)){if(k.slur_end){k.slur_end--}else{for(m2=0;m2<=k.nhd;m2++)if(k.notes[m2].sl2)break;k.notes[m2].sl2--;k.sl2--}}else{if(k.type!=BAR||k.bar_type[0]!=":"&&k.bar_type!="|]"&&k.bar_type!="[|"&&(!k.text||k.text[0]=="1"))cont=1}slur_type=draw_slur(s,k,m1,m2,slur_type);if(cont){k.p_v.slur_start<<=4;k.p_v.slur_start+=slur_type}if(gr1&&gr1.next){gr1.next.prev.next=null;gr1.next.prev=gr1}if(gr2){gr2.prev.next=gr2;gr2.extra.prev=null}if(s.slur_start||s.sl1)continue;if(s==last)break;s=s.next}}function draw_tuplet(s1,lvl){var s2,s3,g,upstaff,nb_only,some_slur,x1,x2,y1,y2,xm,ym,a,s0,yy,yx,dy,a,b,dir,p,q;upstaff=s1.st;for(s2=s1;s2;s2=s2.next){if(s2.type!=NOTE&&s2.type!=REST){if(s2.type==GRACE){for(g=s2.extra;g;g=g.next){if(g.slur_start||g.sl1)some_slur=true}}continue}if(s2.slur_start||s2.slur_end||s2.sl1||s2.sl2)some_slur=true;if(s2.st<upstaff)upstaff=s2.st;if(lvl==0){if(s2.tp1)draw_tuplet(s2,1);if(s2.te0)break}else if(s2.te1)break}if(lvl==0){p=s1.tp0;q=s1.tq0}else{p=s1.tp1;q=s1.tq1}if(s1.tf[0]==1){s1.in_tuplet=false;if(lvl==0)s1.tp0=0;else s1.tp1=0;return}dir=s1.tf[3];if(!dir)dir=s1.stem>0?SL_ABOVE:SL_BELOW;if(s1.tf[1]==1){nb_only=true;draw_slur(s1,s2,-1,-1,dir)}else{if(s1.tf[0]==2||s1.type!=NOTE||s2.type!=NOTE){nb_only=false}else{nb_only=true;for(s3=s1;;s3=s3.next){if(s3.type!=NOTE&&s3.type!=REST){if(s3.type==GRACE||s3.type==SPACE)continue;nb_only=false;break}if(s3==s2)break;if(s3.beam_end){nb_only=false;break}}if(nb_only&&!s1.beam_st&&!s1.beam_br1&&!s1.beam_br2){for(s3=s1.prev;s3;s3=s3.prev){if(s3.type==NOTE||s3.type==REST){if(s3.nflags>=s1.nflags)nb_only=false;break}}}if(nb_only&&!s2.beam_end){for(s3=s2.next;s3;s3=s3.next){if(s3.type==NOTE||s3.type==REST){if(!s3.beam_br1&&!s3.beam_br2&&s3.nflags>=s2.nflags)nb_only=false;break}}}}}if(nb_only){if(s1.tf[2]==1)return;xm=(s2.x+s1.x)/2;if(s1==s2)a=0;else a=(s2.ys-s1.ys)/(s2.x-s1.x);b=s1.ys-a*s1.x;yy=a*xm+b;if(dir==SL_ABOVE){ym=y_get(upstaff,1,xm-4,8);if(ym>yy)b+=ym-yy;b+=2}else{ym=y_get(upstaff,0,xm-4,8);if(ym<yy)b+=ym-yy;b-=10}for(s3=s1;;s3=s3.next){if(s3.x>=xm)break}if(s1.stem*s2.stem>0){if(s1.stem>0)xm+=1.5;else xm-=1.5}ym=a*xm+b;if(s1.tf[2]==0)out_bnum(xm,ym,p);else out_bnum(xm,ym,p+":"+q);if(dir==SL_ABOVE){ym+=10;if(s3.ymx<ym)s3.ymx=ym;y_set(upstaff,true,xm-3,6,ym)}else{if(s3.ymn>ym)s3.ymn=ym;y_set(upstaff,false,xm-3,6,ym)}s1.in_tuplet=false;if(lvl==0)s1.tp0=0;else s1.tp1=0;return}if(some_slur){draw_slurs(s1,s2);if(s1.slur_start||s1.sl1)return;for(s3=s1.next;s3!=s2;s3=s3.next){if(s3.slur_start||s3.slur_end||s3.sl1||s3.sl2)return}if(s2.slur_end||s2.sl2)return}if(s1.tf[1]!=0)error(2,s1,"'what' value of %%tuplets not yet coded");dir=s1.tf[3];if(!dir)dir=s1.multi>=0?SL_ABOVE:SL_BELOW;if(dir==SL_ABOVE){x1=s1.x-4;y1=24;if(s1.st==upstaff){s3=s1;if(s3.type!=NOTE){for(s3=s3.next;s3!=s2;s3=s3.next)if(s3.type==NOTE)break}ym=y_get(upstaff,1,s3.x-4,8);if(ym>y1)y1=ym;if(s1.stem>0)x1+=3}y2=24;if(s2.st==upstaff){s3=s2;if(s3.type!=NOTE){for(s3=s3.prev;s3!=s1;s3=s3.prev)if(s3.type==NOTE)break}ym=y_get(upstaff,1,s3.x-4,8);if(ym>y2)y2=ym}if(s2.dur>s2.prev.dur){if(s2.next)x2=s2.next.x-s2.next.wl-5;else x2=realwidth-6}else{x2=s2.x+4;r=s2.stem>=0?0:s2.nhd;if(s2.notes[r].shhd>0)x2+=s2.notes[r].shhd;if(s2.st==upstaff&&s2.stem>0)x2+=3.5}xm=.5*(x1+x2);ym=.5*(y1+y2);a=(y2-y1)/(x2-x1);s0=3*(s2.notes[s2.nhd].pit-s1.notes[s1.nhd].pit)/(x2-x1);if(s0>0){if(a<0)a=0;else if(a>s0)a=s0}else{if(a>0)a=0;else if(a<s0)a=s0}if(a*a<.1*.1)a=0;dy=0;for(s3=s1;;s3=s3.next){if(!s3.dur||s3.st!=upstaff){if(s3==s2)break;continue}yy=ym+(s3.x-xm)*a;yx=y_get(upstaff,1,s3.x-4,8);if(yx-yy>dy)dy=yx-yy;if(s3==s2)break}ym+=dy+2;y1=ym+a*(x1-xm);y2=ym+a*(x2-xm);out_tubr(x1,y1+4,x2-x1,y2-y1,true);ym+=8;for(s3=s1;;s3=s3.next){if(s3.st==upstaff){yy=ym+(s3.x-xm)*a;if(s3.ymx<yy)s3.ymx=yy;if(s3==s2)break;y_set(upstaff,true,s3.x,s3.next.x-s3.x,yy)}else if(s3==s2){break}}}else{x1=s1.x-7;if(s2.dur>s2.prev.dur){if(s2.next)x2=s2.next.x-s2.next.wl-8;else x2=realwidth-6}else{x2=s2.x+2;if(s2.notes[s2.nhd].shhd>0)x2+=s2.notes[s2.nhd].shhd}if(s1.stem>=0){x1+=2;x2+=2}if(s1.st==upstaff){s3=s1;if(s3.type!=NOTE){for(s3=s3.next;s3!=s2;s3=s3.next)if(s3.type==NOTE)break}y1=y_get(upstaff,0,s3.x-4,8)}else{y1=0}if(s2.st==upstaff){s3=s2;if(s3.type!=NOTE){for(s3=s3.prev;s3!=s1;s3=s3.prev)if(s3.type==NOTE)break}y2=y_get(upstaff,0,s3.x-4,8)}else{y2=0}xm=.5*(x1+x2);ym=.5*(y1+y2);a=(y2-y1)/(x2-x1);s0=3*(s2.notes[0].pit-s1.notes[0].pit)/(x2-x1);if(s0>0){if(a<0)a=0;else if(a>s0)a=s0}else{if(a>0)a=0;else if(a<s0)a=s0}if(a*a<.1*.1)a=0;dy=0;for(s3=s1;;s3=s3.next){if(!s3.dur||s3.st!=upstaff){if(s3==s2)break;continue}yy=ym+(s3.x-xm)*a;yx=y_get(upstaff,0,s3.x-4,8);if(yx-yy<dy)dy=yx-yy;if(s3==s2)break}ym+=dy-10;y1=ym+a*(x1-xm);y2=ym+a*(x2-xm);out_tubr(x1,y1+4,x2-x1,y2-y1);ym-=2;for(s3=s1;;s3=s3.next){if(s3.st==upstaff){if(s3==s2)break;yy=ym+(s3.x-xm)*a;if(s3.ymn>yy)s3.ymn=yy;y_set(upstaff,false,s3.x,s3.next.x-s3.x,yy)}if(s3==s2)break}}if(s1.tf[2]==1){s1.in_tuplet=false;if(lvl==0)s1.tp0=0;else s1.tp1=0;return}yy=.5*(y1+y2);if(s1.tf[2]==0)out_bnum(xm,yy,p,true);else out_bnum(xm,yy,p+":"+q,true);if(dir==SL_ABOVE){yy+=9;y_set(upstaff,true,xm-3,6,yy)}else{y_set(upstaff,false,xm-3,6,yy)}s1.in_tuplet=false;if(lvl==0)s1.tp0=0;else s1.tp1=0}function draw_note_ties(k1,k2,mhead1,mhead2,job){var i,dir,m1,m2,p,p2,y,st,k,x1,x2,h,sh,time;for(i=0;i<mhead1.length;i++){m1=mhead1[i];p=k1.notes[m1].pit;m2=mhead2[i];p2=job!=2?k2.notes[m2].pit:p;dir=(k1.notes[m1].ti1&7)==SL_ABOVE?1:-1;x1=k1.x;sh=k1.notes[m1].shhd;if(dir>0){if(m1<k1.nhd&&p+1==k1.notes[m1+1].pit)if(k1.notes[m1+1].shhd>sh)sh=k1.notes[m1+1].shhd}else{if(m1>0&&p==k1.notes[m1-1].pit+1)if(k1.notes[m1-1].shhd>sh)sh=k1.notes[m1-1].shhd}x1+=sh*.6;x2=k2.x;if(job!=2){sh=k2.notes[m2].shhd;if(dir>0){if(m2<k2.nhd&&p2+1==k2.notes[m2+1].pit)if(k2.notes[m2+1].shhd<sh)sh=k2.notes[m2+1].shhd}else{if(m2>0&&p2==k2.notes[m2-1].pit+1)if(k2.notes[m2-1].shhd<sh)sh=k2.notes[m2-1].shhd}x2+=sh*.6}st=k1.st;switch(job){case 0:if(p!=p2&&!(p&1))p=p2;break;case 3:dir=-dir;case 1:x1=k1.x;if(x1>x2-20)x1=x2-20;p=p2;st=k2.st;break;default:if(k1!=k2){x2-=k2.wl;if(k2.type==BAR)x2+=5}else{time=k1.time+k1.dur;for(k=k1.ts_next;k;k=k.ts_next)if(k.time>time)break;x2=k?k.x:realwidth}if(x2<x1+16)x2=x1+16;break}if(x2-x1>20){x1+=3.5;x2-=3.5}else{x1+=1.5;x2-=1.5}y=3*(p-18);if(job!=1&&job!=3){if(dir>0){if(!(p&1)&&k1.dots)y=3*(p-18)+6}}h=(.04*(x2-x1)+10)*dir;slur_out(x1,staff_tb[st].y+y,x2,staff_tb[st].y+y,dir,h,k1.notes[m1].ti1&SL_DOTTED)}}function draw_ties(k1,k2,job){var k3,i,j,m1,pit,tie2,mhead1=[],mhead2=[],mhead3=[],nh1=k1.nhd,time=k1.time+k1.dur;if(job==2){for(i=0;i<=nh1;i++){if(k1.notes[i].ti1)mhead3.push(i)}draw_note_ties(k1,k2||k1,mhead3,mhead3,job);return}for(i=0;i<=nh1;i++){if(!k1.notes[i].ti1)continue;tie2=-1;pit=k1.notes[i].apit;for(m1=k2.nhd;m1>=0;m1--){switch(k2.notes[m1].apit-pit){case 1:case-1:if(k1.notes[i].acc!=k2.notes[m1].acc)tie2=m1;default:continue;case 0:tie2=m1;break}break}if(tie2>=0){mhead1.push(i);mhead2.push(tie2)}else{mhead3.push(i)}}draw_note_ties(k1,k2,mhead1,mhead2,job);if(!mhead3.length)return;k3=k1.ts_next;while(k3&&k3.time<time)k3=k3.ts_next;while(k3&&k3.time==time){if(k3.type!=NOTE||k3.st!=k1.st){k3=k3.ts_next;continue}mhead1.length=0;mhead2.length=0;for(i=mhead3.length;--i>=0;){j=mhead3[i];pit=k1.notes[j].apit;for(m1=k3.nhd;m1>=0;m1--){if(k3.notes[m1].apit==pit){mhead1.push(j);mhead2.push(m1);mhead3[i]=mhead3.pop();break}}}if(mhead1.length>0){draw_note_ties(k1,k3,mhead1,mhead2,job==1?1:0);if(mhead3.length==0)return}k3=k3.ts_next}if(mhead3.length!=0)error(1,k1,"Bad tie")}function tie_comb(s){var s1,time,st;time=s.time+s.dur;st=s.st;for(s1=s.ts_next;s1;s1=s1.ts_next){if(s1.st!=st)continue;if(s1.time==time){if(s1.type==NOTE)return s1;continue}if(s1.time>time)return s}return null}function draw_all_ties(p_voice){var s1,s2,s3,clef_chg,time,s_rtie,s_tie,x,dx;function draw_ties_g(s1,s2,job){var g;if(s1.type==GRACE){for(g=s1.extra;g;g=g.next){if(g.ti1)draw_ties(g,s2,job)}}else{draw_ties(s1,s2,job)}}for(s1=p_voice.sym;s1;s1=s1.next){switch(s1.type){case CLEF:case KEY:case METER:continue}break}s_rtie=p_voice.s_rtie;for(s2=s1;s2;s2=s2.next){if(s2.dur||s2.type==GRACE)break;if(s2.type!=BAR||!s2.text)continue;if(s2.text[0]=="1")s_rtie=p_voice.s_tie;else p_voice.s_tie=s_rtie}if(!s2)return;if(p_voice.s_tie){p_voice.s_tie.x=s1.x+s1.wr;s1=p_voice.s_tie;p_voice.s_tie=null;s1.st=s2.st;s1.ts_next=s2.ts_next;s1.time=s2.time-s1.dur;draw_ties(s1,s2,1)}while(1){for(s1=s2;s1;s1=s1.next){if(s1.ti1)break;if(!s_rtie)continue;if(s1.type!=BAR||!s1.text)continue;if(s1.text[0]=="1"){s_rtie=null;continue}if(s1.bar_type=="|")continue;for(s2=s1.next;s2;s2=s2.next)if(s2.type==NOTE)break;if(!s2){s1=null;break}s_tie=clone(s_rtie);s_tie.x=s1.x;s_tie.next=s2;s_tie.st=s2.st;s_tie.time=s2.time-s_tie.dur;draw_ties(s_tie,s2,1)}if(!s1)break;time=s1.time+s1.dur;for(s2=s1.next;s2;s2=s2.next){if(s2.dur)break;if(s2.text){if(s2.text[0]!="1")break;s_rtie=s1}}if(!s2){for(s2=s1.ts_next;s2;s2=s2.ts_next){if(s2.st!=s1.st)continue;if(s2.time<time)continue;if(s2.time>time){s2=null;break}if(s2.dur)break}if(!s2){draw_ties_g(s1,null,2);p_voice.s_tie=s1;break}}else{if(s2.type!=NOTE&&s2.type!=BAR){error(1,s1,"Bad tie");continue}if(s2.time!=time)s2=tie_comb(s1)}for(s3=s1.ts_next;s3;s3=s3.ts_next){if(s3.st!=s1.st)continue;if(s3.time>time)break;if(s3.type==CLEF){clef_chg=true;continue}}if(clef_chg||s1.st!=s2.st){clef_chg=false;dx=(s2.x-s1.x)*.4;x=s2.x;s2.x-=dx;if(s2.x>s1.x+32)s2.x=s1.x+32;draw_ties_g(s1,s2,2);s2.x=x;x=s1.x;s1.x+=dx;if(s1.x<s2.x-24)s1.x=s2.x-24;draw_ties(s1,s2,3);s1.x=x;continue}draw_ties_g(s1,s2,s2.type==NOTE?0:2)}p_voice.s_rtie=s_rtie}function draw_all_slurs(p_voice){var k,i,m2,slur_type,s=p_voice.sym;if(!s)return;var slur_st=0;slur_type=p_voice.slur_start;if(slur_type){p_voice.slur_start=0;while(slur_type!=0){slur_st<<=4;slur_st|=slur_type&15;slur_type>>=4}}draw_slurs(s,undefined);for(;s;s=s.next){if(s.type!=NOTE&&s.type!=REST&&s.type!=SPACE)continue;while(s.slur_end||s.sl2){if(s.slur_end){s.slur_end--;m2=-1}else{for(m2=0;m2<=s.nhd;m2++)if(s.notes[m2].sl2)break;s.notes[m2].sl2--;s.sl2--}slur_type=slur_st&15;k=prev_scut(s);draw_slur(k,s,-1,m2,slur_type);if(k.type!=BAR||k.bar_type[0]!=":"&&k.bar_type!="|]"&&k.bar_type!="[|"&&(!k.text||k.text[0]=="1"))slur_st>>=4}}s=p_voice.sym;while(slur_st!=0){slur_type=slur_st&15;slur_st>>=4;k=next_scut(s);draw_slur(s,k,-1,-1,slur_type);if(k.type!=BAR||k.bar_type[0]!=":"&&k.bar_type!="|]"&&k.bar_type!="[|"&&(!k.text||k.text[0]=="1")){p_voice.slur_start<<=4;p_voice.slur_start+=slur_type}}}function draw_sym_near(){var p_voice,p_st,s,v,st,y,g,w,top,bot,i,st,dx;for(v=0;v<voice_tb.length;v++){var bm={},first_note=true;p_voice=voice_tb[v];for(s=p_voice.sym;s;s=s.next){switch(s.type){case GRACE:for(g=s.extra;g;g=g.next){if(g.beam_st&&!g.beam_end)calculate_beam(bm,g)}continue;default:continue;case NOTE:break}if(s.beam_st&&!s.beam_end||first_note&&!s.beam_st){first_note=false;calculate_beam(bm,s)}}}for(st=0;st<=nstaff;st++){p_st=staff_tb[st];if(!p_st.top){top=new ArrayBuffer(YSTEP*4);bot=new ArrayBuffer(YSTEP*4);p_st.top=new Float32Array(top);p_st.bot=new Float32Array(bot)}for(i=0;i<YSTEP;i++){p_st.top[i]=0;p_st.bot[i]=24}}set_tie_room();draw_deco_near();for(s=tsfirst;s;s=s.ts_next){if(s.invis)continue;switch(s.type){case GRACE:for(g=s.extra;g;g=g.next){y_set(s.st,true,g.x-2,4,g.ymx+1);y_set(s.st,false,g.x-2,4,g.ymn-1)}continue;case MREST:y_set(s.st,true,s.x+16,32,s.ymx+2);continue;default:y_set(s.st,true,s.x-s.wl,s.wl+s.wr,s.ymx+2);y_set(s.st,false,s.x-s.wl,s.wl+s.wr,s.ymn-2);continue;case NOTE:break}if(s.stem>0){if(s.beam_st){dx=3;if(s.beam_end)w=4;else w=10}else{dx=-8;if(s.beam_end)w=11;else w=16}y_set(s.st,true,s.x+dx,w,s.ymx+2);y_set(s.st,false,s.x-s.wl,s.wl+s.wr,s.ymn-2)}else{y_set(s.st,true,s.x-s.wl,s.wl+s.wr,s.ymx+2);if(s.beam_st){dx=-6;if(s.beam_end)w=4;else w=10}else{dx=-8;if(s.beam_end)w=5;else w=16}y_set(s.st,false,s.x+dx,w,s.ymn-2)}if(s.notes[s.nhd].acc){y=s.y+8;if(s.ymx<y)s.ymx=y;y_set(s.st,true,s.x,0,y)}if(s.notes[0].acc){y=s.y;if(s.notes[0].acc==1||s.notes[0].acc==3)y-=7;else y-=5;if(s.ymn>y)s.ymn=y;y_set(s.st,false,s.x,0,y)}}if(cfmt.measurenb>=0)draw_measnb();for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];s=p_voice.sym;if(!s)continue;set_color(s.color);st=p_voice.st;set_dscale(st);for(;s;s=s.next){if(s.tp0)draw_tuplet(s,0)}draw_all_slurs(p_voice);for(s=p_voice.sym;s;s=s.next){if(s.tp0)draw_tuplet(s,0)}}for(st=0;st<=nstaff;st++){p_st=staff_tb[st];top=p_st.topbar+2;bot=p_st.botbar-2;for(i=0;i<YSTEP;i++){if(top>p_st.top[i])p_st.top[i]=top;if(bot<p_st.bot[i])p_st.bot[i]=bot}}set_color(undefined);draw_deco_note();draw_deco_staff();set_sscale(-1);set_dscale(-1);for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(p_voice.have_ly){draw_all_lyrics();break}}}function draw_vname(indent){var p_voice,n,st,v,a_p,p,y,name_type,staff_d=[];for(st=cur_sy.nstaff;st>=0;st--){if(!cur_sy.staves[st].empty)break}if(st<0)return;for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(!p_voice.sym)continue;st=cur_sy.voices[v].st;if(!cur_sy.staves[st])continue;if(cur_sy.staves[st].empty)continue;if(p_voice.new_name){name_type=2;break}if(p_voice.snm)name_type=1}if(!name_type)return;for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(!p_voice.sym)continue;st=cur_sy.voices[v].st;if(!cur_sy.staves[st])continue;if(cur_sy.staves[st].empty)continue;if(p_voice.new_name)delete p_voice.new_name;p=name_type==2?p_voice.nm:p_voice.snm;if(!p)continue;if(cur_sy.staves[st].flags&CLOSE_BRACE2){while(!(cur_sy.staves[st].flags&OPEN_BRACE2))st--}else if(cur_sy.staves[st].flags&CLOSE_BRACE){while(!(cur_sy.staves[st].flags&OPEN_BRACE))st--}if(!staff_d[st])staff_d[st]=p;else staff_d[st]+="\\n"+p}if(staff_d.length==0)return;set_font("voice");
  indent=-indent*.5;for(st=0;st<staff_d.length;st++){if(!staff_d[st])continue;a_p=staff_d[st].split("\\n");y=staff_tb[st].y+staff_tb[st].topbar*.5*staff_tb[st].staffscale+9*(a_p.length-1)-gene.curfont.size*.3;n=st;if(cur_sy.staves[st].flags&OPEN_BRACE2){while(!(cur_sy.staves[n].flags&CLOSE_BRACE2))n++}else if(cur_sy.staves[st].flags&OPEN_BRACE){while(!(cur_sy.staves[n].flags&CLOSE_BRACE))n++}if(n!=st)y-=(staff_tb[st].y-staff_tb[n].y)*.5;for(n=0;n<a_p.length;n++){p=a_p[n];xy_str(indent,y,p,"c");y-=18}}}function set_staff(){var s,sy,i,st,prev_staff,v,y,staffsep,dy,maxsep,mbot,val,p_voice,p_staff,empty=[];for(st=0;st<=nstaff;st++){staff_tb[st].empty=false;empty[st]=true}sy=cur_sy;for(st=0;st<sy.staves.length;st++){if(!sy.staves[st].empty)empty[st]=false}for(s=tsfirst;s;s=s.ts_next){if(s.type!=STAVES)continue;sy=sy.next;for(st=0;st<sy.staves.length;st++){if(!sy.staves[st].empty)empty[st]=false}}for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(p_voice.scale!=1)p_voice.scale_str='transform="scale('+p_voice.scale.toFixed(2)+')"'}for(st=0;st<=nstaff;st++){if(!empty[st])break;staff_tb[st].empty=true}y=0;if(st>nstaff){st--;p_staff=staff_tb[st]}else{p_staff=staff_tb[st];for(i=0;i<YSTEP;i++){val=p_staff.top[i];if(y<val)y=val}}y+=draw_partempo(st,y);if(empty[st])return y;y*=p_staff.staffscale;staffsep=cfmt.staffsep*.5+p_staff.topbar*p_staff.staffscale;if(y<staffsep)y=staffsep;if(y<p_staff.ann_top)y=p_staff.ann_top;p_staff.y=-y;prev_staff=st;var sy_staff_prev=sy.staves[prev_staff];for(st++;st<=nstaff;st++){p_staff=staff_tb[st];if(empty[st]){p_staff.empty=true;continue}if(sy_staff_prev.sep)staffsep=sy_staff_prev.sep;else staffsep=cfmt.sysstaffsep;if(sy_staff_prev.maxsep)maxsep=sy_staff_prev.maxsep;else maxsep=cfmt.maxsysstaffsep;dy=0;if(p_staff.staffscale==staff_tb[prev_staff].staffscale){for(i=0;i<YSTEP;i++){val=p_staff.top[i]-staff_tb[prev_staff].bot[i];if(dy<val)dy=val}dy*=p_staff.staffscale}else{for(i=0;i<YSTEP;i++){val=p_staff.top[i]*p_staff.staffscale-staff_tb[prev_staff].bot[i]*staff_tb[prev_staff].staffscale;if(dy<val)dy=val}}staffsep+=p_staff.topbar*p_staff.staffscale;if(dy<staffsep)dy=staffsep;maxsep+=p_staff.topbar*p_staff.staffscale;if(dy>maxsep)dy=maxsep;y+=dy;p_staff.y=-y;prev_staff=st;sy_staff_prev=sy.staves[prev_staff]}mbot=0;for(i=0;i<YSTEP;i++){val=staff_tb[prev_staff].bot[i];if(mbot>val)mbot=val}if(mbot>p_staff.ann_bot)mbot=p_staff.ann_bot;mbot*=staff_tb[prev_staff].staffscale;for(st=0;st<=nstaff;st++){p_staff=staff_tb[st];dy=p_staff.y;if(p_staff.staffscale!=1){p_staff.scale_str='transform="translate(0,'+(posy-dy).toFixed(2)+") "+"scale("+p_staff.staffscale.toFixed(2)+')"'}}if(mbot==0){for(st=nstaff;st>=0;st--){if(!empty[st])break}if(st<0)return y}dy=-mbot;staffsep=cfmt.staffsep*.5;if(dy<staffsep)dy=staffsep;maxsep=cfmt.maxstaffsep*.5;if(dy>maxsep)dy=maxsep;y+=dy;if(y>cfmt.maxstaffsep)y=cfmt.maxstaffsep;return y}function draw_systems(indent){var next_sy,s,s2,st,x,x2,res,xstaff=[],bar_bot=[],bar_height=[];function bar_set(){var st,staffscale,top,bot,dy=0;for(st=0;st<=cur_sy.nstaff;st++){if(cur_sy.staves[st].empty){bar_bot[st]=bar_height[st]=0;continue}staffscale=staff_tb[st].staffscale;top=staff_tb[st].topbar*staffscale;bot=staff_tb[st].botbar*staffscale;if(dy==0)dy=staff_tb[st].y+top;bar_height[st]=dy-staff_tb[st].y-bot;bar_bot[st]=staff_tb[st].y+bot;if(cur_sy.staves[st].flags&STOP_BAR)dy=0;else dy=bar_bot[st]}}function draw_staff(st,x1,x2){var w,i,dx,y=staff_tb[st].y,stafflines=staff_tb[st].stafflines,l=stafflines.length;for(i=0;i<l;i++){if(stafflines[i]!="."){set_sscale(st);w=(x2-x1)/stv_g.scale;xypath(x1,y);output.push("h"+w.toFixed(2));y=0;for(i++;i<l;i++){y-=6;if(stafflines[i]!="."){output.push("m-"+w.toFixed(2)+" "+y);output.push("h"+w.toFixed(2));y=0}}output.push('"/>\n');break}y+=6}}draw_vname(indent);for(st=0;st<=nstaff;st++)xstaff[st]=!cur_sy.staves[st]||cur_sy.staves[st].empty?-1:0;bar_set();draw_lstaff(0);for(s=tsfirst;s;s=s.ts_next){if(s.type==STAVES){next_sy=cur_sy.next;for(st=0;st<=nstaff;st++){x=xstaff[st];if(x<0){if(next_sy.staves[st]&&!next_sy.staves[st].empty){if(s.next&&s.next.type==BAR)xstaff[st]=s.x;else xstaff[st]=s.x-s.wl-2}continue}if(s.ts_prev.type==BAR)x2=s.ts_prev.x;else x2=s.x-s.wl-2;if(!next_sy.staves[st]||next_sy.staves[st].empty)xstaff[st]=-1;else if(next_sy.staves[st].stafflines==cur_sy.staves[st].stafflines)continue;else xstaff[st]=x2;draw_staff(st,x,x2)}cur_sy=next_sy;bar_set();continue}st=s.st;switch(s.type){case BAR:if(s.second||cur_sy.staves[st].empty)break;if(s.invis)break;anno_start(s);draw_bar(s,bar_bot[st],bar_height[st]);anno_stop(s);break;case STBRK:if(cur_sy.voices[s.v].range==0){if(s.xmx>14){var nv=0;for(var i=0;i<voice_tb.length;i++){if(cur_sy.voices[i].range>0)nv++}for(s2=s.ts_next;s2;s2=s2.ts_next){if(s2.type!=STBRK)break;nv--}if(nv==0)draw_lstaff(s.x)}}s2=s.prev;if(!s2)break;x2=s2.x;if(s2.type!=BAR)x2+=s2.wr;st=s.st;x=xstaff[st];if(x>=0){if(x>=x2)continue;draw_staff(st,x,x2)}xstaff[st]=s.x;break;default:break}}for(st=0;st<=nstaff;st++){if((x=xstaff[st])<0)continue;draw_staff(st,x,realwidth)}}function draw_symbols(p_voice){var bm={},s,g,x,y,st,first_note;first_note=true;for(s=p_voice.sym;s;s=s.next){if(s.invis&&s.type!=NOTE&&s.type!=REST&&s.type!=GRACE)continue;x=s.x;set_color(s.color);switch(s.type){case NOTE:set_scale(s);if(s.beam_st&&!s.beam_end||first_note&&!s.beam_st){first_note=false;if(calculate_beam(bm,s))draw_beams(bm)}anno_start(s);draw_note(s,!bm.s2);anno_stop(s);if(s==bm.s2)bm.s2=null;break;case REST:set_scale(s);anno_start(s);draw_rest(s);anno_stop(s);break;case BAR:break;case CLEF:st=s.st;if(s.time>staff_tb[st].clef.time)staff_tb[st].clef=s;if(s.second)break;if(s.invis||staff_tb[st].empty)break;set_color(undefined);set_sscale(st);anno_start(s);y=staff_tb[st].y;if(s.clef_name)xygl(x,y+s.y,s.clef_name);else if(!s.clef_small)xygl(x,y+s.y,s.clef_type+"clef");else xygl(x,y+s.y,"s"+s.clef_type+"clef");if(s.clef_octave){if(s.clef_octave>0){y+=s.ymx-10;if(s.clef_small)y-=1}else{y+=s.ymn+2;if(s.clef_small)y+=1}xygl(x-2,y,"oct")}anno_stop(s);break;case METER:p_voice.meter=s;if(s.second||staff_tb[s.st].empty)break;if(cfmt.alignbars&&s.st!=0)break;set_color(undefined);set_sscale(s.st);anno_start(s);draw_meter(x,s);anno_stop(s);break;case KEY:p_voice.key=s;if(s.second||staff_tb[s.st].empty)break;set_color(undefined);set_sscale(s.st);anno_start(s);draw_keysig(p_voice,x,s);anno_stop(s);break;case MREST:set_scale(s);x+=32;anno_start(s);xygl(x,staff_tb[s.st].y+12,"mrest");out_XYAB('<text style="font:bold 15px serif"\n\tx ="X" y="Y" text-anchor="middle">A</text>\n',x,staff_tb[s.st].y+28,s.nmes);anno_stop(s);break;case GRACE:set_scale(s);draw_gracenotes(s);break;case SPACE:case STBRK:break;case CUSTOS:set_scale(s);draw_note(s,0);break;case BLOCK:case PART:case REMARK:case STAVES:case TEMPO:break;default:error(2,s,"draw_symbols - Cannot draw symbol "+s.type);break}}set_scale(p_voice.sym);draw_all_ties(p_voice);set_color(undefined)}function draw_all_sym(){var p_voice,v,n=voice_tb.length;for(v=0;v<n;v++){p_voice=voice_tb[v];if(p_voice.sym&&p_voice.sym.x!=undefined)draw_symbols(p_voice)}draw_all_deco();set_sscale(-1)}function set_tie_dir(sym){var s,i,ntie,dir,sec,pit,ti;for(s=sym;s;s=s.next){if(!s.ti1)continue;if(s.multi!=0){dir=s.multi>0?SL_ABOVE:SL_BELOW;for(i=0;i<=s.nhd;i++){ti=s.notes[i].ti1;if(!((ti&7)==SL_AUTO))continue;s.notes[i].ti1=ti&SL_DOTTED|dir}continue}sec=ntie=0;pit=128;for(i=0;i<=s.nhd;i++){if(s.notes[i].ti1){ntie++;if(pit<128&&s.notes[i].pit<=pit+1)sec++;pit=s.notes[i].pit}}if(ntie<=1){dir=s.stem<0?SL_ABOVE:SL_BELOW;for(i=0;i<=s.nhd;i++){ti=s.notes[i].ti1;if(ti){if((ti&7)==SL_AUTO)s.notes[i].ti1=ti&SL_DOTTED|dir;break}}continue}if(sec==0){if(ntie&1){ntie=(ntie-1)/2;dir=SL_BELOW;for(i=0;i<=s.nhd;i++){ti=s.notes[i].ti1;if(ti==0)continue;if(ntie==0){if(s.notes[i].pit>=22)dir=SL_ABOVE}if((ti&7)==SL_AUTO)s.notes[i].ti1=ti&SL_DOTTED|dir;if(ntie--==0)dir=SL_ABOVE}continue}ntie/=2;dir=SL_BELOW;for(i=0;i<=s.nhd;i++){ti=s.notes[i].ti1;if(ti==0)continue;if((ti&7)==SL_AUTO)s.notes[i].ti1=ti&SL_DOTTED|dir;if(--ntie==0)dir=SL_ABOVE}continue}pit=128;for(i=0;i<=s.nhd;i++){if(s.notes[i].ti1){if(pit<128&&s.notes[i].pit<=pit+1){ntie=i;break}pit=s.notes[i].pit}}dir=SL_BELOW;for(i=0;i<=s.nhd;i++){ti=s.notes[i].ti1;if(ti==0)continue;if(ntie==i)dir=SL_ABOVE;if((ti&7)==SL_AUTO)s.notes[i].ti1=ti&SL_DOTTED|dir}}}function set_tie_room(){var p_voice,s,s2,v,dx,y,dy;for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];s=p_voice.sym;if(!s)continue;s=s.next;if(!s)continue;set_tie_dir(s);for(;s;s=s.next){if(!s.ti1)continue;if(s.notes[0].pit<20&&(s.notes[0].ti1&7)==SL_BELOW);else if(s.notes[s.nhd].pit>24&&(s.notes[s.nhd].ti1&7)==SL_ABOVE);else continue;s2=s.next;while(s2&&s2.type!=NOTE)s2=s2.next;if(s2){if(s2.st!=s.st)continue;dx=s2.x-s.x-10}else{dx=realwidth-s.x-10}if(dx<100)dy=9;else if(dx<300)dy=12;else dy=16;if(s.notes[s.nhd].pit>24){y=3*(s.notes[s.nhd].pit-18)+dy;if(s.ymx<y)s.ymx=y;if(s2&&s2.ymx<y)s2.ymx=y;y_set(s.st,true,s.x+5,dx,y)}if(s.notes[0].pit<20){y=3*(s.notes[0].pit-18)-dy;if(s.ymn>y)s.ymn=y;if(s2&&s2.ymn>y)s2.ymn=y;y_set(s.st,false,s.x+5,dx,y)}}}}var musicfont='url("data:application/font-woff;base64,d09GRk9UVE8AABjkAAoAAAAAH8QAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABDRkYgAAADPAAAFPgAABncbyRwGEZGVE0AABg0AAAAHAAAABx5N58rT1MvMgAAAVQAAABLAAAAYFjAWjRjbWFwAAACfAAAALEAAAH6kDnc6GhlYWQAAAD0AAAANQAAADYHrbQmaGhlYQAAASwAAAAgAAAAJAihAQhobXR4AAAYUAAAAJMAAAC+V4YAJG1heHAAAAFMAAAABgAAAAYAMFAAbmFtZQAAAaAAAADbAAABfbMpIV9wb3N0AAADMAAAAAwAAAAgAAMAAHicY2BkYGAA4uOtQbLx/DZfGbhZGEDg4nQlcRB92dO/6P+uvzzMX1imALkcDEwgUQAqQwtFAAAAeJxjYGRgYJnyl4chhoX3/67/+5m/MABFUIA+AJ3hBq4AAFAAADAAAHicY2BmvMs4gYGVgYNpJtMZBgaGfgjN+JrBmJGTgYGJgZWZAQ4EEEyGgDTXFAYHBoaXMczG/40ZYlimMH0BCjPCFSgAISMAm6gMnAB4nHWOMW7CQBBFn8EQRUEoVZRyyzRG3kVKQUXFAVLQG7SykJAtrYGLIHGBlDkGB8gRcpV8nGlSsNLsvv3682eACRcybifjgWfjgfjNeIgjGefSP41HPHE1Hkv/kTPLH6VM+64bD8SvxkOWlMa59LPxiBe+jMfSv6nYsCXQcaKGarMN3UnwQdT/yF6OpG+sj/tKsKKl4dC/SY6ojReq/zl/2hxPoQq6g/bxvCugbQ6rNtXRLZxNE8194UMRSi/HvZXWGpak7voVnOJmCmUdU7drG+dn5d3eX0S5NjAAeJxjYGBgZoBgGQZGBhD4AuQxgvksDDeAtBGDApAlxMDwgOGBx4OABzEPkh5kPqh50P1gyUPGh88fpTxZ8GTNkwNPXj1595TxafrT3GcBz0Jfxvz/DzIMqMcdrqfqQdeDBWA9CVA9D4F6GIB6cuB6GBWY5PfIb5VfJb9Ufr58v3yTfJ68kJyu7FbpKul86RhpG2lLaQ2pw1L7Je9K3hS7DHUziYCRjYE8jcMIAADX1VJUAAAAeJxjYGbACwAAfQAEeJydWAlYU1e+v9cQcos2CvHSjQb39uOrrbu2Y1XqUtcqagtUBQSRJUAgEAKEfQvkH8KWsASSEEgIEAg7WigolYrVLmOpto5On9OOXWbacbTtnNiT9nsHor55782bme99N9/3O/fknN/5n/85/+3SlJsbRdM0czw8YlVyahRFz6Joaotj9yzHHo7jcTeYw4E5bkIP6pHUN14AeNiYw9v+S8nPJT5uGq7PLHauD0XN85mV4ulDLfV5wsuLWjnNwqP4FEv5UIspP2oltYHaTO2g9lGHqSNUOBVDJVIyKpsqolRUNaWjTFQb1U0NUaPUBHWJ+pi6Rt2kvqb+Qv1EOWkO7UF70o/TvvQyejm9hv4N/Qq9iz5ABz6fID4ReVKaELN9xYoVM7BmvQs2zMBaV+farTOwbpULXpyB9f4ueMUFriEbXJ0bXJ3+run+K13gmu6/2gVrpmHlipn/Vm6fWXbVuhUuWOmCVS5Y7YKZCWtcnGv8Z9Zbs9X1tn2lC1a5YLULXBO2r3XBOhesd8EGF7zoAn8XbJ+GtS5NrHVJtnbdehdsdcG2aVjn0su6tTPUL67dev/sH1wBiqIVdAldSitpoFV0Ga2my+kKupKuoqtpDa2la+hauo6up3V0A91I62kDbaSbaBPdTLfQZtpCt9JWuo1upztoG91Jd9F2upvuoXvpPrqfHqAHqRXTt2MJtZ5qo9WzFnBC3cq427nR7md53zK6R5Y/csvjSY8dHuEeEx4/zXafvWh24mzTbMecVx6lHw16NPnRdx/97NHv+TSf5R/hN/DvzC2f96rns557PeWebV4HvLK8Ls7P8eZ6v4R8YcxxYIweG0MXxjhj3o7nHUPO593HnAWs4wC64DzA47eCYwkNjqUccHMsdSxxLnXn476mVBpa2to4jlo3u1wfBaEQkxucvCs7vDgaYmFHW+C7J0LtaQMwApbqzjo703pZew7egouS4cCGsQbLELwHp/LOJ36cbS9oBwuMGAZP2ZmAEwmBsANiK6Lrwlt21QRDDISli6LlDP9vZUakuS67jiqMXorrb1gR8+fVf/7iepBV0JPrWIS+Yhd4CD6j8K/7pxtVFHoMH2YFlVsiukM/ElrBWm5raDHabDDCCGooeCfqKmbQLAaJ0FhYB47Gw9zojvCuI90im9wGLXDmnG2EQEtBg5wRVFKivJwMkDPS5lSL1dDcLCQU/VGnXhWKQawQyZNlIhEcIeO2wOHuLYjBNIOj8PCQCEWgIW533EDMSJRN1CCCZAg8JDpCILla3sAQIW3a2kZoYEwpTUlCcVpKii/fkbVzCB3tR8cGaVR3G7n1I7fbnPk+jjjHNyx+moe5uSufycePM1gYgYTuGjz/1mLEzWXw0/3oaR7i1tz6QYPmM0jYj4Xu+Wj+yh8wt4Yhf0U4N7D4KG9/9ZExBVrIoOe8kZu7Ai88ErS/gEGFbugob7JgJLgcL2Lwc97Yzb0cLR0Zmaxm+A6/XwR96Ps+9DPy8oKvh1Eq9h9/f+SDvi8Gmj4Q/HDWsRgZ2DQUwUWrkefZW4D2AXpccXXd18+Ln30B8FHYrX2xenFQ5NHQZFlYzEu/SQpiRAclq2A1YDnywfNQJtRAI9SomEZ8kKvKg3zyLAtODIIkSCmX1TCCe2ffU410wXnmfPD4vh0hQQfXv78DuSGqf/yuEHRKnbKBkaP93MHydl2P4YvBnk/gG/g45/2gd0LPbLHuACYP8pT5SqYWz2Pr3RF1ffWLvrA5ZhN+Oo6J9C6FUoUQZ8M25F2CNqrQko+vo51oCREmgCv46ewKyct74FnY2rHtckaSLbUPbPDhxOBv65j8e1vYmi8HfvyDtldauxOOwaLFi186zBCFlOgUb8NZsKisajPDR3ecdd0OH/tdu+PJHhrQ3IvWT9FK9BtEcRwSZworBpEmytBVYzRqmqG6rBKqmYFwW5iw2P1YhlRCDOnFbw59BxfhfOvE2d5u43DFGUbPg+Gs/qSe6LFX33mOGEhEQXgmE3Ol6yp3ILO/oAus0KJu0d6yT30CiCaji8/IhiO7D549YNlL7l5ScXJBQEZUXEZIcS4Q1UCsSfJWBlPhPjRgG/CF6tJKqMy3yhpjamyNLZp+OAe9ImKJsVXREA2bTmC3bcDEQ3yDvCasNXco+ZR0T1pImjhNLJGIUhlHgfhXjdVRwbOlmiUGMXlCTHtqrJWNFTXN1pHT54kz6DrZuE0TVx4PCcyqo4tWCZ2SnyXsrxo092cNj48+x5v+GoCyo+/0DnkZkAKtRx4fficIMQywL42suSZsBaumuanVVGslB3Hx0PnX4CTkFsZH6r+9ce5TOANdOQMhTE+YYQ+sh1hFZOEJ6cKQZ/bvW70cCw7iRZHogBb5f/3XP96+NYZYQOsZuJD1cfypoCv+dswFBj+GOS/ix30FhnveOIq1GE1tbclNiQkZqQm+cSZZm1CwqQgVs61GU6cltSkxPi09wTfWKG0X8r8D5HcZ+XFQIlKzzhLkxyt1lHCdbt4P28SOnMp21NSB+v608i9e8Kkd7cLCoTO9Y7Ypm3VA8NlN9Hv0CFulqoIq+Bomt8Kr8FrkPjxLwhR9lYaCAJgi3M+WgrJEiPdD0I9F6EkV4o1fQksQjxH86WZFYPnxioBXZPuCYCMEdIX2S1Mtea3EsY6/ZX+nRiVvwUbC0ZXAdqmba9t1l7vazsMVmMgfCz/FCK7djBg+bHydWF4BMZXCi6VvFY8jwe2hKUDPwk+KyVev+Es3+gNew0Bg9a7KDeHxJ6PS5DGJu/ekhicdk22D7YD3IepZ9BpooBY0KqYOL+SqCB0Ugn90fCikQrparmEEN26OqnpbYZgZjjgVfDgq/NjOD1+/fGOg/wthFY/Mq1ZqlTVF54ummJ33vmc1l22XLltt8Zpwcs6vrF++O5QRvHeztFbRB4PQpGouMxL/tNy5sg19aP29Fb3fTsP3Y6YLyAs98QXH4eWcz0pBUpWga9U0NFbpobKsAiqZzhhLjFDhfjIrQ0ZczK4rIddgFIZNbw22tzX2lA8yOh705naktosHDp/aDIkgKozNYRLPt05yO3NsRa1gAmOZoXrK+u55+IKp5ykGM3ri244NHjUFQxqkFqcVHctKSM6KUkxvvgCSGmU9WUy5e1eXudMXKksroKLQJK9P1Fjq9VU2OA0dEsMbmqSKBBDDvrhVB4Eh4aEuWxNjyu9Ks6cHZZ7MlGZKZbKkDMaxQPrLpMnhx7NkGGU6KXlO6oM0por6co3e1NczDL3QGl9/sEqiToYUZlvk+m1Cp9e9TvaXye/vTfL4jh3Y58ZRdEB8o6PLS4cCkfe349cEIbpOdnff9veEJjBV6RtNeq2J3JnRkJE3IR4KipLj66++f/oCUbclrzOKaY/RBcJOSCqJK4pLXxe98Ujw9i1Lj2H3eORXjXw/ufHx9an+HwB5M/B27kSyPfz8AetKYBat2r2QGJWjBr/MNjXom5vTGqUpWRkpvhK9nATTTcXoDdbUoLc0ZTRIkzOzUnyTGtJbhPxurGxDgJ681ERr0CwUPl7XyEHn0AZW6X4SUmNKk5ho64nTfebhYSFYsiyyZllzSGsg4MeUq/cC3ggK1QlQqF9teqMvsvfkqGwUGMQvG7rQeVelTleDOuVIWoQ4WZyckJcEIkiqS2kTDRZehU8ZQM+Nn7tQ1gdnksihKJTFJYoilUJVoGL49v8m0gU7mvVvClUESmURMHeiF/EWZ+7cBfiZvxevPaE3ra+IWWD/QcUjMvZdst3+3zLmQbEqX804dvGqy9Rl1fAP5MNz8S9sQWVhVaHGtRf4wTzZ3Uqi0ArM2tHhDhr6VD+hTDRL1c9xZKFNLL7bj+7ynBZvfLePNJDbAnahB7q1l13kwcebbqMFt2kwO7LNHPNl1pGNFziz3SVoiHXuNY86soPNjr2SYGf2KI9/9QH/DDsHcS7+F08ofnYC7Z6goQMZOjjo9EesuqisiDiGA/iJl14GKaRVFTQoKkngqYLz6Ikb14iJGQo1cqapjpUMhxmPwvOw6GV4AdIqZFqZ+eiQbJhE+B+vETdpKDbmER8QhhcgGwmun3Ni3bafOfmekGRVZfrKW5aro/BHuPx69x7IhMySDMXqlE2vw2qij/1Y7YZmg2OfowTPAS7/b8Fko7c5o8Gs1WSyWqUmsVgqFYtNUqsvfzHi99KjduTxAQc9i/hsR0t8tC9Ey8PjwxPCJMnppQwK5Zl7mi3C9l42AuLTU2IY9Fm08yavC85Yx8fPjltGYBjePtodCgwf+4MVLTFzrMtIBPJDS04D9uPiJY4dbKJSqQTnp2jLGOBUM4DjE/xyIJe/eMJ8p4Xz+8/YO22Ady0Vc7HBDdDu6bdNLQBo+6QOPt+fCWjnpN7V2JoMgPdwlyYB2iQhzR0Hs2DNRAPpI411E42At3L5C/G8K7enaOPU3SkOCiS50dTUQffAM1xDY2cXCRQTH28lr0Hc9MbYmdetWyd4Y8FBwQcB/KcAzo+NcWuz+yPJX/xC5NlLDver8br2r89yriFPVuVeA7UF2uyqLHU6pEMOZCuyC6Kz4lITUhNyM3JgWmFgamlpVjGWXjYKUtPlIgZNxTl/x+uE3rY2u7XLZG/sr9CXN0ADDImGI8iUtZFsOeHVzvCWz/CW3udNJ7xZGbn3eZvv80ZDqkweT3jjCW87DLRZ7W1dRntDf5WL9624U8eJ+G8AKqFL0FWOXsWi/u+4GnVFNbmJ1cXV+eUM7v8bVwlKlZKYX0EmZEORuqiCHPjxjVytAhR5xQyOeIWrKC0tJiaeV5mlhTJQQRlxFc8Deo9cyU5SM/XALfTml/jN94Hc0/e4fCQDNAfN+wMNWpROWnItGaR115HrwH6LlsH0j12uw36M1h3FYD/0CPabeRgcK0RnvLssrZ22RGtsXIJYJGoTd/jysYI4XQ5Su5F8w9uVb0x3qen73ViNvJGax38FeyJP0nmWLEiaZ5En6YP7XcjzYR9+CscR6S9xBhxPsTfwm8/gnegpfICbUi+tSoEjEE4KHjhuO/zl4fDezB5ohz6rZYBAd3Z3LIMpFHEXxRMRPEY6CfNTHLR5ksVc3D8iR36OpVxFmQxKS0MDxGnHQN9UYSAlkD3GGB0bmx5y6LTkrHAQ6qr15QxxHCdsKKXTi1DsRU8Jfos24yB2HT59OgcF31t2nyY++Hji/0Wjn6bZj0O4RenJ8mPACG4u9uBbZlgfEAegp3YQ7h+RcpINeDjyIV0ToUsjdMnTdDWVxkrGtX7Tz//O+kYNWT8AhxJW6cz639wfJ/jjNLVQ8M0Ddt9B0Eyz8//ivC/dAwH3P5TxDtrsfLD/P/u4Vf1/BLj7QIA7LgHu/k8BAv5OWz/+EzX8/bh7RKszcktsKGHml/hA9H0zvy1E+p8pNHWGPYSD/rWKA/DRh9zOLf/qLBx+Ptzcf1cXgfjIQ104cv8x9YweXn8oKNkfRTbokCxCJ0kwvDdg5qAMN2eYGXneG8CeZkeYBHv+MjBtNuggDiT27Ifm0MUEJ9EczjtX2SUejs24mYCz+DCLPF1Bx4sEHdL9YLgMzXEMkeH908PvPT8z/BfpYXapBw52lhDgXyq59FoPcrt4wu4F9Y7l9YKjDo8eloQ1bVodgzcib66sPkUtU6WBrCgzWypLF8MJiNUm66WMoLEhw5BfT2KiphaawJCnTYdcyC7JzM+sKaqHNuhr6mgDRgd1+YZsBm3E3lxBnDHLUmgEA7Gh+rpmo95K8o0R0bmQ6YlypQwyNHkGYIibV+s0utyqLIhnTqaKxEI+igCUQne9xV699iOp7hk0bxqWobXS5k3rF6J5eA6et4zAIryceMKIN9DvcEWqF+poFTShjsssnttKKiZzK1cQdn+erGXjOjJvuz9X0KREcxNJQPOTptynaJJcveYayuXfe650MmAQlXT2fxI6hjbavGAC+Y3Xvvvtuareyb7RCfTC+cpJgfXXn2NLWX1tfUNDVr08MzNL7ivXZemETsOf2PAoOSkKxCC5EmlhetF6rqocpp+x7XpSI5SrKlXlpHsNFy0qPL0NohnBC7+SOih8+jtQlbSXyc7htgQMJXUDM/LO26Mf7foPvAeH+uOlm30Fsl9jGiStwg+gxXz2S0ZezU2JOFmQCjLIaYAWOD11HT2uJZdTxFUWKQtJKDn4QRYcgxJliVIRjx8z9C7ZGy4JPk6yGIW6pAoYPQxop0aMNQazCeqhNksfZw+vzyYpPJj07442JQ1HkmyXbJU4Zl3rUEdQT5ENbsHEFfgKkBBz0Fxc/Tmj5EE62rXtylL8eLR4FyxjBOJfo0Iik4SRkGxKbjMbR+wtw1oDuQP1MJjUHgXhEFaQFC1NKEqFCAZidIlGEdPXymaZFfXQCpP9/XaoA01pubyssKyAbCJEmrL7AIPGfsOGalOb4RTctEx+2d8ibjwEobA1JWCnguF/9E1TY+G3h84d0lv0zd95oVeb877fr29vFEwuQB0on5WkZ6WmpTc0NzfWGYUGY3aKr+DSFvx0AWuuy830JRVKXmGhXJ5ZKiWxOU+Zm5NLkgLSFDdlmUELWrW2nOkyx7XbuzWaqooaIp82HxIZwcVcCI0sgEhG0pLWbGrTkdr+UhgMHlfCOTBVa7SkjNUUagrsJxIkFkl9Xp2iFmzQYIIWBupKdLnaBHNK0snOPI1SQ5RTry3vIOpuCesDdaQNGLuhydycZQn3FZSHxYLyRDDJcXO1pC6urqzVahp1mjI7MBZDR5M12SDxzQNZcUG+WNQmFUXl5eQqCiADio1gUg3bi6Cb6UjRSyWSDJEQRD3K8lDVdFFZDBn5hdNfb3IqCqtP9lvbJJbc6pxykgCJ5JLkFJ1k0LcOdIqGzOrC8ukyNDsnT1JKBNwSoi7uTgAmWi5NEhvSO307QNkzTJKVf5nq81ROdzMo76lIPkkyfoBR4r565jseYevneNg8bNXjlcaKm3PmmGebK8sr3q2tq9XNedSHWiqg3Gl6/t7w7ApSWupNpnS9VJqeLvWV6tNNwtl1bdZK4njq3D8ovrS3JrysUJU/8wmiQFEoT0gqEgOT7b6rYu/FnIHSKiVJyUBbplFXMbOvr34b+2Cf1W+u2/Llm8gH+Xz59nXh7H8SKGZ3trZ2dia2xsYmJsXEtiZ2+s5GfJcjngvc2f8JRfSJYQAAAAEAAAAA1FctoQAAAADRlyIXAAAAANNJT3J4nGNiYGBgnAAkwoH0g/93mOMYqpi5GRiYLgHx/v+7GK8xMID4zHxArP7/BONMBk7GGQycTIJAeRmgngNA7AnEXkC+AcgMIOZlMGI4AaYZGLkZRBjzgLTD/7+MEkA1EUC7miCYUQeCGd4B8X+oOf1AfAOCmYOB6o3/f2BW+P+QcSuQ7cLAC8IsvGC7eRgYAAtYHuQA")';var defined_font={},font_tb={},fid=1,font_scale_tb={serif:1.05,serifBold:1.05,"sans-serif":1.1,"sans-serifBold":1.15,Palatino:1.1,Mono:1.35},fmt_lock={};var cfmt={aligncomposer:1,breaklimit:.7,breakoneoln:true,cancelkey:true,composerspace:6,dblrepbar:":][:",decoerr:true,dynalign:true,fullsvg:"",gracespace:[6.5,8,12],graceslurs:true,hyphencont:true,indent:0,infoname:'R "Rhythm: "\nB "Book: "\nS "Source: "\nD "Discography: "\nN "Notes: "\nZ "Transcription: "\nH "History: "',infospace:0,keywarn:true,leftmargin:.7*IN,lineskipfac:1.1,linewarn:true,maxshrink:.65,maxstaffsep:2e3,maxsysstaffsep:2e3,measurefirst:1,measurenb:-1,musicspace:6,parskipfac:.4,partsspace:8,pagewidth:21*CM,rightmargin:.7*IN,rbdbstop:true,rbmax:4,rbmin:2,scale:1,slurheight:1,staffnonote:1,staffsep:46,stemheight:21,stretchlast:.25,stretchstaff:true,subtitlespace:3,sysstaffsep:34,textspace:14,titlespace:6,titletrim:true,topspace:22,tuplets:[0,0,0,0],vocalspace:10,writefields:"CMOPQTWw",wordsspace:5};function get_bool(param){return!param||!param.match(/^(0|n|f)/i)}function get_int(param){var v=parseInt(param);if(isNaN(v)){parse.line.error("Bad integer value");v=1}return v}function get_font_scale(param){var a=param.split(/\s+/);if(a.length<=1)return;var scale=parseFloat(a[a.length-1]);if(isNaN(scale)||a<=0){parse.line.error("Bad scale value in %%font");return}font_scale_tb[a[0]]=scale;for(var fn in font_tb){var font=font_tb[fn];if(font.name==a[0])font.swfac=font.size*scale}}function param_set_font(xxxfont,param){var font,fn,old_fn,n,a,new_name,new_fn,new_size,scale;if(xxxfont[xxxfont.length-2]=="-"){n=xxxfont[xxxfont.length-1];if(n<"1"||n>"9")return;xxxfont="u"+n+"font"}fn=cfmt[xxxfont];if(fn){font=font_tb[fn];if(font)old_fn=font.name+"."+font.size}a=param.split(/\s+/);new_name=a[0];if(new_name=="*"&&font){new_name=font.name}else{new_name=new_name.replace("Times-Roman","serif");new_name=new_name.replace("Times","serif");new_name=new_name.replace("Helvetica","sans-serif");new_name=new_name.replace("Courier","monospace")}if(a.length>1){new_size=a[a.length-1];if(new_size=="*"&&font)new_size=font.size}else if(font){new_size=font.size}if(!new_size){return}new_fn=new_name+"."+new_size;if(new_fn==old_fn)return;font=font_tb[new_fn];if(!font){scale=font_scale_tb[new_name];if(!scale)scale=1.1;font={name:new_name,size:Number(new_size),swfac:new_size*scale};font_tb[new_fn]=font}cfmt[xxxfont]=new_fn}function get_unit(param){var v=parseFloat(param);switch(param.slice(-2)){case"CM":case"cm":v*=28.35;break;case"IN":case"in":v*=72;break}return v}function get_unitp(param){var v=parseFloat(param);switch(param.slice(-2)){case"CM":case"cm":v*=CM;break;case"IN":case"in":v*=IN;break}return v}function set_infoname(param){var tmp=cfmt.infoname.split("\n"),letter=param[0];for(var i=0;i<tmp.length;i++){var infoname=tmp[i];if(infoname[0]!=letter)continue;if(param.length==1)tmp.splice(i,1);else tmp[i]=param;cfmt.infoname=tmp.join("\n");return}cfmt.infoname+="\n"+param}const textopt={align:"j",center:"c",fill:"f",justify:"j",ragged:"f",right:"r",skip:"s"};function get_textopt(param){return textopt[param]}const posval={above:SL_ABOVE,auto:0,below:SL_BELOW,down:SL_BELOW,hidden:SL_HIDDEN,opposite:SL_HIDDEN,up:SL_ABOVE};function set_pos(k,a){var v,val,pos;val=posval[a];if(val==undefined){parse.line.error("Bad value for %%"+k);return}switch(k[0]){case"d":k="dyn";break;case"g":if(k[1]=="c")k="gch";else k="gst";break;case"o":k="orn";break;case"s":k="stm";break;case"v":if(k[2]=="c")k="voc";else k="vol";break;default:return}if(curvoice)curvoice.pos=clone(curvoice.pos);set_v_param(k,val,"pos")}function set_writefields(param){var c,i,a=param.split(/\s+/);if(get_bool(a[1])){for(i=0;i<a[0].length;i++){c=a[0][i];if(cfmt.writefields.indexOf(c)<0)cfmt.writefields+=c}}else{for(i=0;i<a[0].length;i++){c=a[0][i];if(cfmt.writefields.indexOf(c)>=0)cfmt.writefields=cfmt.writefields.replace(c,"")}}}function set_v_param(k,v,sub){if(curvoice){if(sub)curvoice[sub][k]=v;else curvoice[k]=v;return}k=[k+"=",v];var vid="*";if(!info.V)info.V={};if(info.V[vid])Array.prototype.push.apply(info.V[vid],k);else info.V[vid]=k}function set_format(cmd,param,lock){var f,f2,v,box;if(lock){fmt_lock[cmd]=true}else if(fmt_lock[cmd])return;if(cmd.match(/.+font$/)||cmd.match(/.+font-[\d]$/)){if(param.slice(-4)==" box"){box=true;param=param.slice(0,-4)}param_set_font(cmd,param);switch(cmd){case"gchordfont":cfmt.gchordbox=box;break;case"measurefont":cfmt.measurebox=box;break;case"partsfont":cfmt.partsbox=box;break}return}switch(cmd){case"aligncomposer":case"barsperstaff":case"infoline":case"measurefirst":case"measurenb":case"rbmax":case"rbmin":case"shiftunison":case"staffnonote":cfmt[cmd]=get_int(param);break;case"microscale":f=get_int(param);if(isNaN(f)||f<4||f>256||f%1){parse.line.error("Bad value for %%microscale");break}set_v_param("uscale",f);break;case"bgcolor":case"dblrepbar":case"titleformat":cfmt[cmd]=param;break;case"breaklimit":case"lineskipfac":case"maxshrink":case"pagescale":case"parskipfac":case"scale":case"slurheight":case"stemheight":case"stretchlast":f=parseFloat(param);if(isNaN(f)){parse.line.error("Bad value for %%$1",cmd);break}if(cmd=="scale")f/=.75;else if(cmd=="pagescale")cmd="scale";cfmt[cmd]=f;break;case"bstemdown":case"breakoneoln":case"cancelkey":case"custos":case"decoerr":case"dynalign":case"flatbeams":case"gchordbox":case"graceslurs":case"graceword":case"hyphencont":case"keywarn":case"linewarn":case"measurebox":case"partsbox":case"rbdbstop":case"singleline":case"squarebreve":case"straightflags":case"stretchstaff":case"timewarn":case"titlecaps":case"titleleft":case"titletrim":cfmt[cmd]=get_bool(param);break;case"composerspace":case"indent":case"infospace":case"maxstaffsep":case"maxsysstaffsep":case"musicspace":case"partsspace":case"staffsep":case"subtitlespace":case"sysstaffsep":case"textspace":case"titlespace":case"topspace":case"vocalspace":case"wordsspace":cfmt[cmd]=get_unit(param);break;case"leftmargin":case"pagewidth":case"rightmargin":cfmt[cmd]=get_unitp(param);break;case"concert-score":cfmt.sound="concert";break;case"contbarnb":cfmt.contbarnb=get_int(param);break;case"writefields":set_writefields(param);break;case"dynamic":case"gchord":case"gstemdir":case"ornament":case"stemdir":case"vocal":case"volume":set_pos(cmd,param);break;case"font":get_font_scale(param);break;case"fullsvg":if(parse.state!=0){parse.line.error("Cannot have %%fullsvg inside a tune");break}cfmt[cmd]=param;break;case"gracespace":cfmt[cmd]=param.split(/\s+/);break;case"tuplets":cfmt[cmd]=param.split(/\s+/);v=cfmt[cmd][3];if(v&&posval[v])cfmt[cmd][3]=posval[v];break;case"infoname":set_infoname(param);break;case"notespacingfactor":f=parseFloat(param);if(isNaN(f)||f<1||f>2){parse.line.error("Bad value for %%notespacingfactor");break}i=5;f2=space_tb[i];for(;--i>=0;){f2/=f;space_tb[i]=f2}i=5;f2=space_tb[i];for(;++i<space_tb.length;){f2*=f;space_tb[i]=f2}break;case"play":cfmt.sound="play";break;case"pos":cmd=param.split(/\s+/);set_pos(cmd[0],cmd[1]);break;case"sounding-score":cfmt.sound="sounding";break;case"staffwidth":v=cfmt.pagewidth-get_unitp(param)-cfmt.leftmargin;if(v<0)parse.line.error("%%staffwidth too big");else cfmt.rightmargin=v;break;case"textoption":cfmt[cmd]=get_textopt(param);break;case"combinevoices":case"voicecombine":v=parseInt(param);if(isNaN(v)){parse.line.error("Bad value for %%voicecombine");return}if(curvoice&&cmd=="combinevoices"){for(f=0;f<voice_tb.length;f++)voice_tb[f].combine=v;break}set_v_param("combine",v);break;case"voicemap":set_v_param("map",param);break;case"voicescale":v=parseFloat(param);if(isNaN(v)||v<.6||v>1.5){parse.line.error("Bad value for %%voicescale");return}set_v_param("scale",v);break}}function font_init(){param_set_font("annotationfont","sans-serif 12");param_set_font("composerfont","serifItalic 14");param_set_font("gchordfont","sans-serif 12");param_set_font("historyfont","serif 16");param_set_font("infofont","serifItalic 14");param_set_font("measurefont","serifItalic 14");param_set_font("partsfont","serif 15");param_set_font("repeatfont","serif 13");param_set_font("subtitlefont","serif 16");param_set_font("tempofont","serifBold 15");param_set_font("textfont","serif 16");param_set_font("titlefont","serif 20");param_set_font("vocalfont","serifBold 13");param_set_font("voicefont","serifBold 13");param_set_font("wordsfont","serif 16")}function style_add_font(font){var name=font.name,i=name.indexOf("Italic"),j=100,o=name.indexOf("Oblique"),b=name.indexOf("Bold");font_style+="\n.f"+font.fid+cfmt.fullsvg+" {font:";if(b>0){font_style+="bold ";j=b}if(i>0||o>0){if(i>0){font_style+="italic ";if(i<j)j=i}if(o>0){font_style+="oblique ";if(o<j)j=o}}if(j!=100){if(name[j-1]=="-")j--;name=name.slice(0,j)}font_style+=font.size+"px "+name+"}"}function use_font(font){if(!defined_font[font.fid]){defined_font[font.fid]=true;style_add_font(font)}}function get_font(xxx){xxx+="font";var fn=cfmt[xxx],font=font_tb[fn];if(!font){parse.line.error("Unknown font $1",xxx);font=gene.curfont}if(!font.fid)font.fid=fid++;use_font(font);return font}var abc_utf={"`A":"À","`E":"È","`I":"Ì","`O":"Ò","`U":"Ù","`a":"à","`e":"è","`i":"ì","`o":"ò","`u":"ù","'A":"Á","'E":"É","'I":"Í","'O":"Ó","'U":"Ú","'Y":"Ý","'a":"á","'e":"é","'i":"í","'o":"ó","'u":"ú","'y":"ý","'S":"Ś","'Z":"Ź","'s":"ś","'z":"ź","'R":"Ŕ","'L":"Ĺ","'C":"Ć","'N":"Ń","'r":"ŕ","'l":"ĺ","'c":"ć","'n":"ń","^A":"Â","^E":"Ê","^I":"Î","^O":"Ô","^U":"Û","^a":"â","^e":"ê","^i":"î","^o":"ô","^u":"û","^H":"Ĥ","^J":"Ĵ","^h":"ĥ","^j":"ĵ","^C":"Ĉ","^G":"Ĝ","^S":"Ŝ","^c":"ĉ","^g":"ĝ","^s":"ŝ",",C":"Ç",",c":"ç",",S":"Ş",",s":"ş",",T":"Ţ",",t":"ţ",",R":"Ŗ",",L":"Ļ",",G":"Ģ",",r":"ŗ",",l":"ļ",",g":"ģ",",N":"Ņ",",K":"Ķ",",n":"ņ",",k":"ķ",'"A':"Ä",'"E':"Ë",'"I':"Ï",'"O':"Ö",'"U':"Ü",'"Y':"Ÿ",'"a':"ä",'"e':"ë",'"i':"ï",'"o':"ö",'"u':"ü",'"y':"ÿ","~A":"Ã","~N":"Ñ","~O":"Õ","~a":"ã","~n":"ñ","~o":"õ","~I":"Ĩ","~i":"ĩ","~U":"Ũ","~u":"ũ",oA:"Å",oa:"å",oU:"Ů",ou:"ů","=A":"Ā","=D":"Đ","=E":"Ē","=H":"Ħ","=I":"Ī","=O":"Ō","=T":"Ŧ","=U":"Ū","=a":"ā","=d":"đ","=e":"ē","=h":"ħ","=i":"ī","=o":"ō","=t":"ŧ","=u":"ū","/O":"Ø","/o":"ø","/D":"Đ","/d":"đ","/L":"Ł","/l":"ł",";A":"Ą",";E":"Ę",";I":"Į",";U":"Ų",";a":"ą",";e":"ę",";i":"į",";u":"ų",vL:"Ľ",vS:"Š",vT:"Ť",vZ:"Ž",vl:"ľ",vs:"š",vt:"ť",vz:"ž",vC:"Č",vE:"Ě",vD:"Ď",vN:"Ň",vR:"Ř",vc:"č",ve:"ě",vd:"ď",vn:"ň",vr:"ř",uA:"Ă",ua:"ă",uE:"Ĕ",ue:"ĕ",uG:"Ğ",ug:"ğ",uI:"Ĭ",ui:"ĭ",uO:"Ŏ",uo:"ŏ",uU:"Ŭ",uu:"ŭ",":O":"Ő",":U":"Ű",":o":"ő",":u":"ű",".Z":"Ż",".z":"ż",".I":"İ",".i":"ı",".C":"Ċ",".c":"ċ",".G":"Ġ",".g":"ġ",".E":"Ė",".e":"ė",AA:"Å",aa:"å",AE:"Æ",ae:"æ",cc:"ç",cC:"Ç",DH:"Ð",dh:"ð",ng:"ŋ",OE:"Œ",oe:"œ",ss:"ß",TH:"Þ",th:"þ"};function cnv_escape(src){var c,c2,dst="",i,j=0,codeUnits;while(1){i=src.indexOf("\\",j);if(i<0)break;dst+=src.slice(j,i);c=src[++i];if(!c)return dst+"\\";switch(c){case"0":case"2":if(src[i+1]=="0"){switch(src[i+2]){case"1":dst+="♯";j=i+3;continue;case"2":dst+="♭";j=i+3;continue;case"3":dst+="♮";j=i+3;continue;case"4":dst+="&#x1d12a;";j=i+3;continue;case"5":dst+="&#x1d12b;";j=i+3;continue}}case"1":case"3":if(src[i+1]>="0"&&src[i+1]<="7"&&src[i+2]>="0"&&src[i+2]<="7"){j=parseInt(src.slice(i,i+3),8);dst+=String.fromCharCode(j);j=i+3;continue}break;case"u":codeUnits=[];j=parseInt(src.slice(i+1,i+5),16);codeUnits.push(j);if(j>=55296&&j<=57343){j=parseInt(src.slice(i+7,i+11),16);codeUnits.push(j);j=i+11}else{j=i+5}dst+=String.fromCharCode.apply(null,codeUnits);continue;default:c2=abc_utf[src.slice(i,i+2)];if(c2){dst+=c2;j=i+2;continue}break}dst+="\\"+c;j=i+1}return dst+src.slice(j)}var include=0;function do_include(fn){var file,parse_sav;if(!user.read_file){syntax(1,"No read_file support");return}if(include>2){syntax(1,"Too many include levels");return}include++;file=user.read_file(fn);if(!file){syntax(1,"Cannot read file '$1'",fn);return}if(fn.slice(-3)==".js"){eval(file)}else{parse_sav=clone(parse);tosvg(fn,file);parse=parse_sav}include--}function tosvg(in_fname,file){var i,c,bol,eol,boc,eoc,end,ext,select,line0,line1,last_info,opt,text,a,b,s,cfmt_sav,info_sav,char_tb_sav,glovar_sav,maps_sav,pscom,txt_add="\n",eof=file.length;function set_boc(){boc=bol;while(boc<eol){c=file[boc];if(c!=" "&&c!="\t")break;boc++}}function set_eoc(){eoc=eol-1;while(eoc>bol){c=file[eoc];if(c!=" "&&c!="\t")break;eoc--}eoc++}function tune_selected(){var i=file.indexOf("K:",bol);if(i<0){return false}i=file.indexOf("\n",i);if(parse.select.test(file.slice(bol,i)))return true;eol=file.indexOf("\n\n",i);if(eol<0)eol=eof-1;else eol++;return false}function uncomment(src,do_escape){var i,j,c,l;if(do_escape&&src.indexOf("\\")>=0)src=cnv_escape(src);l=src.length;for(i=0;i<l;i++){c=src[i];switch(c){case"\\":i++;continue;case"%":return src.slice(0,i).replace(/\s+$/,"");case'"':break;default:continue}j=i+1;for(;;){j=src.indexOf('"',j);if(j<0)break;if(src[j-1]!="\\")break}if(j<0)break;i=j}src=src.replace(/\s+$/,"");return src.replace(/\\%/g,"%")}function end_tune(){generate();if(info.W)put_words(info.W);put_history();blk_out();blk_flush();parse.state=0;cfmt=cfmt_sav;posx=cfmt.leftmargin/cfmt.scale;info=info_sav;char_tb=char_tb_sav;glovar=glovar_sav;maps=maps_sav;init_tune()}parse.file=file;parse.ctx={fname:in_fname};var line=new scanBuf;parse.line=line;line.buffer=file;line.index=0;bol=0;for(bol=0;;bol=eol+1){if(bol>=eof)break;eol=file.indexOf("\n",bol);if(eol<0)eol=eof;if(eol==bol){if(parse.state==1){parse.istart=bol;syntax(1,"Empty line in tune header - ignored")}else if(parse.state>=2)end_tune();continue}parse.istart=parse.bol=bol;parse.iend=parse.eol=eol;line0=file[bol];line1=file[bol+1];if(line0=="%"){if(parse.prefix.indexOf(line1)<0)continue;if(file[bol+2]=="a"&&file[bol+3]=="b"&&file[bol+4]=="c"&&file[bol+5]==" "){bol+=6;line0=file[bol];line1=file[bol+1]}else{pscom=true}}else if(line0=="I"&&line1==":"){pscom=true}if(pscom){pscom=false;bol+=2;set_boc();set_eoc();text=file.slice(boc,eoc);if(!text||text[0]=="%")continue;a=text.split(/\s+/,2);if(!a[0])a.shift();switch(a[0]){case"abcm2ps":case"ss-pref":parse.prefix=a[1];continue;case"abc-include":ext=a[1].match(/.*\.(.*)/);if(!ext)continue;switch(ext[1]){case"abc":case"js":do_include(a[1]);break}continue}b=a[0].match(/begin(.*)/);if(b){end="\n"+line0+line1+"end"+b[1];i=file.indexOf(end,eol);if(i<0){syntax(1,"No $1 after %%$2",end.slice(1),b[0]);eol=eof;continue}do_begin_end(b[1],a[1],parse.file.slice(eol+1,i).replace(new RegExp("^"+line0+line1,"gm"),""));eol=file.indexOf("\n",i+6);if(eol<0)eol=eof;continue}switch(a[0]){case"select":
  if(parse.state!=0){syntax(1,"%%select ignored");continue}select=uncomment(text.slice(7).trim(),false);if(select[0]=='"')select=select.slice(1,-1);select=select.replace(/\(/g,"\\(");select=select.replace(/\)/g,"\\)");parse.select=new RegExp(select,"m");continue;case"tune":syntax(1,"%%tune not treated yet");continue;case"voice":if(parse.state!=0){syntax(1,"%%voice ignored");continue}select=uncomment(text.slice(6).trim(),false);if(!select){if(parse.cur_tune_opts)parse.cur_tune_opts.voice_opts=null;else parse.voice_opts=null;continue}if(select=="end")continue;if(parse.cur_tune_opts){if(!parse.cur_tune_opts.voice_opts)parse.cur_tune_opts.voice_opts={};opt=parse.cur_tune_opts.voice_opts}else{if(!parse.voice_opts)parse.voice_opts={};opt=parse.voice_opts}opt[select]=[];while(1){bol=++eol;if(file[bol]!="%")break;eol=file.indexOf("\n",eol);if(file[bol+1]!=line1)continue;bol+=2;if(eol<0)text=file.slice(bol);else text=file.slice(bol,eol);a=text.match(/\S+/);switch(a[0]){default:opt[select].push(uncomment(text.trim(),true));continue;case"score":case"staves":case"tune":case"voice":bol-=2;break}break}eol=bol-1;continue}do_pscom(uncomment(text.trim(),true));continue}if(line1!=":"){last_info=undefined;parse_music_line();continue}text=uncomment(file.slice(bol+2,eol).trim(),true);if(line0=="+"){if(!last_info){syntax(1,"+: without previous info field");continue}txt_add=" ";line0=last_info}switch(line0){case"X":if(parse.state!=0){syntax(1,"$1: inside tune - ignored",line0);continue}if(parse.select&&!tune_selected())continue;cfmt_sav=clone(cfmt);cfmt.pos=clone(cfmt.pos);info_sav=clone(info);if(info.V){info_sav.V={};for(i in info.V)info_sav.V[i]=clone(info.V[i])}char_tb_sav=clone(char_tb);glovar_sav=clone(glovar);maps_sav=maps;info.X=text;parse.state=1;continue;case"T":switch(parse.state){case 0:continue;case 1:if(info.T==undefined)info.T=text;else info.T+="\n"+text;continue}s=new_block("title");s.text=text;continue;case"K":switch(parse.state){case 0:continue;case 1:info.K=text;break}parse.line.buffer=text;parse.line.index=0;do_info(line0,text);continue;case"W":if(parse.state==0||cfmt.writefields.indexOf(line0)<0)break;if(!info.W)info.W=text;else info.W+=txt_add+text;break;case"s":if(parse.state!=3)break;break;case"w":if(parse.state!=3||cfmt.writefields.indexOf(line0)<0)break;get_lyrics(text,txt_add==" ");if(text.slice(-1)=="\\"){txt_add=" ";last_info=line0;continue}break;case"|":parse_music_line();continue;default:if("ABCDFGHOSZ".indexOf(line0)>=0){if(parse.state>=2){syntax(1,"$1: inside tune - ignored",line0);continue}if(!info[line0])info[line0]=text;else info[line0]+=txt_add+text;break}parse.line.buffer=text;parse.line.index=0;do_info(line0,text);continue}txt_add="\n";last_info=line0}if(include)return;if(parse.state>=2)end_tune();blk_flush();parse.state=0}Abc.prototype.tosvg=tosvg;var gene,staff_tb,nstaff,tsnext,realwidth,insert_meter,beta_last,space_tb=[7,10,14.15,20,28.3,40,56.6,80,113,150],smallest_duration;const dx_tb=[10,10,11,13,13];const hw_tb=[4.5,5,6,7,8];function set_head_shift(s){var i,i1,i2,d,ps,dx,dx_head=dx_tb[s.head],dir=s.stem,n=s.nhd;if(n==0){if(s.notes[0].acc){if(s.grace)dx_head*=.7;s.notes[0].shac=dx_head}return}dx=dx_head*.78;if(s.grace)dx*=.5;if(dir>=0){i1=1;i2=n+1;ps=s.notes[0].pit}else{dx=-dx;i1=n-1;i2=-1;ps=s.notes[n].pit}var shift=false,dx_max=0;for(i=i1;i!=i2;i+=dir){d=s.notes[i].pit-ps;ps=s.notes[i].pit;if(d==0){if(shift){var new_dx=s.notes[i].shhd=s.notes[i-dir].shhd+dx;if(dx_max<new_dx)dx_max=new_dx;continue}if(i+dir!=i2&&ps+dir==s.notes[i+dir].pit){s.notes[i].shhd=-dx;if(dx_max<-dx)dx_max=-dx;continue}}if(d<0)d=-d;if(d>3||d>=2&&s.head!=SQUARE){shift=false}else{shift=!shift;if(shift){s.notes[i].shhd=dx;if(dx_max<dx)dx_max=dx}}}s.xmx=dx_max}function set_acc_shft(){var s,s2,st,i,acc,st,t,dx_head;function acc_shift(notes,dx_head){var i,i1,dx,dx1,ps,p1,acc,n=notes.length;for(i=n-1;--i>=0;){dx=notes[i].shhd;if(!dx||dx>0)continue;dx=dx_head-dx;ps=notes[i].pit;for(i1=n;--i1>=0;){if(!notes[i1].acc)continue;p1=notes[i1].pit;if(p1<ps-3)break;if(p1>ps+3)continue;if(notes[i1].shac<dx)notes[i1].shac=dx}}for(i=n;--i>=0;){acc=notes[i].acc;if(!acc)continue;dx=notes[i].shac;if(!dx){dx=notes[i].shhd;if(dx<0)dx=dx_head-dx;else dx=dx_head}ps=notes[i].pit;for(i1=n;--i1>i;){if(!notes[i1].acc)continue;p1=notes[i1].pit;if(p1>=ps+4){if(p1>ps+4||acc<0||notes[i1].acc<0)continue}if(dx>notes[i1].shac-6){dx1=notes[i1].shac+7;if(dx1>dx)dx=dx1}}notes[i].shac=dx}}s=tsfirst;while(s){if(s.type!=NOTE||s.invis){s=s.ts_next;continue}st=s.st;t=s.time;acc=false;for(s2=s;s2;s2=s2.ts_next){if(s2.time!=t||s2.type!=NOTE||s2.st!=st)break;if(acc)continue;for(i=0;i<=s2.nhd;i++){if(s2.notes[i].acc){acc=true;break}}}if(!acc){s=s2;continue}dx_head=dx_tb[s.head];st={notes:[]};for(;s!=s2;s=s.ts_next)st.notes=st.notes.concat(s.notes);sort_pitch(st);acc_shift(st.notes,dx_head)}}function unlksym(s){var g;if(s.next)s.next.prev=s.prev;if(s.prev)s.prev.next=s.next;else s.p_v.sym=s.next;if(s.ts_next){if(s.seqst){s.ts_next.seqst=true;if(s.ts_next.shrink<s.shrink)s.ts_next.shrink=s.shrink;if(s.ts_next.space<s.space)s.ts_next.space=s.space}s.ts_next.ts_prev=s.ts_prev}if(s.ts_prev)s.ts_prev.ts_next=s.ts_next;if(tsfirst==s)tsfirst=s.ts_next;if(tsnext==s)tsnext=s.ts_next}function may_combine(s){var nhd2,s2=s.ts_next;if(!s2||s2.type!=NOTE&&s2.type!=REST)return false;if(s2.v==s.v||s2.st!=s.st||s2.time!=s.time||s2.dur!=s.dur)return false;if(s.combine<=0&&s2.type!=s.type)return false;if(s.a_gch&&s2.a_gch)return false;if(s.type==REST){if(s.type==s2.type&&s.invis&&!s2.invis)return false;return true}if(s2.a_ly||s2.sl1||s2.sl2||s2.slur_start||s2.slur_end)return false;if(s2.beam_st!=s.beam_st||s2.beam_end!=s.beam_end)return false;nhd2=s2.nhd;if(s.combine<=1&&s.notes[0].pit<=s2.notes[nhd2].pit+1)return false;return true}function combine_notes(s,s2){var nhd,type,m;s.notes=s.notes.concat(s2.notes);s.nhd=nhd=s.notes.length-1;sort_pitch(s);if(s.combine>=3){for(m=nhd;m>0;m--){if(s.notes[m].pit==s.notes[m-1].pit&&s.notes[m].acc==s.notes[m-1].acc)s.notes.splice(m,1)}s.nhd=nhd=s.notes.length-1}s.ymx=3*(s.notes[nhd].pit-18)+4;s.ymn=3*(s.notes[0].pit-18)-4;s.yav=(s.ymx+s.ymn)/2;if(s.a_dd){if(s2.a_dd)s.a_dd=s.a_dd.concat(s2.a_dd)}else{s.a_dd=s2.a_dd}type=s.notes[0].ti1;if((type&15)==SL_AUTO)s.notes[0].ti1=SL_BELOW|type&~SL_DOTTED;type=s.notes[nhd].ti1;if((type&15)==SL_AUTO)s.notes[nhd].ti1=SL_ABOVE|type&~SL_DOTTED}function do_combine(s){var s2,nhd,nhd2,type;while(1){nhd=s.nhd;s2=s.ts_next;nhd2=s2.nhd;if(s.type!=s2.type){if(s2.type!=REST){s2=s;s=s2.ts_next}}else if(s.type==REST){if(s.invis&&!s2.invis)delete s.invis}else{combine_notes(s,s2)}if(s2.text)s.text=s2.text;if(s2.a_gch)s.a_gch=s2.a_gch;unlksym(s2);if(s.in_tuplet||!may_combine(s))break}}function combine_voices(){var s,s2,g,i,r;for(s=tsfirst;s.ts_next;s=s.ts_next){switch(s.type){case REST:if(s.combine<0)continue;if(may_combine(s))do_combine(s);continue;default:continue;case NOTE:if(s.combine<=0)continue;break}if(!s.beam_st)continue;if(s.beam_end){if(may_combine(s))do_combine(s);continue}s2=s;while(1){if(!may_combine(s2)){s2=null;break}if(s2.beam_end)break;do{s2=s2.next}while(s2.type!=NOTE&&s2.type!=REST)}if(!s2)continue;s2=s;while(1){do_combine(s2);if(s2.beam_end)break;do{s2=s2.next}while(s2.type!=NOTE&&s2.type!=REST)}}}function insert_clef(s,clef_type,clef_line){var p_voice=s.p_v,new_s,st=s.st;if(s.type==BAR&&s.prev&&s.prev.type==BAR)s=s.prev;p_voice.last_sym=s.prev;if(!p_voice.last_sym)p_voice.sym=null;p_voice.time=s.time;new_s=sym_add(p_voice,CLEF);new_s.next=s;s.prev=new_s;new_s.clef_type=clef_type;new_s.clef_line=clef_line;new_s.st=st;new_s.clef_small=true;new_s.second=undefined;new_s.notes=[];new_s.notes[0]={pit:s.notes[0].pit};new_s.nhd=0;while(!s.seqst)s=s.ts_prev;if(s.ts_prev.type!=CLEF)new_s.seqst=true;new_s.ts_prev=s.ts_prev;new_s.ts_prev.ts_next=new_s;new_s.ts_next=s;s.ts_prev=new_s;return new_s}function set_float(){var p_voice,st,staff_chg,v,s,s1,up,down;for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];staff_chg=false;st=p_voice.st;for(s=p_voice.sym;s;s=s.next){if(!s.floating){while(s&&!s.floating)s=s.next;if(!s)break;staff_chg=false}if(!s.dur){if(staff_chg)s.st++;continue}if(s.notes[0].pit>=19){staff_chg=false;continue}if(s.notes[s.nhd].pit<=12){staff_chg=true;s.st++;continue}up=127;for(s1=s.ts_prev;s1;s1=s1.ts_prev){if(s1.st!=st||s1.v==s.v)break;if(s1.type==NOTE)if(s1.notes[0].pit<up)up=s1.notes[0].pit}if(up==127){if(staff_chg)s.st++;continue}if(s.notes[s.nhd].pit>up-3){staff_chg=false;continue}down=-127;for(s1=s.ts_next;s1;s1=s1.ts_next){if(s1.st!=st+1||s1.v==s.v)break;if(s1.type==NOTE)if(s1.notes[s1.nhd].pit>down)down=s1.notes[s1.nhd].pit}if(down==-127){if(staff_chg)s.st++;continue}if(s.notes[0].pit<down+3){staff_chg=true;s.st++;continue}up-=s.notes[s.nhd].pit;down=s.notes[0].pit-down;if(!staff_chg){if(up<down+3)continue;staff_chg=true}else{if(up<down-3){staff_chg=false;continue}}s.st++}}}function set_graceoffs(s){var next,m,gspleft=Number(cfmt.gracespace[0]),gspinside=Number(cfmt.gracespace[1]),gspright=Number(cfmt.gracespace[2]),xx=0,g=s.extra;g.beam_st=true;for(;;g=g.next){set_head_shift(g);for(m=g.nhd;m>=0;m--){if(g.notes[m].acc){xx+=5;if(g.notes[m].micro)xx+=2;break}}g.x=xx;if(g.nflags<=0){g.beam_st=true;g.beam_end=true}next=g.next;if(!next){g.beam_end=true;break}if(next.nflags<=0)g.beam_end=true;if(g.beam_end){next.beam_st=true;xx+=gspinside/4}if(g.nflags<=0)xx+=gspinside/4;if(g.y>next.y+8)xx-=1.5;xx+=gspinside}xx+=gspleft+gspright;next=s.next;if(next&&next.type==NOTE){if(g.y>=3*(next.notes[next.nhd].pit-18))xx-=1;else if(g.beam_st&&g.y<3*(next.notes[0].pit-18)-7)xx+=2}return xx}function gchord_width(s,wlnote,wlw){var s2,gch,w,wl,ix,lspc=0,rspc=0,alspc=0,arspc=0;for(ix=0;ix<s.a_gch.length;ix++){gch=s.a_gch[ix];switch(gch.type){default:wl=-gch.x;if(wl>lspc)lspc=wl;w=gch.w+2-wl;if(w>rspc)rspc=w;break;case"<":w=gch.w+wlnote;if(w>alspc)alspc=w;break;case">":w=gch.w+s.wr;if(w>arspc)arspc=w;break}}s2=s.prev;if(s2){if(s2.a_gch){for(s2=s.ts_prev;;s2=s2.ts_prev){if(s2==s.prev){if(wlw<lspc)wlw=lspc;break}if(s2.seqst)lspc-=s2.shrink}}if(alspc!=0)if(wlw<alspc)wlw=alspc}s2=s.next;if(s2){if(s2.a_gch){for(s2=s.ts_next;;s2=s2.ts_next){if(s2==s.next){if(s.wr<rspc)s.wr=rspc;break}if(s2.seqst)rspc-=8}}if(arspc!=0)if(s.wr<arspc)s.wr=alspc}return wlw}function set_width(s){var s2,i,m,xx,w,wlnote,wlw,acc;switch(s.type){case NOTE:case REST:s.wr=wlnote=hw_tb[s.head];if(s.xmx>0)s.wr+=s.xmx+4;s2=s.prev;if(s2){switch(s2.type){case BAR:case CLEF:case KEY:case METER:wlnote+=3;break}}for(m=0;m<=s.nhd;m++){xx=s.notes[m].shhd;if(xx<0){if(wlnote<-xx+5)wlnote=-xx+5}if(s.notes[m].acc){var tmp=s.notes[m].shac+(s.notes[m].micro?6.5:4.5);if(wlnote<tmp)wlnote=tmp}}if(s2){switch(s2.type){case BAR:case CLEF:case KEY:case METER:wlnote-=3;break}}if(s.a_dd)wlnote+=deco_width(s);if(s.beam_st&&s.beam_end&&s.stem>0&&s.nflags>0){if(s.wr<s.xmx+9)s.wr=s.xmx+9}if(s.dots>0){switch(s.head){case SQUARE:case OVALBARS:case OVAL:s.xmx+=2;break;case EMPTY:s.xmx+=1;break}if(s.wr<s.xmx+12)s.wr=s.xmx+12;if(s.dots>=2)s.wr+=3.5*(s.dots-1)}if(s.trem2&&s.beam_end&&wlnote<20)wlnote=20;wlw=wlnote;if(s2){switch(s2.type){case NOTE:if(s2.stem>0&&s.stem<0){if(wlw<7)wlw=7}if(s.y>27&&s2.y>27||s.y<-3&&s2.y<-3){if(wlw<6)wlw=6}if(s2.ti1){if(wlw<14)wlw=14}break;case CLEF:if(s2.second||s2.clef_small)break;wlw+=8;break;case KEY:wlw+=4;break}}if(s.a_gch)wlw=gchord_width(s,wlnote,wlw);if(s.a_ly)wlw=ly_width(s,wlw);if(s2&&s2.type==GRACE)s.wl=wlnote-4.5;else s.wl=wlw;return;case SPACE:xx=s.width/2;s.wr=xx;if(s.a_gch)xx=gchord_width(s,xx,xx);if(s.a_dd)xx+=deco_width(s);s.wl=xx;return;case BAR:if(s.norepbra)break;if(!s.invis){var bar_type=s.bar_type;switch(bar_type){case"|":w=5+3;break;case"|:":case":|":w=5+3+3+5;break;case"::":w=5+5+3+3+3+5;break;default:if(!bar_type)break;w=5+3*bar_type.length;for(i=0;i<bar_type.length;i++){switch(bar_type[i]){case"[":case"]":w+=3;break;case":":w+=2;break}}break}s.wl=w;if(s.next&&s.next.type!=METER)s.wr=8;else s.wr=5}else{s.wl=s.wr=0}if(s.a_dd)s.wl+=deco_width(s);if(s.a_gch&&s.a_gch[0].text.length<4)s.wl=gchord_width(s,s.wl,s.wl);return;case CLEF:s.wl=s.wr=s.clef_small?9:12;return;case KEY:var n1,n2,esp;s.wl=3;esp=4;if(!s.k_a_acc){n1=s.k_sf;if(s.k_old_sf&&(cfmt.cancelkey||n1==0))n2=s.k_old_sf;else n2=0;if(n1*n2>=0){if(n1<0)n1=-n1;if(n2<0)n2=-n2;if(n2>n1)n1=n2}else{n1-=n2;if(n1<0)n1=-n1;esp+=3}}else{n1=n2=s.k_a_acc.length;var last_acc=s.k_a_acc[0].acc;for(i=1;i<n2;i++){acc=s.k_a_acc[i];if(acc.pit>s.k_a_acc[i-1].pit+6||acc.pit<s.k_a_acc[i-1].pit-6)n1--;else if(acc.acc!=last_acc)esp+=3;last_acc=acc.acc}}s.wr=5.5*n1+esp;return;case METER:w=0;for(i=0;i<s.a_meter.length;i++){var meter=s.a_meter[i];if(meter.top=="C|"){w+=6.5}else{if(!meter.bot||meter.top.length>meter.bot.length)w+=6.5*meter.top.length;else w+=6.5*meter.bot.length}}s.wl=w;s.wr=w+7;return;case MREST:s.wl=6;s.wr=66;return;case GRACE:s.wl=set_graceoffs(s);s.wr=0;if(s.a_ly)ly_width(s,wlw);return;case STBRK:s.wl=s.xmx;if(s.next&&s.next.type==CLEF){s.wr=2;delete s.next.clef_small}else{s.wr=8}return;case BLOCK:case PART:case REMARK:case STAVES:case TEMPO:break;default:error(2,s,"set_width - Cannot set width for symbol $1",s.type);break}s.wl=s.wr=0}function set_space(s){var s2,i,l,space,prev_time=s.ts_prev.time,len=s.time-prev_time;if(len==0){switch(s.type){case MREST:return s.wl}return 0}if(s.ts_prev.type==MREST)return 71;if(smallest_duration>=BASE_LEN/2){if(smallest_duration>=BASE_LEN)len/=4;else len/=2}if(len>=BASE_LEN/4){if(len<BASE_LEN/2)i=5;else if(len<BASE_LEN)i=6;else if(len<BASE_LEN*2)i=7;else if(len<BASE_LEN*4)i=8;else i=9}else{if(len>=BASE_LEN/8)i=4;else if(len>=BASE_LEN/16)i=3;else if(len>=BASE_LEN/32)i=2;else if(len>=BASE_LEN/64)i=1;else i=0}l=len-(BASE_LEN/16/8<<i);space=space_tb[i];if(l!=0){if(l<0){space=space_tb[0]*len/(BASE_LEN/16/8)}else{if(i>=9)i=8;space+=(space_tb[i+1]-space_tb[i])*l/len}}if(!s.dur){switch(s.type){case BAR:space*=.6;break;case CLEF:space-=s.wl;break}return space}if(!s.beam_st)space*=.9;if(s.type==NOTE&&s.nflags>=-1&&s.stem>0){var stemdir=true;for(s2=s.ts_prev;s2&&s2.time==prev_time;s2=s2.ts_prev){if(s2.type==NOTE&&(s2.nflags<-1||s2.stem>0)){stemdir=false;break}}if(stemdir){for(s2=s.ts_next;s2&&s2.time==s.time;s2=s2.ts_next){if(s2.type==NOTE&&(s2.nflags<-1||s2.stem<0)){stemdir=false;break}}if(stemdir)space*=.9}}return space}function set_allsymwidth(last_s){var new_val,s=tsfirst,xa=0,xl=[];while(1){var maxx=xa;s2=s;do{set_width(s);if(xl[s.st])new_val=xl[s.st]+s.wl;else new_val=s.wl;if(new_val>maxx)maxx=new_val;s=s.ts_next}while(s!=last_s&&!s.seqst);s2.shrink=maxx-xa;if(s2.ts_prev)s2.space=set_space(s2);else s2.space=0;if(s2.shrink==0&&s2.space==0&&s2.type==CLEF){delete s2.seqst;s2.time=s2.ts_prev.time}if(s==last_s)return;xa=maxx;s=s2;do{if(!xl[s.st]||xl[s.st]<xa+s.wr)xl[s.st]=xa+s.wr;s=s.ts_next}while(!s.seqst)}}function to_rest(s){s.type=REST;delete s.in_tuplet;delete s.sl1;delete s.sl2;delete s.a_dd;delete s.a_gch;s.slur_start=s.slur_end=0}const err_no_s="Not enough notes/rests for %%repeat",err_no_m="Not enough measures for %%repeat";function set_repeat(s){var s2,s3,i,j,dur,n=s.repeat_n,k=s.repeat_k,st=s.st,v=s.v;s.repeat_n=0;if(n<0){n=-n;i=n;for(s3=s.prev;s3;s3=s3.prev){if(!s3.dur){if(s3.type==BAR){error(1,s3,"Bar in repeat sequence");return}continue}if(--i<=0)break}if(!s3){error(1,s,err_no_s);return}i=k*n;for(s2=s;s2;s2=s2.next){if(!s2.dur){if(s2.type==BAR){error(1,s2,"Bar in repeat sequence");return}continue}if(--i<=0)break}if(!s2||!s2.next){error(1,s,err_no_s);return}for(s2=s.prev;s2!=s3;s2=s2.prev){if(s2.type==NOTE){s2.beam_end=true;break}}s3=s;for(j=k;--j>=0;){i=n;if(s3.dur)i--;s2=s3.ts_next;while(i>0){if(s2.st!=st)continue;if(s2.v==v&&s2.dur)i--;unlksym(s2);s2=s2.ts_next}to_rest(s3);s3.dur=s3.notes[0].dur=s2.time-s3.time;s3.rep_nb=-1;s3.beam_st=true;set_width(s3);if(s3.seqst)s3.space=set_space(s3);s3.head=SQUARE;s3=s2}return}i=n;for(s2=s.prev.prev;s2;s2=s2.prev){if(s2.type==BAR||s2.time==tsfirst.time){if(--i<=0)break}}if(!s2){error(1,s,err_no_m);return}dur=s.time-s2.time;if(n==1)i=k;else i=n;for(s2=s;s2;s2=s2.next){if(s2.type==BAR){if(--i<=0)break}}if(!s2){error(1,s,err_no_m);return}i=k;if(n==2&&i>1){s2=s2.next;if(!s2){error(1,s,err_no_m);return}s2.repeat_n=n;s2.repeat_k=--i}dur/=n;if(n==2){s3=s;for(s2=s.ts_next;;s2=s2.ts_next){if(s2.st!=st)continue;if(s2.v==v&&s2.type==BAR)break;unlksym(s2)}to_rest(s3);s3.dur=s3.notes[0].dur=dur;s3.invis=true;if(s3.seqst)s3.space=set_space(s3);s2.bar_mrep=2;if(s2.seqst)s2.space=set_space(s2);s3=s2.next;for(s2=s3.ts_next;;s2=s2.ts_next){if(s2.st!=st)continue;if(s2.v==v&&s2.type==BAR)break;unlksym(s2)}to_rest(s3);s3.dur=s3.notes[0].dur=dur;s3.invis=true;set_width(s3);if(s3.seqst)s3.space=set_space(s3);if(s2.seqst)s2.space=set_space(s2);return}s3=s;for(j=k;--j>=0;){for(s2=s3.ts_next;;s2=s2.ts_next){if(s2.st!=st)continue;if(s2.v==v&&s2.type==BAR)break;unlksym(s2)}to_rest(s3);s3.dur=s3.notes[0].dur=dur;s3.beam_st=true;if(s3.seqst)s3.space=set_space(s3);if(s2.seqst)s2.space=set_space(s2);if(k==1){s3.rep_nb=1;break}s3.rep_nb=k-j+1;s3=s2.next}}function custos_add(s){var p_voice,new_s,i,s2=s;while(1){if(s2.type==NOTE)break;s2=s2.next;if(!s2)return}p_voice=s.p_v;p_voice.last_sym=s.prev;p_voice.time=s.time;new_s=sym_add(p_voice,CUSTOS);new_s.next=s;s.prev=new_s;new_s.ts_prev=s.ts_prev;new_s.ts_prev.ts_next=new_s;new_s.ts_next=s;s.ts_prev=new_s;new_s.seqst=true;new_s.wl=8;new_s.wr=4;new_s.shrink=s.shrink;if(new_s.shrink<8+4)new_s.shrink=8+4;new_s.space=s2.space;new_s.nhd=s2.nhd;new_s.notes=[];for(i=0;i<s.notes.length;i++){new_s.notes[i]={pit:s2.notes[i].pit,shhd:0,dur:BASE_LEN/4}}new_s.stemless=true}function set_nl(s){var s2,p_voice,cut_here,done;function set_eol(s){if(cfmt.custos&&voice_tb.length==1)custos_add(s);s.nl=true}function set_eol_next(s){s=s.next;if(!s)return s;while(!s.seqst)s=s.ts_prev;set_eol(s);return s}if(s.eoln&&!cfmt.keywarn&&!cfmt.timewarn)return set_eol_next(s);switch(s.type){case CLEF:case BAR:break;case KEY:if(cfmt.keywarn&&!s.k_none)break;return set_eol_next(s);case METER:if(cfmt.timewarn)break;return set_eol_next(s);case GRACE:s=s.next;if(!s)return s;default:return set_eol_next(s)}for(;s;s=s.ts_prev){if(!s.seqst)continue;switch(s.type){case KEY:case CLEF:case METER:continue}break}done=0;for(;;s=s.ts_next){if(!s)return s;if(!s.seqst)continue;if(done<0)break;switch(s.type){case BAR:if(done)cut_here=true;else done=1;break;case STBRK:if(!s.stbrk_forced){unlksym(s);break}done=-1;break;case METER:if(!cfmt.timewarn)cut_here=true;break;case CLEF:if(done)cut_here=true;break;case KEY:if(!cfmt.keywarn||s.k_none)cut_here=true;break;default:if(!done||s.prev&&s.prev.type==GRACE)break;cut_here=true;break}if(cut_here)break}set_eol(s);return s}function get_ck_width(){var v,p_voice,nv=voice_tb.length;for(v=0;v<nv;v++){p_voice=voice_tb[v];if(p_voice.clef)break}set_width(p_voice.clef);set_width(p_voice.key);if(!cfmt.singleline)return p_voice.clef.wl+p_voice.clef.wr+p_voice.key.wl+p_voice.key.wr;set_width(p_voice.meter);return p_voice.clef.wl+p_voice.clef.wr+p_voice.key.wl+p_voice.key.wr+p_voice.meter.wl+p_voice.meter.wr}function get_width(first,last,w){var s,shrink,space,sp_fac=1-cfmt.maxshrink;for(s=first;s!=last;s=s.ts_next){if(!s.seqst)continue;s.x=w;shrink=s.shrink;if((space=s.space)<shrink)w+=shrink;else w+=shrink*cfmt.maxshrink+space*sp_fac}return w}function set_lines(first,last,lwidth,indent){var s,s2,s3,x,xmin,xmax,wwidth,shrink,space,nlines,cut_here;wwidth=get_width(first,last,indent);s=first;while(1){nlines=Math.ceil(wwidth/lwidth);if(nlines<=1){if(last)last=set_nl(last);return last}s2=first=s;xmin=s.x+wwidth/nlines*cfmt.breaklimit;xmax=s.x+lwidth;cut_here=false;for(;s!=last;s=s.ts_next){x=s.x;if(!x)continue;if(x>xmax)break;if(s.type!=BAR)continue;if(x>xmin){cut_here=true;break}s2=s}if(s==last)return last;if(s.type==BAR)cut_here=true;if(!cut_here){var beam=s2.dur&&!s2.beam_st&&!s2.beam_end?1:0,bar_time=s2.time;s=s2;s2=s3=null;xmax-=6;for(;s!=last;s=s.ts_next){if(s.beam_st){if(!s.beam_end){beam++;continue}}else if(s.beam_end){beam--}x=s.x;if(!x||x<xmin)continue;if(x+x.shrink>=xmax)break;if(beam!=0)continue;s2=s;if((s.time-bar_time)%(BASE_LEN/8)==0)s3=s}if(s3)s2=s3;if(s2)s=s2;while(!s.x||s.x+s.shrink*2>=xmax)s=s.ts_prev}if(s.nl){error(0,s,"Line split problem - adjust maxshrink and/or breaklimit");nlines=2;for(s=s.ts_next;s!=last;s=s.ts_next){if(!s.x)continue;if(--nlines<=0)break}}s=set_nl(s);if(!s||last&&s.time>=last.time)break;wwidth-=s.x-first.x}return s}function cut_tune(lwidth,indent){var s,s2,i,xmin,s=tsfirst;lwidth-=get_ck_width();if(cfmt.custos&&voice_tb.length==1)lwidth-=12;if(cfmt.barsperstaff){i=cfmt.barsperstaff;s2=s;for(;s;s=s.ts_next){if(s.type!=BAR||!s.bar_num)continue;if(--i>0)continue;s.eoln=true;i=cfmt.barsperstaff}s=s2}xmin=indent;s2=s;for(;s;s=s.ts_next){if(!s.seqst&&!s.eoln)continue;xmin+=s.shrink;if(xmin>lwidth){for(;s;s=s.ts_next){if(s.eoln)break}s=s2=set_lines(s2,s,lwidth,indent);if(!s)break;xmin=s.shrink;indent=0;continue}if(!s.eoln)continue;s2=set_nl(s);delete s.eoln;s=s2;if(!s)break;xmin=s.shrink;indent=0}}function set_yval(s){switch(s.type){case CLEF:if(s.second||s.invis){s.ymx=s.ymn=12;break}s.y=(s.clef_line-1)*6;switch(s.clef_type){default:s.ymx=s.y+28;s.ymn=s.y-14;break;case"c":s.ymx=s.y+13;s.ymn=s.y-11;break;case"b":s.ymx=s.y+7;s.ymn=s.y-12;break}if(s.clef_small){s.ymx-=2;s.ymn+=2}if(s.ymx<26)s.ymx=26;if(s.ymn>-1)s.ymn=-1;if(s.clef_octave){if(s.clef_octave>0)s.ymx+=12;else s.ymn-=12}break;case KEY:if(s.k_sf>2)s.ymx=24+10;else if(s.k_sf>0)s.ymx=24+6;else s.ymx=24+2;s.ymn=-2;break;default:s.ymx=24+2;s.ymn=-2;break}}function set_auto_clef(st,s_start,clef_type_start){var s,min,max,time,s2,s3;max=12;min=20;for(s=s_start;s;s=s.ts_next){if(s.type==STAVES&&s!=s_start)break;if(s.st!=st)continue;if(s.type!=NOTE){if(s.type==CLEF){if(s.clef_type!="a")break;unlksym(s)}continue}if(s.notes[0].pit<min)min=s.notes[0].pit;else if(s.notes[s.nhd].pit>max)max=s.notes[s.nhd].pit}if(min>=19||min>=13&&clef_type_start!="b")return"t";if(max<=13||max<=19&&clef_type_start!="t")return"b";if(clef_type_start=="a"){if((max+min)/2>=16)clef_type_start="t";else clef_type_start="b"}var clef_type=clef_type_start,s_last=s,s_last_chg=null;for(s=s_start;s!=s_last;s=s.ts_next){if(s.type==STAVES&&s!=s_start)break;if(s.st!=st||s.type!=NOTE)continue;time=s.time;if(clef_type=="t"){if(s.notes[0].pit>12||s.notes[s.nhd].pit>20){if(s.notes[0].pit>20)s_last_chg=s;continue}s2=s.ts_prev;if(s2&&s2.time==time&&s2.st==st&&s2.type==NOTE&&s2.notes[0].pit>=19)continue;s2=s.ts_next;if(s2&&s2.st==st&&s2.time==time&&s2.type==NOTE&&s2.notes[0].pit>=19)continue}else{if(s.notes[0].pit<12||s.notes[s.nhd].pit<20){if(s.notes[s.nhd].pit<12)s_last_chg=s;continue}s2=s.ts_prev;if(s2&&s2.time==time&&s2.st==st&&s2.type==NOTE&&s2.notes[0].pit<=13)continue;s2=s.ts_next;if(s2&&s2.st==st&&s2.time==time&&s2.type==NOTE&&s2.notes[0].pit<=13)continue}if(!s_last_chg){clef_type=clef_type_start=clef_type=="t"?"b":"t";s_last_chg=s;continue}s3=s;for(s2=s.ts_prev;s2!=s_last_chg;s2=s2.ts_prev){if(s2.st!=st)continue;if(s2.type==BAR&&s2.v==s.v){s3=s2;break}if(s2.type!=NOTE)continue;if(s2.beam_st&&!s2.p_v.second)s3=s2}if(s3.time==s_last_chg.time){s_last_chg=s;continue}s_last_chg=s;clef_type=clef_type=="t"?"b":"t";s2=insert_clef(s3,clef_type,clef_type=="t"?2:4);s2.clef_auto=true}return clef_type_start}function set_clefs(){var s,s2,st,v,p_voice,g,new_type,new_line,p_staff,staff_clef=new Array(nstaff),sy=cur_sy;staff_tb=new Array(nstaff);for(st=0;st<=nstaff;st++){staff_clef[st]={autoclef:true};staff_tb[st]={output:[],sc_out:[]}}for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(sy.voices[v].range<0)continue;st=sy.voices[v].st;if(!sy.voices[v].second){if(p_voice.stafflines!=undefined)sy.staves[st].stafflines=p_voice.stafflines;if(p_voice.staffscale)sy.staves[st].staffscale=p_voice.staffscale;if(sy.voices[v].sep)sy.staves[st].sep=sy.voices[v].sep;if(sy.voices[v].maxsep)sy.staves[st].maxsep=sy.voices[v].maxsep}if(!sy.voices[v].second&&!p_voice.clef.clef_auto)staff_clef[st].autoclef=false}for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(sy.voices[v].range<0||sy.voices[v].second)continue;st=sy.voices[v].st;s=p_voice.clef;if(staff_clef[st].autoclef){s.clef_type=set_auto_clef(st,tsfirst,s.clef_type);s.clef_line=s.clef_type=="t"?2:4}staff_clef[st].clef=staff_tb[st].clef=s}for(s=tsfirst;s;s=s.ts_next){if(s.repeat_n)set_repeat(s);if(s.type==STAVES){sy=sy.next;for(st=0;st<=nstaff;st++)staff_clef[st].autoclef=true;for(v=0;v<voice_tb.length;v++){if(sy.voices[v].range<0)continue;p_voice=voice_tb[v];st=sy.voices[v].st;if(!sy.voices[v].second){if(p_voice.stafflines!=undefined)sy.staves[st].stafflines=p_voice.stafflines;if(p_voice.staffscale)sy.staves[st].staffscale=p_voice.staffscale;if(sy.voices[v].sep)sy.staves[st].sep=sy.voices[v].sep;if(sy.voices[v].maxsep)sy.staves[st].maxsep=sy.voices[v].maxsep}s2=p_voice.clef;if(!s2.clef_auto)staff_clef[st].autoclef=false}for(v=0;v<voice_tb.length;v++){if(sy.voices[v].range<0||sy.voices[v].second)continue;p_voice=voice_tb[v];st=sy.voices[v].st;s2=p_voice.clef;if(s2.clef_auto){new_type=set_auto_clef(st,s,staff_clef[st].clef?staff_clef[st].clef.clef_type:"a");new_line=new_type=="t"?2:4}else{new_type=s2.clef_type;new_line=s2.clef_line}if(!staff_clef[st].clef){if(s2.clef_auto){if(s2.type!="a")p_voice.clef=clone(p_voice.clef);p_voice.clef.clef_type=new_type;p_voice.clef.clef_line=new_line}staff_tb[st].clef=staff_clef[st].clef=p_voice.clef;continue}if(new_type==staff_clef[st].clef.clef_type&&new_line==staff_clef[st].clef.clef_line)continue;g=s;while(g.v!=v)g=g.ts_next;if(g.type!=CLEF){g=insert_clef(g,new_type,new_line);if(s2.clef_auto)g.clef_auto=true}staff_clef[st].clef=p_voice.clef=g}continue}if(s.type!=CLEF)continue;if(s.clef_type=="a"){s.clef_type=set_auto_clef(s.st,s.ts_next,staff_clef[s.st].clef.clef_type);s.clef_line=s.clef_type=="t"?2:4}p_voice=s.p_v;p_voice.clef=s;if(s.second){unlksym(s);continue}st=s.st;if(staff_clef[st].clef){if(s.clef_type==staff_clef[st].clef.clef_type&&s.clef_line==staff_clef[st].clef.clef_line){continue}}else{staff_tb[st].clef=s}staff_clef[st].clef=s}sy=cur_sy;for(v=0;v<voice_tb.length;v++){if(sy.voices[v].range<0)continue;s2=voice_tb[v].sym;if(!s2||s2.notes[0].pit!=127)continue;st=sy.voices[v].st;switch(staff_tb[st].clef.clef_type){default:pitch=22;break;case"c":pitch=16;break;case"b":pitch=10;break}for(s=s2;s;s=s.next)s.notes[0].pit=pitch}}const delta_tb={t:0-2*2,c:6-3*2,b:12-4*2,p:0-3*2};const rest_sp=[[18,18],[12,18],[12,12],[0,12],[6,8],[10,10],[6,4],[10,0],[10,4],[10,10]];function set_pitch(last_s){var s,s2,g,st,delta,m,pitch,note,dur=BASE_LEN,staff_delta=new Array(nstaff),sy=cur_sy;for(st=0;st<=nstaff;st++){s=staff_tb[st].clef;staff_delta[st]=delta_tb[s.clef_type]+s.clef_line*2;if(cfmt.sound){if(s.clef_octaves&&!s.clef_oct_transp)staff_delta[st]+=s.clef_octave}else{if(s.clef_oct_transp)staff_delta[st]-=s.clef_octave}}for(s=tsfirst;s!=last_s;s=s.ts_next){st=s.st;switch(s.type){case CLEF:staff_delta[st]=delta_tb[s.clef_type]+s.clef_line*2;if(cfmt.sound){if(s.clef_octave&&!s.clef_oct_transp)staff_delta[st]+=s.clef_octave}else{if(s.clef_oct_transp)staff_delta[st]-=s.clef_octave}set_yval(s);break;case GRACE:for(g=s.extra;g;g=g.next){delta=staff_delta[g.st];if(delta!=0&&!s.p_v.key.k_drum){for(m=0;m<=g.nhd;m++){note=g.notes[m];note.pit+=delta}}g.ymn=3*(g.notes[0].pit-18)-2;g.ymx=3*(g.notes[g.nhd].pit-18)+2}set_yval(s);break;case KEY:s.k_y_clef=staff_delta[st];default:set_yval(s);break;case MREST:if(s.invis)break;s.y=12;s.ymx=24+15;s.ymn=-2;break;case REST:if(voice_tb.length==1){s.y=12;s.ymx=24;s.ymn=0;break}case NOTE:delta=staff_delta[st];if(delta!=0&&!s.p_v.key.k_drum){for(m=s.nhd;m>=0;m--)s.notes[m].pit+=delta}if(s.type==NOTE){s.ymx=3*(s.notes[s.nhd].pit-18)+4;s.ymn=3*(s.notes[0].pit-18)-4;s.yav=(s.ymx+s.ymn)/2}else{s.y=((s.notes[0].pit-18)/2|0)*6;s.ymx=s.y+rest_sp[5-s.nflags][0];s.ymn=s.y-rest_sp[5-s.nflags][1]}if(s.dur<dur)dur=s.dur;break}}smallest_duration=dur}function set_stem_dir(){var t,u,i,st,rvoice,v,v_s,st_v,vobj,v_st_tb,st_v_tb=[],s=tsfirst,sy=cur_sy,nst=sy.nstaff;while(s){for(st=0;st<=nst;st++)st_v_tb[st]=[];v_st_tb=[];for(u=s;u;u=u.ts_next){if(u.type==BAR)break;if(u.type==STAVES){if(u!=s)break;sy=sy.next;for(st=nst;st<=sy.nstaff;st++)st_v_tb[st]=[];nst=sy.nstaff;continue}if(u.type!=NOTE&&u.type!=REST||u.invis)continue;st=u.st;if(st>nst){var msg="*** fatal set_stem_dir(): bad staff number "+st+" max "+nst;error(2,null,msg);throw new Error(msg)}v=u.v;v_st=v_st_tb[v];if(!v_st){v_st={st1:-1,st2:-1};v_st_tb[v]=v_st}if(v_st.st1<0){v_st.st1=st}else if(v_st.st1!=st){if(st>v_st.st1){if(st>v_st.st2)v_st.st2=st}else{if(v_st.st1>v_st.st2)v_st.st2=v_st.st1;v_st.st1=st}}st_v=st_v_tb[st];rvoice=sy.voices[v].range;for(i=st_v.length;--i>=0;){vobj=st_v[i];if(vobj.v==rvoice)break}if(i<0){vobj={v:rvoice,ymx:0,ymn:24};for(i=0;i<st_v.length;i++){if(rvoice<st_v[i].v){st_v.splice(i,0,vobj);break}}if(i==st_v.length)st_v.push(vobj)}if(u.type!=NOTE)continue;if(u.ymx>vobj.ymx)vobj.ymx=u.ymx;if(u.ymn<vobj.ymn)vobj.ymn=u.ymn;if(u.xstem){if(u.ts_prev.st!=st-1||u.ts_prev.type!=NOTE){error(1,s,"Bad !xstem!");delete u.xstem}else{u.ts_prev.multi=1;u.multi=1;u.stemless=true}}}for(;s!=u;s=s.ts_next){if(s.multi)continue;if(s.type!=NOTE&&s.type!=REST&&s.type!=GRACE)continue;st=s.st;v=s.v;v_st=v_st_tb[v];st_v=st_v_tb[st];if(v_st&&v_st.st2>=0){if(st==v_st.st1)s.multi=-1;else if(st==v_st.st2)s.multi=1;continue}if(st_v.length<=1){if(s.floating){if(st==voice_tb[v].st)s.multi=-1;else s.multi=1}continue}rvoice=sy.voices[v].range;for(i=st_v.length;--i>=0;){if(st_v[i].v==rvoice)break}if(i<0)continue;if(i==st_v.length-1){s.multi=-1}else{s.multi=1;if(i!=0&&i+2==st_v.length){if(st_v[i].ymn-cfmt.stemheight>st_v[i+1].ymx)s.multi=-1;t=s.ts_next;if(s.ts_prev&&s.ts_prev.time==s.time&&s.ts_prev.st==s.st&&s.notes[s.nhd].pit==s.ts_prev.notes[0].pit&&s.beam_st&&s.beam_end&&(!t||t.st!=s.st||t.time!=s.time))s.multi=-1}}}while(s&&s.type==BAR)s=s.ts_next}}function set_rest_offset(){var s,s2,v,end_time,not_alone,v_s,y,ymax,ymin,shift,dots,dx,v_s_tb=[],sy=cur_sy;for(s=tsfirst;s;s=s.ts_next){if(s.invis)continue;if(s.type==STAVES)sy=sy.next;if(!s.dur)continue;v_s=v_s_tb[s.v];if(!v_s){v_s={};v_s_tb[s.v]=v_s}v_s.s=s;v_s.st=s.st;v_s.end_time=s.time+s.dur;if(s.type!=REST)continue;ymin=-127;ymax=127;not_alone=dots=false;for(v=0;v<=v_s_tb.length;v++){v_s=v_s_tb[v];if(!v_s||!v_s.s||v_s.st!=s.st||v==s.v)continue;if(v_s.end_time<=s.time)continue;not_alone=true;s2=v_s.s;if(sy.voices[v].range<sy.voices[s.v].range){if(s2.time==s.time){if(s2.ymn<ymax){ymax=s2.ymn;if(s2.dots)dots=true}}else{if(s2.y<ymax)ymax=s2.y}}else{if(s2.time==s.time){if(s2.ymx>ymin){ymin=s2.ymx;if(s2.dots)dots=true}}else{if(s2.y>ymin)ymin=s2.y}}}end_time=s.time+s.dur;for(s2=s.ts_next;s2;s2=s2.ts_next){if(s2.time>=end_time)break;if(s2.st!=s.st||!s2.dur||s2.invis)continue;not_alone=true;if(sy.voices[s2.v].range<sy.voices[s.v].range){if(s2.time==s.time){if(s2.ymn<ymax){ymax=s2.ymn;if(s2.dots)dots=true}}else{if(s2.y<ymax)ymax=s2.y}}else{if(s2.time==s.time){if(s2.ymx>ymin){ymin=s2.ymx;if(s2.dots)dots=true}}else{if(s2.y>ymin)ymin=s2.y}}}if(!not_alone){s.y=12;s.ymx=24;s.ymn=0;continue}if(ymax==127&&s.y<12){shift=12-s.y;s.y+=shift;s.ymx+=shift;s.ymn+=shift}if(ymin==-127&&s.y>12){shift=s.y-12;s.y-=shift;s.ymx-=shift;s.ymn-=shift}shift=ymax-s.ymx;if(shift<0){shift=Math.ceil(-shift/6)*6;if(s.ymn-shift>=ymin){s.y-=shift;s.ymx-=shift;s.ymn-=shift;continue}dx=dots?15:10;s.notes[0].shhd=dx;s.xmx=dx;continue}shift=ymin-s.ymn;if(shift>0){shift=Math.ceil(shift/6)*6;if(s.ymx+shift<=ymax){s.y+=shift;s.ymx+=shift;s.ymn+=shift;continue}dx=dots?15:10;s.notes[0].shhd=dx;s.xmx=dx;continue}}}function new_sym(type,p_voice,last_s){var s={type:type,ctx:last_s.ctx,v:p_voice.v,p_v:p_voice,st:p_voice.st,time:last_s.time,next:p_voice.last_sym.next};if(s.next)s.next.prev=s;p_voice.last_sym.next=s;s.prev=p_voice.last_sym;p_voice.last_sym=s;s.ts_next=last_s;s.ts_prev=last_s.ts_prev;s.ts_prev.ts_next=s;if(s.ts_prev.type!=type)s.seqst=true;last_s.ts_prev=s;if(last_s.type==type&&s.v!=last_s.v){delete last_s.seqst;last_s.shrink=0}return s}function init_music_line(){var p_voice,s,s2,last_s,v,st,nv=voice_tb.length;for(v=0;v<nv;v++){if(cur_sy.voices[v].range<0)continue;
  p_voice=voice_tb[v];p_voice.second=cur_sy.voices[v].second;st=cur_sy.voices[v].st;while(st<nstaff&&cur_sy.staves[st].empty)st++;p_voice.st=st}last_s=tsfirst;while(last_s.type==CLEF){v=last_s.v;if(cur_sy.voices[v].range>=0&&!cur_sy.voices[v].second){delete last_s.clef_small;p_voice=last_s.p_v;p_voice.last_sym=p_voice.sym=last_s}last_s=last_s.ts_next}for(v=0;v<nv;v++){p_voice=voice_tb[v];if(p_voice.sym&&p_voice.sym.type==CLEF)continue;if(cur_sy.voices[v].range<0||cur_sy.voices[v].second&&!p_voice.bar_start)continue;st=cur_sy.voices[v].st;if(!staff_tb[st]||!staff_tb[st].clef)continue;s=clone(staff_tb[st].clef);s.v=v;s.p_v=p_voice;s.st=st;s.time=last_s.time;s.prev=null;s.next=p_voice.sym;if(s.next)s.next.prev=s;p_voice.sym=s;p_voice.last_sym=s;s.ts_next=last_s;s.ts_prev=last_s.ts_prev;if(!s.ts_prev){tsfirst=s;s.seqst=true}else{s.ts_prev.ts_next=s;delete s.seqst}last_s.ts_prev=s;if(last_s.type==CLEF)delete last_s.seqst;delete s.clef_small;s.second=cur_sy.voices[v].second;if(cur_sy.staves[st].empty)s.invis=true}for(v=0;v<nv;v++){if(cur_sy.voices[v].range<0||cur_sy.voices[v].second||cur_sy.staves[cur_sy.voices[v].st].empty)continue;p_voice=voice_tb[v];if(last_s.v==v&&last_s.type==KEY){p_voice.last_sym=last_s;last_s=last_s.ts_next;continue}s2=p_voice.key;if(s2.k_sf||s2.k_a_acc){s=new_sym(KEY,p_voice,last_s);s.k_sf=s2.k_sf;s.k_old_sf=s2.k_sf;s.k_none=s2.k_none;s.k_a_acc=s2.k_a_acc;s.istart=s2.istart;s.iend=s2.iend;if(s2.k_bagpipe){s.k_bagpipe=s2.k_bagpipe;if(s.k_bagpipe=="p")s.k_old_sf=3}}}if(insert_meter&1){for(v=0;v<nv;v++){p_voice=voice_tb[v];s2=p_voice.meter;if(cur_sy.voices[v].range<0||cur_sy.voices[v].second||cur_sy.staves[cur_sy.voices[v].st].empty||s2.a_meter.length==0)continue;if(last_s.v==v&&last_s.type==METER){p_voice.last_sym=last_s;last_s=last_s.ts_next;continue}s=new_sym(METER,p_voice,last_s);s.istart=s2.istart;s.iend=s2.iend;s.wmeasure=s2.wmeasure;s.a_meter=s2.a_meter;insert_meter&=~1}}for(v=0;v<nv;v++){p_voice=voice_tb[v];s2=p_voice.bar_start;if(!s2)continue;p_voice.bar_start=null;if(cur_sy.voices[v].range<0||cur_sy.staves[cur_sy.voices[v].st].empty)continue;if(last_s.v==v&&last_s.type==BAR){p_voice.last_sym=last_s;last_s=last_s.ts_next;continue}s=new_sym(BAR,p_voice,last_s);s.istart=s2.istart;s.iend=s2.iend;s.bar_type=s2.bar_type;if(s2.invis)s.invis=true;if(s2.norepbra)s.norepbra=true;s.text=s2.text;s.a_gch=s2.a_gch;if(s2.rbstart)s.rbstart=s2.rbstart}set_pitch(last_s);s=last_s;if(s){for(;s;s=s.ts_next)if(s.seqst)break}if(s){for(s=s.ts_next;s;s=s.ts_next)if(s.seqst)break}set_allsymwidth(s)}function set_words(p_voice){var s,s2,nflags,lastnote,start_flag=true,pitch=127;for(s=p_voice.sym;s;s=s.next){if(s.type==NOTE){pitch=s.notes[0].pit;break}}for(s=p_voice.sym;s;s=s.next){switch(s.type){case MREST:start_flag=true;break;case BAR:if(!s.beam_on)start_flag=true;if(!s.next&&s.prev&&s.prev.head==OVALBARS)s.prev.head=SQUARE;break;case NOTE:case REST:if(s.trem2)break;nflags=s.nflags;if(s.ntrem)nflags+=s.ntrem;if(s.type==REST&&s.beam_end){s.beam_end=false;start_flag=true}if(start_flag||nflags<=0){if(lastnote){lastnote.beam_end=true;lastnote=null}if(nflags<=0){s.beam_st=true;s.beam_end=true}else if(s.type==NOTE){s.beam_st=true;start_flag=false}}if(s.beam_end)start_flag=true;if(s.type==NOTE)lastnote=s;break}if(s.type==NOTE){if(s.nhd!=0)sort_pitch(s);pitch=s.notes[0].pit;for(s2=s.prev;s2;s2=s2.prev){if(s2.type!=REST)break;s2.notes[0].pit=pitch}}else{if(!s.notes){s.notes=[];s.notes[0]={};s.nhd=0}s.notes[0].pit=pitch}}if(lastnote)lastnote.beam_end=true}function set_rb(p_voice){var s2,mx,n,s=p_voice.sym;while(s){if(s.type!=BAR||!s.rbstart||s.norepbra){s=s.next;continue}mx=cfmt.rbmax;if(s.text&&s.text[0]=="1"){n=0;s2=null;for(s=s.next;s;s=s.next){if(s.type!=BAR)continue;n++;if(s.rbstop){if(n<=cfmt.rbmax){mx=n;s2=null}break}if(n==cfmt.rbmin)s2=s}if(s2){s2.rbstop=1;mx=cfmt.rbmin}}while(s){if(s.rbstart!=2){s=s.next;if(!s)break;if(s.rbstart!=2){s=s.next;if(!s)break;if(s.rbstart!=2)break}}n=0;s2=null;for(s=s.next;s;s=s.next){if(s.type!=BAR)continue;n++;if(s.rbstop)break;if(!s.next)s.rbstop=2;else if(n==mx)s.rbstop=1}}}}const delpit=[0,-7,-14,0];function set_global(){var p_voice,st,v,nv,sy;sy=cur_sy;st=sy.nstaff;while(1){sy=sy.next;if(!sy)break;if(sy.nstaff>st)st=sy.nstaff}nstaff=st;nv=voice_tb.length;for(v=0;v<nv;v++){p_voice=voice_tb[v];set_words(p_voice);set_rb(p_voice)}set_float();set_clefs();set_pitch(null)}function set_indent(){var st,v,w,p_voice,p,i,j,font,nv=voice_tb.length,maxw=0;for(v=0;v<nv;v++){p_voice=voice_tb[v];if(cur_sy.voices[v].range<0)continue;st=cur_sy.voices[v].st;if(cur_sy.staves[st].empty)continue;p=p_voice.new_name?p_voice.nm:p_voice.snm;if(!p)continue;if(!font){font=get_font("voice");gene.curfont=gene.deffont=font}i=0;while(1){j=p.indexOf("\\n",i);if(j<0)w=strw(p.slice(i));else w=strw(p.slice(i,j));if(w>maxw)maxw=w;if(j<0)break;i=j+1}}if(maxw!=0){w=0;for(st=0;st<=cur_sy.nstaff;st++){if(cur_sy.staves[st].flags&(OPEN_BRACE2|OPEN_BRACKET2)){w=20;break}if(cur_sy.staves[st].flags&(OPEN_BRACE|OPEN_BRACKET)&&w==0)w=10}maxw+=4*cwid(" ")*font.swfac+w}if(insert_meter&2)maxw+=cfmt.indent;return maxw}function set_beams(sym){var s,t,g,beam,s_opp,dy,avg,n,laststem=-1,lasty=0;for(s=sym;s;s=s.next){if(s.type!=NOTE){if(s.type!=GRACE)continue;g=s.extra;if(g.stem==2){s_opp=s;continue}if(!s.stem&&(s.stem=s.multi)==0)s.stem=1;for(;g;g=g.next){g.stem=s.stem;g.multi=s.multi}continue}if(!s.stem&&(s.stem=s.multi)==0){if(beam){s.stem=laststem}else if(s.beam_st&&!s.beam_end){avg=s.yav;n=12;for(t=s.next;t;t=t.next){if(t.type==NOTE){if(t.multi){avg=n-t.multi;break}avg+=t.yav;n+=12}if(t.beam_end)break}if(avg<n)laststem=1;else if(avg>n||cfmt.bstemdown)laststem=-1;beam=true;s.stem=laststem}else{s.stem=s.yav>=12?-1:1;if(s.yav==12&&!cfmt.bstemdown){if(!s.prev||s.prev.type==BAR){for(t=s.next;t;t=t.next){if(t.type==NOTE||t.type==BAR)break}if(t&&t.type==NOTE&&t.yav<12)s.stem=1}else{dy=s.yav-lasty;if(dy>-7&&dy<7)s.stem=laststem}}}}else{if(s.beam_st&&!s.beam_end)beam=true}if(s.beam_end)beam=false;laststem=s.stem;lasty=s.yav;if(s_opp){for(g=s_opp.extra;g;g=g.next)g.stem=-laststem;s_opp.stem=-laststem;s_opp=null}}}function same_head(s1,s2){var i1,i2,l1,l2,head,i11,i12,i21,i22,sh1,sh2;if(s1.shiftunison&&s1.shiftunison>=3)return false;if((l1=s1.dur)>=BASE_LEN)return false;if((l2=s2.dur)>=BASE_LEN)return false;if(s1.stemless&&s2.stemless)return false;if(s1.dots!=s2.dots){if(s1.shiftunison&&s1.shiftunison&1||s1.dots*s2.dots!=0)return false}if(s1.stem*s2.stem>0)return false;i1=i2=0;if(s1.notes[0].pit>s2.notes[0].pit){if(s1.stem<0)return false;while(s2.notes[i2].pit!=s1.notes[0].pit){if(++i2>s2.nhd)return false}}else if(s1.notes[0].pit<s2.notes[0].pit){if(s2.stem<0)return false;while(s2.notes[0].pit!=s1.notes[i1].pit){if(++i1>s1.nhd)return false}}if(s2.notes[i2].acc!=s1.notes[i1].acc)return false;i11=i1;i21=i2;sh1=s1.notes[i1].shhd;sh2=s2.notes[i2].shhd;do{i1++;i2++;if(i1>s1.nhd){break}if(i2>s2.nhd){break}if(s2.notes[i2].acc!=s1.notes[i1].acc)return false;if(sh1<s1.notes[i1].shhd)sh1=s1.notes[i1].shhd;if(sh2<s2.notes[i2].shhd)sh2=s2.notes[i2].shhd}while(s2.notes[i2].pit==s1.notes[i1].pit);if(i1<=s1.nhd){if(i2<=s2.nhd)return false;if(s2.stem>0)return false}else if(i2<=s2.nhd){if(s1.stem>0)return false}i12=i1;i22=i2;head=0;if(l1!=l2){if(l1<l2){l1=l2;l2=s1.dur}if(l1<BASE_LEN/2){if(s2.dots>0)head=2;else if(s1.dots>0)head=1}else if(l2<BASE_LEN/4){if(s1.shiftunison&&s1.shiftunison&2)return false;head=s2.dur>=BASE_LEN/2?2:1}else{return false}}if(head==0)head=s1.p_v.scale<s2.p_v.scale?2:1;if(head==1){for(i2=i21;i2<i22;i2++){s2.notes[i2].invis=true;delete s2.notes[i2].acc}for(i2=0;i2<=s2.nhd;i2++)s2.notes[i2].shhd+=sh1}else{for(i1=i11;i1<i12;i1++){s1.notes[i1].invis=true;delete s1.notes[i1].acc}for(i1=0;i1<=s1.nhd;i1++)s1.notes[i1].shhd+=sh2}return true}const w_note=[3.5,3.7,5,6,7];function unison_acc(s1,s2,i1,i2){var m,d;if(!s2.notes[i2].acc){d=w_note[s2.head]*2+s2.xmx+s1.notes[i1].shac+2;if(s1.notes[i1].micro)d+=2;if(s2.dots)d+=6;for(m=0;m<=s1.nhd;m++){s1.notes[m].shhd+=d;s1.notes[m].shac-=d}s1.xmx+=d}else{d=w_note[s1.head]*2+s1.xmx+s2.notes[i2].shac+2;if(s2.notes[i2].micro)d+=2;if(s1.dots)d+=6;for(m=0;m<=s2.nhd;m++){s2.notes[m].shhd+=d;s2.notes[m].shac-=d}s2.xmx+=d}}const MAXPIT=48*2;function set_left(s){var m,i,j,shift,w_base=w_note[s.head],w=w_base,left=[];for(i=0;i<MAXPIT;i++)left.push(-100);if(s.nflags>-2){if(s.stem>0){w=-w;i=s.notes[0].pit*2;j=(Math.ceil((s.ymx-2)/3)+18)*2}else{i=(Math.ceil((s.ymn+2)/3)+18)*2;j=s.notes[s.nhd].pit*2}if(i<0)i=0;if(j>=MAXPIT)j=MAXPIT-1;while(i<=j)left[i++]=w}shift=s.notes[s.stem>0?0:s.nhd].shhd;for(m=0;m<=s.nhd;m++){w=-s.notes[m].shhd+w_base+shift;i=s.notes[m].pit*2;if(i<0)i=0;else if(i>=MAXPIT-1)i=MAXPIT-2;if(w>left[i])left[i]=w;if(s.head!=SQUARE)w-=1;if(w>left[i-1])left[i-1]=w;if(w>left[i+1])left[i+1]=w}return left}function set_right(s){var m,i,j,k,shift,w_base=w_note[s.head],w=w_base,flags=s.nflags>0&&s.beam_st&&s.beam_end,right=[];for(i=0;i<MAXPIT;i++)right.push(-100);if(s.nflags>-2){if(s.stem<0){w=-w;i=(Math.ceil((s.ymn+2)/3)+18)*2;j=s.notes[s.nhd].pit*2;k=i+4}else{i=s.notes[0].pit*2;j=(Math.ceil((s.ymx-2)/3)+18)*2}if(i<0)i=0;if(j>MAXPIT)j=MAXPIT;while(i<j)right[i++]=w}if(flags){if(s.stem>0){if(s.xmx==0)i=s.notes[s.nhd].pit*2;else i=s.notes[0].pit*2;i+=4;if(i<0)i=0;for(;i<MAXPIT&&i<=j-4;i++)right[i]=11}else{i=k;if(i<0)i=0;for(;i<MAXPIT&&i<=s.notes[0].pit*2-4;i++)right[i]=3.5}}shift=s.notes[s.stem>0?0:s.nhd].shhd;for(m=0;m<=s.nhd;m++){w=s.notes[m].shhd+w_base-shift;i=s.notes[m].pit*2;if(i<0)i=0;else if(i>=MAXPIT-1)i=MAXPIT-2;if(w>right[i])right[i]=w;if(s.head!=SQUARE)w-=1;if(w>right[i-1])right[i-1]=w;if(w>right[i+1])right[i+1]=w}return right}function set_overlap(){var s,s1,s2,s3,i,i1,i2,m,sd,t,dp,d,d2,dr,dr2,dx,left1,right1,left2,right2,right3,pl,pr;function v_invert(){s1=s2;s2=s;d=d2;pl=left1;pr=right1;dr2=dr}for(s=tsfirst;s;s=s.ts_next){if(s.type!=NOTE||s.invis)continue;if(s.xstem&&s.ts_prev.stem<0){s2=s.ts_prev;for(m=0;m<=s2.nhd;m++){s2.notes[m].shhd+=3.5*2;s2.notes[m].shac-=3.5*2}s2.xmx+=3.5*2}s2=s;while(1){s2=s2.ts_next;if(!s2)break;if(s2.time!=s.time){s2=null;break}if(s2.type==NOTE&&!s2.invis&&s2.st==s.st)break}if(!s2)continue;s1=s;if(cur_sy.voices[s1.v].range<cur_sy.voices[s2.v].range)s2.dot_low=true;else s1.dot_low=true;if(s1.ymn>s2.ymx||s1.ymx<s2.ymn)continue;if(same_head(s1,s2))continue;right1=set_right(s1);left2=set_left(s2);s3=s1.ts_prev;if(s3&&s3.time==s1.time&&s3.st==s1.st&&s3.type==NOTE&&!s3.invis){right3=set_right(s3);for(i=0;i<MAXPIT;i++){if(right3[i]>right1[i])right1[i]=right3[i]}}else{s3=null}d=-10;for(i=0;i<MAXPIT;i++){if(left2[i]+right1[i]>d)d=left2[i]+right1[i]}if(d<-3){if(!s1.dots||!s2.dots||!s2.dot_low||s1.stem>0||s2.stem<0||s1.notes[s1.nhd].pit+2!=s2.notes[0].pit||s2.notes[0].pit&1)continue}right2=set_right(s2);left1=set_left(s1);if(s3){right3=set_left(s3);for(i=0;i<MAXPIT;i++){if(right3[i]>left1[i])left1[i]=right3[i]}}d2=dr=dr2=-100;for(i=0;i<MAXPIT;i++){if(left1[i]+right2[i]>d2)d2=left1[i]+right2[i];if(right2[i]>dr2)dr2=right2[i];if(right1[i]>dr)dr=right1[i]}t=0;i1=s1.nhd;i2=s2.nhd;while(1){dp=s1.notes[i1].pit-s2.notes[i2].pit;switch(dp){case 0:if(s1.notes[i1].acc!=s2.notes[i2].acc){t=-1;break}if(s2.notes[i2].acc)s2.notes[i2].acc=0;if(s1.dots&&s2.dots&&s1.notes[i1].pit&1)t=1;break;case-1:if(s1.dots&&s2.dots){if(s1.notes[i1].pit&1){s1.dot_low=false;s2.dot_low=false}else{s1.dot_low=true;s2.dot_low=true}}break;case-2:if(s1.dots&&s2.dots&&!(s1.notes[i1].pit&1)){s1.dot_low=false;s2.dot_low=false;break}break}if(t<0)break;if(dp>=0){if(--i1<0)break}if(dp<=0){if(--i2<0)break}}if(t<0){unison_acc(s1,s2,i1,i2);continue}sd=0;if(s1.dots){if(s2.dots){if(!t)sd=1}}else if(s2.dots){if(d2+dr<d+dr2)sd=1}pl=left2;pr=right2;if(!s3&&d2+dr<d+dr2)v_invert();d+=3;if(d<0)d=0;m=s1.stem>=0?0:s1.nhd;d+=s1.notes[m].shhd;m=s2.stem>=0?0:s2.nhd;d-=s2.notes[m].shhd;if(s1.dots){dx=7.7+s1.xmx+3.5*s1.dots-3.5+3;if(!sd){d2=-100;for(i1=0;i1<=s1.nhd;i1++){i=s1.notes[i1].pit;if(!(i&1)){if(!s1.dot_low)i++;else i--}i*=2;if(i<1)i=1;else if(i>=MAXPIT-1)i=MAXPIT-2;if(pl[i]>d2)d2=pl[i];if(pl[i-1]+1>d2)d2=pl[i-1]+1;if(pl[i+1]+1>d2)d2=pl[i+1]+1}if(dx+d2+2>d)d=dx+d2+2}else{if(dx<d+dr2+s2.xmx){d2=0;for(i1=0;i1<=s1.nhd;i1++){i=s1.notes[i1].pit;if(!(i&1)){if(!s1.dot_low)i++;else i--}i*=2;if(i<1)i=1;else if(i>=MAXPIT-1)i=MAXPIT-2;if(pr[i]>d2)d2=pr[i];if(pr[i-1]+1>d2)d2=pr[i-1]=1;if(pr[i+1]+1>d2)d2=pr[i+1]+1}if(d2>4.5&&7.7+s1.xmx+2<d+d2+s2.xmx)s2.xmx=d2+3-7.7}}}for(m=s2.nhd;m>=0;m--){s2.notes[m].shhd+=d}s2.xmx+=d;if(sd)s1.xmx=s2.xmx}}function set_stems(){var s,s2,g,slen,scale,ymn,ymx,nflags,ymin,ymax;for(s=tsfirst;s;s=s.ts_next){if(s.type!=NOTE){if(s.type!=GRACE)continue;ymin=ymax=12;for(g=s.extra;g;g=g.next){slen=GSTEM;if(g.nflags>1)slen+=1.2*(g.nflags-1);ymn=3*(g.notes[0].pit-18);ymx=3*(g.notes[g.nhd].pit-18);if(s.stem>=0){g.y=ymn;g.ys=ymx+slen;ymx=Math.round(g.ys)}else{g.y=ymx;g.ys=ymn-slen;ymn=Math.round(g.ys)}ymx+=2;ymn-=2;if(ymn<ymin)ymin=ymn;else if(ymx>ymax)ymax=ymx;g.ymx=ymx;g.ymn=ymn}s.ymx=ymax;s.ymn=ymin;continue}set_head_shift(s);nflags=s.nflags;if(s.beam_st&&!s.beam_end){if(s.feathered_beam)nflags=++s.nflags;for(s2=s.next;;s2=s2.next){if(s2.type==NOTE){if(s.feathered_beam)s2.nflags++;if(s2.beam_end)break}}if(s2.nflags>nflags)nflags=s2.nflags}else if(!s.beam_st&&s.beam_end){for(s2=s.prev;;s2=s2.prev){if(s2.beam_st)break}if(s2.nflags>nflags)nflags=s2.nflags}slen=cfmt.stemheight;switch(nflags){case 2:slen+=2;break;case 3:slen+=5;break;case 4:slen+=10;break;case 5:slen+=16;break}if((scale=s.p_v.scale)!=1)slen*=(scale+1)*.5;ymn=3*(s.notes[0].pit-18);if(s.nhd>0){slen-=2;ymx=3*(s.notes[s.nhd].pit-18)}else{ymx=ymn}if(s.ntrem)slen+=2*s.ntrem;if(s.stemless){if(s.stem>=0){s.y=ymn;s.ys=ymx}else{s.ys=ymn;s.y=ymx}if(nflags==-4)ymn-=6;s.ymx=ymx+4;s.ymn=ymn-4}else if(s.stem>=0){if(nflags>=2)slen-=1;if(s.notes[s.nhd].pit>26&&(nflags<=0||!s.beam_st||!s.beam_end)){slen-=2;if(s.notes[s.nhd].pit>28)slen-=2}s.y=ymn;if(s.notes[0].ti1)ymn-=3;s.ymn=ymn-4;s.ys=ymx+slen;if(s.ys<12)s.ys=12;s.ymx=s.ys+2.5|0}else{if(s.notes[0].pit<18&&(nflags<=0||!s.beam_st||!s.beam_end)){slen-=2;if(s.notes[0].pit<16)slen-=2}s.ys=ymn-slen;if(s.ys>12)s.ys=12;s.ymn=s.ys-2.5|0;s.y=ymx;if(s.notes[s.nhd].ti1)ymx+=3;s.ymx=ymx+4}}}function check_bar(s){var bar_type,i,b1,b2,p_voice=s.p_v;while(s.type==CLEF||s.type==KEY||s.type==METER){if(s.type==METER&&s.time>p_voice.sym.time)insert_meter|=1;s=s.prev;if(!s)return}if(s.type!=BAR)return;if(s.text!=undefined){p_voice.bar_start=clone(s);p_voice.bar_start.bar_type="[";delete s.text;delete s.a_gch}bar_type=s.bar_type;if(bar_type==":")return;if(bar_type.slice(-1)!=":")return;if(!p_voice.bar_start)p_voice.bar_start=clone(s);if(bar_type[0]!=":"){if(bar_type=="||:"){p_voice.bar_start.bar_type="|:";s.bar_type="||";return}p_voice.bar_start.bar_type=bar_type;if(s.prev&&s.prev.type==BAR)unlksym(s);else s.bar_type="|";return}if(bar_type=="::"){p_voice.bar_start.bar_type="|:";s.bar_type=":|";return}if(bar_type=="||:"){p_voice.bar_start.bar_type="|:";s.bar_type="||";return}i=0;while(bar_type[i]==":")i++;if(i<bar_type.length){s.bar_type=bar_type.slice(0,i)+"|";i=bar_type.length-1;while(bar_type[i]==":")i--;p_voice.bar_start.bar_type="|"+bar_type.slice(i+1)}else{i=bar_type.length/2|0;s.bar_type=bar_type.slice(0,i)+"|";p_voice.bar_start.bar_type="|"+bar_type.slice(i)}}function sym_staff_move(st){var s;for(s=tsfirst;s;s=s.ts_next){if(s.nl)break;if(s.st==st&&s.type!=CLEF){s.st++;s.invis=true}}}function set_piece(){var s,p_voice,st,v,nst,nv,non_empty,non_empty_gl=[],sy=cur_sy;function reset_staff(st){var p_staff=staff_tb[st],sy_staff=sy.staves[st];if(!p_staff)p_staff=staff_tb[st]={};p_staff.y=0;p_staff.stafflines=sy_staff.stafflines;p_staff.staffscale=sy_staff.staffscale;p_staff.ann_top=p_staff.ann_bot=0}function set_brace(){var st,i,empty_fl,n=sy.staves.length;for(st=0;st<n;st++){if(!(sy.staves[st].flags&(OPEN_BRACE|OPEN_BRACE2)))continue;empty_fl=0;i=st;while(st<n){empty_fl|=non_empty[st]?1:2;if(sy.staves[st].flags&(CLOSE_BRACE|CLOSE_BRACE2))break;st++}if(empty_fl==3){while(i<=st){non_empty[i]=true;non_empty_gl[i++]=true}}}}function set_top_bot(){var st,p_staff,i,l,hole;for(st=0;st<=nstaff;st++){p_staff=staff_tb[st];if(!non_empty_gl[st]){p_staff.botbar=p_staff.topbar=0;continue}l=p_staff.stafflines.length;p_staff.topbar=6*(l-1);for(i=0;i<l-1;i++)if(p_staff.stafflines[i]!=".")break;p_staff.botline=p_staff.botbar=i*6;if(i>=l-2){p_staff.botbar-=6;p_staff.topbar+=6}}}nstaff=nst=sy.nstaff;for(st=0;st<=nst;st++)reset_staff(st);non_empty=[];for(s=tsfirst;s;s=s.ts_next){if(s.nl)break;if(s.type==STAVES){set_brace();for(st=0;st<=nstaff;st++)sy.staves[st].empty=!non_empty[st];for(;st<=nst;st++)sy.staves[st].empty=true;sy=sy.next;nst=sy.nstaff;if(nst>nstaff)nstaff=nst;for(;st<=nst;st++)reset_staff(st);non_empty=[];continue}st=s.st;if(non_empty[st])continue;switch(s.type){case CLEF:if(st>nstaff){staff_tb[s.st].clef=s;unlksym(s)}break;case GRACE:non_empty_gl[st]=non_empty[st]=true;break;case NOTE:case REST:case SPACE:case MREST:if(cfmt.staffnonote>1){non_empty_gl[st]=non_empty[st]=true}else if(!s.invis){if(cfmt.staffnonote!=0||s.type==NOTE)non_empty_gl[st]=non_empty[st]=true}break}}tsnext=s;set_brace();for(st=0;st<=nstaff;st++)sy.staves[st].empty=!non_empty[st];set_top_bot();for(st=0;st<nstaff;st++){if(!non_empty_gl[st])sym_staff_move(st)}if(!non_empty_gl[nstaff])staff_tb[nstaff].topbar=0;init_music_line();if(!tsnext)return;s=tsnext;s.nl=false;s=s.ts_prev;s.ts_next=null;nv=voice_tb.length;for(v=0;v<nv;v++){p_voice=voice_tb[v];if(p_voice.sym&&p_voice.sym.time<=tsnext.time){for(s=tsnext.ts_prev;s;s=s.ts_prev){if(s.v==v){p_voice.s_next=s.next;s.next=null;check_bar(s);break}}if(s)continue}p_voice.s_next=p_voice.sym;p_voice.sym=null}}function set_sym_glue(width){var space,beta0,alfa,beta,min,g,spafac,xmax;var some_grace,s=tsfirst,xmin=0,x=0;while(1){if(s.type==GRACE)some_grace=true;if(s.seqst){space=s.space;xmin+=s.shrink;if(space<s.shrink)space=s.shrink;x+=space}if(!s.ts_next)break;s=s.ts_next}if(x==0){realwidth=0;return}xmax=x;if(cfmt.stretchstaff)xmax*=1.8;beta0=1;if(tsnext){if(x>=width){beta_last=0}else{beta_last=(width-x)/(xmax-x);if(beta_last>beta0){if(cfmt.stretchstaff){if(cfmt.linewarn){error(0,s,"Line underfull ($1pt of $2pt)",(beta0*xmax+(1-beta0)*x).toFixed(2),width.toFixed(2))}}else{width=x;beta_last=0}}}}else{if(x<width){beta=(width-x)/(xmax-x);if(beta>=beta_last){beta=beta_last*xmax+(1-beta_last)*x;if(beta<width*(1-cfmt.stretchlast))width=beta}}}spafac=width/x;x=xmax=0;s=tsfirst;while(1){if(s.seqst){space=s.shrink;if(s.space!=0){if(space<s.space*spafac)space=s.space*spafac;xmax+=s.space*spafac*1.8}x+=space;xmax+=space;s.x=x;s.xmax=xmax}if(!s.ts_next)break;s=s.ts_next}switch(s.type){case BAR:case SPACE:break;case CUSTOS:x+=s.wr;xmin+=s.wr;xmax+=s.wr;break;default:min=s.wr;while(!s.seqst){s=s.ts_prev;if(s.wr>min)min=s.wr}xmin+=min+3;if(tsnext&&tsnext.space*.8>s.wr+4){x+=tsnext.space*.8*spafac;xmax+=tsnext.space*.8*spafac*1.8}else{x+=min+4;xmax+=min+4}break}if(x>=width){beta=0;if(x==xmin){alfa=1}else{alfa=(x-width)/(x-xmin);if(alfa>1){error(0,s,"Line too much shrunk $1 $2 $3",xmin.toFixed(2),x.toFixed(2),width.toFixed(2))}}realwidth=xmin*alfa+x*(1-alfa)}else{alfa=0;if(xmax>x)beta=(width-x)/(xmax-x);else beta=1;if(beta>beta0){if(!cfmt.stretchstaff)beta=0}realwidth=xmax*beta+x*(1-beta)}s=tsfirst;if(alfa!=0){if(alfa<1){x=xmin=0;for(;s;s=s.ts_next){if(s.seqst){xmin+=s.shrink*alfa;x=xmin+s.x*(1-alfa)}s.x=x}}else{alfa=realwidth/x;x=0;for(;s;s=s.ts_next){if(s.seqst)x=s.x*alfa;s.x=x}}}else{x=0;for(;s;s=s.ts_next){if(s.seqst)x=s.xmax*beta+s.x*(1-beta);s.x=x}}if(some_grace){for(s=tsfirst;s;s=s.ts_next){if(s.type!=GRACE)continue;if(s.gr_shift)x=s.prev.x+s.prev.wr+Number(cfmt.gracespace[0]);else x=s.x-s.wl+Number(cfmt.gracespace[0]);for(g=s.extra;g;g=g.next)g.x+=x}}}function set_sym_line(){var p_voice,s,v,nv=voice_tb.length;for(v=0;v<nv;v++){p_voice=voice_tb[v];s=p_voice.s_next;p_voice.sym=s;if(s)s.prev=null}}function gen_init(page_chg){var s;function set_page(){if(!page_chg)return;page_chg=false;if(cfmt.pagewidth-cfmt.leftmargin-cfmt.rightmargin<100){error(0,undefined,"Bad staff width");cfmt.pagewidth=cfmt.leftmargin+cfmt.rightmargin+200}posx=cfmt.leftmargin/cfmt.scale}for(s=tsfirst;s;s=s.ts_next){switch(s.type){case CLEF:case KEY:case METER:continue;case STAVES:unlksym(s);cur_sy=cur_sy.next;continue;case BLOCK:switch(s.subtype){case"center":set_page();write_text(s.text,"c");break;case"leftmargin":case"rightmargin":case"pagescale":case"pagewidth":case"scale":set_format(s.subtype,s.param);page_chg=true;break;case"ml":svg_flush();user.img_out(s.text);break;case"newpage":block.newpage=true;break;case"sep":set_page();vskip(s.sk1);output.push('<path class="stroke"\n\td="M');out_sxsy(s.x," ",0);output.push("h"+s.l.toFixed(2)+'"/>\n');vskip(s.sk2);blk_out();break;case"text":set_page();write_text(s.text,s.opt);break;case"title":set_page();write_title(s.text,true);break;case"vskip":vskip(s.sk);blk_out();break;default:error(2,s,"Block $1 not treated",s.subtype);break}unlksym(s);continue}set_page();return}tsfirst=null}function output_music(){var output_sav,v,lwidth,indent,line_height;gen_init(true);if(!tsfirst)return;set_global();if(voice_tb.length>1){combine_voices();set_stem_dir()}for(v=0;v<voice_tb.length;v++)set_beams(voice_tb[v].sym);set_stems();if(voice_tb.length>1){set_rest_offset();set_overlap()}set_acc_shft();set_allsymwidth(null);indent=set_indent();if(cfmt.singleline){lwidth=get_ck_width()+get_width(tsfirst,null,indent);cfmt.pagewidth=(lwidth+cfmt.leftmargin+cfmt.rightmargin)*cfmt.scale}else{lwidth=(cfmt.pagewidth-cfmt.leftmargin-cfmt.rightmargin)/cfmt.scale;cut_tune(lwidth,indent)}beta_last=0;while(1){set_piece();indent=set_indent();set_sym_glue(lwidth-indent);if(realwidth!=0){if(indent!=0)posx+=indent;output_sav=output;output=undefined;draw_sym_near();output=output_sav;line_height=set_staff();delayed_update();draw_systems(indent);draw_all_sym();vskip(line_height);if(indent!=0){posx-=indent;insert_meter&=~2}}tsfirst=tsnext;if(!tsnext)break;gen_init();if(!tsfirst)break;blk_out();tsfirst.ts_prev=null;set_sym_line()}}function reset_gen(){insert_meter=cfmt.writefields.indexOf("M")>=0?3:2}var a_gch,a_dcn,multicol,maps={};const not_ascii="Not an ASCII character";function new_clef(clef_def){var s={type:CLEF,ctx:parse.ctx,istart:parse.istart,iend:parse.iend,clef_line:2,clef_type:"t",v:curvoice.v,p_v:curvoice,time:curvoice.time,dur:0},i=1;switch(clef_def[0]){case'"':i=clef_def.indexOf('"');s.clef_name=clef_def.slice(1,i);i++;break;case"a":if(clef_def[1]=="u"){s.clef_type="a";s.clef_auto=true;i=4;break}i=4;case"C":s.clef_type="c";s.clef_line=3;break;case"b":i=4;case"F":s.clef_type="b";s.clef_line=4;break;case"n":i=4;s.invis=true;break;case"t":if(clef_def[1]=="e"){s.clef_type="c";s.clef_line=4;break}i=6;case"G":break;case"p":i=4;case"P":s.clef_type="p";s.clef_line=3;break;default:parse.line.error("Unknown clef '$1'",clef_def);delete s;return}if(clef_def[i]>="1"&&clef_def[i]<="9"){s.clef_line=Number(clef_def[i]);i++}if(clef_def[i+1]!="8")return s;switch(clef_def[i]){case"^":s.clef_oct_transp=true;case"+":s.clef_octave=7;break;case"_":s.clef_oct_transp=true;case"-":s.clef_octave=-7;break}return s}const pit_st=[0,2,4,5,7,9,11];function get_transp(param,type){var val,tmp,note,pit=[];if(param[0]=="0")return 0;if("123456789-+".indexOf(param[0])>=0){val=parseInt(param)*3;if(isNaN(val)||val<-108||val>108){parse.line.error(1,"Bad transpose value");return}switch(param.slice(-1)){default:return val;case"#":val++;break;case"b":val+=2;break}if(val>0)return val;return val-3}if(type=="instr"){tmp=param.indexOf("/");if(!cfmt.sound){if(tmp<0)return 0;param=param.replace("/","")}else{if(tmp<0)param="c"+param;else param=param.replace(/.*\//,"c")}}tmp=new scanBuf;tmp.buffer=param;tmp.index=0;tmp.ctx=parse.ctx;for(i=0;i<2;i++){note=parse_acc_pit(tmp);if(!note){return}note.pit+=124;val=(note.pit/7|0)*12+pit_st[note.pit%7];if(note.acc&&note.acc!=3)val+=note.acc;pit[i]=val}if(cfmt.sound)pit[0]=252;val=(pit[1]-pit[0])*3;if(note){switch(note.acc){default:return val;case 2:case 1:val++;break;case-1:case-2:val+=2;break}}if(val>0)return val;return val-3}function set_linebreak(param){var i,item;for(i=0;i<128;i++){if(char_tb[i]=="\n"){char_tb[i]=nil;break}}param=param.split(/\s+/);for(i=0;i<param.length;i++){item=param[i];switch(item){case"!":case"$":case"*":case";":case"?":case"@":break;case"<none>":continue;break;case"<EOL>":item="\n";break;default:parse.line.error("Bad value '$1' in %%linebreak - ignored",item);continue}char_tb[item.charCodeAt(0)]="\n"}}function set_user(param){var k,val,c=param[0],c2="!",i=param.indexOf("!"),j=param.indexOf('"');if(i<0){if(j<0){parse.line.error('Lack of starting ! or " in U: / %%user');return}c2='"';i=j}else if(j>0&&j<i){c2='"';i=j}j=param.indexOf(c2,i+1);if(j<0){parse.line.error("Lack of ending $1 in U:/%%user",c2);return}if(c=="\\"){c=param[1];if(c=="t")c="\t"}k=c.charCodeAt(0);if(k>=128){parse.line.error(not_ascii);return}switch(char_tb[k][0]){case"0":case"d":case"i":case" ":break;case'"':case"!":if(char_tb[k].length>1)break;default:parse.line.error("Bad user character '$1'",c);return}val=param.slice(i,j+1);switch(val){case"!beambreak!":val=" ";break;case"!ignore!":val="i";break;case"!nil!":case"!none!":val="d";break}char_tb[k]=val}function get_st_lines(param){var n,val;if(!param)return;if(param.match(/^[|.]+$/))return param;n=parseInt(param);switch(n){case 0:return"...";case 1:return"..|";case 2:return".||";case 3:return".|||"}if(isNaN(n)||n<0||n>16)return;val="|";while(--n>0)val+="|";return val}function new_block(subtype){var s={type:BLOCK,subtype:subtype,dur:0};if(parse.state==2)goto_tune();if(curvoice.last_sym)curvoice.last_sym.eoln=true;sym_link(s);return s}function next_word(param,i){while(param[i]&&param[i]!=" ")i++;while(param[i]==" ")i++;return i}function set_kv_parm(a){var s,item,parm,pos;if(!curvoice.init){curvoice.init=true;if(info.V){if(info.V["*"])a=info.V["*"].concat(a);if(info.V[curvoice.id])a=info.V[curvoice.id].concat(a)}}if(a.length==0)return 0;while(1){item=a.shift();if(!item)break;switch(item){case"clef=":s=a.shift();break;case"combine=":case"map=":case"octave=":case"scale=":case"uscale=":item=item.slice(0,-1);curvoice[item]=a.shift();break;case"cue=":curvoice.scale=a.shift()=="on"?.7:1;break;case"instrument=":curvoice.transp=get_transp(a.shift(),"instr");break;case"name=":case"nm=":curvoice.nm=a.shift();curvoice.new_name=true;break;case"stem=":item="stm";case"dyn=":case"gch=":case"gst=":case"orn=":case"stm=":case"voc=":case"vol=":item=item.slice(0,-1);if(!pos)pos={};pos[item]=a.shift();break;case"score=":if(cfmt.sound)break;item=a.shift();if(item.indexOf("/")<0)item+="/c";curvoice.transp=get_transp(item);break;case"shift=":curvoice.shift=get_transp(a.shift());break;case"sound=":case"transpose=":if(!cfmt.sound)break;curvoice.transp=get_transp(a.shift());break;case"subname=":case"sname=":case"snm=":curvoice.snm=a.shift();break;case"stafflines=":item=get_st_lines(a.shift());if(item==undefined)parse.line.error("Bad stafflines= value");else curvoice.stafflines=item;break;default:switch(item.slice(0,4)){case"treb":case"bass":case"alto":case"teno":case"perc":s=item;break;default:if("GFC".indexOf(item[0])>=0)s=item;else if(item.slice(-1)=="=")a.shift();break}break}}if(pos){curvoice.pos=clone(curvoice.pos);for(item in pos)curvoice.pos[item]=pos[item]}if(s){s=new_clef(s);if(s)get_clef(s)}}function memo_kv_parm(vid,a){if(a.length==0)return;if(!info.V)info.V={};if(info.V[vid])Array.prototype.push.apply(info.V[vid],a);else info.V[vid]=a}function new_key(param){var i,clef,key_end,c,tmp,mode=0,s={type:KEY,ctx:parse.ctx,istart:parse.istart,iend:parse.iend,k_delta:0,dur:0};i=1;switch(param[0]){case"A":s.k_sf=3;break;case"B":s.k_sf=5;break;case"C":s.k_sf=0;break;case"D":s.k_sf=2;break;case"E":s.k_sf=4;break;case"F":s.k_sf=-1;break;case"G":s.k_sf=1;break;case"H":switch(param[1]){case"P":s.k_bagpipe="P";i++;break;case"p":s.k_bagpipe="p";s.k_sf=2;i++;break;default:parse.line.error("Unknown bagpipe-like key");break}key_end=true;break;case"P":s.k_drum=true;key_end=true;break;case"n":if(param.indexOf("none")==0){s.k_sf=0;s.k_none=true;i=4}default:key_end=true;break}if(!key_end){switch(param[i]){case"#":s.k_sf+=7;i++;break;case"b":s.k_sf-=7;i++;break}param=param.slice(i).trim();switch(param.slice(0,3).toLowerCase()){case"aeo":case"m":case"min":s.k_sf-=3;mode=5;break;case"dor":s.k_sf-=2;mode=1;break;case"ion":case"maj":break;case"loc":s.k_sf-=5;mode=6;break;case"lyd":s.k_sf+=1;mode=3;break;case"mix":s.k_sf-=1;mode=4;break;case"phr":s.k_sf-=4;mode=2;break;default:if(param[0]=="m"&&(param[1]==" "||param[1]=="\t"||param[1]=="\n")){s.k_sf-=3;mode=5;break}key_end=true;break}if(!key_end)param=param.replace(/\w+\s*/,"");if(param.indexOf("exp ")==0){param=param.replace(/\w+\s*/,"");if(!param)parse.line.error("No accidental after 'exp'");s.k_exp=true}c=param[0];if(c=="^"||c=="_"||c=="="){s.k_a_acc=[];tmp=new scanBuf;tmp.buffer=param;tmp.index=0;tmp.ctx=parse.ctx;do{var note=parse_acc_pit(tmp);if(!note)return[s,null];var acc={pit:note.pit,acc:note.acc};s.k_a_acc.push(acc);c=tmp.char();while(c==" ")c=tmp.next_char()}while(c=="^"||c=="_"||c=="=");param=param.slice(tmp.index)}else if(s.k_exp&&param.indexOf("none")==0){s.k_sf=0;param=param.replace(/\w+\s*/,"")}}s.k_delta=(cgd2cde[(s.k_sf+7)%7]+14)%7;s.k_mode=mode;return[s,info_split(param,0)]}function new_meter(text){var s={type:METER,ctx:parse.ctx,istart:parse.istart,iend:parse.iend,dur:0,a_meter:[]},meter={},val,v,m1=0,m2,i=0,j,wmeasure,p=text,in_parenth;if(p.indexOf("none")==0){i=4;wmeasure=1}else{wmeasure=0;while(i<text.length){if(p[i]=="=")break;switch(p[i]){case"C":meter.top=p[i++];if(p[i]=="|")meter.top+=p[i++];m1=4;m2=4;break;case"c":case"o":m1=p[i]=="c"?4:3;m2=4;meter.top=p[i++];if(p[i]==".")meter.top+=p[i++];break;case"(":if(p[i+1]=="("){in_parenth=true;meter.top=p[i++];s.a_meter.push(meter);meter={}}j=i+1;while(j<text.length){if(p[j]==")"||p[j]=="/")break;j++}if(p[j]==")"&&p[j+1]=="/"){i++;continue}case")":in_parenth=p[i]=="(";meter.top=p[i++];s.a_meter.push(meter);meter={};continue;default:if(p[i]<="0"||p[i]>"9"){parse.line.error("Bad char '$1' in M:",p[i]);return}m2=2;meter.top=p[i++];for(;;){while(p[i]>="0"&&p[i]<="9")meter.top+=p[i++];if(p[i]==")"){if(p[i+1]!="/")break;i++}if(p[i]=="/"){i++;if(p[i]<="0"||p[i]>"9"){parse.line.error("Bad char '$1' in M:",p[i]);return}meter.bot=p[i++];while(p[i]>="0"&&p[i]<="9")meter.bot+=p[i++];break}if(p[i]!=" "&&p[i]!="+")break;if(i>=text.length||p[i+1]=="(")break;meter.top+=p[i++]}m1=parseInt(meter.top);break}if(!in_parenth){if(meter.bot)m2=parseInt(meter.bot);wmeasure+=m1*BASE_LEN/m2}s.a_meter.push(meter);meter={};while(p[i]==" ")i++;if(p[i]=="+"){meter.top=p[i++];s.a_meter.push(meter);meter={}}}}if(p[i]=="="){val=p.substring(++i);if(!val.match(/^(\d|\/)+$/)){parse.line.error("Bad duration '$1' in M:",val);return}wmeasure=BASE_LEN*eval(val)}s.wmeasure=wmeasure;if(parse.state!=3){info.M=text;glovar.meter=s;if(parse.state>=1){if(!glovar.ulen){if(wmeasure<=1||wmeasure>=BASE_LEN*3/4)glovar.ulen=BASE_LEN/8;else glovar.ulen=BASE_LEN/16}for(v=0;v<voice_tb.length;v++){voice_tb[v].meter=s;voice_tb[v].wmeasure=wmeasure}}}else{curvoice.wmeasure=wmeasure;if(is_voice_sig()){curvoice.meter=s;reset_gen()}else{sym_link(s)}}}function new_tempo(text){var i=0,j,c,nd,tmp,s={type:TEMPO,ctx:parse.ctx,istart:parse.istart,iend:parse.iend,dur:0};if(cfmt.writefields.indexOf("Q")<0)s.del=true;if(text[0]=='"'){i=text.indexOf('"',1);if(i<0){parse.line.error("Unterminated string in Q:");return}s.tempo_str1=text.slice(1,i);i++;while(text[i]==" ")i++}tmp=new scanBuf;tmp.buffer=text;tmp.index=i;while(1){c=tmp.char();if(c==undefined||c<="0"||c>"9")break;nd=parse_dur(tmp);if(!s.tempo_notes)s.tempo_notes=[];s.tempo_notes.push(BASE_LEN*nd[0]/nd[1]);while(1){c=tmp.char();if(c!=" ")break;tmp.advance()}}if(c=="="){c=tmp.next_char();while(c==" ")c=tmp.next_char();i=tmp.index;if(c=="c"&&text[i+1]=="a"&&text[i+2]=="."&&text[i+3]==" "){s.tempo_ca="ca. ";tmp.index+=4;c=tmp.char()}if(text[tmp.index+1]!="/"){s.tempo=tmp.get_int()}else{nd=parse_dur(tmp);s.new_beat=BASE_LEN*nd[0]/nd[1]}c=tmp.char();while(c==" ")c=tmp.next_char();
  }if(c=='"'){tmp.advance();i=text.indexOf('"',tmp.index+1);if(i<0){parse.line.error("Unterminated string in Q:");return}s.tempo_str2=text.slice(tmp.index,i)}if(parse.state!=3){if(parse.state==1){info.Q=text;glovar.tempo=s;return}goto_tune()}if(curvoice.v==par_sy.top_voice){sym_link(s);if(glovar.tempo&&curvoice.time==0)glovar.tempo.del=true}}function do_info(info_type,text){var s,d1,d2,a,vid;switch(info_type){case"I":do_pscom(text);break;case"L":if(parse.state==2)goto_tune();a=text.match(/^(\d+)\/(\d+)(=(\d+)\/(\d+))?$/);if(a){d1=Number(a[2]);if(!d1||(d1&d1-1)!=0)break;d1=Number(a[1])/d1*BASE_LEN;if(a[3]){d2=Number(a[5]);if(!d2||(d2&d2-1)!=0){d2=0;break}d2=Number(a[4])/d2*BASE_LEN}else{d2=d1}}else if(text=="auto"){d1=d2=-1}if(!d2){parse.line.error("Bad L: value");break}if(parse.state<2){glovar.ulen=d1}else{curvoice.ulen=d1;curvoice.dur_fact=d2/d1}break;case"M":new_meter(text);break;case"U":set_user(text);break;case"P":if(parse.state==0)break;if(parse.state==1){info.P=text;break}if(parse.state==2)goto_tune();if(cfmt.writefields.indexOf(info_type)<0)break;s={type:PART,ctx:parse.ctx,istart:parse.istart,iend:parse.iend,text:text,dur:0};var p_voice=voice_tb[par_sy.top_voice];if(curvoice.v!=p_voice.v){if(curvoice.time!=p_voice.time)break;if(p_voice.last_sym&&p_voice.last_sym.type==PART)break;var curvoice_sav=curvoice;curvoice=p_voice;sym_link(s);curvoice=curvoice_sav}else{sym_link(s)}break;case"Q":if(parse.state==0)break;new_tempo(text);break;case"V":get_voice(text);break;case"K":if(parse.state==0||!text)break;get_key(new_key(text));break;case"N":case"R":if(!info[info_type])info[info_type]=text;else info[info_type]+="\n"+text;break;case"r":if(!user.keep_remark||parse.state!=3)break;s={type:REMARK,ctx:parse.ctx,istart:parse.istart,iend:parse.iend,text:text,dur:0};sym_link(s);break;default:parse.line.error("'$1:' line ignored",info_type);break}}function adjust_dur(s){var s2,time,auto_time,i,res;s2=curvoice.last_sym;if(!s2)return;if(s2.type==MREST||s2.type==BAR)return;while(s2.type!=BAR&&s2.prev)s2=s2.prev;time=s2.time;auto_time=curvoice.time-time;if(time==0){while(s2&&!s2.dur)s2=s2.next;if(s2&&s2.type==REST&&s2.invis){time+=s2.dur*curvoice.wmeasure/auto_time;if(s2.prev)s2.prev.next=s2.next;else curvoice.sym=s2.next;if(s2.next)s2.next.prev=s2.prev;s2=s2.next}}if(curvoice.wmeasure==auto_time)return;for(;s2;s2=s2.next){s2.time=time;if(!s2.dur||s2.grace)continue;s2.dur=s2.dur*curvoice.wmeasure/auto_time;s2.dur_orig=s2.dur_orig*curvoice.wmeasure/auto_time;time+=s2.dur;if(s2.type!=NOTE&&s2.type!=REST)continue;for(i=0;i<=s2.nhd;i++)s2.notes[i].dur=s2.notes[i].dur*curvoice.wmeasure/auto_time;res=identify_note(s2,s2.dur_orig);s2.head=res[0];s2.dots=res[1];s2.nflags=res[2];if(s2.nflags<=-2)s2.stemless=true;else delete s2.stemless}curvoice.time=s.time=time}function new_bar(){var s2,c,bar_type,line=parse.line,s={type:BAR,ctx:parse.ctx,dur:0,multi:0};s.istart=parse.bol+line.index;if(vover&&vover.bar)get_vover("|");if(glovar.new_nbar){s.bar_num=glovar.new_nbar;glovar.new_nbar=0}bar_type=line.char();while(1){c=line.next_char();switch(c){case"|":case"[":case"]":case":":bar_type+=c;continue}break}if(bar_type[0]==":"){if(bar_type.length==1){bar_type="|";s.bar_dotted=true}else{s.rbstop=2}}if(a_gch)gch_build(s);if(a_dcn){deco_cnv(a_dcn,s);a_dcn=null}switch(bar_type.slice(-1)){case"[":if(bar_type.length==1){s.text="";break}bar_type=bar_type.slice(0,-1);line.index--;c="[";break;case":":s.rbstop=2;break}if(c>"0"&&c<="9"){s.text=c;while(1){c=line.next_char();if("0123456789,.-".indexOf(c)<0)break;s.text+=c}s.rbstop=2;s.rbstart=2}else if(c=='"'&&bar_type=="["){while(1){c=line.next_char();if(!c){line.error("No end of repeat string");return}if(c=='"'){line.advance();break}if(c=="\\"){s.text+=c;c=line.next_char()}s.text+=c}s.text=cnv_escape(s.text);s.rbstop=2;s.rbstart=2}if(bar_type[0]=="]"){s.rbstop=2;if(bar_type.length!=1)bar_type=bar_type.slice(1);else s.invis=true}s.iend=parse.bol+line.index;if(s.rbstart&&curvoice.norepbra&&!curvoice.second)s.norepbra=true;if(curvoice.ulen<0)adjust_dur(s);s2=curvoice.last_sym;if(s2&&s2.type==SPACE){s2.time--}else if(s2&&s2.type==BAR){if(bar_type=="["&&!s2.text&&!s2.a_gch&&(curvoice.st==0||par_sy.staves[curvoice.st-1].flags&STOP_BAR||s.norepbra)){if(s.text)s2.text=s.text;if(s.a_gch)s2.a_gch=s.a_gch;if(s.norepbra)s2.norepbra=s.norepbra;if(s.rbstart)s2.rbstart=s.rbstart;if(s.rbstop)s2.rbstop=s.rbstop;return}if(bar_type=="|:"&&!s.text){if(s2.bar_type==":|"){s2.bar_type="::";s2.rbstop=2;return}if(s2.bar_type=="||"){s2.bar_type="||:";s2.rbstop=2;return}}}switch(bar_type){case"[":s.rbstop=2;case"[]":case"[|]":s.invis=true;bar_type="[]";break;case":|:":case":||:":bar_type="::";break;case"||":if(!cfmt.rbdbstop)break;case"[|":case"|]":s.rbstop=2;break}s.bar_type=bar_type;if(!curvoice.lyric_restart)curvoice.lyric_restart=s;if(s2&&s2.type==KEY&&(!s2.prev||s2.prev.type!=BAR)){curvoice.last_sym=s2.prev;if(!curvoice.last_sym)curvoice.sym=null;sym_link(s);s.next=s2;s2.prev=s;curvoice.last_sym=s2}else{sym_link(s)}s.st=curvoice.st;if(s.rbstart&&!curvoice.norepbra&&curvoice.st>0&&!(par_sy.staves[curvoice.st-1].flags&STOP_BAR)){s2={type:BAR,ctx:s.ctx,istart:s.istart,iend:s.iend,bar_type:"[",multi:0,invis:true,text:s.text,rbstart:2};sym_link(s2);s2.st=curvoice.st;s.text=undefined;s.rbstart=0}}function parse_staves(param){var v,vid,a_vf=[],err=false,flags=0,brace=0,bracket=0,parenth=0,flags_st=0,p=param,i=0;while(i<p.length){switch(p[i]){case" ":case"\t":break;case"[":if(parenth||brace+bracket>=2){parse.line.error("Misplaced '[' in %%staves");err=true;break}flags|=brace+bracket==0?OPEN_BRACKET:OPEN_BRACKET2;bracket++;flags_st<<=8;flags_st|=OPEN_BRACKET;break;case"{":if(parenth||brace||bracket>=2){parse.line.error("Misplaced '{' in %%staves");err=true;break}flags|=!bracket?OPEN_BRACE:OPEN_BRACE2;brace++;flags_st<<=8;flags_st|=OPEN_BRACE;break;case"(":if(parenth){parse.line.error("Misplaced '(' in %%staves");err=true;break}flags|=OPEN_PARENTH;parenth++;flags_st<<=8;flags_st|=OPEN_PARENTH;break;case"*":if(brace&&!parenth&&!(flags&(OPEN_BRACE|OPEN_BRACE2)))flags|=FL_VOICE;break;case"+":flags|=MASTER_VOICE;break;default:if(!p[i].match(/\w/)){parse.line.error("Bad voice ID in %%staves");err=true;break}vid="";while(i<p.length){if(" \t()[]{}|*".indexOf(p[i])>=0)break;vid+=p[i++]}for(;i<p.length;i++){switch(p[i]){case" ":case"\t":continue;case"]":if(!(flags_st&OPEN_BRACKET)){parse.line.error("Misplaced ']' in %%staves");err=true;break}bracket--;flags|=brace+bracket==0?CLOSE_BRACKET:CLOSE_BRACKET2;flags_st>>=8;continue;case"}":if(!(flags_st&OPEN_BRACE)){parse.line.error("Misplaced '}' in %%staves");err=true;break}brace--;flags|=!bracket?CLOSE_BRACE:CLOSE_BRACE2;flags&=~FL_VOICE;flags_st>>=8;continue;case")":if(!(flags_st&OPEN_PARENTH)){parse.line.error("Misplaced ')' in %%staves");err=true;break}parenth--;flags|=CLOSE_PARENTH;flags_st>>=8;continue;case"|":flags|=STOP_BAR;continue}break}a_vf.push([vid,flags]);flags=0;continue}i++}if(flags_st!=0){parse.line.error("'}', ')' or ']' missing in %%staves");err=true}if(err||a_vf.length==0)return null;return a_vf}function info_split(text,start){var a=[],item="",i,j,n=text.length;for(i=0;i<n;i++){switch(text[i]){case"=":if(!item){item="=";break}item+="=";if(a.length<start)break;a.push(item);item="";break;case" ":case"\t":if(!item)break;a.push(item);item="";break;case'"':if(item){a.push(item);item=""}j=++i;while(i<n){if(text[i]=='"')break;if(text[i]=="\\")i++;i++}if(text[i]!='"'){parse.line.error("Unterminated string");break}a.push(text.slice(j,i));break;case"\\":item+=text[i++];default:item+=text[i];break}}if(item)a.push(item);return a}function identify_note(s,dur){var head,dots,flags;if(dur%12!=0)parse.line.error("Invalid note duration $1",dur);dur/=12;if(dur==0)parse.line.error("Note too short");for(flags=5;dur!=0;dur>>=1,flags--){if(dur&1)break}dur>>=1;switch(dur){case 0:dots=0;break;case 1:dots=1;break;case 3:dots=2;break;default:dots=3;break}flags-=dots;if(flags>=0){head=FULL}else switch(flags){default:parse.line.error("Note too long");flags=-4;case-4:head=SQUARE;break;case-3:head=cfmt.squarebreve?SQUARE:OVALBARS;break;case-2:head=OVAL;break;case-1:head=EMPTY;break}return[head,dots,flags]}var reg_dur=/(\d*)(\/*)(\d*)/g;function parse_dur(line){var res,num,den;reg_dur.lastIndex=line.index;res=reg_dur.exec(line.buffer);if(!res[0])return[1,1];num=res[1]||1;den=res[3]||1;if(!res[3])den*=1<<res[2].length;line.index=reg_dur.lastIndex;return[num,den]}function parse_acc_pit(line){var note,acc,micro_n,micro_d,pit,c=line.char();switch(c){case"^":c=line.next_char();if(c=="^"){acc=2;c=line.next_char()}else{acc=1}break;case"=":acc=3;c=line.next_char();break;case"_":c=line.next_char();if(c=="_"){acc=-2;c=line.next_char()}else{acc=-1}break}if(acc&&(c>="1"&&c<="9")||c=="/"){nd=parse_dur(line);micro_n=nd[0];micro_d=nd[1];if(micro_d==1)micro_d=curvoice.uscale;else micro_d*=2;c=line.char()}pit="CDEFGABcdefgab".indexOf(c)+16;c=line.next_char();if(pit<16){line.error("'$1' is not a note",line.buffer[line.index-1]);return}while(c=="'"){pit+=7;c=line.next_char()}while(c==","){pit-=7;c=line.next_char()}note={pit:pit,apit:pit,shhd:0,shac:0,ti1:0};if(acc){note.acc=acc;if(micro_n){note.micro_n=micro_n;note.micro_d=micro_d}}return note}function set_map(note){var bn,an,nn,i,nmap,map=maps[curvoice.map];bn="abcdefg"[(note.pit+77)%7];if(note.acc)an=["__","_","","^","^^","="][note.acc+2];else an="";nn=an+bn;for(i=note.pit;i>=28;i-=7)nn+="'";for(i=note.pit;i<21;i+=7)nn+=",";nmap=map[nn];if(nmap){if(nmap[1]){note.apit=note.pit=nmap[1].pit;note.acc=nmap[1].acc}}else{nn="octave,"+an+bn;if(!map[nn]){nn="key,"+"abcdefg"[(note.pit+77-curvoice.ckey.k_delta)%7];if(!map[nn]){nn="all";if(!map[nn])return}}}note.map=map[nn]}function parse_basic_note(line,ulen){var nd,note=parse_acc_pit(line);if(!note)return null;if(line.char()=="0"){parse.stemless=true;line.advance()}nd=parse_dur(line);note.dur=ulen*nd[0]/nd[1];return note}function parse_vpos(){var c,line=parse.line,ti1=0;if(line.buffer[line.index-1]=="."&&!a_dcn)ti1=SL_DOTTED;switch(line.next_char()){case"'":line.index++;return ti1+SL_ABOVE;case",":line.index++;return ti1+SL_BELOW}return ti1+SL_AUTO}const cde2fcg=[0,2,4,-1,1,3,5],cgd2cde=[0,4,1,5,2,6,3],acc2=[-2,-1,3,1,2];function note_transp(s){var i,j,n,d,a,acc,i1,i3,i4,note,m=s.nhd,sf_old=curvoice.okey.k_sf,i2=curvoice.ckey.k_sf-sf_old,dp=cgd2cde[(i2+4*7)%7],t=curvoice.vtransp;if(t<0&&dp!=0)dp-=7;dp+=(t/3/12|0)*7;for(i=0;i<=m;i++){note=s.notes[i];n=note.pit;note.pit+=dp;note.apit=note.pit;i1=cde2fcg[(n+5+16*7)%7];a=note.acc;if(!a){if(!curvoice.okey.a_acc){if(sf_old>0){if(i1<sf_old-1)a=1}else if(sf_old<0){if(i1>=sf_old+6)a=-1}}else{for(j=0;j<curvoice.okey.a_acc.length;j++){acc=curvoice.okey.a_acc[j];if((n+16*7-acc.pit)%7==0){a=acc.acc;break}}}}i3=i1+i2;if(a&&a!=3)i3+=a*7;i1=(((i3+1+21)/7|0)+2-3+32*5)%5;a=acc2[i1];if(note.acc){}else if(curvoice.ckey.k_none){if(a==3||acc_same_pitch(note.pit))continue}else if(curvoice.ckey.a_acc){i4=cgd2cde[(i3+16*7)%7];for(j=0;j<curvoice.ckey.a_acc.length;j++){if((i4+16*7-curvoice.ckey.a_acc[j].pits)%7==0)break}if(j<curvoice.ckey.a_acc.length)continue}else{continue}i1=note.acc;d=note.micro_d;if(d&&i1!=a){n=note.micro_n;switch(a){case 3:if(n>d/2){n-=d/2;note.micro_n=n;a=i1}else{a=-i1}break;case 2:if(n>d/2){note.pit+=1;note.apit=note.pit;n-=d/2}else{n+=d/2}a=i1;note.micro_n=n;break;case-2:if(n>=d/2){note.pit-=1;note.apit=note.pit;n-=d/2}else{n+=d/2}a=i1;note.micro_n=n;break}}note.acc=a}}function sort_pitch(s){function pitch_compare(n1,n2){return n1.pit-n2.pit}s.notes=s.notes.sort(pitch_compare)}function new_note(grace,tp_fact){var note,s,in_chord,c,dcn,i,n,s2,nd,res,num,dur,sl1=0,line=parse.line,a_dcn_sav=a_dcn;a_dcn=null;parse.stemless=false;s={type:NOTE,ctx:parse.ctx,stem:0,multi:0,nhd:0,xmx:0};s.istart=parse.bol+line.index;if(curvoice.color)s.color=curvoice.color;if(grace){s.grace=true}else{if(a_gch)gch_build(s);if(parse.repeat_n){s.repeat_n=parse.repeat_n;s.repeat_k=parse.repeat_k;parse.repeat_n=0}}c=line.char();switch(c){case"X":s.invis=true;case"Z":s.type=MREST;c=line.next_char();if(c>"0"&&c<="9")s.nmes=line.get_int();else s.nmes=1;s.dur=curvoice.wmeasure*s.nmes;if(curvoice.second){curvoice.time+=s.dur;return null}break;case"y":s.type=SPACE;s.invis=true;s.dur=0;c=line.next_char();if(c>="0"&&c<="9")s.width=line.get_int();else s.width=10;break;case"x":s.invis=true;case"z":s.type=REST;line.advance();nd=parse_dur(line);s.dur_orig=(curvoice.ulen<0?BASE_LEN/4:curvoice.ulen)*nd[0]/nd[1];s.dur=s.dur_orig*curvoice.dur_fact;s.notes=[{pit:18,dur:s.dur_orig}];break;case"[":in_chord=true;c=line.next_char();default:if(curvoice.uscale)s.uscale=curvoice.uscale;s.notes=[];while(1){if(in_chord){while(1){if(!c||c=="%")break;i=c.charCodeAt(0);if(i>=128){line.error(not_ascii);return null}type=char_tb[i];switch(type[0]){case"(":sl1<<=4;sl1+=parse_vpos();c=line.char();continue;case"!":if(!a_dcn)a_dcn=[];if(type.length>1){a_dcn.push(type.slice(1,-1))}else{dcn="";while(1){c=line.next_char();if(!c||c=="%"){line.error("No end of decoration");return null}if(c=="!")break;dcn+=c}a_dcn.push(dcn)}c=line.next_char();continue}break}}note=parse_basic_note(line,s.grace||curvoice.ulen<0?BASE_LEN/4:curvoice.ulen);if(!note)return null;if(curvoice.octave)note.apit=note.pit+=curvoice.octave*7;if(curvoice.ottava)note.pit+=curvoice.ottava;if(sl1){note.sl1=sl1;if(s.sl1)s.sl1++;else s.sl1=1;sl1=0}if(a_dcn){note.a_dcn=a_dcn;a_dcn=null}s.notes.push(note);if(!in_chord)break;c=line.char();while(1){switch(c){case")":if(note.sl2)note.sl2++;else note.sl2=1;if(s.sl2)s.sl2++;else s.sl2=1;c=line.next_char();continue;case"-":note.ti1=parse_vpos();s.ti1=true;c=line.char();continue;case".":c=line.next_char();if(c!="-"){line.error("Misplaced dot");break}continue}break}if(c=="]"){line.advance();nd=parse_dur(line);s.nhd=s.notes.length-1;for(i=0;i<=s.nhd;i++){note=s.notes[i];note.dur=note.dur*nd[0]/nd[1]}break}}s.dur_orig=s.notes[0].dur;s.dur=s.notes[0].dur*curvoice.dur_fact}if(s.grace&&s.type!=NOTE){line.error("Not a note in grace note sequence");return null}if(s.notes){if(!s.grace){switch(curvoice.pos.stm){case SL_ABOVE:s.stem=1;break;case SL_BELOW:s.stem=-1;break;case SL_HIDDEN:s.stemless=true;break}s.combine=curvoice.combine;s.dur*=tp_fact;num=curvoice.brk_rhythm;if(num){curvoice.brk_rhythm=0;s2=curvoice.last_note;if(num>0){n=num*2-1;s.dur=s.dur*n/num;s.dur_orig=s.dur_orig*n/num;for(i=0;i<=s.nhd;i++)s.notes[i].dur=s.notes[i].dur*n/num;s2.dur/=num;s2.dur_orig/=num;for(i=0;i<=s2.nhd;i++)s2.notes[i].dur/=num}else{num=-num;n=num*2-1;s.dur/=num;s.dur_orig/=num;for(i=0;i<=s.nhd;i++)s.notes[i].dur/=num;s2.dur=s2.dur*n/num;s2.dur_orig=s2.dur_orig*n/num;for(i=0;i<=s2.nhd;i++)s2.notes[i].dur=s2.notes[i].dur*n/num}curvoice.time=s2.time+s2.dur;res=identify_note(s2,s2.dur_orig);s2.head=res[0];s2.dots=res[1];s2.nflags=res[2];if(s2.nflags<=-2)s2.stemless=true;else delete s2.stemless;for(s2=s2.next;s2;s2=s2.next)s2.time=curvoice.time}}else{var div=curvoice.key.k_bagpipe?8:4;for(i=0;i<=s.nhd;i++)s.notes[i].dur/=div;s.dur/=div;s.dur_orig/=div;if(grace.stem)s.stem=grace.stem}if(s.type==NOTE){res=identify_note(s,s.dur_orig);s.head=res[0];s.dots=res[1];s.nflags=res[2];if(s.xstem)s.nflags=0;if(s.nflags<=-2)s.stemless=true;if(curvoice.map&&maps[curvoice.map]){for(i=0;i<=s.nhd;i++)set_map(s.notes[i])}}else{dur=s.dur_orig;if(dur==curvoice.wmeasure){if(dur<BASE_LEN*2)dur=BASE_LEN;else if(dur<BASE_LEN*4)dur=BASE_LEN*2;else dur=BASE_LEN*4}res=identify_note(s,dur);s.head=res[0];s.dots=res[1];s.nflags=res[2]}curvoice.last_note=s}sym_link(s);if(s.type==NOTE&&curvoice.vtransp)note_transp(s);if(cfmt.shiftunison)s.shiftunison=cfmt.shiftunison;if(!curvoice.lyric_restart)curvoice.lyric_restart=s;if(a_dcn_sav)deco_cnv(a_dcn_sav,s,s.prev);if(parse.stemless)s.stemless=true;s.iend=parse.bol+line.index;return s}const nil=["0"];var char_tb=[nil,nil,nil,nil,nil,nil,nil,nil,nil," ","\n",nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil," ","!",'"',nil,"\n",nil,"&",nil,"(",")",nil,nil,nil,"-","!dot!",nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,"|",nil,"<","n","<",nil,nil,"n","n","n","n","n","n","n","!fermata!","d","d","d","!emphasis!","!lowermordent!","d","!coda!","!uppermordent!","d","d","!segno!","!trill!","d","d","d","n","d","n","[","\\","|","n","n","i","n","n","n","n","n","n","n","d","d","d","d","d","d","d","d","d","d","d","d","d","!upbow!","!downbow!","d","n","n","n","{","|","}","!roll!",nil];function parse_music_line(){var s,grace,sappo,dcn,i,c,idx,type,n,text,last_note_sav,a_dcn_sav,no_eol,tp_a=[],tp,tpn=-1,tp_fact=1,slur_start=0;var line=parse.line;line.buffer=parse.file.slice(parse.bol,parse.eol);line.index=0;if(parse.state!=3){if(parse.state!=2)return;goto_tune()}while(1){c=line.char();if(!c||c=="%")break;if(c=="."){switch(line.buffer[line.index+1]){case"(":case"-":case"|":c=line.next_char();break}}idx=c.charCodeAt(0);if(idx>=128){line.error(not_ascii);line.advance();continue}type=char_tb[idx];switch(type[0]){case" ":s=curvoice.last_note;if(s){s.beam_end=true;if(grace)s.gr_shift=true}break;case"\n":if(cfmt.barsperstaff)break;if(par_sy.voices[curvoice.v].range==0&&curvoice.last_sym)curvoice.last_sym.eoln=true;break;case"&":c=line.next_char();if(c==")"){get_vover(")");break}get_vover("&");continue;case"(":c=line.next_char();if(c>"0"&&c<="9"){var pplet=line.get_int(),qplet=[0,1,3,2,3,0,2,0,3,0][pplet],rplet=pplet,c=line.char();if(c==":"){c=line.next_char();if(c>"0"&&c<="9"){qplet=line.get_int();c=line.char()}if(c==":"){c=line.next_char();if(c>"0"&&c<="9"){rplet=line.get_int();c=line.char()}else{line.error("Invalid 'r' in tuplet");continue}}}else{if(qplet==0)qplet=curvoice.wmeasure%9==0?3:2}tp=tp_a[++tpn];if(!tp)tp_a[tpn]=tp={};tp.p=pplet;tp.q=qplet;tp.r=rplet;tp.f=cfmt.tuplets;tp.fact=tp_fact*qplet/pplet;tp_fact=tp.fact;continue}if(c=="&"){get_vover("(");break}slur_start<<=4;line.index--;slur_start+=parse_vpos();continue;case")":if(curvoice.ignore)break;s=curvoice.last_sym;if(s){switch(s.type){case NOTE:case REST:case SPACE:break;default:s=null;break}}if(!s){line.error("Bad character ')'");break}if(s.slur_end)s.slur_end++;else s.slur_end=1;break;case"!":if(!a_dcn)a_dcn=[];if(type.length>1){dcn=type.slice(1,-1)}else{dcn="";i=line.index;while(1){c=line.next_char();if(c=="%")c=0;if(!c)break;if(c=="!")break;dcn+=c}if(!c){line.index=i;line.error("No end of decoration");break}}a_dcn.push(dcn);if(dcn[0]=="8"||dcn[0]=="1")set_ottava(dcn);break;case'"':parse_gchord(type);break;case"-":var tie_pos=0;if(!curvoice.last_note||curvoice.last_note.type!=NOTE){line.error("No note before '-'");break}tie_pos=parse_vpos();s=curvoice.last_note;for(i=0;i<=s.nhd;i++){if(!s.notes[i].ti1)s.notes[i].ti1=tie_pos;else if(s.nhd==0)line.error("Too many ties")}s.ti1=true;continue;case"[":var c_next=line.buffer[line.index+1];if('|[]: "'.indexOf(c_next)>=0||c_next>="1"&&c_next<="9"){if(grace){line.error("Cannot have a bar in grace notes");break}new_bar();continue}if(line.buffer[line.index+2]==":"){parse.istart=parse.bol+line.index;line.index++;i=line.buffer.indexOf("]",line.index);if(i<0){line.error("Lack of ']'");break}text=line.buffer.slice(line.index+2,i).trim();line.index=i+1;parse.iend=parse.bol+line.index;var err=do_info(c_next,text);if(err)line.error(err);continue}case"n":s=new_note(grace,tp_fact);if(!s)continue;if(s.type==NOTE){s.slur_start=slur_start;slur_start=0;if(sappo){s.sappo=true;sappo=false}}if(grace){if(tpn>=0)s.in_tuplet=true;continue}if(tpn>=0&&s.notes){s.in_tuplet=true;if(tpn>0){if(tp_a[0].p){s.tp0=tp_a[0].p;s.tq0=tp_a[0].q;s.tf=tp_a[0].f;tp_a[0].p=0}tp_a[0].r--;if(tp.p){s.tp1=tp.p;s.tq1=tp.q;s.tf=tp.f;tp.p=0}}else if(tp.p){s.tp0=tp.p;s.tq0=tp.q;s.tf=tp.f;tp.p=0}tp.r--;if(tp.r==0){if(tpn--==0){s.te0=true;tp_fact=1;curvoice.time=Math.round(curvoice.time);s.dur=curvoice.time-s.time}else{s.te1=true;tp=tp_a[0];if(tp.r==0){tpn--;s.te0=true;tp_fact=1;curvoice.time=Math.round(curvoice.time);s.dur=curvoice.time-s.time}else{tp_fact=tp.fact}}}}continue;case"<":if(!curvoice.last_note){line.error("No note before '<'");break}n=c=="<"?1:-1;while(c=="<"||c==">"){n*=2;c=line.next_char()}curvoice.brk_rhythm=n;continue;case"i":break;case"{":if(grace){line.error("'{' in grace note");break}last_note_sav=curvoice.last_note;curvoice.last_note=null;a_dcn_sav=a_dcn;a_dcn=undefined;grace={type:GRACE,ctx:parse.ctx,istart:parse.bol+line.index,dur:0,multi:0};switch(curvoice.pos.gst){case SL_ABOVE:grace.stem=1;break;case SL_BELOW:grace.stem=-1;break;case SL_HIDDEN:grace.stem=2;break}sym_link(grace);c=line.next_char();if(c=="/"){sappo=true;break}continue;case"|":c=line.buffer[line.index-1];new_bar();if(c==".")curvoice.last_sym.bar_dotted=true;continue;case"}":s=curvoice.last_note;if(!grace||!s){line.error("Bad character '}'");break}if(a_dcn)line.error("Decoration ignored");s.gr_end=true;grace.extra=grace.next;grace.extra.prev=null;grace.next=null;curvoice.last_sym=grace;grace=null;if(!s.prev&&!curvoice.key.k_bagpipe){for(i=0;i<=s.nhd;i++)s.notes[i].dur*=2;s.dur*=2;s.dur_orig*=2;var res=identify_note(s,s.dur_orig);s.head=res[0];s.dots=res[1];s.nflags=res[2]}curvoice.last_note=last_note_sav;a_dcn=a_dcn_sav;break;case"\\":for(i=line.index+1;;i++){switch(line.buffer[i]){case" ":case"\t":continue;case"%":line.index=line.buffer.length;case undefined:c=undefined;no_eol=true;break}break}if(!c)break;default:line.error("Bad character '$1'",c);break}line.advance()}if(tpn>=0){line.error("No end of tuplet");for(s=curvoice.last_note;s;s=s.prev){if(s.tp1)s.tp1=0;if(s.tp0){s.tp0=0;break}}}if(grace){line.error("No end of grace note sequence");curvoice.last_sym=grace.prev}if(cfmt.breakoneoln&&curvoice.last_note)curvoice.last_note.beam_end=true;if(no_eol||cfmt.barsperstaff)return;if(char_tb["\n".charCodeAt(0)]=="\n"&&par_sy.voices[curvoice.v].range==0&&curvoice.last_sym)curvoice.last_sym.eoln=true}const cw_tb=[.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.25,.333,.408,.5,.5,.833,.778,.333,.333,.333,.5,.564,.25,.564,.25,.278,.5,.5,.5,.5,.5,.5,.5,.5,.5,.5,.278,.278,.564,.564,.564,.444,.921,.722,.667,.667,.722,.611,.556,.722,.722,.333,.389,.722,.611,.889,.722,.722,.556,.722,.667,.556,.611,.722,.722,.944,.722,.722,.611,.333,.278,.333,.469,.5,.333,.444,.5,.444,.5,.444,.333,.5,.5,.278,.278,.5,.278,.778,.5,.5,.5,.5,.333,.389,.278,.5,.5,.722,.5,.5,.444,.48,.2,.48,.541,.5];function cwid(c){var i=c.charCodeAt(0);if(i>=128)i=97;return cw_tb[i]}function strw(str){var swfac=gene.curfont.swfac,w=0,i,j,c,n=str.length;for(i=0;i<n;i++){c=str[i];switch(c){case"$":c=str[i+1];if(c=="0"){gene.curfont=gene.deffont}else if(c>="1"&&c<="9"){gene.curfont=get_font("u"+c)}else{w+=cwid("$")*swfac;break}i++;swfac=gene.curfont.swfac;continue;case"&":j=str.indexOf(";",i);if(j>0&&j-i<10){i=j;c="a"}break}w+=cwid(c)*swfac}return w}function set_font(xxx){gene.curfont=gene.deffont=get_font(xxx)}function out_str(str){var n_font,o_font=gene.curfont,c_font=o_font;output.push(str.replace(/<|>|&.*?;|&|\$./g,function(c){switch(c[0]){case"<":return"&lt;";case">":return"&gt;";case"&":if(c=="&")return"&amp;";return c;case"$":if(c[1]=="0")n_font=gene.deffont;else if(c[1]>="1"&&c[1]<="9")n_font=get_font("u"+c[1]);else return c;c="";if(n_font==c_font)return c;if(c_font!=o_font)c="</tspan>";c_font=n_font;if(c_font==o_font)return c;return c+'<tspan\n\tclass="f'+n_font.fid+cfmt.fullsvg+'">'}}));if(c_font!=o_font)output.push("</tspan>")}function xy_str(x,y,str,action,line_w){output.push('<text class="f'+gene.curfont.fid+cfmt.fullsvg+'" x="');out_sxsy(x,'" y="',y);switch(action){case"c":output.push('" text-anchor="middle">');break;case"j":output.push('" textLength="'+line_w.toFixed(2)+'">');break;case"r":output.push('" text-anchor="end">');break;default:output.push('">');break}out_str(str);output.push("</text>\n")}function xy_str_b(x,y,str){var w=strw(str);output.push('<rect class="stroke" x="');out_sxsy(x-2,'" y="',y+gene.curfont.size-2);output.push('" width="'+(w+4).toFixed(2)+'" height="'+(gene.curfont.size+3).toFixed(2)+'"/>\n');xy_str(x,y,str)}function trim_title(title,is_subtitle){var i;if(cfmt.titletrim){i=title.lastIndexOf(", ");if(i<0||title[i+2]<"A"||title[i+2]>"Z"||i<title.length-7||title.indexOf(" ",i+3)>=0)i=0}if(!is_subtitle&&cfmt.writefields.indexOf("X")>=0)title=info.X+".  "+title;if(i)title=title.slice(i+2).trim()+" "+title.slice(0,i);if(cfmt.titlecaps)return title.toUpperCase();return title}function write_title(title,is_subtitle){var font,sz;if(!title)return;title=trim_title(title,is_subtitle);if(is_subtitle){set_font("subtitle");sz=gene.curfont.size;vskip(cfmt.subtitlespace+sz)}else{set_font("title");sz=gene.curfont.size;vskip(cfmt.titlespace+sz)}if(cfmt.titleleft)xy_str(0,sz*.2,title);else xy_str((cfmt.pagewidth-cfmt.leftmargin-cfmt.rightmargin)/2/cfmt.scale,sz*.2,title,"c")}function put_inf2r(x,y,str1,str2,action){if(!str1){if(!str2)return;str1=str2;str2=null}if(!str2)xy_str(x,y,str1,action);else xy_str(x,y,str1+" ("+str2+")",action)}function write_text(text,action){if(action=="s")return;set_font("text");var strlw=(cfmt.pagewidth-cfmt.leftmargin-cfmt.rightmargin)/cfmt.scale,lineskip=gene.curfont.size*cfmt.lineskipfac,parskip=gene.curfont.size*cfmt.parskipfac,i,j,x;switch(action){default:switch(action){case"c":x=strlw/2;break;case"r":x=strlw;break;default:x=0;break}j=0;while(1){i=text.indexOf("\n",j);if(i<0){vskip(lineskip);xy_str(x,0,text.slice(j),action);break}if(i==j){vskip(parskip);blk_out();while(text[i+1]=="\n"){vskip(lineskip);i++}if(i==text.length)break}else{vskip(lineskip);xy_str(x,0,text.slice(j,i),action)}j=i+1}vskip(parskip);blk_out();break;case"f":case"j":var words;j=0;while(1){i=text.indexOf("\n\n",j);if(i<0)words=text.slice(j);else words=text.slice(j,i);words=words.split(/\s+/);var w=0,k=0;for(j=0;j<words.length;j++){w+=strw(words[j]+" ");if(w>=strlw){vskip(lineskip);xy_str(0,0,words.slice(k,j).join(" "),action,strlw);k=j;w=0}}if(w!=0){vskip(lineskip);xy_str(0,0,words.slice(k).join(" "))}vskip(parskip);blk_out();if(i<0)break;while(text[i+2]=="\n"){vskip(lineskip);i++}if(i==text.length)break;j=i+2}break}}function put_words(words){var p,i,j,n,nw,i2,i_end,have_text;function put_wline(p,x,right){var i=0,j,k;if(p[i]=="$"&&p[i+1]>="0"&&p[i+1]<="9")i+=2;k=0;j=i;if(p[i]>="0"&&p[i]<="9"||p[i+1]=="."){while(i<p.length){i++;if(p[i]==" "||p[i-1]==":"||p[i-1]==".")break}k=i;while(p[i]==" ")i++}if(k!=0)xy_str(x,0,p.slice(j,k),"r");if(i<p.length)xy_str(x+5,0,p.slice(i),"l");return i>=p.length&&k==0}blk_out();set_font("words");var middle=.5*(cfmt.pagewidth-cfmt.leftmargin-cfmt.rightmargin)/cfmt.scale,max2col=(middle-45)/(cwid("a")*gene.curfont.swfac);n=0;words=words.split("\n");nw=words.length;for(i=0;i<nw;i++){p=words[i];if(p.length>max2col){n=0;break}if(!p){if(have_text){n++;have_text=false}}else{have_text=true}}if(n>0){i=n=(n+1)/2|0;have_text=false;for(i_end=0;i_end<nw;i_end++){p=words[i_end];j=0;while(p[j]==" ")j++;if(j==p.length){if(have_text&&--i<=0)break;have_text=false}else{have_text=true}}i2=i_end+1}else{i2=i_end=nw}vskip(cfmt.wordsspace);for(i=0;i<i_end||i2<nw;i++){var desc=gene.curfont.size*.2;if(i<i_end&&words[i].length==0)blk_out();vskip(cfmt.lineskipfac*gene.curfont.size-desc);if(i<i_end)put_wline(words[i],45,0);if(i2<nw){if(put_wline(words[i2],20+middle,1)){if(--n==0){if(i<i_end){n++}else if(i2<words.length-1){middle*=.6}}}i2++}vskip(desc)}}function put_history(){var i,j,c,str,font,h,w,names=cfmt.infoname.split("\n"),n=names.length;for(i=0;i<n;i++){c=names[i][0];if(cfmt.writefields.indexOf(c)<0)continue;str=info[c];if(!str)continue;if(!font){font=true;set_font("history");vskip(cfmt.textspace);h=gene.curfont.size*cfmt.lineskipfac}head=names[i].slice(2);if(head[0]='"')head=head.slice(1,-1);vskip(h);xy_str(0,0,head);w=strw(head);str=str.split("\n");xy_str(w,0,str[0]);for(j=1;j<str.length;j++){vskip(h);xy_str(w,0,str[j])}vskip(h*.3);blk_out()}}var info_font_init={A:"info",C:"composer",O:"composer",P:"parts",Q:"tempo",R:"info",T:"title",X:"title"};function write_headform(lwidth){var c,font,font_name,align,x,y,sz,info_val={},info_font=clone(info_font_init),info_sz={A:cfmt.infospace,C:cfmt.composerspace,O:cfmt.composerspace,R:cfmt.infospace},info_nb={};var fmt="",p=cfmt.titleformat,j=0,i=0;while(1){while(p[i]==" ")i++;if(i>=p.length)break;c=p[i++];if(c<"A"||c>"Z"){if(c=="+"){if(fmt.length==0||fmt.slice(-1)=="+")continue;fmt=fmt.slice(0,-1)+"+"}else if(c==","){if(fmt.slice(-1)=="+")fmt=fmt.slice(0,-1)+"l";fmt+="\n"}continue}if(!info_val[c]){if(!info[c])continue;info_val[c]=info[c].split("\n");info_nb[c]=1}else{info_nb[c]++}fmt+=c;switch(p[i]){case"-":fmt+="l";i++;break;case"0":fmt+="c";i++;break;case"1":fmt+="r";i++;break;default:fmt+="c";break}}if(fmt.slice(-1)=="+")fmt=fmt.slice(0,-1)+"l";fmt+="\n";var ya={l:cfmt.titlespace,c:cfmt.titlespace,r:cfmt.titlespace},xa={l:0,c:lwidth*.5,r:lwidth},yb={},str;p=fmt;i=0;while(1){yb.l=yb.c=yb.r=y=0;j=i;while(1){c=p[j++];if(c=="\n")break;align=p[j++];if(align=="+")align=p[j+1];else if(yb[align]!=0)continue;str=info_val[c];if(!str)continue;font_name=info_font[c];if(!font_name)font_name="history";font=get_font(font_name);sz=font.size*1.1;if(info_sz[c])sz+=info_sz[c];if(y<sz)y=sz;yb[align]=sz}ya.l+=y-yb.l;ya.c+=y-yb.c;ya.r+=y-yb.r;while(1){c=p[i++];if(c=="\n")break;align=p[i++];if(info_val[c].length==0)continue;str=info_val[c].shift();if(align=="+"){info_nb[c]--;c=p[i++];align=p[i++];if(info_val[c].length>0){if(str)str+=" "+info_val[c].shift();else str=" "+info_val[c].shift()}}font_name=info_font[c];if(!font_name)font_name="history";font=get_font(font_name);sz=font.size*1.1;if(info_sz[c])sz+=info_sz[c];set_font(font_name);x=xa[align];y=ya[align]+sz;if(c=="Q"){if(!glovar.tempo.del){if(align!="l"){var w=tempo_width(glovar.tempo);if(align=="c")w*=.5;x-=w}write_tempo(glovar.tempo,x,-y)}}else if(str){xy_str(x,-y,str,align)}if(c=="T"){font_name=info_font.T="subtitle";info_sz.T=cfmt.subtitlespace}if(info_nb[c]<=1){if(c=="T"){font=get_font(font_name);sz=font.size*1.1;if(info_sz[c])sz+=info_sz[c];set_font(font_name)}while(info_val[c].length>0){y+=sz;str=info_val[c].shift();xy_str(x,-y,str,align)}}info_nb[c]--;ya[align]=y}if(ya.c>ya.l)ya.l=ya.c;if(ya.r>ya.l)ya.l=ya.r;if(i>=fmt.length)break;ya.c=ya.r=ya.l}vskip(ya.l)}function write_heading(){var i,j,area,composer,origin,rhythm,down1,down2,lwidth=(cfmt.pagewidth-cfmt.leftmargin-cfmt.rightmargin)/cfmt.scale;vskip(cfmt.topspace);if(cfmt.titleformat){write_headform(lwidth);vskip(cfmt.musicspace);return}if(info.T&&cfmt.writefields.indexOf("T")>=0){i=0;while(1){j=info.T.indexOf("\n",i);if(j<0){write_title(info.T.substring(i),i!=0);break}write_title(info.T.slice(i,j),i!=0);i=j+1}}set_font("composer");down1=down2=0;if(parse.ckey.k_bagpipe&&!cfmt.infoline&&cfmt.writefields.indexOf("R")>=0)rhythm=info.R;if(rhythm){xy_str(0,-cfmt.composerspace,rhythm);down1=cfmt.composerspace}area=info.A;if(cfmt.writefields.indexOf("C")>=0)composer=info.C;if(cfmt.writefields.indexOf("O")>=0)origin=info.O;if(composer||origin||cfmt.infoline){var xcomp,align;vskip(cfmt.composerspace);if(cfmt.aligncomposer<0){xcomp=0;align=" "}else if(cfmt.aligncomposer==0){xcomp=lwidth*.5;align="c"}else{xcomp=lwidth;align="r"}down2=down1;if(composer||origin){if(cfmt.aligncomposer>=0&&down1!=down2)vskip(down1-down2);i=0;while(1){vskip(gene.curfont.size);if(composer)j=composer.indexOf("\n",i);else j=-1;if(j<0){put_inf2r(xcomp,0,composer?composer.substring(i):null,origin,align);break}xy_str(xcomp,0,composer.slice(i,j),align);down1+=gene.curfont.size;i=j+1}if(down2>down1)vskip(down2-down1)}rhythm=rhythm?null:info.R;if((rhythm||area)&&cfmt.infoline){set_font("info");vskip(gene.curfont.size+cfmt.infospace);put_inf2r(lwidth,0,rhythm,area,"r");down1+=gene.curfont.size+cfmt.infospace}}else{down2=cfmt.composerspace}if(info.P&&cfmt.writefields.indexOf("P")>=0){set_font("parts");down1=cfmt.partsspace+gene.curfont.size-down1;if(down1>0)down2+=down1;if(down2>.01)vskip(down2);xy_str(0,0,info.P);down2=0}vskip(down2+cfmt.musicspace)}var output=[],style="\n.fill {fill: currentColor}\n.stroke {stroke: currentColor; fill: none}\ntext {white-space: pre}\n.music {font-family: music; font-size: 24px; fill: currentColor}\n.music text, .music tspan {fill:currentColor}",font_style="",posy=0,posx=cfmt.leftmargin/cfmt.scale,defined_glyph={},defs="",stv_g={
  scale:1,dy:0,st:-1,v:0,g:0},block={};const tgls={sgno:{x:-6,y:4,c:"&#xe047;"},coda:{x:-12,y:6,c:"&#xe048;"},tclef:{x:-8,y:0,c:"&#xe050;"},cclef:{x:-8,y:0,c:"&#xe05c;"},bclef:{x:-8,y:0,c:"&#xe062;"},pclef:{x:-6,y:0,c:"&#xe069;"},stclef:{x:-8,y:0,c:"&#xe07a;"},scclef:{x:-8,y:0,c:"&#xe07b;"},sbclef:{x:-7,y:0,c:"&#xe07c;"},csig:{x:0,y:0,c:"&#xe08a;"},ctsig:{x:0,y:0,c:"&#xe08b;"},HDD:{x:-7,y:0,c:"&#xe0a0;"},breve:{x:-6,y:0,c:"&#xe0a1;"},HD:{x:-5.2,y:0,c:"&#xe0a2;"},Hd:{x:-3.8,y:0,c:"&#xe0a3;"},hd:{x:-3.7,y:0,c:"&#xe0a4;"},srep:{x:-5,y:0,c:"&#xe101;"},dot:{x:-2,y:0,c:"&#xe1e7;"},"acc-1":{x:-3,y:0,c:"&#xe260;"},acc3:{x:-2,y:0,c:"&#xe261;"},acc1:{x:-3,y:0,c:"&#xe262;"},acc2:{x:-3,y:0,c:"&#xe263;"},pshhd:{x:-3,y:0,c:"&#xe263;"},"acc-2":{x:-3,y:0,c:"&#xe264;"},accent:{x:-3,y:0,c:"&#xe4a0;"},marcato:{x:-3,y:0,c:"&#xe4ac;"},hld:{x:-7,y:0,c:"&#xe4c0;"},r00:{x:-1.5,y:0,c:"&#xe4e1;"},r0:{x:-1.5,y:0,c:"&#xe4e2;"},r1:{x:-3.5,y:6,c:"&#xe4e3;"},r2:{x:-3.2,y:0,c:"&#xe4e4;"},r4:{x:-3,y:0,c:"&#xe4e5;"},r8:{x:-3,y:0,c:"&#xe4e6;"},r16:{x:-4,y:0,c:"&#xe4e7;"},r32:{x:-4,y:0,c:"&#xe4e8;"},r64:{x:-4,y:0,c:"&#xe4e9;"},r128:{x:-4,y:0,c:"&#xe4ea;"},mrest:{x:-10,y:0,c:"&#xe4ee;"},mrep:{x:-6,y:0,c:"&#xe500;"},mrep2:{x:-9,y:0,c:"&#xe501;"},turn:{x:-5,y:4,c:"&#xe567;"},umrd:{x:-7,y:2,c:"&#xe56c;"},lmrd:{x:-7,y:2,c:"&#xe56d;"},ped:{x:-10,y:0,c:"&#xe650;"},pedoff:{x:-5,y:0,c:"&#xe655;"},longa:{x:-6,y:0,c:"&#xe95c;"}};var glyphs={brace:'<text id="brace">&#xe000;</text>',ghd:'<g id="ghd" transform="translate(4.5,0) scale(0.66)">\n\t<text x="-3.7">&#xe0a4;</text>\n</g>',acc1_1_4:'<g id="acc1_1_4">\n\t<path d="m0 7.8v-15.4" class="stroke"/>\n\t<path class="fill" d="M-1.8 2.7l3.6 -1.1v2.2l-3.6 1.1v-2.2z\n\t\tM-1.8 -3.7l3.6 -1.1v2.2l-3.6 1.1v-2.2"/>\n</g>',acc1_3_4:'<g id="acc1_3_4">\n\t<path d="m-2.5 8.7v-15.4M0 7.8v-15.4M2.5 6.9v-15.4" class="stroke"/>\n\t<path class="fill" d="m-3.7 3.1l7.4 -2.2v2.2l-7.4 2.2v-2.2z\n\t\tM-3.7 -3.2l7.4 -2.2v2.2l-7.4 2.2v-2.2"/>\n</g>',"acc-1_1_4":'<g id="acc-1_1_4" transform="scale(-1,1)">\n\t<text x="-3">&#xe260;</text>\n</g>',"acc-1_3_4":'<g id="acc-1_3_4">\n    <path class="fill" d="m0.6 -2.7\n\tc-5.7 -3.1 -5.7 3.6 0 6.7c-3.9 -4 -4 -7.6 0 -5.8\n\tM1 -2.7c5.7 -3.1 5.7 3.6 0 6.7c3.9 -4 4 -7.6 0 -5.8"/>\n    <path d="m1.6 3.5v-13M0 3.5v-13" class="stroke" stroke-width=".6"/>\n</g>',turnx:'<g id="turnx">\n\t<text x="-5" y="-4">&#xe567;</text>\n\t<path class="stroke" d="m0 -1.5v-9"/>\n</g>',pfthd:'<g id="pfthd">\n\t<text x="-3">&#xe263;</text>\n\t<circle r="4" class="stroke"/>\n</g>',pmsig:'<path id="pmsig" class="stroke" stroke-width="0.8"\n\td="m0 -7a5 5 0 0 1 0 -10a5 5 0 0 1 0 10"/>',pMsig:'<g id="pMsig">\n\t<use xlink:href="#pmsig"/>\n\t<path class="fill" d="m0 -10a2 2 0 0 1 0 -4a2 2 0 0 1 0 4"/>\n</g>',imsig:'<path id="imsig" class="stroke" stroke-width="0.8"\n\td="m0 -7a5 5 0 1 1 0 -10"/>',iMsig:'<g id="iMsig">\n\t<use xlink:href="#imsig"/>\n\t<path class="fill" d="m0 -10a2 2 0 0 1 0 -4a2 2 0 0 1 0 4"/>\n</g>',hl:'<path id="hl" class="stroke" stroke-width="1" d="m-6 0h12"/>',hl1:'<path id="hl1" class="stroke" stroke-width="1" d="m-7 0h14"/>',hl2:'<path id="hl2" class="stroke" stroke-width="1" d="m-9 0h18"/>',ghl:'<path id="ghl" class="stroke" d="m-3.5 0h7"/>',rdots:'<g id="rdots" class="fill">\n\t<circle cx="0" cy="-9" r="1.2"/>\n\t<circle cx="0" cy="-15" r="1.2"/>\n</g>',grm:'<path id="grm" class="fill" d="m-5 -2.5\n\tc5 -8.5 5.5 4.5 10 -2\n\t-5 8.5 -5.5 -4.5 -10 2"/>',stc:'<circle id="stc" class="fill" cx="0" cy="-3" r="1.2"/>',sld:'<path id="sld" class="fill" d="m-7.2 4.8\n\tc1.8 .7 4.5 -.2 7.2 -4.8\n\t-2.1 5 -5.4 6.8 -7.6 6"/>',emb:'<path id="emb" class="stroke" stroke-width="1.2" stroke-linecap="round"\n\td="m-2.5 -3h5"/>',brth:'<text id="brth" y="-6" style="font:bold italic 30px serif">,</text>',roll:'<path id="roll" class="fill" d="m-6 0\n\tc0.4 -7.3 11.3 -7.3 11.7 0\n\t-1.3 -6 -10.4 -6 -11.7 0"/>',upb:'<path id="upb" class="stroke" d="m-2.6 -9.4\n\tl2.6 8.8\n\tl2.6 -8.8"/>',dnb:'<g id="dnb">\n\t<path d="M-3.2 -2v-7.2m6.4 0v7.2" class="stroke"/>\n\t<path d="M-3.2 -6.8v-2.4l6.4 0v2.4" class="fill"/>\n</g>',dplus:'<path id="dplus" class="stroke" stroke-width="1.7"\n\td="m0 -.5v-6m-3 3h6"/>',lphr:'<path id="lphr" class="stroke" stroke-width="1.2"\n\td="m0 0v18"/>',mphr:'<path id="mphr" class="stroke" stroke-width="1.2"\n\td="m0 0v12"/>',sphr:'<path id="sphr" class="stroke" stroke-width="1.2"\n\td="m0 0v6"/>',sfz:'<text id="sfz" x="-5" y="-7" style="font:italic 14px serif">\n\ts<tspan font-size="16" font-weight="bold">f</tspan>z</text>',trl:'<text id="trl" x="-2" y="-4"\n\tstyle="font:bold italic 16px serif">tr</text>',opend:'<circle id="opend" class="stroke"\n\tcx="0" cy="-3" r="2.5"/>',snap:'<path id="snap" class="stroke" d="m-3 -6\n\tc0 -5 6 -5 6 0\n\t0 5 -6 5 -6 0\n\tM0 -5v6"/>',thumb:'<path id="thumb" class="stroke" d="m-2.5 -7\n\tc0 -6 5 -6 5 0\n\t0 6 -5 6 -5 0\n\tM-2.5 -9v4"/>',wedge:'<path id="wedge" class="fill" d="m0 -1l-1.5 -5h3l-1.5 5"/>',ltr:'<path id="ltr" class="fill"\n\td="m0 -.4c2 -1.5 3.4 -1.9 3.9 .4\n\t0.2 .8 .7 .7 2.1 -.4\n\tv0.8c-2 1.5 -3.4 1.9 -3.9 -.4\n\t-.2 -.8 -.7 -.7 -2.1 .4z"/>',custos:'<g id="custos">\n\t<path class="fill" d="m-4 0l2 2.5 2 -2.5 2 2.5 2 -2.5\n\t\t-2 -2.5 -2 2.5 -2 -2.5 -2 2.5"/>\n\t<path class="stroke" d="m3.5 0l5 -7"/>\n</g>',oct:'<text id="oct" style="font:12px serif">8</text>',clearbg:'<filter id="clearbg">\n\t<feComposite in="SourceGraphic" result="comp"/>\n\t<feFlood flood-color="white" result="flood"/>\n\t<feMerge><feMergeNode in="flood"/><feMergeNode in="comp"/></feMerge>\n</filter>'};function def_use(gl){var i,j,g;if(defined_glyph[gl])return;defined_glyph[gl]=true;g=glyphs[gl];if(!g){error(1,null,"Unknown glyph: '$1'",gl);return}j=0;while(1){i=g.indexOf('xlink:href="#',j);if(i<0)break;i+=13;j=g.indexOf('"',i);def_use(g.slice(i,j))}defs+="\n"+g}function defs_add(text){var i,j,gl,tag,is,ie=0;while(1){is=text.indexOf("<",ie);if(is<0)break;if(text.slice(is,is+4)=="<!--"){ie=text.indexOf("-->",is+4);if(ie<0)break;continue}i=text.indexOf('id="',is);if(i<0)break;i+=4;j=text.indexOf('"',i);if(j<0)break;gl=text.slice(i,j);ie=text.indexOf(">",j);if(ie<0)break;if(text[ie-1]=="/"){ie++}else{i=text.indexOf(" ",is);if(i<0)break;tag=text.slice(is+1,i);ie=text.indexOf("</"+tag+">",ie);if(ie<0)break;ie+=3+tag.length}glyphs[gl]=text.slice(is,ie)}}function set_g(){if(stv_g.started){stv_g.started=false;output.push("</g>\n")}if(stv_g.scale==1&&!stv_g.color)return;output.push("<g ");if(stv_g.scale!=1){if(stv_g.st>=0)output.push(staff_tb[stv_g.st].scale_str);else output.push(voice_tb[stv_g.v].scale_str)}if(stv_g.color){if(stv_g.scale!=1)output.push(" ");output.push('style="color:'+stv_g.color+'"')}output.push(">\n");stv_g.started=true}function set_color(color){if(color==stv_g.color)return null;var old_color=stv_g.color;stv_g.color=color;set_g();return old_color}function set_sscale(st){var new_scale,dy;if(st!=stv_g.st&&stv_g.scale!=1)stv_g.scale=0;if(st>=0)new_scale=staff_tb[st].staffscale;else new_scale=1;if(st>=0&&new_scale!=1)dy=staff_tb[st].y;else dy=posy;if(new_scale==stv_g.scale&&dy==stv_g.dy)return;stv_g.scale=new_scale;stv_g.dy=dy;stv_g.st=st;set_g()}function set_scale(s){var new_scale=s.p_v.scale;if(new_scale==1){set_sscale(s.st);return}if(new_scale==stv_g.scale&&stv_g.dy==posy)return;stv_g.scale=new_scale;stv_g.dy=posy;stv_g.st=-1;stv_g.v=s.v;set_g()}function set_dscale(st,no_scale){if(st<0){stv_g.scale=1;output=staff_tb[0].output}else{stv_g.scale=no_scale?1:staff_tb[st].staffscale;output=stv_g.scale==1?staff_tb[st].output:staff_tb[st].sc_out}stv_g.st=st;stv_g.dy=0}function delayed_update(){var st,new_out,text;for(st=0;st<=nstaff;st++){if(staff_tb[st].sc_out.length!=0){output.push('<g transform="translate(0,'+(posy-staff_tb[st].y).toFixed(2)+") scale("+staff_tb[st].staffscale.toFixed(2)+')">\n');output.push(staff_tb[st].sc_out.join(""));output.push("</g>\n");staff_tb[st].sc_out=[]}if(staff_tb[st].output.length==0)continue;output.push('<g transform="translate(0,'+(-staff_tb[st].y).toFixed(2)+')">\n');output.push(staff_tb[st].output.join(""));output.push("</g>\n");staff_tb[st].output=[]}}const anno_type=["bar","clef","custos","","grace","key","meter","Zrest","note","part","rest","yspace","staves","Break","tempo","","block","remark"];function anno_out(s,t,f){if(s.istart==undefined)return;var type=s.type,h=s.ymx-s.ymn+4,wl=s.wl||2,wr=s.wr||2;if(s.grace)type=GRACE;f(t||anno_type[type],s.istart,s.iend,s.x-wl-2,staff_tb[s.st].y+s.ymn+h-2,wl+wr+4,h,s)}function a_start(s,t){anno_out(s,t,user.anno_start)}function a_stop(s,t){anno_out(s,t,user.anno_stop)}var anno_start=a_start,anno_stop=a_stop;function out_XYAB(str,x,y,a,b){x=sx(x);y=sy(y);output.push(str.replace(/X|Y|A|B|F|G/g,function(c){switch(c){case"X":return x.toFixed(2);case"Y":return y.toFixed(2);case"A":return a;case"B":return b;case"F":return a.toFixed(2);default:return b.toFixed(2)}}))}function g_open(x,y,rot,sx,sy){out_XYAB('<g transform="translate(X,Y',x,y);if(rot)output.push(") rotate("+rot.toFixed(2));if(sx){if(sy)output.push(") scale("+sx.toFixed(2)+", "+sy.toFixed(2));else output.push(") scale("+sx.toFixed(2))}output.push(')">\n');stv_g.g++}function g_close(){stv_g.g--;output.push("</g>\n")}function out_svg(str){output.push(str)}Abc.prototype.out_svg=out_svg;function sx(x){if(stv_g.g)return x;return(x+posx)/stv_g.scale}Abc.prototype.sx=sx;function sy(y){if(stv_g.g)return y;if(stv_g.scale==1)return posy-y;if(stv_g.st<0)return(posy-y)/stv_g.scale;return stv_g.dy-y}Abc.prototype.sy=sy;function out_sxsy(x,sep,y){x=sx(x);y=sy(y);output.push(x.toFixed(2)+sep+y.toFixed(2))}Abc.prototype.out_sxsy=out_sxsy;function xypath(x,y,fill){out_XYAB('<path class="A" d="mX Y\n',x,y,fill?"fill":"stroke")}Abc.prototype.xypath=xypath;function xygl(x,y,gl){var tgl=tgls[gl];if(tgl&&!glyphs[gl]){out_XYAB('<text x="X" y="Y">A</text>\n',x+tgl.x*stv_g.scale,y+tgl.y,tgl.c);return}def_use(gl);out_XYAB('<use x="X" y="Y" xlink:href="#A"/>\n',x,y,gl)}function out_acciac(x,y,dx,dy,up){if(up){x-=1;y+=4}else{x-=5;y-=4}out_XYAB('<path class="stroke" d="mX YlF G"/>\n',x,y,dx,-dy)}function out_bar(x,y,h){x+=posx;y=posy-y;output.push('<path class="stroke" stroke-width="1" d="m'+x.toFixed(2)+" "+y.toFixed(2)+"v"+(-h).toFixed(2)+'"/>\n')}function out_bnum(x,y,str,erase){if(erase){def_use("clearbg");erase=' filter="url(#clearbg)"'}else{erase=""}out_XYAB('<text style="font:italic 12px serif"\n\tx="X" y="Y" text-anchor="middle"B>A</text>\n',x,y,str.toString(),erase)}function out_brace(x,y,h){def_use("brace");x+=posx-6;y=posy-y;h/=24;output.push('<use transform="translate('+x.toFixed(2)+","+y.toFixed(2)+") scale(2.5,"+h.toFixed(2)+')" xlink:href="#brace"/>\n')}function out_bracket(x,y,h){x+=posx-5;y=posy-y-3;h+=2;output.push('<path class="fill"\n\td="m'+x.toFixed(2)+" "+y.toFixed(2)+"\n\tc10.5 1 12 -4.5 12 -3.5c0 1 -3.5 5.5 -8.5 5.5\n\tv"+h.toFixed(2)+'\n\tc5 0 8.5 4.5 8.5 5.5c0 1 -1.5 -4.5 -12 -3.5"/>\n')}function out_dotbar(x,y,h){x+=posx;y=posy-y;output.push('<path class="stroke" stroke-dasharray="5,5"\n\td="m'+x.toFixed(2)+" "+y.toFixed(2)+"v"+(-h).toFixed(2)+'"/>\n')}function out_hyph(x,y,w){var n,a_y,d=25+(w/20|0)*3;if(w>15)n=(w-15)/d|0;else n=0;x+=(w-d*n-5)/2;out_XYAB('<path class="stroke" stroke-width="1.2"\n\tstroke-dasharray="5,F"\n\td="mX YhG"/>\n',x,y+3,Math.round((d-5)/stv_g.scale),d*n+5)}function out_stem(x,y,h,grace,nflags,straight){var dx=grace?GSTEM_XOFF:3.5,slen=-h;if(h<0)dx=-dx;x+=dx*stv_g.scale;if(stv_g.st<0)slen/=stv_g.scale;out_XYAB('<path class="stroke" d="mX YvF"/>\n',x,y,slen);if(!nflags)return;output.push('<path class="fill"\n\td="');y+=h;if(h>0){if(!straight){if(!grace){if(nflags==1){out_XYAB("MX Yc0.6 5.6 9.6 9 5.6 18.4\n\t1.6 -6 -1.3 -11.6 -5.6 -12.8\n",x,y)}else{while(--nflags>=0){out_XYAB("MX Yc0.9 3.7 9.1 6.4 6 12.4\n\t1 -5.4 -4.2 -8.4 -6 -8.4\n",x,y);y-=5.4}}}else{if(nflags==1){out_XYAB("MX Yc0.6 3.4 5.6 3.8 3 10\n\t1.2 -4.4 -1.4 -7 -3 -7\n",x,y)}else{while(--nflags>=0){out_XYAB("MX Yc1 3.2 5.6 2.8 3.2 8\n\t1.4 -4.8 -2.4 -5.4 -3.2 -5.2\n",x,y);y-=3.5}}}}else{if(!grace){y+=1;while(--nflags>=0){out_XYAB("MX Yl7 3.2 0 3.2 -7 -3.2z\n",x,y);y-=5.4}}else{while(--nflags>=0){out_XYAB("MX Yl3 1.5 0 2 -3 -1.5z\n",x,y);y-=3}}}}else{if(!straight){if(!grace){if(nflags==1){out_XYAB("MX Yc0.6 -5.6 9.6 -9 5.6 -18.4\n\t1.6 6 -1.3 11.6 -5.6 12.8\n",x,y)}else{while(--nflags>=0){out_XYAB("MX Yc0.9 -3.7 9.1 -6.4 6 -12.4\n\t1 5.4 -4.2 8.4 -6 8.4\n",x,y);y+=5.4}}}else{if(nflags==1){out_XYAB("MX Yc0.6 -3.4 5.6 -3.8 3 -10\n\t1.2 4.4 -1.4 7 -3 7\n",x,y)}else{while(--nflags>=0){out_XYAB("MX Yc1 -3.2 5.6 -2.8 3.2 -8\n\t1.4 4.8 -2.4 5.4 -3.2 5.2\n",x,y);y+=3.5}}}}else{if(!grace){y+=1;while(--nflags>=0){out_XYAB("MX Yl7 -3.2 0 -3.2 -7 3.2z\n",x,y);y+=5.4}}}}output.push('"/>\n')}function out_thbar(x,y,h){x+=posx+1.5;y=posy-y;output.push('<path class="stroke" stroke-width="3" d="m'+x.toFixed(2)+" "+y.toFixed(2)+"v"+(-h).toFixed(2)+'"/>\n')}function out_trem(x,y,ntrem){out_XYAB('<path class="fill" d="mX Y\n\t',x-4.5,y);while(1){output.push("l9 -3v3l-9 3z");if(--ntrem<=0)break;output.push("m0 5.4")}output.push('"/>\n')}function out_tubr(x,y,dx,dy,up){var h=up?-3:3;y+=h;dx/=stv_g.scale;output.push('<path class="stroke" d="m');out_sxsy(x," ",y);output.push("v"+h.toFixed(2)+"l"+dx.toFixed(2)+" "+(-dy).toFixed(2)+"v"+(-h).toFixed(2)+'"/>\n')}function out_wln(x,y,w){out_XYAB('<path class="stroke" stroke-width="0.8" d="mX YhF"/>\n',x,y,w)}const deco_str_style={crdc:{dx:0,dy:5,style:'style="font:italic 14px serif"'},dacs:{dx:0,dy:3,style:'style="font:16px serif" text-anchor="middle"'},fng:{dx:0,dy:1,style:'style="font:8px Bookman" text-anchor="middle"'},pf:{dx:0,dy:5,style:'style="font:bold italic 16px serif"'}};function out_deco_str(x,y,name,str){var a,f,a_deco=deco_str_style[name];if(!a_deco){error(1,null,"no definition of $1",name);a_deco=deco_str_style.fng}x+=a_deco.dx;y+=a_deco.dy;out_XYAB('<text x="X" y="Y" A>',x,y,a_deco.style);set_font("annotation");out_str(str);output.push("</text>\n")}function out_arp(x,y,val){g_open(x,y,270);x=0;y=-4;val=Math.ceil(val/6);while(--val>=0){xygl(x,y,"ltr");x+=6}g_close()}function out_cresc(x,y,val,defl){x+=val;val=-val;out_XYAB('<path class="stroke"\n\td="mX YlA ',x,y+5,val);if(defl.nost)output.push("-2.2m0 -3.6l"+(-val).toFixed(2)+' -2.2"/>\n');else output.push("-4l"+(-val).toFixed(2)+' -4"/>\n')}function out_dim(x,y,val,defl){out_XYAB('<path class="stroke"\n\td="mX YlA ',x,y+5,val);if(defl.noen)output.push("-2.2m0 -3.6l"+(-val).toFixed(2)+' -2.2"/>\n');else output.push("-4l"+(-val).toFixed(2)+' -4"/>\n')}function out_ltr(x,y,val){y+=4;val=Math.ceil(val/6);while(--val>=0){xygl(x,y,"ltr");x+=6}}function out_8va(x,y,val,defl){if(!defl.nost){out_XYAB('<text x="X" y="Y" style="font:italic bold 12px serif">8<tspan dy="-4" style="font-size:10px">va</tspan></text>\n',x-8,y);x+=12;val-=12}else{val-=5}y+=6;out_XYAB('<path class="stroke" stroke-dasharray="6,6" d="mX YhA"/>\n',x,y,val);if(!defl.noen)out_XYAB('<path class="stroke" d="mX Yv6"/>\n',x+val,y)}function out_8vb(x,y,val,defl){if(!defl.nost){out_XYAB('<text x="X" y="Y" style="font:italic bold 12px serif">8<tspan dy="-4" style="font-size:10px">vb</tspan></text>\n',x-8,y);x+=4;val-=4}else{val-=5}out_XYAB('<path class="stroke" stroke-dasharray="6,6" d="mX YhA"/>\n',x,y,val);if(!defl.noen)out_XYAB('<path class="stroke" d="mX Yv-6"/>\n',x+val,y)}function out_15ma(x,y,val,defl){if(!defl.nost){out_XYAB('<text x="X" y="Y" style="font:italic bold 12px serif">15<tspan dy="-4" style="font-size:10px">ma</tspan></text>\n',x-10,y);x+=20;val-=20}else{val-=5}y+=6;out_XYAB('<path class="stroke" stroke-dasharray="6,6" d="mX YhA"/>\n',x,y,val);if(!defl.noen)out_XYAB('<path class="stroke" d="mX Yv6"/>\n',x+val,y)}function out_15mb(x,y,val,defl){if(!defl.nost){out_XYAB('<text x="X" y="Y" style="font:italic bold 12px serif">15<tspan dy="-4" style="font-size:10px">mb</tspan></text>\n',x-10,y);x+=7;val-=7}else{val-=5}out_XYAB('<path class="stroke" stroke-dasharray="6,6" d="mX YhA"/>\n',x,y,val);if(!defl.noen)out_XYAB('<path class="stroke" d="mX Yv-6"/>\n',x+val,y)}var deco_val_tb={arp:out_arp,cresc:out_cresc,dim:out_dim,ltr:out_ltr,"8va":out_8va,"8vb":out_8vb,"15ma":out_15ma,"15mb":out_15mb};function out_deco_val(x,y,name,val,defl){if(deco_val_tb[name])deco_val_tb[name](x,y,val,defl);else error(1,null,"No function for decoration '$1'",name)}function out_glisq(x2,y2,de){var de1=de.start,x1=de1.x,y1=de1.y+staff_tb[de1.st].y,ar=-Math.atan2(y2-y1,x2-x1),a=ar/Math.PI*180,len=(x2-x1)/Math.cos(ar);g_open(x1,y1,a);x1=de1.s.dots?13+de1.s.xmx:8;len=(len-x1-6)/6|0;if(len<1)len=1;while(--len>=0){xygl(x1,0,"ltr");x1+=6}g_close()}function out_gliss(x2,y2,de){var de1=de.start,x1=de1.x,y1=de1.y+staff_tb[de1.st].y,ar=-Math.atan2(y2-y1,x2-x1),a=ar/Math.PI*180,len=(x2-x1)/Math.cos(ar);g_open(x1,y1,a);x1=de1.s.dots?13+de1.s.xmx:8;len-=x1+8;xypath(x1,0);output.push("l"+len.toFixed(2)+' 0" stroke-width="1"/>\n');g_close()}var deco_l_tb={glisq:out_glisq,gliss:out_gliss};function out_deco_long(x,y,de){var name=de.dd.glyph;if(deco_l_tb[name])deco_l_tb[name](x,y,de);else error(1,null,"No function for decoration '$1'",name)}function vskip(h){posy+=h}function clean_txt(text){return text.replace(/<|>|&.*?;|&/g,function(c){switch(c){case"<":return"&lt;";case">":return"&gt;";default:if(c=="&")return"&amp;";return c}})}function svg_flush(){var img_title;if(multicol||!output||!user.img_out||posy==0)return;if(info.X){img_title=info.X+".";if(info.T)img_title+=" "+info.T.split("\n")[0];img_title=clean_txt(img_title)}else{img_title="noname"}posy*=cfmt.scale;if(user.imagesize){user.img_out('<svg xmlns="http://www.w3.org/2000/svg" version="1.1"\n\txmlns:xlink="http://www.w3.org/1999/xlink"\n\tcolor="black"\n'+user.imagesize+' viewBox="0 0 '+cfmt.pagewidth.toFixed(0)+" "+posy.toFixed(0)+'">\n<title>abc2svg - '+img_title+"</title>")}else{user.img_out('<svg xmlns="http://www.w3.org/2000/svg" version="1.1"\n\txmlns:xlink="http://www.w3.org/1999/xlink"\n\tcolor="black"\n\twidth="'+cfmt.pagewidth.toFixed(0)+'px" height="'+posy.toFixed(0)+'px">\n<title>abc2svg - '+img_title+"</title>")}if(style||font_style||musicfont){user.img_out('<style type="text/css">'+style+font_style);if(musicfont)user.img_out('@font-face {\n  font-family: "music";\n  src: '+musicfont+"}");user.img_out("</style>")}if(defs)user.img_out("<defs>"+defs+"\n</defs>");if(cfmt.bgcolor)user.img_out('<rect width="100%" height="100%" fill="'+cfmt.bgcolor+'"/>');if(cfmt.scale==1)user.img_out('<g class="music" stroke-width=".7">');else user.img_out('<g class="music" stroke-width=".7" transform="scale('+cfmt.scale.toFixed(2)+')">');if(svgobj){svgobj.setg(0);output.push(svgbuf);svgbuf=""}user.img_out(output.join("")+"</g>\n</svg>");output=[];font_style="";if(cfmt.fullsvg){defined_glyph={};defined_font={}}else{musicfont="";style="";defs=""}posy=0}var block_started;function blk_out(){if(multicol||!output||!user.img_out)return;if(user.page_format&&!block.started){block.started=true;if(block.newpage){block.newpage=false;user.img_out('<div class="nobrk newpage">')}else{user.img_out('<div class="nobrk">')}}svg_flush()}Abc.prototype.blk_out=blk_out;function blk_flush(){if(block.started&&user.img_out){block.started=false;user.img_out("</div>")}}Abc.prototype.blk_flush=blk_flush;var par_sy,cur_sy,voice_tb,curvoice,staves_found,vover,tsfirst;function voice_filter(){var opt,sel,tmp,i;for(opt in parse.voice_opts){sel=new RegExp(opt);if(sel.test(curvoice.id)||sel.test(curvoice.nm)){for(i=0;i<parse.voice_opts[opt].length;i++)do_pscom(parse.voice_opts[opt][i])}}}function sym_link(s){if(!curvoice.ignore){parse.last_sym=s;s.prev=curvoice.last_sym;if(curvoice.last_sym)curvoice.last_sym.next=s;else curvoice.sym=s;curvoice.last_sym=s}s.v=curvoice.v;s.p_v=curvoice;s.st=curvoice.cst;s.time=curvoice.time;if(s.dur&&!s.grace)curvoice.time+=s.dur;s.pos=curvoice.pos;if(curvoice.second)s.second=true;if(curvoice.floating)s.floating=true}function sym_add(p_voice,type){var s={type:type,dur:0},s2,p_voice2=curvoice;curvoice=p_voice;sym_link(s);curvoice=p_voice2;s2=s.prev;if(!s2)s2=s.next;if(s2){s.ctx=s2.ctx;s.istart=s2.istart;s.iend=s2.iend}return s}function mrest_expand(s){var p_voice,s2,next,nb=s.nmes,dur=s.dur/nb;var a_dd=s.a_dd;s.type=REST;s.dur=dur;s.head=FULL;s.nflags=-2;next=s.next;p_voice=s.p_v;p_voice.last_sym=s;p_voice.time=s.time+dur;p_voice.cst=s.st;s2=s;while(--nb>0){s2=sym_add(p_voice,BAR);s2.bar_type="|";s2=sym_add(p_voice,REST);if(s.invis)s2.invis=true;s2.dur=dur;s2.head=FULL;s2.nflags=-2;p_voice.time+=dur}s2.next=next;if(next)next.prev=s2;s2.a_dd=a_dd}const w_tb=[2,1,7,0,3,5,6,9,9,0,9,3,0,7,0,0,0,0];function sort_all(){var s,s2,p_voice,v,time,w,wmin,ir,multi,prev,new_sy,nb,nv=voice_tb.length,vtb=[],vn=[],mrest_time=-1;for(v=0;v<nv;v++)vtb.push(voice_tb[v].sym);var fl=1,sy=cur_sy,set_sy=true;while(1){if(set_sy){set_sy=false;multi=-1;vn=[];for(v=0;v<nv;v++){if(!sy.voices[v]){sy.voices[v]={range:-1};continue}ir=sy.voices[v].range;if(ir<0)continue;vn[ir]=v;multi++}}wmin=time=1e6;for(ir=0;ir<vn.length;ir++){v=vn[ir];if(v==undefined)break;s=vtb[v];if(!s||s.time>time)continue;w=w_tb[s.type];if(s.time<time){time=s.time;wmin=w}else if(w<wmin){wmin=w}if(s.type==MREST){if(s.nmes==1)mrest_expand(s);else if(multi>0)mrest_time=time}}if(wmin>127)break;if(time==mrest_time){nb=0;for(ir=0;ir<vn.length;ir++){v=vn[ir];if(v==undefined)break;s=vtb[v];if(!s||s.time!=time||w_tb[s.type]!=wmin)continue;if(s.type!=MREST){mrest_time=-1;break}if(nb==0){nb=s.nmes}else if(nb!=s.nmes){mrest_time=-1;break}}if(mrest_time<0){for(ir=0;ir<vn.length;ir++){v=vn[ir];if(v==undefined)break;s=vtb[v];if(s&&s.type==MREST)mrest_expand(s)}}}for(ir=0;ir<vn.length;ir++){v=vn[ir];if(v==undefined)break;s=vtb[v];if(!s||s.time!=time||w_tb[s.type]!=wmin)continue;if(s.type==STAVES){sy=sy.next;set_sy=true}if(fl){fl=0;s.seqst=true}s.ts_prev=prev;if(prev)prev.ts_next=s;else tsfirst=s;prev=s;vtb[v]=s.next}fl=wmin}if(!prev)return;if(prev.dur){p_voice=prev.p_v;p_voice.last_sym=prev;s=sym_add(p_voice,SPACE);s.time=prev.time+prev.dur;s.seqst=true;s.width=6;prev.ts_next=s;s.ts_prev=prev;while(1){delete prev.eoln;if(prev.seqst)break;prev=prev.ts_prev}}}function voice_adj(){var p_voice,s,s2,s3,v;function set_feathered_beam(s1){var s,s2,t,d,b,i,a,d=s1.dur,n=1;for(s=s1;s;s=s.next){if(s.beam_end||!s.next)break;n++}if(n<=1){delete s1.feathered_beam;return}s2=s;b=d/2;a=d/(n-1);t=s1.time;if(s1.feathered_beam>0){for(s=s1,i=n-1;s!=s2;s=s.next,i--){d=(a*i|0)+b;s.dur=d;s.time=t;t+=d}}else{for(s=s1,i=0;s!=s2;s=s.next,i++){d=(a*i|0)+b;s.dur=d;s.time=t;t+=d}}s.dur=s.time+s.dur-t;s.time=t}s=glovar.tempo;if(s&&staves_found<=0){v=par_sy.top_voice;p_voice=voice_tb[v];s.v=v;s.p_v=p_voice;s.st=p_voice.st;s.time=0;s.next=p_voice.sym;if(s.next)s.next.prev=s;p_voice.sym=s}for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(p_voice.ignore)p_voice.ignore=false;for(s=p_voice.sym;s;s=s.next){if(s.time>=staves_found)break}for(;s;s=s.next){switch(s.type){case CLEF:for(s2=s.prev;s2;s2=s2.prev){switch(s2.type){case BAR:break;case MREST:case NOTE:case REST:case SPACE:case STBRK:s2=undefined;break;default:continue}break}if(!s2)continue;s.next.prev=s.prev;s.prev.next=s.next;s.next=s2;s.prev=s2.prev;s.prev.next=s;s2.prev=s;continue;case GRACE:if(s.next&&s.next.type==BAR)s.time--;if(!cfmt.graceword)continue;for(s2=s.next;s2;s2=s2.next){switch(s2.type){case SPACE:continue;case NOTE:if(!s2.a_ly)break;s.a_ly=s2.a_ly;s2.a_ly=null;break}break}continue}if(s.feathered_beam)set_feathered_beam(s)}}}function dupl_voice(){var p_voice,p_voice2,s,s2,g,g2,v,i;for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];p_voice2=p_voice.clone;if(!p_voice2)continue;p_voice.clone=null;for(s=p_voice.sym;s;s=s.next){if(s.time>=staves_found)break}p_voice2.clef=clone(p_voice.clef);curvoice=p_voice2;for(;s;s=s.next){s2=clone(s);if(s.notes){s2.notes=[];for(i=0;i<=s.nhd;i++)s2.notes.push(clone(s.notes[i]))}sym_link(s2);if(p_voice2.second)s2.second=true;else delete s2.second;if(p_voice2.floating)s2.floating=true;else delete s2.floating;delete s2.a_ly;g=s2.extra;if(!g)continue;g2=clone(g);s2.extra=g2;s2=g2;s2.v=v;s2.p_v=p_voice;s2.st=p_voice2.st;for(g=g.next;g;g=g.next){g2=clone(g);if(g.notes){g2.notes=[];for(i=0;i<=g.nhd;i++)g2.notes.push(clone(g.notes[i]))}s2.next=g2;g2.prev=s2;s2=g2;s2.v=v;s2.p_v=p_voice;s2.st=p_voice2.st}}}}function new_syst(init){var st,v,sy_new={voices:[],staves:[],top_voice:0};if(init){cur_sy=par_sy=sy_new;return}for(v=0;v<voice_tb.length;v++){st=par_sy.voices[v].st;var sy_staff=par_sy.staves[st],p_voice=voice_tb[v];if(p_voice.stafflines!=undefined)sy_staff.stafflines=p_voice.stafflines;if(p_voice.staffscale)sy_staff.staffscale=p_voice.staffscale;sy_new.voices[v]=clone(par_sy.voices[v]);sy_new.voices[v].range=-1;delete sy_new.voices[v].second}for(st=0;st<par_sy.staves.length;st++){sy_new.staves[st]=clone(par_sy.staves[st]);sy_new.staves[st].flags=0;sy_new.staves[st].empty=true}par_sy.next=sy_new;par_sy=sy_new}function go_global_time(s,symsel){var s2,bar_time,seq;if(symsel.bar<=1){if(symsel.bar==1){for(s2=s;s2;s2=s2.ts_next){if(s2.type==BAR&&s2.time!=0)break}if(s2.time<voice_tb[cur_sy.top_voice].meter.wmeasure)s=s2}}else{for(;s;s=s.ts_next){if(s.type==BAR&&s.bar_num>=symsel.bar)break}if(!s)return null;if(symsel.seq!=0){seq=symsel.seq;for(s=s.ts_next;s;s=s.ts_next){if(s.type==BAR&&s.bar_num==symsel.bar){if(--seq==0)break}}if(!s)return null}}if(symsel.time==0)return s;bar_time=s.time+symsel.time;while(s.time<bar_time){s=s.ts_next;if(!s)return s}do{s=s.ts_prev}while(!s.seqst);return s}function do_clip(){var s,s2,sy,p_voice,v;s=tsfirst;if(clip_start.bar>0||clip_start.time>0){s=go_global_time(s,clip_start);if(!s){tsfirst=null;return}sy=cur_sy;for(s2=tsfirst;s2!=s;s2=s2.ts_next){switch(s2.type){case CLEF:s2.p_v.clef=s2;break;case KEY:s2.p_v.key=clone(s2.as.u.key);break;case METER:s2.p_v.meter=clone(s2.as.u.meter);break;case STAVES:sy=sy.next;break}}cur_sy=sy;for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];for(s2=s;s2;s2=s2.ts_next){if(s2.v==v){delete s2.prev;break}}p_voice.sym=s2}tsfirst=s;delete s.ts_prev}s=go_global_time(s,clip_end);if(!s)return;do{s=s.ts_next;if(!s)return}while(!s.seqst);for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];for(s2=s.ts_prev;s2;s2=s2.ts_prev){if(s2.v==v){delete s2.next;break}}if(!s2)p_voice.sym=null}delete s.ts_prev.ts_next}function set_bar_num(){var s,s2,s3,tim,v=cur_sy.top_voice,wmeasure=voice_tb[v].meter.wmeasure,bar_rep=gene.nbar;function do_break(){var s,i,m,n,d,t;for(i=0;i<glovar.break.length;i++){m=glovar.break[i].m;n=glovar.break[i].n;d=glovar.break[i].d;for(s=voice_tb[v].sym;s;s=s.next){if(s.type==BAR&&s.bar_num==m)break}if(!s)continue;if(n!=0){t=s.time+n/d;for(;s;s=s.next){if(s.time>t)break}if(!s)continue}s.eoln=true}}for(s=tsfirst;;s=s.ts_next){if(!s)return;switch(s.type){case METER:case CLEF:case KEY:case STBRK:continue;case BAR:if(s.bar_num){gene.nbar=s.bar_num;break}if(s.text&&!cfmt.contbarnb){if(s.text[0]=="1"){bar_rep=gene.nbar}else{gene.nbar=bar_rep;s.bar_num=gene.nbar}}break}break}var bar_time=s.time+wmeasure,bar_num=gene.nbar;for(;s;s=s.ts_next){switch(s.type){case CLEF:for(s2=s.ts_prev;s2;s2=s2.ts_prev){switch(s2.type){case BAR:if(s2.seqst)break;continue;case MREST:case NOTE:case REST:case SPACE:case STAVES:case STBRK:s2=undefined;break;default:continue}break}if(!s2)break;s.next.prev=s.prev;s.prev.next=s.next;s.ts_next.ts_prev=s.ts_prev;s.ts_prev.ts_next=s.ts_next;s.next=s2;s.prev=s2.prev;s.prev.next=s;s2.prev=s;s.ts_next=s2;s.ts_prev=s2.ts_prev;s.ts_prev.ts_next=s;s2.ts_prev=s;s=s2;break;case METER:wmeasure=s.wmeasure;if(s.time<bar_time)bar_time=s.time+wmeasure;break;case MREST:bar_num+=s.nmes-1;while(s.ts_next&&s.ts_next.type!=BAR)s=s.ts_next;break;case BAR:if(s.bar_num){bar_num=s.bar_num;if(s.time<bar_time){delete s.bar_num;break}}else{if(s.time<bar_time)break;bar_num++}tim=s.time;s2=s;do{if(s2.type==BAR&&s2.text&&!cfmt.contbarnb){if(s2.text[0]=="1")bar_rep=bar_num;else bar_num=bar_rep;break}s2=s2.next}while(s2&&s2.time==tim);s.bar_num=bar_num;bar_time=s.time+wmeasure;break}}if(glovar.break)do_break();if(cfmt.measurenb<0)gene.nbar=bar_num}function get_break(param){var a=param.split(" "),b,c,d,i,j,k,n;glovar.break=[];for(k=0;k<a.length;k++){b=a[k];i=b.indexOf(":");if(i<0){glovar.break.push({m:b,n:0,d:1});continue}j=b.indexOf("/");if(j<0){parse.line.error("'/' missing in %%break");break}d=parseInt(b.slice(j+1));if(isNaN(d)||d<=1){parse.line.error("Bad denominator in %%break");break}glovar.break.push({m:b.slice(0,i-1),n:b.slice(i+1,j-1),d:d})}}function get_map(text){if(!text)return;var i,note,notes,map,tmp,ns,a=info_split(text,2);if(a.length<3){parse.line.error("Not enough parameters in %%map");return}ns=a[1];if(ns.indexOf("octave,")==0||ns.indexOf("key,")==0){ns=ns.replace(/[,']+$/m,"").toLowerCase();if(ns[0]=="k")ns=ns.replace(/[_=^]+/,"")}else if(ns[0]=="*"||ns.indexOf("all")==0){ns="all"}else{tmp=new scanBuf;tmp.buffer=a[1];tmp.index=0;tmp.ctx=parse.ctx;note=parse_acc_pit(tmp);if(!note){parse.line.error("Bad note in %%map");return}ns="abcdefg"[(note.pit+77)%7];if(note.acc)ns=["__","_","","^","^^","="][note.acc+2]+ns;for(i=note.pit;i>=28;i-=7)ns+="'";for(i=note.pit;i<21;i+=7)ns+=","}notes=maps[a[0]];if(!notes)maps[a[0]]=notes={};map=notes[ns];if(!map)notes[ns]=map=[];if(!a[2])return;i=2;if(a[2].indexOf("=")<0){if(a[2][0]!="*"){tmp=new scanBuf;tmp.buffer=a[2];tmp.index=0;tmp.ctx=parse.ctx;map[1]=parse_acc_pit(tmp)}if(!a[3])return;i++;if(a[3].indexOf("=")<0){map[0]=a[3].split(",");i++}}for(;i<a.length;i++){switch(a[i]){case"heads=":map[0]=a[++i].split(",");break;case"print=":tmp=new scanBuf;tmp.buffer=a[++i];tmp.index=0;tmp.ctx=parse.ctx;map[1]=parse_acc_pit(tmp);break;case"color=":map[2]=a[++i];break}}}function get_midi(param){var chan,prog,a=param.split(/\s+/);switch(a[0]){case"program":if(a[2]){chan=a[1];prog=a[2]}else{chan=0;prog=a[1]}prog=parseInt(prog);if(isNaN(prog)||prog<1||prog>128){parse.line.error("Bad program in %%MIDI");return}prog--;if(curvoice)curvoice.instr=prog;else glovar.instr=prog;break}}function set_transp(){var s,transp,vtransp;if(curvoice.ckey.k_bagpipe||curvoice.ckey.k_drum)return;transp=(cfmt.transp||0)+(curvoice.transp||0)+(curvoice.shift||0);vtransp=curvoice.vtransp||0;if(transp==vtransp)return;curvoice.vtransp=transp;s=curvoice.sym;if(!s){curvoice.key=clone(curvoice.okey);key_transp(curvoice.key);curvoice.ckey=clone(curvoice.key);if(curvoice.key.k_none)curvoice.key.k_sf=0;return}while(1){if(s.type==KEY)break;if(!s.prev){s=curvoice.key;break}s=s.prev}key_transp(s);curvoice.ckey=clone(s);if(curvoice.key.k_none)s.k_sf=0}function set_ottava(dcn){if(cfmt.sound)return;switch(dcn){case"15ma(":curvoice.ottava=-14;break;case"8va(":curvoice.ottava=-7;break;case"8vb(":curvoice.ottava=7;break;case"15mb(":curvoice.ottava=14;break;case"15ma)":case"8va)":case"8vb)":case"15mb)":curvoice.ottava=0;break}}function do_pscom(text){var h1,val,s,cmd,param,n,k,b,lock=false;if(text.match(/ lock$/)){lock=true;text=text.slice(0,-5).trim()}cmd=text.match(/(\w|-)+/);if(!cmd)return;cmd=cmd[0];param=text.replace(cmd,"").trim();switch(cmd){case"break":get_break(param);return;case"center":if(parse.state>=2){s=new_block(cmd);s.text=cnv_escape(param);return}write_text(cnv_escape(param),"c");return;case"clef":if(parse.state>=2){if(parse.state==2)goto_tune();s=new_clef(param);if(s)get_clef(s)}return;case"clip":return;case"deco":deco_add(param);return;case"linebreak":set_linebreak(param);return;case"MIDI":get_midi(param);return;case"map":get_map(param);return;case"maxsysstaffsep":if(parse.state==3){par_sy.voices[curvoice.v].maxsep=get_unit(param);return}break;case"multicol":generate();switch(param){case"start":blk_out();multicol={posy:posy,maxy:posy,lmarg:cfmt.leftmargin,rmarg:cfmt.rightmargin,state:parse.state};break;case"new":if(!multicol){parse.line.error("%%multicol new without start");break}if(posy>multicol.maxy)multicol.maxy=posy;if(parse.state==3){s=new_block("leftmargin");s.param=multicol.lmarg.toFixed(2);s=new_block("rightmargin");s.param=multicol.rmarg.toFixed(2)}else{cfmt.leftmargin=multicol.lmarg;posx=cfmt.leftmargin/cfmt.scale;cfmt.rightmargin=multicol.rmarg}posy=multicol.posy;break;case"end":if(!multicol){parse.line.error("%%multicol end without start");
  break}if(posy<multicol.maxy)posy=multicol.maxy;if(parse.state==3){s=new_block("leftmargin");s.param=multicol.lmarg.toFixed(2);s=new_block("rightmargin");s.param=multicol.rmarg.toFixed(2)}else{cfmt.leftmargin=multicol.lmarg;posx=cfmt.leftmargin/cfmt.scale;cfmt.rightmargin=multicol.rmarg}multicol=undefined;blk_out();break;default:parse.line.error("Unknown keyword '$1' in %%multicol",param);break}return;case"musicfont":musicfont=param;return;case"ottava":if(parse.state!=3){if(parse.state!=2)return;goto_tune()}n=parseInt(param);if(isNaN(n)||n<-2||n>2){parse.line.error("Bad value for %%ottava");return}switch(curvoice.ottava){case 14:b="15mb)";break;case 7:b="8vb)";break;case-7:b="8va)";break;case-14:b="15ma)";break}if(b){if(!a_dcn)a_dcn=[];a_dcn.push(b);set_ottava(b)}switch(n){case-2:b="15mb(";break;case-1:b="8vb(";break;case 0:return;case 1:b="8va(";break;case 2:b="15ma(";break}if(!a_dcn)a_dcn=[];a_dcn.push(b);set_ottava(b);return;case"repbra":if(parse.state>=2){if(parse.state==2)goto_tune();curvoice.norepbra=!get_bool(param)}return;case"repeat":if(parse.state!=3)return;if(!curvoice.last_sym){parse.line.error("%%repeat cannot start a tune");return}if(!param.length){n=1;k=1}else{b=param.split(/\s+/);n=parseInt(b[0]);k=parseInt(b[1]);if(isNaN(n)||n<1||curvoice.last_sym.type==BAR&&n>2){parse.line.error("Incorrect 1st value in %%repeat");return}if(isNaN(k)){k=1}else{if(k<1){parse.line.error("Incorrect 2nd value in %%repeat");return}}}parse.repeat_n=curvoice.last_sym.type==BAR?n:-n;parse.repeat_k=k;return;case"sep":lwidth=cfmt.pagewidth-cfmt.leftmargin-cfmt.rightmargin;var h2,len;h1=h2=len=0;if(param){var values=param.split(/\s+/);h1=get_unit(values[0]);if(values[1]){h2=get_unit(values[1]);if(values[2])len=get_unit(values[2])}}if(h1<1)h1=14;if(h2<1)h2=h1;if(len<1)len=90;if(parse.state>=2){s=new_block(cmd);s.x=(lwidth-len)/2/cfmt.scale;s.l=len/cfmt.scale;s.sk1=h1;s.sk2=h2;return}vskip(h1);output.push('<path class="stroke"\n\td="M');out_sxsy((lwidth-len)/2/cfmt.scale," ",0);output.push("h"+(len/cfmt.scale).toFixed(2)+'"/>\n');vskip(h2);blk_out();return;case"setbarnb":val=parseInt(param);if(isNaN(val))parse.line.error("Bad %%setbarnb value");else if(parse.state>=2)glovar.new_nbar=val;else cfmt.measurefirst=val;return;case"staff":if(parse.state!=3){if(parse.state!=2)return;goto_tune()}val=parseInt(param);if(isNaN(val)){parse.line.error("Bad %%staff value '$1'",param);return}var st;if(param[0]=="+"||param[0]=="-")st=curvoice.cst+val;else st=val-1;if(st<0||st>nstaff){parse.line.error("Bad %%staff number $1 (cur $2, max $3)",st,curvoice.cst,nstaff);return}delete curvoice.floating;curvoice.cst=st;return;case"staffbreak":if(parse.state!=3){if(parse.state!=2)return;goto_tune()}s={type:STBRK,dur:0};if(param[0]>="0"&&param[0]<="9"){s.xmx=get_unit(param);if(param.slice(-1)=="f")s.stbrk_forced=true}else{s.xmx=14;if(param[0]=="f")s.stbrk_forced=true}sym_link(s);return;case"stafflines":val=get_st_lines(param);if(val==undefined)parse.line.error("Bad %%stafflines value");else set_v_param(cmd,val);return;case"staffscale":val=parseFloat(param);if(isNaN(val)||val<.3||val>2)parse.line.error("Bad %%staffscale value");else set_v_param(cmd,val);return;case"staves":case"score":if(parse.state==0)return;get_staves(cmd,param);return;case"sysstaffsep":if(parse.state==3){par_sy.voices[curvoice.v].sep=get_unit(param);return}break;case"text":if(parse.state>=2){s=new_block(cmd);s.text=cnv_escape(param);s.opt=cfmt.textoption;return}write_text(cnv_escape(param),cfmt.textoption);return;case"transpose":if(cfmt.sound)return;switch(parse.state){case 0:cfmt.transp=0;case 1:cfmt.transp=(cfmt.transp||0)+get_transp(param);return;case 2:goto_tune();break}for(s=curvoice.last_sym;s;s=s.prev){switch(s.type){case NOTE:s=clone(curvoice.okey);s.ctx=parse.ctx;s.istart=parse.istart;s.iend=parse.iend;break;case KEY:break;default:continue}}do_info("V",curvoice.id+" shift="+param);return;case"tune":return;case"user":set_user(param);return;case"voicecolor":if(parse.state!=3){if(parse.state!=2)return;goto_tune()}curvoice.color=param;return;case"vskip":val=get_unit(param);if(val<0){parse.line.error("%%vskip cannot be negative");return}if(parse.state>=2){s=new_block(cmd);s.sk=val;return}vskip(val);blk_out();return;case"newpage":case"leftmargin":case"rightmargin":case"pagescale":case"pagewidth":case"scale":if(parse.state==3){s=new_block(cmd);s.param=param;return}if(cmd=="newpage"){block.newpage=true;return}set_format(cmd,param,lock);switch(cmd){case"leftmargin":case"pagescale":case"scale":posx=cfmt.leftmargin/cfmt.scale;break}return}set_format(cmd,param,lock)}function do_begin_end(type,opt,text){var i,j,action,s;switch(type){default:if(wpsobj){wpsobj.parse(text);output.push(svgbuf)}break;case"js":eval(text);break;case"ml":if(parse.state>=2){s=new_block(type);s.text=text}else{svg_flush();user.img_out(text)}break;case"svg":j=0;while(1){i=text.indexOf('<style type="text/css">\n',j);if(i>=0){j=text.indexOf("</style>",i);if(j<0){parse.line.error("No </style> in %%beginsvg sequence");break}style+=text.slice(i+23,j);continue}i=text.indexOf("<defs>",j);if(i>=0){j=text.indexOf("</defs>",i);if(j<0){parse.line.error("No </defs> in %%beginsvg sequence");break}defs_add(text.slice(i+6,j));continue}break}break;case"text":action=get_textopt(opt);if(parse.state>=2){s=new_block(type);s.text=cnv_escape(text);s.opt=get_textopt(opt);break}write_text(cnv_escape(text),action);break}}function generate(){var v,p_voice;if(voice_tb.length==0)return;voice_adj();dupl_voice();sort_all();if(!tsfirst)return;set_bar_num();if(!tsfirst)return;if(user.get_abcmodel)user.get_abcmodel(tsfirst,voice_tb,anno_type,info);if(!user.img_out)return;output_music();for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];p_voice.time=0;p_voice.sym=p_voice.last_sym=null;p_voice.st=cur_sy.voices[v].st;p_voice.second=cur_sy.voices[v].second;delete p_voice.have_ly;p_voice.hy_st=0;delete p_voice.bar_start;delete p_voice.slur_st;delete p_voice.s_tie;delete p_voice.s_rtie}staves_found=0}function key_transp(s_key){var t=curvoice.vtransp/3|0,sf=(t&~1)+(t&1)*7+s_key.k_sf;switch((curvoice.vtransp+210)%3){case 1:sf=(sf+4+12*4)%12-4;break;case 2:sf=(sf+7+12*4)%12-7;break;default:sf=(sf+5+12*4)%12-5;break}s_key.k_sf=sf}function set_k_acc(s){var i,j,n,nacc,p_acc,accs=[],pits=[];if(s.k_sf>0){for(nacc=0;nacc<s.k_sf;nacc++){accs[nacc]=1;pits[nacc]=[26,23,27,24,21,25,22][nacc]}}else{for(nacc=0;nacc<-s.k_sf;nacc++){accs[nacc]=-1;pits[nacc]=[22,25,21,24,20,23,26][nacc]}}n=s.k_a_acc.length;for(i=0;i<n;i++){p_acc=s.k_a_acc[i];for(j=0;j<nacc;j++){if(pits[j]==p_acc.pit){accs[j]=p_acc.acc;break}}if(j==nacc){accs[j]=p_acc.acc;pits[j]=p_acc.pit;nacc++}}for(i=0;i<nacc;i++){p_acc=s.k_a_acc[i];if(!p_acc)p_acc=s.k_a_acc[i]={};p_acc.acc=accs[i];p_acc.pit=pits[i]}}function acc_same_pitch(pitch){var i,time;s=curvoice.last_sym;if(!s)return;time=s.time;for(;s;s=s.prev){switch(s.type){case BAR:if(s.time<time)return;while(1){s=s.prev;if(!s)return;if(s.type==NOTE){if(s.time+s.dur==time)break;return}if(s.time<time)return}for(i=0;i<=s.nhd;i++){if(s.notes[i].pit==pitch&&s.notes[i].ti1)return s.notes[i].acc}return;case NOTE:for(i=0;i<=s.nhd;i++){if(s.notes[i].pit==pitch)return s.notes[i].acc}break}}return}function get_staves(cmd,param){var s,p_voice,p_voice2,i,flags,v,vid,st,range,a_vf=parse_staves(param);if(!a_vf)return;if(voice_tb.length!=0){voice_adj();dupl_voice()}var maxtime=0,no_sym=true;for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(p_voice.time>maxtime)maxtime=p_voice.time;if(p_voice.sym)no_sym=false}if(no_sym||maxtime==0&&staves_found<0){for(v=0;v<par_sy.voices.length;v++)par_sy.voices[v].range=-1}else if(staves_found!=maxtime){for(v=0;v<par_sy.voices.length;v++){if(par_sy.voices[v].range>=0){curvoice=voice_tb[v];break}}curvoice.time=maxtime;s={type:STAVES,dur:0};sym_link(s);par_sy.nstaff=nstaff;new_syst()}staves_found=maxtime;for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];delete p_voice.second;delete p_voice.ignore;delete p_voice.floating}range=0;for(i=0;i<a_vf.length;i++){vid=a_vf[i][0];p_voice=new_voice(vid);p_voice.time=maxtime;v=p_voice.v;if(i==0)par_sy.top_voice=p_voice.v;if(par_sy.voices[v].range>=0){p_voice2=clone(p_voice);par_sy.voices[voice_tb.length]=clone(par_sy.voices[v]);v=voice_tb.length;p_voice2.v=v;p_voice2.sym=p_voice2.last_sym=null;p_voice2.time=maxtime;voice_tb.push(p_voice2);delete p_voice2.clone;while(p_voice.clone)p_voice=p_voice.clone;p_voice.clone=p_voice2;p_voice=p_voice2}a_vf[i][0]=p_voice;par_sy.voices[v].range=range++}if(cmd[1]=="t"){for(i=0;i<a_vf.length;i++){flags=a_vf[i][1];if(!(flags&(OPEN_BRACE|OPEN_BRACE2)))continue;if((flags&(OPEN_BRACE|CLOSE_BRACE))==(OPEN_BRACE|CLOSE_BRACE)||(flags&(OPEN_BRACE2|CLOSE_BRACE2))==(OPEN_BRACE2|CLOSE_BRACE2))continue;if(a_vf[i+1][1]!=0)continue;if(flags&OPEN_PARENTH||a_vf[i+2][1]&OPEN_PARENTH)continue;if(a_vf[i+2][1]&(CLOSE_BRACE|CLOSE_BRACE2)){a_vf[i+1][1]|=FL_VOICE}else if(a_vf[i+2][1]==0&&a_vf[i+3][1]&(CLOSE_BRACE|CLOSE_BRACE2)){a_vf[i][1]|=OPEN_PARENTH;a_vf[i+1][1]|=CLOSE_PARENTH;a_vf[i+2][1]|=OPEN_PARENTH;a_vf[i+3][1]|=CLOSE_PARENTH}}}st=-1;for(i=0;i<a_vf.length;i++){flags=a_vf[i][1];if((flags&(OPEN_PARENTH|CLOSE_PARENTH))==(OPEN_PARENTH|CLOSE_PARENTH)){flags&=~(OPEN_PARENTH|CLOSE_PARENTH);a_vf[i][1]=flags}p_voice=a_vf[i][0];if(flags&FL_VOICE){p_voice.floating=true;p_voice.second=true}else{st++;if(!par_sy.staves[st]){par_sy.staves[st]={stafflines:"|||||",staffscale:1}}par_sy.staves[st].flags=0}v=p_voice.v;p_voice.st=p_voice.cst=par_sy.voices[v].st=st;par_sy.staves[st].flags|=flags;if(flags&OPEN_PARENTH){p_voice2=p_voice;while(i<a_vf.length-1){p_voice=a_vf[++i][0];v=p_voice.v;if(a_vf[i][1]&MASTER_VOICE){p_voice2.second=true;p_voice2=p_voice}else{p_voice.second=true}p_voice.st=p_voice.cst=par_sy.voices[v].st=st;if(a_vf[i][1]&CLOSE_PARENTH)break}par_sy.staves[st].flags|=a_vf[i][1]}}if(st<0)st=0;par_sy.nstaff=nstaff=st;if(cmd[1]=="c"){for(st=0;st<=nstaff;st++)par_sy.staves[st].flags^=STOP_BAR}for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(par_sy.voices[v].range<0){p_voice.ignore=true;continue}par_sy.voices[v].second=p_voice.second;st=p_voice.st;if(st>0&&!p_voice.norepbra&&!(par_sy.staves[st-1].flags&STOP_BAR))p_voice.norepbra=true}if(parse.state>=2)curvoice=voice_tb[par_sy.top_voice];else curvoice=null}function get_vover(type){function clone_voice(id){var v,p_voice;for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(p_voice.id==id)return p_voice}p_voice=clone(curvoice);p_voice.v=voice_tb.length;p_voice.id=id;p_voice.sym=p_voice.last_sym=null;p_voice.nm=null;p_voice.snm=null;p_voice.lyric_restart=p_voice.lyric_restart=p_voice.lyric_cont=p_voice.ly_a_h=null;voice_tb.push(p_voice);return p_voice}var line=parse.line,p_voice2,p_voice3,range,v,v2,v3;if(curvoice.ignore)return;if(type=="|"||type==")"){curvoice.last_sym.beam_end=true;if(!vover){line.error("Erroneous end of voice overlap");return}if(curvoice.time!=vover.mxtime)line.error("Wrong duration in voice overlay");curvoice=vover.p_voice;vover=null;return}if(type=="("){if(vover){line.error("Voice overlay already started");return}vover={time:curvoice.time};return}if(!curvoice.last_note){line.error("No note before start of voice overlay");return}curvoice.last_note.beam_end=true;p_voice2=curvoice.voice_down;if(!p_voice2){p_voice2=clone_voice(curvoice.id+"o");curvoice.voice_down=p_voice2;p_voice2.time=0;p_voice2.second=true;v2=p_voice2.v;par_sy.voices[v2]={st:curvoice.st,second:true,scale:curvoice.scale,key:curvoice.key,ckey:curvoice.ckey,okey:curvoice.okey,pos:p_voice2.pos};var f_clone=curvoice.clone!=undefined?1:0;range=par_sy.voices[curvoice.v].range;for(v=0;v<par_sy.voices.length;v++){if(par_sy.voices[v].range>range)par_sy.voices[v].range+=f_clone+1}par_sy.voices[v2].range=range+1;if(f_clone){p_voice3=clone_voice(p_voice2.id+"c");p_voice3.second=true;v3=p_voice3.v;par_sy.voices[v3]={second:true,scale:curvoice.clone.scale,range:range+2};p_voice2.clone=p_voice3}}p_voice2.ulen=curvoice.ulen;p_voice2.dur_fact=curvoice.dur_fact;if(curvoice.uscale)p_voice2.uscale=curvoice.uscale;if(!vover){vover={bar:true,mxtime:curvoice.time,p_voice:curvoice};var time=p_voice2.time;for(s=curvoice.last_sym;;s=s.prev){if(s.type==BAR||s.time<=time)break}vover.time=s.time}else{if(!vover.p_voice){vover.mxtime=curvoice.time;vover.p_voice=curvoice}else if(curvoice.time!=vover.mxtime){line.error("Wrong duration in voice overlay")}}p_voice2.time=vover.time;curvoice=p_voice2}function is_voice_sig(){if(!curvoice.sym)return true;if(curvoice.time!=0)return false;for(s=curvoice.sym;s;s=s.next){switch(s.type){case TEMPO:case PART:break;default:return false}}return true}function get_clef(s){if(is_voice_sig()){curvoice.clef=s;return}s2=curvoice.last_sym;if(s2&&s2.prev&&s2.time==curvoice.time&&(s2.type==KEY&&!s2.k_none||s2.type==BAR)){for(s3=s2;s3.prev;s3=s3.prev){switch(s3.prev.type){case KEY:case BAR:continue}break}curvoice.last_sym=s3.prev;sym_link(s);s.next=s3;s3.prev=s;curvoice.last_sym=s2}else{sym_link(s)}s.clef_small=true}function get_key(kp){var v,p_voice,s,transp,s_key=kp[0],a=kp[1];if(s_key.k_sf&&!s_key.k_exp&&s_key.k_a_acc)set_k_acc(s_key);if(parse.state==1){if(s_key.k_sf==undefined&&!s_key.k_a_acc)s_key.k_sf=0;for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];p_voice.key=s_key;p_voice.okey=clone(s_key);p_voice.ckey=clone(s_key)}parse.okey=clone(s_key);parse.ckey=s_key;if(a.length!=0)memo_kv_parm("*",a);if(!glovar.ulen)glovar.ulen=BASE_LEN/8;parse.state=2;write_heading();reset_gen();gene.nbar=cfmt.measurefirst;return}if(a.length!=0)set_kv_parm(a);if(parse.state==2)goto_tune(true);transp=(cfmt.transp||0)+(curvoice.transp||0)+(curvoice.shift||0);if(s_key.k_sf==undefined){if(!s_key.k_a_acc&&!transp){delete s_key;return}s_key.k_sf=curvoice.okey.k_sf}curvoice.okey=clone(s_key);if(transp){curvoice.vtransp=transp;key_transp(s_key)}curvoice.ckey=s_key;if(is_voice_sig()){curvoice.key=clone(s_key);if(s_key.k_none)curvoice.key.k_sf=0;return}s_key.k_old_sf=curvoice.ckey.k_sf;s=curvoice.last_sym;if(s&&s.type==METER){curvoice.last_sym=s.prev;if(!curvoice.last_sym)curvoice.sym=null;sym_link(s_key);s_key.next=s;s.prev=s_key;curvoice.last_sym=s}else{sym_link(s_key)}}function new_voice(id){var p_voice,v,p_v_sav,n=voice_tb.length;if(n==1&&voice_tb[0].default){voice_tb[0].default=false;if(!voice_tb[0].last_sym){p_voice=voice_tb[0];p_voice.id=id;if(cfmt.transp&&parse.state>=2){p_v_sav=curvoice;curvoice=p_voice;set_transp();curvoice=p_v_sav}return p_voice}}for(v=0;v<n;v++){p_voice=voice_tb[v];if(p_voice.id==id)return p_voice}p_voice={v:v,id:id,pos:{dyn:0,gch:0,gst:0,orn:0,stm:0,voc:0,vol:0},scale:1,combine:0,ulen:glovar.ulen,dur_fact:1,key:clone(parse.ckey),ckey:clone(parse.ckey),okey:clone(parse.okey),meter:clone(glovar.meter),wmeasure:glovar.meter.wmeasure,clef:{type:CLEF,clef_auto:true,clef_type:"a",time:0},hy_st:0,instr:glovar.instr||0};voice_tb.push(p_voice);par_sy.voices[v]={range:-1};return p_voice}function init_tune(){nstaff=-1;voice_tb=[];curvoice=null;new_syst(true);staves_found=-1;gene={}}function get_voice(parm){var v,transp,vtransp,a=info_split(parm,1),vid=a.shift();if(parse.state<2){if(a.length!=0)memo_kv_parm(vid,a);if(parse.state==1)new_voice(vid);return}curvoice=new_voice(vid);set_kv_parm(a);if(parse.state==2)goto_tune();set_transp();v=curvoice.v;if(curvoice.time==undefined){curvoice.time=0;if(staves_found<0){curvoice.st=curvoice.cst=++nstaff;par_sy.nstaff=nstaff;par_sy.voices[v].st=nstaff;par_sy.voices[v].range=v;par_sy.staves[nstaff]={stafflines:"|||||",staffscale:1}}if(par_sy.voices[v].range<0){if(staves_found>=0)curvoice.ignore=true}}if(!curvoice.filtered&&!curvoice.ignore&&parse.voice_opts){curvoice.filtered=true;voice_filter()}}function goto_tune(is_K){var s,v,p_voice,transp;parse.state=3;if(voice_tb.length==0){curvoice=new_voice("1");curvoice.clef.istart=curvoice.key.istart;curvoice.clef.iend=curvoice.key.iend;curvoice.default=true}else if(!curvoice){curvoice=voice_tb[staves_found<0?0:par_sy.top_voice]}if(!curvoice.init&&!is_K){set_kv_parm([]);set_transp()}for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];p_voice.ulen=glovar.ulen;if(p_voice.key.k_bagpipe&&p_voice.pos.stm==0){p_voice.pos=clone(p_voice.pos);p_voice.pos.stm=SL_BELOW}}if(staves_found<0){nstaff=voice_tb.length-1;for(v=0;v<=nstaff;v++){p_voice=voice_tb[v];p_voice.time=0;p_voice.clef.time=0;p_voice.st=p_voice.cst=par_sy.voices[v].st=par_sy.voices[v].range=v;par_sy.staves[v]={stafflines:"|||||",staffscale:1}}par_sy.nstaff=nstaff}}function get_lyrics(text,cont){var s,word,p,i,j,ly;if(curvoice.ignore)return;if(curvoice.pos.voc!=SL_HIDDEN)curvoice.have_ly=true;if(cont){s=curvoice.lyric_cont;if(!s){parse.line.error("+: lyric without music");return}}else{set_font("vocal");if(curvoice.lyric_restart){curvoice.lyric_start=s=curvoice.lyric_restart;curvoice.lyric_restart=null;curvoice.lyric_line=0}else{curvoice.lyric_line++;s=curvoice.lyric_start}if(!s)s=curvoice.sym;if(!s){parse.line.error("w: without music");return}}p=text;i=0;while(1){while(p[i]==" "||p[i]=="\t")i++;if(!p[i])break;j=parse.istart+i+2;switch(p[i]){case"|":while(s&&s.type!=BAR)s=s.next;if(!s){parse.line.error("Not enough bar lines for lyric line");return}s=s.next;i++;continue;case"-":word="-\n";break;case"_":word="_\n";break;case"*":word=null;break;default:if(p[i]=="\\"&&i==p.length-1){curvoice.lyric_cont=s;return}word="";while(1){if(!p[i])break;switch(p[i]){case"_":case"*":case"|":i--;case" ":case"\t":break;case"~":word+=" ";i++;continue;case"-":word+="\n";break;case"\\":word+=p[++i];i++;continue;default:word+=p[i++];continue}break}break}while(s&&(s.type!=NOTE||s.grace))s=s.next;if(!s){parse.line.error("Too many words in lyric line");return}if(word&&s.pos.voc!=SL_HIDDEN){if(word.match(/^\$\d/)){if(word[1]=="0")set_font("vocal");else set_font("u"+word[1]);word=word.slice(2)}ly={t:word,font:gene.curfont,w:strw(word),istart:j,iend:j+word.length};if(!s.a_ly)s.a_ly=[];s.a_ly[curvoice.lyric_line]=ly}s=s.next;i++}curvoice.lyric_cont=s}function ly_width(s,wlw){var ly,sz,swfac,align,xx,w,i,j,k,shift,p,a_ly=s.a_ly;align=0;for(i=0;i<a_ly.length;i++){ly=a_ly[i];if(!ly)continue;p=ly.t;w=ly.w;swfac=ly.font.swfac;xx=w+2*cwid(" ")*swfac;if(s.type==GRACE){shift=s.wl}else if(p[0]>="0"&&p[0]<="9"&&p.length>2||p[1]==":"||p[0]=="("||p[0]==")"){if(p[0]=="("){sz=cwid("(")*swfac}else{j=p.indexOf(" ");gene.curfont=gene.deffont=ly.font;if(j>0)sz=strw(p.slice(0,j));else sz=w}shift=(w-sz+2*cwid(" ")*swfac)*.4;if(shift>20)shift=20;shift+=sz;if(ly.t[0]>="0"&&ly.t[0]<="9"){if(shift>align)align=shift}}else if(p=="-\n"||p=="_\n"){shift=0}else{shift=xx*.4;if(shift>20)shift=20}ly.shift=shift;if(wlw<shift)wlw=shift;xx-=shift;shift=2*cwid(" ")*swfac;for(k=s.next;k;k=k.next){switch(k.type){case NOTE:case REST:if(!k.a_ly||!k.a_ly[i]||k.a_ly[i].w==0)xx-=9;else if(k.a_ly[i].t=="-\n"||k.a_ly[i].t=="_\n")xx-=shift;else break;if(xx<=0)break;continue;case CLEF:case METER:case KEY:xx-=10;continue;default:xx-=5;break}break}if(xx>s.wr)s.wr=xx}if(align>0){for(i=0;i<a_ly.length;i++){ly=a_ly[i];if(ly&&ly.t[0]>="0"&&ly.t[0]<="9")ly.shift=align}}return wlw}function draw_lyric_line(p_voice,j,y){var l,p,lastx,w,s,s2,f,ly,lyl,hyflag,lflag,x0,font,shift,desc;if(p_voice.hy_st&1<<j){hyflag=true;p_voice.hy_st&=~(1<<j)}for(s=p_voice.sym;;s=s.next)if(s.type!=CLEF&&s.type!=KEY&&s.type!=METER)break;lastx=s.prev?s.prev.x:tsfirst.x;x0=0;for(;s;s=s.next){if(s.a_ly)ly=s.a_ly[j];else ly=null;if(!ly){switch(s.type){case REST:case MREST:if(lflag){out_wln(lastx+3,y,x0-lastx);lflag=false;lastx=s.x+s.wr}}continue}if(ly.font!=gene.curfont){gene.curfont=font=ly.font}p=ly.t;w=ly.w;shift=ly.shift;if(hyflag){if(p=="_\n"){p="-\n"}else if(p!="-\n"){out_hyph(lastx,y,s.x-shift-lastx);hyflag=false;lastx=s.x+s.wr}}if(lflag&&p!="_\n"){out_wln(lastx+3,y,x0-lastx+3);lflag=false;lastx=s.x+s.wr}if(p=="-\n"||p=="_\n"){if(x0==0&&lastx>s.x-18)lastx=s.x-18;if(p[0]=="-")hyflag=true;else lflag=true;x0=s.x-shift;continue}x0=s.x-shift;if(p.slice(-1)=="\n"){p=p.slice(0,-1);hyflag=true}if(user.anno_start||user.anno_stop){s2={st:s.st,istart:ly.istart,iend:ly.iend,x:x0,y:y,ymn:y,ymx:y+gene.curfont.size,wl:0,wr:w};anno_start(s2,"lyrics")}xy_str(x0,y,p);anno_stop(s2,"lyrics");lastx=x0+w}if(hyflag){hyflag=false;x0=realwidth-10;if(x0<lastx+10)x0=lastx+10;out_hyph(lastx,y,x0-lastx);if(cfmt.hyphencont)p_voice.hy_st|=1<<j}for(p_voice.s_next;s;s=s.next){if(s.type==NOTE){if(!s.a_ly)break;ly=s.a_ly[j];if(ly&&ly.t=="_\n"){lflag=true;x0=realwidth-15;if(x0<lastx+12)x0=lastx+12}break}}if(lflag){out_wln(lastx+3,y,x0-lastx+3);lflag=false}}function draw_lyrics(p_voice,nly,a_h,y,incr){var j,top,sc=staff_tb[p_voice.st].staffscale;set_font("vocal");if(incr>0){if(y>-cfmt.vocalspace)y=-cfmt.vocalspace;y+=a_h[0]/6;y*=sc;for(j=0;j<nly;j++){y-=a_h[j]*1.1;draw_lyric_line(p_voice,j,y)}return y/sc}top=staff_tb[p_voice.st].topbar+cfmt.vocalspace;if(y<top)y=top;y+=a_h[nly-1]/6;y*=sc;for(j=nly;--j>=0;){draw_lyric_line(p_voice,j,y);y+=a_h[j]*1.1}return y/sc}function draw_all_lyrics(){var p_voice,s,v,nly,i,x,y,w,a_ly,ly,lyst_tb=new Array(nstaff),h_tb=new Array(voice_tb.length),nly_tb=new Array(voice_tb.length),above_tb=new Array(voice_tb.length),rv_tb=new Array(voice_tb.length),top=0,bot=0,st=-1;for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(!p_voice.sym)continue;if(p_voice.st!=st){top=0;bot=0;st=p_voice.st}nly=0;if(p_voice.have_ly){if(!h_tb[v])h_tb[v]=[];for(s=p_voice.sym;s;s=s.next){a_ly=s.a_ly;if(!a_ly||!a_ly[0])continue;x=s.x;if(a_ly[0].w!=0){x-=a_ly[0].shift;w=a_ly[0].w}else{w=10}y=y_get(p_voice.st,1,x,w);if(top<y)top=y;y=y_get(p_voice.st,0,x,w);if(bot>y)bot=y;if(nly<a_ly.length)nly=a_ly.length;for(i=0;i<a_ly.length;i++){ly=a_ly[i];if(!ly)continue;if(!h_tb[v][i]||ly.font.size>h_tb[v][i])h_tb[v][i]=ly.font.size}}}else{y=y_get(p_voice.st,1,0,realwidth);if(top<y)top=y;y=y_get(p_voice.st,0,0,realwidth);if(bot>y)bot=y}if(!lyst_tb[st])lyst_tb[st]={};lyst_tb[st].top=top;lyst_tb[st].bot=bot;nly_tb[v]=nly;if(nly==0)continue;if(p_voice.pos.voc)above_tb[v]=p_voice.pos.voc==SL_ABOVE;else if(voice_tb[v+1]&&voice_tb[v+1].st==st&&voice_tb[v+1].have_ly)above_tb[v]=true;else above_tb[v]=false;if(above_tb[v])lyst_tb[st].a=true;else lyst_tb[st].b=true}i=0;for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(!p_voice.sym)continue;if(!p_voice.have_ly)continue;if(above_tb[v]){rv_tb[i++]=v;continue}st=p_voice.st;set_dscale(st,true);if(nly_tb[v]>0)lyst_tb[st].bot=draw_lyrics(p_voice,nly_tb[v],h_tb[v],lyst_tb[st].bot,1)}while(--i>=0){v=rv_tb[i];p_voice=voice_tb[v];st=p_voice.st;set_dscale(st,true);lyst_tb[st].top=draw_lyrics(p_voice,nly_tb[v],h_tb[v],lyst_tb[st].top,-1)}for(v=0;v<voice_tb.length;v++){p_voice=voice_tb[v];if(!p_voice.sym)continue;st=p_voice.st;if(lyst_tb[st].a){top=lyst_tb[st].top+2;for(s=p_voice.sym.next;s;s=s.next){if(s.a_ly){y_set(st,1,s.x-2,10,top)}}}if(lyst_tb[st].b){bot=lyst_tb[st].bot-2;if(nly_tb[p_voice.v]>0){for(s=p_voice.sym.next;s;s=s.next){if(s.a_ly){y_set(st,0,s.x-2,10,bot)}}}else{y_set(st,0,0,realwidth,bot)}}}}function parse_gchord(type){var c,text,gch,x_abs,y_abs,type,istart,iend,ann_font=get_font("annotation"),h_ann=ann_font.size,line=parse.line;function get_float(){var txt="";while(1){c=text[i++];if("1234567890.-".indexOf(c)<0)return parseFloat(txt);txt+=c}}istart=parse.bol+line.index;if(type.length>1){text=type.slice(1,-1);iend=istart+1}else{text="";while(1){c=line.next_char();if(!c){line.error("No end of guitar chord");return}if(c=='"')break;if(c=="\\"){text+=c;c=line.next_char()}text+=c}iend=parse.bol+line.index+1}if(curvoice.pos.gch==SL_HIDDEN)return;if(!a_gch)a_gch=[];i=0;type="g";while(1){c=text[i];if(!c)break;gch={text:"",istart:istart,iend:iend};switch(c){case"@":type=c;i++;x_abs=get_float();if(c!=","){line.error("',' lacking in annotation '@x,y'");y_abs=0}else{y_abs=get_float();if(c!=" ")i--}break;case"^":case"_":case"<":case">":i++;type=c;break}gch.type=type;if(type=="@"){gch.x=x_abs;gch.y=y_abs;y_abs-=h_ann}while(1){c=text[i];if(!c)break;switch(c){case"\\":c=text[++i];if(!c||c=="n")break;gch.text+="\\";default:gch.text+=c;i++;continue;case"&":while(1){gch.text+=c;c=text[++i];switch(c){default:continue;case";":case undefined:case"\\":break}break}if(c==";"){gch.text+=c;continue}break;case";":break}i++;break}a_gch.push(gch)}}const note_names="CDEFGAB",latin_names=["Do","Ré","Mi","Fa","Sol","La","Si"],acc_name=["bb","b","","#","##"];function gch_transp(s){var gch,p,i=0,i2=curvoice.ckey.k_sf-curvoice.okey.k_sf;function gch_tr1(p){var new_txt,l,n,i1,i3,i4,ix,a,ip,ip2,latin=0;switch(p[0]){case"A":n=5;break;case"B":n=6;break;case"C":n=0;break;case"D":if(p[1]=="o"){latin++;n=0;break}n=1;break;case"E":n=2;break;case"F":if(p[1]=="a")latin++;n=3;break;case"G":n=4;break;case"L":latin++;n=5;break;case"M":latin++;n=2;break;case"R":latin++;if(p[1]!="e")latin++;n=1;break;case"S":latin++;if(p[1]=="o"){latin++;n=4}else{n=6}break;case"/":latin--;break;default:return p}a=0;ip=latin+1;if(latin>=0){if(p[ip]=="#"){a++;ip++;if(p[ip]=="#"){a++;ip++}}else if(p[ip]=="b"){a--;ip++;if(p[ip]=="b"){a--;ip++}}else if(p[ip]=="="){ip++}i3=cde2fcg[n]+i2+a*7;i4=cgd2cde[(i3+16*7)%7];i1=(((i3+22)/7|0)+159)%5;if(latin==0)new_txt=note_names[i4]+acc_name[i1];else new_txt=latin_names[i4]+acc_name[i1]}else{new_txt=""}ip2=p.indexOf("/",ip);if(ip2<0)return new_txt+p.slice(ip);n=note_names.indexOf(p[++ip2]);if(n<0)return new_txt+p.slice(ip);new_txt+=p.slice(ip,ip2);a=0;if(p[++ip2]=="#"){a++;if(p[++ip2]=="#"){a++;ip2++}}else if(p[ip2]=="b"){a--;if(p[++ip2]=="b"){a--;ip2++}}i3=cde2fcg[n]+i2+a*7;i4=cgd2cde[(i3+16*7)%7];i1=(((i3+1+21)/7|0)+2-3+32*5)%5;return new_txt+note_names[i4]+acc_name[i1]+p.slice(ip2)}while(1){gch=s.a_gch[i++];if(!gch)return;if(gch.type=="g")break}p=gch.text;i=p.indexOf("\t");if(i>=0){i++;p=p.slice(0,i)+gch_tr1(p.slice(i))}gch.text=gch_tr1(p)}function gch_build(s){var gch,w,xspc,ix,pos=curvoice.pos.gch==SL_BELOW?-1:1,gch_font=get_font("gchord"),ann_font=get_font("annotation"),y_above=0,y_below=0,y_left=0,y_right=0,h_gch=gch_font.size,h_ann=ann_font.size,box=cfmt.gchordbox;s.a_gch=a_gch;a_gch=null;if(curvoice.vtransp)gch_transp(s);const GCHPRE=.4;for(ix=0;ix<s.a_gch.length;ix++){gch=s.a_gch[ix];if(gch.type=="g"){gch.text=gch.text.replace(/##|#|=|bb|b/g,function(x){switch(x){case"##":return"&#x1d12a;";case"#":return"♯";case"=":return"♮";case"b":return"♭"}return"&#x1d12b;"});gch.font=gch_font}else{gch.text=cnv_escape(gch.text);gch.font=ann_font;if(gch.type=="@"&&!user.anno_start)continue}gene.curfont=gch.font;w=strw(gch.text);gch.w=w;switch(gch.type){case"@":break;case"^":xspc=w*GCHPRE;if(xspc>8)xspc=8;gch.x=-xspc;y_above-=h_ann;gch.y=y_above;break;case"_":xspc=w*GCHPRE;if(xspc>8)xspc=8;gch.x=-xspc;y_below-=h_ann;gch.y=y_below;break;case"<":gch.x=-(w+6);y_left-=h_ann;gch.y=y_left;break;case">":gch.x=6;y_right-=h_ann;gch.y=y_right;break;default:gch.box=box;xspc=w*GCHPRE;if(xspc>8)xspc=8;gch.x=-xspc;if(pos<0){y_below-=h_gch;gch.y=y_below;if(box){y_below-=2;gch.y-=1}}else{y_above-=h_gch;gch.y=y_above;if(box){y_above-=2;gch.y-=1}}break}}y_left/=2;y_right/=2;for(ix=0;ix<s.a_gch.length;ix++){gch=s.a_gch[ix];switch(gch.type){case"^":gch.y-=y_above;break;case"<":gch.y-=y_left;break;case">":gch.y-=y_right;break;case"g":if(pos>0)gch.y-=y_above;break}}}function draw_gchord(s,gchy_min,gchy_max){var gch,gch2,text,ix,x,y,y2,i,j,hbox;var w=s.a_gch[0].w,y_above=y_get(s.st,1,s.x-2,w),y_below=y_get(s.st,0,s.x-2,w),n=s.a_gch.length,yav=s.yav||0;for(ix=0;ix<n;ix++){gch=s.a_gch[ix];if(gch.type!="g")continue;gch2=gch;if(gch.y<0)break}if(gch2){if(gch2.y>=0){if(y_above<gchy_max)y_above=gchy_max}else{if(y_below>gchy_min)y_below=gchy_min}}set_dscale(s.st,true);for(ix=0;ix<n;ix++){gch=s.a_gch[ix];use_font(gch.font);gene.curfont=gene.deffont=gch.font;h=gch.font.size;w=gch.w;x=s.x+gch.x;text=gch.text;switch(gch.type){case"_":y=gch.y+y_below;y_set(s.st,0,x,w,y-h*.2-2);break;case"^":y=gch.y+y_above;y_set(s.st,1,x,w,y+h*.8+2);break;case"<":if(s.notes[0].acc)x-=s.notes[0].shac;y=gch.y+yav;break;case">":x+=s.xmx;if(s.dots>0)x+=1.5+3.5*s.dots;y=gch.y+yav;break;default:hbox=gch.box?3:2;if(gch.y>=0){y=gch.y+y_above;y_set(s.st,true,x,w,y+h+hbox)}else{y=gch.y+y_below;y_set(s.st,false,x,w,y-hbox)}i=text.indexOf("\t");if(i>=0){x=realwidth;for(var next=s.next;next;next=next.next){switch(next.type){default:continue;case NOTE:case REST:case BAR:x=next.x;break}break}j=2;for(;;){i=text.indexOf("\t",i+1);if(i<0)break;j++}var expdx=(x-s.x)/j;x=s.x;y/=staff_tb[s.st].staffscale;if(user.anno_start)user.anno_start("gchord",gch.istart,gch.iend,x-2,y+h+2,w+4,h+4,s);i=0;j=i;for(;;){i=text.indexOf("\t",j);if(i<0)break;xy_str(x,y+h*.2,text.slice(j,i),"c");x+=expdx;j=i+1}xy_str(x,y+h*.2,text.slice(j),"c");if(user.anno_stop)user.anno_stop("gchord",gch.istart,gch.iend,s.x-2,y+h+2,w+4,h+4,s);continue}break;case"@":y=gch.y+yav;if(y>0){y2=y+h*.8+3;if(y2>staff_tb[s.st].ann_top)staff_tb[s.st].ann_top=y2}else{y2=y-h*.2;if(y2<staff_tb[s.st].ann_bot)staff_tb[s.st].ann_bot=y2}break}y*=staff_tb[s.st].staffscale;if(user.anno_start)user.anno_start("annot",gch.istart,gch.iend,x-2,y+h+2,w+4,h+4,s);if(gch.box)xy_str_b(x,y+h*.2,text);else xy_str(x,y+h*.2,text);if(user.anno_stop)user.anno_stop("annot",gch.istart,gch.iend,x-2,y+h+2,w+4,h+4,s)}}function psdeco(f,x,y,de){return false}function pshdeco(f,x,y,dd){return false}function psxygl(x,y,gl){return false}abc2svg_init();return this}
  