speechflow 1.6.0 → 1.6.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.
Files changed (182) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +1 -1
  3. package/package.json +2 -2
  4. package/speechflow-cli/dst/speechflow-main-api.d.ts +12 -0
  5. package/speechflow-cli/dst/speechflow-main-api.js +319 -0
  6. package/speechflow-cli/dst/speechflow-main-api.js.map +1 -0
  7. package/speechflow-cli/dst/speechflow-main-cli.d.ts +28 -0
  8. package/speechflow-cli/dst/speechflow-main-cli.js +271 -0
  9. package/speechflow-cli/dst/speechflow-main-cli.js.map +1 -0
  10. package/speechflow-cli/dst/speechflow-main-config.d.ts +9 -0
  11. package/speechflow-cli/dst/speechflow-main-config.js +27 -0
  12. package/speechflow-cli/dst/speechflow-main-config.js.map +1 -0
  13. package/speechflow-cli/dst/speechflow-main-graph.d.ts +34 -0
  14. package/speechflow-cli/dst/speechflow-main-graph.js +367 -0
  15. package/speechflow-cli/dst/speechflow-main-graph.js.map +1 -0
  16. package/speechflow-cli/dst/speechflow-main-nodes.d.ts +10 -0
  17. package/speechflow-cli/dst/speechflow-main-nodes.js +60 -0
  18. package/speechflow-cli/dst/speechflow-main-nodes.js.map +1 -0
  19. package/speechflow-cli/dst/speechflow-main-status.d.ts +11 -0
  20. package/speechflow-cli/dst/speechflow-main-status.js +60 -0
  21. package/speechflow-cli/dst/speechflow-main-status.js.map +1 -0
  22. package/speechflow-cli/dst/speechflow-main.d.ts +7 -0
  23. package/speechflow-cli/dst/speechflow-main.js +127 -0
  24. package/speechflow-cli/dst/speechflow-main.js.map +1 -0
  25. package/speechflow-cli/dst/speechflow-node-a2a-compressor-wt.js +4 -4
  26. package/speechflow-cli/dst/speechflow-node-a2a-compressor-wt.js.map +1 -1
  27. package/speechflow-cli/dst/speechflow-node-a2a-compressor.js +5 -6
  28. package/speechflow-cli/dst/speechflow-node-a2a-compressor.js.map +1 -1
  29. package/speechflow-cli/dst/speechflow-node-a2a-expander-wt.js +5 -5
  30. package/speechflow-cli/dst/speechflow-node-a2a-expander-wt.js.map +1 -1
  31. package/speechflow-cli/dst/speechflow-node-a2a-expander.js +5 -6
  32. package/speechflow-cli/dst/speechflow-node-a2a-expander.js.map +1 -1
  33. package/speechflow-cli/dst/speechflow-node-a2a-ffmpeg.js +5 -5
  34. package/speechflow-cli/dst/speechflow-node-a2a-ffmpeg.js.map +1 -1
  35. package/speechflow-cli/dst/speechflow-node-a2a-filler.js +3 -3
  36. package/speechflow-cli/dst/speechflow-node-a2a-filler.js.map +1 -1
  37. package/speechflow-cli/dst/speechflow-node-a2a-gain.js +2 -2
  38. package/speechflow-cli/dst/speechflow-node-a2a-gain.js.map +1 -1
  39. package/speechflow-cli/dst/speechflow-node-a2a-gender.js +4 -4
  40. package/speechflow-cli/dst/speechflow-node-a2a-gender.js.map +1 -1
  41. package/speechflow-cli/dst/speechflow-node-a2a-meter.js +2 -2
  42. package/speechflow-cli/dst/speechflow-node-a2a-meter.js.map +1 -1
  43. package/speechflow-cli/dst/speechflow-node-a2a-rnnoise.js +4 -4
  44. package/speechflow-cli/dst/speechflow-node-a2a-rnnoise.js.map +1 -1
  45. package/speechflow-cli/dst/speechflow-node-a2a-speex.js +4 -4
  46. package/speechflow-cli/dst/speechflow-node-a2a-speex.js.map +1 -1
  47. package/speechflow-cli/dst/speechflow-node-a2a-vad.js +4 -4
  48. package/speechflow-cli/dst/speechflow-node-a2a-vad.js.map +1 -1
  49. package/speechflow-cli/dst/speechflow-node-a2t-amazon.js +6 -6
  50. package/speechflow-cli/dst/speechflow-node-a2t-amazon.js.map +1 -1
  51. package/speechflow-cli/dst/speechflow-node-a2t-deepgram.js +4 -4
  52. package/speechflow-cli/dst/speechflow-node-a2t-deepgram.js.map +1 -1
  53. package/speechflow-cli/dst/speechflow-node-a2t-openai.js +4 -4
  54. package/speechflow-cli/dst/speechflow-node-a2t-openai.js.map +1 -1
  55. package/speechflow-cli/dst/speechflow-node-t2a-amazon.js +2 -2
  56. package/speechflow-cli/dst/speechflow-node-t2a-amazon.js.map +1 -1
  57. package/speechflow-cli/dst/speechflow-node-t2a-kokoro.js +2 -2
  58. package/speechflow-cli/dst/speechflow-node-t2a-kokoro.js.map +1 -1
  59. package/speechflow-cli/dst/speechflow-node-t2t-amazon.js +2 -2
  60. package/speechflow-cli/dst/speechflow-node-t2t-amazon.js.map +1 -1
  61. package/speechflow-cli/dst/speechflow-node-t2t-deepl.js +2 -2
  62. package/speechflow-cli/dst/speechflow-node-t2t-deepl.js.map +1 -1
  63. package/speechflow-cli/dst/speechflow-node-t2t-google.js +5 -5
  64. package/speechflow-cli/dst/speechflow-node-t2t-google.js.map +1 -1
  65. package/speechflow-cli/dst/speechflow-node-t2t-modify.js +2 -2
  66. package/speechflow-cli/dst/speechflow-node-t2t-modify.js.map +1 -1
  67. package/speechflow-cli/dst/speechflow-node-t2t-ollama.js +2 -2
  68. package/speechflow-cli/dst/speechflow-node-t2t-ollama.js.map +1 -1
  69. package/speechflow-cli/dst/speechflow-node-t2t-openai.js +2 -2
  70. package/speechflow-cli/dst/speechflow-node-t2t-openai.js.map +1 -1
  71. package/speechflow-cli/dst/speechflow-node-t2t-sentence.js +2 -2
  72. package/speechflow-cli/dst/speechflow-node-t2t-sentence.js.map +1 -1
  73. package/speechflow-cli/dst/speechflow-node-t2t-subtitle.js +2 -2
  74. package/speechflow-cli/dst/speechflow-node-t2t-subtitle.js.map +1 -1
  75. package/speechflow-cli/dst/speechflow-node-t2t-transformers.js +2 -2
  76. package/speechflow-cli/dst/speechflow-node-t2t-transformers.js.map +1 -1
  77. package/speechflow-cli/dst/speechflow-node-x2x-filter.js +2 -2
  78. package/speechflow-cli/dst/speechflow-node-x2x-filter.js.map +1 -1
  79. package/speechflow-cli/dst/speechflow-node-xio-device.js +5 -5
  80. package/speechflow-cli/dst/speechflow-node-xio-device.js.map +1 -1
  81. package/speechflow-cli/dst/speechflow-node-xio-file.js +27 -27
  82. package/speechflow-cli/dst/speechflow-node-xio-file.js.map +1 -1
  83. package/speechflow-cli/dst/speechflow-node-xio-mqtt.js +4 -4
  84. package/speechflow-cli/dst/speechflow-node-xio-mqtt.js.map +1 -1
  85. package/speechflow-cli/dst/speechflow-node-xio-websocket.js +7 -7
  86. package/speechflow-cli/dst/speechflow-node-xio-websocket.js.map +1 -1
  87. package/speechflow-cli/dst/{speechflow-utils-audio-wt.js → speechflow-util-audio-wt.js} +1 -1
  88. package/speechflow-cli/dst/speechflow-util-audio-wt.js.map +1 -0
  89. package/speechflow-cli/dst/speechflow-util-audio.d.ts +22 -0
  90. package/speechflow-cli/dst/speechflow-util-audio.js +251 -0
  91. package/speechflow-cli/dst/speechflow-util-audio.js.map +1 -0
  92. package/speechflow-cli/dst/speechflow-util-error.d.ts +14 -0
  93. package/speechflow-cli/dst/speechflow-util-error.js +131 -0
  94. package/speechflow-cli/dst/speechflow-util-error.js.map +1 -0
  95. package/speechflow-cli/dst/speechflow-util-queue.d.ts +68 -0
  96. package/speechflow-cli/dst/speechflow-util-queue.js +338 -0
  97. package/speechflow-cli/dst/speechflow-util-queue.js.map +1 -0
  98. package/speechflow-cli/dst/speechflow-util-stream.d.ts +18 -0
  99. package/speechflow-cli/dst/speechflow-util-stream.js +219 -0
  100. package/speechflow-cli/dst/speechflow-util-stream.js.map +1 -0
  101. package/speechflow-cli/dst/speechflow-util-webaudio-wt.js +124 -0
  102. package/speechflow-cli/dst/speechflow-util-webaudio-wt.js.map +1 -0
  103. package/speechflow-cli/dst/{speechflow-utils-audio.js → speechflow-util-webaudio.js} +2 -2
  104. package/speechflow-cli/dst/speechflow-util-webaudio.js.map +1 -0
  105. package/speechflow-cli/dst/speechflow-util.d.ts +4 -0
  106. package/speechflow-cli/dst/speechflow-util.js +26 -0
  107. package/speechflow-cli/dst/speechflow-util.js.map +1 -0
  108. package/speechflow-cli/dst/speechflow.js +3 -912
  109. package/speechflow-cli/dst/speechflow.js.map +1 -1
  110. package/speechflow-cli/etc/oxlint.jsonc +4 -1
  111. package/speechflow-cli/package.json +1 -0
  112. package/speechflow-cli/src/speechflow-main-api.ts +315 -0
  113. package/speechflow-cli/src/speechflow-main-cli.ts +259 -0
  114. package/speechflow-cli/src/speechflow-main-config.ts +17 -0
  115. package/speechflow-cli/src/speechflow-main-graph.ts +372 -0
  116. package/speechflow-cli/src/speechflow-main-nodes.ts +61 -0
  117. package/speechflow-cli/src/speechflow-main-status.ts +70 -0
  118. package/speechflow-cli/src/speechflow-main.ts +106 -0
  119. package/speechflow-cli/src/speechflow-node-a2a-compressor-wt.ts +4 -4
  120. package/speechflow-cli/src/speechflow-node-a2a-compressor.ts +5 -6
  121. package/speechflow-cli/src/speechflow-node-a2a-expander-wt.ts +5 -5
  122. package/speechflow-cli/src/speechflow-node-a2a-expander.ts +5 -6
  123. package/speechflow-cli/src/speechflow-node-a2a-ffmpeg.ts +5 -5
  124. package/speechflow-cli/src/speechflow-node-a2a-filler.ts +4 -4
  125. package/speechflow-cli/src/speechflow-node-a2a-gain.ts +2 -2
  126. package/speechflow-cli/src/speechflow-node-a2a-gender.ts +4 -4
  127. package/speechflow-cli/src/speechflow-node-a2a-meter.ts +2 -2
  128. package/speechflow-cli/src/speechflow-node-a2a-rnnoise.ts +4 -4
  129. package/speechflow-cli/src/speechflow-node-a2a-speex.ts +4 -4
  130. package/speechflow-cli/src/speechflow-node-a2a-vad.ts +4 -4
  131. package/speechflow-cli/src/speechflow-node-a2t-amazon.ts +7 -7
  132. package/speechflow-cli/src/speechflow-node-a2t-deepgram.ts +5 -5
  133. package/speechflow-cli/src/speechflow-node-a2t-openai.ts +5 -5
  134. package/speechflow-cli/src/speechflow-node-t2a-amazon.ts +2 -2
  135. package/speechflow-cli/src/speechflow-node-t2a-kokoro.ts +2 -2
  136. package/speechflow-cli/src/speechflow-node-t2t-amazon.ts +2 -2
  137. package/speechflow-cli/src/speechflow-node-t2t-deepl.ts +2 -2
  138. package/speechflow-cli/src/speechflow-node-t2t-google.ts +5 -5
  139. package/speechflow-cli/src/speechflow-node-t2t-modify.ts +2 -2
  140. package/speechflow-cli/src/speechflow-node-t2t-ollama.ts +2 -2
  141. package/speechflow-cli/src/speechflow-node-t2t-openai.ts +2 -2
  142. package/speechflow-cli/src/speechflow-node-t2t-sentence.ts +2 -2
  143. package/speechflow-cli/src/speechflow-node-t2t-subtitle.ts +2 -2
  144. package/speechflow-cli/src/speechflow-node-t2t-transformers.ts +2 -2
  145. package/speechflow-cli/src/speechflow-node-x2x-filter.ts +2 -2
  146. package/speechflow-cli/src/speechflow-node-xio-device.ts +5 -5
  147. package/speechflow-cli/src/speechflow-node-xio-file.ts +9 -10
  148. package/speechflow-cli/src/speechflow-node-xio-mqtt.ts +5 -5
  149. package/speechflow-cli/src/speechflow-node-xio-websocket.ts +7 -7
  150. package/speechflow-cli/src/{speechflow-utils-audio.ts → speechflow-util-audio.ts} +131 -1
  151. package/speechflow-cli/src/speechflow-util-error.ts +184 -0
  152. package/speechflow-cli/src/speechflow-util-queue.ts +320 -0
  153. package/speechflow-cli/src/speechflow-util-stream.ts +197 -0
  154. package/speechflow-cli/src/speechflow-util.ts +10 -0
  155. package/speechflow-cli/src/speechflow.ts +3 -953
  156. package/speechflow-cli/dst/speechflow-node-a2a-dynamics-wt.js +0 -208
  157. package/speechflow-cli/dst/speechflow-node-a2a-dynamics-wt.js.map +0 -1
  158. package/speechflow-cli/dst/speechflow-node-a2a-dynamics.d.ts +0 -15
  159. package/speechflow-cli/dst/speechflow-node-a2a-dynamics.js +0 -312
  160. package/speechflow-cli/dst/speechflow-node-a2a-dynamics.js.map +0 -1
  161. package/speechflow-cli/dst/speechflow-node-a2t-awstranscribe.d.ts +0 -18
  162. package/speechflow-cli/dst/speechflow-node-a2t-awstranscribe.js +0 -312
  163. package/speechflow-cli/dst/speechflow-node-a2t-awstranscribe.js.map +0 -1
  164. package/speechflow-cli/dst/speechflow-node-a2t-openaitranscribe.d.ts +0 -19
  165. package/speechflow-cli/dst/speechflow-node-a2t-openaitranscribe.js +0 -351
  166. package/speechflow-cli/dst/speechflow-node-a2t-openaitranscribe.js.map +0 -1
  167. package/speechflow-cli/dst/speechflow-node-t2a-awspolly.d.ts +0 -16
  168. package/speechflow-cli/dst/speechflow-node-t2a-awspolly.js +0 -204
  169. package/speechflow-cli/dst/speechflow-node-t2a-awspolly.js.map +0 -1
  170. package/speechflow-cli/dst/speechflow-node-t2t-awstranslate.d.ts +0 -13
  171. package/speechflow-cli/dst/speechflow-node-t2t-awstranslate.js +0 -175
  172. package/speechflow-cli/dst/speechflow-node-t2t-awstranslate.js.map +0 -1
  173. package/speechflow-cli/dst/speechflow-utils-audio-wt.js.map +0 -1
  174. package/speechflow-cli/dst/speechflow-utils-audio.js.map +0 -1
  175. package/speechflow-cli/dst/speechflow-utils.d.ts +0 -108
  176. package/speechflow-cli/dst/speechflow-utils.js +0 -746
  177. package/speechflow-cli/dst/speechflow-utils.js.map +0 -1
  178. package/speechflow-cli/src/speechflow-utils.ts +0 -810
  179. /package/speechflow-cli/dst/{speechflow-node-a2a-dynamics-wt.d.ts → speechflow-util-audio-wt.d.ts} +0 -0
  180. /package/speechflow-cli/dst/{speechflow-utils-audio-wt.d.ts → speechflow-util-webaudio-wt.d.ts} +0 -0
  181. /package/speechflow-cli/dst/{speechflow-utils-audio.d.ts → speechflow-util-webaudio.d.ts} +0 -0
  182. /package/speechflow-cli/src/{speechflow-utils-audio-wt.ts → speechflow-util-audio-wt.ts} +0 -0
@@ -1,810 +0,0 @@
1
- /*
2
- ** SpeechFlow - Speech Processing Flow Graph
3
- ** Copyright (c) 2024-2025 Dr. Ralf S. Engelschall <rse@engelschall.com>
4
- ** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only>
5
- */
6
-
7
- /* standard dependencies */
8
- import Stream from "node:stream"
9
- import { EventEmitter } from "node:events"
10
- import { type, type Type } from "arktype"
11
-
12
- /* external dependencies */
13
- import { DateTime, Duration } from "luxon"
14
- import * as CBOR from "cbor2"
15
- import * as IntervalTree from "node-interval-tree"
16
-
17
- /* internal dependencies */
18
- import { SpeechFlowChunk } from "./speechflow-node"
19
-
20
- /* helper function for retrieving an Error object */
21
- export function ensureError (error: unknown, prefix?: string): Error {
22
- if (error instanceof Error && prefix === undefined)
23
- return error
24
- let msg = error instanceof Error ?
25
- error.message : String(error)
26
- if (prefix)
27
- msg = `${prefix}: ${msg}`
28
- return new Error(msg, { cause: error })
29
- }
30
-
31
- /* helper function for retrieving a Promise object */
32
- export function ensurePromise<T> (arg: T | Promise<T>): Promise<T> {
33
- if (!(arg instanceof Promise))
34
- arg = Promise.resolve(arg)
35
- return arg
36
- }
37
-
38
- /* helper function for running the finally code of "run" */
39
- function runFinally (onfinally?: () => void) {
40
- if (!onfinally)
41
- return
42
- try { onfinally() }
43
- catch (_error: unknown) { /* ignored */ }
44
- }
45
-
46
- /* helper type for ensuring T contains no Promise */
47
- type runNoPromise<T> =
48
- [ T ] extends [ Promise<any> ] ? never : T
49
-
50
- /* run a synchronous or asynchronous action */
51
- export function run<T, X extends runNoPromise<T> | never> (
52
- action: () => X,
53
- oncatch?: (error: Error) => X | never,
54
- onfinally?: () => void
55
- ): X
56
- export function run<T, X extends runNoPromise<T> | never> (
57
- description: string,
58
- action: () => X,
59
- oncatch?: (error: Error) => X | never,
60
- onfinally?: () => void
61
- ): X
62
- export function run<T, X extends (T | Promise<T>)> (
63
- action: () => X,
64
- oncatch?: (error: Error) => X,
65
- onfinally?: () => void
66
- ): Promise<T>
67
- export function run<T, X extends (T | Promise<T>)> (
68
- description: string,
69
- action: () => X,
70
- oncatch?: (error: Error) => X,
71
- onfinally?: () => void
72
- ): Promise<T>
73
- export function run<T> (
74
- ...args: any[]
75
- ): T | Promise<T> | never {
76
- /* support overloaded signatures */
77
- let description: string | undefined
78
- let action: () => T | Promise<T> | never
79
- let oncatch: (error: Error) => T | Promise<T> | never
80
- let onfinally: () => void
81
- if (typeof args[0] === "string") {
82
- description = args[0]
83
- action = args[1]
84
- oncatch = args[2]
85
- onfinally = args[3]
86
- }
87
- else {
88
- action = args[0]
89
- oncatch = args[1]
90
- onfinally = args[2]
91
- }
92
-
93
- /* perform the action */
94
- let result: T | Promise<T>
95
- try {
96
- result = action()
97
- }
98
- catch (arg: unknown) {
99
- /* synchronous case (error branch) */
100
- let error = ensureError(arg, description)
101
- if (oncatch) {
102
- try {
103
- result = oncatch(error)
104
- }
105
- catch (arg: unknown) {
106
- error = ensureError(arg, description)
107
- runFinally(onfinally)
108
- throw error
109
- }
110
- runFinally(onfinally)
111
- return result
112
- }
113
- runFinally(onfinally)
114
- throw error
115
- }
116
- if (result instanceof Promise) {
117
- /* asynchronous case (result or error branch) */
118
- return result.catch((arg: unknown) => {
119
- /* asynchronous case (error branch) */
120
- let error = ensureError(arg, description)
121
- if (oncatch) {
122
- try {
123
- return oncatch(error)
124
- }
125
- catch (arg: unknown) {
126
- error = ensureError(arg, description)
127
- throw error
128
- }
129
- }
130
- throw error
131
- }).finally(() => {
132
- /* asynchronous case (result and error branch) */
133
- runFinally(onfinally)
134
- })
135
- }
136
- else {
137
- /* synchronous case (result branch) */
138
- runFinally(onfinally)
139
- return result
140
- }
141
- }
142
-
143
- /* run a synchronous or asynchronous action */
144
- /* eslint @typescript-eslint/unified-signatures: off */
145
- export function runner<T, X extends runNoPromise<T> | never, F extends (...args: any[]) => X> (
146
- action: F,
147
- oncatch?: (error: Error) => X | never,
148
- onfinally?: () => void
149
- ): F
150
- export function runner<T, X extends runNoPromise<T> | never, F extends (...args: any[]) => X> (
151
- description: string,
152
- action: F,
153
- oncatch?: (error: Error) => X | never,
154
- onfinally?: () => void
155
- ): F
156
- export function runner<T, X extends (T | Promise<T>), F extends (...args: any[]) => Promise<T>> (
157
- action: F,
158
- oncatch?: (error: Error) => X,
159
- onfinally?: () => void
160
- ): F
161
- export function runner<T, X extends (T | Promise<T>), F extends (...args: any[]) => Promise<T>> (
162
- description: string,
163
- action: F,
164
- oncatch?: (error: Error) => X,
165
- onfinally?: () => void
166
- ): F
167
- export function runner<T> (
168
- ...args: any[]
169
- ): (...args: any[]) => T | Promise<T> | never {
170
- /* support overloaded signatures */
171
- let description: string | undefined
172
- let action: (...args: any[]) => T | Promise<T> | never
173
- let oncatch: (error: Error) => T | Promise<T> | never
174
- let onfinally: () => void
175
- if (typeof args[0] === "string") {
176
- description = args[0]
177
- action = args[1]
178
- oncatch = args[2]
179
- onfinally = args[3]
180
- }
181
- else {
182
- action = args[0]
183
- oncatch = args[1]
184
- onfinally = args[2]
185
- }
186
-
187
- /* wrap the "run" operation on "action" into function
188
- which exposes the signature of "action" */
189
- return (...args: any[]) => {
190
- if (description)
191
- return run(description, () => action(...args), oncatch, onfinally)
192
- else
193
- return run(() => action(...args), oncatch, onfinally)
194
- }
195
- }
196
-
197
- /* import an object with parsing and strict error handling */
198
- export function importObject<T>(name: string, arg: object | string, validator: Type<T, {}>): T {
199
- const obj: object = typeof arg === "string" ?
200
- run(`${name}: parsing JSON`, () => JSON.parse(arg)) :
201
- arg
202
- const result = validator(obj)
203
- if (result instanceof type.errors)
204
- throw new Error(`${name}: validation: ${result.summary}`)
205
- return result as T
206
- }
207
-
208
- /* calculate duration of an audio buffer */
209
- export function audioBufferDuration (
210
- buffer: Buffer,
211
- sampleRate = 48000,
212
- bitDepth = 16,
213
- channels = 1,
214
- littleEndian = true
215
- ) {
216
- /* sanity check parameters */
217
- if (!Buffer.isBuffer(buffer))
218
- throw new Error("invalid input (Buffer expected)")
219
- if (littleEndian !== true)
220
- throw new Error("only Little Endian supported")
221
- if (sampleRate <= 0)
222
- throw new Error("sample rate must be positive")
223
- if (bitDepth <= 0 || bitDepth % 8 !== 0)
224
- throw new Error("bit depth must be positive and multiple of 8")
225
- if (channels <= 0)
226
- throw new Error("channels must be positive")
227
-
228
- /* calculate duration */
229
- const bytesPerSample = bitDepth / 8
230
- const totalSamples = buffer.length / (bytesPerSample * channels)
231
- return totalSamples / sampleRate
232
- }
233
-
234
- /* calculate duration of an audio array */
235
- export function audioArrayDuration (
236
- arr: Float32Array,
237
- sampleRate = 48000,
238
- channels = 1
239
- ) {
240
- /* sanity check parameters */
241
- if (arr.length === 0)
242
- return 0
243
- if (sampleRate <= 0)
244
- throw new Error("sample rate must be positive")
245
- if (channels <= 0)
246
- throw new Error("channels must be positive")
247
-
248
- /* calculate duration */
249
- const totalSamples = arr.length / channels
250
- return totalSamples / sampleRate
251
- }
252
-
253
- /* helper function: convert Buffer in PCM/I16 to Float32Array in PCM/F32 format */
254
- export function convertBufToF32 (buf: Buffer, littleEndian = true) {
255
- if (buf.length % 2 !== 0)
256
- throw new Error("buffer length must be even for 16-bit samples")
257
- const dataView = new DataView(buf.buffer)
258
- const arr = new Float32Array(buf.length / 2)
259
- for (let i = 0; i < arr.length; i++)
260
- arr[i] = dataView.getInt16(i * 2, littleEndian) / 32768
261
- return arr
262
- }
263
-
264
- /* helper function: convert Float32Array in PCM/F32 to Buffer in PCM/I16 format */
265
- export function convertF32ToBuf (arr: Float32Array) {
266
- if (arr.length === 0)
267
- return Buffer.alloc(0)
268
- const int16Array = new Int16Array(arr.length)
269
- for (let i = 0; i < arr.length; i++) {
270
- let sample = arr[i]
271
- if (Number.isNaN(sample))
272
- sample = 0
273
- int16Array[i] = Math.max(-32768, Math.min(32767, Math.round(sample * 32768)))
274
- }
275
- return Buffer.from(int16Array.buffer)
276
- }
277
-
278
- /* helper function: convert Buffer in PCM/I16 to Int16Array */
279
- export function convertBufToI16 (buf: Buffer, littleEndian = true) {
280
- if (buf.length % 2 !== 0)
281
- throw new Error("buffer length must be even for 16-bit samples")
282
- const dataView = new DataView(buf.buffer, buf.byteOffset, buf.byteLength)
283
- const arr = new Int16Array(buf.length / 2)
284
- for (let i = 0; i < buf.length / 2; i++)
285
- arr[i] = dataView.getInt16(i * 2, littleEndian)
286
- return arr
287
- }
288
-
289
- /* helper function: convert In16Array in PCM/I16 to Buffer */
290
- export function convertI16ToBuf (arr: Int16Array, littleEndian = true) {
291
- if (arr.length === 0)
292
- return Buffer.alloc(0)
293
- const buf = Buffer.allocUnsafe(arr.length * 2)
294
- for (let i = 0; i < arr.length; i++) {
295
- if (littleEndian)
296
- buf.writeInt16LE(arr[i], i * 2)
297
- else
298
- buf.writeInt16BE(arr[i], i * 2)
299
- }
300
- return buf
301
- }
302
-
303
- /* create a Duplex/Transform stream which has
304
- object-mode on Writable side and buffer/string-mode on Readable side */
305
- export function createTransformStreamForWritableSide () {
306
- return new Stream.Transform({
307
- readableObjectMode: true,
308
- writableObjectMode: true,
309
- decodeStrings: false,
310
- highWaterMark: 1,
311
- transform (chunk: SpeechFlowChunk, encoding, callback) {
312
- this.push(chunk.payload)
313
- callback()
314
- },
315
- final (callback) {
316
- this.push(null)
317
- callback()
318
- }
319
- })
320
- }
321
-
322
- /* create a Duplex/Transform stream which has
323
- object-mode on Readable side and buffer/string-mode on Writable side */
324
- export function createTransformStreamForReadableSide (type: "text" | "audio", getTimeZero: () => DateTime) {
325
- return new Stream.Transform({
326
- readableObjectMode: true,
327
- writableObjectMode: true,
328
- decodeStrings: false,
329
- highWaterMark: (type === "audio" ? 19200 : 65536),
330
- transform (chunk: Buffer | string, encoding, callback) {
331
- if (chunk === null) {
332
- this.push(null)
333
- callback()
334
- return
335
- }
336
- const timeZero = getTimeZero()
337
- const start = DateTime.now().diff(timeZero)
338
- let end = start
339
- if (type === "audio") {
340
- const duration = audioBufferDuration(chunk as Buffer)
341
- end = start.plus(duration * 1000)
342
- }
343
- const payload = ensureStreamChunk(type, chunk) as Buffer | string
344
- const obj = new SpeechFlowChunk(start, end, "final", type, payload)
345
- this.push(obj)
346
- callback()
347
- },
348
- final (callback) {
349
- this.push(null)
350
- callback()
351
- }
352
- })
353
- }
354
-
355
- /* ensure a chunk is of a certain type and format */
356
- export function ensureStreamChunk (type: "audio" | "text", chunk: SpeechFlowChunk | Buffer | string) {
357
- if (chunk instanceof SpeechFlowChunk) {
358
- if (chunk.type !== type)
359
- throw new Error(`invalid payload chunk (expected ${type} type, received ${chunk.type} type)`)
360
- }
361
- else {
362
- if (type === "text" && Buffer.isBuffer(chunk))
363
- chunk = chunk.toString("utf8")
364
- else if (type === "audio" && !Buffer.isBuffer(chunk))
365
- chunk = Buffer.from(chunk)
366
- }
367
- return chunk
368
- }
369
-
370
- /* type of a serialized SpeechFlow chunk */
371
- type SpeechFlowChunkSerialized = {
372
- timestampStart: number,
373
- timestampEnd: number,
374
- kind: string,
375
- type: string,
376
- payload: Uint8Array
377
- }
378
-
379
- /* encode/serialize chunk of data */
380
- export function streamChunkEncode (chunk: SpeechFlowChunk) {
381
- let payload: Uint8Array
382
- if (Buffer.isBuffer(chunk.payload))
383
- payload = new Uint8Array(chunk.payload)
384
- else {
385
- const encoder = new TextEncoder()
386
- payload = encoder.encode(chunk.payload)
387
- }
388
- const data = {
389
- timestampStart: chunk.timestampStart.toMillis(),
390
- timestampEnd: chunk.timestampEnd.toMillis(),
391
- kind: chunk.kind,
392
- type: chunk.type,
393
- payload
394
- } satisfies SpeechFlowChunkSerialized
395
- const _data = CBOR.encode(data)
396
- return _data
397
- }
398
-
399
- /* decode/unserialize chunk of data */
400
- export function streamChunkDecode (_data: Uint8Array) {
401
- let data: SpeechFlowChunkSerialized
402
- try {
403
- data = CBOR.decode<SpeechFlowChunkSerialized>(_data)
404
- }
405
- catch (err: any) {
406
- throw new Error(`CBOR decoding failed: ${err}`)
407
- }
408
- let payload: Buffer | string
409
- if (data.type === "audio")
410
- payload = Buffer.from(data.payload)
411
- else
412
- payload = (new TextDecoder()).decode(data.payload)
413
- const chunk = new SpeechFlowChunk(
414
- Duration.fromMillis(data.timestampStart),
415
- Duration.fromMillis(data.timestampEnd),
416
- data.kind as "intermediate" | "final",
417
- data.type as "audio" | "text",
418
- payload
419
- )
420
- return chunk
421
- }
422
-
423
- /* helper class for single item queue */
424
- export class SingleQueue<T> extends EventEmitter {
425
- private queue = new Array<T>()
426
- write (item: T) {
427
- this.queue.unshift(item)
428
- this.emit("dequeue")
429
- }
430
- read () {
431
- return new Promise<T>((resolve, reject) => {
432
- const consume = () =>
433
- this.queue.length > 0 ? this.queue.pop()! : null
434
- const tryToConsume = () => {
435
- const item = consume()
436
- if (item !== null)
437
- resolve(item)
438
- else
439
- this.once("dequeue", tryToConsume)
440
- }
441
- tryToConsume()
442
- })
443
- }
444
- }
445
-
446
- /* helper class for double-item queue */
447
- export class DoubleQueue<T0, T1> extends EventEmitter {
448
- private queue0 = new Array<T0>()
449
- private queue1 = new Array<T1>()
450
- private notify () {
451
- if (this.queue0.length > 0 && this.queue1.length > 0)
452
- this.emit("dequeue")
453
- }
454
- write0 (item: T0) {
455
- this.queue0.unshift(item)
456
- this.notify()
457
- }
458
- write1 (item: T1) {
459
- this.queue1.unshift(item)
460
- this.notify()
461
- }
462
- read () {
463
- return new Promise<[ T0, T1 ]>((resolve, reject) => {
464
- const consume = (): [ T0, T1 ] | null => {
465
- if (this.queue0.length > 0 && this.queue1.length > 0) {
466
- const item0 = this.queue0.pop() as T0
467
- const item1 = this.queue1.pop() as T1
468
- return [ item0, item1 ]
469
- }
470
- return null
471
- }
472
- const tryToConsume = () => {
473
- const items = consume()
474
- if (items !== null)
475
- resolve(items)
476
- else
477
- this.once("dequeue", tryToConsume)
478
- }
479
- tryToConsume()
480
- })
481
- }
482
- }
483
-
484
- /* queue element */
485
- export type QueueElement = { type: string }
486
-
487
- /* queue pointer */
488
- export class QueuePointer<T extends QueueElement> extends EventEmitter {
489
- /* internal state */
490
- private index = 0
491
-
492
- /* construction */
493
- constructor (
494
- private name: string,
495
- private queue: Queue<T>
496
- ) {
497
- super()
498
- this.setMaxListeners(100)
499
- }
500
-
501
- /* positioning operations */
502
- maxPosition () {
503
- return this.queue.elements.length
504
- }
505
- position (index?: number): number {
506
- if (index !== undefined) {
507
- this.index = Math.max(0, Math.min(index, this.queue.elements.length))
508
- this.emit("position", this.index)
509
- }
510
- return this.index
511
- }
512
- walk (num: number) {
513
- const indexOld = this.index
514
- if (num > 0)
515
- this.index = Math.min(this.index + num, this.queue.elements.length)
516
- else if (num < 0)
517
- this.index = Math.max(this.index + num, 0)
518
- if (this.index !== indexOld)
519
- this.emit("position", { start: this.index })
520
- }
521
- walkForwardUntil (type: T["type"]) {
522
- while (this.index < this.queue.elements.length
523
- && this.queue.elements[this.index].type !== type)
524
- this.index++
525
- this.emit("position", { start: this.index })
526
- }
527
- walkBackwardUntil (type: T["type"]) {
528
- while (this.index > 0
529
- && this.queue.elements[this.index].type !== type)
530
- this.index--
531
- this.emit("position", { start: this.index })
532
- }
533
-
534
- /* search operations */
535
- searchForward (type: T["type"]) {
536
- let position = this.index
537
- while (position < this.queue.elements.length
538
- && this.queue.elements[position].type !== type)
539
- position++
540
- this.emit("search", { start: this.index, end: position })
541
- return position
542
- }
543
- searchBackward (type: T["type"]) {
544
- let position = this.index
545
- while (position > 0
546
- && this.queue.elements[position].type !== type)
547
- position--
548
- this.emit("search", { start: position, end: this.index })
549
- return position
550
- }
551
-
552
- /* reading operations */
553
- peek (position?: number) {
554
- if (position === undefined)
555
- position = this.index
556
- position = Math.max(0, Math.min(position, this.queue.elements.length))
557
- const element = this.queue.elements[position]
558
- this.queue.emit("read", { start: position, end: position })
559
- return element
560
- }
561
- read () {
562
- const element = this.queue.elements[this.index]
563
- if (this.index < this.queue.elements.length)
564
- this.index++
565
- this.queue.emit("read", { start: this.index - 1, end: this.index - 1 })
566
- return element
567
- }
568
- slice (size?: number) {
569
- let slice: T[]
570
- const start = this.index
571
- if (size !== undefined) {
572
- size = Math.max(0, Math.min(size, this.queue.elements.length - this.index))
573
- slice = this.queue.elements.slice(this.index, this.index + size)
574
- this.index += size
575
- }
576
- else {
577
- slice = this.queue.elements.slice(this.index)
578
- this.index = this.queue.elements.length
579
- }
580
- this.queue.emit("read", { start, end: this.index })
581
- return slice
582
- }
583
-
584
- /* writing operations */
585
- touch () {
586
- if (this.index >= this.queue.elements.length)
587
- throw new Error("cannot touch after last element")
588
- this.queue.emit("write", { start: this.index, end: this.index + 1 })
589
- }
590
- append (element: T) {
591
- this.queue.elements.push(element)
592
- this.index = this.queue.elements.length
593
- this.queue.emit("write", { start: this.index - 1, end: this.index - 1 })
594
- }
595
- insert (element: T) {
596
- this.queue.elements.splice(this.index, 0, element)
597
- this.queue.emit("write", { start: this.index - 1, end: this.index })
598
- }
599
- delete () {
600
- if (this.index >= this.queue.elements.length)
601
- throw new Error("cannot delete after last element")
602
- this.queue.elements.splice(this.index, 1)
603
- this.queue.emit("write", { start: this.index, end: this.index })
604
- }
605
- }
606
-
607
- /* queue */
608
- export class Queue<T extends QueueElement> extends EventEmitter {
609
- public elements: T[] = []
610
- private pointers = new Map<string, QueuePointer<T>>()
611
- constructor () {
612
- super()
613
- this.setMaxListeners(100)
614
- }
615
- pointerUse (name: string): QueuePointer<T> {
616
- if (!this.pointers.has(name))
617
- this.pointers.set(name, new QueuePointer<T>(name, this))
618
- return this.pointers.get(name)!
619
- }
620
- pointerDelete (name: string): void {
621
- if (!this.pointers.has(name))
622
- throw new Error("pointer not exists")
623
- this.pointers.delete(name)
624
- }
625
- trim (): void {
626
- /* determine minimum pointer position */
627
- let min = this.elements.length
628
- for (const pointer of this.pointers.values())
629
- if (min > pointer.position())
630
- min = pointer.position()
631
-
632
- /* trim the maximum amount of first elements */
633
- if (min > 0) {
634
- this.elements.splice(0, min)
635
-
636
- /* shift all pointers */
637
- for (const pointer of this.pointers.values())
638
- pointer.position(pointer.position() - min)
639
- }
640
- }
641
- }
642
-
643
- /* utility class for wrapping a custom stream into a regular Transform stream */
644
- export class StreamWrapper extends Stream.Transform {
645
- private foreignStream: any
646
- private onData = (chunk: any) => { this.push(chunk) }
647
- private onError = (err: Error) => { this.emit("error", err) }
648
- private onEnd = () => { this.push(null) }
649
- constructor (foreignStream: any, options: Stream.TransformOptions = {}) {
650
- options.readableObjectMode = true
651
- options.writableObjectMode = true
652
- super(options)
653
- this.foreignStream = foreignStream
654
- if (typeof this.foreignStream.on === "function") {
655
- this.foreignStream.on("data", this.onData)
656
- this.foreignStream.on("error", this.onError)
657
- this.foreignStream.on("end", this.onEnd)
658
- }
659
- }
660
- _transform (chunk: any, encoding: BufferEncoding, callback: Stream.TransformCallback): void {
661
- if (this.destroyed) {
662
- callback(new Error("stream already destroyed"))
663
- return
664
- }
665
- try {
666
- if (typeof this.foreignStream.write === "function") {
667
- const canContinue = this.foreignStream.write(chunk)
668
- if (canContinue)
669
- callback()
670
- else
671
- this.foreignStream.once("drain", callback)
672
- }
673
- else
674
- throw new Error("foreign stream lacks write method")
675
- }
676
- catch (err) {
677
- callback(err as Error)
678
- }
679
- }
680
- _flush (callback: Stream.TransformCallback): void {
681
- if (this.destroyed) {
682
- callback(new Error("stream already destroyed"))
683
- return
684
- }
685
- try {
686
- if (typeof this.foreignStream.end === "function")
687
- this.foreignStream.end()
688
- callback()
689
- }
690
- catch (err) {
691
- callback(err as Error)
692
- }
693
- }
694
- _destroy (error: Error | null, callback: Stream.TransformCallback): void {
695
- if (typeof this.foreignStream.removeListener === "function") {
696
- this.foreignStream.removeListener("data", this.onData)
697
- this.foreignStream.removeListener("error", this.onError)
698
- this.foreignStream.removeListener("end", this.onEnd)
699
- }
700
- super._destroy(error, callback)
701
- }
702
- }
703
-
704
- /* meta store */
705
- interface TimeStoreInterval<T> extends IntervalTree.Interval {
706
- item: T
707
- }
708
- export class TimeStore<T> extends EventEmitter {
709
- private tree = new IntervalTree.IntervalTree<TimeStoreInterval<T>>()
710
- store (start: Duration, end: Duration, item: T): void {
711
- this.tree.insert({ low: start.toMillis(), high: end.toMillis(), item })
712
- }
713
- fetch (start: Duration, end: Duration): T[] {
714
- const intervals = this.tree.search(start.toMillis(), end.toMillis())
715
- return intervals.map((interval) => interval.item)
716
- }
717
- prune (_before: Duration): void {
718
- const before = _before.toMillis()
719
- const intervals = this.tree.search(0, before - 1)
720
- for (const interval of intervals)
721
- if (interval.low < before && interval.high < before)
722
- this.tree.remove(interval)
723
- }
724
- clear (): void {
725
- this.tree = new IntervalTree.IntervalTree<TimeStoreInterval<T>>()
726
- }
727
- }
728
-
729
- /* asynchronous queue */
730
- export class AsyncQueue<T> {
731
- private queue: Array<T | null> = []
732
- private resolvers: ((v: T | null) => void)[] = []
733
- write (v: T | null) {
734
- const resolve = this.resolvers.shift()
735
- if (resolve)
736
- resolve(v)
737
- else
738
- this.queue.push(v)
739
- }
740
- async read () {
741
- if (this.queue.length > 0)
742
- return this.queue.shift()!
743
- else
744
- return new Promise<T | null>((resolve) => this.resolvers.push(resolve))
745
- }
746
- destroy () {
747
- for (const resolve of this.resolvers)
748
- resolve(null)
749
- this.resolvers = []
750
- this.queue = []
751
- }
752
- }
753
-
754
- /* process Int16Array in fixed-size segments */
755
- export async function processInt16ArrayInSegments (
756
- data: Int16Array<ArrayBuffer>,
757
- segmentSize: number,
758
- processor: (segment: Int16Array<ArrayBuffer>) => Promise<Int16Array<ArrayBuffer>>
759
- ): Promise<Int16Array<ArrayBuffer>> {
760
- /* process full segments */
761
- let i = 0
762
- while ((i + segmentSize) <= data.length) {
763
- const segment = data.slice(i, i + segmentSize)
764
- const result = await processor(segment)
765
- data.set(result, i)
766
- i += segmentSize
767
- }
768
-
769
- /* process final partial segment if it exists */
770
- if (i < data.length) {
771
- const len = data.length - i
772
- const segment = new Int16Array(segmentSize)
773
- segment.set(data.slice(i), 0)
774
- segment.fill(0, len, segmentSize)
775
- const result = await processor(segment)
776
- data.set(result.slice(0, len), i)
777
- }
778
- return data
779
- }
780
-
781
- /* cached regular expression class */
782
- export class CachedRegExp {
783
- private cache = new Map<string, RegExp>()
784
- compile (pattern: string): RegExp | null {
785
- if (this.cache.has(pattern))
786
- return this.cache.get(pattern)!
787
- try {
788
- const regex = new RegExp(pattern)
789
- this.cache.set(pattern, regex)
790
- return regex
791
- }
792
- catch (_error) {
793
- return null
794
- }
795
- }
796
- clear (): void {
797
- this.cache.clear()
798
- }
799
- size (): number {
800
- return this.cache.size
801
- }
802
- }
803
-
804
- /* helper functions for linear/decibel conversions */
805
- export function lin2dB (x: number): number {
806
- return 20 * Math.log10(Math.max(x, 1e-12))
807
- }
808
- export function dB2lin (db: number): number {
809
- return Math.pow(10, db / 20)
810
- }