Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better linux support #7

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
51 changes: 37 additions & 14 deletions impl/linux.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@

var spawn = require('child_process').spawn;
var childProcess = require('child_process');

var amixer = function (args, cb) {
var isUbuntu = function (cb) {
childProcess.exec('uname -a', (err, stdout, stderr) => {
if (err) return cb(err);

return cb(null, stdout.toLowerCase().indexOf('ubuntu') > -1);
});
};

var amixer = function (args, cb) {
var ret = '';
var err = null;
var p = spawn('amixer', args);

isUbuntu((err, ubuntu) => {
if (err) return cb(err);

p.stdout.on('data', function (data) {
ret += data;
});

p.stderr.on('data', function (data) {
err = new Error('Alsa Mixer Error: ' + data);
});
if (ubuntu) {
args.unshift('pulse');
args.unshift('-D');
}

var p = childProcess.spawn('amixer', args);
p.stdout.on('data', function (data) {
ret += data;
});

p.stderr.on('data', function (data) {
err = new Error('Alsa Mixer Error: ' + data);
});

p.on('close', function () {
cb(err, ret.trim());
});

p.on('close', function () {
cb(err, ret.trim());
});

};
Expand Down Expand Up @@ -101,7 +118,13 @@ module.exports.getMuted = function (cb) {
};

module.exports.setMuted = function (val, cb) {
amixer(['set', 'PCM', (val?'mute':'unmute')], function (err) {
cb(err);
defaultDevice(function (err, dev) {
if (err) {
cb(err);
} else {
amixer(['set', dev, (val?'mute':'unmute')], function (err) {
cb(err);
});
}
});
};
160 changes: 160 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 27 additions & 6 deletions test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,51 @@ describe('loudness', function () {
});

it('should set and get the volume', function (done) {
loudness.setVolume(15, function (err) {
// test all options - from 0% to 100%
var indexes = Array.apply(null, {length: 101}).map(Number.call, Number);
async.eachSeries(indexes, function (index, next) {
loudness.setVolume(index, function (err) {

assert.ifError(err);

loudness.getVolume(function (err, vol) {

assert.ifError(err);
assert.equal(vol, index);

next();
});
});
}, function () {
done();
});
}).timeout(5000);

it('should mute the volume', function (done) {
loudness.setMuted(true, function (err) {

assert.ifError(err);

loudness.getVolume(function (err, vol) {
loudness.getMuted(function (err, mute) {

assert.ifError(err);
assert.equal(vol, 15);
assert.equal(mute, true);

done();
});

});
});

it('should set and get the mute state', function (done) {
loudness.setMuted(true, function (err) {
it('should unmute the volume', function (done) {
loudness.setMuted(false, function (err) {

assert.ifError(err);

loudness.getMuted(function (err, mute) {

assert.ifError(err);
assert.equal(mute, true);
assert.equal(mute, false);

done();
});
Expand Down