Add start, stop and continue

This commit is contained in:
2023-10-03 22:33:19 +03:00
parent 05a4f8a161
commit e856f045bb
2 changed files with 31 additions and 26 deletions

View File

@ -1,4 +1,5 @@
import { UserAPI } from "../API"; import { UserAPI } from "../API";
import { Clock } from "../Clock";
export class MidiConnection { export class MidiConnection {
/** /**
@ -85,23 +86,27 @@ export class MidiConnection {
public sendStartMessage(): void { public sendStartMessage(): void {
/** /**
* Sends a MIDI Start message to the currently selected MIDI output. * Sends a MIDI Start message to the currently selected MIDI output and MIDI clock is not used
*/ */
if(!this.midiClockInput) {
const output = this.midiOutputs[this.currentOutputIndex]; const output = this.midiOutputs[this.currentOutputIndex];
if (output) { if (output) {
output.send([0xfa]); // Send MIDI Start message output.send([0xfa]); // Send MIDI Start message
} }
} }
}
public sendStopMessage(): void { public sendStopMessage(): void {
/** /**
* Sends a MIDI Stop message to the currently selected MIDI output. * Sends a MIDI Stop message to the currently selected MIDI output and MIDI clock is not used
*/ */
if(!this.midiClockInput) {
const output = this.midiOutputs[this.currentOutputIndex]; const output = this.midiOutputs[this.currentOutputIndex];
if (output) { if (output) {
output.send([0xfc]); // Send MIDI Stop message output.send([0xfc]); // Send MIDI Stop message
} }
} }
}
public getCurrentMidiPortIndex(): number { public getCurrentMidiPortIndex(): number {
/** /**
@ -195,15 +200,19 @@ export class MidiConnection {
} }
} else if(message.data[0] === 0xfa) { } else if(message.data[0] === 0xfa) {
console.log("MIDI start received"); console.log("MIDI start received");
this.api.stop();
this.api.play();
} else if(message.data[0] === 0xfc) { } else if(message.data[0] === 0xfc) {
console.log("MIDI stop received"); console.log("MIDI stop received");
this.api.pause();
} else if(message.data[0] === 0xfb) { } else if(message.data[0] === 0xfb) {
console.log("MIDI continue received"); console.log("MIDI continue received");
this.api.play();
} else if(message.data[0] === 0xfe) { } else if(message.data[0] === 0xfe) {
console.log("MIDI active sensing received"); console.log("MIDI active sensing received");
} else { } else {
// Ignore other MIDI messages // Ignore other MIDI messages
console.log("Ignored MIDI message: ", message.data); // console.log("Ignored MIDI message: ", message.data[0], message.data[1]);
} }
} }
} }
@ -232,30 +241,23 @@ export class MidiConnection {
console.log("BPMs", this.clockBuffer); console.log("BPMs", this.clockBuffer);
console.log("Deltas", this.deltaBuffer); console.log("Deltas", this.deltaBuffer);
this.clockErrorCount = 0; this.clockErrorCount = 0;
this.skipOnError = this.clockPPQN/4; // Skip quarter of the pulses /* I dont know why this happens. But when it does, deltas for the following messages are off.
So skipping ~ quarted of clock resolution usually helps */
this.skipOnError = this.clockPPQN/4;
timestamp = 0; // timestamp 0 == lastTimestamp 0 timestamp = 0; // timestamp 0 == lastTimestamp 0
} else { } else {
if(this.midiClockDelta === 0) {
this.midiClockDelta = timestamp - this.lastTimestamp; this.midiClockDelta = timestamp - this.lastTimestamp;
this.lastBPM = 60 * (1000 / this.midiClockDelta / 24); this.lastBPM = 60 * (1000 / this.midiClockDelta / 24);
} else {
const lastDelta = this.midiClockDelta * (1.0 - SMOOTH);
this.midiClockDelta = timestamp - this.lastTimestamp;
this.lastBPM = (60 * (1000 / (this.midiClockDelta*SMOOTH+lastDelta) / 24) * SMOOTH) + (this.lastBPM * (1.0 - SMOOTH));
}
this.deltaBuffer.push(this.midiClockDelta);
if(this.deltaBuffer.length>this.clockBufferLength) this.deltaBuffer.shift();
this.clockBuffer.push(this.lastBPM); this.clockBuffer.push(this.lastBPM);
if(this.clockBuffer.length>this.clockBufferLength) this.clockBuffer.shift(); if(this.clockBuffer.length>this.clockBufferLength) this.clockBuffer.shift();
const estimatedBPM = this.estimatedBPM(); const estimatedBPM = this.estimatedBPM();
if(estimatedBPM !== this.roundedBPM) { if(estimatedBPM !== this.roundedBPM) {
console.log("Esimated BPM: ", estimatedBPM);
this.api.bpm(estimatedBPM); this.api.bpm(estimatedBPM);
this.roundedBPM = estimatedBPM; this.roundedBPM = estimatedBPM;
console.log(this.roundedBPM);
} }
} }
@ -280,11 +282,13 @@ export class MidiConnection {
/** /**
* Sends a single MIDI clock message to the currently selected MIDI output. * Sends a single MIDI clock message to the currently selected MIDI output.
*/ */
if(!this.midiClockInput) {
const output = this.midiOutputs[this.currentOutputIndex]; const output = this.midiOutputs[this.currentOutputIndex];
if (output) { if (output) {
output.send([0xf8]); // Send a single MIDI clock message output.send([0xf8]); // Send a single MIDI clock message
} }
} }
}
public switchMidiOutput(outputName: string): boolean { public switchMidiOutput(outputName: string): boolean {
/** /**

View File

@ -99,6 +99,7 @@ export class Player extends Event {
this.index = 0; this.index = 0;
this.waitTime = 0; this.waitTime = 0;
this.skipIndex = 0; this.skipIndex = 0;
this.ziffers.index = 0;
} }
const patternIsStarting = (this.notStarted() && const patternIsStarting = (this.notStarted() &&