Last active
October 22, 2017 23:41
-
-
Save beefchimi/a1cc7fdfa722ae1366ca8d982f20ed8d to your computer and use it in GitHub Desktop.
Web audio Single class
This file contains 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
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', | |
// ...more paths... | |
}; | |
export default class Single { | |
static singleKeys = Object.keys(assetPaths); | |
constructor() { | |
this.context = new AudioContext(); | |
// Think of this as the "volume knob" | |
this.gainNode = this.context.createGain(); | |
// `source` will be the active "sound" | |
this.source = null; | |
// Used to store all of my decoded sounds | |
this.sounds = {}; | |
// Loop through all of my paths, fetch them, | |
// and store their decoded AudioBuffer in the `sounds` object | |
Single.singleKeys.forEach((key) => { | |
fetchAudioBuffer(assetPaths[key], this.context).then((response) => { | |
this.sounds[key] = response; | |
return this.sounds[key]; | |
}).catch(() => { | |
this.sounds[key] = null; | |
}); | |
}); | |
this.mobileAudio = new MobileAudioFix(this.context); | |
this.mobileAudio.init(); | |
} | |
play(key) { | |
if (!Single.singleKeys.includes(key)) { | |
return Error(`The requested sound is not available: ${key}`); | |
} | |
return this._startSource(this.sounds[key]); | |
} | |
_startSource(sound) { | |
// We need to create a `AudioBufferSourceNode` in order to attach | |
// our individual decoded sound | |
this.source = this.context.createBufferSource(); | |
this.source.buffer = sound; | |
// Turn the volume knob up to a specific value at the current time | |
this.gainNode.gain.setValueAtTime( | |
singleGain, | |
this.context.currentTime | |
); | |
this.source.connect(this.gainNode); | |
this.gainNode.connect(this.context.destination); | |
this.source.start = methodPatch.start(this.source); | |
this.source.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment