# MusicXML for midipiano.app

This page tells an AI assistant how to write a piano exercise that
[midipiano.app](https://midipiano.app) imports on the first try. It is plain Markdown and it stays
put, so a machine can read it as easily as a person can.

midipiano.app is a free web app for practicing piano with a MIDI keyboard. Someone asks you for an
exercise, imports the file you write, and plays it while the app scores every note. If the file does
not follow these rules, the import fails or the hands land on the wrong staff, and the person has no
way to tell what went wrong.

## What the importer accepts

- **MusicXML 3.1 or 4.0, partwise** (`<score-partwise>`). Extensions: `.musicxml`, `.xml` (uncompressed)
  or `.mxl` (zipped).
- **MEI** (`.mei`), if you prefer it. Everything below is about MusicXML.
- `<score-timewise>` is **not** supported: the renderer cannot read it.
- The person can also paste the code straight into the app, so writing the whole document in your
  answer is always enough. A downloadable file is a bonus, not a requirement.

## The structure it expects

One part, two staves, one voice per staff. These are the things that go wrong most often in
generated files:

- Exactly **one `<part>`** with **`<staves>2</staves>`** in the first `<attributes>`: treble (staff 1,
  right hand) and bass (staff 2, left hand). Two parts come out as two instruments. One staff with two
  voices cannot be practiced hand by hand, which is the whole point of the app.
- **Every note carries `<staff>`** (1 or 2) and its `<voice>`. Use voice 1 on staff 1 and voice 2 on
  staff 2.
- **`<backup>`** with the duration of the whole measure, before the left-hand notes of each measure,
  so the left hand starts again at beat 1 and both staves cover the measure.
- **`<divisions>`, `<time>`, `<key>` and both clefs** (`<clef number="1">` G line 2,
  `<clef number="2">` F line 4) in that same first `<attributes>`. Without the second clef the left
  hand is drawn in treble and becomes unreadable.
- **A tempo direction with `<sound tempo="..."/>`** in the first measure. The app reads the sound tempo, not
  the printed text. Without it the piece plays at 120, whatever tempo the person asked for.
- **Every note and rest carries a `<type>`**, and the value has to be one of the MusicXML ones:
  `whole`, `half`, `quarter`, `eighth`, `16th`, `32nd`, `64th`. Write `16th`, never `sixteenth`, and
  `32nd`, never `thirty-second`. The app repairs the spellings it knows and tells the person about
  the ones it does not, but the note is still drawn with no stem and no beam, and it still lasts the
  wrong time, so the measure runs long and the cursor drifts away from the music. Its `<duration>`
  has to match the type, counted in `<divisions>` units.
- **A dotted value carries a `<dot/>` right after its `<type>`.** The app reads the time of every
  note from its type and its dot, not from its `<duration>`, so a dotted quarter written without the
  `<dot/>` sounds like a plain quarter and the next note arrives half a beat early. In 6/8 the beat
  itself is a dotted quarter, so this one matters in most measures.
- **A rest that fills the whole measure is `<rest measure="yes"/>` with no `<type>`.** Writing
  `<type>whole</type>` on it is the usual guess and it makes the measure last four beats whatever
  the time signature says, so in 2/4, 3/4 and 6/8 everything after it drifts. Rests that do not fill
  the measure are ordinary rests with their own `<type>`.
- **Every measure must add up exactly to the time signature, and so must each staff on its own.** A
  measure that is short or long shifts everything after it and the scoring drifts for the rest of
  the piece.
- Measures are `<measure number="1">`, `<measure number="2">`… (never `<measure-number>`), and the
  title goes in `<work><work-title>`.
- **The last measure ends with a final barline**: `<barline location="right"><bar-style>light-heavy</bar-style></barline>`,
  after the notes. Without it the piece looks like it goes on, and the person sees a
  thin line where the double bar should be.

## Pedal

If the exercise uses the sustain pedal, write **every span as an explicit pair**. Open it before the
first note of the span and close it after the last one, both in **staff 2**, and close one pair
before opening the next:

```xml
<direction placement="below"><direction-type><pedal type="start" line="yes"/></direction-type><staff>2</staff></direction>
<!-- the notes of the span -->
<direction placement="below"><direction-type><pedal type="stop" line="yes"/></direction-type><staff>2</staff></direction>
```

**Never use `<pedal type="change"/>`.** A file with one `start`, a row of `change` marks and one
`stop` imports and sounds fine, but the renderer draws only the first span. The person then sees a
piece with almost no pedal on it. Written as pairs, every span gets its own line with its hooks.

## Fingering

Only if the person asks for it. Write the finger inside the note, after its `<staff>`, with **1 for
the thumb and 5 for the little finger**. It goes **above** the staff in the right hand (staff 1) and
**below** it in the left (staff 2):

```xml
<note><pitch><step>C</step><octave>4</octave></pitch><duration>2</duration><voice>1</voice><type>half</type><staff>1</staff><notations><technical><fingering placement="above">1</fingering></technical></notations></note>
<note><pitch><step>E</step><octave>4</octave></pitch><duration>2</duration><voice>1</voice><type>half</type><staff>1</staff><notations><technical><fingering placement="above">3</fingering></technical></notations></note>
```

The numbers are drawn next to the notes and change nothing of what is played. Same pitches, same
timing. Do not put one on every note of a long exercise unless you were asked to, and never use them
for anything other than a finger number.

## What to leave out

These are either unsupported or a nuisance in a practice exercise:

- tuplets, grace notes, ornaments (trills, mordents, turns)
- ties across measures, slurs
- lyrics, dynamics, hairpins
- repeats, endings, segno or coda jumps
- more than one part, more than two staves, more than one voice per staff

## Ranges

- **Beginner**: the right hand uses only **C4 D4 E4 F4 G4** and the left hand only **C3 D3 E3 F3 G3**
  (transpose that five-finger position to the requested key). No thumb crossings, no hand shifts.
  Quarter, half and whole notes. The left hand can hold whole notes.
- **Intermediate**: right hand within **C4 to C6**, left hand within **C2 to C4**. Eighth notes,
  simple hand shifts, broken chords.
- **Advanced**: same two windows, wider leaps, both hands moving, faster values. Still no tuplets and
  no ties across measures.

## Final checklist

Before you answer, check all of these:

1. The document starts with `<?xml` and ends with `</score-partwise>`.
2. Every measure sums exactly to the time signature, on both staves.
3. `<staves>2</staves>`, `<divisions>`, `<key>`, `<time>`, both clefs and `<sound tempo>` are present.
4. Every note has `<voice>` and `<staff>`, and every measure has its `<backup>`.
5. No tuplets, grace notes, ornaments, ties across measures, slurs, lyrics, dynamics or repeats.
6. The file has a title in `<work><work-title>`.
7. Every `<type>` is a MusicXML value (`16th`, not `sixteenth`) and matches its `<duration>`.
8. Dotted values carry `<dot/>`, and whole-measure rests are `<rest measure="yes"/>` with no `<type>`.
9. The whole document is in **one** code block, with nothing omitted and no `...` placeholders.

## The block midipiano.app puts in its own prompt

The app writes the whole prompt in the language of the person using it, these rules included, so
that they can read what they are about to paste. Only what cannot be translated stays in English:
the MusicXML element names, the snippets to copy as they are, the note names and this page's
address. Those snippets live in one single place and are injected into the thirteen translations,
so they cannot drift apart from one language to another. This page stays in English, because it is
written for assistants.

This is the block for 4/4 at 60 bpm, which is what the app writes when nobody changes them. The
time signature and the tempo follow what the person chose, in the three places that depend on them:
the `<time>`, the `<sound tempo>` and the line about every measure adding up. The rest is the same
set of rules as above, condensed:

```
Follow the MusicXML specification for midipiano.app at https://midipiano.app/ai/musicxml-for-midipiano.md if you can browse the web. Otherwise apply the rules below.
Technical requirements (follow them exactly):
- Use MusicXML 3.1 or 4.0, partwise format (score-partwise, never score-timewise).
- Exactly one part with two staves: put <staves>2</staves> in the first <attributes>, give EVERY note a <staff> element (1 = treble/right hand, 2 = bass/left hand), and use <backup> before the left-hand notes of each measure so both staves cover the full measure.
- In that same first <attributes>, include explicit <divisions>, <time><beats>4</beats><beat-type>4</beat-type></time>, <key>, a <clef number="1"> (G, line 2) and a <clef number="2"> (F, line 4).
- Set the tempo with this direction in the first measure, which sets it without printing a metronome mark: <direction placement="above"><sound tempo="60"/></direction>
- Hide the part label so no instrument name is drawn next to the staves: <part-name print-object="no">Piano</part-name>.
- Every measure must add up exactly to 4/4, and EACH STAFF must add up to it on its own.
- Notes and rests only: no tuplets, grace notes, ornaments, ties across measures, slurs, lyrics or dynamics.
- At beginner level the right hand uses only C4 D4 E4 F4 G4 and the left hand only C3 D3 E3 F3 G3 (transpose that five-finger position to the requested key). No thumb crossings, no hand shifts.
- At intermediate and advanced level the right hand stays within C4 to C6 and the left hand within C2 to C4.
- Every note and rest carries a <type> with one of these exact values: whole, half, quarter, eighth, 16th, 32nd, 64th (write 16th, never sixteenth), a <dot/> right after it when the value is dotted, and a <duration> that matches them in <divisions> units. A rest that fills the whole measure is <rest measure="yes"/> with no <type>.
- Beam consecutive eighths or sixteenths that share a beat, or they are drawn with loose flags: <beam number="1">begin</beam> on the first note, <beam number="1">continue</beam> in the middle ones and <beam number="1">end</beam> on the last.
- Write <alter> on EVERY altered note, also when the key signature already carries that sharp or flat: the key signature does not change the pitch that sounds. Add the matching <accidental> when the note is outside the key, and leave it out when the key signature already prints it.
- Pedal: one explicit pair per span, <direction placement="below"><direction-type><pedal type="start" line="yes"/></direction-type><staff>2</staff></direction> before its first note and the same direction with type="stop" after its last. Never use type="change": the renderer draws only the first span.
- Fingering, only if the user asks for it: inside each <note>, <notations><technical><fingering>N</fingering></technical></notations> with N from 1 (thumb) to 5, placement="above" on staff 1 and "below" on staff 2.
- One voice per staff.
- No repeats and no endings.
- End the last measure with a final barline: <barline location="right"><bar-style>light-heavy</bar-style></barline>.
- Each measure element must be <measure number="N"> (never <measure-number>), and the title goes in <work><work-title>, not anywhere else.
- Return the complete MusicXML document inline, in ONE code block, from the <?xml version="1.0" encoding="UTF-8"?> declaration to </score-partwise>, with no omissions, ellipses or "..." placeholders and no explanation inside the block. Do not describe the file instead of writing it. If you can also attach it as a downloadable .musicxml or .mxl file, do both.
```

## A complete example

Four measures, five-finger position in C, both hands. This exact file imports and plays in
midipiano.app, and a test in this repository keeps it that way. It is also published at
<https://midipiano.app/ai/example-exercise.musicxml>.

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 4.0 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
<score-partwise version="4.0">
  <work><work-title>Five-finger position in C (both hands)</work-title></work>
  <part-list><score-part id="P1"><part-name print-object="no">Piano</part-name></score-part></part-list>
  <part id="P1">
    <measure number="1">
      <attributes><divisions>1</divisions><key><fifths>0</fifths></key><time><beats>4</beats><beat-type>4</beat-type></time><staves>2</staves>
        <clef number="1"><sign>G</sign><line>2</line></clef><clef number="2"><sign>F</sign><line>4</line></clef></attributes>
      <direction placement="above"><sound tempo="70"/></direction>
      <note><pitch><step>C</step><octave>4</octave></pitch><duration>1</duration><voice>1</voice><type>quarter</type><staff>1</staff></note>
      <note><pitch><step>D</step><octave>4</octave></pitch><duration>1</duration><voice>1</voice><type>quarter</type><staff>1</staff></note>
      <note><pitch><step>E</step><octave>4</octave></pitch><duration>1</duration><voice>1</voice><type>quarter</type><staff>1</staff></note>
      <note><pitch><step>F</step><octave>4</octave></pitch><duration>1</duration><voice>1</voice><type>quarter</type><staff>1</staff></note>
      <backup><duration>4</duration></backup>
      <note><pitch><step>C</step><octave>3</octave></pitch><duration>4</duration><voice>2</voice><type>whole</type><staff>2</staff></note>
    </measure>
    <measure number="2">
      <note><pitch><step>G</step><octave>4</octave></pitch><duration>1</duration><voice>1</voice><type>quarter</type><staff>1</staff></note>
      <note><pitch><step>F</step><octave>4</octave></pitch><duration>1</duration><voice>1</voice><type>quarter</type><staff>1</staff></note>
      <note><pitch><step>E</step><octave>4</octave></pitch><duration>1</duration><voice>1</voice><type>quarter</type><staff>1</staff></note>
      <note><pitch><step>D</step><octave>4</octave></pitch><duration>1</duration><voice>1</voice><type>quarter</type><staff>1</staff></note>
      <backup><duration>4</duration></backup>
      <note><pitch><step>G</step><octave>3</octave></pitch><duration>4</duration><voice>2</voice><type>whole</type><staff>2</staff></note>
    </measure>
    <measure number="3">
      <note><pitch><step>C</step><octave>4</octave></pitch><duration>2</duration><voice>1</voice><type>half</type><staff>1</staff></note>
      <note><pitch><step>E</step><octave>4</octave></pitch><duration>2</duration><voice>1</voice><type>half</type><staff>1</staff></note>
      <backup><duration>4</duration></backup>
      <note><pitch><step>C</step><octave>3</octave></pitch><duration>2</duration><voice>2</voice><type>half</type><staff>2</staff></note>
      <note><pitch><step>E</step><octave>3</octave></pitch><duration>2</duration><voice>2</voice><type>half</type><staff>2</staff></note>
    </measure>
    <measure number="4">
      <note><pitch><step>C</step><octave>4</octave></pitch><duration>4</duration><voice>1</voice><type>whole</type><staff>1</staff></note>
      <backup><duration>4</duration></backup>
      <note><pitch><step>C</step><octave>3</octave></pitch><duration>4</duration><voice>2</voice><type>whole</type><staff>2</staff></note>
      <barline location="right"><bar-style>light-heavy</bar-style></barline>
    </measure>
  </part>
</score-partwise>
```

## A second example, with two hands at once

The app also ships a sample, one click away from the paste box, for anyone with no assistant at
hand: <https://midipiano.app/ai/sample-exercise.musicxml>. It is level 1 of the app's own catalogue,
checked note by note against it. It shows the case that generated files get wrong most often: **the
two hands playing at different moments inside the same measure**, the right hand on every beat and
the left hand on every off-beat, each staff filling its four beats with its own rests.

## If the import fails

The app tells the person what went wrong. The usual causes:

- **The document was cut short.** Write it complete, in one block, with no ellipses.
- **It is `score-timewise`.** Write `score-partwise` instead.
- **A measure does not sum to the time signature.** Count each staff again, including the
  `<backup>`.
- **Everything landed on one staff.** Add `<staves>2</staves>`, a `<staff>` in every note and the
  `<backup>` before the left hand.
