All files / src/controller/assets/sources audioSource.js

100% Statements 61/61
100% Branches 36/36
100% Functions 12/12
100% Lines 60/60

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93  1x 1x     3x 1x 2x 2x     49x 1x 48x 48x 48x       7x     49x 48x       17x     41x 41x 41x 41x 41x 41x 41x 41x 41x     12x 1x 11x 2x 2x   11x 11x 11x 11x 11x 11x 11x 11x       3x 3x       2x 2x     44x 44x 44x 3x   44x 3x   44x 44x 44x 44x 44x 44x 44x     1x 1x 1x 1x 1x     1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AudioSource = void 0;
class AudioSource {
    get volume() {
        if (!this._gaineNode)
            return 0;
        const gain = this._gaineNode.gain;
        return gain.value;
    }
    set volume(value) {
        if (!this._gaineNode)
            return;
        this._volume = value;
        const gain = this._gaineNode.gain;
        gain.setValueAtTime(this._volume, 0);
    }
    get loop() {
        var _a, _b;
        return (_b = (_a = this._audioNode) === null || _a === void 0 ? void 0 : _a.loop) !== null && _b !== void 0 ? _b : false;
    }
    set loop(value) {
        if (this._audioNode) {
            this._audioNode.loop = value;
        }
    }
    get isPlaying() {
        return this._isPlaying;
    }
    constructor(audioBuffer, audioContext) {
        this._volume = 1.0;
        this._startTime = 0.0;
        this._endTime = 0.0;
        this._isPlaying = false;
        this._isPaused = false;
        this._audioBuffer = audioBuffer;
        this._audioContext = audioContext;
        this._ended = (event) => this.onEnded(event);
        this.createAudioNodes();
    }
    play(delay, offset, duration) {
        if (!this._audioNode)
            return;
        if (this._isPlaying) {
            this._audioNode.stop();
            this.createAudioNodes();
        }
        this._isPlaying = true;
        const pausedTime = this._isPaused ? this._endTime : undefined;
        const offsetTime = offset !== null && offset !== void 0 ? offset : pausedTime;
        this._audioContext.resume();
        this._startTime = Date.now();
        this._audioNode.addEventListener("ended", this._ended);
        this._audioNode.start(delay, offsetTime, duration);
        this._isPaused = false;
    }
    pause() {
        var _a;
        this._isPaused = true;
        (_a = this._audioNode) === null || _a === void 0 ? void 0 : _a.stop();
    }
    stop() {
        var _a;
        this._isPaused = false;
        (_a = this._audioNode) === null || _a === void 0 ? void 0 : _a.stop();
    }
    createAudioNodes() {
        let volume = 1.0;
        let loop = false;
        if (this._audioNode) {
            loop = this._audioNode.loop;
        }
        if (this._gaineNode) {
            volume = this._gaineNode.gain.value;
        }
        this._gaineNode = this._audioContext.createGain();
        this._audioNode = this._audioContext.createBufferSource();
        this._audioNode.buffer = this._audioBuffer;
        this._audioNode.connect(this._gaineNode);
        this._gaineNode.connect(this._audioContext.destination);
        this.loop = loop;
        this.volume = volume;
    }
    onEnded(event) {
        this._isPlaying = false;
        this._endTime = Date.now() - this._startTime;
        const node = event.target;
        node.removeEventListener("ended", this._ended);
        this.createAudioNodes();
    }
}
exports.AudioSource = AudioSource;