-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_2DNote.min.js
3 lines (3 loc) · 3.3 KB
/
_2DNote.min.js
1
2
3
// _2DNote 1.14.3
/*! LICENSE: https://github.com/hchiam/_2DNote/blob/master/LICENSE */
var _2DNote={audioContext:new(AudioContext||webkitAudioContext),note:null,viewXRange:[0,document.documentElement.clientWidth],viewYRange:[0,document.documentElement.clientHeight],comfyFrequencyRange:[150,400],comfyVolumeRange:[0,.5],panRange:[-1,1],setAs2DArea:function(e,t,n){n=n||!0,this.callbackUponUpdate=t;var i=e||document.body;i.addEventListener("mousedown",this.play.bind(this)),i.addEventListener("mouseup",this.stop.bind(this)),i.addEventListener("mousemove",this.update.bind(this)),i.addEventListener("touchstart",this.play.bind(this)),i.addEventListener("touchend",this.stop.bind(this)),i.addEventListener("touchmove",this.update.bind(this)),n&&this.setupExitedViewDetection(i)},play:function(e,t){t=t||!0,this.stop(),t&&this.setupExitedViewDetection(e);var n=this.getFrequency(e),i=this.getVolume(e),o=this.getPan(e),a=this.audioContext.createGain();a.connect(this.audioContext.destination),a.gain.value=isNaN(i)?.5:i;var r=this.audioContext.createOscillator();r.type="sine",r.frequency.value=isNaN(n)?400:n,r.connect(a);var s=this.audioContext.createStereoPanner();o&&(s.pan.value=o),a.connect(s),s.connect(this.audioContext.destination),r.start(),this.note={oscillator:r,volumeSetup:a,panner:s}},update:function(e,t){if(this.note){var n=this.getFrequency(e),i=this.getVolume(e),o=this.getPan(e);this.note.volumeSetup.gain.value=i,this.note.oscillator.frequency.value=n,this.note.panner.pan.value=o,t?t(i,n,o):this.callbackUponUpdate&&this.callbackUponUpdate(i,n,o)}},stop:function(){null!=this.note&&(this.note.oscillator.stop(this.audioContext.currentTime),this.note=null)},setupExitedViewDetection:function(e){var t=e||document.body;t.removeEventListener&&t.addEventListener&&(t.removeEventListener("mouseleave",this.warnExitedView),t.removeEventListener("touchcancel",this.warnExitedView),t.addEventListener("mouseleave",this.warnExitedView),t.addEventListener("touchcancel",this.warnExitedView))},warnExitedView:function(e){var t=e||document.body,n={currentTarget:!0,clientX:t.clientWidth/2,clientY:t.clientHeight/2};_2DNote.play(n),setTimeout((function(){_2DNote.stop(),t.removeEventListener&&(t.removeEventListener("mouseleave",_2DNote.warnExitedView),t.removeEventListener("touchcancel",_2DNote.warnExitedView))}),100)},getFrequency:function(e){var t=this.getX(e);return this.normalize(t,this.viewXRange,this.comfyFrequencyRange)},getVolume:function(e){var t=this.getY(e);return this.normalize(t,this.viewYRange,this.comfyVolumeRange)},getPan:function(e){var t=this.getX(e);return this.normalize(t,this.viewXRange,this.panRange)},getX:function(e){var t=e.currentTarget&&e.clientX,n=e.touches;return t?e.clientX:n?e.touches[0].clientX:e.offsetLeft},getY:function(e){var t=e.currentTarget&&e.clientY,n=e.touches;return t?e.clientY:n?e.touches[0].clientY:e.offsetTop},normalize:function(e,t,n){var i=t[0],o=t[1],a=n[0],r=n[1],s=(r-a)/(o-i)*(e-i)+a;return Math.min(Math.max(s,a),r)},copy:function(){return function e(t){if(null===t||"object"!=typeof t)return t;if(Array.isArray(t)){var n=[];return t.forEach((function(t){n.push(e(t))})),n}var i={};for(var o in t)t.hasOwnProperty(o)&&("audioContext"===o?i.audioContext=new AudioContext:i[o]=e(t[o]));return i}(this)}};"undefined"!=typeof window&&(window._2DNote=_2DNote),"undefined"!=typeof module&&(module.exports=_2DNote);