-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sound.html
162 lines (159 loc) · 5.54 KB
/
Sound.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
<html>
<head>
<title>Sound -</title>
</head>
<body>
<div>Frequence: <span id="frequency"></span></div>
<button type="button" id="play" onclick="play()">Play</button>
<button type="button" id="pause" onclick="pause()">Pause</button>
<div>
<table>
<tr>
<td>Space - on/off</td>
<td>
</td>
</tr>
<tr>
<td>a - Frequency---</td>
<td>
<button type="button" id="a" onclick="setFrequence(keys[0]);">---</button>
</td>
</tr>
<tr>
<td>z - Frequency+++</td>
<td>
<button type="button" id="z" onclick="setFrequence(keys[1])">+++</button>
</td>
</tr>
<tr>
<td>e - Frequency--</td>
<td>
<button type="button" id="e" onclick="setFrequence(keys[2])">--</button>
</td>
</tr>
<tr>
<td>r - Frequency++</td>
<td>
<button type="button" id="r" onclick="setFrequence(keys[3])">++</button>
</td>
</tr>
<tr>
<td>t - Frequency-</td>
<td>
<button type="button" id="t" onclick="setFrequence(keys[4])">-</button>
</td>
</tr>
<tr>
<td>y - Frequency+</td>
<td>
<button type="button" id="y" onclick="setFrequence(keys[5])">+</button>
</td>
</tr>
</table>
<p>
<a href="https://gist.github.com/steventhebrave/7c16a72fb940b05b5e5218390418b5bf">Source</a> :
<a href="https://gist.github.com/steventhebrave">Author</a>
</p>
</div>
<script type="text/javascript">
var audioCtx = new(window.AudioContext || window.webkitAudioContext)();
var oscillatorNode = audioCtx.createOscillator();
var gainNode = audioCtx.createGain();
var mute = true;
var frequency = 500;
var direction = "";
var volume = 0.8;
var speed = 1;
var grossTune = 5;
var mediumTune = 0.5;
var fineTune = 0.05;
var playButton = document.getElementById("play");
var pauseButton = document.getElementById("pause");
pauseButton.style.visibility = 'hidden';
var keys = [
// {key:81, direction:"down", tune:grossTune, val: -5}, //q
// {key:87, direction:"up", tune:grossTune, val: 5}, //w
// {key:69, direction:"down", tune:mediumTune, val: -1},//e
// {key:82, direction:"up", tune:mediumTune, val: 1}, //r
// {key:84, direction:"down", tune:fineTune, val: -0.25}, //t
// {key:89, direction:"up", tune:fineTune, val: 0.25} //y
{ key: 65, direction: "down", tune: grossTune, val: -5 }, //a
{ key: 90, direction: "up", tune: grossTune, val: 5 }, //z
{ key: 69, direction: "down", tune: mediumTune, val: -1 }, //e
{ key: 82, direction: "up", tune: mediumTune, val: 1 }, //r
{ key: 84, direction: "down", tune: fineTune, val: -0.25 }, //t
{ key: 89, direction: "up", tune: fineTune, val: 0.25 } //y
];
var aButton = document.getElementById("a");
var zButton = document.getElementById("z");
var eButton = document.getElementById("e");
var rButton = document.getElementById("r");
var tButton = document.getElementById("t");
var yButton = document.getElementById("y");
function play() {
playButton.style.visibility = 'hidden';
pauseButton.style.visibility = 'visible';
gainNode.gain.value = volume;
mute = false;
}
function pause() {
playButton.style.visibility = 'visible';
pauseButton.style.visibility = 'hidden';
gainNode.gain.value = 0;
mute = true;
}
pause();
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' +
'([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
}
//to use: add this a the end of the url "?freq=<num>" and <num> is a float number
var newFreq = parseFloat(getURLParameter('freq'));
if (newFreq){
var frequency = newFreq;
}
else{
var frequency = 500;
}
oscillatorNode.connect(gainNode);
gainNode.connect(audioCtx.destination)
oscillatorNode.start();
oscillatorNode.frequency.value = frequency;
gainNode.gain.value = 0;
(function manualLoop() {
setTimeout(function() {
manualLoop();
if (direction == "up") {
frequency += speed;
}
if (direction == "down") {
frequency -= speed;
}
oscillatorNode.frequency.value = frequency;
document.getElementById('frequency').innerHTML = frequency.toFixed(2);
}, 40)
}());
document.addEventListener('keydown', function(event) {
if (event.keyCode == 32) { // space bar
if (mute) {
play();
} else {
pause();
}
}
for (var i = keys.length - 1; i >= 0; i--) {
if (event.keyCode == keys[i].key) {
direction = keys[i].direction;
speed = keys[i].tune;
}
}
});
function setFrequence(k) {
frequency += k.val;
}
document.addEventListener('keyup', function(event) {
direction = "";
});
</script>
</body>
</html>