-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomization.js
More file actions
242 lines (188 loc) · 5.43 KB
/
Copy pathrandomization.js
File metadata and controls
242 lines (188 loc) · 5.43 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//Master object map that contains the schema for the randomization rules.
let mRandomizationMap = {
"oscillator1":{
"volume":{
"r":false,
"type":"numeric-integer",
"min":-20,
"max":0,
"value":0,
"units":"dB"
},
"type":{
"r":true,
"type":"set",
"options":["sine","square","sawtooth","triangle"],
"value":"sine"
}
},
"oscillator2":{
"volume":{
"r":true,
"type":"numeric-integer",
"min":-20,
"max":0,
"value":0,
"units":"dB"
},
"type":{
"r":true,
"type":"set",
"options":["sine","square","sawtooth","triangle"],
"value":"sine"
}
},
"envelope":{
"attack":{
"r":true,
"type":"numeric-normalized",
"value":0.5,
"units":"s"
},
"decay":{
"r":true,
"type":"numeric-normalized",
"value":0.5,
"units":"s"
},
"sustain":{
"r":true,
"type":"numeric-normalized",
"value":1,
"units":"/1"
},
"release":{
"r":true,
"type":"numeric-normalized",
"value":1,
"units":"s"
},
},
"filter":{
"type":{
"r":false,
"type":"set",
"options":["lowpass","highpass"],
"value":"lowpass"
},
"frequency":{
"r":true,
"type":"numeric-integer",
"min":1000,
"max":0,
"value":200,
"units":"Hz"
},
},
"filterEnvelope":{
"frequency":{
"r":true,
"type":"numeric-integer",
"min":1000,
"max":0,
"value":0,
"units":"Hz"
},
"attack":{
"r":true,
"type":"numeric-normalized",
"value":0,
"units":"s"
},
"decay":{
"r":true,
"type":"numeric-normalized",
"value":0,
"units":"s"
},
"sustain":{
"r":true,
"type":"numeric-normalized",
"value":0,
"units":"/1"
},
"release":{
"r":true,
"type":"numeric-normalized",
"value":0,
"units":"s"
}
}
};
function getRandomizedSynthesizerOptions(synthNumber){
let synthOptions = extractSynthOptionsFromMap(synthNumber);
return synthOptions;
}
function randomize(){
traverseAndRandomize();
}
/**
* FUnction that converts the randomization map into a set of SYnthOptions that we can use with a Tone.MonoSynth
* @param {Number} synthNumber
*/
function extractSynthOptionsFromMap(synthNumber){
let synthOptions = {
"oscillator":{
"volume":mRandomizationMap["oscillator"+synthNumber].volume.value,
"type":mRandomizationMap["oscillator"+synthNumber].type.value,
},
"envelope":{
"attack":mRandomizationMap["envelope"].attack.value,
"decay":mRandomizationMap["envelope"].decay.value,
"sustain":mRandomizationMap["envelope"].sustain.value,
"release":mRandomizationMap["envelope"].release.value
},
"filter":{
"type":mRandomizationMap["filter"].type.value,
"frequency":mRandomizationMap["filter"].frequency.value
},
"filterEnvelope":{
"frequency":mRandomizationMap["filterEnvelope"].frequency.value,
"attack":mRandomizationMap["filterEnvelope"].attack.value,
"decay":mRandomizationMap["filterEnvelope"].decay.value,
"sustain":mRandomizationMap["filterEnvelope"].sustain.value,
"release":mRandomizationMap["filterEnvelope"].release.value
}
}
return synthOptions;
}
/**
* Function that traverses the Ranomization Map and follows a few rules to get a ranomized value where it needs to
*/
function traverseAndRandomize(){
let masterKeys = Object.keys(mRandomizationMap);
// console.log("Master Keys ------- "+masterKeys);
masterKeys.forEach(key =>{
let innerKeys = Object.keys(mRandomizationMap[key]);
// console.log("Inner Keys ------- "+innerKeys);
innerKeys.forEach(innerKey =>{
let item = mRandomizationMap[key][innerKey];
if(item.r === false) return;
switch(item.type){
case "numeric-integer":
item.value = getRandomInt(item.min, item.max);
break;
case "set":
item.value = getRandomItemFromArray(item.options);
break;
case "numeric-normalized":
item.value = getRandomNormalizedFloat();
break;
}
});
});
}
/** UTILITY STATIC FUNCTIONS */
function getRandomItemFromArray(array){
let i = Math.floor(Math.random()*array.length);
return array[i];
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
function getRandomNormalizedFloat() {
return parseFloat(Math.random().toFixed(3));
}
/** UTILITY STATIC FUNCTIONS */