UNPKG

react-native-audio-api

Version:

react-native-audio-api provides system for controlling audio in React Native environment compatible with Web Audio API specification

127 lines (125 loc) 3.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _errors = require("../../errors"); var _AudioBuffer = _interopRequireDefault(require("../AudioBuffer.web")); var _AudioParam = _interopRequireDefault(require("../AudioParam.web")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } class AudioBufferSourceNodeStandard { hasBeenStarted = false; _loopSkip = false; _onLoopEnded = undefined; constructor(context, options) { const { buffer, ...rest } = options ?? {}; this.node = new globalThis.AudioBufferSourceNode(context.context, { ...rest, ...(buffer ? { buffer: buffer.buffer } : {}) }); this.detune = new _AudioParam.default(this.node.detune, context); this.playbackRate = new _AudioParam.default(this.node.playbackRate, context); } start(when = 0, offset, duration) { if (when && when < 0) { throw new RangeError(`when must be a finite non-negative number: ${when}`); } if (offset && offset < 0) { throw new RangeError(`offset must be a finite non-negative number: ${offset}`); } if (duration && duration < 0) { throw new RangeError(`duration must be a finite non-negative number: ${duration}`); } if (this.hasBeenStarted) { throw new _errors.InvalidStateError('Cannot call start more than once'); } this.hasBeenStarted = true; this.node.start(when, offset, duration); } stop(when) { if (when !== undefined && when < 0) { throw new RangeError(`when must be a finite non-negative number: ${when}`); } if (!this.hasBeenStarted) { throw new _errors.InvalidStateError('Cannot call stop without calling start first'); } this.node.stop(when); } connect(destination) { if (destination instanceof _AudioParam.default) { this.node.connect(destination.param); } else { this.node.connect(destination.node); } return destination; } disconnect(destination) { if (destination === undefined) { this.node.disconnect(); return; } if (destination instanceof _AudioParam.default) { this.node.disconnect(destination.param); return; } this.node.disconnect(destination.node); } get buffer() { const buffer = this.node.buffer; if (!buffer) { return null; } return new _AudioBuffer.default(buffer); } set buffer(buffer) { if (!buffer) { this.node.buffer = null; return; } this.node.buffer = buffer.buffer; } get loop() { return this.node.loop; } set loop(value) { this.node.loop = value; } get loopStart() { return this.node.loopStart; } set loopStart(value) { this.node.loopStart = value; } get loopEnd() { return this.node.loopEnd; } set loopEnd(value) { this.node.loopEnd = value; } get loopSkip() { return this._loopSkip; } set loopSkip(value) { this._loopSkip = value; } get onEnded() { return this.node.onended; } set onEnded(callback) { this.node.onended = callback; } get onLoopEnded() { return this._onLoopEnded; } // The browser Web Audio API has no per-loop event; callback is stored but never fired. set onLoopEnded(callback) { this._onLoopEnded = callback ?? undefined; } } exports.default = AudioBufferSourceNodeStandard; //# sourceMappingURL=AudioBufferSourceNodeStandard.js.map