From 3b215b622f33fe656e880178d7fe060ee19aeeb2 Mon Sep 17 00:00:00 2001 From: Raphael Forment Date: Wed, 16 Aug 2023 13:59:16 +0200 Subject: [PATCH] code quality --- src/API.ts | 22 +++++++++++++++------- src/Clock.ts | 17 ++++++++++++++--- src/IO/MidiConnection.ts | 5 +++-- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/API.ts b/src/API.ts index 89a199e..c61d084 100644 --- a/src/API.ts +++ b/src/API.ts @@ -16,6 +16,13 @@ import { // This is an LRU cache used for storing persistent patterns const cache = new LRUCache({max: 1000, ttl: 1000 * 60 * 5}); +interface ControlChange { + channel: number + control: number + value: number + } + + interface Pattern { pattern: any[]; options: { @@ -68,7 +75,7 @@ export class UserAPI { get time(): number { /** - * @returns The current time for the AudioContext + * @returns the current AudioContext time (wall clock) */ return this.app.audioContext.currentTime; } @@ -112,7 +119,7 @@ export class UserAPI { } s = this.script; - clearscript(script: number): void { + clear_script(script: number): void { /** * Clears a local script * @@ -124,9 +131,9 @@ export class UserAPI { evaluations: 0, }; } - cs = this.clearscript; + cs = this.clear_script; - copyscript(from: number, to: number): void { + copy_script(from: number, to: number): void { /** * Copy from a local script to another local script * @@ -136,7 +143,7 @@ export class UserAPI { this.app.universes[this.app.selected_universe].locals[to] = this.app.universes[this.app.selected_universe].locals[from]; } - cps = this.copyscript; + cps = this.copy_script; // ============================================================= // MIDI related functions @@ -217,14 +224,15 @@ export class UserAPI { this.MidiConnection.sendMidiClock(); } - public cc(control: number, value: number): void { + + public control_change({ control= 20, value= 0, channel=0 }: ControlChange): void { /** * Sends a MIDI control change to the current MIDI output. * * @param control - The MIDI control to send * @param value - The value of the control */ - this.MidiConnection.sendMidiControlChange(control, value); + this.MidiConnection.sendMidiControlChange(control, value, channel); } public midi_panic(): void { diff --git a/src/Clock.ts b/src/Clock.ts index 2d6d793..223a0b3 100644 --- a/src/Clock.ts +++ b/src/Clock.ts @@ -78,11 +78,22 @@ export class Clock { } get beats_since_origin(): number { + /** + * Returns the number of beats since the origin. + * + * @returns number of beats since origin + */ return (this.time_position.bar - 1) * this.beats_per_bar + this.time_position.beat; } get pulses_since_origin(): number { + /** + * Returns the number of pulses since the origin. + * + * @returns number of pulses since origin + */ return (this.beats_since_origin * this.ppqn) + this.time_position.pulse + } get pulse_duration(): number { @@ -100,7 +111,7 @@ export class Clock { } - start(): void { + public start(): void { /** * Starts the TransportNode (starts the clock). */ @@ -113,14 +124,14 @@ export class Clock { } } - pause(): void { + public pause(): void { /** * Pauses the TransportNode (pauses the clock). */ this.transportNode?.pause(); } - stop(): void { + public stop(): void { /** * Stops the TransportNode (stops the clock). */ diff --git a/src/IO/MidiConnection.ts b/src/IO/MidiConnection.ts index 4630a9b..0927332 100644 --- a/src/IO/MidiConnection.ts +++ b/src/IO/MidiConnection.ts @@ -182,16 +182,17 @@ export class MidiConnection{ } } - public sendMidiControlChange(controlNumber: number, value: number): void { + public sendMidiControlChange(controlNumber: number, value: number, channel: number): void { /** * Sends a MIDI Control Change message to the currently selected MIDI output. * * @param controlNumber MIDI control number (0-127) * @param value MIDI control value (0-127) + * @param channel MIDI channel (0-15) */ const output = this.midiOutputs[this.currentOutputIndex]; if (output) { - output.send([0xB0, controlNumber, value]); // Control Change + output.send([0xB0 + channel, controlNumber, value]); // Control Change } else { console.error('MIDI output not available.'); }