-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (81 loc) · 2.07 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
const name = 'Rack Monome'
const readline = require('readline')
const easymidi = require('easymidi')
const serialosc = require('serialosc')
const output = new easymidi.Output(name, true)
let channel = 1
let grid = null
let fn = false
let brightnessMap = [0,0,1,2,4,8,15]
console.log(`Welcome to ${name}, press any key to stop.`)
serialosc.start()
// Utils
function noteAt (x, y) {
const q = quadAt(x, y)
const o = offsetAt(x, y)
return (((q * 16) + o) + 4) % 128
}
function offsetAt (x, y) {
const pad = padAt(x, y)
return (pad.y * 4) + pad.x
}
function quadAt (x, y) {
return (1 - Math.floor(y / 4)) + (Math.floor(x / 4) * 2)
}
function padAt (x, y) {
return { x: x % 4, y: -(y % 4) + 3 }
}
function posAt (i) {
return { x: i % 16, y: Math.floor(i / 16) }
}
function idAt (x, y) {
return (y * 16) + x
}
function levelAt(x, y) {
return brightnessMap[((15 - x) % 4) + (y % 4)]
}
function redraw () {
for (let i = 0; i < 128; i++) {
const pos = posAt(i)
grid.levelSet(pos.x, pos.y, levelAt(pos.x, pos.y))
}
}
function setChannel (ch) {
channel = ch
console.log('Set Channel: ', channel)
}
function onKeyDown (x, y) {
const id = idAt(x, y)
grid.levelSet(x, y, 10)
output.send('noteon', { note: noteAt(x, y), velocity: 127, channel: channel })
if (fn && idAt(x, y) < 16) {
setChannel(idAt(x, y))
}
fn = id === 127
}
function onKeyUp (x, y) {
grid.levelSet(x, y, levelAt(x, y))
output.send('noteoff', { note: noteAt(x, y), velocity: 127, channel: channel })
fn = false
}
function close () {
console.log('Done.')
process.exit()
}
// Ready
serialosc.on('device:add', (device) => {
grid = device
grid.all(0)
grid.on('key', (data) => { if (data.s === 1) { onKeyDown(data.x, data.y) } else { onKeyUp(data.x, data.y) } })
console.log(`Selecting ${device.model}(${device.sizeX}x${device.sizeY}) ${device.id}..`)
redraw()
})
// Quit
readline.emitKeypressEvents(process.stdin)
process.stdin.setRawMode(true)
process.stdin.on('keypress', (str, key) => {
console.log('Closing..')
grid.all(0)
output.close()
setTimeout(close, 500)
})