-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
107 lines (95 loc) · 8.9 KB
/
index.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
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
const beepBase64 = 'SUQzBAAAAAAASVRQRTEAAAAcAAADU291bmRKYXkuY29tIFNvdW5kIEVmZmVjdHMAVFNTRQAAAA8AAANMYXZmNTguNDUuMTAwAAAAAAAAAAAAAAD/+5DAAAAAAAAAAAAAAAAAAAAAAABJbmZvAAAADwAAAAkAABBTADMzMzMzMzMzMzMzTExMTExMTExMTExmZmZmZmZmZmZmZn9/f39/f39/f39/mZmZmZmZmZmZmZmzs7Ozs7Ozs7Ozs8zMzMzMzMzMzMzM5ubm5ubm5ubm5ub//////////////wAAAABMYXZmAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQUxToXVgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD/+5LAAAAAAAEsFAAAAvpBaSs3UAMMIEAAIBAEBAQJAP//Zzs6amhf9u2bMjG3hH/u2Z0pGoFWV0QMgJ4DazM5PkTQA0sOgRG8DBgO9d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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP/7ksD/gAAAASwAAAAAAAAlgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=';
const singleBeepButton = document.getElementById('single_beep');
const rapidBeepButton = document.getElementById('rapid_beep');
const singleOscillatorButton = document.getElementById('single_oscillator');
const rapidOscillatorButton = document.getElementById('rapid_oscillator');
function getBeepFrequency() {
return parseInt(document.getElementById('beep_freq').value, 10);
}
function getBeepDuration() {
return parseInt(document.getElementById('beep_duration').value, 10);
}
function getBeepArrayBuffer() {
const beepBinary = atob(beepBase64);
const bytes = new Uint8Array(beepBinary.length);
for (let i = 0; i < beepBinary.length; i++) {
bytes[i] = beepBinary.charCodeAt(i);
}
return bytes.buffer;
}
let mp3Ctx = null;
let mp3AudioBuffer = null;
singleBeepButton.onclick = () => {
if (!mp3Ctx) {
mp3Ctx = new AudioContext({latencyHint: 'interactive'});
}
mp3Ctx.decodeAudioData(getBeepArrayBuffer())
.then((audioBuffer) => {
mp3AudioBuffer = audioBuffer;
const bufferSource = mp3Ctx.createBufferSource();
bufferSource.buffer = audioBuffer;
bufferSource.connect(mp3Ctx.destination);
bufferSource.start(0);
bufferSource.onended = () => {
bufferSource.disconnect();
mp3Ctx.close();
};
})
.catch(err => {
window.alert(err.name);
});
};
rapidBeepButton.onclick = async () => {
if (!mp3Ctx) {
mp3Ctx = new AudioContext({latencyHint: 'interactive'});
mp3AudioBuffer = await mp3Ctx.decodeAudioData(getBeepArrayBuffer());
}
let t = 0;
for (let i = 0; i < 10; i++) {
t += 50 + Math.random() * 80;
setTimeout(() => {
const bufferSource = mp3Ctx.createBufferSource();
bufferSource.buffer = mp3AudioBuffer;
bufferSource.connect(mp3Ctx.destination);
bufferSource.start();
bufferSource.onended = () => {
bufferSource.disconnect();
};
}, t);
}
};
singleOscillatorButton.onclick = () => {
const freq = getBeepFrequency();
const duration = getBeepDuration();
const oscillatorCtx = new AudioContext({ latencyHint: 'interactive' });
const oscillator = oscillatorCtx.createOscillator();
// oscillator.type = 'sine';
oscillator.type = 'square';
oscillator.frequency.setValueAtTime(freq, oscillatorCtx.currentTime); // value in Hz
oscillator.connect(oscillatorCtx.destination);
oscillator.start();
setTimeout(function () {
oscillator.stop();
oscillatorCtx.close();
}, duration);
};
rapidOscillatorButton.onclick = () => {
const freq = getBeepFrequency();
const duration = getBeepDuration();
const oscillatorCtx = new AudioContext({ latencyHint: 'interactive' });
const oscillator = oscillatorCtx.createOscillator();
oscillator.type = 'square';
oscillator.frequency.value = freq;
oscillator.connect(oscillatorCtx.destination);
let t = 0;
for (let i = 0; i < 10; i++) {
t += 500 + Math.random() * 80;
setTimeout(() => {
oscillator.start();
//oscillator.stop(oscillatorCtx.currentTime + duration);
setTimeout(() => {
oscillator.stop();
}, duration);
}, t);
}
};