Skip to content

Commit 2b0a1e3

Browse files
Utilised new stop signal from Neuphonic API for indicating when all audio has been receieved. (#347)
1 parent cd4306b commit 2b0a1e3

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

plugins/neuphonic/src/tts.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,6 @@ export class SynthesizeStream extends tts.SynthesizeStream {
154154
async #run() {
155155
const requestId = randomUUID();
156156
let closing = false;
157-
const requestData = { [requestId]: { sent: '', recv: '' } };
158-
159-
const isAllAudioReceived = () => {
160-
const recvText = requestData[requestId]!.recv.toLowerCase()
161-
.replace(/[ \n]/g, '')
162-
.replace('<stop>', '');
163-
164-
const sentText = requestData[requestId]!.sent.toLowerCase()
165-
.replace(/[ \n]/g, '')
166-
.replace('<stop>', '');
167-
168-
return sentText === recvText;
169-
};
170157

171158
const sendTask = async (ws: WebSocket) => {
172159
for await (const data of this.input) {
@@ -175,7 +162,6 @@ export class SynthesizeStream extends tts.SynthesizeStream {
175162
continue;
176163
}
177164

178-
requestData[requestId]!.sent += data;
179165
ws.send(JSON.stringify({ text: data }));
180166
}
181167
};
@@ -196,25 +182,25 @@ export class SynthesizeStream extends tts.SynthesizeStream {
196182

197183
if (json?.data?.audio) {
198184
const audio = new Int8Array(Buffer.from(json.data.audio, 'base64'));
199-
requestData[requestId]!.recv += json.data?.text || '';
200185
for (const frame of bstream.write(audio)) {
201186
sendLastFrame(requestId, false);
202187
lastFrame = frame;
203188
// this.queue.put({frame, requestId, segmentId: requestId, final: false})
204189
}
205-
}
206190

207-
if (isAllAudioReceived()) {
208-
for (const frame of bstream.flush()) {
209-
sendLastFrame(requestId, false);
210-
lastFrame = frame;
211-
}
212-
sendLastFrame(requestId, true);
213-
this.queue.put(SynthesizeStream.END_OF_STREAM);
191+
if (json?.data?.stop) {
192+
// This is a bool flag, it is True when audio reaches "<STOP>"
193+
for (const frame of bstream.flush()) {
194+
sendLastFrame(requestId, false);
195+
lastFrame = frame;
196+
}
197+
sendLastFrame(requestId, true);
198+
this.queue.put(SynthesizeStream.END_OF_STREAM);
214199

215-
closing = true;
216-
ws.close();
217-
return;
200+
closing = true;
201+
ws.close();
202+
return;
203+
}
218204
}
219205
});
220206
ws.on('close', (code, reason) => {

0 commit comments

Comments
 (0)