tone-stream 1.10.0 → 1.11.1
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 +37 -17
- package/index.js +12 -12
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,23 +28,32 @@ const { ToneStream } = require('tone-stream')
|
|
|
28
28
|
|
|
29
29
|
const Speaker = require('speaker')
|
|
30
30
|
|
|
31
|
+
const sampleRate = 8000
|
|
32
|
+
|
|
31
33
|
const format = {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
sampleRate,
|
|
35
|
+
bitDepth: 16,
|
|
36
|
+
channels: 1
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
const ts = new ToneStream(format)
|
|
38
40
|
|
|
39
41
|
const s = new Speaker(format)
|
|
40
42
|
|
|
41
|
-
|
|
42
|
-
ts.add([1000,
|
|
43
|
-
ts.add([1000,
|
|
43
|
+
var ns = 0
|
|
44
|
+
ns += ts.add([1000, 261.63]) // C4
|
|
45
|
+
ns += ts.add([1000, 296.33]) // D4
|
|
46
|
+
ns += ts.add([1000, 329.63]) // E4
|
|
47
|
+
|
|
48
|
+
var duration = ns / sampleRate * 1000
|
|
44
49
|
|
|
45
50
|
ts.pipe(s)
|
|
46
51
|
|
|
47
|
-
setTimeout(() => {
|
|
52
|
+
setTimeout(() => {
|
|
53
|
+
console.log("done")
|
|
54
|
+
process.exit(0)
|
|
55
|
+
}, duration)
|
|
56
|
+
|
|
48
57
|
```
|
|
49
58
|
|
|
50
59
|
Playing some DTMF tones:
|
|
@@ -54,25 +63,36 @@ const { ToneStream } = require('tone-stream')
|
|
|
54
63
|
|
|
55
64
|
const Speaker = require('speaker')
|
|
56
65
|
|
|
66
|
+
const sampleRate = 8000
|
|
67
|
+
|
|
57
68
|
const format = {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
69
|
+
sampleRate,
|
|
70
|
+
bitDepth: 16,
|
|
71
|
+
channels: 1
|
|
61
72
|
}
|
|
62
73
|
|
|
63
74
|
const ts = new ToneStream(format)
|
|
64
75
|
|
|
65
76
|
const s = new Speaker(format)
|
|
66
77
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
ts.add([1000, 'DTMF:
|
|
70
|
-
ts.add([500, 's'])
|
|
71
|
-
ts.add([1000, 'DTMF:
|
|
78
|
+
var ns = 0
|
|
79
|
+
|
|
80
|
+
ns += ts.add([1000, 'DTMF:1'])
|
|
81
|
+
ns += ts.add([500, 's'])
|
|
82
|
+
ns += ts.add([1000, 'DTMF:2'])
|
|
83
|
+
ns += ts.add([500, 's'])
|
|
84
|
+
ns += ts.add([1000, 'DTMF:3'])
|
|
85
|
+
ns += ts.add([500, 's'])
|
|
86
|
+
|
|
87
|
+
var duration = ns / sampleRate * 1000
|
|
72
88
|
|
|
73
89
|
ts.pipe(s)
|
|
74
90
|
|
|
75
|
-
setTimeout(() => {
|
|
91
|
+
setTimeout(() => {
|
|
92
|
+
console.log("done")
|
|
93
|
+
process.exit(0)
|
|
94
|
+
}, duration)
|
|
95
|
+
|
|
76
96
|
```
|
|
77
97
|
|
|
78
98
|
Using some helper function to add miscelaneous tones
|
|
@@ -120,7 +140,7 @@ ts.pipe(s)
|
|
|
120
140
|
```
|
|
121
141
|
## Events
|
|
122
142
|
|
|
123
|
-
The stream emits:
|
|
143
|
+
The stream emits events:
|
|
124
144
|
- 'empty': when there are no more tones to be played in the queue (it happens when the queue of tones is found empty)
|
|
125
145
|
- '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)
|
|
126
146
|
|
package/index.js
CHANGED
|
@@ -7,7 +7,7 @@ const DTMF = require("./lib/dtmf");
|
|
|
7
7
|
const EventEmitter = require('events');
|
|
8
8
|
|
|
9
9
|
class ToneStream extends Readable {
|
|
10
|
-
constructor(format
|
|
10
|
+
constructor(format) {
|
|
11
11
|
super();
|
|
12
12
|
|
|
13
13
|
if (format) {
|
|
@@ -21,8 +21,6 @@ class ToneStream extends Readable {
|
|
|
21
21
|
this.channels = 1;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
this.opts = opts;
|
|
25
|
-
|
|
26
24
|
this.amplitude = 2 ** this.bitDepth / 2 - 1;
|
|
27
25
|
|
|
28
26
|
this.specReadStream = new SpecReadStream();
|
|
@@ -37,13 +35,18 @@ class ToneStream extends Readable {
|
|
|
37
35
|
add(spec) {
|
|
38
36
|
this.specReadStream.add(spec);
|
|
39
37
|
this.pending_ended = true
|
|
38
|
+
var num_samples = spec[0]
|
|
39
|
+
return num_samples
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
concat(specs) {
|
|
43
|
+
var num_samples = 0
|
|
43
44
|
specs.forEach((spec) => {
|
|
44
45
|
this.specReadStream.add(spec);
|
|
46
|
+
num_samples += spec[0]
|
|
45
47
|
});
|
|
46
48
|
this.pending_ended = true
|
|
49
|
+
return num_samples
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
on(evt, cb) {
|
|
@@ -80,17 +83,14 @@ class ToneStream extends Readable {
|
|
|
80
83
|
this.pending_ended = false
|
|
81
84
|
this.eventEmitter.emit("ended")
|
|
82
85
|
}
|
|
83
|
-
if (this.opts && this.opts.stay_alive) {
|
|
84
|
-
for (var j = 0; j < numSamples * this.channels; j++) {
|
|
85
|
-
let offset = j * sampleSize * this.channels;
|
|
86
|
-
setter(0, offset);
|
|
87
|
-
}
|
|
88
86
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
return null;
|
|
87
|
+
for (var j = 0; j < numSamples * this.channels; j++) {
|
|
88
|
+
let offset = j * sampleSize * this.channels;
|
|
89
|
+
setter(0, offset);
|
|
93
90
|
}
|
|
91
|
+
|
|
92
|
+
this.push(buf);
|
|
93
|
+
return;
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
let actualSamples = specs.reduce((total, spec) => {
|