Add docstrings to MIDI file
This commit is contained in:
@ -1,4 +1,15 @@
|
|||||||
export class MidiConnection{
|
export class MidiConnection{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper class for Web MIDI API. Provides methods for sending MIDI messages.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* @param midiAccess - Web MIDI API access object
|
||||||
|
* @param midiOutputs - Array of MIDI output objects
|
||||||
|
* @param currentOutputIndex - Index of the currently selected MIDI output
|
||||||
|
* @param scheduledNotes - Object containing scheduled notes. Keys are note numbers and values are timeout IDs.
|
||||||
|
*/
|
||||||
|
|
||||||
private midiAccess: MIDIAccess | null = null;
|
private midiAccess: MIDIAccess | null = null;
|
||||||
public midiOutputs: MIDIOutput[] = [];
|
public midiOutputs: MIDIOutput[] = [];
|
||||||
private currentOutputIndex: number = 0;
|
private currentOutputIndex: number = 0;
|
||||||
@ -9,6 +20,11 @@ export class MidiConnection{
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async initializeMidiAccess(): Promise<void> {
|
private async initializeMidiAccess(): Promise<void> {
|
||||||
|
/**
|
||||||
|
* Initializes Web MIDI API access and populates the list of MIDI outputs.
|
||||||
|
*
|
||||||
|
* @returns Promise
|
||||||
|
*/
|
||||||
try {
|
try {
|
||||||
this.midiAccess = await navigator.requestMIDIAccess();
|
this.midiAccess = await navigator.requestMIDIAccess();
|
||||||
this.midiOutputs = Array.from(this.midiAccess.outputs.values());
|
this.midiOutputs = Array.from(this.midiAccess.outputs.values());
|
||||||
@ -21,16 +37,24 @@ export class MidiConnection{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public getCurrentMidiPort(): string | null {
|
public getCurrentMidiPort(): string | null {
|
||||||
if (this.midiOutputs.length > 0 && this.currentOutputIndex >= 0 && this.currentOutputIndex < this.midiOutputs.length) {
|
/**
|
||||||
return this.midiOutputs[this.currentOutputIndex].name;
|
* Returns the name of the currently selected MIDI output.
|
||||||
} else {
|
*
|
||||||
console.error('No MIDI output selected or available.');
|
* @returns Name of the currently selected MIDI output or null if no MIDI output is selected or available.
|
||||||
return null;
|
*/
|
||||||
}
|
if (this.midiOutputs.length > 0 && this.currentOutputIndex >= 0 && this.currentOutputIndex < this.midiOutputs.length) {
|
||||||
|
return this.midiOutputs[this.currentOutputIndex].name;
|
||||||
|
} else {
|
||||||
|
console.error('No MIDI output selected or available.');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public sendMidiClock(): void {
|
public sendMidiClock(): void {
|
||||||
|
/**
|
||||||
|
* Sends a single MIDI clock message to the currently selected MIDI output.
|
||||||
|
*/
|
||||||
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
|
||||||
@ -41,6 +65,12 @@ export class MidiConnection{
|
|||||||
|
|
||||||
|
|
||||||
public switchMidiOutput(outputName: string): boolean {
|
public switchMidiOutput(outputName: string): boolean {
|
||||||
|
/**
|
||||||
|
* Switches the currently selected MIDI output.
|
||||||
|
*
|
||||||
|
* @param outputName Name of the MIDI output to switch to
|
||||||
|
* @returns True if the MIDI output was found and switched to, false otherwise
|
||||||
|
*/
|
||||||
const index = this.midiOutputs.findIndex((output) => output.name === outputName);
|
const index = this.midiOutputs.findIndex((output) => output.name === outputName);
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
this.currentOutputIndex = index;
|
this.currentOutputIndex = index;
|
||||||
@ -52,6 +82,9 @@ export class MidiConnection{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public listMidiOutputs(): void {
|
public listMidiOutputs(): void {
|
||||||
|
/**
|
||||||
|
* Lists all available MIDI outputs to the console.
|
||||||
|
*/
|
||||||
console.log('Available MIDI Outputs:');
|
console.log('Available MIDI Outputs:');
|
||||||
this.midiOutputs.forEach((output, index) => {
|
this.midiOutputs.forEach((output, index) => {
|
||||||
console.log(`${index + 1}. ${output.name}`);
|
console.log(`${index + 1}. ${output.name}`);
|
||||||
@ -91,6 +124,12 @@ export class MidiConnection{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public sendMidiControlChange(controlNumber: number, value: number): void {
|
public sendMidiControlChange(controlNumber: number, value: 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)
|
||||||
|
*/
|
||||||
const output = this.midiOutputs[this.currentOutputIndex];
|
const output = this.midiOutputs[this.currentOutputIndex];
|
||||||
if (output) {
|
if (output) {
|
||||||
output.send([0xB0, controlNumber, value]); // Control Change
|
output.send([0xB0, controlNumber, value]); // Control Change
|
||||||
@ -100,6 +139,9 @@ export class MidiConnection{
|
|||||||
}
|
}
|
||||||
|
|
||||||
public panic(): void {
|
public panic(): void {
|
||||||
|
/**
|
||||||
|
* Sends a Note Off message for all scheduled notes.
|
||||||
|
*/
|
||||||
const output = this.midiOutputs[this.currentOutputIndex];
|
const output = this.midiOutputs[this.currentOutputIndex];
|
||||||
if (output) {
|
if (output) {
|
||||||
for (const noteNumber in this.scheduledNotes) {
|
for (const noteNumber in this.scheduledNotes) {
|
||||||
|
|||||||
Reference in New Issue
Block a user