-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimed-buffer.js
More file actions
113 lines (83 loc) · 2.9 KB
/
Copy pathtimed-buffer.js
File metadata and controls
113 lines (83 loc) · 2.9 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
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var EventEmitter = require("events").EventEmitter;
function TimedBuffer ( timeMS, options ){
if(!timeMS || typeof(timeMS) !== 'number')
throw 'TimedBuffer > ERROR: TimedBuffer requires > (int) timeMS';
EventEmitter.call(this);
if(typeof(options) !== 'object') options = {};
var self = this;
this.ms = options.timePadding+timeMS || timeMS;
this.buffer = [];
this.length = options.defaultLength || 2;
this.sampleRate = 0;
this._sampleRateBuffer = [];
this._sampleRateBufferLength = options.sampleRateRetention || 50;
this._lastTime = new Date().getTime();
return this;
}
TimedBuffer.prototype = Object.create(EventEmitter.prototype);
TimedBuffer.prototype._time = function () {
var time = new Date().getTime();
//Step Time
this._addToTimeBuffer(time - this._lastTime);
this._lastTime = time;
//Step Sample Rate
var sampleRate = this._calcSampleRate();
this._setSampleRate(sampleRate);
//Step Length
var length = this._calcLength();
this._setLength(length);
};
TimedBuffer.prototype._addToTimeBuffer = function ( ms ) {
if(this._sampleRateBuffer.length >= this._sampleRateBufferLength)
this._sampleRateBuffer.pop();
this._sampleRateBuffer.unshift(ms);
};
TimedBuffer.prototype._calcSampleRate = function () {
return this._sampleRateBuffer
.reduce(function ( prev, cur, index, array ){
if(index === array.length-1)
return (prev+cur)/array.length;
else
return (prev+cur);
});
};
TimedBuffer.prototype._setSampleRate = function ( sampleRateMS ) {
this.sampleRate = Math.floor(sampleRateMS)-1;
};
TimedBuffer.prototype._calcLength = function () {
if (!this.sampleRate) return options.defaultLength || 2;
return Math.ceil(this.ms/this.sampleRate);
};
TimedBuffer.prototype._setLength = function ( length ) {
this.length = length;
};
TimedBuffer.prototype.getLast = function (){
return this.buffer[0];
};
TimedBuffer.prototype.getByTime = function ( timeMS, skipMS ) {
if(!timeMS || typeof(timeMS) !== 'number')
throw 'TimedBuffer > ERROR: TimedBuffer.getSample requires > int (timeMS) || int (fromMS), int (toMS)';
var requiredFromBuffer,
skip;
if(skipMS) {
requiredFromBuffer = Math.ceil(timeMS/this.sampleRate);
skip = Math.ceil(skipMS/this.sampleRate);
return this.buffer.slice(skip, (skip+requiredFromBuffer));
} else {
requiredFromBuffer = Math.ceil(timeMS/this.sampleRate);
return this.buffer.slice(0, requiredFromBuffer);
}
};
TimedBuffer.prototype.push = function ( item ){
//Step Time
this._time();
//Adjust array size to calculated length
if(this.buffer.length >= this.length)
this.buffer = this.buffer.slice(0, this.length-1);
//Add item to beginning of array
this.buffer.unshift(item);
//Send Push Event
this.emit('add', item);
this.emit('change', this.buffer);
};
module.exports = TimedBuffer;