Debian Codegraph @

utils/m-tx/mtx-src/status.c

raw
1 /* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */
2 /* From input file "status.pas" */
3
4
5 #include "p2c.h"
6
7
8 #define STATUS_G
9 #include "status.h"
10
11
12 #ifndef GLOBALS_H
13 #include "globals.h"
14 #endif
15
16 #ifndef STRINGS_H
17 #include "strings.h"
18 #endif
19
20 #ifndef MTXLINE_H
21 #include "mtxline.h"
22 #endif
23
24 #ifndef CONTROL_H
25 #include "control.h"
26 #endif
27
28 #ifndef UTILITY_H
29 #include "utility.h"
30 #endif
31
32 #ifndef NOTES_H
33 #include "notes.h"
34 #endif
35
36
37 #define lowest_pitch (-9)
38 #define highest_pitch 61
39
40
41 typedef struct line_status {
42 short pitch, chord_pitch, octave_adjust, beam_level, slur_level, after_slur;
43 Char octave, lastnote, chord_lastnote, duration, slurID, tieID;
44 boolean beamnext, beamed, slurnext, slurred, no_beam_melisma;
45 boolean no_slur_melisma[12];
46 int5 chord_tie_pitch;
47 Char chord_tie_label[6];
48 } line_status;
49
50
51 Static Char init_oct[256] = "";
52
53 Static Char range_name[56][256] = {
54 "0c", "0d", "0e", "0f", "0g", "0a", "0b", "1c", "1d", "1e", "1f", "1g",
55 "1a", "1b", "2c", "2d", "2e", "2f", "2g", "2a", "2b", "3c", "3d", "3e",
56 "3f", "3g", "3a", "3b", "4c", "4d", "4e", "4f", "4g", "4a", "4b", "5c",
57 "5d", "5e", "5f", "5g", "5a", "5b", "6c", "6d", "6e", "6f", "6g", "6a",
58 "6b", "7c", "7d", "7e", "7f", "7g", "7a", "7b"
59 /* p2c: status.pas, line 76:
60 * Note: Line breaker spent 0.0 seconds, 5000 tries on line 58 [251] */
61 };
62
63 Static line_status current[maxvoices];
64 Static Char lastdur[maxvoices];
65 Static Char voice_range[maxvoices][256], range_low[maxvoices][256],
66 range_high[maxvoices][256];
67
68
69 /* Range limits are user-specified as e.g. 3c. To make comparisons
70 meaningful, a and b (which come after g) are translated to h and i. */
71
72 void checkRange(short voice, Char *note_)
73 {
74 Char note[256];
75 Char orig_note[256];
76 Char STR1[4];
77 Char STR3[256];
78
79 strcpy(note, note_);
80 if (*voice_range[voice-1] == '\0')
81 return;
82 strcpy(orig_note, note);
83 if (strlen(note) > 2) { /* assume usual PMX form with octave */
84 sprintf(STR1, "%c%c", note[2], note[0]);
85 strcpy(note, STR1);
86 }
87 if (note[1] == 'a')
88 note[1] = 'h';
89 if (note[1] == 'b')
90 note[1] = 'i';
91 if (strcmp(note, range_low[voice-1]) < 0 ||
92 strcmp(note, range_high[voice-1]) > 0) {
93 sprintf(STR3, "%s is out of range, specified as %s",
94 orig_note, voice_range[voice-1]);
95 error3(voice, STR3);
96 }
97 }
98
99
100 void defineRange(short voice, Char *range_)
101 {
102 Char range[256];
103 Char STR2[256];
104
105 strcpy(range, range_);
106 strcpy(voice_range[voice-1], range);
107 if (*range == '\0')
108 return;
109 if ('0' > range[0] || range[0] > '7' || 'a' > range[1] || range[1] > 'g' ||
110 range[2] != '-' || '0' > range[3] || range[3] > '7' || 'a' > range[4] ||
111 range[4] > 'g') {
112 sprintf(STR2,
113 "Badly formatted range \"%s\" for voice %s, must be e.g. 3c-4a",
114 range, voice_label[voice-1]);
115 error(STR2, print);
116 }
117 if (range[1] == 'a')
118 range[1] = 'h';
119 if (range[1] == 'b')
120 range[1] = 'i';
121 if (range[4] == 'a')
122 range[4] = 'h';
123 if (range[4] == 'b')
124 range[4] = 'i';
125 substr_(range_low[voice-1], range, 1, 2);
126 substr_(range_high[voice-1], range, 4, 2);
127 }
128
129
130 void chordTie(short voice, Char *lab)
131 {
132 short n;
133 line_status *WITH;
134
135 WITH = &current[voice-1];
136 n = strlen(WITH->chord_tie_label);
137 if (n == 5)
138 error3(voice, "Only five slur ties allowed per voice");
139 if (n == 0)
140 *lab = 'T';
141 else
142 *lab = WITH->chord_tie_label[n-1];
143 (*lab)++;
144 sprintf(WITH->chord_tie_label + strlen(WITH->chord_tie_label), "%c", *lab);
145 /* p2c: status.pas, line 115:
146 * Note: Possible string truncation in assignment [145] */
147 n++;
148 WITH->chord_tie_pitch[n-1] = WITH->chord_pitch;
149 }
150
151
152 void getChordTies(short voice, short *pitches, Char *labels)
153 {
154 line_status *WITH;
155
156 WITH = &current[voice-1];
157 memcpy(pitches, WITH->chord_tie_pitch, sizeof(int5));
158 strcpy(labels, WITH->chord_tie_label);
159 *WITH->chord_tie_label = '\0';
160 }
161
162
163 void rememberDurations(void)
164 {
165 voice_index v, FORLIM;
166
167 FORLIM = nvoices;
168 for (v = 1; v <= FORLIM; v++)
169 lastdur[v-1] = duration(v);
170 }
171
172
173 void restoreDurations(void)
174 {
175 voice_index v, FORLIM;
176
177 FORLIM = nvoices;
178 for (v = 1; v <= FORLIM; v++)
179 resetDuration(v, lastdur[v-1]);
180 }
181
182
183 Char duration(short voice)
184 {
185 return (current[voice-1].duration);
186 }
187
188
189 void resetDuration(short voice, Char dur)
190 {
191 if (pos1(dur, durations) == 0) {
192 printf("Trying to set duration to %c; ", dur);
193 error3(voice, "M-Tx system error: resetDuration");
194 }
195 current[voice-1].duration = dur;
196 }
197
198
199 void activateBeamsAndSlurs(short voice)
200 {
201 line_status *WITH;
202
203 WITH = &current[voice-1];
204 if (WITH->beamnext) {
205 WITH->beamed = true;
206 WITH->beamnext = false;
207 }
208 if (WITH->slurnext) {
209 WITH->slurred = true;
210 WITH->slurnext = false;
211 }
212 if (WITH->slurred)
213 WITH->after_slur++;
214 }
215
216
217 void saveStatus(short voice)
218 {
219 line_status *WITH;
220
221 WITH = &current[voice-1];
222 WITH->chord_pitch = WITH->pitch;
223 WITH->chord_lastnote = WITH->lastnote;
224 }
225
226
227 boolean noBeamMelisma(short voice)
228 {
229 return (current[voice-1].no_beam_melisma);
230 }
231
232
233 short afterSlur(short voice)
234 {
235 short Result;
236 line_status *WITH;
237
238 WITH = &current[voice-1];
239 Result = WITH->after_slur;
240 if (WITH->after_slur > 0 && WITH->slur_level < 1)
241 error3(voice, "M-Tx system error: afterSlur and slur_level incompatible)");
242 return Result;
243 }
244
245
246 Char octave(short voice)
247 {
248 return (current[voice-1].octave);
249 }
250
251
252 void resetOctave(short voice)
253 {
254 current[voice-1].octave = ' ';
255 }
256
257
258 void initOctaves(Char *octaves)
259 {
260 short i = 1;
261
262 strcpy(init_oct, octaves);
263 while (i <= strlen(init_oct)) {
264 if (init_oct[i-1] == ' ')
265 delete1(init_oct, i);
266 else
267 i++;
268 }
269 }
270
271
272 Static Char initOctave(stave_index voice_stave)
273 {
274 if (voice_stave > strlen(init_oct)) {
275 if (pos1(clef[voice_stave-1], "Gt08") > 0)
276 return '4';
277 else
278 return '3';
279 } else
280 return (init_oct[voice_stave-1]);
281 }
282
283
284 void setOctave(short voice)
285 {
286 current[voice-1].octave = initOctave(voiceStave(voice));
287 }
288
289
290 void newOctave(short voice, Char dir)
291 {
292 line_status *WITH;
293
294 WITH = &current[voice-1];
295 switch (dir) {
296
297 case '+':
298 WITH->octave++;
299 break;
300
301 case '-':
302 WITH->octave--;
303 break;
304 }
305 }
306
307
308 Static short newPitch(short voice, Char *note_, short pitch, Char lastnote)
309 {
310 Char note[256];
311 short interval, npitch;
312 Char oct;
313
314 strcpy(note, note_);
315 oct = octaveCode(note);
316 if (oct == '=')
317 oct = initOctave(voiceStave(voice));
318 if (isdigit(oct)) {
319 pitch = (oct - '0') * 7 - 3;
320 lastnote = 'f';
321 removeOctaveCode(oct, note);
322 oct = octaveCode(note);
323 }
324 interval = note[0] - lastnote;
325 if (interval > 3)
326 interval -= 7;
327 if (interval < -3)
328 interval += 7;
329 npitch = pitch + interval;
330 while (oct != ' ') {
331 if (oct == '+')
332 npitch += 7;
333 else if (oct == '-')
334 npitch -= 7;
335 removeOctaveCode(oct, note);
336 oct = octaveCode(note);
337 }
338 return npitch;
339 }
340
341
342 Local void delins(Char *note, Char c1, Char c2, short l)
343 {
344 short i, n;
345
346 n = strlen(note);
347 i = pos1(c1, note);
348 if (i == 0)
349 i = n + 1;
350 while (l > 0 && i <= n) {
351 delete1(note, i);
352 n--;
353 l--;
354 }
355 i = pos1(c2, note);
356 if (i == 0) {
357 if (strlen(note) < 2)
358 error("M-Tx program error", print);
359 else
360 i = 3;
361 }
362 while (l > 0) {
363 insertChar(c2, note, i);
364 l--;
365 }
366 }
367
368
369 Static void repitch(Char *note, short diff)
370 {
371 diff /= 7;
372 if (diff > 0)
373 delins(note, '-', '+', diff);
374 else
375 delins(note, '+', '-', -diff);
376 }
377
378
379 void setUnbeamed(short voice)
380 {
381 current[voice-1].beamed = false;
382 }
383
384
385 void setUnslurred(short voice)
386 {
387 line_status *WITH;
388
389 WITH = &current[voice-1];
390 WITH->slurred = false;
391 WITH->after_slur = 0;
392 }
393
394
395 void beginBeam(short voice, Char *note)
396 {
397 line_status *WITH;
398
399 WITH = &current[voice-1];
400 if (WITH->beamed)
401 error3(voice, "Starting a forced beam while another is open");
402 if (WITH->beam_level > 0)
403 error3(voice,
404 "Starting a forced beam while another is open (beamlevel>0)");
405 WITH->beam_level++;
406 WITH->beamnext = true;
407 WITH->no_beam_melisma = startsWith(note, "[[");
408 if (WITH->no_beam_melisma)
409 predelete(note, 1);
410 }
411
412
413 void endBeam(short voice)
414 {
415 line_status *WITH;
416
417 WITH = &current[voice-1];
418 if (WITH->beam_level < 1)
419 error3(voice, "Closing a beam that was never opened");
420 WITH->beam_level--;
421 setUnbeamed(voice);
422 }
423
424
425 short slurLevel(short voice)
426 {
427 return (current[voice-1].slur_level);
428 }
429
430
431 short beamLevel(short voice)
432 {
433 return (current[voice-1].beam_level);
434 }
435
436
437 boolean noSlurMelisma(short voice, short history)
438 {
439 line_status *WITH;
440
441 WITH = &current[voice-1];
442 return (WITH->no_slur_melisma[WITH->slur_level + history - 1]);
443 }
444
445
446 Static Char *slurLabel(Char *Result, short voice, Char *note)
447 {
448 Char sl;
449
450 if (*note == '\0')
451 return strcpy(Result, "");
452 if (strlen(note) < 2)
453 return strcpy(Result, " ");
454 if (note[1] >= '0' && note[1] <= 'Z')
455 sl = note[1];
456 else
457 sl = ' ';
458 if (sl >= 'I' && sl <= 'T')
459 warning3(voice, "Slur label in the range I..T may cause conflict");
460 sprintf(Result, "%c", sl);
461 return Result;
462 }
463
464
465 Static void labelSlur(short voice, Char *note)
466 {
467 Char sl;
468 line_status *WITH;
469
470 if (*note == '\0')
471 return;
472 WITH = &current[voice-1];
473 if (note[0] == ')')
474 WITH->slurID += 2;
475 else if (note[0] == '}')
476 WITH->tieID += 2;
477 /** CMO 0.60a: replace assigning tieID to sl by space charater
478 if (note[1]='(') or (note[1]=')') then sl:=slurID else sl:=tieID; */
479 if (note[0] == '(' || note[0] == ')')
480 sl = WITH->slurID;
481 else
482 sl = ' ';
483 /** CMO 0.60d: omit insertchar in case of tie
484 insertchar(sl,note,2); */
485 if (note[0] == '(' || note[0] == ')')
486 insertChar(sl, note, 2);
487 if (note[0] == '(')
488 WITH->slurID -= 2;
489 else if (note[0] == '{')
490 WITH->tieID -= 2;
491 if (WITH->slurID < 'I')
492 warning3(voice, "Too many nested slurs may cause conflict");
493 if (WITH->tieID < 'I')
494 warning3(voice, "Too many nested ties may cause conflict");
495 }
496
497
498 void beginSlur(short voice, Char *note)
499 {
500 short posblind;
501 line_status *WITH;
502 Char STR1[256], STR2[256];
503
504 WITH = &current[voice-1];
505 WITH->slur_level++;
506 if (WITH->slur_level > 12)
507 error3(voice, "Too many open slurs");
508 WITH->no_slur_melisma[WITH->slur_level - 1] = (startsWith(note, "((") ||
509 startsWith(note, "{{"));
510 if (WITH->no_slur_melisma[WITH->slur_level - 1])
511 predelete(note, 1);
512 if (!strcmp(slurLabel(STR1, voice, note), "0"))
513 delete1(note, 2);
514 else {
515 if (!strcmp(slurLabel(STR2, voice, note), " "))
516 labelSlur(voice, note);
517 }
518 posblind = pos1('~', note);
519 if (posblind > 0) {
520 if (hideBlindSlurs())
521 *note = '\0';
522 else
523 delete1(note, posblind);
524 }
525 WITH->slurnext = true;
526 }
527
528
529 void endSlur(short voice, Char *note)
530 {
531 short poscontinue, posblind;
532 Char contslur[256];
533 line_status *WITH;
534 Char STR1[256], STR2[256];
535
536 WITH = &current[voice-1];
537 *contslur = '\0';
538 if (WITH->slur_level < 1)
539 error3(voice, "Ending a slur that was never started");
540 if (note[0] == ')')
541 poscontinue = pos1('(', note);
542 else if (note[0] == '}')
543 poscontinue = pos1('{', note);
544 if (poscontinue == 0)
545 WITH->slur_level--;
546 else {
547 poscontinue--;
548 strcpy(contslur, note);
549 predelete(contslur, poscontinue);
550 shorten(note, poscontinue);
551 }
552 if (WITH->slur_level == 0)
553 setUnslurred(voice);
554 if (!strcmp(slurLabel(STR1, voice, note), "0"))
555 delete1(note, 2);
556 else {
557 if (!strcmp(slurLabel(STR2, voice, note), " "))
558 labelSlur(voice, note);
559 }
560 if (!strcmp(slurLabel(STR1, voice, contslur), "0"))
561 delete1(contslur, 2);
562 else {
563 if (!strcmp(slurLabel(STR2, voice, contslur), " "))
564 labelSlur(voice, contslur);
565 }
566 if (poscontinue > 0) {
567 if (note[0] == '}')
568 strcat(note, "t");
569 note[0] = 's';
570 if (contslur[0] == '{')
571 strcat(contslur, "t");
572 contslur[0] = 's';
573 }
574 posblind = pos1('~', note);
575 if (posblind > 0) {
576 if (hideBlindSlurs())
577 *note = '\0';
578 else
579 delete1(note, posblind);
580 }
581 if (*note != '\0' && *contslur != '\0')
582 sprintf(note + strlen(note), " %s", contslur);
583 }
584
585
586 /* This is the routine called by prepmx.pas for every normal and chordal
587 note, immediately after checkOctave. It does two things: adjusts pitch
588 of notes following a C: chord to what it would have been without that
589 chord, and checks for pitch overruns. */
590 void renewPitch(short voice, Char *note)
591 {
592 short pstat;
593 line_status *WITH;
594
595 WITH = &current[voice-1];
596 pstat = newPitch(voice, note, WITH->chord_pitch, WITH->chord_lastnote);
597 WITH->pitch = newPitch(voice, note, WITH->pitch, WITH->lastnote);
598 if (WITH->pitch != pstat)
599 repitch(note, WITH->pitch - pstat);
600 checkRange(voice, range_name[WITH->pitch + 6]);
601 if (WITH->pitch < lowest_pitch && checkPitch()) {
602 printf("Pitch of note %s following %c reported as %d",
603 note, WITH->lastnote, WITH->pitch);
604 error3(voice, "Pitch too low");
605 }
606 if (WITH->pitch > highest_pitch && checkPitch()) {
607 printf("Pitch of note %s following %c reported as %d",
608 note, WITH->lastnote, WITH->pitch);
609 error3(voice, "Pitch too high");
610 }
611 WITH->lastnote = note[0];
612 }
613
614
615 short chordPitch(short voice)
616 {
617 return (current[voice-1].chord_pitch);
618 }
619
620
621 void renewChordPitch(short voice, Char *note)
622 {
623 line_status *WITH;
624
625 WITH = &current[voice-1];
626 WITH->chord_pitch = newPitch(voice, note, WITH->chord_pitch,
627 WITH->chord_lastnote);
628 if (WITH->chord_pitch < lowest_pitch)
629 error3(voice, "Pitch in chord too low");
630 if (WITH->chord_pitch > highest_pitch)
631 error3(voice, "Pitch in chord too high");
632 WITH->chord_lastnote = note[0];
633 }
634
635
636 void initStatus(void)
637 {
638 short voice, FORLIM;
639 line_status *WITH;
640
641 FORLIM = nvoices;
642 for (voice = 1; voice <= FORLIM; voice++) {
643 WITH = &current[voice-1];
644 WITH->duration = default_duration;
645 WITH->octave_adjust = 0;
646 WITH->slur_level = 0;
647 WITH->beam_level = 0;
648 WITH->beamed = false;
649 WITH->beamnext = false;
650 WITH->slurred = false;
651 WITH->slurnext = false;
652 WITH->after_slur = 0;
653 WITH->octave = initOctave(voiceStave(voice));
654 WITH->slurID = 'S';
655 WITH->tieID = 'T';
656 WITH->lastnote = 'f';
657 WITH->pitch = (WITH->octave - '0') * 7 - 3;
658 *WITH->chord_tie_label = '\0';
659 saveStatus(voice);
660 }
661 }
662
663
664
665
666 /* End. */