| 1 |
unit mtxline; |
| 2 |
{ Break input up into words, classify them. Supply words and |
| 3 |
information about them. } |
| 4 |
|
| 5 |
interface uses globals; |
| 6 |
|
| 7 |
type |
| 8 |
music_word = (other, abcdefg, zword, lyrtag, |
| 9 |
lparen, rparen, rlparen, lbrac, rbrac, |
| 10 |
pmxprefix, pmxl, macro, endmacro, mword, oword, rword, nextvoice, |
| 11 |
barword, texword, atword, FirstOnly, err ); |
| 12 |
|
| 13 |
const |
| 14 |
bind_left: array[music_word] of boolean = ( |
| 15 |
false, false, true, false, |
| 16 |
false, true, true, false, true, |
| 17 |
true, true, false, true, false, true, false, true, |
| 18 |
false, false, false, false, false ); |
| 19 |
{ changed bind_left[barword] to false } |
| 20 |
|
| 21 |
var selected: array[voice_index] of boolean; |
| 22 |
|
| 23 |
procedure error3(voice: voice_index; message: string ); |
| 24 |
procedure warning3(voice: voice_index; message: string ); |
| 25 |
procedure getNextMusWord (var buf, note: string; var nscan: music_word); |
| 26 |
function MusicWord(voice, n: integer): string; |
| 27 |
function nextMusicWord(voice: voice_index): string; |
| 28 |
function thisNote(voice: voice_index): music_word; |
| 29 |
function nextNote(voice: voice_index): music_word; |
| 30 |
function getMusicWord(voice: voice_index): string; |
| 31 |
procedure gotoBar(voice: voice_index; bar_no: integer); |
| 32 |
function endOfBar(voice: voice_index; bar_no: integer): boolean; |
| 33 |
function getBar(voice:voice_index; bar: integer): string; |
| 34 |
function upper(voice: voice_index): boolean; |
| 35 |
procedure clearLabels; |
| 36 |
function findVoice(w: string): voice_index0; |
| 37 |
procedure selectVoices(line: string); |
| 38 |
procedure resetInfo(voice: voice_index; var buf: string); |
| 39 |
procedure setVocal(voice: voice_index; voc: boolean); |
| 40 |
function isVocal(voice: voice_index): boolean; |
| 41 |
procedure setStavePos(voice: voice_index; stave, pos: stave_index); |
| 42 |
function voiceStave(voice: voice_index): stave_index; |
| 43 |
function voicePos(voice: voice_index): stave_index; |
| 44 |
function aloneOnStave(stave: stave_index): boolean; |
| 45 |
function companion(voice: voice_index): voice_index; |
| 46 |
procedure appendNote(voice: voice_index; nscan: music_word); |
| 47 |
procedure appendToLine(voice: voice_index; note: string); |
| 48 |
procedure markBar(voice: voice_index); |
| 49 |
function numberOfBars(voice: voice_index): integer; |
| 50 |
procedure barForward(voice: voice_index; nbars: integer); |
| 51 |
procedure regroup(voice: voice_index); |
| 52 |
|
| 53 |
function beatsPerLine: integer; |
| 54 |
procedure setExtraLength(voice: voice_index; ext: integer); |
| 55 |
function ExtraLength(voice: voice_index): integer; |
| 56 |
function musicLineNo(voice: voice_index): paragraph_index0; |
| 57 |
procedure setMusicLineNo(voice: voice_index; lno: paragraph_index); |
| 58 |
function chordLineNo(voice: voice_index): paragraph_index0; |
| 59 |
procedure setChordLineNo(voice: voice_index; lno: paragraph_index); |
| 60 |
procedure skipChordBar(voice: voice_index); |
| 61 |
|
| 62 |
procedure describeVoice(voice: voice_index; describe_lyr: string); |
| 63 |
function maybeMusicLine(l: string): boolean; |
| 64 |
function musicLine(voice: voice_index): string; |
| 65 |
|
| 66 |
implementation uses strings, notes, control, utility; |
| 67 |
|
| 68 |
const |
| 69 |
|
| 70 |
name: array[music_word] of string[9] = ( |
| 71 |
'?', 'note', 'znote', 'lyricsTag', |
| 72 |
'(', ')', ')(', '[', ']', |
| 73 |
'_','PMX<', 'macro', 'endMacro', 'meter', 'ornament', 'rest', '//', |
| 74 |
'BAR', 'TeX', '@', 'firstonly', 'ERROR'); |
| 75 |
|
| 76 |
type |
| 77 |
word_scan = array[1..max_words] of music_word; |
| 78 |
|
| 79 |
line_info = record |
| 80 |
here, nword: word_index0; |
| 81 |
nbar: bar_index0; |
| 82 |
voice_pos, voice_stave, mus, chord: paragraph_index0; |
| 83 |
extra: integer; |
| 84 |
vocal: boolean; |
| 85 |
bar_bound: array[bar_index0] of word_index0; |
| 86 |
word_bound, orig_word_bound: array[word_index0] of integer; |
| 87 |
scan: word_scan; |
| 88 |
end; |
| 89 |
|
| 90 |
var info: array[voice_index] of line_info; |
| 91 |
|
| 92 |
{ ------------------------------------------------------------- } |
| 93 |
|
| 94 |
function beatsPerLine: integer; |
| 95 |
var voice: voice_index; |
| 96 |
begin for voice:=1 to nvoices do with info[voice] do |
| 97 |
if (nbar>0) or (extra>0) then |
| 98 |
begin |
| 99 |
if extra mod one_beat > 0 |
| 100 |
then error3(voice,'Line length not an integer number of beats'); |
| 101 |
beatsPerLine := nbar*meternum + extra div one_beat; |
| 102 |
end; |
| 103 |
end; |
| 104 |
|
| 105 |
procedure skipChordBar(voice: voice_index); |
| 106 |
begin with info[voice] do |
| 107 |
if chord>0 then if P[chord]=barsym then predelete(P[chord],1); |
| 108 |
end; |
| 109 |
|
| 110 |
function getBar(voice: voice_index; bar: integer): string; |
| 111 |
begin with info[voice] do |
| 112 |
getBar := substr(P[mus],word_bound[bar_bound[bar-1]]+1, |
| 113 |
word_bound[bar_bound[bar]]-word_bound[bar_bound[bar-1]]); |
| 114 |
end; |
| 115 |
|
| 116 |
function musicLine(voice: voice_index): string; |
| 117 |
begin musicLine:=P[musicLineNo(voice)]; end; |
| 118 |
|
| 119 |
function musicLineNo(voice: voice_index): paragraph_index0; |
| 120 |
begin musicLineNo := info[voice].mus; end; |
| 121 |
|
| 122 |
procedure setMusicLineNo(voice: voice_index; lno: paragraph_index); |
| 123 |
begin info[voice].mus := lno; end; |
| 124 |
|
| 125 |
function chordLineNo(voice: voice_index): paragraph_index0; |
| 126 |
begin chordLineNo := info[voice].chord; end; |
| 127 |
|
| 128 |
procedure setChordLineNo(voice: voice_index; lno: paragraph_index); |
| 129 |
begin info[voice].chord := lno; end; |
| 130 |
|
| 131 |
procedure setVocal(voice: voice_index; voc: boolean); |
| 132 |
begin info[voice].vocal:=voc; end; |
| 133 |
|
| 134 |
function isVocal(voice: voice_index): boolean; |
| 135 |
begin isVocal := info[voice].vocal; end; |
| 136 |
|
| 137 |
procedure setStavePos(voice: voice_index; stave, pos: stave_index); |
| 138 |
begin with info[voice] do begin voice_pos:=pos; voice_stave:=stave; end; |
| 139 |
end; |
| 140 |
|
| 141 |
function voiceStave(voice: voice_index): stave_index; |
| 142 |
begin voiceStave:=info[voice].voice_stave; end; |
| 143 |
|
| 144 |
function voicePos(voice: voice_index): stave_index; |
| 145 |
begin voicePos:=info[voice].voice_pos; end; |
| 146 |
|
| 147 |
function companion(voice: voice_index): voice_index; |
| 148 |
var s: integer; |
| 149 |
begin s:=info[voice].voice_stave; |
| 150 |
if number_on_stave[s]=1 then companion:=voice |
| 151 |
else if info[voice].voice_pos=1 then companion:=voice+1 |
| 152 |
else companion:=voice-1; |
| 153 |
end; |
| 154 |
|
| 155 |
procedure regroup(voice: voice_index); |
| 156 |
var i, j, j2: word_index0; |
| 157 |
begin j2 := 0; with info[voice] do |
| 158 |
begin if debugMode then write('Voice ',voice,' has ',nbar,' bars at '); |
| 159 |
if debugMode then for i:=1 to nbar+1 do write(bar_bound[i],' '); |
| 160 |
for i:=1 to nbar do |
| 161 |
begin j2:=bar_bound[i]; j:=j2+1; |
| 162 |
while (j<=here) and (bind_left[scan[j]] or (scan[j]=barword)) do |
| 163 |
begin inc(bar_bound[i]); inc(j) end; |
| 164 |
end; |
| 165 |
if extra>0 then bar_bound[nbar+1]:=here; |
| 166 |
if debugMode then |
| 167 |
begin write(' regrouped to '); |
| 168 |
for i:=1 to nbar+1 do write(bar_bound[i],' ') |
| 169 |
end; |
| 170 |
if debugMode then writeln; |
| 171 |
nword := here |
| 172 |
end; |
| 173 |
end; |
| 174 |
|
| 175 |
procedure resetInfo(voice: voice_index; var buf: string); |
| 176 |
begin with info[voice] do |
| 177 |
begin buf := P[mus]; P[mus]:=''; |
| 178 |
bar_bound[0]:=0; word_bound[0]:=0; orig_word_bound[0]:=0; |
| 179 |
nbar:=0; here := 0; |
| 180 |
end; |
| 181 |
end; |
| 182 |
|
| 183 |
procedure clearLabels; |
| 184 |
var voice: voice_index; |
| 185 |
begin for voice:=1 to nvoices do with info[voice] do |
| 186 |
begin chord:=0; mus:=0; end; |
| 187 |
end; |
| 188 |
|
| 189 |
procedure appendNote(voice: voice_index; nscan: music_word); |
| 190 |
begin with info[voice] do |
| 191 |
begin inc(here); if here>max_words then |
| 192 |
error3(voice,'Words per line limit exceeded'); |
| 193 |
scan[here]:=nscan; end; |
| 194 |
end; |
| 195 |
|
| 196 |
procedure appendToLine(voice: voice_index; note: string); |
| 197 |
begin if note<>'' then with info[voice] do |
| 198 |
begin P[mus]:=P[mus]+note+blank; word_bound[here]:=length(P[mus]); |
| 199 |
orig_word_bound[here]:=nextWordBound(orig_P[mus],note[1], |
| 200 |
orig_word_bound[here-1]); |
| 201 |
end; |
| 202 |
end; |
| 203 |
|
| 204 |
procedure markBar(voice: voice_index); |
| 205 |
begin with info[voice] do |
| 206 |
if nbar=0 then error3(voice,'Empty bar') else bar_bound[nbar]:=here |
| 207 |
end; |
| 208 |
|
| 209 |
function numberOfBars(voice: voice_index): integer; |
| 210 |
begin numberOfBars := info[voice].nbar; end; |
| 211 |
|
| 212 |
procedure barForward(voice: voice_index; nbars: integer); |
| 213 |
begin with info[voice] do |
| 214 |
begin |
| 215 |
if (nbar+nbars<0) then error3(voice, 'Next voice before bar is full'); |
| 216 |
if nbar+nbars>max_bars then error3(voice,'Bars per line limit exceeded'); |
| 217 |
inc(nbar,nbars); |
| 218 |
if nbars>0 then bar_bound[nbar]:=here; |
| 219 |
end; |
| 220 |
end; |
| 221 |
|
| 222 |
procedure setExtraLength(voice: voice_index; ext: integer); |
| 223 |
begin with info[voice] do begin extra:=ext; scan[here+1]:=other; end; |
| 224 |
end; |
| 225 |
|
| 226 |
function ExtraLength(voice: voice_index): integer; |
| 227 |
begin ExtraLength := info[voice].extra; end; |
| 228 |
|
| 229 |
function findVoice(w: string): voice_index0; |
| 230 |
var i: integer; |
| 231 |
begin curtail(w,':'); findVoice:=0; |
| 232 |
for i:=1 to nvoices do |
| 233 |
if w=voice_label[i] then begin findVoice:=i; exit end; |
| 234 |
getNum(w,i); if i=0 then exit; |
| 235 |
if (i>0) and (i<=nvoices) then findVoice:=i |
| 236 |
else error('Numeric label outside range 1..nvoices',print); |
| 237 |
end; |
| 238 |
|
| 239 |
procedure info3(voice: voice_index); |
| 240 |
var p: integer; |
| 241 |
begin with info[voice] do |
| 242 |
begin |
| 243 |
writeln ('In voice "', voice_label[voice], '" near word ', here, ':'); |
| 244 |
p:=orig_word_bound[here-1]-1; |
| 245 |
if p<0 then p:=0; |
| 246 |
writeln(' ':p,'V') |
| 247 |
end |
| 248 |
end; |
| 249 |
|
| 250 |
procedure error3(voice: voice_index; message: string ); |
| 251 |
begin info3(voice); error(' '+message,print) end; |
| 252 |
|
| 253 |
procedure warning3(voice: voice_index; message: string ); |
| 254 |
begin info3(voice); warning(' '+message,print) end; |
| 255 |
|
| 256 |
function nextMusicWord(voice: voice_index): string; |
| 257 |
begin nextMusicWord := MusicWord(voice,info[voice].here); end; |
| 258 |
|
| 259 |
function MusicWord(voice, n: integer): string; |
| 260 |
begin with info[voice] do if (n>0) and (n<=nword) then |
| 261 |
MusicWord := substr(P[mus],word_bound[n-1]+1, |
| 262 |
word_bound[n]-word_bound[n-1]-1) |
| 263 |
else MusicWord := ''; |
| 264 |
end; |
| 265 |
|
| 266 |
function thisNote(voice: voice_index): music_word; |
| 267 |
begin with info[voice] do thisNote := scan[here-1]; end; |
| 268 |
|
| 269 |
function nextNote(voice: voice_index): music_word; |
| 270 |
begin with info[voice] do nextNote := scan[here]; end; |
| 271 |
|
| 272 |
function endOfBar(voice: voice_index; bar_no: integer): boolean; |
| 273 |
begin with info[voice] do endOfBar := here > bar_bound[bar_no]; end; |
| 274 |
|
| 275 |
procedure gotoBar(voice: voice_index; bar_no: integer); |
| 276 |
begin with info[voice] do here := bar_bound[bar_no-1]+1; end; |
| 277 |
|
| 278 |
function getMusicWord(voice: voice_index): string; |
| 279 |
begin with info[voice] do |
| 280 |
begin line_no := orig_line_no[mus]; |
| 281 |
GetMusicWord := MusicWord(voice,here); inc(here); |
| 282 |
end |
| 283 |
end; |
| 284 |
|
| 285 |
function maybeMusicLine(l: string): boolean; |
| 286 |
var w: string; |
| 287 |
nscan: music_word; |
| 288 |
begin w:=GetNextWord(l,blank,dummy); maybeMusicLine := false; |
| 289 |
if pos1(w[1],'abcdefgr()[]{}CMm')=0 then begin maybeMusicLine := false; exit end; |
| 290 |
if pos1(':',w)=0 then begin maybeMusicLine := true; exit end; |
| 291 |
GetNextMusWord(w,l,nscan); maybeMusicLine := nscan=abcdefg; |
| 292 |
end; |
| 293 |
|
| 294 |
const macro_initialized: boolean=false; |
| 295 |
nmacros = 99; |
| 296 |
var macro_text: array[1..nmacros] of string; |
| 297 |
procedure macroInit; |
| 298 |
var i: integer; |
| 299 |
begin if macro_initialized then exit else macro_initialized:=true; |
| 300 |
for i:=1 to nmacros do macro_text[i]:='' |
| 301 |
end; |
| 302 |
|
| 303 |
function identifyMacro(s: string): integer; |
| 304 |
var k: integer; |
| 305 |
begin predelete(s,2); getNum(s,k); identifyMacro:=k |
| 306 |
end; |
| 307 |
|
| 308 |
procedure GetNextMusWord (var buf, note: string; var nscan: music_word); |
| 309 |
var maybe_error: string; |
| 310 |
|
| 311 |
procedure expandThisMacro; |
| 312 |
var playtag: string[10]; |
| 313 |
playID: integer; |
| 314 |
w: string; |
| 315 |
begin macroInit; if length(note)=1 then |
| 316 |
error('Can''t terminate a macro that has not been started',print); |
| 317 |
playID:=identifyMacro(note); playtag:=toString(playID); |
| 318 |
if (playID<1) or (playID>99) then |
| 319 |
error('Macro ID '+playtag+' is not in range 1..99',print); |
| 320 |
if note[2]='P' then |
| 321 |
begin if macro_text[playID]='' then |
| 322 |
warning('Macro '+playtag+ |
| 323 |
' inserts empty text: did you define it before use?',print); |
| 324 |
if length(macro_text[playID])+length(buf)>255 |
| 325 |
then error('Expansion of macro '+playtag+' causes buffer overflow',print) |
| 326 |
else begin if debugMode then |
| 327 |
begin writeln('Inserting macro '+playtag+' text "' |
| 328 |
+macro_text[playID]+'"'); |
| 329 |
writeln('Buffer before insert: ',buf) |
| 330 |
end; |
| 331 |
buf:=macro_text[playID]+buf; exit |
| 332 |
end |
| 333 |
end; |
| 334 |
if pos1(note[2],'SR')=0 then error('Second character '+note[2]+ |
| 335 |
' of macro word should be in "PRS"',print); |
| 336 |
macro_text[playID]:=''; |
| 337 |
repeat w:=getNextWord(buf,blank,dummy); |
| 338 |
if w='' then error('Macro definition '+playtag+ |
| 339 |
' should be terminated on the same input line',print); |
| 340 |
if w='M' then begin |
| 341 |
if debugMode then writeln('Macro '+playtag+' is: '+macro_text[playID]); |
| 342 |
break |
| 343 |
end; |
| 344 |
if (w[1]='M') and (length(w)>1) then |
| 345 |
if (w[2]<>'P') or (identifyMacro(w)=playID) then |
| 346 |
error(w+' not allowed inside macro definition '+playtag,print); |
| 347 |
macro_text[playID]:=macro_text[playID]+w+' ' |
| 348 |
until false; |
| 349 |
if note[2]='R' then begin note[2]:='P'; buf:=note+' '+buf end |
| 350 |
end; |
| 351 |
|
| 352 |
begin note:=GetNextWord(buf,blank,dummy); |
| 353 |
if note='' then exit; |
| 354 |
if (note[1]='M') and expandMacro then |
| 355 |
begin expandThisMacro; GetNextMusWord(buf,note,nscan); exit end; |
| 356 |
if note[1]='\' then |
| 357 |
begin maybe_error:=''; |
| 358 |
if note[length(note)]<>'\' then maybe_error:=note; |
| 359 |
while (buf<>'') and (note[length(note)]<>'\') do |
| 360 |
note:=note+' '+GetNextWord(buf,blank,dummy); |
| 361 |
if note[length(note)]<>'\' then |
| 362 |
error('Unterminated TeX literal',print); |
| 363 |
nscan:=texword; |
| 364 |
if maybe_error<>'' then |
| 365 |
warning('Possible unterminated TeX literal: '+maybe_error,print); |
| 366 |
exit; |
| 367 |
end; |
| 368 |
if solfaNoteNames then |
| 369 |
begin translateSolfa(note[1]); if note[1]='"' then predelete(note,1); |
| 370 |
end; |
| 371 |
case note[1] of |
| 372 |
'_': begin nscan:=pmxprefix;delete1(note,1); end; |
| 373 |
'a'..'g': nscan:=abcdefg; |
| 374 |
'z': nscan:=zword; |
| 375 |
'(': nscan:=lparen; |
| 376 |
'{': if note[length(note)]='}' then nscan:=lyrtag else nscan:=lparen; |
| 377 |
')': if pos1('(',note)=0 then nscan:=rparen else nscan:=rlparen; |
| 378 |
'}': if pos1('{',note)=0 then nscan:=rparen else nscan:=rlparen; |
| 379 |
'[': nscan:=lbrac; |
| 380 |
']': if note='][' then nscan:=other else nscan:=rbrac; |
| 381 |
'@': nscan:=atword; |
| 382 |
'm': nscan:=mword; |
| 383 |
'r': nscan:=rword; |
| 384 |
'#','-','n','x','?','s','t','D': nscan:=pmxl; |
| 385 |
'M': if note='M' then nscan:=endMacro else nscan:=macro; |
| 386 |
'G': if pos1('A',note)>0 then nscan:=pmxl else nscan:=other; |
| 387 |
'1'..'9': if pos1('/',note)>0 then nscan:=mword else nscan:=pmxl; |
| 388 |
'o': nscan:=oword; |
| 389 |
'A', 'V': nscan:=FirstOnly; |
| 390 |
'/': if note='//' then nscan:=nextvoice |
| 391 |
else if note='/' then |
| 392 |
begin note:='//'; warning('/ changed to //',print); nscan:=nextvoice; |
| 393 |
end |
| 394 |
else begin error('Word starts with /: do you mean \?',print); |
| 395 |
nscan:=err; |
| 396 |
end; |
| 397 |
else if pos1('|',note)>0 then nscan:=barword else |
| 398 |
nscan:=other; |
| 399 |
end; |
| 400 |
end; |
| 401 |
|
| 402 |
function upper(voice: voice_index): boolean; |
| 403 |
begin with info[voice] do |
| 404 |
if (voice_pos=1) and (voice<nvoices) then |
| 405 |
upper:=(voice_stave=info[voice+1].voice_stave) |
| 406 |
else upper:=false; |
| 407 |
end; |
| 408 |
|
| 409 |
procedure describeVoice(voice: voice_index; describe_lyr: string); |
| 410 |
var bar, w: integer; |
| 411 |
begin with info[voice] do |
| 412 |
begin write('Voice `',voice_label[voice],''' is on line ',mus); |
| 413 |
if vocal then |
| 414 |
begin write(', is vocal'); writeln(describe_lyr); end |
| 415 |
else |
| 416 |
begin if chord>0 then write(' and has chords on line ',chord); |
| 417 |
writeln; |
| 418 |
end; |
| 419 |
if debugMode then with info[voice] do |
| 420 |
begin write('Line has ', nbar, ' bars'); |
| 421 |
if extra>0 then writeln(' + ', extra, '/64 notes') else writeln; |
| 422 |
write('Bars:'); |
| 423 |
for bar:=1 to nbar do write(' ',getBar(voice,bar),' '); |
| 424 |
writeln; write('Word types:'); |
| 425 |
for w:=1 to nword do write(' ',name[scan[w]]); writeln; |
| 426 |
end; |
| 427 |
end; |
| 428 |
end; |
| 429 |
|
| 430 |
function aloneOnStave(stave: stave_index): boolean; |
| 431 |
var v: voice_index; |
| 432 |
begin |
| 433 |
if number_on_stave[stave]<>2 then aloneOnStave:=true else |
| 434 |
begin v:=first_on_stave[stave]; |
| 435 |
aloneOnStave:= (info[v].mus=0) or (info[v+1].mus=0) |
| 436 |
end |
| 437 |
end; |
| 438 |
|
| 439 |
procedure selectVoices(line: string); |
| 440 |
var i, k: integer; |
| 441 |
word: string; |
| 442 |
begin |
| 443 |
for k:=1 to nvoices do selected[k]:=false; |
| 444 |
write('Voice change to: ',line,' = '); |
| 445 |
i:=0; |
| 446 |
while i<nvoices do |
| 447 |
begin word:=GetNextWord(line,blank,dummy); |
| 448 |
if word='' then begin writeln; exit; end; |
| 449 |
inc(i); write(word,' '); |
| 450 |
k:=findVoice(word); |
| 451 |
if k=0 then error('This voice is not in the style',print); |
| 452 |
selected[k]:=true; |
| 453 |
end; |
| 454 |
writeln; |
| 455 |
end; |
| 456 |
|
| 457 |
end. |