react-native-audio-api
Version:
react-native-audio-api provides system for controlling audio in React Native environment compatible with Web Audio API specification
88 lines (87 loc) • 2.46 kB
JavaScript
"use strict";
import AudioBufferBaseSourceNode from "./AudioBufferBaseSourceNode.js";
import { InvalidStateError, RangeError } from "../errors/index.js";
export default class AudioBufferSourceNode extends AudioBufferBaseSourceNode {
_buffer = null;
bufferHasBeenSet = false;
constructor(context, options) {
const node = context.context.createBufferSource(options || {});
super(context, node);
if (options?.buffer) {
this._buffer = options.buffer;
this.bufferHasBeenSet = true;
}
}
get buffer() {
return this._buffer;
}
set buffer(buffer) {
if (buffer === null) {
if (this.buffer !== null) {
this.node.setBuffer(null);
this._buffer = null;
}
return;
}
if (this.bufferHasBeenSet) {
throw new InvalidStateError('The buffer can only be set once and cannot be changed afterwards.');
}
this.node.setBuffer(buffer.buffer);
this._buffer = buffer;
this.bufferHasBeenSet = true;
}
get loopSkip() {
return this.node.loopSkip;
}
set loopSkip(value) {
this.node.loopSkip = value;
}
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;
}
start(when = 0, offset = 0, duration) {
if (when < 0) {
throw new RangeError(`when must be a finite non-negative number: ${when}`);
}
if (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 InvalidStateError('Cannot call start more than once');
}
this.hasBeenStarted = true;
this.node.start(when, offset, duration);
}
get onLoopEnded() {
return this.onLoopEndedCallback;
}
set onLoopEnded(callback) {
if (!callback) {
this.node.onLoopEnded = '0';
this.onLoopEndedCallback = undefined;
return;
}
this.onLoopEndedCallback = callback;
const sub = this.audioEventEmitter.addAudioEventListener('loopEnded', callback);
this.node.onLoopEnded = sub.subscriptionId;
}
}
//# sourceMappingURL=AudioBufferSourceNode.js.map