node-wav-player

0.2.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 10, 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

  • Node.js 4 +
    • Though the node-wav-player works on Node 4 for now, it is strongly recommended to use Node 6 or newer. The node-wav-player will not support Node 4 in the future.

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

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

Copyright (c) 2018 - 2020 Futomi Hatano

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Readme

Keywords

Package Sidebar

Install

npm i node-wav-player

Weekly Downloads

229

Version

0.2.0

License

MIT

Unpacked Size

12.3 kB

Total Files

4

Last publish

Collaborators

  • futomi