// the main module for Resonator
// API, etc.
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());
    });
};
import ResonatorAudioContext from './audio-context';
import ResonatorScene from './scenes/webaudio-scene';
import AudioGraph from './audio-graph';
import AudioSource from './sources/audio-source';
import DataPool from './data-pool';
import Convolver from './effects/convolver';
import { HTTPLoader } from './loaders/http-loader';
import { SourceType } from './sources/source-type';
import { StreamingSource } from './sources/streaming-source';
export default class Resonator {
    constructor(loader = new HTTPLoader()) {
        this.loader = loader;
        this.environmentImpulse = null;
        this.context = new ResonatorAudioContext();
        this.scene = new ResonatorScene(this.context);
        this.graph = new AudioGraph(this.scene, this.context, false);
        this.dataPool = new DataPool(this.context, this.loader);
    }
    load(path, type = SourceType.WorldSource) {
        return __awaiter(this, void 0, void 0, function* () {
            const data = yield this.dataPool.get(path);
            const source = this.createSource(type, data);
            return source;
        });
    }
    loadImmediate(path, type = SourceType.WorldSource) {
        const source = new AudioSource(this.graph, this.scene, this.context, null, type);
        this.dataPool.get(path).then((data) => {
            source.setBuffer(data);
        });
        return source;
    }
    stream(path, type = SourceType.MasterSource) {
        const element = new Audio(path);
        element.crossOrigin = 'anonymous';
        element.volume = 1;
        const source = new StreamingSource(this.graph, this.scene, this.context, element, type);
        return source;
    }
    createSource(type, data) {
        return new AudioSource(this.graph, this.scene, this.context, data);
    }
    setEnvironmentImpulse(file) {
        return __awaiter(this, void 0, void 0, function* () {
            if (this.environmentImpulse) {
                this.graph.removeEffect(this.environmentImpulse);
            }
            if (file === null) {
                return;
            }
            const buffer = yield this.dataPool.get(file);
            this.environmentImpulse = new Convolver(this.context, this.graph, {
                buffer
            });
            this.graph.applyEffect(this.environmentImpulse);
        });
    }
    setListenerPosition(x, y, z) {
        this.scene.setListenerPosition(x, y, z);
    }
    setListenerOrientation(forward, up) {
        this.scene.setListenerOrientation(forward, up);
    }
    clearDataPool() {
        this.dataPool.clear();
    }
}
