Skip to content

Commit

Permalink
Feature: Support for adding multiple layers in beet.add
Browse files Browse the repository at this point in the history
  • Loading branch information
zya committed Apr 22, 2016
1 parent 0fa9aac commit 86aa793
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 62 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ Pauses the sequencer after a given time in seconds but keeps the current step nu
#### Parameters
* `time` (optional) - a number in seconds - defaults to 0

#### `beet.add(layer)`
#### `beet.add(layer(s))`

Adds a given `beet.layer` to the list of layers to play.
Adds a given `beet.layer` to the list of layers to play. Can also be used with multiple layers. `beet.add(layer1, layer2)`

#### Parameters
* `layer` (Required) - a `beet.layer` object.
Expand Down
48 changes: 31 additions & 17 deletions build/beet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,80 @@ var Pattern = require('./pattern');
var Layer = require('./layer');
var utils = require('./utils');

if (!Array.from) {
Array.from = function (object) {
'use strict';
return [].slice.call(object);
};
}

function Beet(opts) {
this.context = opts.context;
this.tempo = opts.tempo || 120;
this.layers = [];
var self = this;

watch(this, 'tempo', function() {
watch(this, 'tempo', function () {
self._change_tempo(self.tempo);
});
}

Beet.prototype.layer = function(seq, on, off) {
Beet.prototype.layer = function (seq, on, off) {
var layer = new Layer(this.context, this.tempo, seq, on, off);
return layer;
};

Beet.prototype.pattern = function(pulses, steps) {
Beet.prototype.pattern = function (pulses, steps) {
var pattern = new Pattern(pulses, steps);
return pattern;
};

Beet.prototype.add = function(layer) {
this.layers.push(layer);
Beet.prototype.add = function () {
var self = this;
var array = Array.from(arguments);

array.forEach(function (layer) {
self.layers.push(layer);
});

return this;
};

Beet.prototype.remove = function(layer) {
Beet.prototype.remove = function (layer) {
var index = this.layers.indexOf(layer);
var found = this.layers[index];
found.metro.stop();
this.layers.splice(index, 1);
return this;
};

Beet.prototype.start = function(when) {
Beet.prototype.start = function (when) {
var self = this;
var start_time = when || 0;
setTimeout(function() {
self.layers.forEach(function(layer) {
setTimeout(function () {
self.layers.forEach(function (layer) {
layer.start();
});
}, start_time * 1000);
return this;
};

Beet.prototype.stop = function(when) {
Beet.prototype.stop = function (when) {
var self = this;
var start_time = when || 0;
setTimeout(function() {
self.layers.forEach(function(layer) {
setTimeout(function () {
self.layers.forEach(function (layer) {
layer.stop();
});
}, start_time * 1000);
return this;
};

Beet.prototype.pause = function(when) {
Beet.prototype.pause = function (when) {
var self = this;
var start_time = when || 0;
setTimeout(function() {
self.layers.forEach(function(layer) {
setTimeout(function () {
self.layers.forEach(function (layer) {
layer.pause();
});
}, start_time * 1000);
Expand All @@ -76,8 +89,8 @@ Beet.prototype.pause = function(when) {

Beet.prototype.utils = utils;

Beet.prototype._change_tempo = function(value) {
this.layers.forEach(function(layer) {
Beet.prototype._change_tempo = function (value) {
this.layers.forEach(function (layer) {
layer.metro.tempo = value;
});
};
Expand Down Expand Up @@ -129,6 +142,7 @@ Layer.prototype.stop = function () {
};

module.exports = Layer;

},{"wa-metro":8,"watchjs":11}],4:[function(require,module,exports){
var bjork = require('bjorklund');
var watch = require('watchjs').watch;
Expand Down
8 changes: 4 additions & 4 deletions build/beet.min.js

Large diffs are not rendered by default.

48 changes: 31 additions & 17 deletions build/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,67 +256,80 @@ var Pattern = require('./pattern');
var Layer = require('./layer');
var utils = require('./utils');

if (!Array.from) {
Array.from = function (object) {
'use strict';
return [].slice.call(object);
};
}

function Beet(opts) {
this.context = opts.context;
this.tempo = opts.tempo || 120;
this.layers = [];
var self = this;

watch(this, 'tempo', function() {
watch(this, 'tempo', function () {
self._change_tempo(self.tempo);
});
}

Beet.prototype.layer = function(seq, on, off) {
Beet.prototype.layer = function (seq, on, off) {
var layer = new Layer(this.context, this.tempo, seq, on, off);
return layer;
};

Beet.prototype.pattern = function(pulses, steps) {
Beet.prototype.pattern = function (pulses, steps) {
var pattern = new Pattern(pulses, steps);
return pattern;
};

Beet.prototype.add = function(layer) {
this.layers.push(layer);
Beet.prototype.add = function () {
var self = this;
var array = Array.from(arguments);

array.forEach(function (layer) {
self.layers.push(layer);
});

return this;
};

Beet.prototype.remove = function(layer) {
Beet.prototype.remove = function (layer) {
var index = this.layers.indexOf(layer);
var found = this.layers[index];
found.metro.stop();
this.layers.splice(index, 1);
return this;
};

Beet.prototype.start = function(when) {
Beet.prototype.start = function (when) {
var self = this;
var start_time = when || 0;
setTimeout(function() {
self.layers.forEach(function(layer) {
setTimeout(function () {
self.layers.forEach(function (layer) {
layer.start();
});
}, start_time * 1000);
return this;
};

Beet.prototype.stop = function(when) {
Beet.prototype.stop = function (when) {
var self = this;
var start_time = when || 0;
setTimeout(function() {
self.layers.forEach(function(layer) {
setTimeout(function () {
self.layers.forEach(function (layer) {
layer.stop();
});
}, start_time * 1000);
return this;
};

Beet.prototype.pause = function(when) {
Beet.prototype.pause = function (when) {
var self = this;
var start_time = when || 0;
setTimeout(function() {
self.layers.forEach(function(layer) {
setTimeout(function () {
self.layers.forEach(function (layer) {
layer.pause();
});
}, start_time * 1000);
Expand All @@ -325,8 +338,8 @@ Beet.prototype.pause = function(when) {

Beet.prototype.utils = utils;

Beet.prototype._change_tempo = function(value) {
this.layers.forEach(function(layer) {
Beet.prototype._change_tempo = function (value) {
this.layers.forEach(function (layer) {
layer.metro.tempo = value;
});
};
Expand Down Expand Up @@ -594,6 +607,7 @@ Layer.prototype.stop = function () {
};

module.exports = Layer;

},{"wa-metro":16,"watchjs":19}],9:[function(require,module,exports){
var bjork = require('bjorklund');
var watch = require('watchjs').watch;
Expand Down
5 changes: 2 additions & 3 deletions examples/basic-polyrhythm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@
}, timeFromScheduled * 1000);
}

beet.add(layer);
beet.add(layer2);
beet.add(layer, layer2);
beet.start();
</script>
</html>
</html>
47 changes: 30 additions & 17 deletions lib/beet.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,80 @@ var Pattern = require('./pattern');
var Layer = require('./layer');
var utils = require('./utils');

if (!Array.from) {
Array.from = function (object) {
'use strict';
return [].slice.call(object);
};
}

function Beet(opts) {
this.context = opts.context;
this.tempo = opts.tempo || 120;
this.layers = [];
var self = this;

watch(this, 'tempo', function() {
watch(this, 'tempo', function () {
self._change_tempo(self.tempo);
});
}

Beet.prototype.layer = function(seq, on, off) {
Beet.prototype.layer = function (seq, on, off) {
var layer = new Layer(this.context, this.tempo, seq, on, off);
return layer;
};

Beet.prototype.pattern = function(pulses, steps) {
Beet.prototype.pattern = function (pulses, steps) {
var pattern = new Pattern(pulses, steps);
return pattern;
};

Beet.prototype.add = function(layer) {
this.layers.push(layer);
Beet.prototype.add = function () {
var self = this;
var array = Array.from(arguments);

array.forEach(function (layer) {
self.layers.push(layer);
});

return this;
};

Beet.prototype.remove = function(layer) {
Beet.prototype.remove = function (layer) {
var index = this.layers.indexOf(layer);
var found = this.layers[index];
found.metro.stop();
this.layers.splice(index, 1);
return this;
};

Beet.prototype.start = function(when) {
Beet.prototype.start = function (when) {
var self = this;
var start_time = when || 0;
setTimeout(function() {
self.layers.forEach(function(layer) {
setTimeout(function () {
self.layers.forEach(function (layer) {
layer.start();
});
}, start_time * 1000);
return this;
};

Beet.prototype.stop = function(when) {
Beet.prototype.stop = function (when) {
var self = this;
var start_time = when || 0;
setTimeout(function() {
self.layers.forEach(function(layer) {
setTimeout(function () {
self.layers.forEach(function (layer) {
layer.stop();
});
}, start_time * 1000);
return this;
};

Beet.prototype.pause = function(when) {
Beet.prototype.pause = function (when) {
var self = this;
var start_time = when || 0;
setTimeout(function() {
self.layers.forEach(function(layer) {
setTimeout(function () {
self.layers.forEach(function (layer) {
layer.pause();
});
}, start_time * 1000);
Expand All @@ -73,8 +86,8 @@ Beet.prototype.pause = function(when) {

Beet.prototype.utils = utils;

Beet.prototype._change_tempo = function(value) {
this.layers.forEach(function(layer) {
Beet.prototype._change_tempo = function (value) {
this.layers.forEach(function (layer) {
layer.metro.tempo = value;
});
};
Expand Down
2 changes: 1 addition & 1 deletion lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ Layer.prototype.stop = function () {
return this;
};

module.exports = Layer;
module.exports = Layer;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "beet.js",
"version": "1.6.0",
"version": "1.7.0",
"description": "Polyrhythmic Sequencer for Web Audio API",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 86aa793

Please sign in to comment.