Turning pages hands-free

한국어로 읽기

[MusicTools dev note] Half-Page Turns in Play Order: The Half You're Reading Never Moves

MusicTools 1.6.1 turns sheet music by half pages: whole lines in the order you play them, repeats and all, so you always have half a page to turn in.

A white line drawing on black of two tablets showing sheet music, before and after a turn: below a dashed line the bottom half is the same in both, and above it the faded top half has been replaced by new lines

A page turn asks for two things at once: the last bar of one page and the first bar of the next. Turning by half pages splits that moment in two. While you play the bottom of the screen, the top already shows what comes next, so the next music is there before you need it.

MusicTools has turned pages by halves since version 1.4. In 1.6.1 I rebuilt how it does it, after a lead sheet showed me that the old way could leave a player one line to turn in.

What went wrong: one line to turn in

The first version worked page by page. Each page was divided at a gap between two of its lines, and a turn brought in the top part of the next page above the part being played. With Follow Song Form on, MusicTools turns in the order the music is played, so a repeat brought the line it goes back to in at the top of the screen. (How that order is worked out from repeats, endings, D.S. and Fine is for another post.)

Each of those steps made sense on its own. But where a page was divided depended on the page, and a jump could land anywhere on it. Playing a lead sheet from my ensemble (I can’t show it here: the arrangement isn’t mine), I came to a screen where the part coming in was a single line, and the rest of that half was blank. That’s fine when the line ends in a repeat sign and the music goes on somewhere else. On that screen it didn’t. One line was all the time there was to turn. The division also followed the staff lines, so chord symbols printed above a staff could peek over it.

Showing just the one or two lines that matter can look clever. Halves that are always full are more useful. So the rule became simpler.

The new rule: a line that never moves, and whole lines in play order

  • The screen is two halves that meet at one line, and that line never moves. It’s set once, when the sheet opens: the gap between two lines of the page it opens on that’s nearest the middle of the screen, if one is within 15% of the screen’s height from it, otherwise the middle. That way the first screen looks just like the printed page.
  • The music is the lines in the order they’re played, one under another. A repeat back to line 3 just means that line 3 comes next.
  • Each half takes as many of the next lines as fit, whole lines only. A line is its band of the page, from halfway across the gap above it to halfway across the gap below. The chord symbols, lyrics or brackets printed around it go with it.
  • A turn swaps the half you’ve just played for the next lines. You read top, bottom, top, bottom. The half you’re reading never moves, and the next half arrives while you’re still reading the other one, so there’s always a whole half to turn in, never just a line.
Half-page turns in the order the lines are playedEight lines of music, with lines 3 and 4 repeated, laid out in the order they are played: 1 2, 3 4, 3 4, 5 6, 7 8, two lines to a half. Four screens follow, each split by a dashed line that stays in the same place. At the start, lines 1 and 2 are on top and being played, lines 3 and 4 below. Turn 1: lines 3 and 4 are being played at the bottom, and the top fills with lines 3 and 4 again, tagged as a repeat. Turn 2: the top is being played, and the bottom fills with lines 5 and 6. Turn 3: the bottom is being played, and the top fills with lines 7 and 8.The lines in the order they're played, two to a half:1234345678repeatStart12playing34Turn 13repeat4new34playingTurn 23repeat4playing56newTurn 378new56playingThe dashed line never moves. A turn refills only the half you've just played.
Line 4 ends in first and second endings: each time through, the bar not played is dimmed. The line a repeat goes back to is tagged, as in the app.

A line you come to by a jump says so at its top, with a tag like “repeat → bar 18”. The bars of a line that aren’t played this time, like a first ending on the way through for the second time, are dimmed. If a single line is taller than half the screen, MusicTools turns whole pages instead. And if a page’s lines aren’t known, its halves are simply its top and its bottom.

The core of it is short. Here are the lines cut into halves, a little simplified from the app’s turnPlan.ts (in the app, the first half sits where it’s printed on the page rather than at the top):

// The lines in the order they're played, cut into halves: top, bottom, top…
for (const line of strip) {
  const height = (line.to - line.from) * pageHeight;
  if (halves.length === 0 || y + height > limit) {
    const inTop = halves.length % 2 === 0;
    y = inTop ? 0 : boundary;
    limit = inTop ? boundary : screenHeight;
    if (y + height > limit) return null; // taller than a half: turn whole pages
    halves.push([]);
  }
  halves[halves.length - 1].push({ ...line, y });
  y += height;
}

The turns are one line of arithmetic each. At turn i you’re playing half i, and the other half of the screen shows half i + 1:

const top = halves[2 * Math.floor((i + 1) / 2)]; // halves 0, 2, 2, 4, 4…
const bottom = halves[2 * Math.floor(i / 2) + 1]; // halves 1, 1, 3, 3, 5…

Here’s Bach’s chorale “Aus meines Herzens Grunde” (BWV 269) by half pages. Its first line is repeated, so it comes twice, the second time with a tag, and the whole chorale reads from top to bottom with no jump back up.

MusicTools on a tablet showing Bach's chorale BWV 269 by half pages: the title and first line, a thin blue line with a small 'repeat' tag at its right, the title and first line again, another blue line across the middle of the screen, then lines two and three
The repeat played as written out: the first line twice, the second time tagged. The line across the middle is where the halves meet.

The plan is pure TypeScript, unit-tested on made-up pages whose lines are known. I checked the real screens on an Android tablet emulator with the lead sheet that started all this: four screens, each turn changing only the half that had been played, a tag wherever a jump lands and the skipped bar dimmed.

Also in 1.6.1: page turning set for each song

Follow Song Form, Turn by (Whole Page or Half Page) and Turn Pages by Face used to be one setting for the whole app. But a lead sheet full of repeats wants half pages in play order, while a sonata may be better turned a whole page at a time. Now each song keeps its own.

Open Settings over a song, and the Page Turning section names the song it’s changing. Open it with no song open, and you set the defaults for songs that haven’t set their own. The defaults start out as the old app-wide values, so nothing changes for anyone until they change a song. Turn Pages by Face has no default: it’s off until you set a song to it. The settings are kept with the song, so a backup or a shared setlist takes them along.

The Page Turning section of MusicTools' Settings, opened over a song: 'For “Bach - Chorale BWV 269”: each sheet keeps its own.', Follow Song Form switched on, Turn by set to Half Page, and below it Turn Pages by Face, off, with a note that it's this sheet's setting
Settings opened over a song change that song only.

Face gestures took one more thought. MusicTools promises that the camera stays off until you turn it on, and goes off again every time the app starts. So a song set to turn by face can’t simply switch the camera on when it opens. The first time you open one after starting the app, it asks: “Turn pages by face?”, with Turn On and Not Now. (How a wink turns a page is in an earlier post.)

Also in 1.6.1: the metronome carries on through a setlist

In a setlist, moving on to the next song used to stop the metronome. Now, if it’s running, it carries on into the next song at that song’s tempo and meter. That works with the next-song button, the setlist menu, and turning past the last page.

The bug to avoid was in the cleanup. Each click sound has one engine, shared by every screen that uses it. When the next song opens, its screen starts the engine. Then the old screen goes away and runs its cleanup, which used to turn the engine off, under the new song’s clicks. So the engine now remembers which screen is driving it, and a screen that’s going away only turns it off if it still is:

const playing = new WeakMap<object, object>();

/** Whether the screen `who` may turn the shared engine off as it goes away. */
export function mayLetGo(engine: object, who: object): boolean {
  const owner = playing.get(engine);
  if (owner !== undefined && owner !== who) return false; // another screen has it now
  playing.delete(engine);
  return true;
}

All three are in MusicTools 1.6.1, on its way to the App Store.

MusicTools dev note

Next · Turning pages hands-freeTurning Pages with a Wink: ARKit on iPad, MediaPipe on AndroidHow my score app tells a wink from a blink to turn sheet music pages hands-free, with ARKit on iPad and MediaPipe on Android, plus a webcam demo you can run.
All episodes 8 so far

Turning pages hands-free

Sound: tuner, recording and analysis

Extras: notes from my dev setup

Written by

Allen

Independent app developer in South Korea. I write about the apps I make, the piano I practice, the anime I watch, and a fantasy world I'm building.

More from Practical Trivia