Skip to content

Commit

Permalink
add launch control xl support, and chunk mixer
Browse files Browse the repository at this point in the history
  • Loading branch information
mmckegg committed May 26, 2016
1 parent 445d3bd commit 8df9466
Show file tree
Hide file tree
Showing 11 changed files with 744 additions and 11 deletions.
25 changes: 19 additions & 6 deletions lib/midi-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function MidiPort (context, onSwitch) {
var obs = Prop()
var nextValue = null
var settingNext = false
obs.override = Prop(false)

obs.stream = Prop()

Expand All @@ -29,13 +30,15 @@ function MidiPort (context, onSwitch) {
var stack = stacks.get(obs())
if (stack && last(stack) !== obs) {
var lastTop = last(stack)
var index = stack.indexOf(obs)
if (~index) {
stack.splice(index, 1)
if (!lastTop.override()) {
var index = stack.indexOf(obs)
if (~index) {
stack.splice(index, 1)
}
stack.push(obs)
lastTop && lastTop.refresh()
refresh()
}
stack.push(obs)
lastTop && lastTop.refresh()
refresh()
}
}

Expand All @@ -48,6 +51,16 @@ function MidiPort (context, onSwitch) {
}
}

obs.previous = function () {
var stack = stacks.get(obs())
if (stack && last(stack) === obs) {
var previous = stack[stack.length - 2]
if (previous) {
previous.grab()
}
}
}

obs.destroy = function () {
while (listeners.length) {
listeners.pop()()
Expand Down
32 changes: 32 additions & 0 deletions lib/scale-interpolate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = scaleInterpolate

function scaleInterpolate (currentValue, toValue, state) {
var difference = absDifference(currentValue, toValue)
if (difference != null && difference > 3 && (!state.lastSync || Date.now() - state.lastSync > 50)) {
if (state.interpolatingFrom == null) {
state.interpolatingFrom = toValue
}
if (absDifference(state.interpolatingFrom, currentValue) < difference) {
if (currentValue <= 0) {
toValue = 1
} else {
toValue = toValue / state.interpolatingFrom * currentValue
}
} else {
state.interpolatingFrom = toValue
toValue = currentValue
}
state.lastSync = null
} else {
state.interpolatingFrom = null
state.lastSync = Date.now()
}

return toValue
}

function absDifference (a, b) {
if (a != null && b != null) {
return Math.abs(a - b)
}
}
64 changes: 64 additions & 0 deletions lib/watch-knobs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var watch = require('observ/watch')

module.exports = watchKnobs

function watchKnobs (stream, keys, listener, defaultValue) {
var currentStream = null
var release = watch(stream, set)
var currentValues = []
var broadcastedValues = []
var broadcastedAt = []
var lastChangeAt = []
var throttling = []
var timers = []
var minDelay = 10

return function () {
release && release()
currentStream && currentStream.removeListener('data', handler)
}
// scoped

function handler (message) {
var key = message[0] + '/' + message[1]
var index = keys.indexOf(key)
if (~index) {
broadcast(index, message[2])
}
}

function broadcast (index, value) {
currentValues[index] = value
if (!throttling[index]) {
if (Date.now() - lastChangeAt[index] > minDelay) {
broadcastNow(index, value)
} else {
throttling[index] = true
timers[index] = setInterval(function () {
broadcastNow(index, currentValues[index])
}, minDelay)
}
}
lastChangeAt[index] = Date.now()
}

function broadcastNow (index, value) {
broadcastedAt[index] = Date.now()

if (broadcastedValues[index] !== value) {
broadcastedValues[index] = value
listener(index, value)
}

if (throttling[index] && broadcastedAt[index] - lastChangeAt[index] > minDelay) {
throttling[index] = false
clearInterval(timers[index])
}
}

function set (newStream) {
currentStream && currentStream.removeListener('data', handler)
newStream && newStream.on('data', handler)
currentStream = newStream
}
}
7 changes: 7 additions & 0 deletions nodes/global-launch-control-xl/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
name: 'Launch Control XL',
portMatch: /^Launch Control XL/,
node: 'global/launch-control-xl',
group: 'global-controllers',
object: require('./object')
}
Loading

0 comments on commit 8df9466

Please sign in to comment.