Skip to content

Instantly share code, notes, and snippets.

@Nerixyz
Last active September 5, 2022 14:53
Show Gist options
  • Save Nerixyz/97e966abd1dcbeb6b4bc0318fb969c3e to your computer and use it in GitHub Desktop.
Save Nerixyz/97e966abd1dcbeb6b4bc0318fb969c3e to your computer and use it in GitHub Desktop.
Web Audio API

Web Audio API

In here are neat scripts to make the most out of the Web Audio API.

You can paste them into the DevTools.

const opts = {
FREQUENCY: 70,
SIDECHANNEL_LEVEL: -12,
POST_GAIN: 2
};
const ctx = new AudioContext();
const audio = document.querySelector('video');
const src = ctx.createMediaElementSource(audio);
const bi = ctx.createBiquadFilter();
const gain = ctx.createGain();
bi.type = 'highshelf';
bi.frequency.setValueAtTime(opts.FREQUENCY, ctx.currentTime);
bi.gain.setValueAtTime(opts.SIDECHANNEL_LEVEL, ctx.currentTime);
gain.gain.setValueAtTime(opts.POST_GAIN, ctx.currentTime);
src.connect(bi);
bi.connect(gain);
gain.connect(ctx.destination);
const opts = {
DELAY: 1
};
const ctx = new AudioContext();
const audio = document.querySelector('video');
const src = ctx.createMediaElementSource(audio);
const splitter = ctx.createChannelSplitter(2);
const merger = ctx.createChannelMerger(2);
const delay = ctx.createDelay(opts.DELAY);
delay.delayTime.setValueAtTime(opts.DELAY, ctx.currentTime);
src.connect(splitter);
splitter.connect(delay, 0);
delay.connect(merger, 0, 0);
splitter.connect(merger, 1, 1);
merger.connect(ctx.destination);
_=document.querySelector('video');_.mozPreservesPitch=_.preservesPitch=!1
v = document.querySelector('video');
ctx = new AudioContext();
s = ctx.createChannelSplitter(2);
m = ctx.createChannelMerger(2);
vn = ctx.createMediaElementSource(v);
vn.connect(s)
s.connect(m, 1, 0);
s.connect(m, 1, 1);
m.connect(ctx.destination);
FINAL_SPEED = 0.5;
function mapping(time) {
return (1 - time) * (1 - FINAL_SPEED) + FINAL_SPEED;
}
const makeHandler = fn => ({target}) => {
target.playbackRate = fn((target.currentTime / target.duration) || 0);
};
globalThis.__handler = makeHandler(mapping);
((vid = document.querySelector('video') ?? document.querySelector('audio')).addEventListener('timeupdate', globalThis.__handler), vid.preservesPitch = false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment