-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
162 lines (123 loc) · 3.26 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var Stream = require('stream')
var Event = require('geval')
var workerTimer = require('worker-timer')
var inherits = require('util').inherits
module.exports = Bopper
function Bopper(audioContext){
if (!(this instanceof Bopper)){
return new Bopper(audioContext)
}
var self = this
Stream.call(this)
this.readable = true
this.writable = false
this.context = audioContext
var cycleLength = (1 / audioContext.sampleRate) * 1024
workerTimer.setInterval(bopperTick.bind(this), cycleLength * 1000)
var tempo = 120
this._state = {
lastTo: 0,
lastEndTime: 0,
playing: false,
bpm: tempo,
beatDuration: 60 / tempo,
increment: (tempo / 60) * cycleLength,
cycleLength: cycleLength,
preCycle: 2
}
// frp version
this.onSchedule = Event(function(broadcast){
self.on('data', broadcast)
})
}
inherits(Bopper, Stream)
var proto = Bopper.prototype
proto.start = function(){
this._state.playing = true
this.emit('start')
}
proto.stop = function(){
this._state.playing = false
this.emit('stop')
}
proto.schedule = function(duration) {
var state = this._state
var currentTime = this.context.currentTime
var endTime = this.context.currentTime + duration
var time = state.lastEndTime
if (endTime >= time) {
state.lastEndTime = endTime
if (state.playing){
var duration = endTime - time
var length = duration / state.beatDuration
var from = state.lastTo
var to = from + length
state.lastTo = to
// skip if getting behind
//if ((currentTime - (state.cycleLength*3)) < time){
this._schedule(time, from, to)
//}
}
}
}
proto.setTempo = function(tempo){
var bps = tempo/60
var state = this._state
state.beatDuration = 60/tempo
state.increment = bps * state.cycleLength
state.bpm = tempo
this.emit('tempo', state.bpm)
}
proto.getTempo = function(){
return this._state.bpm
}
proto.isPlaying = function(){
return this._state.playing
}
proto.setPosition = function(position){
this._state.lastTo = parseFloat(position)
}
proto.setSpeed = function(multiplier){
var state = this._state
multiplier = parseFloat(multiplier) || 0
var tempo = state.bpm * multiplier
var bps = tempo/60
state.beatDuration = 60/tempo
state.increment = bps * state.cycleLength
}
proto.getPositionAt = function(time){
var state = this._state
var delta = state.lastEndTime - time
return state.lastTo - (delta / state.beatDuration)
}
proto.getTimeAt = function(position){
var state = this._state
var positionOffset = this.getCurrentPosition() - position
return this.context.currentTime - (positionOffset * state.beatDuration)
}
proto.getCurrentPosition = function(){
return this.getPositionAt(this.context.currentTime)
}
proto.getNextScheduleTime = function(){
var state = this._state
return state.lastEndTime
}
proto.getBeatDuration = function(){
var state = this._state
return state.beatDuration
}
proto._schedule = function(time, from, to){
var state = this._state
var duration = (to - from) * state.beatDuration
this.emit('data', {
from: from,
to: to,
time: time,
duration: duration,
beatDuration: state.beatDuration
})
}
function bopperTick () {
var state = this._state
this.schedule(state.cycleLength * state.preCycle)
}