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
|
/* Copyright (C) 2007 Gabriel Maldonado
Csound 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., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
//#include "csdl.h"
#include "csoundCore.h"
#include "interlocks.h"
/* -------------------------------------------------------------------- */
/*
The iConfigTab is made up of the following parameters:
f # time size -2 inactive_flag1 inactive_flag2 ... inactiveflagN
a 1 value means that corresponding parameter is left unchanged by the HVS opcode
The iPositionsTab is made up of the positions of the snapshots
(contained in the iSnapTab) in the two-dimensional grid. Each
subsequent element is actually a pointer representing the position in
the iSnapTab. For example, in an HVS grid such as the following:
5 2 1 3
7 9 6 2
0 4 1 3
This represents the snapshot position within the grid (in this case a
4x3 grid). So, the first element 5, has index zero and represents the
sixth (element zero is the first) element of the iSnapTab, the second
element 2 represents the third element of iSnapTab and so on.
Obviously, iOutTab size must be >= inumParms.
*/
typedef struct {
OPDS h;
MYFLT *kx, *inumParms, *inumPointsX, *iOutTab, *iPositionsTab,
*iSnapTab, *iConfigTab;
MYFLT *outTable, *posTable, *snapTable, *confTable;
int32_t iconfFlag;
} HVS1;
static int32_t hvs1_set(CSOUND *csound, HVS1 *p)
{
FUNC *ftp;
if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->iOutTab)) == NULL))
return csound->InitError(csound, Str("hvs: No out table"));
p->outTable = ftp->ftable;
if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->iPositionsTab)) == NULL))
return csound->InitError(csound, Str("hvs: No positions table"));
p->posTable = ftp->ftable;
if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->iSnapTab)) == NULL))
return csound->InitError(csound, Str("hvs: No snap table"));
p->snapTable = ftp->ftable;
if (UNLIKELY(*p->inumPointsX < 2 ))
return csound->InitError(csound, Str("hvs1: a line segment must be "
"delimited by 2 points at least"));
if (*p->iConfigTab == 0)
p->iconfFlag = 0;
else {
if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->iConfigTab)) == NULL))
return csound->InitError(csound, Str("hvs: no config table"));
p->outTable = ftp->ftable;
p->iconfFlag = 1;
}
return OK;
}
static int32_t hvs1(CSOUND *csound, HVS1 *p)
{
IGN(csound);
MYFLT x = *p->kx * (*p->inumPointsX-1);
int32_t posX = (int32_t) x;
MYFLT fracX = x - posX;
int32_t noc = (int32_t) *p->inumParms;
// int32_t linesX = (int32_t) *p->inumPointsX;
int32_t ndx1 = (int32_t) p->posTable[posX];
int32_t ndx2 = (int32_t) p->posTable[posX+1];
int32_t j;
if (p->iconfFlag) {
for (j =0; j< noc; j++) {
switch ((int32_t) p->confTable[j]) {
case -1: // ignore parameter
break;
case 0: // linear interpolation
{
MYFLT val1 = p->snapTable[ndx1 * noc + j];
MYFLT val2 = p->snapTable[ndx2 * noc + j];
MYFLT valu = (1 - fracX) * val1 + fracX * val2;
p->outTable[j] = valu;
}
break;
default: // special table=shaped interpolations
; // to be implemented...
}
}
}
else {
for ( j =0; j< noc; j++) {
MYFLT val1 = p->snapTable[ndx1 * noc + j];
MYFLT val2 = p->snapTable[ndx2 * noc + j];
MYFLT valu = (1 - fracX) * val1 + fracX * val2;
p->outTable[j] = valu;
}
}
return OK;
}
/* -------------------------------------------------------------------- */
typedef struct {
OPDS h;
MYFLT *kx, *ky, *inumParms, *inumlinesX, *inumlinesY,
*iOutTab, *iPositionsTab, *iSnapTab, *iConfigTab;
MYFLT *outTable, *posTable, *snapTable, *confTable;
int32_t iconfFlag;
} HVS2;
static int32_t hvs2_set(CSOUND *csound, HVS2 *p)
{
FUNC *ftp;
if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->iOutTab)) == NULL))
return csound->InitError(csound, Str("hvs: No out table"));
p->outTable = ftp->ftable;
if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->iPositionsTab)) == NULL))
return csound->InitError(csound, Str("hvs: No positions table"));
p->posTable = ftp->ftable;
if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->iSnapTab)) == NULL))
return csound->InitError(csound, Str("hvs: No snap table"));
p->snapTable = ftp->ftable;
if (UNLIKELY(*p->inumlinesX < 2 || *p->inumlinesY < 2))
return csound->InitError(csound, Str("hvs2: a square area must be "
"delimited by 2 lines at least"));
if (*p->iConfigTab == 0)
p->iconfFlag = 0;
else {
if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->iConfigTab)) == NULL))
return csound->InitError(csound, Str("hvs: no config table"));
p->outTable = ftp->ftable;
p->iconfFlag = 1;
}
return OK;
}
static int32_t hvs2(CSOUND *csound, HVS2 *p)
{
IGN(csound);
MYFLT x = *p->kx * (*p->inumlinesX-1);
MYFLT y = *p->ky * (*p->inumlinesY-1);
int32_t posX = (int32_t) x;
int32_t posY = (int32_t) y;
MYFLT fracX = x - posX;
MYFLT fracY = y - posY;
int32_t noc = (int32_t) *p->inumParms;
int32_t linesX = (int32_t) *p->inumlinesX;
int32_t ndx1 = (int32_t) p->posTable[posX + posY * linesX];
int32_t ndx2 = (int32_t) p->posTable[posX+1 + posY * linesX];
int32_t ndx3 = (int32_t) p->posTable[posX + (posY+1) * linesX];
int32_t ndx4 = (int32_t) p->posTable[posX+1 + (posY+1) * linesX];
int32_t j;
if (p->iconfFlag) {
for ( j =0; j< noc; j++) {
switch ((int32_t) p->confTable[j]) {
case -1: // ignore parameter
break;
case 0: // linear interpolation
{
MYFLT val1 = p->snapTable[ndx1 * noc + j];
MYFLT val2 = p->snapTable[ndx2 * noc + j];
MYFLT val3 = p->snapTable[ndx3 * noc + j];
MYFLT val4 = p->snapTable[ndx4 * noc + j];
MYFLT valX1 = (1 - fracX) * val1 + fracX * val2;
MYFLT valX2 = (1 - fracX) * val3 + fracX * val4;
MYFLT valu = (1 - fracY) * valX1 + fracY * valX2;
p->outTable[j] = valu;
}
break;
default: // special table=shaped interpolations
; // to be implemented...
}
}
}
else {
for ( j =0; j< noc; j++) {
MYFLT val1 = p->snapTable[ndx1 * noc + j];
MYFLT val2 = p->snapTable[ndx2 * noc + j];
MYFLT val3 = p->snapTable[ndx3 * noc + j];
MYFLT val4 = p->snapTable[ndx4 * noc + j];
MYFLT valX1 = (1 - fracX) * val1 + fracX * val2;
MYFLT valX2 = (1 - fracX) * val3 + fracX * val4;
MYFLT valu = (1 - fracY) * valX1 + fracY * valX2;
p->outTable[j] = valu;
}
}
return OK;
}
/* -------------------------------------------------------------------- */
typedef struct {
OPDS h;
MYFLT *kx, *ky, *kz, *inumParms, *inumlinesX, *inumlinesY,
*inumlinesZ, *iOutTab, *iPositionsTab, *iSnapTab, *iConfigTab;
MYFLT *outTable, *posTable, *snapTable, *confTable;
int32_t iconfFlag;
} HVS3;
static int32_t hvs3_set(CSOUND *csound, HVS3 *p)
{
FUNC *ftp;
if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->iOutTab)) == NULL))
return csound->InitError(csound, Str("hvs: No out table"));
p->outTable = ftp->ftable;
if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->iPositionsTab)) == NULL))
return csound->InitError(csound, Str("hvs: No positions table"));
p->posTable = ftp->ftable;
if (UNLIKELY((ftp = csound->FTnp2Find(csound, p->iSnapTab)) == NULL))
return csound->InitError(csound, Str("hvs: No snap table"));
p->snapTable = ftp->ftable;
if (UNLIKELY(*p->inumlinesX < 2 || *p->inumlinesY < 2))
return csound->InitError(csound, Str("hvs3: a square area must be "
"delimited by 2 lines at least"));
if (LIKELY(*p->iConfigTab == 0))
p->iconfFlag = 0;
else {
if ((ftp = csound->FTnp2Find(csound, p->iConfigTab)) == NULL)
return csound->InitError(csound, Str("hvs: no config table"));
p->outTable = ftp->ftable;
p->iconfFlag = 1;
}
return OK;
}
static int32_t hvs3(CSOUND *csound, HVS3 *p)
{
IGN(csound);
MYFLT x = *p->kx * (*p->inumlinesX-1);
MYFLT y = *p->ky * (*p->inumlinesY-1);
MYFLT z = *p->kz * (*p->inumlinesZ-1);
int32_t posX = (int32_t) x;
int32_t posY = (int32_t) y;
int32_t posZ = (int32_t) z;
MYFLT fracX = x - posX;
MYFLT fracY = y - posY;
MYFLT fracZ = z - posZ;
int32_t noc = (int32_t) *p->inumParms;
int32_t linesX = (int32_t) *p->inumlinesX;
int32_t linesXY = linesX * (int32_t) *p->inumlinesY;
int32_t ndx1 = (int32_t) p->posTable[posX +posY *linesX+posZ*linesXY];
int32_t ndx2 = (int32_t) p->posTable[posX+1+posY *linesX+posZ*linesXY];
int32_t ndx3 = (int32_t) p->posTable[posX +(posY+1)*linesX+posZ*linesXY];
int32_t ndx4 = (int32_t) p->posTable[posX+1+(posY+1)*linesX+posZ*linesXY];
int32_t ndx5 = (int32_t) p->posTable[posX +posY *linesX+(posZ+1)*linesXY];
int32_t ndx6 = (int32_t) p->posTable[posX+1+posY *linesX+(posZ+1)*linesXY];
int32_t ndx7 = (int32_t) p->posTable[posX +(posY+1)*linesX+(posZ+1)*linesXY];
int32_t ndx8 = (int32_t) p->posTable[posX+1+(posY+1)*linesX+(posZ+1)*linesXY];
int32_t j;
if (p->iconfFlag) {
for ( j =0; j< noc; j++) {
switch ((int32_t) p->confTable[j]) {
case -1: // ignore parameter
break;
case 0: // linear interpolation
{
MYFLT val1 = p->snapTable[ndx1 * noc + j];
MYFLT val2 = p->snapTable[ndx2 * noc + j];
MYFLT val3 = p->snapTable[ndx3 * noc + j];
MYFLT val4 = p->snapTable[ndx4 * noc + j];
MYFLT valX1 = (1 - fracX) * val1 + fracX * val2;
MYFLT valX2 = (1 - fracX) * val3 + fracX * val4;
MYFLT valY1 = (1 - fracY) * valX1 + fracY * valX2;
MYFLT valY2, valu;
val1 = p->snapTable[ndx5 * noc + j];
val2 = p->snapTable[ndx6 * noc + j];
val3 = p->snapTable[ndx7 * noc + j];
val4 = p->snapTable[ndx8 * noc + j];
valX1 = (1 - fracX) * val1 + fracX * val2;
valX2 = (1 - fracX) * val3 + fracX * val4;
valY2 = (1 - fracY) * valX1 + fracY * valX2;
valu = (1-fracZ) * valY1 + fracZ * valY2;
p->outTable[j] = valu;
}
break;
default: // special table=shaped interpolations
; // to be implemented...
}
}
}
else {
for ( j =0; j< noc; j++) {
MYFLT val1 = p->snapTable[ndx1 * noc + j];
MYFLT val2 = p->snapTable[ndx2 * noc + j];
MYFLT val3 = p->snapTable[ndx3 * noc + j];
MYFLT val4 = p->snapTable[ndx4 * noc + j];
MYFLT valX1 = (1 - fracX) * val1 + fracX * val2;
MYFLT valX2 = (1 - fracX) * val3 + fracX * val4;
MYFLT valY1 = (1 - fracY) * valX1 + fracY * valX2;
MYFLT valY2, valu;
val1 = p->snapTable[ndx5 * noc + j];
val2 = p->snapTable[ndx6 * noc + j];
val3 = p->snapTable[ndx7 * noc + j];
val4 = p->snapTable[ndx8 * noc + j];
valX1 = (1 - fracX) * val1 + fracX * val2;
valX2 = (1 - fracX) * val3 + fracX * val4;
valY2 = (1 - fracY) * valX1 + fracY * valX2;
valu = (1-fracZ) * valY1 + fracZ * valY2;
p->outTable[j] = valu;
}
}
return OK;
}
/* -------------------------------------------------------------------- */
typedef struct {
FUNC *function, *nxtfunction;
double d;
} TSEG2;
typedef struct {
OPDS h;
MYFLT *kphase, *ioutfunc, *ielements,*argums[VARGMAX];
TSEG2 *cursegp;
MYFLT *vector;
int32_t elements;
int64_t nsegs;
AUXCH auxch;
} VPSEG;
static int32_t vphaseseg_set(CSOUND *csound, VPSEG *p)
{
TSEG2 *segp;
int32_t nsegs,j;
MYFLT **argp;
double dur, durtot = 0.0, prevphs;
FUNC *nxtfunc, *curfunc, *ftp;
nsegs = p->nsegs =((p->INCOUNT-3) >> 1); /* count segs & alloc if nec */
if ((segp = (TSEG2 *) p->auxch.auxp) == NULL) {
csound->AuxAlloc(csound, (int64_t)(nsegs+1)*sizeof(TSEG2), &p->auxch);
p->cursegp = segp = (TSEG2 *) p->auxch.auxp;
//(segp+nsegs)->cnt = MAXPOS;
}
argp = p->argums;
if (UNLIKELY((nxtfunc = csound->FTnp2Find(csound, *argp++)) == NULL))
return csound->InitError(csound,
Str("vphaseseg: the first function is "
"invalid or missing"));
if (LIKELY((ftp = csound->FTnp2Find(csound, p->ioutfunc)) != NULL)) {
p->vector = ftp->ftable;
p->elements = (int32_t) *p->ielements;
}
else return csound->InitError(csound, Str("Failed to find ftable"));
if (UNLIKELY(p->elements > (int32_t)ftp->flen))
return csound->InitError(csound,
Str("vphaseseg: invalid num. of elements"));
/* vector = p->vector; */
/* flength = p->elements; */
memset(p->vector, 0, sizeof(MYFLT)*p->elements);
/* do *vector++ = FL(0.0); */
/* while (--flength); */
if (UNLIKELY(**argp <= 0.0)) return NOTOK; /* if idur1 <= 0, skip init */
//p->cursegp = tempsegp =segp; /* else proceed from 1st seg */
segp--;
do {
segp++; /* init each seg .. */
curfunc = nxtfunc;
dur = **argp++;
if (UNLIKELY((nxtfunc = csound->FTnp2Find(csound, *argp++)) == NULL))
return csound->InitError(csound,
Str("vphaseseg: function invalid or missing"));
if (LIKELY(dur > 0.0f)) {
durtot+=dur;
segp->d = dur; // ekr;
segp->function = curfunc;
segp->nxtfunction = nxtfunc;
//segp->cnt = (int64_t) (segp->d + .5);
}
else break; /* .. til 0 dur or done */
} while (--nsegs);
segp++;
segp->function = nxtfunc;
segp->nxtfunction = nxtfunc;
nsegs = p->nsegs;
segp = p->cursegp;
for (j=0; j< nsegs; j++)
segp[j].d /= durtot;
/* This could be a memmove */
for (j=nsegs-1; j>= 0; j--)
segp[j+1].d = segp[j].d;
segp[0].d = prevphs = 0.0;
for (j=0; j<= nsegs; j++) {
segp[j].d += prevphs;
prevphs = segp[j].d;
}
return OK;
}
static int32_t vphaseseg(CSOUND *csound, VPSEG *p)
{
IGN(csound);
TSEG2 *segp = p->cursegp;
double phase = *p->kphase, partialPhase = 0.0;
int32_t j, flength;
MYFLT *curtab = NULL, *nxttab = NULL, curval, nxtval, *vector;
while (phase >= 1.0) phase -= 1.0;
while (phase < 0.0) phase = 0.0;
for (j = 0; j < p->nsegs; j++) {
TSEG2 *seg = &segp[j], *seg1 = &segp[j+1];
if (phase < seg1->d) {
curtab = seg->function->ftable;
nxttab = seg1->function->ftable;
partialPhase = (phase - seg->d) / (seg1->d - seg->d);
break;
}
}
if (UNLIKELY(curtab==NULL)) return NOTOK;
flength = p->elements;
vector = p->vector;
do {
curval = *curtab++;
nxtval = *nxttab++;
*vector++ = (MYFLT) (curval + ((nxtval - curval) * partialPhase));
} while (--flength);
return OK;
}
/* -------------------------------------------------------------------- */
#define S(x) sizeof(x)
OENTRY hvs_localops[] = {
{ "hvs1", S(HVS1), TB, 3, "", "kiiiiio",
(SUBR)hvs1_set, (SUBR)hvs1, (SUBR)NULL },
{ "hvs2", S(HVS2), TB, 3, "", "kkiiiiiio",
(SUBR)hvs2_set, (SUBR)hvs2, (SUBR)NULL },
{ "hvs3", S(HVS3), TB, 3, "", "kkkiiiiiiio",
(SUBR)hvs3_set, (SUBR)hvs3, (SUBR)NULL },
{ "vphaseseg", S(VPSEG), TB, 3, "", "kiim",
(SUBR)vphaseseg_set, (SUBR)vphaseseg }
};
int32_t hvs_init_(CSOUND *csound)
{
return csound->AppendOpcodes(csound, &(hvs_localops[0]),
(int32_t) (sizeof(hvs_localops) / sizeof(OENTRY)));
}
|