speechflow 1.3.1 → 1.3.2
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/CHANGELOG.md +6 -0
- package/dst/speechflow-node-a2a-gender.d.ts +2 -0
- package/dst/speechflow-node-a2a-gender.js +137 -59
- package/dst/speechflow-node-a2a-gender.js.map +1 -1
- package/dst/speechflow-node-a2a-meter.d.ts +3 -1
- package/dst/speechflow-node-a2a-meter.js +80 -39
- package/dst/speechflow-node-a2a-meter.js.map +1 -1
- package/dst/speechflow-node-a2a-mute.d.ts +1 -0
- package/dst/speechflow-node-a2a-mute.js +37 -11
- package/dst/speechflow-node-a2a-mute.js.map +1 -1
- package/dst/speechflow-node-a2a-vad.d.ts +3 -0
- package/dst/speechflow-node-a2a-vad.js +194 -96
- package/dst/speechflow-node-a2a-vad.js.map +1 -1
- package/dst/speechflow-node-a2a-wav.js +27 -11
- package/dst/speechflow-node-a2a-wav.js.map +1 -1
- package/dst/speechflow-node-a2t-deepgram.d.ts +4 -0
- package/dst/speechflow-node-a2t-deepgram.js +136 -46
- package/dst/speechflow-node-a2t-deepgram.js.map +1 -1
- package/dst/speechflow-node-t2a-elevenlabs.d.ts +2 -0
- package/dst/speechflow-node-t2a-elevenlabs.js +61 -12
- package/dst/speechflow-node-t2a-elevenlabs.js.map +1 -1
- package/dst/speechflow-node-t2a-kokoro.d.ts +1 -0
- package/dst/speechflow-node-t2a-kokoro.js +10 -4
- package/dst/speechflow-node-t2a-kokoro.js.map +1 -1
- package/dst/speechflow-node-t2t-deepl.js +8 -4
- package/dst/speechflow-node-t2t-deepl.js.map +1 -1
- package/dst/speechflow-node-t2t-format.js +2 -2
- package/dst/speechflow-node-t2t-format.js.map +1 -1
- package/dst/speechflow-node-t2t-ollama.js +1 -1
- package/dst/speechflow-node-t2t-ollama.js.map +1 -1
- package/dst/speechflow-node-t2t-openai.js +1 -1
- package/dst/speechflow-node-t2t-openai.js.map +1 -1
- package/dst/speechflow-node-t2t-sentence.d.ts +1 -1
- package/dst/speechflow-node-t2t-sentence.js +34 -18
- package/dst/speechflow-node-t2t-sentence.js.map +1 -1
- package/dst/speechflow-node-t2t-subtitle.d.ts +0 -1
- package/dst/speechflow-node-t2t-subtitle.js +78 -190
- package/dst/speechflow-node-t2t-subtitle.js.map +1 -1
- package/dst/speechflow-node-t2t-transformers.js +2 -2
- package/dst/speechflow-node-t2t-transformers.js.map +1 -1
- package/dst/speechflow-node-x2x-filter.js +4 -4
- package/dst/speechflow-node-x2x-trace.js +6 -13
- package/dst/speechflow-node-x2x-trace.js.map +1 -1
- package/dst/speechflow-node-xio-device.js +12 -8
- package/dst/speechflow-node-xio-device.js.map +1 -1
- package/dst/speechflow-node-xio-file.js +9 -3
- package/dst/speechflow-node-xio-file.js.map +1 -1
- package/dst/speechflow-node-xio-mqtt.js +5 -2
- package/dst/speechflow-node-xio-mqtt.js.map +1 -1
- package/dst/speechflow-node-xio-websocket.js +11 -11
- package/dst/speechflow-node-xio-websocket.js.map +1 -1
- package/dst/speechflow-node.d.ts +0 -2
- package/dst/speechflow-node.js +0 -3
- package/dst/speechflow-node.js.map +1 -1
- package/dst/speechflow-utils.d.ts +5 -0
- package/dst/speechflow-utils.js +77 -44
- package/dst/speechflow-utils.js.map +1 -1
- package/dst/speechflow.js +101 -82
- package/dst/speechflow.js.map +1 -1
- package/etc/eslint.mjs +1 -2
- package/etc/stx.conf +3 -3
- package/package.json +6 -6
- package/src/speechflow-node-a2a-gender.ts +148 -64
- package/src/speechflow-node-a2a-meter.ts +87 -40
- package/src/speechflow-node-a2a-mute.ts +39 -11
- package/src/speechflow-node-a2a-vad.ts +206 -100
- package/src/speechflow-node-a2a-wav.ts +27 -11
- package/src/speechflow-node-a2t-deepgram.ts +139 -43
- package/src/speechflow-node-t2a-elevenlabs.ts +65 -12
- package/src/speechflow-node-t2a-kokoro.ts +11 -4
- package/src/speechflow-node-t2t-deepl.ts +9 -4
- package/src/speechflow-node-t2t-format.ts +2 -2
- package/src/speechflow-node-t2t-ollama.ts +1 -1
- package/src/speechflow-node-t2t-openai.ts +1 -1
- package/src/speechflow-node-t2t-sentence.ts +37 -20
- package/src/speechflow-node-t2t-transformers.ts +4 -3
- package/src/speechflow-node-x2x-filter.ts +4 -4
- package/src/speechflow-node-x2x-trace.ts +1 -1
- package/src/speechflow-node-xio-device.ts +12 -8
- package/src/speechflow-node-xio-file.ts +9 -3
- package/src/speechflow-node-xio-mqtt.ts +5 -2
- package/src/speechflow-node-xio-websocket.ts +12 -12
- package/src/speechflow-utils.ts +78 -44
- package/src/speechflow.ts +114 -35
|
@@ -29,12 +29,12 @@ export default class SpeechFlowNodeSentence extends SpeechFlowNode {
|
|
|
29
29
|
public static name = "sentence"
|
|
30
30
|
|
|
31
31
|
/* internal state */
|
|
32
|
-
private static speexInitialized = false
|
|
33
32
|
private queue = new utils.Queue<TextQueueElement>()
|
|
34
33
|
private queueRecv = this.queue.pointerUse("recv")
|
|
35
34
|
private queueSplit = this.queue.pointerUse("split")
|
|
36
35
|
private queueSend = this.queue.pointerUse("send")
|
|
37
36
|
private destroyed = false
|
|
37
|
+
private workingOffTimer: ReturnType<typeof setTimeout> | null = null
|
|
38
38
|
|
|
39
39
|
/* construct node */
|
|
40
40
|
constructor (id: string, cfg: { [ id: string ]: any }, opts: { [ id: string ]: any }, args: any[]) {
|
|
@@ -53,11 +53,7 @@ export default class SpeechFlowNodeSentence extends SpeechFlowNode {
|
|
|
53
53
|
/* clear destruction flag */
|
|
54
54
|
this.destroyed = false
|
|
55
55
|
|
|
56
|
-
/* pass-through logging */
|
|
57
|
-
const log = (level: string, msg: string) => { this.log(level, msg) }
|
|
58
|
-
|
|
59
56
|
/* work off queued audio frames */
|
|
60
|
-
let workingOffTimer: ReturnType<typeof setTimeout> | null = null
|
|
61
57
|
let workingOff = false
|
|
62
58
|
const workOffQueue = async () => {
|
|
63
59
|
if (this.destroyed)
|
|
@@ -67,14 +63,14 @@ export default class SpeechFlowNodeSentence extends SpeechFlowNode {
|
|
|
67
63
|
if (workingOff)
|
|
68
64
|
return
|
|
69
65
|
workingOff = true
|
|
70
|
-
if (workingOffTimer !== null) {
|
|
71
|
-
clearTimeout(workingOffTimer)
|
|
72
|
-
workingOffTimer = null
|
|
66
|
+
if (this.workingOffTimer !== null) {
|
|
67
|
+
clearTimeout(this.workingOffTimer)
|
|
68
|
+
this.workingOffTimer = null
|
|
73
69
|
}
|
|
74
70
|
this.queue.off("write", workOffQueue)
|
|
75
71
|
|
|
76
72
|
/* try to work off one or more chunks */
|
|
77
|
-
while (
|
|
73
|
+
while (!this.destroyed) {
|
|
78
74
|
const element = this.queueSplit.peek()
|
|
79
75
|
if (element === undefined)
|
|
80
76
|
break
|
|
@@ -136,10 +132,12 @@ export default class SpeechFlowNodeSentence extends SpeechFlowNode {
|
|
|
136
132
|
}
|
|
137
133
|
}
|
|
138
134
|
|
|
139
|
-
/* re-initiate working off round */
|
|
135
|
+
/* re-initiate working off round (if still not destroyed) */
|
|
140
136
|
workingOff = false
|
|
141
|
-
|
|
142
|
-
|
|
137
|
+
if (!this.destroyed) {
|
|
138
|
+
this.workingOffTimer = setTimeout(workOffQueue, 100)
|
|
139
|
+
this.queue.once("write", workOffQueue)
|
|
140
|
+
}
|
|
143
141
|
}
|
|
144
142
|
this.queue.once("write", workOffQueue)
|
|
145
143
|
|
|
@@ -153,12 +151,14 @@ export default class SpeechFlowNodeSentence extends SpeechFlowNode {
|
|
|
153
151
|
|
|
154
152
|
/* receive text chunk (writable side of stream) */
|
|
155
153
|
write (chunk: SpeechFlowChunk, encoding, callback) {
|
|
156
|
-
if (
|
|
154
|
+
if (self.destroyed)
|
|
155
|
+
callback(new Error("stream already destroyed"))
|
|
156
|
+
else if (Buffer.isBuffer(chunk.payload))
|
|
157
157
|
callback(new Error("expected text input as string chunks"))
|
|
158
158
|
else if (chunk.payload.length === 0)
|
|
159
159
|
callback()
|
|
160
160
|
else {
|
|
161
|
-
log("info", `received text: ${JSON.stringify(chunk.payload)}`)
|
|
161
|
+
self.log("info", `received text: ${JSON.stringify(chunk.payload)}`)
|
|
162
162
|
self.queueRecv.append({ type: "text-frame", chunk })
|
|
163
163
|
callback()
|
|
164
164
|
}
|
|
@@ -166,6 +166,10 @@ export default class SpeechFlowNodeSentence extends SpeechFlowNode {
|
|
|
166
166
|
|
|
167
167
|
/* receive no more text chunks (writable side of stream) */
|
|
168
168
|
final (callback) {
|
|
169
|
+
if (self.destroyed) {
|
|
170
|
+
callback()
|
|
171
|
+
return
|
|
172
|
+
}
|
|
169
173
|
/* signal end of file */
|
|
170
174
|
self.queueRecv.append({ type: "text-eof" })
|
|
171
175
|
callback()
|
|
@@ -173,8 +177,12 @@ export default class SpeechFlowNodeSentence extends SpeechFlowNode {
|
|
|
173
177
|
|
|
174
178
|
/* send text chunk(s) (readable side of stream) */
|
|
175
179
|
read (_size) {
|
|
176
|
-
/* flush pending
|
|
180
|
+
/* flush pending text chunks */
|
|
177
181
|
const flushPendingChunks = () => {
|
|
182
|
+
if (self.destroyed) {
|
|
183
|
+
this.push(null)
|
|
184
|
+
return
|
|
185
|
+
}
|
|
178
186
|
const element = self.queueSend.peek()
|
|
179
187
|
if (element !== undefined
|
|
180
188
|
&& element.type === "text-eof") {
|
|
@@ -196,13 +204,13 @@ export default class SpeechFlowNodeSentence extends SpeechFlowNode {
|
|
|
196
204
|
else if (element.type === "text-frame"
|
|
197
205
|
&& element.complete !== true)
|
|
198
206
|
break
|
|
199
|
-
log("info", `send text: ${JSON.stringify(element.chunk.payload)}`)
|
|
207
|
+
self.log("info", `send text: ${JSON.stringify(element.chunk.payload)}`)
|
|
200
208
|
this.push(element.chunk)
|
|
201
209
|
self.queueSend.walk(+1)
|
|
202
210
|
self.queue.trim()
|
|
203
211
|
}
|
|
204
212
|
}
|
|
205
|
-
else
|
|
213
|
+
else if (!self.destroyed)
|
|
206
214
|
self.queue.once("write", flushPendingChunks)
|
|
207
215
|
}
|
|
208
216
|
flushPendingChunks()
|
|
@@ -212,13 +220,22 @@ export default class SpeechFlowNodeSentence extends SpeechFlowNode {
|
|
|
212
220
|
|
|
213
221
|
/* close node */
|
|
214
222
|
async close () {
|
|
223
|
+
/* indicate destruction */
|
|
224
|
+
this.destroyed = true
|
|
225
|
+
|
|
226
|
+
/* clean up timer */
|
|
227
|
+
if (this.workingOffTimer !== null) {
|
|
228
|
+
clearTimeout(this.workingOffTimer)
|
|
229
|
+
this.workingOffTimer = null
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/* remove any pending event listeners */
|
|
233
|
+
this.queue.removeAllListeners("write")
|
|
234
|
+
|
|
215
235
|
/* close stream */
|
|
216
236
|
if (this.stream !== null) {
|
|
217
237
|
this.stream.destroy()
|
|
218
238
|
this.stream = null
|
|
219
239
|
}
|
|
220
|
-
|
|
221
|
-
/* indicate destruction */
|
|
222
|
-
this.destroyed = true
|
|
223
240
|
}
|
|
224
241
|
}
|
|
@@ -104,7 +104,7 @@ export default class SpeechFlowNodeTransformers extends SpeechFlowNode {
|
|
|
104
104
|
|
|
105
105
|
/* open node */
|
|
106
106
|
async open () {
|
|
107
|
-
let model
|
|
107
|
+
let model = ""
|
|
108
108
|
|
|
109
109
|
/* track download progress when instantiating Transformers engine and model */
|
|
110
110
|
const progressState = new Map<string, number>()
|
|
@@ -138,7 +138,6 @@ export default class SpeechFlowNodeTransformers extends SpeechFlowNode {
|
|
|
138
138
|
progress_callback: progressCallback
|
|
139
139
|
})
|
|
140
140
|
this.translator = await pipeline
|
|
141
|
-
clearInterval(interval)
|
|
142
141
|
if (this.translator === null)
|
|
143
142
|
throw new Error("failed to instantiate translator pipeline")
|
|
144
143
|
}
|
|
@@ -151,13 +150,15 @@ export default class SpeechFlowNodeTransformers extends SpeechFlowNode {
|
|
|
151
150
|
progress_callback: progressCallback
|
|
152
151
|
})
|
|
153
152
|
this.generator = await pipeline
|
|
154
|
-
clearInterval(interval)
|
|
155
153
|
if (this.generator === null)
|
|
156
154
|
throw new Error("failed to instantiate generator pipeline")
|
|
157
155
|
}
|
|
158
156
|
else
|
|
159
157
|
throw new Error("invalid model")
|
|
160
158
|
|
|
159
|
+
/* clear progress interval again */
|
|
160
|
+
clearInterval(interval)
|
|
161
|
+
|
|
161
162
|
/* provide text-to-text translation */
|
|
162
163
|
const translate = async (text: string) => {
|
|
163
164
|
if (this.params.model === "OPUS") {
|
|
@@ -94,13 +94,13 @@ export default class SpeechFlowNodeFilter extends SpeechFlowNode {
|
|
|
94
94
|
const m = self.params.var.match(/^meta:(.+)$/)
|
|
95
95
|
if (m !== null)
|
|
96
96
|
val1 = chunk.meta.get(m[1]) ?? ""
|
|
97
|
-
else if (self.params.
|
|
97
|
+
else if (self.params.var === "payload:length")
|
|
98
98
|
val1 = chunk.payload.length
|
|
99
|
-
else if (self.params.
|
|
99
|
+
else if (self.params.var === "payload:text")
|
|
100
100
|
val1 = (self.params.type === "text" ? chunk.payload as string : "")
|
|
101
|
-
else if (self.params.
|
|
101
|
+
else if (self.params.var === "time:start")
|
|
102
102
|
val1 = chunk.timestampStart.toMillis()
|
|
103
|
-
else if (self.params.
|
|
103
|
+
else if (self.params.var === "time:end")
|
|
104
104
|
val1 = chunk.timestampEnd.toMillis()
|
|
105
105
|
if (comparison(val1, self.params.op, val2)) {
|
|
106
106
|
self.log("info", `[${self.params.name}]: passing through ${chunk.type} chunk`)
|
|
@@ -75,7 +75,7 @@ export default class SpeechFlowNodeTrace extends SpeechFlowNode {
|
|
|
75
75
|
}
|
|
76
76
|
else {
|
|
77
77
|
if (type === "text")
|
|
78
|
-
log("debug",
|
|
78
|
+
log("debug", `chunk: type=${chunk.type} ` +
|
|
79
79
|
`kind=${chunk.kind} ` +
|
|
80
80
|
`start=${fmtTime(chunk.timestampStart)} ` +
|
|
81
81
|
`end=${fmtTime(chunk.timestampEnd)} ` +
|
|
@@ -159,8 +159,7 @@ export default class SpeechFlowNodeDevice extends SpeechFlowNode {
|
|
|
159
159
|
|
|
160
160
|
/* convert regular stream into object-mode stream */
|
|
161
161
|
const wrapper = utils.createTransformStreamForReadableSide("audio", () => this.timeZero)
|
|
162
|
-
this.stream.
|
|
163
|
-
this.stream = wrapper
|
|
162
|
+
this.stream = Stream.compose(this.stream, wrapper)
|
|
164
163
|
}
|
|
165
164
|
else if (this.params.mode === "w") {
|
|
166
165
|
/* output device */
|
|
@@ -180,8 +179,7 @@ export default class SpeechFlowNodeDevice extends SpeechFlowNode {
|
|
|
180
179
|
|
|
181
180
|
/* convert regular stream into object-mode stream */
|
|
182
181
|
const wrapper = utils.createTransformStreamForWritableSide()
|
|
183
|
-
|
|
184
|
-
this.stream = wrapper
|
|
182
|
+
this.stream = Stream.compose(wrapper, this.stream)
|
|
185
183
|
}
|
|
186
184
|
else
|
|
187
185
|
throw new Error(`device "${device.id}" does not have any input or output channels`)
|
|
@@ -200,13 +198,19 @@ export default class SpeechFlowNodeDevice extends SpeechFlowNode {
|
|
|
200
198
|
/* shutdown PortAudio */
|
|
201
199
|
if (this.io !== null) {
|
|
202
200
|
await new Promise<void>((resolve, reject) => {
|
|
203
|
-
this.io!.abort(() => {
|
|
204
|
-
|
|
201
|
+
this.io!.abort((err?: Error) => {
|
|
202
|
+
if (err)
|
|
203
|
+
reject(err)
|
|
204
|
+
else
|
|
205
|
+
resolve()
|
|
205
206
|
})
|
|
206
207
|
})
|
|
207
208
|
await new Promise<void>((resolve, reject) => {
|
|
208
|
-
this.io!.quit(() => {
|
|
209
|
-
|
|
209
|
+
this.io!.quit((err?: Error) => {
|
|
210
|
+
if (err)
|
|
211
|
+
reject(err)
|
|
212
|
+
else
|
|
213
|
+
resolve()
|
|
210
214
|
})
|
|
211
215
|
})
|
|
212
216
|
this.io = null
|
|
@@ -178,9 +178,15 @@ export default class SpeechFlowNodeFile extends SpeechFlowNode {
|
|
|
178
178
|
async close () {
|
|
179
179
|
/* shutdown stream */
|
|
180
180
|
if (this.stream !== null) {
|
|
181
|
-
await new Promise<void>((resolve) => {
|
|
182
|
-
if (this.stream instanceof Stream.Writable || this.stream instanceof Stream.Duplex)
|
|
183
|
-
this.stream.end(() => {
|
|
181
|
+
await new Promise<void>((resolve, reject) => {
|
|
182
|
+
if (this.stream instanceof Stream.Writable || this.stream instanceof Stream.Duplex) {
|
|
183
|
+
this.stream.end((err?: Error) => {
|
|
184
|
+
if (err)
|
|
185
|
+
reject(err)
|
|
186
|
+
else
|
|
187
|
+
resolve()
|
|
188
|
+
})
|
|
189
|
+
}
|
|
184
190
|
else
|
|
185
191
|
resolve()
|
|
186
192
|
})
|
|
@@ -83,7 +83,10 @@ export default class SpeechFlowNodeMQTT extends SpeechFlowNode {
|
|
|
83
83
|
this.broker.on("connect", (packet: MQTT.IConnackPacket) => {
|
|
84
84
|
this.log("info", `connection opened to MQTT ${this.params.url}`)
|
|
85
85
|
if (this.params.mode !== "w" && !packet.sessionPresent)
|
|
86
|
-
this.broker!.subscribe([ this.params.topicRead ], () => {
|
|
86
|
+
this.broker!.subscribe([ this.params.topicRead ], (err) => {
|
|
87
|
+
if (err)
|
|
88
|
+
this.log("error", `failed to subscribe to MQTT topic "${this.params.topicRead}": ${err.message}`)
|
|
89
|
+
})
|
|
87
90
|
})
|
|
88
91
|
this.broker.on("reconnect", () => {
|
|
89
92
|
this.log("info", `connection re-opened to MQTT ${this.params.url}`)
|
|
@@ -141,7 +144,7 @@ export default class SpeechFlowNodeMQTT extends SpeechFlowNode {
|
|
|
141
144
|
|
|
142
145
|
/* close node */
|
|
143
146
|
async close () {
|
|
144
|
-
/* close
|
|
147
|
+
/* close MQTT broker */
|
|
145
148
|
if (this.broker !== null) {
|
|
146
149
|
if (this.broker.connected)
|
|
147
150
|
this.broker.end()
|
|
@@ -64,15 +64,15 @@ export default class SpeechFlowNodeWebsocket extends SpeechFlowNode {
|
|
|
64
64
|
const url = new URL(this.params.listen)
|
|
65
65
|
const websockets = new Set<ws.WebSocket>()
|
|
66
66
|
const chunkQueue = new utils.SingleQueue<SpeechFlowChunk>()
|
|
67
|
-
|
|
67
|
+
this.server = new ws.WebSocketServer({
|
|
68
68
|
host: url.hostname,
|
|
69
69
|
port: Number.parseInt(url.port),
|
|
70
70
|
path: url.pathname
|
|
71
71
|
})
|
|
72
|
-
server.on("listening", () => {
|
|
72
|
+
this.server.on("listening", () => {
|
|
73
73
|
this.log("info", `listening on URL ${this.params.listen}`)
|
|
74
74
|
})
|
|
75
|
-
server.on("connection", (ws, request) => {
|
|
75
|
+
this.server.on("connection", (ws, request) => {
|
|
76
76
|
const peer = `${request.socket.remoteAddress}:${request.socket.remotePort}`
|
|
77
77
|
this.log("info", `connection opened on URL ${this.params.listen} by peer ${peer}`)
|
|
78
78
|
websockets.add(ws)
|
|
@@ -105,7 +105,7 @@ export default class SpeechFlowNodeWebsocket extends SpeechFlowNode {
|
|
|
105
105
|
chunkQueue.write(chunk)
|
|
106
106
|
})
|
|
107
107
|
})
|
|
108
|
-
server.on("error", (error) => {
|
|
108
|
+
this.server.on("error", (error) => {
|
|
109
109
|
this.log("error", `error of some connection on URL ${this.params.listen}: ${error.message}`)
|
|
110
110
|
})
|
|
111
111
|
const type = this.params.type
|
|
@@ -124,7 +124,7 @@ export default class SpeechFlowNodeWebsocket extends SpeechFlowNode {
|
|
|
124
124
|
callback(new Error("still no Websocket connections available"))
|
|
125
125
|
else {
|
|
126
126
|
const data = utils.streamChunkEncode(chunk)
|
|
127
|
-
const results = []
|
|
127
|
+
const results: Promise<void>[] = []
|
|
128
128
|
for (const websocket of websockets.values()) {
|
|
129
129
|
results.push(new Promise<void>((resolve, reject) => {
|
|
130
130
|
websocket.send(data, (error) => {
|
|
@@ -175,12 +175,12 @@ export default class SpeechFlowNodeWebsocket extends SpeechFlowNode {
|
|
|
175
175
|
const chunkQueue = new utils.SingleQueue<SpeechFlowChunk>()
|
|
176
176
|
this.client.addEventListener("message", (ev: MessageEvent) => {
|
|
177
177
|
if (this.params.mode === "w") {
|
|
178
|
-
this.log("warning", `connection to URL ${this.params.
|
|
178
|
+
this.log("warning", `connection to URL ${this.params.connect}: ` +
|
|
179
179
|
"received remote data on write-only node")
|
|
180
180
|
return
|
|
181
181
|
}
|
|
182
182
|
if (!(ev.data instanceof ArrayBuffer)) {
|
|
183
|
-
this.log("warning", `connection to URL ${this.params.
|
|
183
|
+
this.log("warning", `connection to URL ${this.params.connect}: ` +
|
|
184
184
|
"received non-binary message")
|
|
185
185
|
return
|
|
186
186
|
}
|
|
@@ -204,15 +204,15 @@ export default class SpeechFlowNodeWebsocket extends SpeechFlowNode {
|
|
|
204
204
|
callback(new Error(`written chunk is not of ${type} type`))
|
|
205
205
|
else if (!client.OPEN)
|
|
206
206
|
callback(new Error("still no Websocket connection available"))
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
207
|
+
else {
|
|
208
|
+
const data = utils.streamChunkEncode(chunk)
|
|
209
|
+
client.send(data)
|
|
210
|
+
callback()
|
|
211
|
+
}
|
|
210
212
|
},
|
|
211
213
|
read (size: number) {
|
|
212
214
|
if (mode === "w")
|
|
213
215
|
throw new Error("read operation on write-only node")
|
|
214
|
-
if (!client.OPEN)
|
|
215
|
-
throw new Error("still no Websocket connection available")
|
|
216
216
|
chunkQueue.read().then((chunk) => {
|
|
217
217
|
this.push(chunk, "binary")
|
|
218
218
|
})
|
package/src/speechflow-utils.ts
CHANGED
|
@@ -24,11 +24,19 @@ export function audioBufferDuration (
|
|
|
24
24
|
channels = 1,
|
|
25
25
|
littleEndian = true
|
|
26
26
|
) {
|
|
27
|
+
/* sanity check parameters */
|
|
27
28
|
if (!Buffer.isBuffer(buffer))
|
|
28
29
|
throw new Error("invalid input (Buffer expected)")
|
|
29
30
|
if (littleEndian !== true)
|
|
30
31
|
throw new Error("only Little Endian supported")
|
|
32
|
+
if (sampleRate <= 0)
|
|
33
|
+
throw new Error("sample rate must be positive")
|
|
34
|
+
if (bitDepth <= 0 || bitDepth % 8 !== 0)
|
|
35
|
+
throw new Error("bit depth must be positive and multiple of 8")
|
|
36
|
+
if (channels <= 0)
|
|
37
|
+
throw new Error("channels must be positive")
|
|
31
38
|
|
|
39
|
+
/* calculate duration */
|
|
32
40
|
const bytesPerSample = bitDepth / 8
|
|
33
41
|
const totalSamples = buffer.length / (bytesPerSample * channels)
|
|
34
42
|
return totalSamples / sampleRate
|
|
@@ -40,12 +48,23 @@ export function audioArrayDuration (
|
|
|
40
48
|
sampleRate = 48000,
|
|
41
49
|
channels = 1
|
|
42
50
|
) {
|
|
51
|
+
/* sanity check parameters */
|
|
52
|
+
if (arr.length === 0)
|
|
53
|
+
return 0
|
|
54
|
+
if (sampleRate <= 0)
|
|
55
|
+
throw new Error("sample rate must be positive")
|
|
56
|
+
if (channels <= 0)
|
|
57
|
+
throw new Error("channels must be positive")
|
|
58
|
+
|
|
59
|
+
/* calculate duration */
|
|
43
60
|
const totalSamples = arr.length / channels
|
|
44
61
|
return totalSamples / sampleRate
|
|
45
62
|
}
|
|
46
63
|
|
|
47
64
|
/* helper function: convert Buffer in PCM/I16 to Float32Array in PCM/F32 format */
|
|
48
65
|
export function convertBufToF32 (buf: Buffer, littleEndian = true) {
|
|
66
|
+
if (buf.length % 2 !== 0)
|
|
67
|
+
throw new Error("buffer length must be even for 16-bit samples")
|
|
49
68
|
const dataView = new DataView(buf.buffer)
|
|
50
69
|
const arr = new Float32Array(buf.length / 2)
|
|
51
70
|
for (let i = 0; i < arr.length; i++)
|
|
@@ -55,9 +74,15 @@ export function convertBufToF32 (buf: Buffer, littleEndian = true) {
|
|
|
55
74
|
|
|
56
75
|
/* helper function: convert Float32Array in PCM/F32 to Buffer in PCM/I16 format */
|
|
57
76
|
export function convertF32ToBuf (arr: Float32Array) {
|
|
77
|
+
if (arr.length === 0)
|
|
78
|
+
return Buffer.alloc(0)
|
|
58
79
|
const int16Array = new Int16Array(arr.length)
|
|
59
|
-
for (let i = 0; i < arr.length; i++)
|
|
60
|
-
|
|
80
|
+
for (let i = 0; i < arr.length; i++) {
|
|
81
|
+
let sample = arr[i]
|
|
82
|
+
if (Number.isNaN(sample))
|
|
83
|
+
sample = 0
|
|
84
|
+
int16Array[i] = Math.max(-32768, Math.min(32767, Math.round(sample * 32768)))
|
|
85
|
+
}
|
|
61
86
|
return Buffer.from(int16Array.buffer)
|
|
62
87
|
}
|
|
63
88
|
|
|
@@ -274,26 +299,19 @@ export class QueuePointer<T extends QueueElement> extends EventEmitter {
|
|
|
274
299
|
}
|
|
275
300
|
position (index?: number): number {
|
|
276
301
|
if (index !== undefined) {
|
|
277
|
-
this.index = index
|
|
278
|
-
if (this.index < 0)
|
|
279
|
-
this.index = 0
|
|
280
|
-
else if (this.index >= this.queue.elements.length)
|
|
281
|
-
this.index = this.queue.elements.length
|
|
302
|
+
this.index = Math.max(0, Math.min(index, this.queue.elements.length))
|
|
282
303
|
this.emit("position", this.index)
|
|
283
304
|
}
|
|
284
305
|
return this.index
|
|
285
306
|
}
|
|
286
307
|
walk (num: number) {
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
308
|
+
const indexOld = this.index
|
|
309
|
+
if (num > 0)
|
|
310
|
+
this.index = Math.min(this.index + num, this.queue.elements.length)
|
|
311
|
+
else if (num < 0)
|
|
312
|
+
this.index = Math.max(this.index + num, 0)
|
|
313
|
+
if (this.index !== indexOld)
|
|
290
314
|
this.emit("position", { start: this.index })
|
|
291
|
-
}
|
|
292
|
-
else if (num < 0) {
|
|
293
|
-
for (let i = 0; i < Math.abs(num) && this.index > 0; i++)
|
|
294
|
-
this.index--
|
|
295
|
-
this.emit("position", { start: this.index })
|
|
296
|
-
}
|
|
297
315
|
}
|
|
298
316
|
walkForwardUntil (type: T["type"]) {
|
|
299
317
|
while (this.index < this.queue.elements.length
|
|
@@ -330,12 +348,7 @@ export class QueuePointer<T extends QueueElement> extends EventEmitter {
|
|
|
330
348
|
peek (position?: number) {
|
|
331
349
|
if (position === undefined)
|
|
332
350
|
position = this.index
|
|
333
|
-
|
|
334
|
-
if (position < 0)
|
|
335
|
-
position = 0
|
|
336
|
-
else if (position > this.queue.elements.length)
|
|
337
|
-
position = this.queue.elements.length
|
|
338
|
-
}
|
|
351
|
+
position = Math.max(0, Math.min(position, this.queue.elements.length))
|
|
339
352
|
const element = this.queue.elements[position]
|
|
340
353
|
this.queue.emit("read", { start: position, end: position })
|
|
341
354
|
return element
|
|
@@ -351,11 +364,8 @@ export class QueuePointer<T extends QueueElement> extends EventEmitter {
|
|
|
351
364
|
let slice: T[]
|
|
352
365
|
const start = this.index
|
|
353
366
|
if (size !== undefined) {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
else if (size > this.queue.elements.length - this.index)
|
|
357
|
-
size = this.queue.elements.length - this.index
|
|
358
|
-
slice = this.queue.elements.slice(this.index, size)
|
|
367
|
+
size = Math.max(0, Math.min(size, this.queue.elements.length - this.index))
|
|
368
|
+
slice = this.queue.elements.slice(this.index, this.index + size)
|
|
359
369
|
this.index += size
|
|
360
370
|
}
|
|
361
371
|
else {
|
|
@@ -415,45 +425,58 @@ export class Queue<T extends QueueElement> extends EventEmitter {
|
|
|
415
425
|
min = pointer.position()
|
|
416
426
|
|
|
417
427
|
/* trim the maximum amount of first elements */
|
|
418
|
-
|
|
428
|
+
if (min > 0) {
|
|
429
|
+
this.elements.splice(0, min)
|
|
419
430
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
431
|
+
/* shift all pointers */
|
|
432
|
+
for (const pointer of this.pointers.values())
|
|
433
|
+
pointer.position(pointer.position() - min)
|
|
434
|
+
}
|
|
423
435
|
}
|
|
424
436
|
}
|
|
425
437
|
|
|
426
438
|
/* utility class for wrapping a custom stream into a regular Transform stream */
|
|
427
439
|
export class StreamWrapper extends Stream.Transform {
|
|
428
440
|
private foreignStream: any
|
|
441
|
+
private onData = (chunk: any) => { this.push(chunk) }
|
|
442
|
+
private onError = (err: Error) => { this.emit("error", err) }
|
|
443
|
+
private onEnd = () => { this.push(null) }
|
|
429
444
|
constructor (foreignStream: any, options: Stream.TransformOptions = {}) {
|
|
430
445
|
options.readableObjectMode = true
|
|
431
446
|
options.writableObjectMode = true
|
|
432
447
|
super(options)
|
|
433
448
|
this.foreignStream = foreignStream
|
|
434
|
-
this.foreignStream.on
|
|
435
|
-
this.
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
})
|
|
440
|
-
this.foreignStream.on("end", () => {
|
|
441
|
-
this.push(null)
|
|
442
|
-
})
|
|
449
|
+
if (typeof this.foreignStream.on === "function") {
|
|
450
|
+
this.foreignStream.on("data", this.onData)
|
|
451
|
+
this.foreignStream.on("error", this.onError)
|
|
452
|
+
this.foreignStream.on("end", this.onEnd)
|
|
453
|
+
}
|
|
443
454
|
}
|
|
444
455
|
_transform (chunk: any, encoding: BufferEncoding, callback: Stream.TransformCallback): void {
|
|
456
|
+
if (this.destroyed) {
|
|
457
|
+
callback(new Error("stream already destroyed"))
|
|
458
|
+
return
|
|
459
|
+
}
|
|
445
460
|
try {
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
461
|
+
if (typeof this.foreignStream.write === "function") {
|
|
462
|
+
const canContinue = this.foreignStream.write(chunk)
|
|
463
|
+
if (canContinue)
|
|
464
|
+
callback()
|
|
465
|
+
else
|
|
466
|
+
this.foreignStream.once("drain", callback)
|
|
467
|
+
}
|
|
449
468
|
else
|
|
450
|
-
|
|
469
|
+
throw new Error("foreign stream lacks write method")
|
|
451
470
|
}
|
|
452
471
|
catch (err) {
|
|
453
472
|
callback(err as Error)
|
|
454
473
|
}
|
|
455
474
|
}
|
|
456
475
|
_flush (callback: Stream.TransformCallback): void {
|
|
476
|
+
if (this.destroyed) {
|
|
477
|
+
callback(new Error("stream already destroyed"))
|
|
478
|
+
return
|
|
479
|
+
}
|
|
457
480
|
try {
|
|
458
481
|
if (typeof this.foreignStream.end === "function")
|
|
459
482
|
this.foreignStream.end()
|
|
@@ -463,6 +486,14 @@ export class StreamWrapper extends Stream.Transform {
|
|
|
463
486
|
callback(err as Error)
|
|
464
487
|
}
|
|
465
488
|
}
|
|
489
|
+
_destroy (error: Error | null, callback: Stream.TransformCallback): void {
|
|
490
|
+
if (typeof this.foreignStream.removeListener === "function") {
|
|
491
|
+
this.foreignStream.removeListener("data", this.onData)
|
|
492
|
+
this.foreignStream.removeListener("error", this.onError)
|
|
493
|
+
this.foreignStream.removeListener("end", this.onEnd)
|
|
494
|
+
}
|
|
495
|
+
super._destroy(error, callback)
|
|
496
|
+
}
|
|
466
497
|
}
|
|
467
498
|
|
|
468
499
|
/* meta store */
|
|
@@ -485,4 +516,7 @@ export class TimeStore<T> extends EventEmitter {
|
|
|
485
516
|
if (interval.low < before && interval.high < before)
|
|
486
517
|
this.tree.remove(interval)
|
|
487
518
|
}
|
|
519
|
+
clear (): void {
|
|
520
|
+
this.tree = new IntervalTree.IntervalTree<TimeStoreInterval<T>>()
|
|
521
|
+
}
|
|
488
522
|
}
|