-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
90 lines (68 loc) · 2.06 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Blow</title>
</head>
<body>
<h1 id="blow"></h1>
<p>Blow on your Mic and enjoy !</p>
<button onclick="startRecording(this);">Listen</button>
<button onclick="stopRecording(this);" disabled>stop</button>
<script>
var audio_context;
var recorder;
function startUserMedia(stream) {
var input = audio_context.createMediaStreamSource(stream);
var micPower = 0;
var ALPHA = 0.5;
var i = 0;
var config = {};
config.numChannels = 1;
config.recordingCallback = function(channelData){
//console.log(channelData);
var instantaneousPower = channelData[0];
// low-pass filter
micPower = ALPHA * instantaneousPower + (1.0 - ALPHA) * micPower;
if(Math.abs(micPower) > 0.01){
i++;
// If low-pass filter condition is fulfilled >= 5 times
if(i > 5){
console.log('user is blowing !');
document.getElementById('blow').innerHTML = "User is blowing !";
}
} else {
i = 0;
document.getElementById('blow').innerHTML = "";
}
};
recorder = new Recorder(input, config);
}
function startRecording(button) {
recorder && recorder.record();
button.disabled = true;
button.nextElementSibling.disabled = false;
}
function stopRecording(button) {
recorder && recorder.stop();
button.disabled = true;
button.previousElementSibling.disabled = false;
recorder.clear();
}
window.onload = function init() {
try {
// webkit shim
window.AudioContext = window.AudioContext || window.webkitAudioContext;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
window.URL = window.URL || window.webkitURL;
audio_context = new AudioContext;
} catch (e) {
alert('No web audio support in this browser!');
}
navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
});
};
</script>
<script src="recorder.js"></script>
</body>
</html>