react-native-audio-api
Version:
react-native-audio-api provides system for controlling audio in React Native environment compatible with Web Audio API specification
153 lines (152 loc) • 5.42 kB
JavaScript
"use strict";
import { InvalidAccessError, InvalidStateError, NotSupportedError } from "../errors/index.js";
import { assertWorkletsEnabled } from "../utils/index.js";
import AnalyserNode from "./AnalyserNode.js";
import AudioBuffer from "./AudioBuffer.js";
import AudioBufferQueueSourceNode from "./AudioBufferQueueSourceNode.js";
import AudioBufferSourceNode from "./AudioBufferSourceNode.js";
import { decodeAudioData, decodePCMInBase64 } from "./AudioDecoder.js";
import AudioDestinationNode from "./AudioDestinationNode.js";
import BiquadFilterNode from "./BiquadFilterNode.js";
import ConstantSourceNode from "./ConstantSourceNode.js";
import ConvolverNode from "./ConvolverNode.js";
import DelayNode from "./DelayNode.js";
import GainNode from "./GainNode.js";
import IIRFilterNode from "./IIRFilterNode.js";
import OscillatorNode from "./OscillatorNode.js";
import PeriodicWave from "./PeriodicWave.js";
import RecorderAdapterNode from "./RecorderAdapterNode.js";
import StereoPannerNode from "./StereoPannerNode.js";
import StreamerNode from "./StreamerNode.js";
import WaveShaperNode from "./WaveShaperNode.js";
import WorkletNode from "./WorkletNode.js";
import WorkletProcessingNode from "./WorkletProcessingNode.js";
import WorkletSourceNode from "./WorkletSourceNode.js";
export default class BaseAudioContext {
constructor(context) {
this.context = context;
this.destination = new AudioDestinationNode(this, context.destination);
this.sampleRate = context.sampleRate;
}
get currentTime() {
return this.context.currentTime;
}
get state() {
return this.context.state;
}
async decodeAudioData(input, fetchOptions) {
return await decodeAudioData(input, this.sampleRate, fetchOptions);
}
async decodePCMInBase64(base64String, inputSampleRate, inputChannelCount, isInterleaved = true) {
return await decodePCMInBase64(base64String, inputSampleRate, inputChannelCount, isInterleaved);
}
createWorkletNode(callback, bufferLength, inputChannelCount, workletRuntime = 'AudioRuntime') {
if (inputChannelCount < 1 || inputChannelCount > 32) {
throw new NotSupportedError(`The number of input channels provided (${inputChannelCount}) can not be less than 1 or greater than 32`);
}
if (bufferLength < 1) {
throw new NotSupportedError(`The buffer length provided (${bufferLength}) can not be less than 1`);
}
assertWorkletsEnabled();
return new WorkletNode(this, workletRuntime, callback, bufferLength, inputChannelCount);
}
createWorkletProcessingNode(callback, workletRuntime = 'AudioRuntime') {
assertWorkletsEnabled();
return new WorkletProcessingNode(this, workletRuntime, callback);
}
createWorkletSourceNode(callback, workletRuntime = 'AudioRuntime') {
assertWorkletsEnabled();
return new WorkletSourceNode(this, workletRuntime, callback);
}
createRecorderAdapter() {
return new RecorderAdapterNode(this);
}
createOscillator() {
return new OscillatorNode(this);
}
createStreamer(streamPath) {
return new StreamerNode(this, {
streamPath
});
}
createConstantSource() {
return new ConstantSourceNode(this);
}
createGain() {
return new GainNode(this);
}
createDelay(maxDelayTime) {
if (maxDelayTime !== undefined) {
return new DelayNode(this, {
maxDelayTime
});
} else {
return new DelayNode(this);
}
}
createStereoPanner() {
return new StereoPannerNode(this);
}
createBiquadFilter() {
return new BiquadFilterNode(this);
}
createBufferSource(options) {
if (options !== undefined) {
return new AudioBufferSourceNode(this, options);
} else {
return new AudioBufferSourceNode(this);
}
}
createIIRFilter(feedforward, feedback) {
if (feedforward.length < 1 || feedforward.length > 20) {
throw new NotSupportedError(`The provided feedforward array has length (${feedforward.length}) outside the range [1, 20]`);
}
if (feedback.length < 1 || feedback.length > 20) {
throw new NotSupportedError(`The provided feedback array has length (${feedback.length}) outside the range [1, 20]`);
}
if (feedforward.every(value => value === 0)) {
throw new InvalidStateError(`Feedforward array must contain at least one non-zero value`);
}
if (feedback[0] === 0) {
throw new InvalidStateError(`First value of feedback array cannot be zero`);
}
return new IIRFilterNode(this, {
feedforward,
feedback
});
}
createBufferQueueSource(options) {
if (options !== undefined) {
return new AudioBufferQueueSourceNode(this, options);
} else {
return new AudioBufferQueueSourceNode(this);
}
}
createBuffer(numberOfChannels, length, sampleRate) {
return new AudioBuffer({
numberOfChannels,
length,
sampleRate
});
}
createPeriodicWave(real, imag, constraints) {
if (real.length !== imag.length) {
throw new InvalidAccessError(`The lengths of the real (${real.length}) and imaginary (${imag.length}) arrays must match.`);
}
return new PeriodicWave(this, {
real,
imag,
...constraints
});
}
createAnalyser() {
return new AnalyserNode(this);
}
createConvolver() {
return new ConvolverNode(this);
}
createWaveShaper() {
return new WaveShaperNode(this);
}
}
//# sourceMappingURL=BaseAudioContext.js.map