| 1 |
/* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */ |
| 2 |
/* From input file "mtx.pas" */ |
| 3 |
|
| 4 |
|
| 5 |
#include "p2c.h" |
| 6 |
|
| 7 |
|
| 8 |
#define MTX_G |
| 9 |
#include "mtx.h" |
| 10 |
|
| 11 |
|
| 12 |
#ifndef CONTROL_H |
| 13 |
#include "control.h" |
| 14 |
#endif |
| 15 |
|
| 16 |
#ifndef MTXLINE_H |
| 17 |
#include "mtxline.h" |
| 18 |
#endif |
| 19 |
|
| 20 |
#ifndef STRINGS_H |
| 21 |
#include "strings.h" |
| 22 |
#endif |
| 23 |
|
| 24 |
#ifndef STATUS_H |
| 25 |
#include "status.h" |
| 26 |
#endif |
| 27 |
|
| 28 |
#ifndef NOTES_H |
| 29 |
#include "notes.h" |
| 30 |
#endif |
| 31 |
|
| 32 |
|
| 33 |
#define flagged "8136" |
| 34 |
|
| 35 |
|
| 36 |
typedef boolean sticky['z' + 1 - 'a']; |
| 37 |
|
| 38 |
|
| 39 |
Static sticky note_attrib[maxvoices], rest_attrib[maxvoices]; |
| 40 |
|
| 41 |
|
| 42 |
Static short logTwo(short denom) |
| 43 |
{ |
| 44 |
short l = 0; |
| 45 |
|
| 46 |
while (denom > 1) { |
| 47 |
l++; |
| 48 |
denom /= 2; |
| 49 |
} |
| 50 |
return l; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
Local boolean dotted(short n) |
| 55 |
{ |
| 56 |
boolean Result; |
| 57 |
|
| 58 |
/* p2c: mtx.pas: Note: Eliminated unused assignment statement [338] */ |
| 59 |
if (n == 0) |
| 60 |
return false; |
| 61 |
if (n != n / 3 * 3) |
| 62 |
return false; |
| 63 |
n /= 3; |
| 64 |
while ((n & 1) == 0) |
| 65 |
n /= 2; |
| 66 |
return (n == 1); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/* RESTS should have a third parameter OFFSET. At present we in effect |
| 71 |
assume OFFSET+LENGTH to be a multiple of METERDENOM. */ |
| 72 |
Char *rests(Char *Result, short len, short meterdenom, boolean blind) |
| 73 |
{ |
| 74 |
Char r[256], bl[256]; |
| 75 |
Char STR1[256]; |
| 76 |
|
| 77 |
if (blind) |
| 78 |
strcpy(bl, "b"); |
| 79 |
else |
| 80 |
*bl = '\0'; |
| 81 |
if (len == 0) |
| 82 |
return strcpy(Result, ""); |
| 83 |
else { |
| 84 |
if (len == meterdenom * 128) { |
| 85 |
sprintf(Result, "r9%s ", bl); |
| 86 |
return Result; |
| 87 |
} else { |
| 88 |
if (dotted(len)) { |
| 89 |
rests(r, len - len / 3, meterdenom, false); |
| 90 |
r[2] = 'd'; |
| 91 |
sprintf(Result, "%s%s ", r, bl); |
| 92 |
return Result; |
| 93 |
} else if (len >= 64 / meterdenom) { |
| 94 |
sprintf(Result, "%sr%c%s ", |
| 95 |
rests(STR1, len - 64 / meterdenom, meterdenom, blind), |
| 96 |
durations[whole + logTwo(meterdenom) - 1], bl); |
| 97 |
return Result; |
| 98 |
} else |
| 99 |
return (rests(Result, len, meterdenom * 2, blind)); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
Local Char *attribs(Char *Result, Char *note) |
| 106 |
{ |
| 107 |
Char a[256]; |
| 108 |
short i = 2; |
| 109 |
short l; |
| 110 |
Char n; |
| 111 |
|
| 112 |
*a = '\0'; |
| 113 |
l = strlen(note); |
| 114 |
while (i <= l) { |
| 115 |
n = note[i-1]; |
| 116 |
if (n == 'x') |
| 117 |
i = l; |
| 118 |
else if (islower(n)) |
| 119 |
sprintf(a + strlen(a), "%c", n); |
| 120 |
i++; |
| 121 |
} |
| 122 |
return strcpy(Result, a); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
Static void checkSticky(Char *note, boolean *attrib) |
| 127 |
{ |
| 128 |
short i = 2; |
| 129 |
short l; |
| 130 |
Char c; |
| 131 |
Char a[256]; |
| 132 |
|
| 133 |
if (*note == '\0') |
| 134 |
return; |
| 135 |
l = strlen(note); |
| 136 |
while (i <= l) { |
| 137 |
c = note[i-1]; |
| 138 |
if (islower(c)) { |
| 139 |
if (i < l && note[i] == ':') { |
| 140 |
delete1(note, i + 1); |
| 141 |
l--; |
| 142 |
attrib[c - 'a'] = !attrib[c - 'a']; |
| 143 |
} else |
| 144 |
attrib[c - 'a'] = false; |
| 145 |
} |
| 146 |
i++; |
| 147 |
} |
| 148 |
attribs(a, note); |
| 149 |
for (c = 'z'; c >= 'a'; c--) { |
| 150 |
if (attrib[c - 'a'] && pos1(c, a) == 0) |
| 151 |
insertChar(c, note, 3); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
/* Routine was patched by Hiroaki Morimoto, revised 2003/08/15. |
| 157 |
Rewritten by DPL 2004/05/16 to be more understandable. |
| 158 |
octaveCode returns octave adjustments in the order |
| 159 |
[absolute or =],[relative] */ |
| 160 |
void checkOctave(voice_index voice, Char *note) |
| 161 |
{ |
| 162 |
Char code; |
| 163 |
|
| 164 |
code = octaveCode(note); |
| 165 |
if (code == '=') { |
| 166 |
setOctave(voice); |
| 167 |
removeOctaveCode(code, note); |
| 168 |
} |
| 169 |
if (octave(voice) == blank) |
| 170 |
return; |
| 171 |
code = octaveCode(note); |
| 172 |
if (isdigit(code)) { |
| 173 |
resetOctave(voice); |
| 174 |
return; |
| 175 |
} |
| 176 |
while (code == '+' || code == '-') { |
| 177 |
newOctave(voice, code); |
| 178 |
removeOctaveCode(code, note); |
| 179 |
code = octaveCode(note); |
| 180 |
} |
| 181 |
if (code != ' ') |
| 182 |
error3(voice, "You may have only one absolute octave assignment"); |
| 183 |
insertOctaveCode(octave(voice), note); |
| 184 |
checkRange(voice, note); |
| 185 |
resetOctave(voice); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
void markDebeamed(voice_index voice, Char *note) |
| 190 |
{ |
| 191 |
if (isVocal(voice) && afterSlur(voice) == 0 && unbeamVocal() && |
| 192 |
pos1(duration(voice), flagged) > 0) |
| 193 |
insertChar('a', note, 2); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
Static short barLength(Char *meter) |
| 198 |
{ |
| 199 |
short n1, n2, pn1, pn2; |
| 200 |
|
| 201 |
getMeter(meter, &n1, &n2, &pn1, &pn2); |
| 202 |
return (n1 * (64 / n2)); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
Static boolean isMultiBarRest(Char *rest_) |
| 207 |
{ |
| 208 |
boolean Result; |
| 209 |
|
| 210 |
/* p2c: mtx.pas: Note: Eliminated unused assignment statement [338] */ |
| 211 |
if (strlen(rest_) < 3) |
| 212 |
return false; |
| 213 |
if (rest_[1] != 'm') |
| 214 |
return false; |
| 215 |
if (*multi_bar_rest != '\0') |
| 216 |
error("Only one multibar rest allowed per line", print); |
| 217 |
strcpy(multi_bar_rest, rest_); |
| 218 |
return true; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
/* Double-length in xtuplet detected by a brute search for D anywhere. |
| 223 |
Could be more elegant. */ |
| 224 |
|
| 225 |
Static short macro_length[20]; |
| 226 |
|
| 227 |
Static Char macro_ID[20][3] = { |
| 228 |
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", |
| 229 |
"15", "16", "17", "18", "19", "20" |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
/* Local variables for scanMusic: */ |
| 234 |
struct LOC_scanMusic { |
| 235 |
voice_index voice; |
| 236 |
Char note[256]; |
| 237 |
boolean doublex, store_macro; |
| 238 |
short bar, old_length, bar_length, count, l, mlen, ngrace, nmulti, macroID, |
| 239 |
playID; |
| 240 |
Char macro_type; |
| 241 |
} ; |
| 242 |
|
| 243 |
Local void decmulti(struct LOC_scanMusic *LINK) |
| 244 |
{ |
| 245 |
if (LINK->doublex) |
| 246 |
LINK->nmulti -= 2; |
| 247 |
else |
| 248 |
LINK->nmulti--; |
| 249 |
} |
| 250 |
|
| 251 |
/* Local variables for countIt: */ |
| 252 |
struct LOC_countIt { |
| 253 |
struct LOC_scanMusic *LINK; |
| 254 |
} ; |
| 255 |
|
| 256 |
Local void incbar(short nl, struct LOC_countIt *LINK) |
| 257 |
{ |
| 258 |
LINK->LINK->bar_length += nl; |
| 259 |
if (LINK->LINK->bar_length > LINK->LINK->bar && meternum > 0) |
| 260 |
error3(LINK->LINK->voice, "Bar end occurs in mid-note"); |
| 261 |
} |
| 262 |
|
| 263 |
Local void countIt(struct LOC_scanMusic *LINK) |
| 264 |
{ |
| 265 |
struct LOC_countIt V; |
| 266 |
|
| 267 |
V.LINK = LINK; |
| 268 |
if (LINK->ngrace > 0) { |
| 269 |
LINK->ngrace--; |
| 270 |
return; |
| 271 |
} |
| 272 |
if (LINK->nmulti > 0) { |
| 273 |
decmulti(LINK); |
| 274 |
return; |
| 275 |
} |
| 276 |
if (LINK->count % 3 != 0 && |
| 277 |
(LINK->note[0] != rest && note_attrib[LINK->voice-1]['d' - 'a'] || |
| 278 |
LINK->note[0] == rest && rest_attrib[LINK->voice-1]['d' - 'a'])) |
| 279 |
LINK->count += LINK->count / 2; |
| 280 |
/* p2c: mtx.pas, line 150: |
| 281 |
* Note: Using % for possibly-negative arguments [317] */ |
| 282 |
incbar(LINK->count, &V); |
| 283 |
LINK->l = pos1(multi_group, LINK->note); |
| 284 |
if (LINK->l <= 0) |
| 285 |
return; |
| 286 |
predelete(LINK->note, LINK->l); |
| 287 |
getNum(LINK->note, &LINK->nmulti); |
| 288 |
decmulti(LINK); |
| 289 |
} |
| 290 |
|
| 291 |
Local void maybeGroup(struct LOC_scanMusic *LINK) |
| 292 |
{ |
| 293 |
if (LINK->note[0] != grace_group) |
| 294 |
return; |
| 295 |
if (strlen(LINK->note) == 1) |
| 296 |
LINK->ngrace = 1; |
| 297 |
else |
| 298 |
LINK->ngrace = pos1(LINK->note[1], digits); |
| 299 |
/* bug if ngrace>9 */ |
| 300 |
if (LINK->ngrace > 0) |
| 301 |
LINK->ngrace--; |
| 302 |
} |
| 303 |
|
| 304 |
Local short identifyMacro(Char *s_, struct LOC_scanMusic *LINK) |
| 305 |
{ |
| 306 |
Char s[256]; |
| 307 |
short k; |
| 308 |
|
| 309 |
strcpy(s, s_); |
| 310 |
predelete(s, 2); |
| 311 |
for (k = 1; k <= 20; k++) { |
| 312 |
if (!strcmp(s, macro_ID[k-1])) |
| 313 |
return k; |
| 314 |
} |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
Local void examineMacro(struct LOC_scanMusic *LINK) |
| 319 |
{ |
| 320 |
if (!countMacro()) |
| 321 |
return; |
| 322 |
if (debugMode()) |
| 323 |
printf("%s: ", LINK->note); |
| 324 |
if (strlen(LINK->note) == 1) { |
| 325 |
LINK->mlen = LINK->bar_length - LINK->old_length; |
| 326 |
if (LINK->macroID < 1 || LINK->macroID > 20) |
| 327 |
error3(LINK->voice, "Invalid macro ID"); |
| 328 |
macro_length[LINK->macroID-1] = LINK->mlen; |
| 329 |
if (debugMode()) |
| 330 |
printf("Ending macro %d of type %c, length=%d\n", |
| 331 |
LINK->macroID, LINK->macro_type, LINK->mlen); |
| 332 |
if (LINK->macro_type == 'S') |
| 333 |
LINK->bar_length = LINK->old_length; |
| 334 |
LINK->store_macro = false; |
| 335 |
return; |
| 336 |
} |
| 337 |
LINK->playID = identifyMacro(LINK->note, LINK); |
| 338 |
if (LINK->playID < 1 || LINK->playID > 20) |
| 339 |
error3(LINK->voice, "Invalid macro ID"); |
| 340 |
if (LINK->note[1] == 'P') { |
| 341 |
if (debugMode()) |
| 342 |
printf("Playing macro %d of length %d\n", |
| 343 |
LINK->playID, macro_length[LINK->playID-1]); |
| 344 |
LINK->bar_length += macro_length[LINK->playID-1]; |
| 345 |
return; |
| 346 |
} |
| 347 |
if (pos1(LINK->note[1], "SR") <= 0) |
| 348 |
return; |
| 349 |
LINK->old_length = LINK->bar_length; |
| 350 |
LINK->macro_type = LINK->note[1]; |
| 351 |
LINK->macroID = LINK->playID; |
| 352 |
LINK->store_macro = true; |
| 353 |
if (debugMode()) |
| 354 |
printf("Defining macro %d of type %c\n", LINK->macroID, LINK->note[1]); |
| 355 |
} |
| 356 |
|
| 357 |
|
| 358 |
void scanMusic(voice_index voice_, short *left_over) |
| 359 |
{ |
| 360 |
struct LOC_scanMusic V; |
| 361 |
Char buf[256], enote[256], xnote[256]; |
| 362 |
boolean has_next = false, done = false; |
| 363 |
Char dur1, lastdur; |
| 364 |
music_word nscan; |
| 365 |
Char STR1[256]; |
| 366 |
|
| 367 |
V.voice = voice_; |
| 368 |
strcpy(terminators, "d.x"); |
| 369 |
resetInfo(V.voice, buf); |
| 370 |
*left_over = 0; |
| 371 |
V.store_macro = false; |
| 372 |
V.bar = full_bar; |
| 373 |
V.bar_length = 0; |
| 374 |
V.ngrace = 0; |
| 375 |
V.nmulti = 0; |
| 376 |
if (meternum == 0) |
| 377 |
V.bar = 32000; |
| 378 |
dur1 = duration(V.voice); |
| 379 |
lastdur = dur1; |
| 380 |
do { |
| 381 |
getNextMusWord(buf, V.note, &nscan); |
| 382 |
if (*V.note == '\0') |
| 383 |
break; |
| 384 |
V.count = 0; |
| 385 |
/* if isNoteOrRest(note) and not (isPause(note) or isMultibarRest(note)) |
| 386 |
then note:=toStandard(note); */ |
| 387 |
V.doublex = (pos1('D', V.note) > 0 || pos1('F', V.note) > 0); |
| 388 |
if (nscan == mword) { |
| 389 |
if (*V.note == '\0') |
| 390 |
error3(V.voice, "You may not end a line with a meter change"); |
| 391 |
if (V.bar_length > 0) |
| 392 |
error3(V.voice, "Meter change only allowed at start of bar"); |
| 393 |
else |
| 394 |
V.bar = barLength(V.note); |
| 395 |
} else if (nscan == rword) { |
| 396 |
if (!(isPause(V.note) || isMultiBarRest(V.note) || |
| 397 |
V.ngrace + V.nmulti > 0)) |
| 398 |
{ /*0.63: allow rests in xtuples*/ |
| 399 |
processNote(V.note, xnote, dur1, &lastdur, &V.count); |
| 400 |
checkSticky(V.note, rest_attrib[V.voice-1]); |
| 401 |
} |
| 402 |
} |
| 403 |
if (*V.note != '\0') |
| 404 |
appendNote(V.voice, nscan); |
| 405 |
strcpy(enote, V.note); |
| 406 |
if (nscan == macro || nscan == endmacro) |
| 407 |
examineMacro(&V); |
| 408 |
if (nscan == abcdefg) { |
| 409 |
if (*multi_bar_rest == '\0' && V.ngrace + V.nmulti == 0) { |
| 410 |
processNote(enote, xnote, dur1, &lastdur, &V.count); |
| 411 |
if (*xnote != '\0') { |
| 412 |
checkSticky(enote, note_attrib[V.voice-1]); |
| 413 |
appendToLine(V.voice, enote); |
| 414 |
appendNote(V.voice, nscan); |
| 415 |
strcpy(enote, xnote); |
| 416 |
} |
| 417 |
checkSticky(enote, note_attrib[V.voice-1]); |
| 418 |
} |
| 419 |
} |
| 420 |
appendToLine(V.voice, enote); |
| 421 |
if (*V.note == '\0') /* !!! else word_bound[here]:=length(line); */ |
| 422 |
done = true; |
| 423 |
sprintf(STR1, "%c", barsym); |
| 424 |
if (!strcmp(V.note, STR1)) { |
| 425 |
if (meternum == 0) |
| 426 |
error3(V.voice, "You may not use bar lines in barless music"); |
| 427 |
else if (V.bar_length == 0) |
| 428 |
markBar(V.voice); |
| 429 |
else if (numberOfBars(V.voice) == 0 && V.bar_length < V.bar) { |
| 430 |
if (has_next) |
| 431 |
has_next = false; /*TODO Should check whether pickups are equal*/ |
| 432 |
else if (*left_over > 0) |
| 433 |
error3(V.voice, "Bar is too short"); |
| 434 |
*left_over = V.bar_length; |
| 435 |
V.bar_length = 0; |
| 436 |
} |
| 437 |
} |
| 438 |
if (nscan == nextvoice) { |
| 439 |
if (V.bar_length > 0) |
| 440 |
error3(V.voice, "Next voice before bar is full"); |
| 441 |
else |
| 442 |
barForward(V.voice, -1); |
| 443 |
has_next = true; |
| 444 |
} else if (isPause(V.note)) |
| 445 |
V.bar_length += V.bar; |
| 446 |
else if (*multi_bar_rest == '\0') { /*do nothing*/ |
| 447 |
if (!done && isNoteOrRest(V.note)) |
| 448 |
countIt(&V); |
| 449 |
else |
| 450 |
maybeGroup(&V); |
| 451 |
} |
| 452 |
dur1 = lastdur; |
| 453 |
if (V.bar_length >= V.bar && V.ngrace + V.nmulti == 0 && !V.store_macro) { |
| 454 |
if (debugMode()) |
| 455 |
printf("%d %d\n", V.voice, V.bar_length); |
| 456 |
barForward(V.voice, V.bar_length / V.bar); |
| 457 |
V.bar_length %= V.bar; |
| 458 |
/* p2c: mtx.pas, line 269: |
| 459 |
* Note: Using % for possibly-negative arguments [317] */ |
| 460 |
} |
| 461 |
} while (!done); |
| 462 |
setExtraLength(V.voice, V.bar_length); |
| 463 |
resetDuration(V.voice, dur1); |
| 464 |
regroup(V.voice); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
void initMTX(void) |
| 469 |
{ |
| 470 |
voice_index i; |
| 471 |
Char j; |
| 472 |
|
| 473 |
for (i = 0; i <= maxvoices - 1; i++) { |
| 474 |
for (j = 'a'; j <= 'z'; j++) |
| 475 |
note_attrib[i][j - 'a'] = false; |
| 476 |
} |
| 477 |
memcpy(rest_attrib, note_attrib, maxvoices * sizeof(sticky)); |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
/* End. */ |