/
Helpers.js
209 lines (165 loc) · 3.95 KB
/
Helpers.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
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
/**
* Title: helpers.js
* Main Author: David Healey
* Additional Functions by Christoph Hart
* Date: 29/07/2017
* Modified: 03/12/2017
* License: Public Domain
*/
namespace Helpers
{
//Returns note letter only, for letter + octave use built in Engine.getMidiNoteName()
inline function noteNumberToLetter(n)
{
local noteLetters = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
return noteLetters[n % 12];
}
inline function getAllNoteNames()
{
local i;
local allNoteNames = [];
for (i = 0; i < 128; i++)
{
allNoteNames[i] = Engine.getMidiNoteName(i);
}
return allNoteNames;
}
inline function tempoIndexToString(tempoIndex)
{
local tempos = ["1/1", "1/2", "1/2T", "1/4", "1/4T", "1/8", "1/8T", "1/16", "1/16T", "1/32", "1/32T"];
return tempos[tempoIndex];
}
inline function inRange(value, min, max)
{
return value >= min && value <= max;
}
inline function isEven(n)
{
return !(n & 1);
}
inline function isBlackKey(n)
{
local blackKeys = [1, 3, 6, 8, 10]; //Black keys in one octave assuming C = 0
if (blackKeys.indexOf(parseInt(n) % 12) != -1)
{
return true;
}
return false;
}
inline function skew(value, max, skewFactor)
{
// values > 1 will yield more resolution at the lower end
local normalised = value / max;
// this will "bend" the line towards the lower end
local skewed = Math.pow(normalised, skewFactor);
return skewed * max;
}
inline function remapRange(value, oldMin, oldMax, newMin, newMax)
{
if (oldMax - oldMin == 0)
return newMin;
else
return (((value - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin;
}
inline function getSamplers(exclude)
{
local samplerIds = Synth.getIdList("Sampler");
local samplers = [];
for (id in samplerIds)
{
if (typeof exclude == "string")
if (Engine.matchesRegex(id, exclude) == true) continue; // Skip IDs that match exclusion
samplers.push(Synth.getSampler(id));
}
return samplers;
}
inline function getIdListContainingString(type, containing)
{
local idList = Synth.getIdList(type);
local filteredIds = [];
local i;
for (i = 0; i < idList.length; i++)
{
if (idList[i].indexOf(containing) == -1) continue; // Skip ids that don't contain the substring
filteredIds.push(idList[i]);
}
return filteredIds;
}
inline function keyExists(obj, key)
{
return !(obj[key] == void); // Important: not undefined!
}
inline function getSize(obj)
{
local count = 0;
for (k in obj)
count++;
return count;
}
inline function getPropertyByIndex(obj, index)
{
local count = 0;
for (k in obj)
{
if (count == index)
return obj[k];
count++;
}
return undefined;
}
inline function getIndexByProperty(obj, prop)
{
local count = 0;
for (k in obj)
{
if (k == prop) return count;
count++;
}
return -1;
}
inline function cartesianProduct(array)
{
local i;
local j;
local k;
local result = [[]];
for (i = 0; i < array.length; i++)
{
local subArray = array[i];
local temp = [];
for (j = 0; j < result.length; j++)
{
for (k = 0; k < subArray.length; k++)
{
local c = concatArrays(result[j], [subArray[k]]);
temp.push(c);
}
}
result = temp;
}
return result;
}
inline function concatArrays(a, b)
{
local c = [];
local i;
for (i = 0; i < a.length; i++)
c[i] = a[i];
for (i = 0; i < b.length; i++)
c.push(b[i]);
return c;
}
inline function mergeObjects(arrayOfObjects)
{
local target = {};
for (object in arrayOfObjects)
{
if (!isDefined(object) || !object.length) continue;
for (key in object)
{
target[key] = object[key];
}
}
return target;
}
}