npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

twilio-media-stream-save-audio-file

v0.0.4

Published

Simple helper class for Node.js environment for saving Twilio Media Stream audio directly from Twilio to a local WAV format file.

Downloads

240

Readme

NPM

Twilio Media Stream Save Audio File

This codebase provides a simple Node.js library class that makes it easy to save Twilio media streams to a local audio file in wav format.

Purpose

I was playing around with the Twilio Media Streams and wanted to easily save the phone call to a local wav file after the call completed. I did a quick Google search and found someone on StackOverflow (Inspired by user @tdeo: https://stackoverflow.com/questions/58439005/is-there-any-way-to-save-mulaw-audio-stream-from-twilio-in-a-file) who had provided code snippet for writing a Mulaw header for a WAV-file compatible with twilio format.

To make this a bit simpler I have taken this code snippet and converted it into a Node.js (CommonJS) class library for easier usage.

If you need to save a Twilio Media Stream phone call to a local audio file when the call completes then this library should help you.

Example Usage


const TwilioMediaStreamSaveAudioFile = require("twilio-media-stream-save-audio-file");

const mediaStreamSaver = new TwilioMediaStreamSaveAudioFile({
  saveLocation: __dirname,
  saveFilename: "my-twilio-media-stream-output",
  onSaved: () => console.log("File was saved!"),
});

wss.on("connection", (ws) => {
  console.log("New connection initiated!");

  ws.on("message", (message) => {
    const msg = JSON.parse(message);
    switch (msg.event) {
      case "connected":
        console.log("A new call has connected");
        break;
      case "start":
        console.log(`Starting media stream...`);
        mediaStreamSaver.twilioStreamStart();
        break;
      case "media":
        console.log("Receiving audio...");
        mediaStreamSaver.twilioStreamMedia(msg.media.payload);
        break;
      case "stop":
        console.log("Call has ended");
        mediaStreamSaver.twilioStreamStop();
        break;
      default:
        break;
    }
  });
});

Options

When you instantiate the library you can pass in the following options. They are not required and all optional.

  • saveLocation - (Optional) Defaults to the local dir using __dirname. You can set any path you wish. Make sure path exists.

  • saveFilename - (Optional) Defaults to the current date timestamp using Date.now(). You can set any filename you wish.

  • onSaved - (Optional) This is a optional callback function that you can provide if you want to be notified when the audio wav file has been saved.

Notes

  • Inside the connected websocket message event make sure to call each of the corresponding methods for the incoming Twilio Media Stream message events:

    Twilio Media Stream Message Event: start

    • mediaStreamSaver.twilioStreamStart()

    Twilio Media Stream Message Event: media

    • mediaStreamSaver.twilioStreamMedia(twilioMediaPayload)

    Twilio Media Stream Message Event: stop

    • mediaStreamSaver.twilioStreamStop()

For getting started on how Twilio media streams work, check out Twilio.

Author

👤 Jremi [email protected]

  • Website: http://stackoverflow.com/users/1062503/jremi
  • Github: @jremi

Made with ❤️ in San Diego