forked from possan/druuum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.js
63 lines (55 loc) · 1.4 KB
/
pattern.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
(function(exports) {
var Pattern = function() {
this.position = 0;
this.numtracks = 8;
this.numsteps = 16;
this.tracks = [];
for(var i=0; i<this.numtracks; i++) {
this.tracks.push(new Track(this.numsteps));
}
}
Pattern.prototype.set = function(r, c, v) {
this.tracks[r].steps[c] = v;
}
Pattern.prototype.get = function(r, c) {
return this.tracks[r].steps[c];
}
Pattern.prototype.toggle = function(r, c) {
var v = this.get(r, c);
this.set(r, c, 1 - v);
}
Pattern.prototype.toJson = function() {
var ret = [];
for(var i=0; i<this.tracks.length; i++) {
var trkin = this.tracks[i];
var steps = trkin.steps.join('');
var trkout = {
u: trkin.slice.url,
o: trkin.slice.offset,
d: trkin.slice.decay,
g: trkin.slice.gain,
p: trkin.slice.pitch,
t: steps
}
ret.push(trkout);
}
return JSON.stringify(ret);
}
Pattern.prototype.parseJson = function(json) {
var obj = JSON.parse(json);
if (obj) {
for(var i=0; i<obj.length; i++) {
var trkin = obj[i];
var trkout = this.tracks[i];
var steps = (trkin.t || '0000000000000000').split('').map(function(r) { return parseInt(r, 10); });
trkout.slice.url = trkin.u || '';
trkout.slice.offset = trkin.o;
trkout.slice.decay = trkin.d;
trkout.slice.gain = trkin.g;
trkout.slice.pitch = trkin.p;
trkout.steps = steps;
}
}
}
exports.Pattern = Pattern;
})(window || this);