autowatch=0;mgraphics.init();mgraphics.relative_coords=0;mgraphics.autofill=0;var EDGE_PX=6;var peaks=[];var durSec=0;var barSec=2;var playSec=0;var preBars=4;var loopStart=-1;var loopLen=4;var gesture=null;var dragX0=0;var dragX1=0;var grabOffsetBars=0;var anchorBar=0;var prevStart=-1;var prevLen=1;function totalBars(){if(durSec>0&&barSec>0){var n=Math.round(durSec/barSec);return n>0?n:1}return preBars>0?preBars:1}function pk(){var a=arrayfromargs(arguments);durSec=a[0]>0?a[0]:0;barSec=a[1]>0?a[1]:2;peaks=a.slice(2);mgraphics.redraw()}function ph(sec){playSec=sec;mgraphics.redraw()}function sec(startBar,startS,endS){loopStart=startBar;if(startBar>=0&&barSec>0){var n=Math.round((endS-startS)/barSec);if(n>=1)loopLen=n}mgraphics.redraw()}function pre(bars){if(bars>=1)preBars=Math.round(bars);mgraphics.redraw()}function lst(b){loopStart=Math.round(b);mgraphics.redraw()}function llen(n){if(n>=1)loopLen=Math.round(n);mgraphics.redraw()}function pxPerBar(){var r=box.rect;return(r[2]-r[0])/totalBars()}function clampBar(b){var n=totalBars();return b<0?0:b>n?n:b}function onclick(x){var pb=pxPerBar();dragX0=x;dragX1=x;if(loopStart>=0){var lx=loopStart*pb;var rx=(loopStart+loopLen)*pb;if(Math.abs(x-lx)<=EDGE_PX){gesture="resize-l";anchorBar=loopStart+loopLen}else if(Math.abs(x-rx)<=EDGE_PX){gesture="resize-r";anchorBar=loopStart}else if(x>lx&&x<rx){gesture="move";grabOffsetBars=x/pb-loopStart}else{gesture="draw"}}else{gesture="draw"}prevStart=loopStart;prevLen=loopLen;updatePreview(x);mgraphics.redraw()}function updatePreview(x){var pb=pxPerBar();var n=totalBars();if(gesture==="draw"){if(Math.abs(x-dragX0)<3){prevStart=loopStart;prevLen=loopLen;return}var lo=Math.min(dragX0,x);var hi=Math.max(dragX0,x);var b0=clampBar(Math.floor(lo/pb));var b1=clampBar(Math.ceil(hi/pb));if(b1<=b0)b1=b0+1;prevStart=b0;prevLen=b1-b0}else if(gesture==="move"){var s=Math.round(x/pb-grabOffsetBars);if(s<0)s=0;if(s+loopLen>n)s=n-loopLen;prevStart=s;prevLen=loopLen}else if(gesture==="resize-l"){var b=clampBar(Math.round(x/pb));if(b>=anchorBar)b=anchorBar-1;prevStart=b;prevLen=anchorBar-b}else if(gesture==="resize-r"){var e=clampBar(Math.round(x/pb));if(e<=anchorBar)e=anchorBar+1;prevStart=anchorBar;prevLen=e-anchorBar}}function ondrag(x,y,button){dragX1=x;updatePreview(x);if(button){mgraphics.redraw();return}var was=gesture;gesture=null;if(was==="draw"&&Math.abs(dragX1-dragX0)<3){mgraphics.redraw();return}if(prevStart!==loopStart||prevLen!==loopLen){loopStart=prevStart;loopLen=prevLen;outlet(0,"loop",loopStart,loopLen)}mgraphics.redraw()}function ondblclick(){gesture=null;if(loopStart!==-1){loopStart=-1;outlet(0,"loop",-1,loopLen)}mgraphics.redraw()}function paint(){var r=box.rect;var w=r[2]-r[0];var h=r[3]-r[1];var n=totalBars();var pb=w/n;var x,i;mgraphics.set_source_rgba(.07,.07,.08,1);mgraphics.rectangle(0,0,w,h);mgraphics.fill();var live=durSec>0&&peaks.length>0;if(live){mgraphics.set_source_rgba(.55,.85,.6,.85);var nb=peaks.length;var bw=w/nb;for(i=0;i<nb;i++){var v=peaks[i];if(v>1)v=1;var hh=Math.max(1,v*(h-2));mgraphics.rectangle(i*bw,(h-hh)/2,Math.max(bw-.5,.5),hh)}mgraphics.fill()}else{mgraphics.set_source_rgba(1,1,1,.25);mgraphics.select_font_face("Arial");mgraphics.set_font_size(10);mgraphics.move_to(6,h-5);mgraphics.show_text("drag bars to loop · drag ends/middle to resize/move · double-click for full")}mgraphics.set_line_width(1);for(i=1;i<n;i++){x=i*pb;mgraphics.set_source_rgba(1,1,1,i%4===0?.45:.22);mgraphics.move_to(x,0);mgraphics.line_to(x,h);mgraphics.stroke()}mgraphics.select_font_face("Arial");mgraphics.set_font_size(8);mgraphics.set_source_rgba(1,1,1,.55);for(i=0;i<n;i+=4){mgraphics.move_to(i*pb+2.5,8.5);mgraphics.show_text(String(i+1))}if(gesture!==null&&prevStart>=0){drawBrace(prevStart*pb,(prevStart+prevLen)*pb,w,h,true)}else if(loopStart>=0){var s=Math.min(loopStart,n-1)*pb;var e=Math.min(loopStart+loopLen,n)*pb;drawBrace(s,e,w,h,false)}if(live){x=playSec/durSec*w;if(x>=0&&x<=w){mgraphics.set_source_rgba(1,.35,.25,.95);mgraphics.set_line_width(2);mgraphics.move_to(x,0);mgraphics.line_to(x,h);mgraphics.stroke()}}}function drawBrace(x0,x1,w,h,preview){mgraphics.set_source_rgba(0,0,0,preview?.3:.45);if(x0>0){mgraphics.rectangle(0,0,x0,h);mgraphics.fill()}if(x1<w){mgraphics.rectangle(x1,0,w-x1,h);mgraphics.fill()}mgraphics.set_source_rgba(.98,.82,.33,preview?.7:1);mgraphics.rectangle(x0,0,x1-x0,5);mgraphics.fill();mgraphics.rectangle(x0,0,2.5,h*.5);mgraphics.fill();mgraphics.rectangle(x1-2.5,0,2.5,h*.5);mgraphics.fill()}