Add cc to midicmd, add API Reference for midi related controls

This commit is contained in:
nkymut 2025-01-24 05:37:11 +08:00
parent 57c48f0c45
commit d06a75a2cd
2 changed files with 110 additions and 24 deletions

View file

@ -93,6 +93,12 @@ if (typeof window !== 'undefined') {
});
}
/**
* MIDI output: Opens a MIDI output port.
* @param {string | number} output MIDI device name or index defaulting to 0
* @example
* note("c4").midichan(1).midi("IAC Driver Bus 1")
*/
Pattern.prototype.midi = function (output) {
if (isPattern(output)) {
throw new Error(
@ -105,6 +111,7 @@ Pattern.prototype.midi = function (output) {
let isController = false;
let mapping = {};
//TODO: MIDI mapping related
if (typeof output === 'object') {
const { port, controller = false, ...remainingProps } = output;
portName = port;
@ -187,26 +194,16 @@ Pattern.prototype.midi = function (output) {
const ccvLsb = Math.round(value * 16383) & 0b1111111;
device.sendControlChange(ccnLsb, ccvLsb, paramSpec.channel || midichan, { time: timeOffsetString });
}
} else if (paramSpec.pc !== undefined) {
} else if (paramSpec.progNum !== undefined) {
if (typeof value !== 'number' || value < 0 || value > 127) {
throw new Error(`Expected ${name} to be a number between 0 and 127 for program change`);
}
device.sendProgramChange(value, paramSpec.channel || midichan, { time: timeOffsetString });
}
// ToDo: support sysex for mapped parameters
// } else if (paramSpec.sysex) {
// if (!Array.isArray(value)) {
// throw new Error(`Expected ${name} to be an array of numbers (0-255) for sysex`);
// }
// if (!value.every((byte) => Number.isInteger(byte) && byte >= 0 && byte <= 255)) {
// throw new Error(`All sysex bytes in ${name} must be integers between 0 and 255`);
// }
// device.sendSysex(0x43, value, { time: timeOffsetString });
// //device.sendSysex(0x43, [0x79, 0x09, 0x11, 0x0A, 0x00,0x02], { time: timeOffsetString });
// }
}
});
}
// Handle program change
if (progNum !== undefined) {
if (typeof progNum !== 'number' || progNum < 0 || progNum > 127) {
@ -283,6 +280,16 @@ Pattern.prototype.midi = function (output) {
} else {
device.sendProgramChange(midicmd[1], midichan, { time: timeOffsetString });
}
} else if (midicmd[0] === 'cc') {
if (midicmd.length === 2) {
if (typeof midicmd[0] !== 'number' || midicmd[0] < 0 || midicmd[0] > 127) {
throw new Error('expected ccn (control change number) to be a number between 0 and 127');
}
if (typeof midicmd[1] !== 'number' || midicmd[1] < 0 || midicmd[1] > 127) {
throw new Error('expected ccv (control change value) to be a number between 0 and 127');
}
device.sendControlChange(midicmd[0], midicmd[1], midichan, { time: timeOffsetString });
}
}
}
});
@ -291,6 +298,14 @@ Pattern.prototype.midi = function (output) {
let listeners = {};
const refs = {};
/**
* MIDI input: Opens a MIDI input port to receive MIDI control change messages.
* @param {string | number} input MIDI device name or index defaulting to 0
* @returns {Function}
* @example
* let cc = await midin("IAC Driver Bus 1")
* note("c a f e").lpf(cc(0).range(0, 1000)).lpq(cc(1).range(0, 10)).sound("sawtooth")
*/
export async function midin(input) {
if (isPattern(input)) {
throw new Error(