<!-- source: https://github.com/teajuw/bar2vec/tree/b181f210c99b30a5f25e170f040eecded2f7c230 | last updated: 2026-11-16 | page: https://trevorju.dev/bar2vec/ -->

# bar2vec

One vector per bar of music, used to cut a song into sections and name them.

Nobody tells bar2vec where the chorus is: an audio model that has never seen a labelled song section turns each bar into a list of numbers, and the sections fall out of asking which bars sound like which other bars. On 180 songs that played no part in tuning, its cuts score 72.7 against human annotations (boundary F-measure within three seconds), up from 35.2 for the fixed-window version it started as.

## Terms

This article uses the following definitions:

- A **bar** is one measure of music, about two seconds long. A beat tracker finds the bar lines.
- An **embedding** is the list of 3,840 numbers the audio model returns for one bar. Two bars that sound alike get similar lists.
- The **audio model** (MATPAC++ [1]) is pretrained and frozen. It was never trained on song structure.
- A **section** is a stretch of bars that sound alike, and a **cut** is where one section ends and the next starts.
- The **cutter** compares every bar with every other bar and cuts where the song changes. It uses no training.
- The **namer** gives each section a name: intro, verse, pre-chorus, chorus, post-chorus, bridge, outro or instrumental. The **rules** use the song's shape alone. The **classifier** is trained on 150 annotated songs; it is the one part that was told what a chorus is. The demo uses it with one addition: every bar is also named on its own and a section's bars vote.
- A bar's **colour** in the demo is its embedding squeezed to three numbers, the three directions that vary most across the song, shown as red, green and blue. It keeps about 35% of the variation and is only for looking: the cutter uses all 3,840 numbers.

## Results

Everything is measured on the Harmonix Set [2], pop, hip-hop, dance and country songs with human-annotated sections. Settings were chosen on 153 songs, checked on 154 held-out songs drawn at random, and confirmed once on 180 songs locked away until the end.

Cutting, on the 180 locked songs. The score counts cuts that land near an annotated boundary, balanced against false cuts.

| Cutter | Within 3 s | Within 0.5 s |
|---|---|---|
| Starting point: 10-second windows | 35.2 ±2.2 | 7.0 ±1.4 |
| bar2vec, no training | 72.7 ±2.6 | 53.9 ±3.4 |

Naming, on the same songs, given the annotated boundaries. The score is macro-F1 over the eight names, so a rare name such as bridge counts as much as verse.

| Namer | Macro-F1 | Sections named right |
|---|---|---|
| Starting point: match each section to a text prompt | 9.3 ±2.0 | 14.0% |
| Rules, no training | 55.9 ±3.9 | 63.8% |
| Classifier | 65.9 ±3.7 | 74.7% |

End to end, bar2vec cuts a song and then names its own sections. Scored by time on the held-out songs, the classifier reaches 53.1 ±2.9, and 55.8 ±3.2 with the bar votes added (+2.7 ±2.3). Perfect names on the same cuts would reach 89.5. Naming is the weaker half.

## How it works

The project began as search over 10-second windows embedded with a text-audio model (CLAP [3]). Its cuts could be scored against published annotations, so they were. A March 2026 paper had shown that a frozen audio model can find section boundaries without training [4]; this project takes its two main ideas, bars instead of windows and block matching, and adds a held-out split and a locked final test.

The cutter:

1. A beat tracker (Beat This! [5]) finds the bar lines.
2. The audio model turns each bar into one embedding.
3. Every bar is compared with every other bar.
4. Block matching proposes the split whose blocks of similar bars hold together best.
5. Any cut where the bars on either side do not change is dropped.

The namer's rules: the first section is the intro and the last is the outro, the most repeated group of sections is the chorus, a one-off late in the song is the bridge, short repeats beside a chorus are the pre-chorus or post-chorus, and a section with far less singing is instrumental. Asking a text-audio model which section sounds like a chorus scores at chance: a section's role is a position in the song, not a sound.

## How it got there

Twelve rounds, one change each. A change was kept only if the tuning songs and the held-out songs both improved by more than the noise between songs.

![Step graph of the held-out boundary score over the twelve rounds of the cutter climb. The kept rounds climb from 34.5 to 42.6 at round 1 and 50.8 at round 3, jump to 67.9 at round 7 with one vector per bar, then reach 70.0 and 72.2. Six rounds that were tried and not kept sit on or below the line. The supervised filter at round 12 is marked separately at 75.4.](https://raw.githubusercontent.com/teajuw/bar2vec/b181f21/article/climb.svg)

- **Where a cut may fall mattered most.** Moving from fixed windows to one embedding per bar added 17 points. Swapping in a better audio model added about 2.
- **The first 16 points cost nothing.** Two early rounds only changed where a cut is reported, with the same embeddings.
- **The beat tracker did not leak.** It was trained on Harmonix, so each song was tracked by a model that never saw it. Bars from human annotators gave the same score.

Naming was climbed the same way, one rule a round. Every round of both climbs is in the repo's [process log](https://github.com/teajuw/bar2vec/blob/main/PROCESS.md).

## What did not work

- **A bar-level transformer** for names reached 45 macro-F1 against 57 for the rules on the same songs. It learned the common names and missed the rare ones.
- **Twice the labelled songs** did not help the classifier on held-out songs (+0.3 ±2.4).
- **Fitting the classifier on bar2vec's own cuts** did not raise the end-to-end score on held-out songs (+0.1 ±2.2).
- **Moving cuts to the nearest beat.** About 100 rule settings and a learned placer all failed to beat noise.
- **A learned filter on the cuts** does score higher (75.4) but is trained on one pop-heavy dataset, so the training-free cutter is the one reported.

## Limits

- Only Harmonix has been measured, and the naming rules encode Western pop song form.
- The classifier loses 13 points when it names bar2vec's own cuts instead of annotated sections. Bar votes win back under 3.
- 487 of Harmonix's 912 songs could be matched to audio. The ones that matched may differ from the ones that did not.
- Nobody has listened to the demo's songs in full. 29% of their section names are marked unsure.

## Next

- Confirm the bar-vote gain on the 180 locked songs, which have never been scored end to end.
- Try other ways of grouping bars for picking the chorus, the rules' largest remaining loss.
- Test whether a section's name can be read off its nearest neighbours in other songs.
- Test on music outside Harmonix.

## Sources

1. [MATPAC++: Enhanced Masked Latent Prediction for Self-Supervised Audio Representation Learning](https://arxiv.org/abs/2508.12709), Quelennec et al., arXiv, August 2025. The audio model.
2. [The Harmonix Set](https://github.com/urinieto/harmonixset), Nieto et al., ISMIR 2019. The songs and their human-annotated sections.
3. [Large-scale Contrastive Language-Audio Pretraining with Feature Fusion and Keyword-to-Caption Augmentation](https://arxiv.org/abs/2211.06687), Wu et al., arXiv, November 2022. CLAP, the text-audio model the project started with.
4. [Unsupervised Evaluation of Deep Audio Embeddings for Music Structure Analysis](https://arxiv.org/abs/2603.27218), Marmoret, arXiv, March 2026. The paper the bar embeddings and block matching come from.
5. [Beat This! Accurate beat tracking without DBN postprocessing](https://arxiv.org/abs/2407.21658), Foscarin et al., arXiv, July 2024. The beat tracker.

Live demo: https://trevorju.dev/bar2vec/demo/

---

## Notes

### What it is
A music-structure project: it cuts a song into sections and names each one (intro, verse, chorus, bridge and so on) from frozen audio embeddings, one vector per bar. Both parts were tuned as hill climbs against the Harmonix Set with a random train / held-out split and a locked 180-song final test. Final test: cutter 72.7 (boundary F within 3 s) and 53.9 (within 0.5 s) with no training; namer macro-F1 55.9 with rules and 65.9 with a trained classifier. It began as a moment-search prototype on one playlist, and those pages are still in the repo.

### Where things are
- `article/` — the article for the site (it opens with a Terms section; use those words: bar, embedding, audio model, similarity grid, cut, cutter, namer, rules, classifier; it ends with a Sources section, cited in the text as `[n]`, following the site's ARTICLES.md) and its climb charts (step graphs: kept rounds as a staircase, hollow dots for rounds not kept); the article shows only `climb.svg`, `names.svg` is unused. The article is kept short, about 1,250 words: round-by-round detail lives in `PROCESS.md`, which it links to.
- `article/demo/` — the live demo shown at the top of the page, two stacked parts with no tabs, "Listen to the result" then "See how it works": `index.html` embeds six songs' official YouTube uploads and draws bar2vec's sections under the player; `sections.json` is its data, made by `scripts/demo_songs.py` from `songs.json`. A search box covers 265 more songs in `catalogue.json`: the top 40 of each Billboard year-end Hot 100, 2018 to 2025, that have an upload on the artist's own channel allowing embedding (`scripts/catalogue_list.py` builds the list into `data/catalogue_candidates.json`, `scripts/catalogue_match.py` finds the uploads into `data/catalogue_songs.json`, `scripts/demo_songs.py --catalogue` runs the pipeline and deletes each song's audio). The second part is an animation, started the first time it scrolls into view, following one bar of one song through the method, including inside the audio model (patches, each patch's 768 numbers, the twelve layers, joining and averaging into 3,840): `onebar.js` draws it from `bar.json`, made by `scripts/bar_story_data.py`. No audio is hosted; the audio fetched for analysis stays in the git-ignored `audio_demo/`.
- `PROCESS.md` — the full process log: timeline, every round, who decided what, what can and cannot be claimed, how to reproduce the final rows. Start here.
- `cuts.json`, `scripts/cuts.py` — the cutter and its settings (the surface the first climb tuned). `scripts/hx_run.py` scores it.
- `names.json`, `scripts/names.py` — the namer and its settings (the second climb). `scripts/nm_run.py` scores it on annotated boundaries; `scripts/nm_e2e.py` scores names end to end on the cutter's own cuts, by time; `scripts/name_model.py` is the trained classifier. `scripts/nm_cuts_fit.py` fits and scores namers end to end on the cutter's own cuts (third climb, record in `.claude/hillclimb/section-names-cuts/`); its "stack" regime, the section classifier with per-bar votes added to its inputs, is saved as `data/name_model_cuts.joblib` and is what `scripts/demo_songs.py` uses. Per-bar embeddings of the catalogue songs are kept in the git-ignored `data/catalogue_cache/`, so re-naming needs no audio.
- `scripts/` — everything else: `hx_*` prepare and embed Harmonix songs (download, align, bar tracking, embeddings), `playlist_bars.py`, `playlist_bars_extra.py` and `breakdown_bars.py` run the cutter and the trained namer on the playlist (`--namer rules` for the rule names), `make_*_page.py` build the HTML pages, `bar_transformer.py` is the dropped bar-level transformer.
- `.claude/hillclimb/` — one folder per climb with per-round scores, run logs, `narrative.md` and `_state.json` (split ids). `song-cuts` and `section-names` are the two climbs; `*-final` are the locked test runs; `section-names-e2e` holds the end-to-end name scores.
- `data/` — track lists, alignment results, bar positions, small fitted models and page data. Embeddings (`*.npz`), audio and model weights are not in the repo.
- `breakdown.html`, `similar.html`, `readout.html`, `results_*.html` — generated pages for the playlist. `breakdown.html` shows three cutters per song with names and a scrubbing playhead; names come from the trained classifier, and a name it gives less than 0.8 probability ends in `?`; it needs the local audio to play.
- `papers/` — notes on music language models with links to ten arXiv papers.
- `spotify-landscape-2026-11/` — the research pass on what Spotify and competitors ship, and the idea list the project came from.
- `brief.md`, `brief-song-structure.md` — the project brief and a paper-style brief for the structure work.

### How to run it
- Python with `uv`; scripts run from the repo root as `uv run scripts/<name>.py`.
- Nothing runs from a fresh clone without the audio: Harmonix does not distribute it, so `scripts/hx_download.py` and `scripts/hx_align.py` have to fetch and align it first, and the embedding scripts need the model weights in `models/` and `external/`.
- Score a cutter round: `uv run scripts/hx_run.py --variant <name>`. Score a naming round: `uv run scripts/nm_run.py`. The exact commands for the final rows are at the end of `PROCESS.md`.
- There are no unit tests. The climbs' recorded per-song grades act as the regression check: the runner reproduced all 307 before the final test.

### Tried and dropped
- Fixed 10 s windows as the unit: replaced by one embedding per bar, the largest single gain (+17).
- A window every 2 s, block matching alone, and three bars of context per vector: each reverted, no held-out gain.
- Beat-level cut placement: about 100 rule settings and a learned placer, none beat noise; a control trained on shuffled targets got about half the placer's gain.
- Chroma change score, a guard gap between compared sides, other encoder layers, per-song PCA: no gain on tuning songs, no round spent.
- Naming by matching a section to a text prompt: scores at chance.
- Loudness as a hint for the chorus group: gained on tuning songs, failed on held-out.
- A bar-level transformer for naming: about 45 macro-F1 in cross-validation against 63 for boosted trees on the same 150 songs. Attention helped, rare labels were not learned.
- Doubling the training labels with 213 partly aligned Harmonix songs: +0.3 ±2.4 on held-out, not adopted.
- Using neighbour similarity to rename sections: short lead-in to the chorus becomes prechorus, sounds like the chorus group becomes chorus, neighbour order in the chorus pick. Best +0.5 ±0.6 on the tuning songs, no held-out round spent. The flag itself is reliable (such a verse is truly a verse 47% of the time against 73% otherwise), and was shown on the playlist page as `verse?` until the trained namer replaced the rule names there.
- The static step-by-step figure (`process.svg`), the round-by-round tables and the naming chart in the article: removed when the user asked for a short page; the animation shows the same steps with real data and every round is in `PROCESS.md`.
- Tabs in the demo: replaced by two stacked parts, listening first, at the user's request.
- Horizontal bar charts in the article: replaced by step graphs, which show the shape of a climb and are shorter on screen.
- The learned keep-or-drop filter on cuts (75.4) is kept only as a separate supervised row; the training-free cutter is the project's cutter, because the filter was learned from one pop-heavy dataset.
- For the demo: hosting freely licensed music (the well-known free recordings are not pop form) and 30-second store previews (too short to show structure); embedding official YouTube uploads was used instead.
- Letting visitors look up any song: the YouTube player gives a page no audio, fetching it on request breaks YouTube's terms, and the site has no server. A catalogue processed ahead of time is used instead.
- An unpaced run of the upload matcher: YouTube refused it partway and 88 songs were recorded as missing; requests are now spaced and errored lookups retried.
- Git history before 2026-11-13 was squashed when the repo went public, to remove paper PDFs and a download record.
- Fitting the trained namer on the cutter's own sections: +2.7 ±2.2 on tuning songs, +0.1 ±2.2 on held-out, not kept. The 13-point loss on cuts is about 4 from misplaced edges and 8 from the classifier's inputs changing when edges move.
- Per-bar votes alone, with no section classifier: +2.1 ±2.5 on tuning songs, not scored on held-out; the votes are used as extra inputs instead (held-out 53.1 to 55.8, +2.7 ±2.3, kept by 0.4 over noise).

### Parked
- Scoring the bar-vote namer once on the 180 locked songs, which have never been scored end to end. The playlist page still uses the earlier classifier.
- Listening to the trained names on the playlist page, the six demo songs and the 265 catalogue songs (none has annotations, so nothing there is scored). Across the catalogue 29% of sections are marked unsure and 8 songs have no section named chorus (with the bar-vote namer; 444 of 2,480 demo names changed when it came in and none has been listened to); the catalogue videos were accepted on YouTube's embedding flag and not all played in a browser.
- Learned attention between sections for naming (the rule-based stand-in was tried and dropped); worth returning to with more labelled songs or pretraining on unlabelled music.
- Other ways of grouping bars (k-means, spectral clustering) for the chorus pick, and nearest neighbours across songs for names. Either would reopen the naming climb, which the stopping rule closed.
- Re-cutting inside a long section at a finer scale.
- Section-by-section song similarity.
- Testing on music outside Harmonix.
- Tempo doubling or halving on about 15 of 81 playlist songs.
- Separating the encoder gain in cutter round 10 from dropping the repeat-to-10-s step.
