Permalink
Browse files

move push() to addChannel(), use push() for adding tracks instead

  • Loading branch information...
1 parent c1fab5c commit 537901bfdf4556ea1da5ddb7530d0ea282a6ad23 @substack committed Dec 18, 2012
Showing with 30 additions and 10 deletions.
  1. +1 −3 example/tune.js
  2. +19 −3 index.js
  3. +1 −1 package.json
  4. +9 −3 readme.markdown
View
@@ -2,8 +2,6 @@ var baudio = require('../');
var n = 0;
var b = baudio(function (t) {
- var x = Math.sin(t * 262 + Math.sin(n));
- n += Math.sin(t);
- return x;
+ return Math.sin(t * 400 * Math.PI * 2) + Math.sin(t * 500) * (t % 2 > 1);
});
b.play();
View
@@ -54,12 +54,23 @@ B.prototype.resume = function () {
this.emit('resume');
};
-B.prototype.push = function (type, fn) {
+B.prototype.addChannel = function (type, fn) {
if (typeof type === 'function') {
fn = type;
type = 'float';
}
- this.channels.push([ type, fn ]);
+ this.channels.push([ type, [ fn ].filter(Boolean) ]);
+};
+
+B.prototype.push = function (ix, fn) {
+ if (typeof ix === 'function') {
+ fn = ix;
+ ix = 0;
+ }
+ if (!this.channels[ix]) {
+ this.channels[ix] = [ 'float', [] ];
+ }
+ this.channels[ix][1].push(fn);
};
B.prototype.loop = function () {
@@ -97,7 +108,12 @@ B.prototype.tick = function () {
var counter = self.i + Math.floor(i / 2 / self.channels.length);
var value = 0;
- var n = ch[1].call(self, t, counter);
+ var n = 0;
+ for (var j = 0; j < ch[1].length; j++) {
+ var x = ch[1][j].call(self, t, counter);
+ if (!isNaN(x)) n += x;
+ }
+ n /= ch[1].length;
if (ch[0] === 'float') {
value = signed(n);
View
@@ -1,6 +1,6 @@
{
"name" : "baudio",
- "version" : "0.1.3",
+ "version" : "1.0.0",
"description" : "generate audio streams with functions",
"main" : "index.js",
"directories" : {
View
@@ -31,17 +31,23 @@ The `opts.rate` is the rate of the output stream in Hz, default 44000.
The `opts.size` is the size of data chunks to emit, default 1024.
-## b.push(type, fn)
+## b.push(ix=0, fn)
-Push a new audio channel of `type` with the function `fn(t, i)` for the time in
-seconds `t` and a counter `i`.
+Push a new track onto the channel at index `ix` with the function `fn(t, i)` for
+the time in seconds `t` and a counter `i`.
+
+## b.addChannel(type, fn)
+
+Push a new audio channel of `type`, optionally initializing the channel with a
+function `fn(t, i)`.
`type` defaults to `'float'` which expects inputs in the range `[-1,1]` and
clips higher and lower values.
`type` can also be a power of 2 number of bits to use for each sample which
expects an integer output value in `[0,Math.pow(2,type)-1]`.
+
## b.play(opts)
Play the audio demo with the [play command](http://sox.sourceforge.net/).

0 comments on commit 537901b

Please sign in to comment.