UNPKG

speechflow

Version:

Speech Processing Flow Graph

82 lines (70 loc) 2.51 kB
/* ** SpeechFlow - Speech Processing Flow Graph ** Copyright (c) 2024-2025 Dr. Ralf S. Engelschall <rse@engelschall.com> ** Licensed under GPL 3.0 <https://spdx.org/licenses/GPL-3.0-only> */ /* standard dependencies */ import Stream from "node:stream" /* external dependencies */ import wrapText from "wrap-text" /* internal dependencies */ import SpeechFlowNode, { SpeechFlowChunk } from "./speechflow-node" /* SpeechFlow node for text-to-text formatting */ export default class SpeechFlowNodeT2TFormat extends SpeechFlowNode { /* declare official node name */ public static name = "t2t-format" /* construct node */ constructor (id: string, cfg: { [ id: string ]: any }, opts: { [ id: string ]: any }, args: any[]) { super(id, cfg, opts, args) /* declare node configuration parameters */ this.configure({ width: { type: "number", val: 80 } }) /* declare node input/output format */ this.input = "text" this.output = "text" } /* open node */ async open () { /* provide text-to-text formatter */ const format = (text: string) => { text = wrapText(text, this.params.width) text = text.replace(/([^\n])$/, "$1\n") return text } /* establish a duplex stream and connect it to text formatting */ this.stream = new Stream.Transform({ readableObjectMode: true, writableObjectMode: true, decodeStrings: false, highWaterMark: 1, transform (chunk: SpeechFlowChunk, encoding, callback) { if (Buffer.isBuffer(chunk.payload)) callback(new Error("invalid chunk payload type")) else if (chunk.payload === "") { this.push(chunk) callback() } else { const payload = format(chunk.payload) const chunkNew = chunk.clone() chunkNew.payload = payload this.push(chunkNew) callback() } }, final (callback) { this.push(null) callback() } }) } /* close node */ async close () { /* close stream */ if (this.stream !== null) { this.stream.destroy() this.stream = null } } }