-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsound.js
More file actions
27 lines (19 loc) · 987 Bytes
/
sound.js
File metadata and controls
27 lines (19 loc) · 987 Bytes
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
function makeSound(key, range){
var pitch = [329.628,349.228,369.994,391.995,415.305,440.000,466.164,493.883,523.251,554.365,587.330,622.254,659.255,698.456,739.989,783.991,830.609,880.000,932.328,987.767,1046.502,1108.731,1174.659,1244.508,1318.510,1396.913,1479.978,1567.982,1661.219,1760.000,1864.655,1975.533,2093.005];
window.AudioContext = window.AudioContext||window.webkitAudioContext;
var audioContext = new AudioContext();
var gain = audioContext.createGain();
var osciillatorNode = audioContext.createOscillator();
osciillatorNode.connect(gain);
osciillatorNode.frequency.value = pitch[key] / range;
var audioDestinationNode = audioContext.destination;
gain.gain.setValueAtTime(0, 0);
gain.gain.linearRampToValueAtTime(0.5, 0.2);
gain.gain.linearRampToValueAtTime(0, 1);
gain.connect(audioDestinationNode);
osciillatorNode.start = osciillatorNode.start || osciillatorNode.noteOn;
osciillatorNode.start();
setTimeout(function(){
osciillatorNode.stop();
}, 1000);
}