Permalink
Browse files

volume control

  • Loading branch information...
1 parent cff7b61 commit 90c20334c20f0f820275e47fdd753f2aa8fa9b11 @substack committed Sep 4, 2012
Showing with 35 additions and 3 deletions.
  1. +1 −1 package.json
  2. +6 −0 readme.markdown
  3. +28 −2 server.js
View
@@ -1,6 +1,6 @@
{
"name" : "baudio-party",
- "version" : "0.0.2",
+ "version" : "0.0.3",
"description" : "javascript music party http server",
"bin" : {
"baudio-party" : "server.js"
View
@@ -67,6 +67,12 @@ $ curl -X DELETE http://localhost:5000/3
deleted channel 3
```
+To adjust the volume of a channel, send a POST:
+
+```
+$ curl -X POST -d volume=0.5 http://localhost:5000/3
+```
+
# usage
```
View
@@ -1,6 +1,8 @@
var baudio = require('baudio');
var http = require('http');
var spawn = require('child_process').spawn;
+var qs = require('querystring');
+
var argv = require('optimist')
.alias('channels', 'c')
.default('channels', 8)
@@ -29,6 +31,8 @@ var b = baudio({ rate : argv.rate });
var channels = [];
var sources = [];
+var volumes = [];
+
function save (ch, src, cb) {
if (!cb) cb = function () {};
@@ -43,12 +47,13 @@ function save (ch, src, cb) {
}
channels[ch] = fn;
sources[ch] = src;
+ if (!volumes[ch]) volumes[ch] = 1.0;
cb();
}
for (var i = 0; i < argv.channels; i++) (function (i) {
b.push(function () {
- return channels[i].apply(this, arguments);
+ return volumes[i] * channels[i].apply(this, arguments);
});
save(i, 'return ' + function () { return 0 });
})(i);
@@ -69,7 +74,28 @@ var server = http.createServer(function (req, res) {
res.setHeader('content-type', 'text/javascript');
res.end(String(sources[ch]));
}
- if (req.method === 'PUT') {
+ else if (req.method === 'POST') {
+ var ch = Number(req.url.split('/')[1]);
+ if (!channels[ch]) {
+ res.statusCode = 400;
+ res.end('no such channel ' + ch + '\n');
+ return;
+ }
+
+ var data = '';
+ req.on('data', function (buf) { data += buf });
+
+ req.on('end', function () {
+ var params = qs.parse(data);
+
+ if (params.volume) {
+ volumes[ch] = Number(params.volume);
+ res.end('set volume to ' + volumes[ch] + '\n');
+ }
+ else res.end('unknown parameters specified\n')
+ });
+ }
+ else if (req.method === 'PUT') {
var ch = Number(req.url.slice(1));
var src = '';

0 comments on commit 90c2033

Please sign in to comment.