react-native-audio-api
Version:
react-native-audio-api provides system for controlling audio in React Native environment compatible with Web Audio API specification
71 lines (68 loc) • 2.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _errors = require("../errors");
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 _errors.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 _errors.IndexSizeError(`The channel number provided (${channelNumber}) is outside the range [0, ${this.numberOfChannels - 1}]`);
}
if (startInChannel < 0 || startInChannel >= this.length) {
throw new _errors.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 _errors.IndexSizeError(`The channel number provided (${channelNumber}) is outside the range [0, ${this.numberOfChannels - 1}]`);
}
if (startInChannel < 0 || startInChannel >= this.length) {
throw new _errors.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 _errors.NotSupportedError(`The number of channels provided (${numberOfChannels}) is outside the range [1, 32]`);
}
if (length <= 0) {
throw new _errors.NotSupportedError(`The number of frames provided (${length}) is less than or equal to the minimum bound (0)`);
}
if (sampleRate < 8000 || sampleRate > 96000) {
throw new _errors.NotSupportedError(`The sample rate provided (${sampleRate}) is outside the range [8000, 96000]`);
}
return new globalThis.AudioBuffer({
numberOfChannels,
length,
sampleRate
});
}
isAudioBuffer(obj) {
return typeof obj === 'object' && obj !== null && 'getChannelData' in obj && typeof obj.getChannelData === 'function';
}
}
exports.default = AudioBuffer;
//# sourceMappingURL=AudioBuffer.web.js.map