diff options
| author | Jules Laplace <jules@okfoc.us> | 2017-04-27 08:10:10 -0400 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2017-04-27 08:10:10 -0400 |
| commit | a222d88a13afc1b0f763f5d800fdda1426967bf4 (patch) | |
| tree | 8efca531a12c175ecb9fe9b3f1dbef952f7661ad /client | |
| parent | 934d7dd83b34fd338b28d95da0397f4016516b82 (diff) | |
convert between notes and indices correctly
Diffstat (limited to 'client')
| -rw-r--r-- | client/index.js | 83 | ||||
| -rw-r--r-- | client/lib/util.js | 3 |
2 files changed, 56 insertions, 30 deletions
diff --git a/client/index.js b/client/index.js index 6879688..38f8410 100644 --- a/client/index.js +++ b/client/index.js @@ -4,6 +4,7 @@ import keys from './lib/keys' import color from './lib/color' import kalimba from './lib/kalimba' import scales from './lib/scales' +import { mod } from './lib/util' const nx = window.nx @@ -34,6 +35,7 @@ nx.onload = () => { grid.resize(480, 640) grid.draw() + grid.on('*', e => ('level' in e) && buildLabels()) nx.widgets.shiftUp.on('*', e => e.press && shiftUp()) nx.widgets.shiftDown.on('*', e => e.press && shiftDown()) nx.widgets.slideUp.on('*', e => e.press && slideUp()) @@ -51,13 +53,7 @@ nx.onload = () => { 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 + buildLabels() }) scales.pick(0) @@ -69,43 +65,72 @@ nx.onload = () => { nx.widgets.tempo.on('*', e => Tone.Transport.bpm.value = nx.widgets.tempo.val.value ) Tone.Transport.start() } - -function shiftUp () { +function draw () { + grid.draw() + buildLabels() +} +function buildLabels () { + const labels = document.querySelector('#labels') const scaleCount = scales.current().scale.length - const originalNotes = findNotes() - const subScale = originalNotes.map(n => noteCount - n).reduce((acc, n) => { - const scaleNote = n % scaleCount + const subScale = findSubScale() + let str = '' + for (let i = 0; i < noteCount; i++) { + let index = noteCount - i - 12 + let n = mod(index, scaleCount) + let ns = (n+1) + if (ns == 1) { + let octave = (index / scaleCount) + ns = ns + '<small>' + octave + '</small>' + } + if (subScale.includes(n)) { + str += '<b>' + (ns) + '</b><br>' + } + else { + str += (ns) + '<br>' + } + } + labels.innerHTML = str +} +function findSubScale (notes) { + notes = notes || findNotes() + const scaleCount = scales.current().scale.length + return notes.reduce((acc, n) => { + const scaleNote = mod(noteCount - n - 12, scaleCount) if (! acc.includes(scaleNote)) acc.push(scaleNote) return acc }, []).sort() +} + +function shiftUp () { + const originalNotes = findNotes() + const subScale = findSubScale( originalNotes ) + const scaleCount = scales.current().scale.length assignNotes( mapFunction(originalNotes, (n) => { - let note = noteCount - n - let scaleIndex = subScale.indexOf(note % scaleCount) + 1 - let octave = Math.floor(note / scaleCount) + let index = noteCount - n - 12 + let note = mod(index, scaleCount) + let scaleIndex = subScale.indexOf(note) + 1 + let octave = Math.floor(index / scaleCount) while (scaleIndex >= subScale.length) { scaleIndex -= subScale.length octave += 1 } - return noteCount - (subScale[scaleIndex] + (octave * scaleCount)) + return 12 - (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() + const subScale = findSubScale( originalNotes ) + const scaleCount = scales.current().scale.length assignNotes( mapFunction(originalNotes, (n) => { - let note = noteCount - n - let scaleIndex = subScale.indexOf(note % scaleCount) - 1 - let octave = Math.floor(note / scaleCount) + let index = noteCount - n - 12 + let note = mod(index, scaleCount) + let scaleIndex = subScale.indexOf(note) - 1 + let octave = Math.floor(index / scaleCount) while (scaleIndex < 0) { scaleIndex += subScale.length octave -= 1 } - return noteCount - Math.max(0, subScale[scaleIndex] + (octave * scaleCount)) + return 12 - (subScale[scaleIndex] + (octave * scaleCount)) })) } function slideUp () { @@ -150,7 +175,7 @@ function assignPositions (positions) { } }) assign(grid.matrix, b) - grid.draw() + draw() } function assignNotes (noteMap) { if (! noteMap) return @@ -162,11 +187,11 @@ function assignNotes (noteMap) { } }) assign(grid.matrix, b) - grid.draw() + draw() } function stride (a, f) { - const w = a.length, h = a[0].length + const w = stepCount, h = noteCount for (let i = 0; i < w; i++) { for (let j = 0; j < h; j++) { f(i, j, a[i][j]) diff --git a/client/lib/util.js b/client/lib/util.js index b2d95f5..7c3082d 100644 --- a/client/lib/util.js +++ b/client/lib/util.js @@ -1,3 +1,4 @@ function choice (a){ return a[ Math.floor(Math.random() * a.length) ] } +function mod(n,m){ return n-(m * Math.floor(n/m)) } -export { choice }
\ No newline at end of file +export { choice, mod } |
