react-native-audio-api
Version:
react-native-audio-api provides system for controlling audio in React Native environment compatible with Web Audio API specification
59 lines (58 loc) • 2.25 kB
JavaScript
;
import AudioAPIModule from "../AudioAPIModule/index.js";
import { InvalidStateError, NotSupportedError } from "../errors/index.js";
import AudioBuffer from "./AudioBuffer.js";
import BaseAudioContext from "./BaseAudioContext.js";
export default class OfflineAudioContext extends BaseAudioContext {
constructor(arg0, arg1, arg2) {
const audioRuntime = AudioAPIModule.createAudioRuntime();
if (typeof arg0 === 'object') {
const {
numberOfChannels,
length,
sampleRate
} = arg0;
super(global.createOfflineAudioContext(numberOfChannels, length, sampleRate, audioRuntime));
this.duration = length / sampleRate;
} else if (typeof arg0 === 'number' && typeof arg1 === 'number' && typeof arg2 === 'number') {
super(global.createOfflineAudioContext(arg0, arg1, arg2, audioRuntime));
this.duration = arg1 / arg2;
} else {
throw new NotSupportedError('Invalid constructor arguments');
}
this.isSuspended = false;
this.isRendering = false;
}
async resume() {
if (!this.isRendering) {
throw new InvalidStateError('Cannot resume an OfflineAudioContext while rendering');
}
if (!this.isSuspended) {
throw new InvalidStateError('Cannot resume an OfflineAudioContext that is not suspended');
}
this.isSuspended = false;
await this.context.resume();
}
async suspend(suspendTime) {
if (suspendTime < 0) {
throw new InvalidStateError('suspendTime must be a non-negative number');
}
if (suspendTime < this.context.currentTime) {
throw new InvalidStateError(`suspendTime must be greater than the current time: ${suspendTime}`);
}
if (suspendTime > this.duration) {
throw new InvalidStateError(`suspendTime must be less than the duration of the context: ${suspendTime}`);
}
this.isSuspended = true;
await this.context.suspend(suspendTime);
}
async startRendering() {
if (this.isRendering) {
throw new InvalidStateError('OfflineAudioContext is already rendering');
}
this.isRendering = true;
const audioBuffer = await this.context.startRendering();
return new AudioBuffer(audioBuffer);
}
}
//# sourceMappingURL=OfflineAudioContext.js.map