UNPKG

@ekx/auph

Version:

[![Build](https://github.com/eliasku/auph/actions/workflows/build.yml/badge.svg)](https://github.com/eliasku/auph/actions/workflows/build.yml) [![Version](https://img.shields.io/npm/v/auph)](https://www.npmjs.com/package/auph) [![Downloads](https://img.sh

161 lines 4.34 kB
import { iMask, Unit } from "../protocol/interface"; import { connectAudioNode, disconnectAudioNode, len, nextHandle, setAudioParamValue } from "./common"; import { emptyAudioBuffer } from "./Mixer"; var VoiceObj = /** @class */ (function () { function VoiceObj(g, p, index) { var _this = this; this.g = g; this.p = p; // handle passport this.h = 0; // Control Flags this.s = 0; // Gain this.G = Unit; // Pan this.P = Unit; // Rate this.R = Unit; // Source auph Buffer this.bf = 0; // Destination auph Bus this.bs = 0; // is source-node started: static buffer playback this._s = 0; // Source-Node this.sn = null; // Destination-Node: connected destination audio node this.dn = null; this.pr = null; this._e = function () { // maybe check is useful //if (this.buffer === e.target || (this.stream && this.stream.el === e.target)) { _voiceStop(_this); //} }; this.h = 805306368 /* Voice */ | index; } return VoiceObj; }()); export { VoiceObj }; export function _voiceNew(ctx, index) { var gain = ctx.createGain(); var pan = ctx.createStereoPanner(); connectAudioNode(pan, gain); return new VoiceObj(gain, pan, index); } export function _voiceChangeDestination(v, target) { if (target !== v.dn) { var gain = v.g; if (v.dn) { disconnectAudioNode(gain, v.dn); } v.dn = target; if (target) { connectAudioNode(gain, target); } } } export function _voiceResetDestination(v) { if (v.dn) { disconnectAudioNode(v.g, v.dn); v.dn = null; } } export function _voiceStop(v) { // stop buffer var buffer = v.sn; if (buffer) { if ((v.s & 2 /* Running */) !== 0) { buffer.stop(); } buffer.onended = null; disconnectAudioNode(buffer); try { buffer.buffer = emptyAudioBuffer; } catch (_a) { } v.sn = null; } if (v.pr) { v.pr.disconnect(); v.pr.onaudioprocess = null; v.pr = null; } _voiceResetDestination(v); v.bf = 0; v.bs = 0; v.s = 0; v.h = nextHandle(v.h); } export function _voiceStartBuffer(v) { var source = v.sn; if (source && !v._s) { //source.addEventListener("ended", v._e, {once: true}); source.onended = v._e; source.loop = (v.s & 4 /* Loop */) !== 0; source.start(); v._s = 1; } } export function _voicePrepareBuffer(v, ctx, audioBuffer) { var source = ctx.createBufferSource(); source.buffer = audioBuffer; connectAudioNode(source, v.p); v.sn = source; v._s = 0; } export function _voiceSetLoop(v, value) { var current = (v.s & 4 /* Loop */) !== 0; if (value !== current) { v.s ^= 4 /* Loop */; if (v.sn) { v.sn.loop = value; } } } export function _voiceSetRunning(v, value) { var current = !!(v.s & 2 /* Running */); if (value !== current) { v.s ^= 2 /* Running */; var playbackRate = value ? (v.R / Unit) : 0.0; if (v.sn) { setAudioParamValue(v.sn.playbackRate, playbackRate); if (value) { // restart if play called in pause mode _voiceStartBuffer(v); } } } } export function _voiceApplyPitch(v, value) { if (!!(v.s & 2 /* Running */)) { if (v.sn) { setAudioParamValue(v.sn.playbackRate, value / Unit); } } } export var voicePool = [null]; var voicesMaxCount = 64; export function _getVoiceObj(handle) { var obj = voicePool[handle & iMask]; return (obj && obj.h === handle) ? obj : null; } export function createVoiceObj(ctx) { var next = len(voicePool); for (var i = 1; i < next; ++i) { var v = voicePool[i]; if (v.s === 0) { return v.h; } } if (next < voicesMaxCount) { var v = _voiceNew(ctx, next); v.h = 805306368 /* Voice */ | next; voicePool.push(v); return v.h; } return 0; } //# sourceMappingURL=Voice.js.map