-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path3 crossfader.js
29 lines (23 loc) · 1.08 KB
/
3 crossfader.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
var $ = require('jquery')
var Kefir = require('kefir')
var keyFilter = function (ev, keycode) { if (ev.which == keycode) return ev }
var leftKeyFilter = function (ev) { return keyFilter(ev, 37) }
var rightKeyFilter = function (ev) { return keyFilter(ev, 39) }
var one = function (ev) { return 1 }
var minusOne = function (ev) { return -1 }
var sum = function (acc, cur) { return acc += cur }
var leftGain = function (pos) { return 100 - pos }
var rightGain = function (pos) { return pos }
var setup = function () {
//streams
var pressStream = Kefir.fromEvents(document.body, 'keydown')
var leftStream = pressStream.filter(leftKeyFilter).map(minusOne)
var rightStream = pressStream.filter(rightKeyFilter).map(one)
var dotPosStream = Kefir.merge([leftStream, rightStream]).scan(sum, 50)
//side-effects
dotPosStream.onValue(function (pos) { $('#dot').css('left', pos + '%') })
dotPosStream.map(leftGain).log('set left track gain!')
dotPosStream.map(rightGain).log('set right track gain!')
}
//main
$(document).on('ready', setup)