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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
const Cast = require("../../util/cast");
const Timer = require("./timer");

function MathOver(number, max) {
    let num = number;
    while (num > max) {
        num -= max;
    }
    return num;
}
function Clamp(number, min, max) {
    return Math.min(Math.max(number, min), max);
}

class AudioSource {
    /**
     * @param {AudioContext} audioContext 
     * @param {object} audioGroup 
     * @param {AudioBuffer} source 
     * @param {object} data 
     * @param {object} parent 
     */
    constructor(audioContext, audioGroup, source, data, parent, runtime) {
        if (source == null) source = "";
        if (data == null) data = {};
        this.runtime = runtime;

        this.src = source;
        this.duration = source.duration;
        this.originAudioName = "";

        this.volume = data.volume ?? 1;
        this.speed = data.speed ?? 1;
        this.pitch = data.pitch ?? 0;
        this.pan = data.pan ?? 0;
        this.looping = data.looping ?? false;

        this.startPosition = data.startPosition ?? 0;
        this.endPosition = data.endPosition ?? Infinity;
        this.loopStartPosition = data.loopStartPosition ?? 0;
        this.loopEndPosition = data.loopEndPosition ?? Infinity;

        this.resumeSpot = 0;
        this.paused = false;
        this.notPlaying = true;
        this.parent = parent;

        this._audioNode = null;
        this._audioContext = audioContext;
        this._audioGroup = audioGroup;

        this._audioPanner = this._audioContext.createPanner();
        this._audioGainNode = this._audioContext.createGain();
        this._audioAnalyzerNode = this._audioContext.createAnalyser();
        
        this._audioPanner.panningModel = 'equalpower';
        this._audioGainNode.gain.value = 1;

        this._audioGainNode.connect(this._audioPanner);
        this._audioPanner.connect(this._audioAnalyzerNode);
        this._audioAnalyzerNode.connect(parent.audioGlobalVolumeNode);

        this._originalConfig = data;
        this._playingSrc = null;

        this._timer = new Timer(runtime, audioContext);
        this._disposed = false;
    }

    play(atTime) {
        if (!this.src) throw "Cannot play an empty audio source";
        try {
            if (this._audioNode) {
                this._audioNode.onended = null;
                this._audioNode.stop();
            }
        } catch {
            // ... idk
        } finally {
            this._audioNode = null;
        }

        const source = this._audioContext.createBufferSource();
        this._audioNode = source;
        this.update();

        source.buffer = this.src;
        source.connect(this._audioGainNode);
        this._playingSrc = source.buffer;
        
        if (!this.paused) {
            this._timer.reset();
            this._timer.setTime(Clamp(atTime ?? this.startPosition, 0, this.duration) * 1000);
            this._timer.start();
        } else {
            this.resumeSpot = this.getTimePosition();
            this._timer.start();
        }

        // we need to know when the sound starts, so we know how long to play for
        // we also need to change endTimePos if we are looping
        let startTimePos = this.resumeSpot;
        let endTimePos = this.endPosition;
        if (this.paused) {
            this.paused = false;
        } else {
            startTimePos = atTime ?? this.startPosition;
        }
        if (this.looping) {
            endTimePos = this.loopEndPosition;
        }

        // dont play the sound if the playback duration is less than 1 sample frame, otherwise the ended event will not fire
        this.notPlaying = false;
        const playbackDuration = Clamp(endTimePos - startTimePos, 0, this.duration);
        if (playbackDuration < 1 / this.src.sampleRate) {
            this._onNodeStop(true);
        } else {
            source.start(0, Clamp(startTimePos, 0, this.duration), playbackDuration);
    
            source.onended = () => {
                this._onNodeStop();
            }
        }
    }
    stop() {
        this.notPlaying = true;
        this.paused = false;
        this._timer.stop();
        try {
            if (this._audioNode) {
                this._audioNode.stop();
            }
        } catch {
            // ... idk
        } finally {
            this._audioNode = null;
        }
    }
    pause() {
        if (!this._audioNode) return;
        this.paused = true;
        this.notPlaying = true;
        this._timer.pause();
        
        // onended is already ignored when paused, and stopped nodes cannot restart
        this._audioNode.onended = null;
        this._audioNode.stop();
        this._audioNode = null;
    }

    update() {
        if (!this._audioNode) return;
        const audioNode = this._audioNode;
        const audioGroup = this._audioGroup;
        const audioGainNode = this._audioGainNode;
        const audioPanner = this._audioPanner;

        // we need to manually calculate detune to prevent problems when using playbackRate for other things
        audioNode.playbackRate.value = this.speed * Math.pow(2, this.pitch / 1200);
        audioGainNode.gain.value = this.volume;

        audioNode.playbackRate.value *= audioGroup.globalSpeed * Math.pow(2, audioGroup.globalPitch / 1200);
        audioGainNode.gain.value *= audioGroup.globalVolume;
        this._timer.speed = audioNode.playbackRate.value;

        const pan = Clamp(this.pan + audioGroup.globalPan, -1, 1);
        audioPanner.positionX.value = pan;
        audioPanner.positionY.value = 0;
        audioPanner.positionZ.value = 1 - Math.abs(pan);
    }
    dispose() {
        this._disposed = true;
        this._timer.dispose();
        this.stop();
    }
    clone() {
        const newSource = new AudioSource(this._audioContext, this._audioGroup, this.src, this._originalConfig, this.parent, this.runtime);
        return newSource;
    }
    reverse() {
        if (!this.src) throw "Cannot reverse an empty audio source";

        const buffer = this.src;
        const reversedBuffer = this._audioContext.createBuffer(
            buffer.numberOfChannels,
            buffer.length,
            buffer.sampleRate
        );
    
        for (let channel = 0; channel < buffer.numberOfChannels; channel++) {
            const sourceData = buffer.getChannelData(channel);
            const destinationData = reversedBuffer.getChannelData(channel);
    
            for (let i = 0; i < buffer.length; i++) {
                destinationData[i] = sourceData[buffer.length - 1 - i];
            }
        }
        this.src = reversedBuffer;
    }

    setTimePosition(newSeconds) {
        if (!this._audioNode && !this.paused) return;
        const src = this._getActiveSource();
        newSeconds = Clamp(newSeconds, 0, src.duration);
        if (this.paused) {
            // only update the time
            this._timer.setTime(newSeconds * 1000);
            return;
        }

        this._timer.setTime(newSeconds * 1000);
        this.play(newSeconds);
    }

    getVolume() {
        const analyserNode = this._audioAnalyzerNode;

        const bufferLength = analyserNode.frequencyBinCount;
        const dataArray = new Uint8Array(bufferLength);
        analyserNode.getByteTimeDomainData(dataArray);

        let sumSquares = 0.0;
        for (let i = 0; i < bufferLength; i++) {
          const sample = (dataArray[i] / 128.0) - 1.0;
          sumSquares += sample * sample;
        }
        const volume = Math.sqrt(sumSquares / bufferLength);
        return volume;
    }
    getFrequency() {
        const analyserNode = this._audioAnalyzerNode;
        const src = this._getActiveSource();
    
        const bufferLength = analyserNode.frequencyBinCount;
        const dataArray = new Uint8Array(bufferLength);
        analyserNode.getByteFrequencyData(dataArray);
    
        // find the max frequency
        let maxIndex = 0;
        for (let i = 1; i < bufferLength; i++) {
            if (dataArray[i] > dataArray[maxIndex]) {
                maxIndex = i;
            }
        }
    
        // return the dominant freq
        const nyquist = src.sampleRate / 2;
        return maxIndex * nyquist / bufferLength;
    }
    getTimePosition() {
        const src = this._getActiveSource();
        return Clamp(this._timer.getTime(true), 0, src.duration);
    }

    _getActiveSource() {
        if (this._audioNode) return this._playingSrc;
        return this.src;
    }
    _onNodeStop(didNotPlay) {
        if (this.paused || !this._audioNode) return;
        if (!didNotPlay) {
            if (this.looping && !this.notPlaying) {
                this.play(this.loopStartPosition || 0);
                return;
            }
        }

        this._audioNode.onended = null;
        this.notPlaying = true;
        this._audioNode = null;
        this._timer.stop();
    }
}
class AudioExtensionHelper {
    constructor(runtime) {
        /**
            * The runtime that the helper will use for all functions.
            * @type {runtime}
        */
        this.runtime = runtime;
        this.audioGroups = {};
        this.audioContext = new AudioContext();
        this.audioGlobalVolumeNode = this.audioContext.createGain();

        this.audioGlobalVolumeNode.gain.value = 1;
        this.audioGlobalVolumeNode.connect(this.audioContext.destination);
    }
    
    /**
        * Creates a new AudioGroup.
        * @type {string} AudioGroup name
        * @type {object} AudioGroup settings (optional)
        * @type {object[]} AudioGroup sources (optional)
    */
    AddAudioGroup(name, data, sources) {
        if (data == null) data = {};
        this.audioGroups[name] = {
            id: name,
            sources: (sources == null ? {} : sources),
            globalVolume: (data.globalVolume == null ? 1 : data.globalVolume),
            globalSpeed: (data.globalSpeed == null ? 1 : data.globalSpeed),
            globalPitch: (data.globalPitch == null ? 0 : data.globalPitch),
            globalPan: (data.globalPan == null ? 0 : data.globalPan)
        };
        return this.audioGroups[name];
    }
    /**
        * Deletes an AudioGroup by name.
        * @type {string}
    */
    DeleteAudioGroup(name) {
        const audioGroup = this.audioGroups[name];
        if (!audioGroup) return;
        this.DisposeAudioGroupSources(audioGroup);
        delete this.audioGroups[name];
    }
    /**
        * Gets an AudioGroup by name.
        * @type {string}
    */
    GetAudioGroup(name) {
        return this.audioGroups[name];
    }
    /**
        * Gets all AudioGroups and returns them in an array.
    */
    GetAllAudioGroups() {
        return Object.values(this.audioGroups);
    }
    /**
        * Gets all AudioSources in an AudioGroup and updates them.
        * @type {AudioGroup}
    */
    UpdateAudioGroupSources(audioGroup) {
        const audioSources = this.GrabAllGrabAudioSources(audioGroup);
        for (let i = 0; i < audioSources.length; i++) {
            const source = audioSources[i];
            source.update();
        }
    }
    /**
        * Gets all AudioSources in an AudioGroup and disposes them.
        * @type {AudioGroup}
    */
    DisposeAudioGroupSources(audioGroup) {
        const audioSources = this.GrabAllGrabAudioSources(audioGroup);
        for (let i = 0; i < audioSources.length; i++) {
            const source = audioSources[i];
            source.dispose();
        }
    }

    /**
        * Creates a new AudioSource inside of an AudioGroup.
        * @type {AudioGroup} AudioSource parent
        * @type {string} AudioSource name
        * @type {string} AudioSource source (optional)
        * @type {object} AudioSource settings (optional)
    */
    AppendAudioSource(parent, name, src, settings) {
        const group = typeof parent == "string" ? this.GetAudioGroup(parent) : parent;
        if (!group) return;
        group.sources[name] = new AudioSource(this.audioContext, group, src, settings, this, this.runtime);
        return group.sources[name];
    }
    /**
        * Deletes an AudioSource by name.
        * @type {AudioGroup} AudioSource parent
        * @type {string}
    */
    RemoveAudioSource(parent, name) {
        const group = typeof parent == "string" ? this.GetAudioGroup(parent) : parent;
        if (!group) return;
        const audioSource = group.sources[name];
        if (!audioSource) return;

        audioSource.dispose();
        delete group.sources[name];
    }
    /**
        * Gets an AudioSource by name.
        * @type {AudioGroup} AudioSource parent
        * @type {string}
    */
    GrabAudioSource(audioGroup, name) {
        const group = typeof audioGroup == "string" ? this.GetAudioGroup(audioGroup) : audioGroup;
        if (!group) return;
        return group.sources[name];
    }
    /**
        * Gets all AudioSources and returns them in an array.
        * @type {AudioGroup} AudioSource parent
    */
    GrabAllGrabAudioSources(audioGroup) {
        const group = typeof audioGroup == "string" ? this.GetAudioGroup(audioGroup) : audioGroup;
        if (!group) return [];
        return Object.values(group.sources);
    }

    /**
        * Finds a sound with the specified ID in the sound list.
        * @type {Array} soundList
        * @type {string} Sound ID
    */
    FindSoundBySoundId(soundList, id) {
        for (let i = 0; i < soundList.length; i++) {
            const sound = soundList[i];
            if (sound.soundId == id) return sound;
        }
        return null;
    }
    /**
        * Finds a sound with the specified name in the sound list.
        * @type {Array} soundList
        * @type {string} Sound name
    */
    FindSoundByName(soundList, name) {
        for (let i = 0; i < soundList.length; i++) {
            const sound = soundList[i];
            if (sound.name == name) return sound;
        }
        return null;
    }
    /**
        * Clamps numbers to stay inbetween 2 values.
        * @type {number}
    */
    Clamp(number, min, max) {
        return Math.min(Math.max(number, min), max);
    }
}

module.exports.Helper = AudioExtensionHelper
module.exports.AudioSource = AudioSource