diff options
author | Niels Janssen <[email protected]> | 2020-09-30 11:48:36 +0200 |
---|---|---|
committer | GitHub <[email protected]> | 2020-09-30 11:48:36 +0200 |
commit | bf36c798817bb17b11cfa395cc3bf2a39d28d754 (patch) | |
tree | 10b6cebf3d02450d0894c52e9bb0acd5352662f4 | |
parent | a0f12faee513866c22c04b867ef716df48904223 (diff) | |
parent | 594241c2513564a72721acaea55685103ec31ee8 (diff) | |
download | Orca-bf36c798817bb17b11cfa395cc3bf2a39d28d754.tar.gz Orca-bf36c798817bb17b11cfa395cc3bf2a39d28d754.zip |
Merge pull request #1 from unthingable/midi-input-switching
MIDI clock slave mode: fix timing and note-off behavior
-rw-r--r-- | desktop/sources/scripts/clock.js | 57 | ||||
-rw-r--r-- | desktop/sources/scripts/core/io/midi.js | 2 |
2 files changed, 41 insertions, 18 deletions
diff --git a/desktop/sources/scripts/clock.js b/desktop/sources/scripts/clock.js index a170e2c..95086cf 100644 --- a/desktop/sources/scripts/clock.js +++ b/desktop/sources/scripts/clock.js @@ -56,31 +56,48 @@ function Clock (client) { client.update() } - this.play = function (msg = false) { - console.log('Clock', 'Play', msg) - if (this.isPaused === false) { return } + this.play = function (msg = false, midiStart = false) { + console.log('Clock', 'Play', msg, midiStart) + if (this.isPaused === false && !midiStart) { return } this.isPaused = false - if (this.isPuppet === true) { console.warn('Clock', 'External Midi control'); return } - if (msg === true) { client.io.midi.sendClockStart() } - this.setSpeed(this.speed.target, this.speed.target, true) + if (this.isPuppet === true) { + console.warn('Clock', 'External Midi control') + if (!pulse.frame || midiStart) { // no frames counted while paused (starting from no clock, unlikely) or triggered by MIDI clock START + this.setFrame(0) // make sure frame aligns with pulse count for an accurate beat + pulse.frame = 0 + pulse.count = 5 // by MIDI standard next pulse is the beat + } + } else { + if (msg === true) { client.io.midi.sendClockStart() } + this.setSpeed(this.speed.target, this.speed.target, true) + } } this.stop = function (msg = false) { console.log('Clock', 'Stop') if (this.isPaused === true) { return } this.isPaused = true - if (this.isPuppet === true) { console.warn('Clock', 'External Midi control'); return } - if (msg === true || client.io.midi.isClock) { client.io.midi.sendClockStop() } + if (this.isPuppet === true) { + console.warn('Clock', 'External Midi control') + } else { + if (msg === true || client.io.midi.isClock) { client.io.midi.sendClockStop() } + this.clearTimer() + } client.io.midi.allNotesOff() - this.clearTimer() client.io.midi.silence() } // External Clock - const pulse = { count: 0, last: null, timer: null } + const pulse = { + count: 0, + last: null, + timer: null, + frame: 0 // paused frame counter + } this.tap = function () { + pulse.count = (pulse.count + 1) % 6 pulse.last = performance.now() if (!this.isPuppet) { console.log('Clock', 'Puppeteering starts..') @@ -91,11 +108,15 @@ function Clock (client) { this.untap() }, 2000) } - if (this.isPaused) { return } - pulse.count = pulse.count + 1 - if (pulse.count % 6 === 0) { - client.run() - pulse.count = 0 + if (pulse.count == 0) { + if (this.isPaused) { pulse.frame++ } + else { + if (pulse.frame > 0) { + this.setFrame(client.orca.f + pulse.frame) + pulse.frame = 0 + } + client.run() + } } } @@ -103,9 +124,11 @@ function Clock (client) { console.log('Clock', 'Puppeteering stops..') clearInterval(pulse.timer) this.isPuppet = false - pulse.count = 1 + pulse.frame = 0 pulse.last = null - this.setTimer(this.speed.value) + if (!this.isPaused) { + this.setTimer(this.speed.value) + } } // Timer diff --git a/desktop/sources/scripts/core/io/midi.js b/desktop/sources/scripts/core/io/midi.js index 285cebd..7f4875a 100644 --- a/desktop/sources/scripts/core/io/midi.js +++ b/desktop/sources/scripts/core/io/midi.js @@ -129,7 +129,7 @@ function Midi (client) { break case 0xFA: console.log('MIDI', 'Start Received') - client.clock.play() + client.clock.play(false, true) break case 0xFB: console.log('MIDI', 'Continue Received') |