tone-stream 1.9.0 → 1.11.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.
Files changed (3) hide show
  1. package/README.md +6 -0
  2. package/index.js +26 -12
  3. 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,8 +4,10 @@ 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
- constructor(format, opts) {
10
+ constructor(format) {
9
11
  super();
10
12
 
11
13
  if (format) {
@@ -19,23 +21,32 @@ class ToneStream extends Readable {
19
21
  this.channels = 1;
20
22
  }
21
23
 
22
- this.opts = opts;
23
-
24
24
  this.amplitude = 2 ** this.bitDepth / 2 - 1;
25
25
 
26
26
  this.specReadStream = new SpecReadStream();
27
27
 
28
28
  this.currentSample = 0;
29
+
30
+ this.pending_ended = false;
31
+
32
+ this.eventEmitter = new EventEmitter();
29
33
  }
30
34
 
31
35
  add(spec) {
32
36
  this.specReadStream.add(spec);
37
+ this.pending_ended = true
38
+ var num_samples = spec[0]
39
+ return num_samples
33
40
  }
34
41
 
35
42
  concat(specs) {
43
+ var num_samples = 0
36
44
  specs.forEach((spec) => {
37
45
  this.specReadStream.add(spec);
46
+ num_samples += spec[0]
38
47
  });
48
+ this.pending_ended = true
49
+ return num_samples
39
50
  }
40
51
 
41
52
  on(evt, cb) {
@@ -43,6 +54,8 @@ class ToneStream extends Readable {
43
54
 
44
55
  if (evt == "empty") {
45
56
  this.specReadStream.on(evt, cb);
57
+ } else if (evt == "ended") {
58
+ this.eventEmitter.on(evt, cb);
46
59
  }
47
60
  }
48
61
 
@@ -66,17 +79,18 @@ class ToneStream extends Readable {
66
79
  var buf_idx = 0;
67
80
 
68
81
  if (!specs) {
69
- if (this.opts && this.opts.stay_alive) {
70
- for (var j = 0; j < numSamples * this.channels; j++) {
71
- let offset = j * sampleSize * this.channels;
72
- setter(0, offset);
73
- }
82
+ if(this.pending_ended) {
83
+ this.pending_ended = false
84
+ this.eventEmitter.emit("ended")
85
+ }
74
86
 
75
- this.push(buf);
76
- return;
77
- } else {
78
- return null;
87
+ for (var j = 0; j < numSamples * this.channels; j++) {
88
+ let offset = j * sampleSize * this.channels;
89
+ setter(0, offset);
79
90
  }
91
+
92
+ this.push(buf);
93
+ return;
80
94
  }
81
95
 
82
96
  let actualSamples = specs.reduce((total, spec) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tone-stream",
3
- "version": "1.9.0",
3
+ "version": "1.11.0",
4
4
  "description": "A simple audio tone stream library",
5
5
  "main": "index.js",
6
6
  "repository": {