{"spec_id":"piano-roll-midi","library":"muix","language":"javascript","code":"// anyplot.ai\n// piano-roll-midi: MIDI Piano Roll Visualization\n// Library: muix 9.16.0 | JavaScript 22.23.3\n// Quality: 85/100 | Updated: 2026-10-24\n\nimport { ChartsContainer } from \"@mui/x-charts/ChartsContainer\";\nimport { ChartsXAxis } from \"@mui/x-charts/ChartsXAxis\";\nimport { ChartsYAxis } from \"@mui/x-charts/ChartsYAxis\";\nimport { useXScale, useYScale, useDrawingArea } from \"@mui/x-charts/hooks\";\nimport { Box, Typography } from \"@mui/material\";\nimport { decomposeColor } from \"@mui/material/styles\";\n\nconst t = window.ANYPLOT_TOKENS;\n\n// Constants\nconst TITLE = \"piano-roll-midi · javascript · muix · anyplot.ai\";\nconst BEATS_PER_MEASURE = 4;\nconst PITCH_PAD_SHARE = 0.08;\nconst MIN_PITCH_PAD_SEMITONES = 2;\nconst BEAT_PAD_SHARE = 0.012;\nconst TIME_AXIS_TITLE = \"Time (measures)\";\nconst PITCH_AXIS_TITLE = \"Pitch (note)\";\nconst NOTE_NAMES = [\"C\", \"C#\", \"D\", \"D#\", \"E\", \"F\", \"F#\", \"G\", \"G#\", \"A\", \"A#\", \"B\"];\nconst BLACK_SEMITONES = [1, 3, 6, 8, 10];\n\n// Columns: set these to your data's column names\nconst START = \"Onset (beats)\"; // role: start\nconst DURATION = \"Duration (beats)\"; // role: duration\nconst PITCH = \"Pitch (MIDI note)\"; // role: y\nconst VELOCITY = \"Velocity\"; // role: value\n\n// Data: replace this block with your own records\nconst data: Array<Record<string, number>> = [\n  // Measure 1 (C major)\n  { [START]: 0, [DURATION]: 4, [PITCH]: 48, [VELOCITY]: 78 },\n  { [START]: 0, [DURATION]: 2, [PITCH]: 60, [VELOCITY]: 58 },\n  { [START]: 0, [DURATION]: 2, [PITCH]: 64, [VELOCITY]: 58 },\n  { [START]: 0, [DURATION]: 2, [PITCH]: 67, [VELOCITY]: 58 },\n  { [START]: 2, [DURATION]: 2, [PITCH]: 60, [VELOCITY]: 64 },\n  { [START]: 2, [DURATION]: 2, [PITCH]: 64, [VELOCITY]: 64 },\n  { [START]: 2, [DURATION]: 2, [PITCH]: 67, [VELOCITY]: 64 },\n  { [START]: 0, [DURATION]: 1, [PITCH]: 72, [VELOCITY]: 88 },\n  { [START]: 1, [DURATION]: 1, [PITCH]: 74, [VELOCITY]: 82 },\n  { [START]: 2, [DURATION]: 0.5, [PITCH]: 72, [VELOCITY]: 88 },\n  { [START]: 2.5, [DURATION]: 0.5, [PITCH]: 71, [VELOCITY]: 82 },\n  { [START]: 3, [DURATION]: 1, [PITCH]: 69, [VELOCITY]: 88 },\n\n  // Measure 2 (C major)\n  { [START]: 4, [DURATION]: 4, [PITCH]: 48, [VELOCITY]: 81 },\n  { [START]: 4, [DURATION]: 2, [PITCH]: 60, [VELOCITY]: 60 },\n  { [START]: 4, [DURATION]: 2, [PITCH]: 64, [VELOCITY]: 60 },\n  { [START]: 4, [DURATION]: 2, [PITCH]: 67, [VELOCITY]: 60 },\n  { [START]: 6, [DURATION]: 2, [PITCH]: 60, [VELOCITY]: 66 },\n  { [START]: 6, [DURATION]: 2, [PITCH]: 64, [VELOCITY]: 66 },\n  { [START]: 6, [DURATION]: 2, [PITCH]: 67, [VELOCITY]: 66 },\n  { [START]: 4, [DURATION]: 1, [PITCH]: 72, [VELOCITY]: 91 },\n  { [START]: 5, [DURATION]: 1, [PITCH]: 74, [VELOCITY]: 85 },\n  { [START]: 6, [DURATION]: 0.5, [PITCH]: 72, [VELOCITY]: 91 },\n  { [START]: 6.5, [DURATION]: 0.5, [PITCH]: 71, [VELOCITY]: 85 },\n  { [START]: 7, [DURATION]: 1, [PITCH]: 69, [VELOCITY]: 91 },\n\n  // Measure 3 (A minor)\n  { [START]: 8, [DURATION]: 4, [PITCH]: 45, [VELOCITY]: 84 },\n  { [START]: 8, [DURATION]: 2, [PITCH]: 57, [VELOCITY]: 62 },\n  { [START]: 8, [DURATION]: 2, [PITCH]: 60, [VELOCITY]: 62 },\n  { [START]: 8, [DURATION]: 2, [PITCH]: 64, [VELOCITY]: 62 },\n  { [START]: 10, [DURATION]: 2, [PITCH]: 57, [VELOCITY]: 68 },\n  { [START]: 10, [DURATION]: 2, [PITCH]: 60, [VELOCITY]: 68 },\n  { [START]: 10, [DURATION]: 2, [PITCH]: 64, [VELOCITY]: 68 },\n  { [START]: 8, [DURATION]: 1, [PITCH]: 69, [VELOCITY]: 94 },\n  { [START]: 9, [DURATION]: 1, [PITCH]: 71, [VELOCITY]: 88 },\n  { [START]: 10, [DURATION]: 0.5, [PITCH]: 69, [VELOCITY]: 94 },\n  { [START]: 10.5, [DURATION]: 0.5, [PITCH]: 67, [VELOCITY]: 88 },\n  { [START]: 11, [DURATION]: 1, [PITCH]: 65, [VELOCITY]: 94 },\n\n  // Measure 4 (A minor)\n  { [START]: 12, [DURATION]: 4, [PITCH]: 45, [VELOCITY]: 87 },\n  { [START]: 12, [DURATION]: 2, [PITCH]: 57, [VELOCITY]: 64 },\n  { [START]: 12, [DURATION]: 2, [PITCH]: 60, [VELOCITY]: 64 },\n  { [START]: 12, [DURATION]: 2, [PITCH]: 64, [VELOCITY]: 64 },\n  { [START]: 14, [DURATION]: 2, [PITCH]: 57, [VELOCITY]: 70 },\n  { [START]: 14, [DURATION]: 2, [PITCH]: 60, [VELOCITY]: 70 },\n  { [START]: 14, [DURATION]: 2, [PITCH]: 64, [VELOCITY]: 70 },\n  { [START]: 12, [DURATION]: 1, [PITCH]: 69, [VELOCITY]: 97 },\n  { [START]: 13, [DURATION]: 1, [PITCH]: 71, [VELOCITY]: 91 },\n  { [START]: 14, [DURATION]: 0.5, [PITCH]: 69, [VELOCITY]: 97 },\n  { [START]: 14.5, [DURATION]: 0.5, [PITCH]: 67, [VELOCITY]: 91 },\n  { [START]: 15, [DURATION]: 1, [PITCH]: 65, [VELOCITY]: 97 },\n\n  // Measure 5 (F major)\n  { [START]: 16, [DURATION]: 4, [PITCH]: 53, [VELOCITY]: 90 },\n  { [START]: 16, [DURATION]: 2, [PITCH]: 65, [VELOCITY]: 66 },\n  { [START]: 16, [DURATION]: 2, [PITCH]: 69, [VELOCITY]: 66 },\n  { [START]: 16, [DURATION]: 2, [PITCH]: 72, [VELOCITY]: 66 },\n  { [START]: 18, [DURATION]: 2, [PITCH]: 65, [VELOCITY]: 72 },\n  { [START]: 18, [DURATION]: 2, [PITCH]: 69, [VELOCITY]: 72 },\n  { [START]: 18, [DURATION]: 2, [PITCH]: 72, [VELOCITY]: 72 },\n  { [START]: 16, [DURATION]: 1, [PITCH]: 74, [VELOCITY]: 100 },\n  { [START]: 17, [DURATION]: 1, [PITCH]: 76, [VELOCITY]: 94 },\n  { [START]: 18, [DURATION]: 0.5, [PITCH]: 74, [VELOCITY]: 100 },\n  { [START]: 18.5, [DURATION]: 0.5, [PITCH]: 74, [VELOCITY]: 94 },\n  { [START]: 19, [DURATION]: 1, [PITCH]: 71, [VELOCITY]: 100 },\n\n  // Measure 6 (F major)\n  { [START]: 20, [DURATION]: 4, [PITCH]: 53, [VELOCITY]: 93 },\n  { [START]: 20, [DURATION]: 2, [PITCH]: 65, [VELOCITY]: 68 },\n  { [START]: 20, [DURATION]: 2, [PITCH]: 69, [VELOCITY]: 68 },\n  { [START]: 20, [DURATION]: 2, [PITCH]: 72, [VELOCITY]: 68 },\n  { [START]: 22, [DURATION]: 2, [PITCH]: 65, [VELOCITY]: 74 },\n  { [START]: 22, [DURATION]: 2, [PITCH]: 69, [VELOCITY]: 74 },\n  { [START]: 22, [DURATION]: 2, [PITCH]: 72, [VELOCITY]: 74 },\n  { [START]: 20, [DURATION]: 1, [PITCH]: 74, [VELOCITY]: 103 },\n  { [START]: 21, [DURATION]: 1, [PITCH]: 76, [VELOCITY]: 97 },\n  { [START]: 22, [DURATION]: 0.5, [PITCH]: 74, [VELOCITY]: 103 },\n  { [START]: 22.5, [DURATION]: 0.5, [PITCH]: 74, [VELOCITY]: 97 },\n  { [START]: 23, [DURATION]: 1, [PITCH]: 71, [VELOCITY]: 103 },\n\n  // Measure 7 (G major)\n  { [START]: 24, [DURATION]: 4, [PITCH]: 55, [VELOCITY]: 96 },\n  { [START]: 24, [DURATION]: 2, [PITCH]: 67, [VELOCITY]: 70 },\n  { [START]: 24, [DURATION]: 2, [PITCH]: 71, [VELOCITY]: 70 },\n  { [START]: 24, [DURATION]: 2, [PITCH]: 74, [VELOCITY]: 70 },\n  { [START]: 26, [DURATION]: 2, [PITCH]: 67, [VELOCITY]: 76 },\n  { [START]: 26, [DURATION]: 2, [PITCH]: 71, [VELOCITY]: 76 },\n  { [START]: 26, [DURATION]: 2, [PITCH]: 74, [VELOCITY]: 76 },\n  { [START]: 24, [DURATION]: 1, [PITCH]: 76, [VELOCITY]: 106 },\n  { [START]: 25, [DURATION]: 1, [PITCH]: 77, [VELOCITY]: 100 },\n  { [START]: 26, [DURATION]: 0.5, [PITCH]: 76, [VELOCITY]: 106 },\n  { [START]: 26.5, [DURATION]: 0.5, [PITCH]: 69, [VELOCITY]: 100 },\n  { [START]: 27, [DURATION]: 1, [PITCH]: 72, [VELOCITY]: 106 },\n\n  // Measure 8 (G major)\n  { [START]: 28, [DURATION]: 4, [PITCH]: 55, [VELOCITY]: 99 },\n  { [START]: 28, [DURATION]: 2, [PITCH]: 67, [VELOCITY]: 72 },\n  { [START]: 28, [DURATION]: 2, [PITCH]: 71, [VELOCITY]: 72 },\n  { [START]: 28, [DURATION]: 2, [PITCH]: 74, [VELOCITY]: 72 },\n  { [START]: 30, [DURATION]: 2, [PITCH]: 67, [VELOCITY]: 78 },\n  { [START]: 30, [DURATION]: 2, [PITCH]: 71, [VELOCITY]: 78 },\n  { [START]: 30, [DURATION]: 2, [PITCH]: 74, [VELOCITY]: 78 },\n  { [START]: 28, [DURATION]: 1, [PITCH]: 76, [VELOCITY]: 109 },\n  { [START]: 29, [DURATION]: 1, [PITCH]: 77, [VELOCITY]: 103 },\n  { [START]: 30, [DURATION]: 0.5, [PITCH]: 76, [VELOCITY]: 109 },\n  { [START]: 30.5, [DURATION]: 0.5, [PITCH]: 69, [VELOCITY]: 103 },\n  { [START]: 31, [DURATION]: 1, [PITCH]: 72, [VELOCITY]: 109 },\n];\n\n// Plot: reads only data and the constants above\nconst pitches = data.map((n) => n[PITCH]);\nconst velocities = data.map((n) => n[VELOCITY]);\nconst endBeats = data.map((n) => n[START] + n[DURATION]);\nconst totalBeats = Math.max(...endBeats);\nconst measures = Math.ceil(totalBeats / BEATS_PER_MEASURE);\n\nconst minPitch = Math.min(...pitches);\nconst maxPitch = Math.max(...pitches);\nconst pitchPad = Math.max(\n  MIN_PITCH_PAD_SEMITONES,\n  Math.round((maxPitch - minPitch) * PITCH_PAD_SHARE),\n);\nconst pitchAxisMin = minPitch - pitchPad;\nconst pitchAxisMax = maxPitch + pitchPad;\n\nconst velocityMin = Math.min(...velocities);\nconst velocityMax = Math.max(...velocities);\nconst velocitySpan = velocityMax - velocityMin;\n\nconst beatPad = totalBeats * BEAT_PAD_SHARE;\nconst measureTicks = Array.from({ length: measures }, (_, i) => i * BEATS_PER_MEASURE);\nconst whiteKeyTicks: number[] = [];\nconst blackKeyPitches: number[] = [];\nconst noteNameByPitch: Record<number, string> = {};\n\nfor (let p = Math.ceil(pitchAxisMin); p <= Math.floor(pitchAxisMax); p += 1) {\n  const semitone = ((p % 12) + 12) % 12;\n  noteNameByPitch[p] = `${NOTE_NAMES[semitone]}${Math.floor(p / 12) - 1}`;\n  if (BLACK_SEMITONES.includes(semitone)) blackKeyPitches.push(p);\n  else whiteKeyTicks.push(p);\n}\n\nconst softRgb = decomposeColor(t.seq[0]).values;\nconst loudRgb = decomposeColor(t.seq[1]).values;\n\nconst noteFills = data.map((row) => {\n  const ratio = (row[VELOCITY] - velocityMin) / velocitySpan;\n  const [r, g, b] = softRgb.map((c, k) => Math.round(c + (loudRgb[k] - c) * ratio));\n  return `rgb(${r}, ${g}, ${b})`;\n});\n\nconst beats = Array.from({ length: totalBeats + 1 }, (_, b) => b);\n\nfunction PianoRollOverlay() {\n  const xScale = useXScale(\"beats\") as unknown as (v: number) => number;\n  const yScale = useYScale(\"pitch\") as unknown as (v: number) => number;\n  const drawingArea = useDrawingArea();\n\n  return (\n    <g>\n      {blackKeyPitches.map((p) => (\n        <rect\n          key={`row-${p}`}\n          x={drawingArea.left}\n          y={yScale(p + 0.5)}\n          width={drawingArea.width}\n          height={Math.abs(yScale(p - 0.5) - yScale(p + 0.5))}\n          fill={t.ink}\n          fillOpacity={0.05}\n        />\n      ))}\n\n      {beats.map((b) => {\n        const isMeasure = b % BEATS_PER_MEASURE === 0;\n        return (\n          <line\n            key={`beat-${b}`}\n            x1={xScale(b)}\n            x2={xScale(b)}\n            y1={drawingArea.top}\n            y2={drawingArea.top + drawingArea.height}\n            stroke={isMeasure ? t.ink : t.grid}\n            strokeOpacity={isMeasure ? 0.28 : 1}\n            strokeWidth={isMeasure ? 1.5 : 1}\n          />\n        );\n      })}\n\n      {data.map((row, i) => {\n        const x0 = xScale(row[START]);\n        const x1 = xScale(row[START] + row[DURATION]);\n        const rowTop = yScale(row[PITCH] + 0.5);\n        const rowBottom = yScale(row[PITCH] - 0.5);\n        const pad = (rowBottom - rowTop) * 0.14;\n        return (\n          <rect\n            key={`note-${i}`}\n            x={x0 + 1}\n            y={rowTop + pad}\n            width={Math.max(x1 - x0 - 2, 3)}\n            height={Math.max(rowBottom - rowTop - pad * 2, 2)}\n            rx={3}\n            fill={noteFills[i]}\n            stroke={t.pageBg}\n            strokeWidth={1}\n          />\n        );\n      })}\n    </g>\n  );\n}\n\nexport default function Chart() {\n  const size = window.ANYPLOT_SIZE;\n  const padding = { top: 24, right: 28, bottom: 20, left: 28 };\n  const titleBlockHeight = 46;\n  const chartWidth = size.width - padding.left - padding.right;\n  const chartHeight = size.height - padding.top - padding.bottom - titleBlockHeight;\n\n  return (\n    <Box\n      sx={{\n        width: size.width,\n        height: size.height,\n        backgroundColor: t.pageBg,\n        boxSizing: \"border-box\",\n        padding: `${padding.top}px ${padding.right}px ${padding.bottom}px ${padding.left}px`,\n        display: \"flex\",\n        flexDirection: \"column\",\n      }}\n    >\n      <Box\n        sx={{\n          display: \"flex\",\n          alignItems: \"center\",\n          justifyContent: \"space-between\",\n          mb: \"14px\",\n          height: \"32px\",\n        }}\n      >\n        <Typography sx={{ fontSize: 22, fontWeight: 600, color: t.ink, lineHeight: 1 }}>\n          {TITLE}\n        </Typography>\n        <Box sx={{ display: \"flex\", alignItems: \"center\", gap: \"10px\" }}>\n          <Typography sx={{ fontSize: 14, color: t.inkSoft, lineHeight: 1 }}>\n            {`${VELOCITY}: Soft (${velocityMin})`}\n          </Typography>\n          <Box\n            sx={{\n              width: 180,\n              height: 12,\n              borderRadius: \"4px\",\n              background: `linear-gradient(to right, ${t.seq[0]}, ${t.seq[1]})`,\n              border: `1px solid ${t.grid}`,\n            }}\n          />\n          <Typography sx={{ fontSize: 14, color: t.inkSoft, lineHeight: 1 }}>\n            {`Loud (${velocityMax})`}\n          </Typography>\n        </Box>\n      </Box>\n      <ChartsContainer\n        width={chartWidth}\n        height={chartHeight}\n        series={[]}\n        margin={{ left: 16, right: 24, top: 18, bottom: 12 }}\n        xAxis={[\n          {\n            id: \"beats\",\n            scaleType: \"linear\",\n            min: -beatPad,\n            max: totalBeats + beatPad,\n            height: 54,\n            tickInterval: measureTicks,\n            valueFormatter: (beat: number) => `M${Math.round(beat / BEATS_PER_MEASURE) + 1}`,\n            label: TIME_AXIS_TITLE,\n            tickLabelStyle: { fontSize: 14, fill: t.inkSoft },\n            labelStyle: { fontSize: 15, fill: t.ink },\n          },\n        ]}\n        yAxis={[\n          {\n            id: \"pitch\",\n            scaleType: \"linear\",\n            min: pitchAxisMin,\n            max: pitchAxisMax,\n            width: 68,\n            tickInterval: whiteKeyTicks,\n            tickLabelInterval: () => true,\n            valueFormatter: (pitch: number) => noteNameByPitch[Math.round(pitch)],\n            label: PITCH_AXIS_TITLE,\n            tickLabelStyle: { fontSize: 13, fill: t.inkSoft },\n            labelStyle: { fontSize: 15, fill: t.ink },\n          },\n        ]}\n        disableAxisListener\n        skipAnimation\n      >\n        <PianoRollOverlay />\n        <ChartsYAxis axisId=\"pitch\" />\n        <ChartsXAxis axisId=\"beats\" />\n      </ChartsContainer>\n    </Box>\n  );\n}\n"}