| 1 |
unit status; |
| 2 |
{ Keep track of duration, octave, slur and beam status. } |
| 3 |
|
| 4 |
interface |
| 5 |
|
| 6 |
procedure initStatus; |
| 7 |
procedure saveStatus(voice: integer); |
| 8 |
|
| 9 |
procedure resetDuration (voice: integer; dur: char); |
| 10 |
function duration(voice: integer): char; |
| 11 |
|
| 12 |
function slurLevel(voice: integer): integer; |
| 13 |
function beamLevel(voice: integer): integer; |
| 14 |
function noBeamMelisma(voice: integer): boolean; |
| 15 |
function noSlurMelisma(voice, history: integer): boolean; |
| 16 |
function afterSlur(voice: integer): integer; |
| 17 |
procedure setUnbeamed(voice: integer); |
| 18 |
procedure setUnslurred(voice: integer); |
| 19 |
procedure beginBeam(voice: integer; var note: string); |
| 20 |
procedure endBeam(voice: integer); |
| 21 |
procedure beginSlur(voice: integer; var note: string); |
| 22 |
procedure endSlur(voice: integer; var note: string); |
| 23 |
procedure activateBeamsAndSlurs(voice: integer); |
| 24 |
|
| 25 |
procedure setOctave(voice: integer); |
| 26 |
procedure resetOctave(voice: integer); |
| 27 |
function octave(voice: integer): char; |
| 28 |
procedure newOctave(voice: integer; dir: char); |
| 29 |
procedure initOctaves(octaves: string); |
| 30 |
|
| 31 |
procedure renewPitch (voice: integer; var note: string); |
| 32 |
function chordPitch(voice: integer): integer; |
| 33 |
procedure renewChordPitch (voice: integer; note: string); |
| 34 |
procedure defineRange(voice: integer; range: string); |
| 35 |
procedure checkRange(voice: integer; note: string); |
| 36 |
procedure rememberDurations; |
| 37 |
procedure restoreDurations; |
| 38 |
procedure chordTie(voice: integer; var lab: char); |
| 39 |
type int5 = array[1..5] of integer; |
| 40 |
procedure getChordTies(voice: integer; var pitches: int5; var labels: string); |
| 41 |
|
| 42 |
implementation uses globals, strings, mtxline, control, utility, notes; |
| 43 |
|
| 44 |
const |
| 45 |
init_oct: string = ''; |
| 46 |
lowest_pitch = -9; |
| 47 |
highest_pitch = 61; |
| 48 |
range_name: array[-6..49] of string = ( |
| 49 |
'0c','0d','0e','0f','0g','0a','0b', |
| 50 |
'1c','1d','1e','1f','1g','1a','1b', |
| 51 |
'2c','2d','2e','2f','2g','2a','2b', |
| 52 |
'3c','3d','3e','3f','3g','3a','3b', |
| 53 |
'4c','4d','4e','4f','4g','4a','4b', |
| 54 |
'5c','5d','5e','5f','5g','5a','5b', |
| 55 |
'6c','6d','6e','6f','6g','6a','6b', |
| 56 |
'7c','7d','7e','7f','7g','7a','7b'); |
| 57 |
|
| 58 |
type |
| 59 |
line_status = |
| 60 |
record |
| 61 |
pitch, chord_pitch, octave_adjust, beam_level, slur_level, after_slur: integer; |
| 62 |
octave, lastnote, chord_lastnote, duration, slurID, tieID: char; |
| 63 |
beamnext, beamed, slurnext, slurred, no_beam_melisma: boolean; |
| 64 |
no_slur_melisma: array[1..12] of boolean; |
| 65 |
chord_tie_pitch: int5; |
| 66 |
chord_tie_label: string[5]; |
| 67 |
end; |
| 68 |
|
| 69 |
var current: array[voice_index] of line_status; |
| 70 |
lastdur: array[voice_index] of char; |
| 71 |
voice_range, range_low, range_high: array[voice_index] of string; |
| 72 |
|
| 73 |
{ Range limits are user-specified as e.g. 3c. To make comparisons |
| 74 |
meaningful, a and b (which come after g) are translated to h and i. } |
| 75 |
|
| 76 |
procedure checkRange(voice: integer; note: string); |
| 77 |
var orig_note: string; |
| 78 |
begin |
| 79 |
if voice_range[voice]='' then exit; |
| 80 |
orig_note := note; |
| 81 |
if length(note)>2 { assume usual PMX form with octave } then |
| 82 |
note := note[3]+note[1]; |
| 83 |
if note[2]='a' then note[2]:='h'; |
| 84 |
if note[2]='b' then note[2]:='i'; |
| 85 |
if (note<range_low[voice]) or (note>range_high[voice]) then error3(voice, |
| 86 |
orig_note+' is out of range, specified as '+voice_range[voice]) |
| 87 |
end; |
| 88 |
|
| 89 |
procedure defineRange(voice: integer; range: string); |
| 90 |
begin |
| 91 |
voice_range[voice] := range; |
| 92 |
if range='' then exit; |
| 93 |
if not ( |
| 94 |
('0'<=range[1]) and (range[1]<='7') |
| 95 |
and ('a'<=range[2]) and (range[2]<='g') |
| 96 |
and (range[3]='-') |
| 97 |
and ('0'<=range[4]) and (range[4]<='7') |
| 98 |
and ('a'<=range[5]) and (range[5]<='g') |
| 99 |
) then error('Badly formatted range "'+range+ |
| 100 |
'" for voice '+voice_label[voice]+', must be e.g. 3c-4a',print); |
| 101 |
if range[2]='a' then range[2]:='h'; |
| 102 |
if range[2]='b' then range[2]:='i'; |
| 103 |
if range[5]='a' then range[5]:='h'; |
| 104 |
if range[5]='b' then range[5]:='i'; |
| 105 |
range_low[voice]:=substr(range,1,2); |
| 106 |
range_high[voice]:=substr(range,4,2); |
| 107 |
end; |
| 108 |
|
| 109 |
procedure chordTie(voice: integer; var lab: char); |
| 110 |
var n: integer; |
| 111 |
begin with current[voice] do |
| 112 |
begin n:=length(chord_tie_label); |
| 113 |
if n=5 then error3(voice,'Only five slur ties allowed per voice'); |
| 114 |
if n=0 then lab:='T' else lab:=chord_tie_label[n]; |
| 115 |
inc(lab); chord_tie_label:=chord_tie_label+lab; |
| 116 |
inc(n); chord_tie_pitch[n]:=chord_pitch |
| 117 |
end |
| 118 |
end; |
| 119 |
|
| 120 |
procedure getChordTies(voice: integer; var pitches: int5; var labels: string); |
| 121 |
begin with current[voice] do |
| 122 |
begin pitches:=chord_tie_pitch; labels:=chord_tie_label; chord_tie_label:='' end |
| 123 |
end; |
| 124 |
|
| 125 |
procedure rememberDurations; |
| 126 |
var v: voice_index; |
| 127 |
begin for v:=1 to nvoices do lastdur[v]:=duration(v) end; |
| 128 |
|
| 129 |
procedure restoreDurations; |
| 130 |
var v: voice_index; |
| 131 |
begin for v:=1 to nvoices do resetDuration(v,lastdur[v]) end; |
| 132 |
|
| 133 |
function duration(voice: integer): char; |
| 134 |
begin duration := current[voice].duration; end; |
| 135 |
|
| 136 |
procedure resetDuration(voice: integer; dur: char); |
| 137 |
begin if pos1(dur,durations)=0 then |
| 138 |
begin write('Trying to set duration to ',dur,'; '); |
| 139 |
error3(voice,'M-Tx system error: resetDuration'); |
| 140 |
end; |
| 141 |
current[voice].duration := dur |
| 142 |
end; |
| 143 |
|
| 144 |
|
| 145 |
procedure activateBeamsAndSlurs(voice: integer); |
| 146 |
begin with current[voice] do |
| 147 |
begin |
| 148 |
if beamnext then begin beamed:=true; beamnext:=false; end; |
| 149 |
if slurnext then begin slurred:=true; slurnext:=false; end; |
| 150 |
if slurred then inc(after_slur); |
| 151 |
end |
| 152 |
end; |
| 153 |
|
| 154 |
procedure saveStatus(voice: integer); |
| 155 |
begin with current[voice] do |
| 156 |
begin chord_pitch := pitch; chord_lastnote := lastnote; end; |
| 157 |
end; |
| 158 |
|
| 159 |
function noBeamMelisma(voice: integer): boolean; |
| 160 |
begin noBeamMelisma := current[voice].no_beam_melisma; end; |
| 161 |
|
| 162 |
function afterSlur(voice: integer): integer; |
| 163 |
begin with current[voice] do |
| 164 |
begin afterSlur := after_slur; if (after_slur>0) and (slur_level<1) then |
| 165 |
error3(voice,'M-Tx system error: afterSlur and slur_level incompatible)') |
| 166 |
end |
| 167 |
end; |
| 168 |
|
| 169 |
function octave(voice: integer): char; |
| 170 |
begin octave := current[voice].octave; end; |
| 171 |
|
| 172 |
procedure resetOctave(voice: integer); |
| 173 |
begin current[voice].octave := ' '; end; |
| 174 |
|
| 175 |
procedure initOctaves(octaves: string); |
| 176 |
var i: integer; |
| 177 |
begin init_oct:=octaves; |
| 178 |
i:=1; |
| 179 |
while i<=length(init_oct) do |
| 180 |
if init_oct[i]=' ' then delete1(init_oct,i) else inc(i); |
| 181 |
end; |
| 182 |
|
| 183 |
function initOctave(voice_stave: stave_index): char; |
| 184 |
begin |
| 185 |
if voice_stave>length(init_oct) then |
| 186 |
if pos1(clef[voice_stave],'Gt08')>0 |
| 187 |
then initOctave:='4' else initOctave:='3' |
| 188 |
else initOctave:=init_oct[voice_stave]; |
| 189 |
end; |
| 190 |
|
| 191 |
procedure setOctave(voice: integer); |
| 192 |
begin current[voice].octave:=initOctave(voiceStave(voice)); end; |
| 193 |
|
| 194 |
procedure newOctave(voice: integer; dir: char); |
| 195 |
begin with current[voice] do case dir of |
| 196 |
'+': inc(octave); |
| 197 |
'-': dec(octave); |
| 198 |
end; |
| 199 |
end; |
| 200 |
|
| 201 |
function newPitch (voice: integer; note: string; pitch: integer; |
| 202 |
lastnote: char): integer; |
| 203 |
var interval, npitch: integer; |
| 204 |
oct: char; |
| 205 |
begin |
| 206 |
oct:=octaveCode(note); |
| 207 |
if oct='=' then oct:=initOctave(voiceStave(voice)); |
| 208 |
if (oct>='0') and (oct<='9') then |
| 209 |
begin pitch:=7*(ord(oct)-ord('0'))-3; lastnote:='f'; |
| 210 |
removeOctaveCode(oct,note); oct:=octaveCode(note) |
| 211 |
end; |
| 212 |
interval := ord(note[1])-ord(lastnote); |
| 213 |
if interval>3 then dec(interval,7); |
| 214 |
if interval<-3 then inc(interval,7); |
| 215 |
npitch:=pitch+interval; |
| 216 |
while oct<>' ' do |
| 217 |
begin if oct='+' then inc(npitch,7) else if oct='-' then dec(npitch,7); |
| 218 |
removeOctaveCode(oct,note); oct:=octaveCode(note) |
| 219 |
end; |
| 220 |
newPitch:=npitch |
| 221 |
end; |
| 222 |
|
| 223 |
procedure repitch(var note: string; diff: integer); |
| 224 |
procedure delins(var note: string; c1, c2: char; l: integer); |
| 225 |
var i, n: integer; |
| 226 |
begin n:=length(note); i:=pos1(c1,note); if i=0 then i:=n+1; |
| 227 |
while (l>0) and (i<=n) do begin delete1(note,i); dec(n); dec(l); end; |
| 228 |
i:=pos1(c2,note); |
| 229 |
if i=0 then if length(note)<2 then error('M-Tx program error',print) |
| 230 |
else i:=3; |
| 231 |
while l>0 do begin insertchar(c2,note,i); dec(l); end; |
| 232 |
end; |
| 233 |
begin diff:=diff div 7; |
| 234 |
if diff>0 then delins(note, '-','+',diff) |
| 235 |
else delins(note, '+','-',-diff); |
| 236 |
end; |
| 237 |
|
| 238 |
procedure setUnbeamed(voice: integer); |
| 239 |
begin current[voice].beamed:=false end; |
| 240 |
|
| 241 |
procedure setUnslurred(voice: integer); |
| 242 |
begin with current[voice] do |
| 243 |
begin slurred:=false; after_slur:=0; end; |
| 244 |
end; |
| 245 |
|
| 246 |
procedure beginBeam(voice: integer; var note: string); |
| 247 |
begin with current[voice] do |
| 248 |
begin if beamed then |
| 249 |
error3(voice, 'Starting a forced beam while another is open'); |
| 250 |
if beam_level>0 then error3(voice, |
| 251 |
'Starting a forced beam while another is open (beamlevel>0)'); |
| 252 |
inc(beam_level); |
| 253 |
beamnext := true; no_beam_melisma:=startsWith(note,'[['); |
| 254 |
if no_beam_melisma then predelete(note,1); |
| 255 |
end; |
| 256 |
end; |
| 257 |
|
| 258 |
procedure endBeam(voice: integer); |
| 259 |
begin with current[voice] do |
| 260 |
begin if beam_level<1 then error3(voice, 'Closing a beam that was never opened'); |
| 261 |
dec(beam_level) |
| 262 |
end; |
| 263 |
setUnbeamed(voice) |
| 264 |
end; |
| 265 |
|
| 266 |
function slurLevel(voice: integer): integer; |
| 267 |
begin slurLevel := current[voice].slur_level; end; |
| 268 |
|
| 269 |
function beamLevel(voice: integer): integer; |
| 270 |
begin beamLevel := current[voice].beam_level; end; |
| 271 |
|
| 272 |
function noSlurMelisma(voice, history: integer): boolean; |
| 273 |
begin with current[voice] do |
| 274 |
noSlurMelisma := no_slur_melisma[slur_level+history]; |
| 275 |
end; |
| 276 |
|
| 277 |
function slurLabel(voice: integer; note: string): string; |
| 278 |
var sl: char; |
| 279 |
begin if note='' then begin slurLabel:=''; exit end; |
| 280 |
if length(note)<2 then begin slurLabel:=' '; exit end; |
| 281 |
if (note[2]>='0') and (note[2]<='Z') then sl:=note[2] else sl:=' '; |
| 282 |
if (sl>='I') and (sl<='T') then |
| 283 |
warning3(voice,'Slur label in the range I..T may cause conflict'); |
| 284 |
slurLabel:=sl |
| 285 |
end; |
| 286 |
|
| 287 |
procedure labelSlur(voice: integer; var note: string); |
| 288 |
var sl: char; |
| 289 |
begin if note='' then exit; |
| 290 |
with current[voice] do |
| 291 |
begin |
| 292 |
if note[1]=')' then inc(slurID,2) else if note[1]='}' then inc(tieID,2); |
| 293 |
{* CMO 0.60a: replace assigning tieID to sl by space charater |
| 294 |
if (note[1]='(') or (note[1]=')') then sl:=slurID else sl:=tieID; } |
| 295 |
if (note[1]='(') or (note[1]=')') then sl:=slurID else sl:=' '; |
| 296 |
{* CMO 0.60d: omit insertchar in case of tie |
| 297 |
insertchar(sl,note,2); } |
| 298 |
if (note[1]='(') or (note[1]=')') then insertchar(sl,note,2); |
| 299 |
if note[1]='(' then dec(slurID,2) else if note[1]='{' then dec(tieID,2); |
| 300 |
if slurID<'I' then warning3(voice,'Too many nested slurs may cause conflict'); |
| 301 |
if tieID<'I' then warning3(voice,'Too many nested ties may cause conflict') |
| 302 |
end |
| 303 |
end; |
| 304 |
|
| 305 |
procedure beginSlur(voice: integer; var note: string); |
| 306 |
var posblind: integer; |
| 307 |
begin |
| 308 |
with current[voice] do |
| 309 |
begin |
| 310 |
inc(slur_level); if slur_level>12 then Error3(voice,'Too many open slurs'); |
| 311 |
no_slur_melisma[slur_level] := startsWith(note,'((') or startsWith(note,'{{'); |
| 312 |
if no_slur_melisma[slur_level] then predelete(note,1); |
| 313 |
if slurLabel(voice,note)='0' then delete1(note,2) else |
| 314 |
if slurLabel(voice,note)=' ' then labelSlur(voice,note); |
| 315 |
posblind:=pos1('~',note); if posblind>0 then |
| 316 |
if hideBlindSlurs then note:='' else delete1(note,posblind); |
| 317 |
slurnext := true; |
| 318 |
end; |
| 319 |
end; |
| 320 |
|
| 321 |
procedure endSlur(voice: integer; var note: string); |
| 322 |
var poscontinue, posblind: integer; |
| 323 |
contslur: string; |
| 324 |
begin with current[voice] do |
| 325 |
begin contslur:=''; |
| 326 |
if slur_level<1 then Error3(voice,'Ending a slur that was never started'); |
| 327 |
if note[1]=')' then poscontinue:=pos1('(',note) |
| 328 |
else if note[1]='}' then poscontinue:=pos1('{',note); |
| 329 |
if poscontinue=0 then dec(slur_level) else |
| 330 |
begin dec(poscontinue); contslur:=note; predelete(contslur,poscontinue); |
| 331 |
shorten(note,poscontinue); |
| 332 |
end; |
| 333 |
if slur_level=0 then setUnslurred(voice); |
| 334 |
if slurLabel(voice,note)='0' then delete1(note,2) else |
| 335 |
if slurLabel(voice,note)=' ' then labelSlur(voice,note); |
| 336 |
if slurLabel(voice,contslur)='0' then delete1(contslur,2) else |
| 337 |
if slurLabel(voice,contslur)=' ' then labelSlur(voice,contslur); |
| 338 |
if poscontinue>0 then |
| 339 |
begin if note[1]='}' then note:=note+'t'; note[1]:='s'; |
| 340 |
if contslur[1]='{' then contslur:=contslur+'t'; contslur[1]:='s'; |
| 341 |
end; |
| 342 |
posblind:=pos1('~',note); if posblind>0 then |
| 343 |
if hideBlindSlurs then note:='' else delete1(note,posblind); |
| 344 |
if (note<>'') and (contslur<>'') then note := note + ' ' + contslur; |
| 345 |
end; |
| 346 |
end; |
| 347 |
|
| 348 |
{ This is the routine called by prepmx.pas for every normal and chordal |
| 349 |
note, immediately after checkOctave. It does two things: adjusts pitch |
| 350 |
of notes following a C: chord to what it would have been without that |
| 351 |
chord, and checks for pitch overruns. } |
| 352 |
procedure renewPitch (voice: integer; var note: string); |
| 353 |
var pstat: integer; |
| 354 |
begin with current[voice] do |
| 355 |
begin |
| 356 |
pstat:=newPitch (voice, note, chord_pitch, chord_lastnote); |
| 357 |
pitch := newPitch (voice, note, pitch, lastnote); |
| 358 |
if pitch<>pstat then repitch(note,pitch-pstat); |
| 359 |
checkRange(voice,range_name[pitch]); |
| 360 |
if (pitch<lowest_pitch) and checkPitch then |
| 361 |
begin write('Pitch of note ',note,' following ',lastnote,' reported as ',pitch); |
| 362 |
error3(voice,'Pitch too low') |
| 363 |
end; |
| 364 |
if (pitch>highest_pitch) and checkPitch then |
| 365 |
begin write('Pitch of note ',note,' following ',lastnote,' reported as ',pitch); |
| 366 |
error3(voice,'Pitch too high') |
| 367 |
end; |
| 368 |
lastnote:=note[1]; |
| 369 |
end; |
| 370 |
end; |
| 371 |
|
| 372 |
function chordPitch(voice: integer): integer; |
| 373 |
begin chordPitch:=current[voice].chord_pitch end; |
| 374 |
|
| 375 |
procedure renewChordPitch (voice: integer; note: string); |
| 376 |
begin with current[voice] do |
| 377 |
begin chord_pitch:=newPitch(voice,note,chord_pitch,chord_lastnote); |
| 378 |
if chord_pitch<lowest_pitch then error3(voice,'Pitch in chord too low'); |
| 379 |
if chord_pitch>highest_pitch then error3(voice,'Pitch in chord too high'); |
| 380 |
chord_lastnote:=note[1]; |
| 381 |
end |
| 382 |
end; |
| 383 |
|
| 384 |
procedure initStatus; |
| 385 |
var voice: integer; |
| 386 |
begin for voice:=1 to nvoices do with current[voice] do |
| 387 |
begin duration:=default_duration; |
| 388 |
octave_adjust:=0; slur_level:=0; beam_level:=0; |
| 389 |
beamed:=false; beamnext:=false; |
| 390 |
slurred:=false; slurnext:=false; after_slur:=0; |
| 391 |
octave:=initOctave(voiceStave(voice)); slurID:='S'; tieID:='T'; |
| 392 |
lastnote:='f'; pitch:=7*(ord(octave)-ord('0'))-3; |
| 393 |
chord_tie_label:=''; |
| 394 |
saveStatus(voice); |
| 395 |
end; |
| 396 |
end; |
| 397 |
|
| 398 |
end. |