forked from sptm-unam/seminario-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsonido-min.js
1 lines (1 loc) · 5.48 KB
/
sonido-min.js
1
import{defaultKeymap}from"@codemirror/commands";import{EditorState,Prec,Compartment}from"@codemirror/state";import{EditorView,basicSetup}from"codemirror";import{defaultHighlightStyle,HighlightStyle,syntaxHighlighting}from"@codemirror/language";import{tags}from"@lezer/highlight";import{javascript}from"@codemirror/lang-javascript";import{keymap,KeyBinding}from"@codemirror/view";var AudioContext=window.AudioContext||window.webkitAudioContext;let audioCtx=new AudioContext,randomNoiseNode,sineNode,microphone,video=document.getElementById("videoElement"),sources=[],gainNoise,gainSine;const activar=document.getElementById("activar");activar.addEventListener("click",init);const desactivar=document.getElementById("desactivar");desactivar.addEventListener("click",suspend);const reproducirRuido=document.getElementById("reproducirRuido");reproducirRuido.addEventListener("click",reproducirRuidoFunc);const detenerRuido=document.getElementById("detenerRuido");detenerRuido.addEventListener("click",detenerRuidoFunc);const reproducirSine=document.getElementById("reproducirSine");reproducirSine.addEventListener("click",reproducirSineFunc);const detenerSine=document.getElementById("detenerSine");detenerSine.addEventListener("click",detenerSineFunc);const reproducirMic=document.getElementById("reproducirMic");reproducirMic.addEventListener("click",reproducirMicFunc);const detenerMic=document.getElementById("detenerMic");detenerMic.addEventListener("click",detenerMicFunc);const encenderCamara=document.getElementById("encenderCamara");encenderCamara.addEventListener("click",encenderCamaraFunc);const detenerCamara=document.getElementById("detenerCamara");detenerCamara.addEventListener("click",detenerCamaraFunc);const audioFile1=document.getElementById("audio_file1"),audioFile2=document.getElementById("audio_file2"),sliderRuidoBlanco=document.getElementById("sliderRuidoBlanco"),sliderSine=document.getElementById("sliderSine"),keymaps=[];keymaps.push(keymap.of({key:"Ctrl-Enter",run:()=>evaluar(),preventDefault:!0})),keymaps.push(keymap.of(defaultKeymap));let language=new Compartment,startState=EditorState.create({doc:'// ¡Hola mundo!\n\n// El editor ya tiene highlighter y un estilo génerico (hereda características del estilo general de la página también) \n//También es posible evaluar líneas de código en JS con ctrl+enter\n\nconsole.log("control+e y ver en consola (f12)");\n\n //Por último ya es posible controlar las variables globales de ganancia para la sinusoide y el noise. Primero hay que inicializar el audio (chrome) e iniciar la reproducción de noise o sine. Por el momento se evalúa todo el código (y no línea por línea) entonces las indicaciones se pueden contradecir \n\n//gainNoise.setValueAtTime(0, audioCtx.currentTime);// comentar o descomentar según sea el caso \ngainNoise.setValueAtTime(0.1, audioCtx.currentTime);\n//gainSine.setValueAtTime(0, audioCtx.currentTime);//comentar o descomentar según sea el caso\ngainSine.setValueAtTime(0.1, audioCtx.currentTime);',extensions:[keymaps,basicSetup,language.of(javascript()),EditorView.lineWrapping]}),view=new EditorView({state:startState,parent:document.querySelector("#editor")});function init(){audioCtx=new AudioContext,"suspended"===audioCtx.state&&audioCtx.resume(),navigator.mediaDevices&&navigator.mediaDevices.getUserMedia({audio:!0}).then((e=>{microphone=audioCtx.createMediaStreamSource(e),samples()})).catch((e=>{})),start(),startSine(),audioFile1.onchange=function(){iniciarAF1()},audioFile2.onchange=function(){iniciarAF2()},sliderRuidoBlanco.onchange=function(){gainNoise.setValueAtTime(sliderRuidoBlanco.value,audioCtx.currentTime)},sliderSine.onchange=function(){gainSine.setValueAtTime(sliderSine.value,audioCtx.currentTime)}}async function start(){await audioCtx.audioWorklet.addModule("js/random-noise-processor.js"),randomNoiseNode=new AudioWorkletNode(audioCtx,"random-noise-processor"),gainNoise=randomNoiseNode.parameters.get("customGain"),gainNoise.setValueAtTime(0,audioCtx.currentTime)}async function startSine(){await audioCtx.audioWorklet.addModule("js/sine-processor.js"),sineNode=new AudioWorkletNode(audioCtx,"sine-processor"),gainSine=sineNode.parameters.get("customGain"),gainSine.setValueAtTime(0,audioCtx.currentTime)}function suspend(){audioCtx.suspend()}function reproducirRuidoFunc(){randomNoiseNode.connect(audioCtx.destination)}function detenerRuidoFunc(){randomNoiseNode.disconnect(audioCtx.destination)}function reproducirSineFunc(){sineNode.connect(audioCtx.destination)}function detenerSineFunc(){sineNode.disconnect(audioCtx.destination)}function reproducirMicFunc(){microphone.connect(audioCtx.destination)}function detenerMicFunc(){microphone.disconnect(audioCtx.destination)}function encenderCamaraFunc(){navigator.mediaDevices.getUserMedia&&navigator.mediaDevices.getUserMedia({video:!0}).then((function(e){video.srcObject=e})).catch((function(e){}))}function detenerCamaraFunc(){video.srcObject=null}function iniciarAF1(){var e=new FileReader;e.onload=function(e){audioCtx.decodeAudioData(e.target.result).then((function(e){sources[0]=audioCtx.createBufferSource(),sources[0].buffer=e,sources[0].connect(audioCtx.destination),sources[0].start(0)}))},e.readAsArrayBuffer(audioFile1.files[0])}function iniciarAF2(){var e=new FileReader;e.onload=function(e){audioCtx.decodeAudioData(e.target.result).then((function(e){sources[1]=audioCtx.createBufferSource(),sources[1].buffer=e,sources[1].connect(audioCtx.destination),sources[1].start(0)}))},e.readAsArrayBuffer(audioFile2.files[0])}function evaluar(){const code=view.state.doc.toString();return eval(code),!0}