-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
175 lines (151 loc) · 4.51 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
163
164
165
166
167
168
169
170
171
172
173
174
175
var ArrayGrid = require('array-grid')
var Observ = require('observ')
var ObservDefault = require('./lib/observ-default')
var ObservStruct = require('observ-struct')
var Event = require('geval')
var computed = require('observ/computed')
var setImmediate = require('setimmediate2').setImmediate
var getEvents = require('./lib/get-events')
module.exports = LoopGrid
function LoopGrid (context) {
var obs = ObservStruct({
shape: ObservDefault([8, 8]),
loops: ObservDefault([]),
targets: ObservDefault([]),
loopLength: ObservDefault(8)
})
var listen = Listener()
obs.loopPosition = Observ([0, 8])
obs.context = context
// state layers
obs.grid = computed([obs.targets, obs.shape], ArrayGrid)
obs.playing = Observ(ArrayGrid([], obs.shape()))
obs.active = computed([obs.loops, obs.shape], function (loops, shape) {
return ArrayGrid(loops.map(function (loop) {
return (loop && loop.length && Array.isArray(loop.events) && loop.events.length)
}), shape)
})
var current = {}
var currentlyPlaying = {}
var overriding = {}
var pendingPlayingUpdate = false
var lastPosition = -1
obs.onEvent = Event(function (broadcast) {
obs.triggerEvent = broadcast
obs.triggerEvent = function (event) {
if (event.id) {
if (event.event === 'start') {
overriding[event.id] = true
} else {
overriding[event.id] = false
}
if (!current[event.id]) {
broadcast(event)
}
}
}
listen(context.scheduler.onSchedule, function (schedule) {
var targets = obs.targets()
targets.forEach(function (id, index) {
var loop = obs.loops()[index]
getEvents(loop, schedule.from, schedule.to, 1).forEach(function (event) {
if (id && current[id] !== event[1] && !(!current[id] && !event[1])) {
var delta = (event[0] - schedule.from) * schedule.beatDuration
current[id] = event[1]
if (!overriding[id]) {
broadcast({
id: id,
event: event[1] ? 'start' : 'stop',
position: event[0],
time: schedule.time + delta
})
}
}
})
})
// stop any notes that are no longer targets
Object.keys(current).forEach(function (id) {
if (!~targets.indexOf(id)) {
delete current[id]
if (current[id]) {
broadcast({
id: id,
event: 'stop',
position: schedule.from,
time: schedule.time
})
}
}
})
// update playback position
if (Math.floor(schedule.from * 10) > Math.floor(lastPosition * 10)) {
var loopLength = obs.loopLength() || 8
var pos = Math.floor(schedule.from * 10) % (loopLength * 10)
obs.loopPosition.set([pos / 10, loopLength])
lastPosition = schedule.from
}
})
listen.event(context.scheduler, 'stop', function () {
Object.keys(current).forEach(function (id) {
delete current[id]
if (current[id]) {
broadcast({
id: id,
event: 'stop',
position: context.scheduler.getCurrentPosition(),
time: context.audio.currentTime
})
}
})
})
})
obs.onEvent(function (event) {
if (context.triggerEvent) {
// send events
context.triggerEvent(event)
}
if (event.event === 'start') {
currentlyPlaying[event.id] = true
} else if (event.event === 'stop') {
currentlyPlaying[event.id] = false
}
if (!pendingPlayingUpdate) {
pendingPlayingUpdate = true
setImmediate(refreshPlaying)
}
})
obs.destroy = function () {
listen.releaseAll()
}
return obs
// scoped
function refreshPlaying () {
pendingPlayingUpdate = false
var playing = []
var targets = obs.targets()
var shape = obs.shape()
var max = Array.isArray(shape) && shape[0] * shape[1] || 0
for (var i = 0; i < max; i++) {
if (currentlyPlaying[targets[i]]) {
playing[i] = true
}
}
obs.playing.set(ArrayGrid(playing, shape))
}
}
function Listener () {
var releases = []
function listen (target, listener) {
releases.push(target(listener))
}
listen.event = function (emitter, event, listener) {
emitter.on(event, listener)
releases.push(emitter.removeListener.bind(emitter, event, listener))
}
listen.releaseAll = function () {
while (releases.length) {
releases.pop()()
}
}
return listen
}