| 1 |
unit notes; |
| 2 |
{$V-} |
| 3 |
|
| 4 |
interface uses control; |
| 5 |
|
| 6 |
const count64: array['0'..'9'] of integer = |
| 7 |
( 64, 4, 32, 2, 16, 0, 1, 0, 8, 128 ); |
| 8 |
|
| 9 |
procedure processNote(var note, xnote: string; dur1: char; var dur: char; |
| 10 |
var count: integer); |
| 11 |
function durationCode (note: string): char; |
| 12 |
function octaveCode (note: string): char; |
| 13 |
procedure removeOctaveCode(code: char; var note: string); |
| 14 |
procedure insertOctaveCode(code: char; var note: string); |
| 15 |
procedure translateSolfa(var nt: char); |
| 16 |
|
| 17 |
implementation uses strings, globals; |
| 18 |
|
| 19 |
type |
| 20 |
|
| 21 |
parsedNote = record |
| 22 |
name: char; |
| 23 |
duration: string[1]; |
| 24 |
octave: string[8]; |
| 25 |
accidental, whatever, dotgroup, xtuplet: string[16]; |
| 26 |
shortcut: string[32]; |
| 27 |
end; |
| 28 |
|
| 29 |
procedure printNote(n: parsedNote); |
| 30 |
begin with n do writeln(name,'|',duration,'|',octave,'|',accidental,'|', |
| 31 |
whatever,'|',dotgroup,'|',xtuplet,'|',shortcut) |
| 32 |
end; |
| 33 |
|
| 34 |
{ If rearrangeNote is TRUE, translate original note to the following form: |
| 35 |
1. Note name. |
| 36 |
2. Duration. |
| 37 |
3. Octave adjustments. |
| 38 |
4. Everything except the other six items. |
| 39 |
5. Accidental with adjustments (rest: height adjustment) |
| 40 |
6. Dot with adjustments. |
| 41 |
7. Xtuplet group. |
| 42 |
} |
| 43 |
|
| 44 |
procedure translateSolfa(var nt: char); |
| 45 |
var k: integer; |
| 46 |
begin if solfaNoteNames then |
| 47 |
begin k:=pos1(nt,solfa_names); if k>0 then nt:=has_duration[k] |
| 48 |
end |
| 49 |
end; |
| 50 |
|
| 51 |
function durationCode (note: string): char; |
| 52 |
var code: char; |
| 53 |
begin durationCode:=unspecified; if length(note)>1 then |
| 54 |
begin code:=note[2]; if pos1(code,durations)>0 then durationCode:=code |
| 55 |
end |
| 56 |
end; |
| 57 |
|
| 58 |
function half ( dur: char ) : char; |
| 59 |
var k: integer; |
| 60 |
begin k:= pos1 (dur, durations ); half := dur; |
| 61 |
if k=0 then error ('Invalid duration '+dur,print) |
| 62 |
else if k>ndurs then error (dur+' is too short to halve',print) |
| 63 |
else half := durations[k+1]; |
| 64 |
end; |
| 65 |
|
| 66 |
procedure addDuration ( var note: string; dur: char); |
| 67 |
begin if insertDuration then insertchar(dur,note,2); end; |
| 68 |
|
| 69 |
{ Extract procedures. All of these remove part of "note" (sometimes |
| 70 |
the part is empty) and put it somewhere else. The part may be anywhere |
| 71 |
in "note", except when otherwise specified.} |
| 72 |
|
| 73 |
{ Unconditionally extracts the first character. } |
| 74 |
|
| 75 |
procedure extractFirst(var note: string; var first: char); |
| 76 |
begin first:=note[1]; predelete(note,1) |
| 77 |
end; |
| 78 |
|
| 79 |
{ Extracts at most one of the characters in "hits". } |
| 80 |
|
| 81 |
procedure extractOneOf(var note: string; hits: string; var hit: string); |
| 82 |
var i, l: integer; |
| 83 |
begin l:=length(note); hit:=''; |
| 84 |
for i:=1 to l do if pos1(note[i],hits)>0 then |
| 85 |
begin hit:=note[i]; delete1(note,i); exit; |
| 86 |
end; |
| 87 |
end; |
| 88 |
|
| 89 |
{ Extracts contiguous characters in "hits" until no more are found. |
| 90 |
There may be more later. } |
| 91 |
|
| 92 |
procedure extractContiguous(var note: string; hits: string; var hit: string); |
| 93 |
var i, l, len: integer; |
| 94 |
begin l:=length(note); len:=l; hit:=''; |
| 95 |
for i:=1 to l do if pos1(note[i],hits)>0 then |
| 96 |
begin |
| 97 |
repeat if pos1(note[i],hits)=0 then exit; |
| 98 |
hit:=hit+note[i]; delete1(note,i); dec(len) |
| 99 |
until len<i; |
| 100 |
exit; |
| 101 |
end; |
| 102 |
end; |
| 103 |
|
| 104 |
{ Extracts the specified character and everything after it. } |
| 105 |
|
| 106 |
procedure extractAfter(var note: string; delim: char; var tail: string); |
| 107 |
var newlen: integer; |
| 108 |
begin newlen:=pos1(delim,note); tail:=''; if newlen=0 then exit; |
| 109 |
dec(newlen); tail:=note; predelete(tail,newlen); note[0]:=char(newlen); |
| 110 |
end; |
| 111 |
|
| 112 |
{ Extracts the dot shortcut part of a note: comma shortcut is no problem |
| 113 |
because a comma cannot be part of a number. } |
| 114 |
|
| 115 |
procedure extractDotShortcut(var note: string; var tail: string); |
| 116 |
var names, tail2: string; |
| 117 |
l, lt: integer; |
| 118 |
ch: char; |
| 119 |
begin extractAfter(note,'.',tail); l:=1; lt:=length(tail); |
| 120 |
if (l<lt) and (tail[2]='.') then l:=2; |
| 121 |
if solfaNoteNames then names:=solfa_names else names:=has_duration; |
| 122 |
if (l<lt) and (pos1(tail[l+1],names)>0) then |
| 123 |
begin translateSolfa(tail[l+1]); exit end; |
| 124 |
if l=2 then error('".." followed by non-note',print); |
| 125 |
if l>=lt then begin note:=note+tail; tail:=''; exit end; |
| 126 |
ch:=tail[1]; predelete(tail,1); |
| 127 |
extractDotShortcut(tail,tail2); note:=note+ch+tail; tail:=tail2; |
| 128 |
end; |
| 129 |
|
| 130 |
{ Extracts a signed number. } |
| 131 |
|
| 132 |
procedure extractSignedNumber(var note, number: string); |
| 133 |
var k: integer; |
| 134 |
note0: string; |
| 135 |
begin k:=pos1('+',note); if k=0 then k:=pos1('-',note); |
| 136 |
number:=''; if k=0 then exit; |
| 137 |
note0:=note; |
| 138 |
repeat number:=number+note[k]; delete1(note,k) |
| 139 |
until (k>length(note)) or (note[k]<>'0') and (pos1(note[k],digits)=0); |
| 140 |
if length(number)=1 then begin note:=note0; number:='' end |
| 141 |
end; |
| 142 |
|
| 143 |
{ Extracts a symbol followed by optional +- or <> shift indicators } |
| 144 |
|
| 145 |
procedure extractGroup(var note: string; delim: char; var group: string); |
| 146 |
var gl, k, k0: integer; |
| 147 |
probe, nonumber: boolean; |
| 148 |
tail: string; |
| 149 |
procedure tryMore; |
| 150 |
begin while (k<=gl) and (group[k]=group[1]) do inc(k) end; |
| 151 |
procedure try(s: string); |
| 152 |
begin probe:=(k<gl) and (pos1(group[k],s)>0); if probe then inc(k) |
| 153 |
end; |
| 154 |
procedure tryNumber; |
| 155 |
var dot: boolean; |
| 156 |
begin nonumber:=true; dot:=false; |
| 157 |
while (k<=gl) and (pos1(group[k],digitsdot)>0) do |
| 158 |
begin inc(k); if group[k]='.' then |
| 159 |
if dot then error('Extra dot in number',print) else dot:=true |
| 160 |
else nonumber:=false |
| 161 |
end |
| 162 |
end; |
| 163 |
begin extractAfter(note,delim,group); if group='' then exit; |
| 164 |
gl:=length(group); k:=2; |
| 165 |
if (gl>1) and (group[2]=':') then k:=3 else |
| 166 |
begin tryMore; |
| 167 |
k0:=k; try('+-<>'); if probe then tryNumber; if nonumber then k:=k0; |
| 168 |
k0:=k; try('+-<>'); if probe then tryNumber; if nonumber then k:=k0; |
| 169 |
end; |
| 170 |
tail:=group; dec(k); group[0]:=char(k); predelete(tail,k); |
| 171 |
note:=note+tail |
| 172 |
end; |
| 173 |
|
| 174 |
procedure parseNote(note: string; var pnote: parsedNote); |
| 175 |
var onlymidi: string; |
| 176 |
begin with pnote do |
| 177 |
begin |
| 178 |
shortcut:=''; xtuplet:=''; accidental:=''; dotgroup:=''; |
| 179 |
duration:=''; octave:=''; onlymidi:=''; |
| 180 |
extractFirst(note,name); |
| 181 |
extractAfter(note,'x',xtuplet); |
| 182 |
extractAfter(note,',',shortcut); |
| 183 |
if shortcut='' then extractDotShortcut(note,shortcut); |
| 184 |
if name<>rest then |
| 185 |
begin extractGroup(note,'s',accidental); |
| 186 |
if accidental='' then extractGroup(note,'f',accidental); |
| 187 |
if accidental='' then extractGroup(note,'n',accidental); |
| 188 |
end; |
| 189 |
{ Look for 'i' or 'c' anywhere in what is left of note.} |
| 190 |
if accidental<>'' then |
| 191 |
begin extractOneOf(note,'ic',onlymidi); accidental:=accidental+onlymidi |
| 192 |
end; |
| 193 |
extractGroup(note,'d',dotgroup); |
| 194 |
if name=rest then extractSignedNumber(note,accidental); |
| 195 |
extractOneOf(note,durations,duration); |
| 196 |
if note<>rest then extractContiguous(note,'=+-',octave); |
| 197 |
if (length(note)>0) and (note[1]>='0') and (note[1]<='9') |
| 198 |
then begin octave:=note[1]+octave; delete1(note,1) end; |
| 199 |
whatever := note |
| 200 |
end |
| 201 |
end; |
| 202 |
|
| 203 |
{ On input: "note" is a note word; "dur1" is the default duration. |
| 204 |
On output: "note" has possibly been modified; |
| 205 |
possibly been split into two parts, the second being "shortcut"; |
| 206 |
"dur" is the suggested new default duration; |
| 207 |
"count" is the count of the total of "note" and "shortcut" } |
| 208 |
procedure processNote(var note, xnote: string; dur1: char; var dur: char; |
| 209 |
var count: integer); |
| 210 |
var sc, origdur: string[2]; |
| 211 |
multiplicity, l: integer; |
| 212 |
pnote: parsedNote; |
| 213 |
begin xnote:=''; dur:=dur1; |
| 214 |
if (note='') or not isNoteOrRest(note) or isPause(note) then exit; |
| 215 |
parseNote(note, pnote); |
| 216 |
if debugMode then begin write(note,' => '); printNote(pnote) end; |
| 217 |
with pnote do |
| 218 |
begin |
| 219 |
if pos1('.',whatever)>0 then warning('Suspicious dot in word '+note,print); |
| 220 |
origdur := duration; |
| 221 |
if duration='' then dur:=dur1 else dur:=duration[1]; |
| 222 |
count:=count64[dur]; if dotgroup<>'' then |
| 223 |
begin inc(count,count div 2); |
| 224 |
if startswith(dotgroup,'dd') then inc(count,count div 6) |
| 225 |
end; |
| 226 |
duration:=dur; if shortcut<>'' then |
| 227 |
begin |
| 228 |
if dotgroup<>'' then |
| 229 |
error('You may not explicitly dot a note with a shortcut',print); |
| 230 |
sc:=shortcut[1]; predelete(shortcut,1); |
| 231 |
if sc='.' then |
| 232 |
begin multiplicity:=1; |
| 233 |
if shortcut[1]='.' then |
| 234 |
begin inc(multiplicity); predelete(shortcut,1); sc:=sc+'.' end; |
| 235 |
inc(count,count); dur1:=duration[1]; |
| 236 |
for l:=1 to multiplicity do |
| 237 |
begin dotgroup:=dotgroup+dotcode; dur1:=half(dur1) end; |
| 238 |
addDuration(shortcut,dur1); |
| 239 |
end else |
| 240 |
begin addDuration(shortcut,half(duration[1])); |
| 241 |
inc(count,count div 2) |
| 242 |
end |
| 243 |
end; |
| 244 |
if not insertDuration then duration := origdur; |
| 245 |
if rearrangeNote |
| 246 |
then note := name + duration + octave + whatever |
| 247 |
+ accidental + dotgroup + xtuplet |
| 248 |
else shortcut:=' '; |
| 249 |
if not insertDuration and (shortcut<>'') then shortcut:=sc+shortcut; |
| 250 |
xnote:=shortcut |
| 251 |
end |
| 252 |
end; |
| 253 |
|
| 254 |
function octaveCode (note: string): char; |
| 255 |
var pnote: parsedNote; |
| 256 |
begin {if debugMode then write('Octave code in note "',note,'" is ');} |
| 257 |
parseNote(note,pnote); with pnote do |
| 258 |
begin {if debugMode then writeln('"',octave,'"');} |
| 259 |
if octave='' then octaveCode:=' ' else octaveCode:=octave[1]; end |
| 260 |
end; |
| 261 |
|
| 262 |
procedure removeOctaveCode(code: char; var note: string); |
| 263 |
var k, l: integer; |
| 264 |
begin {if debugMode then writeln('remove ',code,' from ',note);} l:=length(note); |
| 265 |
for k:=1 to l do if note[k]=code then |
| 266 |
if (k=l) or (note[k+1]<'0') or (note[k+1]>'9') then |
| 267 |
begin delete1(note,k); exit end; |
| 268 |
fatalError('Code not found in note') |
| 269 |
end; |
| 270 |
|
| 271 |
procedure insertOctaveCode(code: char; var note: string); |
| 272 |
var l: integer; |
| 273 |
begin {if debugMode then writeln('insert ',code,' into ',note); } |
| 274 |
l:=length(note); |
| 275 |
if (l<2) or (note[2]<'0') or (note[2]>'9') then |
| 276 |
fatalError('Trying to insert octave into note without duration'); |
| 277 |
if (l<=2) or (note[3]<'0') or (note[3]>'9') then insertChar(code,note,3) |
| 278 |
else writeln('Not inserting "',code,'", note already has octave code"') |
| 279 |
end; |
| 280 |
|
| 281 |
end. |