From c7d5d51ca26f96fe7de244f927460b5700fd20de Mon Sep 17 00:00:00 2001 From: katspaugh Date: Sun, 2 Jul 2017 13:50:40 +0200 Subject: [PATCH] feature: added kali for preserving pitch when changing tempo (#149) --- Gruntfile.js | 1 + dist/plugin/wavesurfer.cursor.min.js | 2 +- .../wavesurfer.elan-wave-segment.min.js | 2 +- dist/plugin/wavesurfer.elan.min.js | 2 +- dist/plugin/wavesurfer.mediasession.min.js | 2 +- dist/plugin/wavesurfer.microphone.min.js | 2 +- dist/plugin/wavesurfer.minimap.min.js | 2 +- dist/plugin/wavesurfer.playlist.min.js | 2 +- dist/plugin/wavesurfer.regions.min.js | 2 +- dist/plugin/wavesurfer.spectrogram.min.js | 4 +- dist/plugin/wavesurfer.timeline.min.js | 4 +- dist/wavesurfer.js | 192 +++++++++++++----- dist/wavesurfer.min.js | 6 +- dist/wavesurfer.min.js.map | 2 +- src/kali.js | 12 ++ src/webaudio.js | 42 +++- 16 files changed, 198 insertions(+), 81 deletions(-) create mode 100644 src/kali.js diff --git a/Gruntfile.js b/Gruntfile.js index 6dc37f870..e3564c346 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -21,6 +21,7 @@ module.exports = function (grunt) { src: [ 'src/wavesurfer.js', 'src/util.js', + 'src/kali.js', 'src/webaudio.js', 'src/mediaelement.js', 'src/drawer.js', diff --git a/dist/plugin/wavesurfer.cursor.min.js b/dist/plugin/wavesurfer.cursor.min.js index 03ab7c508..991a4135d 100644 --- a/dist/plugin/wavesurfer.cursor.min.js +++ b/dist/plugin/wavesurfer.cursor.min.js @@ -1,3 +1,3 @@ -/*! wavesurfer.js 1.4.0 (Mon, 10 Apr 2017 08:55:36 GMT) +/*! wavesurfer.js 1.4.0 (Sun, 02 Jul 2017 11:49:04 GMT) * https://github.com/katspaugh/wavesurfer.js * @license BSD-3-Clause */!function(a,b){"function"==typeof define&&define.amd?define(["wavesurfer"],function(a){return b(a)}):"object"==typeof exports?module.exports=b(require("wavesurfer.js")):b(WaveSurfer)}(this,function(a){"use strict";a.Cursor={init:function(a){var b=this;this.wavesurfer=a,this.drawer=this.wavesurfer.drawer,this.wrapper=this.drawer.wrapper,this.wrapper.addEventListener("mousemove",function(a){b.updateCursorPosition(b.drawer.handleEvent(a))}),this.wrapper.addEventListener("mouseenter",function(a){b.showCursor()}),this.wrapper.addEventListener("mouseleave",function(a){b.hideCursor()}),this.cursor=this.wrapper.appendChild(this.drawer.style(document.createElement("wave"),{position:"absolute",zIndex:3,left:0,top:0,bottom:0,width:"0",display:"block",borderRightStyle:"solid",borderRightWidth:"1px",borderRightColor:"black",opacity:".25",pointerEvents:"none"}))},updateCursorPosition:function(a){var b=Math.round(this.drawer.width*a)/this.drawer.params.pixelRatio-1;this.drawer.style(this.cursor,{left:b+"px"})},showCursor:function(){this.drawer.style(this.cursor,{display:"block"})},hideCursor:function(){this.drawer.style(this.cursor,{display:"none"})}},a.util.extend(a.Cursor,a.Observer),a.enableCursor=function(){this.cursor||(this.cursor=Object.create(a.Cursor),this.cursor.init(this))}}); \ No newline at end of file diff --git a/dist/plugin/wavesurfer.elan-wave-segment.min.js b/dist/plugin/wavesurfer.elan-wave-segment.min.js index 7e7947ccd..813f98286 100644 --- a/dist/plugin/wavesurfer.elan-wave-segment.min.js +++ b/dist/plugin/wavesurfer.elan-wave-segment.min.js @@ -1,3 +1,3 @@ -/*! wavesurfer.js 1.4.0 (Mon, 10 Apr 2017 08:55:36 GMT) +/*! wavesurfer.js 1.4.0 (Sun, 02 Jul 2017 11:49:04 GMT) * https://github.com/katspaugh/wavesurfer.js * @license BSD-3-Clause */!function(a,b){"function"==typeof define&&define.amd?define(["wavesurfer"],function(a){return b(a)}):"object"==typeof exports?module.exports=b(require("wavesurfer.js")):b(WaveSurfer)}(this,function(a){"use strict";a.ELANWaveSegment={defaultParams:{waveSegmentWidth:200,waveSegmentPeaksPerSegment:200,waveSegmentHeight:30,waveSegmentRenderer:"Canvas",waveSegmentNormalizeTo:"whole",waveSegmentBarHeight:1,waveSegmentBorderWidth:1,pixelRatio:window.devicePixelRatio||screen.deviceXDPI/screen.logicalXDPI},ELAN:null,wavesurfer:null,waveSegments:[],maxPeak:0,init:function(b){switch(this.params=a.util.extend(this.defaultParams,b),this.ELAN=b.ELAN,this.wavesurfer=b.wavesurfer,this.waveSegments=[],this.maxPeak=0,this.params.waveSegmentNormalizeTo){case"segment":this.params.normalize=!0;break;case"whole":this.calculateMaxPeak(),this.params.noramlize=!1;break;default:this.params.normalize=!1}this.addSegmentColumn()},calculateMaxPeak:function(){var b=this.ELAN.renderedAlignable.length*this.params.waveSegmentWidth,c=this.wavesurfer.backend.getPeaks(b,0,b),d=a.util.max(c),e=a.util.min(c);this.maxPeak=-e>d?-e:d},addSegmentColumn:function(){var a=this.ELAN.container.getElementsByTagName("tr"),b=document.createElement("th");b.textContent="Wave",b.className="wavesurfer-wave",b.setAttribute("style","width: "+this.params.waveSegmentWidth+"px"),a[0].insertBefore(b,a[0].firstChild.nextSibling);for(var c=0;ca)c=0;else if(e=a&&(b=c,!0)}),b},getAnnotationNode:function(a){return document.getElementById("wavesurfer-alignable-"+a.id)}},a.util.extend(a.ELAN,a.Observer)}); \ No newline at end of file diff --git a/dist/plugin/wavesurfer.mediasession.min.js b/dist/plugin/wavesurfer.mediasession.min.js index dbe677102..14b36bebc 100644 --- a/dist/plugin/wavesurfer.mediasession.min.js +++ b/dist/plugin/wavesurfer.mediasession.min.js @@ -1,3 +1,3 @@ -/*! wavesurfer.js 1.4.0 (Mon, 10 Apr 2017 08:55:36 GMT) +/*! wavesurfer.js 1.4.0 (Sun, 02 Jul 2017 11:49:04 GMT) * https://github.com/katspaugh/wavesurfer.js * @license BSD-3-Clause */!function(a,b){"function"==typeof define&&define.amd?define(["wavesurfer"],function(a){return b(a)}):"object"==typeof exports?module.exports=b(require("wavesurfer.js")):b(WaveSurfer)}(this,function(a){"use strict";a.MediaSession={init:function(a){this.params=a;var b=this.wavesurfer=a.wavesurfer;if(!this.wavesurfer)throw new Error("No WaveSurfer instance provided");if("mediaSession"in navigator){this.metadata=this.params.metadata,this.update();var c=this;b.on("play",function(){c.update()}),navigator.mediaSession.setActionHandler("play",function(){b.play()}),navigator.mediaSession.setActionHandler("pause",function(){b.playPause()}),navigator.mediaSession.setActionHandler("seekbackward",function(){b.skipBackward()}),navigator.mediaSession.setActionHandler("seekforward",function(){b.skipForward()})}},update:function(){typeof MediaMetadata==typeof Function&&(navigator.mediaSession.metadata=new MediaMetadata(this.metadata))}},a.util.extend(a.MediaSession,a.Observer)}); \ No newline at end of file diff --git a/dist/plugin/wavesurfer.microphone.min.js b/dist/plugin/wavesurfer.microphone.min.js index 9e868b0ed..bace9adba 100644 --- a/dist/plugin/wavesurfer.microphone.min.js +++ b/dist/plugin/wavesurfer.microphone.min.js @@ -1,3 +1,3 @@ -/*! wavesurfer.js 1.4.0 (Mon, 10 Apr 2017 08:55:36 GMT) +/*! wavesurfer.js 1.4.0 (Sun, 02 Jul 2017 11:49:04 GMT) * https://github.com/katspaugh/wavesurfer.js * @license BSD-3-Clause */!function(a,b){"function"==typeof define&&define.amd?define(["wavesurfer"],function(a){return b(a)}):"object"==typeof exports?module.exports=b(require("wavesurfer.js")):b(WaveSurfer)}(this,function(a){"use strict";a.Microphone={init:function(a){this.params=a;this.wavesurfer=a.wavesurfer;if(!this.wavesurfer)throw new Error("No WaveSurfer instance provided");this.active=!1,this.paused=!1,this.reloadBufferFunction=this.reloadBuffer.bind(this);var b=function(a,b,c){var d=navigator.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia||navigator.msGetUserMedia;return d?new Promise(function(b,c){d.call(navigator,a,b,c)}):Promise.reject(new Error("getUserMedia is not implemented in this browser"))};void 0===navigator.mediaDevices&&(navigator.mediaDevices={}),void 0===navigator.mediaDevices.getUserMedia&&(navigator.mediaDevices.getUserMedia=b),this.constraints=this.params.constraints||{video:!1,audio:!0},this.bufferSize=this.params.bufferSize||4096,this.numberOfInputChannels=this.params.numberOfInputChannels||1,this.numberOfOutputChannels=this.params.numberOfOutputChannels||1,this.micContext=this.wavesurfer.backend.getAudioContext()},start:function(){navigator.mediaDevices.getUserMedia(this.constraints).then(this.gotStream.bind(this))["catch"](this.deviceError.bind(this))},togglePlay:function(){this.active?(this.paused=!this.paused,this.paused?this.pause():this.play()):this.start()},play:function(){this.paused=!1,this.connect()},pause:function(){this.paused=!0,this.disconnect()},stop:function(){this.active&&(this.stopDevice(),this.wavesurfer.empty())},stopDevice:function(){if(this.active=!1,this.disconnect(),this.stream){var a=this.detectBrowser();if(("chrome"===a.browser&&a.version>=45||"firefox"===a.browser&&a.version>=44||"edge"===a.browser)&&this.stream.getTracks)return void this.stream.getTracks().forEach(function(a){a.stop()});this.stream.stop()}},connect:function(){void 0!==this.stream&&(this.mediaStreamSource=this.micContext.createMediaStreamSource(this.stream),this.levelChecker=this.micContext.createScriptProcessor(this.bufferSize,this.numberOfInputChannels,this.numberOfOutputChannels),this.mediaStreamSource.connect(this.levelChecker),this.levelChecker.connect(this.micContext.destination),this.levelChecker.onaudioprocess=this.reloadBufferFunction)},disconnect:function(){void 0!==this.mediaStreamSource&&this.mediaStreamSource.disconnect(),void 0!==this.levelChecker&&(this.levelChecker.disconnect(),this.levelChecker.onaudioprocess=void 0)},reloadBuffer:function(a){this.paused||(this.wavesurfer.empty(),this.wavesurfer.loadDecodedBuffer(a.inputBuffer))},gotStream:function(a){this.stream=a,this.active=!0,this.play(),this.fireEvent("deviceReady",a)},destroy:function(a){this.paused=!0,this.stop()},deviceError:function(a){this.fireEvent("deviceError",a)},extractVersion:function(a,b,c){var d=a.match(b);return d&&d.length>=c&&parseInt(d[c],10)},detectBrowser:function(){var a={};return a.browser=null,a.version=null,a.minVersion=null,"undefined"!=typeof window&&window.navigator?navigator.mozGetUserMedia?(a.browser="firefox",a.version=this.extractVersion(navigator.userAgent,/Firefox\/([0-9]+)\./,1),a.minVersion=31,a):navigator.webkitGetUserMedia&&window.webkitRTCPeerConnection?(a.browser="chrome",a.version=this.extractVersion(navigator.userAgent,/Chrom(e|ium)\/([0-9]+)\./,2),a.minVersion=38,a):navigator.mediaDevices&&navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)?(a.browser="edge",a.version=this.extractVersion(navigator.userAgent,/Edge\/(\d+).(\d+)$/,2),a.minVersion=10547,a):(a.browser="Not a supported browser.",a):(a.browser="Not a supported browser.",a)}},a.util.extend(a.Microphone,a.Observer)}); \ No newline at end of file diff --git a/dist/plugin/wavesurfer.minimap.min.js b/dist/plugin/wavesurfer.minimap.min.js index 25eff70b5..7b41050bb 100644 --- a/dist/plugin/wavesurfer.minimap.min.js +++ b/dist/plugin/wavesurfer.minimap.min.js @@ -1,3 +1,3 @@ -/*! wavesurfer.js 1.4.0 (Mon, 10 Apr 2017 08:55:36 GMT) +/*! wavesurfer.js 1.4.0 (Sun, 02 Jul 2017 11:49:04 GMT) * https://github.com/katspaugh/wavesurfer.js * @license BSD-3-Clause */!function(a,b){"function"==typeof define&&define.amd?define(["wavesurfer"],function(a){return b(a)}):"object"==typeof exports?module.exports=b(require("wavesurfer.js")):b(WaveSurfer)}(this,function(a){"use strict";a.Minimap=a.util.extend({},a.Drawer,a.Drawer.Canvas,{init:function(b,c){this.wavesurfer=b,this.container=this.wavesurfer.drawer.container,this.lastPos=this.wavesurfer.drawer.lastPos,this.params=b.util.extend({},this.wavesurfer.drawer.params,{showRegions:!1,showOverview:!1,overviewBorderColor:"green",overviewBorderSize:2},c,{scrollParent:!1,fillParent:!0}),this.width=0,this.height=this.params.height*this.params.pixelRatio,this.createWrapper(),this.createElements(),a.Regions&&this.params.showRegions&&this.regions(),this.bindWaveSurferEvents(),this.bindMinimapEvents()},regions:function(){var a=this;this.regions={},this.wavesurfer.on("region-created",function(b){a.regions[b.id]=b,a.renderRegions()}),this.wavesurfer.on("region-updated",function(b){a.regions[b.id]=b,a.renderRegions()}),this.wavesurfer.on("region-removed",function(b){delete a.regions[b.id],a.renderRegions()})},renderRegions:function(){for(var a=this,b=this.wrapper.querySelectorAll("region"),c=0;c1||a.target.childElementCount>0||(e=a.targetTouches?a.targetTouches[0].identifier:null,b=!0,c=f.wavesurfer.drawer.handleEvent(a,!0),d=null)};this.wrapper.addEventListener("mousedown",i),this.wrapper.addEventListener("touchstart",i),this.on("disable-drag-selection",function(){f.wrapper.removeEventListener("touchstart",i),f.wrapper.removeEventListener("mousedown",i)});var j=function(a){a.touches&&a.touches.length>1||(b=!1,h=0,d&&(d.fireEvent("update-end",a),f.wavesurfer.fireEvent("region-update-end",d,a)),d=null)};this.wrapper.addEventListener("mouseup",j),this.wrapper.addEventListener("touchend",j),this.on("disable-drag-selection",function(){f.wrapper.removeEventListener("touchend",j),f.wrapper.removeEventListener("mouseup",j)});var k=function(i){if(b&&!(++h<=g||i.touches&&i.touches.length>1||i.targetTouches&&i.targetTouches[0].identifier!=e)){d||(d=f.add(a||{}));var j=f.wavesurfer.getDuration(),k=f.wavesurfer.drawer.handleEvent(i);d.update({start:Math.min(k*j,c*j),end:Math.max(k*j,c*j)})}};this.wrapper.addEventListener("mousemove",k),this.wrapper.addEventListener("touchmove",k),this.on("disable-drag-selection",function(){f.wrapper.removeEventListener("touchmove",k),f.wrapper.removeEventListener("mousemove",k)})},disableDragSelection:function(){this.fireEvent("disable-drag-selection")}},a.util.extend(a.Regions,a.Observer),a.Region={style:a.Drawer.style,init:function(b,c){this.wavesurfer=c,this.wrapper=c.drawer.wrapper,this.id=null==b.id?a.util.getId():b.id,this.start=Number(b.start)||0,this.end=null==b.end?this.start+4/this.wrapper.scrollWidth*this.wavesurfer.getDuration():Number(b.end),this.resize=void 0===b.resize||Boolean(b.resize),this.drag=void 0===b.drag||Boolean(b.drag),this.loop=Boolean(b.loop),this.color=b.color||"rgba(0, 0, 0, 0.1)",this.data=b.data||{},this.attributes=b.attributes||{},this.maxLength=b.maxLength,this.minLength=b.minLength,this.bindInOut(),this.render(),this.onZoom=this.updateRender.bind(this),this.wavesurfer.on("zoom",this.onZoom),this.wavesurfer.fireEvent("region-created",this)},update:function(a){null!=a.start&&(this.start=Number(a.start)),null!=a.end&&(this.end=Number(a.end)),null!=a.loop&&(this.loop=Boolean(a.loop)),null!=a.color&&(this.color=a.color),null!=a.data&&(this.data=a.data),null!=a.resize&&(this.resize=Boolean(a.resize)),null!=a.drag&&(this.drag=Boolean(a.drag)),null!=a.maxLength&&(this.maxLength=Number(a.maxLength)),null!=a.minLength&&(this.minLength=Number(a.minLength)),null!=a.attributes&&(this.attributes=a.attributes),this.updateRender(),this.fireEvent("update"),this.wavesurfer.fireEvent("region-updated",this)},remove:function(){this.element&&(this.wrapper.removeChild(this.element),this.element=null,this.wavesurfer.un("zoom",this.onZoom),this.fireEvent("remove"),this.wavesurfer.fireEvent("region-removed",this))},play:function(){this.wavesurfer.play(this.start,this.end),this.fireEvent("play"),this.wavesurfer.fireEvent("region-play",this)},playLoop:function(){this.play(),this.once("out",this.playLoop.bind(this))},render:function(){var a=document.createElement("region");a.className="wavesurfer-region",a.title=this.formatTime(this.start,this.end),a.setAttribute("data-id",this.id);for(var b in this.attributes)a.setAttribute("data-region-"+b,this.attributes[b]);this.wrapper.scrollWidth;if(this.style(a,{position:"absolute",zIndex:2,height:"100%",top:"0px"}),this.resize){var c=a.appendChild(document.createElement("handle")),d=a.appendChild(document.createElement("handle"));c.className="wavesurfer-handle wavesurfer-handle-start",d.className="wavesurfer-handle wavesurfer-handle-end";var e={cursor:"col-resize",position:"absolute",left:"0px",top:"0px",width:"1%",maxWidth:"4px",height:"100%"};this.style(c,e),this.style(d,e),this.style(d,{left:"100%"})}this.element=this.wrapper.appendChild(a),this.updateRender(),this.bindEvents(a)},formatTime:function(a,b){return(a==b?[a]:[a,b]).map(function(a){return[Math.floor(a%3600/60),("00"+Math.floor(a%60)).slice(-2)].join(":")}).join("-")},getWidth:function(){return this.wavesurfer.drawer.width/this.wavesurfer.params.pixelRatio},updateRender:function(){var a=this.wavesurfer.getDuration(),b=this.getWidth();if(this.start<0&&(this.start=0,this.end=this.end-this.start),this.end>a&&(this.end=a,this.start=a-(this.end-this.start)),null!=this.minLength&&(this.end=Math.max(this.start+this.minLength,this.end)),null!=this.maxLength&&(this.end=Math.min(this.start+this.maxLength,this.end)),null!=this.element){var c=Math.round(this.start/a*b),d=Math.round(this.end/a*b)-c;this.style(this.element,{left:c+"px",width:d+"px",backgroundColor:this.color,cursor:this.drag?"move":"default"});for(var e in this.attributes)this.element.setAttribute("data-region-"+e,this.attributes[e]);this.element.title=this.formatTime(this.start,this.end)}},bindInOut:function(){var a=this;a.firedIn=!1,a.firedOut=!1;var b=function(b){!a.firedOut&&a.firedIn&&(a.start>=Math.round(100*b)/100||a.end<=Math.round(100*b)/100)&&(a.firedOut=!0,a.firedIn=!1,a.fireEvent("out"),a.wavesurfer.fireEvent("region-out",a)),!a.firedIn&&a.start<=b&&a.end>b&&(a.firedIn=!0,a.firedOut=!1,a.fireEvent("in"),a.wavesurfer.fireEvent("region-in",a))};this.wavesurfer.backend.on("audioprocess",b),this.on("remove",function(){a.wavesurfer.backend.un("audioprocess",b)}),this.on("out",function(){a.loop&&a.wavesurfer.play(a.start)})},bindEvents:function(){var a=this;this.element.addEventListener("mouseenter",function(b){a.fireEvent("mouseenter",b),a.wavesurfer.fireEvent("region-mouseenter",a,b)}),this.element.addEventListener("mouseleave",function(b){a.fireEvent("mouseleave",b),a.wavesurfer.fireEvent("region-mouseleave",a,b)}),this.element.addEventListener("click",function(b){b.preventDefault(),a.fireEvent("click",b),a.wavesurfer.fireEvent("region-click",a,b)}),this.element.addEventListener("dblclick",function(b){b.stopPropagation(),b.preventDefault(),a.fireEvent("dblclick",b),a.wavesurfer.fireEvent("region-dblclick",a,b)}),(this.drag||this.resize)&&function(){var b,c,d,e,f=a.wavesurfer.getDuration(),g=function(g){g.touches&&g.touches.length>1||(e=g.targetTouches?g.targetTouches[0].identifier:null,g.stopPropagation(),d=a.wavesurfer.drawer.handleEvent(g,!0)*f,"handle"==g.target.tagName.toLowerCase()?c=g.target.classList.contains("wavesurfer-handle-start")?"start":"end":(b=!0,c=!1))},h=function(d){d.touches&&d.touches.length>1||(b||c)&&(b=!1,c=!1,a.fireEvent("update-end",d),a.wavesurfer.fireEvent("region-update-end",a,d))},i=function(g){if(!(g.touches&&g.touches.length>1)&&(!g.targetTouches||g.targetTouches[0].identifier==e)&&(b||c)){var h=a.wavesurfer.drawer.handleEvent(g)*f,i=h-d;d=h,a.drag&&b&&a.onDrag(i),a.resize&&c&&a.onResize(i,c)}};a.element.addEventListener("mousedown",g),a.element.addEventListener("touchstart",g),a.wrapper.addEventListener("mousemove",i),a.wrapper.addEventListener("touchmove",i),document.body.addEventListener("mouseup",h),document.body.addEventListener("touchend",h),a.on("remove",function(){document.body.removeEventListener("mouseup",h),document.body.removeEventListener("touchend",h),a.wrapper.removeEventListener("mousemove",i),a.wrapper.removeEventListener("touchmove",i)}),a.wavesurfer.on("destroy",function(){document.body.removeEventListener("mouseup",h),document.body.removeEventListener("touchend",h)})}()},onDrag:function(a){var b=this.wavesurfer.getDuration();this.end+a>b||this.start+a<0||this.update({start:this.start+a,end:this.end+a})},onResize:function(a,b){"start"==b?this.update({start:Math.min(this.start+a,this.end),end:Math.max(this.start+a,this.end)}):this.update({start:Math.min(this.end+a,this.start),end:Math.max(this.end+a,this.start)})}},a.util.extend(a.Region,a.Observer),a.initRegions=function(){this.regions||(this.regions=Object.create(a.Regions),this.regions.init(this))},a.addRegion=function(a){return this.initRegions(),this.regions.add(a)},a.clearRegions=function(){this.regions&&this.regions.clear()},a.enableDragSelection=function(a){this.initRegions(),this.regions.enableDragSelection(a)},a.disableDragSelection=function(){this.regions.disableDragSelection()}}); \ No newline at end of file diff --git a/dist/plugin/wavesurfer.spectrogram.min.js b/dist/plugin/wavesurfer.spectrogram.min.js index 55d2c64ec..7fbff60a1 100644 --- a/dist/plugin/wavesurfer.spectrogram.min.js +++ b/dist/plugin/wavesurfer.spectrogram.min.js @@ -1,3 +1,3 @@ -/*! wavesurfer.js 1.4.0 (Mon, 10 Apr 2017 08:55:36 GMT) +/*! wavesurfer.js 1.4.0 (Sun, 02 Jul 2017 11:49:04 GMT) * https://github.com/katspaugh/wavesurfer.js -* @license BSD-3-Clause */!function(a,b){"function"==typeof define&&define.amd?define(["wavesurfer"],function(a){return b(a)}):"object"==typeof exports?module.exports=b(require("wavesurfer.js")):b(WaveSurfer)}(this,function(a){"use strict";a.Spectrogram={init:function(a){this.params=a;var b=this.wavesurfer=a.wavesurfer;if(!this.wavesurfer)throw Error("No WaveSurfer instance provided");this.frequenciesDataUrl=a.frequenciesDataUrl;var c=this.drawer=this.wavesurfer.drawer;if(this.container="string"==typeof a.container?document.querySelector(a.container):a.container,!this.container)throw Error("No container for WaveSurfer spectrogram");this.width=c.width,this.pixelRatio=this.params.pixelRatio||b.params.pixelRatio,this.fftSamples=this.params.fftSamples||b.params.fftSamples||512,this.height=this.fftSamples/2,this.noverlap=a.noverlap,this.windowFunc=a.windowFunc,this.alpha=a.alpha,this.createWrapper(),this.createCanvas(),this.render(),c.wrapper.addEventListener("scroll",function(a){this.updateScroll(a)}.bind(this)),b.on("redraw",this.render.bind(this)),b.on("destroy",this.destroy.bind(this))},destroy:function(){this.unAll(),this.wrapper&&(this.wrapper.parentNode.removeChild(this.wrapper),this.wrapper=null)},createWrapper:function(){var a=this.container.querySelector("spectrogram");if(a&&this.container.removeChild(a),this.params.labels){var b=document.createElement("div");b.setAttribute("id","specLabels"),this.drawer.style(b,{left:0,position:"relative",zIndex:9}),b.innerHTML="",this.wrapper=this.container.appendChild(b),this.loadLabels("rgba(68,68,68,0.5)","12px","10px","","#fff","#f7f7f7","center","#specLabels")}var c=this.wavesurfer.params,d=document.createElement("spectrogram");this.params.labels&&this.drawer.style(d,{left:0,position:"relative"}),this.wrapper=this.container.appendChild(d),this.drawer.style(this.wrapper,{display:"block",position:"relative",userSelect:"none",webkitUserSelect:"none",height:this.height+"px"}),(c.fillParent||c.scrollParent)&&this.drawer.style(this.wrapper,{width:"100%",overflowX:"hidden",overflowY:"hidden"});var e=this;this.wrapper.addEventListener("click",function(a){a.preventDefault();var b="offsetX"in a?a.offsetX:a.layerX;e.fireEvent("click",b/e.scrollWidth||0)})},createCanvas:function(){var a=this.canvas=this.wrapper.appendChild(document.createElement("canvas"));this.spectrCc=a.getContext("2d"),this.wavesurfer.drawer.style(a,{position:"absolute",zIndex:4})},render:function(){this.updateCanvasStyle(),this.frequenciesDataUrl?this.loadFrequenciesData(this.frequenciesDataUrl):this.getFrequencies(this.drawSpectrogram)},updateCanvasStyle:function(){var a=Math.round(this.width/this.pixelRatio)+"px";this.canvas.width=this.width,this.canvas.height=this.height,this.canvas.style.width=a},drawSpectrogram:function(a,b){for(var c=(b.spectrCc,b.wavesurfer.backend.getDuration(),b.height),d=b.resample(a),e=b.buffer?2/b.buffer.numberOfChannels:1,f=0;f=1e3?(a/1e3).toFixed(1):Math.round(a)},unitType:function(a){return a>=1e3?"KHz":"Hz"},loadLabels:function(a,b,c,d,e,f,g,h){var i=this.height,a=a||"rgba(68,68,68,0.5)",b=b||"12px",c=c||"10px",d=d||"Helvetica",e=e||"#fff",f=f||"#fff",g=g||"center",h=h||"#specLabels",j=i||512,k=5*(j/256),l=0,m=(this.wavesurfer.backend.ac.sampleRate/2-l)/k,n=document.querySelectorAll(h+" canvas")[0].getContext("2d");document.querySelectorAll(h+" canvas")[0].height=this.height,document.querySelectorAll(h+" canvas")[0].width=55,n.fillStyle=a,n.fillRect(0,0,55,j),n.fill();for(var o=0;o<=k;o++){n.textAlign=g,n.textBaseline="middle";var p=l+m*o,q=Math.round(p/this.sampleRate/2*this.fftSamples),q=Math.round(p/(this.fftSamples/2)*this.fftSamples),r=q/this.fftSamples/2,s=((1-r)*this.height,this.freqType(p)),t=this.unitType(p),u=16,v=2;0==o?(n.fillStyle=f,n.font=c+" "+d,n.fillText(t,u+24,j+o-10),n.fillStyle=e,n.font=b+" "+d,n.fillText(s,u,j+o-10)):(n.fillStyle=f,n.font=c+" "+d,n.fillText(t,u+24,j-50*o+v),n.fillStyle=e,n.font=b+" "+d,n.fillText(s,u,j-50*o+v))}},updateScroll:function(a){this.wrapper.scrollLeft=a.target.scrollLeft},resample:function(a,b){for(var b=this.width,c=[],d=1/a.length,e=1/b,f=0;f0)for(var n=0;n>1;f>=1}for(e=0;ethis.peak&&(this.peakBand=x,this.peak=d),m[x]=d;return m}},a.util.extend(a.Spectrogram,a.Observer,a.FFT)}); \ No newline at end of file +* @license BSD-3-Clause */!function(a,b){"function"==typeof define&&define.amd?define(["wavesurfer"],function(a){return b(a)}):"object"==typeof exports?module.exports=b(require("wavesurfer.js")):b(WaveSurfer)}(this,function(a){"use strict";a.Spectrogram={init:function(a){this.params=a;var b=this.wavesurfer=a.wavesurfer;if(!this.wavesurfer)throw Error("No WaveSurfer instance provided");this.frequenciesDataUrl=a.frequenciesDataUrl;var c=this.drawer=this.wavesurfer.drawer;if(this.container="string"==typeof a.container?document.querySelector(a.container):a.container,!this.container)throw Error("No container for WaveSurfer spectrogram");this.width=c.width,this.pixelRatio=this.params.pixelRatio||b.params.pixelRatio,this.fftSamples=this.params.fftSamples||b.params.fftSamples||512,this.height=this.fftSamples/2,this.noverlap=a.noverlap,this.windowFunc=a.windowFunc,this.alpha=a.alpha,this.createWrapper(),this.createCanvas(),this.render(),c.wrapper.addEventListener("scroll",function(a){this.updateScroll(a)}.bind(this)),b.on("redraw",this.render.bind(this)),b.on("destroy",this.destroy.bind(this))},destroy:function(){this.unAll(),this.wrapper&&(this.wrapper.parentNode.removeChild(this.wrapper),this.wrapper=null)},createWrapper:function(){var a=this.container.querySelector("spectrogram");if(a&&this.container.removeChild(a),this.params.labels){var b=document.createElement("div");b.setAttribute("id","specLabels"),this.drawer.style(b,{left:0,position:"absolute",zIndex:9}),b.innerHTML="",this.wrapper=this.container.appendChild(b),this.loadLabels("rgba(68,68,68,0.5)","12px","10px","","#fff","#f7f7f7","center","#specLabels")}var c=this.wavesurfer.params,d=document.createElement("spectrogram");this.params.labels&&this.drawer.style(d,{left:0,position:"relative"}),this.wrapper=this.container.appendChild(d),this.drawer.style(this.wrapper,{display:"block",position:"relative",userSelect:"none",webkitUserSelect:"none",height:this.height+"px"}),(c.fillParent||c.scrollParent)&&this.drawer.style(this.wrapper,{width:"100%",overflowX:"hidden",overflowY:"hidden"});var e=this;this.wrapper.addEventListener("click",function(a){a.preventDefault();var b="offsetX"in a?a.offsetX:a.layerX;e.fireEvent("click",b/e.scrollWidth||0)})},createCanvas:function(){var a=this.canvas=this.wrapper.appendChild(document.createElement("canvas"));this.spectrCc=a.getContext("2d"),this.wavesurfer.drawer.style(a,{position:"absolute",zIndex:4})},render:function(){this.updateCanvasStyle(),this.frequenciesDataUrl?this.loadFrequenciesData(this.frequenciesDataUrl):this.getFrequencies(this.drawSpectrogram)},updateCanvasStyle:function(){var a=Math.round(this.width/this.pixelRatio)+"px";this.canvas.width=this.width,this.canvas.height=this.height,this.canvas.style.width=a},drawSpectrogram:function(a,b){for(var c=(b.spectrCc,b.wavesurfer.backend.getDuration(),b.height),d=b.resample(a),e=b.buffer?2/b.buffer.numberOfChannels:1,f=0;f=1e3?(a/1e3).toFixed(1):Math.round(a)},unitType:function(a){return a>=1e3?"KHz":"Hz"},loadLabels:function(a,b,c,d,e,f,g,h){var i=this.height,a=a||"rgba(68,68,68,0.5)",b=b||"12px",c=c||"10px",d=d||"Helvetica",e=e||"#fff",f=f||"#fff",g=g||"center",h=h||"#specLabels",j=i||512,k=5*(j/256),l=0,m=(this.wavesurfer.backend.ac.sampleRate/2-l)/k,n=document.querySelectorAll(h+" canvas")[0].getContext("2d");document.querySelectorAll(h+" canvas")[0].height=this.height,document.querySelectorAll(h+" canvas")[0].width=55,n.fillStyle=a,n.fillRect(0,0,55,j),n.fill();for(var o=0;o<=k;o++){n.textAlign=g,n.textBaseline="middle";var p=l+m*o,q=Math.round(p/this.sampleRate/2*this.fftSamples),q=Math.round(p/(this.fftSamples/2)*this.fftSamples),r=q/this.fftSamples/2,s=((1-r)*this.height,this.freqType(p)),t=this.unitType(p),u=16,v=2;0==o?(n.fillStyle=f,n.font=c+" "+d,n.fillText(t,u+24,j+o-10),n.fillStyle=e,n.font=b+" "+d,n.fillText(s,u,j+o-10)):(n.fillStyle=f,n.font=c+" "+d,n.fillText(t,u+24,j-50*o+v),n.fillStyle=e,n.font=b+" "+d,n.fillText(s,u,j-50*o+v))}},updateScroll:function(a){this.wrapper.scrollLeft=a.target.scrollLeft},resample:function(a){for(var b=this.width,c=[],d=1/a.length,e=1/b,f=0;f0)for(var n=0;n>1;f>=1}for(e=0;ethis.peak&&(this.peakBand=x,this.peak=d),m[x]=d;return m}},a.util.extend(a.Spectrogram,a.Observer,a.FFT)}); \ No newline at end of file diff --git a/dist/plugin/wavesurfer.timeline.min.js b/dist/plugin/wavesurfer.timeline.min.js index 5977a579e..99671b49c 100644 --- a/dist/plugin/wavesurfer.timeline.min.js +++ b/dist/plugin/wavesurfer.timeline.min.js @@ -1,3 +1,3 @@ -/*! wavesurfer.js 1.4.0 (Mon, 10 Apr 2017 08:55:36 GMT) +/*! wavesurfer.js 1.4.0 (Sun, 02 Jul 2017 11:49:04 GMT) * https://github.com/katspaugh/wavesurfer.js -* @license BSD-3-Clause */!function(a,b){"function"==typeof define&&define.amd?define(["wavesurfer"],function(a){return b(a)}):"object"==typeof exports?module.exports=b(require("wavesurfer.js")):b(WaveSurfer)}(this,function(a){"use strict";a.Timeline={init:function(a){this.params=a;var b=this.wavesurfer=a.wavesurfer;if(!this.wavesurfer)throw Error("No WaveSurfer instance provided");var c=this.drawer=this.wavesurfer.drawer;if(this.container="string"==typeof a.container?document.querySelector(a.container):a.container,!this.container)throw Error("No container for WaveSurfer timeline");this.width=c.width,this.pixelRatio=this.drawer.params.pixelRatio,this.maxCanvasWidth=c.maxCanvasWidth||this.width,this.maxCanvasElementWidth=c.maxCanvasElementWidth||Math.round(this.maxCanvasWidth/this.pixelRatio),this.height=this.params.height||20,this.notchPercentHeight=this.params.notchPercentHeight||90,this.primaryColor=this.params.primaryColor||"#000",this.secondaryColor=this.params.secondaryColor||"#c0c0c0",this.primaryFontColor=this.params.primaryFontColor||"#000",this.secondaryFontColor=this.params.secondaryFontColor||"#000",this.fontFamily=this.params.fontFamily||"Arial",this.fontSize=this.params.fontSize||10,this.timeInterval=this.params.timeInterval,this.primaryLabelInterval=this.params.primaryLabelInterval,this.secondaryLabelInterval=this.params.secondaryLabelInterval,this.formatTimeCallback=this.params.formatTimeCallback,this.canvases=[],this.createWrapper(),this.render(),c.wrapper.addEventListener("scroll",function(a){this.updateScroll(a)}.bind(this)),this._onRedraw=b.on("redraw",this.render.bind(this)),this._onZoom=b.on("zoom",this.render.bind(this)),this._onDestroy=b.on("destroy",this.destroy.bind(this))},destroy:function(){this._onRedraw.un(),this._onZoom.un(),this._onDestroy.un(),this.unAll(),this.wrapper&&this.wrapper.parentNode&&(this.wrapper.parentNode.removeChild(this.wrapper),this.wrapper=null)},createWrapper:function(){var a=this.container.querySelector("timeline");a&&this.container.removeChild(a);var b=this.wavesurfer.params;this.wrapper=this.container.appendChild(document.createElement("timeline")),this.drawer.style(this.wrapper,{display:"block",position:"relative",userSelect:"none",webkitUserSelect:"none",height:this.height+"px"}),(b.fillParent||b.scrollParent)&&this.drawer.style(this.wrapper,{width:"100%",overflowX:"hidden",overflowY:"hidden"});var c=this;this.wrapper.addEventListener("click",function(a){a.preventDefault();var b="offsetX"in a?a.offsetX:a.layerX;c.fireEvent("click",b/c.wrapper.scrollWidth||0)})},removeOldCanvases:function(){for(;this.canvases.length>0;){var a=this.canvases.pop();a.parentElement.removeChild(a)}},createCanvases:function(){this.removeOldCanvases();for(var a,b=Math.round(this.drawer.wrapper.scrollWidth),c=Math.ceil(b/this.maxCanvasElementWidth),d=0;d1){var b=parseInt(a/60),a=parseInt(a%60);return a=a<10?"0"+a:a,""+b+":"+a}return a};if(1*f>=25)var k=1,l=10,m=5;else if(5*f>=25)var k=5,l=6,m=2;else if(15*f>=25)var k=15,l=4,m=2;else var k=60*Math.ceil(.5/f),l=4,m=2;k=this.timeInterval||k,l=this.primaryLabelInterval||l,m=this.secondaryLabelInterval||m;for(var n=this.height-4,o=this.height*(this.notchPercentHeight/100)-4,p=this.fontSize*b.pixelRatio,q=0;qb+d)break;e+h>b&&(d=g.measureText(a).width,g.fillText(a,b-e,c)),e+=h}},updateScroll:function(){this.wrapper.scrollLeft=this.drawer.wrapper.scrollLeft}},a.util.extend(a.Timeline,a.Observer)}); \ No newline at end of file +* @license BSD-3-Clause */!function(a,b){"function"==typeof define&&define.amd?define(["wavesurfer"],function(a){return b(a)}):"object"==typeof exports?module.exports=b(require("wavesurfer.js")):b(WaveSurfer)}(this,function(a){"use strict";a.Timeline={init:function(a){this.params=a;var b=this.wavesurfer=a.wavesurfer;if(!this.wavesurfer)throw Error("No WaveSurfer instance provided");var c=this.drawer=this.wavesurfer.drawer;if(this.container="string"==typeof a.container?document.querySelector(a.container):a.container,!this.container)throw Error("No container for WaveSurfer timeline");this.width=c.width,this.pixelRatio=this.drawer.params.pixelRatio,this.maxCanvasWidth=c.maxCanvasWidth||this.width,this.maxCanvasElementWidth=c.maxCanvasElementWidth||Math.round(this.maxCanvasWidth/this.pixelRatio),this.height=this.params.height||20,this.notchPercentHeight=this.params.notchPercentHeight||90,this.primaryColor=this.params.primaryColor||"#000",this.secondaryColor=this.params.secondaryColor||"#c0c0c0",this.primaryFontColor=this.params.primaryFontColor||"#000",this.secondaryFontColor=this.params.secondaryFontColor||"#000",this.fontFamily=this.params.fontFamily||"Arial",this.fontSize=this.params.fontSize||10,this.timeInterval=this.params.timeInterval,this.primaryLabelInterval=this.params.primaryLabelInterval,this.secondaryLabelInterval=this.params.secondaryLabelInterval,this.formatTimeCallback=this.params.formatTimeCallback,this.canvases=[],this.createWrapper(),this.render(),c.wrapper.addEventListener("scroll",function(a){this.updateScroll(a)}.bind(this)),this._onRedraw=b.on("redraw",this.render.bind(this)),this._onZoom=b.on("zoom",this.render.bind(this)),this._onDestroy=b.on("destroy",this.destroy.bind(this))},destroy:function(){this._onRedraw.un(),this._onZoom.un(),this._onDestroy.un(),this.unAll(),this.wrapper&&this.wrapper.parentNode&&(this.wrapper.parentNode.removeChild(this.wrapper),this.wrapper=null)},createWrapper:function(){var a=this.container.querySelector("timeline");a&&this.container.removeChild(a);var b=this.wavesurfer.params;this.wrapper=this.container.appendChild(document.createElement("timeline")),this.drawer.style(this.wrapper,{display:"block",position:"relative",userSelect:"none",webkitUserSelect:"none",height:this.height+"px"}),(b.fillParent||b.scrollParent)&&this.drawer.style(this.wrapper,{width:"100%",overflowX:"hidden",overflowY:"hidden"});var c=this;this.wrapper.addEventListener("click",function(a){a.preventDefault();var b="offsetX"in a?a.offsetX:a.layerX;c.fireEvent("click",b/c.wrapper.scrollWidth||0)})},removeOldCanvases:function(){for(;this.canvases.length>0;){var a=this.canvases.pop();a.parentElement.removeChild(a)}},createCanvases:function(){this.removeOldCanvases();for(var a,b=Math.round(this.drawer.wrapper.scrollWidth),c=Math.ceil(b/this.maxCanvasElementWidth),d=0;d1){var b=parseInt(a/60),a=parseInt(a%60);return a=a<10?"0"+a:a,""+b+":"+a}return Math.round(1e3*a)/1e3};if(1*f>=25)var k=1,l=10,m=5;else if(5*f>=25)var k=5,l=6,m=2;else if(15*f>=25)var k=15,l=4,m=2;else var k=60*Math.ceil(.5/f),l=4,m=2;k=this.timeInterval||k,l=this.primaryLabelInterval||l,m=this.secondaryLabelInterval||m;for(var n=this.height-4,o=this.height*(this.notchPercentHeight/100)-4,p=this.fontSize*b.pixelRatio,q=0;qb+d)break;e+h>b&&(d=g.measureText(a).width,g.fillText(a,b-e,c)),e+=h}},updateScroll:function(){this.wrapper.scrollLeft=this.drawer.wrapper.scrollLeft}},a.util.extend(a.Timeline,a.Observer)}); \ No newline at end of file diff --git a/dist/wavesurfer.js b/dist/wavesurfer.js index 09ebad4fc..f390c6c06 100644 --- a/dist/wavesurfer.js +++ b/dist/wavesurfer.js @@ -161,10 +161,6 @@ var WaveSurfer = { return this.backend.getDuration(); }, - getCurrentTime: function () { - return this.backend.getCurrentTime(); - }, - play: function (start, end) { this.fireEvent('interaction', this.play.bind(this, start, end)); this.backend.play(start, end); @@ -246,6 +242,26 @@ var WaveSurfer = { return this.backend.getVolume(); }, + /** + * Get the current play time. + */ + getCurrentTime: function () { + return this.backend.getCurrentTime(); + }, + + /** + * Set the current play time in seconds. + * + * @param {Number} seconds A positive number in seconds. E.g. 10 means 10 seconds, 60 means 1 minute + */ + setCurrentTime: function (seconds) { + if(this.getDuration() >= seconds) { + this.seekTo(1); + } else { + this.seekTo(seconds/this.getDuration()); + } + }, + /** * Set the playback rate. * @@ -546,11 +562,12 @@ var WaveSurfer = { /** * Exports PCM data into a JSON array and opens in a new window. */ - exportPCM: function (length, accuracy, noWindow) { + exportPCM: function (length, accuracy, noWindow, start) { length = length || 1024; + start = start || 0; accuracy = accuracy || 10000; noWindow = noWindow || false; - var peaks = this.backend.getPeaks(length, accuracy); + var peaks = this.backend.getPeaks(length, start); var arr = [].map.call(peaks, function (val) { return Math.round(val * accuracy) / accuracy; }); @@ -626,6 +643,24 @@ WaveSurfer.create = function (params) { }; WaveSurfer.util = { + requestAnimationFrame: ( + window.requestAnimationFrame || + window.webkitRequestAnimationFrame || + window.mozRequestAnimationFrame || + window.oRequestAnimationFrame || + window.msRequestAnimationFrame || + function (callback, element) { setTimeout(callback, 1000 / 60); } + ).bind(window), + + frame: function (func) { + return function () { + var my = this, args = arguments; + WaveSurfer.util.requestAnimationFrame(function () { + func.apply(my, args); + }); + }; + }, + extend: function (dest) { var sources = Array.prototype.slice.call(arguments, 1); sources.forEach(function (source) { @@ -800,6 +835,18 @@ WaveSurfer.Observer = { /* Make the main WaveSurfer object an observer */ WaveSurfer.util.extend(WaveSurfer, WaveSurfer.Observer); +/* jscs:disable */ + +/** + * @see https://github.com/Infinity/Kali + */ +!function(e){function t(i){if(r[i])return r[i].exports;var n=r[i]={exports:{},id:i,loaded:!1};return e[i].call(n.exports,n,n.exports,t),n.loaded=!0,n.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){e.exports=r(1)},function(e,t,r){function i(e){return Math.floor(e)}var n=r(2),s=function(){function e(){this.is_initialized=!1,this.sample_rate=44100,this.channels=0,this.quick_search=!1,this.factor=0,this.search=0,this.segment=0,this.overlap=0,this.process_size=0,this.samples_in=0,this.samples_out=0,this.segments_total=0,this.skip_total=0}return e}(),o=function(){function e(e){var t=new s;t.channels=e,t.input_fifo=new n(Float32Array),t.output_fifo=new n(Float32Array),this.t=t}return e.prototype.difference=function(e,t,r){for(var i=0,n=0,n=0;r>n;n++)i+=Math.pow(e[n]-t[n],2);return i},e.prototype.tempo_best_overlap_position=function(e,t){var r,i,n,s=e.overlap_buf,o=e.search+1>>>1,a=64,p=i=e.quick_search?o:0,u=this.difference(t.subarray(e.channels*p),s,e.channels*e.overlap),f=0;if(e.quick_search){do{for(f=-1;1>=f;f+=2)for(r=1;(4>r||64==a)&&(p=o+f*r*a,!(0>p||p>=e.search));r++)n=this.difference(t.subarray(e.channels*p),s,e.channels*e.overlap),u>n&&(u=n,i=p);o=i}while(a>>>=2)}else for(p=1;pn&&(u=n,i=p);return i},e.prototype.tempo_overlap=function(e,t,r,i){var n=0,s=0,o=0,a=1/e.overlap;for(n=0;n=e.process_size;){var t,r;e.segments_total?(r=this.tempo_best_overlap_position(e,e.input_fifo.read_ptr(0)),this.tempo_overlap(e,e.overlap_buf,e.input_fifo.read_ptr(e.channels*r),e.output_fifo.write_ptr(e.overlap))):(r=e.search/2,e.output_fifo.write(e.input_fifo.read_ptr(e.channels*r,e.overlap),e.overlap)),e.output_fifo.write(e.input_fifo.read_ptr(e.channels*(r+e.overlap)),e.segment-2*e.overlap);var n=e.channels*e.overlap;e.overlap_buf.set(e.input_fifo.read_ptr(e.channels*(r+e.segment-e.overlap)).subarray(0,n)),e.segments_total++,t=i(e.factor*(e.segment-e.overlap)+.5),e.input_fifo.read(null,t)}},e.prototype.input=function(e,t,r){void 0===t&&(t=null),void 0===r&&(r=0),null==t&&(t=e.length);var i=this.t;i.samples_in+=t,i.input_fifo.write(e,t)},e.prototype.output=function(e){var t=this.t,r=Math.min(e.length,t.output_fifo.occupancy());return t.samples_out+=r,t.output_fifo.read(e,r),r},e.prototype.flush=function(){var e=this.t,t=i(e.samples_in/e.factor+.5),r=t>e.samples_out?t-e.samples_out:0,n=new Float32Array(128*e.channels);if(r>0){for(;e.output_fifo.occupancy()u.segment&&(u.overlap-=8),u.is_initialized){var h=new Float32Array(u.overlap*u.channels),l=0;u.overlap*u.channels16384)this.buffer.set(this.buffer.subarray(this.begin,this.end)),this.end-=this.begin,this.begin=0;else{var r=new this.typedArrayConstructor(this.buffer.length+e);r.set(this.buffer),this.buffer=r}}},e.prototype.write=function(e,t){var r=this.reserve(t);this.buffer.set(e.subarray(0,t),r)},e.prototype.write_ptr=function(e){var t=this.reserve(e);return this.buffer.subarray(t,t+e)},e.prototype.read=function(e,t){t+this.begin>this.end&&console.error("Read out of bounds",t,this.end,this.begin),null!=e&&e.set(this.buffer.subarray(this.begin,this.begin+t)),this.begin+=t},e.prototype.read_ptr=function(e,t){return void 0===t&&(t=-1),t>this.occupancy()&&console.error("Read Pointer out of bounds",t),0>t&&(t=this.occupancy()),this.buffer.subarray(this.begin+e,this.begin+t)},e.prototype.occupancy=function(){return this.end-this.begin},e}();e.exports=r}]); + +WaveSurfer.util.Kali = window.Kali; + +/* jscs:enable */ +/* jshint ignore:end */ + 'use strict'; WaveSurfer.WebAudio = { @@ -837,6 +884,7 @@ WaveSurfer.WebAudio = { this.lastPlay = this.ac.currentTime; this.startPosition = 0; this.scheduledPause = null; + this.playbackRate = this.params.audioRate || 1; this.states = [ Object.create(WaveSurfer.WebAudio.state.playing), @@ -847,9 +895,7 @@ WaveSurfer.WebAudio = { this.createVolumeNode(); this.createScriptNode(); this.createAnalyserNode(); - this.setState(this.PAUSED_STATE); - this.setPlaybackRate(this.params.audioRate); this.setLength(0); }, @@ -1020,6 +1066,9 @@ WaveSurfer.WebAudio = { getPeaks: function (length, first, last) { if (this.peaks) { return this.peaks; } + first = first || 0; + last = last || length - 1; + this.setLength(length); var sampleSize = this.buffer.length / length; @@ -1109,7 +1158,13 @@ WaveSurfer.WebAudio = { this.startPosition = 0; this.lastPlay = this.ac.currentTime; this.buffer = buffer; - this.createSource(); + this.originalBuffer = buffer; + + if (this.playbackRate === 1) { + this.createSource(); + } else { + this.setPlaybackRate(this.playbackRate); + } }, createSource: function () { @@ -1120,7 +1175,6 @@ WaveSurfer.WebAudio = { this.source.start = this.source.start || this.source.noteGrainOn; this.source.stop = this.source.stop || this.source.noteOff; - this.source.playbackRate.value = this.playbackRate; this.source.buffer = this.buffer; this.source.connect(this.analyser); }, @@ -1162,7 +1216,7 @@ WaveSurfer.WebAudio = { }, getPlayedTime: function () { - return (this.ac.currentTime - this.lastPlay) * this.playbackRate; + return (this.ac.currentTime - this.lastPlay); }, /** @@ -1230,11 +1284,31 @@ WaveSurfer.WebAudio = { */ setPlaybackRate: function (value) { value = value || 1; - if (this.isPaused()) { - this.playbackRate = value; - } else { + this.playbackRate = value; + + var isPaused = this.isPaused(); + + if (!isPaused) { this.pause(); - this.playbackRate = value; + } + + var output = new Float32Array(this.originalBuffer.length / value); + var kali = new WaveSurfer.util.Kali(1); + kali.setup(this.originalBuffer.sampleRate, value, true); + kali.input(this.originalBuffer.getChannelData(0)); + kali.process(); + kali.output(output); + + this.buffer = this.ac.createBuffer(1, output.length, this.originalBuffer.sampleRate); + var data = this.buffer.getChannelData(0); + + for (var i = 0; i < output.length; i++) { + data[i] = output[i]; + } + + if (isPaused) { + this.createSource(); + } else { this.play(); } } @@ -1398,6 +1472,16 @@ WaveSurfer.util.extend(WaveSurfer.MediaElement, { my.fireEvent('finish'); }); + // Listen to and relay play and pause events to enable + // playback control from the external media element + media.addEventListener('play', function () { + my.fireEvent('play'); + }); + + media.addEventListener('pause', function () { + my.fireEvent('pause'); + }); + this.media = media; this.peaks = peaks; this.onPlayEnd = null; @@ -1456,7 +1540,6 @@ WaveSurfer.util.extend(WaveSurfer.MediaElement, { this.seekTo(start); this.media.play(); end && this.setPlayEnd(end); - this.fireEvent('play'); }, /** @@ -1465,7 +1548,6 @@ WaveSurfer.util.extend(WaveSurfer.MediaElement, { pause: function () { this.media && this.media.pause(); this.clearPlayEnd(); - this.fireEvent('pause'); }, setPlayEnd: function (end) { @@ -1714,7 +1796,7 @@ WaveSurfer.Drawer = { destroy: function () { this.unAll(); if (this.wrapper) { - this.container.removeChild(this.wrapper); + if (this.wrapper.parentNode == this.container) { this.container.removeChild(this.wrapper); } this.wrapper = null; } }, @@ -1747,7 +1829,8 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.Canvas, { zIndex: 1, left: 0, top: 0, - bottom: 0 + bottom: 0, + pointerEvents: 'none' }) ); this.waveCc = waveCanvas.getContext('2d'); @@ -1765,7 +1848,8 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.Canvas, { boxSizing: 'border-box', borderRightStyle: 'solid', borderRightWidth: this.params.cursorWidth + 'px', - borderRightColor: this.params.cursorColor + borderRightColor: this.params.cursorColor, + pointerEvents: 'none' }) ); @@ -1802,7 +1886,7 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.Canvas, { } }, - drawBars: function (peaks, channelIndex, start, end) { + drawBars: WaveSurfer.util.frame(function (peaks, channelIndex, start, end) { var my = this; // Split channels if (peaks[0] instanceof Array) { @@ -1861,9 +1945,9 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.Canvas, { cc.fillRect(i + $, halfH - h + offsetY, bar + $, h * 2); } }, this); - }, + }), - drawWave: function (peaks, channelIndex, start, end) { + drawWave: WaveSurfer.util.frame(function (peaks, channelIndex, start, end) { var my = this; // Split channels if (peaks[0] instanceof Array) { @@ -1938,7 +2022,7 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.Canvas, { // Always draw a median line cc.fillRect(0, halfH + offsetY - $, this.width, $); }, this); - }, + }), updateProgress: function (pos) { this.style(this.progressWave, { width: pos + 'px' }); @@ -1984,7 +2068,8 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.MultiCanvas, { boxSizing: 'border-box', borderRightStyle: 'solid', borderRightWidth: this.params.cursorWidth + 'px', - borderRightColor: this.params.cursorColor + borderRightColor: this.params.cursorColor, + pointerEvents: 'none' }) ); @@ -2003,7 +2088,7 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.MultiCanvas, { this.removeCanvas(); } - for (var i in this.canvases) { + this.canvases.forEach(function (entry, i) { // Add some overlap to prevent vertical white stripes, keep the width even for simplicity. var canvasWidth = this.maxCanvasWidth + 2 * Math.ceil(this.params.pixelRatio / 2); @@ -2011,9 +2096,9 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.MultiCanvas, { canvasWidth = this.width - (this.maxCanvasWidth * (this.canvases.length - 1)); } - this.updateDimensions(this.canvases[i], canvasWidth, this.height); - this.clearWaveForEntry(this.canvases[i]); - } + this.updateDimensions(entry, canvasWidth, this.height); + this.clearWaveForEntry(entry); + }, this); }, addCanvas: function () { @@ -2027,7 +2112,8 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.MultiCanvas, { left: leftOffset + 'px', top: 0, bottom: 0, - height: '100%' + height: '100%', + pointerEvents: 'none' }) ); entry.waveCtx = entry.wave.getContext('2d'); @@ -2066,21 +2152,21 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.MultiCanvas, { entry.waveCtx.canvas.width = width; entry.waveCtx.canvas.height = height; - this.style(entry.waveCtx.canvas, { width: elementWidth + 'px'}); + this.style(entry.waveCtx.canvas, { width: elementWidth + 'px' }); - this.style(this.progressWave, { display: 'block'}); + this.style(this.progressWave, { display: 'block' }); if (this.hasProgressCanvas) { entry.progressCtx.canvas.width = width; entry.progressCtx.canvas.height = height; - this.style(entry.progressCtx.canvas, { width: elementWidth + 'px'}); + this.style(entry.progressCtx.canvas, { width: elementWidth + 'px' }); } }, clearWave: function () { - for (var i in this.canvases) { - this.clearWaveForEntry(this.canvases[i]); - } + this.canvases.forEach(function (entry) { + this.clearWaveForEntry(entry); + }, this); }, clearWaveForEntry: function (entry) { @@ -2090,16 +2176,15 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.MultiCanvas, { } }, - drawBars: function (peaks, channelIndex, start, end) { - var my = this; + drawBars: WaveSurfer.util.frame(function (peaks, channelIndex, start, end) { // Split channels if (peaks[0] instanceof Array) { var channels = peaks; if (this.params.splitChannels) { this.setHeight(channels.length * this.params.height * this.params.pixelRatio); channels.forEach(function(channelPeaks, i) { - my.drawBars(channelPeaks, i, start, end); - }); + this.drawBars(channelPeaks, i, start, end); + }, this); return; } else { peaks = channels[0]; @@ -2108,7 +2193,7 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.MultiCanvas, { // Bar wave draws the bottom only as a reflection of the top, // so we don't need negative values - var hasMinVals = [].some.call(peaks, function (val) { return val < 0; }); + var hasMinVals = [].some.call(peaks, function (val) {return val < 0;}); // Skip every other value if there are negatives. var peakIndexScale = 1; if (hasMinVals) { @@ -2139,18 +2224,17 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.MultiCanvas, { var h = Math.round(peak / absmax * halfH); this.fillRect(i + this.halfPixel, halfH - h + offsetY, bar + this.halfPixel, h * 2); } - }, + }), - drawWave: function (peaks, channelIndex, start, end) { - var my = this; + drawWave: WaveSurfer.util.frame(function (peaks, channelIndex, start, end) { // Split channels if (peaks[0] instanceof Array) { var channels = peaks; if (this.params.splitChannels) { this.setHeight(channels.length * this.params.height * this.params.pixelRatio); channels.forEach(function(channelPeaks, i) { - my.drawWave(channelPeaks, i, start, end); - }); + this.drawWave(channelPeaks, i, start, end); + }, this); return; } else { peaks = channels[0]; @@ -2184,17 +2268,14 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.MultiCanvas, { // Always draw a median line this.fillRect(0, halfH + offsetY - this.halfPixel, this.width, this.halfPixel); - }, + }), drawLine: function (peaks, absmax, halfH, offsetY, start, end) { - for (var index in this.canvases) { - var entry = this.canvases[index]; - + this.canvases.forEach (function (entry) { this.setFillStyles(entry); - this.drawLineToContext(entry, entry.waveCtx, peaks, absmax, halfH, offsetY, start, end); this.drawLineToContext(entry, entry.progressCtx, peaks, absmax, halfH, offsetY, start, end); - } + }, this); }, drawLineToContext: function (entry, ctx, peaks, absmax, halfH, offsetY, start, end) { @@ -2237,11 +2318,10 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.MultiCanvas, { fillRect: function (x, y, width, height) { var startCanvas = Math.floor(x / this.maxCanvasWidth); var endCanvas = Math.min(Math.ceil((x + width) / this.maxCanvasWidth) + 1, - this.canvases.length); + this.canvases.length); for (var i = startCanvas; i < endCanvas; i++) { var entry = this.canvases[i], leftOffset = i * this.maxCanvasWidth; - var intersection = { x1: Math.max(x, i * this.maxCanvasWidth), y1: y, @@ -2284,13 +2364,13 @@ WaveSurfer.util.extend(WaveSurfer.Drawer.MultiCanvas, { }, /** - * Combine all available canvasses together. + * Combine all available canvases together. * * @param {String} type - an optional value of a format type. Default is image/png. * @param {Number} quality - an optional value between 0 and 1. Default is 0.92. * */ - getImage: function(type, quality) { + getImage: function (type, quality) { var availableCanvas = []; this.canvases.forEach(function (entry) { availableCanvas.push(entry.wave.toDataURL(type, quality)); diff --git a/dist/wavesurfer.min.js b/dist/wavesurfer.min.js index 8f222fc83..7ba9cd6e8 100644 --- a/dist/wavesurfer.min.js +++ b/dist/wavesurfer.min.js @@ -1,6 +1,6 @@ -/*! wavesurfer.js 1.4.0 (Mon, 10 Apr 2017 08:55:35 GMT) +/*! wavesurfer.js 1.4.0 (Sun, 02 Jul 2017 11:49:03 GMT) * https://github.com/katspaugh/wavesurfer.js * @license BSD-3-Clause */ -!function(a,b){"function"==typeof define&&define.amd?define("wavesurfer",[],function(){return a.WaveSurfer=b()}):"object"==typeof exports?module.exports=b():a.WaveSurfer=b()}(this,function(){"use strict";var a={defaultParams:{audioContext:null,audioRate:1,autoCenter:!0,backend:"WebAudio",barHeight:1,closeAudioContext:!1,container:null,cursorColor:"#333",cursorWidth:1,dragSelection:!0,fillParent:!0,forceDecode:!1,height:128,hideScrollbar:!1,interact:!0,loopSelection:!0,mediaContainer:null,mediaControls:!1,mediaType:"audio",minPxPerSec:20,partialRender:!1,pixelRatio:window.devicePixelRatio||screen.deviceXDPI/screen.logicalXDPI,progressColor:"#555",normalize:!1,renderer:"MultiCanvas",scrollParent:!1,skipLength:2,splitChannels:!1,waveColor:"#999"},init:function(b){if(this.params=a.util.extend({},this.defaultParams,b),this.container="string"==typeof b.container?document.querySelector(this.params.container):this.params.container,!this.container)throw new Error("Container element not found");if(null==this.params.mediaContainer?this.mediaContainer=this.container:"string"==typeof this.params.mediaContainer?this.mediaContainer=document.querySelector(this.params.mediaContainer):this.mediaContainer=this.params.mediaContainer,!this.mediaContainer)throw new Error("Media Container element not found");this.savedVolume=0,this.isMuted=!1,this.tmpEvents=[],this.currentAjax=null,this.createDrawer(),this.createBackend(),this.createPeakCache(),this.isDestroyed=!1},createDrawer:function(){var b=this;this.drawer=Object.create(a.Drawer[this.params.renderer]),this.drawer.init(this.container,this.params),this.drawer.on("redraw",function(){b.drawBuffer(),b.drawer.progress(b.backend.getPlayedPercents())}),this.drawer.on("click",function(a,c){setTimeout(function(){b.seekTo(c)},0)}),this.drawer.on("scroll",function(a){b.params.partialRender&&b.drawBuffer(),b.fireEvent("scroll",a)})},createBackend:function(){var b=this;this.backend&&this.backend.destroy(),"AudioElement"==this.params.backend&&(this.params.backend="MediaElement"),"WebAudio"!=this.params.backend||a.WebAudio.supportsWebAudio()||(this.params.backend="MediaElement"),this.backend=Object.create(a[this.params.backend]),this.backend.init(this.params),this.backend.on("finish",function(){b.fireEvent("finish")}),this.backend.on("play",function(){b.fireEvent("play")}),this.backend.on("pause",function(){b.fireEvent("pause")}),this.backend.on("audioprocess",function(a){b.drawer.progress(b.backend.getPlayedPercents()),b.fireEvent("audioprocess",a)})},createPeakCache:function(){this.params.partialRender&&(this.peakCache=Object.create(a.PeakCache),this.peakCache.init())},getDuration:function(){return this.backend.getDuration()},getCurrentTime:function(){return this.backend.getCurrentTime()},play:function(a,b){this.fireEvent("interaction",this.play.bind(this,a,b)),this.backend.play(a,b)},pause:function(){this.backend.isPaused()||this.backend.pause()},playPause:function(){this.backend.isPaused()?this.play():this.pause()},isPlaying:function(){return!this.backend.isPaused()},skipBackward:function(a){this.skip(-a||-this.params.skipLength)},skipForward:function(a){this.skip(a||this.params.skipLength)},skip:function(a){var b=this.getCurrentTime()||0,c=this.getDuration()||1;b=Math.max(0,Math.min(c,b+(a||0))),this.seekAndCenter(b/c)},seekAndCenter:function(a){this.seekTo(a),this.drawer.recenter(a)},seekTo:function(a){this.fireEvent("interaction",this.seekTo.bind(this,a));var b=this.backend.isPaused();b||this.backend.pause();var c=this.params.scrollParent;this.params.scrollParent=!1,this.backend.seekTo(a*this.getDuration()),this.drawer.progress(this.backend.getPlayedPercents()),b||this.backend.play(),this.params.scrollParent=c,this.fireEvent("seek",a)},stop:function(){this.pause(),this.seekTo(0),this.drawer.progress(0)},setVolume:function(a){this.backend.setVolume(a)},getVolume:function(){return this.backend.getVolume()},setPlaybackRate:function(a){this.backend.setPlaybackRate(a)},getPlaybackRate:function(){return this.backend.getPlaybackRate()},toggleMute:function(){this.setMute(!this.isMuted)},setMute:function(a){a!==this.isMuted&&(a?(this.savedVolume=this.backend.getVolume(),this.backend.setVolume(0),this.isMuted=!0):(this.backend.setVolume(this.savedVolume),this.isMuted=!1))},getMute:function(){return this.isMuted},getFilters:function(){return this.backend.filters||[]},toggleScroll:function(){this.params.scrollParent=!this.params.scrollParent,this.drawBuffer()},toggleInteraction:function(){this.params.interact=!this.params.interact},drawBuffer:function(){var a=Math.round(this.getDuration()*this.params.minPxPerSec*this.params.pixelRatio),b=this.drawer.getWidth(),c=a,d=this.drawer.getScrollX(),e=Math.min(d+b,c);if(this.params.fillParent&&(!this.params.scrollParent||ab&&(b=a[c]);return b},getId:function(){return"wavesurfer_"+Math.random().toString(32).substring(2)},ajax:function(b){var c=Object.create(a.Observer),d=new XMLHttpRequest,e=!1;return d.open(b.method||"GET",b.url,!0),d.responseType=b.responseType||"json",d.addEventListener("progress",function(a){c.fireEvent("progress",a),a.lengthComputable&&a.loaded==a.total&&(e=!0)}),d.addEventListener("load",function(a){e||c.fireEvent("progress",a),c.fireEvent("load",a),200==d.status||206==d.status?c.fireEvent("success",d.response,a):c.fireEvent("error",a)}),d.addEventListener("error",function(a){c.fireEvent("error",a)}),d.send(),c.xhr=d,c}},a.Observer={on:function(a,b){this.handlers||(this.handlers={});var c=this.handlers[a];return c||(c=this.handlers[a]=[]),c.push(b),{name:a,callback:b,un:this.un.bind(this,a,b)}},un:function(a,b){if(this.handlers){var c=this.handlers[a];if(c)if(b)for(var d=c.length-1;d>=0;d--)c[d]==b&&c.splice(d,1);else c.length=0}},unAll:function(){this.handlers=null},once:function(a,b){var c=this,d=function(){b.apply(this,arguments),setTimeout(function(){c.un(a,d)},0)};return this.on(a,d)},fireEvent:function(a){if(this.handlers){var b=this.handlers[a],c=Array.prototype.slice.call(arguments,1);b&&b.forEach(function(a){a.apply(null,c)})}}},a.util.extend(a,a.Observer),a.WebAudio={scriptBufferSize:256,PLAYING_STATE:0,PAUSED_STATE:1,FINISHED_STATE:2,supportsWebAudio:function(){return!(!window.AudioContext&&!window.webkitAudioContext)},getAudioContext:function(){return a.WebAudio.audioContext||(a.WebAudio.audioContext=new(window.AudioContext||window.webkitAudioContext)),a.WebAudio.audioContext},getOfflineAudioContext:function(b){return a.WebAudio.offlineAudioContext||(a.WebAudio.offlineAudioContext=new(window.OfflineAudioContext||window.webkitOfflineAudioContext)(1,2,b)),a.WebAudio.offlineAudioContext},init:function(b){this.params=b,this.ac=b.audioContext||this.getAudioContext(),this.lastPlay=this.ac.currentTime,this.startPosition=0,this.scheduledPause=null,this.states=[Object.create(a.WebAudio.state.playing),Object.create(a.WebAudio.state.paused),Object.create(a.WebAudio.state.finished)],this.createVolumeNode(),this.createScriptNode(),this.createAnalyserNode(),this.setState(this.PAUSED_STATE),this.setPlaybackRate(this.params.audioRate),this.setLength(0)},disconnectFilters:function(){this.filters&&(this.filters.forEach(function(a){a&&a.disconnect()}),this.filters=null,this.analyser.connect(this.gainNode))},setState:function(a){this.state!==this.states[a]&&(this.state=this.states[a],this.state.init.call(this))},setFilter:function(){this.setFilters([].slice.call(arguments))},setFilters:function(a){this.disconnectFilters(),a&&a.length&&(this.filters=a,this.analyser.disconnect(),a.reduce(function(a,b){return a.connect(b),b},this.analyser).connect(this.gainNode))},createScriptNode:function(){this.ac.createScriptProcessor?this.scriptNode=this.ac.createScriptProcessor(this.scriptBufferSize):this.scriptNode=this.ac.createJavaScriptNode(this.scriptBufferSize),this.scriptNode.connect(this.ac.destination)},addOnAudioProcess:function(){var a=this;this.scriptNode.onaudioprocess=function(){var b=a.getCurrentTime();b>=a.getDuration()?(a.setState(a.FINISHED_STATE),a.fireEvent("pause")):b>=a.scheduledPause?a.pause():a.state===a.states[a.PLAYING_STATE]&&a.fireEvent("audioprocess",b)}},removeOnAudioProcess:function(){this.scriptNode.onaudioprocess=null},createAnalyserNode:function(){this.analyser=this.ac.createAnalyser(),this.analyser.connect(this.gainNode)},createVolumeNode:function(){this.ac.createGain?this.gainNode=this.ac.createGain():this.gainNode=this.ac.createGainNode(),this.gainNode.connect(this.ac.destination)},setVolume:function(a){this.gainNode.gain.value=a},getVolume:function(){return this.gainNode.gain.value},decodeArrayBuffer:function(a,b,c){this.offlineAc||(this.offlineAc=this.getOfflineAudioContext(this.ac?this.ac.sampleRate:44100)),this.offlineAc.decodeAudioData(a,function(a){b(a)}.bind(this),c)},setPeaks:function(a){this.peaks=a},setLength:function(a){if(!this.mergedPeaks||a!=2*this.mergedPeaks.length-1+2){this.splitPeaks=[],this.mergedPeaks=[];for(var b=this.buffer?this.buffer.numberOfChannels:1,c=0;cn&&(n=p),pthis.mergedPeaks[2*j])&&(this.mergedPeaks[2*j]=n),(0==g||m=this.getDuration()&&(a=0)),null==b&&(b=this.getDuration()),this.startPosition=a,this.lastPlay=this.ac.currentTime,this.state===this.states[this.FINISHED_STATE]&&this.setState(this.PAUSED_STATE),{start:a,end:b}},getPlayedTime:function(){return(this.ac.currentTime-this.lastPlay)*this.playbackRate},play:function(a,b){if(this.buffer){this.createSource();var c=this.seekTo(a,b);a=c.start,b=c.end,this.scheduledPause=b,this.source.start(0,a,b-a),"suspended"==this.ac.state&&this.ac.resume&&this.ac.resume(),this.setState(this.PLAYING_STATE),this.fireEvent("play")}},pause:function(){this.scheduledPause=null,this.startPosition+=this.getPlayedTime(),this.source&&this.source.stop(0),this.setState(this.PAUSED_STATE),this.fireEvent("pause")},getCurrentTime:function(){return this.state.getCurrentTime.call(this)},getPlaybackRate:function(){return this.playbackRate},setPlaybackRate:function(a){a=a||1,this.isPaused()?this.playbackRate=a:(this.pause(),this.playbackRate=a,this.play())}},a.WebAudio.state={},a.WebAudio.state.playing={init:function(){this.addOnAudioProcess()},getPlayedPercents:function(){var a=this.getDuration();return this.getCurrentTime()/a||0},getCurrentTime:function(){return this.startPosition+this.getPlayedTime()}},a.WebAudio.state.paused={init:function(){this.removeOnAudioProcess()},getPlayedPercents:function(){var a=this.getDuration();return this.getCurrentTime()/a||0},getCurrentTime:function(){return this.startPosition}},a.WebAudio.state.finished={init:function(){this.removeOnAudioProcess(),this.fireEvent("finish")},getPlayedPercents:function(){return 1},getCurrentTime:function(){return this.getDuration()}},a.util.extend(a.WebAudio,a.Observer),a.MediaElement=Object.create(a.WebAudio),a.util.extend(a.MediaElement,{init:function(a){this.params=a,this.media={currentTime:0,duration:0,paused:!0,playbackRate:1,play:function(){},pause:function(){}},this.mediaType=a.mediaType.toLowerCase(),this.elementPosition=a.elementPosition,this.setPlaybackRate(this.params.audioRate),this.createTimer()},createTimer:function(){var a=this,b=function(){if(!a.isPaused()){a.fireEvent("audioprocess",a.getCurrentTime());var c=window.requestAnimationFrame||window.webkitRequestAnimationFrame;c(b)}};this.on("play",b)},load:function(a,b,c,d){var e=document.createElement(this.mediaType);e.controls=this.params.mediaControls,e.autoplay=this.params.autoplay||!1,e.preload=null==d?"auto":d,e.src=a,e.style.width="100%";var f=b.querySelector(this.mediaType);f&&b.removeChild(f),b.appendChild(e),this._load(e,c)},loadElt:function(a,b){var c=a;c.controls=this.params.mediaControls,c.autoplay=this.params.autoplay||!1,this._load(c,b)},_load:function(a,b){var c=this;"function"==typeof a.load&&a.load(),a.addEventListener("error",function(){c.fireEvent("error","Error loading media element")}),a.addEventListener("canplay",function(){c.fireEvent("canplay")}),a.addEventListener("ended",function(){c.fireEvent("finish")}),this.media=a,this.peaks=b,this.onPlayEnd=null,this.buffer=null,this.setPlaybackRate(this.playbackRate)},isPaused:function(){return!this.media||this.media.paused},getDuration:function(){var a=(this.buffer||this.media).duration;return a>=1/0&&(a=this.media.seekable.end(0)),a},getCurrentTime:function(){return this.media&&this.media.currentTime},getPlayedPercents:function(){return this.getCurrentTime()/this.getDuration()||0},getPlaybackRate:function(){return this.playbackRate||this.media.playbackRate},setPlaybackRate:function(a){this.playbackRate=a||1,this.media.playbackRate=this.playbackRate},seekTo:function(a){null!=a&&(this.media.currentTime=a),this.clearPlayEnd()},play:function(a,b){this.seekTo(a),this.media.play(),b&&this.setPlayEnd(b),this.fireEvent("play")},pause:function(){this.media&&this.media.pause(),this.clearPlayEnd(),this.fireEvent("pause")},setPlayEnd:function(a){var b=this;this.onPlayEnd=function(c){c>=a&&(b.pause(),b.seekTo(a))},this.on("audioprocess",this.onPlayEnd)},clearPlayEnd:function(){this.onPlayEnd&&(this.un("audioprocess",this.onPlayEnd),this.onPlayEnd=null)},getPeaks:function(b,c,d){return this.buffer?a.WebAudio.getPeaks.call(this,b,c,d):this.peaks||[]},getVolume:function(){return this.media.volume},setVolume:function(a){this.media.volume=a},destroy:function(){this.pause(),this.unAll(),this.media&&this.media.parentNode&&this.media.parentNode.removeChild(this.media),this.media=null}}),a.AudioElement=a.MediaElement,a.Drawer={init:function(a,b){this.container=a,this.params=b,this.width=0,this.height=b.height*this.params.pixelRatio,this.lastPos=0,this.initDrawer(b),this.createWrapper(),this.createElements()},createWrapper:function(){this.wrapper=this.container.appendChild(document.createElement("wave")),this.style(this.wrapper,{display:"block",position:"relative",userSelect:"none",webkitUserSelect:"none",height:this.params.height+"px"}),(this.params.fillParent||this.params.scrollParent)&&this.style(this.wrapper,{width:"100%",overflowX:this.params.hideScrollbar?"hidden":"auto",overflowY:"hidden"}),this.setupWrapperEvents()},handleEvent:function(a,b){!b&&a.preventDefault();var c,d=a.targetTouches?a.targetTouches[0].clientX:a.clientX,e=this.wrapper.getBoundingClientRect(),f=this.width,g=this.getWidth();return!this.params.fillParent&&f1&&(c=1)):c=(d-e.left+this.wrapper.scrollLeft)/this.wrapper.scrollWidth||0,c},setupWrapperEvents:function(){var a=this;this.wrapper.addEventListener("click",function(b){var c=a.wrapper.offsetHeight-a.wrapper.clientHeight;if(0!=c){var d=a.wrapper.getBoundingClientRect();if(b.clientY>=d.bottom-c)return}a.params.interact&&a.fireEvent("click",b,a.handleEvent(b))}),this.wrapper.addEventListener("scroll",function(b){a.fireEvent("scroll",b)})},drawPeaks:function(a,b,c,d){this.setWidth(b),this.params.barWidth?this.drawBars(a,0,c,d):this.drawWave(a,0,c,d)},style:function(a,b){return Object.keys(b).forEach(function(c){a.style[c]!==b[c]&&(a.style[c]=b[c])}),a},resetScroll:function(){null!==this.wrapper&&(this.wrapper.scrollLeft=0)},recenter:function(a){var b=this.wrapper.scrollWidth*a;this.recenterOnPosition(b,!0)},recenterOnPosition:function(a,b){var c=this.wrapper.scrollLeft,d=~~(this.wrapper.clientWidth/2),e=a-d,f=e-c,g=this.wrapper.scrollWidth-this.wrapper.clientWidth;if(0!=g){if(!b&&-d<=f&&f=b){if(this.lastPos=c,this.params.scrollParent&&this.params.autoCenter){var d=~~(this.wrapper.scrollWidth*a);this.recenterOnPosition(d)}this.updateProgress(c)}},destroy:function(){this.unAll(),this.wrapper&&(this.container.removeChild(this.wrapper),this.wrapper=null)},initDrawer:function(){},createElements:function(){},updateSize:function(){},drawWave:function(a,b){},clearWave:function(){},updateProgress:function(a){}},a.util.extend(a.Drawer,a.Observer),a.Drawer.Canvas=Object.create(a.Drawer),a.util.extend(a.Drawer.Canvas,{createElements:function(){var a=this.wrapper.appendChild(this.style(document.createElement("canvas"),{position:"absolute",zIndex:1,left:0,top:0,bottom:0}));if(this.waveCc=a.getContext("2d"),this.progressWave=this.wrapper.appendChild(this.style(document.createElement("wave"),{position:"absolute",zIndex:2,left:0,top:0,bottom:0,overflow:"hidden",width:"0",display:"none",boxSizing:"border-box",borderRightStyle:"solid",borderRightWidth:this.params.cursorWidth+"px",borderRightColor:this.params.cursorColor})),this.params.waveColor!=this.params.progressColor){var b=this.progressWave.appendChild(document.createElement("canvas"));this.progressCc=b.getContext("2d")}},updateSize:function(){var a=Math.round(this.width/this.params.pixelRatio);this.waveCc.canvas.width=this.width,this.waveCc.canvas.height=this.height,this.style(this.waveCc.canvas,{width:a+"px"}),this.style(this.progressWave,{display:"block"}),this.progressCc&&(this.progressCc.canvas.width=this.width,this.progressCc.canvas.height=this.height,this.style(this.progressCc.canvas,{width:a+"px"})),this.clearWave()},clearWave:function(){this.waveCc.clearRect(0,0,this.width,this.height),this.progressCc&&this.progressCc.clearRect(0,0,this.width,this.height)},drawBars:function(b,c,d,e){var f=this;if(b[0]instanceof Array){var g=b;if(this.params.splitChannels)return this.setHeight(g.length*this.params.height*this.params.pixelRatio),void g.forEach(function(a,b){f.drawBars(a,b,d,e)});b=g[0]}var h=[].some.call(b,function(a){return a<0}),i=1;h&&(i=2);var j=.5/this.params.pixelRatio,k=this.width,l=this.params.height*this.params.pixelRatio,m=l*c||0,n=l/2,o=b.length/i,p=this.params.barWidth*this.params.pixelRatio,q=Math.max(this.params.pixelRatio,~~(p/2)),r=p+q,s=1/this.params.barHeight;if(this.params.normalize){var t=a.util.max(b),u=a.util.min(b);s=-u>t?-u:t}var v=o/k;this.waveCc.fillStyle=this.params.waveColor,this.progressCc&&(this.progressCc.fillStyle=this.params.progressColor),[this.waveCc,this.progressCc].forEach(function(a){if(a)for(var c=d/v;cs?-t:s}this.waveCc.fillStyle=this.params.waveColor,this.progressCc&&(this.progressCc.fillStyle=this.params.progressColor),[this.waveCc,this.progressCc].forEach(function(a){if(a){a.beginPath(),a.moveTo(d*q+l,o+n);for(var c=d;c=d;c--){var f=Math.round(b[2*c+1]/r*o);a.lineTo(c*q+l,o-f+n)}a.closePath(),a.fill(),a.fillRect(0,o+n-l,this.width,l)}},this)},updateProgress:function(a){this.style(this.progressWave,{width:a+"px"})},getImage:function(a,b){return this.waveCc.canvas.toDataURL(a,b)}}),a.Drawer.MultiCanvas=Object.create(a.Drawer),a.util.extend(a.Drawer.MultiCanvas,{initDrawer:function(a){if(this.maxCanvasWidth=null!=a.maxCanvasWidth?a.maxCanvasWidth:4e3,this.maxCanvasElementWidth=Math.round(this.maxCanvasWidth/this.params.pixelRatio),this.maxCanvasWidth<=1)throw"maxCanvasWidth must be greater than 1.";if(this.maxCanvasWidth%2==1)throw"maxCanvasWidth must be an even number.";this.hasProgressCanvas=this.params.waveColor!=this.params.progressColor,this.halfPixel=.5/this.params.pixelRatio,this.canvases=[]},createElements:function(){this.progressWave=this.wrapper.appendChild(this.style(document.createElement("wave"),{position:"absolute",zIndex:2,left:0,top:0,bottom:0,overflow:"hidden",width:"0",display:"none",boxSizing:"border-box",borderRightStyle:"solid",borderRightWidth:this.params.cursorWidth+"px",borderRightColor:this.params.cursorColor})),this.addCanvas()},updateSize:function(){for(var a=Math.round(this.width/this.params.pixelRatio),b=Math.ceil(a/this.maxCanvasElementWidth);this.canvases.lengthb;)this.removeCanvas();for(var c in this.canvases){var d=this.maxCanvasWidth+2*Math.ceil(this.params.pixelRatio/2);c==this.canvases.length-1&&(d=this.width-this.maxCanvasWidth*(this.canvases.length-1)),this.updateDimensions(this.canvases[c],d,this.height),this.clearWaveForEntry(this.canvases[c])}},addCanvas:function(){var a={},b=this.maxCanvasElementWidth*this.canvases.length;a.wave=this.wrapper.appendChild(this.style(document.createElement("canvas"),{position:"absolute",zIndex:1,left:b+"px",top:0,bottom:0,height:"100%"})),a.waveCtx=a.wave.getContext("2d"),this.hasProgressCanvas&&(a.progress=this.progressWave.appendChild(this.style(document.createElement("canvas"),{position:"absolute",left:b+"px",top:0,bottom:0,height:"100%"})),a.progressCtx=a.progress.getContext("2d")),this.canvases.push(a)},removeCanvas:function(){var a=this.canvases.pop();a.wave.parentElement.removeChild(a.wave),this.hasProgressCanvas&&a.progress.parentElement.removeChild(a.progress)},updateDimensions:function(a,b,c){var d=Math.round(b/this.params.pixelRatio),e=Math.round(this.width/this.params.pixelRatio);a.start=a.waveCtx.canvas.offsetLeft/e||0,a.end=a.start+d/e,a.waveCtx.canvas.width=b,a.waveCtx.canvas.height=c,this.style(a.waveCtx.canvas,{width:d+"px"}),this.style(this.progressWave,{display:"block"}),this.hasProgressCanvas&&(a.progressCtx.canvas.width=b,a.progressCtx.canvas.height=c,this.style(a.progressCtx.canvas,{width:d+"px"}))},clearWave:function(){for(var a in this.canvases)this.clearWaveForEntry(this.canvases[a])},clearWaveForEntry:function(a){a.waveCtx.clearRect(0,0,a.waveCtx.canvas.width,a.waveCtx.canvas.height),this.hasProgressCanvas&&a.progressCtx.clearRect(0,0,a.progressCtx.canvas.width,a.progressCtx.canvas.height)},drawBars:function(b,c,d,e){var f=this;if(b[0]instanceof Array){var g=b;if(this.params.splitChannels)return this.setHeight(g.length*this.params.height*this.params.pixelRatio),void g.forEach(function(a,b){f.drawBars(a,b,d,e)});b=g[0]}var h=[].some.call(b,function(a){return a<0}),i=1;h&&(i=2);var j=this.width,k=this.params.height*this.params.pixelRatio,l=k*c||0,m=k/2,n=b.length/i,o=this.params.barWidth*this.params.pixelRatio,p=Math.max(this.params.pixelRatio,~~(o/2)),q=o+p,r=1/this.params.barHeight;if(this.params.normalize){var s=a.util.max(b),t=a.util.min(b);r=-t>s?-t:s}for(var u=n/j,v=d/u;vp?-q:p}this.drawLine(b,o,n,m,d,e),this.fillRect(0,n+m-this.halfPixel,this.width,this.halfPixel)},drawLine:function(a,b,c,d,e,f){for(var g in this.canvases){var h=this.canvases[g];this.setFillStyles(h),this.drawLineToContext(h,h.waveCtx,a,b,c,d,e,f),this.drawLineToContext(h,h.progressCtx,a,b,c,d,e,f)}},drawLineToContext:function(a,b,c,d,e,f,g,h){if(b){var i=c.length/2,j=1;this.params.fillParent&&this.width!=i&&(j=this.width/i);var k=Math.round(i*a.start),l=Math.round(i*a.end);if(!(k>h||l=m;o--){var p=c[2*o+1]||0,q=Math.round(p/d*e);b.lineTo((o-k)*j+this.halfPixel,e-q+f)}b.closePath(),b.fill()}}},fillRect:function(a,b,c,d){for(var e=Math.floor(a/this.maxCanvasWidth),f=Math.min(Math.ceil((a+c)/this.maxCanvasWidth)+1,this.canvases.length),g=e;g1?c:c[0]}}),a.Drawer.SplitWavePointPlot=Object.create(a.Drawer.Canvas),a.util.extend(a.Drawer.SplitWavePointPlot,{defaultPlotParams:{plotNormalizeTo:"whole",plotTimeStart:0,plotMin:0, -plotMax:1,plotColor:"#f63",plotProgressColor:"#F00",plotPointHeight:2,plotPointWidth:2,plotSeparator:!0,plotSeparatorColor:"black",plotRangeDisplay:!1,plotRangeUnits:"",plotRangePrecision:4,plotRangeIgnoreOutliers:!1,plotRangeFontSize:12,plotRangeFontType:"Ariel",waveDrawMedianLine:!0,plotFileDelimiter:"\t"},plotTimeStart:0,plotTimeEnd:-1,plotArrayLoaded:!1,plotArray:[],plotPoints:[],plotMin:0,plotMax:1,initDrawer:function(a){var b=this;for(var c in this.defaultPlotParams)void 0===this.params[c]&&(this.params[c]=this.defaultPlotParams[c]);if(this.plotTimeStart=this.params.plotTimeStart,void 0!==this.params.plotTimeEnd&&(this.plotTimeEnd=this.params.plotTimeEnd),Array.isArray(a.plotArray))this.plotArray=a.plotArray,this.plotArrayLoaded=!0;else{var d=function(a){b.plotArray=a,b.plotArrayLoaded=!0,b.fireEvent("plot_array_loaded")};this.loadPlotArrayFromFile(a.plotFileUrl,d,this.params.plotFileDelimiter)}},drawPeaks:function(a,b,c,d){if(1==this.plotArrayLoaded)this.setWidth(b),this.splitChannels=!0,this.params.height=this.params.height/2,a[0]instanceof Array&&(a=a[0]),this.params.barWidth?this.drawBars(a,1,c,d):this.drawWave(a,1,c,d),this.params.height=2*this.params.height,this.calculatePlots(),this.drawPlots();else{var e=this;e.on("plot-array-loaded",function(){e.drawPeaks(a,b,c,d)})}},drawPlots:function(){var a=this.params.height*this.params.pixelRatio/2,b=.5/this.params.pixelRatio;this.waveCc.fillStyle=this.params.plotColor,this.progressCc&&(this.progressCc.fillStyle=this.params.plotProgressColor);for(var c in this.plotPoints){var d=parseInt(c),e=a-this.params.plotPointHeight-this.plotPoints[c]*(a-this.params.plotPointHeight),f=this.params.plotPointHeight;this.waveCc.fillRect(d,e,this.params.plotPointWidth,f),this.progressCc&&this.progressCc.fillRect(d,e,this.params.plotPointWidth,f)}this.params.plotSeparator&&(this.waveCc.fillStyle=this.params.plotSeparatorColor,this.waveCc.fillRect(0,a,this.width,b)),this.params.plotRangeDisplay&&this.displayPlotRange()},displayPlotRange:function(){var a=this.params.plotRangeFontSize*this.params.pixelRatio,b=this.plotMax.toPrecision(this.params.plotRangePrecision)+" "+this.params.plotRangeUnits,c=this.plotMin.toPrecision(this.params.plotRangePrecision)+" "+this.params.plotRangeUnits;this.waveCc.font=a.toString()+"px "+this.params.plotRangeFontType,this.waveCc.fillText(b,3,a),this.waveCc.fillText(c,3,this.height/2)},calculatePlots:function(){this.plotPoints={},this.calculatePlotTimeEnd();for(var a=[],b=-1,c=0,d=99999999999999,e=0,f=99999999999999,g=this.plotTimeEnd-this.plotTimeStart,h=0;hc&&(c=i.value),i.value=this.plotTimeStart&&i.time<=this.plotTimeEnd){var j=Math.round(this.width*(i.time-this.plotTimeStart)/g);if(a.push(i.value),j!==b&&a.length>0){var k=this.avg(a);k>e&&(e=k),k1?this.params.plotRangeIgnoreOutliers||(a[b]=1):c<0?this.params.plotRangeIgnoreOutliers||(a[b]=0):a[b]=c}this.plotPoints=a}},loadPlotArrayFromFile:function(b,c,d){void 0===d&&(d="\t");var e=[],f={url:b,responseType:"text"},g=a.util.ajax(f);g.on("load",function(a){if(200==a.currentTarget.status){for(var b=a.currentTarget.responseText.split("\n"),f=0;f=a?this.seekTo(1):this.seekTo(a/this.getDuration())},setPlaybackRate:function(a){this.backend.setPlaybackRate(a)},getPlaybackRate:function(){return this.backend.getPlaybackRate()},toggleMute:function(){this.setMute(!this.isMuted)},setMute:function(a){a!==this.isMuted&&(a?(this.savedVolume=this.backend.getVolume(),this.backend.setVolume(0),this.isMuted=!0):(this.backend.setVolume(this.savedVolume),this.isMuted=!1))},getMute:function(){return this.isMuted},getFilters:function(){return this.backend.filters||[]},toggleScroll:function(){this.params.scrollParent=!this.params.scrollParent,this.drawBuffer()},toggleInteraction:function(){this.params.interact=!this.params.interact},drawBuffer:function(){var a=Math.round(this.getDuration()*this.params.minPxPerSec*this.params.pixelRatio),b=this.drawer.getWidth(),c=a,d=this.drawer.getScrollX(),e=Math.min(d+b,c);if(this.params.fillParent&&(!this.params.scrollParent||ab&&(b=a[c]);return b},getId:function(){return"wavesurfer_"+Math.random().toString(32).substring(2)},ajax:function(b){var c=Object.create(a.Observer),d=new XMLHttpRequest,e=!1;return d.open(b.method||"GET",b.url,!0),d.responseType=b.responseType||"json",d.addEventListener("progress",function(a){c.fireEvent("progress",a),a.lengthComputable&&a.loaded==a.total&&(e=!0)}),d.addEventListener("load",function(a){e||c.fireEvent("progress",a),c.fireEvent("load",a),200==d.status||206==d.status?c.fireEvent("success",d.response,a):c.fireEvent("error",a)}),d.addEventListener("error",function(a){c.fireEvent("error",a)}),d.send(),c.xhr=d,c}},a.Observer={on:function(a,b){this.handlers||(this.handlers={});var c=this.handlers[a];return c||(c=this.handlers[a]=[]),c.push(b),{name:a,callback:b,un:this.un.bind(this,a,b)}},un:function(a,b){if(this.handlers){var c=this.handlers[a];if(c)if(b)for(var d=c.length-1;d>=0;d--)c[d]==b&&c.splice(d,1);else c.length=0}},unAll:function(){this.handlers=null},once:function(a,b){var c=this,d=function(){b.apply(this,arguments),setTimeout(function(){c.un(a,d)},0)};return this.on(a,d)},fireEvent:function(a){if(this.handlers){var b=this.handlers[a],c=Array.prototype.slice.call(arguments,1);b&&b.forEach(function(a){a.apply(null,c)})}}},a.util.extend(a,a.Observer),!function(a){function b(d){if(c[d])return c[d].exports;var e=c[d]={exports:{},id:d,loaded:!1};return a[d].call(e.exports,e,e.exports,b),e.loaded=!0,e.exports}var c={};return b.m=a,b.c=c,b.p="",b(0)}([function(a,b,c){a.exports=c(1)},function(a,b,c){function d(a){return Math.floor(a)}var e=c(2),f=function(){function a(){this.is_initialized=!1,this.sample_rate=44100,this.channels=0,this.quick_search=!1,this.factor=0,this.search=0,this.segment=0,this.overlap=0,this.process_size=0,this.samples_in=0,this.samples_out=0,this.segments_total=0,this.skip_total=0}return a}(),g=function(){function a(a){var b=new f;b.channels=a,b.input_fifo=new e(Float32Array),b.output_fifo=new e(Float32Array),this.t=b}return a.prototype.difference=function(a,b,c){for(var d=0,e=0,e=0;c>e;e++)d+=Math.pow(a[e]-b[e],2);return d},a.prototype.tempo_best_overlap_position=function(a,b){var c,d,e,f=a.overlap_buf,g=a.search+1>>>1,h=64,i=d=a.quick_search?g:0,j=this.difference(b.subarray(a.channels*i),f,a.channels*a.overlap),k=0;if(a.quick_search){do{for(k=-1;1>=k;k+=2)for(c=1;(4>c||64==h)&&(i=g+k*c*h,!(0>i||i>=a.search));c++)e=this.difference(b.subarray(a.channels*i),f,a.channels*a.overlap),j>e&&(j=e,d=i);g=d}while(h>>>=2)}else for(i=1;ie&&(j=e,d=i);return d},a.prototype.tempo_overlap=function(a,b,c,d){var e=0,f=0,g=0,h=1/a.overlap;for(e=0;e=a.process_size;){var b,c;a.segments_total?(c=this.tempo_best_overlap_position(a,a.input_fifo.read_ptr(0)),this.tempo_overlap(a,a.overlap_buf,a.input_fifo.read_ptr(a.channels*c),a.output_fifo.write_ptr(a.overlap))):(c=a.search/2,a.output_fifo.write(a.input_fifo.read_ptr(a.channels*c,a.overlap),a.overlap)),a.output_fifo.write(a.input_fifo.read_ptr(a.channels*(c+a.overlap)),a.segment-2*a.overlap);var e=a.channels*a.overlap;a.overlap_buf.set(a.input_fifo.read_ptr(a.channels*(c+a.segment-a.overlap)).subarray(0,e)),a.segments_total++,b=d(a.factor*(a.segment-a.overlap)+.5),a.input_fifo.read(null,b)}},a.prototype.input=function(a,b,c){void 0===b&&(b=null),void 0===c&&(c=0),null==b&&(b=a.length);var d=this.t;d.samples_in+=b,d.input_fifo.write(a,b)},a.prototype.output=function(a){var b=this.t,c=Math.min(a.length,b.output_fifo.occupancy());return b.samples_out+=c,b.output_fifo.read(a,c),c},a.prototype.flush=function(){var a=this.t,b=d(a.samples_in/a.factor+.5),c=b>a.samples_out?b-a.samples_out:0,e=new Float32Array(128*a.channels);if(c>0){for(;a.output_fifo.occupancy()j.segment&&(j.overlap-=8),j.is_initialized){var l=new Float32Array(j.overlap*j.channels),m=0;j.overlap*j.channels16384)this.buffer.set(this.buffer.subarray(this.begin,this.end)),this.end-=this.begin,this.begin=0;else{var c=new this.typedArrayConstructor(this.buffer.length+a);c.set(this.buffer),this.buffer=c}}},a.prototype.write=function(a,b){var c=this.reserve(b);this.buffer.set(a.subarray(0,b),c)},a.prototype.write_ptr=function(a){var b=this.reserve(a);return this.buffer.subarray(b,b+a)},a.prototype.read=function(a,b){b+this.begin>this.end&&console.error("Read out of bounds",b,this.end,this.begin),null!=a&&a.set(this.buffer.subarray(this.begin,this.begin+b)),this.begin+=b},a.prototype.read_ptr=function(a,b){return void 0===b&&(b=-1),b>this.occupancy()&&console.error("Read Pointer out of bounds",b),0>b&&(b=this.occupancy()),this.buffer.subarray(this.begin+a,this.begin+b)},a.prototype.occupancy=function(){return this.end-this.begin},a}();a.exports=c}]),a.util.Kali=window.Kali,a.WebAudio={scriptBufferSize:256,PLAYING_STATE:0,PAUSED_STATE:1,FINISHED_STATE:2,supportsWebAudio:function(){return!(!window.AudioContext&&!window.webkitAudioContext)},getAudioContext:function(){return a.WebAudio.audioContext||(a.WebAudio.audioContext=new(window.AudioContext||window.webkitAudioContext)),a.WebAudio.audioContext},getOfflineAudioContext:function(b){return a.WebAudio.offlineAudioContext||(a.WebAudio.offlineAudioContext=new(window.OfflineAudioContext||window.webkitOfflineAudioContext)(1,2,b)),a.WebAudio.offlineAudioContext},init:function(b){this.params=b,this.ac=b.audioContext||this.getAudioContext(),this.lastPlay=this.ac.currentTime,this.startPosition=0,this.scheduledPause=null,this.playbackRate=this.params.audioRate||1,this.states=[Object.create(a.WebAudio.state.playing),Object.create(a.WebAudio.state.paused),Object.create(a.WebAudio.state.finished)],this.createVolumeNode(),this.createScriptNode(),this.createAnalyserNode(),this.setState(this.PAUSED_STATE),this.setLength(0)},disconnectFilters:function(){this.filters&&(this.filters.forEach(function(a){a&&a.disconnect()}),this.filters=null,this.analyser.connect(this.gainNode))},setState:function(a){this.state!==this.states[a]&&(this.state=this.states[a],this.state.init.call(this))},setFilter:function(){this.setFilters([].slice.call(arguments))},setFilters:function(a){this.disconnectFilters(),a&&a.length&&(this.filters=a,this.analyser.disconnect(),a.reduce(function(a,b){return a.connect(b),b},this.analyser).connect(this.gainNode))},createScriptNode:function(){this.ac.createScriptProcessor?this.scriptNode=this.ac.createScriptProcessor(this.scriptBufferSize):this.scriptNode=this.ac.createJavaScriptNode(this.scriptBufferSize),this.scriptNode.connect(this.ac.destination)},addOnAudioProcess:function(){var a=this;this.scriptNode.onaudioprocess=function(){var b=a.getCurrentTime();b>=a.getDuration()?(a.setState(a.FINISHED_STATE),a.fireEvent("pause")):b>=a.scheduledPause?a.pause():a.state===a.states[a.PLAYING_STATE]&&a.fireEvent("audioprocess",b)}},removeOnAudioProcess:function(){this.scriptNode.onaudioprocess=null},createAnalyserNode:function(){this.analyser=this.ac.createAnalyser(),this.analyser.connect(this.gainNode)},createVolumeNode:function(){this.ac.createGain?this.gainNode=this.ac.createGain():this.gainNode=this.ac.createGainNode(),this.gainNode.connect(this.ac.destination)},setVolume:function(a){this.gainNode.gain.value=a},getVolume:function(){return this.gainNode.gain.value},decodeArrayBuffer:function(a,b,c){this.offlineAc||(this.offlineAc=this.getOfflineAudioContext(this.ac?this.ac.sampleRate:44100)),this.offlineAc.decodeAudioData(a,function(a){b(a)}.bind(this),c)},setPeaks:function(a){this.peaks=a},setLength:function(a){if(!this.mergedPeaks||a!=2*this.mergedPeaks.length-1+2){this.splitPeaks=[],this.mergedPeaks=[];for(var b=this.buffer?this.buffer.numberOfChannels:1,c=0;cn&&(n=p),pthis.mergedPeaks[2*j])&&(this.mergedPeaks[2*j]=n),(0==g||m=this.getDuration()&&(a=0)),null==b&&(b=this.getDuration()),this.startPosition=a,this.lastPlay=this.ac.currentTime,this.state===this.states[this.FINISHED_STATE]&&this.setState(this.PAUSED_STATE),{start:a,end:b}},getPlayedTime:function(){return this.ac.currentTime-this.lastPlay},play:function(a,b){if(this.buffer){this.createSource();var c=this.seekTo(a,b);a=c.start,b=c.end,this.scheduledPause=b,this.source.start(0,a,b-a),"suspended"==this.ac.state&&this.ac.resume&&this.ac.resume(),this.setState(this.PLAYING_STATE),this.fireEvent("play")}},pause:function(){this.scheduledPause=null,this.startPosition+=this.getPlayedTime(),this.source&&this.source.stop(0),this.setState(this.PAUSED_STATE),this.fireEvent("pause")},getCurrentTime:function(){return this.state.getCurrentTime.call(this)},getPlaybackRate:function(){return this.playbackRate},setPlaybackRate:function(b){b=b||1,this.playbackRate=b;var c=this.isPaused();c||this.pause();var d=new Float32Array(this.originalBuffer.length/b),e=new a.util.Kali(1);e.setup(this.originalBuffer.sampleRate,b,!0),e.input(this.originalBuffer.getChannelData(0)),e.process(),e.output(d),this.buffer=this.ac.createBuffer(1,d.length,this.originalBuffer.sampleRate);for(var f=this.buffer.getChannelData(0),g=0;g=1/0&&(a=this.media.seekable.end(0)),a},getCurrentTime:function(){return this.media&&this.media.currentTime},getPlayedPercents:function(){return this.getCurrentTime()/this.getDuration()||0},getPlaybackRate:function(){return this.playbackRate||this.media.playbackRate},setPlaybackRate:function(a){this.playbackRate=a||1,this.media.playbackRate=this.playbackRate},seekTo:function(a){null!=a&&(this.media.currentTime=a),this.clearPlayEnd()},play:function(a,b){this.seekTo(a),this.media.play(),b&&this.setPlayEnd(b)},pause:function(){this.media&&this.media.pause(),this.clearPlayEnd()},setPlayEnd:function(a){var b=this;this.onPlayEnd=function(c){c>=a&&(b.pause(),b.seekTo(a))},this.on("audioprocess",this.onPlayEnd)},clearPlayEnd:function(){this.onPlayEnd&&(this.un("audioprocess",this.onPlayEnd),this.onPlayEnd=null)},getPeaks:function(b,c,d){return this.buffer?a.WebAudio.getPeaks.call(this,b,c,d):this.peaks||[]},getVolume:function(){return this.media.volume},setVolume:function(a){this.media.volume=a},destroy:function(){this.pause(),this.unAll(),this.media&&this.media.parentNode&&this.media.parentNode.removeChild(this.media),this.media=null}}),a.AudioElement=a.MediaElement,a.Drawer={init:function(a,b){this.container=a,this.params=b,this.width=0,this.height=b.height*this.params.pixelRatio,this.lastPos=0,this.initDrawer(b),this.createWrapper(),this.createElements()},createWrapper:function(){this.wrapper=this.container.appendChild(document.createElement("wave")),this.style(this.wrapper,{display:"block",position:"relative",userSelect:"none",webkitUserSelect:"none",height:this.params.height+"px"}),(this.params.fillParent||this.params.scrollParent)&&this.style(this.wrapper,{width:"100%",overflowX:this.params.hideScrollbar?"hidden":"auto",overflowY:"hidden"}),this.setupWrapperEvents()},handleEvent:function(a,b){!b&&a.preventDefault();var c,d=a.targetTouches?a.targetTouches[0].clientX:a.clientX,e=this.wrapper.getBoundingClientRect(),f=this.width,g=this.getWidth();return!this.params.fillParent&&f1&&(c=1)):c=(d-e.left+this.wrapper.scrollLeft)/this.wrapper.scrollWidth||0,c},setupWrapperEvents:function(){var a=this;this.wrapper.addEventListener("click",function(b){var c=a.wrapper.offsetHeight-a.wrapper.clientHeight;if(0!=c){var d=a.wrapper.getBoundingClientRect();if(b.clientY>=d.bottom-c)return}a.params.interact&&a.fireEvent("click",b,a.handleEvent(b))}),this.wrapper.addEventListener("scroll",function(b){a.fireEvent("scroll",b)})},drawPeaks:function(a,b,c,d){this.setWidth(b),this.params.barWidth?this.drawBars(a,0,c,d):this.drawWave(a,0,c,d)},style:function(a,b){return Object.keys(b).forEach(function(c){a.style[c]!==b[c]&&(a.style[c]=b[c])}),a},resetScroll:function(){null!==this.wrapper&&(this.wrapper.scrollLeft=0)},recenter:function(a){var b=this.wrapper.scrollWidth*a;this.recenterOnPosition(b,!0)},recenterOnPosition:function(a,b){var c=this.wrapper.scrollLeft,d=~~(this.wrapper.clientWidth/2),e=a-d,f=e-c,g=this.wrapper.scrollWidth-this.wrapper.clientWidth;if(0!=g){if(!b&&-d<=f&&f=b){if(this.lastPos=c,this.params.scrollParent&&this.params.autoCenter){var d=~~(this.wrapper.scrollWidth*a);this.recenterOnPosition(d)}this.updateProgress(c)}},destroy:function(){this.unAll(),this.wrapper&&(this.wrapper.parentNode==this.container&&this.container.removeChild(this.wrapper),this.wrapper=null)},initDrawer:function(){},createElements:function(){},updateSize:function(){},drawWave:function(a,b){},clearWave:function(){},updateProgress:function(a){}},a.util.extend(a.Drawer,a.Observer),a.Drawer.Canvas=Object.create(a.Drawer),a.util.extend(a.Drawer.Canvas,{createElements:function(){var a=this.wrapper.appendChild(this.style(document.createElement("canvas"),{position:"absolute",zIndex:1,left:0,top:0,bottom:0,pointerEvents:"none"}));if(this.waveCc=a.getContext("2d"),this.progressWave=this.wrapper.appendChild(this.style(document.createElement("wave"),{position:"absolute",zIndex:2,left:0,top:0,bottom:0,overflow:"hidden",width:"0",display:"none",boxSizing:"border-box",borderRightStyle:"solid",borderRightWidth:this.params.cursorWidth+"px",borderRightColor:this.params.cursorColor,pointerEvents:"none"})),this.params.waveColor!=this.params.progressColor){var b=this.progressWave.appendChild(document.createElement("canvas"));this.progressCc=b.getContext("2d")}},updateSize:function(){var a=Math.round(this.width/this.params.pixelRatio);this.waveCc.canvas.width=this.width,this.waveCc.canvas.height=this.height,this.style(this.waveCc.canvas,{width:a+"px"}),this.style(this.progressWave,{display:"block"}),this.progressCc&&(this.progressCc.canvas.width=this.width,this.progressCc.canvas.height=this.height,this.style(this.progressCc.canvas,{width:a+"px"})),this.clearWave()},clearWave:function(){this.waveCc.clearRect(0,0,this.width,this.height),this.progressCc&&this.progressCc.clearRect(0,0,this.width,this.height)},drawBars:a.util.frame(function(b,c,d,e){var f=this;if(b[0]instanceof Array){var g=b;if(this.params.splitChannels)return this.setHeight(g.length*this.params.height*this.params.pixelRatio),void g.forEach(function(a,b){f.drawBars(a,b,d,e)});b=g[0]}var h=[].some.call(b,function(a){return a<0}),i=1;h&&(i=2);var j=.5/this.params.pixelRatio,k=this.width,l=this.params.height*this.params.pixelRatio,m=l*c||0,n=l/2,o=b.length/i,p=this.params.barWidth*this.params.pixelRatio,q=Math.max(this.params.pixelRatio,~~(p/2)),r=p+q,s=1/this.params.barHeight;if(this.params.normalize){var t=a.util.max(b),u=a.util.min(b);s=-u>t?-u:t}var v=o/k;this.waveCc.fillStyle=this.params.waveColor,this.progressCc&&(this.progressCc.fillStyle=this.params.progressColor),[this.waveCc,this.progressCc].forEach(function(a){if(a)for(var c=d/v;cs?-t:s}this.waveCc.fillStyle=this.params.waveColor,this.progressCc&&(this.progressCc.fillStyle=this.params.progressColor),[this.waveCc,this.progressCc].forEach(function(a){if(a){a.beginPath(),a.moveTo(d*q+l,o+n);for(var c=d;c=d;c--){var f=Math.round(b[2*c+1]/r*o);a.lineTo(c*q+l,o-f+n)}a.closePath(), +a.fill(),a.fillRect(0,o+n-l,this.width,l)}},this)}),updateProgress:function(a){this.style(this.progressWave,{width:a+"px"})},getImage:function(a,b){return this.waveCc.canvas.toDataURL(a,b)}}),a.Drawer.MultiCanvas=Object.create(a.Drawer),a.util.extend(a.Drawer.MultiCanvas,{initDrawer:function(a){if(this.maxCanvasWidth=null!=a.maxCanvasWidth?a.maxCanvasWidth:4e3,this.maxCanvasElementWidth=Math.round(this.maxCanvasWidth/this.params.pixelRatio),this.maxCanvasWidth<=1)throw"maxCanvasWidth must be greater than 1.";if(this.maxCanvasWidth%2==1)throw"maxCanvasWidth must be an even number.";this.hasProgressCanvas=this.params.waveColor!=this.params.progressColor,this.halfPixel=.5/this.params.pixelRatio,this.canvases=[]},createElements:function(){this.progressWave=this.wrapper.appendChild(this.style(document.createElement("wave"),{position:"absolute",zIndex:2,left:0,top:0,bottom:0,overflow:"hidden",width:"0",display:"none",boxSizing:"border-box",borderRightStyle:"solid",borderRightWidth:this.params.cursorWidth+"px",borderRightColor:this.params.cursorColor,pointerEvents:"none"})),this.addCanvas()},updateSize:function(){for(var a=Math.round(this.width/this.params.pixelRatio),b=Math.ceil(a/this.maxCanvasElementWidth);this.canvases.lengthb;)this.removeCanvas();this.canvases.forEach(function(a,b){var c=this.maxCanvasWidth+2*Math.ceil(this.params.pixelRatio/2);b==this.canvases.length-1&&(c=this.width-this.maxCanvasWidth*(this.canvases.length-1)),this.updateDimensions(a,c,this.height),this.clearWaveForEntry(a)},this)},addCanvas:function(){var a={},b=this.maxCanvasElementWidth*this.canvases.length;a.wave=this.wrapper.appendChild(this.style(document.createElement("canvas"),{position:"absolute",zIndex:1,left:b+"px",top:0,bottom:0,height:"100%",pointerEvents:"none"})),a.waveCtx=a.wave.getContext("2d"),this.hasProgressCanvas&&(a.progress=this.progressWave.appendChild(this.style(document.createElement("canvas"),{position:"absolute",left:b+"px",top:0,bottom:0,height:"100%"})),a.progressCtx=a.progress.getContext("2d")),this.canvases.push(a)},removeCanvas:function(){var a=this.canvases.pop();a.wave.parentElement.removeChild(a.wave),this.hasProgressCanvas&&a.progress.parentElement.removeChild(a.progress)},updateDimensions:function(a,b,c){var d=Math.round(b/this.params.pixelRatio),e=Math.round(this.width/this.params.pixelRatio);a.start=a.waveCtx.canvas.offsetLeft/e||0,a.end=a.start+d/e,a.waveCtx.canvas.width=b,a.waveCtx.canvas.height=c,this.style(a.waveCtx.canvas,{width:d+"px"}),this.style(this.progressWave,{display:"block"}),this.hasProgressCanvas&&(a.progressCtx.canvas.width=b,a.progressCtx.canvas.height=c,this.style(a.progressCtx.canvas,{width:d+"px"}))},clearWave:function(){this.canvases.forEach(function(a){this.clearWaveForEntry(a)},this)},clearWaveForEntry:function(a){a.waveCtx.clearRect(0,0,a.waveCtx.canvas.width,a.waveCtx.canvas.height),this.hasProgressCanvas&&a.progressCtx.clearRect(0,0,a.progressCtx.canvas.width,a.progressCtx.canvas.height)},drawBars:a.util.frame(function(b,c,d,e){if(b[0]instanceof Array){var f=b;if(this.params.splitChannels)return this.setHeight(f.length*this.params.height*this.params.pixelRatio),void f.forEach(function(a,b){this.drawBars(a,b,d,e)},this);b=f[0]}var g=[].some.call(b,function(a){return a<0}),h=1;g&&(h=2);var i=this.width,j=this.params.height*this.params.pixelRatio,k=j*c||0,l=j/2,m=b.length/h,n=this.params.barWidth*this.params.pixelRatio,o=Math.max(this.params.pixelRatio,~~(n/2)),p=n+o,q=1/this.params.barHeight;if(this.params.normalize){var r=a.util.max(b),s=a.util.min(b);q=-s>r?-s:r}for(var t=m/i,u=d/t;uo?-p:o}this.drawLine(b,n,m,l,d,e),this.fillRect(0,m+l-this.halfPixel,this.width,this.halfPixel)}),drawLine:function(a,b,c,d,e,f){this.canvases.forEach(function(g){this.setFillStyles(g),this.drawLineToContext(g,g.waveCtx,a,b,c,d,e,f),this.drawLineToContext(g,g.progressCtx,a,b,c,d,e,f)},this)},drawLineToContext:function(a,b,c,d,e,f,g,h){if(b){var i=c.length/2,j=1;this.params.fillParent&&this.width!=i&&(j=this.width/i);var k=Math.round(i*a.start),l=Math.round(i*a.end);if(!(k>h||l=m;o--){var p=c[2*o+1]||0,q=Math.round(p/d*e);b.lineTo((o-k)*j+this.halfPixel,e-q+f)}b.closePath(),b.fill()}}},fillRect:function(a,b,c,d){for(var e=Math.floor(a/this.maxCanvasWidth),f=Math.min(Math.ceil((a+c)/this.maxCanvasWidth)+1,this.canvases.length),g=e;g1?c:c[0]}}),a.Drawer.SplitWavePointPlot=Object.create(a.Drawer.Canvas),a.util.extend(a.Drawer.SplitWavePointPlot,{defaultPlotParams:{plotNormalizeTo:"whole",plotTimeStart:0,plotMin:0,plotMax:1,plotColor:"#f63",plotProgressColor:"#F00",plotPointHeight:2,plotPointWidth:2,plotSeparator:!0,plotSeparatorColor:"black",plotRangeDisplay:!1,plotRangeUnits:"",plotRangePrecision:4,plotRangeIgnoreOutliers:!1,plotRangeFontSize:12,plotRangeFontType:"Ariel",waveDrawMedianLine:!0,plotFileDelimiter:"\t"},plotTimeStart:0,plotTimeEnd:-1,plotArrayLoaded:!1,plotArray:[],plotPoints:[],plotMin:0,plotMax:1,initDrawer:function(a){var b=this;for(var c in this.defaultPlotParams)void 0===this.params[c]&&(this.params[c]=this.defaultPlotParams[c]);if(this.plotTimeStart=this.params.plotTimeStart,void 0!==this.params.plotTimeEnd&&(this.plotTimeEnd=this.params.plotTimeEnd),Array.isArray(a.plotArray))this.plotArray=a.plotArray,this.plotArrayLoaded=!0;else{var d=function(a){b.plotArray=a,b.plotArrayLoaded=!0,b.fireEvent("plot_array_loaded")};this.loadPlotArrayFromFile(a.plotFileUrl,d,this.params.plotFileDelimiter)}},drawPeaks:function(a,b,c,d){if(1==this.plotArrayLoaded)this.setWidth(b),this.splitChannels=!0,this.params.height=this.params.height/2,a[0]instanceof Array&&(a=a[0]),this.params.barWidth?this.drawBars(a,1,c,d):this.drawWave(a,1,c,d),this.params.height=2*this.params.height,this.calculatePlots(),this.drawPlots();else{var e=this;e.on("plot-array-loaded",function(){e.drawPeaks(a,b,c,d)})}},drawPlots:function(){var a=this.params.height*this.params.pixelRatio/2,b=.5/this.params.pixelRatio;this.waveCc.fillStyle=this.params.plotColor,this.progressCc&&(this.progressCc.fillStyle=this.params.plotProgressColor);for(var c in this.plotPoints){var d=parseInt(c),e=a-this.params.plotPointHeight-this.plotPoints[c]*(a-this.params.plotPointHeight),f=this.params.plotPointHeight;this.waveCc.fillRect(d,e,this.params.plotPointWidth,f),this.progressCc&&this.progressCc.fillRect(d,e,this.params.plotPointWidth,f)}this.params.plotSeparator&&(this.waveCc.fillStyle=this.params.plotSeparatorColor,this.waveCc.fillRect(0,a,this.width,b)),this.params.plotRangeDisplay&&this.displayPlotRange()},displayPlotRange:function(){var a=this.params.plotRangeFontSize*this.params.pixelRatio,b=this.plotMax.toPrecision(this.params.plotRangePrecision)+" "+this.params.plotRangeUnits,c=this.plotMin.toPrecision(this.params.plotRangePrecision)+" "+this.params.plotRangeUnits;this.waveCc.font=a.toString()+"px "+this.params.plotRangeFontType,this.waveCc.fillText(b,3,a),this.waveCc.fillText(c,3,this.height/2)},calculatePlots:function(){this.plotPoints={},this.calculatePlotTimeEnd();for(var a=[],b=-1,c=0,d=99999999999999,e=0,f=99999999999999,g=this.plotTimeEnd-this.plotTimeStart,h=0;hc&&(c=i.value),i.value=this.plotTimeStart&&i.time<=this.plotTimeEnd){var j=Math.round(this.width*(i.time-this.plotTimeStart)/g);if(a.push(i.value),j!==b&&a.length>0){var k=this.avg(a);k>e&&(e=k),k1?this.params.plotRangeIgnoreOutliers||(a[b]=1):c<0?this.params.plotRangeIgnoreOutliers||(a[b]=0):a[b]=c}this.plotPoints=a}},loadPlotArrayFromFile:function(b,c,d){void 0===d&&(d="\t");var e=[],f={url:b,responseType:"text"},g=a.util.ajax(f);g.on("load",function(a){if(200==a.currentTarget.status){for(var b=a.currentTarget.responseText.split("\n"),f=0;fn;n++)i+=Math.pow(e[n]-t[n],2);return i},e.prototype.tempo_best_overlap_position=function(e,t){var r,i,n,s=e.overlap_buf,o=e.search+1>>>1,a=64,p=i=e.quick_search?o:0,u=this.difference(t.subarray(e.channels*p),s,e.channels*e.overlap),f=0;if(e.quick_search){do{for(f=-1;1>=f;f+=2)for(r=1;(4>r||64==a)&&(p=o+f*r*a,!(0>p||p>=e.search));r++)n=this.difference(t.subarray(e.channels*p),s,e.channels*e.overlap),u>n&&(u=n,i=p);o=i}while(a>>>=2)}else for(p=1;pn&&(u=n,i=p);return i},e.prototype.tempo_overlap=function(e,t,r,i){var n=0,s=0,o=0,a=1/e.overlap;for(n=0;n=e.process_size;){var t,r;e.segments_total?(r=this.tempo_best_overlap_position(e,e.input_fifo.read_ptr(0)),this.tempo_overlap(e,e.overlap_buf,e.input_fifo.read_ptr(e.channels*r),e.output_fifo.write_ptr(e.overlap))):(r=e.search/2,e.output_fifo.write(e.input_fifo.read_ptr(e.channels*r,e.overlap),e.overlap)),e.output_fifo.write(e.input_fifo.read_ptr(e.channels*(r+e.overlap)),e.segment-2*e.overlap);var n=e.channels*e.overlap;e.overlap_buf.set(e.input_fifo.read_ptr(e.channels*(r+e.segment-e.overlap)).subarray(0,n)),e.segments_total++,t=i(e.factor*(e.segment-e.overlap)+.5),e.input_fifo.read(null,t)}},e.prototype.input=function(e,t,r){void 0===t&&(t=null),void 0===r&&(r=0),null==t&&(t=e.length);var i=this.t;i.samples_in+=t,i.input_fifo.write(e,t)},e.prototype.output=function(e){var t=this.t,r=Math.min(e.length,t.output_fifo.occupancy());return t.samples_out+=r,t.output_fifo.read(e,r),r},e.prototype.flush=function(){var e=this.t,t=i(e.samples_in/e.factor+.5),r=t>e.samples_out?t-e.samples_out:0,n=new Float32Array(128*e.channels);if(r>0){for(;e.output_fifo.occupancy()u.segment&&(u.overlap-=8),u.is_initialized){var h=new Float32Array(u.overlap*u.channels),l=0;u.overlap*u.channels16384)this.buffer.set(this.buffer.subarray(this.begin,this.end)),this.end-=this.begin,this.begin=0;else{var r=new this.typedArrayConstructor(this.buffer.length+e);r.set(this.buffer),this.buffer=r}}},e.prototype.write=function(e,t){var r=this.reserve(t);this.buffer.set(e.subarray(0,t),r)},e.prototype.write_ptr=function(e){var t=this.reserve(e);return this.buffer.subarray(t,t+e)},e.prototype.read=function(e,t){t+this.begin>this.end&&console.error("Read out of bounds",t,this.end,this.begin),null!=e&&e.set(this.buffer.subarray(this.begin,this.begin+t)),this.begin+=t},e.prototype.read_ptr=function(e,t){return void 0===t&&(t=-1),t>this.occupancy()&&console.error("Read Pointer out of bounds",t),0>t&&(t=this.occupancy()),this.buffer.subarray(this.begin+e,this.begin+t)},e.prototype.occupancy=function(){return this.end-this.begin},e}();e.exports=r}]); + +WaveSurfer.util.Kali = window.Kali; + +/* jscs:enable */ +/* jshint ignore:end */ diff --git a/src/webaudio.js b/src/webaudio.js index ee071c7fb..3e849fa48 100755 --- a/src/webaudio.js +++ b/src/webaudio.js @@ -35,6 +35,7 @@ WaveSurfer.WebAudio = { this.lastPlay = this.ac.currentTime; this.startPosition = 0; this.scheduledPause = null; + this.playbackRate = this.params.audioRate || 1; this.states = [ Object.create(WaveSurfer.WebAudio.state.playing), @@ -45,9 +46,7 @@ WaveSurfer.WebAudio = { this.createVolumeNode(); this.createScriptNode(); this.createAnalyserNode(); - this.setState(this.PAUSED_STATE); - this.setPlaybackRate(this.params.audioRate); this.setLength(0); }, @@ -310,7 +309,13 @@ WaveSurfer.WebAudio = { this.startPosition = 0; this.lastPlay = this.ac.currentTime; this.buffer = buffer; - this.createSource(); + this.originalBuffer = buffer; + + if (this.playbackRate === 1) { + this.createSource(); + } else { + this.setPlaybackRate(this.playbackRate); + } }, createSource: function () { @@ -321,7 +326,6 @@ WaveSurfer.WebAudio = { this.source.start = this.source.start || this.source.noteGrainOn; this.source.stop = this.source.stop || this.source.noteOff; - this.source.playbackRate.value = this.playbackRate; this.source.buffer = this.buffer; this.source.connect(this.analyser); }, @@ -363,7 +367,7 @@ WaveSurfer.WebAudio = { }, getPlayedTime: function () { - return (this.ac.currentTime - this.lastPlay) * this.playbackRate; + return (this.ac.currentTime - this.lastPlay); }, /** @@ -431,11 +435,31 @@ WaveSurfer.WebAudio = { */ setPlaybackRate: function (value) { value = value || 1; - if (this.isPaused()) { - this.playbackRate = value; - } else { + this.playbackRate = value; + + var isPaused = this.isPaused(); + + if (!isPaused) { this.pause(); - this.playbackRate = value; + } + + var output = new Float32Array(this.originalBuffer.length / value); + var kali = new WaveSurfer.util.Kali(1); + kali.setup(this.originalBuffer.sampleRate, value, true); + kali.input(this.originalBuffer.getChannelData(0)); + kali.process(); + kali.output(output); + + this.buffer = this.ac.createBuffer(1, output.length, this.originalBuffer.sampleRate); + var data = this.buffer.getChannelData(0); + + for (var i = 0; i < output.length; i++) { + data[i] = output[i]; + } + + if (isPaused) { + this.createSource(); + } else { this.play(); } }