File size: 7,457 Bytes
68465df | 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | import {BgTimer} from "@mentra/engine"
export class TranscriptProcessor {
private maxCharsPerLine: number = 44
private maxLines: number = 3
private maxFinalTranscripts: number = 30
private isChinese: boolean = false
private lastUserTranscript: string = ""
private lines: string[] = []
private partialText: string = ""
private finalTranscriptHistory: string[] = []
private currentDisplayLines: string[] = []
private lastPartialUpdateTime: number = 0
private readonly throttleInterval: number = 300 // 300ms throttle interval
private pendingUpdate: string | null = null
private pendingTimer: number | null = null
private sendPendingCallback: (() => void) | null = null
constructor(sendPendingCallback?: () => void) {
this.sendPendingCallback = sendPendingCallback || null
}
public processString(newText: string | null, isFinal: boolean): string | null {
const text = (newText || "").trim()
if (!isFinal) {
// Store this as the current partial text (overwriting old partial)
this.partialText = text
this.lastUserTranscript = text
// Combine final history with new partial text
const combinedText = this.getCombinedTranscriptHistory() + " " + text
this.currentDisplayLines = this.wrapText(combinedText, this.maxCharsPerLine)
// Ensure we have exactly maxLines
while (this.currentDisplayLines.length < this.maxLines) {
this.currentDisplayLines.push("")
}
while (this.currentDisplayLines.length > this.maxLines) {
this.currentDisplayLines.shift()
}
const currentTime = Date.now()
const timeSinceLastUpdate = currentTime - this.lastPartialUpdateTime
const processedText = this.currentDisplayLines.join("\n")
// If less than throttle interval has passed, queue the update
if (timeSinceLastUpdate < this.throttleInterval) {
// Store the latest update (overwrites any pending update)
this.pendingUpdate = processedText
// If no timer is running, schedule one
if (!this.pendingTimer) {
this.pendingTimer = BgTimer.setTimeout(() => {
// Trigger sending the most recent pending update
this.lastPartialUpdateTime = Date.now()
this.pendingTimer = null
// Notify that we have a pending update to send
if (this.sendPendingCallback && this.pendingUpdate) {
this.sendPendingCallback()
}
}, this.throttleInterval - timeSinceLastUpdate)
}
return null // Don't send now, will send via timer
}
// Enough time has passed, send immediately
this.lastPartialUpdateTime = currentTime
// Clear any pending update since we're sending now
if (this.pendingTimer) {
BgTimer.clearTimeout(this.pendingTimer)
this.pendingTimer = null
}
this.pendingUpdate = null
return processedText
} else {
// We have a final text -> clear out the partial text to avoid duplication
this.partialText = ""
// Add to transcript history when it's a final transcript
this.addToTranscriptHistory(text)
// Use the same wrapping logic as partial to maintain consistency
const combinedText = this.getCombinedTranscriptHistory()
this.currentDisplayLines = this.wrapText(combinedText, this.maxCharsPerLine)
// Ensure we have exactly maxLines
while (this.currentDisplayLines.length < this.maxLines) {
this.currentDisplayLines.push("")
}
while (this.currentDisplayLines.length > this.maxLines) {
this.currentDisplayLines.shift()
}
return this.currentDisplayLines.join("\n")
}
}
private wrapText(text: string, maxLength: number): string[] {
const result: string[] = []
let remainingText = text
while (remainingText.length > 0) {
if (remainingText.length <= maxLength) {
result.push(remainingText)
break
}
let splitIndex = maxLength
if (this.isChinese) {
// For Chinese text, just split at maxLength
splitIndex = maxLength
} else {
// For non-Chinese text, find the last space before maxLength
const searchSpace = remainingText.substring(0, maxLength)
const lastSpaceIndex = searchSpace.lastIndexOf(" ")
if (lastSpaceIndex > 0) {
splitIndex = lastSpaceIndex
} else {
// If no space found, force split at maxLength
splitIndex = maxLength
}
}
const chunk = remainingText.substring(0, splitIndex).trim()
if (chunk) {
result.push(chunk)
}
remainingText = remainingText.substring(splitIndex).trim()
}
return result
}
private addToTranscriptHistory(transcript: string): void {
const trimmed = transcript.trim()
if (!trimmed) return // Don't add empty transcripts
this.finalTranscriptHistory.push(trimmed)
// Ensure we don't exceed maxFinalTranscripts
while (this.finalTranscriptHistory.length > this.maxFinalTranscripts) {
this.finalTranscriptHistory.shift() // Remove oldest transcript
}
}
// Get the transcript history
public getFinalTranscriptHistory(): string[] {
return [...this.finalTranscriptHistory] // Return a copy to prevent external modification
}
// Get combined transcript history as a single string
public getCombinedTranscriptHistory(): string {
return this.finalTranscriptHistory.join(" ")
}
// Method to set max final transcripts
public setMaxFinalTranscripts(maxFinalTranscripts: number): void {
this.maxFinalTranscripts = maxFinalTranscripts
// Trim history if needed after changing the limit
while (this.finalTranscriptHistory.length > this.maxFinalTranscripts) {
this.finalTranscriptHistory.shift()
}
}
// Get max final transcripts
public getMaxFinalTranscripts(): number {
return this.maxFinalTranscripts
}
public getLastUserTranscript(): string {
return this.lastUserTranscript
}
public clear(): void {
this.lines = []
this.partialText = ""
this.finalTranscriptHistory = []
this.currentDisplayLines = []
this.lastPartialUpdateTime = 0
if (this.pendingTimer) {
BgTimer.clearTimeout(this.pendingTimer)
this.pendingTimer = null
}
this.pendingUpdate = null
}
public getPendingUpdate(): string | null {
const pending = this.pendingUpdate
if (pending) {
this.pendingUpdate = null
}
return pending
}
public getMaxCharsPerLine(): number {
return this.maxCharsPerLine
}
public getMaxLines(): number {
return this.maxLines
}
/**
* Change the language mode for text processing
* @param isChinese - Set to true for Chinese text, false for non-Chinese
* @param reset - If true, clears the current display and history
*/
public changeLanguage(langauge: string): void {
const isChinese = langauge === "zh-CN"
if (this.isChinese !== isChinese) {
this.isChinese = isChinese
if (isChinese) {
this.maxCharsPerLine = 18
} else {
this.maxCharsPerLine = 44
}
this.clear()
}
}
}
export default TranscriptProcessor
|