Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to get audio file from twilio media stream #126

Open
mazaharulhq opened this issue Oct 11, 2021 · 0 comments
Open

Need to get audio file from twilio media stream #126

mazaharulhq opened this issue Oct 11, 2021 · 0 comments

Comments

@mazaharulhq
Copy link

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!")

    const params = {
      //objectMode: true,
     //contentType: 'audio/mulaw',
      rate:'8000',
      contentType: 'audio/wav',
      languageCustomizationId: '**** Dummy *****',
     // keywords: ['colorado', 'tornado', 'tornadoes'],
      grammarName:'name-grammar',
     // keywordsThreshold: 0.5,
     // maxAlternatives: 3,
    
    };

    const recognizeStream = speechToText.recognizeUsingWebSocket(params);

   // Pipe in the audio.
   fs.createReadStream('C:\\TwilioWavFiles\\my-twilio-media-stream-output.wav').pipe(recognizeStream);

 

   recognizeStream.pipe(fs.createWriteStream('transcription.txt'));

   recognizeStream.setEncoding('utf8');

   // Listen for events.

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) {

console.log("New connection initiated!");
mediaStreamSaver.setWebsocket(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":

    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;
}

});
});

//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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant