-
Notifications
You must be signed in to change notification settings - Fork 10
/
example.js
59 lines (50 loc) · 1.51 KB
/
example.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
var audioContext = new AudioContext()
var bopper = require('./')(audioContext)
// save a reference on the window to avoid garbage collection
window.scheduler = bopper
var playback = [
{position: 0, length: 0.1},
{position: 1, length: 0.1},
{position: 2, length: 0.1},
{position: 3, length: 0.1},
{position: 3.5, length: 0.1},
{position: 4, length: 0.1},
{position: 5, length: 0.1},
{position: 6, length: 0.1},
{position: 7, length: 0.1},
{position: 7+1/3, length: 0.1},
{position: 7+2/3, length: 0.1},
{position: 8, length: 0.1},
{position: 9, length: 0.1},
{position: 10, length: 0.1},
{position: 11, length: 0.1},
{position: 11.5, length: 0.1},
{position: 12, length: 0.1},
{position: 13, length: 0.1},
{position: 14, length: 0.1},
{position: 15, length: 0.1},
{position: 15+1/3, length: 0.1},
{position: 15+2/3, length: 0.1}
]
// emits data roughly every 20ms
bopper.on('data', function(schedule){
// schedule: from, to, time, beatDuration
playback.forEach(function(note){
if (note.position >= schedule.from && note.position < schedule.to){
var delta = note.position - schedule.from
var time = schedule.time + delta
var duration = note.length * schedule.beatDuration
play(time, duration)
}
})
})
function play(at, duration){
var oscillator = audioContext.createOscillator()
oscillator.connect(audioContext.destination)
oscillator.start(at)
oscillator.stop(at+duration)
}
bopper.setTempo(120)
setTimeout(function(){
bopper.start()
}, 500)