-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
233 lines (215 loc) · 10.6 KB
/
index.html
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
<!--
Copyright 2019 Toru Higuruma
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="author" content="Toru Higuruma" />
<meta name="application-name" content="BPM2FPS" />
<meta name="description" content="Calculate the optimal frame rate for music video authoring" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous" />
<title>BPM2FPS - Calculate the optimal frame rate for music video authoring</title>
</head>
<body>
<div class="container mt-sm-5" style="max-width: 540px">
<header class="mb-4">
<h1>BPM2FPS</h1>
<p class="lead">Calculate the optimal frame rate for music video authoring</p>
</header>
<form novalidate="novalidate">
<div class="form-group row">
<label for="bpmInput" class="col-sm-3 col-form-label-lg">BPM</label>
<div class="col-sm">
<input type="number" id="bpmInput" class="form-control form-control-lg" value="100" min="1" autofocus="autofocus" />
<small class="form-text text-muted">Beats per minute</small>
</div>
</div>
<div class="form-group row">
<label for="divInput" class="col-sm-3 col-form-label">Beat Divider</label>
<div class="col-sm">
<input type="number" id="divInput" class="form-control" value="4" min="1" />
<small class="form-text text-muted">Frames per beat becomes a multiple of this value</small>
</div>
</div>
<div class="form-group row">
<label for="minFpsInput" class="col-sm-3 col-form-label">Minimum FPS</label>
<div class="col-sm">
<input type="number" id="minFpsInput" class="form-control" value="30" min="1" />
<small class="form-text text-muted">Output FPS satisfies this value
(commonly <a href="#" class="minFpsPreset" data-value="30">30 FPS</a>
or <a href="#" class="minFpsPreset" data-value="60">60 FPS</a>)</small>
</div>
</div>
<hr />
<div class="form-group row">
<label for="fpsOutput" class="col-sm-3 col-form-label-lg">FPS</label>
<div class="col-sm">
<div class="input-group input-group-lg">
<input type="text" id="fpsOutput" class="form-control form-control-lg border-secondary" readonly="readonly" />
<div class="input-group-append">
<button type="button" class="btn btn-outline-secondary" id="fpsCopyButton">Copy</button>
</div>
</div>
</div>
</div>
<div class="form-group row">
<label for="fpsROutput" class="col-sm-3 col-form-label">FPS (Rational)</label>
<div class="col-sm">
<div class="input-group">
<input type="text" id="fpsROutput" class="form-control border-secondary" readonly="readonly" />
<div class="input-group-append">
<button type="button" class="btn btn-outline-secondary" id="fpsRCopyButton">Copy</button>
</div>
</div>
</div>
</div>
<table class="border-bottom border-secondary text-nowrap w-100" style="table-layout: fixed">
<tr id="timelineBeatsRow" style="font-size: 0.75rem">
<td class="border-left border-secondary overflow-hidden">0.000 sec</td>
</tr>
<tr id="timelineFramesRow" class="text-muted" style="font-size: 0.5625rem; line-height: 1">
<td class="border-left border-secondary"><span class="bg-white d-inline-block">1</span></td>
</tr>
</table>
</form>
<footer class="my-5 text-center text-muted">
<p><small>© 2019 <a href="https://github.com/neofuji" rel="me" target="_blank">Toru Higuruma</a></small></p>
<p><small><a href="https://github.com/neofuji/bpm2fps" target="_blank">Source Code</a> (Apache License 2.0)</small></p>
</footer>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script>
var bpmInputElem = document.getElementById("bpmInput");
var divInputElem = document.getElementById("divInput");
var minFpsInputElem = document.getElementById("minFpsInput");
var fpsOutputElem = document.getElementById("fpsOutput");
var fpsROutputElem = document.getElementById("fpsROutput");
var fpsCopyButtonElem = document.getElementById("fpsCopyButton");
var fpsRCopyButtonElem = document.getElementById("fpsRCopyButton");
var timelineBeatsRowElem = document.getElementById("timelineBeatsRow");
var timelineFramesRowElem = document.getElementById("timelineFramesRow");
var inputElems = [bpmInputElem, divInputElem, minFpsInputElem, fpsOutputElem, fpsROutputElem];
inputElems.forEach(function (elem) {
elem.addEventListener("keydown", onKeyDown, false);
});
bpmInputElem.addEventListener("input", onInput);
divInputElem.addEventListener("input", onInput);
minFpsInputElem.addEventListener("input", onInput);
[].forEach.call(document.getElementsByClassName("minFpsPreset"), function (elem) {
elem.addEventListener("click", onClickMinFpsPreset);
});
fpsCopyButtonElem.addEventListener("click", onClickFpsCopy);
fpsRCopyButtonElem.addEventListener("click", onClickFpsRCopy);
calculateFps();
function onInput(event) {
calculateFps();
}
function calculateFps() {
while (timelineBeatsRowElem.childElementCount > 1) {
timelineBeatsRowElem.removeChild(timelineBeatsRowElem.lastElementChild);
}
while (timelineFramesRowElem.childElementCount > 1) {
timelineFramesRowElem.removeChild(timelineFramesRowElem.lastElementChild);
}
var bpmDec = parseDecimal(bpmInputElem.value);
var dividerDec = parseDecimal(divInputElem.value);
var minFpsDec = parseDecimal(minFpsInputElem.value);
if (!applyValidationClass(bpmInputElem, bpmDec[0] > 0) |
!applyValidationClass(divInputElem, dividerDec[0] > 0) |
!applyValidationClass(minFpsInputElem, minFpsDec[0] > 0)) {
fpsOutputElem.value = "";
fpsROutputElem.value = "";
return;
}
var fpsDenom = 60 * Math.pow(10, -bpmDec[1] - dividerDec[1]);
var frameFactor = Math.ceil(fpsDenom * minFpsDec[0] / Math.pow(10, -minFpsDec[1]) / bpmDec[0] / dividerDec[0]);
var fpsNumer = bpmDec[0] * frameFactor * dividerDec[0];
fpsOutputElem.value = fpsNumer / fpsDenom;
fpsROutputElem.value = fpsNumer + "/" + fpsDenom;
var divider = dividerDec[0] / Math.pow(10, -dividerDec[1]);
if (frameFactor * divider > 480) {
var colSpan = 1;
timelineFramesRowElem.firstElementChild.classList.add("bg-secondary");
} else {
var colSpan = frameFactor;
timelineFramesRowElem.firstElementChild.classList.remove("bg-secondary");
}
timelineBeatsRowElem.firstElementChild.colSpan = colSpan;
for (var i = 1; i < divider + 1; i++) {
var cellElem = timelineBeatsRowElem.firstElementChild.cloneNode(false);
cellElem.textContent = (Math.min(i, divider) * frameFactor / (fpsNumer / fpsDenom)).toFixed(3);
if (i + 1 > divider) {
cellElem.colSpan = Math.max(Math.ceil(colSpan * divider % colSpan), 1);
}
timelineBeatsRowElem.appendChild(cellElem);
}
timelineBeatsRowElem.lastElementChild.style.width = "30px";
for (var i = 1; i <= colSpan * divider; i++) {
if (i % colSpan === 0) {
var cellElem = timelineFramesRowElem.firstElementChild.cloneNode(true);
cellElem.firstElementChild.textContent = i * frameFactor / colSpan + 1;
} else {
var cellElem = timelineFramesRowElem.firstElementChild.cloneNode(false);
}
timelineFramesRowElem.appendChild(cellElem);
}
timelineFramesRowElem.lastElementChild.classList.remove("bg-secondary");
}
function parseDecimal(s) {
var match = /^0*(\d+)(?:\.(\d*[1-9])?0*)?$/.exec(s);
if (!match) {
return [NaN, NaN];
}
var significand = parseInt((match[1] || "") + (match[2] || ""), 10);
var numDecimals = match[2] && match[2].length || 0;
return [significand, -numDecimals];
}
function applyValidationClass(inputElem, cond) {
if (cond) {
inputElem.classList.remove("is-invalid");
return true;
} else {
inputElem.classList.add("is-invalid");
return false;
}
}
function onClickMinFpsPreset(event) {
event.preventDefault();
minFpsInputElem.value = event.target.getAttribute("data-value");
calculateFps();
}
function onClickFpsCopy(event) {
fpsOutputElem.select();
document.execCommand("copy");
}
function onClickFpsRCopy(event) {
fpsROutputElem.select();
document.execCommand("copy");
}
function onKeyDown(event) {
if (event.key === "Enter") {
event.preventDefault();
var index = inputElems.indexOf(event.target);
if (event.shiftKey) {
inputElems[(index - 1 + inputElems.length) % inputElems.length].select();
} else {
inputElems[(index + 1) % inputElems.length].select();
}
}
}
</script>
</body>
</html>