Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 53x 53x 53x 25x 53x 53x 25x 53x 53x 53x 24x 24x 1x 1x 19x 19x 19x 19x 19x 19x 19x 19x 19x 38x 30x 23x 22x 19x 105x 105x 32x 32x 2x 2x 2x 26x 26x 26x 15x 13x 12x 32x 7x 7x 7x 7x 4x 4x 3x 2x 2x 7x 7x 7x 32x 105x 105x 1x 1x 1x 91x 91x 19x 19x 19x 96x 32x 32x 32x 32x 96x 19x 19x 19x 19x 19x 36x 36x 36x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 32x 45x 45x 45x 13x 45x 45x 45x 8x 7x 45x 9x 9x 9x 4x 4x 9x 9x 9x 9x 9x 5x 2x 1x 1x 9x 9x 9x 36x 36x 64x 18x 64x 36x 36x 36x 36x 36x 36x 36x 36x 33x 36x 36x 1x 1x 33x 33x 31x 22x 36x 33x 33x 33x 30x 21x 20x 15x 15x 36x 10x 10x 10x 10x 10x 10x 10x 36x 33x 33x 20x 11x 1x 36x 33x 48x 31x 17x 17x 17x 17x 17x 17x 33x 33x 48x 31x 48x 33x 33x 36x 44x 44x 44x 44x 44x 9x 9x 1x 1x 9x 9x 9x 9x 4x 4x 4x 4x 9x 4x 4x 4x 4x 4x 4x 4x 4x 4x 9x 5x 5x 5x 5x 5x 5x 5x 5x 5x 9x 9x 9x 9x 9x 44x 1x 1x 1x 44x 1x 1x 1x 1x 1x 1x 33x 44x 1x 1x 1x 1x 1x 44x 9x 9x 9x 6x 1x 1x 9x 9x 8x 5x 3x 3x 3x 3x 9x 3x 3x 9x 9x 9x 24x 24x 36x 40x 31x 30x 30x 3x 3x 3x 40x 33x 36x 36x 36x 33x 8x 33x 33x 33x 8x 33x 36x 43x 43x 43x 36x 36x 40x 9x 9x 9x 9x 4x 4x 5x 9x 9x 9x 9x 9x 9x 9x 40x 1x 1x 1x 1x 1x 1x 30x 30x 33x 33x 33x 14x 14x 14x | import {
TAB_LINE,
isChord,
isChordLine,
mergeChordLine,
placeChords,
placeInlineChords,
parseHeader,
sectionType,
} from './chordpro'
import {linesToAbc, readNoteLine, type WrittenLine} from './notes'
/**
* OnSong → ChordPro. OnSong songs are plain text: the title, the artist and
* "Key: G" lines on top, then paragraphs separated by blank lines, with
* chords either inline ("[G]words") or on their own line above the words.
* Sections are sometimes labelled ("Chorus:", "VERSE 2:"), sometimes by an
* instruction ("intro- sax solo", "piano solo") and often not at all; a
* blank line is the only boundary. So each paragraph becomes its own
* section, labelled when it says what it is.
*
* A player's own part notes on top ("Piano - Light and airy", "Organ -
* Think Doors") belong to that player, not the band's chart: they come back
* separately as `partNotes`, for a personal cue.
*/
export type OnSongIn = {
title: string
/** Other names it went by, recognised (and dropped) in the header */
aliases?: string[]
byline?: string | null
key?: string | null
content: string
}
export type OnSongOut = {source: string; partNotes: string[]}
const META =
/^(key|tempo|time|capo|duration|ccli|copyright|artist|title|author|bpm|flow|book|number)\s*:/i
const HEADER_DIRECTIVE =
/^\{\s*(t|title|st|subtitle|artist|key|capo|tempo|time|duration|copyright|ccli)\s*(:.*)?\}$/i
/** "Piano - Light and airy", "E. Piano", "Organ - Think Doors" */
const PART_NOTE =
/^(e\.?\s*piano|electric piano|piano|organ|synth|keys|keyboards?|clav(inet)?|rhodes|wurli(tzer)?)\b(\s*[-–:].*)?$/i
/** A line that says what follows: "intro- sax solo", "piano solo", "(softer)" */
const INSTRUCTION =
/\b(intro|outro|solo|break|verse|chorus|bridge|instrumental|interlude|ending|end|vamp|tag|turnaround|horns?|riff|bars?|repeat|x\d+)\b/i
/**
* A line that tells the band what to do rather than what to sing: "guitar
* intro 2 bars", "x2", "main riff 6x", "BACK TO SLOWER TEMPO". It's a note,
* never the words a chord line above it sits on.
*/
const NOTE_WORDS =
/\b(intro|outro|solos?|riffs?|bars?|repeats?|modulates?|guitars?|piano|keys|keyboards?|organ|sax|horns?|drums?|bass|vamp|turnaround|instrumental|double time|tempo|fills?|percussion|chords|x ?\d+|\d+ ?x|\d+ times|(two|three|four) times)\b/i
export function isNoteText(line: string) {
const t = line.trim()
if (!t || /[[{]/.test(t) || readNoteLine(t)) return false
if (t.split(/\s+/).length > 14) return false
// Beat slashes are for the chord row above; what's left decides.
// "/ BREAK" under a chord is the chord's beats, not a note
const words = t.replace(/(^|\s)\/+\.?(?=\s|$)/g, ' ').trim()
if (words !== t && (!words || /^(break|stop|end|hold)\W*$/i.test(words)))
return false
// A section's name on its own line: "Verse", "Chorus 2"
if (/^(verse|chorus|bridge|intro|outro|solo|tag)\s*\d*\s*:?$/i.test(t))
return true
// All capitals and no lyric shape: "BACK TO SLOWER TEMPO"
if (/^[^a-z]*[A-Z]{3}[^a-z]*$/.test(words) && !/[?!]$/.test(words))
return true
return NOTE_WORDS.test(words)
}
const norm = (s: string) => s.toLowerCase().replace(/[^a-z0-9]+/g, '')
export function convertOnSong(song: OnSongIn): OnSongOut {
const lines = song.content
.replace(/\r\n?/g, '\n')
.replace(/ /g, ' ')
.split('\n')
.map((l) => l.replace(/\s+$/, ''))
// The header: title, byline, metadata, copyright, and part notes, up to
// the first line that carries a chord
const partNotes: string[] = []
const body: string[] = []
let inHeader = true
// Where the music starts: a chord, a section label, or written-out notes
const hasChord = (l: string) =>
/\[[^\]]+\]/.test(l) ||
isChordLine(l) ||
Boolean(parseHeader(l.trim())) ||
(/^\s*\(/.test(l) && readNoteLine(l) !== null)
for (const [i, line] of lines.entries()) {
const t = line.trim()
if (inHeader) {
if (!t) continue
// Checked first: a part note can name chords ("Piano - Hit the [G]")
if (PART_NOTE.test(t)) {
partNotes.push(t)
continue
}
// The music starts at a chord, or at a label directly above one
const next = lines[i + 1]
if (
hasChord(line) ||
(next?.trim() &&
!PART_NOTE.test(next.trim()) &&
(hasChord(next) || TAB_LINE.test(next)))
)
inHeader = false
else {
if (
!(
norm(t) === norm(song.title) ||
(song.aliases ?? []).some((a) => norm(t) === norm(a)) ||
(song.byline && norm(t) === norm(song.byline)) ||
META.test(t) ||
HEADER_DIRECTIVE.test(t) ||
t.startsWith('©')
)
)
// Anything else before the music ("note- intro made longer") is
// for everyone: keep it as a remark, and keep looking for part
// notes
body.push(/^\{.*\}$/.test(t) ? t : `{comment: ${t}}`, '')
continue
}
}
if (HEADER_DIRECTIVE.test(t) || META.test(t)) continue
// A rule of dashes is a divider, not tab: a paragraph break
if (/^[-_=]{3,}$/.test(t)) {
body.push('')
continue
}
body.push(line)
}
// Paragraphs
const paragraphs: string[][] = []
let cur: string[] = []
for (const l of body) {
if (l.trim()) cur.push(l)
else if (cur.length) {
paragraphs.push(cur)
cur = []
}
}
if (cur.length) paragraphs.push(cur)
const out: string[] = [`{title: ${song.title}}`]
if (song.key) out.push(`{key: ${song.key}}`)
const ctx: Ctx = {key: song.key ?? null, known: new Map()}
for (const p of paragraphs) {
out.push('')
out.push(...convertParagraph(p, ctx))
}
return {source: out.join('\n') + '\n', partNotes}
}
type Ctx = {
key: string | null
/** Note lines seen so far in this song: a repeat of one is one too */
/** ...and whether each became tab: a repeat is written the same way */
known: Map<string, boolean | null>
}
/** A line that says what the notes under it are: "horn line is" */
const DESCRIBES =
/\b(notes?|horns?|line|riff|melody|lick|sax|keyboards?|keys|piano|organ|strings?|guitar|bass|plays?|under this|unison)\b/i
const SAYS_NOTES = /\b(notes?|horns?|line|riff|melody|lick|unison)\b/i
/** Named instruments: these decide outright */
const TAB_PART = /\b(guitar|bass)\b/i
/** "Riff" leans to tab, but only if nothing else has decided */
const RIFF = /\briff\b/i
const NOT_TAB =
/\b(horns?|sax|keyboards?|keys|piano|organ|strings?|trumpet|trombone)\b/i
const signature = (w: WrittenLine) =>
w.notes
.slice(0, 6)
.map((n) => n.letter + n.acc)
.join(' ')
/**
* Is this line written-out notes rather than chords? Letters alone can be
* either ("D D C C G G G G" is four bars of chords). Notes when bracketed
* the way players write melodies, when the line above says so ("horn line
* is", "intro keyboard notes"), or when it repeats a line already read as
* notes -- never when the line above says "chords".
*/
function noteLine(line: string, before: string, ctx: Ctx) {
const w = readNoteLine(line)
if (!w) return null
const bracketed = /^\(/.test(line.trim())
// Unbracketed letters need the line above to say they're notes: an
// instrument alone ("solo- guitar") is just as likely over chords
const said = SAYS_NOTES.test(before) && !/chord/i.test(before)
if (
bracketed ||
ctx.known.has(signature(w)) ||
(said && w.notes.length >= 5)
) {
if (!ctx.known.has(signature(w))) ctx.known.set(signature(w), null)
return w
}
return null
}
/** A line already written once keeps its first form (notation or tab). */
/**
* Notation or tab for a run of note lines: an instrument named in its own
* label decides ("sax comes in, plays this"); otherwise the way the same
* line was written the first time; otherwise "riff", or the instrument in
* the paragraph's label.
*/
function chooseTab(ctx: Ctx, w: WrittenLine, name: string, para: string) {
const key = signature(w)
const seen = ctx.known.get(key)
let asTab: boolean
if (NOT_TAB.test(name)) asTab = false
else if (TAB_PART.test(name)) asTab = true
else if (seen !== null && seen !== undefined) asTab = seen
else
asTab =
RIFF.test(name) ||
(!NOT_TAB.test(para) && (TAB_PART.test(para) || RIFF.test(para)))
if (seen === null || seen === undefined) ctx.known.set(key, asTab)
return asTab
}
// "[A]ev[D-D]eryo[C]ne accents this then 4 bars guitar solo": chords
// typed into an instruction. The chords, then the instruction as a note
function splitInlineNotes(lines: string[]) {
return lines.flatMap((l) => {
if (!/\[/.test(l) || /^\s*\{/.test(l)) return [l]
const text = l.replace(/\[[^\]]*\]/g, '')
// "Intro: [A] [Bm]" is a labelled section, not a note
if (!isNoteText(text) || /:\s*$/.test(text.trim())) return [l]
const chords = [...l.matchAll(/\[([^\]]*)\]/g)].map((m) => `[${m[1]}]`)
return [chords.join(' '), text.trim()]
})
}
function convertParagraph(para: string[], ctx: Ctx): string[] {
const p = splitInlineNotes(para)
const first = p[0].trim()
// A directive on its own ({chorus}, {comment: ...}) passes through
if (p.length === 1 && /^\{.*\}$/.test(first)) return [first]
// "(softer, hold each chord)" alone: a remark
if (p.length === 1 && /^\(.*\)$/.test(first) && !/\[/.test(first))
return [`{comment: ${first.slice(1, -1)}}`]
// A row of chords alone ("[A///] [A-A-G-F#-F-E]") is never a heading
let label: {label: string; note: string; chords: string} | null =
/^(\s*\[[^\]]+\])+\s*$/.test(first) ? null : parseHeader(first)
// "Intro: [A] [Bm] [A] [Bm]": the chords to play, not a remark
if (label?.note && /^(\s*\[[^\]]+\]\s*)+$/.test(label.note)) {
label = {...label, chords: label.note.trim(), note: ''}
}
// OnSong's "{chorus}" heading a paragraph: this paragraph is the chorus
if (
!label &&
p.length > 1 &&
/^\{\s*(chorus|soc|start_of_chorus)\s*\}$/i.test(first)
)
label = {label: 'Chorus', note: '', chords: ''}
let rest = p.slice(1)
if (
!label &&
p.length > 1 &&
!/\[/.test(first) &&
!isChordLine(first) &&
first.length <= 40 &&
INSTRUCTION.test(first)
) {
// "intro- sax solo" over its chords: the section's name
const [name, ...more] = first.split(/\s*[-–]\s+|\s*-(?=\s*[a-z])/i)
label = {
label: capitalise(name.trim()),
note: more.join(' - ').trim(),
chords: '',
}
}
if (!label) rest = p
// "(softer, hold each chord)" heading a paragraph: a remark about it
if (
!label &&
rest.length > 1 &&
/^\([^[\]]*\)$/.test(rest[0].trim()) &&
!readNoteLine(rest[0])
)
rest = [`{comment: ${rest[0].trim().slice(1, -1)}}`, ...rest.slice(1)]
// Chords typed inline: "[A[fine" for "[A]fine", "[G6-]" (a hold mark on
// the chord), and each chord onto its syllable (see placeChords)
rest = rest.map((l) =>
/^\s*\{/.test(l) || !/\[/.test(l)
? l
: placeInlineChords(
l
.replace(/\[([A-G][^[\]\s]*)\[/g, '[$1]')
.replace(/\[([A-G][^\]]*?)-\]/g, (m, c) =>
isChord(c) ? `[${c}]` : m,
),
),
)
// "A / Cm7 / Bm7 /E7": a beat slash typed against its chord
rest = rest.map((l) => {
if (/\[/.test(l)) return l
const spaced = l.replace(/(^|\s)\/(?=[A-G])/g, '$1/ ')
return spaced !== l && isChordLine(spaced) ? spaced : l
})
const lines: string[] = []
for (let i = 0; i < rest.length; i++) {
const line = rest[i]
const t = line.trim()
const before = i > 0 ? rest[i - 1].trim() : first
const written = noteLine(line, before, ctx)
if (written) {
// The run of note lines, and the line above that names them
const run: WrittenLine[] = [written]
while (i + 1 < rest.length) {
const w = noteLine(rest[i + 1], rest[i].trim(), ctx)
if (!w) break
run.push(w)
i++
}
let name = ''
const last = lines[lines.length - 1]
if (
last !== undefined &&
!last.startsWith('\u0000') &&
!/\[/.test(last) &&
last.trim().length <= 50 &&
DESCRIBES.test(last)
) {
name = capitalise(
lines
.pop()!
.trim()
.replace(/[:>-]+$/, '')
.replace(/^[\s:>-]+/, '')
.trim(),
)
}
// Notes straight under the paragraph's label: the label names them
if (!name && !lines.length && label) {
name = [label.label, label.note]
.filter(Boolean)
.join(' ')
.replace(/[\s:>-]+$/, '')
label = null
const asTab = chooseTab(ctx, run[0], name, '')
lines.push('\u0000written', JSON.stringify({name, asTab, run}))
continue
}
const para = `${label?.label ?? ''} ${label?.note ?? ''}`
const asTab = chooseTab(ctx, run[0], name, para)
lines.push('\u0000written', JSON.stringify({name, asTab, run}))
continue
}
if (/^\{.*\}$/.test(t)) {
lines.push(t)
continue
}
if (TAB_LINE.test(line)) {
// Tab can't sit inside a lyric section: its own block
const tab: string[] = []
while (i < rest.length && TAB_LINE.test(rest[i])) tab.push(rest[i++])
i--
lines.push('\u0000tab', ...tab, '\u0000endtab')
continue
}
// "[Emaj7] [C#m7]" over the words: a chord row typed with
// brackets. Each chord goes where its bracket was typed
const bracketRow = /^(\s*\[[^\]]+\])+\s*$/.test(line)
if (bracketRow) {
const next = rest[i + 1]
if (
next !== undefined &&
next.trim() &&
!/[[{]/.test(next) &&
!isChordLine(next) &&
!isNoteText(next) &&
!readNoteLine(next) &&
!TAB_LINE.test(next)
) {
const marks = [...line.matchAll(/\[([^\]]+)\]/g)].map((m) => ({
col: m.index!,
chord: m[1],
}))
lines.push(placeChords(next, marks))
i++
continue
}
}
if (!/\[/.test(line) && isChordLine(line)) {
const next = rest[i + 1]
// Not onto a line that introduces written-out notes: that line is a
// label, not lyrics
const introducesNotes =
next !== undefined &&
SAYS_NOTES.test(next) &&
rest[i + 2] !== undefined &&
readNoteLine(rest[i + 2]) !== null
if (
!introducesNotes &&
next !== undefined &&
// "guitar intro 2 bars" under a chord line is a note about it
!isNoteText(next) &&
next.trim() &&
!/\[/.test(next) &&
!isChordLine(next) &&
!TAB_LINE.test(next)
) {
lines.push(mergeChordLine(line, next))
i++
} else lines.push(mergeChordLine(line, ''))
continue
}
lines.push(line)
}
// What's left of the instructions are notes, not lyrics
for (let i = 0; i < lines.length; i++) {
if (lines[i] === '\u0000written') i++
else if (lines[i] === '\u0000tab') while (lines[i] !== '\u0000endtab') i++
else if (isNoteText(lines[i]))
lines[i] = `{comment: ${lines[i]
.trim()
.replace(/[\s:>-]+$/, '')
.replace(/^[\s:>-]+/, '')}}`
}
// Tab blocks split the paragraph into lyric parts and tab parts
const out: string[] = []
const type = label ? sectionType(label.label) : null
let open = false
const openSection = () => {
if (!label || open) return
out.push(
`{start_of_${type}: ${label.label}${label.note ? ` (${label.note})` : ''}}`,
)
if (label.chords) out.push(label.chords)
open = true
}
const closeSection = () => {
if (open) out.push(`{end_of_${type}}`)
open = false
}
openSection()
for (let i = 0; i < lines.length; i++) {
if (lines[i] === '\u0000written') {
const {name, asTab, run} = JSON.parse(lines[++i]) as {
name: string
asTab: boolean
run: WrittenLine[]
}
closeSection()
const head = name ? `: ${name}` : ''
// Every lick is notation; guitar and bass parts carry their
// instrument, so the tab view shows the right strings
const instrument = asTab
? /\bbass\b/i.test(`${name} ${label?.label ?? ''}`)
? 'bass'
: 'guitar'
: null
out.push(
`{start_of_abc${head}}`,
linesToAbc(run, ctx.key, instrument),
'{end_of_abc}',
)
continue
}
if (lines[i] === '\u0000tab') {
closeSection()
out.push('{start_of_tab}')
while (lines[++i] !== '\u0000endtab') out.push(lines[i])
out.push('{end_of_tab}')
continue
}
out.push(lines[i])
}
closeSection()
return out
}
function capitalise(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1)
}
|