Created
July 23, 2016 11:34
-
-
Save maxatwork/e2e7d713704736a7dc71d52d0c232a5d to your computer and use it in GitHub Desktop.
Audio demo
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
<audio src="./Maple_Leaf_RagQ.ogg" controls></audio> | |
<script type="text/javascript"> | |
const audioCtx = new AudioContext(); | |
const myAudio = document.querySelector('audio'); | |
const source = audioCtx.createMediaElementSource(myAudio); | |
const gainNode = audioCtx.createGain(); | |
gainNode.gain.value = 0.5; | |
const gainNode2 = audioCtx.createGain(); | |
gainNode2.gain.value = 0.2; | |
const lowFilter = audioCtx.createBiquadFilter(); | |
lowFilter.type = 'lowpass'; | |
lowFilter.frequency = 2000; | |
const highFilter = audioCtx.createBiquadFilter(); | |
highFilter.type = 'highpass'; | |
highFilter.frequency = 1000; | |
const noiseNode = audioCtx.createScriptProcessor(4096, 1, 1); | |
noiseNode.onaudioprocess = (e) => { | |
var outputBuffer = e.outputBuffer; | |
var inputBuffer = e.inputBuffer; | |
for (var channel = 0; channel < outputBuffer.numberOfChannels; channel++) { | |
var inputData = inputBuffer.getChannelData(channel); | |
var outputData = outputBuffer.getChannelData(channel); | |
for (var sample = 0; sample < inputBuffer.length; sample++) { | |
outputData[sample] = inputData[sample]; | |
outputData[sample] += ((Math.random() * 2) - 1) * 0.1; | |
} | |
} | |
}; | |
source.connect(highFilter); | |
highFilter.connect(lowFilter); | |
lowFilter.connect(gainNode); | |
lowFilter.connect(noiseNode); | |
noiseNode.connect(gainNode2); | |
gainNode2.connect(gainNode); | |
gainNode.connect(audioCtx.destination) | |
myAudio.play(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment