-
Notifications
You must be signed in to change notification settings - Fork 13
/
microphone.js
32 lines (26 loc) · 959 Bytes
/
microphone.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
function startExample () {
// shim
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia
const audioContext = new window.AudioContext()
// setup canvas
const canvas = document.createElement('canvas')
canvas.width = window.innerWidth
canvas.height = window.innerHeight
document.body.appendChild(canvas)
// customize drawing options
const ctx = canvas.getContext('2d')
ctx.lineWidth = 2
ctx.strokeStyle = '#ffffff'
// get user microphone
const constraints = { video: false, audio: true }
navigator.getUserMedia(constraints, function (stream) {
const source = audioContext.createMediaStreamSource(stream)
// attach oscilloscope
const scope = new Oscilloscope(source)
// start default animation loop
scope.animate(ctx)
}, function (error) {
console.error('getUserMedia error:', error)
})
}