Closed DecathectZero closed 1 month ago
maybe 👍
if (typeof event.data === "string") {
try {
const data = JSON.parse(event.data);
this.handleTextMessage(data);
}
catch (error) {
this.emit(LiveTTSEvents.Error, {
event,
message: "Unable to parse `data` as JSON.",
error,
});
}
}
else if (event.data instanceof ArrayBuffer) {
this.handleBinaryMessage(event.data);
}
else if (Buffer.isBuffer(event.data)) {
this.handleBinaryMessage(event.data.buffer);
} else if (event.data instanceof Blob) {
const reader = new FileReader();
reader.onload = () => {
this.handleBinaryMessage(reader.result);
};
reader.readAsArrayBuffer(event.data);
}
else {
console.log("Received unknown data type", event.data);
this.emit(LiveTTSEvents.Error, {
event,
message: "Received unknown data type.",
});
}
}
Looks like you can do the following to work around the bug for now handling the error event by checking if event data is a Blob and then converting the blob to a buffer and calling handleBinaryMessage as below:
this.deepgramText2Speech.on(LiveTTSEvents.Error, (error) => {
const event = error.event;
// XXX: deepgramText2Speech client has a bug so at last as of version 3.7.0 we need to hack the handleMessage method
if (event.data instanceof Blob) {
console.log("read the blob", event.data);
event.data.arrayBuffer().then(arrayBuffer => {
const buffer = Buffer.from(arrayBuffer);
this.deepgramText2Speech.handleBinaryMessage(buffer);
}).catch(error => {
console.error("Failed to read blob", error);
});
} else {
console.error(new Date(), "[deepgramText2Speech]: error:", error);
}
});
Looks like you can do the following to work around the bug for now handling the error event by checking if event data is a Blob and then converting the blob to a buffer and calling handleBinaryMessage as below:
this.deepgramText2Speech.on(LiveTTSEvents.Error, (error) => { const event = error.event; // XXX: deepgramText2Speech client has a bug so at last as of version 3.7.0 we need to hack the handleMessage method if (event.data instanceof Blob) { console.log("read the blob", event.data); event.data.arrayBuffer().then(arrayBuffer => { const buffer = Buffer.from(arrayBuffer); this.deepgramText2Speech.handleBinaryMessage(buffer); }).catch(error => { console.error("Failed to read blob", error); }); } else { console.error(new Date(), "[deepgramText2Speech]: error:", error); } });
this isn't on LiveTTSEvents.Error
, but rather on LiveTTSEvents.Audio
@DecathectZero correct, but the library internally does not handle the type correctly... so until there is a new published npm package using error and adding the fixed logic to detect blob resolves the issue...
This doesn't work on my end: https://developers.deepgram.com/docs/streaming-text-to-speech
when we get a websocket message, it will call the
handleMessage
function. https://github.com/deepgram/deepgram-js-sdk/blob/main/src/packages/SpeakLiveClient.ts#L139-L162When
event.data
itself is aBuffer
,buffer.buffer
, which is aArrayBufferLike
- however this looses some crucial metadata in the buffer itself:BYTES_PER_ELEMENT
,byteLength
,byteOffset
. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8ArrayWe shouldn't be using
ArrayBuffer
but justBuffer
instead in the event.Perhaps it works for MP3 or some other files, but it definitely fails with
linear16
.To reproduce:
This is just to generate the file, nothing wrong with
createWavHeader
- because I've also tried this with vonage's streaming API: https://developer.vonage.com/en/voice/voice-api/concepts/websockets#writing-audio-to-the-websocket and with Twilio's https://www.twilio.com/docs/voice/media-streams/websocket-messages#send-websocket-messages-to-twilioIf I don't use the SDK and use my own custom WS client, it works fine. Basically instead of:
it should be: