-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
114 lines (93 loc) · 3.1 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
const name = 'Linn 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 last = null
let down = 0
let fn = false
const keys = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
const notes = [
'F3', 'F3#', 'G3', 'G3#', 'A3', 'A3#', 'B3', 'C4', 'C4#', 'D4', 'D4#', 'E4', 'F4', 'F4#', 'G4', 'G4#',
'C3', 'C3#', 'D3', 'D3#', 'E3', 'F3', 'F3#', 'G3', 'G3#', 'A3', 'A3#', 'B3', 'C4', 'C4#', 'D4', 'D4#',
'G2', 'G2#', 'A2', 'A2#', 'B2', 'C3', 'C3#', 'D3', 'D3#', 'E3', 'F3', 'F3#', 'G3', 'G3#', 'A3', 'A3#',
'D2', 'D2#', 'E2', 'F2', 'F2#', 'G2', 'G2#', 'A2', 'A2#', 'B2', 'C3', 'C3#', 'D3', 'D3#', 'E3', 'F3',
'A1', 'A1#', 'B1', 'C3', 'C2#', 'D2', 'D2#', 'E2', 'F2', 'F2#', 'G2', 'G2#', 'A2', 'A2#', 'B2', 'C3',
'E1', 'F1', 'F1#', 'G1', 'G1#', 'A1', 'A1#', 'B1', 'C2', 'C2#', 'D2', 'D2#', 'E2', 'F2', 'F2#', 'G2',
'B0', 'C1', 'C1#', 'D1', 'D1#', 'E1', 'F1', 'F1#', 'G1', 'G1#', 'A1', 'A1#', 'B1', 'C2', 'C2#', 'D2',
'F0#', 'G0', 'G0#', 'A0', 'A0#', 'B0', 'C1', 'C1#', 'D1', 'D1#', 'E1', 'F1', 'F1#', 'G1', 'G1#', 'A1'
]
console.log(`Welcome to ${name}, press any key to stop.`)
serialosc.start()
// Utils
function noteAt (i) {
const n = notes[i]
const k = n.substr(0, 1)
const o = parseInt(n.substr(1, 1)) + 2
const s = n.indexOf('#') > -1
const l = s ? 0 : k === 'C' ? 15 : 5
const p = k + '' + (s ? '#' : '')
const v = keys.indexOf(p) + (o * 12)
return { i, k, o, s, l, v, p }
}
function posAt (i) {
return { x: i % 16, y: Math.floor(i / 16) }
}
function idAt (x, y) {
return (y * 16) + x
}
function redraw () {
for (let i = 0; i < 128; i++) {
const pos = posAt(i)
const note = noteAt(i)
grid.levelSet(pos.x, pos.y, last && note.p === last ? 10 : note.l)
}
}
function print (note) {
console.log(`${(note.i + '').padStart(3, ' ')} | ${(note.p + '').padStart(3, ' ')} | ${(note.v + '').padStart(3, ' ')} | ch:${(channel + '').padStart(3, ' ')}`)
}
function onKeyDown (x, y) {
const note = noteAt(idAt(x, y))
down += 1
grid.levelSet(x, y, 10)
output.send('noteon', { note: note.v, velocity: 127, channel: channel })
print(note)
if (fn && idAt(x, y) < 16) {
channel = idAt(x, y)
}
fn = note.i === 127
last = note.p
}
function onKeyUp (x, y) {
const note = noteAt(idAt(x, y))
down -= 1
grid.levelSet(x, y, note.l)
output.send('noteoff', { note: note.v, velocity: 127, channel: channel })
fn = false
if (down < 1) {
last = null
}
}
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)
})