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 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/config-manager.h"
#include "common/file.h"
#include "common/system.h"
#include "common/util.h"
#include "common/rect.h"
#include "audio/mixer.h"
#include "graphics/cursorman.h"
#include "graphics/paletteman.h"
#include "scumm/file.h"
#include "scumm/imuse_digi/dimuse_engine.h"
#include "scumm/scumm.h"
#include "scumm/scumm_v7.h"
#include "scumm/sound.h"
#include "scumm/smush/codec37.h"
#include "scumm/smush/codec47.h"
#include "scumm/smush/smush_font.h"
#include "scumm/smush/smush_player.h"
#include "scumm/insane/insane.h"
#include "audio/audiostream.h"
#include "audio/mixer.h"
#include "audio/decoders/mp3.h"
#include "audio/decoders/raw.h"
#include "audio/decoders/vorbis.h"
#include "common/compression/deflate.h"
namespace Scumm {
static const int MAX_STRINGS = 200;
static const int ETRS_HEADER_LENGTH = 16;
class StringResource {
private:
struct {
int id;
char *string;
} _strings[MAX_STRINGS];
int _nbStrings;
int _lastId;
const char *_lastString;
public:
StringResource() :
_nbStrings(0),
_lastId(-1),
_lastString(nullptr) {
for (int i = 0; i < MAX_STRINGS; i++) {
_strings[i].id = 0;
_strings[i].string = nullptr;
}
}
~StringResource() {
for (int32 i = 0; i < _nbStrings; i++) {
delete[] _strings[i].string;
}
}
bool init(char *buffer, int32 length) {
char *def_start = strchr(buffer, '#');
while (def_start != nullptr) {
char *def_end = strchr(def_start, '\n');
assert(def_end != nullptr);
char *id_end = def_end;
while (id_end >= def_start && !Common::isDigit(*(id_end-1))) {
id_end--;
}
assert(id_end > def_start);
char *id_start = id_end;
while (Common::isDigit(*(id_start - 1))) {
id_start--;
}
char idstring[32];
memcpy(idstring, id_start, id_end - id_start);
idstring[id_end - id_start] = 0;
int32 id = atoi(idstring);
char *data_start = def_end;
while (*data_start == '\n' || *data_start == '\r') {
data_start++;
}
char *data_end = data_start;
while (1) {
if (data_end[-2] == '\r' && data_end[-1] == '\n' && data_end[0] == '\r' && data_end[1] == '\n') {
break;
}
// In the Steam Mac version of The Dig, LF-LF is used
// instead of CR-LF
if (data_end[-2] == '\n' && data_end[-1] == '\n') {
break;
}
// In Russian Full Throttle strings are finished with
// just one pair of CR-LF
if (data_end[-2] == '\r' && data_end[-1] == '\n' && data_end[0] == '#') {
break;
}
data_end++;
if (data_end >= buffer + length) {
data_end = buffer + length;
break;
}
}
data_end -= 2;
assert(data_end > data_start);
char *value = new char[data_end - data_start + 1];
assert(value);
memcpy(value, data_start, data_end - data_start);
value[data_end - data_start] = 0;
char *line_start = value;
char *line_end;
while ((line_end = strchr(line_start, '\n'))) {
line_start = line_end+1;
if (line_start[0] == '/' && line_start[1] == '/') {
line_start += 2;
if (line_end[-1] == '\r')
line_end[-1] = ' ';
else
*line_end++ = ' ';
memmove(line_end, line_start, strlen(line_start)+1);
}
}
_strings[_nbStrings].id = id;
_strings[_nbStrings].string = value;
_nbStrings ++;
def_start = strchr(data_end + 2, '#');
}
return true;
}
const char *get(int id) {
if (id == _lastId) {
return _lastString;
}
debugC(DEBUG_SMUSH, "StringResource::get(%d)", id);
for (int i = 0; i < _nbStrings; i++) {
if (_strings[i].id == id) {
_lastId = id;
_lastString = _strings[i].string;
return _strings[i].string;
}
}
warning("invalid string id : %d", id);
_lastId = -1;
_lastString = "unknown string";
return _lastString;
}
};
static StringResource *getStrings(ScummEngine *vm, const char *file, bool is_encoded) {
debugC(DEBUG_SMUSH, "trying to read text resources from %s", file);
ScummFile theFile(vm);
vm->openFile(theFile, file);
if (!theFile.isOpen()) {
return 0;
}
int32 length = theFile.size();
char *filebuffer = new char [length + 1];
assert(filebuffer);
theFile.read(filebuffer, length);
filebuffer[length] = 0;
if (is_encoded && READ_BE_UINT32(filebuffer) == MKTAG('E','T','R','S')) {
assert(length > ETRS_HEADER_LENGTH);
length -= ETRS_HEADER_LENGTH;
for (int i = 0; i < length; ++i) {
filebuffer[i] = filebuffer[i + ETRS_HEADER_LENGTH] ^ 0xCC;
}
filebuffer[length] = '\0';
}
StringResource *sr = new StringResource;
assert(sr);
sr->init(filebuffer, length);
delete[] filebuffer;
return sr;
}
void SmushPlayer::timerCallback() {
parseNextFrame();
}
SmushPlayer::SmushPlayer(ScummEngine_v7 *scumm, IMuseDigital *imuseDigital, Insane *insane) {
_vm = scumm;
_imuseDigital = imuseDigital;
_insane = insane;
_nbframes = 0;
_deltaBlocksCodec = 0;
_deltaGlyphsCodec = 0;
_strings = nullptr;
_sf[0] = nullptr;
_sf[1] = nullptr;
_sf[2] = nullptr;
_sf[3] = nullptr;
_sf[4] = nullptr;
_base = nullptr;
_frameBuffer = nullptr;
_specialBuffer = nullptr;
_seekPos = -1;
_skipNext = false;
_dst = nullptr;
_storeFrame = false;
_compressedFileMode = false;
_width = 0;
_height = 0;
_IACTpos = 0;
_speed = -1;
_insanity = false;
_middleAudio = false;
_skipPalette = false;
_IACTstream = nullptr;
_paused = false;
_pauseStartTime = 0;
_pauseTime = 0;
memset(_pal, 0, sizeof(_pal));
memset(_deltaPal, 0, sizeof(_deltaPal));
memset(_shiftedDeltaPal, 0, sizeof(_shiftedDeltaPal));
for (int i = 0; i < 4; i++)
_iactTable[i] = 0;
_IACTchannel = new Audio::SoundHandle();
_compressedFileSoundHandle = new Audio::SoundHandle();
_smushNumTracks = 0;
_gainReductionLowerBound = 64;
_gainReductionFactor = 256;
_gainReductionMultiplier = 256;
_smushTracksNeedInit = true;
_smushAudioInitialized = false;
_smushAudioCallbackEnabled = false;
initAudio(_imuseDigital->getSampleRate(), 200000);
}
SmushPlayer::~SmushPlayer() {
delete _IACTchannel;
delete _compressedFileSoundHandle;
terminateAudio();
}
void SmushPlayer::init(int32 speed) {
VirtScreen *vs = &_vm->_virtscr[kMainVirtScreen];
_frame = 0;
_speed = speed;
_endOfFile = false;
_vm->_smushVideoShouldFinish = false;
_vm->_smushActive = true;
_vm->setDirtyColors(0, 255);
_dst = vs->getPixels(0, 0);
// HACK HACK HACK: This is an *evil* trick, beware!
// We do this to fix bug #1792. A proper solution would change all the
// drawing code to use the pitch value specified by the virtual screen.
// However, since a lot of the SMUSH code currently assumes the screen
// width and pitch to be equal, this will require lots of changes. So
// we resort to this hackish solution for now.
_origPitch = vs->pitch;
_origNumStrips = _vm->_gdi->_numStrips;
vs->pitch = vs->w;
_vm->_gdi->_numStrips = vs->w / 8;
_vm->_mixer->stopHandle(*_compressedFileSoundHandle);
_vm->_mixer->stopHandle(*_IACTchannel);
_IACTpos = 0;
}
void SmushPlayer::release() {
_vm->_smushVideoShouldFinish = true;
for (int i = 0; i < 5; i++) {
delete _sf[i];
_sf[i] = nullptr;
}
delete _strings;
_strings = nullptr;
delete _base;
_base = nullptr;
free(_specialBuffer);
_specialBuffer = nullptr;
free(_frameBuffer);
_frameBuffer = nullptr;
_IACTstream = nullptr;
_vm->_smushActive = false;
_vm->_fullRedraw = true;
// HACK HACK HACK: This is an *evil* trick, beware! See above for
// some explanation.
_vm->_virtscr[kMainVirtScreen].pitch = _origPitch;
_vm->_gdi->_numStrips = _origNumStrips;
delete _deltaBlocksCodec;
_deltaBlocksCodec = 0;
delete _deltaGlyphsCodec;
_deltaGlyphsCodec = 0;
}
void SmushPlayer::handleStore(int32 subSize, Common::SeekableReadStream &b) {
debugC(DEBUG_SMUSH, "SmushPlayer::handleStore()");
assert(subSize >= 4);
_storeFrame = true;
}
void SmushPlayer::handleFetch(int32 subSize, Common::SeekableReadStream &b) {
debugC(DEBUG_SMUSH, "SmushPlayer::handleFetch()");
assert(subSize >= 6);
if (_frameBuffer != nullptr) {
memcpy(_dst, _frameBuffer, _width * _height);
}
}
void SmushPlayer::handleIACT(int32 subSize, Common::SeekableReadStream &b) {
debugC(DEBUG_SMUSH, "SmushPlayer::IACT()");
assert(subSize >= 8);
int code = b.readUint16LE();
int flags = b.readUint16LE();
int unknown = b.readSint16LE();
int userId = b.readUint16LE();
if ((code != 8) && (flags != 46)) {
_vm->_insane->procIACT(_dst, 0, 0, 0, b, 0, 0, code, flags, unknown, userId);
return;
}
if (_compressedFileMode) {
return;
}
assert(flags == 46 && unknown == 0);
/*int trkId =*/ b.readUint16LE();
int index = b.readUint16LE();
int nbframes = b.readUint16LE();
/*int32 size =*/ b.readUint32LE();
int32 bsize = subSize - 18;
if (_vm->_game.id == GID_CMI) {
// TODO: Move this code into another SmushChannel subclass?
byte *src = (byte *)malloc(bsize);
b.read(src, bsize);
byte *d_src = src;
byte value;
while (bsize > 0) {
if (_IACTpos >= 2) {
int32 len = READ_BE_UINT16(_IACToutput) + 2;
len -= _IACTpos;
if (len > bsize) {
memcpy(_IACToutput + _IACTpos, d_src, bsize);
_IACTpos += bsize;
bsize = 0;
} else {
byte *output_data = (byte *)malloc(4096);
memcpy(_IACToutput + _IACTpos, d_src, len);
byte *dst = output_data;
byte *d_src2 = _IACToutput;
d_src2 += 2;
int32 count = 1024;
byte variable1 = *d_src2++;
byte variable2 = variable1 / 16;
variable1 &= 0x0f;
do {
value = *(d_src2++);
if (value == 0x80) {
*dst++ = *d_src2++;
*dst++ = *d_src2++;
} else {
int16 val = (int8)value << variable2;
*dst++ = val >> 8;
*dst++ = (byte)(val);
}
value = *(d_src2++);
if (value == 0x80) {
*dst++ = *d_src2++;
*dst++ = *d_src2++;
} else {
int16 val = (int8)value << variable1;
*dst++ = val >> 8;
*dst++ = (byte)(val);
}
} while (--count);
if (!_IACTstream) {
_IACTstream = Audio::makeQueuingAudioStream(22050, true);
_vm->_mixer->playStream(Audio::Mixer::kSFXSoundType, _IACTchannel, _IACTstream);
}
_IACTstream->queueBuffer(output_data, 0x1000, DisposeAfterUse::YES, Audio::FLAG_STEREO | Audio::FLAG_16BITS);
bsize -= len;
d_src += len;
_IACTpos = 0;
}
} else {
if (bsize > 1 && _IACTpos == 0) {
*(_IACToutput + 0) = *d_src++;
_IACTpos = 1;
bsize--;
}
*(_IACToutput + _IACTpos) = *d_src++;
_IACTpos++;
bsize--;
}
}
free(src);
} else if ((_vm->_game.id == GID_DIG) && !(_vm->_game.features & GF_DEMO)) {
int bufId, volume, paused, curSoundId;
byte *dataBuffer = (byte *)malloc(bsize);
b.read(dataBuffer, bsize);
switch (userId) {
case TRK_USERID_SPEECH:
bufId = DIMUSE_BUFFER_SPEECH;
volume = 127;
break;
case TRK_USERID_MUSIC:
bufId = DIMUSE_BUFFER_MUSIC;
volume = 127;
break;
case TRK_USERID_SFX:
bufId = DIMUSE_BUFFER_SFX;
volume = 127;
break;
default:
if (userId >= 100 && userId <= 163) {
bufId = DIMUSE_BUFFER_SPEECH;
volume = 2 * userId - 200;
} else if (userId >= 200 && userId <= 263) {
bufId = DIMUSE_BUFFER_MUSIC;
volume = 2 * userId - 400;
} else if (userId >= 300 && userId <= 363) {
bufId = DIMUSE_BUFFER_SFX;
volume = 2 * userId - 600;
} else {
free(dataBuffer);
error("SmushPlayer::handleIACT(): ERROR: got invalid userID (%d)", userId);
}
break;
}
paused = nbframes - index == 1;
// Apparently this is expected to happen (e.g.: Brink's death video)
if (index && _iactTable[bufId] - index != -1) {
free(dataBuffer);
debugC(DEBUG_SMUSH, "SmushPlayer::handleIACT(): WARNING: got out of order block");
return;
}
_iactTable[bufId] = index;
if (index) {
if (_imuseDigital->diMUSEGetParam(bufId + DIMUSE_SMUSH_SOUNDID, DIMUSE_P_SND_TRACK_NUM)) {
_imuseDigital->diMUSEFeedStream(bufId + DIMUSE_SMUSH_SOUNDID, dataBuffer, subSize - 18, paused);
free(dataBuffer);
return;
}
free(dataBuffer);
error("SmushPlayer::handleIACT(): ERROR: got unexpected non-zero IACT block, bufID %d", bufId);
} else {
if (READ_BE_UINT32(dataBuffer) != MKTAG('i', 'M', 'U', 'S')) {
free(dataBuffer);
error("SmushPlayer::handleIACT(): ERROR: got non-IMUS IACT block");
}
curSoundId = 0;
do {
curSoundId = _imuseDigital->diMUSEGetNextSound(curSoundId);
if (!curSoundId)
break;
} while (_imuseDigital->diMUSEGetParam(curSoundId, DIMUSE_P_SND_HAS_STREAM) != 1 || _imuseDigital->diMUSEGetParam(curSoundId, DIMUSE_P_STREAM_BUFID) != bufId);
if (!curSoundId) {
// There isn't any previous sound running: start a new stream
if (_imuseDigital->diMUSEStartStream(bufId + DIMUSE_SMUSH_SOUNDID, 126, bufId)) {
free(dataBuffer);
error("SmushPlayer::handleIACT(): ERROR: couldn't start stream");
}
} else {
// There's an old sound running: switch the stream from the old one to the new one
_imuseDigital->diMUSESwitchStream(curSoundId, bufId + DIMUSE_SMUSH_SOUNDID, bufId == DIMUSE_BUFFER_MUSIC ? 1000 : 150, 0, 0);
}
_imuseDigital->diMUSESetParam(bufId + DIMUSE_SMUSH_SOUNDID, DIMUSE_P_VOLUME, volume);
if (bufId == DIMUSE_BUFFER_SPEECH) {
_imuseDigital->diMUSESetParam(bufId + DIMUSE_SMUSH_SOUNDID, DIMUSE_P_GROUP, DIMUSE_GROUP_SPEECH);
} else if (bufId == DIMUSE_BUFFER_MUSIC) {
_imuseDigital->diMUSESetParam(bufId + DIMUSE_SMUSH_SOUNDID, DIMUSE_P_GROUP, DIMUSE_GROUP_MUSIC);
} else {
_imuseDigital->diMUSESetParam(bufId + DIMUSE_SMUSH_SOUNDID, DIMUSE_P_GROUP, DIMUSE_GROUP_SFX);
}
_imuseDigital->diMUSEFeedStream(bufId + DIMUSE_SMUSH_SOUNDID, dataBuffer, subSize - 18, paused);
free(dataBuffer);
return;
}
}
}
void SmushPlayer::handleTextResource(uint32 subType, int32 subSize, Common::SeekableReadStream &b) {
int pos_x = b.readSint16LE();
int pos_y = b.readSint16LE();
int flags = b.readSint16LE();
int left = b.readSint16LE();
int top = b.readSint16LE();
int width = b.readSint16LE();
int height = b.readSint16LE();
/*int32 unk2 =*/ b.readUint16LE();
const char *str;
char *string = nullptr, *string2 = nullptr;
if (subType == MKTAG('T','E','X','T')) {
string = (char *)malloc(subSize - 16);
str = string;
b.read(string, subSize - 16);
} else {
int string_id = b.readUint16LE();
if (!_strings)
return;
str = _strings->get(string_id);
}
// if subtitles disabled and bit 3 is set, then do not draw
//
// Query ConfMan here. However it may be slower, but
// player may want to switch the subtitles on or off during the
// playback. This fixes bug #2812
if ((!ConfMan.getBool("subtitles")) && ((flags & 8) == 8))
return;
bool isCJKComi = (_vm->_game.id == GID_CMI && _vm->_useCJKMode);
int color = 15;
int fontId = isCJKComi ? 1 : 0;
while (*str == '/') {
str++; // For Full Throttle text resources
}
byte transBuf[512];
if (_vm->_game.id == GID_CMI) {
_vm->translateText((const byte *)str - 1, transBuf, sizeof(transBuf));
while (*str++ != '/')
;
string2 = (char *)transBuf;
// If string2 contains formatting information there probably
// wasn't any translation for it in the language.tab file. In
// that case, pretend there is no string2.
if (string2[0] == '^')
string2[0] = 0;
}
while (str[0] == '^') {
switch (str[1]) {
case 'f':
fontId = str[3] - '0';
str += 4;
break;
case 'c':
color = str[4] - '0' + 10 *(str[3] - '0');
str += 5;
break;
default:
error("invalid escape code in text string");
}
}
if (_vm->_game.id == GID_CMI && string2[0] != 0)
str = string2;
// This is a hack from the original COMI CJK interpreter. Its purpose is to avoid
// ugly combinations of two byte characters (rendered with the respective special
// font) and standard one byte (NUT font) characters (see bug #11947).
if (isCJKComi && !(fontId == 0 && color == 1)) {
fontId = 1;
color = 255;
}
SmushFont *sf = getFont(fontId);
assert(sf != nullptr);
// The hack that used to be here to prevent bug #2220 is no longer necessary and
// has been removed. The font renderer can handle all ^codes it encounters (font
// changes on the fly will be ignored for Smush texts, since our code design does
// not permit it and the feature isn't used anyway).
if (_vm->_language == Common::HE_ISR && !(flags & kStyleAlignCenter)) {
flags |= kStyleAlignRight;
pos_x = _width - 1 - pos_x;
}
TextStyleFlags flg = (TextStyleFlags)(flags & 7);
// flags:
// bit 0 - center 0x01
// bit 1 - not used (align right) 0x02
// bit 2 - word wrap 0x04
// bit 3 - switchable 0x08
// bit 4 - fill background 0x10
// bit 5 - outline/shadow 0x20 (apparently only set by the text renderer itself, not from the smush data)
// bit 6 - vertical fix (COMI) 0x40 (COMI handles this in the printing method, but I haven't seen a case where it is used)
// bit 7 - skip ^ codes (COMI) 0x80 (should be irrelevant for Smush, we strip these commands anyway)
// bit 8 - no vertical fix (COMI) 0x100 (COMI handles this in the printing method, but I haven't seen a case where it is used)
if (flg & kStyleWordWrap) {
// COMI has to do it all a bit different, of course. SCUMM7 games immediately render the text from here and actually use the clipping data
// provided by the text resource. COMI does not render directly, but enqueues a blast string (which is then drawn through the usual main
// loop routines). During that process the rect data will get dumped and replaced with the following default values. It's hard to tell
// whether this is on purpose or not (the text looks not necessarily better or worse, just different), so we follow the original...
if (_vm->_game.id == GID_CMI) {
left = top = 10;
width = _width - 20;
height = _height - 20;
}
Common::Rect clipRect(MAX<int>(0, left), MAX<int>(0, top), MIN<int>(left + width, _width), MIN<int>(top + height, _height));
sf->drawStringWrap(str, _dst, clipRect, pos_x, pos_y, color, flg);
} else {
// Similiar to the wrapped text, COMI will pass on rect coords here, which will later be lost. Unlike with the wrapped text, it will
// finally use the full screen dimenstions. SCUMM7 renders directly from here (see comment above), but also with the full screen.
Common::Rect clipRect(0, 0, _width, _height);
sf->drawString(str, _dst, clipRect, pos_x, pos_y, color, flg);
}
free(string);
}
const char *SmushPlayer::getString(int id) {
if (_strings != nullptr) {
return _strings->get(id);
} else {
warning("Couldn't load string with id {%d}, are you maybe missing a TRS subtitle file?", id);
return nullptr;
}
}
bool SmushPlayer::readString(const char *file) {
const char *i = strrchr(file, '.');
if (i == nullptr) {
error("invalid filename : %s", file);
}
char fname[260];
memcpy(fname, file, MIN<int>(sizeof(fname), i - file));
Common::strlcpy(fname + (i - file), ".trs", sizeof(fname) - (i - file));
if ((_strings = getStrings(_vm, fname, false)) != 0) {
return true;
}
if (_vm->_game.id == GID_DIG && (_strings = getStrings(_vm, "digtxt.trs", true)) != 0) {
return true;
}
return false;
}
void SmushPlayer::readPalette(byte *out, Common::SeekableReadStream &in) {
in.read(out, 0x300);
}
void SmushPlayer::handleDeltaPalette(int32 subSize, Common::SeekableReadStream &b) {
debugC(DEBUG_SMUSH, "SmushPlayer::handleDeltaPalette()");
b.readUint16LE();
uint16 xpalCommand = b.readUint16LE();
if (xpalCommand == 256) {
b.readUint16LE();
for (int i = 0; i < 768; ++i) {
_shiftedDeltaPal[i] += _deltaPal[i];
_pal[i] = CLIP<int32>(_shiftedDeltaPal[i] >> 7, 0, 255);
}
setDirtyColors(0, 255);
} else {
for (int j = 0; j < 768; ++j) {
_shiftedDeltaPal[j] = _pal[j] << 7;
_deltaPal[j] = b.readUint16LE();
}
if (xpalCommand == 512)
readPalette(_pal, b);
setDirtyColors(0, 255);
}
}
void SmushPlayer::handleNewPalette(int32 subSize, Common::SeekableReadStream &b) {
debugC(DEBUG_SMUSH, "SmushPlayer::handleNewPalette()");
assert(subSize >= 0x300);
if (_skipPalette)
return;
readPalette(_pal, b);
setDirtyColors(0, 255);
}
byte *SmushPlayer::getVideoPalette() {
return _pal;
}
void smushDecodeRLE(byte *dst, const byte *src, int left, int top, int width, int height, int pitch);
void smushDecodeUncompressed(byte *dst, const byte *src, int left, int top, int width, int height, int pitch);
void SmushPlayer::decodeFrameObject(int codec, const uint8 *src, int left, int top, int width, int height) {
if ((height == 242) && (width == 384)) {
if (_specialBuffer == 0)
_specialBuffer = (byte *)malloc(242 * 384);
_dst = _specialBuffer;
} else if ((height > _vm->_screenHeight) || (width > _vm->_screenWidth))
return;
// FT Insane uses smaller frames to draw overlays with moving objects
// Other .san files do have them as well but their purpose in unknown
// and often it causes memory overdraw. So just skip those frames
else if (!_insanity && ((height != _vm->_screenHeight) || (width != _vm->_screenWidth)))
return;
if ((height == 242) && (width == 384)) {
_width = width;
_height = height;
} else {
_width = _vm->_screenWidth;
_height = _vm->_screenHeight;
}
switch (codec) {
case SMUSH_CODEC_RLE:
case SMUSH_CODEC_RLE_ALT:
smushDecodeRLE(_dst, src, left, top, width, height, _vm->_screenWidth);
break;
case SMUSH_CODEC_DELTA_BLOCKS:
if (!_deltaBlocksCodec)
_deltaBlocksCodec = new SmushDeltaBlocksDecoder(width, height);
if (_deltaBlocksCodec)
_deltaBlocksCodec->decode(_dst, src);
break;
case SMUSH_CODEC_DELTA_GLYPHS:
if (!_deltaGlyphsCodec)
_deltaGlyphsCodec = new SmushDeltaGlyphsDecoder(width, height);
if (_deltaGlyphsCodec)
_deltaGlyphsCodec->decode(_dst, src);
break;
case SMUSH_CODEC_UNCOMPRESSED:
// Used by Full Throttle Classic (from Remastered)
smushDecodeUncompressed(_dst, src, left, top, width, height, _vm->_screenWidth);
break;
default:
error("Invalid codec for frame object : %d", codec);
}
if (_storeFrame) {
if (_frameBuffer == nullptr) {
_frameBuffer = (byte *)malloc(_width * _height);
}
memcpy(_frameBuffer, _dst, _width * _height);
_storeFrame = false;
}
}
void SmushPlayer::handleZlibFrameObject(int32 subSize, Common::SeekableReadStream &b) {
if (_skipNext) {
_skipNext = false;
return;
}
int32 chunkSize = subSize;
byte *chunkBuffer = (byte *)malloc(chunkSize);
assert(chunkBuffer);
b.read(chunkBuffer, chunkSize);
unsigned long decompressedSize = READ_BE_UINT32(chunkBuffer);
byte *fobjBuffer = (byte *)malloc(decompressedSize);
if (!Common::inflateZlib(fobjBuffer, &decompressedSize, chunkBuffer + 4, chunkSize - 4))
error("SmushPlayer::handleZlibFrameObject() Zlib uncompress error");
free(chunkBuffer);
byte *ptr = fobjBuffer;
int codec = READ_LE_UINT16(ptr); ptr += 2;
int left = READ_LE_UINT16(ptr); ptr += 2;
int top = READ_LE_UINT16(ptr); ptr += 2;
int width = READ_LE_UINT16(ptr); ptr += 2;
int height = READ_LE_UINT16(ptr); ptr += 2;
decodeFrameObject(codec, fobjBuffer + 14, left, top, width, height);
free(fobjBuffer);
}
void SmushPlayer::handleFrameObject(int32 subSize, Common::SeekableReadStream &b) {
assert(subSize >= 14);
if (_skipNext) {
_skipNext = false;
return;
}
int codec = b.readUint16LE();
int left = b.readUint16LE();
int top = b.readUint16LE();
int width = b.readUint16LE();
int height = b.readUint16LE();
b.readUint16LE();
b.readUint16LE();
int32 chunk_size = subSize - 14;
byte *chunk_buffer = (byte *)malloc(chunk_size);
assert(chunk_buffer);
b.read(chunk_buffer, chunk_size);
decodeFrameObject(codec, chunk_buffer, left, top, width, height);
free(chunk_buffer);
}
void SmushPlayer::handleFrame(int32 frameSize, Common::SeekableReadStream &b) {
debugC(DEBUG_SMUSH, "SmushPlayer::handleFrame(%d)", _frame);
uint8 *audioChunk = nullptr;
_skipNext = false;
if (_insanity) {
_vm->_insane->procPreRendering();
}
while (frameSize > 0) {
const uint32 subType = b.readUint32BE();
const int32 subSize = b.readUint32BE();
const int32 subOffset = b.pos();
switch (subType) {
case MKTAG('N','P','A','L'):
handleNewPalette(subSize, b);
break;
case MKTAG('F','O','B','J'):
handleFrameObject(subSize, b);
break;
case MKTAG('Z','F','O','B'):
handleZlibFrameObject(subSize, b);
break;
case MKTAG('P','S','A','D'):
if (!_compressedFileMode) {
audioChunk = (uint8 *)malloc(subSize + 8);
b.seek(-8, SEEK_CUR);
b.read(audioChunk, subSize + 8);
feedAudio(audioChunk, 0, 127, 0, 0);
free(audioChunk);
audioChunk = nullptr;
}
break;
case MKTAG('T','R','E','S'):
handleTextResource(subType, subSize, b);
break;
case MKTAG('X','P','A','L'):
handleDeltaPalette(subSize, b);
break;
case MKTAG('I','A','C','T'):
handleIACT(subSize, b);
break;
case MKTAG('S','T','O','R'):
handleStore(subSize, b);
break;
case MKTAG('F','T','C','H'):
handleFetch(subSize, b);
break;
case MKTAG('S','K','I','P'):
_vm->_insane->procSKIP(subSize, b);
break;
case MKTAG('T','E','X','T'):
handleTextResource(subType, subSize, b);
break;
default:
error("Unknown frame subChunk found : %s, %d", tag2str(subType), subSize);
}
frameSize -= subSize + 8;
b.seek(subOffset + subSize, SEEK_SET);
if (subSize & 1) {
b.skip(1);
frameSize--;
}
}
if (_insanity) {
_vm->_insane->procPostRendering(_dst, 0, 0, 0, _frame, _nbframes-1);
}
if (_width != 0 && _height != 0) {
updateScreen();
}
_frame++;
}
void SmushPlayer::handleAnimHeader(int32 subSize, Common::SeekableReadStream &b) {
debugC(DEBUG_SMUSH, "SmushPlayer::handleAnimHeader()");
assert(subSize >= 0x300 + 6);
byte *headerContent = (byte *)malloc(subSize * sizeof(byte));
if (headerContent) {
// Fill out the header
b.read(headerContent, subSize);
byte headerMajorVersion = headerContent[0];
byte headerMinorVersion = headerContent[1];
_nbframes = READ_LE_UINT16(&headerContent[2]);
// Video files might contain framerate overrides
if (headerMajorVersion > 1) {
uint16 speed = READ_LE_UINT16(&headerContent[6 + 0x300]);
if ((_curVideoFlags & 8) == 0 && speed != 0) {
debug(5, "SmushPlayer::handleAnimHeader(): header version %d.%d, video speed override %d fps (cur speed %d)",
headerMajorVersion,
headerMinorVersion,
speed,
_speed);
_speed = speed;
}
}
if (!_skipPalette) {
byte *palettePtr = &headerContent[6];
memcpy(_pal, palettePtr, sizeof(_pal));
setDirtyColors(0, 255);
}
free(headerContent);
}
}
void SmushPlayer::setupAnim(const char *file) {
if (_insanity) {
if (!((_vm->_game.features & GF_DEMO) && (_vm->_game.platform == Common::kPlatformDOS)))
readString("mineroad.trs");
} else
readString(file);
}
void SmushPlayer::setCurVideoFlags(int16 flags) {
_curVideoFlags = flags;
}
SmushFont *SmushPlayer::getFont(int font) {
char file_font[11];
if (_sf[font])
return _sf[font];
if (_vm->_game.id == GID_FT) {
if (!((_vm->_game.features & GF_DEMO) && (_vm->_game.platform == Common::kPlatformDOS))) {
const char *ft_fonts[] = {
"scummfnt.nut",
"techfnt.nut",
"titlfnt.nut",
"specfnt.nut"
};
assert(font >= 0 && font < ARRAYSIZE(ft_fonts));
_sf[font] = new SmushFont(_vm, ft_fonts[font], true);
}
} else {
int numFonts = (_vm->_game.id == GID_CMI && !(_vm->_game.features & GF_DEMO)) ? 5 : 4;
assert(font >= 0 && font < numFonts);
Common::sprintf_s(file_font, "font%d.nut", font);
_sf[font] = new SmushFont(_vm, file_font, _vm->_game.id == GID_DIG && font != 0);
}
assert(_sf[font]);
return _sf[font];
}
void SmushPlayer::parseNextFrame() {
if (_seekPos >= 0) {
if (_seekFile.size() > 0) {
delete _base;
ScummFile *tmp = new ScummFile(_vm);
if (!g_scumm->openFile(*tmp, Common::Path(_seekFile)))
error("SmushPlayer: Unable to open file %s", _seekFile.c_str());
_base = tmp;
_base->readUint32BE();
_baseSize = _base->readUint32BE();
if (_seekPos > 0) {
assert(_seekPos > 8);
// In this case we need to get palette and number of frames
const uint32 subType = _base->readUint32BE();
const int32 subSize = _base->readUint32BE();
const int32 subOffset = _base->pos();
assert(subType == MKTAG('A','H','D','R'));
handleAnimHeader(subSize, *_base);
_base->seek(subOffset + subSize, SEEK_SET);
_middleAudio = true;
_seekPos -= 8;
} else {
// We need this in Full Throttle when entering/leaving
// the old mine road.
tryCmpFile(_seekFile.c_str());
}
_skipPalette = false;
} else {
_skipPalette = true;
}
_base->seek(_seekPos + 8, SEEK_SET);
_frame = _seekFrame;
_startFrame = _frame;
_startTime = _vm->_system->getMillis();
_seekPos = -1;
}
assert(_base);
const uint32 subType = _base->readUint32BE();
const int32 subSize = _base->readUint32BE();
const int32 subOffset = _base->pos();
if (_base->pos() >= (int32)_baseSize) {
_vm->_smushVideoShouldFinish = true;
_endOfFile = true;
return;
}
debug(3, "Chunk: %s at %x", tag2str(subType), subOffset);
switch (subType) {
case MKTAG('A','H','D','R'): // FT INSANE may seek file to the beginning
handleAnimHeader(subSize, *_base);
break;
case MKTAG('F','R','M','E'):
handleFrame(subSize, *_base);
break;
default:
error("Unknown Chunk found at %x: %s, %d", subOffset, tag2str(subType), subSize);
}
_base->seek(subOffset + subSize, SEEK_SET);
if (_insanity)
_vm->_sound->processSound();
_vm->_imuseDigital->flushTracks();
}
void SmushPlayer::setPalette(const byte *palette) {
memcpy(_pal, palette, 0x300);
setDirtyColors(0, 255);
}
void SmushPlayer::setPaletteValue(int n, byte r, byte g, byte b) {
_pal[n * 3 + 0] = r;
_pal[n * 3 + 1] = g;
_pal[n * 3 + 2] = b;
setDirtyColors(n, n);
}
void SmushPlayer::setDirtyColors(int min, int max) {
if (_palDirtyMin > min)
_palDirtyMin = min;
if (_palDirtyMax < max)
_palDirtyMax = max;
}
void SmushPlayer::warpMouse(int x, int y, int buttons) {
_warpNeeded = true;
_warpX = x;
_warpY = y;
_warpButtons = buttons;
}
void SmushPlayer::updateScreen() {
uint32 end_time, start_time = _vm->_system->getMillis();
_updateNeeded = true;
end_time = _vm->_system->getMillis();
debugC(DEBUG_SMUSH, "Smush stats: updateScreen( %03d )", end_time - start_time);
}
void SmushPlayer::insanity(bool flag) {
_insanity = flag;
}
void SmushPlayer::seekSan(const char *file, int32 pos, int32 contFrame) {
_seekFile = file ? file : "";
_seekPos = pos;
_seekFrame = contFrame;
_pauseTime = 0;
}
void SmushPlayer::tryCmpFile(const char *filename) {
_vm->_mixer->stopHandle(*_compressedFileSoundHandle);
_compressedFileMode = false;
const char *i = strrchr(filename, '.');
if (i == NULL) {
error("invalid filename : %s", filename);
}
#if defined(USE_MAD) || defined(USE_VORBIS)
char fname[260];
#endif
Common::File *file = new Common::File();
// FIXME: How about using AudioStream::openStreamFile instead of the code below?
#ifdef USE_VORBIS
memcpy(fname, filename, MIN<int>(i - filename, sizeof(fname)));
Common::strlcpy(fname + (i - filename), ".ogg", sizeof(fname) - (i - filename));
if (file->open(fname)) {
_compressedFileMode = true;
_vm->_mixer->playStream(Audio::Mixer::kSFXSoundType, _compressedFileSoundHandle, Audio::makeVorbisStream(file, DisposeAfterUse::YES));
return;
}
#endif
#ifdef USE_MAD
memcpy(fname, filename, MIN<int>(i - filename, sizeof(fname)));
Common::strlcpy(fname + (i - filename), ".mp3", sizeof(fname) - (i - filename));
if (file->open(fname)) {
_compressedFileMode = true;
_vm->_mixer->playStream(Audio::Mixer::kSFXSoundType, _compressedFileSoundHandle, Audio::makeMP3Stream(file, DisposeAfterUse::YES));
return;
}
#endif
delete file;
}
void SmushPlayer::pause() {
if (!_paused) {
_paused = true;
_pauseStartTime = _vm->_system->getMillis();
}
}
void SmushPlayer::unpause() {
if (_paused) {
_paused = false;
_pauseTime += (_vm->_system->getMillis() - _pauseStartTime);
_pauseStartTime = 0;
}
}
void SmushPlayer::play(const char *filename, int32 speed, int32 offset, int32 startFrame) {
// Verify the specified file exists
ScummFile f(_vm);
_vm->openFile(f, filename);
if (!f.isOpen()) {
warning("SmushPlayer::play() File not found %s", filename);
return;
}
f.close();
_updateNeeded = false;
_warpNeeded = false;
_palDirtyMin = 256;
_palDirtyMax = -1;
// Hide mouse
bool oldMouseState = CursorMan.showMouse(false);
// Load the video
_seekFile = filename;
_seekPos = offset;
_seekFrame = startFrame;
_base = 0;
setupAnim(filename);
init(speed);
_startTime = _vm->_system->getMillis();
_startFrame = startFrame;
_frame = startFrame;
_pauseTime = 0;
// This piece of code is used to ensure there are
// no audio hiccups while loading the SMUSH video;
// Each version of the engine does it in its own way.
if (_imuseDigital->isFTSoundEngine()) {
_imuseDigital->fillStreamsWhileMusicCritical(20);
} else {
_imuseDigital->floodMusicBuffer();
}
int skipped = 0;
for (;;) {
uint32 now, elapsed;
bool skipFrame = false;
if (_insanity) {
// Seeking makes a mess of trying to sync the audio to
// the sound. Synt to time instead.
now = _vm->_system->getMillis() - _pauseTime;
elapsed = now - _startTime;
} else if (_vm->_mixer->isSoundHandleActive(*_compressedFileSoundHandle)) {
// Compressed SMUSH files.
elapsed = _vm->_mixer->getSoundElapsedTime(*_compressedFileSoundHandle);
} else if (_vm->_mixer->isSoundHandleActive(*_IACTchannel)) {
// Curse of Monkey Island SMUSH files.
elapsed = _vm->_mixer->getSoundElapsedTime(*_IACTchannel);
} else {
// For other SMUSH files, we don't necessarily have any
// one channel to sync against, so we have to use
// elapsed real time.
now = _vm->_system->getMillis() - _pauseTime;
elapsed = now - _startTime;
}
if (elapsed >= ((_frame - _startFrame) * 1000) / _speed) {
if (elapsed >= ((_frame + 1) * 1000) / _speed)
skipFrame = true;
else
skipFrame = false;
timerCallback();
}
_vm->scummLoop_handleSound();
if (_warpNeeded) {
_vm->_system->warpMouse(_vm->_macScreen ? _warpX * 2 : _warpX, _vm->_macScreen ? (_warpY * 2 + 2 * _vm->_macScreenDrawOffset) : _warpY);
_warpNeeded = false;
}
_vm->parseEvents();
_vm->processInput();
if (_palDirtyMax >= _palDirtyMin) {
// Apply gamma correction for Mac versions
if (_vm->_macScreen) {
byte palette[768];
memcpy(palette, _pal, 768);
for (int i = 0; i < ARRAYSIZE(palette); i++) {
palette[i] = _vm->_macGammaCorrectionLookUp[_pal[i]];
}
_vm->_system->getPaletteManager()->setPalette(palette + _palDirtyMin * 3, _palDirtyMin, _palDirtyMax - _palDirtyMin + 1);
} else {
_vm->_system->getPaletteManager()->setPalette(_pal + _palDirtyMin * 3, _palDirtyMin, _palDirtyMax - _palDirtyMin + 1);
}
_palDirtyMax = -1;
_palDirtyMin = 256;
skipFrame = false;
}
if (skipFrame) {
if (++skipped > 10) {
skipFrame = false;
skipped = 0;
}
} else
skipped = 0;
if (_updateNeeded) {
if (!skipFrame) {
// WORKAROUND for bug #2415: "FT DEMO: assertion triggered
// when playing movie". Some frames there are 384 x 224
int frameWidth = MIN(_width, _vm->_screenWidth);
int frameHeight = MIN(_height, _vm->_screenHeight);
if (_vm->_macScreen) {
_vm->mac_drawBufferToScreen(_dst, frameWidth, 0, 0, frameWidth, frameHeight);
} else {
_vm->_system->copyRectToScreen(_dst, _width, 0, 0, frameWidth, frameHeight);
}
_vm->_system->updateScreen();
_updateNeeded = false;
}
}
if (_endOfFile)
break;
if (_vm->shouldQuit() || _vm->_saveLoadFlag || _vm->_smushVideoShouldFinish) {
_vm->_mixer->stopHandle(*_compressedFileSoundHandle);
_vm->_mixer->stopHandle(*_IACTchannel);
_IACTpos = 0;
resetAudioTracks(); // For DIG demo
_imuseDigital->stopSMUSHAudio(); // For DIG & COMI
break;
}
_vm->_system->delayMillis(10);
}
release();
// Reset mouse state
CursorMan.showMouse(oldMouseState);
}
void SmushPlayer::initAudio(int samplerate, int32 maxChunkSize) {
int32 maxSizes[SMUSH_MAX_TRACKS] = {100000, 100000, 100000, 400000};
_imuseDigital->setSmushPlayer(this);
// DIG demo uses this audio system but doesn't use INSANE
if (_insane)
_insane->setSmushPlayer(this);
setGainReductionParams(114, 2048);
memset(_smushAudioTable, 0, sizeof(_smushAudioTable));
for (int i = 0; i < SMUSH_MAX_TRACKS; i++) {
_smushTrackVols[i] = 127;
_smushTrackFlags[i] = 1;
addAudioTrack(maxSizes[i], maxChunkSize);
}
_smushAudioSampleRate = samplerate;
_smushAudioInitialized = true;
_smushAudioCallbackEnabled = true;
resetAudioTracks();
}
void SmushPlayer::terminateAudio() {
if (_smushAudioInitialized) {
_smushAudioInitialized = false;
_smushAudioCallbackEnabled = false;
}
for (int i = 0; i < _smushNumTracks; i++) {
free(_smushTracks[i].blockPtr);
free(_smushTracks[i].fadeBuf);
}
_smushNumTracks = 0;
}
int SmushPlayer::isChanActive(int flagId) {
return _smushTrackFlags[flagId];
}
int SmushPlayer::setChanFlag(int id, int flagVal) {
if (id >= 0 && id <= _smushNumTracks)
_smushTrackFlags[id] = flagVal;
return id;
}
int SmushPlayer::addAudioTrack(int32 trackBlockSize, int32 maxBlockSize) {
int id = _smushNumTracks;
_smushTracks[id].state = TRK_STATE_INACTIVE;
_smushTracks[id].audioRemaining = 0;
_smushTracks[id].flags = 0;
_smushTracks[id].groupId = GRP_MASTER;
_smushTracks[id].blockSize = trackBlockSize;
_smushTracks[id].parsedChunks = 0;
_smushTracks[id].fadeBuf = (uint8 *)malloc(SMUSH_FADE_SIZE);
if (!_smushTracks[id].fadeBuf)
return -1;
_smushTracks[id].blockPtr = (uint8 *)malloc(_smushTracks[id].blockSize);
if (!_smushTracks[id].blockPtr)
return -1;
memset(_smushTracks[id].blockPtr, 127, _smushTracks[id].blockSize);
// Track the effective number of tracks, so that if only one fails,
// the others can carry on as they should
_smushNumTracks++;
return 0;
}
void SmushPlayer::resetAudioTracks() {
for (int i = 0; i < _smushNumTracks; i++) {
_smushTracks[i].state = TRK_STATE_INACTIVE;
_smushTracks[i].groupId = GRP_MASTER;
}
}
void SmushPlayer::setGainReductionParams(int16 gainReductionLowerBound, int16 gainReductionMultiplier) {
if (gainReductionLowerBound)
_gainReductionLowerBound = gainReductionLowerBound;
if (gainReductionMultiplier)
_gainReductionFactor = gainReductionMultiplier;
}
void SmushPlayer::setGroupVolume(int groupId, int volValue) {
switch (groupId) {
case GRP_MASTER:
_smushTrackVols[0] = volValue; // Master
break;
case GRP_SFX:
_smushTrackVols[1] = volValue; // Sfx
break;
case GRP_BKGMUS:
_smushTrackVols[3] = volValue; // Music
break;
case GRP_SPEECH:
_smushTrackVols[2] = volValue; // Voice
break;
default: // Internal groups
for (int i = 0; i < SMUSH_MAX_TRACKS; i++) {
if (_smushTracks[i].groupId == groupId)
_smushTracks[i].volume = volValue;
}
}
}
void SmushPlayer::handleSAUDChunk(uint8 *srcBuf, uint32 size, int groupId, int vol, int pan, int16 flags, int trkId, int index, int maxFrames) {
int targetTrk, diff1, diff2;
uint32 dataSize, mod;
int32 maxBlockSize, saudSize;
uint16 flagsAccumulator[SMUSH_MAX_TRACKS];
if (!index) {
saudSize = READ_BE_UINT32(&srcBuf[4]);
// Find the maximum block sizes between the current ones
maxBlockSize = 0;
for (int i = 0; i < _smushNumTracks; i++) {
if (_smushTracks[i].blockSize > maxBlockSize) {
maxBlockSize = _smushTracks[i].blockSize;
}
}
diff2 = 10000000;
if ((flags & TRK_TYPE_MASK) != IS_BKG_MUSIC) {
for (int i = 0; i < _smushNumTracks; i++) {
if (_smushTracks[i].blockSize - saudSize >= 0) {
diff1 = _smushTracks[i].blockSize - saudSize;
} else {
diff1 = saudSize - _smushTracks[i].blockSize;
}
if (diff1 < diff2 && saudSize < 3 * _smushTracks[i].blockSize / 2) {
if (_smushTracks[i].blockSize - saudSize >= 0) {
diff2 = _smushTracks[i].blockSize - saudSize;
} else {
diff2 = saudSize - _smushTracks[i].blockSize;
}
maxBlockSize = _smushTracks[i].blockSize;
}
}
}
for (int i = 0; i < _smushNumTracks; i++) {
flagsAccumulator[i] = 0;
if (_smushTracks[i].blockSize == maxBlockSize) {
if (_smushTracks[i].state == TRK_STATE_INACTIVE ||
_smushTracks[i].state == TRK_STATE_ENDING ||
_smushTracks[i].flags <= flags) {
if (_smushTracks[i].state == TRK_STATE_INACTIVE)
flagsAccumulator[i] = 0x1000;
if (_smushTracks[i].state == TRK_STATE_ENDING)
flagsAccumulator[i] += 0x200;
if (_smushTracks[i].flags == flags)
flagsAccumulator[i] += 0x400;
if (_smushTracks[i].flags < flags)
flagsAccumulator[i] += 0x800;
flagsAccumulator[i] += _smushTracks[i].parsedChunks + 1;
}
}
}
fillAudioTrackInfo(srcBuf, flagsAccumulator, size, groupId, vol, pan, flags, trkId, index, maxFrames);
return;
}
targetTrk = -1;
for (int i = 0; i < _smushNumTracks; i++) {
if (_smushTracks[i].state != TRK_STATE_INACTIVE &&
trkId == _smushTrackIds[i] &&
index == (_smushTrackIdxs[i] + 1) &&
maxFrames == _smushMaxFrames[i]) {
targetTrk = i;
break;
}
}
if (targetTrk != -1) {
_smushTrackIdxs[targetTrk]++;
dataSize = _smushTracks[targetTrk].dataSize;
mod = _smushTracks[targetTrk].availableSize % dataSize;
if (mod + size <= dataSize) {
memcpy(&_smushTracks[targetTrk].subChunkPtr[mod], srcBuf, size);
} else {
memcpy(&_smushTracks[targetTrk].subChunkPtr[mod], srcBuf, dataSize - mod);
memcpy(
_smushTracks[targetTrk].subChunkPtr,
&srcBuf[_smushTracks[targetTrk].dataSize - mod],
size + mod - _smushTracks[targetTrk].dataSize);
}
if (vol >= 0 && vol < 128)
_smushTracks[targetTrk].volume = vol;
if (pan > -128 && pan < 128)
_smushTracks[targetTrk].pan = pan;
_smushTracks[targetTrk].availableSize += size;
}
}
void SmushPlayer::fillAudioTrackInfo(uint8 *srcBuf, uint16 *flagsAccumulator, uint32 size, int groupId, int vol, int pan, int16 flags, int trkId, int index, int maxFrames) {
uint32 sdatSize, subChunkOffset, chunkSize;
int maxFlagAcc = -1;
int maxAccId = -1;
for (int i = 0; i < _smushNumTracks; i++) {
if (flagsAccumulator[i] && flagsAccumulator[i] > maxFlagAcc) {
maxFlagAcc = flagsAccumulator[i];
maxAccId = i;
}
}
if (maxAccId != -1) {
for (int i = 0; i < _smushNumTracks; i++) {
if (_smushTracks[i].parsedChunks < 255) {
_smushTracks[i].parsedChunks++;
}
}
_smushTracks[maxAccId].parsedChunks = 0;
_smushTracks[maxAccId].state = TRK_STATE_INACTIVE;
_smushTrackIds[maxAccId] = trkId;
_smushTrackIdxs[maxAccId] = 0;
_smushMaxFrames[maxAccId] = maxFrames;
subChunkOffset = READ_BE_UINT32(&srcBuf[12]);
sdatSize = READ_BE_UINT32(&srcBuf[subChunkOffset + 20]);
chunkSize = _smushTracks[maxAccId].blockSize;
if (size < chunkSize)
chunkSize = size;
memset(_smushTracks[maxAccId].blockPtr, 127, _smushTracks[maxAccId].blockSize);
memcpy(_smushTracks[maxAccId].blockPtr, srcBuf, chunkSize);
_smushTracks[maxAccId].dataBuf = _smushTracks[maxAccId].blockPtr + 16;
_smushTracks[maxAccId].dataSize = _smushTracks[maxAccId].blockSize - subChunkOffset - 24;
_smushTracks[maxAccId].subChunkPtr = &_smushTracks[maxAccId].dataBuf[subChunkOffset + 8];
_smushTracks[maxAccId].availableSize = size - subChunkOffset - 24;
_smushTracks[maxAccId].sdatSize = sdatSize;
_smushTracks[maxAccId].groupId = groupId;
_smushTracks[maxAccId].volume = 127;
_smushTracks[maxAccId].pan = 0;
if (vol >= 0 && vol < 128)
_smushTracks[maxAccId].volume = vol;
if (pan > -128 && pan < 128)
_smushTracks[maxAccId].pan = pan;
_smushTracks[maxAccId].flags = flags;
_smushTracks[maxAccId].audioRemaining = 0;
_smushTracks[maxAccId].state = TRK_STATE_FADING;
}
return;
}
void SmushPlayer::processDispatches(int16 feedSize) {
int32 fadeStartOffset, cpySize, fadeRemaining, fadeFeedSize, mixFeedSize, mixInFrameCount;
int32 offset, tmpFeedSize, maxFadeChunkSize;
int16 fadeInFrameCount, flags, fadeMixStartingPoint;
int fadePan, fadeVolume, mixPan, mixVolume, baseVolume, mixStartingPoint;
bool isPlayableTrack;
bool speechIsPlaying = false;
int engineBaseFeedSize = _imuseDigital->getFeedSize();
if (!_paused) {
if (_smushTracksNeedInit) {
_smushTracksNeedInit = false;
for (int i = 0; i < SMUSH_MAX_TRACKS; i++) {
_smushDispatch[i].fadeRemaining = 0;
_smushDispatch[i].fadeVolume = 0;
_smushDispatch[i].fadeSampleRate = 0;
_smushDispatch[i].elapsedAudio = 0;
_smushDispatch[i].audioLength = 0;
}
}
for (int i = 0; i < _smushNumTracks; i++) {
isPlayableTrack = ((_smushTracks[i].flags & TRK_TYPE_MASK) == IS_SPEECH && isChanActive(CHN_SPEECH)) ||
((_smushTracks[i].flags & TRK_TYPE_MASK) == IS_BKG_MUSIC && isChanActive(CHN_BKGMUS)) ||
((_smushTracks[i].flags & TRK_TYPE_MASK) == IS_SFX && isChanActive(CHN_OTHER));
flags = _smushTracks[i].flags;
switch (flags & TRK_TYPE_MASK) {
case IS_SFX:
baseVolume = (_smushTrackVols[1] * _smushTracks[i].volume) >> 7;
break;
case IS_BKG_MUSIC:
baseVolume = (_smushTrackVols[3] * _smushTracks[i].volume) >> 7;
break;
case IS_SPEECH:
baseVolume = (_smushTrackVols[2] * _smushTracks[i].volume) >> 7;
break;
default:
error("SmushPlayer::processDispatches(): unrecognized flag %d", _smushTracks[i].flags & TRK_TYPE_MASK);
}
mixVolume = baseVolume * _smushTrackVols[0] / 127;
if ((flags & TRK_TYPE_MASK) == IS_BKG_MUSIC && isChanActive(CHN_SPEECH))
mixVolume = ((baseVolume * _smushTrackVols[0] / 127) * _gainReductionMultiplier) >> 8;
// Check if there's the need to allocate a crossfade
if (_smushTracks[i].state == TRK_STATE_FADING && _smushDispatch[i].state == TRK_STATE_PLAYING) {
fadeStartOffset = _smushDispatch[i].audioRemaining % _smushDispatch[i].dataSize;
_smushDispatch[i].fadeRemaining = SMUSH_FADE_SIZE;
_smushDispatch[i].fadeVolume = _smushTracks[i].volume;
_smushDispatch[i].fadeSampleRate = _smushDispatch[i].sampleRate;
memset(_smushTracks[i].fadeBuf, 127, _smushDispatch[i].fadeRemaining);
cpySize = _smushDispatch[i].dataSize - fadeStartOffset;
if (cpySize > _smushDispatch[i].fadeRemaining)
cpySize = _smushDispatch[i].fadeRemaining;
memcpy(_smushTracks[i].fadeBuf, &_smushDispatch[i].dataBuf[fadeStartOffset], cpySize);
_smushDispatch[i].volumeStep = 0;
} else if (_smushTracks[i].state == TRK_STATE_PLAYING) {
if (_smushDispatch[i].audioRemaining < _smushTracks[i].availableSize - _smushTracks[i].dataSize + 15000) {
if (_smushTracks[i].availableSize < _smushTracks[i].sdatSize) {
_smushDispatch[i].volumeStep = 0;
mixInFrameCount = _smushTracks[i].availableSize - _smushDispatch[i].audioRemaining - 15000;
if (mixInFrameCount > _smushDispatch[i].currentOffset)
mixInFrameCount = _smushDispatch[i].currentOffset;
if (_smushDispatch[i].audioRemaining + mixInFrameCount > _smushTracks[i].sdatSize - _smushDispatch[i].dataSize)
mixInFrameCount = _smushTracks[i].sdatSize - _smushDispatch[i].dataSize - _smushDispatch[i].audioRemaining;
if (mixInFrameCount > 0) {
_smushDispatch[i].fadeRemaining = SMUSH_FADE_SIZE;
_smushDispatch[i].fadeSampleRate = _smushDispatch[i].sampleRate;
memcpy(
_smushTracks[i].fadeBuf,
&_smushDispatch[i].dataBuf[_smushDispatch[i].audioRemaining % _smushDispatch[i].dataSize],
_smushDispatch[i].fadeRemaining);
_smushDispatch[i].audioRemaining += mixInFrameCount;
_smushDispatch[i].currentOffset -= mixInFrameCount;
}
}
}
}
// If the fade has been allocated, flush it in the mixer
if (_smushDispatch[i].fadeRemaining) {
maxFadeChunkSize = _smushDispatch[i].fadeSampleRate * feedSize / _smushAudioSampleRate;
if (_smushDispatch[i].fadeRemaining > maxFadeChunkSize) {
fadeRemaining = maxFadeChunkSize;
} else {
fadeRemaining = _smushDispatch[i].fadeRemaining;
}
fadeMixStartingPoint = 0;
while (fadeRemaining) {
fadeInFrameCount = (fadeRemaining < engineBaseFeedSize / 4) ? fadeRemaining : engineBaseFeedSize / 4;
if (fadeInFrameCount == maxFadeChunkSize) {
fadeFeedSize = feedSize;
} else {
fadeFeedSize = _smushAudioSampleRate * fadeInFrameCount / _smushDispatch[i].fadeSampleRate;
}
if (isPlayableTrack) {
fadeVolume = _smushDispatch[i].fadeRemaining * _smushDispatch[i].fadeVolume * _smushTrackVols[0] / (SMUSH_FADE_SIZE * 127);
fadePan = _smushTracks[i].pan;
debug(5, "SmushPlayer::processDispatches(): fading dispatch %d, volume %d", i, fadeVolume);
sendAudioToDiMUSE(
&_smushTracks[i].fadeBuf[SMUSH_FADE_SIZE - _smushDispatch[i].fadeRemaining],
fadeMixStartingPoint,
fadeFeedSize,
fadeInFrameCount,
fadeVolume,
fadePan);
}
fadeMixStartingPoint += fadeFeedSize;
fadeRemaining -= fadeInFrameCount;
_smushDispatch[i].fadeRemaining -= fadeInFrameCount;
}
}
if (_smushTracks[i].state == TRK_STATE_FADING) {
if (_smushDispatch[i].state != TRK_STATE_PLAYING)
_smushDispatch[i].volumeStep = 16;
_smushDispatch[i].headerPtr = _smushTracks[i].dataBuf;
_smushDispatch[i].dataBuf = _smushTracks[i].subChunkPtr;
_smushDispatch[i].dataSize = _smushTracks[i].dataSize;
_smushDispatch[i].currentOffset = 0;
_smushDispatch[i].audioLength = 0;
_smushTracks[i].state = TRK_STATE_PLAYING;
}
if (_smushTracks[i].state != TRK_STATE_INACTIVE) {
tmpFeedSize = feedSize;
mixStartingPoint = 0;
if (feedSize > 0) {
while (1) {
mixInFrameCount = _smushDispatch[i].currentOffset;
if (mixInFrameCount > 0) {
offset = _smushDispatch[i].audioRemaining % _smushDispatch[i].dataSize;
if (mixInFrameCount > _smushDispatch[i].sampleRate * tmpFeedSize / _smushAudioSampleRate)
mixInFrameCount = _smushDispatch[i].sampleRate * tmpFeedSize / _smushAudioSampleRate;
if (offset + mixInFrameCount > _smushDispatch[i].dataSize)
mixInFrameCount = _smushDispatch[i].dataSize - offset;
if (mixInFrameCount + _smushDispatch[i].audioRemaining <= _smushTracks[i].availableSize) {
// Fade-in until full volume is reached
if (_smushDispatch[i].volumeStep < 16) {
_smushDispatch[i].volumeStep++;
debug(5, "SmushPlayer::processDispatches(): fading track %d, volume step %d", i, _smushDispatch[i].volumeStep);
}
if (mixInFrameCount > engineBaseFeedSize / 4)
mixInFrameCount = engineBaseFeedSize / 4;
_smushTracks[i].state = TRK_STATE_PLAYING;
// This flag is toggled one time per chunk: if it's yields "true" for
// even one track, it stays like that for the whole chunk
speechIsPlaying = !speechIsPlaying ? (_smushTracks[i].flags & TRK_TYPE_MASK) == IS_SPEECH : true;
} else {
// Fade-out until silent
if (_smushDispatch[i].volumeStep) {
_smushDispatch[i].volumeStep--;
debug(5, "SmushPlayer::processDispatches(): fading track %d, volume step %d", i, _smushDispatch[i].volumeStep);
}
_smushTracks[i].state = TRK_STATE_ENDING;
if (mixInFrameCount > engineBaseFeedSize / 4)
mixInFrameCount = engineBaseFeedSize / 4;
_smushDispatch[i].audioRemaining -= mixInFrameCount;
_smushDispatch[i].currentOffset += mixInFrameCount;
offset = _smushDispatch[i].audioRemaining % _smushDispatch[i].dataSize;
}
if (mixInFrameCount == _smushDispatch[i].sampleRate * tmpFeedSize / _smushAudioSampleRate) {
mixFeedSize = tmpFeedSize;
} else {
mixFeedSize = mixInFrameCount * _smushAudioSampleRate / _smushDispatch[i].sampleRate;
}
if (isPlayableTrack) {
mixPan = _smushTracks[i].pan;
sendAudioToDiMUSE(
&_smushDispatch[i].dataBuf[offset],
mixStartingPoint,
mixFeedSize,
mixInFrameCount,
(mixVolume * _smushDispatch[i].volumeStep) >> 4,
mixPan);
}
_smushDispatch[i].currentOffset -= mixInFrameCount;
_smushDispatch[i].audioRemaining += mixInFrameCount;
tmpFeedSize -= mixFeedSize;
mixStartingPoint += mixFeedSize;
}
if (_smushDispatch[i].currentOffset <= 0) {
if (processAudioCodes(i, tmpFeedSize, mixVolume) && tmpFeedSize <= 0) {
break;
}
} else if (tmpFeedSize <= 0) {
break;
}
}
}
}
_smushTracks[i].audioRemaining = _smushDispatch[i].audioRemaining;
_smushDispatch[i].state = _smushTracks[i].state;
}
if (speechIsPlaying) {
if (_gainReductionMultiplier > _gainReductionLowerBound) {
_gainReductionMultiplier -= (feedSize * 2 * _gainReductionFactor) >> 13;
if (_gainReductionMultiplier < _gainReductionLowerBound)
_gainReductionMultiplier = _gainReductionLowerBound;
}
} else {
if (_gainReductionMultiplier < 256) {
_gainReductionMultiplier += (feedSize * 2 * _gainReductionFactor) >> 15;
if (_gainReductionMultiplier > 256)
_gainReductionMultiplier = 256;
}
}
}
}
bool SmushPlayer::processAudioCodes(int idx, int32 &tmpFeedSize, int &mixVolume) {
uint8 *code, *buf, subcode, value;
int chunk;
while (tmpFeedSize) {
code = _smushDispatch[idx].headerPtr;
switch (code[0]) {
case SAUD_OP_INIT:
_smushDispatch[idx].audioLength = 0;
buf = _smushDispatch[idx].headerPtr;
_smushDispatch[idx].audioRemaining = READ_BE_UINT32(buf + 2);
_smushDispatch[idx].currentOffset = READ_BE_UINT32(buf + 6);
_smushDispatch[idx].sampleRate = _smushAudioSampleRate;
_smushDispatch[idx].headerPtr += _smushDispatch[idx].headerPtr[1] + 2;
if (_smushDispatch[idx].audioRemaining < _smushTracks[idx].availableSize + (_smushTracks[idx].availableSize >= _smushTracks[idx].sdatSize ? 0 : 15000) - _smushTracks[idx].dataSize) {
chunk = _smushTracks[idx].availableSize - _smushTracks[idx].dataSize - _smushDispatch[idx].audioRemaining + 15000;
if (chunk > _smushDispatch[idx].currentOffset) {
_smushTracks[idx].state = TRK_STATE_INACTIVE;
_smushTracks[idx].groupId = GRP_MASTER;
tmpFeedSize = 0;
break;
}
_smushDispatch[idx].audioRemaining += chunk;
_smushDispatch[idx].currentOffset -= chunk;
}
break;
case SAUD_OP_UPDATE_HEADER:
case SAUD_OP_COMPARE_GT:
case SAUD_OP_COMPARE_LT:
case SAUD_OP_COMPARE_EQ:
case SAUD_OP_COMPARE_NE:
subcode = code[4];
switch (subcode) {
case SAUD_VALUEID_ALL_VOLS:
value = _smushTrackVols[0];
break;
case SAUD_VALUEID_TRK_VOL:
value = _smushTracks[idx].volume;
break;
case SAUD_VALUEID_TRK_PAN:
value = _smushTracks[idx].pan;
break;
default:
value = _smushAudioTable[subcode];
break;
}
switch (code[0]) {
case SAUD_OP_UPDATE_HEADER:
if (value || (subcode == 0)) {
_smushDispatch[idx].headerPtr = &code[READ_BE_UINT16(&code[2])];
}
break;
case SAUD_OP_COMPARE_GT:
value = value > code[5];
break;
case SAUD_OP_COMPARE_LT:
value = value < code[5];
break;
case SAUD_OP_COMPARE_EQ:
value = value == code[5];
break;
case SAUD_OP_COMPARE_NE:
value = value != code[5];
break;
default:
break;
}
if (!value) {
_smushDispatch[idx].headerPtr = &code[code[1] + 2];
} else {
_smushDispatch[idx].headerPtr = &code[READ_BE_UINT16(&code[2])];
}
break;
case SAUD_OP_SET_PARAM:
switch (code[2]) {
case SAUD_VALUEID_ALL_VOLS:
_smushTrackVols[0] = code[3];
break;
case SAUD_VALUEID_TRK_VOL:
_smushTracks[idx].volume = code[3];
mixVolume = (_smushTrackVols[0] * _smushTracks[idx].volume) / 127;
// Set a lower mix volume of the background music if speech is active
if ((_smushTracks[idx].flags & TRK_TYPE_MASK) == IS_BKG_MUSIC && isChanActive(CHN_SPEECH))
mixVolume = (mixVolume * _gainReductionMultiplier) >> 8;
break;
case SAUD_VALUEID_TRK_PAN:
_smushTracks[idx].pan = code[3];
break;
default:
_smushAudioTable[code[2]] = code[3];
break;
}
_smushDispatch[idx].headerPtr = &code[code[1] + 2];
break;
case SAUD_OP_INCR_PARAM:
switch (code[2]) {
case SAUD_VALUEID_ALL_VOLS:
_smushTrackVols[0] += code[3];
break;
case SAUD_VALUEID_TRK_VOL:
_smushTracks[idx].volume += code[3];
break;
case SAUD_VALUEID_TRK_PAN:
_smushTracks[idx].pan += code[3];
break;
default:
_smushAudioTable[code[2]] += code[3];
break;
}
_smushDispatch[idx].headerPtr = &code[code[1] + 2];
break;
case SAUD_OP_SET_OFFSET:
_smushDispatch[idx].audioLength = 0;
buf = _smushDispatch[idx].headerPtr;
_smushDispatch[idx].audioRemaining = READ_BE_UINT32(buf + 2);
_smushDispatch[idx].currentOffset = READ_BE_UINT32(buf + 6);
_smushDispatch[idx].sampleRate = _smushAudioSampleRate;
_smushDispatch[idx].headerPtr += _smushDispatch[idx].headerPtr[1] + 2;
if (_smushDispatch[idx].audioRemaining < _smushTracks[idx].availableSize + (_smushTracks[idx].availableSize >= _smushTracks[idx].sdatSize ? 0 : 15000) - _smushTracks[idx].dataSize) {
chunk = _smushTracks[idx].availableSize - _smushTracks[idx].dataSize - _smushDispatch[idx].audioRemaining + 15000;
if (chunk > _smushDispatch[idx].currentOffset) {
_smushTracks[idx].state = TRK_STATE_INACTIVE;
_smushTracks[idx].groupId = GRP_MASTER;
tmpFeedSize = 0;
break;
}
_smushDispatch[idx].audioRemaining += chunk;
_smushDispatch[idx].currentOffset -= chunk;
}
break;
case SAUD_OP_SET_LENGTH:
if (!_smushDispatch[idx].audioLength) {
_smushDispatch[idx].audioLength = READ_BE_UINT32(&code[6]);
_smushDispatch[idx].elapsedAudio = 0;
}
buf = _smushDispatch[idx].headerPtr;
_smushDispatch[idx].audioRemaining = _smushDispatch[idx].elapsedAudio + READ_BE_UINT32(buf + 2);
_smushDispatch[idx].currentOffset = READ_BE_UINT32(buf + 14);
if (_smushDispatch[idx].currentOffset > _smushDispatch[idx].audioLength)
_smushDispatch[idx].currentOffset = _smushDispatch[idx].audioLength;
_smushDispatch[idx].sampleRate = _smushAudioSampleRate;
_smushDispatch[idx].audioLength -= _smushDispatch[idx].currentOffset;
_smushDispatch[idx].elapsedAudio += _smushDispatch[idx].currentOffset;
if (_smushDispatch[idx].audioLength) {
_smushDispatch[idx].headerPtr = &code[code[1] + 2];
} else {
_smushDispatch[idx].headerPtr = &code[READ_BE_UINT16(&code[18])];
}
if (_smushDispatch[idx].audioRemaining >= _smushTracks[idx].availableSize + (_smushTracks[idx].availableSize >= _smushTracks[idx].sdatSize ? 0 : 15000) - _smushTracks[idx].dataSize) {
chunk = _smushTracks[idx].availableSize - _smushTracks[idx].dataSize - _smushDispatch[idx].audioRemaining + 15000;
if (chunk > _smushDispatch[idx].currentOffset) {
_smushTracks[idx].state = TRK_STATE_INACTIVE;
_smushTracks[idx].groupId = GRP_MASTER;
tmpFeedSize = 0;
} else {
_smushDispatch[idx].audioRemaining += chunk;
_smushDispatch[idx].currentOffset -= chunk;
}
}
break;
default:
_smushTracks[idx].state = TRK_STATE_INACTIVE;
_smushTracks[idx].groupId = GRP_MASTER;
tmpFeedSize = 0;
}
if (_smushDispatch[idx].currentOffset > 0) {
return false;
}
}
return true;
}
void SmushPlayer::sendAudioToDiMUSE(uint8 *mixBuf, int32 mixStartingPoint, int32 mixFeedSize, int32 mixInFrameCount, int volume, int pan) {
int clampedVol, clampedPan;
bool is11025Hz = false;
if (mixFeedSize == 2 * mixInFrameCount) {
is11025Hz = true;
} else if (mixFeedSize != mixInFrameCount) {
return;
}
clampedPan = CLIP<int>((pan / 2) + 64, 0, 127);
clampedVol = CLIP<int>(volume, 0, 127);
_imuseDigital->receiveAudioFromSMUSH(mixBuf, mixInFrameCount, mixFeedSize, mixStartingPoint, clampedVol, clampedPan, is11025Hz);
}
void SmushPlayer::feedAudio(uint8 *srcBuf, int groupId, int volume, int pan, int16 flags) {
int panDelta, effPan;
int32 maxFrames;
uint16 trkId, index;
if (_smushAudioInitialized) {
// Check file encoding
if (srcBuf[8] == 0 && srcBuf[9] == 0 && srcBuf[12] == 0 && srcBuf[13] == 0 && srcBuf[16] == 0 && srcBuf[17] == 0) {
trkId = READ_BE_INT16(&srcBuf[10]);
index = READ_BE_INT16(&srcBuf[14]);
maxFrames = READ_BE_INT16(&srcBuf[18]);
handleSAUDChunk(
srcBuf + 20,
READ_BE_UINT32(&srcBuf[4]) - 12,
groupId,
volume,
pan,
flags,
trkId,
index,
maxFrames);
} else {
trkId = READ_LE_INT16(&srcBuf[8]);
index = READ_LE_INT16(&srcBuf[10]);
maxFrames = READ_LE_INT16(&srcBuf[12]);
flags |= READ_LE_INT16(&srcBuf[14]);
volume = (volume * srcBuf[16]) >> 7;
panDelta = srcBuf[17];
if (panDelta == 128) {
effPan = 128;
} else {
effPan = pan + panDelta;
}
handleSAUDChunk(
srcBuf + 18,
READ_BE_UINT32(&srcBuf[4]) - 10,
groupId,
(volume * srcBuf[16]) >> 7,
effPan,
flags,
trkId,
index,
maxFrames);
}
}
}
bool SmushPlayer::isAudioCallbackEnabled() {
return _smushAudioCallbackEnabled;
}
} // End of namespace Scumm
|