Skip to content

The node-wav-player is a simple Node.js module that lets you play sound files on the host computer.

License

Notifications You must be signed in to change notification settings

noiredayz/node-wav-player

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-wav-player

The node-wav-player is a Node.js module which allows you to play a wav file on the host computer.

This module can plays a wav file on:

  • Windows
  • Mac OS X
    • A wav file is played using afplay command.
  • Linux
  • FreeBSD and DragonflyBSD
    • Just like under Linux you can use ALSA's aplay which is in the alsa-utils compatibility package
  • OpenBSD
    • Wav files are played using aucat
  • NEW Any platform: specify any application to play sounds. This lets you play almost anything (incl. ogg, flac, mp4, m4a) using mpv for example!

Basically you don't have to install any additional libraries in most environments or you can use what you already have or want to use.

Most default methods only can play plain wav files (the Mac method also supports mp3). For other formats you can use the external player.

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


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 ).
sndPlayer String Optional Path to an external application (such as mpv or mplayer) to play your sounds. The default sound players used if not set and error is thrown if there is none for your platform.

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

  • v0.3.0. (2022-06-05) Added support for playing sounds using any application. This makes the module work on virtually any platform that is capable of playing audio.
  • v0.2.2 (2021-10-03) Added OpenBSD support using aucat
  • v0.2.1 (2021-07-17) Forked the project with plans to expand support for other OS'. Dead project? NOIDONTTHINKSO Added a trivial patch by Kiraseira (deleted github account) to support sound playback using aplay under FreeBSD and DragonflyBSD
  • 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)

Copyright (c) 2018 - 2020 Futomi Hatano Copyright (c) 2021 Noiredayz

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.

About

The node-wav-player is a simple Node.js module that lets you play sound files on the host computer.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%