-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoscillator.html
More file actions
244 lines (166 loc) · 5.01 KB
/
oscillator.html
File metadata and controls
244 lines (166 loc) · 5.01 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<html>
<head>
<meta charset="utf-8">
<meta name="Description" CONTENT="A simple page and site that plays a tone at 440 hz, also known as A 440">
<title>A-440</title>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
/*
Thanks to Boris @ http://www.html5rocks.com/en/tutorials/webaudio/intro/ !
*/
$(document).ready(function(){
var iOS = ( navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false );
var context = null;
var oscillator = null;
var gain = null;
var analyser = null;
var playing = false;
window.AudioContext = window.AudioContext || window.webkitAudioContext;
context = new AudioContext();
$('#container,.center').click(function(e) {
var offset = $(this).offset();
//console.log(e.clientX - offset.left);
//console.log(e.clientY - offset.top);
var frequency = e.clientX - offset.left;
var amp = e.clientY - offset.top;
if (playing) {
//dbl-click to stop
return;
}
gainNode = context.createGain();
gainNode.gain.value = 0.5; //initial value
gainNode.connect( context.destination );
analyser = context.createAnalyser();
analyser.fftSize = 1024;
analyser.smoothingTimeConstant = 0.9;
analyser.connect( context.destination );
oscillator = context.createOscillator();
oscillator.connect( analyser ); //was gainNode
oscillator.frequency.value = frequency/2;
oscillator.detune.value = 0;
oscillator.type = oscillator.SINE;
oscillator.start(0);
playing = true;
$('#a').hide();
$('#b,#c').show();
requestAnimFrame( visualize.bind() ) ;
});
$('#container,.center').mousemove( function(e){
_gain = ( e.clientY );
//console.log( (Math.abs(_gain) * -1 ) );
if(oscillator){
oscillator.frequency.value = e.pageX;
}
});
$('#container,.center').dblclick( function(e){
oscillator ? oscillator.stop(0) : null;
playing = false;
$('#b,#c').hide();
$('#a').show();
});
$('#b,#c').hide();
visualize = function() {
var canvas = $('#monitor');
canvas = document.querySelector('canvas');
//console.log(canvas.context);
var HEIGHT = '120';
var WIDTH = '600';
canvas.width = WIDTH;
canvas.height = HEIGHT;
var drawContext = canvas.getContext('2d');
var times = new Uint8Array( analyser.frequencyBinCount );
analyser.getByteTimeDomainData(times);
for (var i = 0; i < times.length; i++) {
var value = times[i];
var percent = value / 256;
var height = HEIGHT * percent;
var offset = HEIGHT - height - 1;
var barWidth = WIDTH/times.length;
drawContext.fillStyle = 'blue';
drawContext.fillRect(i * barWidth, offset, 2, 2);
}
requestAnimFrame( visualize.bind() ) ;
};
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
}); //end document.ready(...)
</script>
<style type="text/css">
@import url(http://fonts.googleapis.com/css?family=Over+the+Rainbow);
body {
background-color: #fff;
font-family: 'Over the Rainbow';
font-size: 2em;
text-align: center;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
cursor: default;
}
.canvas {
width: 100%;
height: auto;
bottom: 0px;
top: 0px;
left: 0;
position: absolute;
}
.center {
font-family: 'Over the Rainbow';
font-size: 2em;
position: fixed;
border: 0px ridge gray;
top: 50%;
left: 55%;
margin-top: -200px;
margin-left: -400px;
cursor: inherit;
}
#monitor {
position: fixed;
border: 0px dotted gray;
top: 90%;
left: 56%;
margin-top: -200px;
margin-left: -400px;
cursor: inherit;
z-index: -1;
}
</style>
</head>
<body>
<div class="canvas" id="container" style="width:100%;height:100%">
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="smallGrid" width="8" height="8" patternUnits="userSpaceOnUse">
<path d="M 8 0 L 0 0 0 8" fill="none" stroke="#eaeaea" stroke-width="0.5"/>
</pattern>
<pattern id="grid" width="80" height="80" patternUnits="userSpaceOnUse">
<rect width="80" height="80" fill="url(#smallGrid)"/>
<path d="M 80 0 L 0 0 0 80" fill="none" stroke="#eaeaea" stroke-width="1"/>
</pattern>
</defs>
<rect width="100%" height="100%" fill="url(#grid)" />
</svg>
</div>
<div class="center">
<span id="a"> Click anywhere to start </span>
<span id="b"> Move your mouse </span><br />
<span id="c"> Double-click to shut up </span>
</div>
<canvas id="monitor" />
</body>
</html>