Debian Codegraph @

utils/autosp/autosp-src/xtuplet.c

raw
1
2 /* Copyright (C) 2014-21 R. D. Tennent School of Computing,
3 * Queen's University, [email protected]
4 *
5 * This program is free software; you can redistribute it
6 * and/or modify it under the terms of the GNU General
7 * Public License as published by the Free Software
8 * Foundation; either version 2 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will
12 * be useful, but WITHOUT ANY WARRANTY; without even the
13 * implied warranty of MERCHANTABILITY or FITNESS FOR A
14 * PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General
18 * Public License along with this program; if not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23 # include "process_command.h"
24
25 PRIVATE
26 void save_state (int i)
27 /* used in process_xtuplet */
28 {
29 beamingi = beaming[i];
30 currenti = current[i];
31 cspacingi = cspacing[i];
32 vspacingi = vspacing[i];
33 first_collectivei = first_collective[i];
34 collectivei[0] = '\0';
35 append (collectivei, NULL, collective[i], SHORT_LEN);
36 }
37
38 PRIVATE
39 void restore_state (int i)
40 /* used in process_xtuplet */
41 {
42 beaming[i] = beamingi;
43 current[i] = currenti;
44 cspacing[i] = cspacingi;
45 vspacing[i] = vspacingi;
46 first_collective[i] = first_collectivei;
47 collective[i][0] = '\0';
48 append (collective[i], NULL, collectivei, SHORT_LEN);
49 }
50
51 PRIVATE
52 void pseudo_output_notes (int i)
53 { /* process notes like output_notes but without outputting */
54 if (vspacing[i] > 0)
55 vspacing[i] = vspacing[i] - spacing;
56 else if (collective[i][0])
57 {
58 first_collective[i] = false;
59 if (*current[i] == '}')
60 { collective[i][0] = '\0';
61 current[i]++;
62 }
63 }
64 }
65
66 void process_xtuplet (void)
67 {
68 int i, xi=0;
69 int xspacing = MAX_SPACING; /* xtuplet total spacing */
70 int normalized_xspacing; /* (xspacing / xtuplet) * (xtuplet - 1); */
71 int xsp;
72 double multnoteskip;
73
74 if (debug)
75 { fprintf (logfile, "\nEntering process_xtuplet:\n");
76 for (i=1; i <= nstaffs; i++)
77 fprintf (logfile, "i=%d xtuplet[i]=%d\n", i, xtuplet[i]);
78 }
79
80 if (old_spacing < MAX_SPACING)
81 { char *s, *t;
82 if (debug) fprintf (logfile, "Terminate current notes command:\n");
83 t = TransformNotes2;
84 while (true)
85 { s = strchr (t, '#');
86 if (s == NULL) break;
87 while (t < s) /* output any initial \transpose etc. */
88 { putc (*t, outfile); t++; }
89 t++; /* skip # */
90 i = atoi (t) - 1; t++;
91 fprintf (outfile, "%s", outstrings[i]);
92 if (debug) fprintf (logfile, "\noutputting %s from outstrings[%d].\n", outstrings[i], i);
93 outstrings[i][0] = '\0';
94 n_outstrings[i] = outstrings[i];
95 if (*t != '\0')
96 { putc (*t, outfile); t++; } /* terminator */
97 }
98 fprintf (outfile, "\\en");
99 if (debug)
100 { fprintf (logfile, "\\en");
101 fprintf (logfile, "\nAfter termination of notes command:\n");
102 status_all ();
103 }
104 }
105
106 if (debug)
107 { fprintf (logfile, "\nDetermine xtuplet duration:\n");
108 fflush (logfile);
109 }
110 for (i=1; i <= nstaffs; i++)
111 if (xtuplet[i] > 1)
112 {
113 xi = i;
114 save_state (xi);
115 pseudo_output_notes (xi);
116 xspacing = spacings[xi];
117 while (xspacing % xtuplet[xi] != 0)
118 {
119 xspacing += spacing_note (xi);
120 pseudo_output_notes (xi);
121 if (xspacing >= MAX_SPACING)
122 error ("Can't determine xtuplet duration.");
123 }
124 restore_state (xi);
125 break;
126 }
127 if (debug) fprintf (logfile, "\nxspacing=%s\n", ps(xspacing));
128 if (debug) fprintf (logfile, "xi=%d xtuplet[xi]=%d\n", xi, xtuplet[xi]);
129 normalized_xspacing = (xspacing / xtuplet[xi]) * (xtuplet[xi] - 1);
130 if (debug)
131 { fprintf (logfile, "normalized_xspacing=%s\n", ps(normalized_xspacing));
132 }
133 spacing = xspacing / xtuplet[xi];
134 if (debug)
135 {
136 fprintf (logfile, "\nDetermine minimal spacing over all active staffs:\n");
137 }
138 for (i=1; i <= nstaffs; i++)
139 if (active[i] && xtuplet[i] == 1)
140 { save_state (i);
141 pseudo_output_notes (i);
142 xsp = spacings[i];
143 if (spacings[i] < spacing)
144 spacing = spacings[i];
145 while (xsp < normalized_xspacing)
146 { int spi = spacing_note (i);
147 pseudo_output_notes (i);
148 xsp += spi;
149 if (spi < spacing)
150 { spacing = spi;
151 if (debug) fprintf (logfile, "for i=%d spi=%s\n", i, ps(spi));
152 }
153 }
154 restore_state (i);
155 }
156 if (debug) fprintf (logfile, "\nxspacing=%s\n", ps(spacing));
157
158 if (debug) fprintf (logfile, "\nCreate a new notes command for the xtuplet:\n");
159 initialize_notes ();
160
161
162 if (debug) fprintf (logfile, "\nProcess non-xtuplet staffs:\n");
163 for (i=1; i <= nstaffs; i++)
164 vspacing[i] = 0;
165 xsp = 0;
166 while (true)
167 {
168 for (i=1; i <= nstaffs; i++)
169 if (active[i] && xtuplet[i] == 1)
170 output_notes (i);
171 xsp += spacing;
172 for (i=1; i <= nstaffs; i++)
173 {
174 /* virtual notes needed? */
175 if (active[i] && xtuplet[i] == 1 && spacings[i] != 0 && spacings[i] != spacing && vspacing[i] == 0)
176 {
177 vspacing[i] = spacings[i] - spacing;
178 }
179 }
180 if (xsp >= normalized_xspacing) break;
181 global_skip = 0;
182 for (i=1; i <= nstaffs; i++)
183 if (active[i] && xtuplet[i] == 1)
184 spacings[i] = spacing_note(i);
185 }
186 if (debug)
187 { fprintf (logfile, "\nAfter processing non-xtuplet staffs:\n");
188 status_all ();
189 }
190 if (debug)
191 {
192 fprintf (logfile, "Generate \\multnoteskip factors.\n");
193 fprintf (logfile, "xi=%d xtuplet[xi]=%d\n", xi, xtuplet[xi]);
194 }
195 multnoteskip = (double) (xtuplet[xi] - 1) / xtuplet[xi];
196 for (i=1; i <= nstaffs; i++)
197 if (xtuplet[i] > 1)
198 {
199 n_outstrings[i] += sprintf (n_outstrings[i], "\\multnoteskip{%5.3f}", multnoteskip);
200 if (debug)
201 { fprintf (logfile, "\noutstrings[%d]=", i);
202 note_segment (outstrings[i]);
203 }
204 }
205 if (debug) fprintf (logfile, "\nRe-process xtuplet staffs:\n");
206 for (i=1; i <= nstaffs; i++)
207 if (xtuplet[i] > 1)
208 {
209 xsp = 0;
210 while (true)
211 { output_notes (i);
212 xsp += spacing;
213 if (spacings[i] != spacing && vspacing[i] == 0)
214 vspacing[i] = spacings[i] - spacing;
215 if (xsp >= xspacing) break;
216 global_skip = 0;
217 spacings[i] = spacing_note (i);
218 }
219 }
220
221 /* Restore normal \noteskip in the xtuplet staffs. */
222 if (debug)
223 {
224 fprintf (logfile, "Restore \\multnoteskip factors.\n");
225 fprintf (logfile, "xi=%d xtuplet[xi]=%d\n", xi, xtuplet[xi]);
226 }
227 multnoteskip = (double) xtuplet[xi] / (xtuplet[xi]-1);
228 for (i=1; i <= nstaffs; i++)
229 if (xtuplet[i] > 1)
230 {
231 n_outstrings[i] += sprintf (n_outstrings[i], "\\multnoteskip{%5.3f}", multnoteskip);
232 xtuplet[i] = 1;
233 }
234 nonvirtual_notes = true;
235 if (debug)
236 { fprintf (logfile, "\nAfter process_xtuplet:\n");
237 status_all ();
238 }
239 return;
240 }