react-native-audio-api
Version:
react-native-audio-api provides system for controlling audio in React Native environment compatible with Web Audio API specification
116 lines (115 loc) • 4.18 kB
JavaScript
;
import { AutomationEventType } from "../types.js";
import { InvalidStateError, NotSupportedError, RangeError } from "../errors/index.js";
export default class AudioParam {
constructor(audioParam, context) {
this.audioParam = audioParam;
this.context = context;
this.value = audioParam.value;
this.defaultValue = audioParam.defaultValue;
this.minValue = audioParam.minValue;
this.maxValue = audioParam.maxValue;
}
get value() {
return this.audioParam.value;
}
set value(value) {
this.audioParam.value = value;
}
setValueAtTime(value, startTime) {
if (startTime < 0) {
throw new RangeError(`startTime must be a finite non-negative number: ${startTime}`);
}
this.checkCurveExclusion({
type: AutomationEventType.SET_VALUE,
automationTime: startTime
});
const clampedTime = Math.max(startTime, this.context.currentTime);
this.audioParam.setValueAtTime(value, clampedTime);
return this;
}
linearRampToValueAtTime(value, endTime) {
if (endTime < 0) {
throw new RangeError(`endTime must be a finite non-negative number: ${endTime}`);
}
this.checkCurveExclusion({
type: AutomationEventType.LINEAR_RAMP,
automationTime: endTime
});
const clampedTime = Math.max(endTime, this.context.currentTime);
this.audioParam.linearRampToValueAtTime(value, clampedTime);
return this;
}
exponentialRampToValueAtTime(value, endTime) {
if (value === 0) {
throw new RangeError(`value must be a non-zero number: ${value}`);
}
if (endTime <= 0) {
throw new RangeError(`endTime must be a finite non-negative number: ${endTime}`);
}
this.checkCurveExclusion({
type: AutomationEventType.EXPONENTIAL_RAMP,
automationTime: endTime
});
const clampedTime = Math.max(endTime, this.context.currentTime);
this.audioParam.exponentialRampToValueAtTime(value, clampedTime);
return this;
}
setTargetAtTime(target, startTime, timeConstant) {
if (startTime < 0) {
throw new RangeError(`startTime must be a finite non-negative number: ${startTime}`);
}
if (timeConstant < 0) {
throw new RangeError(`timeConstant must be a finite non-negative number: ${timeConstant}`);
}
this.checkCurveExclusion({
type: AutomationEventType.SET_TARGET,
automationTime: startTime
});
const clampedTime = Math.max(startTime, this.context.currentTime);
this.audioParam.setTargetAtTime(target, clampedTime, timeConstant);
return this;
}
setValueCurveAtTime(values, startTime, duration) {
if (startTime < 0) {
throw new RangeError(`startTime must be a finite non-negative number: ${startTime}`);
}
if (duration <= 0) {
throw new RangeError(`duration must be a finite strictly-positive number: ${duration}`);
}
if (values.length < 2) {
throw new InvalidStateError(`values must contain at least two values`);
}
const clampedTime = Math.max(startTime, this.context.currentTime);
this.checkCurveExclusion({
type: AutomationEventType.SET_VALUE_CURVE,
automationTime: clampedTime,
duration
});
this.audioParam.setValueCurveAtTime(values, clampedTime, duration);
return this;
}
cancelScheduledValues(cancelTime) {
if (cancelTime < 0) {
throw new RangeError(`cancelTime must be a finite non-negative number: ${cancelTime}`);
}
const clampedTime = Math.max(cancelTime, this.context.currentTime);
this.audioParam.cancelScheduledValues(clampedTime);
return this;
}
cancelAndHoldAtTime(cancelTime) {
if (cancelTime < 0) {
throw new RangeError(`cancelTime must be a finite non-negative number: ${cancelTime}`);
}
const clampedTime = Math.max(cancelTime, this.context.currentTime);
this.audioParam.cancelAndHoldAtTime(clampedTime);
return this;
}
checkCurveExclusion(eventData) {
const checkExclusionResult = this.audioParam.checkCurveExclusion(eventData);
if (checkExclusionResult.status === 'error') {
throw new NotSupportedError(checkExclusionResult.message);
}
}
}
//# sourceMappingURL=AudioParam.js.map