-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtones.js
46 lines (38 loc) · 1.29 KB
/
tones.js
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
const AudioContextClass = (
window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext);
let contextSingleton = null;
function getAudioContext() {
if (!contextSingleton && AudioContextClass) {
// Web Audio API is available.
contextSingleton = new AudioContextClass();
}
return contextSingleton;
}
let oscillator1, oscillator2;
function dialTone(freq1, freq2){
const context = getAudioContext();
stop();
oscillator1 = context.createOscillator();
oscillator1.frequency.value = freq1;
gainNode = context.createGain ? context.createGain() : context.createGainNode();
oscillator1.connect(gainNode,0,0);
gainNode.connect(context.destination);
gainNode.gain.value = .1;
oscillator1.start ? oscillator1.start(0) : oscillator1.noteOn(0)
oscillator2 = context.createOscillator();
oscillator2.frequency.value = freq2;
gainNode = context.createGain ? context.createGain() : context.createGainNode();
oscillator2.connect(gainNode);
gainNode.connect(context.destination);
gainNode.gain.value = .1;
oscillator2.start ? oscillator2.start(0) : oscillator2.noteOn(0)
};
function stop() {
const context = getAudioContext();
oscillator1 && oscillator1.disconnect();
oscillator2 && oscillator2.disconnect();
}