forked from bfirsh/dynamicaudio.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.html
49 lines (42 loc) · 1.51 KB
/
demo.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
<!DOCTYPE html>
<html>
<head>
<title>dynamicaudio demo</title>
<script type="text/javascript" src="dynamicaudio-src.js"></script>
</head>
<body>
<input type="text" size="4" id="freq" value="440"><label for="hz">Hz</label>
<button onclick="generateWaveform()">set</button>
<button onclick="start()">play</button>
<button onclick="stop()">stop</button>
<script type="text/javascript" charset="utf-8">
var sampledata = [];
var freq = 440;
var interval = null;
var dynamicaudio = new DynamicAudio({'swf': 'dynamicaudio.swf'});
function start() {
interval = setInterval(function() {
var n = Math.ceil(freq / 100) * 2;
for (var i=0; i < n; i++) {
dynamicaudio.write(sampledata);
}
}, 10);
}
function stop() {
if (interval != null) {
clearInterval(interval);
interval = null;
}
}
(function generateWaveform() {
freq = parseFloat(document.getElementById("freq").value);
// we're playing at 44.1kHz, so figure out how many samples
// will give us one full period
var samples = 44100 / freq;
sampledata = Array(Math.round(samples));
for (var i=0; i < sampledata.length; i++) {
sampledata[i] = Math.sin(2*Math.PI * (i / sampledata.length));
}
})();
</script>
</body>