Debian Codegraph @

utils/m-tx/mtx-src/prepmx.pas

raw
1 program prepmx;
2 uses control, strings, globals, preamble, lyrics, mtx, analyze,
3 mtxline, status, uptext, notes, files, utility;
4
5 { CMO: addition/change by Christian Mondrup }
6
7 {* M-Tx preprocessor to PMX Dirk Laurie }
8 const version = '0.63b';
9 version_date = '<11 April 2018>';
10
11 {* See file "Corrections" for updates }
12
13 {* To do next:
14 {* Current bugs:
15 {* Old bugs:
16 Does not check PMX syntax
17 {* Weaknesses that may later cause bugs:
18 Decisions sometimes made on note, sometimes on scan
19 Can 'here' overrun?
20 Where does one note stop and the next begin?
21 See comment on '!' in uptext
22 }
23 chord_flat = 't';
24 chord_left = 'l';
25 blind = true;
26 visible = false;
27
28 var
29 last_bar: boolean;
30 repeat_sign: string;
31 bar_of_line{, bars_of_rest}: integer;
32
33 { --------------- Bars and rests --------------- }
34
35 procedure writeRepeat(var bar: string);
36 var repcode: string;
37 begin if bar='' then exit;
38 repcode:='';
39 if bar='||' then repcode:='Rd'
40 else if (bar='|]') then repcode:='RD'
41 else if bar='|:' then repcode:='Rl'
42 else if bar=':|:' then repcode:='Rlr'
43 else if bar=':|' then repcode:='Rr'
44 else if last_bar and (bar='|') then repcode:='Rb';
45 if repcode<>'' then putLine(' '+repcode);
46 bar:='';
47 end;
48
49 procedure supplyRests(voice: voice_index);
50 begin
51 if (bar_of_line=1) and pedanticWarnings then
52 begin write('Bar ', bar_no, ' Voice ', voice);
53 warning(' Filling missing voice with rests',not print);
54 end;
55 if pickup>0 then
56 put(rests(pickup,meterdenom,visible),nospace);
57 put(pause+' ',putspace);
58 end;
59
60 { ---------------------------------------------------------------- }
61
62 procedure processLine(voice: voice_index; bar_no: integer);
63 var chords, note, cutnote, pretex: string;
64 par_line: paragraph_index;
65 ngrace, nmulti: integer;
66 no_chords, no_uptext: boolean;
67
68 procedure output(note: string);
69 begin if note<>'' then
70 begin { pretex is saved up until a spacing note or rest comes }
71 if endsWith(note,'\') and (note[2]<>'\') then
72 begin curtail(note,'\'); pretex:=pretex+note; note:=''; exit end;
73 if (pretex<>'') and isNoteOrRest(note) then
74 begin note:=pretex+'\ '+note; pretex:='' end;
75 if (outlen>0) and (note[1]<>' ') then note:=' '+note;
76 put(note,nospace);
77 end;
78 if thisNote(voice)=nextvoice then putLine('');
79 end;
80
81 procedure maybeDotted(var note: string);
82 begin if length(note)<2 then exit;
83 if note[2]='"' then
84 begin note[2]:=note[1]; pretex:=pretex+'\mtxDotted' end
85 end;
86
87 procedure addChords;
88 var w, nt, labels: string;
89 j, mus_line: integer;
90 chord_line: paragraph_index0;
91 arpeggio, tieme: boolean;
92 lab: char;
93 pitches: int5;
94 procedure outChord;
95 var k: integer;
96 begin if nt='' then exit; tieme:=nt[1]='{'; if tieme then predelete(nt,1);
97 if (nt='') or (nt[1]<'a') or (nt[1]>'g') then
98 error('Must have a note name at the start of "'+nt
99 +'", not "'+nt[1]+'"',print);
100 renewChordPitch(voice,nt);
101 chords := chords+' z'+nt; nt:='';
102 for k:=1 to length(labels) do if chordPitch(voice)=pitches[k] then
103 begin chords:=chords+' s'+labels[k]+'t'; labels[k]:=' ' end;
104 if tieme then begin chordTie(voice,lab); chords:=chords+' s'+lab+'t' end
105 end;
106 begin saveStatus(voice);
107 getChordTies(voice, pitches, labels);
108 chords:=''; chord_line:=chordLineNo(voice);
109 if chord_line=0 then no_chords:=true;
110 if no_chords then exit;
111 w := getNextWord(P[chord_line],blank,dummy);
112 if (w=barsym) or (w='') then no_chords:=true;
113 if (w=tilde) or no_chords then exit;
114 mus_line:=line_no; line_no:=orig_line_no[chord_line];
115 arpeggio := w[1]='?'; if arpeggio then
116 begin chords:=' ?'; predelete(w,1); end;
117 j:=1; nt:='';
118 while j<=length(w) do
119 begin
120 if (w[j]='{') or ((nt<>'{') and (w[j]>='a') and (w[j]<='g')) then outChord
121 else if w[j]=chord_flat then w[j]:='f'
122 else if w[j]=chord_left then w[j]:='e';
123 nt:=nt+w[j]; j:=j+1
124 end;
125 if nt<>'' then outChord; if arpeggio then chords:=chords+' ?';
126 note:=note+chords;
127 for j:=1 to length(labels) do if labels[j]<>' ' then
128 error('Tie on chord note started but not finished',print);
129 line_no:=mus_line;
130 end;
131
132 function processOther(note: string): string;
133 begin
134 case thisNote(voice) of
135 other: if note[1]=grace_group then
136 begin if length(note)=1 then ngrace:=1
137 else ngrace := pos1(note[2],digits);
138 if ngrace>0 then dec(ngrace);
139 end;
140 { For a zword, take note of pitch but do not change the contents }
141 { Add spurious duration because repitch expects duration to be present }
142 zword: begin cutnote:=note; predelete(cutnote,1); insertchar('4',cutnote,2);
143 checkOctave(voice,cutnote); renewPitch(voice,cutnote)
144 end;
145 lyrtag: extractLyrtag(voice,note);
146 rbrac: endBeam(voice);
147 rparen, rlparen: endSlur(voice,note);
148 lbrac: beginBeam(voice,note);
149 lparen: begin maybeDotted(note); beginSlur(voice,note); end;
150 mword: error3(voice,'Meter change must be the first word of its bar');
151 atword: lyricsAdjust(voice, note);
152 end;
153 processOther:=note;
154 end;
155
156 procedure lookahead;
157 begin while bind_left[nextNote(voice)] do
158 note:=note+' '+processOther(getMusicWord(voice));
159 end;
160
161 var l: integer;
162 in_group: boolean;
163
164 procedure processUsual;
165 begin
166 begin if hasVerseNumber(voice) then pretex:=pretex+'\mtxVerse';
167 l := pos1(multi_group,note);
168 if l>0 then scan1(note,l+1,nmulti);
169 activateBeamsAndSlurs(voice);
170 in_group:=false;
171 if ngrace>0 then begin in_group:=true; dec(ngrace) end else
172 if nmulti>0 then begin in_group:=true; dec(nmulti); end;
173 checkOctave(voice,note); renewPitch(voice,note);
174 if not in_group then
175 begin resetDuration(voice,durationCode(note)); markDebeamed(voice,note) end;
176 lookahead; getSyllable(voice,pretex);
177 addUptext(voice, no_uptext, pretex);
178 addChords;
179 end;
180 end;
181
182 begin
183 pretex:=''; no_chords:=false; no_uptext := false;
184 par_line:=musicLineNo(voice);
185 nmulti :=0; ngrace := 0; line_no:=orig_line_no[par_line];
186 repeat note:=getMusicWord(voice); if note='' then exit;
187 { if debugMode then writeln(voice,' ',note); }
188 case thisNote(voice) of
189 rword: begin if multi_bar_rest<>'' then
190 begin if uptextOnRests then
191 addUptext(voice, no_uptext, pretex);
192 end
193 else begin
194 l := pos1(multi_group,note);
195 if l>0 then scan1(note,l+1,nmulti);
196 if nmulti>0 then begin in_group:=true; dec(nmulti); end;
197 if uptextOnRests then
198 addUptext(voice, no_uptext, pretex);
199 if not (isPause(note) or in_group) {0.63: allow rests in xtuples}
200 then resetDuration(voice,durationCode(note));
201 end
202 end;
203 abcdefg: processUsual;
204 barword: begin
205 if voice=nvoices then
206 if endOfBar(voice,bar_no) then repeat_sign := note
207 else writeRepeat(note);
208 if note<>barsym then note:='';
209 no_chords:=false;
210 end;
211 FirstOnly: if voice<>nvoices then note:='' else
212 note:=processOther(note);
213 else note:=processOther(note);
214 end;
215 output(note);
216 until endOfBar(voice,bar_no);
217 if not no_chords then skipChordBar(voice);
218 end;
219
220 { ------------------------------------------------------------------- }
221
222 procedure getMeterChange(voice: voice_index; var new_meter: string);
223 var pn1, pn2: integer;
224 w, new_command: string;
225 begin if nextNote(voice)<>mword then exit;
226 w:=getMusicWord(voice);
227 getMeter(w, meternum, meterdenom, pn1, pn2);
228 full_bar := meternum*(64 div meterdenom);
229 { CMO: process denominator value with function PMXmeterdenom }
230 new_command := meterWord(meternum, PMXmeterdenom(meterdenom), pn1, pn2);
231 if (new_meter<>'') and (new_meter<>new_command)
232 then error3(voice,'The same meter change must appear in all voices');
233 new_meter:=new_command;
234 end;
235
236 procedure musicParagraph;
237 var j, nvoice: voice_index0;
238 new_meter, lyrassign: string;
239
240 procedure putPMXlines;
241 var i: paragraph_index;
242 begin for i:=1 to para_len do
243 if startsWith(P[i],double_comment) then
244 begin predelete(P[i],2); putLine(P[i]); P[i]:='%'; end;
245 end;
246
247 procedure processOneBar;
248 var m, cm: paragraph_index0;
249 voice, cvoice: voice_index;
250 ignore_voice, wrote_repeat, alone: boolean;
251 begin
252 if bar_of_line>1 then putLine(comment+'Bar '+toString(bar_no));
253 last_bar := (bar_of_line=nbars) and final_paragraph;
254 if last_bar and (repeat_sign='|') then repeat_sign:='';
255 writeRepeat(repeat_sign); new_meter := '';
256 for voice:=nvoices downto 1 do
257 if musicLineNo(voice)>0 then
258 begin gotoBar(voice,bar_of_line); getMeterChange(voice,new_meter);
259 end;
260 if last_bar and (new_meter='') and (nleft>pickup) and (meternum>0) then
261 new_meter := meterChange(nleft,64,true);
262 if new_meter<>'' then putLine(new_meter);
263 wrote_repeat := false;
264 for voice:=nvoices downto 1 do
265 begin ignore_voice:=not selected[voice]; cvoice:=companion(voice);
266 m:=musicLineNo(voice); cm:=musicLineNo(cvoice);
267 alone:=(voice=cvoice) or ((m>0) and (cm=0))
268 or ((m=0) and (cm=0) and (voice<cvoice)) or not selected[cvoice];
269 if selected[voice] then
270 begin
271 if m>0 then processLine(voice,bar_of_line)
272 else if alone then supplyRests(voice) else ignore_voice:=true;
273 if last_bar and (repeat_sign<>'') and not wrote_repeat then
274 begin writeRepeat(repeat_sign); wrote_repeat := true; end;
275 if not ignore_voice then
276 if alone or (voicePos(voice)=1) then putLine(' /')
277 else putLine(' //');
278 end;
279 end;
280 inc(bar_no); pickup:=0; putLine('');
281 end;
282
283 procedure putMeter(new_meter_word: string);
284 begin if new_meter_word<>old_meter_word then putLine(new_meter_word);
285 old_meter_word := new_meter_word;
286 end;
287
288 procedure processMBR;
289 var s, bars_of_rest: integer;
290 mbr: string;
291 begin
292 mbr := multi_bar_rest;
293 predelete(mbr,2); getNum(mbr,bars_of_rest);
294 bar_no := bar_no + bars_of_rest;
295 for s:=1 to nstaves do
296 begin
297 if pickup>0 then put(rests(pickup,meterdenom,visible),nospace);
298 putLine('rm' + toString(bars_of_rest) + ' /')
299 end;
300 putLine('')
301 end;
302
303 begin
304 paragraphSetup(nvoice);
305 if nvoice=0 then begin nonMusic; exit end
306 else if nvoice>nvoices then
307 begin if nvoice=0 then
308 error('No voices! Did you remember to to supply a Style?',not print);
309 error('Paragraph has '+toString(nvoice)+
310 ' voices but Style allows only '+toString(nvoices),not print); exit;
311 end;
312 if first_paragraph then includeStartString;
313 if pmx_preamble_done and (not final_paragraph or (nvoice>0)) then
314 putLine(comment +' Paragraph ' + toString(paragraph_no) +
315 ' line ' + toString(orig_line_no[1]) + ' bar ' + toString(bar_no));
316 testParagraph; rememberDurations;
317 if beVerbose then describeParagraph;
318 { ---- Knowing the score, we can start setting music ---------------- }
319 if not pmx_preamble_done then
320 begin doPMXpreamble;
321 put( comment + ' Paragraph ' + toString(paragraph_no) + ' line ' +
322 toString(orig_line_no[1]) + ' bar ',putspace);
323 if pickup>0 then putLine('0') else putLine('1');
324 end;
325 putPMXlines;
326 if must_restyle then restyle;
327 if some_vocal and ((nvoice>0) or not final_paragraph) then
328 for j:=1 to ninstr do
329 begin assignLyrics(j,lyrassign);
330 if lyrassign<>'' then putLine('\\'+lyrassign+'\');
331 end;
332 if must_respace then respace;
333 if (meternum=0) then putMeter(meterChange(beatsPerLine,meterdenom,true));
334 if nleft > 0 then inc(nbars);
335 if (nbars=0) and (multi_bar_rest<>'') then
336 processMBR
337 else for bar_of_line:=1 to nbars do
338 processOneBar;
339 restoreDurations;
340 end;
341
342 { ----------------------------------------------------------------------- }
343
344 var no_commands_yet: boolean;
345
346 procedure doMusic;
347 begin first_paragraph:=true; pmx_preamble_done:=false; bar_no:=1;
348 repeat_sign:=''; must_respace:=false; must_restyle:=false;
349 repeat final_paragraph := endOfInfile;
350 orig_P := P;
351 if (para_len>0) and not ignore_input and thisCase then
352 begin if no_commands_yet then
353 begin interpretCommands; printFeatures(false);
354 one_beat := 64 div meterdenom; full_bar := meternum*one_beat;
355 if nvoices>standardPMXvoices then warning('You have '
356 +toString(nvoices)+' voices; standard PMX can only handle '
357 +toString(standardPMXvoices),not print);
358 initMTX; initUptext; initStatus; initLyrics;
359 no_commands_yet:=false
360 end;
361 if startsWithBracedWord(P[1]) then lyricsParagraph else
362 begin
363 musicParagraph; first_paragraph:=false;
364 writeRepeat(repeat_sign);
365 end
366 end;
367 readParagraph(P,orig_line_no,para_len);
368 until para_len=0;
369 end;
370
371 var control_paragraph, no_report_errors: boolean;
372
373 function isControlParagraph (var P: paragraph; para_len: paragraph_index)
374 : boolean;
375 var commands, labels, voices, guff, i: paragraph_index0;
376 w: string;
377 begin isControlParagraph:=true; commands:=0; labels:=0; voices:=0; guff:=0;
378 for i:=1 to para_len do
379 if not (startsWith(P[i],'%')) then
380 begin w:=nextWord(P[i],' ',':');
381 if not endsWith(w,':') then inc(guff)
382 else if (length(w)<3) or (findVoice(w)>0) then inc(voices)
383 else if isCommand(w) then inc(commands)
384 else inc(labels)
385 end;
386 if (voices+guff>commands) then isControlParagraph:=false
387 end;
388
389 procedure topOfPMXfile;
390 begin
391 putLine('---');
392 putLine('\def\mtxversion{'+version+'}');
393 putline('\def\mtxdate{'+version_date+'}');
394 putline('\input mtx');
395 end;
396
397 begin { ---- Main program ------------------------ }
398 this_version := version; this_version_date := version_date;
399 writeln ('==> This is M-Tx ' + version + ' (Music from TeXt) ' +
400 version_date );
401
402 mtxLevel(version); OpenFiles; no_commands_yet:=true; preambleDefaults;
403 no_report_errors:=false;
404 topOfPMXfile;
405 repeat readParagraph(P,orig_line_no,para_len);
406 control_paragraph:=isControlParagraph(P,para_len);
407 if control_paragraph then
408 begin augmentPreamble(no_report_errors);
409 no_report_errors:=true;
410 if para_len=0 then error('No music paragraphs!',not print)
411 end
412 until not control_paragraph;
413 doPreamble; doMusic;
414 if not pmx_preamble_done then error('No music paragraphs!',not print);
415 putline('% Coded by M-Tx');
416 CloseFiles;
417 writeln ('PrePMX done. Now run PMX.'); halt(0);
418 end.
419