Skip to content

Instantly share code, notes, and snippets.

@nnirror
Created January 24, 2024 15:09
Show Gist options
  • Save nnirror/ed4f8a08707d42079c8ae72897ab31da to your computer and use it in GitHub Desktop.
Save nnirror/ed4f8a08707d42079c8ae72897ab31da to your computer and use it in GitHub Desktop.
Safari web audio glitch test
<!DOCTYPE html>
<html>
<body>
<button id="startButton" onclick="startAudio()">Start</button>
<script>
let context = new (window.AudioContext || window.webkitAudioContext)();
let oscillator = context.createOscillator();
let panner = context.createPanner();
oscillator.type = 'sine';
oscillator.frequency.value = 440;
oscillator.connect(panner);
panner.connect(context.destination);
// copied from Inviso default cone parameters
panner.coneInnerAngle = 24.22774531795417;
panner.coneOuterAngle = 72.6832359538625;
panner.coneOuterGain = 0.05;
let angle = 0;
let intervalId;
function startAudio() {
if (intervalId) {
return;
}
oscillator.start();
// set the position of the sound source
if (panner.positionX) {
panner.positionX.value = 0;
panner.positionY.value = 0;
panner.positionZ.value = 1;
} else {
panner.setPosition(0, 0, 1);
}
intervalId = setInterval(() => {
angle = (angle + 1) % 360;
let radian = angle * Math.PI / 180;
// there are certain combinations of x and z that when you move through,
// the sound will glitch in safari. for example:
// let x = 0.3416666666666667;
// let z = -0.5446390350150271;
// whereas these cycle through areas that are fine and areas that are not.
let x = angle / 360;
let z = Math.cos(radian);
if (panner.orientationX) {
panner.orientationX.value = x;
panner.orientationY.value = 0;
panner.orientationZ.value = z;
} else {
panner.setOrientation(x, 0, z);
}
}, 20);
// 60fps
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment