I build Melupia, an Android app for piano sight-reading practice: a note appears on the
staff and you play its key, on the phone's keyboard or on your own digital piano over USB or Bluetooth MIDI.
The hard part was deciding which note comes next. Random notes waste the learner's time on what they already know.
A fixed order turns into a memory game. This post covers what we ended up with. All of it is pure Dart and
unit-tested, with the random source and the clock injected.
1. One memory per note: an FSRS-style model
Every note on a staff (say G4 on the treble staff, G4 on the bass staff) gets its own small memory record,
modelled on FSRS, the scheduler behind
modern Anki:
- stability S: days until the chance of reading it right drops to 90 %;
- difficulty D: 1 to 10;
- retrievability R after t days:
R = (1 + 19/81 · t / S)^−0.5.
Flashcard apps ask you to grade yourself. We grade from the answer itself, using correctness and response time:
| Answer | Grade |
|---|---|
| wrong key | again |
| right, but slow | hard |
| right, in time | good |
| right and fluent (< 1.5 s) | easy |
Two details mattered in practice:
- Same-day answers have a cap. Twenty right answers in one evening do not prove you will know the note next week, so stability can only grow so far on the same day. Lasting memory needs a night between.
- Octave errors are special. Playing G3 for G4 means you read the letter right and the register wrong. It counts for accuracy but never moves the long-term memory.
2. Confusions: a Bayesian "trap" book
Beginners do not fail at random. They confuse specific pairs: E and G on the treble lines, or a bass-clef note read
as if it were treble. For every written note we count which wrong keys get played instead, and keep a Beta posterior
per pair. A pair becomes a trap only once the evidence is confident. It decays again once the learner beats it.
3. The choice: a bandit with Thompson sampling
Picking the next note is an explore/exploit problem. The model gives each candidate note a predicted chance of a right
answer p and an amount of evidence n (pseudo-answers). We draw a plausible success rate from
Beta(p·n, (1−p)·n): little-known notes get wide draws and are explored, well-known ones get narrow draws.
Then each draw is scored by how much it would teach:
value = need × fit × spacing - need =
1 − p̃, plus the pull of an active trap. Weak, forgotten and trap notes teach the most. - fit: a soft floor on
p̃that follows the session's recent success. While the learner is above about 85 % correct, harder notes may come; when they struggle, easier ones come back. 85 % is the "desirable difficulty" sweet spot from Wilson et al. (2019), and it keeps a session from turning into a wall of red. - spacing: a note that was just asked waits a little, so notes interleave.
A simplified sketch of the pick:
Target pickNext(List<Target> candidates, LearnerModel model, Random rnd, int nowMs) { Target? best; var bestValue = -1.0; for (final t in candidates) { final pred = model.predict(t, nowMs); // mean p and evidence n final draw = sampleBeta(pred.p * pred.n, (1 - pred.p) * pred.n, rnd); final value = need(draw, pred.trapPull) * fit(draw, session.recentSuccess) * spacing(t); if (value > bestValue) { bestValue = value; best = t; } } return best!; } Around the bandit are a few plain rules. A missed note comes back after two questions. With both clefs on, each staff
keeps a minimum share, and the weaker staff gets more. After a trap's target, its contrast note often follows, so the
learner has to tell the pair apart.
4. Real songs as a reward
Once a round goes well (enough answers, high recent success, several notes clearly stronger), the next page may be a
short phrase of a real song. The phrase is built only from notes the learner now reads well. It turns a drill into
music without breaking the grading.
5. Reading a real piano: MIDI without a package
For USB and Bluetooth LE MIDI we wrote a thin Android plugin over android.media.midi. It only lists, opens and
watches devices and forwards raw bytes with their timestamps. Parsing lives in pure Dart:
- running status (a status byte can be omitted for repeated messages);
- note-on with velocity 0 is a note-off;
- several messages per packet, SysEx and real-time bytes skipped but kept in step.
Keeping the parser in Dart made it trivial to test with byte fixtures, and keeping device timestamps made rhythm
grading and latency calibration possible later.
What I would do again
- Grade from behaviour (right/wrong + time), never ask the learner to rate themselves.
- Keep the model, the scheduler and the parser free of Flutter imports: thousands of fast unit tests.
- Treat "too hard" as a bug. The 85 % floor changed retention more than any other knob.
If you play piano or teach it, the free lessons on reading sheet music
and sight-reading practice explain the same ideas without code.
Questions about the model are welcome in the comments.
