/
swritestr.c
576 lines (543 loc) · 16.8 KB
/
swritestr.c
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
swritestr.c:
Copyright (C) 2011 John ffitch (after Barry Vercoe)
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
*/
#include "csoundCore.h" /* SWRITESTR.C */
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
#include "corfile.h"
static SRTBLK *nxtins(SRTBLK *), *prvins(SRTBLK *);
static char *pfout(CSOUND *,SRTBLK *, char *, int, int, CORFIL *sco);
static char *nextp(CSOUND *,SRTBLK *, char *, int, int, CORFIL *sco);
static char *prevp(CSOUND *,SRTBLK *, char *, int, int, CORFIL *sco);
static char *ramp(CSOUND *,SRTBLK *, char *, int, int, CORFIL *sco);
static char *expramp(CSOUND *,SRTBLK *, char *, int, int,CORFIL *sco);
static char *randramp(CSOUND *,SRTBLK *, char *, int, int, CORFIL *sco);
static char *pfStr(CSOUND *,char *, int, int, CORFIL *sco);
static char *fpnum(CSOUND *,char *, int, int, CORFIL *sco);
static void fltout(CSOUND *csound, MYFLT n, CORFIL *sco)
{
char *c, buffer[1024];
#ifdef USE_DOUBLE
CS_SPRINTF(buffer, "%la", n);
#else
CS_SPRINTF(buffer, "%a", n);
#endif
/* corfile_puts(buffer, sco); */
for (c = buffer; *c != '\0'; c++)
corfile_putc(*c, sco);
}
/*
The 'first' parameter was added so that the
copies of p2 and p3 are made only in scores
loaded before the first compilation before
performance starts; in this case swritestr()
is called with first = 0;
In the case of scores passed in after Csound
is running, events are scheduled as RT events
through the linevent mechanism (linevent.c)
and in that scenario, cannot contain duplicate
p2 and p3 values. In this case, swritestr() is
called with first = 1;
VL - new in Csound 6.
*/
void swritestr(CSOUND *csound, CORFIL *sco, int first)
{
SRTBLK *bp;
char *p, c, isntAfunc;
int lincnt, pcnt=0;
if (UNLIKELY((bp = csound->frstbp) == NULL))
return;
lincnt = 0;
if ((c = bp->text[0]) != 'w'
&& c != 's' && c != 'e') { /* if no warp stmnt but real data, */
/* create warp-format indicator */
if(first) corfile_puts("w 0 60\n", sco);
lincnt++;
}
nxtlin:
lincnt++; /* now for each line: */
p = bp->text;
c = *p++;
isntAfunc = 1;
switch (c) {
case 'z':
printf("skip z\n");
//corfile_putc('\n', sco);
break;
case 'f':
isntAfunc = 0;
case 'q':
case 'i':
case 'a':
corfile_putc(c, sco);
corfile_putc(*p++, sco);
while ((c = *p++) != SP && c != LF)
corfile_putc(c, sco); /* put p1 */
corfile_putc(c, sco);
if (c == LF)
break;
fltout(csound, bp->p2val, sco); /* put p2val, */
corfile_putc(SP, sco);
if (first) fltout(csound, bp->newp2, sco); /* newp2, */
while ((c = *p++) != SP && c != LF)
;
corfile_putc(c, sco); /* and delim */
if (c == LF)
break;
if (isntAfunc) {
fltout(csound, bp->p3val, sco); /* put p3val, */
corfile_putc(SP, sco);
if (first) fltout(csound, bp->newp3, sco); /* newp3, */
while ((c = *p++) != SP && c != LF)
;
}
else { /*make sure p3s (table length) are ints */
char temp[256];
snprintf(temp,256,"%d ",(int32)bp->p3val); /* put p3val */
fpnum(csound,temp, lincnt, pcnt, sco);
corfile_putc(SP, sco);
if (first) {
snprintf(temp,256,"%d ",(int32)bp->newp3); /* put newp3 */
fpnum(csound,temp, lincnt, pcnt, sco);
}
while ((c = *p++) != SP && c != LF)
;
}
pcnt = 3;
while (c != LF) {
pcnt++;
corfile_putc(SP, sco);
p = pfout(csound,bp,p,lincnt,pcnt, sco); /* now put each pfield */
c = *p++;
}
corfile_putc('\n', sco);
break;
case 's':
case 'e':
if (bp->pcnt > 0) {
char buffer[80];
CS_SPRINTF(buffer, "f 0 %f %f\n", bp->p2val, bp->newp2);
corfile_puts(buffer, sco);
}
corfile_putc(c, sco);
corfile_putc(LF, sco);
break;
case 'w':
case 't':
corfile_putc(c, sco);
while ((c = *p++) != LF) /* put entire line */
corfile_putc(c, sco);
corfile_putc(LF, sco);
break;
case 'y':
case -1:
break;
default:
csound->Message(csound,
Str("swrite: unexpected opcode %c, section %d line %d\n"),
c, csound->sectcnt, lincnt);
break;
}
if ((bp = bp->nxtblk) != NULL)
goto nxtlin;
}
static char *pfout(CSOUND *csound, SRTBLK *bp, char *p,
int lincnt, int pcnt, CORFIL *sco)
{
switch (*p) {
case 'n':
p = nextp(csound, bp,p, lincnt, pcnt, sco);
break;
case 'p':
p = prevp(csound, bp,p, lincnt, pcnt, sco);
break;
case '<':
case '>':
p = ramp(csound, bp,p, lincnt, pcnt, sco);
break;
case '(':
case ')':
p = expramp(csound, bp, p, lincnt, pcnt, sco);
break;
case '~':
p = randramp(csound, bp, p, lincnt, pcnt, sco);
break;
case '"':
p = pfStr(csound, p, lincnt, pcnt, sco);
break;
default:
p = fpnum(csound, p, lincnt, pcnt, sco);
break;
}
return(p);
}
static SRTBLK *nxtins(SRTBLK *bp) /* find nxt note with same p1 */
{
MYFLT p1;
p1 = bp->p1val;
while ((bp = bp->nxtblk) != NULL
&& (bp->p1val != p1 || bp->text[0] != 'i'))
;
return(bp);
}
static SRTBLK *prvins(SRTBLK *bp) /* find prv note with same p1 */
{
MYFLT p1;
p1 = bp->p1val;
while ((bp = bp->prvblk) != NULL
&& (bp->p1val != p1 || bp->text[0] != 'i'))
;
return(bp);
}
static char *nextp(CSOUND *csound, SRTBLK *bp, char *p,
int lincnt, int pcnt, CORFIL *sco)
{
char *q;
int n;
q = p;
p++; /* 1st char */
if (UNLIKELY(*p++ != 'p')) /* 2nd char */
goto error;
n = 999;
if (isdigit(*p))
n = *p++ - '0';
if (isdigit(*p)) /* n is np subscript no */
n = 10*n + (*p++ - '0');
if (UNLIKELY(*p != SP && *p != LF))
goto error;
if (LIKELY((bp = nxtins(bp)) != NULL /* for nxtins, same p1 */
&& n <= bp->pcnt)) {
q = bp->text;
while (n--)
while (*q++ != SP) /* go find the pfield */
;
pfout(csound,bp,q,lincnt,pcnt,sco); /* and put it out */
}
else {
error:
csound->Message(csound,Str("swrite: output, sect%d line%d p%d makes"
" illegal reference to "),
csound->sectcnt,lincnt,pcnt);
while (q < p)
csound->Message(csound,"%c", *q++);
while (*p != SP && *p != LF)
csound->Message(csound,"%c", *p++);
csound->Message(csound,Str(" Zero substituted\n"));
corfile_putc('0', sco);
}
return(p);
}
static char *prevp(CSOUND *csound, SRTBLK *bp, char *p,
int lincnt, int pcnt, CORFIL *sco)
{
char *q;
int n;
q = p;
p++; /* 1st char */
if (UNLIKELY(*p++ != 'p')) /* 2nd char */
goto error;
n = 999;
if (isdigit(*p))
n = *p++ - '0';
if (isdigit(*p)) /* n is np subscript no */
n = 10*n + (*p++ - '0');
if (UNLIKELY(*p != SP && *p != LF))
goto error;
if (LIKELY((bp = prvins(bp)) != NULL /* for prvins, same p1, */
&& n <= bp->pcnt)) {
q = bp->text;
while (n--)
while (*q++ != SP) /* go find the pfield */
;
pfout(csound,bp,q,lincnt,pcnt, sco); /* and put it out */
}
else {
error:
csound->Message(csound,
Str("swrite: output, sect%d line%d p%d makes illegal reference to "),
csound->sectcnt,lincnt,pcnt);
while (q < p)
csound->Message(csound,"%c", *q++);
while (*p != SP && *p != LF)
csound->Message(csound,"%c", *p++);
csound->Message(csound,Str(" Zero substituted\n"));
corfile_putc('0', sco);
}
return(p);
}
static char *ramp(CSOUND *csound, SRTBLK *bp, char *p,
int lincnt, int pcnt, CORFIL *sco)
/* NB np's may reference a ramp but ramps must terminate in valid nums */
{
char *q;
char *psav;
SRTBLK *prvbp, *nxtbp;
MYFLT pval, qval, rval, p2span;
extern MYFLT stof(CSOUND *, char *);
int pnum, n;
psav = ++p;
if (UNLIKELY(*psav != SP && *psav != LF))
goto error1;
pnum = 0;
q = bp->text;
while (q < p)
if (*q++ == SP)
pnum++;
prvbp = bp;
backup:
if (LIKELY((prvbp = prvins(prvbp)) != NULL)) {
p = prvbp->text;
n = pnum;
while (n--)
while (*p++ != SP)
;
if (*p == '>' || *p == '<')
goto backup;
}
else goto error2;
nxtbp = bp;
forwrd:
if (LIKELY((nxtbp = nxtins(nxtbp)) != NULL)) {
q = nxtbp->text;
n = pnum;
while (n--)
while (*q++ != SP)
;
if (*q == '>' || *q == '<')
goto forwrd;
}
else goto error2;
pval = stof(csound, p); /* the error msgs generated by stof */
qval = stof(csound, q); /* are misleading */
if (UNLIKELY((p2span = nxtbp->newp2 - prvbp->newp2) <= 0))
goto error2;
rval = (qval - pval) * (bp->newp2 - prvbp->newp2) / p2span + pval;
fltout(csound, rval, sco);
return(psav);
error1:
csound->Message(csound,
Str("swrite: output, sect%d line%d p%d has illegal ramp symbol\n"),
csound->sectcnt,lincnt,pcnt);
goto put0;
error2:
csound->Message(csound, Str("swrite: output, sect%d line%d p%d ramp "
"has illegal forward or backward ref\n"),
csound->sectcnt, lincnt, pcnt);
put0:
corfile_putc('0', sco);
return(psav);
}
static char *expramp(CSOUND *csound, SRTBLK *bp, char *p,
int lincnt, int pcnt, CORFIL *sco)
/* NB np's may reference a ramp but ramps must terminate in valid nums */
{
char *q;
char *psav;
SRTBLK *prvbp, *nxtbp;
MYFLT pval, qval, rval;
double p2span;
extern MYFLT stof(CSOUND *, char *);
int pnum, n;
psav = ++p;
if (UNLIKELY(*psav != SP && *psav != LF))
goto error1;
pnum = 0;
q = bp->text;
while (q < p)
if (*q++ == SP)
pnum++;
prvbp = bp;
backup:
if (LIKELY((prvbp = prvins(prvbp)) != NULL)) {
p = prvbp->text;
n = pnum;
while (n--)
while (*p++ != SP)
;
if (*p == '}' || *p == '{' || *p == '(' || *p == ')')
goto backup;
}
else goto error2;
nxtbp = bp;
forwrd:
if (LIKELY((nxtbp = nxtins(nxtbp)) != NULL)) {
q = nxtbp->text;
n = pnum;
while (n--)
while (*q++ != SP)
;
if (*q == '}' || *q == '{' || *q == '(' || *q == ')')
goto forwrd;
}
else goto error2;
pval = stof(csound, p); /* the error msgs generated by stof */
qval = stof(csound, q); /* are misleading */
p2span = (double)(nxtbp->newp2 - prvbp->newp2);
/* printf("pval=%f qval=%f span = %f\n", pval, qval, p2span); */
rval = pval * (MYFLT)pow((double)(qval/pval),
(double)(bp->newp2 - prvbp->newp2) / p2span);
/* printf("rval=%f bp->newp2=%f prvbp->newp2-%f\n",
rval, bp->newp2, prvbp->newp2); */
fltout(csound, rval, sco);
return(psav);
error1:
csound->Message(csound,Str("swrite: output, sect%d line%d p%d has illegal"
" expramp symbol\n"),
csound->sectcnt,lincnt,pcnt);
goto put0;
error2:
csound->Message(csound, Str("swrite: output, sect%d line%d p%d expramp "
"has illegal forward or backward ref\n"),
csound->sectcnt, lincnt, pcnt);
put0:
corfile_putc('0', sco);
return(psav);
}
static char *randramp(CSOUND *csound, SRTBLK *bp, char *p,
int lincnt, int pcnt, CORFIL *sco)
/* NB np's may reference a ramp but ramps must terminate in valid nums */
{
char *q;
char *psav;
SRTBLK *prvbp, *nxtbp;
MYFLT pval, qval, rval;
extern MYFLT stof(CSOUND *, char *);
int pnum, n;
psav = ++p;
if (UNLIKELY(*psav != SP && *psav != LF))
goto error1;
pnum = 0;
q = bp->text;
while (q < p)
if (*q++ == SP)
pnum++;
prvbp = bp;
backup:
if (LIKELY((prvbp = prvins(prvbp)) != NULL)) {
p = prvbp->text;
n = pnum;
while (n--)
while (*p++ != SP)
;
if (UNLIKELY(*p == '~'))
goto backup;
}
else goto error2;
nxtbp = bp;
forwrd:
if (LIKELY((nxtbp = nxtins(nxtbp)) != NULL)) {
q = nxtbp->text;
n = pnum;
while (n--)
while (*q++ != SP)
;
if (*q == '~')
goto forwrd;
}
else goto error2;
pval = stof(csound, p); /* the error msgs generated by stof */
qval = stof(csound, q); /* are misleading */
rval = (MYFLT) (((double) (csound->Rand31(&(csound->randSeed1)) - 1)
/ 2147483645.0) * ((double) qval - (double) pval)
+ (double) pval);
fltout(csound, rval, sco);
return(psav);
error1:
csound->Message(csound,Str("swrite: output, sect%d line%d p%d has illegal"
" expramp symbol\n"),
csound->sectcnt,lincnt,pcnt);
goto put0;
error2:
csound->Message(csound,Str("swrite: output, sect%d line%d p%d expramp has"
" illegal forward or backward ref\n"),
csound->sectcnt,lincnt,pcnt);
put0:
corfile_putc('0', sco);
return(psav);
}
static char *pfStr(CSOUND *csound, char *p, int lincnt, int pcnt, CORFIL *sco)
{ /* moves quoted ascii string to SCOREOUT file */
char *q = p; /* with no internal format chk */
corfile_putc(*p++, sco);
while (*p != '"')
corfile_putc(*p++, sco);
corfile_putc(*p++, sco);
if (UNLIKELY(*p != SP && *p != LF)) {
csound->Message(csound, Str("swrite: output, sect%d line%d p%d "
"has illegally terminated string "),
csound->sectcnt, lincnt, pcnt);
while (q < p)
csound->Message(csound,"%c", *q++);
while (*p != SP && *p != LF)
csound->Message(csound,"%c", *p++);
csound->Message(csound,"\n");
}
return(p);
}
static char *fpnum(CSOUND *csound, char *p,
int lincnt, int pcnt, CORFIL *sco) /* moves ascii string */
/* to SCOREOUT file with fpnum format chk */
{
char *q;
int dcnt;
q = p;
if (*p == '+')
p++;
if (*p == '-')
corfile_putc(*p++, sco);
dcnt = 0;
while (isdigit(*p)) {
// printf("*p=%c\n", *p);
corfile_putc(*p++, sco);
dcnt++;
}
// printf("%d:output: %s<<\n", __LINE__, sco);
if (*p == '.')
corfile_putc(*p++, sco);
while (isdigit(*p)) {
corfile_putc(*p++, sco);
dcnt++;
}
// printf("%d:output: %s<<\n", __LINE__, sco);
if (*p == 'E' || *p == 'e') { /* Allow exponential notation */
corfile_putc(*p++, sco);
dcnt++;
if (*p == '+' || *p == '-') {
corfile_putc(*p++, sco);
dcnt++;
}
while (isdigit(*p)) {
corfile_putc(*p++, sco);
dcnt++;
}
}
// printf("%d:output: %s<<\n", __LINE__, sco);
if (UNLIKELY((*p != SP && *p != LF) || !dcnt)) {
csound->Message(csound,Str("swrite: output, sect%d line%d p%d has "
"illegal number "),
csound->sectcnt,lincnt,pcnt);
while (q < p)
csound->Message(csound,"%c", *q++);
while (*p != SP && *p != LF)
csound->Message(csound,"%c", *p++);
csound->Message(csound,Str(" String truncated\n"));
if (!dcnt)
corfile_putc('0', sco);
}
return(p);
}