All files / ts/controller/assets/sources audioSource.ts

100% Statements 59/59
100% Branches 36/36
100% Functions 12/12
100% Lines 58/58

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 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                      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    
export interface IAudioSource {
    readonly isPlaying: boolean;
    loop: boolean;
    volume: number;
    play(delay: number, offset: number, duration: number): void;
    play(delay: number, offset: number): void;
    play(delay: number): void;
    play(): void;
    pause(): void;
    stop(): void;
}
export class AudioSource implements IAudioSource {
 
    private readonly _audioBuffer: AudioBuffer;
    private readonly _audioContext: AudioContext;
    private readonly _ended: (event: Event) => void;
 
    private _volume: number;
    private _isPlaying: boolean;
    private _isPaused: boolean;
    private _startTime: number;
    private _endTime: number;
 
    private _audioNode?: AudioBufferSourceNode;
    private _gaineNode?: GainNode;
 
    public get volume(): number {
        if (!this._gaineNode)
            return 0;
 
        const gain = this._gaineNode.gain;
        return gain.value;
    }
 
    public set volume(value: number) {
        if (!this._gaineNode)
            return;
 
        this._volume = value;
        const gain = this._gaineNode.gain;
        gain.setValueAtTime(this._volume, 0);
    }
 
    public get loop(): boolean {
        return this._audioNode?.loop ?? false;
    }
 
    public set loop(value: boolean) {
        if (this._audioNode) {
            this._audioNode.loop = value;
        }
    }
 
    public get isPlaying(): boolean {
        return this._isPlaying;
    }
 
    public constructor(audioBuffer: AudioBuffer, audioContext: 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: Event): void => this.onEnded(event);
        this.createAudioNodes();
    }
 
    public play(delay?: number, offset?: number, duration?: number): void {
        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 ?? pausedTime;
 
        this._audioContext.resume();
        this._startTime = Date.now();
        this._audioNode.addEventListener("ended", this._ended);
        this._audioNode.start(delay, offsetTime, duration);
        this._isPaused = false;
    }
 
    public pause(): void {
        this._isPaused = true;
        this._audioNode?.stop();
    }
 
    public stop(): void {
        this._isPaused = false;
        this._audioNode?.stop();
    }
 
    private createAudioNodes(): void {
        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;
    }
 
    private onEnded(event: Event): void {
        this._isPlaying = false;
        this._endTime = Date.now() - this._startTime;
        const node = <AudioBufferSourceNode>event.target;
        node.removeEventListener("ended", this._ended);
        this.createAudioNodes();
    }
}