-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioBuffer.ts
More file actions
35 lines (30 loc) · 1.42 KB
/
AudioBuffer.ts
File metadata and controls
35 lines (30 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
export class PolyfillAudioBuffer implements AudioBuffer {
public readonly duration: number;
public readonly length: number;
public readonly numberOfChannels: number;
public readonly sampleRate: number;
private _channels: Float32Array[];
public constructor(options: AudioBufferOptions) {
this.duration = options.length / options.sampleRate;
this.length = options.length;
this.numberOfChannels = options.numberOfChannels;
this.sampleRate = options.sampleRate;
this._channels = new Array(options.numberOfChannels);
for (let i = 0; i < options.numberOfChannels; i++) {
this._channels[i] = new Float32Array(options.length);
}
}
public copyFromChannel(destination: Float32Array, channelNumber: number, startInChannel: number = 0): void {
destination.set(this._channels[channelNumber].subarray(startInChannel, startInChannel + destination.length));
}
public copyToChannel(source: Float32Array, channelNumber: number, startInChannel: number = 0): void {
const copySize = this._channels[channelNumber].length - startInChannel;
if (copySize <= 0) return;
this._channels[channelNumber].set(source.subarray(0, copySize), startInChannel);
}
public getChannelData(channel: number): Float32Array {
let o = new Float32Array(this.length);
o.set(this._channels[channel]);
return o;
}
}