Code Monkey home page Code Monkey logo

troubadour's Introduction

Troubadour

Build Status NPM

Troubadour is wrapper over command-line audio players that makes it easy to play audio in Node.js. At this point, the following audio players are supported:

  • mpg123
  • MPlayer
  • SoX
  • VLC

Other audio players can easily be added by adding entries into the supportedAudioPlayers.json file.

Installation

npm install troubadour

Since Troubadour uses command-line audio players to facilitate audio playback, you will also need to install one of the supported players.

Usage

Getting Started

To get started, require the module in your program and create a new Troubadour instance by passing the name of the supported audio player into the constructor.

const Troubadour = require('troubadour');
const troubadour = new Troubadour('sox');

Troubadour is an event emitter that indicates when certain actions are being performed by the audio player. The following code snippet shows how to add an event listener to your new troubadour player.

troubadour.on('eventname', () => {
  // Event listener function
});

You do not need to have listeners set up in order to use Troubadour but they provide a programmatic way of defining actions that you would like to take when the audio playback starts, pauses, stops, resumes, ends, and errors.

Play

To play an audio source, use the play function and pass in the file path of the audio source that is to be played:

troubadour.on('start', () => {
  // Do something here when the audio starts playing
});

troubadour.on('end', () => {
  // Do something here when the audio finishes playing
});

troubadour.play('~/Music/audiofile.mp3');

Pause

Pause the playback (with the ability to resume later) by using the pause function:

troubadour.on('pause', () => {
  // Do something here when the audio is paused
});

troubadour.pause();

Stop

Stop the playback (without the ability to resume later) by using the stop function:

troubadour.on('stop', () => {
  // Do something here when the audio is stopped
});

troubadour.stop();

Resume

Resume the playback from a paused state by using the resume function:

troubadour.on('resume', () => {
  // Do something here when the audio is resumed
});

troubadour.resume();

Errors

Troubadour handles errors in two different ways: abruptly throwing an error when the error is not recoverable or gracefully emitting the error when it is recoverable.

Errors like not passing the name of a supported command-line audio player to the Troubadour constructor are not recoverable, so the module will throw an error. Errors like calling the play function without specifying a file path are recoverable, so Troubadour will just emit an error event to let the user know.

The error listener can be set up by including the following:

troubadour.on('error', (error) => {
  // Do something here to handle the errors
});

The error parameter in the callback listener function is an error object. Just like any error object, accessing the error message can be done through error.message.

troubadour's People

Contributors

jaredpetersen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

aea9

troubadour's Issues

Fix typo in docs for stop

In the documentation for stop, the example includes the text:

// Do something here when the audio is paused

This needs to be replaced with the following, since the action is "stop" not "pause":

// Do something here when the audio is stopped

Deal with the remaining unimplemented integration test statements

There are some unimplemented integration test statements in the test run. Determine if those should be implemented and if not, remove them.

Unimplemented integration test statements (duplicated for each supported audio player):

  • Play
    • emits an error event when the audio encounters an error
  • Pause
    • emits an error event when audio playback is not ongoing
  • Stop
    • emits an error event when audio playback is not ongoing

How to check if an audio file is being played

In my use case, I want to make sure to stop any audio that is being played before playing the next one. So I try to use the stop method before playing the next audio file, but when running the function to do that, on first run time it will error out with Error: no audio playback to stop. How to bypass it, and write an if block before stopping to make sure an audio is being played?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.