-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
109 lines (81 loc) · 2.08 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
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
/**
* Rendering stream.
*
* It does not transform audio to text
* because rendering may require repetition of the same data multiple times.
* It just plots captured amount of data to a canvas in user-defined way.
*
* Separation by node/browser files is also unnecessary,
* we can detect environment in-place without serious size overdraft.
* All the required modules provide short browser versions.
*
* Any routines which may vary, like sliding window behaviour,
* realtime data binding, etc are left to user.
*
* It is a transform stream with exception where there are no piped outputs,
* it releases data, i. e. behaves like a writable.
*
* @module audio-render
*/
var inherits = require('inherits');
var Analyser = require('audio-analyser');
var isBrowser = require('is-browser');
var Canvas = require('drawille-canvas');
//detect rendering scheduler
var raf, cancel;
if (isBrowser && (raf = requestAnimationFrame)) {
cancel = cancelAnimationFrame;
}
else {
raf = setTimeout;
cancel = clearTimeout;
}
/**
* @constructor
*/
function RenderStream (options) {
if (!(this instanceof RenderStream)) return new RenderStream(options);
var self = this;
if (options instanceof Function) {
options = {
render: options
};
}
Analyser.call(self, options);
//ensure canvas
if (!self.canvas) {
self.canvas = new Canvas();
}
//set throttling
self.throttle = 1000 / self.framesPerSecond;
//plan rendering
self._id = raf(update, self.throttle);
//stop on end
self.on('end', function () {
cancel(self._id);
});
function update () {
self._id = raf(update, self.throttle);
try {
self.render(self.canvas, self._data);
} catch (e) {
throw e;
}
try {
self.emit('render', self.canvas);
} catch (e) {
throw e;
}
}
}
/** It should be duplex not to block pipe if there is no output sink */
inherits(RenderStream, Analyser);
/** How often to update */
RenderStream.prototype.framesPerSecond = 20;
/**
* Default rendering method, does nothing
* @override
*/
RenderStream.prototype.render = function (canvas, data) {
};
module.exports = RenderStream;