-
Notifications
You must be signed in to change notification settings - Fork 21
/
jjy.js
265 lines (228 loc) · 7.61 KB
/
jjy.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
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
258
259
260
261
262
263
264
265
(function() {
var freq = 13333;
var ctx;
var signal;
var AudioContext = window.AudioContext || window.webkitAudioContext;
// うるう秒挿入日一覧(日本時)
var plus_leapsecond_list = [
new Date(2017, 0, 1, 9)
];
// うるう秒 +1:一ヶ月以内に挿入 -1:一ヶ月以内に削除
function getleapsecond() {
var now = Date.now();
for(var i = 0; i < plus_leapsecond_list.length; i++) {
var diff = plus_leapsecond_list[i] - now;
if (diff > 0 && diff <= 31*24*60*60*1000) {
return 1;
}
}
return 0;
}
function schedule(date, summer_time) {
var now = Date.now();
var start = date.getTime();
var offset = (start - now) / 1000 + ctx.currentTime;
var minute = date.getMinutes();
var hour = date.getHours();
var fullyear = date.getFullYear();
var year = fullyear % 100;
var week_day = date.getDay();
var year_day = (new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime() - new Date(date.getFullYear(), 0, 1).getTime()) / (24*60*60*1000) + 1;
var array = [];
var leapsecond = getleapsecond();
// 毎分s秒の位置のマーカーを出力する
function marker(s) {
array.push(0.2);
var t = s + offset;
if (t < 0) return;
var osc = ctx.createOscillator();
osc.type = "square";
osc.frequency.value = freq;
osc.start(t);
osc.stop(t + 0.2);
osc.connect(ctx.destination);
}
// パリティービット
var pa;
// bitを出力し、パリティビットを更新する
function bit(s, value, weight) {
var b = value >= weight;
value -= b ? weight : 0;
pa += b ? 1 : 0;
array.push(b ? 0.5 : 0.8);
var t = s + offset;
if (t < 0) return value;
var osc = ctx.createOscillator();
osc.type = "square";
osc.frequency.value = freq;
osc.start(t);
osc.stop(t + (b ? 0.5 : 0.8));
osc.connect(ctx.destination);
return value;
}
marker(0); // マーカー(M)
// 分
pa = 0;
minute = bit(1, minute, 40);
minute = bit(2, minute, 20);
minute = bit(3, minute, 10);
minute = bit(4, minute, 16);
minute = bit(5, minute, 8);
minute = bit(6, minute, 4);
minute = bit(7, minute, 2);
minute = bit(8, minute, 1);
var pa2 = pa;
marker(9); // P1
// 時
pa = 0;
hour = bit(10, hour, 80);
hour = bit(11, hour, 40);
hour = bit(12, hour, 20);
hour = bit(13, hour, 10);
hour = bit(14, hour, 16);
hour = bit(15, hour, 8);
hour = bit(16, hour, 4);
hour = bit(17, hour, 2);
hour = bit(18, hour, 1);
var pa1 = pa;
marker(19); // P2
// 1月1日からの通算日
year_day = bit(20, year_day, 800);
year_day = bit(21, year_day, 400);
year_day = bit(22, year_day, 200);
year_day = bit(23, year_day, 100);
year_day = bit(24, year_day, 160);
year_day = bit(25, year_day, 80);
year_day = bit(26, year_day, 40);
year_day = bit(27, year_day, 20);
year_day = bit(28, year_day, 10);
marker(29); // P3
year_day = bit(30, year_day, 8);
year_day = bit(31, year_day, 4);
year_day = bit(32, year_day, 2);
year_day = bit(33, year_day, 1);
bit(34, 0, 1); // 0
bit(35, 0, 1); // 0
bit(36, pa1 % 2, 1);
bit(37, pa2 % 2, 1);
bit(38, 0, 1); // SU1
marker(39); // P4
// SU2
if (summer_time) {
bit(40, 1, 1);
} else {
// 夏時間実施中(6日以内に夏時間から通常時間への変更なし)
bit(40, 0, 1);
}
// 年
year = bit(41, year, 80);
year = bit(42, year, 40);
year = bit(43, year, 20);
year = bit(44, year, 10);
year = bit(45, year, 8);
year = bit(46, year, 4);
year = bit(47, year, 2);
year = bit(48, year, 1);
marker(49); // P5
// 曜日
week_day = bit(50, week_day, 4);
week_day = bit(51, week_day, 2);
week_day = bit(52, week_day, 1);
// うるう秒
if (leapsecond === 0) {
// うるう秒なし
bit(53, 0, 1); // 0
bit(54, 0, 1); // 0
} else if (leapsecond > 0) {
// 正のうるう秒
bit(53, 1, 1); // 1
bit(54, 1, 1); // 1
} else {
// 負のうるう秒
bit(53, 1, 1); // 1
bit(54, 0, 1); // 0
}
bit(55, 0, 1); // 0
bit(56, 0, 1); // 0
bit(57, 0, 1); // 0
bit(58, 0, 1); // 0
marker(59); // P0
return array;
}
var intervalId;
var summer_time_input = document.getElementById("summer-time")
function start() {
ctx = new AudioContext();
var now = Date.now();
var t = Math.floor(now / (60 * 1000)) * 60 * 1000;
var next = t + 60 * 1000;
var delay = next - now - 1000; // 毎分0秒ピッタリの少し前にタイマーをセットする
if (delay < 0) {
t = next;
delay += 60 * 1000;
}
signal = schedule(new Date(t), summer_time_input.checked);
// HACK: timeout発火前にキャンセルする
intervalId = setTimeout(function() {
interval();
intervalId = setInterval(interval, 60 * 1000);
}, delay);
function interval() {
t += 60 * 1000;
signal = schedule(new Date(t), summer_time_input.checked);
}
}
function stop() {
if (intervalId) {
clearInterval(intervalId);
intervalId = null;
}
if (ctx) {
ctx.close();
ctx = null;
}
signal = undefined;
}
var control_button = document.getElementById("control-button");
var play_flag = false;
control_button.addEventListener('click', function() {
if (play_flag) {
control_button.innerText = "Start";
play_flag = false;
stop();
} else {
control_button.innerText = "Stop";
play_flag = true;
start();
}
});
var nowtime = document.getElementById('time');
var canvas = document.getElementById('canvas');
var ctx2d = canvas.getContext('2d');
var w = canvas.width;
var h = canvas.height;
render();
function render() {
nowtime.innerText = new Date().toString();
var i;
ctx2d.clearRect(0, 0, w, h);
if (!signal) {
requestAnimationFrame(render);
return;
}
var now = Math.floor(Date.now() / 1000) % 60;
for (i = 0; i < signal.length; i++) {
if (i == now) {
if (signal[i] < 0.3) ctx2d.fillStyle = "#FF0000";
else if (signal[i] < 0.7) ctx2d.fillStyle = "#FFFF00";
else ctx2d.fillStyle = "#00FF00";
} else {
if (signal[i] < 0.3) ctx2d.fillStyle = "#7F0000";
else if (signal[i] < 0.7) ctx2d.fillStyle = "#7F7F00";
else ctx2d.fillStyle = "#007F00";
}
ctx2d.fillRect((i%30)*30, Math.floor(i/30)*100, 30 * signal[i], 80);
}
requestAnimationFrame(render);
}
})();