-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexperiment22.html
129 lines (116 loc) · 4.07 KB
/
experiment22.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
<html>
<head>
<title>Making Things Smart Experiment 22</title>
</head>
<body>
<p>Remember to update xxx_MY_KEY_HERE_xxx with your key from IFTTT!</p>
<pre id="log"></pre>
<script>
var logElement = document.getElementById("log");
console.log = function(s) {
logElement.innerHTML += s+"\n";
}
// The threshold for detecting a change to a 1 or a 0 in the signal
var THRESH = 0.01;
// Do we think the input is a 1 or a 0?
var currentState = 0;
// How many samples have we been in this state (in samples)?
var timeInState = 0;
var currentCode = "";
// the last code we received
var lastCode;
function gotCode(code) {
console.log("Got code "+code);
// Make sure we only call this when we get a new code
if (lastCode == code || code.length<20) return;
lastCode = code;
// Now send a message to IFTTT
var oReq = new XMLHttpRequest();
oReq.addEventListener("load", function() {
console.log("Got response: "+this.responseText)
});
oReq.open("POST", "https://maker.ifttt.com/trigger/infrared/with/key/"+
"xxx_MY_KEY_HERE_xxx"+
"?value1=_"+code);
oReq.send();
}
// Called when the input changes state
function changedState(newState, timePassed) {
if (timePassed > 0.02) {
// let's assume there was a gap. Handle the last code, and reset it
if (currentCode!="") gotCode(currentCode);
currentCode = "";
}
// Add a bit only when the signal was high (it's now 0)
// Since in the last code, 'Hi' was the signal
// that changed while 'Lo' was constant
if (newState == 0) {
if (timePassed > 0.001) currentCode += "1";
else currentCode += "0";
}
}
function processAudio(e) {
var data = e.inputBuffer.getChannelData(0);
// Now search for changes in value
for (var i=0;i<data.length;i++) {
// Did it suddely go high? it's a 1
if (currentState==0 && data[i]>THRESH) {
currentState=1;
changedState(1, timeInState / e.inputBuffer.sampleRate);
timeInState = 0;
}
// Did it suddely go low? it's a 0
if (currentState==1 && data[i]<-THRESH) {
currentState=0;
changedState(0, timeInState / e.inputBuffer.sampleRate)
timeInState = 0;
}
timeInState++;
}
if (timeInState > 2000) {
if (currentCode!="") gotCode(currentCode);
currentCode = "";
}
}
function startRecord() {
window.AudioContext = window.AudioContext || window.webkitAudioContext;
if (!window.AudioContext) {
console.log("No window.AudioContext");
return; // no audio available
}
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
if (!navigator.getUserMedia) {
console.log("No navigator.getUserMedia");
return; // no audio available
}
var context = new AudioContext();
var userMediaStream;
var inputNode = context.createScriptProcessor(4096, 1/*in*/, 1/*out*/);
window.dontGarbageCollectMePlease = inputNode;
inputNode.onaudioprocess = processAudio;
navigator.getUserMedia({
video:false,
audio:{
mandatory:[],
optional:[{ echoCancellation:false },
{ googEchoCancellation: false },
{ googAutoGainControl: false },
{ googNoiseSuppression: false },
{ googHighpassFilter: false }
,{ sampleRate:22050 /* 44100 */ }]
}
}, function(stream) {
var inputStream = context.createMediaStreamSource(stream);
inputStream.connect(inputNode);
inputNode.connect(context.destination);
console.log("Record start successful");
}, function(e) {
console.log('getUserMedia error', e);
});
}
startRecord();
</script>
</body>
</html>