speechflow 0.9.5 → 0.9.7
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 +19 -0
- package/README.md +221 -53
- package/dst/speechflow-node-a2a-ffmpeg.d.ts +13 -0
- package/dst/speechflow-node-a2a-ffmpeg.js +152 -0
- package/dst/speechflow-node-a2a-wav.d.ts +11 -0
- package/dst/speechflow-node-a2a-wav.js +170 -0
- package/dst/speechflow-node-a2t-deepgram.d.ts +12 -0
- package/dst/speechflow-node-a2t-deepgram.js +220 -0
- package/dst/speechflow-node-deepgram.d.ts +3 -1
- package/dst/speechflow-node-deepgram.js +86 -22
- package/dst/speechflow-node-deepl.d.ts +3 -1
- package/dst/speechflow-node-deepl.js +25 -20
- package/dst/speechflow-node-device.d.ts +3 -1
- package/dst/speechflow-node-device.js +53 -2
- package/dst/speechflow-node-elevenlabs.d.ts +3 -1
- package/dst/speechflow-node-elevenlabs.js +37 -42
- package/dst/speechflow-node-ffmpeg.d.ts +3 -1
- package/dst/speechflow-node-ffmpeg.js +42 -4
- package/dst/speechflow-node-file.d.ts +3 -1
- package/dst/speechflow-node-file.js +84 -13
- package/dst/speechflow-node-format.d.ts +11 -0
- package/dst/speechflow-node-format.js +80 -0
- package/dst/speechflow-node-gemma.d.ts +3 -1
- package/dst/speechflow-node-gemma.js +84 -23
- package/dst/speechflow-node-mqtt.d.ts +13 -0
- package/dst/speechflow-node-mqtt.js +181 -0
- package/dst/speechflow-node-opus.d.ts +12 -0
- package/dst/speechflow-node-opus.js +135 -0
- package/dst/speechflow-node-subtitle.d.ts +12 -0
- package/dst/speechflow-node-subtitle.js +96 -0
- package/dst/speechflow-node-t2a-elevenlabs.d.ts +13 -0
- package/dst/speechflow-node-t2a-elevenlabs.js +182 -0
- package/dst/speechflow-node-t2t-deepl.d.ts +12 -0
- package/dst/speechflow-node-t2t-deepl.js +133 -0
- package/dst/speechflow-node-t2t-format.d.ts +11 -0
- package/dst/speechflow-node-t2t-format.js +80 -0
- package/dst/speechflow-node-t2t-gemma.d.ts +13 -0
- package/dst/speechflow-node-t2t-gemma.js +213 -0
- package/dst/speechflow-node-t2t-opus.d.ts +12 -0
- package/dst/speechflow-node-t2t-opus.js +135 -0
- package/dst/speechflow-node-t2t-subtitle.d.ts +12 -0
- package/dst/speechflow-node-t2t-subtitle.js +96 -0
- package/dst/speechflow-node-trace.d.ts +11 -0
- package/dst/speechflow-node-trace.js +88 -0
- package/dst/speechflow-node-wav.d.ts +11 -0
- package/dst/speechflow-node-wav.js +170 -0
- package/dst/speechflow-node-websocket.d.ts +3 -1
- package/dst/speechflow-node-websocket.js +149 -49
- package/dst/speechflow-node-whisper-common.d.ts +34 -0
- package/dst/speechflow-node-whisper-common.js +7 -0
- package/dst/speechflow-node-whisper-ggml.d.ts +1 -0
- package/dst/speechflow-node-whisper-ggml.js +97 -0
- package/dst/speechflow-node-whisper-onnx.d.ts +1 -0
- package/dst/speechflow-node-whisper-onnx.js +131 -0
- package/dst/speechflow-node-whisper-worker-ggml.d.ts +1 -0
- package/dst/speechflow-node-whisper-worker-ggml.js +97 -0
- package/dst/speechflow-node-whisper-worker-onnx.d.ts +1 -0
- package/dst/speechflow-node-whisper-worker-onnx.js +131 -0
- package/dst/speechflow-node-whisper-worker.d.ts +1 -0
- package/dst/speechflow-node-whisper-worker.js +116 -0
- package/dst/speechflow-node-whisper-worker2.d.ts +1 -0
- package/dst/speechflow-node-whisper-worker2.js +82 -0
- package/dst/speechflow-node-whisper.d.ts +19 -0
- package/dst/speechflow-node-whisper.js +604 -0
- package/dst/speechflow-node-x2x-trace.d.ts +11 -0
- package/dst/speechflow-node-x2x-trace.js +88 -0
- package/dst/speechflow-node-xio-device.d.ts +13 -0
- package/dst/speechflow-node-xio-device.js +205 -0
- package/dst/speechflow-node-xio-file.d.ts +11 -0
- package/dst/speechflow-node-xio-file.js +176 -0
- package/dst/speechflow-node-xio-mqtt.d.ts +13 -0
- package/dst/speechflow-node-xio-mqtt.js +181 -0
- package/dst/speechflow-node-xio-websocket.d.ts +13 -0
- package/dst/speechflow-node-xio-websocket.js +275 -0
- package/dst/speechflow-node.d.ts +24 -6
- package/dst/speechflow-node.js +63 -6
- package/dst/speechflow-utils.d.ts +23 -0
- package/dst/speechflow-utils.js +194 -0
- package/dst/speechflow.js +146 -43
- package/etc/biome.jsonc +12 -4
- package/etc/stx.conf +65 -0
- package/package.d/@ericedouard+vad-node-realtime+0.2.0.patch +18 -0
- package/package.json +49 -31
- package/sample.yaml +59 -27
- package/src/lib.d.ts +6 -1
- package/src/{speechflow-node-ffmpeg.ts → speechflow-node-a2a-ffmpeg.ts} +10 -4
- package/src/speechflow-node-a2a-wav.ts +143 -0
- package/src/speechflow-node-a2t-deepgram.ts +199 -0
- package/src/{speechflow-node-elevenlabs.ts → speechflow-node-t2a-elevenlabs.ts} +38 -45
- package/src/{speechflow-node-deepl.ts → speechflow-node-t2t-deepl.ts} +36 -25
- package/src/speechflow-node-t2t-format.ts +85 -0
- package/src/{speechflow-node-gemma.ts → speechflow-node-t2t-gemma.ts} +89 -25
- package/src/speechflow-node-t2t-opus.ts +111 -0
- package/src/speechflow-node-t2t-subtitle.ts +101 -0
- package/src/speechflow-node-x2x-trace.ts +92 -0
- package/src/{speechflow-node-device.ts → speechflow-node-xio-device.ts} +25 -3
- package/src/speechflow-node-xio-file.ts +153 -0
- package/src/speechflow-node-xio-mqtt.ts +154 -0
- package/src/speechflow-node-xio-websocket.ts +248 -0
- package/src/speechflow-node.ts +63 -6
- package/src/speechflow-utils.ts +212 -0
- package/src/speechflow.ts +150 -43
- package/etc/nps.yaml +0 -40
- package/src/speechflow-node-deepgram.ts +0 -133
- package/src/speechflow-node-file.ts +0 -108
- package/src/speechflow-node-websocket.ts +0 -179
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
** SpeechFlow - Speech Processing Flow Graph
|
|
4
|
+
** Copyright (c) 2024-2025 Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
5
|
+
** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
+
}) : function(o, v) {
|
|
21
|
+
o["default"] = v;
|
|
22
|
+
});
|
|
23
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
+
var ownKeys = function(o) {
|
|
25
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
+
var ar = [];
|
|
27
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
+
return ar;
|
|
29
|
+
};
|
|
30
|
+
return ownKeys(o);
|
|
31
|
+
};
|
|
32
|
+
return function (mod) {
|
|
33
|
+
if (mod && mod.__esModule) return mod;
|
|
34
|
+
var result = {};
|
|
35
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
+
__setModuleDefault(result, mod);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
})();
|
|
40
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
41
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
42
|
+
};
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
/* standard dependencies */
|
|
45
|
+
const node_stream_1 = __importDefault(require("node:stream"));
|
|
46
|
+
/* external dependencies */
|
|
47
|
+
const mqtt_1 = __importDefault(require("mqtt"));
|
|
48
|
+
const pure_uuid_1 = __importDefault(require("pure-uuid"));
|
|
49
|
+
/* internal dependencies */
|
|
50
|
+
const speechflow_node_1 = __importDefault(require("./speechflow-node"));
|
|
51
|
+
const utils = __importStar(require("./speechflow-utils"));
|
|
52
|
+
/* SpeechFlow node for MQTT networking */
|
|
53
|
+
class SpeechFlowNodeMQTT extends speechflow_node_1.default {
|
|
54
|
+
/* declare official node name */
|
|
55
|
+
static name = "mqtt";
|
|
56
|
+
/* internal state */
|
|
57
|
+
broker = null;
|
|
58
|
+
clientId = (new pure_uuid_1.default(1)).format();
|
|
59
|
+
/* construct node */
|
|
60
|
+
constructor(id, cfg, opts, args) {
|
|
61
|
+
super(id, cfg, opts, args);
|
|
62
|
+
/* declare node configuration parameters */
|
|
63
|
+
this.configure({
|
|
64
|
+
url: { type: "string", pos: 0, val: "", match: /^(?:|(?:ws|mqtt):\/\/(.+?):(\d+)(?:\/.*)?)$/ },
|
|
65
|
+
username: { type: "string", pos: 1, val: "", match: /^.+$/ },
|
|
66
|
+
password: { type: "string", pos: 2, val: "", match: /^.+$/ },
|
|
67
|
+
topicRead: { type: "string", pos: 3, val: "", match: /^.+$/ },
|
|
68
|
+
topicWrite: { type: "string", pos: 4, val: "", match: /^.+$/ },
|
|
69
|
+
mode: { type: "string", pos: 5, val: "w", match: /^(?:r|w|rw)$/ },
|
|
70
|
+
type: { type: "string", pos: 6, val: "text", match: /^(?:audio|text)$/ }
|
|
71
|
+
});
|
|
72
|
+
/* logical parameter sanity check */
|
|
73
|
+
if ((this.params.mode === "w" || this.params.mode === "rw") && this.params.topicWrite === "")
|
|
74
|
+
throw new Error("writing to MQTT requires a topicWrite parameter");
|
|
75
|
+
if ((this.params.mode === "r" || this.params.mode === "rw") && this.params.topicRead === "")
|
|
76
|
+
throw new Error("reading from MQTT requires a topicRead parameter");
|
|
77
|
+
/* declare node input/output format */
|
|
78
|
+
if (this.params.mode === "rw") {
|
|
79
|
+
this.input = this.params.type;
|
|
80
|
+
this.output = this.params.type;
|
|
81
|
+
}
|
|
82
|
+
else if (this.params.mode === "r") {
|
|
83
|
+
this.input = "none";
|
|
84
|
+
this.output = this.params.type;
|
|
85
|
+
}
|
|
86
|
+
else if (this.params.mode === "w") {
|
|
87
|
+
this.input = this.params.type;
|
|
88
|
+
this.output = "none";
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/* open node */
|
|
92
|
+
async open() {
|
|
93
|
+
/* connect remotely to a MQTT broker */
|
|
94
|
+
this.broker = mqtt_1.default.connect(this.params.url, {
|
|
95
|
+
protocolId: "MQTT",
|
|
96
|
+
protocolVersion: 5,
|
|
97
|
+
username: this.params.username,
|
|
98
|
+
password: this.params.password,
|
|
99
|
+
clientId: this.clientId,
|
|
100
|
+
clean: true,
|
|
101
|
+
resubscribe: true,
|
|
102
|
+
keepalive: 60, /* 60s */
|
|
103
|
+
reconnectPeriod: 2 * 1000, /* 2s */
|
|
104
|
+
connectTimeout: 30 * 1000 /* 30s */
|
|
105
|
+
});
|
|
106
|
+
this.broker.on("error", (error) => {
|
|
107
|
+
this.log("error", `error on MQTT ${this.params.url}: ${error.message}`);
|
|
108
|
+
});
|
|
109
|
+
this.broker.on("connect", (packet) => {
|
|
110
|
+
this.log("info", `connection opened to MQTT ${this.params.url}`);
|
|
111
|
+
if (this.params.mode !== "w" && !packet.sessionPresent)
|
|
112
|
+
this.broker.subscribe([this.params.topicRead], () => { });
|
|
113
|
+
});
|
|
114
|
+
this.broker.on("reconnect", () => {
|
|
115
|
+
this.log("info", `connection re-opened to MQTT ${this.params.url}`);
|
|
116
|
+
});
|
|
117
|
+
this.broker.on("disconnect", (packet) => {
|
|
118
|
+
this.log("info", `connection closed to MQTT ${this.params.url}`);
|
|
119
|
+
});
|
|
120
|
+
const chunkQueue = new utils.SingleQueue();
|
|
121
|
+
this.broker.on("message", (topic, payload, packet) => {
|
|
122
|
+
if (topic !== this.params.topicRead)
|
|
123
|
+
return;
|
|
124
|
+
try {
|
|
125
|
+
const chunk = utils.streamChunkDecode(payload);
|
|
126
|
+
chunkQueue.write(chunk);
|
|
127
|
+
}
|
|
128
|
+
catch (_err) {
|
|
129
|
+
this.log("warning", `received invalid CBOR chunk from MQTT ${this.params.url}`);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
const broker = this.broker;
|
|
133
|
+
const topicWrite = this.params.topicWrite;
|
|
134
|
+
const type = this.params.type;
|
|
135
|
+
const mode = this.params.mode;
|
|
136
|
+
this.stream = new node_stream_1.default.Duplex({
|
|
137
|
+
writableObjectMode: true,
|
|
138
|
+
readableObjectMode: true,
|
|
139
|
+
decodeStrings: false,
|
|
140
|
+
write(chunk, encoding, callback) {
|
|
141
|
+
if (mode === "r")
|
|
142
|
+
callback(new Error("write operation on read-only node"));
|
|
143
|
+
else if (chunk.type !== type)
|
|
144
|
+
callback(new Error(`written chunk is not of ${type} type`));
|
|
145
|
+
else if (!broker.connected)
|
|
146
|
+
callback(new Error("still no MQTT connection available"));
|
|
147
|
+
else {
|
|
148
|
+
const data = Buffer.from(utils.streamChunkEncode(chunk));
|
|
149
|
+
broker.publish(topicWrite, data, { qos: 2, retain: false }, (err) => {
|
|
150
|
+
if (err)
|
|
151
|
+
callback(new Error(`failed to publish to MQTT topic "${topicWrite}": ${err}`));
|
|
152
|
+
else
|
|
153
|
+
callback();
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
read(size) {
|
|
158
|
+
if (mode === "w")
|
|
159
|
+
throw new Error("read operation on write-only node");
|
|
160
|
+
chunkQueue.read().then((chunk) => {
|
|
161
|
+
this.push(chunk, "binary");
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
/* close node */
|
|
167
|
+
async close() {
|
|
168
|
+
/* close Websocket server */
|
|
169
|
+
if (this.broker !== null) {
|
|
170
|
+
if (this.broker.connected)
|
|
171
|
+
this.broker.end();
|
|
172
|
+
this.broker = null;
|
|
173
|
+
}
|
|
174
|
+
/* close stream */
|
|
175
|
+
if (this.stream !== null) {
|
|
176
|
+
this.stream.destroy();
|
|
177
|
+
this.stream = null;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
exports.default = SpeechFlowNodeMQTT;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import SpeechFlowNode from "./speechflow-node";
|
|
2
|
+
export default class SpeechFlowNodeWebsocket extends SpeechFlowNode {
|
|
3
|
+
static name: string;
|
|
4
|
+
private server;
|
|
5
|
+
private client;
|
|
6
|
+
constructor(id: string, cfg: {
|
|
7
|
+
[id: string]: any;
|
|
8
|
+
}, opts: {
|
|
9
|
+
[id: string]: any;
|
|
10
|
+
}, args: any[]);
|
|
11
|
+
open(): Promise<void>;
|
|
12
|
+
close(): Promise<void>;
|
|
13
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
** SpeechFlow - Speech Processing Flow Graph
|
|
4
|
+
** Copyright (c) 2024-2025 Dr. Ralf S. Engelschall <rse@engelschall.com>
|
|
5
|
+
** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
|
|
6
|
+
*/
|
|
7
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
8
|
+
if (k2 === undefined) k2 = k;
|
|
9
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
10
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
+
}
|
|
13
|
+
Object.defineProperty(o, k2, desc);
|
|
14
|
+
}) : (function(o, m, k, k2) {
|
|
15
|
+
if (k2 === undefined) k2 = k;
|
|
16
|
+
o[k2] = m[k];
|
|
17
|
+
}));
|
|
18
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
+
}) : function(o, v) {
|
|
21
|
+
o["default"] = v;
|
|
22
|
+
});
|
|
23
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
+
var ownKeys = function(o) {
|
|
25
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
+
var ar = [];
|
|
27
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
+
return ar;
|
|
29
|
+
};
|
|
30
|
+
return ownKeys(o);
|
|
31
|
+
};
|
|
32
|
+
return function (mod) {
|
|
33
|
+
if (mod && mod.__esModule) return mod;
|
|
34
|
+
var result = {};
|
|
35
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
+
__setModuleDefault(result, mod);
|
|
37
|
+
return result;
|
|
38
|
+
};
|
|
39
|
+
})();
|
|
40
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
41
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
42
|
+
};
|
|
43
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
44
|
+
/* standard dependencies */
|
|
45
|
+
const node_stream_1 = __importDefault(require("node:stream"));
|
|
46
|
+
/* external dependencies */
|
|
47
|
+
const ws_1 = __importDefault(require("ws"));
|
|
48
|
+
const reconnecting_websocket_1 = __importDefault(require("@opensumi/reconnecting-websocket"));
|
|
49
|
+
/* internal dependencies */
|
|
50
|
+
const speechflow_node_1 = __importDefault(require("./speechflow-node"));
|
|
51
|
+
const utils = __importStar(require("./speechflow-utils"));
|
|
52
|
+
/* SpeechFlow node for Websocket networking */
|
|
53
|
+
class SpeechFlowNodeWebsocket extends speechflow_node_1.default {
|
|
54
|
+
/* declare official node name */
|
|
55
|
+
static name = "websocket";
|
|
56
|
+
/* internal state */
|
|
57
|
+
server = null;
|
|
58
|
+
client = null;
|
|
59
|
+
/* construct node */
|
|
60
|
+
constructor(id, cfg, opts, args) {
|
|
61
|
+
super(id, cfg, opts, args);
|
|
62
|
+
/* declare node configuration parameters */
|
|
63
|
+
this.configure({
|
|
64
|
+
listen: { type: "string", val: "", match: /^(?:|ws:\/\/(.+?):(\d+))$/ },
|
|
65
|
+
connect: { type: "string", val: "", match: /^(?:|ws:\/\/(.+?):(\d+)(?:\/.*)?)$/ },
|
|
66
|
+
mode: { type: "string", val: "r", match: /^(?:r|w|rw)$/ },
|
|
67
|
+
type: { type: "string", val: "text", match: /^(?:audio|text)$/ }
|
|
68
|
+
});
|
|
69
|
+
/* sanity check usage */
|
|
70
|
+
if (this.params.listen !== "" && this.params.connect !== "")
|
|
71
|
+
throw new Error("Websocket node cannot listen and connect at the same time");
|
|
72
|
+
else if (this.params.listen === "" && this.params.connect === "")
|
|
73
|
+
throw new Error("Websocket node requires either listen or connect mode");
|
|
74
|
+
/* declare node input/output format */
|
|
75
|
+
if (this.params.mode === "rw") {
|
|
76
|
+
this.input = this.params.type;
|
|
77
|
+
this.output = this.params.type;
|
|
78
|
+
}
|
|
79
|
+
else if (this.params.mode === "r") {
|
|
80
|
+
this.input = "none";
|
|
81
|
+
this.output = this.params.type;
|
|
82
|
+
}
|
|
83
|
+
else if (this.params.mode === "w") {
|
|
84
|
+
this.input = this.params.type;
|
|
85
|
+
this.output = "none";
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/* open node */
|
|
89
|
+
async open() {
|
|
90
|
+
if (this.params.listen !== "") {
|
|
91
|
+
/* listen locally on a Websocket port */
|
|
92
|
+
const url = new URL(this.params.listen);
|
|
93
|
+
const websockets = new Set();
|
|
94
|
+
const chunkQueue = new utils.SingleQueue();
|
|
95
|
+
const server = new ws_1.default.WebSocketServer({
|
|
96
|
+
host: url.hostname,
|
|
97
|
+
port: Number.parseInt(url.port),
|
|
98
|
+
path: url.pathname
|
|
99
|
+
});
|
|
100
|
+
server.on("listening", () => {
|
|
101
|
+
this.log("info", `listening on URL ${this.params.listen}`);
|
|
102
|
+
});
|
|
103
|
+
server.on("connection", (ws, request) => {
|
|
104
|
+
const peer = `${request.socket.remoteAddress}:${request.socket.remotePort}`;
|
|
105
|
+
this.log("info", `connection opened on URL ${this.params.listen} by peer ${peer}`);
|
|
106
|
+
websockets.add(ws);
|
|
107
|
+
ws.on("close", () => {
|
|
108
|
+
this.log("info", `connection closed on URL ${this.params.listen} by peer ${peer}`);
|
|
109
|
+
websockets.delete(ws);
|
|
110
|
+
});
|
|
111
|
+
ws.on("error", (error) => {
|
|
112
|
+
this.log("error", `error of connection on URL ${this.params.listen} for peer ${peer}: ${error.message}`);
|
|
113
|
+
});
|
|
114
|
+
ws.on("message", (data, isBinary) => {
|
|
115
|
+
if (this.params.mode === "w") {
|
|
116
|
+
this.log("warning", `connection on URL ${this.params.listen} by peer ${peer}: ` +
|
|
117
|
+
"received remote data on write-only node");
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (!isBinary) {
|
|
121
|
+
this.log("warning", `connection on URL ${this.params.listen} by peer ${peer}: ` +
|
|
122
|
+
"received non-binary message");
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
let buffer;
|
|
126
|
+
if (Buffer.isBuffer(data))
|
|
127
|
+
buffer = data;
|
|
128
|
+
else if (data instanceof ArrayBuffer)
|
|
129
|
+
buffer = Buffer.from(data);
|
|
130
|
+
else
|
|
131
|
+
buffer = Buffer.concat(data);
|
|
132
|
+
const chunk = utils.streamChunkDecode(buffer);
|
|
133
|
+
chunkQueue.write(chunk);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
server.on("error", (error) => {
|
|
137
|
+
this.log("error", `error of some connection on URL ${this.params.listen}: ${error.message}`);
|
|
138
|
+
});
|
|
139
|
+
const type = this.params.type;
|
|
140
|
+
const mode = this.params.mode;
|
|
141
|
+
this.stream = new node_stream_1.default.Duplex({
|
|
142
|
+
writableObjectMode: true,
|
|
143
|
+
readableObjectMode: true,
|
|
144
|
+
decodeStrings: false,
|
|
145
|
+
write(chunk, encoding, callback) {
|
|
146
|
+
if (mode === "r")
|
|
147
|
+
callback(new Error("write operation on read-only node"));
|
|
148
|
+
else if (chunk.type !== type)
|
|
149
|
+
callback(new Error(`written chunk is not of ${type} type`));
|
|
150
|
+
else if (websockets.size === 0)
|
|
151
|
+
callback(new Error("still no Websocket connections available"));
|
|
152
|
+
else {
|
|
153
|
+
const data = utils.streamChunkEncode(chunk);
|
|
154
|
+
const results = [];
|
|
155
|
+
for (const websocket of websockets.values()) {
|
|
156
|
+
results.push(new Promise((resolve, reject) => {
|
|
157
|
+
websocket.send(data, (error) => {
|
|
158
|
+
if (error)
|
|
159
|
+
reject(error);
|
|
160
|
+
else
|
|
161
|
+
resolve();
|
|
162
|
+
});
|
|
163
|
+
}));
|
|
164
|
+
}
|
|
165
|
+
Promise.all(results).then(() => {
|
|
166
|
+
callback();
|
|
167
|
+
}).catch((errors) => {
|
|
168
|
+
const error = new Error(errors.map((e) => e.message).join("; "));
|
|
169
|
+
callback(error);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
read(size) {
|
|
174
|
+
if (mode === "w")
|
|
175
|
+
throw new Error("read operation on write-only node");
|
|
176
|
+
chunkQueue.read().then((chunk) => {
|
|
177
|
+
this.push(chunk, "binary");
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
else if (this.params.connect !== "") {
|
|
183
|
+
/* connect remotely to a Websocket port */
|
|
184
|
+
this.client = new reconnecting_websocket_1.default(this.params.connect, [], {
|
|
185
|
+
WebSocket: ws_1.default,
|
|
186
|
+
WebSocketOptions: {},
|
|
187
|
+
reconnectionDelayGrowFactor: 1.3,
|
|
188
|
+
maxReconnectionDelay: 4000,
|
|
189
|
+
minReconnectionDelay: 1000,
|
|
190
|
+
connectionTimeout: 4000,
|
|
191
|
+
minUptime: 5000
|
|
192
|
+
});
|
|
193
|
+
this.client.addEventListener("open", (ev) => {
|
|
194
|
+
this.log("info", `connection opened to URL ${this.params.connect}`);
|
|
195
|
+
});
|
|
196
|
+
this.client.addEventListener("close", (ev) => {
|
|
197
|
+
this.log("info", `connection closed to URL ${this.params.connect}`);
|
|
198
|
+
});
|
|
199
|
+
this.client.addEventListener("error", (ev) => {
|
|
200
|
+
this.log("error", `error of connection on URL ${this.params.connect}: ${ev.error.message}`);
|
|
201
|
+
});
|
|
202
|
+
const chunkQueue = new utils.SingleQueue();
|
|
203
|
+
this.client.addEventListener("message", (ev) => {
|
|
204
|
+
if (this.params.mode === "w") {
|
|
205
|
+
this.log("warning", `connection to URL ${this.params.listen}: ` +
|
|
206
|
+
"received remote data on write-only node");
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (!(ev.data instanceof ArrayBuffer)) {
|
|
210
|
+
this.log("warning", `connection to URL ${this.params.listen}: ` +
|
|
211
|
+
"received non-binary message");
|
|
212
|
+
return;
|
|
213
|
+
}
|
|
214
|
+
const buffer = Buffer.from(ev.data);
|
|
215
|
+
const chunk = utils.streamChunkDecode(buffer);
|
|
216
|
+
chunkQueue.write(chunk);
|
|
217
|
+
});
|
|
218
|
+
const client = this.client;
|
|
219
|
+
client.binaryType = "arraybuffer";
|
|
220
|
+
const type = this.params.type;
|
|
221
|
+
const mode = this.params.mode;
|
|
222
|
+
this.stream = new node_stream_1.default.Duplex({
|
|
223
|
+
writableObjectMode: true,
|
|
224
|
+
readableObjectMode: true,
|
|
225
|
+
decodeStrings: false,
|
|
226
|
+
write(chunk, encoding, callback) {
|
|
227
|
+
if (mode === "r")
|
|
228
|
+
callback(new Error("write operation on read-only node"));
|
|
229
|
+
else if (chunk.type !== type)
|
|
230
|
+
callback(new Error(`written chunk is not of ${type} type`));
|
|
231
|
+
else if (!client.OPEN)
|
|
232
|
+
callback(new Error("still no Websocket connection available"));
|
|
233
|
+
const data = utils.streamChunkEncode(chunk);
|
|
234
|
+
client.send(data);
|
|
235
|
+
callback();
|
|
236
|
+
},
|
|
237
|
+
read(size) {
|
|
238
|
+
if (mode === "w")
|
|
239
|
+
throw new Error("read operation on write-only node");
|
|
240
|
+
if (!client.OPEN)
|
|
241
|
+
throw new Error("still no Websocket connection available");
|
|
242
|
+
chunkQueue.read().then((chunk) => {
|
|
243
|
+
this.push(chunk, "binary");
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/* close node */
|
|
250
|
+
async close() {
|
|
251
|
+
/* close Websocket server */
|
|
252
|
+
if (this.server !== null) {
|
|
253
|
+
await new Promise((resolve, reject) => {
|
|
254
|
+
this.server.close((error) => {
|
|
255
|
+
if (error)
|
|
256
|
+
reject(error);
|
|
257
|
+
else
|
|
258
|
+
resolve();
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
this.server = null;
|
|
262
|
+
}
|
|
263
|
+
/* close Websocket client */
|
|
264
|
+
if (this.client !== null) {
|
|
265
|
+
this.client.close();
|
|
266
|
+
this.client = null;
|
|
267
|
+
}
|
|
268
|
+
/* close stream */
|
|
269
|
+
if (this.stream !== null) {
|
|
270
|
+
this.stream.destroy();
|
|
271
|
+
this.stream = null;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
exports.default = SpeechFlowNodeWebsocket;
|
package/dst/speechflow-node.d.ts
CHANGED
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
import Events from "node:events";
|
|
2
2
|
import Stream from "node:stream";
|
|
3
|
+
import { DateTime, Duration } from "luxon";
|
|
4
|
+
export declare class SpeechFlowChunk {
|
|
5
|
+
timestampStart: Duration;
|
|
6
|
+
timestampEnd: Duration;
|
|
7
|
+
kind: "intermediate" | "final";
|
|
8
|
+
type: "audio" | "text";
|
|
9
|
+
payload: Buffer | string;
|
|
10
|
+
constructor(timestampStart: Duration, timestampEnd: Duration, kind: "intermediate" | "final", type: "audio" | "text", payload: Buffer | string);
|
|
11
|
+
clone(): SpeechFlowChunk;
|
|
12
|
+
}
|
|
3
13
|
export default class SpeechFlowNode extends Events.EventEmitter {
|
|
4
14
|
id: string;
|
|
15
|
+
private cfg;
|
|
5
16
|
private opts;
|
|
6
17
|
private args;
|
|
7
18
|
config: {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
19
|
+
audioChannels: number;
|
|
20
|
+
audioBitDepth: (1 | 8 | 16 | 24 | 32);
|
|
21
|
+
audioLittleEndian: boolean;
|
|
22
|
+
audioSampleRate: number;
|
|
23
|
+
textEncoding: BufferEncoding;
|
|
24
|
+
cacheDir: string;
|
|
13
25
|
};
|
|
14
26
|
input: string;
|
|
15
27
|
output: string;
|
|
@@ -19,9 +31,15 @@ export default class SpeechFlowNode extends Events.EventEmitter {
|
|
|
19
31
|
stream: Stream.Writable | Stream.Readable | Stream.Duplex | null;
|
|
20
32
|
connectionsIn: Set<SpeechFlowNode>;
|
|
21
33
|
connectionsOut: Set<SpeechFlowNode>;
|
|
22
|
-
|
|
34
|
+
timeOpen: DateTime<boolean> | undefined;
|
|
35
|
+
timeZero: DateTime<boolean>;
|
|
36
|
+
timeZeroOffset: Duration<boolean>;
|
|
37
|
+
constructor(id: string, cfg: {
|
|
38
|
+
[id: string]: any;
|
|
39
|
+
}, opts: {
|
|
23
40
|
[id: string]: any;
|
|
24
41
|
}, args: any[]);
|
|
42
|
+
setTimeZero(time: DateTime): void;
|
|
25
43
|
configure(spec: {
|
|
26
44
|
[id: string]: {
|
|
27
45
|
type: string;
|
package/dst/speechflow-node.js
CHANGED
|
@@ -8,20 +8,48 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
8
8
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
9
9
|
};
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
|
+
exports.SpeechFlowChunk = void 0;
|
|
11
12
|
/* standard dependencies */
|
|
12
13
|
const node_events_1 = __importDefault(require("node:events"));
|
|
14
|
+
const luxon_1 = require("luxon");
|
|
15
|
+
/* the definition of a single payload chunk passed through the SpeechFlow nodes */
|
|
16
|
+
class SpeechFlowChunk {
|
|
17
|
+
timestampStart;
|
|
18
|
+
timestampEnd;
|
|
19
|
+
kind;
|
|
20
|
+
type;
|
|
21
|
+
payload;
|
|
22
|
+
constructor(timestampStart, timestampEnd, kind, type, payload) {
|
|
23
|
+
this.timestampStart = timestampStart;
|
|
24
|
+
this.timestampEnd = timestampEnd;
|
|
25
|
+
this.kind = kind;
|
|
26
|
+
this.type = type;
|
|
27
|
+
this.payload = payload;
|
|
28
|
+
}
|
|
29
|
+
clone() {
|
|
30
|
+
let payload;
|
|
31
|
+
if (Buffer.isBuffer(this.payload))
|
|
32
|
+
payload = Buffer.from(this.payload);
|
|
33
|
+
else
|
|
34
|
+
payload = String(this.payload);
|
|
35
|
+
return new SpeechFlowChunk(luxon_1.Duration.fromMillis(this.timestampStart.toMillis()), luxon_1.Duration.fromMillis(this.timestampEnd.toMillis()), this.kind, this.type, payload);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.SpeechFlowChunk = SpeechFlowChunk;
|
|
13
39
|
/* the base class for all SpeechFlow nodes */
|
|
14
40
|
class SpeechFlowNode extends node_events_1.default.EventEmitter {
|
|
15
41
|
id;
|
|
42
|
+
cfg;
|
|
16
43
|
opts;
|
|
17
44
|
args;
|
|
18
45
|
/* general constant configuration (for reference) */
|
|
19
46
|
config = {
|
|
20
|
-
audioChannels: 1, /* audio mono channel
|
|
21
|
-
audioBitDepth: 16, /* audio PCM 16-bit integer
|
|
22
|
-
audioLittleEndian: true, /* audio PCM little-endian
|
|
23
|
-
audioSampleRate: 48000, /* audio 48kHz sample rate
|
|
24
|
-
textEncoding: "utf8" /* UTF-8 text encoding
|
|
47
|
+
audioChannels: 1, /* audio mono channel */
|
|
48
|
+
audioBitDepth: 16, /* audio PCM 16-bit integer */
|
|
49
|
+
audioLittleEndian: true, /* audio PCM little-endian */
|
|
50
|
+
audioSampleRate: 48000, /* audio 48kHz sample rate */
|
|
51
|
+
textEncoding: "utf8", /* UTF-8 text encoding */
|
|
52
|
+
cacheDir: "" /* directory for cache files */
|
|
25
53
|
};
|
|
26
54
|
/* announced information */
|
|
27
55
|
input = "none";
|
|
@@ -30,12 +58,28 @@ class SpeechFlowNode extends node_events_1.default.EventEmitter {
|
|
|
30
58
|
stream = null;
|
|
31
59
|
connectionsIn = new Set();
|
|
32
60
|
connectionsOut = new Set();
|
|
61
|
+
timeOpen;
|
|
62
|
+
timeZero = luxon_1.DateTime.fromMillis(0);
|
|
63
|
+
timeZeroOffset = luxon_1.Duration.fromMillis(0);
|
|
33
64
|
/* the default constructor */
|
|
34
|
-
constructor(id, opts, args) {
|
|
65
|
+
constructor(id, cfg, opts, args) {
|
|
35
66
|
super();
|
|
36
67
|
this.id = id;
|
|
68
|
+
this.cfg = cfg;
|
|
37
69
|
this.opts = opts;
|
|
38
70
|
this.args = args;
|
|
71
|
+
for (const key of Object.keys(cfg)) {
|
|
72
|
+
const idx = key;
|
|
73
|
+
if (this.config[idx] !== undefined)
|
|
74
|
+
this.config[idx] = cfg[key];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/* set base/zero time for relative timestamp calculations */
|
|
78
|
+
setTimeZero(time) {
|
|
79
|
+
this.timeZero = time;
|
|
80
|
+
if (this.timeOpen === undefined)
|
|
81
|
+
this.timeOpen = this.timeZero;
|
|
82
|
+
this.timeZeroOffset = this.timeZero.diff(this.timeOpen);
|
|
39
83
|
}
|
|
40
84
|
/* INTERNAL: utility function: create "params" attribute from constructor of sub-classes */
|
|
41
85
|
configure(spec) {
|
|
@@ -79,6 +123,19 @@ class SpeechFlowNode extends node_events_1.default.EventEmitter {
|
|
|
79
123
|
else
|
|
80
124
|
throw new Error(`required parameter "${name}" not given`);
|
|
81
125
|
}
|
|
126
|
+
for (const name of Object.keys(this.opts)) {
|
|
127
|
+
if (spec[name] === undefined)
|
|
128
|
+
throw new Error(`named parameter "${name}" not known`);
|
|
129
|
+
}
|
|
130
|
+
for (let i = 0; i < this.args.length; i++) {
|
|
131
|
+
let found = false;
|
|
132
|
+
for (const name of Object.keys(spec))
|
|
133
|
+
if (spec[name].pos === i)
|
|
134
|
+
found = true;
|
|
135
|
+
if (!found)
|
|
136
|
+
throw new Error(`positional parameter #${i} ("${this.args[i]}") ` +
|
|
137
|
+
"not mappable to any known argument");
|
|
138
|
+
}
|
|
82
139
|
}
|
|
83
140
|
/* connect node to another one */
|
|
84
141
|
connect(other) {
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import Stream from "node:stream";
|
|
2
|
+
import { EventEmitter } from "node:events";
|
|
3
|
+
import { DateTime } from "luxon";
|
|
4
|
+
import { SpeechFlowChunk } from "./speechflow-node";
|
|
5
|
+
export declare function audioBufferDuration(buffer: Buffer, sampleRate?: number, bitDepth?: number, channels?: number, littleEndian?: boolean): number;
|
|
6
|
+
export declare function createTransformStreamForWritableSide(): Stream.Transform;
|
|
7
|
+
export declare function createTransformStreamForReadableSide(type: "text" | "audio", getTimeZero: () => DateTime): Stream.Transform;
|
|
8
|
+
export declare function ensureStreamChunk(type: "audio" | "text", chunk: SpeechFlowChunk | Buffer | string): string | SpeechFlowChunk | Buffer<ArrayBufferLike>;
|
|
9
|
+
export declare function streamChunkEncode(chunk: SpeechFlowChunk): Uint8Array<ArrayBufferLike>;
|
|
10
|
+
export declare function streamChunkDecode(_data: Uint8Array): SpeechFlowChunk;
|
|
11
|
+
export declare class SingleQueue<T> extends EventEmitter {
|
|
12
|
+
private queue;
|
|
13
|
+
write(item: T): void;
|
|
14
|
+
read(): Promise<T>;
|
|
15
|
+
}
|
|
16
|
+
export declare class DoubleQueue<T0, T1> extends EventEmitter {
|
|
17
|
+
private queue0;
|
|
18
|
+
private queue1;
|
|
19
|
+
private notify;
|
|
20
|
+
write0(item: T0): void;
|
|
21
|
+
write1(item: T1): void;
|
|
22
|
+
read(): Promise<[T0, T1]>;
|
|
23
|
+
}
|