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

92.15% Statements 47/51
100% Branches 15/15
95.23% Functions 20/21
97.72% Lines 43/44

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  1x 6x 3x 6x   9x 3x     1x 1x 1x     7x 7x     4x     2x 2x 2x 1x 1x 1x       1x 1x 1x 1x   1x       1x 1x 1x 1x 2x 2x 2x 2x 2x   2x 2x   1x 1x 1x 1x 1x 1x 3x         4x     1x  
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AudioSources = void 0;
const audioSource_1 = require("./audioSource");
class AudioSources extends Map {
    constructor() {
        super();
        this._audioPaths = new Array();
    }
    add(...audios) {
        this._audioPaths.push(...audios);
    }
    remove(...audios) {
        audios.forEach(audio => {
            const index = this._audioPaths.indexOf(audio);
            if (index < 0 || index >= this._audioPaths.length)
                return;
            this._audioPaths.splice(index, 1);
            this.delete(audio);
        });
    }
    loadAsync() {
        return __awaiter(this, void 0, void 0, function* () {
            const audioContext = new AudioContext();
            yield Promise.all(this._audioPaths.map((audioPath) => __awaiter(this, void 0, void 0, function* () {
                yield this.loadAudioAsync(audioPath, audioContext);
            })));
            yield audioContext.resume();
        });
    }
    loadAudioAsync(audioPath, audioContext) {
        return __awaiter(this, void 0, void 0, function* () {
            let loaded = false;
            const request = new XMLHttpRequest();
            const onRequestLoaded = () => {
                request.removeEventListener("error", onRequestLoaded);
                request.removeEventListener("load", onRequestLoaded);
                const createBuffer = (audioBuffer) => {
                    const audioSource = new audioSource_1.AudioSource(audioBuffer, audioContext);
                    this.set(audioPath.split('/').pop(), audioSource);
                };
                audioContext.decodeAudioData(request.response, createBuffer);
                loaded = true;
            };
            request.responseType = "arraybuffer";
            request.open("GET", audioPath, true);
            request.addEventListener("error", onRequestLoaded);
            request.addEventListener("load", onRequestLoaded);
            request.send();
            while (!loaded) {
                yield this.delay(1);
            }
        });
    }
    delay(ms) {
        return new Promise((resolve) => setTimeout(resolve, ms));
    }
}
exports.AudioSources = AudioSources;