├── README.md ├── node-aplay.js └── package.json /README.md: -------------------------------------------------------------------------------- 1 | # aplay 2 | 3 | [![NPM Version](https://img.shields.io/npm/v/aplay.svg)](https://www.npmjs.com/package/aplay) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) 4 | 5 | > ALSA aplay wrapper for Node.js. It provides basic audio capabilities. 6 | 7 | `aplay` works on: 8 | 1. any Debian/Ubuntu system providing ALSA support has been installed. 9 | 2. MAC OSX (comes with `afplay` by default) 10 | 11 | ALSA stands for Advanced Linux Sound Architecture. It is a suite of hardware drivers, libraries and utilities which provide audio and MIDI functionality for the Linux operating system. 12 | 13 | **aplay is a simple native ALSA wav player** (to reproduce .mp3 see *mpg321*). 14 | 15 | ## Installation 16 | 17 | ### Debian/Ubuntu/Raspbian 18 | 19 | Get ready. 20 | Before we start the real work, please update the system. 21 | 22 | sudo apt-get update 23 | sudo apt-get upgrade 24 | 25 | If you are running on Raspberry Pi, please update Raspbian 26 | 27 | sudo rpi-update 28 | 29 | Install ALSA for audio playback 30 | 31 | sudo apt-get install alsa-base alsa-utils 32 | 33 | ### USB Audio on Raspberry Pi 34 | 35 | If you are planning on using a USB audio on Raspberry Pi you will need to set your USB audio device as the default device. 36 | 37 | Edit /etc/modprobe.d/alsa-base.conf and replaced the line: 38 | 39 | options snd-usb-audio index=-2 40 | 41 | With the following lines: 42 | 43 | options snd-usb-audio index=0 nrpacks=1 44 | options snd-bcm2835 index=-2 45 | 46 | After a reboot of your Raspberry Pi 47 | 48 | aplay -l 49 | 50 | Should output the following: 51 | 52 | **** List of PLAYBACK Hardware Devices **** 53 | card 0: XXXX [XXXX], device 0: USB Audio [USB Audio] 54 | Subdevices: 1/1 55 | Subdevice #0: subdevice #0 56 | 57 | Your device volume will be set to 0 by default. Use the ALSA mixer to adjust the volume using your arrow keys: 58 | 59 | alsamixer 60 | 61 | ### Example Usage 62 | 63 | Get it through npm: 64 | 65 | $ npm install aplay --save 66 | 67 | and then: 68 | 69 | ```javascript 70 | 71 | var Sound = require('aplay'); 72 | 73 | // fire and forget: 74 | new Sound().play('/path/to/the/file/filename.wav'); 75 | 76 | // with ability to pause/resume: 77 | var music = new Sound(); 78 | music.play('/path/to/the/file/filename.wav'); 79 | 80 | setTimeout(function () { 81 | music.pause(); // pause the music after five seconds 82 | }, 5000); 83 | 84 | setTimeout(function () { 85 | music.resume(); // and resume it two seconds after pausing 86 | }, 7000); 87 | 88 | // you can also listen for various callbacks: 89 | music.on('complete', function () { 90 | console.log('Done with playback!'); 91 | }); 92 | 93 | ``` 94 | 95 | ## Options 96 | 97 | The constructor accepts a config object where you can provide: 98 | 99 | - `channel`: specify a channel. 100 | 101 | ### CLI Usage 102 | 103 | $ node node_modules/aplay my-song.wav 104 | 105 | 106 | It's simple as that. 107 | -------------------------------------------------------------------------------- /node-aplay.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Javascript ALSA aplay wrapper for Node.js 3 | * 4 | * @mantainedBy Rocco Musolino - @roccomuso 5 | * @author Patrik Melander (lotAballs) node-aplay module 6 | * @originalAuthor Maciej Sopyło @ KILLAHFORGE. 7 | * 8 | * Dependencies: sudo apt-get install alsa-base alsa-utils 9 | * MIT License 10 | */ 11 | 12 | var os = require('os') 13 | var spawn = require('child_process').spawn 14 | var events = require('events') 15 | var util = require('util') 16 | 17 | var aplayExec = os.platform() === 'darwin' ? 'afplay' : 'aplay' 18 | 19 | function Sound (opts) { 20 | events.EventEmitter.call(this) 21 | opts = opts || {} 22 | this.channel = opts.channel || null 23 | } 24 | 25 | util.inherits(Sound, events.EventEmitter) 26 | 27 | Sound.prototype.play = function (fileName) { 28 | this.stopped = false 29 | if (typeof this.process !== 'undefined') this.process.kill('SIGTERM') // avoid multiple play for the same istance 30 | var args = [] 31 | if (this.channel) args = args.concat(['-c ' + this.channel]) 32 | args = args.concat([fileName]) 33 | this.process = spawn(aplayExec, args) 34 | var self = this 35 | this.process.on('exit', function (code, sig) { 36 | self.stopped = true 37 | if (code !== null && sig === null) { 38 | self.emit('complete') 39 | } 40 | }) 41 | return this 42 | } 43 | Sound.prototype.stop = function () { 44 | if (this.process) { 45 | this.stopped = true 46 | this.process.kill('SIGTERM') 47 | this.emit('stop') 48 | } 49 | return this 50 | } 51 | Sound.prototype.pause = function () { 52 | if (this.process) { 53 | if (this.stopped) return true 54 | this.process.kill('SIGSTOP') 55 | this.emit('pause') 56 | } 57 | return this 58 | } 59 | Sound.prototype.resume = function () { 60 | if (this.process) { 61 | if (this.stopped) return this.play() 62 | this.process.kill('SIGCONT') 63 | this.emit('resume') 64 | } 65 | return this 66 | } 67 | Sound.prototype.channel = function (ch) { 68 | this.channel = ch 69 | return this 70 | } 71 | 72 | module.exports = Sound 73 | 74 | // autonomous execution: node node-aplay.js my-song.wav 75 | if (!module.parent) { 76 | var player = new Sound() 77 | player.play(process.argv[2]) 78 | } 79 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aplay", 3 | "version": "1.2.0", 4 | "description": "aplay wrapper for Node.js", 5 | "main": "node-aplay.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/roccomuso/node-aplay.git" 12 | }, 13 | "keywords": [ 14 | "aplay", 15 | "wav", 16 | "song", 17 | "wrapper", 18 | "node", 19 | "rpi", 20 | "raspberry", 21 | "sound" 22 | ], 23 | "author": "Rocco Musolino", 24 | "license": "ISC", 25 | "bugs": { 26 | "url": "https://github.com/roccomuso/node-aplay/issues" 27 | }, 28 | "homepage": "https://github.com/roccomuso/node-aplay#readme" 29 | } 30 | --------------------------------------------------------------------------------