| 1 |
unit mtx; |
| 2 |
{ Details of M-Tx -> PMX translation } |
| 3 |
|
| 4 |
interface uses globals; |
| 5 |
|
| 6 |
procedure scanMusic(voice: voice_index; var left_over: integer); |
| 7 |
procedure markDebeamed(voice: voice_index; var note: string); |
| 8 |
procedure checkOctave(voice: voice_index; var note: string); |
| 9 |
function rests (len, meterdenom: integer; blind: boolean): string; |
| 10 |
procedure initMTX; |
| 11 |
|
| 12 |
implementation uses control, mtxline, strings, status, notes; |
| 13 |
|
| 14 |
const flagged = '8136'; |
| 15 |
|
| 16 |
type sticky = array['a'..'z'] of boolean; |
| 17 |
|
| 18 |
var note_attrib, rest_attrib: array[voice_index] of sticky; |
| 19 |
|
| 20 |
function logTwo(denom: integer): integer; |
| 21 |
var l: integer; |
| 22 |
begin l:=0; while denom>1 do begin inc(l); denom:=denom div 2; end; |
| 23 |
logtwo:=l; |
| 24 |
end; |
| 25 |
|
| 26 |
{ RESTS should have a third parameter OFFSET. At present we in effect |
| 27 |
assume OFFSET+LENGTH to be a multiple of METERDENOM. } |
| 28 |
function rests (len, meterdenom: integer; blind: boolean): string; |
| 29 |
var r, bl: string; |
| 30 |
function dotted(n: integer): boolean; |
| 31 |
begin dotted:=false; if n=0 then exit; |
| 32 |
if n<>3*(n div 3) then exit; |
| 33 |
n:=n div 3; while n mod 2 = 0 do n:=n div 2; |
| 34 |
dotted := n=1; |
| 35 |
end; |
| 36 |
begin if blind then bl:='b' else bl:=''; |
| 37 |
if len=0 then rests:='' else |
| 38 |
if len=128*meterdenom then rests:='r9'+bl+' ' else |
| 39 |
if dotted(len) then |
| 40 |
begin r:=rests(len-len div 3,meterdenom,false); |
| 41 |
r[3]:='d'; rests:=r+bl+' '; |
| 42 |
end |
| 43 |
else if len>=64 div meterdenom then |
| 44 |
rests:=rests(len-64 div meterdenom,meterdenom,blind)+'r' |
| 45 |
+durations[whole+logtwo(meterdenom)]+bl+' ' |
| 46 |
else rests:=rests(len,meterdenom*2,blind); |
| 47 |
end; |
| 48 |
|
| 49 |
procedure checkSticky(var note: string; var attrib: sticky); |
| 50 |
var i,l: integer; |
| 51 |
c: char; |
| 52 |
a: string; |
| 53 |
function attribs(note: string): string; |
| 54 |
var a: string; |
| 55 |
i,l: integer; |
| 56 |
n: char; |
| 57 |
begin a:=''; i:=2; l:=length(note); |
| 58 |
while i<=l do |
| 59 |
begin n:=note[i]; |
| 60 |
if n='x' then i:=l else if (n>='a') and (n<='z') then a:=a+n; |
| 61 |
inc(i); |
| 62 |
end; |
| 63 |
attribs:=a; |
| 64 |
end; |
| 65 |
begin if note='' then exit; |
| 66 |
l:=length(note); i:=2; |
| 67 |
while i<=l do |
| 68 |
begin c:=note[i]; |
| 69 |
if (c>='a') and (c<='z') then |
| 70 |
if (i<l) and (note[i+1]=':') then |
| 71 |
begin delete1(note,i+1); dec(l); attrib[c]:=not attrib[c]; end |
| 72 |
else attrib[c]:=false; |
| 73 |
inc(i); |
| 74 |
end; |
| 75 |
a:=attribs(note); |
| 76 |
for c:='z' downto 'a' do |
| 77 |
if attrib[c] and (pos1(c,a)=0) then insertchar(c,note,3); |
| 78 |
end; |
| 79 |
|
| 80 |
{ Routine was patched by Hiroaki Morimoto, revised 2003/08/15. |
| 81 |
Rewritten by DPL 2004/05/16 to be more understandable. |
| 82 |
octaveCode returns octave adjustments in the order |
| 83 |
[absolute or =],[relative] } |
| 84 |
procedure checkOctave(voice: voice_index; var note: string); |
| 85 |
var code: char; |
| 86 |
begin |
| 87 |
code:=octaveCode(note); |
| 88 |
if code='=' then begin setOctave(voice); removeOctaveCode(code,note); end; |
| 89 |
if octave(voice)=blank then exit; |
| 90 |
code:=octaveCode(note); |
| 91 |
if (code>='0') and (code<='9') then begin resetOctave(voice); exit end; |
| 92 |
while (code='+') or (code='-') do |
| 93 |
begin |
| 94 |
newOctave(voice,code); removeOctaveCode(code,note); code:=octaveCode(note) |
| 95 |
end; |
| 96 |
if code<>' ' then error3(voice,'You may have only one absolute octave assignment'); |
| 97 |
insertOctaveCode(octave(voice),note); |
| 98 |
checkRange(voice,note); |
| 99 |
resetOctave(voice) |
| 100 |
end; |
| 101 |
|
| 102 |
procedure markDebeamed(voice: voice_index; var note: string); |
| 103 |
begin |
| 104 |
if isVocal(voice) and (afterSlur(voice)=0) and unbeamVocal |
| 105 |
and (pos1(duration(voice),flagged)>0) then insertchar('a',note,2); |
| 106 |
end; |
| 107 |
|
| 108 |
function barLength (meter: string): integer; |
| 109 |
var n1, n2, pn1, pn2: integer; |
| 110 |
begin |
| 111 |
getMeter(meter,n1,n2,pn1,pn2); barLength := n1*(64 div n2); |
| 112 |
end; |
| 113 |
|
| 114 |
function isMultiBarRest(rest: string): boolean; |
| 115 |
begin isMultibarRest:=false; if length(rest)<3 then exit; |
| 116 |
if rest[2]<>'m' then exit; |
| 117 |
if multi_bar_rest<>'' then error( |
| 118 |
'Only one multibar rest allowed per line',print); |
| 119 |
multi_bar_rest := rest; isMultibarRest:=true; |
| 120 |
end; |
| 121 |
|
| 122 |
{ Double-length in xtuplet detected by a brute search for D anywhere. |
| 123 |
Could be more elegant. } |
| 124 |
|
| 125 |
var macro_length: array[1..20] of integer; |
| 126 |
const macro_ID: array[1..20] of string[2]=('1','2','3','4','5','6','7','8', |
| 127 |
'9','10','11','12','13','14','15','16','17','18','19','20'); |
| 128 |
|
| 129 |
procedure scanMusic(voice: voice_index; var left_over: integer); |
| 130 |
var buf, note, enote, xnote: string; |
| 131 |
has_next, done, doublex, store_macro: boolean; |
| 132 |
bar, old_length, bar_length, count, l, mlen, ngrace, nmulti, |
| 133 |
macroID, playID: integer; |
| 134 |
dur1, lastdur, macro_type: char; |
| 135 |
nscan: music_word; |
| 136 |
|
| 137 |
procedure decmulti; |
| 138 |
begin if doublex then dec(nmulti,2) else dec(nmulti) |
| 139 |
end; |
| 140 |
|
| 141 |
procedure countIt; |
| 142 |
procedure incbar(nl: integer); |
| 143 |
begin inc(bar_length,nl); |
| 144 |
if (bar_length>bar) and (meternum>0) |
| 145 |
then error3(voice,'Bar end occurs in mid-note'); |
| 146 |
end; |
| 147 |
begin |
| 148 |
if ngrace>0 then dec(ngrace) else |
| 149 |
if nmulti>0 then decmulti else |
| 150 |
begin if (count mod 3<>0) and ( |
| 151 |
(note[1]<>rest) and note_attrib[voice,'d'] or |
| 152 |
(note[1]=rest) and rest_attrib[voice,'d']) then |
| 153 |
inc(count,count div 2); |
| 154 |
incbar(count); |
| 155 |
l := pos1(multi_group,note); |
| 156 |
if l>0 then |
| 157 |
begin predelete(note,l); getNum(note,nmulti); decmulti; |
| 158 |
end; |
| 159 |
end |
| 160 |
end; |
| 161 |
|
| 162 |
procedure maybeGroup; |
| 163 |
begin if note[1]=grace_group then |
| 164 |
begin if length(note)=1 then ngrace:=1 |
| 165 |
else ngrace := pos1(note[2],digits); (* bug if ngrace>9 *) |
| 166 |
if ngrace>0 then dec(ngrace); |
| 167 |
end; |
| 168 |
end; |
| 169 |
|
| 170 |
function identifyMacro(s: string): integer; |
| 171 |
var k: integer; |
| 172 |
begin predelete(s,2); for k:=1 to 20 do if s=macro_ID[k] then |
| 173 |
begin identifyMacro:=k; exit end; |
| 174 |
identifyMacro:=0 |
| 175 |
end; |
| 176 |
|
| 177 |
procedure examineMacro; |
| 178 |
begin if not countMacro then exit; |
| 179 |
if debugMode then write(note,': '); if length(note)=1 then |
| 180 |
begin |
| 181 |
mlen:=bar_length-old_length; |
| 182 |
if (macroID<1) or (macroID>20) then error3(voice,'Invalid macro ID'); |
| 183 |
macro_length[macroID]:=mlen; |
| 184 |
if debugMode then |
| 185 |
writeln('Ending macro ',macroID,' of type ',macro_type,', length=', mlen); |
| 186 |
if macro_type='S' then bar_length:=old_length; store_macro:=false |
| 187 |
end |
| 188 |
else begin playID:=identifyMacro(note); |
| 189 |
if (playID<1) or (playID>20) then error3(voice,'Invalid macro ID'); |
| 190 |
if note[2]='P' then |
| 191 |
begin if debugMode then |
| 192 |
writeln('Playing macro ',playID,' of length ', |
| 193 |
macro_length[playID]); |
| 194 |
inc(bar_length,macro_length[playID]) |
| 195 |
end |
| 196 |
else if pos1(note[2],'SR')>0 then |
| 197 |
begin old_length:=bar_length; macro_type:=note[2]; macroID:=playID; |
| 198 |
store_macro:=true; |
| 199 |
if debugMode then writeln('Defining macro ',macroID,' of type ',note[2]) |
| 200 |
end |
| 201 |
end |
| 202 |
end; |
| 203 |
|
| 204 |
begin |
| 205 |
terminators:='d.x'; |
| 206 |
resetInfo(voice, buf); has_next := false; left_over:=0; store_macro:=false; |
| 207 |
bar:=full_bar; bar_length:=0; ngrace:=0; nmulti:=0; |
| 208 |
if meternum=0 then bar:=32000; |
| 209 |
dur1:=duration(voice); lastdur:=dur1; |
| 210 |
done:=false; |
| 211 |
repeat GetNextMusWord(buf,note,nscan); if length(note)=0 then break; count:=0; |
| 212 |
{ if isNoteOrRest(note) and not (isPause(note) or isMultibarRest(note)) |
| 213 |
then note:=toStandard(note); } |
| 214 |
doublex := (pos1('D',note)>0) or (pos1('F',note)>0); |
| 215 |
if nscan=mword then |
| 216 |
begin |
| 217 |
if length(note)=0 then |
| 218 |
Error3(voice,'You may not end a line with a meter change'); |
| 219 |
if bar_length>0 then |
| 220 |
error3(voice,'Meter change only allowed at start of bar') |
| 221 |
else bar:=barLength(note); |
| 222 |
end |
| 223 |
else if nscan=rword then |
| 224 |
if not (isPause(note) or isMultiBarRest(note) |
| 225 |
or (ngrace + nmulti > 0)) then {0.63: allow rests in xtuples} |
| 226 |
begin |
| 227 |
processNote(note,xnote,dur1,lastdur,count); |
| 228 |
checkSticky(note,rest_attrib[voice]); |
| 229 |
end; |
| 230 |
if note<>'' then appendNote(voice,nscan); |
| 231 |
enote := note; |
| 232 |
if (nscan=macro) or (nscan=endMacro) then examineMacro; |
| 233 |
if nscan=abcdefg then |
| 234 |
if (multi_bar_rest='') and (ngrace + nmulti = 0) then |
| 235 |
begin |
| 236 |
processNote(enote,xnote,dur1,lastdur,count); |
| 237 |
if xnote<>'' then |
| 238 |
begin |
| 239 |
checkSticky(enote,note_attrib[voice]); |
| 240 |
appendToLine(voice,enote); |
| 241 |
appendNote(voice,nscan); enote := xnote; |
| 242 |
end; |
| 243 |
checkSticky(enote,note_attrib[voice]); |
| 244 |
end; |
| 245 |
appendToLine(voice,enote); |
| 246 |
if note='' then done:=true; { !!! else word_bound[here]:=length(line); } |
| 247 |
if note=barsym then |
| 248 |
if meternum=0 then |
| 249 |
error3(voice,'You may not use bar lines in barless music') |
| 250 |
else if bar_length=0 then markBar(voice) |
| 251 |
else if (numberOfBars(voice)=0) and (bar_length<bar) then |
| 252 |
begin if has_next then |
| 253 |
has_next:=false {TODO Should check whether pickups are equal} |
| 254 |
else if left_over>0 then error3(voice,'Bar is too short'); |
| 255 |
left_over:=bar_length; bar_length := 0; |
| 256 |
end; |
| 257 |
if nscan=nextvoice then |
| 258 |
begin if bar_length>0 then error3(voice, 'Next voice before bar is full') |
| 259 |
else barForward(voice,-1); has_next := true; |
| 260 |
end |
| 261 |
else if isPause(note) then inc(bar_length,bar) |
| 262 |
else if multi_bar_rest<>'' then {do nothing} |
| 263 |
else if not done and isNoteOrRest(note) then countIt |
| 264 |
else maybeGroup; |
| 265 |
dur1:=lastdur; |
| 266 |
if (bar_length>=bar) and (ngrace+nmulti=0) and not store_macro then |
| 267 |
begin if debugMode then writeln(voice,' ',bar_length); |
| 268 |
barForward(voice,bar_length div bar); bar_length:=bar_length mod bar |
| 269 |
end; |
| 270 |
until done; |
| 271 |
setExtraLength(voice,bar_length); |
| 272 |
resetDuration(voice,dur1); |
| 273 |
regroup(voice) |
| 274 |
end; |
| 275 |
|
| 276 |
procedure initMTX; |
| 277 |
var i: voice_index; j: char; |
| 278 |
begin for i:=1 to maxvoices do for j:='a' to 'z' do |
| 279 |
note_attrib[i,j] := false; |
| 280 |
rest_attrib := note_attrib; |
| 281 |
end; |
| 282 |
|
| 283 |
end. |