UNPKG

@4players/odin

Version:

A cross-platform SDK enabling developers to integrate real-time VoIP chat technology into their projects

197 lines (196 loc) 10.1 kB
"use strict"; var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { if (kind === "m") throw new TypeError("Private method is not writable"); if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it"); return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value; }; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _AudioInput_instances, _AudioInput_active, _AudioInput_settings, _AudioInput_capture, _AudioInput_activityHandler, _AudioInput_powerLevelHandler, _AudioInput_activityIntervalId, _AudioInput_powerLevelIntervalId, _AudioInput_activityInterval, _AudioInput_powerLevelInterval, _AudioInput_dispatchActivity, _AudioInput_dispatchPowerLvl; Object.defineProperty(exports, "__esModule", { value: true }); exports.AudioInput = void 0; const odin_event_target_1 = require("../../utils/odin-event-target"); const helpers_1 = require("../../utils/helpers"); class AudioInput extends odin_event_target_1.OdinEventTarget { constructor(settings, capture) { super(); _AudioInput_instances.add(this); _AudioInput_active.set(this, true); _AudioInput_settings.set(this, void 0); _AudioInput_capture.set(this, void 0); this.kind = 'audio-input'; _AudioInput_activityHandler.set(this, () => { __classPrivateFieldGet(this, _AudioInput_instances, "m", _AudioInput_dispatchActivity).call(this); }); _AudioInput_powerLevelHandler.set(this, () => { __classPrivateFieldGet(this, _AudioInput_instances, "m", _AudioInput_dispatchPowerLvl).call(this); }); _AudioInput_activityIntervalId.set(this, void 0); _AudioInput_powerLevelIntervalId.set(this, void 0); /** * Interval how often the AudioActivity of Inputs is dispatched. * The lower the value, the more accurate the indicator is at the cost of performance. */ _AudioInput_activityInterval.set(this, 20); _AudioInput_powerLevelInterval.set(this, 100); __classPrivateFieldSet(this, _AudioInput_settings, settings, "f"); __classPrivateFieldSet(this, _AudioInput_capture, capture, "f"); __classPrivateFieldSet(this, _AudioInput_activityIntervalId, setInterval(__classPrivateFieldGet(this, _AudioInput_activityHandler, "f"), __classPrivateFieldGet(this, _AudioInput_activityInterval, "f")), "f"); __classPrivateFieldSet(this, _AudioInput_powerLevelIntervalId, setInterval(__classPrivateFieldGet(this, _AudioInput_powerLevelHandler, "f"), __classPrivateFieldGet(this, _AudioInput_powerLevelInterval, "f")), "f"); } /** * The AudioCapture that is provided by the underling plugin. * * @return {AudioCapture} The AudioCapture of the underling plugin. */ get capture() { return __classPrivateFieldGet(this, _AudioInput_capture, "f"); } /** * Retrieves the current volume. Is the Volume 'muted', the associated Device * was stopped. * * @return {CaptureVolume} The current volume level. */ get volume() { return __classPrivateFieldGet(this, _AudioInput_capture, "f").volume; } /** * Retrieves the customType that was set when the AudioInput was created. * Remote peers can use this to identify the purpose of the stream. */ get customType() { return __classPrivateFieldGet(this, _AudioInput_capture, "f").customType; } /** * Sets the volume of the AudioInput. * By setting the value to 'muted', will stop the Capturing of the MediaStream. * The volume value should be between 0 and 2 (inclusive). Values outside this range are clamped. * * @param {CaptureVolume} value - The desired volume level to be set. It must be a valid `Volume` instance or a value supported by the underlying `_capture` object. * @return {Promise<void>} A Promise that resolves when the volume has been successfully set. */ async setVolume(value) { await __classPrivateFieldGet(this, _AudioInput_capture, "f").setVolume(value); } /** * Whether the AudioInput is currently active or not. * Only works when voice activity detection was enabled. * * @return {boolean} True if the instance is active, false otherwise. */ get isActive() { if (!__classPrivateFieldGet(this, _AudioInput_capture, "f")) return false; if (this.volume === 0 || this.volume === 'muted') return false; return !__classPrivateFieldGet(this, _AudioInput_capture, "f").activity.isSilent; } /** * Sets the device configuration for the capture process. * * @param {DeviceParameters} device - The parameters for the device to be set. * @return {Promise<void>} A promise that resolves when the device has been successfully set. */ async setDevice(device) { await __classPrivateFieldGet(this, _AudioInput_capture, "f").setDevice(device); } /** * Retrieves the Root Mean Square (RMS) in decibels relative to full scale (dBFS) * for the captured audio activity. This is a measure of the average power level * of the audio signal. * * @return {number} The RMS value expressed in dBFS. */ get powerLevel() { return __classPrivateFieldGet(this, _AudioInput_capture, "f").activity.rmsDBFS; } /** * Updates the input settings for the current instance and applies necessary changes. * * @param {InputSettings} settings - The new input settings to be applied. Each key in the settings object * determines a specific input property, such as volume, voice activity detection, or audio processing modules. * @return {Promise<void>} A promise that resolves once all updates and configurations have been processed. */ async setInputSettings(settings) { const changes = []; for (const key in settings) { const newValue = settings[key]; const oldValue = __classPrivateFieldGet(this, _AudioInput_settings, "f")[key]; if (oldValue !== newValue) { changes.push(key); } } __classPrivateFieldSet(this, _AudioInput_settings, { ...__classPrivateFieldGet(this, _AudioInput_settings, "f"), ...settings, }, "f"); if (changes.find((prop) => { return prop === 'voiceActivity' || prop === 'volumeGate'; })) { const vad = (0, helpers_1.generateVadConfig)(__classPrivateFieldGet(this, _AudioInput_settings, "f")); __classPrivateFieldGet(this, _AudioInput_capture, "f").setVad(vad); } if (changes.find((prop) => { return (prop === 'echoCanceller' || prop === 'noiseSuppression' || prop === 'gainController'); })) { const apm = (0, helpers_1.generateApmConfig)(__classPrivateFieldGet(this, _AudioInput_settings, "f")); await __classPrivateFieldGet(this, _AudioInput_capture, "f").setApm(apm); } if (typeof settings.volume !== 'undefined' && changes.includes('volume')) { await this.setVolume(settings.volume); } } /** * Initiates and starts the loopback device. This can be useful to play back * the voice after processing vad. * * @return {Promise<void>} A promise that resolves when the loopback process has successfully started. */ async startLoopback() { await __classPrivateFieldGet(this, _AudioInput_capture, "f").startLoopback(); } /** * Stops the loopback operation. * * @return {Promise<void>} A promise that resolves when the loopback operation is successfully stopped. */ async stopLoopback() { await __classPrivateFieldGet(this, _AudioInput_capture, "f").stopLoopback(); } /** * Closes the AudioInput. */ close() { clearInterval(__classPrivateFieldGet(this, _AudioInput_activityIntervalId, "f")); clearInterval(__classPrivateFieldGet(this, _AudioInput_powerLevelIntervalId, "f")); this.capture.close(); } } exports.AudioInput = AudioInput; _AudioInput_active = new WeakMap(), _AudioInput_settings = new WeakMap(), _AudioInput_capture = new WeakMap(), _AudioInput_activityHandler = new WeakMap(), _AudioInput_powerLevelHandler = new WeakMap(), _AudioInput_activityIntervalId = new WeakMap(), _AudioInput_powerLevelIntervalId = new WeakMap(), _AudioInput_activityInterval = new WeakMap(), _AudioInput_powerLevelInterval = new WeakMap(), _AudioInput_instances = new WeakSet(), _AudioInput_dispatchActivity = function _AudioInput_dispatchActivity() { if (__classPrivateFieldGet(this, _AudioInput_active, "f") !== this.isActive) { __classPrivateFieldSet(this, _AudioInput_active, this.isActive, "f"); const payload = { media: this, }; this.dispatchEvent(new odin_event_target_1.OdinEvent('Activity', payload)); if (this.onAudioActivity) { this.onAudioActivity(payload); } } }, _AudioInput_dispatchPowerLvl = function _AudioInput_dispatchPowerLvl() { const payload = { media: this, }; this.dispatchEvent(new odin_event_target_1.OdinEvent('PowerLevel', payload)); if (this.onPowerLevel) { this.onPowerLevel(payload); } };