Created
August 28, 2018 00:02
-
-
Save tenub/d5621a2b529daf3547057ccb56ec0a7e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const AudioContext = window.AudioContext || window.webkitAudioContext; | |
| const context = new AudioContext(); | |
| const osc = context.createOscillator(); | |
| osc.type = 'sine'; | |
| osc.frequency.value = 440; | |
| osc.connect(delay); | |
| osc.connect(context.destination); | |
| osc.start(); | |
| osc.stop(context.currentTime + 2); | |
| const delay = context.createDelay(5.0); | |
| const analyser = context.createAnalyser(); | |
| analyser.fftSize = 2048; | |
| const bufferLength = analyser.frequencyBinCount; | |
| const dataArray = new Uint8Array(bufferLength); | |
| analyser.getByteTimeDomainData(dataArray); | |
| const canvas = document.getElementById('oscilloscope'); | |
| const canvasCtx = canvas.getContext('2d'); | |
| function draw() { | |
| drawVisual = requestAnimationFrame(draw); | |
| analyser.getByteTimeDomainData(dataArray); | |
| canvasCtx.fillStyle = 'rgb(200, 200, 200)'; | |
| canvasCtx.fillRect(0, 0, canvas.width, canvas.height); | |
| canvasCtx.lineWidth = 2; | |
| canvasCtx.strokeStyle = 'rgb(0, 0, 0)'; | |
| canvasCtx.beginPath(); | |
| const sliceWidth = canvas.width * 1.0 / bufferLength; | |
| let x = 0; | |
| for (let i = 0; i < bufferLength; i++) { | |
| const v = dataArray[i] / 128.0; | |
| const y = v * canvas.height / 2; | |
| if (i === 0) { | |
| canvasCtx.moveTo(x, y); | |
| } else { | |
| canvasCtx.lineTo(x, y); | |
| } | |
| x += sliceWidth; | |
| } | |
| canvasCtx.lineTo(canvas.width, canvas.height / 2); | |
| canvasCtx.stroke(); | |
| }; | |
| draw(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment