forked from zlovatt/zl_Scriptlets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phone A Friend.jsx
257 lines (227 loc) · 6.63 KB
/
Phone A Friend.jsx
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* Generates Touch Tone noises for your input in AE!
*
* Uses markers & expressions-- move markers & change text to change audio!
*
* @author Zack Lovatt <[email protected]>
* @version 0.1.0
*/
(function phoneAFriend () {
// # Frames for the tone to last
var toneDuration = 8.5;
var number = getUserInput();
if (!number) {
return;
}
var comp = app.project.activeItem;
if (!(comp && comp instanceof CompItem)) {
alert("Open a comp!");
return;
}
app.beginUndoGroup("Phone A Friend");
try {
var toneNull = comp.layers.addNull();
toneNull.name = "Phone a Friend!";
// Add the effect & expressions
var toneEffect = toneNull
.property("ADBE Effect Parade")
.addProperty("ADBE Aud Tone");
var freq1 = toneEffect.property("ADBE Aud Tone-0002");
addPhoneAFriendExpression(freq1, toneDuration, 0);
var freq2 = toneEffect.property("ADBE Aud Tone-0003");
addPhoneAFriendExpression(freq2, toneDuration, 1);
toneEffect.property("ADBE Aud Tone-0004").setValue(0);
toneEffect.property("ADBE Aud Tone-0005").setValue(0);
toneEffect.property("ADBE Aud Tone-0006").setValue(0);
// Add muter. Annoying but needed to prevent warpy audio.
var audioLevels = toneNull.property("ADBE Audio Group").property("ADBE Audio Levels");
audioLevels.expression = [
"// # frames for tone to last",
"var toneLength = " + toneDuration + ";",
"",
"var priorMarkerIndex = 0;",
"if (thisLayer.marker.numKeys > 0) {",
" var nearestMarker = thisLayer.marker.nearestKey(time);",
" priorMarkerIndex = nearestMarker.index;",
"",
" if (nearestMarker.time > time) {",
" priorMarkerIndex = nearestMarker.index - 1;",
" }",
"}",
"",
"var result = [-192, -192];",
"if (priorMarkerIndex !== 0) {",
" var priorMarker = thisLayer.marker.key(priorMarkerIndex);",
" var inputMin = priorMarker.time + framesToTime(1);",
" var inputMax = inputMin + framesToTime(toneLength - 2);",
"",
" if (time >= inputMin && time < inputMax) {",
" result = [0, 0];",
" }",
"}",
"",
"result;",
].join("\n");
// Add the markers
addMarkersToLayer(toneNull, number, 15);
} catch (e) {
alert(e);
} finally {
app.endUndoGroup();
}
/**
* Gets user input
*
* @returns {string} User input phone number
*/
function getUserInput() {
var valid = "1234567890*#";
var number = prompt("Enter phone number!", "967-1111", "Phone A Friend");
if (!number) {
return;
}
number = number.toUpperCase();
number = number.split("-").join("");
number = number.split(" ").join("");
var invalid = [];
var result = [];
for (var ii = 0, il = number.length; ii < il; ii++) {
var key = number.charAt(ii);
if (valid.indexOf(key) > -1) {
result.push(key);
continue;
}
switch (key) {
case "A":
case "B":
case "C":
result.push("2");
break;
case "D":
case "E":
case "F":
result.push("3");
break;
case "G":
case "H":
case "I":
result.push("4");
break;
case "J":
case "K":
case "L":
result.push("5");
break;
case "M":
case "N":
case "O":
result.push("6");
break;
case "P":
case "Q":
case "R":
case "S":
result.push("7");
break;
case "T":
case "U":
case "V":
result.push("8");
break;
case "W":
case "X":
case "Y":
case "Z":
result.push("9");
break;
case "+":
result.push("0");
break;
default:
if (invalid.join("").indexOf(key) === -1) {
invalid.push(key);
}
break;
}
}
if (invalid.length > 0) {
throw "Invalid characters: '" + invalid.join(", ") + "'";
}
return result;
}
/**
* Adds expression to a given prop
*
* @param {Property} prop Property to add exp to
* @param {number} toneLength # Frames to have the tone last for
* @param {number} matrixIndex Matrix index to use
*/
function addPhoneAFriendExpression(prop, toneLength, matrixIndex) {
if (!prop.canSetExpression) {
throw "Can't add expression!";
}
prop.expression = [
"// # frames for tone to last",
"var toneLength = " + toneLength + ";",
"",
"function characterToTone(character, matrixIndex) {",
" var toneMap = [",
' { "123": 697, "456": 770, "789": 852, "*0#": 941 },',
' { "147*": 1209, "2580": 1336, "369#": 1477 }',
" ];",
"",
" var matrixSet = toneMap[matrixIndex];",
"",
" for (var button in matrixSet) {",
" if (!matrixSet.hasOwnProperty(button)) { continue; }",
" if (button.indexOf(character) === -1) { continue; }",
" return matrixSet[button];",
" }",
"}",
"",
"var priorMarkerIndex = 0;",
"if (thisLayer.marker.numKeys > 0) {",
" var nearestMarker = thisLayer.marker.nearestKey(time);",
" priorMarkerIndex = nearestMarker.index;",
"",
" if (nearestMarker.time > time) {",
" priorMarkerIndex = nearestMarker.index - 1;",
" }",
"}",
"",
"var result = 0;",
'var valid = "1234567890*#ABCD";',
"if (priorMarkerIndex !== 0) {",
" var priorMarker = thisLayer.marker.key(priorMarkerIndex);",
"",
" var button = priorMarker.comment;",
"",
' if (button !== "" && valid.indexOf(button) > -1) {',
" var inputMin = priorMarker.time;",
" var inputMax = inputMin + framesToTime(toneLength);",
"",
" if (time >= inputMin && time < inputMax) {",
" result = characterToTone(button, " + matrixIndex + ");",
" }",
" }",
"}",
"",
"result;"
].join("\n");
}
/**
* Adds markers to a layer at a given interval
*
* @param {Layer} layer Layer to add markers to
* @param {string[]} markerText Marker text array
* @param {nubmer} interval Interval in frames
*/
function addMarkersToLayer(layer, markerText, interval) {
var markers = layer.property("ADBE Marker");
var intervalInSeconds = interval * layer.containingComp.frameDuration;
for (var ii = 0, il = markerText.length; ii < il; ii++) {
var marker = new MarkerValue(markerText[ii]);
markers.setValueAtTime(intervalInSeconds * ii, marker);
}
}
})();