-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_context.html
40 lines (34 loc) · 1.25 KB
/
audio_context.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
<html>
<head></head>
<body>
<button onclick="onclickRecord()">Record</button>
<button onclick="play()">Stop & Play</button>
<audio></audio>
<script>
const HIGHEST_FREQUENCY = 1500;
const LOWEST_FREQUENCY = 125;
let audioStream = undefined;
let audioBufferSource = undefined;
function onclickRecord(e) {
audioStream = undefined;
navigator.mediaDevices.getUserMedia({audio: true}).then((stream) => {
audioStream = stream;
const audioContext = new AudioContext();
const bandPassFilter = audioContext.createBiquadFilter();
bandPassFilter.type = 'bandpass';
const bandPassCenterFrequency = (HIGHEST_FREQUENCY + LOWEST_FREQUENCY) / 2;
bandPassFilter.value = bandPassCenterFrequency;
bandPassFilter.Q = HIGHEST_FREQUENCY - bandPassCenterFrequency;
const mediaStreamSource = audioContext.createMediaStreamSource(audioStream);
mediaStreamSource.connect(bandPassFilter);
bandPassFilter.connect(audioContext.destination);
});
}
function play() {
const audio = document.querySelector('audio');
audio.srcObject = audioStream;
audio.play();
}
</script>
</body>
</html>