-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsegment.js
125 lines (107 loc) · 2.83 KB
/
segment.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class Segment {
constructor(ratios=[], subdivide = 1, synths=[]) {
this.voices = [];
this.synths = synths;
this._subdivide = subdivide;
for (var i = 0; i < ratios.length; i++) {
this.voices.push(new Voice(1 / ratios[i], subdivide, synths[i]));
}
this.ratios = ratios;
this._start = 0;
this._end = 1;
this._repeat = 1;
}
reset() {
for (var v of this.voices) {
v.dispose();
}
this.voices = [];
}
set ratios(rat) {
this._get_ratios = null;
this.voice_count = rat.length;
for (var i = 0; i < rat.length; i++) {
if(rat[i] % 1 != 0)throw new Error("ratio is not an integer!")
this.voices[i].interval = 1 / rat[i];
}
}
get ratios(){
if(!this._get_ratios){
this._get_ratios = this.voices.map(v=>1/v.interval);
}
return this._get_ratios;
}
get voice_count() {
return this.voices.length;
}
set voice_count(l) {
if (l < this.voices.length) {
for (let i = this.voices.length - 1; i >= l; i--) {
this.voices.pop().dispose();
}
} else if (l > this.voices.length) {
for (let i = this.voices.length; i < l; i++) {
this.voices.push(new Voice(null, this._get_subdivide_at(i), this.synths[i], this._start, this._end, this._repeat));
}
}
}
_get_subdivide_at(i){
return ( typeof this._subdivide == "number" ? this._subdivide : this._subdivide[i] ) || 1;
}
set subdivide(sub) {
if(this._subdivide == sub)return;
if(typeof sub == "number"){
if(sub<=0){ return; }
this._subdivide = sub;
for (var v of this.voices) {
v.subdivide = sub;
}
}else if(sub instanceof Array){
var old_subdivide = this._subdivide;
this._subdivide = [];
for(var i=0; i<this.voices.length; i++){
var v = this.voices[i];
var s = (sub[i] > 0 ? sub[i] : false) || old_subdivide[i] || (typeof old_subdivide == "number" ? old_subdivide : null) || 1;
this._subdivide[i] = s;
v.subdivide = s;
}
}
}
get subdivide() {
return this._subdivide;
}
get looplength(){
return this._end - this._start;
}
set looplength(v){
if(v == this._end - this._start)return;
this.setLoop(this._start, this._start + v);
}
getWholeLength(){
return (this._end - this._start) * this._repeat;
}
set repeat(v){
if(this._repeat == v)return;
if(v <= 0 || (v|0) != v)throw new Error("the value must be an int and superior to zero.");
this._repeat = v;
for(var vo of this.voices){
vo.repeat = this._repeat;
}
}
get repeat(){
return this._repeat;
}
setLoop(start, end=null) {
if(start == this._start && (!end || end == this._end))return;
this._end = end || start + (this._end-this._start);
this._start = start;
for (var v of this.voices) {
v.setLoop(this._start, this._end);
}
}
dispose(){
for(var v of this.voices){
v.dispose();
}
}
}