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 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800
|
/*
ugens2.c:
Copyright (C) 1991 Barry Vercoe, John ffitch, Robin Whittle
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "csoundCore.h" /* UGENS2.C */
#include "ugens2.h"
#include <math.h>
/* Macro form of Istvan's speedup ; constant should be 3fefffffffffffff */
/* #define FLOOR(x) (x >= FL(0.0) ? (int64_t)x */
/* : (int64_t)((double)x - 0.999999999999999))
*/
/* 1.0-1e-8 is safe for a maximum table length of 16777216 */
/* 1.0-1e-15 could incorrectly round down large negative integers, */
/* because doubles do not have sufficient resolution for numbers like */
/* -1000.999999999999999 (FLOOR(-1000) might possibly be -1001 which is wrong)*/
/* it should be noted, though, that the above incorrect result would not be */
/* a problem in the case of interpolating table opcodes, as the fractional */
/* part would then be exactly 1.0, still giving a correct output value */
#define MYFLOOR(x) (x >= FL(0.0) ? (int32_t)x : (int32_t)((double)x - 0.99999999))
int32_t phsset(CSOUND *csound, PHSOR *p)
{
MYFLT phs;
int32_t longphs;
if ((phs = *p->iphs) >= FL(0.0)) {
if (UNLIKELY((longphs = (int32_t)phs))) {
csound->Warning(csound, Str("init phase truncation\n"));
}
p->curphs = phs - (MYFLT)longphs;
}
return OK;
}
int32_t ephsset(CSOUND *csound, EPHSOR *p)
{
MYFLT phs;
int32_t longphs;
if ((phs = *p->iphs) >= FL(0.0)) {
if (UNLIKELY((longphs = (int32_t)phs))) {
csound->Warning(csound, Str("init phase truncation\n"));
}
p->curphs = phs - (MYFLT)longphs;
}
p->b = 1.0;
return OK;
}
int32_t ephsor(CSOUND *csound, EPHSOR *p)
{
double phase;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
MYFLT *rs, *aphs, onedsr = csound->onedsr;
double b = p->b;
double incr, R = *p->kR;
rs = p->sr;
if (UNLIKELY(offset)) memset(rs, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&rs[nsmps], '\0', early*sizeof(MYFLT));
}
aphs = p->aphs;
phase = p->curphs;
if (IS_ASIG_ARG(p->xcps)) {
MYFLT *cps = p->xcps;
for (n=offset; n<nsmps; n++) {
incr = (double)(cps[n] * onedsr);
rs[n] = (MYFLT) b;
aphs[n] = (MYFLT) phase;
phase += incr;
b *= R;
if (UNLIKELY(phase >= 1.0)) {
phase -= 1.0;
b = pow(R, 1.0+phase);
}
else if (UNLIKELY(phase < 0.0)) {
phase += 1.0;
b = pow(R, 1.0+phase);
}
}
}
else {
incr = (double)(*p->xcps * onedsr);
for (n=offset; n<nsmps; n++) {
rs[n] = (MYFLT) b;
aphs[n] = (MYFLT) phase;
phase += incr;
b *= R;
if (UNLIKELY(phase >= 1.0)) {
phase -= 1.0;
b = pow(R, 1.0+phase);
}
else if (UNLIKELY(phase < 0.0)) {
phase += 1.0;
b = pow(R, 1.0+phase);
}
}
}
p->curphs = phase;
p->b = b;
return OK;
}
int32_t kphsor(CSOUND *csound, PHSOR *p)
{
IGN(csound);
double phs;
*p->sr = (MYFLT)(phs = p->curphs);
if (UNLIKELY((phs += (double)*p->xcps * CS_ONEDKR) >= 1.0))
phs -= 1.0;
else if (UNLIKELY(phs < 0.0))
phs += 1.0;
p->curphs = phs;
return OK;
}
int32_t phsor(CSOUND *csound, PHSOR *p)
{
double phase;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
MYFLT *rs, onedsr = csound->onedsr;
double incr;
rs = p->sr;
if (UNLIKELY(offset)) memset(rs, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&rs[nsmps], '\0', early*sizeof(MYFLT));
}
phase = p->curphs;
if (IS_ASIG_ARG(p->xcps)) {
MYFLT *cps = p->xcps;
for (n=offset; n<nsmps; n++) {
incr = (double)(cps[n] * onedsr);
rs[n] = (MYFLT)phase;
phase += incr;
if (UNLIKELY((MYFLT)phase >= FL(1.0))) /* VL convert to MYFLT
to avoid rounded output
exceeding 1.0 on float version */
phase -= 1.0;
else if (UNLIKELY((MYFLT)phase < FL(0.0)))
phase += 1.0;
}
}
else {
incr = (double)(*p->xcps * onedsr);
for (n=offset; n<nsmps; n++) {
rs[n] = (MYFLT)phase;
phase += incr;
if (UNLIKELY((MYFLT)phase >= FL(1.0))) {
phase -= 1.0;
}
else if (UNLIKELY((MYFLT)phase < FL(0.0)))
phase += 1.0;
}
}
p->curphs = phase;
return OK;
}
#ifdef SOME_FINE_DAY
/*****************************************************************************/
/*****************************************************************************/
/* Table read code - see TABLE data structure in ugens2.h. */
/*************************************/
/* itblchk()
*
* This is called at init time by tblset() to set up the TABLE data
* structure for subsequent k and a rate operations.
*
* It is also called at init time by itable() and itablei() prior to
* them calling ktable() and ktabli() respectively to produce a single
* result at init time.
*
* A similar function - ptblchk() does the same job, but reports
* errors in a way suitable for performance time. */
/* If the specified table number can be found, then the purpose is to
* read the three i rate input variables and the function table number
* input variable - (which we can assume here is also i rate) to set
* up the TABLE data structure ready for the k and a rate functions. */
static int32_t itblchk(CSOUND *csound, TABLE *p)
{
if (UNLIKELY((p->ftp = csound->FTnp2Finde(csound, p->xfn)) == NULL))
return NOTOK;
/* Although TABLE has an integer variable for the table number
* (p->pfn) we do not need to write it. We know that the k
* and a rate functions which will follow will not be
* expecting a changed table number.
*
* p->pfn exists only for checking table number changes for
* functions which are expecting a k rate table number. */
/* Set denormalisation factor to 1 or table length, depending
* on the state of ixmode. */
if (*p->ixmode)
p->xbmul = p->ftp->flen;
else
p->xbmul = 1L;
/* Multiply the ixoff value by the xbmul denormalisation
* factor and then check it is between 0 and the table length.
*
* Bug fix: was p->offset * *p->ixoff */
if (UNLIKELY((p->offset = p->xbmul * *p->ixoff) < FL(0.0) ||
p->offset > p->ftp->flen)) {
return csound->InitError(csound, Str("Offset %f < 0 or > tablelength"),
p->offset);
}
p->wrap = (int32_t)*p->iwrap;
return OK;
}
/* ptblchk()
*
* This is called at init time by tblsetkt() to set up the TABLE data
* structure for subsequent k and a rate operations which are
* expecting the table number to change at k rate.
*
* tblsetkt() does very little - just setting up the wrap variable in
* TABLE. All the other variables depend on the table number. This is
* not available at init time, so the following 4 functions must look
* for the changed table number and set up the variables accordingly -
* generating error messages in a way which works at performance time.
*
* k rate a rate
*
* ktablekt tablekt Non interpolated
* ktablikt tablikt Interpolated
* */
static int32_t ptblchk(CSOUND *csound, TABLE *p)
{
IGN(csound); /* Argument is needed to fit structure */
/* TABLE has an integer variable for the previous table number
* (p->pfn).
*
* Now (at init time) we do not know the function table number
* which will be provided at perf time, so set p->pfn to 0, so
* that the k or a rate code will recognise that the first table
* number is different from the "previous" one. */
p->pfn = 0;
/* The only other thing to do is write the wrap value into the
* immediate copy of it in TABLE. */
p->wrap = (int32_t)*p->iwrap;
return OK;
}
/*---------------------------------------------------------------------------*/
/* tblset() */
int32_t tblset(CSOUND *csound, TABLE *p)
{
if (UNLIKELY(p->XINCODE != p->XOUTCODE)) {
const char *opname = csound->GetOpcodeName(p);
const char *msg = Str("%s: table index type inconsistent with output");
if (UNLIKELY(CS_KSMPS == 1))
csound->Warning(csound, msg, opname);
else {
return csound->InitError(csound, msg, opname);
}
}
p->h.iopadr = (SUBR) itblchk;
return itblchk(csound, p);
}
/* tblsetkt() */
int32_t tblsetkt(CSOUND *csound, TABLE *p)
{
if (UNLIKELY(p->XINCODE != p->XOUTCODE)) {
const char *opname = csound->GetOpcodeName(p);
const char *msg = Str("%s: table index type inconsistent with output");
if (UNLIKELY(CS_KSMPS == 1))
csound->Warning(csound, msg, opname);
else {
return csound->InitError(csound, msg, opname);
}
}
p->h.iopadr = (SUBR) ptblchk;
return ptblchk(csound, p);
}
/*************************************/
/* Special functions to use when the output value is an init time
* variable.
*
* These are called by the opodlst lines for itable and itablei ugens.
*
* They call itblchk() and if the table was found, they call the k
* rate function just once.
*
* If the table was not found, an error will result from ftfind. */
int32_t ktable(CSOUND *,TABLE*);
int32_t ktabli(CSOUND *,TABLE*);
int32_t ktabl3(CSOUND *,TABLE*);
int32_t itable(CSOUND *csound, TABLE *p)
{
if (LIKELY(itblchk(csound,p)==OK)) return ktable(csound,p);
return NOTOK;
}
int32_t itabli(CSOUND *csound, TABLE *p)
{
if (LIKELY(itblchk(csound,p)==OK)) return ktabli(csound,p);
return NOTOK;
}
int32_t itabl3(CSOUND *csound, TABLE *p)
{
if (LIKELY(itblchk(csound,p)==OK)) return ktabl3(csound,p);
return NOTOK;
}
/*---------------------------------------------------------------------------*/
/* Functions which read the table.
*
* First we have the four basic functions for a and k rate, non
* interpolated and interpolated reading. These all assume that the
* TABLE data structure has been correctly set up - they are not
* expecting the table number to change at k rate.
*
* These are:
* k rate a rate
*
* ktable table Non interpolated
* ktabli tabli Interpolated
* ktabl3 tabl3 Interpolated with cubic
*
* Then we have four more functions which are expecting the table
* number to change at k rate. They deal with this, and then call one
* of the above functions to do the reading.
*
* These are:
* k rate a rate
*
* ktablekt tablekt Non interpolated
* ktablikt tablikt Interpolated
* */
/* ktable() and ktabli()
* ---------------------
*
* These both read a single value from the table. ktabli() does it
* with interpolation.
*
* This is typically used for k rate reading - where they are called
* as a result of being listed in a line in engineState.opcodlst. They are also
* called by two functions which after they have coped with any change
* in the k rate function table number.
*
* ktablekt() and ktablikt().
*
* In addition, they can be called by the init time functions:
* itable() and itabli().
*
*
* tablefn() and tabli()
* -------------------
*
* These do the reading at a rate with an a rate index.
*
* They are called directly via their entries in engineState.opcodlst, and also by
* two functions which call them after they have coped with any change
* in the k rate function table number.
*
* tablekt() and tablikt().
*
* */
/*************************************/
/* ktable() */
int32_t ktable(CSOUND *csound, TABLE *p)
{
FUNC *ftp;
int32_t indx, length;
MYFLT ndx;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1; /* RWD fix */
ndx = *p->xndx;
length = ftp->flen;
/* Multiply ndx by denormalisation factor, and add in the offset
* - already denormalised - by tblchk().
* xbmul = 1 or table length depending on state of ixmode. */
ndx = (ndx * p->xbmul) + p->offset;
/* ndx now includes the offset and is ready to address the table.
*
* The original code was:
* indx = (int64_t) (ndx + p->offset);
*
* This is a problem, causes problems with negative numbers.
*
*/
indx = (int32_t) MYFLOOR((double)ndx);
/* Now for "limit mode" - the "non-wrap" function, depending on
* iwrap.
*
* The following section of code limits the final index to 0 and
* the last location in the table.
*
* It is only used when wrap is OFF. The wrapping is achieved by
* code after this - when this code is not run. */
if (!p->wrap) {
/* Previously this code limited the upper range of the indx to
* the table length - for instance 8. Then the result was ANDed
* with a mask (for instance 7).
*
* This meant that when the input index was 8 or above, it got
* reduced to 0. What we want is for it to stick at the index
* which reads the last value from the table - in this example
* from location 7.
*
* So instead of limiting to the table length, we limit to
* (table length - 1). */
if (UNLIKELY(indx > length - 1))
indx = length - 1;
/* Now limit negative values to zero. */
else if (UNLIKELY(indx < 0L))
indx = 0L;
}
/* The following code uses an AND with an integer like 0000 0111
* to wrap the current index within the range of the table. In
* the original version, this code always ran, but with the new
* (length - 1) code above, it would have no effect, so it is now
* an else operation - running only when iwrap = 1. This may save
* half a usec or so. */
else indx &= ftp->lenmask;
/* Now find the address of the start of the table, add it to the
* index, read the value from there and write it to the
* destination. */
*p->rslt = *(ftp->ftable + indx);
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("table(krate): not initialised"));
}
/* tablefn() */
/* table() is similar to ktable() above, except that it processes an
* array of input indexes, to send results to another array. These
* arrays are ksmps long. */
/*sbrandon: NeXT m68k does not like 'table' */
int32_t tablefn(CSOUND *csound, TABLE *p)
{
FUNC *ftp;
MYFLT *rslt, *pxndx, *tab;
int32_t indx, mask, length;
uint32_t koffset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
MYFLT ndx, xbmul, offset;
int32_t wrap = p->wrap;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1; /* RWD fix */
rslt = p->rslt;
if (UNLIKELY(koffset)) memset(rslt, '\0', koffset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&rslt[nsmps], '\0', early*sizeof(MYFLT));
}
length = ftp->flen;
pxndx = p->xndx;
xbmul = (MYFLT)p->xbmul;
offset = p->offset;
mask = ftp->lenmask;
tab = ftp->ftable;
for (n=koffset; n<nsmps; n++) {
/* Read in the next raw index and increment the pointer ready
* for the next cycle.
*
* Then multiply the ndx by the denormalising factor and add in
* the offset. */
ndx = (pxndx[n] * xbmul) + offset;
indx = (int32_t) MYFLOOR((double)ndx);
/* Limit = non-wrap. Limits to 0 and (length - 1), or does the
* wrap code. See notes above in ktable(). */
if (!wrap) {
if (UNLIKELY(indx > length - 1))
indx = length - 1;
else if (UNLIKELY(indx < (int32_t)0))
indx = 0L;
}
/* do the wrap code only if we are not doing the non-wrap code. */
else
indx &= mask;
rslt[n] = tab[indx];
}
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("table: not initialised"));
}
/* ktabli() */
/* ktabli() is similar to ktable() above, except that it uses the
* fractional part of the final index to interpolate between one value
* in the table and the next.
*
* This means that it may read the guard point. In a table of
* "length" 8, the guardpoint is at locaton 8. The normal part of the
* table is locations 0 to 7.
*
* In non-wrap mode, when the final index is negative, the output
* should be the value in location 0.
*
* In non-wrap mode, when the final index is >= length, then the
* output should be the value in the guard point location. */
int32_t ktabli(CSOUND *csound, TABLE *p)
{
FUNC *ftp;
int32_t indx, length;
MYFLT v1, v2, fract, ndx;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
ndx = *p->xndx;
length = ftp->flen;
/* Multiply ndx by denormalisation factor.
* xbmul is 1 or table length depending on state of ixmode.
* Add in the offset, which has already been denormalised by
* tblchk(). */
ndx = (ndx * p->xbmul) + p->offset;
indx = (int32_t) MYFLOOR((double)ndx);
/* We need to generate a fraction - How much above indx is ndx?
* It will be between 0 and just below 1.0. */
fract = ndx - indx;
/* Start of changes to fix non- wrap bug.
*
* There are two changes here:
*
* 1 - We only run the wrap code if iwrap = 1. Previously it was
* always run.
*
* 2 - The other change is similar in concept to limiting the
* index to (length - 1) when in non-wrap mode.
*
* This would be fine - the fractional code would enable us to
* interpolate using an index value which is almost as high as the
* length of the table. This would be good for 7.99999 etc.
* However, to be a little pedantic, what we want is for any index
* of 8 or more to produce a result exactly equal to the value at
* the guard point.
*
* We will let all (non negative) values which are less than
* length pass through. This deals with all cases 0 to 7.9999
* . . .
*
* However we will look for final indexes of length (8) and above
* and take the following steps:
*
* fract = 1
* indx = length - 1
*
* We then continue with the rest of code. This causes the result
* to be the value read from the guard point - which is what we
* want.
*
* Likewise, if the final index is negative, set both fract and
* indx to 0. */
if (!p->wrap) {
if (UNLIKELY(ndx > length)) {
indx = length - 1;
fract = FL(1.0);
}
else if (UNLIKELY(indx < 0L)) {
indx = 0L;
fract = FL(0.0);
}
}
/* We are in wrap mode, so do the wrap function. */
else indx &= ftp->lenmask;
/* Now read the value at indx and the one beyond */
v1 = *(ftp->ftable + indx);
v2 = *(ftp->ftable + indx + 1);
*p->rslt = v1 + (v2 - v1) * fract;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("tablei(krate): not initialised"));
}
int32_t ktabl3(CSOUND *csound, TABLE *p)
{
FUNC *ftp;
int32_t indx, length;
MYFLT v1, v2, fract, ndx;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
ndx = *p->xndx;
length = ftp->flen;
/* Multiply ndx by denormalisation factor.
* xbmul is 1 or table length depending on state of ixmode.
* Add in the offset, which has already been denormalised by
* tblchk(). */
ndx = (ndx * p->xbmul) + p->offset;
indx = (int32_t) MYFLOOR((double)ndx);
/* We need to generate a fraction - How much above indx is ndx?
* It will be between 0 and just below 1.0. */
fract = ndx - indx;
/* Start of changes to fix non- wrap bug.
*
* There are two changes here:
*
* 1 - We only run the wrap code if iwrap = 1. Previously it was
* always run.
*
* 2 - The other change is similar in concept to limiting the
* index to (length - 1) when in non-wrap mode.
*
* This would be fine - the fractional code would enable us to
* interpolate using an index value which is almost as high as the
* length of the table. This would be good for 7.99999 etc.
* However, to be a little pedantic, what we want is for any index
* of 8 or more to produce a result exactly equal to the value at
* the guard point.
*
* We will let all (non negative) values which are less than
* length pass through. This deals with all cases 0 to 7.9999
* . . .
*
* However we will look for final indexes of length (8) and above
* and take the following steps:
*
* fract = 1
* indx = length - 1
*
* We then continue with the rest of code. This causes the result
* to be the value read from the guard point - which is what we
* want.
*
* Likewise, if the final index is negative, set both fract and
* indx to 0. */
if (!p->wrap) {
if (UNLIKELY(ndx > length)) {
indx = length - 1;
fract = FL(1.0);
}
else if (UNLIKELY(indx < 0L)) {
indx = 0L;
fract = FL(0.0);
}
}
/* We are in wrap mode, so do the wrap function. */
else indx &= ftp->lenmask;
/* interpolate with cubic if we can, else linear */
if (UNLIKELY(indx<1 || indx==length-1 || length <4)) {
v1 = *(ftp->ftable + indx);
v2 = *(ftp->ftable + indx + 1);
*p->rslt = v1 + (v2 - v1) * fract;
}
else {
MYFLT *tab = ftp->ftable;
MYFLT ym1 = tab[indx-1], y0 = tab[indx];
MYFLT y1 = tab[indx+1], y2 = tab[indx+2];
MYFLT frsq = fract*fract;
MYFLT frcu = frsq*ym1;
MYFLT t1 = y2 + y0+y0+y0;
*p->rslt = y0 + FL(0.5)*frcu
+ fract*(y1 - frcu/FL(6.0) - t1/FL(6.0) - ym1/FL(3.0))
+ frsq*fract*(t1/FL(6.0) - FL(0.5)*y1) + frsq*(FL(0.5)* y1 - y0);
}
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("table3(krate): not initialised"));
}
/* tabli() */
/* tabli() is similar to ktabli() above, except that it processes an
* array of input indexes, to send results to another array. */
int32_t tabli(CSOUND *csound, TABLE *p)
{
FUNC *ftp;
int32_t indx, mask, length;
uint32_t koffset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
MYFLT *rslt, *pxndx, *tab;
MYFLT fract, v1, v2, ndx, xbmul, offset;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
rslt = p->rslt;
if (UNLIKELY(koffset)) memset(rslt, '\0', koffset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&rslt[nsmps], '\0', early*sizeof(MYFLT));
}
length = ftp->flen;
pxndx = p->xndx;
xbmul = (MYFLT)p->xbmul;
offset = p->offset;
mask = ftp->lenmask;
tab = ftp->ftable;
/* As for ktabli() code to handle non wrap mode, and wrap mode. */
if (!p->wrap) {
for (n=koffset; n<nsmps; n++) {
/* Read in the next raw index and increment the pointer ready
* for the next cycle.
* Then multiply the ndx by the denormalising factor and add in
* the offset. */
ndx = (pxndx[n] * xbmul) + offset;
indx = (int32_t) ndx;
if (UNLIKELY(indx <= 0L)) {
rslt[n] = tab[0];
continue;
}
if (UNLIKELY(indx >= length)) {
rslt[n] = tab[length];
continue;
}
/* We need to generate a fraction - How much above indx is ndx?
* It will be between 0 and just below 1.0. */
fract = ndx - indx;
/* As for ktabli(), read two values and interpolate between
* them. */
v1 = tab[indx];
v2 = tab[indx + 1];
rslt[n] = v1 + (v2 - v1)*fract;
}
}
else { /* wrap mode */
for (n=koffset; n<nsmps; n++) {
/* Read in the next raw index and increment the pointer ready
* for the next cycle.
* Then multiply the ndx by the denormalising factor and add in
* the offset. */
ndx = (pxndx[n] * xbmul) + offset;
indx = (int32_t) MYFLOOR(ndx);
/* We need to generate a fraction - How much above indx is ndx?
* It will be between 0 and just below 1.0. */
fract = ndx - indx;
indx &= mask;
/* As for ktabli(), read two values and interpolate between
* them. */
v1 = tab[indx];
v2 = tab[indx + 1];
rslt[n] = v1 + (v2 - v1)*fract;
}
}
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("tablei: not initialised"));
}
int32_t tabl3(CSOUND *csound, TABLE *p) /* Like tabli but cubic interpolation */
{
FUNC *ftp;
int32_t indx, mask, length;
uint32_t koffset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
MYFLT *rslt, *pxndx, *tab;
MYFLT fract, v1, v2, ndx, xbmul, offset;
int32_t wrap = p->wrap;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
rslt = p->rslt;
if (UNLIKELY(koffset)) memset(rslt, '\0', koffset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&rslt[nsmps], '\0', early*sizeof(MYFLT));
}
length = ftp->flen;
pxndx = p->xndx;
xbmul = (MYFLT)p->xbmul;
offset = p->offset;
mask = ftp->lenmask;
tab = ftp->ftable;
for (n=koffset; n<nsmps; n++) {
/* Read in the next raw index and increment the pointer ready
* for the next cycle.
* Then multiply the ndx by the denormalising factor and add in
* the offset. */
ndx = (pxndx[n] * xbmul) + offset;
indx = (int32_t) MYFLOOR((double)ndx);
/* We need to generate a fraction - How much above indx is ndx?
* It will be between 0 and just below 1.0. */
fract = ndx - indx;
/* As for ktabli() code to handle non wrap mode, and wrap mode. */
if (!wrap) {
if (UNLIKELY(ndx > length)) {
indx = length - 1;
fract = FL(1.0);
}
else if (UNLIKELY(indx < 0L)) {
indx = 0L;
fract = FL(0.0);
}
}
else
indx &= mask;
/* interpolate with cubic if we can */
if (UNLIKELY(indx <1 || indx == length-1 || length<4)) {
/* Too short or at ends */
v1 = tab[indx];
v2 = tab[indx + 1];
rslt[n] = v1 + (v2 - v1)*fract;
}
else {
MYFLT ym1 = tab[indx-1], y0 = tab[indx];
MYFLT y1 = tab[indx+1], y2 = tab[indx+2];
MYFLT frsq = fract*fract;
MYFLT frcu = frsq*ym1;
MYFLT t1 = y2 + y0+y0+y0;
rslt[n] = y0 + FL(0.5)*frcu +
fract*(y1 - frcu/FL(6.0) - t1/FL(6.0) - ym1/FL(3.0)) +
frsq*fract*(t1/FL(6.0) - FL(0.5)*y1) + frsq*(FL(0.5)* y1 - y0);
}
}
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("table3: not initialised"));
}
/*************************************/
/* Four functions to call the above four, after handling the k rate
* table number variable.
*
* tblsetkt() does very little - just setting up the wrap variable in
* TABLE. All the other variables depend on the table number. This is
* not available at init time, so the following 4 functions must look
* for the changed table number and set up the variables accordingly -
* generating error messages in a way which works at performance time.
* * k rate a rate
*
* ktablekt tablekt Non interpolated
* ktablikt tablikt Interpolated
*
* Since these perform identical operations, apart from the function
* they call, create a common function to do this work:
*
* ftkrchk() */
static int32_t ftkrchk(CSOUND *csound, TABLE *p)
{
/* Check the table number is >= 1. Print error and deactivate if
* it is not. Return NOTOK to tell calling function not to proceed
* with a or k rate operations.
*
* We must do this to catch the situation where the first call has
* a table number of 0, and since that equals pfn, we would
* otherwise proceed without checking the table number - and none
* of the pointers would have been set up. */
if (*p->xfn < 1) goto err1;
/* Check to see if table number has changed from previous value.
* On the first run through, the previous value will be 0. */
if (p->pfn != (int32_t)*p->xfn) {
/* If it is different, check to see if the table exists.
*
* If it doesn't, an error message should be produced by
* csoundFTFindP() which should also deactivate the instrument.
*
* Return 0 to tell calling function not to proceed with a or
* k rate operations. */
if (UNLIKELY((p->ftp = csound->FTFindP(csound, p->xfn) ) == NULL)) {
return NOTOK;
}
/* p->ftp now points to the FUNC data structure of the newly
* selected table.
*
* Now we set up some variables in TABLE ready for the k or a
* rate functions which follow. */
/* Write the integer version of the table number into pfn so
* we can later decide whether subsequent calls to the k and a
* rate functions occur with a table number value which points
* to a different table. */
p->pfn = (int32_t)*p->xfn;
/* Set denormalisation factor to 1 or table length, depending
* on the state of ixmode. */
if (*p->ixmode)
p->xbmul = p->ftp->flen;
else p->xbmul = 1L;
/* Multiply the ixoff value by the xbmul denormalisation
* factor and then check it is between 0 and the table length. */
if (UNLIKELY((p->offset = p->xbmul * *p->ixoff) < FL(0.0) ||
p->offset > p->ftp->flen)) goto err2;
}
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("k rate function table no. %f < 1"),
*p->xfn);
err2:
return csound->PerfError(csound, &(p->h),
Str("Offset %f < 0 or > tablelength"),
p->offset);
}
/* Now for the four functions, which are called as a result of being
* listed in engineState.opcodlst in entry.c */
int32_t ktablekt(CSOUND *csound, TABLE *p)
{
if (LIKELY(ftkrchk(csound,p)==OK)) return ktable(csound,p);
return NOTOK;
}
int32_t tablekt(CSOUND *csound, TABLE *p)
{
if (LIKELY(ftkrchk(csound,p)==OK)) return tablefn(csound,p);
return NOTOK;
}
int32_t ktablikt(CSOUND *csound, TABLE *p)
{
if (LIKELY(ftkrchk(csound,p)==OK)) return ktabli(csound,p);
return NOTOK;
}
int32_t tablikt(CSOUND *csound, TABLE *p)
{
if (LIKELY(ftkrchk(csound,p)==OK)) return tabli(csound,p);
return NOTOK;
}
int32_t ktabl3kt(CSOUND *csound, TABLE *p)
{
if (LIKELY(ftkrchk(csound,p)==OK)) return ktabl3(csound,p);
return NOTOK;
}
int32_t tabl3kt(CSOUND *csound, TABLE *p)
{
if (LIKELY(ftkrchk(csound,p)==OK)) return tabl3(csound,p);
return NOTOK;
}
#endif /* SOME_FINE_DAY */
int32_t ko1set(CSOUND *csound, OSCIL1 *p)
{
FUNC *ftp;
if (UNLIKELY((ftp = csound->FTFind(csound, p->ifn)) == NULL))
return NOTOK;
if (UNLIKELY(*p->idur <= FL(0.0))) {
/*csound->Warning(csound, Str("duration < zero\n"));*/
p->phs = MAXLEN-1;
}
else p->phs = 0;
p->ftp = ftp;
p->dcnt = (int32_t)(*p->idel * CS_EKR);
p->kinc = (int32_t) (CS_KICVT / *p->idur);
if (p->kinc==0) p->kinc = 1;
return OK;
}
int32_t kosc1(CSOUND *csound, OSCIL1 *p)
{
FUNC *ftp;
int32_t phs, dcnt;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
phs = p->phs;
*p->rslt = *(ftp->ftable + (phs >> ftp->lobits)) * *p->kamp;
if ((dcnt = p->dcnt) > 0)
dcnt--;
else if (dcnt == 0) {
phs += p->kinc;
if (UNLIKELY(phs >= MAXLEN)){
phs = MAXLEN;
dcnt--;
}
else if (UNLIKELY(phs < 0)){
phs = 0;
dcnt--;
}
p->phs = phs;
}
p->dcnt = dcnt;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscil1(krate): not initialised"));
}
int32_t kosc1i(CSOUND *csound, OSCIL1 *p)
{
FUNC *ftp;
MYFLT fract, v1, *ftab;
int32_t phs, dcnt;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
phs = p->phs;
fract = PFRAC(phs);
ftab = ftp->ftable + (phs >> ftp->lobits);
v1 = *ftab++;
*p->rslt = (v1 + (*ftab - v1) * fract) * *p->kamp;
if ((dcnt = p->dcnt) > 0) {
dcnt--;
p->dcnt = dcnt;
}
else if (dcnt == 0) {
phs += p->kinc;
if (UNLIKELY(phs >= MAXLEN)) {
phs = MAXLEN;
dcnt--;
p->dcnt = dcnt;
} else if (UNLIKELY(phs < 0)){
phs = 0;
dcnt--;
}
p->phs = phs;
}
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscil1i(krate): not initialised"));
}
int32_t oscnset(CSOUND *csound, OSCILN *p)
{
FUNC *ftp;
if (LIKELY((ftp = csound->FTnp2Finde(csound, p->ifn)) != NULL)) {
p->ftp = ftp;
p->inc = ftp->flen * *p->ifrq * csound->onedsr;
p->index = FL(0.0);
p->maxndx = ftp->flen - FL(1.0);
p->ntimes = (int32_t)*p->itimes;
return OK;
}
else return NOTOK;
}
int32_t osciln(CSOUND *csound, OSCILN *p)
{
MYFLT *rs = p->rslt;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
if (UNLIKELY(p->ftp==NULL)) goto err1;
if (UNLIKELY(offset)) memset(rs, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&rs[nsmps], '\0', early*sizeof(MYFLT));
}
if (p->ntimes) {
MYFLT *ftbl = p->ftp->ftable;
MYFLT amp = *p->kamp;
MYFLT ndx = p->index;
MYFLT inc = p->inc;
MYFLT maxndx = p->maxndx;
for (n=offset; n<nsmps; n++) {
rs[n] = ftbl[(int32_t)ndx] * amp;
if (UNLIKELY((ndx += inc) > maxndx)) {
if (--p->ntimes)
ndx -= maxndx;
else if (UNLIKELY(n==nsmps))
return OK;
else
goto putz;
}
}
p->index = ndx;
}
else {
n=0; /* Can jump out of previous loop into this one */
putz:
memset(&rs[n], 0, (nsmps-n)*sizeof(MYFLT));
/* for (; n<nsmps; n++) { */
/* rs[n] = FL(0.0); */
/* } */
}
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("osciln: not initialised"));
}
static int32_t fill_func_from_array(ARRAYDAT *a, FUNC *f)
{
int32_t lobits, ltest, flen, i;
int32_t nonpowof2_flag = 0;
flen = f->flen = a->sizes[0];
flen &= -2L;
for (ltest = flen, lobits = 0;
(ltest & MAXLEN) == 0L;
lobits++, ltest <<= 1)
;
if (UNLIKELY(ltest != MAXLEN)) {
lobits = 0;
nonpowof2_flag = 1;
}
f->ftable = a->data;
f->lenmask = ((flen & (flen - 1L)) ?
0L : (flen - 1L)); /* init hdr w powof2 data */
f->lobits = lobits;
i = (1 << lobits);
f->lomask = (int32_t) (i - 1);
f->lodiv = FL(1.0) / (MYFLT) i; /* & other useful vals */
f->nchanls = 1; /* presume mono for now */
f->flenfrms = flen;
if (nonpowof2_flag)
f->lenmask = 0xFFFFFFFF;
return OK;
}
int32_t oscsetA(CSOUND *csound, OSC *p)
{
FUNC *ftp = &p->FF;
int32_t x;
if (*p->iphs >= 0)
p->lphs = ((int32_t)(*p->iphs * FMAXLEN)) & PHMASK;
//check p->ifn is a valid array with power-of-two length
x = ((ARRAYDAT*)p->ifn)->sizes[0];
if (LIKELY((x != 0) && !(x & (x - 1)))) {
p->ftp = ftp;
fill_func_from_array((ARRAYDAT*)p->ifn, ftp);
return OK;
}
else return csound->InitError(csound, Str("array size not pow-of-two\n"));
}
int32_t oscset(CSOUND *csound, OSC *p)
{
FUNC *ftp;
if (LIKELY((ftp = csound->FTFind(csound, p->ifn)) != NULL)) {
p->ftp = ftp;
if (*p->iphs >= 0)
p->lphs = ((int32_t)(*p->iphs * FMAXLEN)) & PHMASK;
return OK;
}
return NOTOK;
}
int32_t koscil(CSOUND *csound, OSC *p)
{
FUNC *ftp;
int32_t phs, inc;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
phs = p->lphs;
inc = (int32_t) (*p->xcps * CS_KICVT);
*p->sr = ftp->ftable[phs >> ftp->lobits] * *p->xamp;
phs += inc;
phs &= PHMASK;
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscil(krate): not initialised"));
}
int32_t osckk(CSOUND *csound, OSC *p)
{
FUNC *ftp;
MYFLT amp, *ar, *ftbl;
int32_t phs, inc, lobits;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
ftbl = ftp->ftable;
phs = p->lphs;
inc = MYFLT2LONG(*p->xcps * csound->sicvt);
lobits = ftp->lobits;
amp = *p->xamp;
ar = p->sr;
if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
}
for (n=offset;n<nsmps;n++) {
ar[n] = ftbl[phs >> lobits] * amp;
/* phs += inc; */
/* phs &= PHMASK; */
phs = (phs+inc)&PHMASK;
}
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscil: not initialised"));
}
int32_t oscka(CSOUND *csound, OSC *p)
{
FUNC *ftp;
MYFLT *ar, amp, *cpsp, *ftbl;
int32_t phs, lobits;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
MYFLT sicvt = csound->sicvt;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
ftbl = ftp->ftable;
lobits = ftp->lobits;
amp = *p->xamp;
cpsp = p->xcps;
phs = p->lphs;
ar = p->sr;
if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
}
for (n=offset;n<nsmps;n++) {
int32_t inc = MYFLT2LONG(cpsp[n] * sicvt);
ar[n] = ftbl[phs >> lobits] * amp;
phs += inc;
phs &= PHMASK;
}
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscil: not initialised"));
}
int32_t oscak(CSOUND *csound, OSC *p)
{
FUNC *ftp;
MYFLT *ar, *ampp, *ftbl;
int32_t phs, inc, lobits;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
ftbl = ftp->ftable;
lobits = ftp->lobits;
phs = p->lphs;
inc = MYFLT2LONG(*p->xcps * csound->sicvt);
ampp = p->xamp;
ar = p->sr;
if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
}
for (n=offset;n<nsmps;n++) {
ar[n] = ftbl[phs >> lobits] * ampp[n];
phs = (phs+inc) & PHMASK;
}
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscil: not initialised"));
}
int32_t oscaa(CSOUND *csound, OSC *p)
{
FUNC *ftp;
MYFLT *ar, *ampp, *cpsp, *ftbl;
int32_t phs, lobits;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
MYFLT sicvt = csound->sicvt;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
ftbl = ftp->ftable;
lobits = ftp->lobits;
phs = p->lphs;
ampp = p->xamp;
cpsp = p->xcps;
ar = p->sr;
if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
}
for (n=offset;n<nsmps;n++) {
int32_t inc = MYFLT2LONG(cpsp[n] * sicvt);
ar[n] = ftbl[phs >> lobits] * ampp[n];
phs = (phs+inc) & PHMASK;
}
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscil: not initialised"));
}
int32_t koscli(CSOUND *csound, OSC *p)
{
FUNC *ftp;
int32_t phs, inc;
MYFLT *ftab, fract, v1;
phs = p->lphs;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
fract = PFRAC(phs);
ftab = ftp->ftable + (phs >> ftp->lobits);
v1 = ftab[0];
*p->sr = (v1 + (ftab[1] - v1) * fract) * *p->xamp;
inc = (int32_t)(*p->xcps * CS_KICVT);
phs += inc;
phs &= PHMASK;
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscili(krate): not initialised"));
}
int32_t osckki(CSOUND *csound, OSC *p)
{
FUNC *ftp;
MYFLT fract, v1, amp, *ar, *ft, *ftab;
int32_t phs, inc, lobits;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
if (UNLIKELY((ftp = p->ftp)==NULL)) goto err1;
lobits = ftp->lobits;
phs = p->lphs;
inc = MYFLT2LONG(*p->xcps * csound->sicvt);
amp = *p->xamp;
ar = p->sr;
if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
}
ft = ftp->ftable;
for (n=offset; n<nsmps; n++) {
fract = PFRAC(phs);
ftab = ft + (phs >> lobits);
v1 = ftab[0];
ar[n] = (v1 + (ftab[1] - v1) * fract) * amp;
phs = (phs+inc) & PHMASK;
}
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscili: not initialised"));
}
int32_t osckai(CSOUND *csound, OSC *p)
{
FUNC *ftp;
MYFLT *ar, amp, *cpsp, fract, v1, *ftab, *ft;
int32_t phs, lobits;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
MYFLT sicvt = csound->sicvt;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
lobits = ftp->lobits;
amp = *p->xamp;
cpsp = p->xcps;
phs = p->lphs;
ar = p->sr;
if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
}
ft = ftp->ftable;
for (n=offset;n<nsmps;n++) {
int32_t inc;
inc = MYFLT2LONG(cpsp[n] * sicvt);
fract = PFRAC(phs);
ftab = ft + (phs >> lobits);
v1 = ftab[0];
ar[n] = (v1 + (ftab[1] - v1) * fract) * amp;
phs += inc;
phs &= PHMASK;
}
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscili: not initialised"));
}
int32_t oscaki(CSOUND *csound, OSC *p)
{
FUNC *ftp;
MYFLT v1, fract, *ar, *ampp, *ftab, *ft;
int32_t phs, inc, lobits;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
lobits = ftp->lobits;
phs = p->lphs;
inc = MYFLT2LONG(*p->xcps * csound->sicvt);
ampp = p->xamp;
ar = p->sr;
if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
}
ft = ftp->ftable;
for (n=offset;n<nsmps;n++) {
fract = (MYFLT) PFRAC(phs);
ftab = ft + (phs >> lobits);
v1 = ftab[0];
ar[n] = (v1 + (ftab[1] - v1) * fract) * ampp[n];
phs = (phs+inc) & PHMASK;
}
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscili: not initialised"));
}
int32_t oscaai(CSOUND *csound, OSC *p)
{
FUNC *ftp;
MYFLT v1, fract, *ar, *ampp, *cpsp, *ftab, *ft;
int32_t phs, lobits;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
MYFLT sicvt = csound->sicvt;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
ft = ftp->ftable;
lobits = ftp->lobits;
phs = p->lphs;
ampp = p->xamp;
cpsp = p->xcps;
ar = p->sr;
if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
}
for (n=offset;n<nsmps;n++) {
int32_t inc;
inc = MYFLT2LONG(cpsp[n] * sicvt);
fract = (MYFLT) PFRAC(phs);
ftab = ft + (phs >> lobits);
v1 = ftab[0];
ar[n] = (v1 + (ftab[1] - v1) * fract) * ampp[n];
phs = (phs+inc) & PHMASK;
}
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscili: not initialised"));
}
int32_t koscl3(CSOUND *csound, OSC *p)
{
FUNC *ftp;
int32_t phs, inc;
MYFLT *ftab, fract;
int32_t x0;
MYFLT y0, y1, ym1, y2, amp = *p->xamp;
phs = p->lphs;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
ftab = ftp->ftable;
fract = PFRAC(phs);
x0 = (phs >> ftp->lobits);
x0--;
if (UNLIKELY(x0<0)) {
ym1 = ftab[ftp->flen-1]; x0 = 0;
}
else ym1 = ftab[x0++];
y0 = ftab[x0++];
y1 = ftab[x0++];
if (UNLIKELY(x0>(int32_t)ftp->flen)) y2 = ftab[1]; else y2 = ftab[x0];
{
MYFLT frsq = fract*fract;
MYFLT frcu = frsq*ym1;
MYFLT t1 = y2 + y0+y0+y0;
*p->sr = amp * (y0 + FL(0.5)*frcu +
fract*(y1 - frcu/FL(6.0) - t1/FL(6.0) - ym1/FL(3.0)) +
frsq*fract*(t1/FL(6.0) - FL(0.5)*y1) +
frsq*(FL(0.5)* y1 - y0));
}
inc = (int32_t)(*p->xcps * CS_KICVT);
phs += inc;
phs &= PHMASK;
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscil3(krate): not initialised"));
}
int32_t osckk3(CSOUND *csound, OSC *p)
{
FUNC *ftp;
MYFLT fract, amp, *ar, *ftab;
int32_t phs, inc, lobits;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
int32_t x0;
MYFLT y0, y1, ym1, y2;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
ftab = ftp->ftable;
lobits = ftp->lobits;
phs = p->lphs;
inc = MYFLT2LONG(*p->xcps * csound->sicvt);
amp = *p->xamp;
ar = p->sr;
if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
}
for (n=offset;n<nsmps;n++) {
fract = PFRAC(phs);
x0 = (phs >> lobits);
x0--;
if (UNLIKELY(x0<0)) {
ym1 = ftab[ftp->flen-1]; x0 = 0;
}
else ym1 = ftab[x0++];
y0 = ftab[x0++];
y1 = ftab[x0++];
if (UNLIKELY(x0>(int32_t)ftp->flen)) y2 = ftab[1]; else y2 = ftab[x0];
/* printf("fract = %f; y = %f, %f, %f, %f\n", fract,ym1,y0,y1,y2); */
{
MYFLT frsq = fract*fract;
MYFLT frcu = frsq*ym1;
MYFLT t1 = y2 + y0+y0+y0;
/* MYFLT old = (y0 + (y1 - y0) * fract) * amp; */
/* double x = ((double)(x0-2)+fract)*twopi/32.0; */
/* MYFLT tr = amp*sin(x); */
ar[n] = amp * (y0 + FL(0.5)*frcu +
fract*(y1 - frcu/FL(6.0) - t1/FL(6.0) - ym1/FL(3.0)) +
frsq*fract*(t1/FL(6.0) - FL(0.5)*y1) +
frsq*(FL(0.5)* y1 - y0));
/* printf("oscilkk3: old=%.4f new=%.4f true=%.4f (%f; %f)\n", */
/* old, *(ar-1), tr, fabs(*(ar-1)-tr), fabs(old-tr)); */
}
phs = (phs+inc) & PHMASK;
}
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscil3: not initialised"));
}
int32_t oscka3(CSOUND *csound, OSC *p)
{
FUNC *ftp;
MYFLT *ar, amp, *cpsp, fract, *ftab;
int32_t phs, lobits;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
int32_t x0;
MYFLT y0, y1, ym1, y2;
MYFLT sicvt = csound->sicvt;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
ftab = ftp->ftable;
lobits = ftp->lobits;
amp = *p->xamp;
cpsp = p->xcps;
phs = p->lphs;
ar = p->sr;
if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
}
for (n=offset;n<nsmps;n++) {
int32_t inc;
inc = MYFLT2LONG(cpsp[n] * sicvt);
fract = PFRAC(phs);
x0 = (phs >> lobits);
x0--;
if (UNLIKELY(x0<0)) {
ym1 = ftab[ftp->flen-1]; x0 = 0;
}
else ym1 = ftab[x0++];
y0 = ftab[x0++];
y1 = ftab[x0++];
if (UNLIKELY(x0>(int32_t)ftp->flen)) y2 = ftab[1]; else y2 = ftab[x0];
{
MYFLT frsq = fract*fract;
MYFLT frcu = frsq*ym1;
MYFLT t1 = y2 + y0+y0+y0;
ar[n] = amp * (y0 + FL(0.5)*frcu +
fract*(y1 - frcu/FL(6.0) - t1/FL(6.0) - ym1/FL(3.0)) +
frsq*fract*(t1/FL(6.0) - FL(0.5)*y1) + frsq*(FL(0.5)*
y1 - y0));
}
phs = (phs+inc) & PHMASK;
}
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscil3: not initialised"));
}
int32_t oscak3(CSOUND *csound, OSC *p)
{
FUNC *ftp;
MYFLT fract, *ar, *ampp, *ftab;
int32_t phs, inc, lobits;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
int32_t x0;
MYFLT y0, y1, ym1, y2;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
ftab = ftp->ftable;
lobits = ftp->lobits;
phs = p->lphs;
inc = MYFLT2LONG(*p->xcps * csound->sicvt);
ampp = p->xamp;
ar = p->sr;
if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
}
for (n=offset;n<nsmps;n++) {
fract = (MYFLT) PFRAC(phs);
x0 = (phs >> lobits);
x0--;
if (UNLIKELY(x0<0)) {
ym1 = ftab[ftp->flen-1]; x0 = 0;
}
else ym1 = ftab[x0++];
y0 = ftab[x0++];
y1 = ftab[x0++];
if (UNLIKELY(x0>(int32_t)ftp->flen)) y2 = ftab[1]; else y2 = ftab[x0];
{
MYFLT frsq = fract*fract;
MYFLT frcu = frsq*ym1;
MYFLT t1 = y2 + y0+y0+y0;
ar[n] = ampp[n] *(y0 + FL(0.5)*frcu
+ fract*(y1 - frcu/FL(6.0) - t1/FL(6.0) - ym1/FL(3.0))
+ frsq*fract*(t1/FL(6.0) - FL(0.5)*y1)
+ frsq*(FL(0.5)* y1 - y0));
}
phs = (phs+inc) & PHMASK;
}
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscil3: not initialised"));
}
int32_t oscaa3(CSOUND *csound, OSC *p)
{
FUNC *ftp;
MYFLT fract, *ar, *ampp, *cpsp, *ftab;
int32_t phs, lobits;
uint32_t offset = p->h.insdshead->ksmps_offset;
uint32_t early = p->h.insdshead->ksmps_no_end;
uint32_t n, nsmps = CS_KSMPS;
int32_t x0;
MYFLT y0, y1, ym1, y2;
MYFLT sicvt = csound->sicvt;
ftp = p->ftp;
if (UNLIKELY(ftp==NULL)) goto err1;
ftab = ftp->ftable;
lobits = ftp->lobits;
phs = p->lphs;
ampp = p->xamp;
cpsp = p->xcps;
ar = p->sr;
if (UNLIKELY(offset)) memset(ar, '\0', offset*sizeof(MYFLT));
if (UNLIKELY(early)) {
nsmps -= early;
memset(&ar[nsmps], '\0', early*sizeof(MYFLT));
}
for (n=offset;n<nsmps;n++) {
int32_t inc = MYFLT2LONG(cpsp[n] * sicvt);
fract = (MYFLT) PFRAC(phs);
x0 = (phs >> lobits);
x0--;
if (UNLIKELY(x0<0)) {
ym1 = ftab[ftp->flen-1]; x0 = 0;
}
else ym1 = ftab[x0++];
y0 = ftab[x0++];
y1 = ftab[x0++];
if (UNLIKELY(x0>(int32_t)ftp->flen)) y2 = ftab[1]; else y2 = ftab[x0];
{
MYFLT frsq = fract*fract;
MYFLT frcu = frsq*ym1;
MYFLT t1 = y2 + y0+y0+y0;
ar[n] = ampp[n] *(y0 + FL(0.5)*frcu
+ fract*(y1 - frcu/FL(6.0) - t1/FL(6.0) - ym1/FL(3.0))
+ frsq*fract*(t1/FL(6.0) - FL(0.5)*y1)
+ frsq*(FL(0.5)* y1 - y0));
}
phs = (phs+inc) & PHMASK;
}
p->lphs = phs;
return OK;
err1:
return csound->PerfError(csound, &(p->h),
Str("oscil3: not initialised"));
}
|