node-wav-player
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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


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.


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.

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

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

  • 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


The MIT License (MIT)

Readme

Keywords

Package Sidebar

Install

npm i node-wav-player

Weekly Downloads

250

Version

1.0.0

License

MIT

Unpacked Size

12 kB

Total Files

4

Last publish

Collaborators

  • futomi