LabDAW

Piano

A piano keyboard for a MIDI pitch range, played with the pointer, touch or the keyboard.

Plays or shows notes, for a virtual instrument, a MIDI visualizer or a chord reference. The registry component is written on the piano primitive, which installs with it, and draws white and black keys for any MIDI range.

Overview

  • Range: minPitch and maxPitch are MIDI pitches (default 48 and 72, C3 to C5), or pitches lists the exact keys to show. Key colors and layout follow the chromatic pattern.
  • Events: onNoteOn(pitch, eventDetails) and onNoteOff(pitch, eventDetails) report MIDI pitches, ready for an audio engine. eventDetails.reason says how the note started ("press", "glide" or "keyboard"), and eventDetails.cancel() in onNoteOn keeps the key silent.
  • Pointer: press and hold a key to hold its note. Drag across keys for a glissando: each key plays as the pointer crosses it. Each touch holds its own key, so chords work on a touch screen.
  • External notes: activeNotes lights keys from outside, such as a MIDI input or a sequencer's current chord, without calling onNoteOn or onNoteOff.
  • Accessibility: each key is a button named after its pitch ("C4"), behind one tab stop. The arrow keys move between keys, and Space or Enter holds the focused key until it is released.
  • Layout: the primitive can also lay keys out vertically (orientation="vertical", lowest key at the bottom) or in equal widths (layout="even"). The piano roll's keyboard uses this.

Installation

The primitives are built on the UI SDK kernel, in beta. Nothing of ours comes from npm: the primitive and its adapter are files you copy into your project and own, and the only packages are the kernel's. The commands name its version, @ui-sdk/*@0.1.0-beta.14.

The styled component, with sizes and themed keys, is one command:

The styled component, with size variants and theme styles:

pnpm dlx shadcn@latest add @audio-ui/piano

Or the primitive alone, to style yourself:

pnpm dlx shadcn@latest add @audio-ui/piano-react

It writes the primitive, its adapter and primitive.ts into components/audio-ui/ and installs the kernel's packages and what each file imports. The optional styles are one more command:

pnpm dlx shadcn@latest add @audio-ui/primitives-css

Usage

import { Piano } from "@/components/audio-ui/piano";

Note events:

<Piano
  maxPitch={72}
  minPitch={48}
  onNoteOff={(pitch) => synth.release(pitch)}
  onNoteOn={(pitch) => synth.trigger(pitch)}
/>

External notes (MIDI input, a sequencer's current chord):

<Piano activeNotes={midiInputNotes} maxPitch={72} minPitch={48} />

A MIDI keyboard: useMidiInput listens to the page's MIDI input. Browsers ask for permission, so connect it from a click: with requestAccess, or the ready-made button, npx shadcn@latest add @audio-ui/midi-connect-control.

import { useMidiInput } from "@/hooks/use-midi-input";

const midi = useMidiInput({
  onNoteOff: (pitch) => synth.release(pitch),
  onNoteOn: (pitch, velocity) => synth.trigger(pitch, velocity),
});

<button onClick={midi.requestAccess} type="button">Connect MIDI</button>
<Piano activeNotes={midi.activeNotes} maxPitch={72} minPitch={48} />

Everything useMidiInput gives is in Input.

The primitive alone, part by part:

A Root is the keyboard, and a Key is one key, a <button> placed along it for its pitch and named by its note, such as "C4". A Label inside a Key writes that name for you to style. You write a Key for each pitch: the Root's content can read the state, whose pitches lists them lowest first, so the keys follow minPitch, maxPitch and pitches. That is a function in React, the default slot in Vue and a snippet in Svelte.

Give the Root its size. The keys are placed with percentages, so a Root with no height or width has nothing to place them in.

import { Piano } from "@/components/audio-ui/piano/react";

export function Keys({ on, off }: { on: (pitch: number, velocity: number) => void; off: (pitch: number) => void }) {
  return (
    <Piano.Root
      className="relative h-32 w-96"
      maxPitch={72}
      minPitch={60}
      onNoteOff={off}
      onNoteOn={(pitch, { velocity }) => on(pitch, velocity)}
    >
      {({ pitches }) =>
        pitches.map((pitch) => (
          <Piano.Key
            className="rounded-b border bg-background data-[color=black]:z-10 data-[color=black]:h-2/3 data-[color=black]:bg-foreground data-active:bg-primary"
            key={pitch}
            pitch={pitch}
          />
        ))
      }
    </Piano.Root>
  );
}

In the web adapter, the keyboard makes a key for each pitch, each a wrapper around a <button>. To style or fill them yourself, write the audio-ui-piano-key elements with a pitch and a button inside: the keyboard then leaves them as they are, and an audio-ui-piano-label inside a button gets its note.

What a note reports

  • onNoteOn gets velocity from 0 to 1, a reason ("press", "glide" or "keyboard"), a source and the event.
  • onNoteOff gets a reason: "press", "glide" or "keyboard".
  • In the web adapter, they are the noteon and noteoff events, with detail: { value, reason, source, velocity }, where value is the pitch.

Interaction

Pointer

Behavior · 4

Press a key
Fires onNoteOn; the key stays pressed while held.
Release
Fires onNoteOff.
Drag across keys
Glissando: the previous key releases (onNoteOff) and the next one sounds (onNoteOn) as the pointer crosses it.
Multiple pointers
Each pointer (finger) holds its own key, so chords work on touch devices.

Keyboard

The keys share one tab stop. On the focused key:

Keys · 4

ArrowLeft / ArrowRight (or ArrowDown / ArrowUp)
Move focus to the next lower / higher key
Shift + arrow, PageDown / PageUp
Move focus an octave down / up
Space / Enter
Hold the focused key: onNoteOn on press, onNoteOff on release
Home / End
Move focus to the lowest / highest key

API reference

Registry component

Piano accepts style props (size, className) plus the props of the Piano primitive's Root.

Value and state

Props · 9

Layout and display

Props · 2

Additional valid HTML attributes passed to Piano.Root are forwarded.

Options

Set on the Root, as props (an attribute in kebab case in the web adapter).

OptionTypeDefault
minPitch, maxPitchnumber48, 72The lowest and highest MIDI pitch, inclusive: 60 is middle C.
pitchesnumber[]Only these pitches get keys, instead of minPitch to maxPitch.
layout"keyboard" or "even""keyboard"Like a piano, with the black keys between the white ones, or one equal share for each key, as the rows of a piano roll are.
orientation"horizontal" or "vertical""horizontal"Which way the keys run. A vertical keyboard puts the low end at the bottom.
activeNotesnumber[]Pitches lit from elsewhere, such as a MIDI input or a sequencer, on top of the keys pressed here.
disabledbooleanfalsePlays nothing.
onNoteOn(pitch, details) => voidA key sounds. details.cancel() keeps it silent.
onNoteOff(pitch, details) => voidA key is released, the pointer glides off it, or it loses focus while held.

States to style from

  • data-active on a key while it sounds, pressed here or listed in activeNotes.
  • data-color on a key, "black" or "white", data-disabled, data-layout and data-orientation.

Parts

Parts · 3

Events

Events · 2

Examples

External notes via activeNotes

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