forked from mathiasvr/audio-oscilloscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio-element.js
31 lines (24 loc) · 830 Bytes
/
audio-element.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
var audioContext = new window.AudioContext()
// setup canvas
var canvas = document.createElement('canvas')
canvas.width = window.innerWidth
canvas.height = window.innerHeight - 50
document.body.appendChild(canvas)
// setup audio element
var audioElement = document.createElement('audio')
audioElement.controls = true
audioElement.autoplay = true
audioElement.src = 'audio.mp3'
document.body.appendChild(audioElement)
// create source from html5 audio element
var source = audioContext.createMediaElementSource(audioElement)
// attach oscilloscope
var scope = new Oscilloscope(source)
// reconnect audio output to speakers
source.connect(audioContext.destination)
// customize drawing options
var ctx = canvas.getContext('2d')
ctx.lineWidth = 2
ctx.strokeStyle = '#ffffff'
// start default animation loop
scope.animate(ctx)