|
| 1 | +// SPDX-FileCopyrightText: 2024 LiveKit, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import { TTS, SynthesisEvent, SynthesisEventType, SynthesizedAudio, SynthesizeStream } from './tts'; |
| 6 | +import { SentenceStream, SentenceTokenizer } from '../tokenize'; |
| 7 | + |
| 8 | +export class StreamAdapterWrapper extends SynthesizeStream { |
| 9 | + closed: boolean; |
| 10 | + tts: TTS; |
| 11 | + sentenceStream: SentenceStream; |
| 12 | + eventQueue: (SynthesisEvent | undefined)[]; |
| 13 | + task: { |
| 14 | + run: Promise<void>; |
| 15 | + cancel: () => void; |
| 16 | + }; |
| 17 | + |
| 18 | + constructor(tts: TTS, sentenceStream: SentenceStream) { |
| 19 | + super(); |
| 20 | + this.closed = false; |
| 21 | + this.tts = tts; |
| 22 | + this.sentenceStream = sentenceStream; |
| 23 | + this.eventQueue = []; |
| 24 | + this.task = { |
| 25 | + run: new Promise((_, reject) => { |
| 26 | + this.run(reject); |
| 27 | + }), |
| 28 | + cancel: () => {}, |
| 29 | + }; |
| 30 | + } |
| 31 | + |
| 32 | + async run(reject: (arg: Error) => void) { |
| 33 | + while (!this.closed) { |
| 34 | + this.task.cancel = () => { |
| 35 | + this.closed = true; |
| 36 | + reject(new Error('cancelled')); |
| 37 | + }; |
| 38 | + for await (const sentence of this.sentenceStream) { |
| 39 | + const audio = await this.tts.synthesize(sentence.text); |
| 40 | + this.eventQueue.push(new SynthesisEvent(SynthesisEventType.STARTED)); |
| 41 | + this.eventQueue.push(new SynthesisEvent(SynthesisEventType.AUDIO, audio)); |
| 42 | + this.eventQueue.push(new SynthesisEvent(SynthesisEventType.FINISHED)); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + pushText(token: string) { |
| 48 | + this.sentenceStream.pushText(token); |
| 49 | + } |
| 50 | + |
| 51 | + async flush() { |
| 52 | + await this.sentenceStream.flush(); |
| 53 | + } |
| 54 | + |
| 55 | + next(): IteratorResult<SynthesisEvent> { |
| 56 | + const event = this.eventQueue.shift(); |
| 57 | + if (event) { |
| 58 | + return { done: false, value: event }; |
| 59 | + } else { |
| 60 | + return { done: true, value: undefined }; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + async close(): Promise<void> { |
| 65 | + this.task.cancel(); |
| 66 | + try { |
| 67 | + await this.task.run; |
| 68 | + } finally { |
| 69 | + this.eventQueue.push(undefined); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +export class StreamAdapter extends TTS { |
| 75 | + tts: TTS; |
| 76 | + tokenizer: SentenceTokenizer; |
| 77 | + |
| 78 | + constructor(tts: TTS, tokenizer: SentenceTokenizer) { |
| 79 | + super(true); |
| 80 | + this.tts = tts; |
| 81 | + this.tokenizer = tokenizer; |
| 82 | + } |
| 83 | + |
| 84 | + synthesize(text: string): Promise<SynthesizedAudio> { |
| 85 | + return this.tts.synthesize(text); |
| 86 | + } |
| 87 | + |
| 88 | + stream() { |
| 89 | + return new StreamAdapterWrapper(this.tts, this.tokenizer.stream(undefined)); |
| 90 | + } |
| 91 | +} |
0 commit comments