import { chordsOf } from "../midi/chords.ts"; import type { MidiPart } from "../midi/smf.ts"; export type PartKind = "chords" | "notes"; /** ask whether each part of a midi file comes in as chords or as melody: null if cancelled */ export function choosePartKinds( fileName: string, parts: readonly MidiPart[], ): Promise { return new Promise(resolve => { const done = (choice: PartKind[] | null) => { dialog.close(); dialog.remove(); resolve(choice); }; const rows = parts.map(part => { const chords = chordsOf(part.notes); const timeWith = (atLeast: number) => chords.filter(c => c.notes.length >= atLeast).reduce((t, c) => t + c.dur, 0); const most = Math.max(...chords.map(c => c.notes.length)); const select = ( ) as HTMLSelectElement; select.value = timeWith(2) > timeWith(1) / 2 ? "chords" : "notes"; return { select, row: (
{part.name} {`${part.notes.length} notes ยท ${most === 1 ? "one" : `up to ${most}`} at once`} {select}
), }; }); const confirm = ( ) as HTMLButtonElement; const dialog = ( done(null)}>

{fileName}

{rows.map(r => r.row)}
{confirm}
) as HTMLDialogElement; document.body.append(dialog); dialog.showModal(); confirm.focus(); }); }