react-native-audio-api
Version:
react-native-audio-api provides system for controlling audio in React Native environment compatible with Web Audio API specification
62 lines (59 loc) • 2.8 kB
JavaScript
;
import { IndexSizeError, NotSupportedError } from "../errors/index.js";
export default class AudioBuffer {
/** @internal */
/** @internal */
/** @internal */
constructor(arg) {
this.buffer = this.isAudioBuffer(arg) ? arg : AudioBuffer.createBufferFromOptions(arg);
this.length = this.buffer.length;
this.duration = this.buffer.duration;
this.sampleRate = this.buffer.sampleRate;
this.numberOfChannels = this.buffer.numberOfChannels;
}
getChannelData(channel) {
if (channel < 0 || channel >= this.numberOfChannels) {
throw new IndexSizeError(`The channel number provided (${channel}) is outside the range [0, ${this.numberOfChannels - 1}]`);
}
return this.buffer.getChannelData(channel);
}
copyFromChannel(destination, channelNumber, startInChannel = 0) {
if (channelNumber < 0 || channelNumber >= this.numberOfChannels) {
throw new IndexSizeError(`The channel number provided (${channelNumber}) is outside the range [0, ${this.numberOfChannels - 1}]`);
}
if (startInChannel < 0 || startInChannel >= this.length) {
throw new IndexSizeError(`The startInChannel number provided (${startInChannel}) is outside the range [0, ${this.length - 1}]`);
}
this.buffer.copyFromChannel(destination, channelNumber, startInChannel);
}
copyToChannel(source, channelNumber, startInChannel = 0) {
if (channelNumber < 0 || channelNumber >= this.numberOfChannels) {
throw new IndexSizeError(`The channel number provided (${channelNumber}) is outside the range [0, ${this.numberOfChannels - 1}]`);
}
if (startInChannel < 0 || startInChannel >= this.length) {
throw new IndexSizeError(`The startInChannel number provided (${startInChannel}) is outside the range [0, ${this.length - 1}]`);
}
this.buffer.copyToChannel(source, channelNumber, startInChannel);
}
static createBufferFromOptions(options) {
const {
numberOfChannels = 1,
length,
sampleRate
} = options;
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 globalThis.createAudioBuffer(numberOfChannels, length, sampleRate);
}
isAudioBuffer(obj) {
return typeof obj === 'object' && obj !== null && 'getChannelData' in obj && typeof obj.getChannelData === 'function';
}
}
//# sourceMappingURL=AudioBuffer.js.map