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

node-wav-player

v1.0.0

Published

The node-wav-player is a Node.js module which allows you to play a wav file on the host computer. It supports Windows 11, MacOS X, and some Linux distros.

Downloads

836

Readme

node-wav-player

The node-wav-player is a Node.js module which allows you to play a wav file on the host computer. It supports Windows 11, MacOS X, and some Linux distros.

This module can plays a wav file on:

Basically you don't have to install any additional libraries in most environments.

Though this module is intended to play a wav file, it probably can play some audio formats. That depends on the OS. For example, a mp3 file could be played on Mac OS X. But at least on Windows, the supported audio format is only wav.

Dependencies

Installation

$ cd ~
$ npm install node-wav-player

Table of Contents


Quick Start

The code blow plays a wav file:

const player = require('node-wav-player');
player.play({
  path: './speech.wav',
}).then(() => {
  console.log('The wav file started to be played successfully.');
}).catch((error) => {
  console.error(error);
});

That's it. If this code is run successfully, you will hear the audio from your computer.


WavPlayer object

In order to use the node-wav-player, you have to load the node-wav-player module as follows:

const player = require('node-wav-player');

In the code snippet above, the variable player is a WavPlayer object. The WavPlayer object has methods as described in sections below.

play() method

The play() method plays a wav file specified to the path parameter. This method returns a Promise object.

This method takes a hash object containing the properties as follows:

Property | Type | Required | Description :--------|:--------|:---------|:------------------------- path | String | Required | Path of a wav file (e.g., "./speech.wav") loop | Boolean | Optional | If true, the wave file will be played repeatedly until the stop() method is called. The default value is false. sync | Boolean | Optional | If true, this method calls the resove() after finishing to play the wav file. Otherwise, if false, this method calles the resolve() immediately after starting to play the wav file. The default value is false. If the loop is set to true, this parameter is ignored (i.e., this parameter is set to the default value false ).

If you want to wait for the end of the audio, you can set the sync to true as follows:

const player = require('node-wav-player');
player.play({
  path: './speech.wav',
  sync: true
}).then(() => {
  console.log('The wav file was played through.');
}).catch((error) => {
  console.error(error);
});

stop() method

The stop() method stops playing the wav file. This method returns nothing. Note that this method is not asynchronous but synchronous.

The code blow starts to play a wav file. Then it stops playing the wav file in 1 second.

const player = require('node-wav-player');
player.play({
  path: './speech.wav',
}).then(() => {
  console.log('The wav file started to be played successfully.');
}).catch((error) => {
  console.error(error);
});

setTimeout(() => {
  player.stop();
  console.log('Stopped.')
}, 1000);

Release Note

  • v1.0.0 (2024-07-22)
    • Rewrote all codes in modern coding style using class, async, await, etc.
    • Added TypeScript definitions (Thanks to @Stephen-Hamilton-C, #11)
  • v0.2.0 (2020-10-27)
    • Added error catching to spawn child process (Thanks to @Tmp2k, #7)
  • v0.1.0 (2018-10-26)
    • Added the loop parameter to the play() method. (Thanks to @TmpR, #3)
  • v0.0.2 (2018-10-21)
    • Fixed the bug on Win7 + PowerShell. (Thanks to @Joe-Kerr, #1, #2)
  • v0.0.1 (2018-01-03)
    • First public release

References


License

The MIT License (MIT)