LabDAW

Scale menu

A menu that picks a root and a scale and turns the scale on or off, in parts you place beside items of your own.

Audio UI

Installation

pnpm dlx shadcn@latest add @audio-ui/scale-menu

Anatomy

<ScaleMenu>
  <ScaleMenuTrigger />
  <ScaleMenuContent>
    <ScaleMenuToggle />
    <ScaleMenuRoot />
    <ScaleMenuName />
  </ScaleMenuContent>
</ScaleMenu>

Usage

import type { Scale } from "@/lib/engine/pitch";
import {
  ScaleMenu,
  ScaleMenuContent,
  ScaleMenuName,
  ScaleMenuRoot,
  ScaleMenuToggle,
  ScaleMenuTrigger,
} from "@/components/audio-ui/scale-menu";
import { DropdownMenuSeparator } from "@/components/ui/dropdown-menu";

const [enabled, setEnabled] = useState(false);
const [scale, setScale] = useState<Scale>({ name: "major", root: 0 });

<ScaleMenu
  enabled={enabled}
  onEnabledChange={setEnabled}
  onScaleChange={setScale}
  scale={scale}
>
  <ScaleMenuTrigger />
  <ScaleMenuContent>
    <ScaleMenuToggle />
    <DropdownMenuSeparator />
    <ScaleMenuRoot />
    <ScaleMenuName />
  </ScaleMenuContent>
</ScaleMenu>;

A scale is { root, name }: root is a pitch class, 0 for C to 11 for B, and name one of the scales in SCALES from @/lib/engine/pitch ("major", "minor", "dorian", "blues"…). The menu keeps the scale while it is off, so turning it back on brings back the last one; picking a root or a scale turns it on.

Pass enabled ? scale : null to whatever marks the scale: a Piano roll's scale marks its rows, and inScale(pitch, scale) from @/lib/engine/pitch tells whether any pitch is in it.

Items of your own

ScaleMenuContent is a dropdown menu's content, so any item goes beside the parts. The piano roll block adds a checkbox that folds the roll onto the scale's rows, and shows each item's key:

<ScaleMenuContent>
  <ScaleMenuToggle>
    Mark the scale
    <DropdownMenuShortcut>K</DropdownMenuShortcut>
  </ScaleMenuToggle>
  <DropdownMenuCheckboxItem checked={folded} onCheckedChange={setFolded}>
    Only its rows
    <DropdownMenuShortcut>G</DropdownMenuShortcut>
  </DropdownMenuCheckboxItem>
  <DropdownMenuSeparator />
  <ScaleMenuRoot />
  <ScaleMenuName />
</ScaleMenuContent>

Detect the key

ScaleMenuDetect offers the keys some notes are likeliest in, three at most, likeliest first: picking one sets the root and the scale, major or minor, and turns it on. Each pitch class is weighed by how long it sounds and matched against the major and minor key profiles of @audio/mir-key (Krumhansl-Schmuckler).

<ScaleMenuContent>
  <ScaleMenuRoot />
  <ScaleMenuName />
  <DropdownMenuSeparator />
  <ScaleMenuDetect notes={notes} />
</ScaleMenuContent>

Pass every part of the song, not one. On 60 loops of known key, a single bass or guitar part named its key 21 times in 60 (39 times in its first three); the parts of each song together named it 8 times in 10. One part alone is often ambiguous: a bass line in E minor reads as well in C major.

Recordings count too: audio takes a weight per pitch class, C to B, in the unit of the notes' lengths, such as the audioChroma(channels, sampleRate) of each recording (from @/lib/engine/analysis, @audio/mir-chroma on its windows that are not silence) times the seconds it plays. The audio of the same songs, guitar and bass together, had the key first 6 times in 10 and in its first three 9 times in 10. detectKeys(notes, count, audio), from @/lib/engine/pitch, gives the same keys without the menu.

API reference

ScaleMenu

Props · 4

Parts

Components · 6

ScaleMenuTrigger
The button that opens the menu: filled and named after the scale ("D Dorian") when it is on, outlined and named "Scale" when off. Takes the button's props.
ScaleMenuContent
The menu. Aligned to the trigger's end unless given align.
ScaleMenuToggle
A checkbox item that turns the scale on or off. Its children are the label, "Mark the scale" when left out.
ScaleMenuRoot
A submenu of the twelve roots.
ScaleMenuName
A submenu of the scales.
ScaleMenuDetect
The keys notes (each { pitch, length, muted? }) and audio (a weight per pitch class) are likeliest in, count at most (3), under a "Detected" label. One picked sets the scale and turns it on.

The file also exports SCALE_LABELS, each scale's name as the menu writes it, and scaleLabel(scale), which writes a scale as "D Dorian"; the keys ScaleMenuDetect offers are detectKeys(notes, count, audio) of @/lib/engine/pitch.

Built by Ouest Labs, the source code is available on GitHub.
RSS