You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ws.on("message", function incoming(message) {
const msg = JSON.parse(message);
// console.log("msg :: "+msg);
// console.log("message :: "+message);
switch (msg.event) {
case "connected":
console.log(A new call has connected.);
mediaStreamSaver.twilioStreamStart();
break;
case "start":
console.log(Starting Media Stream ${msg.streamSid});
break;
case "media":
let decoded = base64decode(msg.media.payload);
console.log("decoded string is :: "+decoded);
mediaStreamSaver.twilioStreamMedia(msg.media.payload);
break;
case "stop":
console.log(`Call Has Ended`);
mediaStreamSaver.twilioStreamStop();
break;
}
I am trying to get audio file from twilio media stream and later need to transcribe that audio from IBM watson. I followed https://stackoverflow.com/questions/58439005/is-there-any-way-to-save-mulaw-audio-stream-from-twilio-in-a-file thread and following npm package https://www.npmjs.com/package/twilio-media-stream-save-audio-file and was able to get wav file successfully but IBM watson complains about wrong file format.
Below is the code I used.
const WebSocket = require("ws");
const express = require("express");
const app = express();
const server = require("http").createServer(app);
const wss = new WebSocket.Server({ server });
const fs = require('fs');
const SpeechToTextV1 = require('ibm-watson/speech-to-text/v1');
const { base64encode, base64decode } = require('nodejs-base64');
const { IamAuthenticator } = require('ibm-watson/auth');
const TwilioMediaStreamSaveAudioFile = require("twilio-media-stream-save-audio-file");
const speechToText = new SpeechToTextV1({
authenticator: new IamAuthenticator({
apikey: 'Dummy*',
}),
serviceUrl: 'https://api.us-east.speech-to-text.watson.cloud.ibm.com/instances/****Dummy*****/v1/recognize/',
});
const mediaStreamSaver = new TwilioMediaStreamSaveAudioFile({
saveLocation: "C:\TwilioWavFiles\",
saveFilename: "my-twilio-media-stream-output",
onSaved: () =>
{
console.log("File was saved!")
recognizeStream.on('data', function(event)
{
onEvent('Data:', event);
});
recognizeStream.on('error', function(event)
{
onEvent('Error:', event);
});
recognizeStream.on('close', function(event)
{
onEvent('Close:', event);
});
// Display events on the console.
function onEvent(name, event) {
console.log(name, JSON.stringify(event, null, 2));
};
}
});
// Handle Web Socket Connection
wss.on("connection", function connection(ws) {
ws.on("message", function incoming(message) {
const msg = JSON.parse(message);
// console.log("msg :: "+msg);
// console.log("message :: "+message);
switch (msg.event) {
case "connected":
console.log(
A new call has connected.
);mediaStreamSaver.twilioStreamStart();
break;
case "start":
console.log(
Starting Media Stream ${msg.streamSid}
);break;
case "media":
});
});
//Handle HTTP Request
app.get("/", (req, res) => res.send("Hello World"));
app.post("/twiml", (req, res) => {
res.set("Content-Type", "text/xml");
res.send(
<Response> <Start> <Stream url="wss://${req.headers.host} " /> </Start> <Say>Say your name </Say> <Pause length="5" /> </Response>
);});
console.log("Listening at Port 8080");
server.listen(8080);
The text was updated successfully, but these errors were encountered: