forked from MrZenW/BPC.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpc.js
220 lines (208 loc) · 6.27 KB
/
bpc.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
/**
* Author: ZenWANG
* Email: [email protected]
* Github: https://github.com/zenboss
*/
;
'use strict';
// eslint-disable-next-line no-unused-vars
var BPCStart = (function ModuleSpace() {
var frequency = (68.5 * 1e3) / 4;
/**
* the _getUndefined() function is used to avoid
* the programmer, I, make any mistake cause the
* undefined variable to be overridden
*/
function _getUndefined() { return void 0; }
function BLANK_FUNCTION() { }
function gxkSetInterval(cb) {
// cb.apply(this, Array.prototype.slice.call(arguments, 2));
// return setInterval.apply(this, arguments);
var timeoutHandle = 0;
function _process() {
if (timeoutHandle !== false) {
timeoutHandle = setTimeout(_process, 1e3 - Date.now() % 1e3);
cb();
}
}
_process();
return function() {
if (timeoutHandle !== false) {
clearTimeout(timeoutHandle);
timeoutHandle = false;
}
};
}
function toQuaternary(n, length) {
var r = Number(n).toString(4);
if (r.length < length) {
for (var i = r.length; i < length; i += 1) {
r = '0' + r;
}
}
return r;
}
function toBinary(n, length) {
var r = Number(n).toString(2);
if (r.length < length) {
for (var i = r.length; i < length; i += 1) {
r = '0' + r;
}
}
return r;
}
function getCheckCodeP3(frame) {
var binary = '';
binary += frame.P1;
binary += frame.P2;
binary += frame.hour;
binary += frame.minute;
binary += frame.dayOfWeek;
return binary.split('').filter(function(item) {
return '' + item === '1';
}).length % 2;
}
function getCheckCodeP4(frame) {
var binary = '';
binary += frame.day;
binary += frame.month;
binary += frame.year;
return binary.split('').filter(function(item) {
return '' + item === '1';
}).length % 2;
}
function generateDateInfo(time) {
time = time || Date.now();
var localTime = new Date(time);
var localOffset = localTime.getTimezoneOffset() * 60000;
var utc = time + localOffset;
var dateObj = new Date(utc + ((3600 * 1e3) * 8));
var h24 = dateObj.getHours();
var isAM = h24 < 12;
var h12 = h24 % 12;
var dayOfWeek = dateObj.getDay();
if (dayOfWeek === 0) dayOfWeek = 7;
var frame1 = {
P1: toBinary((parseInt(dateObj.getSeconds() / 20, 10)), 2),
P2: '00',
hour: toBinary(h12, 4),
minute: toBinary(dateObj.getMinutes(), 6),
dayOfWeek: toBinary(dayOfWeek, 4),
day: toBinary(dateObj.getDate(), 6),
month: toBinary(dateObj.getMonth() + 1, 4),
year: toBinary((dateObj.getFullYear() + '').slice(2), 6),
};
frame1.P3 = (isAM ? '0' : '1') + '' + getCheckCodeP3(frame1);
frame1.P4 = '0' + getCheckCodeP4(frame1);
return frame1;
}
function dateInfoTo4String(frames) {
return [
toQuaternary(parseInt(frames.P1, 2), 1),
toQuaternary(parseInt(frames.P2, 2), 1),
toQuaternary(parseInt(frames.hour, 2), 2),
toQuaternary(parseInt(frames.minute, 2), 3),
toQuaternary(parseInt(frames.dayOfWeek, 2), 2),
toQuaternary(parseInt(frames.P3, 2), 1),
toQuaternary(parseInt(frames.day, 2), 3),
toQuaternary(parseInt(frames.month, 2), 2),
toQuaternary(parseInt(frames.year, 2), 3),
toQuaternary(parseInt(frames.P4, 2), 1),
].join('');
}
function dateInfoStringToSoundCode(dateString) {
return dateString.split('').map(function(one) {
if (one === '0') return 0.1;
if (one === '1') return 0.2;
if (one === '2') return 0.3;
if (one === '3') return 0.4;
return 0;
});
}
var oscillator = null;
function reinitAudio() {
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
oscillator = audioCtx.createOscillator();
oscillator.type = 'square';
oscillator.frequency.setValueAtTime(frequency, audioCtx.currentTime);
oscillator.connect(audioCtx.destination);
}
function startAudioIfNeeded() {
if (oscillator) return;
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
oscillator = audioCtx.createOscillator();
oscillator.type = 'square';
oscillator.frequency.setValueAtTime(frequency, audioCtx.currentTime);
oscillator.connect(audioCtx.destination);
oscillator.start();
}
var stopFunction = null;
var isRunning = false;
function BPCStart(utctime, cb) {
if (true === isRunning) {
return stopFunction;
}
isRunning = true;
if ('function' === typeof utctime) {
cb = utctime;
utctime = null;
}
utctime = utctime || Date.now();
var timeDifference = utctime - Date.now();
cb = cb || BLANK_FUNCTION;
var frames = null;
var framesSoundCode = [];
var intervalHandleStopFunction = gxkSetInterval(function() {
startAudioIfNeeded();
var soundSecond = framesSoundCode.shift();
if (soundSecond === _getUndefined()) {
var second = (new Date()).getSeconds();
var modeOfSecond = second % 20;
if (modeOfSecond === 0) {
frames = generateDateInfo(Date.now() + timeDifference);
var framesString = dateInfoTo4String(frames);
framesSoundCode = dateInfoStringToSoundCode(framesString);
cb({
frames: frames,
countdown: 0,
});
} else {
oscillator.stop(0.1);
oscillator = null;
cb({
frames: frames,
countdown: 20 - modeOfSecond,
});
}
} else {
oscillator.stop();
reinitAudio();
setTimeout(function() {
oscillator.start();
}, soundSecond * 1e3);
cb({
frames: frames,
countdown: 0,
soundSecond: soundSecond,
});
}
});
stopFunction = function () {
isRunning = false;
intervalHandleStopFunction();
setTimeout(function() {
try {
oscillator.stop();
} catch (error) {
// console.error(error);
}
}, 1 * 1e3);
};
return stopFunction;
}
if ('object' === typeof module) module.exports = BPCStart;
if ('object' === typeof window) window.BPCStart = BPCStart;
if ('object' === typeof self) self.BPCStart = BPCStart;
if ('object' === typeof this) this.BPCStart = BPCStart;
return BPCStart;
})();