Debian Codegraph @

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

raw
1 /* Output from p2c 1.21alpha-07.Dec.93, the Pascal-to-C translator */
2 /* From input file "notes.pas" */
3
4
5 #include "p2c.h"
6
7
8 #define NOTES_G
9 #include "notes.h"
10
11
12 short count64['9' + 1 - '0'] = {
13 64, 4, 32, 2, 16, 0, 1, 0, 8, 128
14 };
15
16 #ifndef STRINGS_H
17 #include "strings.h"
18 #endif
19
20 #ifndef GLOBALS_H
21 #include "globals.h"
22 #endif
23
24
25 typedef struct parsedNote {
26 Char name;
27 Char duration[2];
28 Char octave[9];
29 Char accidental[17], whatever[17], dotgroup[17], xtuplet[17];
30 Char shortcut[33];
31 } parsedNote;
32
33
34 Static void printNote(parsedNote n)
35 {
36 printf("%c|%s|%s|%s|%s|%s|%s|%s\n",
37 n.name, n.duration, n.octave, n.accidental, n.whatever, n.dotgroup,
38 n.xtuplet, n.shortcut);
39 }
40
41
42 /* If rearrangeNote is TRUE, translate original note to the following form:
43 1. Note name.
44 2. Duration.
45 3. Octave adjustments.
46 4. Everything except the other six items.
47 5. Accidental with adjustments (rest: height adjustment)
48 6. Dot with adjustments.
49 7. Xtuplet group.
50 */
51
52 void translateSolfa(Char *nt)
53 {
54 short k;
55
56 if (!solfaNoteNames())
57 return;
58 k = pos1(*nt, solfa_names);
59 if (k > 0)
60 *nt = has_duration[k-1];
61 }
62
63
64 Char durationCode(Char *note)
65 {
66 Char Result = unspecified;
67 Char code;
68
69 if (strlen(note) <= 1)
70 return Result;
71 code = note[1];
72 if (pos1(code, durations) > 0)
73 return code;
74 return Result;
75 }
76
77
78 Static Char half(Char dur)
79 {
80 Char Result;
81 short k;
82 Char STR1[20];
83 Char STR2[24];
84
85 k = pos1(dur, durations);
86 Result = dur;
87 if (k == 0) {
88 sprintf(STR1, "Invalid duration %c", dur);
89 error(STR1, print);
90 return Result;
91 }
92 if (k <= ndurs)
93 return (durations[k]);
94 sprintf(STR2, "%c is too short to halve", dur);
95 error(STR2, print);
96 return Result;
97 }
98
99
100 Static void addDuration(Char *note, Char dur)
101 {
102 if (insertDuration())
103 insertChar(dur, note, 2);
104 }
105
106
107 /* Extract procedures. All of these remove part of "note" (sometimes
108 the part is empty) and put it somewhere else. The part may be anywhere
109 in "note", except when otherwise specified.*/
110
111 /* Unconditionally extracts the first character. */
112
113 Static void extractFirst(Char *note, Char *first)
114 {
115 *first = note[0];
116 predelete(note, 1);
117 }
118
119
120 /* Extracts at most one of the characters in "hits". */
121
122 Static void extractOneOf(Char *note, Char *hits, Char *hit)
123 {
124 short i, l;
125
126 l = strlen(note);
127 *hit = '\0';
128 for (i = 1; i <= l; i++) {
129 if (pos1(note[i-1], hits) > 0) {
130 sprintf(hit, "%c", note[i-1]);
131 delete1(note, i);
132 return;
133 }
134 }
135 }
136
137
138 /* Extracts contiguous characters in "hits" until no more are found.
139 There may be more later. */
140
141 Static void extractContiguous(Char *note, Char *hits, Char *hit)
142 {
143 short i, l, len;
144
145 l = strlen(note);
146 len = l;
147 *hit = '\0';
148 for (i = 1; i <= l; i++) {
149 if (pos1(note[i-1], hits) > 0) {
150 do {
151 if (pos1(note[i-1], hits) == 0)
152 return;
153 sprintf(hit + strlen(hit), "%c", note[i-1]);
154 delete1(note, i);
155 len--;
156 } while (len >= i);
157 return;
158 }
159 }
160 }
161
162
163 /* Extracts the specified character and everything after it. */
164
165 Static void extractAfter(Char *note, Char delim, Char *tail)
166 {
167 short newlen;
168
169 newlen = pos1(delim, note);
170 *tail = '\0';
171 if (newlen == 0)
172 return;
173 newlen--;
174 strcpy(tail, note);
175 predelete(tail, newlen);
176 note[(long)((int)newlen)] = '\0';
177 /* p2c: notes.pas, line 109:
178 * Note: Modification of string length may translate incorrectly [146] */
179 }
180
181
182 /* Extracts the dot shortcut part of a note: comma shortcut is no problem
183 because a comma cannot be part of a number. */
184
185 Static void extractDotShortcut(Char *note, Char *tail)
186 {
187 Char names[256], tail2[256];
188 short l = 1;
189 short lt;
190 Char ch;
191
192 extractAfter(note, '.', tail);
193 lt = strlen(tail);
194 if (l < lt && tail[1] == '.')
195 l = 2;
196 if (solfaNoteNames())
197 strcpy(names, solfa_names);
198 else
199 strcpy(names, has_duration);
200 if (l < lt && pos1(tail[l], names) > 0) {
201 translateSolfa(&tail[l]);
202 return;
203 }
204 if (l == 2)
205 error("\"..\" followed by non-note", print);
206 if (l >= lt) {
207 strcat(note, tail);
208 *tail = '\0';
209 return;
210 }
211 ch = tail[0];
212 predelete(tail, 1);
213 extractDotShortcut(tail, tail2);
214 sprintf(note + strlen(note), "%c%s", ch, tail);
215 strcpy(tail, tail2);
216 }
217
218
219 /* Extracts a signed number. */
220
221 Static void extractSignedNumber(Char *note, Char *number)
222 {
223 short k;
224 Char note0[256];
225
226 k = pos1('+', note);
227 if (k == 0)
228 k = pos1('-', note);
229 *number = '\0';
230 if (k == 0)
231 return;
232 strcpy(note0, note);
233 do {
234 sprintf(number + strlen(number), "%c", note[k-1]);
235 delete1(note, k);
236 } while (k <= strlen(note) &&
237 (note[k-1] == '0' || pos1(note[k-1], digits) != 0));
238 if (strlen(number) == 1) {
239 strcpy(note, note0);
240 *number = '\0';
241 }
242 }
243
244
245 /* Local variables for extractGroup: */
246 struct LOC_extractGroup {
247 Char *group;
248 short gl, k;
249 boolean probe, nonumber;
250 } ;
251
252 Local void tryMore(struct LOC_extractGroup *LINK)
253 {
254 while (LINK->k <= LINK->gl && LINK->group[LINK->k-1] == LINK->group[0])
255 LINK->k++;
256 }
257
258 Local void try_(Char *s, struct LOC_extractGroup *LINK)
259 {
260 LINK->probe = (LINK->k < LINK->gl && pos1(LINK->group[LINK->k-1], s) > 0);
261 if (LINK->probe)
262 LINK->k++;
263 }
264
265 Local void tryNumber(struct LOC_extractGroup *LINK)
266 {
267 boolean dot_ = false;
268
269 LINK->nonumber = true;
270 while (LINK->k <= LINK->gl && pos1(LINK->group[LINK->k-1], digitsdot) > 0) {
271 LINK->k++;
272 if (LINK->group[LINK->k-1] == '.') {
273 if (dot_)
274 error("Extra dot in number", print);
275 else
276 dot_ = true;
277 } else
278 LINK->nonumber = false;
279 }
280 }
281
282
283 /* Extracts a symbol followed by optional +- or <> shift indicators */
284
285 Static void extractGroup(Char *note, Char delim, Char *group_)
286 {
287 struct LOC_extractGroup V;
288 short k0;
289 Char tail[256];
290
291 V.group = group_;
292 extractAfter(note, delim, V.group);
293 if (*V.group == '\0')
294 return;
295 V.gl = strlen(V.group);
296 V.k = 2;
297 if (V.gl > 1 && V.group[1] == ':')
298 V.k = 3;
299 else {
300 tryMore(&V);
301 k0 = V.k;
302 try_("+-<>", &V);
303 if (V.probe)
304 tryNumber(&V);
305 if (V.nonumber)
306 V.k = k0;
307 k0 = V.k;
308 try_("+-<>", &V);
309 if (V.probe)
310 tryNumber(&V);
311 if (V.nonumber)
312 V.k = k0;
313 }
314 strcpy(tail, V.group);
315 V.k--;
316 V.group[(long)((int)V.k)] = '\0';
317 /* p2c: notes.pas, line 170:
318 * Note: Modification of string length may translate incorrectly [146] */
319 predelete(tail, V.k);
320 strcat(note, tail);
321 }
322
323
324 Static void parseNote(Char *note_, parsedNote *pnote)
325 {
326 Char note[256];
327 Char onlymidi[256];
328 Char STR1[256];
329 Char STR2[10];
330
331 strcpy(note, note_);
332 *pnote->shortcut = '\0';
333 *pnote->xtuplet = '\0';
334 *pnote->accidental = '\0';
335 *pnote->dotgroup = '\0';
336 *pnote->duration = '\0';
337 *pnote->octave = '\0';
338 *onlymidi = '\0';
339 extractFirst(note, &pnote->name);
340 extractAfter(note, 'x', pnote->xtuplet);
341 extractAfter(note, ',', pnote->shortcut);
342 if (*pnote->shortcut == '\0')
343 extractDotShortcut(note, pnote->shortcut);
344 if (pnote->name != rest) {
345 extractGroup(note, 's', pnote->accidental);
346 if (*pnote->accidental == '\0')
347 extractGroup(note, 'f', pnote->accidental);
348 if (*pnote->accidental == '\0')
349 extractGroup(note, 'n', pnote->accidental);
350 }
351 /* Look for 'i' or 'c' anywhere in what is left of note.*/
352 if (*pnote->accidental != '\0') {
353 extractOneOf(note, "ic", onlymidi);
354 strcat(pnote->accidental, onlymidi);
355 /* p2c: notes.pas, line 192:
356 * Note: Possible string truncation in assignment [145] */
357 }
358 extractGroup(note, 'd', pnote->dotgroup);
359 if (pnote->name == rest)
360 extractSignedNumber(note, pnote->accidental);
361 extractOneOf(note, durations, pnote->duration);
362 sprintf(STR1, "%c", rest);
363 if (strcmp(note, STR1))
364 extractContiguous(note, "=+-", pnote->octave);
365 if (*note != '\0' && isdigit(note[0])) {
366 sprintf(STR2, "%c%s", note[0], pnote->octave);
367 strcpy(pnote->octave, STR2);
368 /* p2c: notes.pas, line 198:
369 * Note: Possible string truncation in assignment [145] */
370 delete1(note, 1);
371 }
372 strcpy(pnote->whatever, note);
373 /* p2c: notes.pas, line 200:
374 * Note: Possible string truncation in assignment [145] */
375 }
376
377
378 /* On input: "note" is a note word; "dur1" is the default duration.
379 On output: "note" has possibly been modified;
380 possibly been split into two parts, the second being "shortcut";
381 "dur" is the suggested new default duration;
382 "count" is the count of the total of "note" and "shortcut" */
383 void processNote(Char *note, Char *xnote, Char dur1, Char *dur, short *count)
384 {
385 Char sc[3], origdur[3];
386 short multiplicity, l;
387 parsedNote pnote;
388 Char STR1[36];
389 Char STR3[256];
390
391 *xnote = '\0';
392 *dur = dur1;
393 if (*note == '\0' || !isNoteOrRest(note) || isPause(note))
394 return;
395 parseNote(note, &pnote);
396 if (debugMode()) {
397 printf("%s => ", note);
398 printNote(pnote);
399 }
400 if (pos1('.', pnote.whatever) > 0) {
401 sprintf(STR3, "Suspicious dot in word %s", note);
402 warning(STR3, print);
403 }
404 strcpy(origdur, pnote.duration);
405 if (*pnote.duration == '\0')
406 *dur = dur1;
407 else
408 *dur = pnote.duration[0];
409 *count = count64[*dur - '0'];
410 if (*pnote.dotgroup != '\0') {
411 *count += *count / 2;
412 if (startsWith(pnote.dotgroup, "dd"))
413 *count += *count / 6;
414 }
415 sprintf(pnote.duration, "%c", *dur);
416 if (*pnote.shortcut != '\0') {
417 if (*pnote.dotgroup != '\0')
418 error("You may not explicitly dot a note with a shortcut", print);
419 sprintf(sc, "%c", pnote.shortcut[0]);
420 predelete(pnote.shortcut, 1);
421 if (!strcmp(sc, ".")) {
422 multiplicity = 1;
423 if (pnote.shortcut[0] == '.') {
424 multiplicity++;
425 predelete(pnote.shortcut, 1);
426 strcat(sc, ".");
427 /* p2c: notes.pas, line 234:
428 * Note: Possible string truncation in assignment [145] */
429 }
430 *count += *count;
431 dur1 = pnote.duration[0];
432 for (l = 1; l <= multiplicity; l++) {
433 sprintf(pnote.dotgroup + strlen(pnote.dotgroup), "%c", dotcode);
434 /* p2c: notes.pas, line 237:
435 * Note: Possible string truncation in assignment [145] */
436 dur1 = half(dur1);
437 }
438 addDuration(pnote.shortcut, dur1);
439 } else {
440 addDuration(pnote.shortcut, half(pnote.duration[0]));
441 *count += *count / 2;
442 }
443 }
444 if (!insertDuration()) {
445 strcpy(pnote.duration, origdur);
446 /* p2c: notes.pas, line 244:
447 * Note: Possible string truncation in assignment [145] */
448 }
449 if (rearrangeNote())
450 sprintf(note, "%c%s%s%s%s%s%s",
451 pnote.name, pnote.duration, pnote.octave, pnote.whatever,
452 pnote.accidental, pnote.dotgroup, pnote.xtuplet);
453 else
454 strcpy(pnote.shortcut, " ");
455 if (!insertDuration() && *pnote.shortcut != '\0') {
456 sprintf(STR1, "%s%s", sc, pnote.shortcut);
457 strcpy(pnote.shortcut, STR1);
458 /* p2c: notes.pas, line 249:
459 * Note: Possible string truncation in assignment [145] */
460 }
461 strcpy(xnote, pnote.shortcut);
462 }
463
464
465 Char octaveCode(Char *note)
466 { /*if debugMode then write('Octave code in note "',note,'" is ');*/
467 parsedNote pnote;
468
469 parseNote(note, &pnote);
470 /*if debugMode then writeln('"',octave,'"');*/
471 if (*pnote.octave == '\0')
472 return ' ';
473 else
474 return (pnote.octave[0]);
475 }
476
477
478 void removeOctaveCode(Char code, Char *note)
479 { /*if debugMode then writeln('remove ',code,' from ',note);*/
480 short k, l;
481
482 l = strlen(note);
483 for (k = 1; k <= l; k++) {
484 if (note[k-1] == code) {
485 if (k == l || note[k] < '0' || note[k] > '9') {
486 delete1(note, k);
487 return;
488 }
489 }
490 }
491 fatalerror("Code not found in note");
492 }
493
494
495 void insertOctaveCode(Char code, Char *note)
496 { /*if debugMode then writeln('insert ',code,' into ',note); */
497 short l;
498
499 l = strlen(note);
500 if (l < 2 || note[1] < '0' || note[1] > '9')
501 fatalerror("Trying to insert octave into note without duration");
502 if (l <= 2 || note[2] < '0' || note[2] > '9')
503 insertChar(code, note, 3);
504 else
505 printf("Not inserting \"%c\", note already has octave code\"\n", code);
506 }
507
508
509
510
511 /* End. */