3 lines
3.7 KiB
JavaScript
3 lines
3.7 KiB
JavaScript
/*! wavesurfer.js 1.3.0 (Thu, 19 Jan 2017 15:50:04 GMT)
|
|
* https://github.com/katspaugh/wavesurfer.js
|
|
* @license CC-BY-3.0 */!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)}); |