K

Build chords

Turn one note into a chord.

To stamp a chord from a single note, write the chord's MIDI notes into a fixed buffer with buildChord, then emit each one. Size the buffer for the worst case (8 covers the largest quality).

private buf: StaticArray<i32> = new StaticArray<i32>(8);

buildChord(root, quality, out) fills the buffer and returns the note count:

const quality = <i32>Math.round(ctx.param(0)); // a ChordQuality index
const count = ctx.midiInCount();
for (let e = 0; e < count; e++) {
  const ev = ctx.midiEvent(e);
  if (ev.isNoteOn()) {
    const n = buildChord(ev.note, quality, this.buf);
    for (let i = 0; i < n; i++)
      ctx.noteOn(ev.offset, this.buf[i], ev.velocity, ev.channel);
  } else if (ev.isNoteOff()) {
    const n = buildChord(ev.note, quality, this.buf);
    for (let i = 0; i < n; i++) ctx.noteOff(ev.offset, this.buf[i], ev.channel);
  }
}

Let the user pick the quality with ParamDef.chordSelect("quality", "Chord", "Chord"): the value is the ChordQuality index, so it reads straight into buildChord.

Reshape the chord in place after building it: invert(buf, count, n) rotates voices up, dropVoicing(buf, count, n) drops a voice an octave. To follow a scale instead of a fixed quality, diatonicSeventh(degree, octave, root, scaleId, buf) stacks thirds within the key.

See Harmony for the quality catalog and interval constants.