Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AudioNode from './AudioNode.web';
import { AnalyserOptions } from '../types';
import BaseAudioContext from './BaseAudioContext.web';
import type BaseAudioContext from './BaseAudioContext.web';

export default class AnalyserNode extends AudioNode {
fftSize: number;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AudioParam from './AudioParam.web';
import AudioBuffer from './AudioBuffer.web';
import BaseAudioContext from './BaseAudioContext.web';
import type BaseAudioContext from './BaseAudioContext.web';
import AudioNode from './AudioNode.web';

import { AudioBufferSourceOptions } from '../types';
Expand Down
171 changes: 8 additions & 163 deletions packages/react-native-audio-api/src/web-core/AudioContext.web.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,26 @@
import { InvalidAccessError, NotSupportedError } from '../errors';
import { assertSupportedSampleRate } from '../utils/validation';
import { AudioContextOptions, ContextState, DecodeDataInput } from '../types';
import AnalyserNode from './AnalyserNode.web';
import AudioBuffer from './AudioBuffer.web';
import AudioBufferSourceNode from './AudioBufferSourceNode.web';
import AudioDestinationNode from './AudioDestinationNode.web';
import AudioListener from './AudioListener.web';
import { AudioContextOptions } from '../types';
import BaseAudioContext from './BaseAudioContext.web';
import BiquadFilterNode from './BiquadFilterNode.web';
import ChannelMergerNode from './ChannelMergerNode.web';
import ChannelSplitterNode from './ChannelSplitterNode.web';
import ConvolverNode from './ConvolverNode.web';
import DelayNode from './DelayNode.web';
import GainNode from './GainNode.web';
import IIRFilterNode from './IIRFilterNode.web';
import MediaElementAudioSourceNode from './MediaElementAudioSourceNode.web';
import OscillatorNode from './OscillatorNode.web';
import PeriodicWave from './PeriodicWave.web';
import StereoPannerNode from './StereoPannerNode.web';
import PannerNode from './PannerNode.web';
import ConstantSourceNode from './ConstantSourceNode.web';
import WaveShaperNode from './WaveShaperNode.web';

export default class AudioContext implements BaseAudioContext {
readonly context: globalThis.AudioContext;

readonly destination: AudioDestinationNode;
readonly listener: AudioListener;
readonly sampleRate: number;

export default class AudioContext extends BaseAudioContext<globalThis.AudioContext> {
public onerror: (() => void) | null = null;

constructor(options?: AudioContextOptions) {
if (options?.sampleRate != null) {
assertSupportedSampleRate(options.sampleRate);
}

this.context = new window.AudioContext({
sampleRate: options?.sampleRate,
latencyHint: options?.latencyHint,
});

this.sampleRate = this.context.sampleRate;
this.destination = new AudioDestinationNode(this, this.context.destination);
this.listener = new AudioListener(this, this.context.listener);
super(
new window.AudioContext({
sampleRate: options?.sampleRate,
latencyHint: options?.latencyHint,
})
);

this.context.addEventListener('error', () => this.onerror?.());
}

public get currentTime(): number {
return this.context.currentTime;
}

public get state(): ContextState {
return this.context.state as ContextState;
}

public get baseLatency(): number {
return this.context.baseLatency;
}
Expand All @@ -64,62 +29,6 @@ export default class AudioContext implements BaseAudioContext {
return this.context.outputLatency;
}

createOscillator(): OscillatorNode {
return new OscillatorNode(this);
}

createConstantSource(): ConstantSourceNode {
return new ConstantSourceNode(this);
}

createGain(): GainNode {
return new GainNode(this);
}

createDelay(maxDelayTime?: number): DelayNode {
return new DelayNode(this, { maxDelayTime });
}

createStereoPanner(): StereoPannerNode {
return new StereoPannerNode(this);
}

createPanner(): PannerNode {
return new PannerNode(this);
}

createBiquadFilter(): BiquadFilterNode {
return new BiquadFilterNode(this);
}

createConvolver(): ConvolverNode {
return new ConvolverNode(this);
}

createChannelMerger(numberOfInputs?: number): ChannelMergerNode {
return new ChannelMergerNode(
this,
numberOfInputs !== undefined ? { numberOfInputs } : undefined
);
}

createChannelSplitter(numberOfOutputs?: number): ChannelSplitterNode {
return new ChannelSplitterNode(
this,
numberOfOutputs !== undefined ? { numberOfOutputs } : undefined
);
}

createIIRFilter(feedforward: number[], feedback: number[]): IIRFilterNode {
return new IIRFilterNode(this, { feedforward, feedback });
}

createBufferSource(options?: {
pitchCorrection: boolean;
}): AudioBufferSourceNode {
return new AudioBufferSourceNode(this, options);
}

createMediaElementSource(
mediaElement: HTMLMediaElement
): MediaElementAudioSourceNode {
Expand All @@ -130,70 +39,6 @@ export default class AudioContext implements BaseAudioContext {
);
}

createBuffer(
numberOfChannels: number,
length: number,
sampleRate: number
): AudioBuffer {
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)`
);
}

assertSupportedSampleRate(sampleRate);

return new AudioBuffer({ numberOfChannels, length, sampleRate });
}

createPeriodicWave(
real: Float32Array,
imag: Float32Array,
constraints?: PeriodicWaveConstraints
): PeriodicWave {
return new PeriodicWave(this, { real, imag, ...constraints });
}

createAnalyser(): AnalyserNode {
return new AnalyserNode(this);
}

createWaveShaper(): WaveShaperNode {
return new WaveShaperNode(this);
}

async decodeAudioData(
source: DecodeDataInput,
fetchOptions?: RequestInit
): Promise<AudioBuffer> {
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(): Promise<void> {
await this.context.close();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AudioParam from './AudioParam.web';
import BaseAudioContext from './BaseAudioContext.web';
import type BaseAudioContext from './BaseAudioContext.web';

export default class AudioListener {
readonly context: BaseAudioContext;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import BaseAudioContext from './BaseAudioContext.web';
import type BaseAudioContext from './BaseAudioContext.web';
import { ChannelCountMode, ChannelInterpretation } from '../types';
import AudioParam from './AudioParam.web';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RangeError, InvalidStateError } from '../errors';
import BaseAudioContext from './BaseAudioContext.web';
import type BaseAudioContext from './BaseAudioContext.web';

export default class AudioParam {
readonly defaultValue: number;
Expand Down
Loading
Loading