-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
133 lines (103 loc) · 3.4 KB
/
app.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
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var audioContext = new AudioContext();
var sampleBis = document.querySelector('#sampleBis');
var initBt = document.querySelector('#init');
var timeIt = document.querySelector('#time');
var playBt = document.querySelector('#play');
var stopBt = document.querySelector('#stop');
var collectBt = document.querySelector('#collect');
var val1 = document.querySelector('#val1');
var val2 = document.querySelector('#val2');
var val4 = document.querySelector('#val4');
var val5 = document.querySelector('#val5');
var val6 = document.querySelector('#val6');
var val7 = document.querySelector('#val7');
var evt1 = document.querySelector('#evt1');
var evt2 = document.querySelector('#evt2');
var initTime;
var data = [['x'], ['Diff CurrentTime HTML5 Web vs. Audio API']];
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
document.write('<a id="init" ontouchstart="javascript:sndInit();"></a>');
function sndInit(){
sampleBis.play();
sampleBis.pause();
document.getElementById('init').style.display = 'none';
};
};
function getChart(data){
var chart = c3.generate({
bindto: '#chart',
data: {
x: 'x',
xFormat: '%Y-%m-%d %H:%M:%S.%L',
columns: data
},
axis: {
x: {
type: 'timeseries',
tick: {
format: '%Y-%m-%d %H:%M:%S.%L'
}
}
}
});
return;
}
getChart(data);
val2.textContent = audioContext.currentTime;
playBt.onclick = function(evt){
data = [
['x'],
['diff']
];
initTime = audioContext.currentTime;
sampleBis.currentTime = 0;
sampleBis.play();
};
stopBt.onclick = function(evt){
sampleBis.pause();
};
timeIt.onclick = function(evt){
var diff = audioContext.currentTime-initTime;
val1.textContent = sampleBis.currentTime;
val2.textContent = audioContext.currentTime;
val4.textContent = diff;
var currentTimeDiff = sampleBis.currentTime-diff;
val5.textContent = currentTimeDiff;
var now = new Date();
var ms = now.getMilliseconds();
if(now.getMilliseconds() < 100){
ms = '0'+ms;
}
var d = now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds()+"."+ms;
data[0].push(d);
data[1].push(currentTimeDiff);
getChart(data);
val6.textContent = average(data[1].slice(1));
val7.textContent = standardDeviation(data[1].slice(1));
};
var collId;
collectBt.onchange = function(evt){
console.log(evt.target.checked);
if(evt.target.checked){
collId = setInterval(timeIt.onclick, 500);
}else{
clearInterval(collId);
}
};
var initFunction = function() {
var buffer = audioContext.createBuffer(1, 1, 22050);
var source = audioContext.createBufferSource();
source.buffer = buffer;
source.connect(audioContext.destination);
source.start(0);
val2.textContent = audioContext.currentTime;
};
initBt.onclick = initFunction;
initBt.addEventListener("touchstart", initFunction, false);
sampleBis.addEventListener("loadstart", function() {
evt2.textContent = "loadstart";
}, true);
sampleBis.addEventListener("loadeddata", function() {
evt2.textContent = "loadeddata";
}, true);