---
title: "A Markdown Music prototype"
subtitle: "Markdown and scores in one file"
author: alpelito7
lang: en
format:
  html:
    embed-resources: true
  pdf:
    documentclass: article
filters:
  - mdm
---

This document is ordinary Markdown, rendered and editable at once: 
- text, **emphasis**, LaTeX equations, source code and, on top of that,
- music blocks that render as a score and play,
- with multicursor, an outline, a look set from the toolbar and export options.

## From equations to score

Take a string of length $L$, tension $T$ and linear density $\mu$, clamped at both ends. The boundary conditions $y(0,t) = y(L,t) = 0$ leave only the standing waves whose half wavelength fits an integer number of times in $L$, so the general motion is a sum over one integer:

$$
y(x,t) = \sum_{n=1}^{\infty} A_n \sin\!\left(\frac{n \pi x}{L}\right) \cos\!\left(2\pi f_n t + \varphi_n\right),
\qquad
f_n = \frac{n}{2L}\sqrt{\frac{T}{\mu}} = n f_1 .
$$

Because $f_n = n f_1$, every mode is a note, and the ratio between two neighbours, $(n+1)/n$, is the musical interval between them: $2{:}1$ the octave, $3{:}2$ the fifth, $4{:}3$ the fourth, $5{:}4$ the major third.

That is what the score below draws, one note per mode: each $n$ is a partial, the name acoustics gives to a single component of a complex sound. The upper system is the first eight partials of a low C, the lower one pairs each partial with the next and names the ratio underneath; the seventh is the odd one out, $31$ cents flat of the tempered minor seventh, hence the mark over the B flat.

```abc
%%stretchlast 1
X:1
T:Partials of a vibrating string
L:1/4
K:C clef=treble
P:partials
C, C G c e g "^-31c"_b c' |
w: n=1 2 3 4 5 6 7 8
P:ratios
[C,C] [CG] [Gc] [ce] [eg] [g_b] [_bc'] |
w: 2:1 3:2 4:3 5:4 6:5 7:6 8:7
```

## From code to scores

A Python loop is enough to walk the circle of fifths. One modular step, `d = (d + 3) % 7`, drops each root a fifth below the last, the $3:2$ of $\Delta_2 \approx 702$ cents from the previous section, and seven of them exhaust C major and land back at the start. The loop stacks every other note of the scale into a chord, reads its name off the gaps between its own notes, and prints the line that becomes the score below.

Two cadences fall out unasked: a II-V-I in C opens the sequence, one in A minor closes it. Only the second needs help, since A minor's leading tone is G sharp and C major hasn't got one, so one `if` supplies it. The same trick adds a bar of A7, and the repeat button resolves it into the first.

```python
NAME = "C _D D _E E F _G G _A A _B B".split()  # ABC names
MAJOR = [0, 2, 4, 5, 7, 9, 11]                 # one scale
QUALITY = {(3, 4, 3): "m7",  (4, 3, 3): "7",   # the gaps name it
           (4, 3, 4): "maj7", (3, 3, 4): "m7b5"}
deg = lambda d: MAJOR[d % 7] + 12 * (d // 7)   # d-th note up

def spell(p):                   # semitones over middle C
    n, o = NAME[p % 12], p // 12
    return n.lower() + "'" * (o - 1) if o > 0 else n + "," * -o

bars, d = [], 1                 # start on the second degree
while True:                     # a fourth up each bar...
    tones = [deg(d + 2 * i) for i in range(4)]
    gaps = tuple(b - a for a, b in zip(tones, tones[1:]))
    chord = NAME[tones[0] % 12] + QUALITY[gaps]
    notes = "".join(map(spell, tones))
    if d == 2:                  # the note C major hasn't got
        chord, notes = "E7", notes.replace("G", "^G")
    bars.append('"%s"[%s]4' % (chord, notes))
    d = (d + 3) % 7
    if d == 1:                  # ...until the root is D again
        break
# C sharp on the A chord, as G sharp on E: the last bar becomes the
# dominant of the first, and the repeat button closes the circle
bars.append(bars[-1].replace("Am7", "A7").replace("c", "^c"))

print(" | ".join(bars), "|]")   # the abc code to render the score
# "Dm7"[DFAc]4 | "G7"[GBdf]4 | "Cmaj7"[CEGB]4 | "Fmaj7"[FAce]4 | ...
# ... "Bm7b5"[Bdfa]4 | "E7"[E^GBd]4 | "Am7"[Aceg]4 | "A7"[A^ceg]4 |]
```

```abc
X:1
M:4/4
L:1/4
K:C
"Dm7"[DFAc]4 | "G7"[GBdf]4 | "Cmaj7"[CEGB]4 | "Fmaj7"[FAce]4 | "Bm7b5"[Bdfa]4 | "E7"[E^GBd]4 | "Am7"[Aceg]4 | "A7"[A^ceg]4 |]
```

## From score to sound

In the editor the headphone on the block plays it, and each note lights up on the score as it sounds; in HTML the `.play` class puts a player bar under the score, and the PDF keeps the engraving alone.

```{.abc .play}
X:1
M:3/4
L:1/8
Q:1/4=110
K:Am
V:1
G2 G2 A2 | B2 B2 B2 | B2 c B c2 | B4 z2 | z2 A G A2 | G4 z2 |
A2 B2 c2 | d2 d2 d2 | e2 d c B2 | A2 z4 |
A2 B2 c2 | d2 d2 d2 | e2 f e d2 | c2 z2 e d | c2 B2 z2 | d c B2 A2- | A6 |]
V:2 clef=bass
[G,,B,,D,]4 [G,,B,,D,]2 | [E,G,B,]4 [E,G,B,]2 | [C,E,G,]4 [C,E,G,]2 | [G,,B,,D,]6 | [A,,C,E,]4 [A,,C,E,]2 | [E,G,B,]6 |
[E,A,,C]2 [E,A,C]4 | D,,4 [D,F,A,]2 | E,,2 [E,^G,B,]2 [E,^G,B,]2 | A,,2 E,2 C2 |
A,,2 C,2 E,,2 | [D,,F,]4 [D,F,A,]2 | [C,E,G,]4 [G,,B,,D,]2 | C,,4 [E,G,C]2 | E,,2 [E,,^G,B,]4 | D,,2 B,,2 A,,2- | A,,6 |]
```