-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
166 lines (152 loc) · 3.84 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
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
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<meta name="author" content"Jessie Schalles" />
<meta name="description" content="Accurately recreate DTMF tones using the Web Audio API." />
<meta name="keywords" content="Jessie Schalles, front end engineer, web audio API, css, javascript, synthesizer, euphonium, portfolio" />
<meta name="viewport" content="width=device-width; initial-scale=1; maximum-scale=1.0;" />
<title>Web Audio Phone</title>
<link href='http://fonts.googleapis.com/css?family=Josefin+Slab:700|Josefin+Sans:400' rel='stylesheet' type='text/css' />
<link rel="stylesheet" href="main.css" />
</head>
<body>
<h1>Web Audio Phone</h1>
<div class="container">
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>*</li>
<li>0</li>
<li>#</li>
</ul>
<div class="speaker">
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
<div class="dot"></div>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-37469464-1']);
_gaq.push(['_setDomainName', 'github.com']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<script type="text/javascript">
$('li').on('mousedown', function() {
$('.dot').addClass('selected');
});
$('li').on('mouseup', function() {
$('.dot').removeClass('selected');
});
/*
1209Hz 1336Hz 1477Hz
697Hz 1 2 3
770Hz 4 5 6
852Hz 7 8 9
941Hz * 0 #
*/
var Keypad = {
init: function() {
this.cacheEls();
this.bindEvents();
},
cacheEls: function() {
this.button = $('.container li');
this.ctx = new AudioContext();
this.osc1;
this.osc2;
this.one = [1209,1336,1477];
this.two = [697,770,852,941];
this.keys1 = [49,50,51,52,53,54,55,56,57,42,48,35];
this.num;
this.val;
this.flag2 = 1;
this.doc = $(document);
},
bindEvents: function() {
this.button.on('mousedown', this.handlePlay);
this.doc.on('keypress', this.handlePlay);
this.button.on('mouseup', this.stop);
this.doc.on('keyup', this.stop);
},
handlePlay: function(e) {
var that = Keypad;
if (e.type === 'keypress') {
that.num = e.keyCode;
that.q = that.keys1.indexOf(that.num);
while ((that.flag2 === 1 && that.keys1.indexOf(e.keyCode) !== -1)) {
that.flag2 = 0;
that.play(e);
}
}
else {
that.q = $(this).index();
that.play(e);
}
},
play: function(e) {
var that = Keypad;
that.osc1 = that.ctx.createOscillator();
that.osc2 = that.ctx.createOscillator();
var flag = 0,
p = 0;
i = 0,
j = 0,
k = 0,
one = [1209, 1336, 1477],
two = [697, 770, 852, 941],
len1 = one.length,
len2 = two.length;
for (j=0; j<len2; j++) {
for (k=0; k<len1; k++) {
if (p === that.q) {
p++;
flag = 1;
break;
}
p++;
}
if (flag === 1) {
// console.log('outer: ', p, that.q, ' ', one[k] , two[j]);
that.osc1.frequency.value = one[k];
that.osc2.frequency.value = two[j];
break;
}
}
that.osc1.connect(that.ctx.destination);
that.osc2.connect(that.ctx.destination);
that.osc1.start(0);
that.osc2.start(0);
},
stop: function() {
var that = Keypad;
that.osc1.stop(0);
that.osc2.stop(0);
that.flag2 = 1;
}
};
Keypad.init();
</script>
</body>
</html>