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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
import Tone from 'tone'
import 'nexusui'
import keys from './lib/keys'
import color from './lib/color'
import kalimba from './lib/kalimba'
import scales from './lib/scales'
const nx = window.nx
const noteCount = 24
const stepCount = 16
let grid
var loop = new Tone.Sequence(function(time, col){
var column = grid.matrix[col]
grid.jumpToCol(col)
for (var i = 0; i < noteCount; i++){
if (column[i] === 1){
const freq = scales.current().index(noteCount - i - 12)
kalimba.play(freq)
}
}
}, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], "16n")
nx.onload = () => {
grid = nx.widgets.grid
grid.sequenceMode = true
grid.bpm = 1
grid.col = stepCount
grid.row = noteCount
grid.init()
grid.resize(480, 640)
grid.draw()
nx.widgets.shiftUp.on('*', e => e.press && shiftUp())
nx.widgets.shiftDown.on('*', e => e.press && shiftDown())
nx.widgets.slideUp.on('*', e => e.press && slideUp())
nx.widgets.slideDown.on('*', e => e.press && slideDown())
nx.widgets.slideLeft.on('*', e => e.press && slideLeft())
nx.widgets.slideRight.on('*', e => e.press && slideRight())
nx.widgets.rotateUp.on('*', e => e.press && rotateVertical(-1))
nx.widgets.rotateDown.on('*', e => e.press && rotateVertical(1))
nx.widgets.rotateLeft.on('*', e => e.press && rotateHorizontal(-1))
nx.widgets.rotateRight.on('*', e => e.press && rotateHorizontal(1))
nx.widgets.flip.on('*', e => e.press && flip())
nx.widgets.flop.on('*', e => e.press && flop())
nx.colorize('#f4d142')
scales.build()
scales.onChange((s) => {
const labels = document.querySelector('#labels')
let str = ''
for (let i = 0; i < noteCount; i++) {
let n = i % s.scale.length
str += (n+1) + '<br>'
}
labels.innerHTML = str
})
scales.pick(0)
loop.start()
Tone.Transport.bpm.value = 108
nx.widgets.tempo.min = 10
nx.widgets.tempo.max = 1000
nx.widgets.tempo.set({ value: 108 })
nx.widgets.tempo.on('*', e => Tone.Transport.bpm.value = nx.widgets.tempo.val.value )
Tone.Transport.start()
}
function shiftUp () {
const scaleCount = scales.current().scale.length
const originalNotes = findNotes()
const subScale = originalNotes.map(n => noteCount - n).reduce((acc, n) => {
const scaleNote = n % scaleCount
if (! acc.includes(scaleNote)) acc.push(scaleNote)
return acc
}, []).sort()
assignNotes( mapFunction(originalNotes, (n) => {
let note = noteCount - n
let scaleIndex = subScale.indexOf(note % scaleCount) + 1
let octave = Math.floor(note / scaleCount)
while (scaleIndex >= subScale.length) {
scaleIndex -= subScale.length
octave += 1
}
return noteCount - (subScale[scaleIndex] + (octave * scaleCount))
}))
}
function shiftDown () {
const scaleCount = scales.current().scale.length
const originalNotes = findNotes()
const subScale = originalNotes.map(n => noteCount - n).reduce((acc, n) => {
const scaleNote = n % scaleCount
if (! acc.includes(scaleNote)) acc.push(scaleNote)
return acc
}, []).sort()
assignNotes( mapFunction(originalNotes, (n) => {
let note = noteCount - n
let scaleIndex = subScale.indexOf(note % scaleCount) - 1
let octave = Math.floor(note / scaleCount)
while (scaleIndex < 0) {
scaleIndex += subScale.length
octave -= 1
}
return noteCount - Math.max(0, subScale[scaleIndex] + (octave * scaleCount))
}))
}
function slideUp () {
assignNotes( mapFunction(findNotes(), (n) => {
return (n - 1 + noteCount) % noteCount
}))
}
function slideDown () {
assignNotes( mapFunction(findNotes(), (n) => {
return (n + 1 + noteCount) % noteCount
}))
}
function slideLeft () {
assignPositions( mapFunction(findPositions(), (n) => {
return (n - 1 + stepCount) % stepCount
}))
}
function slideRight () {
assignPositions( mapFunction(findPositions(), (n) => {
return (n + 1 + stepCount) % stepCount
}))
}
function flip () {
assignNotes( mapReverse(findNotes()) )
}
function flop () {
assignPositions( mapReverse(findPositions()) )
}
function rotateHorizontal (n) {
assignPositions( remapArray(findPositions(), n) )
}
function rotateVertical (n) {
assignNotes( remapArray(findNotes(), n) )
}
function assignPositions (positions) {
if (! positions) return
const a = grid.matrix
const b = iota()
stride(a, (i,j,v) => {
if (i in positions) {
b[positions[i]][j] = v
}
})
assign(grid.matrix, b)
grid.draw()
}
function assignNotes (noteMap) {
if (! noteMap) return
const a = grid.matrix
const b = iota()
stride(a, (i,j,v) => {
if (j in noteMap) {
b[i][noteMap[j]] = v
}
})
assign(grid.matrix, b)
grid.draw()
}
function stride (a, f) {
const w = a.length, h = a[0].length
for (let i = 0; i < w; i++) {
for (let j = 0; j < h; j++) {
f(i, j, a[i][j])
}
}
return a
}
// assign b -> a
function assign (a, b) {
return stride(b, (i,j,v) => a[i][j] = b[i][j])
}
function clone (a) {
const b = iota(a.length, a[0].length)
return assign(b, a)
}
function iota () {
const w = grid.matrix.length, h = grid.matrix[0].length
const a = new Array(w)
for (let i = 0; i < w; i++) {
a[i] = new Array(h)
for (let j = 0; j < h; j++) {
a[i][j] = 0
}
}
return a
}
function findNotes () {
const a = new Array(noteCount)
stride(grid.matrix, (i, j, v) => {
if (v) a[j] = 1
})
return a.reduce((acc, v, i) => {
if (v === 1) acc.push(i)
return acc
}, [])
}
function findPositions () {
return grid.matrix.reduce((acc, row, i) => {
if (row.some((x) => x === 1)) acc.push(i)
return acc
}, [])
}
function remapArray (a, n) {
if (! a.length) return
const h = {}
const b = rotate(a, n)
for (let i = 0; i < b.length; i++) {
h[b[i]] = a[i]
}
return h
}
function rotate(a, n) {
const b = a.slice(0)
b.unshift.apply( b, b.splice( -n, b.length ) )
return b
}
function mapFunction (a, f) {
if (! a.length) return
const h = {}
for (let i = 0; i < a.length; i++) {
h[a[i]] = f(a[i])
}
return h
}
function mapReverse (a) {
if (! a.length) return
const h = {}
const b = a.slice(0).reverse()
for (let i = 0; i < b.length; i++) {
h[b[i]] = a[i]
}
return h
}
keys.listen(function(index){
const freq = scales.current().index(index)
kalimba.play(freq)
})
|