-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
76 lines (65 loc) · 2.78 KB
/
index.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* AudioBuffer class
*/
export default class AudioBuffer {
/**
* Create AudioBuffer instance.
* @constructor
* @param {Object} options - buffer init options.
* @param {number} options.length - buffer length in samples.
* @param {number} options.sampleRate - buffer sample rate.
* @param {number} options.numberOfChannels - number of channels.
*/
constructor(options) {
if (!options) throw TypeError('options argument is required')
if (!options.sampleRate) throw TypeError('options.sampleRate is required')
if (options.sampleRate < 3000 || options.sampleRate > 768000) throw TypeError('options.sampleRate must be within 3000..768000')
if (!options.length) throw TypeError('options.length must be more than 0')
this.sampleRate = options.sampleRate
this.numberOfChannels = options.numberOfChannels || 1
this.length = options.length | 0
this.duration = this.length / this.sampleRate
//data is stored as a planar sequence
this._data = new Float32Array(this.length * this.numberOfChannels)
//channels data is cached as subarrays
this._channelData = []
for (let c = 0; c < this.numberOfChannels; c++) {
this._channelData.push(this._data.subarray(c * this.length, (c+1) * this.length))
}
}
/**
* Return data associated with the channel.
* @param {number} channel - Channel index, starting from 0.
* @return {Float32Array} Array containing the data.
*/
getChannelData (channel) {
if (channel >= this.numberOfChannels || channel < 0 || channel == null) throw Error('Cannot getChannelData: channel number (' + channel + ') exceeds number of channels (' + this.numberOfChannels + ')');
return this._channelData[channel]
}
/**
* Place data to the destination buffer, starting from the position.
* @param {Float32Array} destination - Destination array to write data to.
* @param {number} channelNumber - Channel to take data from.
* @param {number} startInChannel - Data offset in channel to read from.
*/
copyFromChannel (destination, channelNumber, startInChannel) {
if (startInChannel == null) startInChannel = 0;
var data = this._channelData[channelNumber]
for (var i = startInChannel, j = 0; i < this.length && j < destination.length; i++, j++) {
destination[j] = data[i];
}
}
/**
* Place data from the source to the channel, starting (in self) from the position.
* @param {Float32Array | Array} source - source array to read data from.
* @param {number} channelNumber - channel index to copy data to.
* @param {number} startInChannel - offset in channel to copy data to.
*/
copyToChannel (source, channelNumber, startInChannel) {
var data = this._channelData[channelNumber]
if (!startInChannel) startInChannel = 0;
for (var i = startInChannel, j = 0; i < this.length && j < source.length; i++, j++) {
data[i] = source[j];
}
}
}