-
Notifications
You must be signed in to change notification settings - Fork 0
/
standardtone.html
84 lines (71 loc) · 2.52 KB
/
standardtone.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
<!DOCTYPE html>
<!-- saved from url=(0047)https://www.phpied.com/files/webaudio/osci.html -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width">
<title>oscillator.js</title>
<style>
body {
-webkit-user-select: none;
}
</style>
</head>
<body>
<button onmousedown="play(440)" onmouseup="stop()" ontouchstart="play(440)" ontouchend="stop()">440</button>
<button onmousedown="play(880)" onmouseup="stop()" ontouchstart="play(880)" ontouchend="stop()">880</button>
<br>
<input type="range" id="range" max="20000" min="20" value="10000" step="5"><button onmousedown="play(this.previousSibling.value)" onmouseup="stop()" ontouchstart="play(this.previousSibling.value)" ontouchend="stop()">you try</button>
<br>
<button onmousedown="cmajor.yo()" onmouseup="cmajor.no()" ontouchstart="cmajor.yo(440)" ontouchend="cmajor.no()">C major chord</button>
<pre id="log"></pre>
<input type="range" id="change" max="20000" min="20" value="10000" step="5"><button onclick=""></button>
<script>
var audio_context, oscillator;
function stop() {
oscillator.stop(0);
fire('stop');
}
const freq = document.getElementById('range');
function play(freq) {
oscillator = audio_context.createOscillator();
oscillator.frequency.value = freq;
oscillator.connect(audio_context.destination);
oscillator.start(0);
fire('play', oscillator.frequency.value);
}
var cmajor = {};
cmajor.yo = function () {
var oscs = [], o, i, freqs = [261.63, 329.63, 392];
freqs.forEach(function(freq) {
o = audio_context.createOscillator();
o.frequency.value = freq;
o.connect(audio_context.destination);
o.start(0);
oscs.push(o);
});
this.oscs = oscs;
fire('play', '\n - ' + freqs.join('Hz\n - '));
};
cmajor.no = function () {
this.oscs.forEach(function(o) {
o.stop(0);
});
fire('stop');
};
function fire(e, data) {
if (e === 'stop') {
log.innerHTML = '';
}
if (e === 'play') {
log.innerHTML = "Playing " + data + 'Hz';
}
}
(function init(g){
try {
audio_context = new (g.AudioContext || g.webkitAudioContext);
oscillator = audio_context.createOscillator();
} catch (e) {
alert('No web audio oscillator support in this browser');
}
}(window));
</script>
</body></html>