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 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649
|
/************************************************************************
************************************************************************
FAUST compiler
Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
---------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
************************************************************************
************************************************************************/
#include "propagate.hh"
#include "Text.hh"
#include "exception.hh"
#include "floats.hh"
#include "global.hh"
#include "labels.hh"
#include "names.hh"
#include "ppbox.hh"
#include "ppsig.hh"
#include "prim2.hh"
#include "simplify.hh"
#include "xtended.hh"
////////////////////////////////////////////////////////////////////////
/**
* propagate : box listOfSignal-> listOfSignal'
*
* Propage a list of signals into a box expression representing a
* signal processor
*/
///////////////////////////////////////////////////////////////////////
//! mix une liste de signaux sur n bus
siglist mix(const siglist& lsig, int nbus)
{
int nlines = (int)lsig.size();
siglist dst(nbus);
for (int b = 0; b < nbus; b++) {
Tree t = (b < nlines) ? lsig[b] : sigInt(0);
for (int i = b + nbus; i < nlines; i += nbus) {
t = sigAdd(t, lsig[i]);
}
dst[b] = t;
}
return dst;
}
//! split une liste de signaux sur n bus
siglist split(const siglist& inputs, int nbus)
{
int nlines = (int)inputs.size();
siglist outputs(nbus);
for (int b = 0; b < nbus; b++) {
outputs[b] = inputs[b % nlines];
}
return outputs;
}
//! Fabrique une liste de n projections d'un groupe récursif
siglist makeSigProjList(Tree t, int n)
{
siglist l(n);
for (int i = 0; i < n; i++) l[i] = sigDelay0(sigProj(i, t));
return l;
}
//! Fabrique une liste de n mem projections d'un groupe récursif
siglist makeMemSigProjList(Tree t, int n)
{
siglist l(n);
for (int i = 0; i < n; i++) l[i] = sigDelay1(sigProj(i, t));
return l;
}
//! Fabrique une liste de n entrées
siglist makeSigInputList(int n)
{
siglist l(n);
for (int i = 0; i < n; i++) l[i] = sigInput(i);
return l;
}
inline siglist makeList(Tree t)
{
siglist l(1);
l[0] = t;
return l;
}
siglist listRange(const siglist& l, int i, int j)
{
siglist r(j - i);
for (int x = i; x < j; x++) r[x - i] = l[x];
return r;
}
siglist listConcat(const siglist& a, const siglist& b)
{
int n1 = (int)a.size();
int n2 = (int)b.size();
siglist r(n1 + n2);
for (int x = 0; x < n1; x++) r[x] = a[x];
for (int x = 0; x < n2; x++) r[x + n1] = b[x];
return r;
}
/**
* Convert an stl list of signals into a tree list of signals
*/
Tree listConvert(const siglist& a)
{
int n = (int)a.size();
Tree t = gGlobal->nil;
while (n--) t = cons(a[n], t);
return t;
}
/**
* Convert a tree list of signals into an stl list of signals
*/
void treelist2siglist(Tree l, siglist& r)
{
r.clear();
while (!isNil(l)) {
r.push_back(hd(l));
l = tl(l);
}
}
siglist listLift(const siglist& l)
{
int n = (int)l.size();
siglist r(n);
for (int i = 0; i < n; i++) r[i] = lift(l[i]);
return r;
}
/**
* Propagate computes the outputs signals of a block-diagram according to a list of input signals.
*
*\param slotenv input signals associated with symbolic slots
*\param path stack of user interface groups : (type,label)*
*\param box block-diagram where we propagate the signals
*\param lsig list of signals to be propagated into box
*\return list of resulting signals
*/
/**
* Store the propagation result as a property of the arguments tuplet
* @param args propagation arguments
* @param value propagation result
*/
void setPropagateProperty(Tree args, const siglist& lsig)
{
setProperty(args, tree(gGlobal->PROPAGATEPROPERTY), listConvert(lsig));
}
/**
* Retreive the propagation result as a property of the arguments tuplet
* @param args propagation arguments
* @param lsig the propagation result if any
* @return true if a propagation result was stored
*/
bool getPropagateProperty(Tree args, siglist& lsig)
{
Tree value;
if (getProperty(args, tree(gGlobal->PROPAGATEPROPERTY), value)) {
treelist2siglist(value, lsig);
return true;
} else {
return false;
}
}
/**
* Propagate a list of signals into a block diagram.
* @param slotenv environment associating slots and signals
* @param path user interface group path
* @param box the block diagram
* @param lsig the list of input signals to propagate
* @return the resulting list of output signals
*/
siglist realPropagate(Tree slotenv, Tree path, Tree box, const siglist& lsig);
/**
* Propagate a list of signals into a block diagram. Do memoization.
* @param slotenv environment associating slots and signals
* @param path user interface group path
* @param box the block diagram
* @param lsig the list of input signals to propagate
* @return the resulting list of output signals
*/
siglist propagate(Tree slotenv, Tree path, Tree box, const siglist& lsig)
{
Tree args = tree(gGlobal->PROPAGATEPROPERTY, slotenv, path, box, listConvert(lsig));
siglist result;
if (!getPropagateProperty(args, result)) {
result = realPropagate(slotenv, path, box, lsig);
setPropagateProperty(args, result);
}
// cerr << "propagate in " << boxpp(box) << endl;
// for (int i=0; i<lsig.size(); i++) { cerr << " -> signal " << i << " : " << *(lsig[i]) << endl; }
// cerr << endl;
return result;
}
// Apply sigFTZ() to all signals of a vector
static siglist wrapWithFTZ(const siglist& l1)
{
siglist l2;
for (auto x : l1) {
l2.push_back(sigFTZ(x));
}
return l2;
}
// Collect the leaf numbers of tree l into vector v.
// return true if l a number or a parallel tree of numbers
static bool isIntTree(Tree l, vector<int>& v)
{
int n;
double r;
Tree x, y;
if (isBoxInt(l, &n)) {
v.push_back(n);
return true;
} else if (isBoxReal(l, &r)) {
v.push_back(int(r));
return true;
} else if (isBoxPar(l, x, y)) {
return isIntTree(x, v) && isIntTree(y, v);
} else {
stringstream error;
error << "ERROR in file " << __FILE__ << ':' << __LINE__ << ", not a valid list of numbers : " << boxpp(l)
<< endl;
throw faustexception(error.str());
}
}
/**
* Propagate a list of signals into a block diagram. Actual function.
* @param slotenv environment associating slots and signals
* @param path user interface group path
* @param box the block diagram
* @param lsig the list of input signals to propagate
* @return the resulting list of output signals
*/
siglist realPropagate(Tree slotenv, Tree path, Tree box, const siglist& lsig)
{
int i;
double r;
prim0 p0;
prim1 p1;
prim2 p2;
prim3 p3;
prim4 p4;
prim5 p5;
Tree t1, t2, t3, ff, label, cur, min, max, step, type, name, file, slot, body, chan;
tvec wf;
xtended* xt = (xtended*)getUserData(box);
// Extended Primitives
if (xt) {
faustassert(lsig.size() == xt->arity());
return makeList(xt->computeSigOutput(lsig));
}
// Numbers and Constants
else if (isBoxInt(box, &i)) {
faustassert(lsig.size() == 0);
return makeList(sigInt(i));
} else if (isBoxReal(box, &r)) {
faustassert(lsig.size() == 0);
return makeList(sigReal(r));
}
// A Waveform has two outputs it size and a period signal representing its content
else if (isBoxWaveform(box)) {
faustassert(lsig.size() == 0);
const tvec br = box->branches();
return listConcat(makeList(sigInt(int(br.size()))), makeList(sigWaveform(br)));
}
else if (isBoxFConst(box, type, name, file)) {
faustassert(lsig.size() == 0);
return makeList(sigFConst(type, name, file));
}
else if (isBoxFVar(box, type, name, file)) {
faustassert(lsig.size() == 0);
return makeList(sigFVar(type, name, file));
}
// Wire and Cut
else if (isBoxCut(box)) {
faustassert(lsig.size() == 1);
return siglist();
}
else if (isBoxWire(box)) {
faustassert(lsig.size() == 1);
return lsig;
}
// Slots and Symbolic Boxes
else if (isBoxSlot(box)) {
Tree sig;
faustassert(lsig.size() == 0);
if (!searchEnv(box, sig, slotenv)) {
// test YO simplification des diagrammes
// fprintf(stderr, "propagate : internal error (slot undefined)\n");
sig = sigInput(++gGlobal->gDummyInput);
}
return makeList(sig);
}
else if (isBoxSymbolic(box, slot, body)) {
faustassert(lsig.size() > 0);
return propagate(pushEnv(slot, lsig[0], slotenv), path, body, listRange(lsig, 1, (int)lsig.size()));
}
// Primitives
else if (isBoxPrim0(box, &p0)) {
faustassert(lsig.size() == 0);
return makeList(p0());
}
else if (isBoxPrim1(box, &p1)) {
faustassert(lsig.size() == 1);
num n;
if (isNum(lsig[0], n)) {
return makeList(simplify(p1(lsig[0])));
} else {
return makeList(p1(lsig[0]));
}
}
else if (isBoxPrim2(box, &p2)) {
// printf("prim2 recoit : "); print(lsig); printf("\n");
faustassert(lsig.size() == 2);
if (p2 == &sigEnable) {
if (gGlobal->gEnableFlag) {
// special case for sigEnable that requires a transformation
// enable(X,Y) -> sigControl(X*Y, Y!=0)
return makeList(sigControl(sigMul(lsig[0], lsig[1]), sigNE(lsig[1], sigReal(0.0))));
} else {
// If gEnableFlag is false we replace enable by a simple multiplication
return makeList(sigMul(lsig[0], lsig[1]));
}
} else if (p2 == &sigControl) {
if (gGlobal->gEnableFlag) {
// special case for sigControl that requires a transformation
// control(X,Y) -> sigControl(X, Y!=0)
return makeList(sigControl(lsig[0], sigNE(lsig[1], sigReal(0.0))));
} else {
// If gEnableFlag is false we replace control by identity function
return makeList(lsig[0]);
}
} else {
num n, m;
if (isNum(lsig[0], n) && isNum(lsig[1], m)) {
return makeList(simplify(p2(lsig[0], lsig[1])));
} else {
return makeList(p2(lsig[0], lsig[1]));
}
}
}
else if (isBoxPrim3(box, &p3)) {
faustassert(lsig.size() == 3);
return makeList(p3(lsig[0], lsig[1], lsig[2]));
}
else if (isBoxPrim4(box, &p4)) {
faustassert(lsig.size() == 4);
return makeList(p4(lsig[0], lsig[1], lsig[2], lsig[3]));
}
else if (isBoxPrim5(box, &p5)) {
faustassert(lsig.size() == 5);
return makeList(p5(lsig[0], lsig[1], lsig[2], lsig[3], lsig[4]));
}
else if (isBoxFFun(box, ff)) {
// cerr << "propagate en boxFFun of arity " << ffarity(ff) << endl;
faustassert(int(lsig.size()) == ffarity(ff));
return makeList(sigFFun(ff, listConvert(lsig)));
}
// User Interface Widgets
else if (isBoxButton(box, label)) {
faustassert(lsig.size() == 0);
return makeList(sigButton(normalizePath(cons(label, path))));
}
else if (isBoxCheckbox(box, label)) {
faustassert(lsig.size() == 0);
return makeList(sigCheckbox(normalizePath(cons(label, path))));
}
else if (isBoxVSlider(box, label, cur, min, max, step)) {
faustassert(lsig.size() == 0);
Tree slider = sigVSlider(normalizePath(cons(label, path)), cur, min, max, step);
// Possibly limit the value in [min..max]
if (gGlobal->gRangeUI) {
return makeList(tree(gGlobal->gMaxPrim->symbol(), min, tree(gGlobal->gMinPrim->symbol(), max, slider)));
} else {
return makeList(slider);
}
}
else if (isBoxHSlider(box, label, cur, min, max, step)) {
faustassert(lsig.size() == 0);
Tree slider = sigHSlider(normalizePath(cons(label, path)), cur, min, max, step);
// Possibly limit the value in [min..max]
if (gGlobal->gRangeUI) {
return makeList(tree(gGlobal->gMaxPrim->symbol(), min, tree(gGlobal->gMinPrim->symbol(), max, slider)));
} else {
return makeList(slider);
}
}
else if (isBoxNumEntry(box, label, cur, min, max, step)) {
faustassert(lsig.size() == 0);
Tree nentry = sigNumEntry(normalizePath(cons(label, path)), cur, min, max, step);
// Possibly limit the value in [min..max]
if (gGlobal->gRangeUI) {
return makeList(tree(gGlobal->gMaxPrim->symbol(), min, tree(gGlobal->gMinPrim->symbol(), max, nentry)));
} else {
return makeList(nentry);
}
}
else if (isBoxVBargraph(box, label, min, max)) {
faustassert(lsig.size() == 1);
return makeList(sigVBargraph(normalizePath(cons(label, path)), min, max, lsig[0]));
}
else if (isBoxHBargraph(box, label, min, max)) {
faustassert(lsig.size() == 1);
return makeList(sigHBargraph(normalizePath(cons(label, path)), min, max, lsig[0]));
}
else if (isBoxSoundfile(box, label, chan)) {
faustassert(lsig.size() == 2);
Tree soundfile = sigSoundfile(normalizePath(cons(label, path)));
Tree part = sigIntCast(lsig[0]);
int c = tree2int(chan);
siglist lsig2(c + 2);
lsig2[0] = sigSoundfileLength(soundfile, part);
lsig2[1] = sigSoundfileRate(soundfile, part);
// compute bound limited read index : int(max(0, min(ridx,length-1)))
Tree ridx = sigIntCast(tree(gGlobal->gMaxPrim->symbol(), sigInt(0),
tree(gGlobal->gMinPrim->symbol(), lsig[1], sigAdd(lsig2[0], sigInt(-1)))));
for (int i1 = 0; i1 < c; i1++) {
lsig2[i1 + 2] = sigSoundfileBuffer(soundfile, sigInt(i1), part, ridx);
}
return lsig2;
}
// User Interface Groups
else if (isBoxVGroup(box, label, t1)) {
return propagate(slotenv, cons(cons(tree(0), label), path), t1, lsig);
}
else if (isBoxHGroup(box, label, t1)) {
return propagate(slotenv, cons(cons(tree(1), label), path), t1, lsig);
}
else if (isBoxTGroup(box, label, t1)) {
return propagate(slotenv, cons(cons(tree(2), label), path), t1, lsig);
}
// Block Diagram Composition Algebra
else if (isBoxSeq(box, t1, t2)) {
int in1, out1, in2, out2;
getBoxType(t1, &in1, &out1);
getBoxType(t2, &in2, &out2);
faustassert(out1 == in2);
if (out1 == in2) {
return propagate(slotenv, path, t2, propagate(slotenv, path, t1, lsig));
} else if (out1 > in2) {
siglist lr = propagate(slotenv, path, t1, lsig);
return listConcat(propagate(slotenv, path, t2, listRange(lr, 0, in2)), listRange(lr, in2, out1));
} else {
return propagate(slotenv, path, t2,
listConcat(propagate(slotenv, path, t1, listRange(lsig, 0, in1)),
listRange(lsig, in1, in1 + in2 - out1)));
}
}
else if (isBoxPar(box, t1, t2)) {
int in1, out1, in2, out2;
getBoxType(t1, &in1, &out1);
getBoxType(t2, &in2, &out2);
return listConcat(propagate(slotenv, path, t1, listRange(lsig, 0, in1)),
propagate(slotenv, path, t2, listRange(lsig, in1, in1 + in2)));
}
else if (isBoxSplit(box, t1, t2)) {
int in1, out1, in2, out2;
getBoxType(t1, &in1, &out1);
getBoxType(t2, &in2, &out2);
siglist l1 = propagate(slotenv, path, t1, lsig);
siglist l2 = split(l1, in2);
return propagate(slotenv, path, t2, l2);
}
else if (isBoxMerge(box, t1, t2)) {
int in1, out1, in2, out2;
getBoxType(t1, &in1, &out1);
getBoxType(t2, &in2, &out2);
siglist l1 = propagate(slotenv, path, t1, lsig);
siglist l2 = mix(l1, in2);
return propagate(slotenv, path, t2, l2);
}
else if (isBoxRec(box, t1, t2)) {
// Bug Corrected
int in1, out1, in2, out2;
getBoxType(t1, &in1, &out1);
getBoxType(t2, &in2, &out2);
Tree slotenv2 = lift(slotenv); // the environment must also be lifted
siglist l0 = makeMemSigProjList(ref(1), in2);
siglist l1 = propagate(slotenv2, path, t2, l0);
siglist l2 = propagate(slotenv2, path, t1, listConcat(l1, listLift(lsig)));
siglist l3 = (gGlobal->gFTZMode > 0) ? wrapWithFTZ(l2) : l2;
Tree g = rec(listConvert(l3));
// compute output list of recursive signals
siglist ol(out1); // output list
int p = 0; // projection number
for (auto exp : l3) {
if (exp->aperture() > 0) {
// it is a regular recursive expression branch
ol[p] = sigDelay0(sigProj(p, g));
} else {
// this expression is a closed term,
// it don't need to be inside this recursion group.
// cerr << "degenerate recursion " << exp << endl;
ol[p] = exp;
}
p++;
}
return ol;
}
else if (isBoxEnvironment(box)) {
faustassert(lsig.size() == 0);
return siglist();
} else if (isBoxRoute(box, t1, t2, t3)) {
int ins, outs;
vector<int> route;
siglist outsigs;
// cerr << "TRACE propagate into a route " << boxpp(box) << endl;
if (isBoxInt(t1, &ins) && isBoxInt(t2, &outs) && isIntTree(t3, route)) {
// initialize output signals
for (int i1 = 0; i1 < outs; i1++) outsigs.push_back(sigInt(0));
// route propagation
size_t m = route.size() - 1;
for (size_t i1 = 0; i1 < m; i1 += 2) {
int src = route[i1];
int dst = route[i1 + 1];
if ((dst > 0) & (dst <= outs)) {
// we have a destination
Tree exp = outsigs[dst - 1];
if ((src > 0) & (src <= ins)) {
// we have a source
outsigs[dst - 1] = sigAdd(exp, lsig[src - 1]);
}
}
}
return outsigs;
} else {
stringstream error;
error << "ERROR in file " << __FILE__ << ':' << __LINE__ << ", invalid route expression : " << boxpp(box)
<< endl;
throw faustexception(error.str());
}
}
stringstream error;
error << "ERROR in file " << __FILE__ << ':' << __LINE__ << ", unrecognised box expression : " << boxpp(box)
<< endl;
throw faustexception(error.str());
return siglist();
}
/**
* Top level propagate a list of signals into a block diagram. Do memoization.
* @param path user interface group path
* @param box the block diagram
* @param lsig the list of input signals to propagate
* @return the resulting list of output signals
*/
Tree boxPropagateSig(Tree path, Tree box, const siglist& lsig)
{
return listConvert(propagate(gGlobal->nil, path, box, lsig));
}
|