Skip to content

Instantly share code, notes, and snippets.

View beefchimi's full-sized avatar
🤠
yeeeeehaw!

Curtis Dulmage beefchimi

🤠
yeeeeehaw!
View GitHub Profile
@beefchimi
beefchimi / webaudio-example-1.js
Created October 19, 2017 00:49
Simple AudioContext
export const AudioContext = window.AudioContext || window.webkitAudioContext;
const myContext = new AudioContext();
@beefchimi
beefchimi / audio-method-patch.js
Last active October 22, 2017 23:39
Web Audio common method patch
const methodPatch = {
start(source) {
return (source.start) ? source.start : (startTime = 0) => source.noteOn(startTime);
},
stop(source) {
return (source.stop) ? source.stop : (stopTime = 0) => source.noteOff(stopTime);
},
};
export default methodPatch;
@beefchimi
beefchimi / MobileAudioFix.js
Last active October 22, 2017 23:40
iOS mobile web audio fix
import methodPatch from './method-patch';
export default class MobileAudioFix {
constructor(context) {
this.context = context;
this.source = this.context.createBufferSource();
// now fill that buffer with an extremely brief and silent sound
this.source.buffer = this.context.createBuffer(1, 1, 22050);
// and connect it to the context destination (audio output speakers)
this.source.connect(this.context.destination);
@beefchimi
beefchimi / fetch-audio-buffer.js
Last active November 16, 2021 13:01
Fetch audio buffer
export default function fetchAudioBuffer(path, context) {
const audioRequest = new Request(path);
return fetch(audioRequest).then((response) => {
return response.arrayBuffer();
}).then((arrayBuffer) => {
return context.decodeAudioData(arrayBuffer);
}).catch((error) => {
throw Error(`Asset failed to load: ${error.message}`);
});
@beefchimi
beefchimi / Single.js
Last active October 22, 2017 23:41
Web audio Single class
import fetchAudioBuffer from './helpers/fetch-audio-buffer';
import methodPatch from './helpers/method-patch';
import MobileAudioFix from './helpers/mobile-audio-fix';
// All of my MP3s were built to be a consistent volume on export,
// so I can safely set the same gain on all of them.
const singleGain = 0.6;
const assetPaths = {
cubeUp: 'assets/audio/cube-up.mp3',
@beefchimi
beefchimi / AnimationFeature.js
Last active October 22, 2017 23:42
Basic Single class implementation
import SoundFx from '../../SoundFx';
export default function AnimationFeature() {
const target1 = document.getElementById('AnimationCube1');
// ...more targets...
target1.addEventListener('mouseenter', () => {
SoundFx.Single.play('animationUp1');
});
@beefchimi
beefchimi / webaudio-example-2.js
Last active October 22, 2017 23:43
Basic oscillator
const context = new AudioContext();
const oscNode = context.createOscillator();
const gainNode = context.createGain();
oscNode.connect(gainNode);
gainNode.connect(context.destination);
oscNode.start();
oscNode.stop(1);
@beefchimi
beefchimi / webaudio-example-3.js
Last active October 22, 2017 23:44
Fade in gainNode
const duration = 0.2;
const volume = {
min: 0.001,
max: 1,
};
gainNode.gain.setValueAtTime(
volume.min,
context.currentTime
);
@beefchimi
beefchimi / synth-presets.js
Last active October 22, 2017 23:45
Oscillator presets
const up = {
wave: 'sine',
freq: {
start: 392,
end: 493.88,
},
volume: 1,
duration: 0.2,
};
@beefchimi
beefchimi / Synth.js
Last active October 22, 2017 23:46
Synth class
import synthPresets from './helpers/synth-presets';
import methodPatch from './helpers/method-patch';
import MobileAudioFix from './helpers/mobile-audio-fix';
const initialGain = 0.001;
export default class Synth {
static synthKeys = Object.keys(synthPresets);
constructor() {