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

129 lines (128 loc) 4.45 kB
"use strict"; import { InvalidAccessError, NotSupportedError } from "../errors/index.js"; import AnalyserNode from "./AnalyserNode.web.js"; import AudioBuffer from "./AudioBuffer.web.js"; import AudioBufferSourceNode from "./AudioBufferSourceNode.web.js"; import AudioDestinationNode from "./AudioDestinationNode.web.js"; import BiquadFilterNode from "./BiquadFilterNode.web.js"; import ConvolverNode from "./ConvolverNode.web.js"; import DelayNode from "./DelayNode.web.js"; import GainNode from "./GainNode.web.js"; import IIRFilterNode from "./IIRFilterNode.web.js"; import MediaElementAudioSourceNode from "./MediaElementAudioSourceNode.web.js"; import OscillatorNode from "./OscillatorNode.web.js"; import PeriodicWave from "./PeriodicWave.web.js"; import StereoPannerNode from "./StereoPannerNode.web.js"; import ConstantSourceNode from "./ConstantSourceNode.web.js"; import WaveShaperNode from "./WaveShaperNode.web.js"; export default class AudioContext { constructor(options) { if (options && options.sampleRate && (options.sampleRate < 8000 || options.sampleRate > 96000)) { throw new NotSupportedError(`The provided sampleRate is not supported: ${options.sampleRate}`); } this.context = new window.AudioContext({ sampleRate: options?.sampleRate }); this.sampleRate = this.context.sampleRate; this.destination = new AudioDestinationNode(this, this.context.destination); } get currentTime() { return this.context.currentTime; } get state() { return this.context.state; } createOscillator() { return new OscillatorNode(this); } createConstantSource() { return new ConstantSourceNode(this); } createGain() { return new GainNode(this); } createDelay(maxDelayTime) { return new DelayNode(this, { maxDelayTime }); } createStereoPanner() { return new StereoPannerNode(this); } createBiquadFilter() { return new BiquadFilterNode(this); } createConvolver() { return new ConvolverNode(this); } createIIRFilter(feedforward, feedback) { return new IIRFilterNode(this, { feedforward, feedback }); } createBufferSource(options) { return new AudioBufferSourceNode(this, options); } createMediaElementSource(mediaElement) { return new MediaElementAudioSourceNode(this, this.context.createMediaElementSource(mediaElement), mediaElement); } createBuffer(numberOfChannels, length, sampleRate) { if (numberOfChannels < 1 || numberOfChannels >= 32) { throw new NotSupportedError(`The number of channels provided (${numberOfChannels}) is outside the range [1, 32]`); } if (length <= 0) { throw new NotSupportedError(`The number of frames provided (${length}) is less than or equal to the minimum bound (0)`); } if (sampleRate < 8000 || sampleRate > 96000) { throw new NotSupportedError(`The sample rate provided (${sampleRate}) is outside the range [8000, 96000]`); } 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); } createWaveShaper() { return new WaveShaperNode(this, this.context.createWaveShaper()); } async decodeAudioData(source, fetchOptions) { if (source instanceof ArrayBuffer) { const decodedData = await this.context.decodeAudioData(source); return new AudioBuffer(decodedData); } if (typeof source === 'string') { const response = await fetch(source, fetchOptions); if (!response.ok) { throw new InvalidAccessError(`Failed to fetch audio data from the provided source: ${source}`); } const arrayBuffer = await response.arrayBuffer(); const decodedData = await this.context.decodeAudioData(arrayBuffer); return new AudioBuffer(decodedData); } throw new TypeError('Unsupported source for decodeAudioData: ' + source); } async close() { await this.context.close(); } async resume() { await this.context.resume(); } async suspend() { await this.context.suspend(); } } //# sourceMappingURL=AudioContext.web.js.map