diff --git a/packages/midi/midi.mjs b/packages/midi/midi.mjs index 32f030125..0fee380c6 100644 --- a/packages/midi/midi.mjs +++ b/packages/midi/midi.mjs @@ -479,14 +479,23 @@ Pattern.prototype.midi = function (midiport, options = {}) { let listeners = {}; const refs = {}; +const refsByChan = {}; /** * MIDI input: Opens a MIDI input port to receive MIDI control change messages. + * + * The output is a function that accepts a midi cc value to query as well as (optionally) a midi channel * @param {string | number} input MIDI device name or index defaulting to 0 - * @returns {Function} + * @returns {function(number, number=): function(): number} A function from (cc, channel?) to + * the most recently received midi cc value through the input (normalized to 0 to 1) * @example - * let cc = await midin('IAC Driver Bus 1') + * const 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") + * @example + * const allCC = await midin('IAC Driver Bus 1') + * const cc = (ccNum) => allCC(ccNum, 2) // just channel 2 + * note("c a f e").s("saw") + * .when(cc(0).gt(0), x => x.postgain(0)) */ export async function midin(input) { if (isPattern(input)) { @@ -511,17 +520,24 @@ export async function midin(input) { }`, ); } - // ensure refs for this input are initialized - if (!refs[input]) { - refs[input] = {}; - } - const cc = (cc) => ref(() => refs[input][cc] || 0); + refs[input] ??= {}; + refsByChan[input] ??= {}; + const cc = (cc, chan) => { + if (chan !== undefined) { + return ref(() => refsByChan[input][cc]?.[chan] || 0); + } + return ref(() => refs[input][cc] || 0); + }; listeners[input] && device.removeListener('midimessage', listeners[input]); listeners[input] = (e) => { - const cc = e.dataBytes[0]; + const ccNum = e.dataBytes[0]; const v = e.dataBytes[1]; - refs[input] && (refs[input][cc] = v / 127); + const chan = e.message.channel; + const scaled = v / 127; + refsByChan[input][ccNum] ??= {}; + refsByChan[input][ccNum][chan] = scaled; + refs[input][ccNum] = scaled; }; device.addListener('midimessage', listeners[input]); return cc; diff --git a/test/__snapshots__/examples.test.mjs.snap b/test/__snapshots__/examples.test.mjs.snap index 31e4bddd3..783fee998 100644 --- a/test/__snapshots__/examples.test.mjs.snap +++ b/test/__snapshots__/examples.test.mjs.snap @@ -6533,6 +6533,27 @@ exports[`runs examples > example "midin" example index 0 1`] = ` ] `; +exports[`runs examples > example "midin" example index 1 1`] = ` +[ + "[ 0/1 → 1/4 | note:c s:saw ]", + "[ 1/4 → 1/2 | note:a s:saw ]", + "[ 1/2 → 3/4 | note:f s:saw ]", + "[ 3/4 → 1/1 | note:e s:saw ]", + "[ 1/1 → 5/4 | note:c s:saw ]", + "[ 5/4 → 3/2 | note:a s:saw ]", + "[ 3/2 → 7/4 | note:f s:saw ]", + "[ 7/4 → 2/1 | note:e s:saw ]", + "[ 2/1 → 9/4 | note:c s:saw ]", + "[ 9/4 → 5/2 | note:a s:saw ]", + "[ 5/2 → 11/4 | note:f s:saw ]", + "[ 11/4 → 3/1 | note:e s:saw ]", + "[ 3/1 → 13/4 | note:c s:saw ]", + "[ 13/4 → 7/2 | note:a s:saw ]", + "[ 7/2 → 15/4 | note:f s:saw ]", + "[ 15/4 → 4/1 | note:e s:saw ]", +] +`; + exports[`runs examples > example "midiport" example index 0 1`] = ` [ "[ 0/1 → 1/4 | note:c midiport:0 ]",