From 58b6f896d5472572b8e017fff674dc7aa739a79f Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Mon, 16 Jan 2023 17:29:17 -0700 Subject: [PATCH] Volume adjustment --- static/scripts/audio.mjs | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 static/scripts/audio.mjs diff --git a/static/scripts/audio.mjs b/static/scripts/audio.mjs new file mode 100644 index 0000000..bb0eee6 --- /dev/null +++ b/static/scripts/audio.mjs @@ -0,0 +1,53 @@ +/** + * @file Provides some base audio tools. + */ + +/** + * Compute the special "Audio Context" time + * + * This is is a duration from now, in seconds. + * + * @param {AudioContext} context + * @param {Date} when Date to compute + * @returns audiocontext time + */ + function AudioContextTime(context, when) { + if (!when) return 0 + let acOffset = Date.now() - (context.currentTime * Second) + return Math.max(when - acOffset, 0) / Second +} + +class AudioSource { + /** + * A generic audio source + * + * @param {AudioContext} context + */ + constructor(context) { + this.context = context + this.masterGain = new GainNode(this.context) + } + + /** + * Connect to an audio node + * + * @param {AudioNode} destinationNode + */ + connect(destinationNode) { + this.masterGain.connect(destinationNode) + } + + /** + * Set the master gain for this audio source. + * + * @param {Number} value New gain value + */ + SetGain(value) { + this.masterGain.gain.value = value + } +} + +export { + AudioContextTime, + AudioSource, +}