-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspeechflow-node.ts
More file actions
203 lines (182 loc) · 8.15 KB
/
speechflow-node.ts
File metadata and controls
203 lines (182 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
** SpeechFlow - Speech Processing Flow Graph
** Copyright (c) 2024-2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
*/
/* standard dependencies */
import Events, { EventEmitter } from "node:events"
import Stream from "node:stream"
/* external dependencies */
import { DateTime, Duration } from "luxon"
/* internal dependencies */
import { deepClone } from "./speechflow-util-misc"
/* the definition of a single payload chunk passed through the SpeechFlow nodes */
export class SpeechFlowChunk {
constructor (
public timestampStart: Duration,
public timestampEnd: Duration,
public kind: "intermediate" | "final",
public type: "audio" | "text",
public payload: Buffer | string,
public meta = new Map<string, any>()
) {}
clone () {
let payload: Buffer | string
if (Buffer.isBuffer(this.payload))
payload = Buffer.from(this.payload)
else
payload = String(this.payload)
return new SpeechFlowChunk(
Duration.fromMillis(this.timestampStart.toMillis()),
Duration.fromMillis(this.timestampEnd.toMillis()),
this.kind,
this.type,
payload,
deepClone(this.meta)
)
}
}
/* the base class for all SpeechFlow nodes */
export default class SpeechFlowNode extends Events.EventEmitter {
public static name: string | undefined
/* general constant configuration (for reference) */
config = {
audioChannels: 1, /* audio mono channel */
audioBitDepth: 16 as (1 | 8 | 16 | 24 | 32), /* audio PCM 16-bit integer */
audioLittleEndian: true, /* audio PCM little-endian */
audioSampleRate: 48000, /* audio 48kHz sample rate */
textEncoding: "utf8" as BufferEncoding, /* UTF-8 text encoding */
cacheDir: "" /* directory for cache files */
}
/* announced information */
input = "none"
output = "none"
params: { [ id: string ]: any } = {}
stream: Stream.Writable | Stream.Readable | Stream.Duplex | null = null
connectionsIn = new Set<SpeechFlowNode>()
connectionsOut = new Set<SpeechFlowNode>()
timeOpen: DateTime<boolean> | undefined
timeZero: DateTime<boolean> = DateTime.fromMillis(0)
timeZeroOffset: Duration<boolean> = Duration.fromMillis(0)
_accessBus: ((name: string) => EventEmitter) | null = null
/* the default constructor */
constructor (
public id: string,
private cfg: { [ id: string ]: any },
private opts: { [ id: string ]: any },
private args: any[]
) {
super()
for (const key of Object.keys(cfg)) {
const idx = key as keyof typeof this.config
if (this.config[idx] !== undefined)
Reflect.set(this.config, idx, cfg[key])
}
}
/* set base/zero time for relative timestamp calculations */
setTimeZero (time: DateTime) {
this.timeZero = time
if (this.timeOpen === undefined)
this.timeOpen = this.timeZero
this.timeZeroOffset = this.timeZero.diff(this.timeOpen)
}
/* receive external request */
async receiveRequest (args: any[]): Promise<void> {
/* no-op */
}
/* send external response */
sendResponse (args: any[]): void {
this.emit("send-response", args)
}
/* receive dashboard information */
async receiveDashboard (type: "audio" | "text", id: string, kind: "final" | "intermediate", value: number | string): Promise<void> {
/* no-op */
}
/* send dashboard information */
sendDashboard (type: "audio", id: string, kind: "final" | "intermediate", value: number): void
sendDashboard (type: "text", id: string, kind: "final" | "intermediate", value: string): void
sendDashboard (type: "audio" | "text", id: string, kind: "final" | "intermediate", value: number | string): void {
this.emit("send-dashboard", { type, id, kind, value })
}
/* access communication bus */
accessBus (name: string): EventEmitter {
if (this._accessBus === null)
throw new Error("access to communication bus still not possible")
return this._accessBus(name)
}
/* INTERNAL: utility function: create "params" attribute from constructor of sub-classes */
configure (spec: { [ id: string ]: { type: string, pos?: number, val?: any, match?: RegExp | ((x: any) => boolean) } }) {
for (const name of Object.keys(spec)) {
if (this.opts[name] !== undefined) {
/* named parameter */
if (typeof this.opts[name] !== spec[name].type)
throw new Error(`invalid type of named parameter "${name}" ` +
`(has to be ${spec[name].type})`)
if ("match" in spec[name]
&& ( ( spec[name].match instanceof RegExp
&& this.opts[name].match(spec[name].match) === null)
|| ( typeof spec[name].match === "function"
&& !spec[name].match(this.opts[name]) ) ))
throw new Error(`invalid value "${this.opts[name]}" of named parameter "${name}"`)
this.params[name] = this.opts[name]
}
else if (this.opts[name] === undefined
&& "pos" in spec[name]
&& typeof spec[name].pos === "number"
&& spec[name].pos < this.args.length) {
/* positional argument */
if (typeof this.args[spec[name].pos] !== spec[name].type)
throw new Error(`invalid type of positional parameter "${name}" ` +
`(has to be ${spec[name].type})`)
if ("match" in spec[name]
&& ( ( spec[name].match instanceof RegExp
&& this.args[spec[name].pos].match(spec[name].match) === null)
|| ( typeof spec[name].match === "function"
&& !spec[name].match(this.args[spec[name].pos]) ) ))
throw new Error(`invalid value "${this.args[spec[name].pos!]}" of positional parameter "${name}"`)
this.params[name] = this.args[spec[name].pos]
}
else if ("val" in spec[name] && spec[name].val !== undefined)
/* default argument */
this.params[name] = spec[name].val
else
throw new Error(`required parameter "${name}" not given`)
}
for (const name of Object.keys(this.opts)) {
if (spec[name] === undefined)
throw new Error(`named parameter "${name}" not known`)
}
for (let i = 0; i < this.args.length; i++) {
let found = false
for (const name of Object.keys(spec))
if (spec[name].pos === i)
found = true
if (!found)
throw new Error(`positional parameter #${i} ("${this.args[i]}") ` +
"not mappable to any known argument")
}
}
/* connect node to another one */
connect (other: SpeechFlowNode) {
this.connectionsOut.add(other)
other.connectionsIn.add(this)
}
/* disconnect node from another one */
disconnect (other: SpeechFlowNode) {
if (!this.connectionsOut.has(other))
throw new Error("invalid node: not connected to this node")
this.connectionsOut.delete(other)
other.connectionsIn.delete(this)
}
/* internal log function */
log (level: string, msg: string, data?: any) {
this.emit("log", level, msg, data)
}
/* default implementation for status operation */
async status (): Promise<{ [ key: string ]: string | number }> {
return {}
}
/* default implementation for open/close operations */
async open () {}
async close () {}
}