Codebase list abcmidi / HEAD position.c
HEAD

Tree @HEAD (Download .tar.gz)

position.c @HEADraw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
 * yaps - program to convert abc files to PostScript.
 * Copyright (C) 1999 James Allwright
 * e-mail: J.R.Allwright@westminster.ac.uk
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 *
 */

/* position.c */
/* part of YAPS - abc to PostScript converter */
/* This file contains routines to calculate symbol positions within */
/* a line of music. */

/* for Microsoft Visual C++ 6.0 and higher */
#ifdef _MSC_VER
#define ANSILIBS
#endif

#include <stdio.h>
#ifdef ANSILIBS
#include <stdlib.h>
#endif
#include "abc.h"
#include "structs.h"
#include "sizes.h"

extern struct tune thetune;
extern double scaledwidth;

static void addfract(f, n, m)
struct fract* f;
int n, m;
/* add n/m to fraction pointed to by f */
/* like addunits(), but does not use unitlength */
{
  f->num = n*f->denom + m*f->num;
  f->denom = m*f->denom;
  reducef(f);
}

static void mulfract(f, n, m)
struct fract* f;
int n, m;
/* multiply  n/m to fraction pointed to by f */
/* like addunits(), but does not use unitlength */
{
  f->num =   n*f->num;
  f->denom = m*f->denom;
  reducef(f);
}

static void advance(struct voice* v, int phase, int* items, double* itemspace, double x)
/* move on one symbol in the specified voice */
{
  struct feature* p;
  struct rest* arest;
  struct note* anote;
  struct fract tuplefactor, notelen;
  int done;
  int stepon;
  int zerotime, newline;

  switch(phase) {
  case 1:
    zerotime = 1;
    newline=0;
    break;
  case 2:
    zerotime = 0;
    newline=0;
    break;
  case 3:
    zerotime = 0;
    newline=1;
    break;
  default:
    printf("Internal error: phase = %d\n", phase);
    exit(1);
    break;
  };

  *itemspace = 0.0;
  *items = 0;
  p = v->place;
  if (p == NULL) {
    v->atlineend = 1;
  };
  done = 0;
  while ((p != NULL) && (done==0)) {
    p->x = (float) (x + p->xleft);
    stepon = 1;
    switch(p->type) {
    case MUSICLINE:
      v->inmusic = 1;
      break;
    case PRINTLINE:
      v->inmusic = 0;
      done = 1;
      if (!newline) {
        v->atlineend = 1;
        stepon = 0;
      };
      break;
    case CLEF:
    case KEY:
    case TIME:
      if (!v->inmusic) {
        break;
      };
    case SINGLE_BAR:
    case DOUBLE_BAR:
    case BAR_REP:
    case REP_BAR:
    case BAR1:
    case REP_BAR2:
    case DOUBLE_REP:
    case THICK_THIN:
    case THIN_THICK:
      *itemspace = *itemspace +  p->xleft + p->xright;
      *items = *items + 1;
      if (!newline) {
        done = 1;
      };
      break;
    case REST:
    case NOTE:
      tuplefactor = v->tuplefactor;
      if ((zerotime==1) && (!v->ingrace)) {
        done = 1;
        stepon = 0;
      } else {
        if ((!v->inchord)&&(!newline)) {
          done = 1;
        };
        *itemspace = *itemspace +  p->xleft + p->xright;
        *items = *items + 1;
        if (p->type == REST) {
          arest = p->item.voidptr;
          addfract(&v->time, arest->len.num, arest->len.denom);
        };
        if ((p->type == NOTE) && (!v->ingrace)) {
          anote = p->item.voidptr;
	  notelen = anote->len;

 	 if (anote->tuplenotes >0) {
		  mulfract(&notelen,tuplefactor.num,tuplefactor.denom);
	           }


          addfract(&v->time, notelen.num, notelen.denom);
/*	  printf("%c %d/%d %d/%d\n",anote->pitch,notelen.num,notelen.denom,
			  v->time.num,v->time.denom);
*/
        };
      };
      break;
    case CHORDON:
      if ((zerotime==1)&&(!v->ingrace)) {
        done = 1;
        stepon = 0;
      } else {
        v->inchord = 1;
      };
      break;
    case CHORDOFF:
      if (v->inchord == 1) {
        v->inchord = 0;
        if ((!v->ingrace)&&(!newline)) {
          done = 1;
        };
      };
      break;
    case GRACEON:
      v->ingrace = 1;
      break;
    case GRACEOFF:
      v->ingrace = 0;
      break;
    default:
      break;
    };
    if (stepon) {
      p = p->next;
    } else {
      done = 1;
    };
  };
  v->place = p;
  if (p == NULL) {
    v->atlineend = 1;
  };
}

static int gefract(struct fract* a, struct fract* b)
/* compare two fractions  a greater than or equal to b */
/* returns   (a >= b)                                  */
{
  if ((a->num*b->denom) >= (b->num*a->denom)) {
    return(1);
  } else {
    return(0);
  };
}

static int gtfract(struct fract* a, struct fract* b)
/* compare two fractions  a greater than b */
/* returns   (a > b)                       */
{
  if ((a->num*b->denom) > (b->num*a->denom)) {
    return(1);
  } else {
    return(0);
  };
}

static int spacemultiline(struct fract* mastertime, struct tune* t)
/* calculate spacing for one line (but possibly multiple voices) */
{
  int i;
  int items, thisitems, maxitems;
  int totalitems;
  double thiswidth, maxwidth;
  double totalwidth;
  double x, gap;
  int done;
  struct voice* v;
  struct fract minlen;

  /* two passes - on the second pass, inter-symbol spacing is */
  /* known so elements can be given their correct x position */
  gap = 0.0;
  for (i=0; i<2; i++) {
    setfract(mastertime, 0, 1);
    v = firstitem(&t->voices);
    while (v != NULL) {
      v->place = v->lineplace;
      v->ingrace = 0;
      v->atlineend = 0;
      setfract(&v->time, 0, 1);
      v = nextitem(&t->voices);
    };
    done = 0;
    items = 0;
    x = 0.0;
    totalitems = 0;
    totalwidth = 0.0;
    /* count up items in a line */
    while (done == 0) {
      maxitems = 0;
      maxwidth = 0.0;
      /* first do zero-time symbols */
      v = firstitem(&t->voices);
      while (v != NULL) {
        if ((!v->atlineend)&&(gefract(mastertime, &v->time))) {
          advance(v, 1, &thisitems, &thiswidth, x);
          if (thisitems > maxitems) {
            maxitems = thisitems;
          };
          if (thiswidth > maxwidth) {
            maxwidth = thiswidth;
          };
        };
        v = nextitem(&t->voices);
      };
      if (maxitems == 0) {
        /* now try moving forward in time */
        /* advance all voices at or before mastertime */
        v = firstitem(&t->voices);
        while (v != NULL) {
          if ((!v->atlineend)&&(gefract(mastertime, &v->time))) {
            advance(v, 2, &thisitems, &thiswidth, x);
            if (thisitems > maxitems) {
              maxitems = thisitems;
            };
            if (thiswidth > maxwidth) {
              maxwidth = thiswidth;
            };
          };
          v = nextitem(&t->voices);
        };
        /* calculate new mastertime */
        v = firstitem(&t->voices);
        setfract(&minlen, 0, 1);
        done = 1;
        while (v != NULL) {
          if (!v->atlineend) {
            done = 0;
            if (minlen.num == 0) {
              setfract(&minlen, v->time.num, v->time.denom);
            } else {
              if (gtfract(&minlen, &v->time)) {
                setfract(&minlen, v->time.num, v->time.denom);
              };
            };
          };
          v = nextitem(&t->voices);
        };
        setfract(mastertime, minlen.num, minlen.denom);
      };
      totalitems = totalitems + maxitems;
      totalwidth = totalwidth + maxwidth;
      if (maxitems > 0) {
        x = x + maxwidth + gap;
      };
    };
    /* now calculate inter-symbol gap */
    if (totalitems > 1) {
      gap = (scaledwidth - totalwidth)/(totalitems-1);
    } else {
      gap = 1.0;
    };
    if (gap < 0.0) {
      event_error("Overfull music line");
    };
    if (gap > MAXGAP) {
      event_error("Underfull music line");
      gap = MAXGAP;
    };
  };
  if (totalitems == 0) {
    return(1);
  } else {
    return(0);
  };
}

void spacevoices(struct tune* t)
{
  struct fract mastertime;
  int donelines;
  struct voice* v;
  int items;
  double x1;

  /* initialize voices */
  v = firstitem(&t->voices);
  while (v != NULL) {
    v->lineplace = v->first;
    v->inmusic=0;
    v = nextitem(&t->voices);
  };
  donelines = 0;
  while(donelines == 0) {
    donelines = spacemultiline(&mastertime, t);
    v = firstitem(&t->voices);
    while (v != NULL) {
      v->lineplace = v->place;
      advance(v, 3, &items, &x1, 0.0);
      v->lineplace = v->place;
      v = nextitem(&t->voices);
    };
  };
}

static int spaceline(struct voice* v)
/* allocate spare space across the width of a single stave line  */
/* thereby fixing the x position of all notes and other elements */
/* returns 0 when the end of the voice is reached, 1 otherwise   */
{
  struct feature* p;
  double x, lastx = 0.0;  /* [SDG] 2020-06-03 */
  int inmusic, items;
  double itemspace;
  double gap;

  itemspace = 0.0;
  items = 0;
  inmusic = 0;
  p = v->place;
  while ((p != NULL) && (p->type != PRINTLINE)) {
    switch(p->type) {
    case MUSICLINE:
      inmusic = 1;
      break;
    case PRINTLINE:
      inmusic = 0;
      break;
    case CLEF:
    case KEY:
    case TIME:
      if (!inmusic) {
        break;
      };
    case SINGLE_BAR:
    case DOUBLE_BAR:
    case BAR_REP:
    case REP_BAR:
    case BAR1:
    case REP_BAR2:
    case DOUBLE_REP:
    case THICK_THIN:
    case THIN_THICK:
    case REST:
    case NOTE:
      itemspace = itemspace +  p->xleft + p->xright;
      items = items + 1;
      break;
    default:
      break;
    };
    p = p->next;
  };
  if (items > 1) {
    gap = (scaledwidth - itemspace)/((double)(items-1));
  } else {
    gap = 1.0;
  };
  if (gap < 0.0) {
    event_error("Overfull music line");
  };
  if (gap > MAXGAP) {
    event_error("Underfull music line");
    gap = MAXGAP;
  };
  /* now assign positions */
  x = 0.0; 
  p = v->place;
  inmusic = 0;
  while ((p != NULL) && (p->type != PRINTLINE)) {
    switch(p->type) {
    case MUSICLINE:
      inmusic = 1;
      break;
    case PRINTLINE:
      inmusic = 0;
      break;
    case CHORDNOTE:
      p->x = (float) lastx;
      break;
    case CLEF:
    case KEY:
    case TIME:
      if (!inmusic) {
        break;
      };
    case SINGLE_BAR:
    case DOUBLE_BAR:
    case BAR_REP:
    case REP_BAR:
    case BAR1:
    case REP_BAR2:
    case DOUBLE_REP:
    case THICK_THIN:
    case THIN_THICK:
    case REST:
    case NOTE:
      x = x + p->xleft;
      p->x = (float) x;
      lastx = x;
      x = x + p->xright + gap;
      break;
    default:
      break;
    };
    p = p->next;
  };
  while ((p!=NULL)&&((p->type == PRINTLINE)||(p->type==LINENUM))) {
    p = p->next;
  };
  v->place = p;
  if (p == NULL) {
    return(0);
  } else {
    return(1);
  };
}

void monospace(struct tune* t)
{
  int doneline;
  struct voice* v;

  v = firstitem(&t->voices);
  while (v != NULL) {
    doneline = 1;
    v->place = v->first;
    while (doneline == 1) {
      doneline = spaceline(v);
    };
    v = nextitem(&t->voices);
  };
}