Hello Community,
My use case is to generate STT transcript (+ translation) using cloud API (I am using Google Speech Recognise API) but it is not working.
I am able to handle open, close, pause and resume etc. JSON text message and also able to receive binary audio stream (PCMU format, 8K rate).
Please review following code and advice. I already took GPT help but no success.
if (isBinary) {
try {
let audioBuffer: Buffer;
if (Buffer.isBuffer(data)) {
audioBuffer = data;
} else if (data instanceof ArrayBuffer) {
audioBuffer = Buffer.from(data);
} else {
// Handle other possible types or throw an error
throw new Error('Unsupported binary data type received');
}
// Customer stream
if (!this.customerStream) {
this.customerStream = this.client.streamingRecognize({
config: {
encoding: 'LINEAR16',
sampleRateHertz: 8000,
languageCode: 'en-US',
enableAutomaticPunctuation: true,
},
interimResults: true,
})
.on('data', data => {
const transcript = data.results?.[0]?.alternatives?.[0]?.transcript;
if (transcript) console.log(`[CUSTOMER] ${transcript}`);
});
}
// Agent stream
if (!this.agentStream) {
this.agentStream = this.client.streamingRecognize({
config: {
encoding: 'LINEAR16',
sampleRateHertz: 8000,
languageCode: 'en-US',
enableAutomaticPunctuation: true,
},
interimResults: true,
})
.on('data', data => {
const transcript = data.results?.[0]?.alternatives?.[0]?.transcript;
if (transcript) console.log(`[AGENT] ${transcript}`);
});
}
// On binary audio
const { customer, agent } = this.splitStreamChunk(audioBuffer);
const customerPcm = this.convertMulawBufferToPCM16LE(customer as Buffer);
const agentPcm = this.convertMulawBufferToPCM16LE(agent as Buffer);
this.customerStream.write({ audio_content: customerPcm });
this.agentStream.write({ audio_content: agentPcm });
}
catch(err) {
console.error('Error processing binary message:', err);
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({
type: 'error',
message: 'Error processing audio data',
details: err instanceof Error ? err.message : String(err)
}));
}
}
}
#EmbeddableFramework#Integrations#PlatformAPI#PlatformCLI#PlatformSDK------------------------------
Parvez Alam
------------------------------