tone-stream 1.14.0 → 1.16.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 +2 -1
- package/index.js +1 -0
- package/lib/ssml.js +86 -0
- package/lib/utils.js +2 -2
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -95,7 +95,7 @@ setTimeout(() => {
|
|
|
95
95
|
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
-
Using some helper
|
|
98
|
+
Using some helper functions to add miscelaneous tones
|
|
99
99
|
```
|
|
100
100
|
const { ToneStream, utils } = require('tone-stream')
|
|
101
101
|
|
|
@@ -119,6 +119,7 @@ const tones = _.flatten([
|
|
|
119
119
|
utils.gen_dtmf_tones("1234567890abcdef", 100, 100, SAMPLE_RATE),
|
|
120
120
|
utils.gen_morse_tones("Be yourself; everyone else is already taken", 880, 70, SAMPLE_RATE),
|
|
121
121
|
utils.gen_music_scale("C5 D5 E5 F5 G5 A5 B5 C6", 100, 0, SAMPLE_RATE),
|
|
122
|
+
utils.gen_binary_tones_from_text("hello world", 5, 500, 2000, SAMPLE_RATE),
|
|
122
123
|
])
|
|
123
124
|
|
|
124
125
|
console.log("Inspecting tones:")
|
package/index.js
CHANGED
package/lib/ssml.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
|
|
2
|
+
const xml = require("xml-js")
|
|
3
|
+
|
|
4
|
+
function process(sampleRate, params, gen_tones, default_tone_duration) {
|
|
5
|
+
var elements
|
|
6
|
+
if(typeof params == 'string') {
|
|
7
|
+
if(params.startsWith('<speak>')) {
|
|
8
|
+
const parsed = xml.xml2js(params)
|
|
9
|
+
elements = parsed.elements[0].elements
|
|
10
|
+
} else {
|
|
11
|
+
elements = [
|
|
12
|
+
{
|
|
13
|
+
type: "text",
|
|
14
|
+
text: params
|
|
15
|
+
},
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
} else {
|
|
19
|
+
if(params["content-type"] == "application/ssml+xml" || params.text.startsWith('<speak>')) {
|
|
20
|
+
const parsed = xml.xml2js(params.text)
|
|
21
|
+
elements = parsed.elements[0].elements
|
|
22
|
+
} else {
|
|
23
|
+
elements = [
|
|
24
|
+
{
|
|
25
|
+
type: "text",
|
|
26
|
+
text: params.text,
|
|
27
|
+
},
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
//console.log(elements)
|
|
33
|
+
|
|
34
|
+
return process_elements(sampleRate, elements, gen_tones, default_tone_duration)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function parse_duration(duration) {
|
|
38
|
+
if (duration.endsWith("ms")) {
|
|
39
|
+
return parseInt(duration)
|
|
40
|
+
} else if (duration.endsWith("s")) {
|
|
41
|
+
return parseInt(duration) * 1000
|
|
42
|
+
} else {
|
|
43
|
+
throw `parse-failure: invalid duration ${duration}`
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function gen_silence(sampleRate, duration) {
|
|
48
|
+
var milliseconds = parse_duration(duration)
|
|
49
|
+
var samples = Math.round((sampleRate / 1000) * milliseconds)
|
|
50
|
+
return [samples, "s"]
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function process_elements(sampleRate, elements, gen_tones, default_tone_duration) {
|
|
54
|
+
var res
|
|
55
|
+
var spec = []
|
|
56
|
+
for (var i = 0; i < elements.length; i++) {
|
|
57
|
+
var e = elements[i]
|
|
58
|
+
if (e.type == "text") {
|
|
59
|
+
spec = spec.concat(gen_tones(sampleRate, e.text, default_tone_duration))
|
|
60
|
+
} else if (
|
|
61
|
+
e.type == "element" &&
|
|
62
|
+
e.name == "prosody" &&
|
|
63
|
+
e.attributes.rate &&
|
|
64
|
+
e.elements &&
|
|
65
|
+
e.elements[0] &&
|
|
66
|
+
e.elements[0].type == "text"
|
|
67
|
+
) {
|
|
68
|
+
var duration = parse_duration(e.attributes.rate)
|
|
69
|
+
spec = spec.concat(gen_tones(sampleRate, e.elements[0].text, duration))
|
|
70
|
+
} else if (
|
|
71
|
+
e.type == "element" &&
|
|
72
|
+
e.name == "break" &&
|
|
73
|
+
typeof e.attributes.time == "string"
|
|
74
|
+
) {
|
|
75
|
+
spec.push(gen_silence(sampleRate, e.attributes.time))
|
|
76
|
+
} else {
|
|
77
|
+
throw `parse-failure: invalid SSML element ${JSON.stringify(e)}`
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return spec
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = {
|
|
85
|
+
process,
|
|
86
|
+
}
|
package/lib/utils.js
CHANGED
|
@@ -119,9 +119,9 @@ const gen_binary_tones_from_text = (text, tone_duration, zero_freq, one_freq, sa
|
|
|
119
119
|
const samples_per_tone = Math.round(tone_duration * sampleRate / 1000)
|
|
120
120
|
|
|
121
121
|
const binaries = text.split("").map(asciiToBinary).join("").split("")
|
|
122
|
-
console.log("binaries", binaries)
|
|
122
|
+
//console.log("binaries", binaries)
|
|
123
123
|
const freqs = binaries.map(binary => binary == '0' ? zero_freq : one_freq)
|
|
124
|
-
console.log("freqs", freqs)
|
|
124
|
+
//console.log("freqs", freqs)
|
|
125
125
|
|
|
126
126
|
var last = null
|
|
127
127
|
for (var i=0 ; i<freqs.length; i++) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tone-stream",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.16.0",
|
|
4
4
|
"description": "A simple audio tone stream library",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -30,7 +30,8 @@
|
|
|
30
30
|
"dependencies": {
|
|
31
31
|
"morse-node": "^0.1.1",
|
|
32
32
|
"note-to-frequency": "^1.4.1",
|
|
33
|
-
"spec-read-stream": "^1.2.8"
|
|
33
|
+
"spec-read-stream": "^1.2.8",
|
|
34
|
+
"xml-js": "^1.6.11"
|
|
34
35
|
},
|
|
35
36
|
"files": [
|
|
36
37
|
"index.js",
|