diff options
-rw-r--r-- | README.md | 4 | ||||
-rw-r--r-- | desktop/core/io.js | 2 | ||||
-rw-r--r-- | desktop/core/io/midi.js | 119 | ||||
-rw-r--r-- | desktop/core/io/mono.js | 4 | ||||
-rw-r--r-- | desktop/core/library.js | 4 | ||||
-rw-r--r-- | desktop/core/library/_keys.js | 23 | ||||
-rw-r--r-- | desktop/core/library/_midi.js | 4 | ||||
-rw-r--r-- | desktop/main.js | 2 | ||||
-rw-r--r-- | desktop/sources/scripts/terminal.js | 2 |
9 files changed, 75 insertions, 89 deletions
@@ -62,10 +62,6 @@ npm start - `;` **udp**: Sends UDP message. - `=` **osc**(*path*): Sends OSC message. -#### Receive - -- `&` **mono**: Receive MIDI note. - ## MIDI The [MIDI](https://en.wikipedia.org/wiki/MIDI) operator `:` takes up to 5 inputs('channel, 'octave, 'note, velocity, length). diff --git a/desktop/core/io.js b/desktop/core/io.js index 19f721d..32169b3 100644 --- a/desktop/core/io.js +++ b/desktop/core/io.js @@ -44,7 +44,7 @@ export default function IO (terminal) { } this.length = function () { - return this.midi.stack.length + this.cc.stack.length + this.udp.stack.length + this.osc.stack.length + return this.midi.length() + this.mono.length() + this.cc.stack.length + this.udp.stack.length + this.osc.stack.length } this.inspect = function (limit = terminal.grid.w) { diff --git a/desktop/core/io/midi.js b/desktop/core/io/midi.js index 0a80c71..dc3fee8 100644 --- a/desktop/core/io/midi.js +++ b/desktop/core/io/midi.js @@ -23,6 +23,62 @@ export default function Midi (terminal) { } + this.run = function () { + for (const id in this.stack) { + if (this.stack[id].isPlayed === false) { + this.press(this.stack[id]) + } + if (this.stack[id].length <= 1) { + this.release(this.stack[id]) + } + if (this.stack[id]) { + this.stack[id].length-- + } + } + + this.stack = this.stack.filter((item) => { return item.length > 0 }) + } + + this.trigger = function (item, down) { + if (!this.outputDevice()) { console.warn('Midi', 'No midi output!'); return } + + const transposed = this.transpose(item.note, item.octave) + const channel = terminal.orca.valueOf(item.channel) + + const c = down === true ? 0x90 + channel : 0x80 + channel + const n = transposed.id + const v = parseInt((item.velocity / 16) * 127) + + this.outputDevice().send([c, n, v]) + } + + this.press = function (item) { + if (!item) { return } + this.trigger(item, true) + item.isPlayed = true + } + + this.release = function (item) { + if (!item) { return } + this.trigger(item, false) + } + + this.silence = function () { + for (const id in this.stack) { + this.release(this.stack[id]) + } + } + + this.send = function (channel, octave, note, velocity, length, isPlayed = false) { + // Stop duplicates + for (const id in this.stack) { + if (this.stack[id].channel === channel) { return } + if (this.stack[id].octave === octave) { return } + if (this.stack[id].note === note) { return } + } + this.stack.push({ channel, octave, note, velocity, length, isPlayed }) + } + this.update = function () { terminal.controller.clearCat('default', 'Midi') terminal.controller.add('default', 'Midi', `Refresh Device List`, () => { terminal.io.midi.setup(); terminal.io.midi.update() }) @@ -52,62 +108,13 @@ export default function Midi (terminal) { terminal.controller.commit() } - this.run = function () { - this.stack = this.stack.filter((item) => { - const alive = item[4] > 0 - const played = item[5] - if (alive !== true) { - this.trigger(item, false) - } else if (played !== true) { - this.trigger(item, true) - } - item[4]-- - return alive - }) - } - - this.trigger = function (item, down) { - if (!this.outputDevice()) { console.warn('Midi', 'No midi output!'); return } - - const transposed = this.transpose(item.note, item.octave) - const channel = terminal.orca.valueOf(item.channel) - - const c = down === true ? 0x90 + channel : 0x80 + channel - const n = transposed.id - const v = parseInt((item.velocity / 16) * 127) - - console.log(c,n,v,down) - - this.outputDevice().send([c, n, v]) - } - - this.send = function (channel, octave, note, velocity, length, played = false) { - for (const id in this.stack) { - const item = this.stack[id] - if (item[0] === channel && item[1] === octave && item[2] === note) { - item[3] = velocity - item[4] = length - item[5] = played - return - } - } - this.stack.push([channel, octave, note, velocity, length, played]) - } - - this.silence = function () { - this.stack = this.stack.filter((item) => { - this.trigger(item, false) - return false - }) - } - // Keys - this.press = function (key) { + this.keyDown = function (key) { this.key = parseInt(key) } - this.release = function () { + this.keyUp = function () { this.key = null } @@ -134,10 +141,10 @@ export default function Midi (terminal) { switch (msg.data[0]) { // Keys case 0x90: - this.press(msg.data[1]) + this.keyDown(msg.data[1]) break case 0x80: - this.release() + this.keyUp() break // Clock case 0xF8: @@ -216,7 +223,7 @@ export default function Midi (terminal) { const note = transpose[n].charAt(0) const octave = clamp(parseInt(transpose[n].charAt(1)) + o, 0, 8) const value = ['C', 'c', 'D', 'd', 'E', 'F', 'f', 'G', 'g', 'A', 'a', 'B'].indexOf(note) - const id = clamp((octave * 12) + value, 0, 127) + const id = clamp((octave * 12) + value + 24, 0, 127) const real = id < 89 ? Object.keys(transpose)[id - 45] : null return { id, value, note, octave, real } } @@ -225,5 +232,9 @@ export default function Midi (terminal) { return this.outputDevice() ? `${this.outputDevice().name}` : 'No Midi' } + this.length = function () { + return this.stack.length + } + function clamp (v, min, max) { return v < min ? min : v > max ? max : v } } diff --git a/desktop/core/io/mono.js b/desktop/core/io/mono.js index 95ae59a..7b3d27e 100644 --- a/desktop/core/io/mono.js +++ b/desktop/core/io/mono.js @@ -49,4 +49,8 @@ export default function Mono (terminal) { } this.stack[channel] = { channel, octave, note, velocity, length, isPlayed } } + + this.length = function () { + return Object.keys(this.stack).length + } } diff --git a/desktop/core/library.js b/desktop/core/library.js index 0f92eee..4feaf70 100644 --- a/desktop/core/library.js +++ b/desktop/core/library.js @@ -34,7 +34,6 @@ import _mono from './library/_mono.js' import _cc from './library/_cc.js' import _udp from './library/_udp.js' import _osc from './library/_osc.js' -import _keys from './library/_keys.js' export default { '0': _null, @@ -79,6 +78,5 @@ export default { '%': _mono, '!': _cc, ';': _udp, - '=': _osc, - '&': _keys + '=': _osc } diff --git a/desktop/core/library/_keys.js b/desktop/core/library/_keys.js deleted file mode 100644 index add4162..0000000 --- a/desktop/core/library/_keys.js +++ /dev/null @@ -1,23 +0,0 @@ -'use strict' - -import Operator from '../operator.js' - -const OCTAVE = ['C', 'c', 'D', 'd', 'E', 'F', 'f', 'G', 'g', 'A', 'a', 'B'] - -export default function OperatorKeys (orca, x, y, passive) { - Operator.call(this, orca, x, y, '&', true) - - this.name = 'mono' - this.info = 'Receive MIDI note' - - this.ports.output = { x: 0, y: 1 } - - this.operation = function (force = false) { - if (!terminal.io.midi.key) { return '.' } - const octave = Math.floor(terminal.io.midi.key / 12) - const value = terminal.io.midi.key % 12 - const note = OCTAVE[value] - const transposed = this.transpose(note, octave) - return transposed && transposed.real ? transposed.real : '.' - } -} diff --git a/desktop/core/library/_midi.js b/desktop/core/library/_midi.js index e4b1bfc..18a3996 100644 --- a/desktop/core/library/_midi.js +++ b/desktop/core/library/_midi.js @@ -24,8 +24,8 @@ export default function OperatorMidi (orca, x, y, passive) { const note = this.listen(this.ports.note) if (note === '.') { return } - const velocity = this.listen(this.ports.velocity) - const length = this.listen(this.ports.length) + const velocity = this.listen(this.ports.velocity, true) + const length = this.listen(this.ports.length, true) terminal.io.midi.send(channel, octave, note, velocity, length) diff --git a/desktop/main.js b/desktop/main.js index 2c3bc30..7740460 100644 --- a/desktop/main.js +++ b/desktop/main.js @@ -37,7 +37,7 @@ app.on('ready', () => { }) app.win.loadURL(`file://${__dirname}/sources/index.html`) - app.inspect() + // app.inspect() app.win.on('closed', () => { win = null diff --git a/desktop/sources/scripts/terminal.js b/desktop/sources/scripts/terminal.js index 3d834d9..6881f9c 100644 --- a/desktop/sources/scripts/terminal.js +++ b/desktop/sources/scripts/terminal.js @@ -12,7 +12,7 @@ import Controller from './lib/controller.js' import library from '../../core/library.js' export default function Terminal () { - this.version = 132 + this.version = 133 this.library = library this.orca = new Orca(this) |