vail

Internet morse code repeater
git clone https://git.woozle.org/neale/vail.git

vail / static / scripts
Neale Pickett  ·  2023-01-17

audio.mjs

 1/**
 2 * @file Provides some base audio tools.
 3 */
 4
 5import * as time from "./time.mjs"
 6
 7/**
 8 * Compute the special "Audio Context" time
 9 * 
10 * This is is a duration from now, in seconds.
11 * 
12 * @param {AudioContext} context
13 * @param {Date} when Date to compute
14 * @returns audiocontext time
15 */
16 function AudioContextTime(context, when) {
17    if (!when) return 0
18    let acOffset = Date.now() - (context.currentTime * time.Second)
19    return Math.max(when - acOffset, 0) / time.Second
20}
21
22class AudioSource {
23	/**
24	 * A generic audio source
25	 * 
26	 * @param {AudioContext} context 
27	 */
28	constructor(context) {
29		this.context = context
30		this.masterGain = new GainNode(this.context)
31	}
32
33	/**
34	 * Connect to an audio node
35	 * 
36	 * @param {AudioNode} destinationNode 
37	 */
38	connect(destinationNode) {
39		this.masterGain.connect(destinationNode)
40	}
41
42  /**
43	 * Set the master gain for this audio source.
44	 * 
45	 * @param {Number} value New gain value
46	 */
47	SetGain(value) {
48		this.masterGain.gain.value = value
49	}
50}
51
52export {
53    AudioContextTime,
54    AudioSource,
55}