react-native-audio-api
Version:
react-native-audio-api provides system for controlling audio in React Native environment compatible with Web Audio API specification
69 lines (59 loc) • 2.17 kB
text/typescript
import { IAudioBuffer } from '../interfaces';
import { IndexSizeError } from '../errors';
export default class AudioBuffer {
readonly length: number;
readonly duration: number;
readonly sampleRate: number;
readonly numberOfChannels: number;
/** @internal */
public readonly buffer: IAudioBuffer;
constructor(buffer: IAudioBuffer) {
this.buffer = buffer;
this.length = buffer.length;
this.duration = buffer.duration;
this.sampleRate = buffer.sampleRate;
this.numberOfChannels = buffer.numberOfChannels;
}
public getChannelData(channel: number): Float32Array {
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);
}
public copyFromChannel(
destination: Float32Array,
channelNumber: number,
startInChannel: number = 0
): void {
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);
}
public copyToChannel(
source: Float32Array,
channelNumber: number,
startInChannel: number = 0
): void {
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);
}
}