This repository has been archived by the owner on Sep 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwritable.js
109 lines (81 loc) · 2.12 KB
/
writable.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
/**
* @module web-audio-stream/writable
*
* Write stream data to web-audio.
*/
'use strict';
var inherits = require('inherits');
var Writable = require('stream').Writable;
var createWriter = require('./write');
module.exports = WAAWritable;
/**
* @constructor
*/
function WAAWritable (node, options) {
if (!(this instanceof WAAWritable)) return new WAAWritable(node, options);
let write = createWriter(node, options);
Writable.call(this, {
//we need object mode to recognize any type of input
objectMode: true,
//to keep processing delays very short, in case of RT binding.
//otherwise each stream will hoard data and release only when it’s full.
highWaterMark: 0,
write: (chunk, enc, cb) => {
return write(chunk, cb);
}
});
//manage input pipes number
this.inputsCount = 0;
this.on('pipe', (source) => {
this.inputsCount++;
//do autoend
source.once('end', () => {
this.end()
});
}).on('unpipe', (source) => {
this.inputsCount--;
})
//end writer
this.once('end', () => {
write.end()
})
}
inherits(WAAWritable, Writable);
/**
* Rendering modes
*/
WAAWritable.WORKER_MODE = 2;
WAAWritable.SCRIPT_MODE = 1;
WAAWritable.BUFFER_MODE = 0;
/**
* There is an opinion that script mode is better.
* https://github.com/brion/audio-feeder/issues/13
*
* But for me there are moments of glitch when it infinitely cycles sound. Very disappointing and makes feel desperate.
*
* But buffer mode also tend to create noisy clicks. Not sure why, cannot remove that.
* With script mode I at least defer my responsibility.
*/
WAAWritable.prototype.mode = WAAWritable.SCRIPT_MODE;
/** Count of inputs */
WAAWritable.prototype.inputsCount = 0;
/**
* Overrides stream’s end to ensure event.
*/
//FIXME: not sure why `end` is triggered here like 10 times.
WAAWritable.prototype.end = function () {
if (this.isEnded) return;
this.isEnded = true;
var triggered = false;
this.once('end', () => {
triggered = true;
});
Writable.prototype.end.call(this);
//timeout cb, because native end emits after a tick
setTimeout(() => {
if (!triggered) {
this.emit('end');
}
});
return this;
};