tone-stream 1.9.0 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/index.js +14 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,6 +12,7 @@ or DTMF tones:
|
|
|
12
12
|
```
|
|
13
13
|
[NUMBER_OF_SAMPLES, 'DTMF:ID']
|
|
14
14
|
```
|
|
15
|
+
You can add multiple of such tones and they will be enequeued and played in order.
|
|
15
16
|
|
|
16
17
|
## Installation
|
|
17
18
|
```
|
|
@@ -117,6 +118,11 @@ console.log("Starting playing tones")
|
|
|
117
118
|
ts.pipe(s)
|
|
118
119
|
|
|
119
120
|
```
|
|
121
|
+
## Events
|
|
122
|
+
|
|
123
|
+
The stream emits:
|
|
124
|
+
- 'empty': when there are no more tones to be played in the queue (it happens when the queue of tones is found empty)
|
|
125
|
+
- 'ended': when all tones in the queue were generated (it happens when the consumer tries to read data from the stream and there are no more tones to be generated)
|
|
120
126
|
|
|
121
127
|
## More examples
|
|
122
128
|
|
package/index.js
CHANGED
|
@@ -4,6 +4,8 @@ const SpecReadStream = require("spec-read-stream");
|
|
|
4
4
|
|
|
5
5
|
const DTMF = require("./lib/dtmf");
|
|
6
6
|
|
|
7
|
+
const EventEmitter = require('events');
|
|
8
|
+
|
|
7
9
|
class ToneStream extends Readable {
|
|
8
10
|
constructor(format, opts) {
|
|
9
11
|
super();
|
|
@@ -26,16 +28,22 @@ class ToneStream extends Readable {
|
|
|
26
28
|
this.specReadStream = new SpecReadStream();
|
|
27
29
|
|
|
28
30
|
this.currentSample = 0;
|
|
31
|
+
|
|
32
|
+
this.pending_ended = false;
|
|
33
|
+
|
|
34
|
+
this.eventEmitter = new EventEmitter();
|
|
29
35
|
}
|
|
30
36
|
|
|
31
37
|
add(spec) {
|
|
32
38
|
this.specReadStream.add(spec);
|
|
39
|
+
this.pending_ended = true
|
|
33
40
|
}
|
|
34
41
|
|
|
35
42
|
concat(specs) {
|
|
36
43
|
specs.forEach((spec) => {
|
|
37
44
|
this.specReadStream.add(spec);
|
|
38
45
|
});
|
|
46
|
+
this.pending_ended = true
|
|
39
47
|
}
|
|
40
48
|
|
|
41
49
|
on(evt, cb) {
|
|
@@ -43,6 +51,8 @@ class ToneStream extends Readable {
|
|
|
43
51
|
|
|
44
52
|
if (evt == "empty") {
|
|
45
53
|
this.specReadStream.on(evt, cb);
|
|
54
|
+
} else if (evt == "ended") {
|
|
55
|
+
this.eventEmitter.on(evt, cb);
|
|
46
56
|
}
|
|
47
57
|
}
|
|
48
58
|
|
|
@@ -66,6 +76,10 @@ class ToneStream extends Readable {
|
|
|
66
76
|
var buf_idx = 0;
|
|
67
77
|
|
|
68
78
|
if (!specs) {
|
|
79
|
+
if(this.pending_ended) {
|
|
80
|
+
this.pending_ended = false
|
|
81
|
+
this.eventEmitter.emit("ended")
|
|
82
|
+
}
|
|
69
83
|
if (this.opts && this.opts.stay_alive) {
|
|
70
84
|
for (var j = 0; j < numSamples * this.channels; j++) {
|
|
71
85
|
let offset = j * sampleSize * this.channels;
|