1 /*
2 uggab.c:
3
4 Copyright (C) 1998 Gabriel Maldonado, John ffitch
5
6 This file is part of Csound.
7
8 The Csound Library is free software; you can redistribute it
9 and/or modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 Csound is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with Csound; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 02110-1301 USA
22 */
23
24 /********************************************/
25 /* wrap and mirror UGs by Gabriel Maldonado */
26 /* and others by same author */
27 /* Code adapted by JPff 1998 Sep 19 */
28 /********************************************/
29
30 #include "stdopcod.h"
31 #include "uggab.h"
32 #include <math.h>
33
wrap(CSOUND * csound,WRAP * p)34 static int32_t wrap(CSOUND *csound, WRAP *p)
35 {
36 IGN(csound);
37 MYFLT *adest= p->xdest;
38 MYFLT *asig = p->xsig;
39 MYFLT xlow, xhigh, xsig;
40 uint32_t offset = p->h.insdshead->ksmps_offset;
41 uint32_t early = p->h.insdshead->ksmps_no_end;
42 uint32_t n, nsmps = CS_KSMPS;
43
44 if (UNLIKELY(offset)) memset(adest, '\0', offset*sizeof(MYFLT));
45 if (UNLIKELY(early)) {
46 nsmps -= early;
47 memset(&adest[nsmps], '\0', early*sizeof(MYFLT));
48 }
49 if ((xlow=*p->xlow) >= (xhigh=*p->xhigh)) {
50 MYFLT xaverage;
51 xaverage = (xlow + xhigh) * FL(0.5);
52 for (n=offset; n<nsmps; n++) {
53 adest[n] = xaverage;
54 }
55 }
56 else
57 for (n=offset; n<nsmps; n++) {
58 if ((xsig=asig[n]) >= xlow )
59 adest[n] = xlow + FMOD(xsig - xlow, FABS(xlow-xhigh));
60 else
61 adest[n] = xhigh- FMOD(xhigh- xsig, FABS(xlow-xhigh));
62 }
63 return OK;
64 }
65
kwrap(CSOUND * csound,WRAP * p)66 static int32_t kwrap(CSOUND *csound, WRAP *p)
67 {
68 IGN(csound);
69 MYFLT xsig, xlow, xhigh;
70
71 if ((xlow=*p->xlow) >= (xhigh=*p->xhigh))
72 *p->xdest = (xlow + xhigh)*FL(0.5);
73 else {
74 if ((xsig=*p->xsig) >= xlow )
75 *p->xdest = xlow + FMOD(xsig - xlow, FABS(xlow-xhigh));
76 else
77 *p->xdest = xhigh- FMOD(xhigh- xsig, FABS(xlow-xhigh));
78 }
79 return OK;
80 }
81
82 /*---------------------------------------------------------------------*/
83
kmirror(CSOUND * csound,WRAP * p)84 static int32_t kmirror(CSOUND *csound, WRAP *p)
85 {
86 IGN(csound);
87 MYFLT xsig, xlow, xhigh;
88 xsig = *p->xsig;
89 xhigh= *p->xhigh;
90 xlow = *p->xlow;
91
92 if (xlow >= xhigh) *p->xdest = (xlow + xhigh)*FL(0.5);
93 else {
94 while ((xsig > xhigh) || (xsig < xlow)) {
95 if (xsig > xhigh)
96 xsig = xhigh + xhigh - xsig;
97 else
98 xsig = xlow + xlow - xsig;
99 }
100 *p->xdest = xsig;
101 }
102 return OK;
103 }
104
mirror(CSOUND * csound,WRAP * p)105 static int32_t mirror(CSOUND *csound, WRAP *p)
106 {
107 IGN(csound);
108 MYFLT *adest, *asig;
109 MYFLT xlow, xhigh, xaverage, xsig;
110 uint32_t offset = p->h.insdshead->ksmps_offset;
111 uint32_t early = p->h.insdshead->ksmps_no_end;
112 uint32_t n, nsmps = CS_KSMPS;
113
114 adest = p->xdest;
115 asig = p->xsig;
116 xlow = *p->xlow;
117 xhigh = *p->xhigh;
118
119 if (UNLIKELY(offset)) memset(adest, '\0', offset*sizeof(MYFLT));
120 if (UNLIKELY(early)) {
121 nsmps -= early;
122 memset(&adest[nsmps], '\0', early*sizeof(MYFLT));
123 }
124 if (xlow >= xhigh) {
125 xaverage = (xlow + xhigh)*FL(0.5);
126 for (n=offset;n<nsmps;n++) {
127 adest[n] = xaverage;
128 }
129 return OK; /* Suggested by Istvan Varga */
130 }
131
132 for (n=offset;n<nsmps;n++) {
133 xsig = asig[n];
134 while ((xsig > xhigh) || ( xsig < xlow )) {
135 if (xsig > xhigh)
136 xsig = xhigh + xhigh - xsig;
137 else
138 xsig = xlow + xlow - xsig;
139 }
140 adest[n] = xsig;
141 }
142 return OK;
143 }
144
trig_set(CSOUND * csound,TRIG * p)145 static int32_t trig_set(CSOUND *csound, TRIG *p)
146 {
147 IGN(csound);
148 p->old_sig = FL(0.0);
149 return OK;
150 }
151
trig(CSOUND * csound,TRIG * p)152 static int32_t trig(CSOUND *csound, TRIG *p)
153 {
154 MYFLT sig = *p->ksig;
155 MYFLT threshold = *p->kthreshold;
156
157 switch ((int32_t) MYFLT2LONG(*p->kmode)) {
158 case 0: /* down-up */
159 if (p->old_sig <= threshold &&
160 sig > threshold)
161 *p->kout = FL(1.0);
162 else
163 *p->kout = FL(0.0);
164 break;
165 case 1: /* up-down */
166 if (p->old_sig >= threshold &&
167 sig < threshold)
168 *p->kout = FL(1.0);
169 else
170 *p->kout = FL(0.0);
171 break;
172 case 2: /* both */
173 if ((p->old_sig <= threshold && sig > threshold) ||
174 (p->old_sig >= threshold && sig < threshold ) )
175 *p->kout = FL(1.0);
176 else
177 *p->kout = FL(0.0);
178 break;
179 default:
180 return
181 csound->PerfError(csound, &(p->h),
182 Str(" bad imode value"));
183 }
184 p->old_sig = sig;
185 return OK;
186 }
187
188 /*-------------------------------*/
189
interpol(CSOUND * csound,INTERPOL * p)190 static int32_t interpol(CSOUND *csound, INTERPOL *p)
191 {
192 IGN(csound);
193 MYFLT point_value = (*p->point - *p->imin) / (*p->imax - *p->imin);
194 *p->r = point_value * (*p->val2 - *p->val1) + *p->val1;
195 return OK;
196 }
197
nterpol_init(CSOUND * csound,INTERPOL * p)198 static int32_t nterpol_init(CSOUND *csound, INTERPOL *p)
199 {
200 if (LIKELY(*p->imax != *p->imin))
201 p->point_factor = FL(1.0)/(*p->imax - *p->imin);
202 else
203 return csound->InitError(csound, Str("Min and max the same"));
204 return OK;
205 }
206
knterpol(CSOUND * csound,INTERPOL * p)207 static int32_t knterpol(CSOUND *csound, INTERPOL *p)
208 {
209 IGN(csound);
210 MYFLT point_value = (*p->point - *p->imin ) * p->point_factor;
211 *p->r = point_value * (*p->val2 - *p->val1) + *p->val1;
212 return OK;
213 }
214
anterpol(CSOUND * csound,INTERPOL * p)215 static int32_t anterpol(CSOUND *csound, INTERPOL *p)
216 {
217 IGN(csound);
218 MYFLT point_value = (*p->point - *p->imin ) * p->point_factor;
219 MYFLT *out = p->r, *val1 = p->val1, *val2 = p->val2;
220 uint32_t offset = p->h.insdshead->ksmps_offset;
221 uint32_t early = p->h.insdshead->ksmps_no_end;
222 uint32_t n, nsmps = CS_KSMPS;
223 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
224 if (UNLIKELY(early)) {
225 nsmps -= early;
226 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
227 }
228 for (n=offset; n<nsmps; n++) {
229 MYFLT fv1 = val1[n];
230 out[n] = point_value * (val2[n] - fv1) + fv1;
231 }
232 return OK;
233 }
234
235 /* Oscilators */
236
posc_set(CSOUND * csound,POSC * p)237 static int32_t posc_set(CSOUND *csound, POSC *p)
238 {
239 FUNC *ftp;
240
241 if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->ift)) == NULL))
242 return csound->InitError(csound, Str("table not found in poscil"));
243 p->ftp = ftp;
244 p->tablen = ftp->flen;
245 p->tablenUPsr = p->tablen * csound->onedsr;
246 if (*p->iphs>=FL(0.0))
247 p->phs = *p->iphs * p->tablen;
248 while (UNLIKELY(p->phs >= p->tablen))
249 p->phs -= p->tablen;
250 return OK;
251 }
252
posckk(CSOUND * csound,POSC * p)253 static int32_t posckk(CSOUND *csound, POSC *p)
254 {
255 FUNC *ftp = p->ftp;
256 MYFLT *out = p->out, *ft;
257 MYFLT *curr_samp, fract;
258 double phs = p->phs;
259 double si = *p->freq * p->tablenUPsr; /* gab c3 */
260 uint32_t offset = p->h.insdshead->ksmps_offset;
261 uint32_t early = p->h.insdshead->ksmps_no_end;
262 uint32_t n, nsmps = CS_KSMPS;
263 MYFLT amp = *p->amp;
264
265 if (UNLIKELY(ftp==NULL))
266 return csound->PerfError(csound, &(p->h),
267 Str("poscil: not initialised"));
268 ft = p->ftp->ftable;
269 if (UNLIKELY(early)) nsmps -= early;
270 for (n=offset; n<nsmps; n++) {
271 curr_samp = ft + (int32)phs;
272 fract = (MYFLT)(phs - (int32)phs);
273 out[n] = amp * (*curr_samp +(*(curr_samp+1)-*curr_samp)*fract);
274 phs += si;
275 while (UNLIKELY(phs >= p->tablen))
276 phs -= p->tablen;
277 while (UNLIKELY(phs < 0.0 ))
278 phs += p->tablen;
279 }
280 p->phs = phs;
281 return OK;
282 }
283
poscaa(CSOUND * csound,POSC * p)284 static int32_t poscaa(CSOUND *csound, POSC *p)
285 {
286 FUNC *ftp = p->ftp;
287 MYFLT *out = p->out, *ft = p->ftp->ftable;
288 MYFLT *curr_samp, fract;
289 double phs = p->phs;
290 MYFLT *freq = p->freq;
291 uint32_t offset = p->h.insdshead->ksmps_offset;
292 uint32_t early = p->h.insdshead->ksmps_no_end;
293 uint32_t n, nsmps = CS_KSMPS;
294 MYFLT *amp = p->amp; /*gab c3*/
295
296 if (UNLIKELY(ftp==NULL))
297 return csound->PerfError(csound, &(p->h),
298 Str("poscil: not initialised"));
299 ft = p->ftp->ftable;
300 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
301 if (UNLIKELY(early)) {
302 nsmps -= early;
303 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
304 }
305 for (n=offset; n<nsmps; n++) {
306 MYFLT ff = freq[n];
307 curr_samp = ft + (int32)phs;
308 fract = (MYFLT)(phs - (int32)phs);
309 out[n] = amp[n] *
310 (*curr_samp +(*(curr_samp+1)-*curr_samp)*fract);/* gab c3 */
311 phs += ff * p->tablenUPsr;/* gab c3 */
312 while (UNLIKELY(phs >= p->tablen))
313 phs -= p->tablen;
314 while (UNLIKELY(phs < 0.0) )
315 phs += p->tablen;
316 }
317 p->phs = phs;
318 return OK;
319 }
320
poscka(CSOUND * csound,POSC * p)321 static int32_t poscka(CSOUND *csound, POSC *p)
322 {
323 FUNC *ftp = p->ftp;
324 MYFLT *out = p->out, *ft;
325 MYFLT *curr_samp, fract;
326 double phs = p->phs;
327 uint32_t offset = p->h.insdshead->ksmps_offset;
328 uint32_t early = p->h.insdshead->ksmps_no_end;
329 uint32_t n, nsmps = CS_KSMPS;
330 MYFLT amp = *p->amp;
331 MYFLT *freq = p->freq;
332
333 if (UNLIKELY(ftp==NULL))
334 return csound->PerfError(csound, &(p->h),
335 Str("poscil: not initialised"));
336 ft = p->ftp->ftable;
337 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
338 if (UNLIKELY(early)) {
339 nsmps -= early;
340 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
341 }
342 for (n=offset; n<nsmps; n++) {
343 MYFLT ff = freq[n];
344 curr_samp = ft + (int32)phs;
345 fract = (MYFLT)(phs - (int32)phs);
346 out[n] = amp * (*curr_samp +(*(curr_samp+1)-*curr_samp)*fract);
347 phs += ff * p->tablenUPsr;/* gab c3 */
348 while (UNLIKELY(phs >= p->tablen))
349 phs -= p->tablen;
350 while (UNLIKELY(phs < 0.0 ))
351 phs += p->tablen;
352 }
353 p->phs = phs;
354 return OK;
355 }
356
poscak(CSOUND * csound,POSC * p)357 static int32_t poscak(CSOUND *csound, POSC *p)
358 {
359
360 FUNC *ftp = p->ftp;
361 MYFLT *out = p->out, *ft;
362 MYFLT *curr_samp, fract;
363 double phs = p->phs;
364 double si = *p->freq * p->tablenUPsr;
365 uint32_t offset = p->h.insdshead->ksmps_offset;
366 uint32_t early = p->h.insdshead->ksmps_no_end;
367 uint32_t n, nsmps = CS_KSMPS;
368 MYFLT *amp = p->amp; /*gab c3*/
369
370 if (UNLIKELY(ftp==NULL))
371 return csound->PerfError(csound, &(p->h),
372 Str("poscil: not initialised"));
373 ft = p->ftp->ftable;
374 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
375 if (UNLIKELY(early)) {
376 nsmps -= early;
377 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
378 }
379 for (n=offset; n<nsmps; n++) {
380 curr_samp = ft + (int32)phs;
381 fract = (MYFLT)(phs - (int32)phs);
382 out[n] = amp[n] *
383 (*curr_samp +(*(curr_samp+1)-*curr_samp)*fract);/* gab c3 */
384 phs += si;
385 while (UNLIKELY(phs >= p->tablen))
386 phs -= p->tablen;
387 while (UNLIKELY(phs < 0.0) )
388 phs += p->tablen;
389 }
390 p->phs = phs;
391 return OK;
392 }
393
kposc(CSOUND * csound,POSC * p)394 static int32_t kposc(CSOUND *csound, POSC *p)
395 {
396 IGN(csound);
397 double phs = p->phs;
398 double si = *p->freq * p->tablen * CS_ONEDKR;
399 MYFLT *curr_samp = p->ftp->ftable + (int32)phs;
400 MYFLT fract = (MYFLT)(phs - (double)((int32)phs));
401
402 *p->out = *p->amp * (*curr_samp +(*(curr_samp+1)-*curr_samp)*fract);
403 phs += si;
404 while (UNLIKELY(phs >= p->tablen))
405 phs -= p->tablen;
406 while (UNLIKELY(phs < 0.0))
407 phs += p->tablen;
408 p->phs = phs;
409 return OK;
410 }
411
posc3kk(CSOUND * csound,POSC * p)412 static int32_t posc3kk(CSOUND *csound, POSC *p)
413 {
414 FUNC *ftp = p->ftp;
415 MYFLT *out = p->out, *ftab;
416 MYFLT fract;
417 double phs = p->phs;
418 double si = *p->freq * p->tablen * csound->onedsr;
419 uint32_t offset = p->h.insdshead->ksmps_offset;
420 uint32_t early = p->h.insdshead->ksmps_no_end;
421 uint32_t n, nsmps = CS_KSMPS;
422 MYFLT amp = *p->amp;
423 int32_t x0;
424 MYFLT y0, y1, ym1, y2;
425
426 if (UNLIKELY(ftp==NULL))
427 return csound->PerfError(csound, &(p->h),
428 Str("poscil3: not initialised"));
429 ftab = p->ftp->ftable;
430 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
431 if (UNLIKELY(early)) {
432 nsmps -= early;
433 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
434 }
435 for (n=offset; n<nsmps; n++) {
436 x0 = (int32)phs;
437 fract = (MYFLT)(phs - (double)x0);
438 x0--;
439 if (UNLIKELY(x0<0)) {
440 ym1 = ftab[p->tablen-1]; x0 = 0;
441 }
442 else ym1 = ftab[x0++];
443 y0 = ftab[x0++];
444 y1 = ftab[x0++];
445 if (UNLIKELY(x0>p->tablen)) y2 = ftab[1];
446 else y2 = ftab[x0];
447 {
448 MYFLT frsq = fract*fract;
449 MYFLT frcu = frsq*ym1;
450 MYFLT t1 = y2 + y0+y0+y0;
451 out[n] = amp * (y0 + FL(0.5)*frcu +
452 fract*(y1 - frcu/FL(6.0) - t1/FL(6.0)
453 - ym1/FL(3.0)) +
454 frsq*fract*(t1/FL(6.0) - FL(0.5)*y1) +
455 frsq*(FL(0.5)* y1 - y0));
456 }
457 phs += si;
458 while (UNLIKELY(phs >= p->tablen))
459 phs -= p->tablen;
460 while (UNLIKELY(phs < 0.0) )
461 phs += p->tablen;
462 }
463 p->phs = phs;
464 return OK;
465 }
466
posc3ak(CSOUND * csound,POSC * p)467 static int32_t posc3ak(CSOUND *csound, POSC *p)
468 {
469 FUNC *ftp = p->ftp;
470 MYFLT *out = p->out, *ftab;
471 MYFLT fract;
472 double phs = p->phs;
473 double si = *p->freq * p->tablen * csound->onedsr;
474 uint32_t offset = p->h.insdshead->ksmps_offset;
475 uint32_t early = p->h.insdshead->ksmps_no_end;
476 uint32_t n, nsmps = CS_KSMPS;
477 MYFLT *ampp = p->amp;
478 int32_t x0;
479 MYFLT y0, y1, ym1, y2;
480
481 if (UNLIKELY(ftp==NULL))
482 return csound->PerfError(csound, &(p->h),
483 Str("poscil3: not initialised"));
484 ftab = p->ftp->ftable;
485 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
486 if (UNLIKELY(early)) {
487 nsmps -= early;
488 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
489 }
490 for (n=offset; n<nsmps; n++) {
491 x0 = (int32)phs;
492 fract = (MYFLT)(phs - (double)x0);
493 x0--;
494 if (UNLIKELY(x0<0)) {
495 ym1 = ftab[p->tablen-1]; x0 = 0;
496 }
497 else ym1 = ftab[x0++];
498 y0 = ftab[x0++];
499 y1 = ftab[x0++];
500 if (UNLIKELY(x0>p->tablen)) y2 = ftab[1];
501 else y2 = ftab[x0];
502 {
503 MYFLT frsq = fract*fract;
504 MYFLT frcu = frsq*ym1;
505 MYFLT t1 = y2 + y0+y0+y0;
506 out[n] = ampp[n] * (y0 + FL(0.5)*frcu +
507 fract*(y1 - frcu/FL(6.0) - t1/FL(6.0)
508 - ym1/FL(3.0)) +
509 frsq*fract*(t1/FL(6.0) - FL(0.5)*y1) +
510 frsq*(FL(0.5)* y1 - y0));
511 }
512 phs += si;
513 while (UNLIKELY(phs >= p->tablen))
514 phs -= p->tablen;
515 while (UNLIKELY(phs < 0.0) )
516 phs += p->tablen;
517 }
518 p->phs = phs;
519 return OK;
520 }
521
posc3ka(CSOUND * csound,POSC * p)522 static int32_t posc3ka(CSOUND *csound, POSC *p)
523 {
524 FUNC *ftp = p->ftp;
525 MYFLT *out = p->out, *ftab;
526 MYFLT fract;
527 double phs = p->phs;
528 /*double si = *p->freq * p->tablen * csound->onedsr;*/
529 MYFLT *freq = p->freq;
530 uint32_t offset = p->h.insdshead->ksmps_offset;
531 uint32_t early = p->h.insdshead->ksmps_no_end;
532 uint32_t n, nsmps = CS_KSMPS;
533 MYFLT amp = *p->amp;
534 int32_t x0;
535 MYFLT y0, y1, ym1, y2;
536
537 if (UNLIKELY(ftp==NULL))
538 return csound->PerfError(csound, &(p->h),
539 Str("poscil3: not initialised"));
540 ftab = p->ftp->ftable;
541 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
542 if (UNLIKELY(early)) {
543 nsmps -= early;
544 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
545 }
546 for (n=offset; n<nsmps; n++) {
547 MYFLT ff = freq[n];
548 x0 = (int32)phs;
549 fract = (MYFLT)(phs - (double)x0);
550 x0--;
551 if (UNLIKELY(x0<0)) {
552 ym1 = ftab[p->tablen-1]; x0 = 0;
553 }
554 else ym1 = ftab[x0++];
555 y0 = ftab[x0++];
556 y1 = ftab[x0++];
557 if (UNLIKELY(x0>p->tablen)) y2 = ftab[1];
558 else y2 = ftab[x0];
559 {
560 MYFLT frsq = fract*fract;
561 MYFLT frcu = frsq*ym1;
562 MYFLT t1 = y2 + y0+y0+y0;
563 out[n] = amp * (y0 + FL(0.5)*frcu +
564 fract*(y1 - frcu/FL(6.0) - t1/FL(6.0)
565 - ym1/FL(3.0)) +
566 frsq*fract*(t1/FL(6.0) - FL(0.5)*y1) +
567 frsq*(FL(0.5)* y1 - y0));
568 }
569 phs += ff * p->tablenUPsr;
570 while (UNLIKELY(phs >= p->tablen))
571 phs -= p->tablen;
572 while (UNLIKELY(phs < 0.0) )
573 phs += p->tablen;
574 }
575 p->phs = phs;
576 return OK;
577 }
578
posc3aa(CSOUND * csound,POSC * p)579 static int32_t posc3aa(CSOUND *csound, POSC *p)
580 {
581 FUNC *ftp = p->ftp;
582 MYFLT *out = p->out, *ftab;
583 MYFLT fract;
584 double phs = p->phs;
585 /*double si = *p->freq * p->tablen * csound->onedsr;*/
586 MYFLT *freq = p->freq;
587 uint32_t offset = p->h.insdshead->ksmps_offset;
588 uint32_t early = p->h.insdshead->ksmps_no_end;
589 uint32_t n, nsmps = CS_KSMPS;
590 MYFLT *ampp = p->amp;
591 int32_t x0;
592 MYFLT y0, y1, ym1, y2;
593
594 if (UNLIKELY(ftp==NULL))
595 return csound->PerfError(csound, &(p->h),
596 Str("poscil3: not initialised"));
597 ftab = p->ftp->ftable;
598 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
599 if (UNLIKELY(early)) {
600 nsmps -= early;
601 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
602 }
603 for (n=offset; n<nsmps; n++) {
604 MYFLT ff = freq[n];
605 x0 = (int32)phs;
606 fract = (MYFLT)(phs - (double)x0);
607 x0--;
608 if (UNLIKELY(x0<0)) {
609 ym1 = ftab[p->tablen-1]; x0 = 0;
610 }
611 else ym1 = ftab[x0++];
612 y0 = ftab[x0++];
613 y1 = ftab[x0++];
614 if (UNLIKELY(x0>p->tablen)) y2 = ftab[1];
615 else y2 = ftab[x0];
616 {
617 MYFLT frsq = fract*fract;
618 MYFLT frcu = frsq*ym1;
619 MYFLT t1 = y2 + y0+y0+y0;
620 out[n] = ampp[n] * (y0 + FL(0.5)*frcu +
621 fract*(y1 - frcu/FL(6.0) - t1/FL(6.0)
622 - ym1/FL(3.0)) +
623 frsq*fract*(t1/FL(6.0) - FL(0.5)*y1) +
624 frsq*(FL(0.5)* y1 - y0));
625 phs += ff * p->tablenUPsr;
626 }
627 while (UNLIKELY(phs >= p->tablen))
628 phs -= p->tablen;
629 while (UNLIKELY(phs < 0.0) )
630 phs += p->tablen;
631 }
632 p->phs = phs;
633 return OK;
634 }
635
kposc3(CSOUND * csound,POSC * p)636 static int32_t kposc3(CSOUND *csound, POSC *p)
637 {
638 IGN(csound);
639 double phs = p->phs;
640 double si = *p->freq * p->tablen * CS_ONEDKR;
641 MYFLT *ftab = p->ftp->ftable;
642 int32_t x0 = (int32_t)phs;
643 MYFLT fract = (MYFLT)(phs - (double)x0);
644 MYFLT y0, y1, ym1, y2;
645 MYFLT amp = *p->amp;
646
647 x0--;
648 if (UNLIKELY(x0<0)) {
649 ym1 = ftab[p->tablen-1]; x0 = 0;
650 }
651 else ym1 = ftab[x0++];
652 y0 = ftab[x0++];
653 y1 = ftab[x0++];
654 if (UNLIKELY(x0>p->tablen)) y2 = ftab[1];
655 else y2 = ftab[x0];
656 {
657 MYFLT frsq = fract*fract;
658 MYFLT frcu = frsq*ym1;
659 MYFLT t1 = y2 + y0+y0+y0;
660 *p->out = amp * (y0 + FL(0.5)*frcu +
661 fract*(y1 - frcu/FL(6.0) - t1/FL(6.0)
662 - ym1/FL(3.0)) +
663 frsq*fract*(t1/FL(6.0) - FL(0.5)*y1) +
664 frsq*(FL(0.5)* y1 - y0));
665 }
666 phs += si;
667 while (UNLIKELY(phs >= p->tablen))
668 phs -= p->tablen;
669 while (UNLIKELY(phs < 0.0))
670 phs += p->tablen;
671 p->phs = phs;
672 return OK;
673 }
674
lposc_set(CSOUND * csound,LPOSC * p)675 static int32_t lposc_set(CSOUND *csound, LPOSC *p)
676 {
677 FUNC *ftp;
678 MYFLT loop, end, looplength;
679
680 if (UNLIKELY((ftp = csound->FTnp2Finde(csound, p->ift)) == NULL))
681 return NOTOK;
682 if (UNLIKELY(!(p->fsr=ftp->gen01args.sample_rate))) {
683 csound->Warning(csound, Str("losc: no sample rate stored in function "
684 "assuming=sr\n"));
685 p->fsr=CS_ESR;
686 }
687 p->ftp = ftp;
688 p->tablen = ftp->flen;
689 /* changed from
690 p->phs = *p->iphs * p->tablen; */
691
692 if (UNLIKELY((loop = *p->kloop) < 0)) loop=FL(0.0);
693 if ((end = *p->kend) > p->tablen || end <=0 )
694 end = (MYFLT)p->tablen;
695 looplength = end - loop;
696
697 if (*p->iphs >= 0)
698 p->phs = *p->iphs;
699 while (UNLIKELY(p->phs >= end))
700 p->phs -= looplength;
701 return OK;
702 }
703
lposc(CSOUND * csound,LPOSC * p)704 static int32_t lposc(CSOUND *csound, LPOSC *p)
705 {
706 MYFLT *out = p->out, *ft = p->ftp->ftable;
707 MYFLT *curr_samp, fract;
708 double phs= p->phs, si= *p->freq * (p->fsr*csound->onedsr);
709 uint32_t offset = p->h.insdshead->ksmps_offset;
710 uint32_t early = p->h.insdshead->ksmps_no_end;
711 uint32_t n, nsmps = CS_KSMPS;
712 double loop, end, looplength = p->looplength;
713 MYFLT amp = *p->amp;
714
715 if ((loop = *p->kloop) < 0) loop=0;
716 if ((end = *p->kend) > p->tablen || end <=0 )
717 end = p->tablen;
718 looplength = end - loop;
719
720 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
721 if (UNLIKELY(early)) {
722 nsmps -= early;
723 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
724 }
725 for (n=offset; n<nsmps; n++) {
726 curr_samp = ft + (int32)phs;
727 fract = (MYFLT)(phs - (double)((int32)phs));
728 out[n] = amp * (*curr_samp +(*(curr_samp+1)-*curr_samp)*fract);
729 phs += si;
730 if (phs >= end) phs -= looplength;
731 }
732 p->phs = phs;
733 return OK;
734 }
735
lposc3(CSOUND * csound,LPOSC * p)736 static int32_t lposc3(CSOUND *csound, LPOSC *p)
737 {
738 MYFLT *out = p->out, *ftab = p->ftp->ftable;
739 MYFLT fract;
740 double phs = p->phs, si= *p->freq * (p->fsr*csound->onedsr);
741 uint32_t offset = p->h.insdshead->ksmps_offset;
742 uint32_t early = p->h.insdshead->ksmps_no_end;
743 uint32_t n, nsmps = CS_KSMPS;
744 double loop, end, looplength = p->looplength;
745 MYFLT amp = *p->amp;
746 int32_t x0;
747 MYFLT y0, y1, ym1, y2;
748
749 if (UNLIKELY((loop = *p->kloop) < 0)) loop=0;
750 if ((end = *p->kend) > p->tablen || end <=0 ) end = p->tablen;
751 looplength = end - loop;
752
753 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
754 if (UNLIKELY(early)) {
755 nsmps -= early;
756 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
757 }
758 for (n=offset; n<nsmps; n++) {
759 x0 = (int32)phs;
760 fract = (MYFLT)(phs - (double)x0);
761 x0--;
762 if (x0<0) {
763 ym1 = ftab[p->tablen-1]; x0 = 0;
764 }
765 else ym1 = ftab[x0++];
766 y0 = ftab[x0++];
767 y1 = ftab[x0++];
768 if (x0>p->tablen) y2 = ftab[1]; else y2 = ftab[x0];
769 {
770 MYFLT frsq = fract*fract;
771 MYFLT frcu = frsq*ym1;
772 MYFLT t1 = y2 + y0+y0+y0;
773 out[n] = amp * (y0 + FL(0.5)*frcu +
774 fract*(y1 - frcu/FL(6.0) - t1/FL(6.0)
775 - ym1/FL(3.0)) +
776 frsq*fract*(t1/FL(6.0) - FL(0.5)*y1) +
777 frsq*(FL(0.5)* y1 - y0));
778 }
779 phs += si;
780 while (phs >= end) phs -= looplength;
781 }
782 p->phs = phs;
783 return OK;
784 }
785
sum(CSOUND * csound,SUM * p)786 static int32_t sum(CSOUND *csound, SUM *p)
787 {
788 IGN(csound);
789 uint32_t offset = p->h.insdshead->ksmps_offset;
790 uint32_t early = p->h.insdshead->ksmps_no_end;
791 uint32_t k, nsmps = CS_KSMPS;
792 int32_t count = (int32_t) p->INOCOUNT;
793 MYFLT *ar = p->ar, **args = p->argums;
794 MYFLT *ag = *args;
795 if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
796 if (UNLIKELY(early)) {
797 nsmps -= early;
798 memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
799 }
800 memcpy(&ar[offset], &ag[offset], sizeof(MYFLT)*(nsmps-offset));
801 while (--count) {
802 ag = *(++args); /* over all arguments */
803 for (k=offset; k<nsmps; k++) {
804 ar[k] += ag[k]; /* Over audio vector */
805 }
806 }
807 return OK;
808 }
809
810 /* Actually by JPff but after Gabriel */
product(CSOUND * csound,SUM * p)811 static int32_t product(CSOUND *csound, SUM *p)
812 {
813 IGN(csound);
814 int32_t count = (int32_t) p->INOCOUNT;
815 uint32_t offset = p->h.insdshead->ksmps_offset;
816 uint32_t early = p->h.insdshead->ksmps_no_end;
817 uint32_t k, nsmps = CS_KSMPS;
818 MYFLT *ar = p->ar, **args = p->argums;
819 MYFLT *ag = *args;
820
821 if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
822 if (UNLIKELY(early)) {
823 nsmps -= early;
824 memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
825 }
826 memcpy(&ar[offset], &ag[offset], sizeof(MYFLT)*(nsmps-offset));
827 while (--count) {
828 ag = *(++args); /* over all arguments */
829 for (k=offset; k<nsmps; k++) {
830 ar[k] *= ag[k]; /* Over audio vector */
831 }
832 }
833 return OK;
834 }
835
rsnsety(CSOUND * csound,RESONY * p)836 static int32_t rsnsety(CSOUND *csound, RESONY *p)
837 {
838 int32_t scale;
839 uint32_t nsmps = CS_KSMPS;
840 p->scale = scale = (int32_t) *p->iscl;
841 if ((p->loop = (int32_t) MYFLT2LONG(*p->ord)) < 1)
842 p->loop = 4; /* default value */
843 if (!*p->istor && (p->aux.auxp == NULL ||
844 (uint32_t) (p->loop * 2 * sizeof(MYFLT)) > p->aux.size))
845 csound->AuxAlloc(csound, (size_t) (p->loop * 2 * sizeof(MYFLT)), &p->aux);
846 p->yt1 = (MYFLT*)p->aux.auxp; p->yt2 = (MYFLT*)p->aux.auxp + p->loop;
847 if (UNLIKELY(scale && scale != 1 && scale != 2)) {
848 return csound->InitError(csound, Str("illegal reson iscl value: %f"),
849 *p->iscl);
850 }
851 if (!(*p->istor)) {
852 memset(p->yt1, 0, p->loop*sizeof(MYFLT));
853 memset(p->yt2, 0, p->loop*sizeof(MYFLT));
854 /* for (j = 0; j < p->loop; j++) */
855 /* p->yt1[j] = p->yt2[j] = FL(0.0); */
856 }
857 if (p->buffer.auxp == NULL || p->buffer.size<nsmps*sizeof(MYFLT))
858 csound->AuxAlloc(csound, (size_t)(nsmps*sizeof(MYFLT)), &p->buffer);
859 return OK;
860 }
861
resony(CSOUND * csound,RESONY * p)862 static int32_t resony(CSOUND *csound, RESONY *p)
863 {
864 int32_t j;
865 MYFLT *ar = p->ar, *asig;
866 MYFLT c3p1, c3t4, omc3, c2sqr;
867 MYFLT *yt1, *yt2, c1, c2, c3, cosf;
868 double cf;
869 int32_t loop = p->loop;
870 if (UNLIKELY(loop==0))
871 return csound->InitError(csound, Str("loop cannot be zero"));
872 {
873 MYFLT sep = (*p->sep / (MYFLT) loop);
874 int32_t flag = (int32_t) *p->iflag;
875 MYFLT *buffer = (MYFLT*) (p->buffer.auxp);
876 uint32_t offset = p->h.insdshead->ksmps_offset;
877 uint32_t early = p->h.insdshead->ksmps_no_end;
878 uint32_t n, nsmps = CS_KSMPS;
879
880 asig = p->asig;
881
882 memset(buffer, 0, nsmps*sizeof(MYFLT));
883 if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
884 if (UNLIKELY(early)) {
885 nsmps -= early;
886 memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
887 }
888
889 yt1 = p->yt1;
890 yt2 = p->yt2;
891
892 for (j = 0; j < loop; j++) {
893 if (flag) /* linear separation in hertz */
894 cosf = (MYFLT) cos((cf = (double) (*p->kcf * sep * j))
895 * (double) csound->tpidsr);
896 else /* logarithmic separation in octaves */
897 cosf = (MYFLT) cos((cf = (double) (*p->kcf * pow(2.0, sep * j)))
898 * (double) csound->tpidsr);
899 c3 = EXP(*p->kbw * (cf / *p->kcf) * csound->mtpdsr);
900 c3p1 = c3 + FL(1.0);
901 c3t4 = c3 * FL(4.0);
902 c2 = c3t4 * cosf / c3p1;
903 c2sqr = c2 * c2;
904 omc3 = FL(1.0) - c3;
905 if (p->scale == 1)
906 c1 = omc3 * SQRT(FL(1.0) - c2sqr / c3t4);
907 else if (p->scale == 2)
908 c1 = SQRT((c3p1*c3p1-c2sqr) * omc3/c3p1);
909 else
910 c1 = FL(1.0);
911 for (n = offset; n < nsmps; n++) {
912 MYFLT temp = c1 * asig[n] + c2 * *yt1 - c3 * *yt2;
913 buffer[n] += temp;
914 *yt2 = *yt1;
915 *yt1 = temp;
916 }
917 yt1++;
918 yt2++;
919 }
920 memcpy(&ar[offset], &buffer[offset], sizeof(MYFLT)*(nsmps-offset));
921 return OK;
922 }
923 }
924
fold_set(CSOUND * csound,FOLD * p)925 static int32_t fold_set(CSOUND *csound, FOLD *p)
926 {
927 IGN(csound);
928 p->sample_index = 0;
929 p->index = 0.0;
930 p->value = FL(0.0); /* This was not initialised -- JPff */
931 return OK;
932 }
933
fold(CSOUND * csound,FOLD * p)934 static int32_t fold(CSOUND *csound, FOLD *p)
935 {
936 IGN(csound);
937 uint32_t offset = p->h.insdshead->ksmps_offset;
938 uint32_t early = p->h.insdshead->ksmps_no_end;
939 uint32_t n, nsmps = CS_KSMPS;
940 MYFLT *ar = p->ar;
941 MYFLT *asig = p->asig;
942 MYFLT kincr = *p->kincr;
943 double index = p->index;
944 int32 sample_index = p->sample_index;
945 MYFLT value = p->value;
946 if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
947 if (UNLIKELY(early)) {
948 nsmps -= early;
949 memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
950 }
951 for (n=offset; n<nsmps; n++) {
952 if (index < (double)sample_index) {
953 index += (double)kincr;
954 ar[n] = value = asig[n];
955 }
956 else ar[n]= value;
957 sample_index++;
958 }
959 p->index = index;
960 p->sample_index = sample_index;
961 p->value = value;
962 return OK;
963 }
964
965 /* by Gab Maldonado. Under GNU license with a special exception for
966 Canonical Csound addition */
967
loopseg_set(CSOUND * csound,LOOPSEG * p)968 static int32_t loopseg_set(CSOUND *csound, LOOPSEG *p)
969 {
970 p->nsegs = p->INOCOUNT-3;
971 // Should check this is even
972 if (UNLIKELY((p->nsegs&1)!=0))
973 csound->Warning(csound, Str("loop opcode: wrong argument count"));
974 p->args[0] = FL(0.0);
975 p->phs = *p->iphase;
976 return OK;
977 }
978
loopseg(CSOUND * csound,LOOPSEG * p)979 static int32_t loopseg(CSOUND *csound, LOOPSEG *p)
980 {
981 IGN(csound);
982 MYFLT *argp=p->args;
983 MYFLT beg_seg=FL(0.0), end_seg, durtot=FL(0.0);
984 double phs, si=*p->freq*CS_ONEDKR;
985 int32_t nsegs=p->nsegs+1;
986 int32_t j;
987 if (*p->retrig)
988 phs=p->phs=*p->iphase;
989 else
990 phs=p->phs;
991
992 for (j=1; j<nsegs; j++)
993 argp[j] = *p->argums[j-1];
994
995 argp[nsegs] = *p->argums[0];
996
997 for ( j=0; j <nsegs; j+=2)
998 durtot += argp[j];
999 for ( j=0; j < nsegs; j+=2) {
1000 beg_seg += argp[j] / durtot;
1001 end_seg = beg_seg + argp[j+2] / durtot;
1002
1003 if (beg_seg <= phs && end_seg > phs) {
1004 MYFLT diff = end_seg - beg_seg;
1005 MYFLT fract = ((MYFLT)phs-beg_seg)/diff;
1006 MYFLT v1 = argp[j+1];
1007 MYFLT v2 = argp[j+3];
1008 *p->out = v1 + (v2-v1) * fract;
1009 break;
1010 }
1011 }
1012 phs += si;
1013 while (phs >= 1.0)
1014 phs -= 1.0;
1015 while (phs < 0.0 )
1016 phs += 1.0;
1017 p->phs = phs;
1018 return OK;
1019 }
1020
loopxseg(CSOUND * csound,LOOPSEG * p)1021 static int32_t loopxseg(CSOUND *csound, LOOPSEG *p)
1022 {
1023 IGN(csound);
1024 MYFLT exp1 = FL(1.0)/(FL(1.0)-EXP(FL(1.0)));
1025 MYFLT *argp=p->args;
1026 MYFLT beg_seg=FL(0.0), end_seg, durtot=FL(0.0);
1027 double phs, si=*p->freq*CS_ONEDKR;
1028 int32_t nsegs=p->nsegs+1;
1029 int32_t j;
1030 if (*p->retrig)
1031 phs=p->phs=*p->iphase;
1032 else
1033 phs=p->phs;
1034
1035 for (j=1; j<nsegs; j++)
1036 argp[j] = *p->argums[j-1];
1037
1038 argp[nsegs] = *p->argums[0];
1039
1040 for ( j=0; j <nsegs; j+=2)
1041 durtot += argp[j];
1042 for ( j=0; j < nsegs; j+=2) {
1043 beg_seg += argp[j] / durtot;
1044 end_seg = beg_seg + argp[j+2] / durtot;
1045
1046 if (beg_seg <= phs && end_seg > phs) {
1047 MYFLT diff = end_seg - beg_seg;
1048 MYFLT fract = ((MYFLT)phs-beg_seg)/diff;
1049 MYFLT v1 = argp[j+1];
1050 MYFLT v2 = argp[j+3];
1051 *p->out = v1 + (v2 - v1) * (1 - EXP(fract)) * exp1;
1052 break;
1053 }
1054 }
1055 phs += si;
1056 while (phs >= 1.0)
1057 phs -= 1.0;
1058 while (phs < 0.0 )
1059 phs += 1.0;
1060 p->phs = phs;
1061 return OK;
1062 }
1063
looptseg_set(CSOUND * csound,LOOPTSEG * p)1064 static int32_t looptseg_set(CSOUND *csound, LOOPTSEG *p)
1065 {
1066 IGN(csound);
1067 p->nsegs = (p->INOCOUNT-2)/3;
1068 p->phs = *p->iphase;
1069 return OK;
1070 }
1071
looptseg(CSOUND * csound,LOOPTSEG * p)1072 static int32_t looptseg(CSOUND *csound, LOOPTSEG *p)
1073 {
1074 IGN(csound);
1075 MYFLT beg_seg=FL(0.0), end_seg=FL(0.0), durtot=FL(0.0);
1076 double phs, si=*p->freq*CS_ONEDKR;
1077 int32_t nsegs=p->nsegs;
1078 int32_t j;
1079
1080 if (*p->retrig)
1081 phs=p->phs=*p->iphase;
1082 else
1083 phs=p->phs;
1084
1085 for ( j=0; j<nsegs; j++)
1086 durtot += *(p->argums[j].time);
1087 for ( j=0; j < nsegs; j++) {
1088 beg_seg = end_seg;
1089 end_seg = beg_seg + *(p->argums[j].time) / durtot;
1090 if (beg_seg <= phs && end_seg > phs) {
1091 MYFLT alpha = *(p->argums[j].type);
1092 MYFLT diff = end_seg - beg_seg;
1093 MYFLT fract = ((MYFLT)phs-beg_seg)/diff;
1094 MYFLT v1 = *(p->argums[j].start);
1095 MYFLT v2 = (j!=nsegs-1)?*(p->argums[j+1].start):*(p->argums[0].start);
1096 if (alpha==FL(0.0))
1097 *p->out = v1 + (v2 - v1) * fract;
1098 else
1099 *p->out = v1 +
1100 (v2 - v1) * (FL(1.0)-EXP(alpha*fract))/(FL(1.0)-EXP(alpha));
1101 break;
1102 }
1103 }
1104 phs += si;
1105 while (UNLIKELY(phs >= 1.0))
1106 phs -= 1.0;
1107 while (UNLIKELY(phs < 0.0 ))
1108 phs += 1.0;
1109 p->phs = phs;
1110 return OK;
1111 }
1112
lpshold(CSOUND * csound,LOOPSEG * p)1113 static int32_t lpshold(CSOUND *csound, LOOPSEG *p)
1114 {
1115 IGN(csound);
1116 MYFLT *argp=p->args;
1117 MYFLT beg_seg=0, end_seg, durtot=FL(0.0);
1118 double phs, si=*p->freq*CS_ONEDKR;
1119 int32_t nsegs=p->nsegs+1;
1120 int32_t j;
1121
1122 if (*p->retrig)
1123 phs=p->phs=*p->iphase;
1124 else
1125 phs=p->phs;
1126
1127 for (j=1; j<nsegs; j++)
1128 argp[j] = *p->argums[j-1];
1129 argp[nsegs] = *p->argums[0];
1130 for ( j=0; j <nsegs; j+=2)
1131 durtot += argp[j];
1132
1133 for ( j=0; j < nsegs; j+=2) {
1134 beg_seg += argp[j] / durtot;
1135 end_seg = beg_seg + argp[j+2] / durtot;
1136 if (beg_seg <= phs && end_seg > phs) {
1137 if (beg_seg <= phs && end_seg > phs) {
1138 *p->out = argp[j+1];
1139 break;
1140 }
1141 }
1142 }
1143 phs += si;
1144 while (phs >= 1.0)
1145 phs -= 1.0;
1146 while (phs < 0.0 )
1147 phs += 1.0;
1148 p->phs = phs;
1149 return OK;
1150 }
1151
loopsegp_set(CSOUND * csound,LOOPSEGP * p)1152 static int32_t loopsegp_set(CSOUND *csound, LOOPSEGP *p)
1153 {
1154 IGN(csound);
1155 p->nsegs = p->INOCOUNT-1;
1156 p->args[0] = FL(0.0);
1157 return OK;
1158 }
1159
loopsegp(CSOUND * csound,LOOPSEGP * p)1160 static int32_t loopsegp(CSOUND *csound, LOOPSEGP *p)
1161 {
1162 IGN(csound);
1163 MYFLT *argp = p->args;
1164 MYFLT beg_seg=0, end_seg, durtot=FL(0.0);
1165 MYFLT phs;
1166 int32_t nsegs=p->nsegs+1;
1167 int32_t j;
1168
1169 phs = *p->kphase;
1170
1171 while (phs >= FL(1.0))
1172 phs -= FL(1.0);
1173 while (phs < FL(0.0))
1174 phs += FL(1.0);
1175
1176 for (j=1; j<nsegs; j++)
1177 argp[j] = *p->argums[j-1];
1178
1179 argp[nsegs] = *p->argums[0];
1180
1181 for ( j=0; j <nsegs; j+=2)
1182 durtot += argp[j];
1183 for ( j=0; j < nsegs; j+=2) {
1184 beg_seg += argp[j] / durtot;
1185 end_seg = beg_seg + argp[j+2] / durtot;
1186
1187 if (beg_seg <= phs && end_seg > phs) {
1188 MYFLT diff = end_seg - beg_seg;
1189 MYFLT fract = ((MYFLT)phs-beg_seg)/diff;
1190 MYFLT v1 = argp[j+1];
1191 MYFLT v2 = argp[j+3];
1192 *p->out = v1 + (v2-v1) * fract;
1193 break;
1194 }
1195 }
1196 return OK;
1197 }
1198
lpsholdp(CSOUND * csound,LOOPSEGP * p)1199 static int32_t lpsholdp(CSOUND *csound, LOOPSEGP *p)
1200 {
1201 IGN(csound);
1202 MYFLT *argp=p->args;
1203 MYFLT beg_seg=FL(0.0), end_seg, durtot=FL(0.0);
1204 MYFLT phs;
1205 int32_t nsegs=p->nsegs+1;
1206 int32_t j;
1207
1208 phs = *p->kphase;
1209
1210 while (phs >= FL(1.0))
1211 phs -= FL(1.0);
1212 while (phs < FL(0.0))
1213 phs += FL(1.0);
1214
1215 for (j=1; j<nsegs; j++)
1216 argp[j] = *p->argums[j-1];
1217
1218 argp[nsegs] = *p->argums[0];
1219
1220 for ( j=0; j <nsegs; j+=2)
1221 durtot += argp[j];
1222 for ( j=0; j < nsegs; j+=2) {
1223 beg_seg += argp[j] / durtot;
1224 end_seg = beg_seg + argp[j+2] / durtot;
1225
1226 if (beg_seg <= phs && end_seg > phs) {
1227 if (beg_seg <= phs && end_seg > phs) {
1228 *p->out = argp[j+1];
1229 break;
1230 }
1231 }
1232 }
1233 return OK;
1234 }
1235
1236 /* by Gab Maldonado. Under GNU license with a special exception
1237 for Canonical Csound addition */
1238
lineto_set(CSOUND * csound,LINETO * p)1239 static int32_t lineto_set(CSOUND *csound, LINETO *p)
1240 {
1241 IGN(csound);
1242 p->current_time = FL(0.0);
1243 p->incr=FL(0.0);
1244 p->old_time=FL(0.0);
1245 p->flag = 1;
1246 return OK;
1247 }
1248
lineto(CSOUND * csound,LINETO * p)1249 static int32_t lineto(CSOUND *csound, LINETO *p)
1250 {
1251 IGN(csound);
1252 if (UNLIKELY(p->flag)) {
1253 p->val_incremented = p->current_val = *p->ksig;
1254 p->flag=0;
1255 }
1256 /* printf("lineto: ktime=%lf ksig=%lf\n " */
1257 /* "old_time=%lf val_inc=%lf incr=%lf val=%lf\n", */
1258 /* *p->ktime, *p->ksig, p->old_time, p->val_incremented, p->incr, */
1259 /* p->current_val); */
1260 if (*p->ksig != p->current_val && p->current_time > p->old_time) {
1261 p->old_time = *p->ktime;
1262 p->val_incremented = p->current_val;
1263 p->current_time = FL(0.0);
1264 p->incr = (*p->ksig - p->current_val)
1265 / ((int32) (CS_EKR * p->old_time) -1); /* by experiment */
1266 p->current_val = *p->ksig;
1267 }
1268 else if (p->current_time < p->old_time) {
1269 p->val_incremented += p->incr;
1270 }
1271 p->current_time += 1/CS_EKR;
1272 *p->kr = p->val_incremented;
1273 return OK;
1274 }
1275
tlineto_set(CSOUND * csound,LINETO2 * p)1276 static int32_t tlineto_set(CSOUND *csound, LINETO2 *p)
1277 {
1278 IGN(csound);
1279 p->current_time = FL(0.0);
1280 p->incr=FL(0.0);
1281 p->old_time=FL(1.0);
1282 p->flag = 1;
1283 return OK;
1284 }
1285
tlineto(CSOUND * csound,LINETO2 * p)1286 static int32_t tlineto(CSOUND *csound, LINETO2 *p)
1287 {
1288 IGN(csound);
1289 if (UNLIKELY(p->flag)) {
1290 p->val_incremented = p->current_val = *p->ksig;
1291 p->flag=0;
1292 }
1293 if (*p->ktrig) {
1294 p->old_time = *p->ktime;
1295 /* p->val_incremented = p->current_val; */
1296 p->current_time = FL(0.0);
1297 p->incr = (*p->ksig - p->current_val)
1298 / ((int32) (CS_EKR * p->old_time) + 1);
1299 p->current_val = *p->ksig;
1300 }
1301 else if (p->current_time < p->old_time) {
1302 p->current_time += CS_ONEDKR;
1303 p->val_incremented += p->incr;
1304 }
1305 *p->kr = p->val_incremented;
1306 return OK;
1307 }
1308
1309 /* by Gabriel Maldonado. Under GNU license with a special exception
1310 for Canonical Csound addition */
1311
vibrato_set(CSOUND * csound,VIBRATO * p)1312 static int32_t vibrato_set(CSOUND *csound, VIBRATO *p)
1313 {
1314 FUNC *ftp;
1315
1316 if (LIKELY((ftp = csound->FTnp2Finde(csound, p->ifn)) != NULL)) {
1317 p->ftp = ftp;
1318 if (*p->iphs >= 0 && *p->iphs<1.0)
1319 p->lphs = (((int64_t)(*p->iphs * FMAXLEN)) & PHMASK) >> ftp->lobits;
1320 else if (UNLIKELY(*p->iphs>=1.0))
1321 return csound->InitError(csound, Str("vibrato@ Phase out of range"));
1322 }
1323 else return NOTOK;
1324 p->xcpsAmpRate = randGab *(*p->cpsMaxRate - *p->cpsMinRate) +
1325 *p->cpsMinRate;
1326 p->xcpsFreqRate = randGab *(*p->ampMaxRate - *p->ampMinRate) +
1327 *p->ampMinRate;
1328 p->tablen = ftp->flen;
1329 p->tablenUPkr = p->tablen * CS_ONEDKR;
1330 return OK;
1331 }
1332
vibrato(CSOUND * csound,VIBRATO * p)1333 static int32_t vibrato(CSOUND *csound, VIBRATO *p)
1334 {
1335 FUNC *ftp;
1336 double phs, inc;
1337 MYFLT *ftab, fract, v1;
1338 MYFLT RandAmountAmp,RandAmountFreq;
1339
1340 RandAmountAmp = (p->num1amp + (MYFLT)p->phsAmpRate * p->dfdmaxAmp) *
1341 *p->randAmountAmp ;
1342 RandAmountFreq = (p->num1freq + (MYFLT)p->phsFreqRate * p->dfdmaxFreq) *
1343 *p->randAmountFreq ;
1344
1345 phs = p->lphs;
1346 ftp = p->ftp;
1347 if (UNLIKELY(ftp==NULL)) goto err1;
1348 fract = (MYFLT) (phs - (int32)phs);
1349 ftab = ftp->ftable + (int32)phs;
1350 v1 = *ftab++;
1351 *p->out = (v1 + (*ftab - v1) * fract) *
1352 (*p->AverageAmp * POWER(FL(2.0),RandAmountAmp));
1353 inc = ( *p->AverageFreq * POWER(FL(2.0),RandAmountFreq)) * p->tablenUPkr;
1354 phs += inc;
1355 while (phs >= p->tablen)
1356 phs -= p->tablen;
1357 while (phs < 0.0 )
1358 phs += p->tablen;
1359 p->lphs = phs;
1360 p->phsAmpRate += (int32)(p->xcpsAmpRate * CS_KICVT);
1361 if (p->phsAmpRate >= MAXLEN) {
1362 p->xcpsAmpRate = randGab * (*p->ampMaxRate - *p->ampMinRate) +
1363 *p->ampMinRate;
1364 p->phsAmpRate &= PHMASK;
1365 p->num1amp = p->num2amp;
1366 p->num2amp = BiRandGab ;
1367 p->dfdmaxAmp = (p->num2amp - p->num1amp) / FMAXLEN;
1368 }
1369 p->phsFreqRate += (int32)(p->xcpsFreqRate * CS_KICVT);
1370 if (p->phsFreqRate >= MAXLEN) {
1371 p->xcpsFreqRate = randGab * (*p->cpsMaxRate - *p->cpsMinRate) +
1372 *p->cpsMinRate;
1373 p->phsFreqRate &= PHMASK;
1374 p->num1freq = p->num2freq;
1375 p->num2freq = BiRandGab ;
1376 p->dfdmaxFreq = (p->num2freq - p->num1freq) / FMAXLEN;
1377 }
1378 return OK;
1379 err1:
1380 return csound->PerfError(csound, &(p->h),
1381 Str("vibrato(krate): not initialised"));
1382 }
1383
vibr_set(CSOUND * csound,VIBR * p)1384 static int32_t vibr_set(CSOUND *csound, VIBR *p)
1385 /* faster and easier to use than vibrato, but less flexible */
1386 {
1387 FUNC *ftp;
1388 #define randAmountAmp FL(1.59055) /* these default values are far from */
1389 #define randAmountFreq FL(0.629921) /* being the best. If you think you */
1390 #define ampMinRate FL(1.0) /* found better ones, please tell me */
1391 #define ampMaxRate FL(3.0) /* by posting a message to
1392 g.maldonado@agora.stm.it */
1393 #define cpsMinRate FL(1.19377)
1394 #define cpsMaxRate FL(2.28100)
1395 #define iphs FL(0.0)
1396
1397 if (LIKELY((ftp = csound->FTnp2Finde(csound, p->ifn)) != NULL)) {
1398 p->ftp = ftp;
1399 p->lphs = (((int32)(iphs * FMAXLEN)) & PHMASK) >> ftp->lobits;
1400 }
1401 else return NOTOK;
1402 p->xcpsAmpRate = randGab * (cpsMaxRate - cpsMinRate) + cpsMinRate;
1403 p->xcpsFreqRate = randGab * (ampMaxRate - ampMinRate) + ampMinRate;
1404 p->tablen = ftp->flen;
1405 p->tablenUPkr = p->tablen * CS_ONEDKR;
1406 return OK;
1407 }
1408
vibr(CSOUND * csound,VIBR * p)1409 static int32_t vibr(CSOUND *csound, VIBR *p)
1410 {
1411 FUNC *ftp;
1412 double phs, inc;
1413 MYFLT *ftab, fract, v1;
1414 MYFLT rAmountAmp,rAmountFreq;
1415
1416 rAmountAmp =
1417 (p->num1amp+(MYFLT)p->phsAmpRate * p->dfdmaxAmp)*randAmountAmp;
1418 rAmountFreq =
1419 (p->num1freq+(MYFLT)p->phsFreqRate*p->dfdmaxFreq)*randAmountFreq;
1420 phs = p->lphs;
1421 ftp = p->ftp;
1422 if (UNLIKELY(ftp==NULL)) {
1423 return csound->PerfError(csound, &(p->h),
1424 Str("vibrato(krate): not initialised"));
1425 }
1426 fract = (MYFLT) (phs - (int32)phs); /*PFRAC(phs);*/
1427 ftab = ftp->ftable + (int32)phs; /*(phs >> ftp->lobits);*/
1428 v1 = *ftab++;
1429 *p->out = (v1 + (*ftab - v1) * fract) *
1430 (*p->AverageAmp * POWER(FL(2.0),rAmountAmp));
1431 inc = ( *p->AverageFreq * POWER(FL(2.0),rAmountFreq) ) * p->tablenUPkr;
1432 phs += inc;
1433 while (phs >= p->tablen)
1434 phs -= p->tablen;
1435 while (phs < 0.0 )
1436 phs += p->tablen;
1437 p->lphs = phs;
1438
1439 p->phsAmpRate += (int32)(p->xcpsAmpRate * CS_KICVT);
1440 if (p->phsAmpRate >= MAXLEN) {
1441 p->xcpsAmpRate = randGab * (ampMaxRate - ampMinRate) + ampMinRate;
1442 p->phsAmpRate &= PHMASK;
1443 p->num1amp = p->num2amp;
1444 p->num2amp = BiRandGab;
1445 p->dfdmaxAmp = (p->num2amp - p->num1amp) / FMAXLEN;
1446 }
1447
1448 p->phsFreqRate += (int32)(p->xcpsFreqRate * CS_KICVT);
1449 if (p->phsFreqRate >= MAXLEN) {
1450 p->xcpsFreqRate = randGab * (cpsMaxRate - cpsMinRate) + cpsMinRate;
1451 p->phsFreqRate &= PHMASK;
1452 p->num1freq = p->num2freq;
1453 p->num2freq = BiRandGab;
1454 p->dfdmaxFreq = (p->num2freq - p->num1freq) / FMAXLEN;
1455 }
1456 #undef randAmountAmp
1457 #undef randAmountFreq
1458 #undef ampMinRate
1459 #undef ampMaxRate
1460 #undef cpsMinRate
1461 #undef cpsMaxRate
1462 #undef iphs
1463 return OK;
1464 }
1465
jitter2_set(CSOUND * csound,JITTER2 * p)1466 static int32_t jitter2_set(CSOUND *csound, JITTER2 *p)
1467 {
1468 if (*p->cps1==FL(0.0) && *p->cps2==FL(0.0) && /* accept default values */
1469 *p->cps2==FL(0.0) && *p->amp1==FL(0.0) &&
1470 *p->amp2==FL(0.0) && *p->amp3==FL(0.0))
1471 p->flag = 1;
1472 else
1473 p->flag = 0;
1474 p->dfdmax1 = p->dfdmax2 = p->dfdmax3 = FL(0.0);
1475 p->phs1 = p->phs2 = p->phs3 = 0;
1476 p->num1a = p->num1b = p->num1c = FL(0.0); /* JPff Jul 2016 */
1477 if (*p->option != FL(0.0)) {
1478 p->num1a = p->num2a;
1479 p->num2a = BiRandGab;
1480 p->dfdmax1 = (p->num2a - p->num1a) / FMAXLEN;
1481 p->num1b = p->num2b;
1482 p->num2b = BiRandGab;
1483 p->dfdmax2 = (p->num2b- p->num1b) / FMAXLEN;
1484 p->num1c = p->num2c;
1485 p->num2c = BiRandGab;
1486 p->dfdmax3 = (p->num2c- p->num1c) / FMAXLEN;
1487 }
1488 return OK;
1489 }
1490
jitter2(CSOUND * csound,JITTER2 * p)1491 static int32_t jitter2(CSOUND *csound, JITTER2 *p)
1492 {
1493 MYFLT out1,out2,out3;
1494 out1 = (p->num1a + (MYFLT)p->phs1 * p->dfdmax1);
1495 out2 = (p->num1b + (MYFLT)p->phs2 * p->dfdmax2);
1496 out3 = (p->num1c + (MYFLT)p->phs3 * p->dfdmax3);
1497
1498 if (p->flag) { /* accept default values */
1499 *p->out = (out1* FL(0.5) + out2 * FL(0.3) + out3* FL(0.2)) * *p->gamp;
1500 p->phs1 += (int32) (FL(0.82071231913) * CS_KICVT);
1501 p->phs2 += (int32) (FL(7.009019029039107) * CS_KICVT);
1502 p->phs3 += (int32) (FL(10.0) * CS_KICVT);
1503 }
1504 else {
1505 *p->out = (out1* *p->amp1 + out2* *p->amp2 +out3* *p->amp3) * *p->gamp;
1506 p->phs1 += (int32)( *p->cps1 * CS_KICVT);
1507 p->phs2 += (int32)( *p->cps2 * CS_KICVT);
1508 p->phs3 += (int32)( *p->cps3 * CS_KICVT);
1509 }
1510 if (p->phs1 >= MAXLEN) {
1511 p->phs1 &= PHMASK;
1512 p->num1a = p->num2a;
1513 p->num2a = BiRandGab;
1514 p->dfdmax1 = (p->num2a - p->num1a) / FMAXLEN;
1515 }
1516 if (p->phs2 >= MAXLEN) {
1517 p->phs2 &= PHMASK;
1518 p->num1b = p->num2b;
1519 p->num2b = BiRandGab;
1520 p->dfdmax2 = (p->num2b - p->num1b) / FMAXLEN;
1521 }
1522 if (p->phs3 >= MAXLEN) {
1523 p->phs3 &= PHMASK;
1524 p->num1c = p->num2c;
1525 p->num2c = BiRandGab;
1526 p->dfdmax3 = (p->num2c - p->num1c) / FMAXLEN;
1527 }
1528 return OK;
1529 }
1530
jitter_set(CSOUND * csound,JITTER * p)1531 static int32_t jitter_set(CSOUND *csound, JITTER *p)
1532 {
1533 p->num2 = BiRandGab;
1534 p->initflag = 1;
1535 p->phs=0;
1536 return OK;
1537 }
1538
jitter(CSOUND * csound,JITTER * p)1539 static int32_t jitter(CSOUND *csound, JITTER *p)
1540 {
1541 if (p->initflag) {
1542 p->initflag = 0;
1543 *p->ar = p->num2 * *p->amp;
1544 goto next;
1545 }
1546 *p->ar = (p->num1 + (MYFLT)p->phs * p->dfdmax) * *p->amp;
1547 p->phs += (int32)(p->xcps * CS_KICVT);
1548
1549 if (p->phs >= MAXLEN) {
1550 next:
1551 p->xcps = randGab * (*p->cpsMax - *p->cpsMin) + *p->cpsMin;
1552 p->phs &= PHMASK;
1553 p->num1 = p->num2;
1554 p->num2 = BiRandGab;
1555 p->dfdmax = (p->num2 - p->num1) / FMAXLEN;
1556 }
1557 return OK;
1558 }
1559
jitters_set(CSOUND * csound,JITTERS * p)1560 static int32_t jitters_set(CSOUND *csound, JITTERS *p)
1561 {
1562 p->num1 = BiRandGab;
1563 p->num2 = BiRandGab;
1564 p->df1 = FL(0.0);
1565 p->initflag = 1;
1566 p->cod = IS_ASIG_ARG(p->amp) ? 1 : 0;
1567 p->phs = 0;
1568 return OK;
1569 }
1570
jitters(CSOUND * csound,JITTERS * p)1571 static int32_t jitters(CSOUND *csound, JITTERS *p)
1572 {
1573 MYFLT x, c3= p->c3, c2= p->c2;
1574 MYFLT f0 = p->num0, df0= p->df0;
1575
1576 if (p->initflag == 1) {
1577 p->initflag = 0;
1578 goto next;
1579 }
1580 p->phs += p->si;
1581 if (p->phs >= 1.0) {
1582 MYFLT slope, resd1, resd0, f2, f1;
1583 next:
1584 p->si = (randGab * (*p->cpsMax-*p->cpsMin) + *p->cpsMin)*CS_ONEDKR;
1585 if (p->si == 0) p->si = 1; /* Is this necessary? */
1586 while (p->phs > 1.0)
1587 p->phs -= 1.0;
1588 f0 = p->num0 = p->num1;
1589 f1 = p->num1 = p->num2;
1590 f2 = p->num2 = BiRandGab;
1591 df0 = p->df0 = p->df1;
1592 p->df1 = ( f2 - f0 ) * FL(0.5);
1593 slope = f1 - f0;
1594 resd0 = df0 - slope;
1595 resd1 = p->df1 - slope;
1596 c3 = p->c3 = resd0 + resd1;
1597 c2 = p->c2 = - (resd1 + FL(2.0)* resd0);
1598 }
1599 x= (MYFLT) p->phs;
1600 *p->ar = (((c3 * x + c2) * x + df0) * x + f0) * *p->amp;
1601 return OK;
1602 }
1603
jittersa(CSOUND * csound,JITTERS * p)1604 static int32_t jittersa(CSOUND *csound, JITTERS *p)
1605 {
1606 MYFLT x, c3=p->c3, c2=p->c2;
1607 MYFLT f0= p->num0, df0 = p->df0;
1608 MYFLT *ar = p->ar, *amp = p->amp;
1609 MYFLT cpsMax = *p->cpsMax, cpsMin = *p->cpsMin;
1610 uint32_t offset = p->h.insdshead->ksmps_offset;
1611 uint32_t early = p->h.insdshead->ksmps_no_end;
1612 uint32_t n, nsmps = CS_KSMPS;
1613 int32_t cod = p->cod;
1614 double phs = p->phs, si = p->si;
1615
1616 if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
1617 if (UNLIKELY(early)) {
1618 nsmps -= early;
1619 memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
1620 }
1621 if (p->initflag) {
1622 p->initflag = 0;
1623 n = offset;
1624 goto next;
1625 }
1626 for (n=offset; n<nsmps; n++) {
1627 phs += si;
1628 if (phs >= 1.0) {
1629 MYFLT slope, resd1, resd0, f2, f1;
1630 next:
1631 si = (randGab * (cpsMax - cpsMin) + cpsMin)*csound->onedsr;
1632 if (si == 0) si = 1; /* Is this necessary? */
1633 while (phs > 1.0)
1634 phs -= 1.0;
1635 f0 = p->num0 = p->num1;
1636 f1 = p->num1 = p->num2;
1637 f2 = p->num2 = BiRandGab;
1638 df0 = p->df0 = p->df1;
1639 p->df1 = ( f2 - f0 ) * FL(0.5);
1640 slope = f1 - f0;
1641 resd0 = df0 - slope;
1642 resd1 = p->df1 - slope;
1643 c3 = p->c3 = resd0 + resd1;
1644 c2 = p->c2 = - (resd1 + FL(2.0)* resd0);
1645 }
1646 x = (MYFLT) phs;
1647 ar[n] = (((c3 * x + c2) * x + df0) * x + f0) * *amp;
1648 if (cod) amp++;
1649 }
1650 p->phs = phs;
1651 p->si =si;
1652 return OK;
1653 }
1654
kDiscreteUserRand(CSOUND * csound,DURAND * p)1655 static int32_t kDiscreteUserRand(CSOUND *csound, DURAND *p)
1656 { /* gab d5*/
1657 if (p->pfn != (int32)*p->tableNum) {
1658 if (UNLIKELY( (p->ftp = csound->FTFindP(csound, p->tableNum) ) == NULL))
1659 goto err1;
1660 p->pfn = (int32)*p->tableNum;
1661 }
1662 *p->out = p->ftp->ftable[(int32)(randGab * p->ftp->flen)];
1663 return OK;
1664 err1:
1665 return csound->PerfError(csound, &(p->h),
1666 Str("Invalid ftable no. %f"),
1667 *p->tableNum);
1668 }
1669
iDiscreteUserRand(CSOUND * csound,DURAND * p)1670 static int32_t iDiscreteUserRand(CSOUND *csound, DURAND *p)
1671 {
1672 p->pfn = 0L;
1673 kDiscreteUserRand(csound,p);
1674 return OK;
1675 }
1676
aDiscreteUserRand(CSOUND * csound,DURAND * p)1677 static int32_t aDiscreteUserRand(CSOUND *csound, DURAND *p)
1678 { /* gab d5*/
1679 MYFLT *out = p->out, *table;
1680 uint32_t offset = p->h.insdshead->ksmps_offset;
1681 uint32_t early = p->h.insdshead->ksmps_no_end;
1682 uint32_t n, nsmps = CS_KSMPS, flen;
1683
1684 if (p->pfn != (int32)*p->tableNum) {
1685 if (UNLIKELY( (p->ftp = csound->FTFindP(csound, p->tableNum) ) == NULL))
1686 goto err1;
1687 p->pfn = (int32)*p->tableNum;
1688 }
1689 table = p->ftp->ftable;
1690 flen = p->ftp->flen;
1691 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
1692 if (UNLIKELY(early)) {
1693 nsmps -= early;
1694 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
1695 }
1696 for (n=offset; n<nsmps; n++) {
1697 out[n] = table[(int32)(randGab) * flen];
1698 }
1699 return OK;
1700 err1:
1701 return csound->PerfError(csound, &(p->h),
1702 Str("Invalid ftable no. %f"),
1703 *p->tableNum);
1704 }
1705
kContinuousUserRand(CSOUND * csound,CURAND * p)1706 static int32_t kContinuousUserRand(CSOUND *csound, CURAND *p)
1707 { /* gab d5*/
1708 int32 indx;
1709 MYFLT findx, fract, v1, v2;
1710 if (p->pfn != (int32)*p->tableNum) {
1711 if (UNLIKELY( (p->ftp = csound->FTFindP(csound, p->tableNum) ) == NULL))
1712 goto err1;
1713 p->pfn = (int32)*p->tableNum;
1714 }
1715 findx = (MYFLT) (randGab * p->ftp->flen);
1716 indx = (int32) findx;
1717 fract = findx - indx;
1718 v1 = *(p->ftp->ftable + indx);
1719 v2 = *(p->ftp->ftable + indx + 1);
1720 *p->out = (v1 + (v2 - v1) * fract) * (*p->max - *p->min) + *p->min;
1721 return OK;
1722 err1:
1723 return csound->PerfError(csound, &(p->h),
1724 Str("Invalid ftable no. %f"),
1725 *p->tableNum);
1726 }
1727
iContinuousUserRand(CSOUND * csound,CURAND * p)1728 static int32_t iContinuousUserRand(CSOUND *csound, CURAND *p)
1729 {
1730 p->pfn = 0;
1731 kContinuousUserRand(csound,p);
1732 return OK;
1733 }
1734
Cuserrnd_set(CSOUND * csound,CURAND * p)1735 static int32_t Cuserrnd_set(CSOUND *csound, CURAND *p)
1736 {
1737 IGN(csound);
1738 p->pfn = 0;
1739 return OK;
1740 }
1741
aContinuousUserRand(CSOUND * csound,CURAND * p)1742 static int32_t aContinuousUserRand(CSOUND *csound, CURAND *p)
1743 { /* gab d5*/
1744 MYFLT min = *p->min, rge = *p->max;
1745 MYFLT *out = p->out, *table;
1746 uint32_t offset = p->h.insdshead->ksmps_offset;
1747 uint32_t early = p->h.insdshead->ksmps_no_end;
1748 uint32_t n, nsmps = CS_KSMPS, flen;
1749 int32_t indx;
1750 MYFLT findx, fract,v1,v2;
1751
1752 if (p->pfn != (int32)*p->tableNum) {
1753 if (UNLIKELY( (p->ftp = csound->FTFindP(csound, p->tableNum) ) == NULL))
1754 goto err1;
1755 p->pfn = (int32)*p->tableNum;
1756 }
1757
1758 table = p->ftp->ftable;
1759 flen = p->ftp->flen;
1760
1761 rge -= min;
1762 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
1763 if (UNLIKELY(early)) {
1764 nsmps -= early;
1765 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
1766 }
1767 for (n=offset; n<nsmps; n++) {
1768 findx = (MYFLT) (randGab * flen);
1769 indx = (int32) findx;
1770 fract = findx - indx;
1771 v1 = table[indx];
1772 v2 = table[indx+1];
1773 out[n] = (v1 + (v2 - v1) * fract) * rge + min;
1774 }
1775 return OK;
1776 err1:
1777 return csound->PerfError(csound, &(p->h),
1778 Str("Invalid ftable no. %f"),
1779 *p->tableNum);
1780 }
1781
ikRangeRand(CSOUND * csound,RANGERAND * p)1782 static int32_t ikRangeRand(CSOUND *csound, RANGERAND *p)
1783 { /* gab d5*/
1784 *p->out = randGab * (*p->max - *p->min) + *p->min;
1785 return OK;
1786 }
1787
aRangeRand(CSOUND * csound,RANGERAND * p)1788 static int32_t aRangeRand(CSOUND *csound, RANGERAND *p)
1789 { /* gab d5*/
1790 MYFLT min = *p->min, max = *p->max, *out = p->out;
1791 uint32_t offset = p->h.insdshead->ksmps_offset;
1792 uint32_t early = p->h.insdshead->ksmps_no_end;
1793 uint32_t n, nsmps = CS_KSMPS;
1794 MYFLT rge = max - min;
1795
1796 if (UNLIKELY(offset)) memset(out, '\0', offset*sizeof(MYFLT));
1797 if (UNLIKELY(early)) {
1798 nsmps -= early;
1799 memset(&out[nsmps], '\0', early*sizeof(MYFLT));
1800 }
1801 for (n=offset; n<nsmps; n++) {
1802 out[n] = randGab * rge + min;
1803 }
1804 return OK;
1805 }
1806
1807 /* mode and fstval arguments added */
1808 /* by Francois Pinot, jan. 2011 */
randomi_set(CSOUND * csound,RANDOMI * p)1809 static int32_t randomi_set(CSOUND *csound, RANDOMI *p)
1810 {
1811 int32_t mode = (int32_t)(*p->mode);
1812 p->phs = 0;
1813 switch (mode) {
1814 case 1: /* immediate interpolation between kmin and 1st random number */
1815 p->num1 = FL(0.0);
1816 p->num2 = randGab;
1817 p->dfdmax = (p->num2 - p->num1) / FMAXLEN;
1818 break;
1819 case 2: /* immediate interpolation between ifirstval and 1st random number */
1820 p->num1 = (*p->max - *p->min) ?
1821 (*p->fstval - *p->min) / (*p->max - *p->min) : FL(0.0);
1822 p->num2 = randGab;
1823 p->dfdmax = (p->num2 - p->num1) / FMAXLEN;
1824 break;
1825 case 3: /* immediate interpolation between 1st and 2nd random number */
1826 p->num1 = randGab;
1827 p->num2 = randGab;
1828 p->dfdmax = (p->num2 - p->num1) / FMAXLEN;
1829 break;
1830 default: /* old behaviour as developped by Gabriel */
1831 p->num1 = p->num2 = FL(0.0);
1832 p->dfdmax = FL(0.0);
1833 }
1834 p->cpscod = IS_ASIG_ARG(p->xcps) ? 1 : 0;
1835 return OK;
1836 }
1837
krandomi(CSOUND * csound,RANDOMI * p)1838 static int32_t krandomi(CSOUND *csound, RANDOMI *p)
1839 {
1840 *p->ar = (p->num1 + (MYFLT)p->phs * p->dfdmax) * (*p->max - *p->min) + *p->min;
1841 p->phs += (int32)(*p->xcps * CS_KICVT);
1842 if (p->phs >= MAXLEN) {
1843 p->phs &= PHMASK;
1844 p->num1 = p->num2;
1845 p->num2 = randGab;
1846 p->dfdmax = (p->num2 - p->num1) / FMAXLEN;
1847 }
1848 return OK;
1849 }
1850
randomi(CSOUND * csound,RANDOMI * p)1851 static int32_t randomi(CSOUND *csound, RANDOMI *p)
1852 {
1853 int32 phs = p->phs, inc;
1854 uint32_t offset = p->h.insdshead->ksmps_offset;
1855 uint32_t early = p->h.insdshead->ksmps_no_end;
1856 uint32_t n, nsmps = CS_KSMPS;
1857 MYFLT *ar, *cpsp;
1858 MYFLT amp, min;
1859
1860 cpsp = p->xcps;
1861 min = *p->min;
1862 amp = (*p->max - min);
1863 ar = p->ar;
1864 inc = (int32)(*cpsp++ * csound->sicvt);
1865 if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
1866 if (UNLIKELY(early)) {
1867 nsmps -= early;
1868 memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
1869 }
1870 for (n=offset; n<nsmps; n++) {
1871 ar[n] = (p->num1 + (MYFLT)phs * p->dfdmax) * amp + min;
1872 phs += inc;
1873 if (p->cpscod)
1874 inc = (int32)(*cpsp++ * csound->sicvt);
1875 if (phs >= MAXLEN) {
1876 phs &= PHMASK;
1877 p->num1 = p->num2;
1878 p->num2 = randGab;
1879 p->dfdmax = (p->num2 - p->num1) / FMAXLEN;
1880 }
1881 }
1882 p->phs = phs;
1883 return OK;
1884 }
1885
1886 /* mode and fstval arguments added */
1887 /* by Francois Pinot, jan. 2011 */
randomh_set(CSOUND * csound,RANDOMH * p)1888 static int32_t randomh_set(CSOUND *csound, RANDOMH *p)
1889 {
1890 int32_t mode = (int32_t)(*p->mode);
1891 p->phs = 0;
1892 switch (mode) {
1893 case 2: /* the first output value is ifirstval */
1894 p->num1 = (*p->max - *p->min) ?
1895 (*p->fstval - *p->min) / (*p->max - *p->min) : FL(0.0);
1896 break;
1897 case 3: /* the first output value is a random number within the defined range */
1898 p->num1 = randGab;
1899 break;
1900 default: /* old behaviour as developped by Gabriel */
1901 p->num1 = FL(0.0);
1902 }
1903 p->cpscod = IS_ASIG_ARG(p->xcps) ? 1 : 0;
1904 return OK;
1905 }
1906
krandomh(CSOUND * csound,RANDOMH * p)1907 static int32_t krandomh(CSOUND *csound, RANDOMH *p)
1908 {
1909 *p->ar = p->num1 * (*p->max - *p->min) + *p->min;
1910 p->phs += (int32)(*p->xcps * CS_KICVT);
1911 if (p->phs >= MAXLEN) {
1912 p->phs &= PHMASK;
1913 p->num1 = randGab;
1914 }
1915 return OK;
1916 }
1917
randomh(CSOUND * csound,RANDOMH * p)1918 static int32_t randomh(CSOUND *csound, RANDOMH *p)
1919 {
1920 int32 phs = p->phs, inc;
1921 uint32_t offset = p->h.insdshead->ksmps_offset;
1922 uint32_t early = p->h.insdshead->ksmps_no_end;
1923 uint32_t n, nsmps = CS_KSMPS;
1924 MYFLT *ar, *cpsp;
1925 MYFLT amp, min;
1926
1927 cpsp = p->xcps;
1928 min = *p->min;
1929 amp = (*p->max - min);
1930 ar = p->ar;
1931 inc = (int32)(*cpsp++ * csound->sicvt);
1932 if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
1933 if (UNLIKELY(early)) {
1934 nsmps -= early;
1935 memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
1936 }
1937 for (n=offset; n<nsmps; n++) {
1938 ar[n] = p->num1 * amp + min;
1939 phs += inc;
1940 if (p->cpscod)
1941 inc = (int32)(*cpsp++ * csound->sicvt);
1942 if (phs >= MAXLEN) {
1943 phs &= PHMASK;
1944 p->num1 = randGab;
1945 }
1946 }
1947 p->phs = phs;
1948 return OK;
1949 }
1950
random3_set(CSOUND * csound,RANDOM3 * p)1951 static int32_t random3_set(CSOUND *csound, RANDOM3 *p)
1952 {
1953 p->num1 = randGab;
1954 p->num2 = randGab;
1955 p->df1 = FL(0.0);
1956 p->initflag = 1;
1957 p->rangeMin_cod = IS_ASIG_ARG(p->rangeMin);
1958 p->rangeMax_cod = IS_ASIG_ARG(p->rangeMin);
1959 p->phs = 0.0;
1960 return OK;
1961 }
1962
random3(CSOUND * csound,RANDOM3 * p)1963 static int32_t random3(CSOUND *csound, RANDOM3 *p)
1964 {
1965 MYFLT x, c3= p->c3, c2= p->c2;
1966 MYFLT f0 = p->num0, df0= p->df0;
1967
1968 if (p->initflag) {
1969 p->initflag = 0;
1970 goto next;
1971 }
1972 p->phs += p->si;
1973 if (p->phs >= 1.0) {
1974 MYFLT slope, resd1, resd0, f2, f1;
1975 next:
1976 p->si = (randGab * (*p->cpsMax-*p->cpsMin) + *p->cpsMin)*CS_ONEDKR;
1977 while (p->phs > 1.0)
1978 p->phs -= 1.0;
1979 f0 = p->num0 = p->num1;
1980 f1 = p->num1 = p->num2;
1981 f2 = p->num2 = randGab;
1982 df0 = p->df0 = p->df1;
1983 p->df1 = ( f2 - f0 ) * FL(0.5);
1984 slope = f1 - f0;
1985 resd0 = df0 - slope;
1986 resd1 = p->df1 - slope;
1987 c3 = p->c3 = resd0 + resd1;
1988 c2 = p->c2 = - (resd1 + FL(2.0)* resd0);
1989 }
1990 x = (MYFLT) p->phs;
1991 *p->ar = (((c3 * x + c2) * x + df0) * x + f0) *
1992 (*p->rangeMax - *p->rangeMin) + *p->rangeMin;
1993 return OK;
1994 }
1995
random3a(CSOUND * csound,RANDOM3 * p)1996 static int32_t random3a(CSOUND *csound, RANDOM3 *p)
1997 {
1998 int32_t rangeMin_cod = p->rangeMin_cod, rangeMax_cod = p->rangeMax_cod;
1999 MYFLT x, c3=p->c3, c2=p->c2;
2000 MYFLT f0 = p->num0, df0 = p->df0;
2001 MYFLT *ar = p->ar, *rangeMin = p->rangeMin;
2002 MYFLT *rangeMax = p->rangeMax;
2003 MYFLT cpsMin = *p->cpsMin, cpsMax = *p->cpsMax;
2004 uint32_t offset = p->h.insdshead->ksmps_offset;
2005 uint32_t early = p->h.insdshead->ksmps_no_end;
2006 uint32_t n, nsmps = CS_KSMPS;
2007 double phs = p->phs, si = p->si;
2008
2009 if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
2010 if (UNLIKELY(early)) {
2011 nsmps -= early;
2012 memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
2013 }
2014 if (p->initflag) {
2015 p->initflag = 0;
2016 n = offset;
2017 goto next;
2018 }
2019 for (n=offset; n<nsmps; n++) {
2020 phs += si;
2021 if (phs >= 1.0) {
2022 MYFLT slope, resd1, resd0, f2, f1;
2023 next:
2024 si = (randGab * (cpsMax - cpsMin) + cpsMin)*csound->onedsr;
2025 while (phs > 1.0) phs -= 1.0;
2026 f0 = p->num0 = p->num1;
2027 f1 = p->num1 = p->num2;
2028 f2 = p->num2 = BiRandGab;
2029 df0 = p->df0 = p->df1;
2030 p->df1 = ( f2 - f0 ) * FL(0.5);
2031 slope = f1 - f0;
2032 resd0 = df0 - slope;
2033 resd1 = p->df1 - slope;
2034 c3 = p->c3 = resd0 + resd1;
2035 c2 = p->c2 = - (resd1 + FL(2.0)* resd0);
2036 }
2037 x = (MYFLT) phs;
2038 ar[n] = (((c3 * x + c2) * x + df0) * x + f0) *
2039 (*rangeMax - *rangeMin) + *rangeMin;
2040 if (rangeMin_cod) rangeMin++;
2041 if (rangeMax_cod) rangeMax++;
2042 }
2043 p->phs = phs;
2044 p->si = si;
2045 return OK;
2046 }
2047
2048 #define S(x) sizeof(x)
2049
2050 static OENTRY localops[] = {
2051 { "wrap", 0xffff },
2052 { "wrap.i", S(WRAP), 0,1, "i", "iii", (SUBR)kwrap, NULL, NULL },
2053 { "wrap.k", S(WRAP), 0,2, "k", "kkk", NULL, (SUBR)kwrap, NULL },
2054 { "wrap.a", S(WRAP), 0,2, "a", "akk", NULL, (SUBR)wrap },
2055 { "mirror", 0xffff },
2056 { "mirror.i", S(WRAP), 0,1, "i", "iii", (SUBR)kmirror, NULL, NULL },
2057 { "mirror.k", S(WRAP), 0,2, "k", "kkk", NULL, (SUBR)kmirror, NULL },
2058 { "mirror.a", S(WRAP), 0,2, "a", "akk", NULL, (SUBR)mirror },
2059 { "ntrpol.i",S(INTERPOL), 0,1, "i", "iiiop",(SUBR)interpol },
2060 { "ntrpol.k",S(INTERPOL), 0,3, "k", "kkkop",(SUBR)nterpol_init, (SUBR)knterpol },
2061 { "ntrpol.a",S(INTERPOL), 0,3, "a", "aakop",(SUBR)nterpol_init,(SUBR)anterpol},
2062 { "fold", S(FOLD), 0,3, "a", "ak", (SUBR)fold_set, (SUBR)fold },
2063 { "lineto", S(LINETO), 0,3, "k", "kk", (SUBR)lineto_set, (SUBR)lineto, NULL },
2064 { "tlineto", S(LINETO2), 0,3, "k", "kkk", (SUBR)tlineto_set, (SUBR)tlineto, NULL},
2065 { "vibrato", S(VIBRATO), TR, 3, "k", "kkkkkkkkio",
2066 (SUBR)vibrato_set, (SUBR)vibrato, NULL },
2067 { "vibr", S(VIBRATO), TR, 3, "k", "kki", (SUBR)vibr_set, (SUBR)vibr, NULL },
2068 { "jitter2", S(JITTER2), 0,3, "k", "kkkkkkko", (SUBR)jitter2_set, (SUBR)jitter2 },
2069 { "jitter", S(JITTER), 0,3, "k", "kkk", (SUBR)jitter_set, (SUBR)jitter, NULL },
2070 { "jspline", S(JITTERS), 0,3, "k", "xkk",
2071 (SUBR)jitters_set, (SUBR)jitters, NULL },
2072 { "jspline.a", S(JITTERS), 0,3, "a", "xkk",
2073 (SUBR)jitters_set, (SUBR)jittersa },
2074 { "loopseg", S(LOOPSEG), 0,3, "k", "kkiz", (SUBR)loopseg_set, (SUBR)loopseg, NULL},
2075 { "loopxseg", S(LOOPSEG), 0,3, "k", "kkiz", (SUBR)loopseg_set,(SUBR)loopxseg, NULL},
2076 { "looptseg", S(LOOPSEG), 0,3, "k", "kkiz",(SUBR)looptseg_set,(SUBR)looptseg, NULL},
2077 { "lpshold", S(LOOPSEG), 0,3, "k", "kkiz",(SUBR)loopseg_set, (SUBR)lpshold, NULL },
2078 { "loopsegp", S(LOOPSEGP), 0,3,"k", "kz", (SUBR)loopsegp_set,(SUBR)loopsegp, NULL},
2079 { "lpsholdp", S(LOOPSEGP), 0,3,"k", "kz", (SUBR)loopsegp_set,(SUBR)lpsholdp, NULL},
2080 { "cuserrnd.i", S(CURAND),0,1,"i", "iii", (SUBR)iContinuousUserRand, NULL, NULL },
2081 { "cuserrnd.k", S(CURAND),0,2,"k", "kkk",
2082 (SUBR)Cuserrnd_set, (SUBR)kContinuousUserRand, NULL },
2083 { "cuserrnd.a",S(CURAND),0,2, "a", "kkk",
2084 (SUBR)Cuserrnd_set, (SUBR)aContinuousUserRand },
2085 { "random.i", S(RANGERAND), 0,1, "i", "ii", (SUBR)ikRangeRand, NULL, NULL },
2086 { "random.k", S(RANGERAND), 0,2, "k", "kk", NULL, (SUBR)ikRangeRand, NULL },
2087 { "random.a", S(RANGERAND), 0,2, "a", "kk", NULL, (SUBR)aRangeRand },
2088 { "rspline", S(RANDOM3), 0,3, "k", "xxkk",
2089 (SUBR)random3_set, (SUBR)random3, NULL },
2090 { "rspline.a", S(RANDOM3), 0,3, "a", "xxkk",
2091 (SUBR)random3_set, (SUBR)random3a },
2092 { "randomi", S(RANDOMI), 0,3, "a", "kkxoo",
2093 (SUBR)randomi_set, (SUBR)randomi },
2094 { "randomi.k", S(RANDOMI), 0,3, "k", "kkkoo",
2095 (SUBR)randomi_set, (SUBR)krandomi,NULL },
2096 { "randomh", S(RANDOMH), 0,3, "a", "kkxoo",
2097 (SUBR)randomh_set,(SUBR)randomh },
2098 { "randomh.k", S(RANDOMH), 0,3, "k", "kkkoo",
2099 (SUBR)randomh_set,(SUBR)krandomh,NULL},
2100 { "urd.i", S(DURAND), 0,1, "i", "i", (SUBR)iDiscreteUserRand, NULL, NULL },
2101 { "urd.k", S(DURAND), 0,2, "k", "k", (SUBR)Cuserrnd_set,(SUBR)kDiscreteUserRand },
2102 { "urd.a", S(DURAND), 0,2, "a", "k",
2103 (SUBR)Cuserrnd_set, (SUBR)aDiscreteUserRand },
2104 { "duserrnd.i", S(DURAND),0,1, "i", "i", (SUBR)iDiscreteUserRand, NULL, NULL },
2105 { "duserrnd.k", S(DURAND),0,2, "k", "k",
2106 (SUBR)Cuserrnd_set,(SUBR)kDiscreteUserRand,NULL },
2107 { "duserrnd.a", S(DURAND),0,2, "a", "k",
2108 (SUBR)Cuserrnd_set,(SUBR)aDiscreteUserRand },
2109 //{ "poscil", 0xfffe, TR },
2110 { "poscil.a", S(POSC), TR,3, "a", "kkjo", (SUBR)posc_set,(SUBR)posckk },
2111 { "poscil.kk", S(POSC), TR,3, "k", "kkjo", (SUBR)posc_set,(SUBR)kposc,NULL },
2112 { "poscil.ka", S(POSC), TR,3, "a", "kajo", (SUBR)posc_set, (SUBR)poscka },
2113 { "poscil.ak", S(POSC), TR,3, "a", "akjo", (SUBR)posc_set, (SUBR)poscak },
2114 { "poscil.aa", S(POSC), TR,3, "a", "aajo", (SUBR)posc_set, (SUBR)poscaa },
2115 { "lposcil", S(LPOSC), TR, 3, "a", "kkkkjo", (SUBR)lposc_set, (SUBR)lposc},
2116 //{ "poscil3", 0xfffe, TR },
2117 { "poscil3.a",S(POSC), TR,3, "a", "kkjo",
2118 (SUBR)posc_set,(SUBR)posc3kk },
2119 { "poscil3.kk",S(POSC), TR,3, "k", "kkjo",
2120 (SUBR)posc_set,(SUBR)kposc3,NULL},
2121 { "poscil3.ak", S(POSC), TR,3, "a", "akjo", (SUBR)posc_set, (SUBR)posc3ak },
2122 { "poscil3.ka", S(POSC), TR,3, "a", "kajo", (SUBR)posc_set, (SUBR)posc3ka },
2123 { "poscil3.aa", S(POSC), TR,3, "a", "aajo", (SUBR)posc_set, (SUBR)posc3aa },
2124 { "lposcil3", S(LPOSC), TR, 3, "a", "kkkkjo", (SUBR)lposc_set,(SUBR)lposc3},
2125 { "trigger", S(TRIG), 0,3, "k", "kkk", (SUBR)trig_set, (SUBR)trig, NULL },
2126 { "sum", S(SUM), 0,2, "a", "y", NULL, (SUBR)sum },
2127 { "product", S(SUM), 0,2, "a", "y", NULL, (SUBR)product },
2128 { "resony", S(RESONY), 0,3, "a", "akkikooo", (SUBR)rsnsety, (SUBR)resony }
2129 };
2130
uggab_init_(CSOUND * csound)2131 int32_t uggab_init_(CSOUND *csound)
2132 {
2133 return csound->AppendOpcodes(csound, &(localops[0]),
2134 (int32_t) (sizeof(localops) / sizeof(OENTRY)));
2135 }
2136