-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
98 lines (80 loc) · 2.61 KB
/
main.js
File metadata and controls
98 lines (80 loc) · 2.61 KB
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
var mic;
function validateForm() {
var min = document.forms["options"]["interval-min"].value;
var max = document.forms["options"]["interval-max"].value;
var delay = document.forms["options"]["activation-delay"].value;
if((delay == "") || (delay > 60)) {
alert("Please enter a valid delay.");
return false;
}
if(min == ""){
alert("Please enter a minimum interval value.");
return false;
}
if(max == ""){
alert("Please enter a maximum interval value.");
return false;
}
if((isNaN(min)) || (isNaN(max)) || (isNaN(delay))){
alert("Must input numbers.");
return false;
}
if((min > max) || (min < 0) || (max < 0) || ((min - max) > 60) || ((max - min) > 60)){
alert("Please enter a valid interval.");
return false;
}
else{
document.getElementById("timer").classList.remove('hidden'); // unhides stopwatch upon form submission
document.getElementById("timer").classList.add('not-hidden');
document.getElementById("submit").disabled = true; // grey out start button
findInterval();
}
}
function findInterval() {
var min = document.forms["options"]["interval-min"].value;
var max = document.forms["options"]["interval-max"].value;
var delay = document.forms["options"]["activation-delay"].value;
min = Math.ceil(min);
max = Math.ceil(max);
var result = Math.floor(Math.random() * (max - min + 1)) + min; // calculates random inclusive integer between intervals
var total = parseInt(delay) + parseInt(result);
setTimeout(doBeep, (total * 1000)); // setTimeout in ms
event.preventDefault();
function doBeep (){
document.getElementById("display").innerHTML = total;
callStopwatch();
}
}
function callStopwatch() {
var timer = document.getElementById('timer');
var toggleBtn = document.getElementById('submit');
var resetBtn = document.getElementById('reset');
var watch = new Stopwatch(timer);
watch.start();
if(draw()){
watch.stop();
}
resetBtn.addEventListener('click', function() {
watch.stop();
watch.reset();
document.getElementById("submit").disabled = false;
});
}
function setup() {
mic = new p5.AudioIn();
mic.start();
}
function touchStarted() {
if (getAudioContext().state !== 'running') {
getAudioContext().resume();
}
}
function draw() {
let flag = false;
var vol = mic.getLevel();
if(vol > 0.2){
console.log("true"); //created to check whether sound detection is working
flag = true;
}
return flag;
}