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 1801 1802 1803 1804 1805 1806 1807
|
// ------------------------------------------------------------------------
// eca-engine.cpp: Main processing engine
// Copyright (C) 1999-2009,2012,2015,2020 Kai Vehmanen
// Copyright (C) 2005 Stuart Allie
//
// Attributes:
// eca-style-version: 3 (see Ecasound Programmer's Guide)
//
// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// ------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cmath>
#include <utility>
#include <assert.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <sys/time.h> /* gettimeofday() */
#include <kvu_dbc.h>
#include <kvu_numtostr.h>
#include <kvu_procedure_timer.h>
#include <kvu_rtcaps.h>
#include <kvu_threads.h>
#include "samplebuffer.h"
#include "audioio.h"
#include "audioio-buffered.h"
#include "audioio-device.h"
#include "audioio-db-client.h"
#include "audioio-loop.h"
#include "audioio-barrier.h"
#include "audioio-mp3.h"
#include "midi-server.h"
#include "eca-chain.h"
#include "eca-chainop.h"
#include "eca-error.h"
#include "eca-logger.h"
#include "eca-chainsetup-edit.h"
#include "eca-engine.h"
#include "eca-engine_impl.h"
using std::cerr;
using std::endl;
using std::vector;
/**
* Enable and disable features
*/
/* Profile callback execution */
// #define PROFILE_ENGINE
/**
* Local macro definitions
*/
#ifdef PROFILE_ENGINE_EXECUTION
#define PROFILE_ENGINE_STATEMENT(x) (x)
#else
#define PROFILE_ENGINE_STATEMENT(x) ((void)0)
#endif
/**
* Prototypes of static functions
*/
static void mix_to_outputs_divide_helper(const SAMPLE_BUFFER *from, SAMPLE_BUFFER *to, int divide_by, bool first_time);
static void mix_to_outputs_sum_helper(const SAMPLE_BUFFER *from, SAMPLE_BUFFER *to, bool first_time);
/**
* Implementations of non-static functions
*/
/**********************************************************************
* Driver implementation
**********************************************************************/
int ECA_ENGINE_DEFAULT_DRIVER::exec(ECA_ENGINE* engine, ECA_CHAINSETUP* csetup)
{
bool drain_at_end = false;
engine_repp = engine;
exit_request_rep = false;
engine->init_engine_state();
while(true) {
engine_repp->check_command_queue();
/* case 1: external exit request */
if (exit_request_rep == true) break;
/* case 2: engine running, execute one loop iteration */
if (engine_repp->status() == ECA_ENGINE::engine_status_running) {
engine_repp->engine_iteration();
}
else {
/* case 3a-i: engine finished and in batch mode -> exit */
if (engine_repp->status() == ECA_ENGINE::engine_status_finished &&
engine_repp->batch_mode() == true) {
ECA_LOG_MSG(ECA_LOGGER::system_objects, "batch finished in exec, exit");
drain_at_end = true;
break;
}
/* case 3a-ii: engine error occured -> exit */
else if (engine_repp->status() == ECA_ENGINE::engine_status_error) {
ECA_LOG_MSG(ECA_LOGGER::system_objects, "engine error, exit");
break;
}
/* case 3b: engine not running, wait for commands */
engine_repp->wait_for_commands();
}
engine_repp->update_engine_state();
}
if (engine_repp->is_prepared() == true)
engine_repp->stop_operation(drain_at_end);
return 0;
}
void ECA_ENGINE_DEFAULT_DRIVER::start(void)
{
if (engine_repp->is_prepared() != true) engine_repp->prepare_operation();
engine_repp->start_operation();
}
void ECA_ENGINE_DEFAULT_DRIVER::stop(bool drain)
{
if (engine_repp->is_prepared() == true) engine_repp->stop_operation(drain);
}
void ECA_ENGINE_DEFAULT_DRIVER::exit(void)
{
if (engine_repp->is_prepared() == true) engine_repp->stop_operation();
exit_request_rep = true;
/* no need to block here as the next
* function will be exec()
*/
}
/**********************************************************************
* Engine implementation - Public functions
**********************************************************************/
/**
* Context help:
* J = originates from driver callback
* E = ----- " ------- engine thread (exec())
* C = ----- " ------- control thread (external)
*
* X-level-Y -> Y = steps from originating functions
*/
/**
* Class constructor. A pointer to an enabled
* ECA_CHAINSETUP object must be given as argument.
*
* @pre csetup != 0
* @pre csetup->is_enabled() == true
* @post status() == ECA_ENGINE::engine_status_stopped
*/
ECA_ENGINE::ECA_ENGINE(ECA_CHAINSETUP* csetup)
: prepared_rep(false),
running_rep(false),
finished_rep(false),
outputs_finished_rep(0),
driver_errors_rep(0),
csetup_repp(csetup),
mixslot_repp(0)
{
// --
DBC_REQUIRE(csetup != 0);
DBC_REQUIRE(csetup->is_enabled() == true);
// --
ECA_LOG_MSG(ECA_LOGGER::system_objects, "ECA_ENGINE constructor");
csetup_repp->toggle_locked_state(true);
impl_repp = new ECA_ENGINE_impl;
mixslot_repp = new SAMPLE_BUFFER(buffersize(), 0);
init_variables();
init_connection_to_chainsetup();
PROFILE_ENGINE_STATEMENT(init_profiling());
csetup_repp->toggle_locked_state(false);
// --
DBC_ENSURE(status() == ECA_ENGINE::engine_status_stopped);
// --
}
/**
* Class destructor.
*/
ECA_ENGINE::~ECA_ENGINE(void)
{
ECA_LOG_MSG(ECA_LOGGER::system_objects, "ECA_ENGINE destructor");
if (csetup_repp != 0) {
command(ECA_ENGINE::ep_exit, 0.0f);
wait_for_exit(5);
if (csetup_repp != 0) {
cleanup();
}
}
PROFILE_ENGINE_STATEMENT(dump_profile_info());
if (driver_local == true) {
delete driver_repp;
driver_repp = 0;
}
for(size_t n = 0; n < cslots_rep.size(); n++) {
delete cslots_rep[n];
}
delete mixslot_repp;
delete impl_repp;
ECA_LOG_MSG(ECA_LOGGER::subsystems, "Engine exiting");
}
/**
* Launches the engine. This function will block
* until processing is finished.
*
* Note that a exec() is a one-shot function.
* It's not possible to call it multiple times.
*
* @param batch_mode if true, once engine is started
* it will continue processing until
* 'status() == engine_status_finished'
* condition is reached and then exit;
* if false, engine will react to
* commands until explicit 'exit' is
* given
*
* @see command()
* @see status()
*
* @pre is_valid() == true
* @post status() == ECA_ENGINE::engine_status_notready
* @post is_valid() != true
*/
int ECA_ENGINE::exec(bool batch_mode)
{
// --
DBC_REQUIRE(csetup_repp != 0);
DBC_REQUIRE(csetup_repp->is_enabled() == true);
// --
int result = 0;
csetup_repp->toggle_locked_state(true);
batchmode_enabled_rep = batch_mode;
ECA_LOG_MSG(ECA_LOGGER::subsystems, "Engine - Driver start");
int res = driver_repp->exec(this, csetup_repp);
if (res < 0) {
++driver_errors_rep;
ECA_LOG_MSG(ECA_LOGGER::info,
"WARNING: Engine has raised an error! "
"Possible causes: connection lost to system services, unable to adapt "
"to changes in operating environment, etc.");
}
csetup_repp->toggle_locked_state(false);
signal_exit();
if (outputs_finished_rep > 0) {
ECA_LOG_MSG(ECA_LOGGER::info, "WARNING: An output object has raised an error! "
"Possible causes: Out of disk space, permission denied, unable to launch external "
"applications needed in processing, etc.");
}
DBC_CHECK(status() == ECA_ENGINE::engine_status_stopped ||
status() == ECA_ENGINE::engine_status_finished ||
status() == ECA_ENGINE::engine_status_error);
if (status() == ECA_ENGINE::engine_status_error) {
result = -1;
}
cleanup();
ECA_LOG_MSG(ECA_LOGGER::user_objects,
"Engine state when finishing: " +
kvu_numtostr(static_cast<int>(status())));
// --
DBC_ENSURE(status() == ECA_ENGINE::engine_status_notready);
// --
return result;
}
/**
* Sends 'cmd' to engines command queue. Commands are
* processed in the server's main loop.
*
* context: C-level-0
* must no be called from exec() context
*/
void ECA_ENGINE::command(Engine_command_t cmd, double arg)
{
ECA_ENGINE::complex_command_t item;
item.type = cmd;
item.m.legacy.value = arg;
impl_repp->command_queue_rep.push_back(item);
}
/**
* Sends 'ccmd' to engines command queue. Commands are
* processed in the server's main loop. Passing a complex
* command allows to address objects regardless of
* the state of ECA_CHAINSETUP iterators (i.e. currently
* selected objects).
*
* context: C-level-0
* must no be called from exec() context
*/
void ECA_ENGINE::command(complex_command_t ccmd)
{
impl_repp->command_queue_rep.push_back(ccmd);
}
/**
* Wait for a stop signal. Functions blocks until
* the signal is received or 'timeout' seconds
* has elapsed.
*
* context: C-level-0
* must not be run from the engine
* driver context
*
* @see signal_stop()
*/
void ECA_ENGINE::wait_for_stop(int timeout)
{
struct timespec timeoutspec;
// FIXME: start to use MONOTONIC, needs pthread_condattr_setclock
int ret = kvu_pthread_cond_timeout(timeout, &timeoutspec, false);
DBC_CHECK(ret == 0);
pthread_mutex_lock(&impl_repp->ecasound_stop_mutex_repp);
while(running_rep == true) {
ret = pthread_cond_timedwait(&impl_repp->ecasound_stop_cond_repp,
&impl_repp->ecasound_stop_mutex_repp,
&timeoutspec);
ECA_LOG_MSG(ECA_LOGGER::system_objects,
kvu_pthread_timed_wait_result(ret, "wait_for_stop"));
}
pthread_mutex_unlock(&impl_repp->ecasound_stop_mutex_repp);
}
/**
* Wait for an exit signal. Function blocks until
* the signal is received or 'timeout' seconds
* has elapsed.
*
* context: C-level-0
*
* @see signal_exit()
*/
void ECA_ENGINE::wait_for_exit(int timeout)
{
int ret = kvu_pthread_timed_wait(&impl_repp->ecasound_exit_mutex_repp,
&impl_repp->ecasound_exit_cond_repp,
timeout);
ECA_LOG_MSG(ECA_LOGGER::info,
kvu_pthread_timed_wait_result(ret, "(eca_main) wait_for_exit"));
}
/**********************************************************************
* Engine implementation - Public functions for observing engine
* status information
**********************************************************************/
/**
* Returns true engine's internal
* state is valid for processing.
*
* context: C-level-0
* no limitations
*/
bool ECA_ENGINE::is_valid(void) const
{
if (csetup_repp == 0 ||
csetup_repp->is_enabled() != true ||
csetup_repp->is_valid() != true ||
chains_repp == 0 ||
chains_repp->size() == 0 ||
inputs_repp == 0 ||
inputs_repp->size() == 0 ||
outputs_repp == 0 ||
outputs_repp->size() == 0) {
return false;
}
return true;
}
/**
* Whether current setup has finite length.
*
* context: C-level-0
*/
bool ECA_ENGINE::is_finite_length(void) const
{
DBC_CHECK(csetup_repp != 0);
if (csetup_repp->max_length_set() == true ||
csetup_repp->number_of_realtime_inputs() == 0) {
return true;
}
return false;
}
/**
* Returns engine's current status.
*
* context: C-level-0
* no limitations
*/
ECA_ENGINE::Engine_status_t ECA_ENGINE::status(void) const
{
if (csetup_repp == 0)
return ECA_ENGINE::engine_status_notready;
/* calculated in update_engine_status() */
if (finished_rep == true)
return ECA_ENGINE::engine_status_finished;
if (outputs_finished_rep > 0 ||
driver_errors_rep > 0)
return ECA_ENGINE::engine_status_error;
if (is_running() == true)
return ECA_ENGINE::engine_status_running;
if (is_prepared() == true)
return ECA_ENGINE::engine_status_stopped;
return ECA_ENGINE::engine_status_stopped;
}
/**********************************************************************
* Engine implementation - API for engine driver objects
**********************************************************************/
/**
* Processes available new commands. If no
* messages are available, function will return
* immediately without blocking.
*
* context: E-level-1
* can be run at the same time as engine_iteration();
* note! this is called with the engine lock held
* so no long operations allowed!
*/
void ECA_ENGINE::check_command_queue(void)
{
while(impl_repp->command_queue_rep.is_empty() != true) {
ECA_ENGINE::complex_command_t item;
int popres = impl_repp->command_queue_rep.pop_front(&item);
if (popres <= 0) {
/* queue is empty or temporarily unavailable, unable to continue
* processing messages without blocking */
break;
}
switch(item.type)
{
// ---
// Basic commands.
// ---
case ep_exit:
{
// FIXME: is clear the right thing or should remaining cmds
// be still processed? OTOH, client app should know...
impl_repp->command_queue_rep.clear();
ECA_LOG_MSG(ECA_LOGGER::system_objects,"ecasound_queue: exit!");
driver_repp->exit();
return;
}
// ---
// Chain operators (stateless addressing)
// ---
case ep_exec_edit:
{
csetup_repp->execute_edit(item.cs);
if (item.cs.need_chain_reinit) {
reinit_chains(true);
}
break;
}
case ep_prepare:
{
if (is_prepared() != true)
prepare_operation();
break;
}
case ep_start:
{
if (status() != engine_status_running)
request_start();
break;
}
case ep_stop:
{
if (status() == engine_status_running ||
status() == engine_status_finished) request_stop(false);
break;
}
case ep_stop_with_drain:
{
if (status() == engine_status_running ||
status() == engine_status_finished)
request_stop(true);
break;
}
// ---
// Global position
// ---
case ep_rewind: { change_position(- item.m.legacy.value); break; }
case ep_forward: { change_position(item.m.legacy.value); break; }
case ep_setpos: { set_position(item.m.legacy.value); break; }
case ep_setpos_samples: { set_position_samples(static_cast<SAMPLE_SPECS::sample_pos_t>(item.m.legacy.value)); break; }
case ep_setpos_live_samples: { set_position_samples_live(static_cast<SAMPLE_SPECS::sample_pos_t>(item.m.legacy.value)); break; }
case ep_debug: break;
} /* switch */
}
}
/**
* Waits for new commands to arrive. Function
* will block until at least one new message
* is available or until a timeout occurs.
*
* context: E-level-1
* can be run at the same time as
* engine_iteration()
*/
void ECA_ENGINE::wait_for_commands(void)
{
impl_repp->command_queue_rep.poll(5, 0);
}
/**
* Intializes internal state variables.
*
* context: E-level-1
* must not be run at the same
* time as engine_iteration()
*
* @see update_engine_state()
*/
void ECA_ENGINE::init_engine_state(void)
{
finished_rep = false;
inputs_not_finished_rep = 1; // for the 1st iteration
outputs_finished_rep = 0;
mixslot_repp->event_tag_set(SAMPLE_BUFFER::tag_end_of_stream, false);
for(size_t n = 0; n < cslots_rep.size(); n++) {
cslots_rep[n]->event_tag_set(SAMPLE_BUFFER::tag_end_of_stream, false);
}
}
/**
* Updates engine state to match current
* situation.
*
* context: J-level-0
* must not be run at the same
* time as engine_iteration()
*
* @see init_engine_state()
*/
void ECA_ENGINE::update_engine_state(void)
{
// --
// Check whether all inputs have finished
// (note: as update_engine_state() is
// guaranteed to not to be called at the same
// time as engine_iteration(), this is the
// only safe place to calculate finished state
if (inputs_not_finished_rep == 0 &&
outputs_finished_rep == 0 &&
finished_rep != true) {
if (is_running() == true) {
ECA_LOG_MSG(ECA_LOGGER::system_objects,"all inputs finished - stop");
// FIXME: this is still wrong, command() is not fully rt-safe
// we are not allowed to call request_stop here
command(ECA_ENGINE::ep_stop_with_drain, 0.0f);
}
state_change_to_finished();
}
// --
// Check whether some output has raised an error
if (status() == ECA_ENGINE::engine_status_error) {
if (is_running() == true) {
ECA_LOG_MSG(ECA_LOGGER::system_objects,"output error - stop");
// FIXME: this is still wrong, command() is not fully rt-safe
// we are not allowed to call request_stop here
command(ECA_ENGINE::ep_stop, 0.0f);
}
}
}
/**
* Executes one engine loop iteration. It is critical
* that this function is only called when engine is
* running.
*
* @pre is_running() == true
*
* context: J-level-0
*/
void ECA_ENGINE::engine_iteration(void)
{
DBC_CHECK(is_running() == true);
PROFILE_ENGINE_STATEMENT(impl_repp->looptimer_rep.start(); impl_repp->looptimer_range_rep.start());
inputs_not_finished_rep = 0;
prehandle_control_position();
inputs_to_chains();
process_chains();
// FIXME: add support for sub-buffersize offsets
if (preroll_samples_rep >= recording_offset_rep) {
/* record material to non-real-time outputs */
mix_to_outputs(false);
}
else {
/* skip slave targets */
mix_to_outputs(true);
preroll_samples_rep += buffersize();
}
posthandle_control_position();
PROFILE_ENGINE_STATEMENT(impl_repp->looptimer_rep.stop(); impl_repp->looptimer_range_rep.stop());
}
/**
* Reinitialize chains
*
* Flush any existing state from the chain operators (e.g.
* old audio data in buffers, changes in configuration, and
* so forth).
*
* @arg force force reinitialization even if chain itself
* reports it is properly initialized
*/
void ECA_ENGINE::reinit_chains(bool force)
{
for(size_t i = 0; i != chains_repp->size(); i++) {
if (force == true ||
(*chains_repp)[i]->is_initialized() != true) {
(*chains_repp)[i]->init(0, 0, 0);
}
}
}
/**
* Prepares engine for operation. Prepares all
* realtime devices and starts servers.
*
* This function should be called by the
* driver before it starts iterating the
* engine's main loop.
*
* context: E-level-1/3
* must not be run at the same time
* as engine_iteration()
*
* @pre is_running() != true
* @pre is_prepared() != true
* @post is_prepared() == true
* @post status() == ECA_ENGINE::engine_status_running
*/
void ECA_ENGINE::prepare_operation(void)
{
// ---
DBC_REQUIRE(is_running() != true);
DBC_REQUIRE(is_prepared() != true);
// ---
/* 1. acquire rt-lock for chainsetup and samplebuffers */
csetup_repp->toggle_locked_state(true);
for(size_t n = 0; n < cslots_rep.size(); n++) {
cslots_rep[n]->set_rt_lock(true);
}
mixslot_repp->set_rt_lock(true);
/* 2. reinitialize chains if necessary */
reinit_chains(true);
/* 3. start subsystem servers and forked audio objects */
start_forked_objects();
start_servers();
/* 4. prepare rt objects */
prepare_realtime_objects();
/* ... initial offset is needed because preroll is
* incremented only after checking whether we are
* still in preroll mode */
preroll_samples_rep = buffersize();
/* 6. enable rt-scheduling */
if (csetup_repp->raised_priority() == true) {
if (kvu_set_thread_scheduling(SCHED_FIFO, csetup_repp->get_sched_priority()) != 0)
ECA_LOG_MSG(ECA_LOGGER::system_objects, "Unable to change scheduling policy!");
else
ECA_LOG_MSG(ECA_LOGGER::user_objects,
std::string("Using realtime-scheduling (SCHED_FIFO:")
+ kvu_numtostr(csetup_repp->get_sched_priority()) + ").");
}
/* 7. change engine to active and running */
prepared_rep = true;
init_engine_state();
ECA_LOG_MSG(ECA_LOGGER::system_objects, "engine prepared");
// ---
DBC_ENSURE(is_prepared() == true);
DBC_ENSURE(status() == ECA_ENGINE::engine_status_stopped);
// ---
}
/**
* Starts engine operation.
*
* This function should be called by the
* driver just before it starts iterating the
* engine's main loop.
*
* context: E-level-1/3
* must not be run at the same time
* as engine_iteration()
*
* @pre is_prepared() == true
* @pre is_running() != true
* @post is_running() == true
* @post status() == ECA_ENGINE::engine_status_running
*/
void ECA_ENGINE::start_operation(void)
{
// ---
DBC_REQUIRE(is_prepared() == true);
DBC_REQUIRE(is_running() != true);
// ---
ECA_LOG_MSG(ECA_LOGGER::system_objects, "starting engine operation!");
start_realtime_objects();
running_rep = true;
// ---
DBC_ENSURE(is_running() == true);
DBC_ENSURE(status() == ECA_ENGINE::engine_status_running);
// ---
}
/**
* Stops all realtime devices and servers.
*
* This function should be called by the
* driver when it stops iterating the
* engine's main loop.
*
* context: E-level-1/3
* must not be run at the same time
* as engine_iteration()
*
* @param drain whether to block until all queued data is processed
* by realtime devices before stopping
*
* @pre is_running() == true
* @post is_running() != true
* @post is_prepared() != true
*/
void ECA_ENGINE::stop_operation(bool drain)
{
// ---
DBC_REQUIRE(is_prepared() == true);
// ---
ECA_LOG_MSG(ECA_LOGGER::system_objects, "stopping engine operation!");
/* stop realtime devices */
for (unsigned int adev_sizet = 0; adev_sizet != realtime_objects_rep.size(); adev_sizet++) {
if (realtime_objects_rep[adev_sizet]->is_running() == true)
realtime_objects_rep[adev_sizet]->stop(drain);
}
prepared_rep = false;
/* release samplebuffer rt-locks */
for(size_t n = 0; n < cslots_rep.size(); n++) {
cslots_rep[n]->set_rt_lock(false);
}
mixslot_repp->set_rt_lock(false);
stop_servers();
stop_forked_objects();
/* lower priority back to normal */
if (csetup_repp->raised_priority() == true) {
if (kvu_set_thread_scheduling(SCHED_OTHER, 0) != 0)
ECA_LOG_MSG(ECA_LOGGER::info, "Unable to change scheduling back to SCHED_OTHER!");
else
ECA_LOG_MSG(ECA_LOGGER::system_objects, "Changed back to non-realtime scheduling SCHED_OTHER.");
}
/* release chainsetup lock */
csetup_repp->toggle_locked_state(false);
/* signals wait_for_stop() that engine operation has stopped */
signal_stop();
// ---
DBC_ENSURE(is_running() != true);
DBC_ENSURE(is_prepared() != true);
// ---
}
/**
* Whether engine has been actived
* with prepare_operation().
*
* context: no limitations
*/
bool ECA_ENGINE::is_prepared(void) const
{
return prepared_rep;
}
/**
* Whether engine has been started
* with start_operation().
*
* context: no limitations
*/
bool ECA_ENGINE::is_running(void) const
{
return running_rep;
}
/**********************************************************************
* Engine implementation - Attribute functions
**********************************************************************/
long int ECA_ENGINE::buffersize(void) const
{
DBC_CHECK(csetup_repp != 0);
return csetup_repp->buffersize();
}
int ECA_ENGINE::max_channels(void) const
{
int result = 0;
for(unsigned int n = 0; n < csetup_repp->inputs.size(); n++) {
if (csetup_repp->inputs[n]->channels() > result)
result = csetup_repp->inputs[n]->channels();
}
for(unsigned int n = 0; n < csetup_repp->outputs.size(); n++) {
if (csetup_repp->outputs[n]->channels() > result)
result = csetup_repp->outputs[n]->channels();
}
return result;
}
/**********************************************************************
* Engine implementation - Private functions for transport control
**********************************************************************/
/**
* Requests the engine driver to start operation.
*
* This function should only be called from
* check_command_queue().
*
* @pre status() != engine_status_running
*
* context: E-level-2
*/
void ECA_ENGINE::request_start(void)
{
// ---
DBC_REQUIRE(status() != engine_status_running);
// ---
ECA_LOG_MSG(ECA_LOGGER::user_objects, "Request start");
// --
// start the driver
driver_repp->start();
}
/**
* Requests the engine driver to stop operation.
*
* This function should only be called from
* check_command_queue().
*
* @pre status() == ECA_ENGINE::engine_status_running ||
* status() == ECA_ENGINE::engine_status_finished
*
* context: E-level-2
*/
void ECA_ENGINE::request_stop(bool drain)
{
// ---
DBC_REQUIRE(status() == engine_status_running ||
status() == engine_status_finished);
// ---
ECA_LOG_MSG(ECA_LOGGER::user_objects,
std::string("Request stop (") +
(drain ? std::string("drain") : std::string("no-drain")) + ")");
driver_repp->stop(drain);
}
/**
* Sends a stop signal indicating that engine
* state has changed to stopped.
*
* context: E-level-1/4
*
* @see wait_for_stop()
*/
void ECA_ENGINE::signal_stop(void)
{
pthread_mutex_lock(&impl_repp->ecasound_stop_mutex_repp);
ECA_LOG_MSG(ECA_LOGGER::system_objects, "Signaling stop");
running_rep = false;
pthread_cond_broadcast(&impl_repp->ecasound_stop_cond_repp);
pthread_mutex_unlock(&impl_repp->ecasound_stop_mutex_repp);
}
/**
* Sends an exit signal indicating that engine
* driver has exited.
*
* context: E-level-1/4
*
* @see wait_for_exit()
*/
void ECA_ENGINE::signal_exit(void)
{
pthread_mutex_lock(&impl_repp->ecasound_exit_mutex_repp);
ECA_LOG_MSG(ECA_LOGGER::system_objects, "Signaling exit");
pthread_cond_broadcast(&impl_repp->ecasound_exit_cond_repp);
pthread_mutex_unlock(&impl_repp->ecasound_exit_mutex_repp);
}
/**
* Processing is start If and only if processing
* previously stopped with conditional_stop().
*
* context: E-level-3
*/
void ECA_ENGINE::conditional_start(void)
{
if (was_running_rep == true) {
// don't call request_start(), as it would signal that we are
// starting from completely halted state
if (is_prepared() != true) prepare_operation();
start_operation();
}
}
/**
* Processing is stopped.
*
* context: E-level-3
*
* @see conditional_stop()
* @see request_stop()
*/
void ECA_ENGINE::conditional_stop(void)
{
if (status() == ECA_ENGINE::engine_status_running) {
ECA_LOG_MSG(ECA_LOGGER::system_objects,"conditional stop");
was_running_rep = true;
// don't call request_stop(), as it would signal that we are
// stopping completely (JACK transport stop will be sent to all)
if (is_prepared() == true) stop_operation(false);
}
else was_running_rep = false;
}
void ECA_ENGINE::start_servers(void)
{
if (csetup_repp->double_buffering() == true) {
csetup_repp->pserver_repp->start();
ECA_LOG_MSG(ECA_LOGGER::user_objects, "prefilling i/o buffers.");
csetup_repp->pserver_repp->wait_for_full();
ECA_LOG_MSG(ECA_LOGGER::user_objects, "i/o buffers prefilled.");
}
if (use_midi_rep == true) {
csetup_repp->midi_server_repp->start();
}
}
void ECA_ENGINE::stop_servers(void)
{
if (csetup_repp->double_buffering() == true) {
csetup_repp->pserver_repp->stop();
csetup_repp->pserver_repp->wait_for_stop();
}
if (use_midi_rep == true) {
csetup_repp->midi_server_repp->stop();
}
}
/**
* Goes through all input/outputs in 'vec', checks whether they
* implement the AUDIO_IO_BARRIER interface, and if yes,
* issues either 'start_io()' or 'stop_io()' on them.
*/
static void priv_toggle_forked_objects(bool start, std::vector<AUDIO_IO*>* vec)
{
unsigned int n;
for(n = 0; n < vec->size(); n++) {
AUDIO_IO_BARRIER *barrier
= dynamic_cast<AUDIO_IO_BARRIER*>((*vec)[n]);
if (barrier) {
if (start)
barrier->start_io();
else
barrier->stop_io();
}
}
}
void ECA_ENGINE::start_forked_objects(void)
{
priv_toggle_forked_objects(true, inputs_repp);
priv_toggle_forked_objects(true, outputs_repp);
}
void ECA_ENGINE::stop_forked_objects(void)
{
priv_toggle_forked_objects(false, inputs_repp);
priv_toggle_forked_objects(false, outputs_repp);
}
void ECA_ENGINE::state_change_to_finished(void)
{
if (finished_rep != true) {
ECA_LOG_MSG_NOPREFIX(ECA_LOGGER::info, "");
ECA_LOG_MSG(ECA_LOGGER::subsystems, "Engine - Processing finished");
}
finished_rep = true;
}
void ECA_ENGINE::prepare_realtime_objects(void)
{
/* 1. prepare objects */
for (unsigned int n = 0; n < realtime_objects_rep.size(); n++) {
realtime_objects_rep[n]->prepare();
}
/* 2. prefill rt output objects with silence */
mixslot_repp->make_silent();
for (unsigned int n = 0; n < realtime_outputs_rep.size(); n++) {
if (realtime_outputs_rep[n]->prefill_space() > 0) {
if (realtime_outputs_rep[n]->prefill_space() <
prefill_threshold_rep * buffersize()) {
ECA_LOG_MSG(ECA_LOGGER::user_objects,
"audio output '" +
realtime_outputs_rep[n]->name() +
"' only offers " +
kvu_numtostr(realtime_outputs_rep[n]->prefill_space()) +
" frames of prefill space. Decreasing amount of prefill.");
prefill_threshold_rep = realtime_outputs_rep[n]->prefill_space() / buffersize();
}
ECA_LOG_MSG(ECA_LOGGER::user_objects,
"prefilling rt-outputs with " +
kvu_numtostr(prefill_threshold_rep) +
" blocks.");
for (int m = 0; m < prefill_threshold_rep; m++) {
realtime_outputs_rep[n]->write_buffer(mixslot_repp);
}
}
}
}
void ECA_ENGINE::start_realtime_objects(void)
{
/* 1. start all realtime devices */
for (unsigned int n = 0; n < realtime_objects_rep.size(); n++)
realtime_objects_rep[n]->start();
}
/**
* Performs a close-open cycle for all realtime
* devices.
*/
void ECA_ENGINE::reset_realtime_devices(void)
{
for (size_t n = 0; n < realtime_objects_rep.size(); n++) {
if (realtime_objects_rep[n]->is_open() == true) {
ECA_LOG_MSG(ECA_LOGGER::user_objects,
"Resetting rt-object " +
realtime_objects_rep[n]->label());
realtime_objects_rep[n]->close();
}
}
for (size_t n = 0; n < realtime_objects_rep.size(); n++) {
realtime_objects_rep[n]->open();
}
}
/**********************************************************************
* Engine implementation - Private functions for observing and
* modifying position
**********************************************************************/
SAMPLE_SPECS::sample_pos_t ECA_ENGINE::current_position_in_samples(void) const
{
return csetup_repp->position_in_samples();
}
double ECA_ENGINE::current_position_in_seconds_exact(void) const
{
return csetup_repp->position_in_seconds_exact();
}
// FIXME: remove
#if 0
double ECA_ENGINE::current_position_chain(void) const
{
AUDIO_IO* ptr = (*inputs_repp)[(*chains_repp)[csetup_repp->active_chain_index_rep]->connected_input()];
return ptr->position_in_seconds_exact();
return 0.0f;
}
#endif
/**
* Seeks to position 'seconds'. Affects all input and
* outputs objects, and the chainsetup object position.
*
* context: E-level-2
*/
void ECA_ENGINE::set_position(double seconds)
{
conditional_stop();
csetup_repp->seek_position_in_seconds(seconds);
// reinit chains to flush out any stale audio data related
// to the old position
reinit_chains(true);
// FIXME: calling init_engine_state() may lead to races
init_engine_state();
conditional_start();
}
/**
* Seeks to position 'samples'. Affects all input and
* outputs objects, and the chainsetup object position.
*
* context: E-level-2
*/
void ECA_ENGINE::set_position_samples(SAMPLE_SPECS::sample_pos_t samples)
{
conditional_stop();
csetup_repp->seek_position_in_samples(samples);
// reinit chains to flush out any stale audio data related
// to the old position
reinit_chains(true);
// FIXME: calling init_engine_state() may lead to races
init_engine_state();
conditional_start();
}
/**
* Seeks to position 'samples' without stopping the engine.
* Affects all input and outputs objects, and the chainsetup
* object position.
*
* context: E-level-2
*/
void ECA_ENGINE::set_position_samples_live(SAMPLE_SPECS::sample_pos_t samples)
{
csetup_repp->seek_position_in_samples(samples);
// FIXME: calling init_engine_state() may lead to races
init_engine_state();
}
/**
* Seeks to position 'current+seconds'. Affects all input and
* outputs objects, and the chainsetup object position.
*
* context: E-level-2
*/
void ECA_ENGINE::change_position(double seconds)
{
double curpos = csetup_repp->position_in_seconds_exact();
conditional_stop();
csetup_repp->seek_position_in_seconds(curpos + seconds);
conditional_start();
}
/**
* Calculates how much data we need to process and sets the
* buffersize accordingly for all non-real-time inputs.
*
* context: J-level-1
*/
void ECA_ENGINE::prehandle_control_position(void)
{
csetup_repp->change_position_in_samples(buffersize());
if (csetup_repp->max_length_set() == true &&
csetup_repp->is_over_max_length() == true) {
int extra_tail = csetup_repp->position_in_samples() -
csetup_repp->max_length_in_samples();
int buffer_remain = buffersize() - extra_tail;
if (buffer_remain < 0)
buffer_remain = 0;
else if (buffer_remain > buffersize())
buffer_remain = buffersize();
for(unsigned int adev_sizet = 0; adev_sizet < non_realtime_inputs_rep.size(); adev_sizet++) {
non_realtime_inputs_rep[adev_sizet]->set_buffersize(buffer_remain);
}
}
}
/**
* If we've processed all the data that was requested, stop or rewind.
* Also resets buffersize to its default value. If finished
* state is reached, engine status is set to
* 'ECA_ENGINE::engine_status_finished'.
*/
void ECA_ENGINE::posthandle_control_position(void)
{
if (csetup_repp->max_length_set() == true &&
csetup_repp->is_over_max_length() == true) {
if (csetup_repp->looping_enabled() == true) {
ECA_LOG_MSG(ECA_LOGGER::system_objects,"loop point reached");
inputs_not_finished_rep = 1;
csetup_repp->seek_position_in_samples(0);
for(unsigned int adev_sizet = 0; adev_sizet < non_realtime_inputs_rep.size(); adev_sizet++) {
non_realtime_inputs_rep[adev_sizet]->set_buffersize(buffersize());
}
}
else {
ECA_LOG_MSG(ECA_LOGGER::system_objects,"posthandle_c_p over_max - stop");
if (status() == ECA_ENGINE::engine_status_running ||
status() == ECA_ENGINE::engine_status_finished) {
command(ECA_ENGINE::ep_stop_with_drain, 0.0f);
}
state_change_to_finished();
}
}
}
/**********************************************************************
* Engine implementation - Private functions for setup and cleanup
**********************************************************************/
/**
* Called only from class constructor.
*/
void ECA_ENGINE::init_variables(void)
{
use_midi_rep = false;
batchmode_enabled_rep = false;
driver_local = false;
pthread_cond_init(&impl_repp->ecasound_stop_cond_repp, NULL);
pthread_mutex_init(&impl_repp->ecasound_stop_mutex_repp, NULL);
pthread_cond_init(&impl_repp->ecasound_exit_cond_repp, NULL);
pthread_mutex_init(&impl_repp->ecasound_exit_mutex_repp, NULL);
}
/**
* Called only from class constructor.
*/
void ECA_ENGINE::init_connection_to_chainsetup(void)
{
inputs_repp = &(csetup_repp->inputs);
outputs_repp = &(csetup_repp->outputs);
chains_repp = &(csetup_repp->chains);
init_engine_state();
init_driver();
init_prefill();
init_servers();
init_chains();
create_cache_object_lists();
update_cache_chain_connections();
update_cache_latency_values();
}
/**
* Initializes the engine driver object.
*/
void ECA_ENGINE::init_driver(void)
{
if (csetup_repp->engine_driver_repp != 0) {
driver_repp = csetup_repp->engine_driver_repp;
driver_local = false;
}
else {
driver_repp = new ECA_ENGINE_DEFAULT_DRIVER();
driver_local = true;
}
}
/**
* Initializes prefill variables.
*/
void ECA_ENGINE::init_prefill(void)
{
int channels = (max_channels() > 0 ? max_channels() : 1);
prefill_threshold_rep = 0;
if (csetup_repp->max_buffers() == true)
prefill_threshold_rep = ECA_ENGINE::prefill_threshold_constant / buffersize() / channels;
if (prefill_threshold_rep < ECA_ENGINE::prefill_blocks_constant)
prefill_threshold_rep = ECA_ENGINE::prefill_blocks_constant;
ECA_LOG_MSG(ECA_LOGGER::system_objects,
"Prefill loops: " +
kvu_numtostr(prefill_threshold_rep) +
" (blocksize " +
kvu_numtostr(buffersize()) + ").");
}
/**
*
* Called only from init_connection_to_chainsetup().
*/
void ECA_ENGINE::init_servers(void)
{
if (csetup_repp->midi_devices.size() > 0) {
use_midi_rep = true;
ECA_LOG_MSG(ECA_LOGGER::info, "Initializing MIDI-server.");
csetup_repp->midi_server_repp->init();
}
}
/**
* Called only from init_connection_to_chainsetup().
*/
void ECA_ENGINE::init_chains(void)
{
mixslot_repp->number_of_channels(max_channels());
mixslot_repp->event_tag_set(SAMPLE_BUFFER::tag_mixed_content);
mixslot_repp->event_tag_set(SAMPLE_BUFFER::tag_var_length, false);
cslots_rep.resize(chains_repp->size());
for(size_t n = 0; n < cslots_rep.size(); n++) {
cslots_rep[n] = new SAMPLE_BUFFER(buffersize(), max_channels());
cslots_rep[n]->event_tag_set(SAMPLE_BUFFER::tag_var_length, false);
}
for (unsigned int c = 0; c != chains_repp->size(); c++) {
int inch = (*inputs_repp)[(*chains_repp)[c]->connected_input()]->channels();
int outch = (*outputs_repp)[(*chains_repp)[c]->connected_output()]->channels();
(*chains_repp)[c]->init(cslots_rep[c], inch, outch);
}
}
/**
* Frees all reserved resources.
*
* @post status() == ECA_ENGINE::engine_status_notready
* @post is_valid() != true
*/
void ECA_ENGINE::cleanup(void)
{
if (csetup_repp != 0) {
csetup_repp->toggle_locked_state(true);
vector<CHAIN*>::iterator q = csetup_repp->chains.begin();
while(q != csetup_repp->chains.end()) {
if (*q != 0) {
(*q)->disconnect_buffer();
}
++q;
}
csetup_repp->toggle_locked_state(false);
}
csetup_repp = 0;
// --
DBC_ENSURE(status() == ECA_ENGINE::engine_status_notready);
DBC_ENSURE(is_valid() != true);
// --
}
/**
* Updates 'input_chain_count_rep' and
* 'output_chain_count_rep'.
*/
void ECA_ENGINE::update_cache_chain_connections(void)
{
input_chain_count_rep.resize(inputs_repp->size());
for(unsigned int n = 0; n < inputs_repp->size(); n++) {
input_chain_count_rep[n] =
csetup_repp->number_of_attached_chains_to_input(csetup_repp->inputs[n]);
}
output_chain_count_rep.resize(outputs_repp->size());
for(unsigned int n = 0; n < outputs_repp->size(); n++) {
output_chain_count_rep[n] =
csetup_repp->number_of_attached_chains_to_output(csetup_repp->outputs[n]);
}
}
/**
* Update system latency values for multitrack
* recording.
*/
void ECA_ENGINE::update_cache_latency_values(void)
{
if (csetup_repp->multitrack_mode() == true &&
csetup_repp->multitrack_mode_offset() == -1) {
long int in_latency = -1;
for(unsigned int n = 0; n < realtime_inputs_rep.size(); n++) {
if (in_latency == -1) {
in_latency = realtime_inputs_rep[n]->latency();
}
else {
if (in_latency != realtime_inputs_rep[n]->latency()) {
ECA_LOG_MSG(ECA_LOGGER::info,
"WARNING: Latency mismatch between input objects!");
}
}
ECA_LOG_MSG(ECA_LOGGER::user_objects,
"Input latency for '" +
realtime_inputs_rep[n]->name() +
"' is " + kvu_numtostr(in_latency) + ".");
}
long int out_latency = -1;
for(unsigned int n = 0; n < realtime_outputs_rep.size(); n++) {
if (out_latency == -1) {
if (realtime_outputs_rep[n]->prefill_space() > 0) {
long int max_prefill = prefill_threshold_rep * buffersize();
if (max_prefill > realtime_outputs_rep[n]->prefill_space()) {
max_prefill = realtime_outputs_rep[n]->prefill_space();
}
out_latency = max_prefill + realtime_outputs_rep[n]->latency();
}
else
out_latency = realtime_outputs_rep[n]->latency();
}
else {
if ((realtime_outputs_rep[n]->prefill_space() > 0 &&
out_latency != (prefill_threshold_rep * buffersize()) + realtime_outputs_rep[n]->latency()) ||
(realtime_outputs_rep[n]->prefill_space() == 0 &&
out_latency != realtime_outputs_rep[n]->latency())) {
ECA_LOG_MSG(ECA_LOGGER::info,
"WARNING: Latency mismatch between output objects!");
}
}
ECA_LOG_MSG(ECA_LOGGER::user_objects,
"Output latency for '" +
realtime_outputs_rep[n]->name() +
"' is " + kvu_numtostr(out_latency) + ".");
}
recording_offset_rep = (out_latency > in_latency ?
out_latency : in_latency);
if (recording_offset_rep % buffersize()) {
ECA_LOG_MSG(ECA_LOGGER::info,
"WARNING: Recording offset not divisible with chainsetup buffersize.");
}
ECA_LOG_MSG(ECA_LOGGER::user_objects,
"recording offset is " +
kvu_numtostr(recording_offset_rep) +
" samples.");
}
else if (csetup_repp->multitrack_mode() == true) {
/* multitrack_mode_offset() explicitly given (not -1) */
recording_offset_rep = csetup_repp->multitrack_mode_offset();
}
else {
recording_offset_rep = 0;
}
}
/**
* Assigns input and output objects in lists of realtime
* and nonrealtime objects.
*/
void ECA_ENGINE::create_cache_object_lists(void)
{
for(unsigned int n = 0; n < inputs_repp->size(); n++) {
if (AUDIO_IO_DEVICE::is_realtime_object((*inputs_repp)[n]) == true) {
realtime_inputs_rep.push_back(static_cast<AUDIO_IO_DEVICE*>((*inputs_repp)[n]));
realtime_objects_rep.push_back(static_cast<AUDIO_IO_DEVICE*>((*inputs_repp)[n]));
}
else {
non_realtime_inputs_rep.push_back((*inputs_repp)[n]);
non_realtime_objects_rep.push_back((*inputs_repp)[n]);
}
}
DBC_CHECK(static_cast<int>(realtime_inputs_rep.size()) == csetup_repp->number_of_realtime_inputs());
for(unsigned int n = 0; n < outputs_repp->size(); n++) {
if (AUDIO_IO_DEVICE::is_realtime_object((*outputs_repp)[n]) == true) {
realtime_outputs_rep.push_back(static_cast<AUDIO_IO_DEVICE*>((*outputs_repp)[n]));
realtime_objects_rep.push_back(static_cast<AUDIO_IO_DEVICE*>((*outputs_repp)[n]));
}
else {
non_realtime_outputs_rep.push_back((*outputs_repp)[n]);
non_realtime_objects_rep.push_back((*outputs_repp)[n]);
}
}
DBC_CHECK(static_cast<int>(realtime_outputs_rep.size()) == csetup_repp->number_of_realtime_outputs());
}
/**
* Called only from class constructor.
*/
void ECA_ENGINE::init_profiling(void)
{
impl_repp->looptimer_low_rep = static_cast<double>(buffersize()) / csetup_repp->samples_per_second();
impl_repp->looptimer_mid_rep = static_cast<double>(buffersize() * 2) / csetup_repp->samples_per_second();
impl_repp->looptimer_high_rep = static_cast<double>(buffersize()) * prefill_threshold_rep / csetup_repp->samples_per_second();
impl_repp->looptimer_rep.set_lower_bound_seconds(impl_repp->looptimer_low_rep);
impl_repp->looptimer_rep.set_upper_bound_seconds(impl_repp->looptimer_high_rep);
impl_repp->looptimer_range_rep.set_lower_bound_seconds(impl_repp->looptimer_mid_rep);
impl_repp->looptimer_range_rep.set_upper_bound_seconds(impl_repp->looptimer_mid_rep);
}
/**
* Prints profiling information to stderr.
*/
void ECA_ENGINE::dump_profile_info(void)
{
long int slower_than_rt = impl_repp->looptimer_rep.event_count() -
impl_repp->looptimer_rep.events_under_lower_bound() -
impl_repp->looptimer_rep.events_over_upper_bound();
cerr << "*** profile begin ***" << endl;
cerr << "Loops faster than realtime: " << kvu_numtostr(impl_repp->looptimer_rep.events_under_lower_bound());
cerr << " (<" << kvu_numtostr(impl_repp->looptimer_low_rep * 1000, 1) << " msec)" << endl;
cerr << "Loops slower than realtime: " << kvu_numtostr(slower_than_rt);
cerr << " (>=" << kvu_numtostr(impl_repp->looptimer_low_rep * 1000, 1) << " msec)" << endl;
cerr << "Loops slower than realtime: " << kvu_numtostr(impl_repp->looptimer_range_rep.events_over_upper_bound());
cerr << " (>" << kvu_numtostr(impl_repp->looptimer_mid_rep * 1000, 1) << " msec)" << endl;
cerr << "Loops exceeding all buffering: " << kvu_numtostr(impl_repp->looptimer_rep.events_over_upper_bound());
cerr << " (>" << kvu_numtostr(impl_repp->looptimer_high_rep * 1000, 1) << " msec)" << endl;
cerr << "Total loops: " << kvu_numtostr(impl_repp->looptimer_rep.event_count()) << endl;
cerr << "Fastest/slowest/average loop time: ";
cerr << kvu_numtostr(impl_repp->looptimer_rep.min_duration_seconds() * 1000, 1);
cerr << "/";
cerr << kvu_numtostr(impl_repp->looptimer_rep.max_duration_seconds() * 1000, 1);
cerr << "/";
cerr << kvu_numtostr(impl_repp->looptimer_rep.average_duration_seconds() * 1000, 1);
cerr << " msec." << endl;
cerr << "*** profile end ***" << endl;
}
/**********************************************************************
* Engine implementation - Private functions for signal routing
**********************************************************************/
/**
* Reads audio data from input objects.
*
* context: J-level-1 (see
*/
void ECA_ENGINE::inputs_to_chains(void)
{
/**
* - go through all inputs
* - depending on connectivity, read either to a mixdown slot, or
* directly to a per-chain slot
*/
for(size_t inputnum = 0; inputnum < inputs_repp->size(); inputnum++) {
if (input_chain_count_rep[inputnum] > 1) {
/* case-1a: read buffer from input 'inputnum' to 'mixslot';
* later (1b) the data is copied to each per-chain slow
* to which input is connected to */
mixslot_repp->length_in_samples(buffersize());
if ((*inputs_repp)[inputnum]->finished() != true) {
(*inputs_repp)[inputnum]->read_buffer(mixslot_repp);
if ((*inputs_repp)[inputnum]->finished() != true) {
inputs_not_finished_rep++;
}
}
else {
/* note: no more input data for this change (N:1 input-chain case) */
mixslot_repp->make_empty();
}
}
for (size_t c = 0; c != chains_repp->size(); c++) {
if ((*chains_repp)[c]->connected_input() == static_cast<int>(inputnum)) {
if (input_chain_count_rep[inputnum] == 1) {
/* case-2: read buffer from input 'inputnum' to chain 'c' */
cslots_rep[c]->length_in_samples(buffersize());
if ((*inputs_repp)[inputnum]->finished() != true) {
(*inputs_repp)[inputnum]->read_buffer(cslots_rep[c]);
if ((*inputs_repp)[inputnum]->finished() != true) {
inputs_not_finished_rep++;
}
}
else {
/* note: no more input data for this change (1:1 input-chain case) */
cslots_rep[c]->make_empty();
}
/* note: input connected to only one chain, so no need to
iterate through the other chains */
break;
}
else {
/* case-1b: input connected to chain 'n', copy 'mixslot' to
* the matching per-chain slot */
cslots_rep[c]->copy_all_content(*mixslot_repp);
}
}
}
}
}
/**
* context: J-level-1
*/
void ECA_ENGINE::process_chains(void)
{
vector<CHAIN*>::const_iterator p = chains_repp->begin();
while(p != chains_repp->end()) {
(*p)->process();
++p;
}
}
void mix_to_outputs_divide_helper(const SAMPLE_BUFFER *from, SAMPLE_BUFFER *to, int divide_by, bool first_time)
{
if (first_time == true) {
// this is the first output connected to this chain
if (from->number_of_channels() < to->number_of_channels()) {
to->make_silent();
}
to->copy_matching_channels(*from);
to->divide_by(divide_by);
}
else {
to->add_with_weight(*from, divide_by);
}
}
void mix_to_outputs_sum_helper(const SAMPLE_BUFFER *from, SAMPLE_BUFFER *to, bool first_time)
{
if (first_time == true) {
// this is the first output connected to this chain
if (from->number_of_channels() < to->number_of_channels()) {
to->make_silent();
}
to->copy_matching_channels(*from);
}
else {
to->add_matching_channels(*from);
}
}
/**
* context: J-level-1
*/
void ECA_ENGINE::mix_to_outputs(bool skip_realtime_target_outputs)
{
for(size_t outputnum = 0; outputnum < outputs_repp->size(); outputnum++) {
if (skip_realtime_target_outputs == true) {
if (csetup_repp->is_realtime_target_output(outputnum) == true) {
ECA_LOG_MSG(ECA_LOGGER::system_objects,
"Skipping rt-target output " +
(*outputs_repp)[outputnum]->label() + ".");
continue;
}
}
int count = 0;
/* FIXME: number_of_channels() may end up allocating memory! */
mixslot_repp->number_of_channels((*outputs_repp)[outputnum]->channels());
for(size_t n = 0; n != chains_repp->size(); n++) {
// --
// if chain is already released, skip
// --
if ((*chains_repp)[n]->connected_output() == -1) {
// --
// skip, if chain is not connected
// --
continue;
}
if ((*chains_repp)[n]->connected_output() == static_cast<int>(outputnum)) {
// --
// output is connected to this chain
// --
if (output_chain_count_rep[outputnum] == 1) {
// --
// there's only one output connected to this chain,
// so we don't need to mix anything
// --
(*outputs_repp)[outputnum]->write_buffer(cslots_rep[n]);
if ((*outputs_repp)[outputnum]->finished() == true)
/* note: loop devices always connected both as inputs as
* outputs, so their finished status must not be
* counted as an error (like for other output types) */
if (dynamic_cast<LOOP_DEVICE*>((*outputs_repp)[outputnum]) == 0)
outputs_finished_rep++;
break;
}
else {
++count;
if (csetup_repp->mix_mode() == ECA_CHAINSETUP::cs_mmode_avg)
mix_to_outputs_divide_helper(cslots_rep[n], mixslot_repp, output_chain_count_rep[outputnum], (count == 1));
else
mix_to_outputs_sum_helper(cslots_rep[n], mixslot_repp, (count == 1));
mixslot_repp->event_tags_add(*cslots_rep[n]);
if (count == output_chain_count_rep[outputnum]) {
(*outputs_repp)[outputnum]->write_buffer(mixslot_repp);
if ((*outputs_repp)[outputnum]->finished() == true)
/* note: loop devices always connected both as inputs as
* outputs, so their finished status must not be
* counted as an error (like for other output types) */
if (dynamic_cast<LOOP_DEVICE*>((*outputs_repp)[outputnum]) == 0)
outputs_finished_rep++;
}
}
}
}
}
}
/**********************************************************************
* Engine implementation - Obsolete functions
**********************************************************************/
|